authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-17 20:53:43-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-17 20:53:43-06:00
logb773a8b175b6be130af2249f267e4113fdbf30ea
tree0ae3b738d90819fc06bc4cad579c9e4d89638aa1
parent4532f5ecad23a3478310f159f43d31217c863c35

Make `fcntlFlock` follow conventions of `os.zig`


3 files changed, 20 insertions(+), 12 deletions(-)

lib/std/fs.zig+1-1
...@@ -1605,7 +1605,7 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {...@@ -1605,7 +1605,7 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
1605 return os.readlinkC(pathname_c, buffer);1605 return os.readlinkC(pathname_c, buffer);
1606}1606}
16071607
1608pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;1608pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError || os.FcntlError;
16091609
1610pub fn openSelfExe() OpenSelfExeError!File {1610pub fn openSelfExe() OpenSelfExeError!File {
1611 if (builtin.os.tag == .linux) {1611 if (builtin.os.tag == .linux) {
lib/std/fs/file.zig+1-1
...@@ -34,7 +34,7 @@ pub const File = struct {...@@ -34,7 +34,7 @@ pub const File = struct {
34 else => 0o666,34 else => 0o666,
35 };35 };
3636
37 pub const OpenError = windows.CreateFileError || os.OpenError;37 pub const OpenError = windows.CreateFileError || os.OpenError || os.FcntlError;
3838
39 /// TODO https://github.com/ziglang/zig/issues/380239 /// TODO https://github.com/ziglang/zig/issues/3802
40 pub const OpenFlags = struct {40 pub const OpenFlags = struct {
lib/std/os.zig+18-10
...@@ -1146,18 +1146,26 @@ pub const LockCmd = enum {...@@ -1146,18 +1146,26 @@ pub const LockCmd = enum {
1146 SetLockBlocking,1146 SetLockBlocking,
1147};1147};
11481148
1149pub const FcntlError = error{
1150 /// The file is locked by another process
1151 FileLocked,
1152} || UnexpectedError;
1153
1149/// Attempts to get lock the file, blocking if the file is locked.1154/// Attempts to get lock the file, blocking if the file is locked.
1150pub fn fcntlFlock(fd: fd_t, lock_cmd: LockCmd, flock_p: *Flock) OpenError!void {1155pub fn fcntlFlock(fd: fd_t, lock_cmd: LockCmd, flock_p: *Flock) FcntlError!void {
1151 const cmd: i32 = cmdval: {1156 const cmd: i32 = switch (lock_cmd) {
1152 switch (lock_cmd) {1157 .GetLock => F_GETLK,
1153 .GetLock => break :cmdval F_GETLK,1158 .SetLock => F_SETLK,
1154 .SetLock => break :cmdval F_SETLK,1159 .SetLockBlocking => F_SETLKW,
1155 .SetLockBlocking => break :cmdval F_SETLKW,
1156 }
1157 };1160 };
1158 const rc = system.fcntl(fd, cmd, flock_p);1161 while (true) {
1159 if (rc < 0) {1162 switch (errno(system.fcntl(fd, cmd, flock_p))) {
1160 std.debug.panic("fcntl error: {}\n", .{rc});1163 0 => return,
1164 EACCES => return error.FileLocked,
1165 EAGAIN => return error.FileLocked,
1166 EINTR => continue,
1167 else => |err| return unexpectedErrno(err),
1168 }
1161 }1169 }
1162}1170}
11631171