diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-03-18 22:39:59 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2024-03-19 11:45:09 -0700 |
| commit | cd62005f19ff966d2c42de4daeb9a1e4b644bf76 (patch) | |
| tree | 4bb316708afaf79c971808df792d8fe86274789b /lib/std/fs/Dir.zig | |
| parent | 7057bffc14602add697eb566b83934b7ad3fd81c (diff) | |
| download | zig-cd62005f19ff966d2c42de4daeb9a1e4b644bf76.tar.gz zig-cd62005f19ff966d2c42de4daeb9a1e4b644bf76.zip | |
extract std.posix from std.os
closes #5019
Diffstat (limited to 'lib/std/fs/Dir.zig')
| -rw-r--r-- | lib/std/fs/Dir.zig | 155 |
1 files changed, 75 insertions, 80 deletions
diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig index 11fbc13c41..e6777e2c09 100644 --- a/lib/std/fs/Dir.zig +++ b/lib/std/fs/Dir.zig @@ -18,7 +18,7 @@ const IteratorError = error{ InvalidUtf8, } || posix.UnexpectedError; -pub const Iterator = switch (builtin.os.tag) { +pub const Iterator = switch (native_os) { .macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => struct { dir: Dir, seek: i64, @@ -34,7 +34,7 @@ pub const Iterator = switch (builtin.os.tag) { /// Memory such as file names referenced in this returned entry becomes invalid /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized. pub fn next(self: *Self) Error!?Entry { - switch (builtin.os.tag) { + switch (native_os) { .macos, .ios => return self.nextDarwin(), .freebsd, .netbsd, .dragonfly, .openbsd => return self.nextBsd(), .solaris, .illumos => return self.nextSolaris(), @@ -183,7 +183,7 @@ pub const Iterator = switch (builtin.os.tag) { const name = @as([*]u8, @ptrCast(&bsd_entry.name))[0..bsd_entry.namlen]; - const skip_zero_fileno = switch (builtin.os.tag) { + const skip_zero_fileno = switch (native_os) { // fileno=0 is used to mark invalid entries or deleted files. .openbsd, .netbsd => true, else => false, @@ -315,7 +315,7 @@ pub const Iterator = switch (builtin.os.tag) { dir: Dir, // The if guard is solely there to prevent compile errors from missing `linux.dirent64` // definition when compiling for other OSes. It doesn't do anything when compiling for Linux. - buf: [1024]u8 align(if (builtin.os.tag != .linux) 1 else @alignOf(linux.dirent64)), + buf: [1024]u8 align(if (native_os != .linux) 1 else @alignOf(linux.dirent64)), index: usize, end_index: usize, first_iter: bool, @@ -348,7 +348,7 @@ pub const Iterator = switch (builtin.os.tag) { self.first_iter = false; } const rc = linux.getdents64(self.dir.fd, &self.buf, self.buf.len); - switch (linux.getErrno(rc)) { + switch (linux.E.init(rc)) { .SUCCESS => {}, .BADF => unreachable, // Dir is invalid or was opened without iteration ability .FAULT => unreachable, @@ -398,7 +398,7 @@ pub const Iterator = switch (builtin.os.tag) { }, .windows => struct { dir: Dir, - buf: [1024]u8 align(@alignOf(std.os.windows.FILE_BOTH_DIR_INFORMATION)), + buf: [1024]u8 align(@alignOf(windows.FILE_BOTH_DIR_INFORMATION)), index: usize, end_index: usize, first_iter: bool, @@ -411,8 +411,8 @@ pub const Iterator = switch (builtin.os.tag) { /// Memory such as file names referenced in this returned entry becomes invalid /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized. pub fn next(self: *Self) Error!?Entry { + const w = windows; while (true) { - const w = std.os.windows; if (self.index >= self.end_index) { var io: w.IO_STATUS_BLOCK = undefined; const rc = w.ntdll.NtQueryDirectoryFile( @@ -582,7 +582,7 @@ pub fn iterateAssumeFirstIteration(self: Dir) Iterator { } fn iterateImpl(self: Dir, first_iter_start_value: bool) Iterator { - switch (builtin.os.tag) { + switch (native_os) { .macos, .ios, .freebsd, @@ -770,11 +770,11 @@ pub fn close(self: *Dir) void { /// On WASI, `sub_path` should be encoded as valid UTF-8. /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File { - if (builtin.os.tag == .windows) { - const path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sub_path); + if (native_os == .windows) { + const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.openFileW(path_w.span(), flags); } - if (builtin.os.tag == .wasi) { + if (native_os == .wasi) { var base: std.os.wasi.rights_t = .{}; if (flags.isRead()) { base.FD_READ = true; @@ -803,9 +803,9 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope /// Same as `openFile` but the path parameter is null-terminated. pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File.OpenError!File { - switch (builtin.os.tag) { + switch (native_os) { .windows => { - const path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, sub_path); + const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path); return self.openFileW(path_w.span(), flags); }, .wasi => { @@ -884,7 +884,7 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File /// Same as `openFile` but Windows-only and the path parameter is /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded. pub fn openFileW(self: Dir, sub_path_w: []const u16, flags: File.OpenFlags) File.OpenError!File { - const w = std.os.windows; + const w = windows; const file: File = .{ .handle = try w.OpenFile(sub_path_w, .{ .dir = self.fd, @@ -925,11 +925,11 @@ pub fn openFileW(self: Dir, sub_path_w: []const u16, flags: File.OpenFlags) File /// On WASI, `sub_path` should be encoded as valid UTF-8. /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File { - if (builtin.os.tag == .windows) { - const path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sub_path); + if (native_os == .windows) { + const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.createFileW(path_w.span(), flags); } - if (builtin.os.tag == .wasi) { + if (native_os == .wasi) { return .{ .handle = try posix.openatWasi(self.fd, sub_path, .{}, .{ .CREAT = true, @@ -957,9 +957,9 @@ pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File /// Same as `createFile` but the path parameter is null-terminated. pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags) File.OpenError!File { - switch (builtin.os.tag) { + switch (native_os) { .windows => { - const path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, sub_path_c); + const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c); return self.createFileW(path_w.span(), flags); }, .wasi => { @@ -968,7 +968,7 @@ pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags else => {}, } - var os_flags: std.os.O = .{ + var os_flags: posix.O = .{ .ACCMODE = if (flags.read) .RDWR else .WRONLY, .CREAT = true, .TRUNC = flags.truncate, @@ -1032,7 +1032,7 @@ pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags /// Same as `createFile` but Windows-only and the path parameter is /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded. pub fn createFileW(self: Dir, sub_path_w: []const u16, flags: File.CreateFlags) File.OpenError!File { - const w = std.os.windows; + const w = windows; const read_flag = if (flags.read) @as(u32, w.GENERIC_READ) else 0; const file: File = .{ .handle = try w.OpenFile(sub_path_w, .{ @@ -1145,7 +1145,7 @@ pub fn makePath(self: Dir, sub_path: []const u8) !void { /// have been modified regardless. /// `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). fn makeOpenPathAccessMaskW(self: Dir, sub_path: []const u8, access_mask: u32, no_follow: bool) OpenError!Dir { - const w = std.os.windows; + const w = windows; var it = try fs.path.componentIterator(sub_path); // If there are no components in the path, then create a dummy component with the full path. var component = it.last() orelse fs.path.NativeComponentIterator.Component{ @@ -1180,9 +1180,9 @@ fn makeOpenPathAccessMaskW(self: Dir, sub_path: []const u8, access_mask: u32, no /// On WASI, `sub_path` should be encoded as valid UTF-8. /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. pub fn makeOpenPath(self: Dir, sub_path: []const u8, open_dir_options: OpenDirOptions) !Dir { - return switch (builtin.os.tag) { + return switch (native_os) { .windows => { - const w = std.os.windows; + const w = windows; const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA | w.SYNCHRONIZE | w.FILE_TRAVERSE | (if (open_dir_options.iterate) w.FILE_LIST_DIRECTORY else @as(u32, 0)); @@ -1215,11 +1215,11 @@ pub const RealPathError = posix.RealPathError; /// Currently supported hosts are: Linux, macOS, and Windows. /// See also `Dir.realpathZ`, `Dir.realpathW`, and `Dir.realpathAlloc`. pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError![]u8 { - if (builtin.os.tag == .wasi) { + if (native_os == .wasi) { @compileError("realpath is not available on WASI"); } - if (builtin.os.tag == .windows) { - const pathname_w = try std.os.windows.sliceToPrefixedFileW(self.fd, pathname); + if (native_os == .windows) { + const pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname); return self.realpathW(pathname_w.span(), out_buffer); } const pathname_c = try posix.toPosixPath(pathname); @@ -1229,12 +1229,12 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError /// Same as `Dir.realpath` except `pathname` is null-terminated. /// See also `Dir.realpath`, `realpathZ`. pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 { - if (builtin.os.tag == .windows) { - const pathname_w = try posix.windows.cStrToPrefixedFileW(self.fd, pathname); + if (native_os == .windows) { + const pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname); return self.realpathW(pathname_w.span(), out_buffer); } - const flags: posix.O = switch (builtin.os.tag) { + const flags: posix.O = switch (native_os) { .linux => .{ .NONBLOCK = true, .CLOEXEC = true, @@ -1255,14 +1255,8 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE }; defer posix.close(fd); - // Use of MAX_PATH_BYTES here is valid as the realpath function does not - // have a variant that takes an arbitrary-size buffer. - // TODO(#4812): Consider reimplementing realpath or using the POSIX.1-2008 - // NULL out parameter (GNU's canonicalize_file_name) to handle overelong - // paths. musl supports passing NULL but restricts the output to PATH_MAX - // anyway. var buffer: [fs.MAX_PATH_BYTES]u8 = undefined; - const out_path = try posix.getFdPath(fd, &buffer); + const out_path = try std.os.getFdPath(fd, &buffer); if (out_path.len > out_buffer.len) { return error.NameTooLong; @@ -1277,7 +1271,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE /// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). /// See also `Dir.realpath`, `realpathW`. pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathError![]u8 { - const w = std.os.windows; + const w = windows; const access_mask = w.GENERIC_READ | w.SYNCHRONIZE; const share_access = w.FILE_SHARE_READ; @@ -1331,16 +1325,16 @@ pub fn realpathAlloc(self: Dir, allocator: Allocator, pathname: []const u8) Real /// Not all targets support this. For example, WASI does not have the concept /// of a current working directory. pub fn setAsCwd(self: Dir) !void { - if (builtin.os.tag == .wasi) { + if (native_os == .wasi) { @compileError("changing cwd is not currently possible in WASI"); } - if (builtin.os.tag == .windows) { - var dir_path_buffer: [std.os.windows.PATH_MAX_WIDE]u16 = undefined; - const dir_path = try std.os.windows.GetFinalPathNameByHandle(self.fd, .{}, &dir_path_buffer); + if (native_os == .windows) { + var dir_path_buffer: [windows.PATH_MAX_WIDE]u16 = undefined; + const dir_path = try windows.GetFinalPathNameByHandle(self.fd, .{}, &dir_path_buffer); if (builtin.link_libc) { return posix.chdirW(dir_path); } - return std.os.windows.SetCurrentDirectory(dir_path); + return windows.SetCurrentDirectory(dir_path); } try posix.fchdir(self.fd); } @@ -1368,9 +1362,9 @@ pub const OpenDirOptions = struct { /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. /// Asserts that the path parameter has no null bytes. pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir { - switch (builtin.os.tag) { + switch (native_os) { .windows => { - const sub_path_w = try posix.windows.sliceToPrefixedFileW(self.fd, sub_path); + const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.openDirW(sub_path_w.span().ptr, args); }, .wasi => { @@ -1427,9 +1421,9 @@ pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError! /// Same as `openDir` except the parameter is null-terminated. pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) OpenError!Dir { - switch (builtin.os.tag) { + switch (native_os) { .windows => { - const sub_path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, sub_path_c); + const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c); return self.openDirW(sub_path_w.span().ptr, args); }, .wasi => { @@ -1453,7 +1447,7 @@ pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) Open /// Same as `openDir` except the path parameter is WTF-16 LE encoded, NT-prefixed. /// This function asserts the target OS is Windows. pub fn openDirW(self: Dir, sub_path_w: [*:0]const u16, args: OpenDirOptions) OpenError!Dir { - const w = std.os.windows; + const w = windows; // TODO remove some of these flags if args.access_sub_paths is false const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA | w.SYNCHRONIZE | w.FILE_TRAVERSE; @@ -1487,7 +1481,7 @@ const MakeOpenDirAccessMaskWOptions = struct { }; fn makeOpenDirAccessMaskW(self: Dir, sub_path_w: [*:0]const u16, access_mask: u32, flags: MakeOpenDirAccessMaskWOptions) OpenError!Dir { - const w = std.os.windows; + const w = windows; var result = Dir{ .fd = undefined, @@ -1545,10 +1539,10 @@ pub const DeleteFileError = posix.UnlinkError; /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. /// Asserts that the path parameter has no null bytes. pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void { - if (builtin.os.tag == .windows) { - const sub_path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sub_path); + if (native_os == .windows) { + const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.deleteFileW(sub_path_w.span()); - } else if (builtin.os.tag == .wasi and !builtin.link_libc) { + } else if (native_os == .wasi and !builtin.link_libc) { posix.unlinkat(self.fd, sub_path, 0) catch |err| switch (err) { error.DirNotEmpty => unreachable, // not passing AT.REMOVEDIR else => |e| return e, @@ -1563,7 +1557,7 @@ pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void { pub fn deleteFileZ(self: Dir, sub_path_c: [*:0]const u8) DeleteFileError!void { posix.unlinkatZ(self.fd, sub_path_c, 0) catch |err| switch (err) { error.DirNotEmpty => unreachable, // not passing AT.REMOVEDIR - error.AccessDenied => |e| switch (builtin.os.tag) { + error.AccessDenied => |e| switch (native_os) { // non-Linux POSIX systems return EPERM when trying to delete a directory, so // we need to handle that case specifically and translate the error .macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => { @@ -1615,10 +1609,10 @@ pub const DeleteDirError = error{ /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. /// Asserts that the path parameter has no null bytes. pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void { - if (builtin.os.tag == .windows) { - const sub_path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sub_path); + if (native_os == .windows) { + const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.deleteDirW(sub_path_w.span()); - } else if (builtin.os.tag == .wasi and !builtin.link_libc) { + } else if (native_os == .wasi and !builtin.link_libc) { posix.unlinkat(self.fd, sub_path, posix.AT.REMOVEDIR) catch |err| switch (err) { error.IsDir => unreachable, // not possible since we pass AT.REMOVEDIR else => |e| return e, @@ -1691,15 +1685,15 @@ pub fn symLink( sym_link_path: []const u8, flags: SymLinkFlags, ) !void { - if (builtin.os.tag == .wasi and !builtin.link_libc) { + if (native_os == .wasi and !builtin.link_libc) { return self.symLinkWasi(target_path, sym_link_path, flags); } - if (builtin.os.tag == .windows) { + if (native_os == .windows) { // Target path does not use sliceToPrefixedFileW because certain paths // are handled differently when creating a symlink than they would be // when converting to an NT namespaced path. CreateSymbolicLink in // symLinkW will handle the necessary conversion. - var target_path_w: std.os.windows.PathSpace = undefined; + var target_path_w: windows.PathSpace = undefined; target_path_w.len = try std.unicode.wtf8ToWtf16Le(&target_path_w.data, target_path); target_path_w.data[target_path_w.len] = 0; // However, we need to canonicalize any path separators to `\`, since if @@ -1711,7 +1705,7 @@ pub fn symLink( mem.nativeToLittle(u16, '\\'), ); - const sym_link_path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sym_link_path); + const sym_link_path_w = try windows.sliceToPrefixedFileW(self.fd, sym_link_path); return self.symLinkW(target_path_w.span(), sym_link_path_w.span(), flags); } const target_path_c = try posix.toPosixPath(target_path); @@ -1736,9 +1730,9 @@ pub fn symLinkZ( sym_link_path_c: [*:0]const u8, flags: SymLinkFlags, ) !void { - if (builtin.os.tag == .windows) { - const target_path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, target_path_c); - const sym_link_path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, sym_link_path_c); + if (native_os == .windows) { + const target_path_w = try windows.cStrToPrefixedFileW(self.fd, target_path_c); + const sym_link_path_w = try windows.cStrToPrefixedFileW(self.fd, sym_link_path_c); return self.symLinkW(target_path_w.span(), sym_link_path_w.span(), flags); } return posix.symlinkatZ(target_path_c, self.fd, sym_link_path_c); @@ -1756,7 +1750,7 @@ pub fn symLinkW( sym_link_path_w: []const u16, flags: SymLinkFlags, ) !void { - return std.os.windows.CreateSymbolicLink(self.fd, sym_link_path_w, target_path_w, flags.is_directory); + return windows.CreateSymbolicLink(self.fd, sym_link_path_w, target_path_w, flags.is_directory); } pub const ReadLinkError = posix.ReadLinkError; @@ -1768,11 +1762,11 @@ pub const ReadLinkError = posix.ReadLinkError; /// On WASI, `sub_path` should be encoded as valid UTF-8. /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. pub fn readLink(self: Dir, sub_path: []const u8, buffer: []u8) ReadLinkError![]u8 { - if (builtin.os.tag == .wasi and !builtin.link_libc) { + if (native_os == .wasi and !builtin.link_libc) { return self.readLinkWasi(sub_path, buffer); } - if (builtin.os.tag == .windows) { - const sub_path_w = try std.os.windows.sliceToPrefixedFileW(self.fd, sub_path); + if (native_os == .windows) { + const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.readLinkW(sub_path_w.span(), buffer); } const sub_path_c = try posix.toPosixPath(sub_path); @@ -1786,8 +1780,8 @@ pub fn readLinkWasi(self: Dir, sub_path: []const u8, buffer: []u8) ![]u8 { /// Same as `readLink`, except the `sub_path_c` parameter is null-terminated. pub fn readLinkZ(self: Dir, sub_path_c: [*:0]const u8, buffer: []u8) ![]u8 { - if (builtin.os.tag == .windows) { - const sub_path_w = try std.os.windows.cStrToPrefixedFileW(self.fd, sub_path_c); + if (native_os == .windows) { + const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c); return self.readLinkW(sub_path_w.span(), buffer); } return posix.readlinkatZ(self.fd, sub_path_c, buffer); @@ -1796,7 +1790,7 @@ pub fn readLinkZ(self: Dir, sub_path_c: [*:0]const u8, buffer: []u8) ![]u8 { /// Windows-only. Same as `readLink` except the pathname parameter /// is WTF16 LE encoded. pub fn readLinkW(self: Dir, sub_path_w: []const u16, buffer: []u8) ![]u8 { - return std.os.windows.ReadLink(self.fd, sub_path_w, buffer); + return windows.ReadLink(self.fd, sub_path_w, buffer); } /// Read all of file contents using a preallocated buffer. @@ -2319,8 +2313,8 @@ pub const AccessError = posix.AccessError; /// For example, instead of testing if a file exists and then opening it, just /// open it and handle the error for file not found. pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void { - if (builtin.os.tag == .windows) { - const sub_path_w = std.os.windows.sliceToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) { + if (native_os == .windows) { + const sub_path_w = windows.sliceToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) { error.AccessDenied => return error.PermissionDenied, else => |e| return e, }; @@ -2332,8 +2326,8 @@ pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessErro /// Same as `access` except the path parameter is null-terminated. pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void { - if (builtin.os.tag == .windows) { - const sub_path_w = std.os.windows.cStrToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) { + if (native_os == .windows) { + const sub_path_w = windows.cStrToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) { error.AccessDenied => return error.PermissionDenied, else => |e| return e, }; @@ -2355,7 +2349,7 @@ pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) Access /// TODO currently this ignores `flags`. pub fn accessW(self: Dir, sub_path_w: [*:0]const u16, flags: File.OpenFlags) AccessError!void { _ = flags; - return posix.faccessatW(self.fd, sub_path_w, 0, 0); + return posix.faccessatW(self.fd, sub_path_w); } pub const CopyFileOptions = struct { @@ -2473,7 +2467,7 @@ fn copy_file(fd_in: posix.fd_t, fd_out: posix.fd_t, maybe_size: ?u64) CopyFileRa } } - if (builtin.os.tag == .linux) { + if (native_os == .linux) { // Try copy_file_range first as that works at the FS level and is the // most efficient method (if available). var offset: u64 = 0; @@ -2555,12 +2549,12 @@ pub const StatFileError = File.OpenError || File.StatError || posix.FStatAtError /// On WASI, `sub_path` should be encoded as valid UTF-8. /// On other platforms, `sub_path` is an opaque sequence of bytes with no particular encoding. pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat { - if (builtin.os.tag == .windows) { + if (native_os == .windows) { var file = try self.openFile(sub_path, .{}); defer file.close(); return file.stat(); } - if (builtin.os.tag == .wasi and !builtin.link_libc) { + if (native_os == .wasi and !builtin.link_libc) { const st = try posix.fstatat_wasi(self.fd, sub_path, .{ .SYMLINK_FOLLOW = true }); return Stat.fromWasi(st); } @@ -2617,9 +2611,10 @@ const builtin = @import("builtin"); const std = @import("../std.zig"); const File = std.fs.File; const AtomicFile = std.fs.AtomicFile; -// https://github.com/ziglang/zig/issues/5019 -const posix = std.os; +const posix = std.posix; const mem = std.mem; const fs = std.fs; const Allocator = std.mem.Allocator; const assert = std.debug.assert; +const windows = std.os.windows; +const native_os = builtin.os.tag; |
