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 {...@@ -45,7 +45,7 @@ pub fn main() !void {
45 }45 }
46 }46 }
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 });
49 defer in_file.close();49 defer in_file.close();
5050
51 var out_file = try fs.cwd().createFile(out_file_name, .{});51 var out_file = try fs.cwd().createFile(out_file_name, .{});
...@@ -1866,7 +1866,7 @@ test "shell parsed" {...@@ -1866,7 +1866,7 @@ test "shell parsed" {
1866 // intentional space after "--build-option1 \"1866 // intentional space after "--build-option1 \"
1867 const shell_out =1867 const shell_out =
1868 \\$ zig build test.zig \1868 \\$ zig build test.zig \
1869 \\ --build-option1 \ 1869 \\ --build-option1 \
1870 \\ --build-option21870 \\ --build-option2
1871 \\$ ./test1871 \\$ ./test
1872 ;1872 ;
lib/std/Thread.zig+1-1
...@@ -79,7 +79,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {...@@ -79,7 +79,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void {
79 var buf: [32]u8 = undefined;79 var buf: [32]u8 = undefined;
80 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});80 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 });
83 defer file.close();83 defer file.close();
8484
85 try file.writer().writeAll(name);85 try file.writer().writeAll(name);
lib/std/fs.zig+14-16
...@@ -938,10 +938,10 @@ pub const Dir = struct {...@@ -938,10 +938,10 @@ pub const Dir = struct {
938 const w = os.wasi;938 const w = os.wasi;
939 var fdflags: w.fdflags_t = 0x0;939 var fdflags: w.fdflags_t = 0x0;
940 var base: w.rights_t = 0x0;940 var base: w.rights_t = 0x0;
941 if (flags.read) {941 if (flags.isRead()) {
942 base |= w.RIGHT.FD_READ | w.RIGHT.FD_TELL | w.RIGHT.FD_SEEK | w.RIGHT.FD_FILESTAT_GET;942 base |= w.RIGHT.FD_READ | w.RIGHT.FD_TELL | w.RIGHT.FD_SEEK | w.RIGHT.FD_FILESTAT_GET;
943 }943 }
944 if (flags.write) {944 if (flags.isWrite()) {
945 fdflags |= w.FDFLAG.APPEND;945 fdflags |= w.FDFLAG.APPEND;
946 base |= w.RIGHT.FD_WRITE |946 base |= w.RIGHT.FD_WRITE |
947 w.RIGHT.FD_TELL |947 w.RIGHT.FD_TELL |
...@@ -988,12 +988,11 @@ pub const Dir = struct {...@@ -988,12 +988,11 @@ pub const Dir = struct {
988 if (!flags.allow_ctty) {988 if (!flags.allow_ctty) {
989 os_flags |= os.O.NOCTTY;989 os_flags |= os.O.NOCTTY;
990 }990 }
991 os_flags |= if (flags.write and flags.read)991 os_flags |= switch (flags.mode) {
992 @as(u32, os.O.RDWR)992 .read_only => @as(u32, os.O.RDONLY),
993 else if (flags.write)993 .write_only => @as(u32, os.O.WRONLY),
994 @as(u32, os.O.WRONLY)994 .read_write => @as(u32, os.O.RDWR),
995 else995 };
996 @as(u32, os.O.RDONLY);
997 const fd = if (flags.intended_io_mode != .blocking)996 const fd = if (flags.intended_io_mode != .blocking)
998 try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0)997 try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0)
999 else998 else
...@@ -1045,8 +1044,8 @@ pub const Dir = struct {...@@ -1045,8 +1044,8 @@ pub const Dir = struct {
1045 .handle = try w.OpenFile(sub_path_w, .{1044 .handle = try w.OpenFile(sub_path_w, .{
1046 .dir = self.fd,1045 .dir = self.fd,
1047 .access_mask = w.SYNCHRONIZE |1046 .access_mask = w.SYNCHRONIZE |
1048 (if (flags.read) @as(u32, w.GENERIC_READ) else 0) |1047 (if (flags.isRead()) @as(u32, w.GENERIC_READ) else 0) |
1049 (if (flags.write) @as(u32, w.GENERIC_WRITE) else 0),1048 (if (flags.isWrite()) @as(u32, w.GENERIC_WRITE) else 0),
1050 .creation = w.FILE_OPEN,1049 .creation = w.FILE_OPEN,
1051 .io_mode = flags.intended_io_mode,1050 .io_mode = flags.intended_io_mode,
1052 }),1051 }),
...@@ -2042,12 +2041,11 @@ pub const Dir = struct {...@@ -2042,12 +2041,11 @@ pub const Dir = struct {
2042 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);2041 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);
2043 return self.accessW(sub_path_w.span().ptr, flags);2042 return self.accessW(sub_path_w.span().ptr, flags);
2044 }2043 }
2045 const os_mode = if (flags.write and flags.read)2044 const os_mode = switch (flags.mode) {
2046 @as(u32, os.R_OK | os.W_OK)2045 .read_only => @as(u32, os.F_OK),
2047 else if (flags.write)2046 .write_only => @as(u32, os.W_OK),
2048 @as(u32, os.W_OK)2047 .read_write => @as(u32, os.R_OK | os.W_OK),
2049 else2048 };
2050 @as(u32, os.F_OK);
2051 const result = if (need_async_thread and flags.intended_io_mode != .blocking)2049 const result = if (need_async_thread and flags.intended_io_mode != .blocking)
2052 std.event.Loop.instance.?.faccessatZ(self.fd, sub_path, os_mode, 0)2050 std.event.Loop.instance.?.faccessatZ(self.fd, sub_path, os_mode, 0)
2053 else2051 else
lib/std/fs/file.zig+15-2
...@@ -69,12 +69,17 @@ pub const File = struct {...@@ -69,12 +69,17 @@ pub const File = struct {
69 Unexpected,69 Unexpected,
70 } || os.OpenError || os.FlockError;70 } || os.OpenError || os.FlockError;
7171
72 pub const OpenMode = enum {
73 read_only,
74 write_only,
75 read_write,
76 };
77
72 pub const Lock = enum { None, Shared, Exclusive };78 pub const Lock = enum { None, Shared, Exclusive };
7379
74 /// TODO https://github.com/ziglang/zig/issues/380280 /// TODO https://github.com/ziglang/zig/issues/3802
75 pub const OpenFlags = struct {81 pub const OpenFlags = struct {
76 read: bool = true,82 mode: OpenMode = .read_only,
77 write: bool = false,
7883
79 /// Open the file with an advisory lock to coordinate with other processes84 /// Open the file with an advisory lock to coordinate with other processes
80 /// accessing it at the same time. An exclusive lock will prevent other85 /// accessing it at the same time. An exclusive lock will prevent other
...@@ -118,6 +123,14 @@ pub const File = struct {...@@ -118,6 +123,14 @@ pub const File = struct {
118 /// Set this to allow the opened file to automatically become the123 /// Set this to allow the opened file to automatically become the
119 /// controlling TTY for the current process.124 /// controlling TTY for the current process.
120 allow_ctty: bool = false,125 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 }
121 };134 };
122135
123 /// TODO https://github.com/ziglang/zig/issues/3802136 /// TODO https://github.com/ziglang/zig/issues/3802
lib/std/fs/test.zig+2-2
...@@ -319,9 +319,9 @@ test "file operations on directories" {...@@ -319,9 +319,9 @@ test "file operations on directories" {
319 try testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));319 try testing.expectError(error.IsDir, tmp_dir.dir.readFileAlloc(testing.allocator, test_dir_name, std.math.maxInt(usize)));
320 },320 },
321 }321 }
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.
323 // TODO: Add a read-only test as well, see https://github.com/ziglang/zig/issues/5732323 // 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
326 switch (builtin.os.tag) {326 switch (builtin.os.tag) {
327 .wasi, .freebsd, .netbsd, .openbsd, .dragonfly => {},327 .wasi, .freebsd, .netbsd, .openbsd, .dragonfly => {},
lib/std/fs/watch.zig+1-1
...@@ -678,7 +678,7 @@ fn testWriteWatchWriteDelete(allocator: Allocator) !void {...@@ -678,7 +678,7 @@ fn testWriteWatchWriteDelete(allocator: Allocator) !void {
678 };678 };
679679
680 // overwrite line 2680 // 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 });
682 {682 {
683 defer file.close();683 defer file.close();
684 const write_contents = "lorem ipsum";684 const write_contents = "lorem ipsum";
src/Cache.zig+2-3
...@@ -308,8 +308,7 @@ pub const Manifest = struct {...@@ -308,8 +308,7 @@ pub const Manifest = struct {
308 // comparing the hashes on the files used for the cached item308 // comparing the hashes on the files used for the cached item
309 while (true) {309 while (true) {
310 if (self.cache.manifest_dir.openFile(&manifest_file_path, .{310 if (self.cache.manifest_dir.openFile(&manifest_file_path, .{
311 .read = true,311 .mode = .read_write,
312 .write = true,
313 .lock = .Exclusive,312 .lock = .Exclusive,
314 .lock_nonblocking = self.want_shared_lock,313 .lock_nonblocking = self.want_shared_lock,
315 })) |manifest_file| {314 })) |manifest_file| {
...@@ -410,7 +409,7 @@ pub const Manifest = struct {...@@ -410,7 +409,7 @@ pub const Manifest = struct {
410 cache_hash_file.path = try self.cache.gpa.dupe(u8, file_path);409 cache_hash_file.path = try self.cache.gpa.dupe(u8, file_path);
411 }410 }
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) {
414 error.FileNotFound => {413 error.FileNotFound => {
415 try self.upgradeToExclusiveLock();414 try self.upgradeToExclusiveLock();
416 return false;415 return false;
src/test.zig+2-2
...@@ -958,14 +958,14 @@ pub const TestContext = struct {...@@ -958,14 +958,14 @@ pub const TestContext = struct {
958958
959 switch (update.case) {959 switch (update.case) {
960 .Header => |expected_output| {960 .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 });
962 defer file.close();962 defer file.close();
963 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);963 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
964964
965 try std.testing.expectEqualStrings(expected_output, out);965 try std.testing.expectEqualStrings(expected_output, out);
966 },966 },
967 .CompareObjectFile => |expected_output| {967 .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 });
969 defer file.close();969 defer file.close();
970 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);970 const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024);
971971