authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-23 09:08:55+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-23 18:00:05+01:00
log0d4f05bb8ad12e140b0f93157c3c3d2c87e2738c
tree7191bab14506296e3fd40405df8dfb122764772f
parentf8ddc3d8732feece71d6ee55cdfc4d61a5a7e16e

std: Remove O_NONBLOCK flag after locking

We only need O_NONBLOCK when O_SHLOCK/O_EXLOCK are used and we don't want open() to block, don't let this bit leak to the user fd.

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

lib/std/fs.zig+34-2
...@@ -733,10 +733,10 @@ pub const Dir = struct {...@@ -733,10 +733,10 @@ pub const Dir = struct {
733 // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag)733 // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag)
734 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin;734 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin;
735 if (has_flock_open_flags) {735 if (has_flock_open_flags) {
736 const nonblocking_lock_flag = if (flags.lock_nonblocking)736 const nonblocking_lock_flag: u32 = if (flags.lock_nonblocking)
737 os.O_NONBLOCK | os.O_SYNC737 os.O_NONBLOCK | os.O_SYNC
738 else738 else
739 @as(u32, 0);739 0;
740 os_flags |= switch (flags.lock) {740 os_flags |= switch (flags.lock) {
741 .None => @as(u32, 0),741 .None => @as(u32, 0),
742 .Shared => os.O_SHLOCK | nonblocking_lock_flag,742 .Shared => os.O_SHLOCK | nonblocking_lock_flag,
...@@ -771,6 +771,22 @@ pub const Dir = struct {...@@ -771,6 +771,22 @@ pub const Dir = struct {
771 });771 });
772 }772 }
773773
774 if (has_flock_open_flags and flags.lock_nonblocking) {
775 var fl_flags = os.fcntl(fd, os.F_GETFL, 0) catch |err| switch (err) {
776 error.FileBusy => unreachable,
777 error.Locked => unreachable,
778 error.PermissionDenied => unreachable,
779 else => |e| return e,
780 };
781 fl_flags &= ~@as(usize, os.O_NONBLOCK);
782 _ = os.fcntl(fd, os.F_SETFL, fl_flags) catch |err| switch (err) {
783 error.FileBusy => unreachable,
784 error.Locked => unreachable,
785 error.PermissionDenied => unreachable,
786 else => |e| return e,
787 };
788 }
789
774 return File{790 return File{
775 .handle = fd,791 .handle = fd,
776 .capable_io_mode = .blocking,792 .capable_io_mode = .blocking,
...@@ -887,6 +903,22 @@ pub const Dir = struct {...@@ -887,6 +903,22 @@ pub const Dir = struct {
887 });903 });
888 }904 }
889905
906 if (has_flock_open_flags and flags.lock_nonblocking) {
907 var fl_flags = os.fcntl(fd, os.F_GETFL, 0) catch |err| switch (err) {
908 error.FileBusy => unreachable,
909 error.Locked => unreachable,
910 error.PermissionDenied => unreachable,
911 else => |e| return e,
912 };
913 fl_flags &= ~@as(usize, os.O_NONBLOCK);
914 _ = os.fcntl(fd, os.F_SETFL, fl_flags) catch |err| switch (err) {
915 error.FileBusy => unreachable,
916 error.Locked => unreachable,
917 error.PermissionDenied => unreachable,
918 else => |e| return e,
919 };
920 }
921
890 return File{922 return File{
891 .handle = fd,923 .handle = fd,
892 .capable_io_mode = .blocking,924 .capable_io_mode = .blocking,