Commit 7f4cce3345

Andrew Kelley <andrew@ziglang.org>
2020-02-07 18:11:15
add fcntl support on darwin
1 parent 71873e7
Changed files (3)
lib
lib/std/os/bits/darwin.zig
@@ -1224,3 +1224,161 @@ pub const RTLD_NEXT = @intToPtr(*c_void, ~maxInt(usize));
 pub const RTLD_DEFAULT = @intToPtr(*c_void, ~maxInt(usize) - 1);
 pub const RTLD_SELF = @intToPtr(*c_void, ~maxInt(usize) - 2);
 pub const RTLD_MAIN_ONLY = @intToPtr(*c_void, ~maxInt(usize) - 4);
+
+/// duplicate file descriptor
+pub const F_DUPFD = 0;
+
+/// get file descriptor flags
+pub const F_GETFD = 1;
+
+/// set file descriptor flags
+pub const F_SETFD = 2;
+
+/// get file status flags
+pub const F_GETFL = 3;
+
+/// set file status flags
+pub const F_SETFL = 4;
+
+/// get SIGIO/SIGURG proc/pgrp
+pub const F_GETOWN = 5;
+
+/// set SIGIO/SIGURG proc/pgrp
+pub const F_SETOWN = 6;
+
+/// get record locking information
+pub const F_GETLK = 7;
+
+/// set record locking information
+pub const F_SETLK = 8;
+
+/// F_SETLK; wait if blocked
+pub const F_SETLKW = 9;
+
+/// F_SETLK; wait if blocked, return on timeout
+pub const F_SETLKWTIMEOUT = 10;
+pub const F_FLUSH_DATA = 40;
+
+/// Used for regression test
+pub const F_CHKCLEAN = 41;
+
+/// Preallocate storage
+pub const F_PREALLOCATE = 42;
+
+/// Truncate a file without zeroing space
+pub const F_SETSIZE = 43;
+
+/// Issue an advisory read async with no copy to user
+pub const F_RDADVISE = 44;
+
+/// turn read ahead off/on for this fd
+pub const F_RDAHEAD = 45;
+
+/// turn data caching off/on for this fd
+pub const F_NOCACHE = 48;
+
+/// file offset to device offset
+pub const F_LOG2PHYS = 49;
+
+/// return the full path of the fd
+pub const F_GETPATH = 50;
+
+/// fsync + ask the drive to flush to the media
+pub const F_FULLFSYNC = 51;
+
+/// find which component (if any) is a package
+pub const F_PATHPKG_CHECK = 52;
+
+/// "freeze" all fs operations
+pub const F_FREEZE_FS = 53;
+
+/// "thaw" all fs operations
+pub const F_THAW_FS = 54;
+
+/// turn data caching off/on (globally) for this file
+pub const F_GLOBAL_NOCACHE = 55;
+
+/// add detached signatures
+pub const F_ADDSIGS = 59;
+
+/// add signature from same file (used by dyld for shared libs)
+pub const F_ADDFILESIGS = 61;
+
+/// used in conjunction with F_NOCACHE to indicate that DIRECT, synchonous writes
+/// should not be used (i.e. its ok to temporaily create cached pages)
+pub const F_NODIRECT = 62;
+
+///Get the protection class of a file from the EA, returns int
+pub const F_GETPROTECTIONCLASS = 63;
+
+///Set the protection class of a file for the EA, requires int
+pub const F_SETPROTECTIONCLASS = 64;
+
+///file offset to device offset, extended
+pub const F_LOG2PHYS_EXT = 65;
+
+///get record locking information, per-process
+pub const F_GETLKPID = 66;
+
+///Mark the file as being the backing store for another filesystem
+pub const F_SETBACKINGSTORE = 70;
+
+///return the full path of the FD, but error in specific mtmd circumstances
+pub const F_GETPATH_MTMINFO = 71;
+
+///Returns the code directory, with associated hashes, to the caller
+pub const F_GETCODEDIR = 72;
+
+///No SIGPIPE generated on EPIPE
+pub const F_SETNOSIGPIPE = 73;
+
+///Status of SIGPIPE for this fd
+pub const F_GETNOSIGPIPE = 74;
+
+///For some cases, we need to rewrap the key for AKS/MKB
+pub const F_TRANSCODEKEY = 75;
+
+///file being written to a by single writer... if throttling enabled, writes
+///may be broken into smaller chunks with throttling in between
+pub const F_SINGLE_WRITER = 76;
+
+///Get the protection version number for this filesystem
+pub const F_GETPROTECTIONLEVEL = 77;
+
+///Add detached code signatures (used by dyld for shared libs)
+pub const F_FINDSIGS = 78;
+
+///Add signature from same file, only if it is signed by Apple (used by dyld for simulator)
+pub const F_ADDFILESIGS_FOR_DYLD_SIM = 83;
+
+///fsync + issue barrier to drive
+pub const F_BARRIERFSYNC = 85;
+
+///Add signature from same file, return end offset in structure on success
+pub const F_ADDFILESIGS_RETURN = 97;
+
+///Check if Library Validation allows this Mach-O file to be mapped into the calling process
+pub const F_CHECK_LV = 98;
+
+///Deallocate a range of the file
+pub const F_PUNCHHOLE = 99;
+
+///Trim an active file
+pub const F_TRIM_ACTIVE_FILE = 100;
+
+pub const FCNTL_FS_SPECIFIC_BASE = 0x00010000;
+
+///mark the dup with FD_CLOEXEC
+pub const F_DUPFD_CLOEXEC = 67;
+
+///close-on-exec flag
+pub const FD_CLOEXEC = 1;
+
+/// shared or read lock
+pub const F_RDLCK = 1;
+
+/// unlock
+pub const F_UNLCK = 2;
+
+/// exclusive or write lock
+pub const F_WRLCK = 3;
lib/std/c.zig
@@ -121,6 +121,7 @@ pub extern "c" fn sysctlbyname(name: [*:0]const u8, oldp: ?*c_void, oldlenp: ?*u
 pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
 pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
 pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
+pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int;
 
 pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
 pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
lib/std/os.zig
@@ -2529,15 +2529,8 @@ pub fn pipe() PipeError![2]fd_t {
 
 pub fn pipe2(flags: u32) PipeError![2]fd_t {
     if (comptime std.Target.current.isDarwin()) {
-        var fds: [2]fd_t = undefined;
-        switch (errno(system.pipe(&fds, flags))) {
-            0 => {},
-            EINVAL => unreachable, // Invalid flags
-            EFAULT => unreachable, // Invalid fds pointer
-            ENFILE => return error.SystemFdQuotaExceeded,
-            EMFILE => return error.ProcessFdQuotaExceeded,
-            else => |err| return unexpectedErrno(err),
-        }
+        var fds: [2]fd_t = try pipe();
+        if (flags == 0) return fds;
         errdefer {
             close(fds[0]);
             close(fds[1]);