authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-10 18:54:05-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-14 10:12:46-06:00
loga636b59cb58caf86dacd9814116bb459075b4cae
treed8b0e9b88acda7ee50e76ac201d943819af514fd
parent4eff48b12ea90ddeb86f8f089f258ae2d615c7c4

Add `lock` option to CreateFlags


2 files changed, 25 insertions(+), 1 deletions(-)

lib/std/fs.zig+19-1
......@@ -744,6 +744,19 @@ pub const Dir = struct {
744744 try std.event.Loop.instance.?.openatZ(self.fd, sub_path_c, os_flags, flags.mode)
745745 else
746746 try os.openatC(self.fd, sub_path_c, os_flags, flags.mode);
747
748 if (flags.lock) {
749 // TODO: integrate async I/O
750 // mem.zeroes is used here because flock's structure can vary across architectures and systems
751 var flock = mem.zeroes(os.Flock);
752 flock.l_type = os.F_WRLCK;
753 flock.l_whence = os.SEEK_SET;
754 flock.l_start = 0;
755 flock.l_len = 0;
756 flock.l_pid = 0;
757 try os.fcntlFlock(fd, .SetLockBlocking, &flock);
758 }
759
747760 return File{ .handle = fd, .io_mode = .blocking };
748761 }
749762
......@@ -759,7 +772,12 @@ pub const Dir = struct {
759772 @as(u32, w.FILE_OVERWRITE_IF)
760773 else
761774 @as(u32, w.FILE_OPEN_IF);
762 return self.openFileWindows(sub_path_w, access_mask, null, creation);
775
776 const share_access = if (flags.lock)
777 @as(os.windows.ULONG, w.FILE_SHARE_DELETE)
778 else
779 null;
780 return self.openFileWindows(sub_path_w, access_mask, share_access, creation);
763781 }
764782
765783 /// Deprecated; call `openFile` directly.
lib/std/fs/file.zig+6
......@@ -69,6 +69,12 @@ pub const File = struct {
6969 /// `error.FileAlreadyExists` to be returned.
7070 exclusive: bool = false,
7171
72 /// Prevent other files from accessing this file while this process has it is open.
73 ///
74 /// Note that the lock is only advisory on Linux. This means that a process that does not
75 /// respect the locking API can still read and write to the file, despite the lock.
76 lock: bool = false,
77
7278 /// For POSIX systems this is the file system mode the file will
7379 /// be created with.
7480 mode: Mode = default_mode,