From 0cc42d090fefcdd10ab64f6ae484404a6c6a710e Mon Sep 17 00:00:00 2001 From: Krzysztof Wolicki Date: Tue, 9 Jul 2024 22:36:38 +0200 Subject: [PATCH] std.fs.Dir: Rename OpenDirOptions to OpenOptions (#20542) * std.fs.Dir: Rename OpenDirOptions to OpenOptions https://ziglang.org/documentation/master/#Avoid-Redundant-Names-in-Fully-Qualified-Namespaces * std.fs.Dir: Add deprecated alias `OpenDirOptions` --- lib/std/Build/Cache/Path.zig | 2 +- lib/std/fs.zig | 6 +++--- lib/std/fs/Dir.zig | 19 +++++++++++-------- lib/std/posix.zig | 2 +- lib/std/testing.zig | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/std/Build/Cache/Path.zig b/lib/std/Build/Cache/Path.zig index d7266da9b0cc..48bb8c32beab 100644 --- a/lib/std/Build/Cache/Path.zig +++ b/lib/std/Build/Cache/Path.zig @@ -58,7 +58,7 @@ pub fn openFile( return p.root_dir.handle.openFile(joined_path, flags); } -pub fn makeOpenPath(p: Path, sub_path: []const u8, opts: fs.Dir.OpenDirOptions) !fs.Dir { +pub fn makeOpenPath(p: Path, sub_path: []const u8, opts: fs.Dir.OpenOptions) !fs.Dir { var buf: [fs.max_path_bytes]u8 = undefined; const joined_path = if (p.sub_path.len == 0) sub_path else p: { break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{ diff --git a/lib/std/fs.zig b/lib/std/fs.zig index e56d68e40728..8dc79f9ef4df 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -278,18 +278,18 @@ pub fn defaultWasiCwd() std.os.wasi.fd_t { /// On Windows, `absolute_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). /// On WASI, `absolute_path` should be encoded as valid UTF-8. /// On other platforms, `absolute_path` is an opaque sequence of bytes with no particular encoding. -pub fn openDirAbsolute(absolute_path: []const u8, flags: Dir.OpenDirOptions) File.OpenError!Dir { +pub fn openDirAbsolute(absolute_path: []const u8, flags: Dir.OpenOptions) File.OpenError!Dir { assert(path.isAbsolute(absolute_path)); return cwd().openDir(absolute_path, flags); } /// Same as `openDirAbsolute` but the path parameter is null-terminated. -pub fn openDirAbsoluteZ(absolute_path_c: [*:0]const u8, flags: Dir.OpenDirOptions) File.OpenError!Dir { +pub fn openDirAbsoluteZ(absolute_path_c: [*:0]const u8, flags: Dir.OpenOptions) File.OpenError!Dir { assert(path.isAbsoluteZ(absolute_path_c)); return cwd().openDirZ(absolute_path_c, flags); } /// Same as `openDirAbsolute` but the path parameter is null-terminated. -pub fn openDirAbsoluteW(absolute_path_c: [*:0]const u16, flags: Dir.OpenDirOptions) File.OpenError!Dir { +pub fn openDirAbsoluteW(absolute_path_c: [*:0]const u16, flags: Dir.OpenOptions) File.OpenError!Dir { assert(path.isAbsoluteWindowsW(absolute_path_c)); return cwd().openDirW(absolute_path_c, flags); } diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig index 597c158d631b..c7a436bc73af 100644 --- a/lib/std/fs/Dir.zig +++ b/lib/std/fs/Dir.zig @@ -740,7 +740,7 @@ pub const Walker = struct { /// Recursively iterates over a directory. /// -/// `self` must have been opened with `OpenDirOptions{.iterate = true}`. +/// `self` must have been opened with `OpenOptions{.iterate = true}`. /// /// `Walker.deinit` releases allocated memory and directory handles. /// @@ -1233,7 +1233,7 @@ fn makeOpenPathAccessMaskW(self: Dir, sub_path: []const u8, access_mask: u32, no /// On Windows, `sub_path` should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). /// 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) (MakeError || OpenError || StatFileError)!Dir { +pub fn makeOpenPath(self: Dir, sub_path: []const u8, open_dir_options: OpenOptions) (MakeError || OpenError || StatFileError)!Dir { return switch (native_os) { .windows => { const w = windows; @@ -1393,7 +1393,10 @@ pub fn setAsCwd(self: Dir) !void { try posix.fchdir(self.fd); } -pub const OpenDirOptions = struct { +/// Deprecated: use `OpenOptions` +pub const OpenDirOptions = OpenOptions; + +pub const OpenOptions = struct { /// `true` means the opened directory can be used as the `Dir` parameter /// for functions which operate based on an open directory handle. When `false`, /// such operations are Illegal Behavior. @@ -1415,7 +1418,7 @@ pub const OpenDirOptions = struct { /// 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. /// Asserts that the path parameter has no null bytes. -pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir { +pub fn openDir(self: Dir, sub_path: []const u8, args: OpenOptions) OpenError!Dir { switch (native_os) { .windows => { const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); @@ -1473,7 +1476,7 @@ 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 { +pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenOptions) OpenError!Dir { switch (native_os) { .windows => { const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c); @@ -1530,7 +1533,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 { +pub fn openDirW(self: Dir, sub_path_w: [*:0]const u16, args: OpenOptions) OpenError!Dir { 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 | @@ -2650,7 +2653,7 @@ pub const ChmodError = File.ChmodError; /// The process must have the correct privileges in order to do this /// successfully, or must have the effective user ID matching the owner /// of the directory. Additionally, the directory must have been opened -/// with `OpenDirOptions{ .iterate = true }`. +/// with `OpenOptions{ .iterate = true }`. pub fn chmod(self: Dir, new_mode: File.Mode) ChmodError!void { const file: File = .{ .handle = self.fd }; try file.chmod(new_mode); @@ -2660,7 +2663,7 @@ pub fn chmod(self: Dir, new_mode: File.Mode) ChmodError!void { /// The process must have the correct privileges in order to do this /// successfully. The group may be changed by the owner of the directory to /// any group of which the owner is a member. Additionally, the directory -/// must have been opened with `OpenDirOptions{ .iterate = true }`. If the +/// must have been opened with `OpenOptions{ .iterate = true }`. If the /// owner or group is specified as `null`, the ID is not changed. pub fn chown(self: Dir, owner: ?File.Uid, group: ?File.Gid) ChownError!void { const file: File = .{ .handle = self.fd }; diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 57eae4bbd66f..e2af96c48749 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -493,7 +493,7 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void { switch (errno(res)) { .SUCCESS => return, .INTR => continue, - .BADF => unreachable, // Can be reached if the fd refers to a directory opened without `OpenDirOptions{ .iterate = true }` + .BADF => unreachable, // Can be reached if the fd refers to a directory opened without `Dir.OpenOptions{ .iterate = true }` .FAULT => unreachable, .INVAL => unreachable, diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 341161a64ba1..190182db70fb 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -558,7 +558,7 @@ pub const TmpDir = struct { } }; -pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir { +pub fn tmpDir(opts: std.fs.Dir.OpenOptions) TmpDir { var random_bytes: [TmpDir.random_bytes_count]u8 = undefined; std.crypto.random.bytes(&random_bytes); var sub_path: [TmpDir.sub_path_len]u8 = undefined;