authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 21:29:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 21:29:29-04:00
log3dce41b61a20c23c494a485b1d3092e7c67dd97f
treec5aee6a9c66bdfd672134885fb3001c9aee5c7c6
parentbf7b6fbbdb7d28c0d7dba3e17c46ce156712cfc8
signaturelock-open Commit is signed but in an unrecognized format.

improvements to std lib for event-based I/O


8 files changed, 219 insertions(+), 50 deletions(-)

std/c.zig+1-1
......@@ -64,6 +64,7 @@ pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int;
6464pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int;
6565pub extern "c" fn lseek(fd: fd_t, offset: isize, whence: c_int) isize;
6666pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int;
67pub extern "c" fn openat(fd: c_int, path: [*]const u8, oflag: c_uint, ...) c_int;
6768pub extern "c" fn raise(sig: c_int) c_int;
6869pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize;
6970pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize;
......@@ -112,7 +113,6 @@ pub extern "c" fn accept4(sockfd: fd_t, addr: *sockaddr, addrlen: *socklen_t, fl
112113pub extern "c" fn getsockopt(sockfd: fd_t, level: c_int, optname: c_int, optval: *c_void, optlen: *socklen_t) c_int;
113114pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;
114115pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
115pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int;
116116pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int;
117117pub extern "c" fn setuid(uid: c_uint) c_int;
118118pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
std/event/fs.zig+89-46
......@@ -23,6 +23,7 @@ pub const Request = struct {
2323 };
2424
2525 pub const Msg = union(enum) {
26 WriteV: WriteV,
2627 PWriteV: PWriteV,
2728 PReadV: PReadV,
2829 Open: Open,
......@@ -30,6 +31,14 @@ pub const Request = struct {
3031 WriteFile: WriteFile,
3132 End, // special - means the fs thread should exit
3233
34 pub const WriteV = struct {
35 fd: fd_t,
36 iov: []const os.iovec_const,
37 result: Error!void,
38
39 pub const Error = os.WriteError;
40 };
41
3342 pub const PWriteV = struct {
3443 fd: fd_t,
3544 iov: []const os.iovec_const,
......@@ -77,7 +86,7 @@ pub const Request = struct {
7786pub const PWriteVError = error{OutOfMemory} || File.WriteError;
7887
7988/// data - just the inner references - must live until pwritev frame completes.
80pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) PWriteVError!void {
89pub fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) PWriteVError!void {
8190 switch (builtin.os) {
8291 .macosx,
8392 .linux,
......@@ -94,31 +103,31 @@ pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: us
94103 };
95104 }
96105
97 return await (async pwritevPosix(loop, fd, iovecs, offset) catch unreachable);
106 return pwritevPosix(loop, fd, iovecs, offset);
98107 },
99108 .windows => {
100109 const data_copy = try std.mem.dupe(loop.allocator, []const u8, data);
101110 defer loop.allocator.free(data_copy);
102 return await (async pwritevWindows(loop, fd, data, offset) catch unreachable);
111 return pwritevWindows(loop, fd, data, offset);
103112 },
104113 else => @compileError("Unsupported OS"),
105114 }
106115}
107116
108117/// data must outlive the returned frame
109pub async fn pwritevWindows(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) os.WindowsWriteError!void {
118pub fn pwritevWindows(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) os.WindowsWriteError!void {
110119 if (data.len == 0) return;
111 if (data.len == 1) return await (async pwriteWindows(loop, fd, data[0], offset) catch unreachable);
120 if (data.len == 1) return pwriteWindows(loop, fd, data[0], offset);
112121
113122 // TODO do these in parallel
114123 var off = offset;
115124 for (data) |buf| {
116 try await (async pwriteWindows(loop, fd, buf, off) catch unreachable);
125 try pwriteWindows(loop, fd, buf, off);
117126 off += buf.len;
118127 }
119128}
120129
121pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) os.WindowsWriteError!void {
130pub fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) os.WindowsWriteError!void {
122131 var resume_node = Loop.ResumeNode.Basic{
123132 .base = Loop.ResumeNode{
124133 .id = Loop.ResumeNode.Id.Basic,
......@@ -158,7 +167,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64)
158167}
159168
160169/// iovecs must live until pwritev frame completes.
161pub async fn pwritevPosix(
170pub fn pwritevPosix(
162171 loop: *Loop,
163172 fd: fd_t,
164173 iovecs: []const os.iovec_const,
......@@ -195,10 +204,44 @@ pub async fn pwritevPosix(
195204 return req_node.data.msg.PWriteV.result;
196205}
197206
207/// iovecs must live until pwritev frame completes.
208pub fn writevPosix(
209 loop: *Loop,
210 fd: fd_t,
211 iovecs: []const os.iovec_const,
212) os.WriteError!void {
213 var req_node = RequestNode{
214 .prev = null,
215 .next = null,
216 .data = Request{
217 .msg = Request.Msg{
218 .WriteV = Request.Msg.WriteV{
219 .fd = fd,
220 .iov = iovecs,
221 .result = undefined,
222 },
223 },
224 .finish = Request.Finish{
225 .TickNode = Loop.NextTickNode{
226 .prev = null,
227 .next = null,
228 .data = @frame(),
229 },
230 },
231 },
232 };
233
234 suspend {
235 loop.posixFsRequest(&req_node);
236 }
237
238 return req_node.data.msg.WriteV.result;
239}
240
198241pub const PReadVError = error{OutOfMemory} || File.ReadError;
199242
200243/// data - just the inner references - must live until preadv frame completes.
201pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PReadVError!usize {
244pub fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PReadVError!usize {
202245 assert(data.len != 0);
203246 switch (builtin.os) {
204247 .macosx,
......@@ -216,21 +259,21 @@ pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PR
216259 };
217260 }
218261
219 return await (async preadvPosix(loop, fd, iovecs, offset) catch unreachable);
262 return preadvPosix(loop, fd, iovecs, offset);
220263 },
221264 .windows => {
222265 const data_copy = try std.mem.dupe(loop.allocator, []u8, data);
223266 defer loop.allocator.free(data_copy);
224 return await (async preadvWindows(loop, fd, data_copy, offset) catch unreachable);
267 return preadvWindows(loop, fd, data_copy, offset);
225268 },
226269 else => @compileError("Unsupported OS"),
227270 }
228271}
229272
230273/// data must outlive the returned frame
231pub async fn preadvWindows(loop: *Loop, fd: fd_t, data: []const []u8, offset: u64) !usize {
274pub fn preadvWindows(loop: *Loop, fd: fd_t, data: []const []u8, offset: u64) !usize {
232275 assert(data.len != 0);
233 if (data.len == 1) return await (async preadWindows(loop, fd, data[0], offset) catch unreachable);
276 if (data.len == 1) return preadWindows(loop, fd, data[0], offset);
234277
235278 // TODO do these in parallel?
236279 var off: usize = 0;
......@@ -238,7 +281,7 @@ pub async fn preadvWindows(loop: *Loop, fd: fd_t, data: []const []u8, offset: u6
238281 var inner_off: usize = 0;
239282 while (true) {
240283 const v = data[iov_i];
241 const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len - inner_off], offset + off) catch unreachable);
284 const amt_read = try preadWindows(loop, fd, v[inner_off .. v.len - inner_off], offset + off);
242285 off += amt_read;
243286 inner_off += amt_read;
244287 if (inner_off == v.len) {
......@@ -252,7 +295,7 @@ pub async fn preadvWindows(loop: *Loop, fd: fd_t, data: []const []u8, offset: u6
252295 }
253296}
254297
255pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize {
298pub fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize {
256299 var resume_node = Loop.ResumeNode.Basic{
257300 .base = Loop.ResumeNode{
258301 .id = Loop.ResumeNode.Id.Basic,
......@@ -291,7 +334,7 @@ pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize
291334}
292335
293336/// iovecs must live until preadv frame completes
294pub async fn preadvPosix(
337pub fn preadvPosix(
295338 loop: *Loop,
296339 fd: fd_t,
297340 iovecs: []const os.iovec,
......@@ -328,7 +371,7 @@ pub async fn preadvPosix(
328371 return req_node.data.msg.PReadV.result;
329372}
330373
331pub async fn openPosix(
374pub fn openPosix(
332375 loop: *Loop,
333376 path: []const u8,
334377 flags: u32,
......@@ -367,11 +410,11 @@ pub async fn openPosix(
367410 return req_node.data.msg.Open.result;
368411}
369412
370pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t {
413pub fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t {
371414 switch (builtin.os) {
372415 .macosx, .linux, .freebsd, .netbsd => {
373416 const flags = os.O_LARGEFILE | os.O_RDONLY | os.O_CLOEXEC;
374 return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable);
417 return openPosix(loop, path, flags, File.default_mode);
375418 },
376419
377420 .windows => return windows.CreateFile(
......@@ -390,12 +433,12 @@ pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t {
390433
391434/// Creates if does not exist. Truncates the file if it exists.
392435/// Uses the default mode.
393pub async fn openWrite(loop: *Loop, path: []const u8) File.OpenError!fd_t {
394 return await (async openWriteMode(loop, path, File.default_mode) catch unreachable);
436pub fn openWrite(loop: *Loop, path: []const u8) File.OpenError!fd_t {
437 return openWriteMode(loop, path, File.default_mode);
395438}
396439
397440/// Creates if does not exist. Truncates the file if it exists.
398pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.OpenError!fd_t {
441pub fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.OpenError!fd_t {
399442 switch (builtin.os) {
400443 .macosx,
401444 .linux,
......@@ -403,7 +446,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.
403446 .netbsd,
404447 => {
405448 const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC;
406 return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable);
449 return openPosix(loop, path, flags, File.default_mode);
407450 },
408451 .windows => return windows.CreateFile(
409452 path,
......@@ -419,7 +462,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.
419462}
420463
421464/// Creates if does not exist. Does not truncate.
422pub async fn openReadWrite(
465pub fn openReadWrite(
423466 loop: *Loop,
424467 path: []const u8,
425468 mode: File.Mode,
......@@ -427,7 +470,7 @@ pub async fn openReadWrite(
427470 switch (builtin.os) {
428471 .macosx, .linux, .freebsd, .netbsd => {
429472 const flags = os.O_LARGEFILE | os.O_RDWR | os.O_CREAT | os.O_CLOEXEC;
430 return await (async openPosix(loop, path, flags, mode) catch unreachable);
473 return openPosix(loop, path, flags, mode);
431474 },
432475
433476 .windows => return windows.CreateFile(
......@@ -576,24 +619,24 @@ pub const CloseOperation = struct {
576619
577620/// contents must remain alive until writeFile completes.
578621/// TODO make this atomic or provide writeFileAtomic and rename this one to writeFileTruncate
579pub async fn writeFile(loop: *Loop, path: []const u8, contents: []const u8) !void {
580 return await (async writeFileMode(loop, path, contents, File.default_mode) catch unreachable);
622pub fn writeFile(loop: *Loop, path: []const u8, contents: []const u8) !void {
623 return writeFileMode(loop, path, contents, File.default_mode);
581624}
582625
583626/// contents must remain alive until writeFile completes.
584pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void {
627pub fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void {
585628 switch (builtin.os) {
586629 .linux,
587630 .macosx,
588631 .freebsd,
589632 .netbsd,
590 => return await (async writeFileModeThread(loop, path, contents, mode) catch unreachable),
591 .windows => return await (async writeFileWindows(loop, path, contents) catch unreachable),
633 => return writeFileModeThread(loop, path, contents, mode),
634 .windows => return writeFileWindows(loop, path, contents),
592635 else => @compileError("Unsupported OS"),
593636 }
594637}
595638
596async fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) !void {
639fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) !void {
597640 const handle = try windows.CreateFile(
598641 path,
599642 windows.GENERIC_WRITE,
......@@ -605,10 +648,10 @@ async fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) !
605648 );
606649 defer os.close(handle);
607650
608 try await (async pwriteWindows(loop, handle, contents, 0) catch unreachable);
651 try pwriteWindows(loop, handle, contents, 0);
609652}
610653
611async fn writeFileModeThread(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void {
654fn writeFileModeThread(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void {
612655 const path_with_null = try std.cstr.addNullByte(loop.allocator, path);
613656 defer loop.allocator.free(path_with_null);
614657
......@@ -646,11 +689,11 @@ async fn writeFileModeThread(loop: *Loop, path: []const u8, contents: []const u8
646689/// The frame resumes when the last data has been confirmed written, but before the file handle
647690/// is closed.
648691/// Caller owns returned memory.
649pub async fn readFile(loop: *Loop, file_path: []const u8, max_size: usize) ![]u8 {
692pub fn readFile(loop: *Loop, file_path: []const u8, max_size: usize) ![]u8 {
650693 var close_op = try CloseOperation.start(loop);
651694 defer close_op.finish();
652695
653 const fd = try await (async openRead(loop, file_path) catch unreachable);
696 const fd = try openRead(loop, file_path);
654697 close_op.setHandle(fd);
655698
656699 var list = std.ArrayList(u8).init(loop.allocator);
......@@ -660,7 +703,7 @@ pub async fn readFile(loop: *Loop, file_path: []const u8, max_size: usize) ![]u8
660703 try list.ensureCapacity(list.len + mem.page_size);
661704 const buf = list.items[list.len..];
662705 const buf_array = [_][]u8{buf};
663 const amt = try await (async preadv(loop, fd, buf_array, list.len) catch unreachable);
706 const amt = try preadv(loop, fd, buf_array, list.len);
664707 list.len += amt;
665708 if (list.len > max_size) {
666709 return error.FileTooBig;
......@@ -1273,11 +1316,11 @@ const test_tmp_dir = "std_event_fs_test";
12731316// return result;
12741317//}
12751318
1276async fn testFsWatchCantFail(loop: *Loop, result: *(anyerror!void)) void {
1277 result.* = await (async testFsWatch(loop) catch unreachable);
1319fn testFsWatchCantFail(loop: *Loop, result: *(anyerror!void)) void {
1320 result.* = testFsWatch(loop);
12781321}
12791322
1280async fn testFsWatch(loop: *Loop) !void {
1323fn testFsWatch(loop: *Loop) !void {
12811324 const file_path = try std.fs.path.join(loop.allocator, [][]const u8{ test_tmp_dir, "file.txt" });
12821325 defer loop.allocator.free(file_path);
12831326
......@@ -1288,27 +1331,27 @@ async fn testFsWatch(loop: *Loop) !void {
12881331 const line2_offset = 7;
12891332
12901333 // first just write then read the file
1291 try await try async writeFile(loop, file_path, contents);
1334 try writeFile(loop, file_path, contents);
12921335
1293 const read_contents = try await try async readFile(loop, file_path, 1024 * 1024);
1336 const read_contents = try readFile(loop, file_path, 1024 * 1024);
12941337 testing.expectEqualSlices(u8, contents, read_contents);
12951338
12961339 // now watch the file
12971340 var watch = try Watch(void).create(loop, 0);
12981341 defer watch.destroy();
12991342
1300 testing.expect((try await try async watch.addFile(file_path, {})) == null);
1343 testing.expect((try watch.addFile(file_path, {})) == null);
13011344
1302 const ev = try async watch.channel.get();
1345 const ev = async watch.channel.get();
13031346 var ev_consumed = false;
13041347 defer if (!ev_consumed) await ev;
13051348
13061349 // overwrite line 2
1307 const fd = try await try async openReadWrite(loop, file_path, File.default_mode);
1350 const fd = try await openReadWrite(loop, file_path, File.default_mode);
13081351 {
13091352 defer os.close(fd);
13101353
1311 try await try async pwritev(loop, fd, []const []const u8{"lorem ipsum"}, line2_offset);
1354 try pwritev(loop, fd, []const []const u8{"lorem ipsum"}, line2_offset);
13121355 }
13131356
13141357 ev_consumed = true;
......@@ -1316,7 +1359,7 @@ async fn testFsWatch(loop: *Loop) !void {
13161359 WatchEventId.CloseWrite => {},
13171360 WatchEventId.Delete => @panic("wrong event"),
13181361 }
1319 const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024);
1362 const contents_updated = try readFile(loop, file_path, 1024 * 1024);
13201363 testing.expectEqualSlices(u8,
13211364 \\line 1
13221365 \\lorem ipsum
std/event/loop.zig+6
......@@ -89,12 +89,15 @@ pub const Loop = struct {
8989 pub const IoMode = enum {
9090 blocking,
9191 evented,
92 mixed,
9293 };
9394 pub const io_mode: IoMode = if (@hasDecl(root, "io_mode")) root.io_mode else IoMode.blocking;
9495 var global_instance_state: Loop = undefined;
96 threadlocal var per_thread_instance: ?*Loop = null;
9597 const default_instance: ?*Loop = switch (io_mode) {
9698 .blocking => null,
9799 .evented => &global_instance_state,
100 .mixed => per_thread_instance,
98101 };
99102 pub const instance: ?*Loop = if (@hasDecl(root, "event_loop")) root.event_loop else default_instance;
100103
......@@ -796,6 +799,9 @@ pub const Loop = struct {
796799 while (self.os_data.fs_queue.get()) |node| {
797800 switch (node.data.msg) {
798801 .End => return,
802 .WriteV => |*msg| {
803 msg.result = os.writev(msg.fd, msg.iov);
804 },
799805 .PWriteV => |*msg| {
800806 msg.result = os.pwritev(msg.fd, msg.iov, msg.offset);
801807 },
std/fs.zig+13
......@@ -442,6 +442,8 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!
442442 }
443443}
444444
445/// TODO: separate this API into the one that opens directory handles to then subsequently open
446/// files, and into the one that reads files from an open directory handle.
445447pub const Dir = struct {
446448 handle: Handle,
447449 allocator: *Allocator,
......@@ -564,6 +566,17 @@ pub const Dir = struct {
564566 }
565567 }
566568
569 pub fn openRead(self: Dir, file_path: []const u8) os.OpenError!File {
570 const path_c = try os.toPosixPath(file_path);
571 return self.openReadC(&path_c);
572 }
573
574 pub fn openReadC(self: Dir, file_path: [*]const u8) OpenError!File {
575 const flags = os.O_LARGEFILE | os.O_RDONLY;
576 const fd = try os.openatC(self.handle.fd, file_path, flags, 0);
577 return File.openHandle(fd);
578 }
579
567580 fn nextDarwin(self: *Dir) !?Entry {
568581 start_over: while (true) {
569582 if (self.handle.index >= self.handle.end_index) {
std/fs/file.zig+8
......@@ -302,6 +302,14 @@ pub const File = struct {
302302 return os.write(self.handle, bytes);
303303 }
304304
305 pub fn writev_iovec(self: File, iovecs: []const os.iovec_const) WriteError!void {
306 if (std.event.Loop.instance) |loop| {
307 return std.event.fs.writevPosix(loop, self.handle, iovecs);
308 } else {
309 return os.writev(self.handle, iovecs);
310 }
311 }
312
305313 pub fn inStream(file: File) InStream {
306314 return InStream{
307315 .file = file,
std/net.zig+30
......@@ -215,3 +215,33 @@ test "std.net.parseIp6" {
215215 assert(addr.addr[1] == 0x01);
216216 assert(addr.addr[2] == 0x00);
217217}
218
219pub fn connectUnixSocket(path: []const u8) !std.fs.File {
220 const opt_non_block = if (std.event.Loop.instance != null) os.SOCK_NONBLOCK else 0;
221 const sockfd = try os.socket(
222 os.AF_UNIX,
223 os.SOCK_STREAM | os.SOCK_CLOEXEC | opt_non_block,
224 0,
225 );
226 errdefer os.close(sockfd);
227
228 var sock_addr = os.sockaddr{
229 .un = os.sockaddr_un{
230 .family = os.AF_UNIX,
231 .path = undefined,
232 },
233 };
234
235 if (path.len > @typeOf(sock_addr.un.path).len) return error.NameTooLong;
236 mem.copy(u8, sock_addr.un.path[0..], path);
237 const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len);
238 if (std.event.Loop.instance) |loop| {
239 try os.connect_async(sockfd, &sock_addr, size);
240 try loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET);
241 try os.getsockoptError(sockfd);
242 } else {
243 try os.connect(sockfd, &sock_addr, size);
244 }
245
246 return std.fs.File.openHandle(sockfd);
247}
std/os.zig+71-3
......@@ -440,6 +440,33 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void {
440440
441441/// Write multiple buffers to a file descriptor. Keeps trying if it gets interrupted.
442442/// This function is for blocking file descriptors only. For non-blocking, see
443/// `writevAsync`.
444pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!void {
445 while (true) {
446 // TODO handle the case when iov_len is too large and get rid of this @intCast
447 const rc = system.writev(fd, iov.ptr, @intCast(u32, iov.len));
448 switch (errno(rc)) {
449 0 => return,
450 EINTR => continue,
451 EINVAL => unreachable,
452 EFAULT => unreachable,
453 EAGAIN => unreachable, // This function is for blocking writes.
454 EBADF => unreachable, // Always a race condition.
455 EDESTADDRREQ => unreachable, // `connect` was never called.
456 EDQUOT => return error.DiskQuota,
457 EFBIG => return error.FileTooBig,
458 EIO => return error.InputOutput,
459 ENOSPC => return error.NoSpaceLeft,
460 EPERM => return error.AccessDenied,
461 EPIPE => return error.BrokenPipe,
462 else => |err| return unexpectedErrno(err),
463 }
464 }
465}
466
467/// Write multiple buffers to a file descriptor, with a position offset.
468/// Keeps trying if it gets interrupted.
469/// This function is for blocking file descriptors only. For non-blocking, see
443470/// `pwritevAsync`.
444471pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void {
445472 if (darwin.is_the_target) {
......@@ -524,7 +551,6 @@ pub const OpenError = error{
524551};
525552
526553/// Open and possibly create a file. Keeps trying if it gets interrupted.
527/// `file_path` needs to be copied in memory to add a null terminating byte.
528554/// See also `openC`.
529555pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t {
530556 const file_path_c = try toPosixPath(file_path);
......@@ -564,6 +590,47 @@ pub fn openC(file_path: [*]const u8, flags: u32, perm: usize) OpenError!fd_t {
564590 }
565591}
566592
593/// Open and possibly create a file. Keeps trying if it gets interrupted.
594/// `file_path` is relative to the open directory handle `dir_fd`.
595/// See also `openatC`.
596pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: usize) OpenError!fd_t {
597 const file_path_c = try toPosixPath(file_path);
598 return openatC(dir_fd, &file_path_c, flags, mode);
599}
600
601/// Open and possibly create a file. Keeps trying if it gets interrupted.
602/// `file_path` is relative to the open directory handle `dir_fd`.
603/// See also `openat`.
604pub fn openatC(dir_fd: fd_t, file_path: [*]const u8, flags: u32, mode: usize) OpenError!fd_t {
605 while (true) {
606 const rc = system.openat(dir_fd, file_path, flags, mode);
607 switch (errno(rc)) {
608 0 => return @intCast(fd_t, rc),
609 EINTR => continue,
610
611 EFAULT => unreachable,
612 EINVAL => unreachable,
613 EACCES => return error.AccessDenied,
614 EFBIG => return error.FileTooBig,
615 EOVERFLOW => return error.FileTooBig,
616 EISDIR => return error.IsDir,
617 ELOOP => return error.SymLinkLoop,
618 EMFILE => return error.ProcessFdQuotaExceeded,
619 ENAMETOOLONG => return error.NameTooLong,
620 ENFILE => return error.SystemFdQuotaExceeded,
621 ENODEV => return error.NoDevice,
622 ENOENT => return error.FileNotFound,
623 ENOMEM => return error.SystemResources,
624 ENOSPC => return error.NoSpaceLeft,
625 ENOTDIR => return error.NotDir,
626 EPERM => return error.AccessDenied,
627 EEXIST => return error.PathAlreadyExists,
628 EBUSY => return error.DeviceBusy,
629 else => |err| return unexpectedErrno(err),
630 }
631 }
632}
633
567634pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
568635 while (true) {
569636 switch (errno(system.dup2(old_fd, new_fd))) {
......@@ -1655,7 +1722,7 @@ pub const ConnectError = error{
16551722/// For non-blocking, see `connect_async`.
16561723pub fn connect(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void {
16571724 while (true) {
1658 switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) {
1725 switch (errno(system.connect(sockfd, sock_addr, len))) {
16591726 0 => return,
16601727 EACCES => return error.PermissionDenied,
16611728 EPERM => return error.PermissionDenied,
......@@ -1683,7 +1750,8 @@ pub fn connect(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!v
16831750/// It expects to receive EINPROGRESS`.
16841751pub fn connect_async(sockfd: i32, sock_addr: *sockaddr, len: socklen_t) ConnectError!void {
16851752 while (true) {
1686 switch (errno(system.connect(sockfd, sock_addr, @sizeOf(sockaddr)))) {
1753 switch (errno(system.connect(sockfd, sock_addr, len))) {
1754 EINVAL => unreachable,
16871755 EINTR => continue,
16881756 0, EINPROGRESS => return,
16891757 EACCES => return error.PermissionDenied,
std/os/bits/linux.zig+1
......@@ -783,6 +783,7 @@ pub const socklen_t = u32;
783783pub const sockaddr = extern union {
784784 in: sockaddr_in,
785785 in6: sockaddr_in6,
786 un: sockaddr_un,
786787};
787788
788789pub const sockaddr_in = extern struct {