authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-30 17:23:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-30 17:28:35-04:00
log9d4eaf1e07525a72fa54cfaa346a18bf18953af4
treeccac84631127e309d5fb500c4407e8e40c444aff
parentba78ae0ae73ac38a2bb32c618cd145b4d2f9602e
signaturelock-open Commit is signed but in an unrecognized format.

update std lib API for I/O

std.io.FileInStream -> std.os.File.InStream std.io.FileInStream.init(file) -> file.inStream() std.io.FileOutStream -> std.os.File.OutStream std.io.FileOutStream.init(file) -> file.outStream() remove a lot of error code possibilities from os functions std.event.net.socketRead -> std.event.net.read std.event.net.socketWrite -> std.event.net.write add std.event.net.readv add std.event.net.writev add std.event.net.readvPosix add std.event.net.writevPosix add std.event.net.OutStream add std.event.net.InStream add std.event.io.InStream add std.event.io.OutStream

28 files changed, 437 insertions(+), 257 deletions(-)

CMakeLists.txt+1
......@@ -470,6 +470,7 @@ set(ZIG_STD_FILES
470470 "event/fs.zig"
471471 "event/future.zig"
472472 "event/group.zig"
473 "event/io.zig"
473474 "event/lock.zig"
474475 "event/locked.zig"
475476 "event/loop.zig"
doc/docgen.zig+3-3
......@@ -41,12 +41,12 @@ pub fn main() !void {
4141 var out_file = try os.File.openWrite(out_file_name);
4242 defer out_file.close();
4343
44 var file_in_stream = io.FileInStream.init(in_file);
44 var file_in_stream = in_file.inStream();
4545
4646 const input_file_bytes = try file_in_stream.stream.readAllAlloc(allocator, max_doc_file_size);
4747
48 var file_out_stream = io.FileOutStream.init(out_file);
49 var buffered_out_stream = io.BufferedOutStream(io.FileOutStream.Error).init(&file_out_stream.stream);
48 var file_out_stream = out_file.outStream();
49 var buffered_out_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream);
5050
5151 var tokenizer = Tokenizer.init(in_file_name, input_file_bytes);
5252 var toc = try genToc(allocator, &tokenizer);
example/guess_number/main.zig+1-2
......@@ -6,8 +6,7 @@ const os = std.os;
66
77pub fn main() !void {
88 var stdout_file = try io.getStdOut();
9 var stdout_file_stream = io.FileOutStream.init(stdout_file);
10 const stdout = &stdout_file_stream.stream;
9 const stdout = &stdout_file.outStream().stream;
1110
1211 try stdout.print("Welcome to the Guess Number Game in Zig.\n");
1312
src-self-hosted/errmsg.zig+1-1
......@@ -278,7 +278,7 @@ pub const Msg = struct {
278278 Color.On => true,
279279 Color.Off => false,
280280 };
281 var stream = &std.io.FileOutStream.init(file).stream;
281 var stream = &file.outStream().stream;
282282 return msg.printToStream(stream, color_on);
283283 }
284284};
src-self-hosted/libc_installation.zig+2-2
......@@ -30,7 +30,7 @@ pub const LibCInstallation = struct {
3030 self: *LibCInstallation,
3131 allocator: *std.mem.Allocator,
3232 libc_file: []const u8,
33 stderr: *std.io.OutStream(std.io.FileOutStream.Error),
33 stderr: *std.io.OutStream(std.os.File.WriteError),
3434 ) !void {
3535 self.initEmpty();
3636
......@@ -100,7 +100,7 @@ pub const LibCInstallation = struct {
100100 }
101101 }
102102
103 pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(std.io.FileOutStream.Error)) !void {
103 pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(std.os.File.WriteError)) !void {
104104 @setEvalBranchQuota(4000);
105105 try out.print(
106106 \\# The directory that contains `stdlib.h`.
src-self-hosted/main.zig+5-5
......@@ -21,8 +21,8 @@ const errmsg = @import("errmsg.zig");
2121const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
2222
2323var stderr_file: os.File = undefined;
24var stderr: *io.OutStream(io.FileOutStream.Error) = undefined;
25var stdout: *io.OutStream(io.FileOutStream.Error) = undefined;
24var stderr: *io.OutStream(os.File.WriteError) = undefined;
25var stdout: *io.OutStream(os.File.WriteError) = undefined;
2626
2727const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
2828
......@@ -55,11 +55,11 @@ pub fn main() !void {
5555 const allocator = std.heap.c_allocator;
5656
5757 var stdout_file = try std.io.getStdOut();
58 var stdout_out_stream = std.io.FileOutStream.init(stdout_file);
58 var stdout_out_stream = stdout_file.outStream();
5959 stdout = &stdout_out_stream.stream;
6060
6161 stderr_file = try std.io.getStdErr();
62 var stderr_out_stream = std.io.FileOutStream.init(stderr_file);
62 var stderr_out_stream = stderr_file.outStream();
6363 stderr = &stderr_out_stream.stream;
6464
6565 const args = try os.argsAlloc(allocator);
......@@ -619,7 +619,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
619619 }
620620
621621 var stdin_file = try io.getStdIn();
622 var stdin = io.FileInStream.init(stdin_file);
622 var stdin = stdin_file.inStream();
623623
624624 const source_code = try stdin.stream.readAllAlloc(allocator, max_src_size);
625625 defer allocator.free(source_code);
std/atomic/queue.zig+1-1
......@@ -114,7 +114,7 @@ pub fn Queue(comptime T: type) type {
114114
115115 fn dumpRecursive(optional_node: ?*Node, indent: usize) void {
116116 var stderr_file = std.io.getStdErr() catch return;
117 const stderr = &std.io.FileOutStream.init(stderr_file).stream;
117 const stderr = &stderr_file.outStream().stream;
118118 stderr.writeByteNTimes(' ', indent) catch return;
119119 if (optional_node) |node| {
120120 std.debug.warn("0x{x}={}\n", @ptrToInt(node), node.data);
std/coff.zig+4-4
......@@ -41,7 +41,7 @@ pub const Coff = struct {
4141 pub fn loadHeader(self: *Coff) !void {
4242 const pe_pointer_offset = 0x3C;
4343
44 var file_stream = io.FileInStream.init(self.in_file);
44 var file_stream = self.in_file.inStream();
4545 const in = &file_stream.stream;
4646
4747 var magic: [2]u8 = undefined;
......@@ -77,7 +77,7 @@ pub const Coff = struct {
7777 try self.loadOptionalHeader(&file_stream);
7878 }
7979
80 fn loadOptionalHeader(self: *Coff, file_stream: *io.FileInStream) !void {
80 fn loadOptionalHeader(self: *Coff, file_stream: *os.File.InStream) !void {
8181 const in = &file_stream.stream;
8282 self.pe_header.magic = try in.readIntLe(u16);
8383 // For now we're only interested in finding the reference to the .pdb,
......@@ -115,7 +115,7 @@ pub const Coff = struct {
115115 const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data;
116116 try self.in_file.seekTo(file_offset + debug_dir.size);
117117
118 var file_stream = io.FileInStream.init(self.in_file);
118 var file_stream = self.in_file.inStream();
119119 const in = &file_stream.stream;
120120
121121 var cv_signature: [4]u8 = undefined; // CodeView signature
......@@ -146,7 +146,7 @@ pub const Coff = struct {
146146
147147 self.sections = ArrayList(Section).init(self.allocator);
148148
149 var file_stream = io.FileInStream.init(self.in_file);
149 var file_stream = self.in_file.inStream();
150150 const in = &file_stream.stream;
151151
152152 var name: [8]u8 = undefined;
std/crypto/throughput_test.zig+1-1
......@@ -130,7 +130,7 @@ fn printPad(stdout: var, s: []const u8) !void {
130130
131131pub fn main() !void {
132132 var stdout_file = try std.io.getStdOut();
133 var stdout_out_stream = std.io.FileOutStream.init(stdout_file);
133 var stdout_out_stream = stdout_file.outStream();
134134 const stdout = &stdout_out_stream.stream;
135135
136136 var buffer: [1024]u8 = undefined;
std/debug/index.zig+11-11
......@@ -34,10 +34,10 @@ const Module = struct {
3434/// Tries to write to stderr, unbuffered, and ignores any error returned.
3535/// Does not append a newline.
3636var stderr_file: os.File = undefined;
37var stderr_file_out_stream: io.FileOutStream = undefined;
37var stderr_file_out_stream: os.File.OutStream = undefined;
3838
3939/// TODO multithreaded awareness
40var stderr_stream: ?*io.OutStream(io.FileOutStream.Error) = null;
40var stderr_stream: ?*io.OutStream(os.File.WriteError) = null;
4141var stderr_mutex = std.Mutex.init();
4242pub fn warn(comptime fmt: []const u8, args: ...) void {
4343 const held = stderr_mutex.acquire();
......@@ -46,12 +46,12 @@ pub fn warn(comptime fmt: []const u8, args: ...) void {
4646 stderr.print(fmt, args) catch return;
4747}
4848
49pub fn getStderrStream() !*io.OutStream(io.FileOutStream.Error) {
49pub fn getStderrStream() !*io.OutStream(os.File.WriteError) {
5050 if (stderr_stream) |st| {
5151 return st;
5252 } else {
5353 stderr_file = try io.getStdErr();
54 stderr_file_out_stream = io.FileOutStream.init(stderr_file);
54 stderr_file_out_stream = stderr_file.outStream();
5555 const st = &stderr_file_out_stream.stream;
5656 stderr_stream = st;
5757 return st;
......@@ -876,7 +876,7 @@ fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DebugInfo {
876876}
877877
878878pub fn findElfSection(elf: *Elf, name: []const u8) ?*elf.Shdr {
879 var file_stream = io.FileInStream.init(elf.in_file);
879 var file_stream = elf.in_file.inStream();
880880 const in = &file_stream.stream;
881881
882882 section_loop: for (elf.section_headers) |*elf_section| {
......@@ -1068,7 +1068,7 @@ pub const DebugInfo = switch (builtin.os) {
10681068 }
10691069
10701070 pub fn readString(self: *DebugInfo) ![]u8 {
1071 var in_file_stream = io.FileInStream.init(self.self_exe_file);
1071 var in_file_stream = self.self_exe_file.inStream();
10721072 const in_stream = &in_file_stream.stream;
10731073 return readStringRaw(self.allocator(), in_stream);
10741074 }
......@@ -1405,7 +1405,7 @@ fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64
14051405
14061406fn parseAbbrevTable(st: *DebugInfo) !AbbrevTable {
14071407 const in_file = st.self_exe_file;
1408 var in_file_stream = io.FileInStream.init(in_file);
1408 var in_file_stream = in_file.inStream();
14091409 const in_stream = &in_file_stream.stream;
14101410 var result = AbbrevTable.init(st.allocator());
14111411 while (true) {
......@@ -1456,7 +1456,7 @@ fn getAbbrevTableEntry(abbrev_table: *const AbbrevTable, abbrev_code: u64) ?*con
14561456
14571457fn parseDie(st: *DebugInfo, abbrev_table: *const AbbrevTable, is_64: bool) !Die {
14581458 const in_file = st.self_exe_file;
1459 var in_file_stream = io.FileInStream.init(in_file);
1459 var in_file_stream = in_file.inStream();
14601460 const in_stream = &in_file_stream.stream;
14611461 const abbrev_code = try readULeb128(in_stream);
14621462 const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) orelse return error.InvalidDebugInfo;
......@@ -1682,7 +1682,7 @@ fn getLineNumberInfoLinux(di: *DebugInfo, compile_unit: *const CompileUnit, targ
16821682 var this_offset = di.debug_line.offset;
16831683 var this_index: usize = 0;
16841684
1685 var in_file_stream = io.FileInStream.init(in_file);
1685 var in_file_stream = in_file.inStream();
16861686 const in_stream = &in_file_stream.stream;
16871687
16881688 while (this_offset < debug_line_end) : (this_index += 1) {
......@@ -1857,7 +1857,7 @@ fn scanAllCompileUnits(st: *DebugInfo) !void {
18571857 var this_unit_offset = st.debug_info.offset;
18581858 var cu_index: usize = 0;
18591859
1860 var in_file_stream = io.FileInStream.init(st.self_exe_file);
1860 var in_file_stream = st.self_exe_file.inStream();
18611861 const in_stream = &in_file_stream.stream;
18621862
18631863 while (this_unit_offset < debug_info_end) {
......@@ -1923,7 +1923,7 @@ fn scanAllCompileUnits(st: *DebugInfo) !void {
19231923}
19241924
19251925fn findCompileUnit(st: *DebugInfo, target_address: u64) !*const CompileUnit {
1926 var in_file_stream = io.FileInStream.init(st.self_exe_file);
1926 var in_file_stream = st.self_exe_file.inStream();
19271927 const in_stream = &in_file_stream.stream;
19281928 for (st.compile_unit_list.toSlice()) |*compile_unit| {
19291929 if (compile_unit.pc_range) |range| {
std/elf.zig+2-2
......@@ -381,7 +381,7 @@ pub const Elf = struct {
381381 elf.in_file = file;
382382 elf.auto_close_stream = false;
383383
384 var file_stream = io.FileInStream.init(elf.in_file);
384 var file_stream = elf.in_file.inStream();
385385 const in = &file_stream.stream;
386386
387387 var magic: [4]u8 = undefined;
......@@ -525,7 +525,7 @@ pub const Elf = struct {
525525 }
526526
527527 pub fn findSection(elf: *Elf, name: []const u8) !?*SectionHeader {
528 var file_stream = io.FileInStream.init(elf.in_file);
528 var file_stream = elf.in_file.inStream();
529529 const in = &file_stream.stream;
530530
531531 section_loop: for (elf.section_headers) |*elf_section| {
std/event.zig+2
......@@ -6,6 +6,7 @@ pub const Locked = @import("event/locked.zig").Locked;
66pub const RwLock = @import("event/rwlock.zig").RwLock;
77pub const RwLocked = @import("event/rwlocked.zig").RwLocked;
88pub const Loop = @import("event/loop.zig").Loop;
9pub const io = @import("event/io.zig");
910pub const fs = @import("event/fs.zig");
1011pub const net = @import("event/net.zig");
1112
......@@ -14,6 +15,7 @@ test "import event tests" {
1415 _ = @import("event/fs.zig");
1516 _ = @import("event/future.zig");
1617 _ = @import("event/group.zig");
18 _ = @import("event/io.zig");
1719 _ = @import("event/lock.zig");
1820 _ = @import("event/locked.zig");
1921 _ = @import("event/rwlock.zig");
std/event/fs.zig-2
......@@ -1246,9 +1246,7 @@ pub fn Watch(comptime V: type) type {
12461246 os.linux.EPOLLET | os.linux.EPOLLIN,
12471247 ) catch unreachable)) catch |err| {
12481248 const transformed_err = switch (err) {
1249 error.InvalidFileDescriptor => unreachable,
12501249 error.FileDescriptorAlreadyPresentInSet => unreachable,
1251 error.InvalidSyscall => unreachable,
12521250 error.OperationCausesCircularLoop => unreachable,
12531251 error.FileDescriptorNotRegistered => unreachable,
12541252 error.SystemResources => error.SystemResources,
std/event/io.zig created+48
......@@ -0,0 +1,48 @@
1const std = @import("../index.zig");
2const builtin = @import("builtin");
3const Allocator = std.mem.Allocator;
4const assert = std.debug.assert;
5
6pub fn InStream(comptime ReadError: type) type {
7 return struct {
8 const Self = @This();
9 pub const Error = ReadError;
10
11 /// Return the number of bytes read. It may be less than buffer.len.
12 /// If the number of bytes read is 0, it means end of stream.
13 /// End of stream is not an error condition.
14 readFn: async<*Allocator> fn (self: *Self, buffer: []u8) Error!usize,
15
16 /// Return the number of bytes read. It may be less than buffer.len.
17 /// If the number of bytes read is 0, it means end of stream.
18 /// End of stream is not an error condition.
19 pub async fn read(self: *Self, buffer: []u8) !usize {
20 return await (async self.readFn(self, buffer) catch unreachable);
21 }
22
23 /// Same as `read` but end of stream returns `error.EndOfStream`.
24 pub async fn readFull(self: *Self, buf: []u8) !void {
25 var index: usize = 0;
26 while (index != buf.len) {
27 const amt_read = try await (async self.read(buf[index..]) catch unreachable);
28 if (amt_read == 0) return error.EndOfStream;
29 index += amt_read;
30 }
31 }
32
33 pub async fn readStruct(self: *Self, comptime T: type, ptr: *T) !void {
34 // Only extern and packed structs have defined in-memory layout.
35 comptime assert(@typeInfo(T).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
36 return await (async self.readFull(@sliceToBytes((*[1]T)(ptr)[0..])) catch unreachable);
37 }
38 };
39}
40
41pub fn OutStream(comptime WriteError: type) type {
42 return struct {
43 const Self = @This();
44 pub const Error = WriteError;
45
46 writeFn: async<*Allocator> fn (self: *Self, buffer: []u8) Error!void,
47 };
48}
std/event/net.zig+200-60
......@@ -3,12 +3,12 @@ const builtin = @import("builtin");
33const assert = std.debug.assert;
44const event = std.event;
55const mem = std.mem;
6const posix = std.os.posix;
7const windows = std.os.windows;
6const os = std.os;
7const posix = os.posix;
88const Loop = std.event.Loop;
99
1010pub const Server = struct {
11 handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const std.os.File) void,
11 handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const os.File) void,
1212
1313 loop: *Loop,
1414 sockfd: ?i32,
......@@ -40,17 +40,17 @@ pub const Server = struct {
4040 pub fn listen(
4141 self: *Server,
4242 address: *const std.net.Address,
43 handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const std.os.File) void,
43 handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const os.File) void,
4444 ) !void {
4545 self.handleRequestFn = handleRequestFn;
4646
47 const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
48 errdefer std.os.close(sockfd);
47 const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
48 errdefer os.close(sockfd);
4949 self.sockfd = sockfd;
5050
51 try std.os.posixBind(sockfd, &address.os_addr);
52 try std.os.posixListen(sockfd, posix.SOMAXCONN);
53 self.listen_address = std.net.Address.initPosix(try std.os.posixGetSockName(sockfd));
51 try os.posixBind(sockfd, &address.os_addr);
52 try os.posixListen(sockfd, posix.SOMAXCONN);
53 self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd));
5454
5555 self.accept_coro = try async<self.loop.allocator> Server.handler(self);
5656 errdefer cancel self.accept_coro.?;
......@@ -63,19 +63,25 @@ pub const Server = struct {
6363 /// Stop listening
6464 pub fn close(self: *Server) void {
6565 self.loop.linuxRemoveFd(self.sockfd.?);
66 std.os.close(self.sockfd.?);
66 os.close(self.sockfd.?);
6767 }
6868
6969 pub fn deinit(self: *Server) void {
7070 if (self.accept_coro) |accept_coro| cancel accept_coro;
71 if (self.sockfd) |sockfd| std.os.close(sockfd);
71 if (self.sockfd) |sockfd| os.close(sockfd);
7272 }
7373
7474 pub async fn handler(self: *Server) void {
7575 while (true) {
7676 var accepted_addr: std.net.Address = undefined;
77 if (std.os.posixAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
78 var socket = std.os.File.openHandle(accepted_fd);
77 // TODO just inline the following function here and don't expose it as posixAsyncAccept
78 if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
79 if (accepted_fd == -1) {
80 // would block
81 suspend; // we will get resumed by epoll_wait in the event loop
82 continue;
83 }
84 var socket = os.File.openHandle(accepted_fd);
7985 _ = async<self.loop.allocator> self.handleRequestFn(self, accepted_addr, socket) catch |err| switch (err) {
8086 error.OutOfMemory => {
8187 socket.close();
......@@ -83,22 +89,16 @@ pub const Server = struct {
8389 },
8490 };
8591 } else |err| switch (err) {
86 error.WouldBlock => {
87 suspend; // we will get resumed by epoll_wait in the event loop
88 continue;
89 },
9092 error.ProcessFdQuotaExceeded => {
91 errdefer std.os.emfile_promise_queue.remove(&self.waiting_for_emfile_node);
93 errdefer os.emfile_promise_queue.remove(&self.waiting_for_emfile_node);
9294 suspend {
9395 self.waiting_for_emfile_node = PromiseNode.init(@handle());
94 std.os.emfile_promise_queue.append(&self.waiting_for_emfile_node);
96 os.emfile_promise_queue.append(&self.waiting_for_emfile_node);
9597 }
9698 continue;
9799 },
98 error.ConnectionAborted, error.FileDescriptorClosed => continue,
100 error.ConnectionAborted => continue,
99101
100 error.PageFault => unreachable,
101 error.InvalidSyscall => unreachable,
102102 error.FileDescriptorNotASocket => unreachable,
103103 error.OperationNotSupported => unreachable,
104104
......@@ -111,64 +111,161 @@ pub const Server = struct {
111111};
112112
113113pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 {
114 const sockfd = try std.os.posixSocket(
114 const sockfd = try os.posixSocket(
115115 posix.AF_UNIX,
116116 posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK,
117117 0,
118118 );
119 errdefer std.os.close(sockfd);
119 errdefer os.close(sockfd);
120120
121 var sock_addr = posix.sockaddr{
122 .un = posix.sockaddr_un{
123 .family = posix.AF_UNIX,
124 .path = undefined,
125 },
121 var sock_addr = posix.sockaddr_un{
122 .family = posix.AF_UNIX,
123 .path = undefined,
126124 };
127125
128 if (path.len > @typeOf(sock_addr.un.path).len) return error.NameTooLong;
129 mem.copy(u8, sock_addr.un.path[0..], path);
126 if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong;
127 mem.copy(u8, sock_addr.path[0..], path);
130128 const size = @intCast(u32, @sizeOf(posix.sa_family_t) + path.len);
131 try std.os.posixConnectAsync(sockfd, &sock_addr, size);
129 try os.posixConnectAsync(sockfd, &sock_addr, size);
132130 try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET);
133 try std.os.posixGetSockOptConnectError(sockfd);
131 try os.posixGetSockOptConnectError(sockfd);
134132
135133 return sockfd;
136134}
137135
138pub async fn socketRead(loop: *std.event.Loop, fd: i32, buffer: []u8) !void {
136pub const ReadError = error{
137 SystemResources,
138 Unexpected,
139 UserResourceLimitReached,
140 InputOutput,
141
142 FileDescriptorNotRegistered, // TODO remove this possibility
143 OperationCausesCircularLoop, // TODO remove this possibility
144 FileDescriptorAlreadyPresentInSet, // TODO remove this possibility
145 FileDescriptorIncompatibleWithEpoll, // TODO remove this possibility
146};
147
148/// returns number of bytes read. 0 means EOF.
149pub async fn read(loop: *std.event.Loop, fd: os.FileHandle, buffer: []u8) ReadError!usize {
150 const iov = posix.iovec{
151 .iov_base = buffer.ptr,
152 .iov_len = buffer.len,
153 };
154 const iovs: *const [1]posix.iovec = &iov;
155 return await (async readvPosix(loop, fd, iovs, 1) catch unreachable);
156}
157
158pub const WriteError = error{};
159
160pub async fn write(loop: *std.event.Loop, fd: os.FileHandle, buffer: []const u8) WriteError!void {
161 const iov = posix.iovec_const{
162 .iov_base = buffer.ptr,
163 .iov_len = buffer.len,
164 };
165 const iovs: *const [1]posix.iovec_const = &iov;
166 return await (async writevPosix(loop, fd, iovs, 1) catch unreachable);
167}
168
169pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, count: usize) !void {
139170 while (true) {
140 return std.os.posixRead(fd, buffer) catch |err| switch (err) {
141 error.WouldBlock => {
142 try await try async loop.linuxWaitFd(fd, std.os.posix.EPOLLET | std.os.posix.EPOLLIN);
143 continue;
171 switch (builtin.os) {
172 builtin.Os.macosx, builtin.Os.linux => {
173 const rc = posix.writev(fd, iov, count);
174 const err = posix.getErrno(rc);
175 switch (err) {
176 0 => return,
177 posix.EINTR => continue,
178 posix.ESPIPE => unreachable,
179 posix.EINVAL => unreachable,
180 posix.EFAULT => unreachable,
181 posix.EAGAIN => {
182 try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLOUT) catch unreachable);
183 continue;
184 },
185 posix.EBADF => unreachable, // always a race condition
186 posix.EDESTADDRREQ => unreachable, // connect was never called
187 posix.EDQUOT => unreachable,
188 posix.EFBIG => unreachable,
189 posix.EIO => return error.InputOutput,
190 posix.ENOSPC => unreachable,
191 posix.EPERM => return error.AccessDenied,
192 posix.EPIPE => unreachable,
193 else => return os.unexpectedErrorPosix(err),
194 }
144195 },
145 else => return err,
146 };
196 else => @compileError("Unsupported OS"),
197 }
147198 }
148199}
149pub async fn socketWrite(loop: *std.event.Loop, fd: i32, buffer: []const u8) !void {
200
201/// returns number of bytes read. 0 means EOF.
202pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, count: usize) !usize {
150203 while (true) {
151 return std.os.posixWrite(fd, buffer) catch |err| switch (err) {
152 error.WouldBlock => {
153 try await try async loop.linuxWaitFd(fd, std.os.posix.EPOLLET | std.os.posix.EPOLLOUT);
154 continue;
204 switch (builtin.os) {
205 builtin.Os.linux, builtin.Os.freebsd, builtin.Os.macosx => {
206 const rc = posix.readv(fd, iov, count);
207 const err = posix.getErrno(rc);
208 switch (err) {
209 0 => return rc,
210 posix.EINTR => continue,
211 posix.EINVAL => unreachable,
212 posix.EFAULT => unreachable,
213 posix.EAGAIN => {
214 try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLIN) catch unreachable);
215 continue;
216 },
217 posix.EBADF => unreachable, // always a race condition
218 posix.EIO => return error.InputOutput,
219 posix.EISDIR => unreachable,
220 posix.ENOBUFS => return error.SystemResources,
221 posix.ENOMEM => return error.SystemResources,
222 else => return os.unexpectedErrorPosix(err),
223 }
155224 },
156 else => return err,
225 else => @compileError("Unsupported OS"),
226 }
227 }
228}
229
230pub async fn writev(loop: *Loop, fd: os.FileHandle, data: []const []const u8) !void {
231 const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len);
232 defer loop.allocator.free(iovecs);
233
234 for (data) |buf, i| {
235 iovecs[i] = os.posix.iovec_const{
236 .iov_base = buf.ptr,
237 .iov_len = buf.len,
157238 };
158239 }
240
241 return await (async writevPosix(loop, fd, iovecs.ptr, data.len) catch unreachable);
242}
243
244pub async fn readv(loop: *Loop, fd: os.FileHandle, data: []const []u8) !usize {
245 const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len);
246 defer loop.allocator.free(iovecs);
247
248 for (data) |buf, i| {
249 iovecs[i] = os.posix.iovec{
250 .iov_base = buf.ptr,
251 .iov_len = buf.len,
252 };
253 }
254
255 return await (async readvPosix(loop, fd, iovecs.ptr, data.len) catch unreachable);
159256}
160257
161pub async fn connect(loop: *Loop, _address: *const std.net.Address) !std.os.File {
162 var address = _address.*; // TODO https://github.com/ziglang/zig/issues/733
258pub async fn connect(loop: *Loop, _address: *const std.net.Address) !os.File {
259 var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592
163260
164 const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
165 errdefer std.os.close(sockfd);
261 const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
262 errdefer os.close(sockfd);
166263
167 try std.os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in));
264 try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in));
168265 try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET);
169 try std.os.posixGetSockOptConnectError(sockfd);
266 try os.posixGetSockOptConnectError(sockfd);
170267
171 return std.os.File.openHandle(sockfd);
268 return os.File.openHandle(sockfd);
172269}
173270
174271test "listen on a port, send bytes, receive bytes" {
......@@ -181,9 +278,9 @@ test "listen on a port, send bytes, receive bytes" {
181278 tcp_server: Server,
182279
183280 const Self = @This();
184 async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: *const std.os.File) void {
281 async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: *const os.File) void {
185282 const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
186 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
283 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/1592
187284 defer socket.close();
188285 // TODO guarantee elision of this allocation
189286 const next_handler = async errorableHandler(self, _addr, socket) catch unreachable;
......@@ -194,12 +291,11 @@ test "listen on a port, send bytes, receive bytes" {
194291 cancel @handle();
195292 }
196293 }
197 async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: std.os.File) !void {
198 const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/733
199 var socket = _socket; // TODO https://github.com/ziglang/zig/issues/733
294 async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: os.File) !void {
295 const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/1592
296 var socket = _socket; // TODO https://github.com/ziglang/zig/issues/1592
200297
201 var adapter = std.io.FileOutStream.init(socket);
202 var stream = &adapter.stream;
298 const stream = &socket.outStream().stream;
203299 try stream.print("hello from server\n");
204300 }
205301 };
......@@ -230,3 +326,47 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address, server: *Serv
230326 assert(mem.eql(u8, msg, "hello from server\n"));
231327 server.close();
232328}
329
330pub const OutStream = struct {
331 fd: os.FileHandle,
332 stream: Stream,
333 loop: *Loop,
334
335 pub const Error = WriteError;
336 pub const Stream = event.io.OutStream(Error);
337
338 pub fn init(loop: *Loop, fd: os.FileHandle) OutStream {
339 return OutStream{
340 .fd = fd,
341 .loop = loop,
342 .stream = Stream{ .writeFn = writeFn },
343 };
344 }
345
346 async<*mem.Allocator> fn writeFn(out_stream: *Stream, bytes: []const u8) Error!void {
347 const self = @fieldParentPtr(OutStream, "stream", out_stream);
348 return await (async write(self.loop, self.fd, bytes) catch unreachable);
349 }
350};
351
352pub const InStream = struct {
353 fd: os.FileHandle,
354 stream: Stream,
355 loop: *Loop,
356
357 pub const Error = ReadError;
358 pub const Stream = event.io.InStream(Error);
359
360 pub fn init(loop: *Loop, fd: os.FileHandle) InStream {
361 return InStream{
362 .fd = fd,
363 .loop = loop,
364 .stream = Stream{ .readFn = readFn },
365 };
366 }
367
368 async<*mem.Allocator> fn readFn(in_stream: *Stream, bytes: []u8) Error!usize {
369 const self = @fieldParentPtr(InStream, "stream", in_stream);
370 return await (async read(self.loop, self.fd, bytes) catch unreachable);
371 }
372};
std/io.zig+9-49
......@@ -32,48 +32,6 @@ pub fn getStdIn() GetStdIoErrs!File {
3232 return File.openHandle(handle);
3333}
3434
35/// Implementation of InStream trait for File
36pub const FileInStream = struct {
37 file: File,
38 stream: Stream,
39
40 pub const Error = @typeOf(File.read).ReturnType.ErrorSet;
41 pub const Stream = InStream(Error);
42
43 pub fn init(file: File) FileInStream {
44 return FileInStream{
45 .file = file,
46 .stream = Stream{ .readFn = readFn },
47 };
48 }
49
50 fn readFn(in_stream: *Stream, buffer: []u8) Error!usize {
51 const self = @fieldParentPtr(FileInStream, "stream", in_stream);
52 return self.file.read(buffer);
53 }
54};
55
56/// Implementation of OutStream trait for File
57pub const FileOutStream = struct {
58 file: File,
59 stream: Stream,
60
61 pub const Error = File.WriteError;
62 pub const Stream = OutStream(Error);
63
64 pub fn init(file: File) FileOutStream {
65 return FileOutStream{
66 .file = file,
67 .stream = Stream{ .writeFn = writeFn },
68 };
69 }
70
71 fn writeFn(out_stream: *Stream, bytes: []const u8) !void {
72 const self = @fieldParentPtr(FileOutStream, "stream", out_stream);
73 return self.file.write(bytes);
74 }
75};
76
7735pub fn InStream(comptime ReadError: type) type {
7836 return struct {
7937 const Self = @This();
......@@ -280,7 +238,7 @@ pub fn readFileAllocAligned(allocator: *mem.Allocator, path: []const u8, comptim
280238 const buf = try allocator.alignedAlloc(u8, A, size);
281239 errdefer allocator.free(buf);
282240
283 var adapter = FileInStream.init(file);
241 var adapter = file.inStream();
284242 try adapter.stream.readNoEof(buf[0..size]);
285243 return buf;
286244}
......@@ -577,8 +535,8 @@ pub const BufferOutStream = struct {
577535
578536pub const BufferedAtomicFile = struct {
579537 atomic_file: os.AtomicFile,
580 file_stream: FileOutStream,
581 buffered_stream: BufferedOutStream(FileOutStream.Error),
538 file_stream: os.File.OutStream,
539 buffered_stream: BufferedOutStream(os.File.WriteError),
582540
583541 pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile {
584542 // TODO with well defined copy elision we don't need this allocation
......@@ -592,8 +550,8 @@ pub const BufferedAtomicFile = struct {
592550 self.atomic_file = try os.AtomicFile.init(allocator, dest_path, os.File.default_mode);
593551 errdefer self.atomic_file.deinit();
594552
595 self.file_stream = FileOutStream.init(self.atomic_file.file);
596 self.buffered_stream = BufferedOutStream(FileOutStream.Error).init(&self.file_stream.stream);
553 self.file_stream = self.atomic_file.file.outStream();
554 self.buffered_stream = BufferedOutStream(os.File.WriteError).init(&self.file_stream.stream);
597555 return self;
598556 }
599557
......@@ -609,7 +567,7 @@ pub const BufferedAtomicFile = struct {
609567 try self.atomic_file.finish();
610568 }
611569
612 pub fn stream(self: *BufferedAtomicFile) *OutStream(FileOutStream.Error) {
570 pub fn stream(self: *BufferedAtomicFile) *OutStream(os.File.WriteError) {
613571 return &self.buffered_stream.stream;
614572 }
615573};
......@@ -622,7 +580,7 @@ test "import io tests" {
622580
623581pub fn readLine(buf: []u8) !usize {
624582 var stdin = getStdIn() catch return error.StdInUnavailable;
625 var adapter = FileInStream.init(stdin);
583 var adapter = stdin.inStream();
626584 var stream = &adapter.stream;
627585 var index: usize = 0;
628586 while (true) {
......@@ -642,3 +600,5 @@ pub fn readLine(buf: []u8) !usize {
642600 }
643601 }
644602}
603
604
std/io_test.zig+4-4
......@@ -19,8 +19,8 @@ test "write a file, read it, then delete it" {
1919 var file = try os.File.openWrite(tmp_file_name);
2020 defer file.close();
2121
22 var file_out_stream = io.FileOutStream.init(file);
23 var buf_stream = io.BufferedOutStream(io.FileOutStream.Error).init(&file_out_stream.stream);
22 var file_out_stream = file.outStream();
23 var buf_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream);
2424 const st = &buf_stream.stream;
2525 try st.print("begin");
2626 try st.write(data[0..]);
......@@ -35,8 +35,8 @@ test "write a file, read it, then delete it" {
3535 const expected_file_size = "begin".len + data.len + "end".len;
3636 assert(file_size == expected_file_size);
3737
38 var file_in_stream = io.FileInStream.init(file);
39 var buf_stream = io.BufferedInStream(io.FileInStream.Error).init(&file_in_stream.stream);
38 var file_in_stream = file.inStream();
39 var buf_stream = io.BufferedInStream(os.File.ReadError).init(&file_in_stream.stream);
4040 const st = &buf_stream.stream;
4141 const contents = try st.readAllAlloc(allocator, 2 * 1024);
4242 defer allocator.free(contents);
std/os/child_process.zig+2-2
......@@ -211,8 +211,8 @@ pub const ChildProcess = struct {
211211 defer Buffer.deinit(&stdout);
212212 defer Buffer.deinit(&stderr);
213213
214 var stdout_file_in_stream = io.FileInStream.init(child.stdout.?);
215 var stderr_file_in_stream = io.FileInStream.init(child.stderr.?);
214 var stdout_file_in_stream = child.stdout.?.inStream();
215 var stderr_file_in_stream = child.stderr.?.inStream();
216216
217217 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
218218 try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size);
std/os/file.zig+44-2
......@@ -1,6 +1,7 @@
11const std = @import("../index.zig");
22const builtin = @import("builtin");
33const os = std.os;
4const io = std.io;
45const mem = std.mem;
56const math = std.math;
67const assert = std.debug.assert;
......@@ -368,7 +369,6 @@ pub const File = struct {
368369 FileClosed,
369370 InputOutput,
370371 IsDir,
371 WouldBlock,
372372 SystemResources,
373373
374374 Unexpected,
......@@ -385,7 +385,7 @@ pub const File = struct {
385385 posix.EINTR => continue,
386386 posix.EINVAL => unreachable,
387387 posix.EFAULT => unreachable,
388 posix.EAGAIN => return error.WouldBlock,
388 posix.EAGAIN => unreachable,
389389 posix.EBADF => return error.FileClosed,
390390 posix.EIO => return error.InputOutput,
391391 posix.EISDIR => return error.IsDir,
......@@ -431,4 +431,46 @@ pub const File = struct {
431431 @compileError("Unsupported OS");
432432 }
433433 }
434
435 pub fn inStream(file: File) InStream {
436 return InStream{
437 .file = file,
438 .stream = InStream.Stream{ .readFn = InStream.readFn },
439 };
440 }
441
442 pub fn outStream(file: File) OutStream {
443 return OutStream{
444 .file = file,
445 .stream = OutStream.Stream{ .writeFn = OutStream.writeFn },
446 };
447 }
448
449 /// Implementation of io.InStream trait for File
450 pub const InStream = struct {
451 file: File,
452 stream: Stream,
453
454 pub const Error = ReadError;
455 pub const Stream = io.InStream(Error);
456
457 fn readFn(in_stream: *Stream, buffer: []u8) Error!usize {
458 const self = @fieldParentPtr(InStream, "stream", in_stream);
459 return self.file.read(buffer);
460 }
461 };
462
463 /// Implementation of io.OutStream trait for File
464 pub const OutStream = struct {
465 file: File,
466 stream: Stream,
467
468 pub const Error = WriteError;
469 pub const Stream = io.OutStream(Error);
470
471 fn writeFn(out_stream: *Stream, bytes: []const u8) Error!void {
472 const self = @fieldParentPtr(OutStream, "stream", out_stream);
473 return self.file.write(bytes);
474 }
475 };
434476};
std/os/index.zig+57-75
......@@ -242,8 +242,8 @@ pub fn posixRead(fd: i32, buf: []u8) !void {
242242 return switch (err) {
243243 posix.EINTR => continue,
244244 posix.EINVAL, posix.EFAULT => unreachable,
245 posix.EAGAIN => error.WouldBlock,
246 posix.EBADF => error.FileClosed,
245 posix.EAGAIN => unreachable,
246 posix.EBADF => unreachable, // always a race condition
247247 posix.EIO => error.InputOutput,
248248 posix.EISDIR => error.IsDir,
249249 posix.ENOBUFS, posix.ENOMEM => error.SystemResources,
......@@ -284,8 +284,8 @@ pub fn posix_preadv(fd: i32, iov: [*]const posix.iovec, count: usize, offset: u6
284284 posix.EINVAL => unreachable,
285285 posix.EFAULT => unreachable,
286286 posix.ESPIPE => unreachable, // fd is not seekable
287 posix.EAGAIN => return error.WouldBlock,
288 posix.EBADF => return error.FileClosed,
287 posix.EAGAIN => unreachable, // use posixAsyncPReadV for non blocking
288 posix.EBADF => unreachable, // always a race condition
289289 posix.EIO => return error.InputOutput,
290290 posix.EISDIR => return error.IsDir,
291291 posix.ENOBUFS => return error.SystemResources,
......@@ -302,8 +302,8 @@ pub fn posix_preadv(fd: i32, iov: [*]const posix.iovec, count: usize, offset: u6
302302 posix.EINTR => continue,
303303 posix.EINVAL => unreachable,
304304 posix.EFAULT => unreachable,
305 posix.EAGAIN => return error.WouldBlock,
306 posix.EBADF => return error.FileClosed,
305 posix.EAGAIN => unreachable, // use posixAsyncPReadV for non blocking
306 posix.EBADF => unreachable, // always a race condition
307307 posix.EIO => return error.InputOutput,
308308 posix.EISDIR => return error.IsDir,
309309 posix.ENOBUFS => return error.SystemResources,
......@@ -316,9 +316,6 @@ pub fn posix_preadv(fd: i32, iov: [*]const posix.iovec, count: usize, offset: u6
316316}
317317
318318pub const PosixWriteError = error{
319 WouldBlock,
320 FileClosed,
321 DestinationAddressRequired,
322319 DiskQuota,
323320 FileTooBig,
324321 InputOutput,
......@@ -349,9 +346,9 @@ pub fn posixWrite(fd: i32, bytes: []const u8) !void {
349346 posix.EINTR => continue,
350347 posix.EINVAL => unreachable,
351348 posix.EFAULT => unreachable,
352 posix.EAGAIN => return PosixWriteError.WouldBlock,
353 posix.EBADF => return PosixWriteError.FileClosed,
354 posix.EDESTADDRREQ => return PosixWriteError.DestinationAddressRequired,
349 posix.EAGAIN => unreachable, // use posixAsyncWrite for non-blocking
350 posix.EBADF => unreachable, // always a race condition
351 posix.EDESTADDRREQ => unreachable, // connect was never called
355352 posix.EDQUOT => return PosixWriteError.DiskQuota,
356353 posix.EFBIG => return PosixWriteError.FileTooBig,
357354 posix.EIO => return PosixWriteError.InputOutput,
......@@ -391,9 +388,9 @@ pub fn posix_pwritev(fd: i32, iov: [*]const posix.iovec_const, count: usize, off
391388 posix.ESPIPE => unreachable, // fd is not seekable
392389 posix.EINVAL => unreachable,
393390 posix.EFAULT => unreachable,
394 posix.EAGAIN => return PosixWriteError.WouldBlock,
395 posix.EBADF => return PosixWriteError.FileClosed,
396 posix.EDESTADDRREQ => return PosixWriteError.DestinationAddressRequired,
391 posix.EAGAIN => unreachable, // use posixAsyncPWriteV for non-blocking
392 posix.EBADF => unreachable, // always a race condition
393 posix.EDESTADDRREQ => unreachable, // connect was never called
397394 posix.EDQUOT => return PosixWriteError.DiskQuota,
398395 posix.EFBIG => return PosixWriteError.FileTooBig,
399396 posix.EIO => return PosixWriteError.InputOutput,
......@@ -412,9 +409,9 @@ pub fn posix_pwritev(fd: i32, iov: [*]const posix.iovec_const, count: usize, off
412409 posix.EINTR => continue,
413410 posix.EINVAL => unreachable,
414411 posix.EFAULT => unreachable,
415 posix.EAGAIN => return PosixWriteError.WouldBlock,
416 posix.EBADF => return PosixWriteError.FileClosed,
417 posix.EDESTADDRREQ => return PosixWriteError.DestinationAddressRequired,
412 posix.EAGAIN => unreachable, // use posixAsyncPWriteV for non-blocking
413 posix.EBADF => unreachable, // always a race condition
414 posix.EDESTADDRREQ => unreachable, // connect was never called
418415 posix.EDQUOT => return PosixWriteError.DiskQuota,
419416 posix.EFBIG => return PosixWriteError.FileTooBig,
420417 posix.EIO => return PosixWriteError.InputOutput,
......@@ -2287,22 +2284,9 @@ pub const PosixBindError = error{
22872284 /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range ip(7).
22882285 AddressInUse,
22892286
2290 /// sockfd is not a valid file descriptor.
2291 InvalidFileDescriptor,
2292
2293 /// The socket is already bound to an address, or addrlen is wrong, or addr is not
2294 /// a valid address for this socket's domain.
2295 InvalidSocketOrAddress,
2296
2297 /// The file descriptor sockfd does not refer to a socket.
2298 FileDescriptorNotASocket,
2299
23002287 /// A nonexistent interface was requested or the requested address was not local.
23012288 AddressNotAvailable,
23022289
2303 /// addr points outside the user's accessible address space.
2304 PageFault,
2305
23062290 /// Too many symbolic links were encountered in resolving addr.
23072291 SymLinkLoop,
23082292
......@@ -2333,11 +2317,11 @@ pub fn posixBind(fd: i32, addr: *const posix.sockaddr) PosixBindError!void {
23332317 0 => return,
23342318 posix.EACCES => return PosixBindError.AccessDenied,
23352319 posix.EADDRINUSE => return PosixBindError.AddressInUse,
2336 posix.EBADF => return PosixBindError.InvalidFileDescriptor,
2337 posix.EINVAL => return PosixBindError.InvalidSocketOrAddress,
2338 posix.ENOTSOCK => return PosixBindError.FileDescriptorNotASocket,
2320 posix.EBADF => unreachable, // always a race condition if this error is returned
2321 posix.EINVAL => unreachable,
2322 posix.ENOTSOCK => unreachable,
23392323 posix.EADDRNOTAVAIL => return PosixBindError.AddressNotAvailable,
2340 posix.EFAULT => return PosixBindError.PageFault,
2324 posix.EFAULT => unreachable,
23412325 posix.ELOOP => return PosixBindError.SymLinkLoop,
23422326 posix.ENAMETOOLONG => return PosixBindError.NameTooLong,
23432327 posix.ENOENT => return PosixBindError.FileNotFound,
......@@ -2356,9 +2340,6 @@ const PosixListenError = error{
23562340 /// use. See the discussion of /proc/sys/net/ipv4/ip_local_port_range in ip(7).
23572341 AddressInUse,
23582342
2359 /// The argument sockfd is not a valid file descriptor.
2360 InvalidFileDescriptor,
2361
23622343 /// The file descriptor sockfd does not refer to a socket.
23632344 FileDescriptorNotASocket,
23642345
......@@ -2375,7 +2356,7 @@ pub fn posixListen(sockfd: i32, backlog: u32) PosixListenError!void {
23752356 switch (err) {
23762357 0 => return,
23772358 posix.EADDRINUSE => return PosixListenError.AddressInUse,
2378 posix.EBADF => return PosixListenError.InvalidFileDescriptor,
2359 posix.EBADF => unreachable,
23792360 posix.ENOTSOCK => return PosixListenError.FileDescriptorNotASocket,
23802361 posix.EOPNOTSUPP => return PosixListenError.OperationNotSupported,
23812362 else => return unexpectedErrorPosix(err),
......@@ -2383,21 +2364,8 @@ pub fn posixListen(sockfd: i32, backlog: u32) PosixListenError!void {
23832364}
23842365
23852366pub const PosixAcceptError = error{
2386 /// The socket is marked nonblocking and no connections are present to be accepted.
2387 WouldBlock,
2388
2389 /// sockfd is not an open file descriptor.
2390 FileDescriptorClosed,
2391
23922367 ConnectionAborted,
23932368
2394 /// The addr argument is not in a writable part of the user address space.
2395 PageFault,
2396
2397 /// Socket is not listening for connections, or addrlen is invalid (e.g., is negative),
2398 /// or invalid value in flags.
2399 InvalidSyscall,
2400
24012369 /// The per-process limit on the number of open file descriptors has been reached.
24022370 ProcessFdQuotaExceeded,
24032371
......@@ -2433,14 +2401,15 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!
24332401 posix.EINTR => continue,
24342402 else => return unexpectedErrorPosix(err),
24352403
2436 posix.EAGAIN => return PosixAcceptError.WouldBlock,
2437 posix.EBADF => return PosixAcceptError.FileDescriptorClosed,
2404 posix.EAGAIN => unreachable, // use posixAsyncAccept for non-blocking
2405 posix.EBADF => unreachable, // always a race condition
24382406 posix.ECONNABORTED => return PosixAcceptError.ConnectionAborted,
2439 posix.EFAULT => return PosixAcceptError.PageFault,
2440 posix.EINVAL => return PosixAcceptError.InvalidSyscall,
2407 posix.EFAULT => unreachable,
2408 posix.EINVAL => unreachable,
24412409 posix.EMFILE => return PosixAcceptError.ProcessFdQuotaExceeded,
24422410 posix.ENFILE => return PosixAcceptError.SystemFdQuotaExceeded,
2443 posix.ENOBUFS, posix.ENOMEM => return PosixAcceptError.SystemResources,
2411 posix.ENOBUFS => return PosixAcceptError.SystemResources,
2412 posix.ENOMEM => return PosixAcceptError.SystemResources,
24442413 posix.ENOTSOCK => return PosixAcceptError.FileDescriptorNotASocket,
24452414 posix.EOPNOTSUPP => return PosixAcceptError.OperationNotSupported,
24462415 posix.EPROTO => return PosixAcceptError.ProtocolFailure,
......@@ -2449,10 +2418,35 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!
24492418 }
24502419}
24512420
2452pub const LinuxEpollCreateError = error{
2453 /// Invalid value specified in flags.
2454 InvalidSyscall,
2421/// Returns -1 if would block.
2422pub fn posixAsyncAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!i32 {
2423 while (true) {
2424 var sockaddr_size = u32(@sizeOf(posix.sockaddr));
2425 const rc = posix.accept4(fd, addr, &sockaddr_size, flags);
2426 const err = posix.getErrno(rc);
2427 switch (err) {
2428 0 => return @intCast(i32, rc),
2429 posix.EINTR => continue,
2430 else => return unexpectedErrorPosix(err),
2431
2432 posix.EAGAIN => return -1,
2433 posix.EBADF => unreachable, // always a race condition
2434 posix.ECONNABORTED => return PosixAcceptError.ConnectionAborted,
2435 posix.EFAULT => unreachable,
2436 posix.EINVAL => unreachable,
2437 posix.EMFILE => return PosixAcceptError.ProcessFdQuotaExceeded,
2438 posix.ENFILE => return PosixAcceptError.SystemFdQuotaExceeded,
2439 posix.ENOBUFS => return PosixAcceptError.SystemResources,
2440 posix.ENOMEM => return PosixAcceptError.SystemResources,
2441 posix.ENOTSOCK => return PosixAcceptError.FileDescriptorNotASocket,
2442 posix.EOPNOTSUPP => return PosixAcceptError.OperationNotSupported,
2443 posix.EPROTO => return PosixAcceptError.ProtocolFailure,
2444 posix.EPERM => return PosixAcceptError.BlockedByFirewall,
2445 }
2446 }
2447}
24552448
2449pub const LinuxEpollCreateError = error{
24562450 /// The per-user limit on the number of epoll instances imposed by
24572451 /// /proc/sys/fs/epoll/max_user_instances was encountered. See epoll(7) for further
24582452 /// details.
......@@ -2476,7 +2470,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
24762470 0 => return @intCast(i32, rc),
24772471 else => return unexpectedErrorPosix(err),
24782472
2479 posix.EINVAL => return LinuxEpollCreateError.InvalidSyscall,
2473 posix.EINVAL => unreachable,
24802474 posix.EMFILE => return LinuxEpollCreateError.ProcessFdQuotaExceeded,
24812475 posix.ENFILE => return LinuxEpollCreateError.SystemFdQuotaExceeded,
24822476 posix.ENOMEM => return LinuxEpollCreateError.SystemResources,
......@@ -2484,22 +2478,10 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
24842478}
24852479
24862480pub const LinuxEpollCtlError = error{
2487 /// epfd or fd is not a valid file descriptor.
2488 InvalidFileDescriptor,
2489
24902481 /// op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already registered
24912482 /// with this epoll instance.
24922483 FileDescriptorAlreadyPresentInSet,
24932484
2494 /// epfd is not an epoll file descriptor, or fd is the same as epfd, or the requested
2495 /// operation op is not supported by this interface, or
2496 /// An invalid event type was specified along with EPOLLEXCLUSIVE in events, or
2497 /// op was EPOLL_CTL_MOD and events included EPOLLEXCLUSIVE, or
2498 /// op was EPOLL_CTL_MOD and the EPOLLEXCLUSIVE flag has previously been applied to
2499 /// this epfd, fd pair, or
2500 /// EPOLLEXCLUSIVE was specified in event and fd refers to an epoll instance.
2501 InvalidSyscall,
2502
25032485 /// fd refers to an epoll instance and this EPOLL_CTL_ADD operation would result in a
25042486 /// circular loop of epoll instances monitoring one another.
25052487 OperationCausesCircularLoop,
......@@ -2531,9 +2513,9 @@ pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) Lin
25312513 0 => return,
25322514 else => return unexpectedErrorPosix(err),
25332515
2534 posix.EBADF => return LinuxEpollCtlError.InvalidFileDescriptor,
2516 posix.EBADF => unreachable, // always a race condition if this happens
25352517 posix.EEXIST => return LinuxEpollCtlError.FileDescriptorAlreadyPresentInSet,
2536 posix.EINVAL => return LinuxEpollCtlError.InvalidSyscall,
2518 posix.EINVAL => unreachable,
25372519 posix.ELOOP => return LinuxEpollCtlError.OperationCausesCircularLoop,
25382520 posix.ENOENT => return LinuxEpollCtlError.FileDescriptorNotRegistered,
25392521 posix.ENOMEM => return LinuxEpollCtlError.SystemResources,
std/os/linux/index.zig+8
......@@ -793,6 +793,14 @@ pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize {
793793 return syscall4(SYS_preadv, @intCast(usize, fd), @ptrToInt(iov), count, offset);
794794}
795795
796pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize {
797 return syscall3(SYS_readv, @intCast(usize, fd), @ptrToInt(iov), count);
798}
799
800pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize {
801 return syscall3(SYS_writev, @intCast(usize, fd), @ptrToInt(iov), count);
802}
803
796804pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize {
797805 return syscall4(SYS_pwritev, @intCast(usize, fd), @ptrToInt(iov), count, offset);
798806}
std/pdb.zig+3-3
......@@ -482,7 +482,7 @@ const Msf = struct {
482482 streams: []MsfStream,
483483
484484 fn openFile(self: *Msf, allocator: *mem.Allocator, file: os.File) !void {
485 var file_stream = io.FileInStream.init(file);
485 var file_stream = file.inStream();
486486 const in = &file_stream.stream;
487487
488488 var superblock: SuperBlock = undefined;
......@@ -597,7 +597,7 @@ const MsfStream = struct {
597597 .stream = Stream{ .readFn = readFn },
598598 };
599599
600 var file_stream = io.FileInStream.init(file);
600 var file_stream = file.inStream();
601601 const in = &file_stream.stream;
602602 try file.seekTo(pos);
603603
......@@ -627,7 +627,7 @@ const MsfStream = struct {
627627 var offset = self.pos % self.block_size;
628628
629629 try self.in_file.seekTo(block * self.block_size + offset);
630 var file_stream = io.FileInStream.init(self.in_file);
630 var file_stream = self.in_file.inStream();
631631 const in = &file_stream.stream;
632632
633633 var size: usize = 0;
std/special/build_runner.zig+4-4
......@@ -48,16 +48,16 @@ pub fn main() !void {
4848 var prefix: ?[]const u8 = null;
4949
5050 var stderr_file = io.getStdErr();
51 var stderr_file_stream: io.FileOutStream = undefined;
51 var stderr_file_stream: os.File.OutStream = undefined;
5252 var stderr_stream = if (stderr_file) |f| x: {
53 stderr_file_stream = io.FileOutStream.init(f);
53 stderr_file_stream = f.outStream();
5454 break :x &stderr_file_stream.stream;
5555 } else |err| err;
5656
5757 var stdout_file = io.getStdOut();
58 var stdout_file_stream: io.FileOutStream = undefined;
58 var stdout_file_stream: os.File.OutStream = undefined;
5959 var stdout_stream = if (stdout_file) |f| x: {
60 stdout_file_stream = io.FileOutStream.init(f);
60 stdout_file_stream = f.outStream();
6161 break :x &stdout_file_stream.stream;
6262 } else |err| err;
6363
std/zig/bench.zig+1-1
......@@ -24,7 +24,7 @@ pub fn main() !void {
2424 const mb_per_sec = bytes_per_sec / (1024 * 1024);
2525
2626 var stdout_file = try std.io.getStdOut();
27 const stdout = &std.io.FileOutStream.init(stdout_file).stream;
27 const stdout = &stdout_file.outStream().stream;
2828 try stdout.print("{.3} MiB/s, {} KiB used \n", mb_per_sec, memory_used / 1024);
2929}
3030
std/zig/parser_test.zig+1-1
......@@ -1873,7 +1873,7 @@ var fixed_buffer_mem: [100 * 1024]u8 = undefined;
18731873
18741874fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *bool) ![]u8 {
18751875 var stderr_file = try io.getStdErr();
1876 var stderr = &io.FileOutStream.init(stderr_file).stream;
1876 var stderr = &stderr_file.outStream().stream;
18771877
18781878 var tree = try std.zig.parse(allocator, source);
18791879 defer tree.deinit();
test/compare_output.zig+15-15
......@@ -19,7 +19,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
1919 \\
2020 \\pub fn main() void {
2121 \\ privateFunction();
22 \\ const stdout = &FileOutStream.init(getStdOut() catch unreachable).stream;
22 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;
2323 \\ stdout.print("OK 2\n") catch unreachable;
2424 \\}
2525 \\
......@@ -34,7 +34,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
3434 \\// purposefully conflicting function with main.zig
3535 \\// but it's private so it should be OK
3636 \\fn privateFunction() void {
37 \\ const stdout = &FileOutStream.init(getStdOut() catch unreachable).stream;
37 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;
3838 \\ stdout.print("OK 1\n") catch unreachable;
3939 \\}
4040 \\
......@@ -60,7 +60,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
6060 tc.addSourceFile("foo.zig",
6161 \\use @import("std").io;
6262 \\pub fn foo_function() void {
63 \\ const stdout = &FileOutStream.init(getStdOut() catch unreachable).stream;
63 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;
6464 \\ stdout.print("OK\n") catch unreachable;
6565 \\}
6666 );
......@@ -71,7 +71,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
7171 \\
7272 \\pub fn bar_function() void {
7373 \\ if (foo_function()) {
74 \\ const stdout = &FileOutStream.init(getStdOut() catch unreachable).stream;
74 \\ const stdout = &(getStdOut() catch unreachable).outStream().stream;
7575 \\ stdout.print("OK\n") catch unreachable;
7676 \\ }
7777 \\}
......@@ -103,7 +103,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
103103 \\pub const a_text = "OK\n";
104104 \\
105105 \\pub fn ok() void {
106 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
106 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
107107 \\ stdout.print(b_text) catch unreachable;
108108 \\}
109109 );
......@@ -121,7 +121,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
121121 \\const io = @import("std").io;
122122 \\
123123 \\pub fn main() void {
124 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
124 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
125125 \\ stdout.print("Hello, world!\n{d4} {x3} {c}\n", u32(12), u16(0x12), u8('a')) catch unreachable;
126126 \\}
127127 , "Hello, world!\n0012 012 a\n");
......@@ -274,7 +274,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
274274 \\ var x_local : i32 = print_ok(x);
275275 \\}
276276 \\fn print_ok(val: @typeOf(x)) @typeOf(foo) {
277 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
277 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
278278 \\ stdout.print("OK\n") catch unreachable;
279279 \\ return 0;
280280 \\}
......@@ -356,7 +356,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
356356 \\pub fn main() void {
357357 \\ const bar = Bar {.field2 = 13,};
358358 \\ const foo = Foo {.field1 = bar,};
359 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
359 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
360360 \\ if (!foo.method()) {
361361 \\ stdout.print("BAD\n") catch unreachable;
362362 \\ }
......@@ -370,7 +370,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
370370 cases.add("defer with only fallthrough",
371371 \\const io = @import("std").io;
372372 \\pub fn main() void {
373 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
373 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
374374 \\ stdout.print("before\n") catch unreachable;
375375 \\ defer stdout.print("defer1\n") catch unreachable;
376376 \\ defer stdout.print("defer2\n") catch unreachable;
......@@ -383,7 +383,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
383383 \\const io = @import("std").io;
384384 \\const os = @import("std").os;
385385 \\pub fn main() void {
386 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
386 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
387387 \\ stdout.print("before\n") catch unreachable;
388388 \\ defer stdout.print("defer1\n") catch unreachable;
389389 \\ defer stdout.print("defer2\n") catch unreachable;
......@@ -400,7 +400,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
400400 \\ do_test() catch return;
401401 \\}
402402 \\fn do_test() !void {
403 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
403 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
404404 \\ stdout.print("before\n") catch unreachable;
405405 \\ defer stdout.print("defer1\n") catch unreachable;
406406 \\ errdefer stdout.print("deferErr\n") catch unreachable;
......@@ -419,7 +419,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
419419 \\ do_test() catch return;
420420 \\}
421421 \\fn do_test() !void {
422 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
422 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
423423 \\ stdout.print("before\n") catch unreachable;
424424 \\ defer stdout.print("defer1\n") catch unreachable;
425425 \\ errdefer stdout.print("deferErr\n") catch unreachable;
......@@ -436,7 +436,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
436436 \\const io = @import("std").io;
437437 \\
438438 \\pub fn main() void {
439 \\ const stdout = &io.FileOutStream.init(io.getStdOut() catch unreachable).stream;
439 \\ const stdout = &(io.getStdOut() catch unreachable).outStream().stream;
440440 \\ stdout.print(foo_txt) catch unreachable;
441441 \\}
442442 , "1234\nabcd\n");
......@@ -456,7 +456,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
456456 \\pub fn main() !void {
457457 \\ var args_it = os.args();
458458 \\ var stdout_file = try io.getStdOut();
459 \\ var stdout_adapter = io.FileOutStream.init(stdout_file);
459 \\ var stdout_adapter = stdout_file.outStream();
460460 \\ const stdout = &stdout_adapter.stream;
461461 \\ var index: usize = 0;
462462 \\ _ = args_it.skip();
......@@ -497,7 +497,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
497497 \\pub fn main() !void {
498498 \\ var args_it = os.args();
499499 \\ var stdout_file = try io.getStdOut();
500 \\ var stdout_adapter = io.FileOutStream.init(stdout_file);
500 \\ var stdout_adapter = stdout_file.outStream();
501501 \\ const stdout = &stdout_adapter.stream;
502502 \\ var index: usize = 0;
503503 \\ _ = args_it.skip();
test/standalone/brace_expansion/main.zig+1-1
......@@ -191,7 +191,7 @@ pub fn main() !void {
191191 var stdin_buf = try Buffer.initSize(global_allocator, 0);
192192 defer stdin_buf.deinit();
193193
194 var stdin_adapter = io.FileInStream.init(stdin_file);
194 var stdin_adapter = stdin_file.inStream();
195195 try stdin_adapter.stream.readAllBuffer(&stdin_buf, @maxValue(usize));
196196
197197 var result_buf = try Buffer.initSize(global_allocator, 0);
test/tests.zig+6-6
......@@ -278,8 +278,8 @@ pub const CompareOutputContext = struct {
278278 var stdout = Buffer.initNull(b.allocator);
279279 var stderr = Buffer.initNull(b.allocator);
280280
281 var stdout_file_in_stream = io.FileInStream.init(child.stdout.?);
282 var stderr_file_in_stream = io.FileInStream.init(child.stderr.?);
281 var stdout_file_in_stream = child.stdout.?.inStream();
282 var stderr_file_in_stream = child.stderr.?.inStream();
283283
284284 stdout_file_in_stream.stream.readAllBuffer(&stdout, max_stdout_size) catch unreachable;
285285 stderr_file_in_stream.stream.readAllBuffer(&stderr, max_stdout_size) catch unreachable;
......@@ -593,8 +593,8 @@ pub const CompileErrorContext = struct {
593593 var stdout_buf = Buffer.initNull(b.allocator);
594594 var stderr_buf = Buffer.initNull(b.allocator);
595595
596 var stdout_file_in_stream = io.FileInStream.init(child.stdout.?);
597 var stderr_file_in_stream = io.FileInStream.init(child.stderr.?);
596 var stdout_file_in_stream = child.stdout.?.inStream();
597 var stderr_file_in_stream = child.stderr.?.inStream();
598598
599599 stdout_file_in_stream.stream.readAllBuffer(&stdout_buf, max_stdout_size) catch unreachable;
600600 stderr_file_in_stream.stream.readAllBuffer(&stderr_buf, max_stdout_size) catch unreachable;
......@@ -857,8 +857,8 @@ pub const TranslateCContext = struct {
857857 var stdout_buf = Buffer.initNull(b.allocator);
858858 var stderr_buf = Buffer.initNull(b.allocator);
859859
860 var stdout_file_in_stream = io.FileInStream.init(child.stdout.?);
861 var stderr_file_in_stream = io.FileInStream.init(child.stderr.?);
860 var stdout_file_in_stream = child.stdout.?.inStream();
861 var stderr_file_in_stream = child.stderr.?.inStream();
862862
863863 stdout_file_in_stream.stream.readAllBuffer(&stdout_buf, max_stdout_size) catch unreachable;
864864 stderr_file_in_stream.stream.readAllBuffer(&stderr_buf, max_stdout_size) catch unreachable;