Implement blocking file locking API for windows
parent
9af0590a28
commit
e1868029e9
|
@ -708,7 +708,12 @@ pub const Dir = struct {
|
|||
const access_mask = w.SYNCHRONIZE |
|
||||
(if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
|
||||
(if (flags.write) @as(u32, w.GENERIC_WRITE) else 0);
|
||||
return self.openFileWindows(sub_path_w, access_mask, w.FILE_OPEN);
|
||||
const share_access = if (flags.lock)
|
||||
w.FILE_SHARE_DELETE |
|
||||
(if (flags.write) @as(os.windows.ULONG, 0) else w.FILE_SHARE_READ)
|
||||
else
|
||||
null;
|
||||
return self.openFileWindows(sub_path_w, access_mask, share_access, w.FILE_OPEN);
|
||||
}
|
||||
|
||||
/// Creates, opens, or overwrites a file with write access.
|
||||
|
@ -753,7 +758,7 @@ pub const Dir = struct {
|
|||
@as(u32, w.FILE_OVERWRITE_IF)
|
||||
else
|
||||
@as(u32, w.FILE_OPEN_IF);
|
||||
return self.openFileWindows(sub_path_w, access_mask, creation);
|
||||
return self.openFileWindows(sub_path_w, access_mask, null, creation);
|
||||
}
|
||||
|
||||
/// Deprecated; call `openFile` directly.
|
||||
|
@ -775,8 +780,11 @@ pub const Dir = struct {
|
|||
self: Dir,
|
||||
sub_path_w: [*:0]const u16,
|
||||
access_mask: os.windows.ACCESS_MASK,
|
||||
share_access_opt: ?os.windows.ULONG,
|
||||
creation: os.windows.ULONG,
|
||||
) File.OpenError!File {
|
||||
var delay: usize = 1;
|
||||
while (true) {
|
||||
const w = os.windows;
|
||||
|
||||
if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
|
||||
|
@ -808,6 +816,7 @@ pub const Dir = struct {
|
|||
.SecurityQualityOfService = null,
|
||||
};
|
||||
var io: w.IO_STATUS_BLOCK = undefined;
|
||||
const share_access = share_access_opt orelse w.FILE_SHARE_WRITE | w.FILE_SHARE_READ | w.FILE_SHARE_DELETE;
|
||||
const rc = w.ntdll.NtCreateFile(
|
||||
&result.handle,
|
||||
access_mask,
|
||||
|
@ -815,7 +824,7 @@ pub const Dir = struct {
|
|||
&io,
|
||||
null,
|
||||
w.FILE_ATTRIBUTE_NORMAL,
|
||||
w.FILE_SHARE_WRITE | w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
|
||||
share_access,
|
||||
creation,
|
||||
w.FILE_NON_DIRECTORY_FILE | w.FILE_SYNCHRONOUS_IO_NONALERT,
|
||||
null,
|
||||
|
@ -828,7 +837,17 @@ pub const Dir = struct {
|
|||
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
|
||||
.NO_MEDIA_IN_DEVICE => return error.NoDevice,
|
||||
.INVALID_PARAMETER => unreachable,
|
||||
.SHARING_VIOLATION => return error.SharingViolation,
|
||||
.SHARING_VIOLATION => {
|
||||
// TODO: check if async or blocking
|
||||
//return error.SharingViolation
|
||||
// Sleep so we don't consume a ton of CPU waiting to get lock on file
|
||||
std.time.sleep(delay);
|
||||
// Increase sleep time as long as it is less than 5 seconds
|
||||
if (delay < 5 * std.time.ns_per_s) {
|
||||
delay *= 2;
|
||||
}
|
||||
continue;
|
||||
},
|
||||
.ACCESS_DENIED => return error.AccessDenied,
|
||||
.PIPE_BUSY => return error.PipeBusy,
|
||||
.OBJECT_PATH_SYNTAX_BAD => unreachable,
|
||||
|
@ -836,6 +855,7 @@ pub const Dir = struct {
|
|||
else => return w.unexpectedStatus(rc),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn makeDir(self: Dir, sub_path: []const u8) !void {
|
||||
try os.mkdirat(self.fd, sub_path, default_new_dir_mode);
|
||||
|
|
|
@ -1141,8 +1141,8 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
|
|||
}
|
||||
|
||||
/// Attempts to get lock the file, blocking if the file is locked.
|
||||
pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *flock) OpenError!void {
|
||||
const rc = system.fcntlFlock(fd, F_SETLKW, flock_p);
|
||||
pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const flock) OpenError!void {
|
||||
const rc = system.fcntl(fd, F_SETLKW, flock_p);
|
||||
if (rc < 0) {
|
||||
std.debug.panic("fcntl error: {}\n", .{rc});
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ const iovec = linux.iovec;
|
|||
const iovec_const = linux.iovec_const;
|
||||
const uid_t = linux.uid_t;
|
||||
const gid_t = linux.gid_t;
|
||||
const pid_t = linux.pid_t;
|
||||
const stack_t = linux.stack_t;
|
||||
const sigset_t = linux.sigset_t;
|
||||
|
||||
|
|
|
@ -219,7 +219,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of
|
|||
}
|
||||
}
|
||||
|
||||
pub fn fcntlFlock(fd: fd_t, cmd: i32, flock_p: *flock) usize {
|
||||
pub fn fcntl(fd: fd_t, cmd: i32, flock_p: *const c_void) usize {
|
||||
return syscall3(SYS_fcntl, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, cmd)), @ptrToInt(flock_p));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue