aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os.zig
diff options
context:
space:
mode:
authorTau <tauoverpi@yandex.com>2021-01-25 07:23:03 +0100
committerVeikka Tuominen <git@vexu.eu>2021-02-21 12:04:40 +0200
commit840331ee48e54b1ce4a3c8196f90eec73ce6deea (patch)
tree674cd1223b59a58f7db0f944a65980cc97e3c56a /lib/std/os.zig
parentc70832bc411388c3e9749013d7cf99704ff95a36 (diff)
downloadzig-840331ee48e54b1ce4a3c8196f90eec73ce6deea.tar.gz
zig-840331ee48e54b1ce4a3c8196f90eec73ce6deea.zip
Rebase link(at) properly
Diffstat (limited to 'lib/std/os.zig')
-rw-r--r--lib/std/os.zig86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 02eec2de71..c2431ff12c 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -1634,6 +1634,92 @@ pub fn symlinkatZ(target_path: [*:0]const u8, newdirfd: fd_t, sym_link_path: [*:
}
}
+pub const LinkError = UnexpectedError || error{
+ AccessDenied,
+ DiskQuota,
+ PathAlreadyExists,
+ FileSystem,
+ SymLinkLoop,
+ LinkQuotaExceeded,
+ NameTooLong,
+ FileNotFound,
+ SystemResources,
+ NoSpaceLeft,
+ ReadOnlyFileSystem,
+ NotSameFileSystem,
+};
+
+pub fn linkZ(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: i32) LinkError!void {
+ switch (errno(system.link(oldpath, newpath, flags))) {
+ 0 => return,
+ EACCES => return error.AccessDenied,
+ EDQUOT => return error.DiskQuota,
+ EEXIST => return error.PathAlreadyExists,
+ EFAULT => unreachable,
+ EIO => return error.FileSystem,
+ ELOOP => return error.SymLinkLoop,
+ EMLINK => return error.LinkQuotaExceeded,
+ ENAMETOOLONG => return error.NameTooLong,
+ ENOENT => return error.FileNotFound,
+ ENOMEM => return error.SystemResources,
+ ENOSPC => return error.NoSpaceLeft,
+ EPERM => return error.AccessDenied,
+ EROFS => return error.ReadOnlyFileSystem,
+ EXDEV => return error.NotSameFileSystem,
+ EINVAL => unreachable,
+ else => |err| return unexpectedErrno(err),
+ }
+}
+
+pub fn link(oldpath: []const u8, newpath: []const u8, flags: i32) LinkError!void {
+ const old = try toPosixPath(oldpath);
+ const new = try toPosixPath(newpath);
+ return try linkZ(&old, &new, flags);
+}
+
+pub const LinkatError = LinkError || error{NotDir};
+
+pub fn linkatZ(
+ olddir: fd_t,
+ oldpath: [*:0]const u8,
+ newdir: fd_t,
+ newpath: [*:0]const u8,
+ flags: i32,
+) LinkatError!void {
+ switch (errno(system.linkat(olddir, oldpath, newdir, newpath, flags))) {
+ 0 => return,
+ EACCES => return error.AccessDenied,
+ EDQUOT => return error.DiskQuota,
+ EEXIST => return error.PathAlreadyExists,
+ EFAULT => unreachable,
+ EIO => return error.FileSystem,
+ ELOOP => return error.SymLinkLoop,
+ EMLINK => return error.LinkQuotaExceeded,
+ ENAMETOOLONG => return error.NameTooLong,
+ ENOENT => return error.FileNotFound,
+ ENOMEM => return error.SystemResources,
+ ENOSPC => return error.NoSpaceLeft,
+ ENOTDIR => return error.NotDir,
+ EPERM => return error.AccessDenied,
+ EROFS => return error.ReadOnlyFileSystem,
+ EXDEV => return error.NotSameFileSystem,
+ EINVAL => unreachable,
+ else => |err| return unexpectedErrno(err),
+ }
+}
+
+pub fn linkat(
+ olddir: fd_t,
+ oldpath: []const u8,
+ newdir: fd_t,
+ newpath: []const u8,
+ flags: i32,
+) LinkatError!void {
+ const old = try toPosixPath(oldpath);
+ const new = try toPosixPath(newpath);
+ return try linkatZ(olddir, &old, newdir, &new, flags);
+}
+
pub const UnlinkError = error{
FileNotFound,