Commit 71873e7133

Andrew Kelley <andrew@ziglang.org>
2020-02-07 17:28:42
implement os.pipe2 for darwin
1 parent 0b5bcd2
Changed files (2)
lib
lib/std/fs.zig
@@ -817,7 +817,10 @@ pub const Dir = struct {
     ) File.OpenError!File {
         const w = os.windows;
 
-        var result = File{ .handle = undefined };
+        var result = File{
+            .handle = undefined,
+            .io_mode = .blocking,
+        };
 
         const path_len_bytes = math.cast(u16, mem.toSliceConst(u16, sub_path_w).len * 2) catch |err| switch (err) {
             error.Overflow => return error.NameTooLong,
lib/std/os.zig
@@ -2528,6 +2528,29 @@ 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),
+        }
+        errdefer {
+            close(fds[0]);
+            close(fds[1]);
+        }
+        for (fds) |fd| switch (errno(system.fcntl(fd, F_SETFL, flags))) {
+            0 => {},
+            EINVAL => unreachable, // Invalid flags
+            EBADF => unreachable, // Always a race condition
+            else => |err| return unexpectedErrno(err),
+        };
+        return fds;
+    }
+
     var fds: [2]fd_t = undefined;
     switch (errno(system.pipe2(&fds, flags))) {
         0 => return fds,