authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-09 22:57:07-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-14 10:12:46-06:00
log4eff48b12ea90ddeb86f8f089f258ae2d615c7c4
tree6aaaa85c979d7e77593c1da6357efc0a0dd90ff2
parentf66a607607c20841ed59a1fe11486333f8621426

Add flock command paramter to `os.fcntlFlock`

Also, replace `os.fcntlFlockBlocking` with `os.fcntlFlock`

2 files changed, 16 insertions(+), 3 deletions(-)

lib/std/fs.zig+1-1
...@@ -688,7 +688,7 @@ pub const Dir = struct {...@@ -688,7 +688,7 @@ pub const Dir = struct {
688 flock.l_start = 0;688 flock.l_start = 0;
689 flock.l_len = 0;689 flock.l_len = 0;
690 flock.l_pid = 0;690 flock.l_pid = 0;
691 try os.fcntlFlockBlocking(fd, &flock);691 try os.fcntlFlock(fd, .SetLockBlocking, &flock);
692 locked = true;692 locked = true;
693 }693 }
694694
lib/std/os.zig+15-2
...@@ -1140,9 +1140,22 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)...@@ -1140,9 +1140,22 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
1140 allocator.free(envp_buf);1140 allocator.free(envp_buf);
1141}1141}
11421142
1143pub const LockCmd = enum {
1144 GetLock,
1145 SetLock,
1146 SetLockBlocking,
1147};
1148
1143/// Attempts to get lock the file, blocking if the file is locked.1149/// Attempts to get lock the file, blocking if the file is locked.
1144pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const Flock) OpenError!void {1150pub fn fcntlFlock(fd: fd_t, lock_cmd: LockCmd, flock_p: *const Flock) OpenError!void {
1145 const rc = system.fcntl(fd, F_SETLKW, flock_p);1151 const cmd: i32 = cmdval: {
1152 switch (lock_cmd) {
1153 .GetLock => break :cmdval F_GETLK,
1154 .SetLock => break :cmdval F_SETLK,
1155 .SetLockBlocking => break :cmdval F_SETLKW,
1156 }
1157 };
1158 const rc = system.fcntl(fd, cmd, flock_p);
1146 if (rc < 0) {1159 if (rc < 0) {
1147 std.debug.panic("fcntl error: {}\n", .{rc});1160 std.debug.panic("fcntl error: {}\n", .{rc});
1148 }1161 }