authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-02 16:29:58-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-02 16:29:58-04:00
log03a7124543a52f7904090516e572b71b893c7ba2
treef21587b5bfbb2ff0e209bbd3c4014d01bf49cc4b
parentb7914d901c8c5761457a4774858f1004febc2d3a
parent7998e2b0f41bff86d8fbbb8112dd6c629d47e849
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5249 from ziglang/FireFox317-windows-evented-io

fix behavior test with --test-evented-io on windows

15 files changed, 646 insertions(+), 663 deletions(-)

lib/std/child_process.zig+10-30
......@@ -49,8 +49,6 @@ pub const ChildProcess = struct {
4949 /// Set to change the current working directory when spawning the child process.
5050 /// This is not yet implemented for Windows. See https://github.com/ziglang/zig/issues/5190
5151 /// Once that is done, `cwd` will be deprecated in favor of this field.
52 /// The directory handle must be opened with the ability to be passed
53 /// to a child process (no `O_CLOEXEC` flag on POSIX).
5452 cwd_dir: ?fs.Dir = null,
5553
5654 err_pipe: if (builtin.os.tag == .windows) void else [2]os.fd_t,
......@@ -443,26 +441,17 @@ pub const ChildProcess = struct {
443441 // we are the parent
444442 const pid = @intCast(i32, pid_result);
445443 if (self.stdin_behavior == StdIo.Pipe) {
446 self.stdin = File{
447 .handle = stdin_pipe[1],
448 .io_mode = std.io.mode,
449 };
444 self.stdin = File{ .handle = stdin_pipe[1] };
450445 } else {
451446 self.stdin = null;
452447 }
453448 if (self.stdout_behavior == StdIo.Pipe) {
454 self.stdout = File{
455 .handle = stdout_pipe[0],
456 .io_mode = std.io.mode,
457 };
449 self.stdout = File{ .handle = stdout_pipe[0] };
458450 } else {
459451 self.stdout = null;
460452 }
461453 if (self.stderr_behavior == StdIo.Pipe) {
462 self.stderr = File{
463 .handle = stderr_pipe[0],
464 .io_mode = std.io.mode,
465 };
454 self.stderr = File{ .handle = stderr_pipe[0] };
466455 } else {
467456 self.stderr = null;
468457 }
......@@ -686,26 +675,17 @@ pub const ChildProcess = struct {
686675 };
687676
688677 if (g_hChildStd_IN_Wr) |h| {
689 self.stdin = File{
690 .handle = h,
691 .io_mode = io.mode,
692 };
678 self.stdin = File{ .handle = h };
693679 } else {
694680 self.stdin = null;
695681 }
696682 if (g_hChildStd_OUT_Rd) |h| {
697 self.stdout = File{
698 .handle = h,
699 .io_mode = io.mode,
700 };
683 self.stdout = File{ .handle = h };
701684 } else {
702685 self.stdout = null;
703686 }
704687 if (g_hChildStd_ERR_Rd) |h| {
705 self.stderr = File{
706 .handle = h,
707 .io_mode = io.mode,
708 };
688 self.stderr = File{ .handle = h };
709689 } else {
710690 self.stderr = null;
711691 }
......@@ -845,8 +825,8 @@ const ErrInt = std.meta.Int(false, @sizeOf(anyerror) * 8);
845825fn writeIntFd(fd: i32, value: ErrInt) !void {
846826 const file = File{
847827 .handle = fd,
848 .io_mode = .blocking,
849 .async_block_allowed = File.async_block_allowed_yes,
828 .capable_io_mode = .blocking,
829 .intended_io_mode = .blocking,
850830 };
851831 file.outStream().writeIntNative(u64, @intCast(u64, value)) catch return error.SystemResources;
852832}
......@@ -854,8 +834,8 @@ fn writeIntFd(fd: i32, value: ErrInt) !void {
854834fn readIntFd(fd: i32) !ErrInt {
855835 const file = File{
856836 .handle = fd,
857 .io_mode = .blocking,
858 .async_block_allowed = File.async_block_allowed_yes,
837 .capable_io_mode = .blocking,
838 .intended_io_mode = .blocking,
859839 };
860840 return @intCast(ErrInt, file.inStream().readIntNative(u64) catch return error.SystemResources);
861841}
lib/std/debug.zig+219-209
......@@ -112,39 +112,43 @@ pub fn detectTTYConfig() TTY.Config {
112112/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
113113/// TODO multithreaded awareness
114114pub fn dumpCurrentStackTrace(start_addr: ?usize) void {
115 const stderr = getStderrStream();
116 if (builtin.strip_debug_info) {
117 noasync stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
118 return;
115 noasync {
116 const stderr = getStderrStream();
117 if (builtin.strip_debug_info) {
118 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
119 return;
120 }
121 const debug_info = getSelfDebugInfo() catch |err| {
122 stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
123 return;
124 };
125 writeCurrentStackTrace(stderr, debug_info, detectTTYConfig(), start_addr) catch |err| {
126 stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
127 return;
128 };
119129 }
120 const debug_info = getSelfDebugInfo() catch |err| {
121 noasync stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
122 return;
123 };
124 writeCurrentStackTrace(stderr, debug_info, detectTTYConfig(), start_addr) catch |err| {
125 noasync stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
126 return;
127 };
128130}
129131
130132/// Tries to print the stack trace starting from the supplied base pointer to stderr,
131133/// unbuffered, and ignores any error returned.
132134/// TODO multithreaded awareness
133135pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
134 const stderr = getStderrStream();
135 if (builtin.strip_debug_info) {
136 noasync stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
137 return;
138 }
139 const debug_info = getSelfDebugInfo() catch |err| {
140 noasync stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
141 return;
142 };
143 const tty_config = detectTTYConfig();
144 printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
145 var it = StackIterator.init(null, bp);
146 while (it.next()) |return_address| {
147 printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return;
136 noasync {
137 const stderr = getStderrStream();
138 if (builtin.strip_debug_info) {
139 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
140 return;
141 }
142 const debug_info = getSelfDebugInfo() catch |err| {
143 stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
144 return;
145 };
146 const tty_config = detectTTYConfig();
147 printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
148 var it = StackIterator.init(null, bp);
149 while (it.next()) |return_address| {
150 printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return;
151 }
148152 }
149153}
150154
......@@ -199,19 +203,21 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace
199203/// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned.
200204/// TODO multithreaded awareness
201205pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void {
202 const stderr = getStderrStream();
203 if (builtin.strip_debug_info) {
204 noasync stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
205 return;
206 noasync {
207 const stderr = getStderrStream();
208 if (builtin.strip_debug_info) {
209 stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
210 return;
211 }
212 const debug_info = getSelfDebugInfo() catch |err| {
213 stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
214 return;
215 };
216 writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, detectTTYConfig()) catch |err| {
217 stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
218 return;
219 };
206220 }
207 const debug_info = getSelfDebugInfo() catch |err| {
208 noasync stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", .{@errorName(err)}) catch return;
209 return;
210 };
211 writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, detectTTYConfig()) catch |err| {
212 noasync stderr.print("Unable to dump stack trace: {}\n", .{@errorName(err)}) catch return;
213 return;
214 };
215221}
216222
217223/// This function invokes undefined behavior when `ok` is `false`.
......@@ -255,7 +261,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
255261 resetSegfaultHandler();
256262 }
257263
258 switch (panic_stage) {
264 noasync switch (panic_stage) {
259265 0 => {
260266 panic_stage = 1;
261267
......@@ -267,7 +273,7 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
267273 defer held.release();
268274
269275 const stderr = getStderrStream();
270 noasync stderr.print(format ++ "\n", args) catch os.abort();
276 stderr.print(format ++ "\n", args) catch os.abort();
271277 if (trace) |t| {
272278 dumpStackTrace(t.*);
273279 }
......@@ -292,12 +298,12 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
292298 // we're still holding the mutex but that's fine as we're going to
293299 // call abort()
294300 const stderr = getStderrStream();
295 noasync stderr.print("Panicked during a panic. Aborting.\n", .{}) catch os.abort();
301 stderr.print("Panicked during a panic. Aborting.\n", .{}) catch os.abort();
296302 },
297303 else => {
298304 // Panicked while printing "Panicked during a panic."
299305 },
300 }
306 };
301307
302308 os.abort();
303309}
......@@ -666,158 +672,160 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
666672
667673/// TODO resources https://github.com/ziglang/zig/issues/4353
668674fn openCoffDebugInfo(allocator: *mem.Allocator, coff_file_path: [:0]const u16) !ModuleDebugInfo {
669 const coff_file = try std.fs.openFileAbsoluteW(coff_file_path.ptr, .{});
670 errdefer coff_file.close();
675 noasync {
676 const coff_file = try std.fs.openFileAbsoluteW(coff_file_path, .{ .intended_io_mode = .blocking });
677 errdefer coff_file.close();
671678
672 const coff_obj = try allocator.create(coff.Coff);
673 coff_obj.* = coff.Coff.init(allocator, coff_file);
679 const coff_obj = try allocator.create(coff.Coff);
680 coff_obj.* = coff.Coff.init(allocator, coff_file);
674681
675 var di = ModuleDebugInfo{
676 .base_address = undefined,
677 .coff = coff_obj,
678 .pdb = undefined,
679 .sect_contribs = undefined,
680 .modules = undefined,
681 };
682 var di = ModuleDebugInfo{
683 .base_address = undefined,
684 .coff = coff_obj,
685 .pdb = undefined,
686 .sect_contribs = undefined,
687 .modules = undefined,
688 };
682689
683 try di.coff.loadHeader();
690 try di.coff.loadHeader();
684691
685 var path_buf: [windows.MAX_PATH]u8 = undefined;
686 const len = try di.coff.getPdbPath(path_buf[0..]);
687 const raw_path = path_buf[0..len];
692 var path_buf: [windows.MAX_PATH]u8 = undefined;
693 const len = try di.coff.getPdbPath(path_buf[0..]);
694 const raw_path = path_buf[0..len];
688695
689 const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
696 const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
690697
691 try di.pdb.openFile(di.coff, path);
698 try di.pdb.openFile(di.coff, path);
692699
693 var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
694 const version = try pdb_stream.inStream().readIntLittle(u32);
695 const signature = try pdb_stream.inStream().readIntLittle(u32);
696 const age = try pdb_stream.inStream().readIntLittle(u32);
697 var guid: [16]u8 = undefined;
698 try pdb_stream.inStream().readNoEof(&guid);
699 if (version != 20000404) // VC70, only value observed by LLVM team
700 return error.UnknownPDBVersion;
701 if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
702 return error.PDBMismatch;
703 // We validated the executable and pdb match.
700 var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
701 const version = try pdb_stream.inStream().readIntLittle(u32);
702 const signature = try pdb_stream.inStream().readIntLittle(u32);
703 const age = try pdb_stream.inStream().readIntLittle(u32);
704 var guid: [16]u8 = undefined;
705 try pdb_stream.inStream().readNoEof(&guid);
706 if (version != 20000404) // VC70, only value observed by LLVM team
707 return error.UnknownPDBVersion;
708 if (!mem.eql(u8, &di.coff.guid, &guid) or di.coff.age != age)
709 return error.PDBMismatch;
710 // We validated the executable and pdb match.
704711
705 const string_table_index = str_tab_index: {
706 const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
707 const name_bytes = try allocator.alloc(u8, name_bytes_len);
708 try pdb_stream.inStream().readNoEof(name_bytes);
712 const string_table_index = str_tab_index: {
713 const name_bytes_len = try pdb_stream.inStream().readIntLittle(u32);
714 const name_bytes = try allocator.alloc(u8, name_bytes_len);
715 try pdb_stream.inStream().readNoEof(name_bytes);
709716
710 const HashTableHeader = packed struct {
711 Size: u32,
712 Capacity: u32,
717 const HashTableHeader = packed struct {
718 Size: u32,
719 Capacity: u32,
713720
714 fn maxLoad(cap: u32) u32 {
715 return cap * 2 / 3 + 1;
716 }
717 };
718 const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
719 if (hash_tbl_hdr.Capacity == 0)
720 return error.InvalidDebugInfo;
721 fn maxLoad(cap: u32) u32 {
722 return cap * 2 / 3 + 1;
723 }
724 };
725 const hash_tbl_hdr = try pdb_stream.inStream().readStruct(HashTableHeader);
726 if (hash_tbl_hdr.Capacity == 0)
727 return error.InvalidDebugInfo;
721728
722 if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
723 return error.InvalidDebugInfo;
729 if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
730 return error.InvalidDebugInfo;
724731
725 const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
726 if (present.len != hash_tbl_hdr.Size)
727 return error.InvalidDebugInfo;
728 const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
732 const present = try readSparseBitVector(&pdb_stream.inStream(), allocator);
733 if (present.len != hash_tbl_hdr.Size)
734 return error.InvalidDebugInfo;
735 const deleted = try readSparseBitVector(&pdb_stream.inStream(), allocator);
729736
730 const Bucket = struct {
731 first: u32,
732 second: u32,
733 };
734 const bucket_list = try allocator.alloc(Bucket, present.len);
735 for (present) |_| {
736 const name_offset = try pdb_stream.inStream().readIntLittle(u32);
737 const name_index = try pdb_stream.inStream().readIntLittle(u32);
738 const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
739 if (mem.eql(u8, name, "/names")) {
740 break :str_tab_index name_index;
737 const Bucket = struct {
738 first: u32,
739 second: u32,
740 };
741 const bucket_list = try allocator.alloc(Bucket, present.len);
742 for (present) |_| {
743 const name_offset = try pdb_stream.inStream().readIntLittle(u32);
744 const name_index = try pdb_stream.inStream().readIntLittle(u32);
745 const name = mem.spanZ(@ptrCast([*:0]u8, name_bytes.ptr + name_offset));
746 if (mem.eql(u8, name, "/names")) {
747 break :str_tab_index name_index;
748 }
741749 }
742 }
743 return error.MissingDebugInfo;
744 };
750 return error.MissingDebugInfo;
751 };
745752
746 di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
747 di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
753 di.pdb.string_table = di.pdb.getStreamById(string_table_index) orelse return error.MissingDebugInfo;
754 di.pdb.dbi = di.pdb.getStream(pdb.StreamType.Dbi) orelse return error.MissingDebugInfo;
748755
749 const dbi = di.pdb.dbi;
756 const dbi = di.pdb.dbi;
750757
751 // Dbi Header
752 const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
753 if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
754 return error.UnknownPDBVersion;
755 if (dbi_stream_header.Age != age)
756 return error.UnmatchingPDB;
758 // Dbi Header
759 const dbi_stream_header = try dbi.inStream().readStruct(pdb.DbiStreamHeader);
760 if (dbi_stream_header.VersionHeader != 19990903) // V70, only value observed by LLVM team
761 return error.UnknownPDBVersion;
762 if (dbi_stream_header.Age != age)
763 return error.UnmatchingPDB;
757764
758 const mod_info_size = dbi_stream_header.ModInfoSize;
759 const section_contrib_size = dbi_stream_header.SectionContributionSize;
765 const mod_info_size = dbi_stream_header.ModInfoSize;
766 const section_contrib_size = dbi_stream_header.SectionContributionSize;
760767
761 var modules = ArrayList(Module).init(allocator);
768 var modules = ArrayList(Module).init(allocator);
762769
763 // Module Info Substream
764 var mod_info_offset: usize = 0;
765 while (mod_info_offset != mod_info_size) {
766 const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
767 var this_record_len: usize = @sizeOf(pdb.ModInfo);
770 // Module Info Substream
771 var mod_info_offset: usize = 0;
772 while (mod_info_offset != mod_info_size) {
773 const mod_info = try dbi.inStream().readStruct(pdb.ModInfo);
774 var this_record_len: usize = @sizeOf(pdb.ModInfo);
768775
769 const module_name = try dbi.readNullTermString(allocator);
770 this_record_len += module_name.len + 1;
776 const module_name = try dbi.readNullTermString(allocator);
777 this_record_len += module_name.len + 1;
771778
772 const obj_file_name = try dbi.readNullTermString(allocator);
773 this_record_len += obj_file_name.len + 1;
779 const obj_file_name = try dbi.readNullTermString(allocator);
780 this_record_len += obj_file_name.len + 1;
774781
775 if (this_record_len % 4 != 0) {
776 const round_to_next_4 = (this_record_len | 0x3) + 1;
777 const march_forward_bytes = round_to_next_4 - this_record_len;
778 try dbi.seekBy(@intCast(isize, march_forward_bytes));
779 this_record_len += march_forward_bytes;
780 }
782 if (this_record_len % 4 != 0) {
783 const round_to_next_4 = (this_record_len | 0x3) + 1;
784 const march_forward_bytes = round_to_next_4 - this_record_len;
785 try dbi.seekBy(@intCast(isize, march_forward_bytes));
786 this_record_len += march_forward_bytes;
787 }
781788
782 try modules.append(Module{
783 .mod_info = mod_info,
784 .module_name = module_name,
785 .obj_file_name = obj_file_name,
789 try modules.append(Module{
790 .mod_info = mod_info,
791 .module_name = module_name,
792 .obj_file_name = obj_file_name,
786793
787 .populated = false,
788 .symbols = undefined,
789 .subsect_info = undefined,
790 .checksum_offset = null,
791 });
794 .populated = false,
795 .symbols = undefined,
796 .subsect_info = undefined,
797 .checksum_offset = null,
798 });
792799
793 mod_info_offset += this_record_len;
794 if (mod_info_offset > mod_info_size)
795 return error.InvalidDebugInfo;
796 }
800 mod_info_offset += this_record_len;
801 if (mod_info_offset > mod_info_size)
802 return error.InvalidDebugInfo;
803 }
797804
798 di.modules = modules.toOwnedSlice();
805 di.modules = modules.toOwnedSlice();
799806
800 // Section Contribution Substream
801 var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
802 var sect_cont_offset: usize = 0;
803 if (section_contrib_size != 0) {
804 const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
805 if (ver != pdb.SectionContrSubstreamVersion.Ver60)
806 return error.InvalidDebugInfo;
807 sect_cont_offset += @sizeOf(u32);
808 }
809 while (sect_cont_offset != section_contrib_size) {
810 const entry = try sect_contribs.addOne();
811 entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
812 sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
807 // Section Contribution Substream
808 var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
809 var sect_cont_offset: usize = 0;
810 if (section_contrib_size != 0) {
811 const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.inStream().readIntLittle(u32));
812 if (ver != pdb.SectionContrSubstreamVersion.Ver60)
813 return error.InvalidDebugInfo;
814 sect_cont_offset += @sizeOf(u32);
815 }
816 while (sect_cont_offset != section_contrib_size) {
817 const entry = try sect_contribs.addOne();
818 entry.* = try dbi.inStream().readStruct(pdb.SectionContribEntry);
819 sect_cont_offset += @sizeOf(pdb.SectionContribEntry);
813820
814 if (sect_cont_offset > section_contrib_size)
815 return error.InvalidDebugInfo;
816 }
821 if (sect_cont_offset > section_contrib_size)
822 return error.InvalidDebugInfo;
823 }
817824
818 di.sect_contribs = sect_contribs.toOwnedSlice();
825 di.sect_contribs = sect_contribs.toOwnedSlice();
819826
820 return di;
827 return di;
828 }
821829}
822830
823831fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
......@@ -1001,7 +1009,7 @@ fn openMachODebugInfo(allocator: *mem.Allocator, macho_file_path: []const u8) !M
10011009fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void {
10021010 // Need this to always block even in async I/O mode, because this could potentially
10031011 // be called from e.g. the event loop code crashing.
1004 var f = try fs.cwd().openFile(line_info.file_name, .{ .always_blocking = true });
1012 var f = try fs.cwd().openFile(line_info.file_name, .{ .intended_io_mode = .blocking });
10051013 defer f.close();
10061014 // TODO fstat and make sure that the file has the correct size
10071015
......@@ -1049,7 +1057,7 @@ const MachoSymbol = struct {
10491057
10501058fn mapWholeFile(path: []const u8) ![]align(mem.page_size) const u8 {
10511059 noasync {
1052 const file = try fs.cwd().openFile(path, .{ .always_blocking = true });
1060 const file = try fs.cwd().openFile(path, .{ .intended_io_mode = .blocking });
10531061 defer file.close();
10541062
10551063 const file_len = try math.cast(usize, try file.getEndPos());
......@@ -1410,59 +1418,61 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
14101418 }
14111419
14121420 fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
1413 // Translate the VA into an address into this object
1414 const relocated_address = address - self.base_address;
1415 assert(relocated_address >= 0x100000000);
1416
1417 // Find the .o file where this symbol is defined
1418 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
1419 return SymbolInfo{};
1420
1421 // Take the symbol name from the N_FUN STAB entry, we're going to
1422 // use it if we fail to find the DWARF infos
1423 const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
1421 noasync {
1422 // Translate the VA into an address into this object
1423 const relocated_address = address - self.base_address;
1424 assert(relocated_address >= 0x100000000);
14241425
1425 if (symbol.ofile == null)
1426 return SymbolInfo{ .symbol_name = stab_symbol };
1426 // Find the .o file where this symbol is defined
1427 const symbol = machoSearchSymbols(self.symbols, relocated_address) orelse
1428 return SymbolInfo{};
14271429
1428 const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
1430 // Take the symbol name from the N_FUN STAB entry, we're going to
1431 // use it if we fail to find the DWARF infos
1432 const stab_symbol = mem.spanZ(self.strings[symbol.nlist.n_strx..]);
14291433
1430 // Check if its debug infos are already in the cache
1431 var o_file_di = self.ofiles.getValue(o_file_path) orelse
1432 (self.loadOFile(o_file_path) catch |err| switch (err) {
1433 error.FileNotFound,
1434 error.MissingDebugInfo,
1435 error.InvalidDebugInfo,
1436 => {
1434 if (symbol.ofile == null)
14371435 return SymbolInfo{ .symbol_name = stab_symbol };
1438 },
1439 else => return err,
1440 });
14411436
1442 // Translate again the address, this time into an address inside the
1443 // .o file
1444 const relocated_address_o = relocated_address - symbol.reloc;
1437 const o_file_path = mem.spanZ(self.strings[symbol.ofile.?.n_strx..]);
14451438
1446 if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
1447 return SymbolInfo{
1448 .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
1449 .compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
1450 error.MissingDebugInfo, error.InvalidDebugInfo => "???",
1451 else => return err,
1439 // Check if its debug infos are already in the cache
1440 var o_file_di = self.ofiles.getValue(o_file_path) orelse
1441 (self.loadOFile(o_file_path) catch |err| switch (err) {
1442 error.FileNotFound,
1443 error.MissingDebugInfo,
1444 error.InvalidDebugInfo,
1445 => {
1446 return SymbolInfo{ .symbol_name = stab_symbol };
14521447 },
1453 .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
1454 error.MissingDebugInfo, error.InvalidDebugInfo => null,
1455 else => return err,
1448 else => return err,
1449 });
1450
1451 // Translate again the address, this time into an address inside the
1452 // .o file
1453 const relocated_address_o = relocated_address - symbol.reloc;
1454
1455 if (o_file_di.findCompileUnit(relocated_address_o)) |compile_unit| {
1456 return SymbolInfo{
1457 .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
1458 .compile_unit_name = compile_unit.die.getAttrString(&o_file_di, DW.AT_name) catch |err| switch (err) {
1459 error.MissingDebugInfo, error.InvalidDebugInfo => "???",
1460 else => return err,
1461 },
1462 .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o) catch |err| switch (err) {
1463 error.MissingDebugInfo, error.InvalidDebugInfo => null,
1464 else => return err,
1465 },
1466 };
1467 } else |err| switch (err) {
1468 error.MissingDebugInfo, error.InvalidDebugInfo => {
1469 return SymbolInfo{ .symbol_name = stab_symbol };
14561470 },
1457 };
1458 } else |err| switch (err) {
1459 error.MissingDebugInfo, error.InvalidDebugInfo => {
1460 return SymbolInfo{ .symbol_name = stab_symbol };
1461 },
1462 else => return err,
1463 }
1471 else => return err,
1472 }
14641473
1465 unreachable;
1474 unreachable;
1475 }
14661476 }
14671477 },
14681478 .uefi, .windows => struct {
lib/std/dynamic_library.zig+2-2
......@@ -328,14 +328,14 @@ pub const WindowsDynLib = struct {
328328
329329 pub fn open(path: []const u8) !WindowsDynLib {
330330 const path_w = try windows.sliceToPrefixedFileW(path);
331 return openW(&path_w);
331 return openW(path_w.span().ptr);
332332 }
333333
334334 pub const openC = @compileError("deprecated: renamed to openZ");
335335
336336 pub fn openZ(path_c: [*:0]const u8) !WindowsDynLib {
337337 const path_w = try windows.cStrToPrefixedFileW(path_c);
338 return openW(&path_w);
338 return openW(path_w.span().ptr);
339339 }
340340
341341 pub fn openW(path_w: [*:0]const u16) !WindowsDynLib {
lib/std/event/loop.zig+69-144
......@@ -4,19 +4,27 @@ const root = @import("root");
44const assert = std.debug.assert;
55const testing = std.testing;
66const mem = std.mem;
7const AtomicRmwOp = builtin.AtomicRmwOp;
8const AtomicOrder = builtin.AtomicOrder;
97const os = std.os;
108const windows = os.windows;
119const maxInt = std.math.maxInt;
1210const Thread = std.Thread;
1311
12const is_windows = std.Target.current.os.tag == .windows;
13
1414pub const Loop = struct {
1515 next_tick_queue: std.atomic.Queue(anyframe),
1616 os_data: OsData,
1717 final_resume_node: ResumeNode,
1818 pending_event_count: usize,
1919 extra_threads: []*Thread,
20 /// TODO change this to a pool of configurable number of threads
21 /// and rename it to be not file-system-specific. it will become
22 /// a thread pool for turning non-CPU-bound blocking things into
23 /// async things. A fallback for any missing OS-specific API.
24 fs_thread: *Thread,
25 fs_queue: std.atomic.Queue(Request),
26 fs_end_request: Request.Node,
27 fs_thread_wakeup: std.ResetEvent,
2028
2129 /// For resources that have the same lifetime as the `Loop`.
2230 /// This is only used by `Loop` for the thread pool and associated resources.
......@@ -143,7 +151,12 @@ pub const Loop = struct {
143151 .handle = undefined,
144152 .overlapped = ResumeNode.overlapped_init,
145153 },
154 .fs_end_request = .{ .data = .{ .msg = .end, .finish = .NoAction } },
155 .fs_queue = std.atomic.Queue(Request).init(),
156 .fs_thread = undefined,
157 .fs_thread_wakeup = std.ResetEvent.init(),
146158 };
159 errdefer self.fs_thread_wakeup.deinit();
147160 errdefer self.arena.deinit();
148161
149162 // We need at least one of these in case the fs thread wants to use onNextTick
......@@ -158,10 +171,19 @@ pub const Loop = struct {
158171
159172 try self.initOsData(extra_thread_count);
160173 errdefer self.deinitOsData();
174
175 if (!builtin.single_threaded) {
176 self.fs_thread = try Thread.spawn(self, posixFsRun);
177 }
178 errdefer if (!builtin.single_threaded) {
179 self.posixFsRequest(&self.fs_end_request);
180 self.fs_thread.wait();
181 };
161182 }
162183
163184 pub fn deinit(self: *Loop) void {
164185 self.deinitOsData();
186 self.fs_thread_wakeup.deinit();
165187 self.arena.deinit();
166188 self.* = undefined;
167189 }
......@@ -173,21 +195,10 @@ pub const Loop = struct {
173195 const wakeup_bytes = [_]u8{0x1} ** 8;
174196
175197 fn initOsData(self: *Loop, extra_thread_count: usize) InitOsDataError!void {
176 switch (builtin.os.tag) {
198 noasync switch (builtin.os.tag) {
177199 .linux => {
178 self.os_data.fs_queue = std.atomic.Queue(Request).init();
179 self.os_data.fs_queue_item = 0;
180 // we need another thread for the file system because Linux does not have an async
181 // file system I/O API.
182 self.os_data.fs_end_request = Request.Node{
183 .data = Request{
184 .msg = .end,
185 .finish = .NoAction,
186 },
187 };
188
189200 errdefer {
190 while (self.available_eventfd_resume_nodes.pop()) |node| noasync os.close(node.data.eventfd);
201 while (self.available_eventfd_resume_nodes.pop()) |node| os.close(node.data.eventfd);
191202 }
192203 for (self.eventfd_resume_nodes) |*eventfd_node| {
193204 eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{
......@@ -206,10 +217,10 @@ pub const Loop = struct {
206217 }
207218
208219 self.os_data.epollfd = try os.epoll_create1(os.EPOLL_CLOEXEC);
209 errdefer noasync os.close(self.os_data.epollfd);
220 errdefer os.close(self.os_data.epollfd);
210221
211222 self.os_data.final_eventfd = try os.eventfd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK);
212 errdefer noasync os.close(self.os_data.final_eventfd);
223 errdefer os.close(self.os_data.final_eventfd);
213224
214225 self.os_data.final_eventfd_event = os.epoll_event{
215226 .events = os.EPOLLIN,
......@@ -222,12 +233,6 @@ pub const Loop = struct {
222233 &self.os_data.final_eventfd_event,
223234 );
224235
225 self.os_data.fs_thread = try Thread.spawn(self, posixFsRun);
226 errdefer {
227 self.posixFsRequest(&self.os_data.fs_end_request);
228 self.os_data.fs_thread.wait();
229 }
230
231236 if (builtin.single_threaded) {
232237 assert(extra_thread_count == 0);
233238 return;
......@@ -236,7 +241,7 @@ pub const Loop = struct {
236241 var extra_thread_index: usize = 0;
237242 errdefer {
238243 // writing 8 bytes to an eventfd cannot fail
239 const amt = noasync os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable;
244 const amt = os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable;
240245 assert(amt == wakeup_bytes.len);
241246 while (extra_thread_index != 0) {
242247 extra_thread_index -= 1;
......@@ -249,22 +254,7 @@ pub const Loop = struct {
249254 },
250255 .macosx, .freebsd, .netbsd, .dragonfly => {
251256 self.os_data.kqfd = try os.kqueue();
252 errdefer noasync os.close(self.os_data.kqfd);
253
254 self.os_data.fs_kqfd = try os.kqueue();
255 errdefer noasync os.close(self.os_data.fs_kqfd);
256
257 self.os_data.fs_queue = std.atomic.Queue(Request).init();
258 // we need another thread for the file system because Darwin does not have an async
259 // file system I/O API.
260 self.os_data.fs_end_request = Request.Node{
261 .prev = undefined,
262 .next = undefined,
263 .data = Request{
264 .msg = .end,
265 .finish = .NoAction,
266 },
267 };
257 errdefer os.close(self.os_data.kqfd);
268258
269259 const empty_kevs = &[0]os.Kevent{};
270260
......@@ -310,30 +300,6 @@ pub const Loop = struct {
310300 self.os_data.final_kevent.flags = os.EV_ENABLE;
311301 self.os_data.final_kevent.fflags = os.NOTE_TRIGGER;
312302
313 self.os_data.fs_kevent_wake = os.Kevent{
314 .ident = 0,
315 .filter = os.EVFILT_USER,
316 .flags = os.EV_ADD | os.EV_ENABLE,
317 .fflags = os.NOTE_TRIGGER,
318 .data = 0,
319 .udata = undefined,
320 };
321
322 self.os_data.fs_kevent_wait = os.Kevent{
323 .ident = 0,
324 .filter = os.EVFILT_USER,
325 .flags = os.EV_ADD | os.EV_CLEAR,
326 .fflags = 0,
327 .data = 0,
328 .udata = undefined,
329 };
330
331 self.os_data.fs_thread = try Thread.spawn(self, posixFsRun);
332 errdefer {
333 self.posixFsRequest(&self.os_data.fs_end_request);
334 self.os_data.fs_thread.wait();
335 }
336
337303 if (builtin.single_threaded) {
338304 assert(extra_thread_count == 0);
339305 return;
......@@ -401,25 +367,24 @@ pub const Loop = struct {
401367 }
402368 },
403369 else => {},
404 }
370 };
405371 }
406372
407373 fn deinitOsData(self: *Loop) void {
408 switch (builtin.os.tag) {
374 noasync switch (builtin.os.tag) {
409375 .linux => {
410 noasync os.close(self.os_data.final_eventfd);
411 while (self.available_eventfd_resume_nodes.pop()) |node| noasync os.close(node.data.eventfd);
412 noasync os.close(self.os_data.epollfd);
376 os.close(self.os_data.final_eventfd);
377 while (self.available_eventfd_resume_nodes.pop()) |node| os.close(node.data.eventfd);
378 os.close(self.os_data.epollfd);
413379 },
414380 .macosx, .freebsd, .netbsd, .dragonfly => {
415 noasync os.close(self.os_data.kqfd);
416 noasync os.close(self.os_data.fs_kqfd);
381 os.close(self.os_data.kqfd);
417382 },
418383 .windows => {
419384 windows.CloseHandle(self.os_data.io_port);
420385 },
421386 else => {},
422 }
387 };
423388 }
424389
425390 /// resume_node must live longer than the anyframe that it holds a reference to.
......@@ -657,7 +622,7 @@ pub const Loop = struct {
657622 .freebsd,
658623 .netbsd,
659624 .dragonfly,
660 => self.os_data.fs_thread.wait(),
625 => self.fs_thread.wait(),
661626 else => {},
662627 }
663628
......@@ -694,23 +659,25 @@ pub const Loop = struct {
694659
695660 /// call finishOneEvent when done
696661 pub fn beginOneEvent(self: *Loop) void {
697 _ = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
662 _ = @atomicRmw(usize, &self.pending_event_count, .Add, 1, .SeqCst);
698663 }
699664
700665 pub fn finishOneEvent(self: *Loop) void {
701 const prev = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
702 if (prev == 1) {
666 noasync {
667 const prev = @atomicRmw(usize, &self.pending_event_count, .Sub, 1, .SeqCst);
668 if (prev != 1) return;
669
703670 // cause all the threads to stop
671 self.posixFsRequest(&self.fs_end_request);
672
704673 switch (builtin.os.tag) {
705674 .linux => {
706 self.posixFsRequest(&self.os_data.fs_end_request);
707675 // writing 8 bytes to an eventfd cannot fail
708 const amt = noasync os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable;
676 const amt = os.write(self.os_data.final_eventfd, &wakeup_bytes) catch unreachable;
709677 assert(amt == wakeup_bytes.len);
710678 return;
711679 },
712680 .macosx, .freebsd, .netbsd, .dragonfly => {
713 self.posixFsRequest(&self.os_data.fs_end_request);
714681 const final_kevent = @as(*const [1]os.Kevent, &self.os_data.final_kevent);
715682 const empty_kevs = &[0]os.Kevent{};
716683 // cannot fail because we already added it and this just enables it
......@@ -1063,73 +1030,55 @@ pub const Loop = struct {
10631030
10641031 fn posixFsRequest(self: *Loop, request_node: *Request.Node) void {
10651032 self.beginOneEvent(); // finished in posixFsRun after processing the msg
1066 self.os_data.fs_queue.put(request_node);
1067 switch (builtin.os.tag) {
1068 .macosx, .freebsd, .netbsd, .dragonfly => {
1069 const fs_kevs = @as(*const [1]os.Kevent, &self.os_data.fs_kevent_wake);
1070 const empty_kevs = &[0]os.Kevent{};
1071 _ = os.kevent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable;
1072 },
1073 .linux => {
1074 @atomicStore(i32, &self.os_data.fs_queue_item, 1, AtomicOrder.SeqCst);
1075 const rc = os.linux.futex_wake(&self.os_data.fs_queue_item, os.linux.FUTEX_WAKE, 1);
1076 switch (os.linux.getErrno(rc)) {
1077 0 => {},
1078 os.EINVAL => unreachable,
1079 else => unreachable,
1080 }
1081 },
1082 else => @compileError("Unsupported OS"),
1083 }
1033 self.fs_queue.put(request_node);
1034 self.fs_thread_wakeup.set();
10841035 }
10851036
10861037 fn posixFsCancel(self: *Loop, request_node: *Request.Node) void {
1087 if (self.os_data.fs_queue.remove(request_node)) {
1038 if (self.fs_queue.remove(request_node)) {
10881039 self.finishOneEvent();
10891040 }
10901041 }
10911042
1092 // TODO make this whole function noasync
1093 // https://github.com/ziglang/zig/issues/3157
10941043 fn posixFsRun(self: *Loop) void {
1095 while (true) {
1096 if (builtin.os.tag == .linux) {
1097 @atomicStore(i32, &self.os_data.fs_queue_item, 0, .SeqCst);
1098 }
1099 while (self.os_data.fs_queue.get()) |node| {
1044 noasync while (true) {
1045 self.fs_thread_wakeup.reset();
1046 while (self.fs_queue.get()) |node| {
11001047 switch (node.data.msg) {
11011048 .end => return,
11021049 .read => |*msg| {
1103 msg.result = noasync os.read(msg.fd, msg.buf);
1050 msg.result = os.read(msg.fd, msg.buf);
11041051 },
11051052 .readv => |*msg| {
1106 msg.result = noasync os.readv(msg.fd, msg.iov);
1053 msg.result = os.readv(msg.fd, msg.iov);
11071054 },
11081055 .write => |*msg| {
1109 msg.result = noasync os.write(msg.fd, msg.bytes);
1056 msg.result = os.write(msg.fd, msg.bytes);
11101057 },
11111058 .writev => |*msg| {
1112 msg.result = noasync os.writev(msg.fd, msg.iov);
1059 msg.result = os.writev(msg.fd, msg.iov);
11131060 },
11141061 .pwritev => |*msg| {
1115 msg.result = noasync os.pwritev(msg.fd, msg.iov, msg.offset);
1062 msg.result = os.pwritev(msg.fd, msg.iov, msg.offset);
11161063 },
11171064 .pread => |*msg| {
1118 msg.result = noasync os.pread(msg.fd, msg.buf, msg.offset);
1065 msg.result = os.pread(msg.fd, msg.buf, msg.offset);
11191066 },
11201067 .preadv => |*msg| {
1121 msg.result = noasync os.preadv(msg.fd, msg.iov, msg.offset);
1068 msg.result = os.preadv(msg.fd, msg.iov, msg.offset);
11221069 },
11231070 .open => |*msg| {
1124 msg.result = noasync os.openZ(msg.path, msg.flags, msg.mode);
1071 if (is_windows) unreachable; // TODO
1072 msg.result = os.openZ(msg.path, msg.flags, msg.mode);
11251073 },
11261074 .openat => |*msg| {
1127 msg.result = noasync os.openatZ(msg.fd, msg.path, msg.flags, msg.mode);
1075 if (is_windows) unreachable; // TODO
1076 msg.result = os.openatZ(msg.fd, msg.path, msg.flags, msg.mode);
11281077 },
11291078 .faccessat => |*msg| {
1130 msg.result = noasync os.faccessatZ(msg.dirfd, msg.path, msg.mode, msg.flags);
1079 msg.result = os.faccessatZ(msg.dirfd, msg.path, msg.mode, msg.flags);
11311080 },
1132 .close => |*msg| noasync os.close(msg.fd),
1081 .close => |*msg| os.close(msg.fd),
11331082 }
11341083 switch (node.data.finish) {
11351084 .TickNode => |*tick_node| self.onNextTick(tick_node),
......@@ -1137,22 +1086,8 @@ pub const Loop = struct {
11371086 }
11381087 self.finishOneEvent();
11391088 }
1140 switch (builtin.os.tag) {
1141 .linux => {
1142 const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null);
1143 switch (os.linux.getErrno(rc)) {
1144 0, os.EINTR, os.EAGAIN => continue,
1145 else => unreachable,
1146 }
1147 },
1148 .macosx, .freebsd, .netbsd, .dragonfly => {
1149 const fs_kevs = @as(*const [1]os.Kevent, &self.os_data.fs_kevent_wait);
1150 var out_kevs: [1]os.Kevent = undefined;
1151 _ = os.kevent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable;
1152 },
1153 else => @compileError("Unsupported OS"),
1154 }
1155 }
1089 self.fs_thread_wakeup.wait();
1090 };
11561091 }
11571092
11581093 const OsData = switch (builtin.os.tag) {
......@@ -1168,22 +1103,12 @@ pub const Loop = struct {
11681103 const KEventData = struct {
11691104 kqfd: i32,
11701105 final_kevent: os.Kevent,
1171 fs_kevent_wake: os.Kevent,
1172 fs_kevent_wait: os.Kevent,
1173 fs_thread: *Thread,
1174 fs_kqfd: i32,
1175 fs_queue: std.atomic.Queue(Request),
1176 fs_end_request: Request.Node,
11771106 };
11781107
11791108 const LinuxOsData = struct {
11801109 epollfd: i32,
11811110 final_eventfd: i32,
11821111 final_eventfd_event: os.linux.epoll_event,
1183 fs_thread: *Thread,
1184 fs_queue_item: i32,
1185 fs_queue: std.atomic.Queue(Request),
1186 fs_end_request: Request.Node,
11871112 };
11881113
11891114 pub const Request = struct {
......@@ -1324,11 +1249,11 @@ test "std.event.Loop - basic" {
13241249 loop.run();
13251250}
13261251
1327async fn testEventLoop() i32 {
1252fn testEventLoop() i32 {
13281253 return 1234;
13291254}
13301255
1331async fn testEventLoop2(h: anyframe->i32, did_it: *bool) void {
1256fn testEventLoop2(h: anyframe->i32, did_it: *bool) void {
13321257 const value = await h;
13331258 testing.expect(value == 1234);
13341259 did_it.* = true;
lib/std/fs.zig+78-71
......@@ -8,6 +8,8 @@ const Allocator = std.mem.Allocator;
88const assert = std.debug.assert;
99const math = std.math;
1010
11const is_darwin = std.Target.current.os.tag.isDarwin();
12
1113pub const path = @import("fs/path.zig");
1214pub const File = @import("fs/file.zig").File;
1315
......@@ -197,7 +199,7 @@ pub const AtomicFile = struct {
197199 if (std.Target.current.os.tag == .windows) {
198200 const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_basename);
199201 const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);
200 try os.renameatW(self.dir.fd, &tmp_path_w, self.dir.fd, &dest_path_w, os.windows.TRUE);
202 try os.renameatW(self.dir.fd, tmp_path_w.span(), self.dir.fd, dest_path_w.span(), os.windows.TRUE);
201203 self.file_exists = false;
202204 } else {
203205 const dest_path_c = try os.toPosixPath(self.dest_basename);
......@@ -580,7 +582,7 @@ pub const Dir = struct {
580582 pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
581583 if (builtin.os.tag == .windows) {
582584 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
583 return self.openFileW(&path_w, flags);
585 return self.openFileW(path_w.span(), flags);
584586 }
585587 const path_c = try os.toPosixPath(sub_path);
586588 return self.openFileZ(&path_c, flags);
......@@ -592,13 +594,16 @@ pub const Dir = struct {
592594 pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File.OpenError!File {
593595 if (builtin.os.tag == .windows) {
594596 const path_w = try os.windows.cStrToPrefixedFileW(sub_path);
595 return self.openFileW(&path_w, flags);
597 return self.openFileW(path_w.span(), flags);
596598 }
597599
598600 // Use the O_ locking flags if the os supports them
599601 // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag)
600 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !builtin.os.tag.isDarwin();
601 const nonblocking_lock_flag = if (has_flock_open_flags and flags.lock_nonblocking) (os.O_NONBLOCK | os.O_SYNC) else @as(u32, 0);
602 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin;
603 const nonblocking_lock_flag = if (has_flock_open_flags and flags.lock_nonblocking)
604 os.O_NONBLOCK | os.O_SYNC
605 else
606 @as(u32, 0);
602607 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
603608 .None => @as(u32, 0),
604609 .Shared => os.O_SHLOCK | nonblocking_lock_flag,
......@@ -606,14 +611,13 @@ pub const Dir = struct {
606611 } else 0;
607612
608613 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
609 const O_CLOEXEC: u32 = if (flags.share_with_child_process) 0 else os.O_CLOEXEC;
610 const os_flags = lock_flag | O_LARGEFILE | O_CLOEXEC | if (flags.write and flags.read)
614 const os_flags = lock_flag | O_LARGEFILE | os.O_CLOEXEC | if (flags.write and flags.read)
611615 @as(u32, os.O_RDWR)
612616 else if (flags.write)
613617 @as(u32, os.O_WRONLY)
614618 else
615619 @as(u32, os.O_RDONLY);
616 const fd = if (need_async_thread and !flags.always_blocking)
620 const fd = if (flags.intended_io_mode != .blocking)
617621 try std.event.Loop.instance.?.openatZ(self.fd, sub_path, os_flags, 0)
618622 else
619623 try os.openatZ(self.fd, sub_path, os_flags, 0);
......@@ -630,31 +634,32 @@ pub const Dir = struct {
630634
631635 return File{
632636 .handle = fd,
633 .io_mode = .blocking,
634 .async_block_allowed = if (flags.always_blocking)
635 File.async_block_allowed_yes
636 else
637 File.async_block_allowed_no,
637 .capable_io_mode = .blocking,
638 .intended_io_mode = flags.intended_io_mode,
638639 };
639640 }
640641
641642 /// Same as `openFile` but Windows-only and the path parameter is
642643 /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
643 pub fn openFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.OpenFlags) File.OpenError!File {
644 pub fn openFileW(self: Dir, sub_path_w: []const u16, flags: File.OpenFlags) File.OpenError!File {
644645 const w = os.windows;
645 const access_mask = w.SYNCHRONIZE |
646 (if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
647 (if (flags.write) @as(u32, w.GENERIC_WRITE) else 0);
648
649 const share_access = switch (flags.lock) {
650 .None => @as(?w.ULONG, null),
651 .Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
652 .Exclusive => w.FILE_SHARE_DELETE,
653 };
654
655646 return @as(File, .{
656 .handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, w.FILE_OPEN),
657 .io_mode = .blocking,
647 .handle = try os.windows.OpenFile(sub_path_w, .{
648 .dir = self.fd,
649 .access_mask = w.SYNCHRONIZE |
650 (if (flags.read) @as(u32, w.GENERIC_READ) else 0) |
651 (if (flags.write) @as(u32, w.GENERIC_WRITE) else 0),
652 .share_access = switch (flags.lock) {
653 .None => w.FILE_SHARE_WRITE | w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
654 .Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
655 .Exclusive => w.FILE_SHARE_DELETE,
656 },
657 .share_access_nonblocking = flags.lock_nonblocking,
658 .creation = w.FILE_OPEN,
659 .io_mode = flags.intended_io_mode,
660 }),
661 .capable_io_mode = std.io.default_mode,
662 .intended_io_mode = flags.intended_io_mode,
658663 });
659664 }
660665
......@@ -664,7 +669,7 @@ pub const Dir = struct {
664669 pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
665670 if (builtin.os.tag == .windows) {
666671 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
667 return self.createFileW(&path_w, flags);
672 return self.createFileW(path_w.span(), flags);
668673 }
669674 const path_c = try os.toPosixPath(sub_path);
670675 return self.createFileZ(&path_c, flags);
......@@ -676,13 +681,16 @@ pub const Dir = struct {
676681 pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags) File.OpenError!File {
677682 if (builtin.os.tag == .windows) {
678683 const path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
679 return self.createFileW(&path_w, flags);
684 return self.createFileW(path_w.span(), flags);
680685 }
681686
682687 // Use the O_ locking flags if the os supports them
683688 // (Or if it's darwin, as darwin's `open` doesn't support the O_SYNC flag)
684 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !builtin.os.tag.isDarwin();
685 const nonblocking_lock_flag = if (has_flock_open_flags and flags.lock_nonblocking) (os.O_NONBLOCK | os.O_SYNC) else @as(u32, 0);
689 const has_flock_open_flags = @hasDecl(os, "O_EXLOCK") and !is_darwin;
690 const nonblocking_lock_flag: u32 = if (has_flock_open_flags and flags.lock_nonblocking)
691 os.O_NONBLOCK | os.O_SYNC
692 else
693 0;
686694 const lock_flag: u32 = if (has_flock_open_flags) switch (flags.lock) {
687695 .None => @as(u32, 0),
688696 .Shared => os.O_SHLOCK,
......@@ -690,12 +698,11 @@ pub const Dir = struct {
690698 } else 0;
691699
692700 const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0;
693 const O_CLOEXEC: u32 = if (flags.share_with_child_process) 0 else os.O_CLOEXEC;
694 const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | O_CLOEXEC |
701 const os_flags = lock_flag | O_LARGEFILE | os.O_CREAT | os.O_CLOEXEC |
695702 (if (flags.truncate) @as(u32, os.O_TRUNC) else 0) |
696703 (if (flags.read) @as(u32, os.O_RDWR) else os.O_WRONLY) |
697704 (if (flags.exclusive) @as(u32, os.O_EXCL) else 0);
698 const fd = if (need_async_thread)
705 const fd = if (flags.intended_io_mode != .blocking)
699706 try std.event.Loop.instance.?.openatZ(self.fd, sub_path_c, os_flags, flags.mode)
700707 else
701708 try os.openatZ(self.fd, sub_path_c, os_flags, flags.mode);
......@@ -710,31 +717,38 @@ pub const Dir = struct {
710717 });
711718 }
712719
713 return File{ .handle = fd, .io_mode = .blocking };
720 return File{
721 .handle = fd,
722 .capable_io_mode = .blocking,
723 .intended_io_mode = flags.intended_io_mode,
724 };
714725 }
715726
716727 /// Same as `createFile` but Windows-only and the path parameter is
717728 /// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
718 pub fn createFileW(self: Dir, sub_path_w: [*:0]const u16, flags: File.CreateFlags) File.OpenError!File {
729 pub fn createFileW(self: Dir, sub_path_w: []const u16, flags: File.CreateFlags) File.OpenError!File {
719730 const w = os.windows;
720 const access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE |
721 (if (flags.read) @as(u32, w.GENERIC_READ) else 0);
722 const creation = if (flags.exclusive)
723 @as(u32, w.FILE_CREATE)
724 else if (flags.truncate)
725 @as(u32, w.FILE_OVERWRITE_IF)
726 else
727 @as(u32, w.FILE_OPEN_IF);
728
729 const share_access = switch (flags.lock) {
730 .None => @as(?w.ULONG, null),
731 .Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
732 .Exclusive => w.FILE_SHARE_DELETE,
733 };
734
731 const read_flag = if (flags.read) @as(u32, w.GENERIC_READ) else 0;
735732 return @as(File, .{
736 .handle = try os.windows.OpenFileW(self.fd, sub_path_w, null, access_mask, share_access, flags.lock_nonblocking, creation),
737 .io_mode = .blocking,
733 .handle = try os.windows.OpenFile(sub_path_w, .{
734 .dir = self.fd,
735 .access_mask = w.SYNCHRONIZE | w.GENERIC_WRITE | read_flag,
736 .share_access = switch (flags.lock) {
737 .None => w.FILE_SHARE_WRITE | w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
738 .Shared => w.FILE_SHARE_READ | w.FILE_SHARE_DELETE,
739 .Exclusive => w.FILE_SHARE_DELETE,
740 },
741 .share_access_nonblocking = flags.lock_nonblocking,
742 .creation = if (flags.exclusive)
743 @as(u32, w.FILE_CREATE)
744 else if (flags.truncate)
745 @as(u32, w.FILE_OVERWRITE_IF)
746 else
747 @as(u32, w.FILE_OPEN_IF),
748 .io_mode = flags.intended_io_mode,
749 }),
750 .capable_io_mode = std.io.default_mode,
751 .intended_io_mode = flags.intended_io_mode,
738752 });
739753 }
740754
......@@ -818,11 +832,6 @@ pub const Dir = struct {
818832 /// `true` means the opened directory can be scanned for the files and sub-directories
819833 /// of the result. It means the `iterate` function can be called.
820834 iterate: bool = false,
821
822 /// `true` means the opened directory can be passed to a child process.
823 /// `false` means the directory handle is considered to be closed when a child
824 /// process is spawned. This corresponds to the inverse of `O_CLOEXEC` on POSIX.
825 share_with_child_process: bool = false,
826835 };
827836
828837 /// Opens a directory at the given path. The directory is a system resource that remains
......@@ -832,7 +841,7 @@ pub const Dir = struct {
832841 pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir {
833842 if (builtin.os.tag == .windows) {
834843 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
835 return self.openDirW(&sub_path_w, args);
844 return self.openDirW(sub_path_w.span().ptr, args);
836845 } else {
837846 const sub_path_c = try os.toPosixPath(sub_path);
838847 return self.openDirZ(&sub_path_c, args);
......@@ -845,14 +854,12 @@ pub const Dir = struct {
845854 pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) OpenError!Dir {
846855 if (builtin.os.tag == .windows) {
847856 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
848 return self.openDirW(&sub_path_w, args);
857 return self.openDirW(sub_path_w.span().ptr, args);
849858 } else if (!args.iterate) {
850859 const O_PATH = if (@hasDecl(os, "O_PATH")) os.O_PATH else 0;
851 const O_CLOEXEC: u32 = if (args.share_with_child_process) 0 else os.O_CLOEXEC;
852 return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | O_CLOEXEC | O_PATH);
860 return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC | O_PATH);
853861 } else {
854 const O_CLOEXEC: u32 = if (args.share_with_child_process) 0 else os.O_CLOEXEC;
855 return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | O_CLOEXEC);
862 return self.openDirFlagsZ(sub_path_c, os.O_DIRECTORY | os.O_RDONLY | os.O_CLOEXEC);
856863 }
857864 }
858865
......@@ -989,7 +996,7 @@ pub const Dir = struct {
989996 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
990997 if (builtin.os.tag == .windows) {
991998 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
992 return self.deleteDirW(&sub_path_w);
999 return self.deleteDirW(sub_path_w.span().ptr);
9931000 }
9941001 const sub_path_c = try os.toPosixPath(sub_path);
9951002 return self.deleteDirZ(&sub_path_c);
......@@ -1248,7 +1255,7 @@ pub const Dir = struct {
12481255 pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {
12491256 if (builtin.os.tag == .windows) {
12501257 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1251 return self.accessW(&sub_path_w, flags);
1258 return self.accessW(sub_path_w.span().ptr, flags);
12521259 }
12531260 const path_c = try os.toPosixPath(sub_path);
12541261 return self.accessZ(&path_c, flags);
......@@ -1258,7 +1265,7 @@ pub const Dir = struct {
12581265 pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {
12591266 if (builtin.os.tag == .windows) {
12601267 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);
1261 return self.accessW(&sub_path_w, flags);
1268 return self.accessW(sub_path_w.span().ptr, flags);
12621269 }
12631270 const os_mode = if (flags.write and flags.read)
12641271 @as(u32, os.R_OK | os.W_OK)
......@@ -1266,7 +1273,7 @@ pub const Dir = struct {
12661273 @as(u32, os.W_OK)
12671274 else
12681275 @as(u32, os.F_OK);
1269 const result = if (need_async_thread)
1276 const result = if (need_async_thread and flags.intended_io_mode != .blocking)
12701277 std.event.Loop.instance.?.faccessatZ(self.fd, sub_path, os_mode, 0)
12711278 else
12721279 os.faccessatZ(self.fd, sub_path, os_mode, 0);
......@@ -1408,8 +1415,8 @@ pub fn openFileAbsoluteZ(absolute_path_c: [*:0]const u8, flags: File.OpenFlags)
14081415}
14091416
14101417/// Same as `openFileAbsolute` but the path parameter is WTF-16 encoded.
1411pub fn openFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.OpenFlags) File.OpenError!File {
1412 assert(path.isAbsoluteWindowsW(absolute_path_w));
1418pub fn openFileAbsoluteW(absolute_path_w: []const u16, flags: File.OpenFlags) File.OpenError!File {
1419 assert(path.isAbsoluteWindowsWTF16(absolute_path_w));
14131420 return cwd().openFileW(absolute_path_w, flags);
14141421}
14151422
......@@ -1598,7 +1605,7 @@ pub fn openSelfExe() OpenSelfExeError!File {
15981605 if (builtin.os.tag == .windows) {
15991606 const wide_slice = selfExePathW();
16001607 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
1601 return cwd().openFileW(&prefixed_path_w, .{});
1608 return cwd().openFileW(prefixed_path_w.span(), .{});
16021609 }
16031610 var buf: [MAX_PATH_BYTES]u8 = undefined;
16041611 const self_exe_path = try selfExePath(&buf);
......@@ -1626,7 +1633,7 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
16261633/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
16271634/// TODO make the return type of this a null terminated pointer
16281635pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1629 if (comptime std.Target.current.isDarwin()) {
1636 if (is_darwin) {
16301637 var u32_len: u32 = out_buffer.len;
16311638 const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
16321639 if (rc != 0) return error.NameTooLong;
lib/std/fs/file.zig+62-34
......@@ -8,7 +8,7 @@ const assert = std.debug.assert;
88const windows = os.windows;
99const Os = builtin.Os;
1010const maxInt = std.math.maxInt;
11const need_async_thread = std.fs.need_async_thread;
11const is_windows = std.Target.current.os.tag == .windows;
1212
1313pub const File = struct {
1414 /// The OS-specific file descriptor or file handle.
......@@ -17,15 +17,14 @@ pub const File = struct {
1717 /// On some systems, such as Linux, file system file descriptors are incapable of non-blocking I/O.
1818 /// This forces us to perform asynchronous I/O on a dedicated thread, to achieve non-blocking
1919 /// file-system I/O. To do this, `File` must be aware of whether it is a file system file descriptor,
20 /// or, more specifically, whether the I/O is blocking.
21 io_mode: io.Mode,
20 /// or, more specifically, whether the I/O is always blocking.
21 capable_io_mode: io.ModeOverride = io.default_mode,
2222
23 /// Even when 'std.io.mode' is async, it is still sometimes desirable to perform blocking I/O, although
24 /// not by default. For example, when printing a stack trace to stderr.
25 async_block_allowed: @TypeOf(async_block_allowed_no) = async_block_allowed_no,
26
27 pub const async_block_allowed_yes = if (io.is_async) true else {};
28 pub const async_block_allowed_no = if (io.is_async) false else {};
23 /// Furthermore, even when `std.io.mode` is async, it is still sometimes desirable to perform blocking I/O,
24 /// although not by default. For example, when printing a stack trace to stderr.
25 /// This field tracks both by acting as an overriding I/O mode. When not building in async I/O mode,
26 /// the type only has the `.blocking` tag, making it a zero-bit type.
27 intended_io_mode: io.ModeOverride = io.default_mode,
2928
3029 pub const Mode = os.mode_t;
3130
......@@ -36,9 +35,7 @@ pub const File = struct {
3635
3736 pub const OpenError = windows.CreateFileError || os.OpenError || os.FlockError;
3837
39 pub const Lock = enum {
40 None, Shared, Exclusive
41 };
38 pub const Lock = enum { None, Shared, Exclusive };
4239
4340 /// TODO https://github.com/ziglang/zig/issues/3802
4441 pub const OpenFlags = struct {
......@@ -63,17 +60,15 @@ pub const File = struct {
6360 /// Sets whether or not to wait until the file is locked to return. If set to true,
6461 /// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
6562 /// is available to proceed.
63 /// In async I/O mode, non-blocking at the OS level is
64 /// determined by `intended_io_mode`, and `true` means `error.WouldBlock` is returned,
65 /// and `false` means `error.WouldBlock` is handled by the event loop.
6666 lock_nonblocking: bool = false,
6767
68 /// This prevents `O_NONBLOCK` from being passed even if `std.io.is_async`.
69 /// It allows the use of `noasync` when calling functions related to opening
70 /// the file, reading, and writing.
71 always_blocking: bool = false,
72
73 /// `true` means the opened directory can be passed to a child process.
74 /// `false` means the directory handle is considered to be closed when a child
75 /// process is spawned. This corresponds to the inverse of `O_CLOEXEC` on POSIX.
76 share_with_child_process: bool = false,
68 /// Setting this to `.blocking` prevents `O_NONBLOCK` from being passed even
69 /// if `std.io.is_async`. It allows the use of `noasync` when calling functions
70 /// related to opening the file, reading, writing, and locking.
71 intended_io_mode: io.ModeOverride = io.default_mode,
7772 };
7873
7974 /// TODO https://github.com/ziglang/zig/issues/3802
......@@ -107,22 +102,27 @@ pub const File = struct {
107102 /// Sets whether or not to wait until the file is locked to return. If set to true,
108103 /// `error.WouldBlock` will be returned. Otherwise, the file will wait until the file
109104 /// is available to proceed.
105 /// In async I/O mode, non-blocking at the OS level is
106 /// determined by `intended_io_mode`, and `true` means `error.WouldBlock` is returned,
107 /// and `false` means `error.WouldBlock` is handled by the event loop.
110108 lock_nonblocking: bool = false,
111109
112110 /// For POSIX systems this is the file system mode the file will
113111 /// be created with.
114112 mode: Mode = default_mode,
115113
116 /// `true` means the opened directory can be passed to a child process.
117 /// `false` means the directory handle is considered to be closed when a child
118 /// process is spawned. This corresponds to the inverse of `O_CLOEXEC` on POSIX.
119 share_with_child_process: bool = false,
114 /// Setting this to `.blocking` prevents `O_NONBLOCK` from being passed even
115 /// if `std.io.is_async`. It allows the use of `noasync` when calling functions
116 /// related to opening the file, reading, writing, and locking.
117 intended_io_mode: io.ModeOverride = io.default_mode,
120118 };
121119
122120 /// Upon success, the stream is in an uninitialized state. To continue using it,
123121 /// you must use the open() function.
124122 pub fn close(self: File) void {
125 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
123 if (is_windows) {
124 windows.CloseHandle(self.handle);
125 } else if (self.capable_io_mode != self.intended_io_mode) {
126126 std.event.Loop.instance.?.close(self.handle);
127127 } else {
128128 os.close(self.handle);
......@@ -305,7 +305,9 @@ pub const File = struct {
305305 pub const PReadError = os.PReadError;
306306
307307 pub fn read(self: File, buffer: []u8) ReadError!usize {
308 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
308 if (is_windows) {
309 return windows.ReadFile(self.handle, buffer, null, self.intended_io_mode);
310 } else if (self.capable_io_mode != self.intended_io_mode) {
309311 return std.event.Loop.instance.?.read(self.handle, buffer);
310312 } else {
311313 return os.read(self.handle, buffer);
......@@ -325,7 +327,9 @@ pub const File = struct {
325327 }
326328
327329 pub fn pread(self: File, buffer: []u8, offset: u64) PReadError!usize {
328 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
330 if (is_windows) {
331 return windows.ReadFile(self.handle, buffer, offset, self.intended_io_mode);
332 } else if (self.capable_io_mode != self.intended_io_mode) {
329333 return std.event.Loop.instance.?.pread(self.handle, buffer, offset);
330334 } else {
331335 return os.pread(self.handle, buffer, offset);
......@@ -345,7 +349,12 @@ pub const File = struct {
345349 }
346350
347351 pub fn readv(self: File, iovecs: []const os.iovec) ReadError!usize {
348 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
352 if (is_windows) {
353 // TODO improve this to use ReadFileScatter
354 if (iovecs.len == 0) return @as(usize, 0);
355 const first = iovecs[0];
356 return windows.ReadFile(self.handle, first.iov_base[0..first.iov_len], null, self.intended_io_mode);
357 } else if (self.capable_io_mode != self.intended_io_mode) {
349358 return std.event.Loop.instance.?.readv(self.handle, iovecs);
350359 } else {
351360 return os.readv(self.handle, iovecs);
......@@ -379,7 +388,12 @@ pub const File = struct {
379388 }
380389
381390 pub fn preadv(self: File, iovecs: []const os.iovec, offset: u64) PReadError!usize {
382 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
391 if (is_windows) {
392 // TODO improve this to use ReadFileScatter
393 if (iovecs.len == 0) return @as(usize, 0);
394 const first = iovecs[0];
395 return windows.ReadFile(self.handle, first.iov_base[0..first.iov_len], offset, self.intended_io_mode);
396 } else if (self.capable_io_mode != self.intended_io_mode) {
383397 return std.event.Loop.instance.?.preadv(self.handle, iovecs, offset);
384398 } else {
385399 return os.preadv(self.handle, iovecs, offset);
......@@ -416,7 +430,9 @@ pub const File = struct {
416430 pub const PWriteError = os.PWriteError;
417431
418432 pub fn write(self: File, bytes: []const u8) WriteError!usize {
419 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
433 if (is_windows) {
434 return windows.WriteFile(self.handle, bytes, null, self.intended_io_mode);
435 } else if (self.capable_io_mode != self.intended_io_mode) {
420436 return std.event.Loop.instance.?.write(self.handle, bytes);
421437 } else {
422438 return os.write(self.handle, bytes);
......@@ -431,7 +447,9 @@ pub const File = struct {
431447 }
432448
433449 pub fn pwrite(self: File, bytes: []const u8, offset: u64) PWriteError!usize {
434 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
450 if (is_windows) {
451 return windows.WriteFile(self.handle, bytes, offset, self.intended_io_mode);
452 } else if (self.capable_io_mode != self.intended_io_mode) {
435453 return std.event.Loop.instance.?.pwrite(self.handle, bytes, offset);
436454 } else {
437455 return os.pwrite(self.handle, bytes, offset);
......@@ -446,7 +464,12 @@ pub const File = struct {
446464 }
447465
448466 pub fn writev(self: File, iovecs: []const os.iovec_const) WriteError!usize {
449 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
467 if (is_windows) {
468 // TODO improve this to use WriteFileScatter
469 if (iovecs.len == 0) return @as(usize, 0);
470 const first = iovecs[0];
471 return windows.WriteFile(self.handle, first.iov_base[0..first.iov_len], null, self.intended_io_mode);
472 } else if (self.capable_io_mode != self.intended_io_mode) {
450473 return std.event.Loop.instance.?.writev(self.handle, iovecs);
451474 } else {
452475 return os.writev(self.handle, iovecs);
......@@ -472,7 +495,12 @@ pub const File = struct {
472495 }
473496
474497 pub fn pwritev(self: File, iovecs: []os.iovec_const, offset: usize) PWriteError!usize {
475 if (need_async_thread and self.io_mode == .blocking and !self.async_block_allowed) {
498 if (is_windows) {
499 // TODO improve this to use WriteFileScatter
500 if (iovecs.len == 0) return @as(usize, 0);
501 const first = iovecs[0];
502 return windows.WriteFile(self.handle, first.iov_base[0..first.iov_len], offset, self.intended_io_mode);
503 } else if (self.capable_io_mode != self.intended_io_mode) {
476504 return std.event.Loop.instance.?.pwritev(self.handle, iovecs, offset);
477505 } else {
478506 return os.pwritev(self.handle, iovecs, offset);
lib/std/fs/path.zig+4
......@@ -177,6 +177,10 @@ pub fn isAbsoluteWindowsW(path_w: [*:0]const u16) bool {
177177 return isAbsoluteWindowsImpl(u16, mem.spanZ(path_w));
178178}
179179
180pub fn isAbsoluteWindowsWTF16(path: []const u16) bool {
181 return isAbsoluteWindowsImpl(u16, path);
182}
183
180184pub const isAbsoluteWindowsC = @compileError("deprecated: renamed to isAbsoluteWindowsZ");
181185
182186pub fn isAbsoluteWindowsZ(path_c: [*:0]const u8) bool {
lib/std/fs/test.zig+11-1
......@@ -27,7 +27,12 @@ test "open file with exclusive nonblocking lock twice" {
2727}
2828
2929test "open file with lock twice, make sure it wasn't open at the same time" {
30 if (builtin.single_threaded) return;
30 if (builtin.single_threaded) return error.SkipZigTest;
31
32 if (std.io.is_async) {
33 // This test starts its own threads and is not compatible with async I/O.
34 return error.SkipZigTest;
35 }
3136
3237 const filename = "file_lock_test.txt";
3338
......@@ -58,6 +63,11 @@ test "open file with lock twice, make sure it wasn't open at the same time" {
5863test "create file, lock and read from multiple process at once" {
5964 if (builtin.single_threaded) return error.SkipZigTest;
6065
66 if (std.io.is_async) {
67 // This test starts its own threads and is not compatible with async I/O.
68 return error.SkipZigTest;
69 }
70
6171 if (true) {
6272 // https://github.com/ziglang/zig/issues/5006
6373 return error.SkipZigTest;
lib/std/io.zig+17-4
......@@ -30,6 +30,11 @@ else
3030 Mode.blocking;
3131pub const is_async = mode != .blocking;
3232
33/// This is an enum value to use for I/O mode at runtime, since it takes up zero bytes at runtime,
34/// and makes expressions comptime-known when `is_async` is `false`.
35pub const ModeOverride = if (is_async) Mode else enum { blocking };
36pub const default_mode: ModeOverride = if (is_async) Mode.evented else .blocking;
37
3338fn getStdOutHandle() os.fd_t {
3439 if (builtin.os.tag == .windows) {
3540 return os.windows.peb().ProcessParameters.hStdOutput;
......@@ -42,10 +47,13 @@ fn getStdOutHandle() os.fd_t {
4247 return os.STDOUT_FILENO;
4348}
4449
50/// TODO: async stdout on windows without a dedicated thread.
51/// https://github.com/ziglang/zig/pull/4816#issuecomment-604521023
4552pub fn getStdOut() File {
4653 return File{
4754 .handle = getStdOutHandle(),
48 .io_mode = .blocking,
55 .capable_io_mode = .blocking,
56 .intended_io_mode = default_mode,
4957 };
5058}
5159
......@@ -61,11 +69,13 @@ fn getStdErrHandle() os.fd_t {
6169 return os.STDERR_FILENO;
6270}
6371
72/// This returns a `File` that is configured to block with every write, in order
73/// to facilitate better debugging. This can be changed by modifying the `intended_io_mode` field.
6474pub fn getStdErr() File {
6575 return File{
6676 .handle = getStdErrHandle(),
67 .io_mode = .blocking,
68 .async_block_allowed = File.async_block_allowed_yes,
77 .capable_io_mode = .blocking,
78 .intended_io_mode = .blocking,
6979 };
7080}
7181
......@@ -81,10 +91,13 @@ fn getStdInHandle() os.fd_t {
8191 return os.STDIN_FILENO;
8292}
8393
94/// TODO: async stdin on windows without a dedicated thread.
95/// https://github.com/ziglang/zig/pull/4816#issuecomment-604521023
8496pub fn getStdIn() File {
8597 return File{
8698 .handle = getStdInHandle(),
87 .io_mode = .blocking,
99 .capable_io_mode = .blocking,
100 .intended_io_mode = default_mode,
88101 };
89102}
90103
lib/std/net.zig+2-5
......@@ -412,7 +412,7 @@ pub fn tcpConnectToAddress(address: Address) !fs.File {
412412 errdefer os.close(sockfd);
413413 try os.connect(sockfd, &address.any, address.getOsSockLen());
414414
415 return fs.File{ .handle = sockfd, .io_mode = std.io.mode };
415 return fs.File{ .handle = sockfd };
416416}
417417
418418/// Call `AddressList.deinit` on the result.
......@@ -1381,10 +1381,7 @@ pub const StreamServer = struct {
13811381 var adr_len: os.socklen_t = @sizeOf(Address);
13821382 if (os.accept4(self.sockfd.?, &accepted_addr.any, &adr_len, accept_flags)) |fd| {
13831383 return Connection{
1384 .file = fs.File{
1385 .handle = fd,
1386 .io_mode = std.io.mode,
1387 },
1384 .file = fs.File{ .handle = fd },
13881385 .address = accepted_addr,
13891386 };
13901387 } else |err| switch (err) {
lib/std/os.zig+59-44
......@@ -177,8 +177,8 @@ fn getRandomBytesDevURandom(buf: []u8) !void {
177177
178178 const file = std.fs.File{
179179 .handle = fd,
180 .io_mode = .blocking,
181 .async_block_allowed = std.fs.File.async_block_allowed_yes,
180 .capable_io_mode = .blocking,
181 .intended_io_mode = .blocking,
182182 };
183183 const stream = file.inStream();
184184 stream.readNoEof(buf) catch return error.Unexpected;
......@@ -309,7 +309,7 @@ pub const ReadError = error{
309309/// For POSIX the limit is `math.maxInt(isize)`.
310310pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
311311 if (builtin.os.tag == .windows) {
312 return windows.ReadFile(fd, buf, null);
312 return windows.ReadFile(fd, buf, null, std.io.default_mode);
313313 }
314314
315315 if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -369,7 +369,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
369369/// On these systems, the read races with concurrent writes to the same file descriptor.
370370pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
371371 if (std.Target.current.os.tag == .windows) {
372 // TODO does Windows have a way to read an io vector?
372 // TODO improve this to use ReadFileScatter
373373 if (iov.len == 0) return @as(usize, 0);
374374 const first = iov[0];
375375 return read(fd, first.iov_base[0..first.iov_len]);
......@@ -412,7 +412,7 @@ pub const PReadError = ReadError || error{Unseekable};
412412/// used to perform the I/O. `error.WouldBlock` is not possible on Windows.
413413pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
414414 if (builtin.os.tag == .windows) {
415 return windows.ReadFile(fd, buf, offset);
415 return windows.ReadFile(fd, buf, offset, std.io.default_mode);
416416 }
417417
418418 while (true) {
......@@ -588,7 +588,7 @@ pub const WriteError = error{
588588/// The corresponding POSIX limit is `math.maxInt(isize)`.
589589pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
590590 if (builtin.os.tag == .windows) {
591 return windows.WriteFile(fd, bytes, null);
591 return windows.WriteFile(fd, bytes, null, std.io.default_mode);
592592 }
593593
594594 if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -655,7 +655,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
655655/// If `iov.len` is larger than will fit in a `u31`, a partial write will occur.
656656pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
657657 if (std.Target.current.os.tag == .windows) {
658 // TODO does Windows have a way to write an io vector?
658 // TODO improve this to use WriteFileScatter
659659 if (iov.len == 0) return @as(usize, 0);
660660 const first = iov[0];
661661 return write(fd, first.iov_base[0..first.iov_len]);
......@@ -713,7 +713,7 @@ pub const PWriteError = WriteError || error{Unseekable};
713713/// The corresponding POSIX limit is `math.maxInt(isize)`.
714714pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
715715 if (std.Target.current.os.tag == .windows) {
716 return windows.WriteFile(fd, bytes, offset);
716 return windows.WriteFile(fd, bytes, offset, std.io.default_mode);
717717 }
718718
719719 // Prevent EINVAL.
......@@ -858,8 +858,11 @@ pub const OpenError = error{
858858
859859/// Open and possibly create a file. Keeps trying if it gets interrupted.
860860/// See also `openC`.
861/// TODO support windows
862861pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t {
862 if (std.Target.current.os.tag == .windows) {
863 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
864 return openW(file_path_w.span(), flags, perm);
865 }
863866 const file_path_c = try toPosixPath(file_path);
864867 return openZ(&file_path_c, flags, perm);
865868}
......@@ -868,8 +871,11 @@ pub const openC = @compileError("deprecated: renamed to openZ");
868871
869872/// Open and possibly create a file. Keeps trying if it gets interrupted.
870873/// See also `open`.
871/// TODO support windows
872874pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t {
875 if (std.Target.current.os.tag == .windows) {
876 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
877 return openW(file_path_w.span(), flags, perm);
878 }
873879 while (true) {
874880 const rc = system.open(file_path, flags, perm);
875881 switch (errno(rc)) {
......@@ -899,6 +905,13 @@ pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t
899905 }
900906}
901907
908/// Windows-only. The path parameter is
909/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
910/// Translates the POSIX open API call to a Windows API call.
911pub fn openW(file_path_w: []const u16, flags: u32, perm: usize) OpenError!fd_t {
912 @compileError("TODO implement openW for windows");
913}
914
902915/// Open and possibly create a file. Keeps trying if it gets interrupted.
903916/// `file_path` is relative to the open directory handle `dir_fd`.
904917/// See also `openatC`.
......@@ -1308,7 +1321,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!
13081321 if (builtin.os.tag == .windows) {
13091322 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
13101323 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
1311 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
1324 return windows.CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, 0);
13121325 } else {
13131326 const target_path_c = try toPosixPath(target_path);
13141327 const sym_link_path_c = try toPosixPath(sym_link_path);
......@@ -1324,7 +1337,7 @@ pub fn symlinkZ(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLin
13241337 if (builtin.os.tag == .windows) {
13251338 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
13261339 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
1327 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
1340 return windows.CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, 0);
13281341 }
13291342 switch (errno(system.symlink(target_path, sym_link_path))) {
13301343 0 => return,
......@@ -1400,7 +1413,7 @@ pub const UnlinkError = error{
14001413pub fn unlink(file_path: []const u8) UnlinkError!void {
14011414 if (builtin.os.tag == .windows) {
14021415 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1403 return windows.DeleteFileW(&file_path_w);
1416 return windows.DeleteFileW(file_path_w.span().ptr);
14041417 } else {
14051418 const file_path_c = try toPosixPath(file_path);
14061419 return unlinkZ(&file_path_c);
......@@ -1413,7 +1426,7 @@ pub const unlinkC = @compileError("deprecated: renamed to unlinkZ");
14131426pub fn unlinkZ(file_path: [*:0]const u8) UnlinkError!void {
14141427 if (builtin.os.tag == .windows) {
14151428 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
1416 return windows.DeleteFileW(&file_path_w);
1429 return windows.DeleteFileW(file_path_w.span().ptr);
14171430 }
14181431 switch (errno(system.unlink(file_path))) {
14191432 0 => return,
......@@ -1444,7 +1457,7 @@ pub const UnlinkatError = UnlinkError || error{
14441457pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
14451458 if (builtin.os.tag == .windows) {
14461459 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1447 return unlinkatW(dirfd, &file_path_w, flags);
1460 return unlinkatW(dirfd, file_path_w.span().ptr, flags);
14481461 }
14491462 const file_path_c = try toPosixPath(file_path);
14501463 return unlinkatZ(dirfd, &file_path_c, flags);
......@@ -1456,7 +1469,7 @@ pub const unlinkatC = @compileError("deprecated: renamed to unlinkatZ");
14561469pub fn unlinkatZ(dirfd: fd_t, file_path_c: [*:0]const u8, flags: u32) UnlinkatError!void {
14571470 if (builtin.os.tag == .windows) {
14581471 const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);
1459 return unlinkatW(dirfd, &file_path_w, flags);
1472 return unlinkatW(dirfd, file_path_w.span().ptr, flags);
14601473 }
14611474 switch (errno(system.unlinkat(dirfd, file_path_c, flags))) {
14621475 0 => return,
......@@ -1571,7 +1584,7 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
15711584 if (builtin.os.tag == .windows) {
15721585 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
15731586 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
1574 return renameW(&old_path_w, &new_path_w);
1587 return renameW(old_path_w.span().ptr, new_path_w.span().ptr);
15751588 } else {
15761589 const old_path_c = try toPosixPath(old_path);
15771590 const new_path_c = try toPosixPath(new_path);
......@@ -1586,7 +1599,7 @@ pub fn renameZ(old_path: [*:0]const u8, new_path: [*:0]const u8) RenameError!voi
15861599 if (builtin.os.tag == .windows) {
15871600 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
15881601 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
1589 return renameW(&old_path_w, &new_path_w);
1602 return renameW(old_path_w.span().ptr, new_path_w.span().ptr);
15901603 }
15911604 switch (errno(system.rename(old_path, new_path))) {
15921605 0 => return,
......@@ -1629,7 +1642,7 @@ pub fn renameat(
16291642 if (builtin.os.tag == .windows) {
16301643 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
16311644 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
1632 return renameatW(old_dir_fd, &old_path_w, new_dir_fd, &new_path_w, windows.TRUE);
1645 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
16331646 } else {
16341647 const old_path_c = try toPosixPath(old_path);
16351648 const new_path_c = try toPosixPath(new_path);
......@@ -1647,7 +1660,7 @@ pub fn renameatZ(
16471660 if (builtin.os.tag == .windows) {
16481661 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
16491662 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
1650 return renameatW(old_dir_fd, &old_path_w, new_dir_fd, &new_path_w, windows.TRUE);
1663 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
16511664 }
16521665
16531666 switch (errno(system.renameat(old_dir_fd, old_path, new_dir_fd, new_path))) {
......@@ -1674,38 +1687,40 @@ pub fn renameatZ(
16741687 }
16751688}
16761689
1677/// Same as `renameat` except the parameters are null-terminated UTF16LE encoded byte arrays.
1678/// Assumes target is Windows.
1679/// TODO these args can actually be slices when using ntdll. audit the rest of the W functions too.
1690/// Same as `renameat` but Windows-only and the path parameters are
1691/// [WTF-16](https://simonsapin.github.io/wtf-8/#potentially-ill-formed-utf-16) encoded.
16801692pub fn renameatW(
16811693 old_dir_fd: fd_t,
1682 old_path: [*:0]const u16,
1694 old_path_w: []const u16,
16831695 new_dir_fd: fd_t,
1684 new_path_w: [*:0]const u16,
1696 new_path_w: []const u16,
16851697 ReplaceIfExists: windows.BOOLEAN,
16861698) RenameError!void {
1687 const access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE;
1688 const src_fd = windows.OpenFileW(old_dir_fd, old_path, null, access_mask, null, false, windows.FILE_OPEN) catch |err| switch (err) {
1689 error.WouldBlock => unreachable,
1699 const src_fd = windows.OpenFile(old_path_w, .{
1700 .dir = old_dir_fd,
1701 .access_mask = windows.SYNCHRONIZE | windows.GENERIC_WRITE | windows.DELETE,
1702 .creation = windows.FILE_OPEN,
1703 .io_mode = .blocking,
1704 }) catch |err| switch (err) {
1705 error.WouldBlock => unreachable, // Not possible without `.share_access_nonblocking = true`.
16901706 else => |e| return e,
16911707 };
16921708 defer windows.CloseHandle(src_fd);
16931709
16941710 const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (MAX_PATH_BYTES - 1);
16951711 var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION)) = undefined;
1696 const new_path = mem.span(new_path_w);
1697 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path.len * 2;
1712 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
16981713 if (struct_len > struct_buf_len) return error.NameTooLong;
16991714
17001715 const rename_info = @ptrCast(*windows.FILE_RENAME_INFORMATION, &rename_info_buf);
17011716
17021717 rename_info.* = .{
17031718 .ReplaceIfExists = ReplaceIfExists,
1704 .RootDirectory = if (std.fs.path.isAbsoluteWindowsW(new_path_w)) null else new_dir_fd,
1705 .FileNameLength = @intCast(u32, new_path.len * 2), // already checked error.NameTooLong
1719 .RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
1720 .FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong
17061721 .FileName = undefined,
17071722 };
1708 std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path.len], new_path);
1723 std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
17091724
17101725 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
17111726
......@@ -1749,7 +1764,7 @@ pub const MakeDirError = error{
17491764pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!void {
17501765 if (builtin.os.tag == .windows) {
17511766 const sub_dir_path_w = try windows.sliceToPrefixedFileW(sub_dir_path);
1752 return mkdiratW(dir_fd, &sub_dir_path_w, mode);
1767 return mkdiratW(dir_fd, sub_dir_path_w.span().ptr, mode);
17531768 } else {
17541769 const sub_dir_path_c = try toPosixPath(sub_dir_path);
17551770 return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
......@@ -1761,7 +1776,7 @@ pub const mkdiratC = @compileError("deprecated: renamed to mkdiratZ");
17611776pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
17621777 if (builtin.os.tag == .windows) {
17631778 const sub_dir_path_w = try windows.cStrToPrefixedFileW(sub_dir_path);
1764 return mkdiratW(dir_fd, &sub_dir_path_w, mode);
1779 return mkdiratW(dir_fd, sub_dir_path_w.span().ptr, mode);
17651780 }
17661781 switch (errno(system.mkdirat(dir_fd, sub_dir_path, mode))) {
17671782 0 => return,
......@@ -1805,7 +1820,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
18051820pub fn mkdirZ(dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
18061821 if (builtin.os.tag == .windows) {
18071822 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
1808 const sub_dir_handle = try windows.CreateDirectoryW(null, &dir_path_w, null);
1823 const sub_dir_handle = try windows.CreateDirectoryW(null, dir_path_w.span().ptr, null);
18091824 windows.CloseHandle(sub_dir_handle);
18101825 return;
18111826 }
......@@ -1846,7 +1861,7 @@ pub const DeleteDirError = error{
18461861pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
18471862 if (builtin.os.tag == .windows) {
18481863 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
1849 return windows.RemoveDirectoryW(&dir_path_w);
1864 return windows.RemoveDirectoryW(dir_path_w.span().ptr);
18501865 } else {
18511866 const dir_path_c = try toPosixPath(dir_path);
18521867 return rmdirZ(&dir_path_c);
......@@ -1859,7 +1874,7 @@ pub const rmdirC = @compileError("deprecated: renamed to rmdirZ");
18591874pub fn rmdirZ(dir_path: [*:0]const u8) DeleteDirError!void {
18601875 if (builtin.os.tag == .windows) {
18611876 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
1862 return windows.RemoveDirectoryW(&dir_path_w);
1877 return windows.RemoveDirectoryW(dir_path_w.span().ptr);
18631878 }
18641879 switch (errno(system.rmdir(dir_path))) {
18651880 0 => return,
......@@ -2869,7 +2884,7 @@ pub const AccessError = error{
28692884pub fn access(path: []const u8, mode: u32) AccessError!void {
28702885 if (builtin.os.tag == .windows) {
28712886 const path_w = try windows.sliceToPrefixedFileW(path);
2872 _ = try windows.GetFileAttributesW(&path_w);
2887 _ = try windows.GetFileAttributesW(path_w.span().ptr);
28732888 return;
28742889 }
28752890 const path_c = try toPosixPath(path);
......@@ -2882,7 +2897,7 @@ pub const accessC = @compileError("Deprecated in favor of `accessZ`");
28822897pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {
28832898 if (builtin.os.tag == .windows) {
28842899 const path_w = try windows.cStrToPrefixedFileW(path);
2885 _ = try windows.GetFileAttributesW(&path_w);
2900 _ = try windows.GetFileAttributesW(path_w.span().ptr);
28862901 return;
28872902 }
28882903 switch (errno(system.access(path, mode))) {
......@@ -2923,7 +2938,7 @@ pub fn accessW(path: [*:0]const u16, mode: u32) windows.GetFileAttributesError!v
29232938pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessError!void {
29242939 if (builtin.os.tag == .windows) {
29252940 const path_w = try windows.sliceToPrefixedFileW(path);
2926 return faccessatW(dirfd, &path_w, mode, flags);
2941 return faccessatW(dirfd, path_w.span().ptr, mode, flags);
29272942 }
29282943 const path_c = try toPosixPath(path);
29292944 return faccessatZ(dirfd, &path_c, mode, flags);
......@@ -2933,7 +2948,7 @@ pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessErr
29332948pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) AccessError!void {
29342949 if (builtin.os.tag == .windows) {
29352950 const path_w = try windows.cStrToPrefixedFileW(path);
2936 return faccessatW(dirfd, &path_w, mode, flags);
2951 return faccessatW(dirfd, path_w.span().ptr, mode, flags);
29372952 }
29382953 switch (errno(system.faccessat(dirfd, path, mode, flags))) {
29392954 0 => return,
......@@ -3288,7 +3303,7 @@ pub const RealPathError = error{
32883303pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
32893304 if (builtin.os.tag == .windows) {
32903305 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
3291 return realpathW(&pathname_w, out_buffer);
3306 return realpathW(pathname_w.span().ptr, out_buffer);
32923307 }
32933308 const pathname_c = try toPosixPath(pathname);
32943309 return realpathZ(&pathname_c, out_buffer);
......@@ -3300,7 +3315,7 @@ pub const realpathC = @compileError("deprecated: renamed realpathZ");
33003315pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
33013316 if (builtin.os.tag == .windows) {
33023317 const pathname_w = try windows.cStrToPrefixedFileW(pathname);
3303 return realpathW(&pathname_w, out_buffer);
3318 return realpathW(pathname_w.span().ptr, out_buffer);
33043319 }
33053320 if (builtin.os.tag == .linux and !builtin.link_libc) {
33063321 const fd = openZ(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0) catch |err| switch (err) {
lib/std/os/windows.zig+110-116
......@@ -59,7 +59,7 @@ pub fn CreateFile(
5959 hTemplateFile: ?HANDLE,
6060) CreateFileError!HANDLE {
6161 const file_path_w = try sliceToPrefixedFileW(file_path);
62 return CreateFileW(&file_path_w, desired_access, share_mode, lpSecurityAttributes, creation_disposition, flags_and_attrs, hTemplateFile);
62 return CreateFileW(file_path_w.span().ptr, desired_access, share_mode, lpSecurityAttributes, creation_disposition, flags_and_attrs, hTemplateFile);
6363}
6464
6565pub fn CreateFileW(
......@@ -103,57 +103,59 @@ pub const OpenError = error{
103103 WouldBlock,
104104};
105105
106/// TODO rename to CreateFileW
107/// TODO actually we don't need the path parameter to be null terminated
108pub fn OpenFileW(
109 dir: ?HANDLE,
110 sub_path_w: [*:0]const u16,
111 sa: ?*SECURITY_ATTRIBUTES,
106pub const OpenFileOptions = struct {
112107 access_mask: ACCESS_MASK,
113 share_access_opt: ?ULONG,
114 share_access_nonblocking: bool,
108 dir: ?HANDLE = null,
109 sa: ?*SECURITY_ATTRIBUTES = null,
110 share_access: ULONG = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
111 share_access_nonblocking: bool = false,
115112 creation: ULONG,
116) OpenError!HANDLE {
117 if (sub_path_w[0] == '.' and sub_path_w[1] == 0) {
113 io_mode: std.io.ModeOverride,
114};
115
116/// TODO when share_access_nonblocking is false, this implementation uses
117/// untinterruptible sleep() to block. This is not the final iteration of the API.
118pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HANDLE {
119 if (mem.eql(u16, sub_path_w, &[_]u16{'.'})) {
118120 return error.IsDir;
119121 }
120 if (sub_path_w[0] == '.' and sub_path_w[1] == '.' and sub_path_w[2] == 0) {
122 if (mem.eql(u16, sub_path_w, &[_]u16{ '.', '.' })) {
121123 return error.IsDir;
122124 }
123125
124126 var result: HANDLE = undefined;
125127
126 const path_len_bytes = math.cast(u16, mem.lenZ(sub_path_w) * 2) catch |err| switch (err) {
128 const path_len_bytes = math.cast(u16, sub_path_w.len * 2) catch |err| switch (err) {
127129 error.Overflow => return error.NameTooLong,
128130 };
129131 var nt_name = UNICODE_STRING{
130132 .Length = path_len_bytes,
131133 .MaximumLength = path_len_bytes,
132 .Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w)),
134 .Buffer = @intToPtr([*]u16, @ptrToInt(sub_path_w.ptr)),
133135 };
134136 var attr = OBJECT_ATTRIBUTES{
135137 .Length = @sizeOf(OBJECT_ATTRIBUTES),
136 .RootDirectory = if (std.fs.path.isAbsoluteWindowsW(sub_path_w)) null else dir,
138 .RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(sub_path_w)) null else options.dir,
137139 .Attributes = 0, // Note we do not use OBJ_CASE_INSENSITIVE here.
138140 .ObjectName = &nt_name,
139 .SecurityDescriptor = if (sa) |ptr| ptr.lpSecurityDescriptor else null,
141 .SecurityDescriptor = if (options.sa) |ptr| ptr.lpSecurityDescriptor else null,
140142 .SecurityQualityOfService = null,
141143 };
142144 var io: IO_STATUS_BLOCK = undefined;
143 const share_access = share_access_opt orelse (FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE);
144145
145146 var delay: usize = 1;
146147 while (true) {
148 const blocking_flag: ULONG = if (options.io_mode == .blocking) FILE_SYNCHRONOUS_IO_NONALERT else 0;
147149 const rc = ntdll.NtCreateFile(
148150 &result,
149 access_mask,
151 options.access_mask,
150152 &attr,
151153 &io,
152154 null,
153155 FILE_ATTRIBUTE_NORMAL,
154 share_access,
155 creation,
156 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT,
156 options.share_access,
157 options.creation,
158 FILE_NON_DIRECTORY_FILE | blocking_flag,
157159 null,
158160 0,
159161 );
......@@ -165,14 +167,16 @@ pub fn OpenFileW(
165167 .NO_MEDIA_IN_DEVICE => return error.NoDevice,
166168 .INVALID_PARAMETER => unreachable,
167169 .SHARING_VIOLATION => {
168 if (share_access_nonblocking) {
170 if (options.share_access_nonblocking) {
169171 return error.WouldBlock;
170172 }
173 // TODO sleep in a way that is interruptable
174 // TODO integrate with async I/O
171175 std.time.sleep(delay);
172176 if (delay < 1 * std.time.ns_per_s) {
173177 delay *= 2;
174178 }
175 continue; // TODO: don't loop for async
179 continue;
176180 },
177181 .ACCESS_DENIED => return error.AccessDenied,
178182 .PIPE_BUSY => return error.PipeBusy,
......@@ -195,7 +199,7 @@ pub fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) C
195199
196200pub fn CreateEventEx(attributes: ?*SECURITY_ATTRIBUTES, name: []const u8, flags: DWORD, desired_access: DWORD) !HANDLE {
197201 const nameW = try sliceToPrefixedFileW(name);
198 return CreateEventExW(attributes, &nameW, flags, desired_access);
202 return CreateEventExW(attributes, nameW.span().ptr, flags, desired_access);
199203}
200204
201205pub fn CreateEventExW(attributes: ?*SECURITY_ATTRIBUTES, nameW: [*:0]const u16, flags: DWORD, desired_access: DWORD) !HANDLE {
......@@ -328,42 +332,6 @@ pub fn WaitForMultipleObjectsEx(handles: []const HANDLE, waitAll: bool, millisec
328332 }
329333}
330334
331pub const FindFirstFileError = error{
332 FileNotFound,
333 InvalidUtf8,
334 BadPathName,
335 NameTooLong,
336 Unexpected,
337};
338
339pub fn FindFirstFile(dir_path: []const u8, find_file_data: *WIN32_FIND_DATAW) FindFirstFileError!HANDLE {
340 const dir_path_w = try sliceToPrefixedSuffixedFileW(dir_path, [_]u16{ '\\', '*' });
341 const handle = kernel32.FindFirstFileW(&dir_path_w, find_file_data);
342
343 if (handle == INVALID_HANDLE_VALUE) {
344 switch (kernel32.GetLastError()) {
345 .FILE_NOT_FOUND => return error.FileNotFound,
346 .PATH_NOT_FOUND => return error.FileNotFound,
347 else => |err| return unexpectedError(err),
348 }
349 }
350
351 return handle;
352}
353
354pub const FindNextFileError = error{Unexpected};
355
356/// Returns `true` if there was another file, `false` otherwise.
357pub fn FindNextFile(handle: HANDLE, find_file_data: *WIN32_FIND_DATAW) FindNextFileError!bool {
358 if (kernel32.FindNextFileW(handle, find_file_data) == 0) {
359 switch (kernel32.GetLastError()) {
360 .NO_MORE_FILES => return false,
361 else => |err| return unexpectedError(err),
362 }
363 }
364 return true;
365}
366
367335pub const CreateIoCompletionPortError = error{Unexpected};
368336
369337pub fn CreateIoCompletionPort(
......@@ -447,10 +415,11 @@ pub const ReadFileError = error{
447415
448416/// If buffer's length exceeds what a Windows DWORD integer can hold, it will be broken into
449417/// multiple non-atomic reads.
450pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usize {
451 if (std.event.Loop.instance) |loop| {
452 // TODO support async ReadFile with no offset
453 const off = offset.?;
418pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64, io_mode: std.io.ModeOverride) ReadFileError!usize {
419 if (io_mode != .blocking) {
420 const loop = std.event.Loop.instance.?;
421 // TODO make getting the file position non-blocking
422 const off = if (offset) |o| o else try SetFilePointerEx_CURRENT_get(in_hFile);
454423 var resume_node = std.event.Loop.ResumeNode.Basic{
455424 .base = .{
456425 .id = .Basic,
......@@ -465,22 +434,27 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usiz
465434 },
466435 };
467436 // TODO only call create io completion port once per fd
468 _ = windows.CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined) catch undefined;
437 _ = CreateIoCompletionPort(in_hFile, loop.os_data.io_port, undefined, undefined) catch undefined;
469438 loop.beginOneEvent();
470439 suspend {
471440 // TODO handle buffer bigger than DWORD can hold
472 _ = windows.kernel32.ReadFile(fd, buffer.ptr, @intCast(windows.DWORD, buffer.len), null, &resume_node.base.overlapped);
441 _ = kernel32.ReadFile(in_hFile, buffer.ptr, @intCast(DWORD, buffer.len), null, &resume_node.base.overlapped);
473442 }
474 var bytes_transferred: windows.DWORD = undefined;
475 if (windows.kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, windows.FALSE) == 0) {
476 switch (windows.kernel32.GetLastError()) {
443 var bytes_transferred: DWORD = undefined;
444 if (kernel32.GetOverlappedResult(in_hFile, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
445 switch (kernel32.GetLastError()) {
477446 .IO_PENDING => unreachable,
478447 .OPERATION_ABORTED => return error.OperationAborted,
479448 .BROKEN_PIPE => return error.BrokenPipe,
480449 .HANDLE_EOF => return @as(usize, bytes_transferred),
481 else => |err| return windows.unexpectedError(err),
450 else => |err| return unexpectedError(err),
482451 }
483452 }
453 if (offset == null) {
454 // TODO make setting the file position non-blocking
455 const new_off = off + bytes_transferred;
456 try SetFilePointerEx_CURRENT(in_hFile, @bitCast(i64, new_off));
457 }
484458 return @as(usize, bytes_transferred);
485459 } else {
486460 var index: usize = 0;
......@@ -520,10 +494,16 @@ pub const WriteFileError = error{
520494 Unexpected,
521495};
522496
523pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError!usize {
524 if (std.event.Loop.instance) |loop| {
525 // TODO support async WriteFile with no offset
526 const off = offset.?;
497pub fn WriteFile(
498 handle: HANDLE,
499 bytes: []const u8,
500 offset: ?u64,
501 io_mode: std.io.ModeOverride,
502) WriteFileError!usize {
503 if (std.event.Loop.instance != null and io_mode != .blocking) {
504 const loop = std.event.Loop.instance.?;
505 // TODO make getting the file position non-blocking
506 const off = if (offset) |o| o else try SetFilePointerEx_CURRENT_get(handle);
527507 var resume_node = std.event.Loop.ResumeNode.Basic{
528508 .base = .{
529509 .id = .Basic,
......@@ -538,14 +518,14 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
538518 },
539519 };
540520 // TODO only call create io completion port once per fd
541 _ = CreateIoCompletionPort(fd, loop.os_data.io_port, undefined, undefined);
521 _ = CreateIoCompletionPort(handle, loop.os_data.io_port, undefined, undefined) catch undefined;
542522 loop.beginOneEvent();
543523 suspend {
544 const adjusted_len = math.cast(windows.DWORD, bytes.len) catch maxInt(windows.DWORD);
545 _ = kernel32.WriteFile(fd, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
524 const adjusted_len = math.cast(DWORD, bytes.len) catch maxInt(DWORD);
525 _ = kernel32.WriteFile(handle, bytes.ptr, adjusted_len, null, &resume_node.base.overlapped);
546526 }
547 var bytes_transferred: windows.DWORD = undefined;
548 if (kernel32.GetOverlappedResult(fd, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
527 var bytes_transferred: DWORD = undefined;
528 if (kernel32.GetOverlappedResult(handle, &resume_node.base.overlapped, &bytes_transferred, FALSE) == 0) {
549529 switch (kernel32.GetLastError()) {
550530 .IO_PENDING => unreachable,
551531 .INVALID_USER_BUFFER => return error.SystemResources,
......@@ -553,9 +533,14 @@ pub fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64) WriteFileError
553533 .OPERATION_ABORTED => return error.OperationAborted,
554534 .NOT_ENOUGH_QUOTA => return error.SystemResources,
555535 .BROKEN_PIPE => return error.BrokenPipe,
556 else => |err| return windows.unexpectedError(err),
536 else => |err| return unexpectedError(err),
557537 }
558538 }
539 if (offset == null) {
540 // TODO make setting the file position non-blocking
541 const new_off = off + bytes_transferred;
542 try SetFilePointerEx_CURRENT(handle, @bitCast(i64, new_off));
543 }
559544 return bytes_transferred;
560545 } else {
561546 var bytes_written: DWORD = undefined;
......@@ -623,7 +608,7 @@ pub fn CreateSymbolicLink(
623608) CreateSymbolicLinkError!void {
624609 const sym_link_path_w = try sliceToPrefixedFileW(sym_link_path);
625610 const target_path_w = try sliceToPrefixedFileW(target_path);
626 return CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, flags);
611 return CreateSymbolicLinkW(sym_link_path_w.span().ptr, target_path_w.span().ptr, flags);
627612}
628613
629614pub fn CreateSymbolicLinkW(
......@@ -648,7 +633,7 @@ pub const DeleteFileError = error{
648633
649634pub fn DeleteFile(filename: []const u8) DeleteFileError!void {
650635 const filename_w = try sliceToPrefixedFileW(filename);
651 return DeleteFileW(&filename_w);
636 return DeleteFileW(filename_w.span().ptr);
652637}
653638
654639pub fn DeleteFileW(filename: [*:0]const u16) DeleteFileError!void {
......@@ -670,7 +655,7 @@ pub const MoveFileError = error{Unexpected};
670655pub fn MoveFileEx(old_path: []const u8, new_path: []const u8, flags: DWORD) MoveFileError!void {
671656 const old_path_w = try sliceToPrefixedFileW(old_path);
672657 const new_path_w = try sliceToPrefixedFileW(new_path);
673 return MoveFileExW(&old_path_w, &new_path_w, flags);
658 return MoveFileExW(old_path_w.span().ptr, new_path_w.span().ptr, flags);
674659}
675660
676661pub fn MoveFileExW(old_path: [*:0]const u16, new_path: [*:0]const u16, flags: DWORD) MoveFileError!void {
......@@ -695,7 +680,7 @@ pub const CreateDirectoryError = error{
695680/// Returns an open directory handle which the caller is responsible for closing with `CloseHandle`.
696681pub fn CreateDirectory(dir: ?HANDLE, pathname: []const u8, sa: ?*SECURITY_ATTRIBUTES) CreateDirectoryError!HANDLE {
697682 const pathname_w = try sliceToPrefixedFileW(pathname);
698 return CreateDirectoryW(dir, &pathname_w, sa);
683 return CreateDirectoryW(dir, pathname_w.span().ptr, sa);
699684}
700685
701686/// Same as `CreateDirectory` except takes a WTF-16 encoded path.
......@@ -763,7 +748,7 @@ pub const RemoveDirectoryError = error{
763748
764749pub fn RemoveDirectory(dir_path: []const u8) RemoveDirectoryError!void {
765750 const dir_path_w = try sliceToPrefixedFileW(dir_path);
766 return RemoveDirectoryW(&dir_path_w);
751 return RemoveDirectoryW(dir_path_w.span().ptr);
767752}
768753
769754pub fn RemoveDirectoryW(dir_path_w: [*:0]const u16) RemoveDirectoryError!void {
......@@ -892,7 +877,7 @@ pub const GetFileAttributesError = error{
892877
893878pub fn GetFileAttributes(filename: []const u8) GetFileAttributesError!DWORD {
894879 const filename_w = try sliceToPrefixedFileW(filename);
895 return GetFileAttributesW(&filename_w);
880 return GetFileAttributesW(filename_w.span().ptr);
896881}
897882
898883pub fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWORD {
......@@ -1232,34 +1217,22 @@ pub fn nanoSecondsToFileTime(ns: i64) FILETIME {
12321217 };
12331218}
12341219
1235pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {
1236 return sliceToPrefixedFileW(mem.spanZ(s));
1237}
1220pub const PathSpace = struct {
1221 data: [PATH_MAX_WIDE:0]u16,
1222 len: usize,
12381223
1239pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE:0]u16 {
1240 return sliceToPrefixedSuffixedFileW(s, &[_]u16{});
1241}
1242
1243/// Assumes an absolute path.
1244pub fn wToPrefixedFileW(s: []const u16) ![PATH_MAX_WIDE:0]u16 {
1245 // TODO https://github.com/ziglang/zig/issues/2765
1246 var result: [PATH_MAX_WIDE:0]u16 = undefined;
1224 pub fn span(self: PathSpace) [:0]const u16 {
1225 return self.data[0..self.len :0];
1226 }
1227};
12471228
1248 const start_index = if (mem.startsWith(u16, s, &[_]u16{ '\\', '?' })) 0 else blk: {
1249 const prefix = [_]u16{ '\\', '?', '?', '\\' };
1250 mem.copy(u16, result[0..], &prefix);
1251 break :blk prefix.len;
1252 };
1253 const end_index = start_index + s.len;
1254 if (end_index + 1 > result.len) return error.NameTooLong;
1255 mem.copy(u16, result[start_index..], s);
1256 result[end_index] = 0;
1257 return result;
1229pub fn cStrToPrefixedFileW(s: [*:0]const u8) !PathSpace {
1230 return sliceToPrefixedFileW(mem.spanZ(s));
12581231}
12591232
1260pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) ![PATH_MAX_WIDE + suffix.len:0]u16 {
1233pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace {
12611234 // TODO https://github.com/ziglang/zig/issues/2765
1262 var result: [PATH_MAX_WIDE + suffix.len:0]u16 = undefined;
1235 var path_space: PathSpace = undefined;
12631236 for (s) |byte| {
12641237 switch (byte) {
12651238 '*', '?', '"', '<', '>', '|' => return error.BadPathName,
......@@ -1268,25 +1241,46 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16)
12681241 }
12691242 const start_index = if (mem.startsWith(u8, s, "\\?") or !std.fs.path.isAbsolute(s)) 0 else blk: {
12701243 const prefix = [_]u16{ '\\', '?', '?', '\\' };
1271 mem.copy(u16, result[0..], &prefix);
1244 mem.copy(u16, path_space.data[0..], &prefix);
12721245 break :blk prefix.len;
12731246 };
1274 const end_index = start_index + try std.unicode.utf8ToUtf16Le(result[start_index..], s);
1275 if (end_index + suffix.len > result.len) return error.NameTooLong;
1247 path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s);
1248 if (path_space.len > path_space.data.len) return error.NameTooLong;
12761249 // > File I/O functions in the Windows API convert "/" to "\" as part of
12771250 // > converting the name to an NT-style name, except when using the "\\?\"
12781251 // > prefix as detailed in the following sections.
12791252 // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
12801253 // Because we want the larger maximum path length for absolute paths, we
12811254 // convert forward slashes to backward slashes here.
1282 for (result[0..end_index]) |*elem| {
1255 for (path_space.data[0..path_space.len]) |*elem| {
12831256 if (elem.* == '/') {
12841257 elem.* = '\\';
12851258 }
12861259 }
1287 mem.copy(u16, result[end_index..], suffix);
1288 result[end_index + suffix.len] = 0;
1289 return result;
1260 path_space.data[path_space.len] = 0;
1261 return path_space;
1262}
1263
1264/// Assumes an absolute path.
1265pub fn wToPrefixedFileW(s: []const u16) !PathSpace {
1266 // TODO https://github.com/ziglang/zig/issues/2765
1267 var path_space: PathSpace = undefined;
1268
1269 const start_index = if (mem.startsWith(u16, s, &[_]u16{ '\\', '?' })) 0 else blk: {
1270 const prefix = [_]u16{ '\\', '?', '?', '\\' };
1271 mem.copy(u16, path_space.data[0..], &prefix);
1272 break :blk prefix.len;
1273 };
1274 path_space.len = start_index + s.len;
1275 if (path_space.len > path_space.data.len) return error.NameTooLong;
1276 mem.copy(u16, path_space.data[start_index..], s);
1277 for (path_space.data[0..path_space.len]) |*elem| {
1278 if (elem.* == '/') {
1279 elem.* = '\\';
1280 }
1281 }
1282 path_space.data[path_space.len] = 0;
1283 return path_space;
12901284}
12911285
12921286inline fn MAKELANGID(p: c_ushort, s: c_ushort) LANGID {
lib/std/pdb.zig+1-1
......@@ -470,7 +470,7 @@ pub const Pdb = struct {
470470 msf: Msf,
471471
472472 pub fn openFile(self: *Pdb, coff_ptr: *coff.Coff, file_name: []u8) !void {
473 self.in_file = try fs.cwd().openFile(file_name, .{});
473 self.in_file = try fs.cwd().openFile(file_name, .{ .intended_io_mode = .blocking });
474474 self.allocator = coff_ptr.allocator;
475475 self.coff = coff_ptr;
476476
lib/std/reset_event.zig+1-1
......@@ -52,7 +52,7 @@ pub const ResetEvent = struct {
5252
5353 /// Wait for the event to be set by blocking the current thread.
5454 /// A timeout in nanoseconds can be provided as a hint for how
55 /// long the thread should block on the unset event before throwind error.TimedOut.
55 /// long the thread should block on the unset event before throwing error.TimedOut.
5656 pub fn timedWait(self: *ResetEvent, timeout_ns: u64) !void {
5757 return self.os_event.wait(timeout_ns);
5858 }
src-self-hosted/test.zig+1-1
......@@ -96,7 +96,7 @@ pub const TestContext = struct {
9696 case: ZIRCompareOutputCase,
9797 target: std.Target,
9898 ) !void {
99 var tmp = std.testing.tmpDir(.{ .share_with_child_process = true });
99 var tmp = std.testing.tmpDir(.{});
100100 defer tmp.cleanup();
101101
102102 var prg_node = root_node.start(case.name, 4);