From a3d9cd1c1d9f1acbc9715a46bee76282e340e294 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 3 Dec 2021 22:47:50 -0700 Subject: [PATCH] std.os: handle ETXTBSY from open() --- lib/std/fs.zig | 2 ++ lib/std/os.zig | 15 ++++++++++++++- lib/std/zig/system/NativeTargetInfo.zig | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 4d900d2e67..0221cc5da3 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1481,6 +1481,7 @@ pub const Dir = struct { error.PathAlreadyExists => unreachable, // not providing O.CREAT error.FileLocksNotSupported => unreachable, // locking folders is not supported error.WouldBlock => unreachable, // can't happen for directories + error.FileBusy => unreachable, // can't happen for directories else => |e| return e, }; return Dir{ .fd = fd }; @@ -1525,6 +1526,7 @@ pub const Dir = struct { error.PathAlreadyExists => unreachable, // not providing O.CREAT error.FileLocksNotSupported => unreachable, // locking folders is not supported error.WouldBlock => unreachable, // can't happen for directories + error.FileBusy => unreachable, // can't happen for directories else => |e| return e, }; return Dir{ .fd = fd }; diff --git a/lib/std/os.zig b/lib/std/os.zig index 58e46e4f9b..e9a82b2d35 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1275,6 +1275,16 @@ pub const OpenError = error{ BadPathName, InvalidUtf8, + /// One of these three things: + /// * pathname refers to an executable image which is currently being + /// executed and write access was requested. + /// * pathname refers to a file that is currently in use as a swap + /// file, and the O_TRUNC flag was specified. + /// * pathname refers to a file that is currently being read by the + /// kernel (e.g., for module/firmware loading), and write access was + /// requested. + FileBusy, + WouldBlock, } || UnexpectedError; @@ -1468,6 +1478,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: u32, mode: mode_t) .BUSY => return error.DeviceBusy, .OPNOTSUPP => return error.FileLocksNotSupported, .AGAIN => return error.WouldBlock, + .TXTBSY => return error.FileBusy, else => |err| return unexpectedErrno(err), } } @@ -4577,7 +4588,8 @@ pub const FlockError = error{ FileLocksNotSupported, } || UnexpectedError; -/// Depending on the operating system `flock` may or may not interact with `fcntl` locks made by other processes. +/// 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); @@ -4650,6 +4662,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP const fd = openZ(pathname, flags, 0) catch |err| switch (err) { error.FileLocksNotSupported => unreachable, error.WouldBlock => unreachable, + error.FileBusy => unreachable, // not asking for write permissions else => |e| return e, }; defer close(fd); diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig index 8ae9a4c708..830f2aa1ad 100644 --- a/lib/std/zig/system/NativeTargetInfo.zig +++ b/lib/std/zig/system/NativeTargetInfo.zig @@ -365,6 +365,7 @@ fn detectAbiAndDynamicLinker( error.PipeBusy => unreachable, error.FileLocksNotSupported => unreachable, error.WouldBlock => unreachable, + error.FileBusy => unreachable, // opened without write permissions error.IsDir, error.NotDir,