authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:47:27 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:47:27 -07:00
log6743e4e3b03cd3e8175a11f328e530edd054f2f1
tree921f44875d469c7729bbdb6764f599f384c3d32e
parent40983a94c0afb69cc7a4e692986d99e16a27ad0a
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

Dir: allow openFile to acquire a lock


1 files changed, 13 insertions(+), 1 deletions(-)

Dir.zig+13-1
...@@ -29,8 +29,19 @@ pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File {...@@ -29,8 +29,19 @@ pub fn openFile(self: Dir, sub_path: [:0]const u8, flags: OpenFileFlags) !File {
29 .write_only => sys.O.WRONLY,29 .write_only => sys.O.WRONLY,
30 .read_write => sys.O.RDWR,30 .read_write => sys.O.RDWR,
31 };31 };
32 if (os != .linux) oflag |= switch (flags.lock) {
33 .none => 0,
34 .shared => sys.O.SHLOCK,
35 .exclusive => sys.O.EXLOCK,
36 };
32 oflag |= sys.O.CLOEXEC;37 oflag |= sys.O.CLOEXEC;
33 return .{ .fd = @enumFromInt(try sys.openat(@intFromEnum(self.fd), sub_path.ptr, oflag)) };38 const fd = try sys.openat(@intFromEnum(self.fd), sub_path.ptr, oflag);
39 if (os == .linux) switch (flags.lock) {
40 .none => {},
41 .shared => try sys.flock(fd, sys.LOCK.SH),
42 .exclusive => try sys.flock(fd, sys.LOCK.EX),
43 };
44 return .{ .fd = @enumFromInt(fd) };
34}45}
35pub fn openFileC(self: Dir, sub_path: []const u8, flags: OpenFileFlags) !File {46pub fn openFileC(self: Dir, sub_path: []const u8, flags: OpenFileFlags) !File {
36 std.debug.assert(sub_path.len <= sys.PATH_MAX);47 std.debug.assert(sub_path.len <= sys.PATH_MAX);
...@@ -42,6 +53,7 @@ pub fn openFileC(self: Dir, sub_path: []const u8, flags: OpenFileFlags) !File {...@@ -42,6 +53,7 @@ pub fn openFileC(self: Dir, sub_path: []const u8, flags: OpenFileFlags) !File {
4253
43pub const OpenFileFlags = packed struct {54pub const OpenFileFlags = packed struct {
44 mode: enum(u8) { read_only, write_only, read_write } = .read_only,55 mode: enum(u8) { read_only, write_only, read_write } = .read_only,
56 lock: enum(u8) { none, shared, exclusive } = .none,
45};57};
4658
47pub fn openDir(self: Dir, sub_path: [:0]const u8, flags: OpenDirFlags) !Dir {59pub fn openDir(self: Dir, sub_path: [:0]const u8, flags: OpenDirFlags) !Dir {