authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-06 22:28:43-06:00
committergravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-06 22:28:43-06:00
log20597c85968432e1de22f17d5593471eeb7475f2
tree57cd700d51e4c2c6e039258d9e1f20a76339059f
parent49886d2e452fd57f996795d92cd951649e4bc255

Only call `os.flock` on systems that lack openat locks


1 files changed, 8 insertions(+), 11 deletions(-)

lib/std/fs.zig+8-11
...@@ -596,13 +596,12 @@ pub const Dir = struct {...@@ -596,13 +596,12 @@ pub const Dir = struct {
596 }596 }
597597
598 // Use the O_ locking flags if the os supports them598 // 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");
599 const lock_flag: u32 = lock_flag: {600 const lock_flag: u32 = lock_flag: {
600 if (!flags.lock) break :lock_flag 0;601 if (has_flock_open_flags and flags.lock) {
601 if (flags.write) {602 break :lock_flag if (flags.write) os.O_EXLOCK else os.O_SHLOCK;
602 break :lock_flag if (@hasDecl(os, "O_EXLOCK")) os.O_EXLOCK else 0;
603 } else {
604 break :lock_flag if (@hasDecl(os, "O_SHLOCK")) os.O_SHLOCK else 0;
605 }603 }
604 break :lock_flag 0;
606 };605 };
607606
608 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;607 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
...@@ -617,7 +616,7 @@ pub const Dir = struct {...@@ -617,7 +616,7 @@ pub const Dir = struct {
617 else616 else
618 try os.openatZ(self.fd, sub_path, os_flags, 0);617 try os.openatZ(self.fd, sub_path, os_flags, 0);
619618
620 if (flags.lock and lock_flag == 0) {619 if (!has_flock_open_flags and flags.lock) {
621 // TODO: integrate async I/O620 // TODO: integrate async I/O
622 try os.flock(fd, if (flags.write) os.LOCK_EX else os.LOCK_SH);621 try os.flock(fd, if (flags.write) os.LOCK_EX else os.LOCK_SH);
623 }622 }
...@@ -672,10 +671,8 @@ pub const Dir = struct {...@@ -672,10 +671,8 @@ pub const Dir = struct {
672 }671 }
673672
674 // Use the O_ locking flags if the os supports them673 // Use the O_ locking flags if the os supports them
675 const lock_flag: u32 = lock_flag: {674 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK");
676 if (!flags.lock) break :lock_flag 0;675 const lock_flag: u32 = if (has_flock_open_flags and flags.lock) os.O_EXLOCK else 0;
677 break :lock_flag if (@hasDecl(os, "O_EXLOCK")) os.O_EXLOCK else 0;
678 };
679676
680 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;677 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
681 const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | os.O_CLOEXEC |678 const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | os.O_CLOEXEC |
...@@ -687,7 +684,7 @@ pub const Dir = struct {...@@ -687,7 +684,7 @@ pub const Dir = struct {
687 else684 else
688 try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);685 try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
689686
690 if (flags.lock and lock_flag == 0) {687 if (!has_flock_open_flags and flags.lock) {
691 // TODO: integrate async I/O688 // TODO: integrate async I/O
692 try os.flock(fd, os.LOCK_EX);689 try os.flock(fd, os.LOCK_EX);
693 }690 }