Add flock command paramter to os.fcntlFlock

Also, replace `os.fcntlFlockBlocking` with `os.fcntlFlock`
This commit is contained in:
LeRoyce Pearson 2020-03-09 22:57:07 -06:00
parent f66a607607
commit 4eff48b12e
2 changed files with 16 additions and 3 deletions

View File

@ -688,7 +688,7 @@ pub const Dir = struct {
flock.l_start = 0;
flock.l_len = 0;
flock.l_pid = 0;
try os.fcntlFlockBlocking(fd, &flock);
try os.fcntlFlock(fd, .SetLockBlocking, &flock);
locked = true;
}

View File

@ -1140,9 +1140,22 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
allocator.free(envp_buf);
}
pub const LockCmd = enum {
GetLock,
SetLock,
SetLockBlocking,
};
/// Attempts to get lock the file, blocking if the file is locked.
pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const Flock) OpenError!void {
const rc = system.fcntl(fd, F_SETLKW, flock_p);
pub fn fcntlFlock(fd: fd_t, lock_cmd: LockCmd, flock_p: *const Flock) OpenError!void {
const cmd: i32 = cmdval: {
switch (lock_cmd) {
.GetLock => break :cmdval F_GETLK,
.SetLock => break :cmdval F_SETLK,
.SetLockBlocking => break :cmdval F_SETLKW,
}
};
const rc = system.fcntl(fd, cmd, flock_p);
if (rc < 0) {
std.debug.panic("fcntl error: {}\n", .{rc});
}