authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-07 16:23:12-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-07 16:49:30-06:00
log71c5aab3e740c131e63766be57de19b3dd451972
tree56761bbf7ed18991e831e70aed7cc6c924d05d6d
parent28d71c97d1874abdeb782a82f655d250a77a2f69

Make lock option an enum

For some reason, this breaks file locking on windows. Not sure if this is a problem with wine.

2 files changed, 67 insertions(+), 39 deletions(-)

lib/std/fs.zig+45-30
......@@ -596,13 +596,12 @@ pub const Dir = struct {
596596 }
597597
598598 // Use the O_ locking flags if the os supports them
599 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and @hasDecl(os, "O_SHLOCK");
600 const lock_flag: u32 = lock_flag: {
601 if (has_flock_open_flags and flags.lock) {
602 break :lock_flag if (flags.write) @as(u32, os.O_EXLOCK) else @as(u32, os.O_SHLOCK);
603 }
604 break :lock_flag @as(u32, 0);
605 };
599 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK");
600 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
601 .None => @as(u32, 0),
602 .Shared => os.O_SHLOCK,
603 .Exclusive => os.O_EXLOCK,
604 } else 0;
606605
607606 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
608607 const os_flags = lock_flag | O_LARGEFILE | os.O_CLOEXEC | if (flags.write and flags.read)
......@@ -616,9 +615,13 @@ pub const Dir = struct {
616615 else
617616 try os.openatZ(self.fd, sub_path, os_flags, 0);
618617
619 if (!has_flock_open_flags and flags.lock) {
618 if (!has_flock_open_flags and flags.lock != .None) {
620619 // TODO: integrate async I/O
621 try os.flock(fd, if (flags.write) os.LOCK_EX else os.LOCK_SH);
620 try os.flock(fd, switch (flags.lock) {
621 .None => unreachable,
622 .Shared => os.LOCK_SH,
623 .Exclusive => os.LOCK_EX,
624 });
622625 }
623626
624627 return File{
......@@ -638,11 +641,13 @@ pub const Dir = struct {
638641 const access_mask = w.SYNCHRONIZE |
639642 (if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
640643 (if (flags.write) @as(u32, w.GENERIC_WRITE) else 0);
641 const share_access = if (flags.lock)
642 w.FILE_SHARE_DELETE |
643 (if (flags.write) @as(os.windows.ULONG, 0) else w.FILE_SHARE_READ)
644 else
645 null;
644
645 const share_access = switch (flags.lock) {
646 .None => @as(?w.ULONG, null),
647 .Shared => w.FILE_SHARE_READ,
648 .Exclusive => @as(?w.ULONG, 0),
649 };
650
646651 return @as(File, .{
647652 .handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, w.FILE_OPEN),
648653 .io_mode = .blocking,
......@@ -672,7 +677,11 @@ pub const Dir = struct {
672677
673678 // Use the O_ locking flags if the os supports them
674679 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK");
675 const lock_flag: u32 = if (has_flock_open_flags and flags.lock) os.O_EXLOCK else 0;
680 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
681 .None => @as(u32, 0),
682 .Shared => os.O_SHLOCK,
683 .Exclusive => os.O_EXLOCK,
684 } else 0;
676685
677686 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
678687 const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | os.O_CLOEXEC |
......@@ -684,9 +693,13 @@ pub const Dir = struct {
684693 else
685694 try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
686695
687 if (!has_flock_open_flags and flags.lock) {
696 if (!has_flock_open_flags and flags.lock != .None) {
688697 // TODO: integrate async I/O
689 try os.flock(fd, os.LOCK_EX);
698 try os.flock(fd, switch (flags.lock) {
699 .None => unreachable,
700 .Shared => os.LOCK_SH,
701 .Exclusive => os.LOCK_EX,
702 });
690703 }
691704
692705 return File{ .handle = fd, .io_mode = .blocking };
......@@ -705,10 +718,12 @@ pub const Dir = struct {
705718 else
706719 @as(u32, w.FILE_OPEN_IF);
707720
708 const share_access = if (flags.lock)
709 @as(os.windows.ULONG, w.FILE_SHARE_DELETE)
710 else
711 null;
721 const share_access = switch (flags.lock) {
722 .None => @as(?w.ULONG, null),
723 .Shared => w.FILE_SHARE_READ,
724 .Exclusive => @as(?w.ULONG, 0),
725 };
726
712727 return @as(File, .{
713728 .handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, creation),
714729 .io_mode = .blocking,
......@@ -1671,8 +1686,8 @@ test "open file with lock twice, make sure it wasn't open at the same time" {
16711686 const filename = "file_lock_test.txt";
16721687
16731688 var contexts = [_]FileLockTestContext{
1674 .{ .filename = filename, .create = true, .exclusive = true },
1675 .{ .filename = filename, .create = true, .exclusive = true },
1689 .{ .filename = filename, .create = true, .lock = .Exclusive },
1690 .{ .filename = filename, .create = true, .lock = .Exclusive },
16761691 };
16771692 try run_lock_file_test(&contexts);
16781693
......@@ -1703,9 +1718,9 @@ test "create file, lock and read from multiple process at once" {
17031718 try std.fs.cwd().writeFile(filename, filedata);
17041719
17051720 var contexts = [_]FileLockTestContext{
1706 .{ .filename = filename, .create = false, .exclusive = false },
1707 .{ .filename = filename, .create = false, .exclusive = false },
1708 .{ .filename = filename, .create = false, .exclusive = true },
1721 .{ .filename = filename, .create = false, .lock = .Shared },
1722 .{ .filename = filename, .create = false, .lock = .Shared },
1723 .{ .filename = filename, .create = false, .lock = .Exclusive },
17091724 };
17101725
17111726 try run_lock_file_test(&contexts);
......@@ -1740,8 +1755,8 @@ const FileLockTestContext = struct {
17401755
17411756 // use file.createFile
17421757 create: bool,
1743 // get a read/write lock, instead of just a read lock
1744 exclusive: bool,
1758 // the type of lock to use
1759 lock: File.Lock,
17451760
17461761 // Output variables
17471762 err: ?(File.OpenError || std.os.ReadError) = null,
......@@ -1756,12 +1771,12 @@ const FileLockTestContext = struct {
17561771 fn run(ctx: *@This()) void {
17571772 var file: File = undefined;
17581773 if (ctx.create) {
1759 file = cwd().createFile(ctx.filename, .{ .lock = true }) catch |err| {
1774 file = cwd().createFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| {
17601775 ctx.err = err;
17611776 return;
17621777 };
17631778 } else {
1764 file = cwd().openFile(ctx.filename, .{ .lock = true, .write = ctx.exclusive }) catch |err| {
1779 file = cwd().openFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| {
17651780 ctx.err = err;
17661781 return;
17671782 };
lib/std/fs/file.zig+22-9
......@@ -36,22 +36,29 @@ pub const File = struct {
3636
3737 pub const OpenError = windows.CreateFileError || os.OpenError || os.FlockError;
3838
39 pub const Lock = enum {
40 None, Shared, Exclusive
41 };
42
3943 /// TODO https://github.com/ziglang/zig/issues/3802
4044 pub const OpenFlags = struct {
4145 read: bool = true,
4246 write: bool = false,
4347
44 /// Open the file with exclusive access. If `write` is true, then the file is opened with an
45 /// exclusive lock, meaning that no other processes can read or write to the file. Otherwise
46 /// the file is opened with a shared lock, allowing the other processes to read from the
47 /// file, but not to write to the file.
48 /// Open the file with a lock to prevent other processes from accessing it at the
49 /// same time. An exclusive lock will prevent other processes from acquiring a lock.
50 /// A shared lock will prevent other processes from acquiring a exclusive lock, but
51 /// doesn't prevent other process from getting their own shared locks.
4852 ///
4953 /// Note that the lock is only advisory on Linux, except in very specific cirsumstances[1].
50 /// This means that a process that does not respect the locking API can still read and write
54 /// This means that a process that does not respect the locking API can still get access
5155 /// to the file, despite the lock.
5256 ///
57 /// Windows' file locks are mandatory, and any process attempting to access the file will
58 /// receive an error.
59 ///
5360 /// [1]: https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
54 lock: bool = false,
61 lock: Lock = .None,
5562
5663 /// This prevents `O_NONBLOCK` from being passed even if `std.io.is_async`.
5764 /// It allows the use of `noasync` when calling functions related to opening
......@@ -72,14 +79,20 @@ pub const File = struct {
7279 /// `error.FileAlreadyExists` to be returned.
7380 exclusive: bool = false,
7481
75 /// Prevent other files from accessing this file while this process has it is open.
82 /// Open the file with a lock to prevent other processes from accessing it at the
83 /// same time. An exclusive lock will prevent other processes from acquiring a lock.
84 /// A shared lock will prevent other processes from acquiring a exclusive lock, but
85 /// doesn't prevent other process from getting their own shared locks.
7686 ///
7787 /// Note that the lock is only advisory on Linux, except in very specific cirsumstances[1].
78 /// This means that a process that does not respect the locking API can still read and write
88 /// This means that a process that does not respect the locking API can still get access
7989 /// to the file, despite the lock.
8090 ///
91 /// Windows' file locks are mandatory, and any process attempting to access the file will
92 /// receive an error.
93 ///
8194 /// [1]: https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
82 lock: bool = false,
95 lock: Lock = .None,
8396
8497 /// For POSIX systems this is the file system mode the file will
8598 /// be created with.