| author | |
| committer | |
| log | e288148f60770a2cfa4c64f832b599172c383d36 |
| tree | dc6042ecb785d35e47f6c53f58ce05225af869ca |
| parent | 88edde4edc2d744a2f92b3f2b9417f1ec9e9af81 |
| signature |
8 files changed, 39 insertions(+), 29 deletions(-)
doc/docgen.zig+2-2| ... | ... | @@ -45,7 +45,7 @@ pub fn main() !void { |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 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 | 49 | defer in_file.close(); |
| 50 | 50 | |
| 51 | 51 | var out_file = try fs.cwd().createFile(out_file_name, .{}); |
| ... | ... | @@ -1866,7 +1866,7 @@ test "shell parsed" { |
| 1866 | 1866 | // intentional space after "--build-option1 \" |
| 1867 | 1867 | const shell_out = |
| 1868 | 1868 | \\$ zig build test.zig \ |
| 1869 | \\ --build-option1 \ | |
| 1869 | \\ --build-option1 \ | |
| 1870 | 1870 | \\ --build-option2 |
| 1871 | 1871 | \\$ ./test |
| 1872 | 1872 | ; |
lib/std/Thread.zig+1-1| ... | ... | @@ -79,7 +79,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { |
| 79 | 79 | var buf: [32]u8 = undefined; |
| 80 | 80 | const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()}); |
| 81 | 81 | |
| 82 | const file = try std.fs.cwd().openFile(path, .{ .write = true }); | |
| 82 | const file = try std.fs.cwd().openFile(path, .{ .mode = .write_only }); | |
| 83 | 83 | defer file.close(); |
| 84 | 84 | |
| 85 | 85 | try file.writer().writeAll(name); |
lib/std/fs.zig+14-16| ... | ... | @@ -938,10 +938,10 @@ pub const Dir = struct { |
| 938 | 938 | const w = os.wasi; |
| 939 | 939 | var fdflags: w.fdflags_t = 0x0; |
| 940 | 940 | var base: w.rights_t = 0x0; |
| 941 | if (flags.read) { | |
| 941 | if (flags.isRead()) { | |
| 942 | 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 | 945 | fdflags |= w.FDFLAG.APPEND; |
| 946 | 946 | base |= w.RIGHT.FD_WRITE | |
| 947 | 947 | w.RIGHT.FD_TELL | |
| ... | ... | @@ -988,12 +988,11 @@ pub const Dir = struct { |
| 988 | 988 | if (!flags.allow_ctty) { |
| 989 | 989 | os_flags |= os.O.NOCTTY; |
| 990 | 990 | } |
| 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 | }; | |
| 997 | 996 | const fd = if (flags.intended_io_mode != .blocking) |
| 998 | 997 | try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0) |
| 999 | 998 | else |
| ... | ... | @@ -1045,8 +1044,8 @@ pub const Dir = struct { |
| 1045 | 1044 | .handle = try w.OpenFile(sub_path_w, .{ |
| 1046 | 1045 | .dir = self.fd, |
| 1047 | 1046 | .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), | |
| 1050 | 1049 | .creation = w.FILE_OPEN, |
| 1051 | 1050 | .io_mode = flags.intended_io_mode, |
| 1052 | 1051 | }), |
| ... | ... | @@ -2042,12 +2041,11 @@ pub const Dir = struct { |
| 2042 | 2041 | const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path); |
| 2043 | 2042 | return self.accessW(sub_path_w.span().ptr, flags); |
| 2044 | 2043 | } |
| 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 | }; | |
| 2051 | 2049 | const result = if (need_async_thread and flags.intended_io_mode != .blocking) |
| 2052 | 2050 | std.event.Loop.instance.?.faccessatZ(self.fd, sub_path, os_mode, 0) |
| 2053 | 2051 | else |
lib/std/fs/file.zig+15-2| ... | ... | @@ -69,12 +69,17 @@ pub const File = struct { |
| 69 | 69 | Unexpected, |
| 70 | 70 | } || os.OpenError || os.FlockError; |
| 71 | 71 | |
| 72 | pub const OpenMode = enum { | |
| 73 | read_only, | |
| 74 | write_only, | |
| 75 | read_write, | |
| 76 | }; | |
| 77 | ||
| 72 | 78 | pub const Lock = enum { None, Shared, Exclusive }; |
| 73 | 79 | |
| 74 | 80 | /// TODO https://github.com/ziglang/zig/issues/3802 |
| 75 | 81 | pub const OpenFlags = struct { |
| 76 | read: bool = true, | |
| 77 | write: bool = false, | |
| 82 | mode: OpenMode = .read_only, | |
| 78 | 83 | |
| 79 | 84 | /// Open the file with an advisory lock to coordinate with other processes |
| 80 | 85 | /// accessing it at the same time. An exclusive lock will prevent other |
| ... | ... | @@ -118,6 +123,14 @@ pub const File = struct { |
| 118 | 123 | /// Set this to allow the opened file to automatically become the |
| 119 | 124 | /// controlling TTY for the current process. |
| 120 | 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 | }; |
| 122 | 135 | |
| 123 | 136 | /// 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 | 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 | 323 | // 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 })); | |
| 325 | 325 | |
| 326 | 326 | switch (builtin.os.tag) { |
| 327 | 327 | .wasi, .freebsd, .netbsd, .openbsd, .dragonfly => {}, |
lib/std/fs/watch.zig+1-1| ... | ... | @@ -678,7 +678,7 @@ fn testWriteWatchWriteDelete(allocator: Allocator) !void { |
| 678 | 678 | }; |
| 679 | 679 | |
| 680 | 680 | // 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 | 683 | defer file.close(); |
| 684 | 684 | const write_contents = "lorem ipsum"; |
src/Cache.zig+2-3| ... | ... | @@ -308,8 +308,7 @@ pub const Manifest = struct { |
| 308 | 308 | // comparing the hashes on the files used for the cached item |
| 309 | 309 | while (true) { |
| 310 | 310 | if (self.cache.manifest_dir.openFile(&manifest_file_path, .{ |
| 311 | .read = true, | |
| 312 | .write = true, | |
| 311 | .mode = .read_write, | |
| 313 | 312 | .lock = .Exclusive, |
| 314 | 313 | .lock_nonblocking = self.want_shared_lock, |
| 315 | 314 | })) |manifest_file| { |
| ... | ... | @@ -410,7 +409,7 @@ pub const Manifest = struct { |
| 410 | 409 | cache_hash_file.path = try self.cache.gpa.dupe(u8, file_path); |
| 411 | 410 | } |
| 412 | 411 | |
| 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 | 413 | error.FileNotFound => { |
| 415 | 414 | try self.upgradeToExclusiveLock(); |
| 416 | 415 | return false; |
src/test.zig+2-2| ... | ... | @@ -958,14 +958,14 @@ pub const TestContext = struct { |
| 958 | 958 | |
| 959 | 959 | switch (update.case) { |
| 960 | 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 | 962 | defer file.close(); |
| 963 | 963 | const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024); |
| 964 | 964 | |
| 965 | 965 | try std.testing.expectEqualStrings(expected_output, out); |
| 966 | 966 | }, |
| 967 | 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 | 969 | defer file.close(); |
| 970 | 970 | const out = try file.reader().readAllAlloc(arena, 5 * 1024 * 1024); |
| 971 | 971 |