authorgravatar for sage@sagehane.comSage Hane <sage@sagehane.com> 2022-01-29 13:52:08+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-29 15:52:08+02:00
loge288148f60770a2cfa4c64f832b599172c383d36
treedc6042ecb785d35e47f6c53f58ce05225af869ca
parent88edde4edc2d744a2f92b3f2b9417f1ec9e9af81
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

fs: Use `OpenMode` enum instead of read/write flags.


8 files changed, 39 insertions(+), 29 deletions(-)

doc/docgen.zig+2-2
......@@ -45,7 +45,7 @@ pub fn main() !void {
4545 }
4646 }
4747
48 var in_file = try fs.cwd().openFile(in_file_name, .{ .read = true });
48 var in_file = try fs.cwd().openFile(in_file_name, .{ .mode = .read_only });
4949 defer in_file.close();
5050
5151 var out_file = try fs.cwd().createFile(out_file_name, .{});
......@@ -1866,7 +1866,7 @@ test "shell parsed" {
18661866 // intentional space after "--build-option1 \"
18671867 const shell_out =
18681868 \\$ zig build test.zig \
1869 \\ --build-option1 \
1869 \\ --build-option1 \
18701870 \\ --build-option2
18711871 \\$ ./test
18721872 ;
lib/std/Thread.zig+1-1
......@@ -79,7 +79,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
7979 var buf: [32]u8 = undefined;
8080 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
8181
82 const file = try std.fs.cwd().openFile(path, .{ .write = true });
82 const file = try std.fs.cwd().openFile(path, .{ .mode = .write_only });
8383 defer file.close();
8484
8585 try file.writer().writeAll(name);
lib/std/fs.zig+14-16
......@@ -938,10 +938,10 @@ pub const Dir = struct {
938938 const w = os.wasi;
939939 var fdflags: w.fdflags_t = 0x0;
940940 var base: w.rights_t = 0x0;
941 if (flags.read) {
941 if (flags.isRead()) {
942942 base |= w.RIGHT.FD_READ | w.RIGHT.FD_TELL | w.RIGHT.FD_SEEK | w.RIGHT.FD_FILESTAT_GET;
943943 }
944 if (flags.write) {
944 if (flags.isWrite()) {
945945 fdflags |= w.FDFLAG.APPEND;
946946 base |= w.RIGHT.FD_WRITE |
947947 w.RIGHT.FD_TELL |
......@@ -988,12 +988,11 @@ pub const Dir = struct {
988988 if (!flags.allow_ctty) {
989989 os_flags |= os.O.NOCTTY;
990990 }
991 os_flags |= if (flags.write and flags.read)
992 @as(u32, os.O.RDWR)
993 else if (flags.write)
994 @as(u32, os.O.WRONLY)
995 else
996 @as(u32, os.O.RDONLY);
991 os_flags |= switch (flags.mode) {
992 .read_only => @as(u32, os.O.RDONLY),
993 .write_only => @as(u32, os.O.WRONLY),
994 .read_write => @as(u32, os.O.RDWR),
995 };
997996 const fd = if (flags.intended_io_mode != .blocking)
998997 try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0)
999998 else
......@@ -1045,8 +1044,8 @@ pub const Dir = struct {
10451044 .handle = try w.OpenFile(sub_path_w, .{
10461045 .dir = self.fd,
10471046 .access_mask = w.SYNCHRONIZE |
1048 (if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
1049 (if (flags.write) @as(u32, w.GENERIC_WRITE) else 0),
1047 (if (flags.isRead()) @as(u32, w.GENERIC_READ) else 0) |
1048 (if (flags.isWrite()) @as(u32, w.GENERIC_WRITE) else 0),
10501049 .creation = w.FILE_OPEN,
10511050 .io_mode = flags.intended_io_mode,
10521051 }),
......@@ -2042,12 +2041,11 @@ pub const Dir = struct {
20422041 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);
20432042 return self.accessW(sub_path_w.span().ptr, flags);
20442043 }
2045 const os_mode = if (flags.write and flags.read)
2046 @as(u32, os.R_OK | os.W_OK)
2047 else if (flags.write)
2048 @as(u32, os.W_OK)
2049 else
2050 @as(u32, os.F_OK);
2044 const os_mode = switch (flags.mode) {
2045 .read_only => @as(u32, os.F_OK),
2046 .write_only => @as(u32, os.W_OK),
2047 .read_write => @as(u32, os.R_OK | os.W_OK),
2048 };
20512049 const result = if (need_async_thread and flags.intended_io_mode != .blocking)
20522050 std.event.Loop.instance.?.faccessatZ(self.fd, sub_path, os_mode, 0)
20532051 else
lib/std/fs/file.zig+15-2
......@@ -69,12 +69,17 @@ pub const File = struct {
6969 Unexpected,
7070 } || os.OpenError || os.FlockError;
7171
72 pub const OpenMode = enum {
73 read_only,
74 write_only,
75 read_write,
76 };
77
7278 pub const Lock = enum { None, Shared, Exclusive };
7379
7480 /// TODO https://github.com/ziglang/zig/issues/3802
7581 pub const OpenFlags = struct {
76 read: bool = true,
77 write: bool = false,
82 mode: OpenMode = .read_only,
7883
7984 /// Open the file with an advisory lock to coordinate with other processes
8085 /// accessing it at the same time. An exclusive lock will prevent other
......@@ -118,6 +123,14 @@ pub const File = struct {
118123 /// Set this to allow the opened file to automatically become the
119124 /// controlling TTY for the current process.
120125 allow_ctty: bool = false,
126
127 pub fn isRead(self: OpenFlags) bool {
128 return self.mode != .write_only;
129 }
130
131 pub fn isWrite(self: OpenFlags) bool {
132 return self.mode != .read_only;
133 }
121134 };
122135
123136 /// TODO https://github.com/ziglang/zig/issues/3802
lib/std/fs/test.zig+2-2
......@@ -319,9 +319,9 @@ test "file operations on directories" {
319319 try testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
320320 },
321321 }
322 // Note: The `.write = true` is necessary to ensure the error occurs on all platforms.
322 // Note: The `.mode = .read_write` is necessary to ensure the error occurs on all platforms.
323323 // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732
324 try testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .write = true }));
324 try testing.expectError(error.IsDir, tmp_dir.dir.openFile(test_dir_name, .{ .mode = .read_write }));
325325
326326 switch (builtin.os.tag) {
327327 .wasi, .freebsd, .netbsd, .openbsd, .dragonfly => {},
lib/std/fs/watch.zig+1-1
......@@ -678,7 +678,7 @@ fn testWriteWatchWriteDelete(allocator: Allocator) !void {
678678 };
679679
680680 // overwrite line 2
681 const file = try std.fs.cwd().openFile(file_path, .{ .read = true, .write = true });
681 const file = try std.fs.cwd().openFile(file_path, .{ .mode = .read_write });
682682 {
683683 defer file.close();
684684 const write_contents = "lorem ipsum";
src/Cache.zig+2-3
......@@ -308,8 +308,7 @@ pub const Manifest = struct {
308308 // comparing the hashes on the files used for the cached item
309309 while (true) {
310310 if (self.cache.manifest_dir.openFile(&manifest_file_path, .{
311 .read = true,
312 .write = true,
311 .mode = .read_write,
313312 .lock = .Exclusive,
314313 .lock_nonblocking = self.want_shared_lock,
315314 })) |manifest_file| {
......@@ -410,7 +409,7 @@ pub const Manifest = struct {
410409 cache_hash_file.path = try self.cache.gpa.dupe(u8, file_path);
411410 }
412411
413 const this_file = fs.cwd().openFile(cache_hash_file.path.?, .{ .read = true }) catch |err| switch (err) {
412 const this_file = fs.cwd().openFile(cache_hash_file.path.?, .{ .mode = .read_only }) catch |err| switch (err) {
414413 error.FileNotFound => {
415414 try self.upgradeToExclusiveLock();
416415 return false;
src/test.zig+2-2
......@@ -958,14 +958,14 @@ pub const TestContext = struct {
958958
959959 switch (update.case) {
960960 .Header => |expected_output| {
961 var file = try tmp.dir.openFile("test_case.h", .{ .read = true });
961 var file = try tmp.dir.openFile("test_case.h", .{ .mode = .read_only });
962962 defer file.close();
963963 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
964964
965965 try std.testing.expectEqualStrings(expected_output, out);
966966 },
967967 .CompareObjectFile => |expected_output| {
968 var file = try tmp.dir.openFile(bin_name, .{ .read = true });
968 var file = try tmp.dir.openFile(bin_name, .{ .mode = .read_only });
969969 defer file.close();
970970 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
971971