Commit f66a607607

LeRoyce Pearson <leroycepearson@geemili.xyz>
2020-03-10 05:04:05
Define Flock for all posix systems
1 parent e186802
lib/std/os/bits/linux/arm-eabi.zig
@@ -8,6 +8,7 @@ const stack_t = linux.stack_t;
 const sigset_t = linux.sigset_t;
 const uid_t = linux.uid_t;
 const gid_t = linux.gid_t;
+const pid_t = linux.pid_t;
 
 pub const SYS_restart_syscall = 0;
 pub const SYS_exit = 1;
@@ -446,6 +447,10 @@ pub const F_GETLK = 12;
 pub const F_SETLK = 13;
 pub const F_SETLKW = 14;
 
+pub const F_RDLCK = 0;
+pub const F_WRLCK = 1;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
@@ -493,6 +498,16 @@ pub const HWCAP_IDIV = HWCAP_IDIVA | HWCAP_IDIVT;
 pub const HWCAP_LPAE = 1 << 20;
 pub const HWCAP_EVTSTRM = 1 << 21;
 
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    __pad0: [4]u8,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    __unused: [4]u8,
+};
+
 pub const msghdr = extern struct {
     msg_name: ?*sockaddr,
     msg_namelen: socklen_t,
lib/std/os/bits/linux/arm64.zig
@@ -8,6 +8,7 @@ const iovec = linux.iovec;
 const iovec_const = linux.iovec_const;
 const uid_t = linux.uid_t;
 const gid_t = linux.gid_t;
+const pid_t = linux.pid_t;
 const stack_t = linux.stack_t;
 const sigset_t = linux.sigset_t;
 
@@ -339,6 +340,10 @@ pub const F_GETLK = 5;
 pub const F_SETLK = 6;
 pub const F_SETLKW = 7;
 
+pub const F_RDLCK = 0;
+pub const F_WRLCK = 1;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
@@ -362,6 +367,15 @@ pub const MAP_NORESERVE = 0x4000;
 pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
 pub const VDSO_CGT_VER = "LINUX_2.6.39";
 
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    __unused: [4]u8,
+};
+
 pub const msghdr = extern struct {
     msg_name: ?*sockaddr,
     msg_namelen: socklen_t,
lib/std/os/bits/linux/i386.zig
@@ -472,6 +472,10 @@ pub const F_GETLK = 12;
 pub const F_SETLK = 13;
 pub const F_SETLKW = 14;
 
+pub const F_RDLCK = 0;
+pub const F_WRLCK = 1;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
@@ -489,12 +493,12 @@ pub const MMAP2_UNIT = 4096;
 pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
 pub const VDSO_CGT_VER = "LINUX_2.6";
 
-pub const flock = extern struct {
-    lock_type: i16,
-    whence: i16,
-    start: off_t,
-    len: off_t,
-    pid: pid_t,
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
 };
 
 pub const msghdr = extern struct {
lib/std/os/bits/linux/mipsel.zig
@@ -5,6 +5,7 @@ const iovec = linux.iovec;
 const iovec_const = linux.iovec_const;
 const uid_t = linux.uid_t;
 const gid_t = linux.gid_t;
+const pid_t = linux.pid_t;
 
 pub const SYS_Linux = 4000;
 pub const SYS_syscall = (SYS_Linux + 0);
@@ -412,6 +413,10 @@ pub const F_GETLK = 33;
 pub const F_SETLK = 34;
 pub const F_SETLKW = 35;
 
+pub const F_RDLCK = 0;
+pub const F_WRLCK = 1;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
@@ -457,6 +462,16 @@ pub const SO_RCVBUFFORCE = 33;
 pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
 pub const VDSO_CGT_VER = "LINUX_2.6.39";
 
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    __pad0: [4]u8,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    __unused: [4]u8,
+};
+
 pub const blksize_t = i32;
 pub const nlink_t = u32;
 pub const time_t = isize;
lib/std/os/bits/linux/riscv64.zig
@@ -2,6 +2,7 @@
 const std = @import("../../../std.zig");
 const uid_t = std.os.linux.uid_t;
 const gid_t = std.os.linux.gid_t;
+const pid_t = std.os.linux.pid_t;
 
 pub const SYS_io_setup = 0;
 pub const SYS_io_destroy = 1;
@@ -332,6 +333,10 @@ pub const F_GETOWN = 9;
 pub const F_SETSIG = 10;
 pub const F_GETSIG = 11;
 
+pub const F_RDLCK = 0;
+pub const F_WRLCK = 1;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
@@ -350,6 +355,15 @@ pub const timespec = extern struct {
     tv_nsec: isize,
 };
 
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    __unused: [4]u8,
+};
+
 /// Renamed to Stat to not conflict with the stat function.
 /// atime, mtime, and ctime have functions to return `timespec`,
 /// because although this is a POSIX API, the layout and names of
lib/std/os/bits/linux/x86_64.zig
@@ -460,12 +460,12 @@ pub const F_RDLCK = 0;
 pub const F_WRLCK = 1;
 pub const F_UNLCK = 2;
 
-pub const flock = extern struct {
-    lock_type: i16,
-    whence: i16,
-    start: off_t,
-    len: off_t,
-    pid: pid_t,
+pub const Flock = extern struct {
+    l_type: i16,
+    l_whence: i16,
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
 };
 
 pub const msghdr = extern struct {
lib/std/os/bits/darwin.zig
@@ -55,6 +55,14 @@ pub const mach_timebase_info_data = extern struct {
 pub const off_t = i64;
 pub const ino_t = u64;
 
+pub const Flock = extern struct {
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    l_type: i16,
+    l_whence: i16,
+};
+
 /// Renamed to Stat to not conflict with the stat function.
 /// atime, mtime, and ctime have functions to return `timespec`,
 /// because although this is a POSIX API, the layout and names of
lib/std/os/bits/freebsd.zig
@@ -51,6 +51,16 @@ pub const dl_phdr_info = extern struct {
     dlpi_phnum: u16,
 };
 
+pub const Flock = extern struct {
+    l_start: off_t,
+    l_len: off_t,
+    l_pid: pid_t,
+    l_type: i16,
+    l_whence: i16,
+    l_sysid: i32,
+    __unused: [4]u8,
+};
+
 pub const msghdr = extern struct {
     /// optional address
     msg_name: ?*sockaddr,
@@ -350,6 +360,10 @@ pub const F_GETLK = 5;
 pub const F_SETLK = 6;
 pub const F_SETLKW = 7;
 
+pub const F_RDLCK = 1;
+pub const F_WRLCK = 3;
+pub const F_UNLCK = 2;
+
 pub const F_SETOWN_EX = 15;
 pub const F_GETOWN_EX = 16;
 
lib/std/os/bits/netbsd.zig
@@ -22,6 +22,14 @@ pub const dl_phdr_info = extern struct {
     dlpi_phnum: u16,
 };
 
+pub const Flock = extern struct {
+    start: off_t,
+    len: off_t,
+    pid: pid_t,
+    lock_type: i16,
+    whence: i16,
+};
+
 pub const msghdr = extern struct {
     /// optional address
     msg_name: ?*sockaddr,
@@ -312,6 +320,10 @@ pub const F_GETLK = 7;
 pub const F_SETLK = 8;
 pub const F_SETLKW = 9;
 
+pub const F_RDLCK = 1;
+pub const F_WRLCK = 3;
+pub const F_UNLCK = 2;
+
 pub const SEEK_SET = 0;
 pub const SEEK_CUR = 1;
 pub const SEEK_END = 2;
lib/std/fs.zig
@@ -681,13 +681,14 @@ pub const Dir = struct {
         var locked = false;
         if (flags.lock) {
             // TODO: integrate async I/O
-            try os.fcntlFlockBlocking(fd, &os.flock{
-                .lock_type = if (flags.write) os.F_WRLCK else os.F_RDLCK,
-                .whence = os.SEEK_SET,
-                .start = 0,
-                .len = 0,
-                .pid = 0,
-            });
+            // mem.zeroes is used here because flock's structure can vary across architectures and systems
+            var flock = mem.zeroes(os.Flock);
+            flock.l_type = if (flags.write) os.F_WRLCK else os.F_RDLCK;
+            flock.l_whence = os.SEEK_SET;
+            flock.l_start = 0;
+            flock.l_len = 0;
+            flock.l_pid = 0;
+            try os.fcntlFlockBlocking(fd, &flock);
             locked = true;
         }
 
lib/std/os.zig
@@ -1141,7 +1141,7 @@ pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8)
 }
 
 /// Attempts to get lock the file, blocking if the file is locked.
-pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const flock) OpenError!void {
+pub fn fcntlFlockBlocking(fd: fd_t, flock_p: *const Flock) OpenError!void {
     const rc = system.fcntl(fd, F_SETLKW, flock_p);
     if (rc < 0) {
         std.debug.panic("fcntl error: {}\n", .{rc});