authorgravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2023-05-20 23:11:53+01:00
committergravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2023-05-25 20:17:07+01:00
logba35eeb417e4d54ce4c559871b9330bc95afc053
tree280c28c4deda5fb30b84e84128caa0999a631b89
parent4159add4abe92e903d8797a005344d8325def2fc

std.fs.file: Rename File.Lock enum values to snake case


5 files changed, 57 insertions(+), 53 deletions(-)

lib/std/Build/Cache.zig+3-3
......@@ -408,7 +408,7 @@ pub const Manifest = struct {
408408 if (self.cache.manifest_dir.createFile(&manifest_file_path, .{
409409 .read = true,
410410 .truncate = false,
411 .lock = .Exclusive,
411 .lock = .exclusive,
412412 .lock_nonblocking = self.want_shared_lock,
413413 })) |manifest_file| {
414414 self.manifest_file = manifest_file;
......@@ -418,7 +418,7 @@ pub const Manifest = struct {
418418 error.WouldBlock => {
419419 self.manifest_file = try self.cache.manifest_dir.openFile(&manifest_file_path, .{
420420 .mode = .read_write,
421 .lock = .Shared,
421 .lock = .shared,
422422 });
423423 break;
424424 },
......@@ -885,7 +885,7 @@ pub const Manifest = struct {
885885 // Here we intentionally have a period where the lock is released, in case there are
886886 // other processes holding a shared lock.
887887 manifest_file.unlock();
888 try manifest_file.lock(.Exclusive);
888 try manifest_file.lock(.exclusive);
889889 }
890890 self.have_exclusive_lock = true;
891891 return true;
lib/std/fs.zig+20-20
......@@ -1157,9 +1157,9 @@ pub const Dir = struct {
11571157 else
11581158 0;
11591159 os_flags |= switch (flags.lock) {
1160 .None => @as(u32, 0),
1161 .Shared => os.O.SHLOCK | nonblocking_lock_flag,
1162 .Exclusive => os.O.EXLOCK | nonblocking_lock_flag,
1160 .none => @as(u32, 0),
1161 .shared => os.O.SHLOCK | nonblocking_lock_flag,
1162 .exclusive => os.O.EXLOCK | nonblocking_lock_flag,
11631163 };
11641164 }
11651165 if (@hasDecl(os.O, "LARGEFILE")) {
......@@ -1182,13 +1182,13 @@ pub const Dir = struct {
11821182 // WASI doesn't have os.flock so we intetinally check OS prior to the inner if block
11831183 // since it is not compiltime-known and we need to avoid undefined symbol in Wasm.
11841184 if (builtin.target.os.tag != .wasi) {
1185 if (!has_flock_open_flags and flags.lock != .None) {
1185 if (!has_flock_open_flags and flags.lock != .none) {
11861186 // TODO: integrate async I/O
11871187 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK.NB else @as(i32, 0);
11881188 try os.flock(fd, switch (flags.lock) {
1189 .None => unreachable,
1190 .Shared => os.LOCK.SH | lock_nonblocking,
1191 .Exclusive => os.LOCK.EX | lock_nonblocking,
1189 .none => unreachable,
1190 .shared => os.LOCK.SH | lock_nonblocking,
1191 .exclusive => os.LOCK.EX | lock_nonblocking,
11921192 });
11931193 }
11941194 }
......@@ -1241,9 +1241,9 @@ pub const Dir = struct {
12411241 const range_off: w.LARGE_INTEGER = 0;
12421242 const range_len: w.LARGE_INTEGER = 1;
12431243 const exclusive = switch (flags.lock) {
1244 .None => return file,
1245 .Shared => false,
1246 .Exclusive => true,
1244 .none => return file,
1245 .shared => false,
1246 .exclusive => true,
12471247 };
12481248 try w.LockFile(
12491249 file.handle,
......@@ -1320,9 +1320,9 @@ pub const Dir = struct {
13201320 else
13211321 0;
13221322 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
1323 .None => @as(u32, 0),
1324 .Shared => os.O.SHLOCK | nonblocking_lock_flag,
1325 .Exclusive => os.O.EXLOCK | nonblocking_lock_flag,
1323 .none => @as(u32, 0),
1324 .shared => os.O.SHLOCK | nonblocking_lock_flag,
1325 .exclusive => os.O.EXLOCK | nonblocking_lock_flag,
13261326 } else 0;
13271327
13281328 const O_LARGEFILE = if (@hasDecl(os.O, "LARGEFILE")) os.O.LARGEFILE else 0;
......@@ -1339,13 +1339,13 @@ pub const Dir = struct {
13391339 // WASI doesn't have os.flock so we intetinally check OS prior to the inner if block
13401340 // since it is not compiltime-known and we need to avoid undefined symbol in Wasm.
13411341 if (builtin.target.os.tag != .wasi) {
1342 if (!has_flock_open_flags and flags.lock != .None) {
1342 if (!has_flock_open_flags and flags.lock != .none) {
13431343 // TODO: integrate async I/O
13441344 const lock_nonblocking = if (flags.lock_nonblocking) os.LOCK.NB else @as(i32, 0);
13451345 try os.flock(fd, switch (flags.lock) {
1346 .None => unreachable,
1347 .Shared => os.LOCK.SH | lock_nonblocking,
1348 .Exclusive => os.LOCK.EX | lock_nonblocking,
1346 .none => unreachable,
1347 .shared => os.LOCK.SH | lock_nonblocking,
1348 .exclusive => os.LOCK.EX | lock_nonblocking,
13491349 });
13501350 }
13511351 }
......@@ -1402,9 +1402,9 @@ pub const Dir = struct {
14021402 const range_off: w.LARGE_INTEGER = 0;
14031403 const range_len: w.LARGE_INTEGER = 1;
14041404 const exclusive = switch (flags.lock) {
1405 .None => return file,
1406 .Shared => false,
1407 .Exclusive => true,
1405 .none => return file,
1406 .shared => false,
1407 .exclusive => true,
14081408 };
14091409 try w.LockFile(
14101410 file.handle,
lib/std/fs/file.zig+19-15
......@@ -81,7 +81,11 @@ pub const File = struct {
8181 read_write,
8282 };
8383
84 pub const Lock = enum { None, Shared, Exclusive };
84 pub const Lock = enum {
85 none,
86 shared,
87 exclusive,
88 };
8589
8690 pub const OpenFlags = struct {
8791 mode: OpenMode = .read_only,
......@@ -110,7 +114,7 @@ pub const File = struct {
110114 /// * Windows
111115 ///
112116 /// [1]: https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
113 lock: Lock = .None,
117 lock: Lock = .none,
114118
115119 /// Sets whether or not to wait until the file is locked to return. If set to true,
116120 /// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
......@@ -174,7 +178,7 @@ pub const File = struct {
174178 /// * Windows
175179 ///
176180 /// [1]: https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
177 lock: Lock = .None,
181 lock: Lock = .none,
178182
179183 /// Sets whether or not to wait until the file is locked to return. If set to true,
180184 /// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
......@@ -1465,9 +1469,9 @@ pub const File = struct {
14651469 if (is_windows) {
14661470 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
14671471 const exclusive = switch (l) {
1468 .None => return,
1469 .Shared => false,
1470 .Exclusive => true,
1472 .none => return,
1473 .shared => false,
1474 .exclusive => true,
14711475 };
14721476 return windows.LockFile(
14731477 file.handle,
......@@ -1486,9 +1490,9 @@ pub const File = struct {
14861490 };
14871491 } else {
14881492 return os.flock(file.handle, switch (l) {
1489 .None => os.LOCK.UN,
1490 .Shared => os.LOCK.SH,
1491 .Exclusive => os.LOCK.EX,
1493 .none => os.LOCK.UN,
1494 .shared => os.LOCK.SH,
1495 .exclusive => os.LOCK.EX,
14921496 }) catch |err| switch (err) {
14931497 error.WouldBlock => unreachable, // non-blocking=false
14941498 else => |e| return e,
......@@ -1532,9 +1536,9 @@ pub const File = struct {
15321536 if (is_windows) {
15331537 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
15341538 const exclusive = switch (l) {
1535 .None => return,
1536 .Shared => false,
1537 .Exclusive => true,
1539 .none => return,
1540 .shared => false,
1541 .exclusive => true,
15381542 };
15391543 windows.LockFile(
15401544 file.handle,
......@@ -1553,9 +1557,9 @@ pub const File = struct {
15531557 };
15541558 } else {
15551559 os.flock(file.handle, switch (l) {
1556 .None => os.LOCK.UN,
1557 .Shared => os.LOCK.SH | os.LOCK.NB,
1558 .Exclusive => os.LOCK.EX | os.LOCK.NB,
1560 .none => os.LOCK.UN,
1561 .shared => os.LOCK.SH | os.LOCK.NB,
1562 .exclusive => os.LOCK.EX | os.LOCK.NB,
15591563 }) catch |err| switch (err) {
15601564 error.WouldBlock => return false,
15611565 else => |e| return e,
lib/std/fs/test.zig+11-11
......@@ -336,7 +336,7 @@ test "Dir.realpath smoke test" {
336336 var tmp_dir = tmpDir(.{});
337337 defer tmp_dir.cleanup();
338338
339 var file = try tmp_dir.dir.createFile("test_file", .{ .lock = File.Lock.Shared });
339 var file = try tmp_dir.dir.createFile("test_file", .{ .lock = .shared });
340340 // We need to close the file immediately as otherwise on Windows we'll end up
341341 // with a sharing violation.
342342 file.close();
......@@ -1035,10 +1035,10 @@ test "open file with exclusive nonblocking lock twice" {
10351035 var tmp = tmpDir(.{});
10361036 defer tmp.cleanup();
10371037
1038 const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
1038 const file1 = try tmp.dir.createFile(filename, .{ .lock = .exclusive, .lock_nonblocking = true });
10391039 defer file1.close();
10401040
1041 const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
1041 const file2 = tmp.dir.createFile(filename, .{ .lock = .exclusive, .lock_nonblocking = true });
10421042 try testing.expectError(error.WouldBlock, file2);
10431043}
10441044
......@@ -1050,10 +1050,10 @@ test "open file with shared and exclusive nonblocking lock" {
10501050 var tmp = tmpDir(.{});
10511051 defer tmp.cleanup();
10521052
1053 const file1 = try tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
1053 const file1 = try tmp.dir.createFile(filename, .{ .lock = .shared, .lock_nonblocking = true });
10541054 defer file1.close();
10551055
1056 const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
1056 const file2 = tmp.dir.createFile(filename, .{ .lock = .exclusive, .lock_nonblocking = true });
10571057 try testing.expectError(error.WouldBlock, file2);
10581058}
10591059
......@@ -1065,10 +1065,10 @@ test "open file with exclusive and shared nonblocking lock" {
10651065 var tmp = tmpDir(.{});
10661066 defer tmp.cleanup();
10671067
1068 const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
1068 const file1 = try tmp.dir.createFile(filename, .{ .lock = .exclusive, .lock_nonblocking = true });
10691069 defer file1.close();
10701070
1071 const file2 = tmp.dir.createFile(filename, .{ .lock = .Shared, .lock_nonblocking = true });
1071 const file2 = tmp.dir.createFile(filename, .{ .lock = .shared, .lock_nonblocking = true });
10721072 try testing.expectError(error.WouldBlock, file2);
10731073}
10741074
......@@ -1085,13 +1085,13 @@ test "open file with exclusive lock twice, make sure second lock waits" {
10851085 var tmp = tmpDir(.{});
10861086 defer tmp.cleanup();
10871087
1088 const file = try tmp.dir.createFile(filename, .{ .lock = .Exclusive });
1088 const file = try tmp.dir.createFile(filename, .{ .lock = .exclusive });
10891089 errdefer file.close();
10901090
10911091 const S = struct {
10921092 fn checkFn(dir: *fs.Dir, started: *std.Thread.ResetEvent, locked: *std.Thread.ResetEvent) !void {
10931093 started.set();
1094 const file1 = try dir.createFile(filename, .{ .lock = .Exclusive });
1094 const file1 = try dir.createFile(filename, .{ .lock = .exclusive });
10951095
10961096 locked.set();
10971097 file1.close();
......@@ -1138,12 +1138,12 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
11381138 defer gpa.free(filename);
11391139
11401140 const file1 = try fs.createFileAbsolute(filename, .{
1141 .lock = .Exclusive,
1141 .lock = .exclusive,
11421142 .lock_nonblocking = true,
11431143 });
11441144
11451145 const file2 = fs.createFileAbsolute(filename, .{
1146 .lock = .Exclusive,
1146 .lock = .exclusive,
11471147 .lock_nonblocking = true,
11481148 });
11491149 file1.close();
src/Module.zig+4-4
......@@ -3618,7 +3618,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
36183618 file.sub_file_path, want_local_cache, &digest,
36193619 });
36203620
3621 break :lock .Shared;
3621 break :lock .shared;
36223622 },
36233623 .parse_failure, .astgen_failure, .success_zir => lock: {
36243624 const unchanged_metadata =
......@@ -3633,7 +3633,7 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
36333633
36343634 log.debug("metadata changed: {s}", .{file.sub_file_path});
36353635
3636 break :lock .Exclusive;
3636 break :lock .exclusive;
36373637 },
36383638 };
36393639
......@@ -3715,11 +3715,11 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
37153715 }
37163716
37173717 // If we already have the exclusive lock then it is our job to update.
3718 if (builtin.os.tag == .wasi or lock == .Exclusive) break;
3718 if (builtin.os.tag == .wasi or lock == .exclusive) break;
37193719 // Otherwise, unlock to give someone a chance to get the exclusive lock
37203720 // and then upgrade to an exclusive lock.
37213721 cache_file.unlock();
3722 lock = .Exclusive;
3722 lock = .exclusive;
37233723 try cache_file.lock(lock);
37243724 }
37253725