Commit 15f55b2805

xackus <14938807+xackus@users.noreply.github.com>
2021-09-24 20:09:31
os.flock: FreeBSD can return EOPNOTSUPP
1 parent 42aa1ea
Changed files (2)
lib
lib/std/fs/file.zig
@@ -866,6 +866,7 @@ pub const File = struct {
 
     pub const LockError = error{
         SystemResources,
+        FileLocksNotSupported,
     } || os.UnexpectedError;
 
     /// Blocks when an incompatible lock is held by another process.
@@ -928,6 +929,7 @@ pub const File = struct {
             return os.flock(file.handle, os.LOCK.UN) catch |err| switch (err) {
                 error.WouldBlock => unreachable, // unlocking can't block
                 error.SystemResources => unreachable, // We are deallocating resources.
+                error.FileLocksNotSupported => unreachable, // We already got the lock.
                 error.Unexpected => unreachable, // Resource deallocation must succeed.
             };
         }
lib/std/os.zig
@@ -4501,8 +4501,12 @@ pub const FlockError = error{
 
     /// The kernel ran out of memory for allocating file locks
     SystemResources,
+
+    /// The underlying filesystem does not support file locks
+    FileLocksNotSupported,
 } || UnexpectedError;
 
+/// Depending on the operating system `flock` may or may not interact with `fcntl` locks made by other processes.
 pub fn flock(fd: fd_t, operation: i32) FlockError!void {
     while (true) {
         const rc = system.flock(fd, operation);
@@ -4513,6 +4517,7 @@ pub fn flock(fd: fd_t, operation: i32) FlockError!void {
             .INVAL => unreachable, // invalid parameters
             .NOLCK => return error.SystemResources,
             .AGAIN => return error.WouldBlock, // TODO: integrate with async instead of just returning an error
+            .OPNOTSUPP => return error.FileLocksNotSupported,
             else => |err| return unexpectedErrno(err),
         }
     }