| author | |
| committer | |
| log | 3d780cf2ef8391b6b48124f599858ee99ddc4cdc |
| tree | 5e073a9784a6fa4699e0eca9a3eb0148756e6722 |
| parent | b2917e6be09138adcf7cfdab51a1909a30eec320 |
| parent | 3dd1026c8bcb438228c336add7cc4014552aa05c |
This does a proof of concept of changing most file system APIs to not
require an allocator and remove the possibility of failure via
OutOfMemory.
This also does most of the work of #534.25 files changed, 794 insertions(+), 566 deletions(-)
build.zig+1-1| ... | ... | @@ -19,7 +19,7 @@ pub fn build(b: *Builder) !void { |
| 19 | 19 | var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8{ |
| 20 | 20 | docgen_exe.getOutputPath(), |
| 21 | 21 | rel_zig_exe, |
| 22 | "doc/langref.html.in", | |
| 22 | "doc" ++ os.path.sep_str ++ "langref.html.in", | |
| 23 | 23 | os.path.join(b.allocator, b.cache_root, "langref.html") catch unreachable, |
| 24 | 24 | }); |
| 25 | 25 | docgen_cmd.step.dependOn(&docgen_exe.step); |
doc/docgen.zig+3-3| ... | ... | @@ -34,10 +34,10 @@ pub fn main() !void { |
| 34 | 34 | const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg")); |
| 35 | 35 | defer allocator.free(out_file_name); |
| 36 | 36 | |
| 37 | var in_file = try os.File.openRead(allocator, in_file_name); | |
| 37 | var in_file = try os.File.openRead(in_file_name); | |
| 38 | 38 | defer in_file.close(); |
| 39 | 39 | |
| 40 | var out_file = try os.File.openWrite(allocator, out_file_name); | |
| 40 | var out_file = try os.File.openWrite(out_file_name); | |
| 41 | 41 | defer out_file.close(); |
| 42 | 42 | |
| 43 | 43 | var file_in_stream = io.FileInStream.init(&in_file); |
| ... | ... | @@ -738,7 +738,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var |
| 738 | 738 | try out.print("<pre><code class=\"zig\">{}</code></pre>", escaped_source); |
| 739 | 739 | const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name); |
| 740 | 740 | const tmp_source_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_ext); |
| 741 | try io.writeFile(allocator, tmp_source_file_name, trimmed_raw_source); | |
| 741 | try io.writeFile(tmp_source_file_name, trimmed_raw_source); | |
| 742 | 742 | |
| 743 | 743 | switch (code.id) { |
| 744 | 744 | Code.Id.Exe => |expected_outcome| { |
example/cat/main.zig+1-1| ... | ... | @@ -20,7 +20,7 @@ pub fn main() !void { |
| 20 | 20 | } else if (arg[0] == '-') { |
| 21 | 21 | return usage(exe); |
| 22 | 22 | } else { |
| 23 | var file = os.File.openRead(allocator, arg) catch |err| { | |
| 23 | var file = os.File.openRead(arg) catch |err| { | |
| 24 | 24 | warn("Unable to open file: {}\n", @errorName(err)); |
| 25 | 25 | return err; |
| 26 | 26 | }; |
src-self-hosted/compilation.zig+2-4| ... | ... | @@ -257,8 +257,6 @@ pub const Compilation = struct { |
| 257 | 257 | pub const BuildError = error{ |
| 258 | 258 | OutOfMemory, |
| 259 | 259 | EndOfStream, |
| 260 | BadFd, | |
| 261 | Io, | |
| 262 | 260 | IsDir, |
| 263 | 261 | Unexpected, |
| 264 | 262 | SystemResources, |
| ... | ... | @@ -273,7 +271,6 @@ pub const Compilation = struct { |
| 273 | 271 | NameTooLong, |
| 274 | 272 | SystemFdQuotaExceeded, |
| 275 | 273 | NoDevice, |
| 276 | PathNotFound, | |
| 277 | 274 | NoSpaceLeft, |
| 278 | 275 | NotDir, |
| 279 | 276 | FileSystem, |
| ... | ... | @@ -302,6 +299,7 @@ pub const Compilation = struct { |
| 302 | 299 | UnsupportedLinkArchitecture, |
| 303 | 300 | UserResourceLimitReached, |
| 304 | 301 | InvalidUtf8, |
| 302 | BadPathName, | |
| 305 | 303 | }; |
| 306 | 304 | |
| 307 | 305 | pub const Event = union(enum) { |
| ... | ... | @@ -961,7 +959,7 @@ pub const Compilation = struct { |
| 961 | 959 | if (self.root_src_path) |root_src_path| { |
| 962 | 960 | const root_scope = blk: { |
| 963 | 961 | // TODO async/await os.path.real |
| 964 | const root_src_real_path = os.path.real(self.gpa(), root_src_path) catch |err| { | |
| 962 | const root_src_real_path = os.path.realAlloc(self.gpa(), root_src_path) catch |err| { | |
| 965 | 963 | try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err)); |
| 966 | 964 | return; |
| 967 | 965 | }; |
src-self-hosted/errmsg.zig+1-1| ... | ... | @@ -235,7 +235,7 @@ pub const Msg = struct { |
| 235 | 235 | const allocator = msg.getAllocator(); |
| 236 | 236 | const tree = msg.getTree(); |
| 237 | 237 | |
| 238 | const cwd = try os.getCwd(allocator); | |
| 238 | const cwd = try os.getCwdAlloc(allocator); | |
| 239 | 239 | defer allocator.free(cwd); |
| 240 | 240 | |
| 241 | 241 | const relpath = try os.path.relative(allocator, cwd, msg.realpath); |
src-self-hosted/introspect.zig+2-2| ... | ... | @@ -14,7 +14,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ |
| 14 | 14 | const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig"); |
| 15 | 15 | defer allocator.free(test_index_file); |
| 16 | 16 | |
| 17 | var file = try os.File.openRead(allocator, test_index_file); | |
| 17 | var file = try os.File.openRead(test_index_file); | |
| 18 | 18 | file.close(); |
| 19 | 19 | |
| 20 | 20 | return test_zig_dir; |
| ... | ... | @@ -22,7 +22,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ |
| 22 | 22 | |
| 23 | 23 | /// Caller must free result |
| 24 | 24 | pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { |
| 25 | const self_exe_path = try os.selfExeDirPath(allocator); | |
| 25 | const self_exe_path = try os.selfExeDirPathAlloc(allocator); | |
| 26 | 26 | defer allocator.free(self_exe_path); |
| 27 | 27 | |
| 28 | 28 | var cur_path: []const u8 = self_exe_path; |
src-self-hosted/libc_installation.zig+7-8| ... | ... | @@ -233,7 +233,7 @@ pub const LibCInstallation = struct { |
| 233 | 233 | const stdlib_path = try std.os.path.join(loop.allocator, search_path, "stdlib.h"); |
| 234 | 234 | defer loop.allocator.free(stdlib_path); |
| 235 | 235 | |
| 236 | if (try fileExists(loop.allocator, stdlib_path)) { | |
| 236 | if (try fileExists(stdlib_path)) { | |
| 237 | 237 | self.include_dir = try std.mem.dupe(loop.allocator, u8, search_path); |
| 238 | 238 | return; |
| 239 | 239 | } |
| ... | ... | @@ -257,7 +257,7 @@ pub const LibCInstallation = struct { |
| 257 | 257 | const stdlib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "stdlib.h"); |
| 258 | 258 | defer loop.allocator.free(stdlib_path); |
| 259 | 259 | |
| 260 | if (try fileExists(loop.allocator, stdlib_path)) { | |
| 260 | if (try fileExists(stdlib_path)) { | |
| 261 | 261 | self.include_dir = result_buf.toOwnedSlice(); |
| 262 | 262 | return; |
| 263 | 263 | } |
| ... | ... | @@ -285,7 +285,7 @@ pub const LibCInstallation = struct { |
| 285 | 285 | } |
| 286 | 286 | const ucrt_lib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "ucrt.lib"); |
| 287 | 287 | defer loop.allocator.free(ucrt_lib_path); |
| 288 | if (try fileExists(loop.allocator, ucrt_lib_path)) { | |
| 288 | if (try fileExists(ucrt_lib_path)) { | |
| 289 | 289 | self.lib_dir = result_buf.toOwnedSlice(); |
| 290 | 290 | return; |
| 291 | 291 | } |
| ... | ... | @@ -360,7 +360,7 @@ pub const LibCInstallation = struct { |
| 360 | 360 | } |
| 361 | 361 | const kernel32_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "kernel32.lib"); |
| 362 | 362 | defer loop.allocator.free(kernel32_path); |
| 363 | if (try fileExists(loop.allocator, kernel32_path)) { | |
| 363 | if (try fileExists(kernel32_path)) { | |
| 364 | 364 | self.kernel32_lib_dir = result_buf.toOwnedSlice(); |
| 365 | 365 | return; |
| 366 | 366 | } |
| ... | ... | @@ -449,12 +449,11 @@ fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search { |
| 449 | 449 | return search_buf[0..search_end]; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | fn fileExists(allocator: *std.mem.Allocator, path: []const u8) !bool { | |
| 453 | if (std.os.File.access(allocator, path)) |_| { | |
| 452 | fn fileExists(path: []const u8) !bool { | |
| 453 | if (std.os.File.access(path)) |_| { | |
| 454 | 454 | return true; |
| 455 | 455 | } else |err| switch (err) { |
| 456 | error.NotFound, error.PermissionDenied => return false, | |
| 457 | error.OutOfMemory => return error.OutOfMemory, | |
| 456 | error.FileNotFound, error.PermissionDenied => return false, | |
| 458 | 457 | else => return error.FileSystem, |
| 459 | 458 | } |
| 460 | 459 | } |
src-self-hosted/test.zig+2-2| ... | ... | @@ -94,7 +94,7 @@ pub const TestContext = struct { |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | // TODO async I/O |
| 97 | try std.io.writeFile(allocator, file1_path, source); | |
| 97 | try std.io.writeFile(file1_path, source); | |
| 98 | 98 | |
| 99 | 99 | var comp = try Compilation.create( |
| 100 | 100 | &self.zig_compiler, |
| ... | ... | @@ -128,7 +128,7 @@ pub const TestContext = struct { |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // TODO async I/O |
| 131 | try std.io.writeFile(allocator, file1_path, source); | |
| 131 | try std.io.writeFile(file1_path, source); | |
| 132 | 132 | |
| 133 | 133 | var comp = try Compilation.create( |
| 134 | 134 | &self.zig_compiler, |
std/build.zig+15-9| ... | ... | @@ -267,7 +267,7 @@ pub const Builder = struct { |
| 267 | 267 | if (self.verbose) { |
| 268 | 268 | warn("rm {}\n", installed_file); |
| 269 | 269 | } |
| 270 | _ = os.deleteFile(self.allocator, installed_file); | |
| 270 | _ = os.deleteFile(installed_file); | |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // TODO remove empty directories |
| ... | ... | @@ -1182,7 +1182,7 @@ pub const LibExeObjStep = struct { |
| 1182 | 1182 | |
| 1183 | 1183 | if (self.build_options_contents.len() > 0) { |
| 1184 | 1184 | const build_options_file = try os.path.join(builder.allocator, builder.cache_root, builder.fmt("{}_build_options.zig", self.name)); |
| 1185 | try std.io.writeFile(builder.allocator, build_options_file, self.build_options_contents.toSliceConst()); | |
| 1185 | try std.io.writeFile(build_options_file, self.build_options_contents.toSliceConst()); | |
| 1186 | 1186 | try zig_args.append("--pkg-begin"); |
| 1187 | 1187 | try zig_args.append("build_options"); |
| 1188 | 1188 | try zig_args.append(builder.pathFromRoot(build_options_file)); |
| ... | ... | @@ -1491,11 +1491,14 @@ pub const LibExeObjStep = struct { |
| 1491 | 1491 | } |
| 1492 | 1492 | |
| 1493 | 1493 | if (!is_darwin) { |
| 1494 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable); | |
| 1494 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", try os.path.realAlloc( | |
| 1495 | builder.allocator, | |
| 1496 | builder.pathFromRoot(builder.cache_root), | |
| 1497 | )); | |
| 1495 | 1498 | defer builder.allocator.free(rpath_arg); |
| 1496 | cc_args.append(rpath_arg) catch unreachable; | |
| 1499 | try cc_args.append(rpath_arg); | |
| 1497 | 1500 | |
| 1498 | cc_args.append("-rdynamic") catch unreachable; | |
| 1501 | try cc_args.append("-rdynamic"); | |
| 1499 | 1502 | } |
| 1500 | 1503 | |
| 1501 | 1504 | for (self.full_path_libs.toSliceConst()) |full_path_lib| { |
| ... | ... | @@ -1566,11 +1569,14 @@ pub const LibExeObjStep = struct { |
| 1566 | 1569 | cc_args.append("-o") catch unreachable; |
| 1567 | 1570 | cc_args.append(output_path) catch unreachable; |
| 1568 | 1571 | |
| 1569 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable); | |
| 1572 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", try os.path.realAlloc( | |
| 1573 | builder.allocator, | |
| 1574 | builder.pathFromRoot(builder.cache_root), | |
| 1575 | )); | |
| 1570 | 1576 | defer builder.allocator.free(rpath_arg); |
| 1571 | cc_args.append(rpath_arg) catch unreachable; | |
| 1577 | try cc_args.append(rpath_arg); | |
| 1572 | 1578 | |
| 1573 | cc_args.append("-rdynamic") catch unreachable; | |
| 1579 | try cc_args.append("-rdynamic"); | |
| 1574 | 1580 | |
| 1575 | 1581 | { |
| 1576 | 1582 | var it = self.link_libs.iterator(); |
| ... | ... | @@ -1917,7 +1923,7 @@ pub const WriteFileStep = struct { |
| 1917 | 1923 | warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); |
| 1918 | 1924 | return err; |
| 1919 | 1925 | }; |
| 1920 | io.writeFile(self.builder.allocator, full_path, self.data) catch |err| { | |
| 1926 | io.writeFile(full_path, self.data) catch |err| { | |
| 1921 | 1927 | warn("unable to write {}: {}\n", full_path, @errorName(err)); |
| 1922 | 1928 | return err; |
| 1923 | 1929 | }; |
std/cstr.zig+6-5| ... | ... | @@ -9,10 +9,9 @@ pub const line_sep = switch (builtin.os) { |
| 9 | 9 | else => "\n", |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | /// Deprecated, use mem.len | |
| 12 | 13 | pub fn len(ptr: [*]const u8) usize { |
| 13 | var count: usize = 0; | |
| 14 | while (ptr[count] != 0) : (count += 1) {} | |
| 15 | return count; | |
| 14 | return mem.len(u8, ptr); | |
| 16 | 15 | } |
| 17 | 16 | |
| 18 | 17 | pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { |
| ... | ... | @@ -27,12 +26,14 @@ pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { |
| 27 | 26 | } |
| 28 | 27 | } |
| 29 | 28 | |
| 29 | /// Deprecated, use mem.toSliceConst | |
| 30 | 30 | pub fn toSliceConst(str: [*]const u8) []const u8 { |
| 31 | return str[0..len(str)]; | |
| 31 | return mem.toSliceConst(u8, str); | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | /// Deprecated, use mem.toSlice | |
| 34 | 35 | pub fn toSlice(str: [*]u8) []u8 { |
| 35 | return str[0..len(str)]; | |
| 36 | return mem.toSlice(u8, str); | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | test "cstr fns" { |
std/debug/index.zig+3-3| ... | ... | @@ -255,7 +255,7 @@ pub fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address |
| 255 | 255 | address, |
| 256 | 256 | compile_unit_name, |
| 257 | 257 | ); |
| 258 | if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) { | |
| 258 | if (printLineFromFile(out_stream, line_info)) { | |
| 259 | 259 | if (line_info.column == 0) { |
| 260 | 260 | try out_stream.write("\n"); |
| 261 | 261 | } else { |
| ... | ... | @@ -340,8 +340,8 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) !*ElfStackTrace { |
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | fn printLineFromFile(allocator: *mem.Allocator, out_stream: var, line_info: *const LineInfo) !void { | |
| 344 | var f = try os.File.openRead(allocator, line_info.file_name); | |
| 343 | fn printLineFromFile(out_stream: var, line_info: *const LineInfo) !void { | |
| 344 | var f = try os.File.openRead(line_info.file_name); | |
| 345 | 345 | defer f.close(); |
| 346 | 346 | // TODO fstat and make sure that the file has the correct size |
| 347 | 347 |
std/event/fs.zig+24-36| ... | ... | @@ -78,8 +78,7 @@ pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, o |
| 78 | 78 | builtin.Os.macosx, |
| 79 | 79 | builtin.Os.linux, |
| 80 | 80 | => return await (async pwritevPosix(loop, fd, data, offset) catch unreachable), |
| 81 | builtin.Os.windows, | |
| 82 | => return await (async pwritevWindows(loop, fd, data, offset) catch unreachable), | |
| 81 | builtin.Os.windows => return await (async pwritevWindows(loop, fd, data, offset) catch unreachable), | |
| 83 | 82 | else => @compileError("Unsupported OS"), |
| 84 | 83 | } |
| 85 | 84 | } |
| ... | ... | @@ -147,7 +146,6 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off |
| 147 | 146 | } |
| 148 | 147 | } |
| 149 | 148 | |
| 150 | ||
| 151 | 149 | /// data - just the inner references - must live until pwritev promise completes. |
| 152 | 150 | pub async fn pwritevPosix(loop: *Loop, fd: os.FileHandle, data: []const []const u8, offset: usize) !void { |
| 153 | 151 | // workaround for https://github.com/ziglang/zig/issues/1194 |
| ... | ... | @@ -203,8 +201,7 @@ pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: |
| 203 | 201 | builtin.Os.macosx, |
| 204 | 202 | builtin.Os.linux, |
| 205 | 203 | => return await (async preadvPosix(loop, fd, data, offset) catch unreachable), |
| 206 | builtin.Os.windows, | |
| 207 | => return await (async preadvWindows(loop, fd, data, offset) catch unreachable), | |
| 204 | builtin.Os.windows => return await (async preadvWindows(loop, fd, data, offset) catch unreachable), | |
| 208 | 205 | else => @compileError("Unsupported OS"), |
| 209 | 206 | } |
| 210 | 207 | } |
| ... | ... | @@ -222,7 +219,7 @@ pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, o |
| 222 | 219 | var inner_off: usize = 0; |
| 223 | 220 | while (true) { |
| 224 | 221 | const v = data_copy[iov_i]; |
| 225 | const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len-inner_off], offset + off) catch unreachable); | |
| 222 | const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len - inner_off], offset + off) catch unreachable); | |
| 226 | 223 | off += amt_read; |
| 227 | 224 | inner_off += amt_read; |
| 228 | 225 | if (inner_off == v.len) { |
| ... | ... | @@ -340,8 +337,7 @@ pub async fn openPosix( |
| 340 | 337 | resume @handle(); |
| 341 | 338 | } |
| 342 | 339 | |
| 343 | const path_with_null = try std.cstr.addNullByte(loop.allocator, path); | |
| 344 | defer loop.allocator.free(path_with_null); | |
| 340 | const path_c = try std.os.toPosixPath(path); | |
| 345 | 341 | |
| 346 | 342 | var req_node = RequestNode{ |
| 347 | 343 | .prev = null, |
| ... | ... | @@ -349,7 +345,7 @@ pub async fn openPosix( |
| 349 | 345 | .data = Request{ |
| 350 | 346 | .msg = Request.Msg{ |
| 351 | 347 | .Open = Request.Msg.Open{ |
| 352 | .path = path_with_null[0..path.len], | |
| 348 | .path = path_c[0..path.len], | |
| 353 | 349 | .flags = flags, |
| 354 | 350 | .mode = mode, |
| 355 | 351 | .result = undefined, |
| ... | ... | @@ -382,7 +378,6 @@ pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHa |
| 382 | 378 | }, |
| 383 | 379 | |
| 384 | 380 | builtin.Os.windows => return os.windowsOpen( |
| 385 | loop.allocator, | |
| 386 | 381 | path, |
| 387 | 382 | windows.GENERIC_READ, |
| 388 | 383 | windows.FILE_SHARE_READ, |
| ... | ... | @@ -409,9 +404,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os |
| 409 | 404 | const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; |
| 410 | 405 | return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); |
| 411 | 406 | }, |
| 412 | builtin.Os.windows, | |
| 413 | => return os.windowsOpen( | |
| 414 | loop.allocator, | |
| 407 | builtin.Os.windows => return os.windowsOpen( | |
| 415 | 408 | path, |
| 416 | 409 | windows.GENERIC_WRITE, |
| 417 | 410 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| ... | ... | @@ -435,9 +428,8 @@ pub async fn openReadWrite( |
| 435 | 428 | }, |
| 436 | 429 | |
| 437 | 430 | builtin.Os.windows => return os.windowsOpen( |
| 438 | loop.allocator, | |
| 439 | 431 | path, |
| 440 | windows.GENERIC_WRITE|windows.GENERIC_READ, | |
| 432 | windows.GENERIC_WRITE | windows.GENERIC_READ, | |
| 441 | 433 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| 442 | 434 | windows.OPEN_ALWAYS, |
| 443 | 435 | windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_OVERLAPPED, |
| ... | ... | @@ -513,8 +505,7 @@ pub const CloseOperation = struct { |
| 513 | 505 | self.loop.allocator.destroy(self); |
| 514 | 506 | } |
| 515 | 507 | }, |
| 516 | builtin.Os.windows, | |
| 517 | => { | |
| 508 | builtin.Os.windows => { | |
| 518 | 509 | if (self.os_data.handle) |handle| { |
| 519 | 510 | os.close(handle); |
| 520 | 511 | } |
| ... | ... | @@ -532,8 +523,7 @@ pub const CloseOperation = struct { |
| 532 | 523 | self.os_data.close_req_node.data.msg.Close.fd = handle; |
| 533 | 524 | self.os_data.have_fd = true; |
| 534 | 525 | }, |
| 535 | builtin.Os.windows, | |
| 536 | => { | |
| 526 | builtin.Os.windows => { | |
| 537 | 527 | self.os_data.handle = handle; |
| 538 | 528 | }, |
| 539 | 529 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -548,8 +538,7 @@ pub const CloseOperation = struct { |
| 548 | 538 | => { |
| 549 | 539 | self.os_data.have_fd = false; |
| 550 | 540 | }, |
| 551 | builtin.Os.windows, | |
| 552 | => { | |
| 541 | builtin.Os.windows => { | |
| 553 | 542 | self.os_data.handle = null; |
| 554 | 543 | }, |
| 555 | 544 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -564,8 +553,7 @@ pub const CloseOperation = struct { |
| 564 | 553 | assert(self.os_data.have_fd); |
| 565 | 554 | return self.os_data.close_req_node.data.msg.Close.fd; |
| 566 | 555 | }, |
| 567 | builtin.Os.windows, | |
| 568 | => { | |
| 556 | builtin.Os.windows => { | |
| 569 | 557 | return self.os_data.handle.?; |
| 570 | 558 | }, |
| 571 | 559 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -585,15 +573,13 @@ pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, |
| 585 | 573 | builtin.Os.linux, |
| 586 | 574 | builtin.Os.macosx, |
| 587 | 575 | => return await (async writeFileModeThread(loop, path, contents, mode) catch unreachable), |
| 588 | builtin.Os.windows, | |
| 589 | => return await (async writeFileWindows(loop, path, contents) catch unreachable), | |
| 576 | builtin.Os.windows => return await (async writeFileWindows(loop, path, contents) catch unreachable), | |
| 590 | 577 | else => @compileError("Unsupported OS"), |
| 591 | 578 | } |
| 592 | 579 | } |
| 593 | 580 | |
| 594 | 581 | async fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) !void { |
| 595 | 582 | const handle = try os.windowsOpen( |
| 596 | loop.allocator, | |
| 597 | 583 | path, |
| 598 | 584 | windows.GENERIC_WRITE, |
| 599 | 585 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| ... | ... | @@ -1004,7 +990,7 @@ pub fn Watch(comptime V: type) type { |
| 1004 | 990 | const basename_utf16le_null = try std.unicode.utf8ToUtf16LeWithNull(self.channel.loop.allocator, basename); |
| 1005 | 991 | var basename_utf16le_null_consumed = false; |
| 1006 | 992 | defer if (!basename_utf16le_null_consumed) self.channel.loop.allocator.free(basename_utf16le_null); |
| 1007 | const basename_utf16le_no_null = basename_utf16le_null[0..basename_utf16le_null.len-1]; | |
| 993 | const basename_utf16le_no_null = basename_utf16le_null[0 .. basename_utf16le_null.len - 1]; | |
| 1008 | 994 | |
| 1009 | 995 | const dir_handle = windows.CreateFileW( |
| 1010 | 996 | dirname_utf16le.ptr, |
| ... | ... | @@ -1018,9 +1004,8 @@ pub fn Watch(comptime V: type) type { |
| 1018 | 1004 | if (dir_handle == windows.INVALID_HANDLE_VALUE) { |
| 1019 | 1005 | const err = windows.GetLastError(); |
| 1020 | 1006 | switch (err) { |
| 1021 | windows.ERROR.FILE_NOT_FOUND, | |
| 1022 | windows.ERROR.PATH_NOT_FOUND, | |
| 1023 | => return error.PathNotFound, | |
| 1007 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1008 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 1024 | 1009 | else => return os.unexpectedErrorWindows(err), |
| 1025 | 1010 | } |
| 1026 | 1011 | } |
| ... | ... | @@ -1106,7 +1091,10 @@ pub fn Watch(comptime V: type) type { |
| 1106 | 1091 | |
| 1107 | 1092 | // TODO handle this error not in the channel but in the setup |
| 1108 | 1093 | _ = os.windowsCreateIoCompletionPort( |
| 1109 | dir_handle, self.channel.loop.os_data.io_port, completion_key, undefined, | |
| 1094 | dir_handle, | |
| 1095 | self.channel.loop.os_data.io_port, | |
| 1096 | completion_key, | |
| 1097 | undefined, | |
| 1110 | 1098 | ) catch |err| { |
| 1111 | 1099 | await (async self.channel.put(err) catch unreachable); |
| 1112 | 1100 | return; |
| ... | ... | @@ -1126,10 +1114,10 @@ pub fn Watch(comptime V: type) type { |
| 1126 | 1114 | &event_buf, |
| 1127 | 1115 | @intCast(windows.DWORD, event_buf.len), |
| 1128 | 1116 | windows.FALSE, // watch subtree |
| 1129 | windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME | | |
| 1130 | windows.FILE_NOTIFY_CHANGE_ATTRIBUTES | windows.FILE_NOTIFY_CHANGE_SIZE | | |
| 1131 | windows.FILE_NOTIFY_CHANGE_LAST_WRITE | windows.FILE_NOTIFY_CHANGE_LAST_ACCESS | | |
| 1132 | windows.FILE_NOTIFY_CHANGE_CREATION | windows.FILE_NOTIFY_CHANGE_SECURITY, | |
| 1117 | windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME | | |
| 1118 | windows.FILE_NOTIFY_CHANGE_ATTRIBUTES | windows.FILE_NOTIFY_CHANGE_SIZE | | |
| 1119 | windows.FILE_NOTIFY_CHANGE_LAST_WRITE | windows.FILE_NOTIFY_CHANGE_LAST_ACCESS | | |
| 1120 | windows.FILE_NOTIFY_CHANGE_CREATION | windows.FILE_NOTIFY_CHANGE_SECURITY, | |
| 1133 | 1121 | null, // number of bytes transferred (unused for async) |
| 1134 | 1122 | &overlapped, |
| 1135 | 1123 | null, // completion routine - unused because we use IOCP |
| ... | ... | @@ -1156,7 +1144,7 @@ pub fn Watch(comptime V: type) type { |
| 1156 | 1144 | else => null, |
| 1157 | 1145 | }; |
| 1158 | 1146 | if (emit) |id| { |
| 1159 | const basename_utf16le = ([*]u16)(&ev.FileName)[0..ev.FileNameLength/2]; | |
| 1147 | const basename_utf16le = ([*]u16)(&ev.FileName)[0 .. ev.FileNameLength / 2]; | |
| 1160 | 1148 | const user_value = blk: { |
| 1161 | 1149 | const held = await (async dir.table_lock.acquire() catch unreachable); |
| 1162 | 1150 | defer held.release(); |
std/io.zig+3-4| ... | ... | @@ -254,9 +254,8 @@ pub fn OutStream(comptime WriteError: type) type { |
| 254 | 254 | }; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. | |
| 258 | pub fn writeFile(allocator: *mem.Allocator, path: []const u8, data: []const u8) !void { | |
| 259 | var file = try File.openWrite(allocator, path); | |
| 257 | pub fn writeFile(path: []const u8, data: []const u8) !void { | |
| 258 | var file = try File.openWrite(path); | |
| 260 | 259 | defer file.close(); |
| 261 | 260 | try file.write(data); |
| 262 | 261 | } |
| ... | ... | @@ -268,7 +267,7 @@ pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 { |
| 268 | 267 | |
| 269 | 268 | /// On success, caller owns returned buffer. |
| 270 | 269 | pub fn readFileAllocAligned(allocator: *mem.Allocator, path: []const u8, comptime A: u29) ![]align(A) u8 { |
| 271 | var file = try File.openRead(allocator, path); | |
| 270 | var file = try File.openRead(path); | |
| 272 | 271 | defer file.close(); |
| 273 | 272 | |
| 274 | 273 | const size = try file.getEndPos(); |
std/io_test.zig+5-5| ... | ... | @@ -16,7 +16,7 @@ test "write a file, read it, then delete it" { |
| 16 | 16 | prng.random.bytes(data[0..]); |
| 17 | 17 | const tmp_file_name = "temp_test_file.txt"; |
| 18 | 18 | { |
| 19 | var file = try os.File.openWrite(allocator, tmp_file_name); | |
| 19 | var file = try os.File.openWrite(tmp_file_name); | |
| 20 | 20 | defer file.close(); |
| 21 | 21 | |
| 22 | 22 | var file_out_stream = io.FileOutStream.init(&file); |
| ... | ... | @@ -28,7 +28,7 @@ test "write a file, read it, then delete it" { |
| 28 | 28 | try buf_stream.flush(); |
| 29 | 29 | } |
| 30 | 30 | { |
| 31 | var file = try os.File.openRead(allocator, tmp_file_name); | |
| 31 | var file = try os.File.openRead(tmp_file_name); | |
| 32 | 32 | defer file.close(); |
| 33 | 33 | |
| 34 | 34 | const file_size = try file.getEndPos(); |
| ... | ... | @@ -45,7 +45,7 @@ test "write a file, read it, then delete it" { |
| 45 | 45 | assert(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data)); |
| 46 | 46 | assert(mem.eql(u8, contents[contents.len - "end".len ..], "end")); |
| 47 | 47 | } |
| 48 | try os.deleteFile(allocator, tmp_file_name); | |
| 48 | try os.deleteFile(tmp_file_name); | |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | test "BufferOutStream" { |
| ... | ... | @@ -63,7 +63,7 @@ test "BufferOutStream" { |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | test "SliceInStream" { |
| 66 | const bytes = []const u8 { 1, 2, 3, 4, 5, 6, 7 }; | |
| 66 | const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7 }; | |
| 67 | 67 | var ss = io.SliceInStream.init(bytes); |
| 68 | 68 | |
| 69 | 69 | var dest: [4]u8 = undefined; |
| ... | ... | @@ -81,7 +81,7 @@ test "SliceInStream" { |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | test "PeekStream" { |
| 84 | const bytes = []const u8 { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| 84 | const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; | |
| 85 | 85 | var ss = io.SliceInStream.init(bytes); |
| 86 | 86 | var ps = io.PeekStream(2, io.SliceInStream.Error).init(&ss.stream); |
| 87 | 87 |
std/mem.zig+17-2| ... | ... | @@ -179,8 +179,8 @@ pub fn secureZero(comptime T: type, s: []T) void { |
| 179 | 179 | // NOTE: We do not use a volatile slice cast here since LLVM cannot |
| 180 | 180 | // see that it can be replaced by a memset. |
| 181 | 181 | const ptr = @ptrCast([*]volatile u8, s.ptr); |
| 182 | const len = s.len * @sizeOf(T); | |
| 183 | @memset(ptr, 0, len); | |
| 182 | const length = s.len * @sizeOf(T); | |
| 183 | @memset(ptr, 0, length); | |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | test "mem.secureZero" { |
| ... | ... | @@ -252,6 +252,20 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { |
| 252 | 252 | return true; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | pub fn len(comptime T: type, ptr: [*]const T) usize { | |
| 256 | var count: usize = 0; | |
| 257 | while (ptr[count] != 0) : (count += 1) {} | |
| 258 | return count; | |
| 259 | } | |
| 260 | ||
| 261 | pub fn toSliceConst(comptime T: type, ptr: [*]const T) []const T { | |
| 262 | return ptr[0..len(T, ptr)]; | |
| 263 | } | |
| 264 | ||
| 265 | pub fn toSlice(comptime T: type, ptr: [*]T) []T { | |
| 266 | return ptr[0..len(T, ptr)]; | |
| 267 | } | |
| 268 | ||
| 255 | 269 | /// Returns true if all elements in a slice are equal to the scalar value provided |
| 256 | 270 | pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool { |
| 257 | 271 | for (slice) |item| { |
| ... | ... | @@ -809,3 +823,4 @@ pub fn endianSwap(comptime T: type, x: T) T { |
| 809 | 823 | test "std.mem.endianSwap" { |
| 810 | 824 | assert(endianSwap(u32, 0xDEADBEEF) == 0xEFBEADDE); |
| 811 | 825 | } |
| 826 |
std/os/child_process.zig+2-12| ... | ... | @@ -349,14 +349,7 @@ pub const ChildProcess = struct { |
| 349 | 349 | }; |
| 350 | 350 | |
| 351 | 351 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); |
| 352 | const dev_null_fd = if (any_ignore) blk: { | |
| 353 | const dev_null_path = "/dev/null"; | |
| 354 | var fixed_buffer_mem: [dev_null_path.len + 1]u8 = undefined; | |
| 355 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 356 | break :blk try os.posixOpen(&fixed_allocator.allocator, "/dev/null", posix.O_RDWR, 0); | |
| 357 | } else blk: { | |
| 358 | break :blk undefined; | |
| 359 | }; | |
| 352 | const dev_null_fd = if (any_ignore) try os.posixOpenC(c"/dev/null", posix.O_RDWR, 0) else undefined; | |
| 360 | 353 | defer { |
| 361 | 354 | if (any_ignore) os.close(dev_null_fd); |
| 362 | 355 | } |
| ... | ... | @@ -453,10 +446,7 @@ pub const ChildProcess = struct { |
| 453 | 446 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); |
| 454 | 447 | |
| 455 | 448 | const nul_handle = if (any_ignore) blk: { |
| 456 | const nul_file_path = "NUL"; | |
| 457 | var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined; | |
| 458 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 459 | break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL); | |
| 449 | break :blk try os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL); | |
| 460 | 450 | } else blk: { |
| 461 | 451 | break :blk undefined; |
| 462 | 452 | }; |
std/os/file.zig+88-53| ... | ... | @@ -7,6 +7,7 @@ const assert = std.debug.assert; |
| 7 | 7 | const posix = os.posix; |
| 8 | 8 | const windows = os.windows; |
| 9 | 9 | const Os = builtin.Os; |
| 10 | const windows_util = @import("windows/util.zig"); | |
| 10 | 11 | |
| 11 | 12 | const is_posix = builtin.os != builtin.Os.windows; |
| 12 | 13 | const is_windows = builtin.os == builtin.Os.windows; |
| ... | ... | @@ -27,16 +28,27 @@ pub const File = struct { |
| 27 | 28 | |
| 28 | 29 | pub const OpenError = os.WindowsOpenError || os.PosixOpenError; |
| 29 | 30 | |
| 30 | /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. | |
| 31 | /// Call close to clean up. | |
| 32 | pub fn openRead(allocator: *mem.Allocator, path: []const u8) OpenError!File { | |
| 31 | /// `openRead` except with a null terminated path | |
| 32 | pub fn openReadC(path: [*]const u8) OpenError!File { | |
| 33 | 33 | if (is_posix) { |
| 34 | 34 | const flags = posix.O_LARGEFILE | posix.O_RDONLY; |
| 35 | const fd = try os.posixOpen(allocator, path, flags, 0); | |
| 35 | const fd = try os.posixOpenC(path, flags, 0); | |
| 36 | 36 | return openHandle(fd); |
| 37 | } else if (is_windows) { | |
| 37 | } | |
| 38 | if (is_windows) { | |
| 39 | return openRead(mem.toSliceConst(u8, path)); | |
| 40 | } | |
| 41 | @compileError("Unsupported OS"); | |
| 42 | } | |
| 43 | ||
| 44 | /// Call close to clean up. | |
| 45 | pub fn openRead(path: []const u8) OpenError!File { | |
| 46 | if (is_posix) { | |
| 47 | const path_c = try os.toPosixPath(path); | |
| 48 | return openReadC(&path_c); | |
| 49 | } | |
| 50 | if (is_windows) { | |
| 38 | 51 | const handle = try os.windowsOpen( |
| 39 | allocator, | |
| 40 | 52 | path, |
| 41 | 53 | windows.GENERIC_READ, |
| 42 | 54 | windows.FILE_SHARE_READ, |
| ... | ... | @@ -44,28 +56,25 @@ pub const File = struct { |
| 44 | 56 | windows.FILE_ATTRIBUTE_NORMAL, |
| 45 | 57 | ); |
| 46 | 58 | return openHandle(handle); |
| 47 | } else { | |
| 48 | @compileError("TODO implement openRead for this OS"); | |
| 49 | 59 | } |
| 60 | @compileError("Unsupported OS"); | |
| 50 | 61 | } |
| 51 | 62 | |
| 52 | 63 | /// Calls `openWriteMode` with os.File.default_mode for the mode. |
| 53 | pub fn openWrite(allocator: *mem.Allocator, path: []const u8) OpenError!File { | |
| 54 | return openWriteMode(allocator, path, os.File.default_mode); | |
| 64 | pub fn openWrite(path: []const u8) OpenError!File { | |
| 65 | return openWriteMode(path, os.File.default_mode); | |
| 55 | 66 | } |
| 56 | 67 | |
| 57 | 68 | /// If the path does not exist it will be created. |
| 58 | 69 | /// If a file already exists in the destination it will be truncated. |
| 59 | /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. | |
| 60 | 70 | /// Call close to clean up. |
| 61 | pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File { | |
| 71 | pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File { | |
| 62 | 72 | if (is_posix) { |
| 63 | 73 | const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; |
| 64 | const fd = try os.posixOpen(allocator, path, flags, file_mode); | |
| 74 | const fd = try os.posixOpen(path, flags, file_mode); | |
| 65 | 75 | return openHandle(fd); |
| 66 | 76 | } else if (is_windows) { |
| 67 | 77 | const handle = try os.windowsOpen( |
| 68 | allocator, | |
| 69 | 78 | path, |
| 70 | 79 | windows.GENERIC_WRITE, |
| 71 | 80 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| ... | ... | @@ -80,16 +89,14 @@ pub const File = struct { |
| 80 | 89 | |
| 81 | 90 | /// If the path does not exist it will be created. |
| 82 | 91 | /// If a file already exists in the destination this returns OpenError.PathAlreadyExists |
| 83 | /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator. | |
| 84 | 92 | /// Call close to clean up. |
| 85 | pub fn openWriteNoClobber(allocator: *mem.Allocator, path: []const u8, file_mode: Mode) OpenError!File { | |
| 93 | pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File { | |
| 86 | 94 | if (is_posix) { |
| 87 | 95 | const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_EXCL; |
| 88 | const fd = try os.posixOpen(allocator, path, flags, file_mode); | |
| 96 | const fd = try os.posixOpen(path, flags, file_mode); | |
| 89 | 97 | return openHandle(fd); |
| 90 | 98 | } else if (is_windows) { |
| 91 | 99 | const handle = try os.windowsOpen( |
| 92 | allocator, | |
| 93 | 100 | path, |
| 94 | 101 | windows.GENERIC_WRITE, |
| 95 | 102 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| ... | ... | @@ -108,23 +115,43 @@ pub const File = struct { |
| 108 | 115 | |
| 109 | 116 | pub const AccessError = error{ |
| 110 | 117 | PermissionDenied, |
| 111 | NotFound, | |
| 118 | FileNotFound, | |
| 112 | 119 | NameTooLong, |
| 113 | BadMode, | |
| 114 | BadPathName, | |
| 115 | Io, | |
| 120 | InputOutput, | |
| 116 | 121 | SystemResources, |
| 117 | OutOfMemory, | |
| 122 | BadPathName, | |
| 123 | ||
| 124 | /// On Windows, file paths must be valid Unicode. | |
| 125 | InvalidUtf8, | |
| 118 | 126 | |
| 119 | 127 | Unexpected, |
| 120 | 128 | }; |
| 121 | 129 | |
| 122 | pub fn access(allocator: *mem.Allocator, path: []const u8) AccessError!void { | |
| 123 | const path_with_null = try std.cstr.addNullByte(allocator, path); | |
| 124 | defer allocator.free(path_with_null); | |
| 130 | /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. | |
| 131 | /// Otherwise use `access` or `accessC`. | |
| 132 | pub fn accessW(path: [*]const u16) AccessError!void { | |
| 133 | if (os.windows.GetFileAttributesW(path) != os.windows.INVALID_FILE_ATTRIBUTES) { | |
| 134 | return; | |
| 135 | } | |
| 136 | ||
| 137 | const err = windows.GetLastError(); | |
| 138 | switch (err) { | |
| 139 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 140 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 141 | windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, | |
| 142 | else => return os.unexpectedErrorWindows(err), | |
| 143 | } | |
| 144 | } | |
| 125 | 145 | |
| 146 | /// Call if you have a UTF-8 encoded, null-terminated string. | |
| 147 | /// Otherwise use `access` or `accessW`. | |
| 148 | pub fn accessC(path: [*]const u8) AccessError!void { | |
| 149 | if (is_windows) { | |
| 150 | const path_w = try windows_util.cStrToPrefixedFileW(path); | |
| 151 | return accessW(&path_w); | |
| 152 | } | |
| 126 | 153 | if (is_posix) { |
| 127 | const result = posix.access(path_with_null.ptr, posix.F_OK); | |
| 154 | const result = posix.access(path, posix.F_OK); | |
| 128 | 155 | const err = posix.getErrno(result); |
| 129 | 156 | switch (err) { |
| 130 | 157 | 0 => return, |
| ... | ... | @@ -132,32 +159,33 @@ pub const File = struct { |
| 132 | 159 | posix.EROFS => return error.PermissionDenied, |
| 133 | 160 | posix.ELOOP => return error.PermissionDenied, |
| 134 | 161 | posix.ETXTBSY => return error.PermissionDenied, |
| 135 | posix.ENOTDIR => return error.NotFound, | |
| 136 | posix.ENOENT => return error.NotFound, | |
| 162 | posix.ENOTDIR => return error.FileNotFound, | |
| 163 | posix.ENOENT => return error.FileNotFound, | |
| 137 | 164 | |
| 138 | 165 | posix.ENAMETOOLONG => return error.NameTooLong, |
| 139 | 166 | posix.EINVAL => unreachable, |
| 140 | posix.EFAULT => return error.BadPathName, | |
| 141 | posix.EIO => return error.Io, | |
| 167 | posix.EFAULT => unreachable, | |
| 168 | posix.EIO => return error.InputOutput, | |
| 142 | 169 | posix.ENOMEM => return error.SystemResources, |
| 143 | 170 | else => return os.unexpectedErrorPosix(err), |
| 144 | 171 | } |
| 145 | } else if (is_windows) { | |
| 146 | if (os.windows.GetFileAttributesA(path_with_null.ptr) != os.windows.INVALID_FILE_ATTRIBUTES) { | |
| 147 | return; | |
| 148 | } | |
| 172 | } | |
| 173 | @compileError("Unsupported OS"); | |
| 174 | } | |
| 149 | 175 | |
| 150 | const err = windows.GetLastError(); | |
| 151 | switch (err) { | |
| 152 | windows.ERROR.FILE_NOT_FOUND, | |
| 153 | windows.ERROR.PATH_NOT_FOUND, | |
| 154 | => return error.NotFound, | |
| 155 | windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, | |
| 156 | else => return os.unexpectedErrorWindows(err), | |
| 157 | } | |
| 158 | } else { | |
| 159 | @compileError("TODO implement access for this OS"); | |
| 176 | pub fn access(path: []const u8) AccessError!void { | |
| 177 | if (is_windows) { | |
| 178 | const path_w = try windows_util.sliceToPrefixedFileW(path); | |
| 179 | return accessW(&path_w); | |
| 180 | } | |
| 181 | if (is_posix) { | |
| 182 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 183 | if (path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 184 | mem.copy(u8, path_with_null[0..], path); | |
| 185 | path_with_null[path.len] = 0; | |
| 186 | return accessC(&path_with_null); | |
| 160 | 187 | } |
| 188 | @compileError("Unsupported OS"); | |
| 161 | 189 | } |
| 162 | 190 | |
| 163 | 191 | /// Upon success, the stream is in an uninitialized state. To continue using it, |
| ... | ... | @@ -179,7 +207,9 @@ pub const File = struct { |
| 179 | 207 | const err = posix.getErrno(result); |
| 180 | 208 | if (err > 0) { |
| 181 | 209 | return switch (err) { |
| 182 | posix.EBADF => error.BadFd, | |
| 210 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 211 | // since the fd could have been reused. | |
| 212 | posix.EBADF => unreachable, | |
| 183 | 213 | posix.EINVAL => error.Unseekable, |
| 184 | 214 | posix.EOVERFLOW => error.Unseekable, |
| 185 | 215 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -192,7 +222,7 @@ pub const File = struct { |
| 192 | 222 | if (windows.SetFilePointerEx(self.handle, amount, null, windows.FILE_CURRENT) == 0) { |
| 193 | 223 | const err = windows.GetLastError(); |
| 194 | 224 | return switch (err) { |
| 195 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 225 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 196 | 226 | else => os.unexpectedErrorWindows(err), |
| 197 | 227 | }; |
| 198 | 228 | } |
| ... | ... | @@ -209,7 +239,9 @@ pub const File = struct { |
| 209 | 239 | const err = posix.getErrno(result); |
| 210 | 240 | if (err > 0) { |
| 211 | 241 | return switch (err) { |
| 212 | posix.EBADF => error.BadFd, | |
| 242 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 243 | // since the fd could have been reused. | |
| 244 | posix.EBADF => unreachable, | |
| 213 | 245 | posix.EINVAL => error.Unseekable, |
| 214 | 246 | posix.EOVERFLOW => error.Unseekable, |
| 215 | 247 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -223,7 +255,7 @@ pub const File = struct { |
| 223 | 255 | if (windows.SetFilePointerEx(self.handle, ipos, null, windows.FILE_BEGIN) == 0) { |
| 224 | 256 | const err = windows.GetLastError(); |
| 225 | 257 | return switch (err) { |
| 226 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 258 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 227 | 259 | else => os.unexpectedErrorWindows(err), |
| 228 | 260 | }; |
| 229 | 261 | } |
| ... | ... | @@ -239,7 +271,9 @@ pub const File = struct { |
| 239 | 271 | const err = posix.getErrno(result); |
| 240 | 272 | if (err > 0) { |
| 241 | 273 | return switch (err) { |
| 242 | posix.EBADF => error.BadFd, | |
| 274 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 275 | // since the fd could have been reused. | |
| 276 | posix.EBADF => unreachable, | |
| 243 | 277 | posix.EINVAL => error.Unseekable, |
| 244 | 278 | posix.EOVERFLOW => error.Unseekable, |
| 245 | 279 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -254,7 +288,7 @@ pub const File = struct { |
| 254 | 288 | if (windows.SetFilePointerEx(self.handle, 0, &pos, windows.FILE_CURRENT) == 0) { |
| 255 | 289 | const err = windows.GetLastError(); |
| 256 | 290 | return switch (err) { |
| 257 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 291 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 258 | 292 | else => os.unexpectedErrorWindows(err), |
| 259 | 293 | }; |
| 260 | 294 | } |
| ... | ... | @@ -287,7 +321,6 @@ pub const File = struct { |
| 287 | 321 | } |
| 288 | 322 | |
| 289 | 323 | pub const ModeError = error{ |
| 290 | BadFd, | |
| 291 | 324 | SystemResources, |
| 292 | 325 | Unexpected, |
| 293 | 326 | }; |
| ... | ... | @@ -298,7 +331,9 @@ pub const File = struct { |
| 298 | 331 | const err = posix.getErrno(posix.fstat(self.handle, &stat)); |
| 299 | 332 | if (err > 0) { |
| 300 | 333 | return switch (err) { |
| 301 | posix.EBADF => error.BadFd, | |
| 334 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 335 | // since the fd could have been reused. | |
| 336 | posix.EBADF => unreachable, | |
| 302 | 337 | posix.ENOMEM => error.SystemResources, |
| 303 | 338 | else => os.unexpectedErrorPosix(err), |
| 304 | 339 | }; |
std/os/get_app_data_dir.zig+2-1| ... | ... | @@ -10,6 +10,7 @@ pub const GetAppDataDirError = error{ |
| 10 | 10 | }; |
| 11 | 11 | |
| 12 | 12 | /// Caller owns returned memory. |
| 13 | /// TODO determine if we can remove the allocator requirement | |
| 13 | 14 | pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataDirError![]u8 { |
| 14 | 15 | switch (builtin.os) { |
| 15 | 16 | builtin.Os.windows => { |
| ... | ... | @@ -22,7 +23,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD |
| 22 | 23 | )) { |
| 23 | 24 | os.windows.S_OK => { |
| 24 | 25 | defer os.windows.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr)); |
| 25 | const global_dir = unicode.utf16leToUtf8(allocator, utf16lePtrSlice(dir_path_ptr)) catch |err| switch (err) { | |
| 26 | const global_dir = unicode.utf16leToUtf8Alloc(allocator, utf16lePtrSlice(dir_path_ptr)) catch |err| switch (err) { | |
| 26 | 27 | error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, |
| 27 | 28 | error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, |
| 28 | 29 | error.DanglingSurrogateHalf => return error.AppDataDirUnavailable, |
std/os/index.zig+294-243| ... | ... | @@ -39,6 +39,15 @@ pub const File = @import("file.zig").File; |
| 39 | 39 | pub const time = @import("time.zig"); |
| 40 | 40 | |
| 41 | 41 | pub const page_size = 4 * 1024; |
| 42 | pub const MAX_PATH_BYTES = switch (builtin.os) { | |
| 43 | Os.linux, Os.macosx, Os.ios => posix.PATH_MAX, | |
| 44 | // Each UTF-16LE character may be expanded to 3 UTF-8 bytes. | |
| 45 | // If it would require 4 UTF-8 bytes, then there would be a surrogate | |
| 46 | // pair in the UTF-16LE, and we (over)account 3 bytes for it that way. | |
| 47 | // +1 for the null byte at the end, which can be encoded in 1 byte. | |
| 48 | Os.windows => windows_util.PATH_MAX_WIDE * 3 + 1, | |
| 49 | else => @compileError("Unsupported OS"), | |
| 50 | }; | |
| 42 | 51 | |
| 43 | 52 | pub const UserInfo = @import("get_user_id.zig").UserInfo; |
| 44 | 53 | pub const getUserInfo = @import("get_user_id.zig").getUserInfo; |
| ... | ... | @@ -317,6 +326,8 @@ pub const PosixWriteError = error{ |
| 317 | 326 | NoSpaceLeft, |
| 318 | 327 | AccessDenied, |
| 319 | 328 | BrokenPipe, |
| 329 | ||
| 330 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 320 | 331 | Unexpected, |
| 321 | 332 | }; |
| 322 | 333 | |
| ... | ... | @@ -417,7 +428,6 @@ pub fn posix_pwritev(fd: i32, iov: [*]const posix.iovec_const, count: usize, off |
| 417 | 428 | } |
| 418 | 429 | |
| 419 | 430 | pub const PosixOpenError = error{ |
| 420 | OutOfMemory, | |
| 421 | 431 | AccessDenied, |
| 422 | 432 | FileTooBig, |
| 423 | 433 | IsDir, |
| ... | ... | @@ -426,22 +436,22 @@ pub const PosixOpenError = error{ |
| 426 | 436 | NameTooLong, |
| 427 | 437 | SystemFdQuotaExceeded, |
| 428 | 438 | NoDevice, |
| 429 | PathNotFound, | |
| 439 | FileNotFound, | |
| 430 | 440 | SystemResources, |
| 431 | 441 | NoSpaceLeft, |
| 432 | 442 | NotDir, |
| 433 | 443 | PathAlreadyExists, |
| 444 | ||
| 445 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 434 | 446 | Unexpected, |
| 435 | 447 | }; |
| 436 | 448 | |
| 437 | 449 | /// ::file_path needs to be copied in memory to add a null terminating byte. |
| 438 | 450 | /// Calls POSIX open, keeps trying if it gets interrupted, and translates |
| 439 | 451 | /// the return value into zig errors. |
| 440 | pub fn posixOpen(allocator: *Allocator, file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { | |
| 441 | const path_with_null = try cstr.addNullByte(allocator, file_path); | |
| 442 | defer allocator.free(path_with_null); | |
| 443 | ||
| 444 | return posixOpenC(path_with_null.ptr, flags, perm); | |
| 452 | pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { | |
| 453 | const file_path_c = try toPosixPath(file_path); | |
| 454 | return posixOpenC(&file_path_c, flags, perm); | |
| 445 | 455 | } |
| 446 | 456 | |
| 447 | 457 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -463,7 +473,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 { |
| 463 | 473 | posix.ENAMETOOLONG => return PosixOpenError.NameTooLong, |
| 464 | 474 | posix.ENFILE => return PosixOpenError.SystemFdQuotaExceeded, |
| 465 | 475 | posix.ENODEV => return PosixOpenError.NoDevice, |
| 466 | posix.ENOENT => return PosixOpenError.PathNotFound, | |
| 476 | posix.ENOENT => return PosixOpenError.FileNotFound, | |
| 467 | 477 | posix.ENOMEM => return PosixOpenError.SystemResources, |
| 468 | 478 | posix.ENOSPC => return PosixOpenError.NoSpaceLeft, |
| 469 | 479 | posix.ENOTDIR => return PosixOpenError.NotDir, |
| ... | ... | @@ -476,6 +486,16 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 { |
| 476 | 486 | } |
| 477 | 487 | } |
| 478 | 488 | |
| 489 | /// Used to convert a slice to a null terminated slice on the stack. | |
| 490 | /// TODO well defined copy elision | |
| 491 | pub fn toPosixPath(file_path: []const u8) ![posix.PATH_MAX]u8 { | |
| 492 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 493 | if (file_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 494 | mem.copy(u8, path_with_null[0..], file_path); | |
| 495 | path_with_null[file_path.len] = 0; | |
| 496 | return path_with_null; | |
| 497 | } | |
| 498 | ||
| 479 | 499 | pub fn posixDup2(old_fd: i32, new_fd: i32) !void { |
| 480 | 500 | while (true) { |
| 481 | 501 | const err = posix.getErrno(posix.dup2(old_fd, new_fd)); |
| ... | ... | @@ -591,6 +611,8 @@ pub const PosixExecveError = error{ |
| 591 | 611 | FileNotFound, |
| 592 | 612 | NotDir, |
| 593 | 613 | FileBusy, |
| 614 | ||
| 615 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 594 | 616 | Unexpected, |
| 595 | 617 | }; |
| 596 | 618 | |
| ... | ... | @@ -719,43 +741,39 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned |
| 719 | 741 | } |
| 720 | 742 | |
| 721 | 743 | /// Caller must free the returned memory. |
| 722 | pub fn getCwd(allocator: *Allocator) ![]u8 { | |
| 723 | switch (builtin.os) { | |
| 724 | Os.windows => { | |
| 725 | var buf = try allocator.alloc(u8, 256); | |
| 726 | errdefer allocator.free(buf); | |
| 727 | ||
| 728 | while (true) { | |
| 729 | const result = windows.GetCurrentDirectoryA(@intCast(windows.WORD, buf.len), buf.ptr); | |
| 744 | pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { | |
| 745 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 746 | return mem.dupe(allocator, u8, try getCwd(&buf)); | |
| 747 | } | |
| 730 | 748 | |
| 731 | if (result == 0) { | |
| 732 | const err = windows.GetLastError(); | |
| 733 | return switch (err) { | |
| 734 | else => unexpectedErrorWindows(err), | |
| 735 | }; | |
| 736 | } | |
| 749 | pub const GetCwdError = error{Unexpected}; | |
| 737 | 750 | |
| 738 | if (result > buf.len) { | |
| 739 | buf = try allocator.realloc(u8, buf, result); | |
| 740 | continue; | |
| 751 | /// The result is a slice of out_buffer. | |
| 752 | pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) GetCwdError![]u8 { | |
| 753 | switch (builtin.os) { | |
| 754 | Os.windows => { | |
| 755 | var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; | |
| 756 | const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast | |
| 757 | const casted_ptr = ([*]u16)(&utf16le_buf); // TODO shouldn't need this cast | |
| 758 | const result = windows.GetCurrentDirectoryW(casted_len, casted_ptr); | |
| 759 | if (result == 0) { | |
| 760 | const err = windows.GetLastError(); | |
| 761 | switch (err) { | |
| 762 | else => return unexpectedErrorWindows(err), | |
| 741 | 763 | } |
| 742 | ||
| 743 | return allocator.shrink(u8, buf, result); | |
| 744 | 764 | } |
| 765 | assert(result <= utf16le_buf.len); | |
| 766 | const utf16le_slice = utf16le_buf[0..result]; | |
| 767 | // Trust that Windows gives us valid UTF-16LE. | |
| 768 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; | |
| 769 | return out_buffer[0..end_index]; | |
| 745 | 770 | }, |
| 746 | 771 | else => { |
| 747 | var buf = try allocator.alloc(u8, 1024); | |
| 748 | errdefer allocator.free(buf); | |
| 749 | while (true) { | |
| 750 | const err = posix.getErrno(posix.getcwd(buf.ptr, buf.len)); | |
| 751 | if (err == posix.ERANGE) { | |
| 752 | buf = try allocator.realloc(u8, buf, buf.len * 2); | |
| 753 | continue; | |
| 754 | } else if (err > 0) { | |
| 755 | return unexpectedErrorPosix(err); | |
| 756 | } | |
| 757 | ||
| 758 | return allocator.shrink(u8, buf, cstr.len(buf.ptr)); | |
| 772 | const err = posix.getErrno(posix.getcwd(out_buffer, out_buffer.len)); | |
| 773 | switch (err) { | |
| 774 | 0 => return cstr.toSlice(out_buffer), | |
| 775 | posix.ERANGE => unreachable, | |
| 776 | else => return unexpectedErrorPosix(err), | |
| 759 | 777 | } |
| 760 | 778 | }, |
| 761 | 779 | } |
| ... | ... | @@ -763,7 +781,9 @@ pub fn getCwd(allocator: *Allocator) ![]u8 { |
| 763 | 781 | |
| 764 | 782 | test "os.getCwd" { |
| 765 | 783 | // at least call it so it gets compiled |
| 766 | _ = getCwd(debug.global_allocator); | |
| 784 | _ = getCwdAlloc(debug.global_allocator); | |
| 785 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 786 | _ = getCwd(&buf); | |
| 767 | 787 | } |
| 768 | 788 | |
| 769 | 789 | pub const SymLinkError = PosixSymLinkError || WindowsSymLinkError; |
| ... | ... | @@ -778,6 +798,8 @@ pub fn symLink(allocator: *Allocator, existing_path: []const u8, new_path: []con |
| 778 | 798 | |
| 779 | 799 | pub const WindowsSymLinkError = error{ |
| 780 | 800 | OutOfMemory, |
| 801 | ||
| 802 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 781 | 803 | Unexpected, |
| 782 | 804 | }; |
| 783 | 805 | |
| ... | ... | @@ -808,6 +830,8 @@ pub const PosixSymLinkError = error{ |
| 808 | 830 | NoSpaceLeft, |
| 809 | 831 | ReadOnlyFileSystem, |
| 810 | 832 | NotDir, |
| 833 | ||
| 834 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 811 | 835 | Unexpected, |
| 812 | 836 | }; |
| 813 | 837 | |
| ... | ... | @@ -866,7 +890,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: |
| 866 | 890 | b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf); |
| 867 | 891 | |
| 868 | 892 | if (symLink(allocator, existing_path, tmp_path)) { |
| 869 | return rename(allocator, tmp_path, new_path); | |
| 893 | return rename(tmp_path, new_path); | |
| 870 | 894 | } else |err| switch (err) { |
| 871 | 895 | error.PathAlreadyExists => continue, |
| 872 | 896 | else => return err, // TODO zig should know this set does not include PathAlreadyExists |
| ... | ... | @@ -885,70 +909,75 @@ pub const DeleteFileError = error{ |
| 885 | 909 | NotDir, |
| 886 | 910 | SystemResources, |
| 887 | 911 | ReadOnlyFileSystem, |
| 888 | OutOfMemory, | |
| 889 | 912 | |
| 913 | /// On Windows, file paths must be valid Unicode. | |
| 914 | InvalidUtf8, | |
| 915 | ||
| 916 | /// On Windows, file paths cannot contain these characters: | |
| 917 | /// '/', '*', '?', '"', '<', '>', '|' | |
| 918 | BadPathName, | |
| 919 | ||
| 920 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 890 | 921 | Unexpected, |
| 891 | 922 | }; |
| 892 | 923 | |
| 893 | pub fn deleteFile(allocator: *Allocator, file_path: []const u8) DeleteFileError!void { | |
| 924 | pub fn deleteFile(file_path: []const u8) DeleteFileError!void { | |
| 894 | 925 | if (builtin.os == Os.windows) { |
| 895 | return deleteFileWindows(allocator, file_path); | |
| 926 | return deleteFileWindows(file_path); | |
| 896 | 927 | } else { |
| 897 | return deleteFilePosix(allocator, file_path); | |
| 928 | return deleteFilePosix(file_path); | |
| 898 | 929 | } |
| 899 | 930 | } |
| 900 | 931 | |
| 901 | pub fn deleteFileWindows(allocator: *Allocator, file_path: []const u8) !void { | |
| 902 | const buf = try allocator.alloc(u8, file_path.len + 1); | |
| 903 | defer allocator.free(buf); | |
| 932 | pub fn deleteFileWindows(file_path: []const u8) !void { | |
| 933 | const file_path_w = try windows_util.sliceToPrefixedFileW(file_path); | |
| 904 | 934 | |
| 905 | mem.copy(u8, buf, file_path); | |
| 906 | buf[file_path.len] = 0; | |
| 907 | ||
| 908 | if (windows.DeleteFileA(buf.ptr) == 0) { | |
| 935 | if (windows.DeleteFileW(&file_path_w) == 0) { | |
| 909 | 936 | const err = windows.GetLastError(); |
| 910 | return switch (err) { | |
| 911 | windows.ERROR.FILE_NOT_FOUND => error.FileNotFound, | |
| 912 | windows.ERROR.ACCESS_DENIED => error.AccessDenied, | |
| 913 | windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong, | |
| 914 | else => unexpectedErrorWindows(err), | |
| 915 | }; | |
| 937 | switch (err) { | |
| 938 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 939 | windows.ERROR.ACCESS_DENIED => return error.AccessDenied, | |
| 940 | windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, | |
| 941 | windows.ERROR.INVALID_PARAMETER => return error.NameTooLong, | |
| 942 | else => return unexpectedErrorWindows(err), | |
| 943 | } | |
| 916 | 944 | } |
| 917 | 945 | } |
| 918 | 946 | |
| 919 | pub fn deleteFilePosix(allocator: *Allocator, file_path: []const u8) !void { | |
| 920 | const buf = try allocator.alloc(u8, file_path.len + 1); | |
| 921 | defer allocator.free(buf); | |
| 922 | ||
| 923 | mem.copy(u8, buf, file_path); | |
| 924 | buf[file_path.len] = 0; | |
| 925 | ||
| 926 | const err = posix.getErrno(posix.unlink(buf.ptr)); | |
| 927 | if (err > 0) { | |
| 928 | return switch (err) { | |
| 929 | posix.EACCES, posix.EPERM => error.AccessDenied, | |
| 930 | posix.EBUSY => error.FileBusy, | |
| 931 | posix.EFAULT, posix.EINVAL => unreachable, | |
| 932 | posix.EIO => error.FileSystem, | |
| 933 | posix.EISDIR => error.IsDir, | |
| 934 | posix.ELOOP => error.SymLinkLoop, | |
| 935 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 936 | posix.ENOENT => error.FileNotFound, | |
| 937 | posix.ENOTDIR => error.NotDir, | |
| 938 | posix.ENOMEM => error.SystemResources, | |
| 939 | posix.EROFS => error.ReadOnlyFileSystem, | |
| 940 | else => unexpectedErrorPosix(err), | |
| 941 | }; | |
| 947 | pub fn deleteFilePosixC(file_path: [*]const u8) !void { | |
| 948 | const err = posix.getErrno(posix.unlink(file_path)); | |
| 949 | switch (err) { | |
| 950 | 0 => return, | |
| 951 | posix.EACCES => return error.AccessDenied, | |
| 952 | posix.EPERM => return error.AccessDenied, | |
| 953 | posix.EBUSY => return error.FileBusy, | |
| 954 | posix.EFAULT => unreachable, | |
| 955 | posix.EINVAL => unreachable, | |
| 956 | posix.EIO => return error.FileSystem, | |
| 957 | posix.EISDIR => return error.IsDir, | |
| 958 | posix.ELOOP => return error.SymLinkLoop, | |
| 959 | posix.ENAMETOOLONG => return error.NameTooLong, | |
| 960 | posix.ENOENT => return error.FileNotFound, | |
| 961 | posix.ENOTDIR => return error.NotDir, | |
| 962 | posix.ENOMEM => return error.SystemResources, | |
| 963 | posix.EROFS => return error.ReadOnlyFileSystem, | |
| 964 | else => return unexpectedErrorPosix(err), | |
| 942 | 965 | } |
| 943 | 966 | } |
| 944 | 967 | |
| 968 | pub fn deleteFilePosix(file_path: []const u8) !void { | |
| 969 | const file_path_c = try toPosixPath(file_path); | |
| 970 | return deleteFilePosixC(&file_path_c); | |
| 971 | } | |
| 972 | ||
| 945 | 973 | /// Guaranteed to be atomic. However until https://patchwork.kernel.org/patch/9636735/ is |
| 946 | 974 | /// merged and readily available, |
| 947 | 975 | /// there is a possibility of power loss or application termination leaving temporary files present |
| 948 | 976 | /// in the same directory as dest_path. |
| 949 | 977 | /// Destination file will have the same mode as the source file. |
| 978 | /// TODO investigate if this can work with no allocator | |
| 950 | 979 | pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []const u8) !void { |
| 951 | var in_file = try os.File.openRead(allocator, source_path); | |
| 980 | var in_file = try os.File.openRead(source_path); | |
| 952 | 981 | defer in_file.close(); |
| 953 | 982 | |
| 954 | 983 | const mode = try in_file.mode(); |
| ... | ... | @@ -969,8 +998,9 @@ pub fn copyFile(allocator: *Allocator, source_path: []const u8, dest_path: []con |
| 969 | 998 | /// Guaranteed to be atomic. However until https://patchwork.kernel.org/patch/9636735/ is |
| 970 | 999 | /// merged and readily available, |
| 971 | 1000 | /// there is a possibility of power loss or application termination leaving temporary files present |
| 1001 | /// TODO investigate if this can work with no allocator | |
| 972 | 1002 | pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void { |
| 973 | var in_file = try os.File.openRead(allocator, source_path); | |
| 1003 | var in_file = try os.File.openRead(source_path); | |
| 974 | 1004 | defer in_file.close(); |
| 975 | 1005 | |
| 976 | 1006 | var atomic_file = try AtomicFile.init(allocator, dest_path, mode); |
| ... | ... | @@ -987,6 +1017,7 @@ pub fn copyFileMode(allocator: *Allocator, source_path: []const u8, dest_path: [ |
| 987 | 1017 | } |
| 988 | 1018 | |
| 989 | 1019 | pub const AtomicFile = struct { |
| 1020 | /// TODO investigate if we can make this work with no allocator | |
| 990 | 1021 | allocator: *Allocator, |
| 991 | 1022 | file: os.File, |
| 992 | 1023 | tmp_path: []u8, |
| ... | ... | @@ -1014,7 +1045,7 @@ pub const AtomicFile = struct { |
| 1014 | 1045 | try getRandomBytes(rand_buf[0..]); |
| 1015 | 1046 | b64_fs_encoder.encode(tmp_path[dirname_component_len..], rand_buf); |
| 1016 | 1047 | |
| 1017 | const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) { | |
| 1048 | const file = os.File.openWriteNoClobber(tmp_path, mode) catch |err| switch (err) { | |
| 1018 | 1049 | error.PathAlreadyExists => continue, |
| 1019 | 1050 | // TODO zig should figure out that this error set does not include PathAlreadyExists since |
| 1020 | 1051 | // it is handled in the above switch |
| ... | ... | @@ -1035,7 +1066,7 @@ pub const AtomicFile = struct { |
| 1035 | 1066 | pub fn deinit(self: *AtomicFile) void { |
| 1036 | 1067 | if (!self.finished) { |
| 1037 | 1068 | self.file.close(); |
| 1038 | deleteFile(self.allocator, self.tmp_path) catch {}; | |
| 1069 | deleteFile(self.tmp_path) catch {}; | |
| 1039 | 1070 | self.allocator.free(self.tmp_path); |
| 1040 | 1071 | self.finished = true; |
| 1041 | 1072 | } |
| ... | ... | @@ -1044,70 +1075,72 @@ pub const AtomicFile = struct { |
| 1044 | 1075 | pub fn finish(self: *AtomicFile) !void { |
| 1045 | 1076 | assert(!self.finished); |
| 1046 | 1077 | self.file.close(); |
| 1047 | try rename(self.allocator, self.tmp_path, self.dest_path); | |
| 1078 | try rename(self.tmp_path, self.dest_path); | |
| 1048 | 1079 | self.allocator.free(self.tmp_path); |
| 1049 | 1080 | self.finished = true; |
| 1050 | 1081 | } |
| 1051 | 1082 | }; |
| 1052 | 1083 | |
| 1053 | pub fn rename(allocator: *Allocator, old_path: []const u8, new_path: []const u8) !void { | |
| 1054 | const full_buf = try allocator.alloc(u8, old_path.len + new_path.len + 2); | |
| 1055 | defer allocator.free(full_buf); | |
| 1056 | ||
| 1057 | const old_buf = full_buf; | |
| 1058 | mem.copy(u8, old_buf, old_path); | |
| 1059 | old_buf[old_path.len] = 0; | |
| 1060 | ||
| 1061 | const new_buf = full_buf[old_path.len + 1 ..]; | |
| 1062 | mem.copy(u8, new_buf, new_path); | |
| 1063 | new_buf[new_path.len] = 0; | |
| 1084 | pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) !void { | |
| 1085 | if (is_windows) { | |
| 1086 | @compileError("TODO implement for windows"); | |
| 1087 | } else { | |
| 1088 | const err = posix.getErrno(posix.rename(old_path, new_path)); | |
| 1089 | switch (err) { | |
| 1090 | 0 => return, | |
| 1091 | posix.EACCES => return error.AccessDenied, | |
| 1092 | posix.EPERM => return error.AccessDenied, | |
| 1093 | posix.EBUSY => return error.FileBusy, | |
| 1094 | posix.EDQUOT => return error.DiskQuota, | |
| 1095 | posix.EFAULT => unreachable, | |
| 1096 | posix.EINVAL => unreachable, | |
| 1097 | posix.EISDIR => return error.IsDir, | |
| 1098 | posix.ELOOP => return error.SymLinkLoop, | |
| 1099 | posix.EMLINK => return error.LinkQuotaExceeded, | |
| 1100 | posix.ENAMETOOLONG => return error.NameTooLong, | |
| 1101 | posix.ENOENT => return error.FileNotFound, | |
| 1102 | posix.ENOTDIR => return error.NotDir, | |
| 1103 | posix.ENOMEM => return error.SystemResources, | |
| 1104 | posix.ENOSPC => return error.NoSpaceLeft, | |
| 1105 | posix.EEXIST => return error.PathAlreadyExists, | |
| 1106 | posix.ENOTEMPTY => return error.PathAlreadyExists, | |
| 1107 | posix.EROFS => return error.ReadOnlyFileSystem, | |
| 1108 | posix.EXDEV => return error.RenameAcrossMountPoints, | |
| 1109 | else => return unexpectedErrorPosix(err), | |
| 1110 | } | |
| 1111 | } | |
| 1112 | } | |
| 1064 | 1113 | |
| 1114 | pub fn rename(old_path: []const u8, new_path: []const u8) !void { | |
| 1065 | 1115 | if (is_windows) { |
| 1066 | 1116 | const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; |
| 1067 | if (windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags) == 0) { | |
| 1117 | const old_path_w = try windows_util.sliceToPrefixedFileW(old_path); | |
| 1118 | const new_path_w = try windows_util.sliceToPrefixedFileW(new_path); | |
| 1119 | if (windows.MoveFileExW(&old_path_w, &new_path_w, flags) == 0) { | |
| 1068 | 1120 | const err = windows.GetLastError(); |
| 1069 | return switch (err) { | |
| 1070 | else => unexpectedErrorWindows(err), | |
| 1071 | }; | |
| 1121 | switch (err) { | |
| 1122 | else => return unexpectedErrorWindows(err), | |
| 1123 | } | |
| 1072 | 1124 | } |
| 1073 | 1125 | } else { |
| 1074 | const err = posix.getErrno(posix.rename(old_buf.ptr, new_buf.ptr)); | |
| 1075 | if (err > 0) { | |
| 1076 | return switch (err) { | |
| 1077 | posix.EACCES, posix.EPERM => error.AccessDenied, | |
| 1078 | posix.EBUSY => error.FileBusy, | |
| 1079 | posix.EDQUOT => error.DiskQuota, | |
| 1080 | posix.EFAULT, posix.EINVAL => unreachable, | |
| 1081 | posix.EISDIR => error.IsDir, | |
| 1082 | posix.ELOOP => error.SymLinkLoop, | |
| 1083 | posix.EMLINK => error.LinkQuotaExceeded, | |
| 1084 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 1085 | posix.ENOENT => error.FileNotFound, | |
| 1086 | posix.ENOTDIR => error.NotDir, | |
| 1087 | posix.ENOMEM => error.SystemResources, | |
| 1088 | posix.ENOSPC => error.NoSpaceLeft, | |
| 1089 | posix.EEXIST, posix.ENOTEMPTY => error.PathAlreadyExists, | |
| 1090 | posix.EROFS => error.ReadOnlyFileSystem, | |
| 1091 | posix.EXDEV => error.RenameAcrossMountPoints, | |
| 1092 | else => unexpectedErrorPosix(err), | |
| 1093 | }; | |
| 1094 | } | |
| 1126 | const old_path_c = try toPosixPath(old_path); | |
| 1127 | const new_path_c = try toPosixPath(new_path); | |
| 1128 | return renameC(&old_path_c, &new_path_c); | |
| 1095 | 1129 | } |
| 1096 | 1130 | } |
| 1097 | 1131 | |
| 1098 | pub fn makeDir(allocator: *Allocator, dir_path: []const u8) !void { | |
| 1132 | pub fn makeDir(dir_path: []const u8) !void { | |
| 1099 | 1133 | if (is_windows) { |
| 1100 | return makeDirWindows(allocator, dir_path); | |
| 1134 | return makeDirWindows(dir_path); | |
| 1101 | 1135 | } else { |
| 1102 | return makeDirPosix(allocator, dir_path); | |
| 1136 | return makeDirPosix(dir_path); | |
| 1103 | 1137 | } |
| 1104 | 1138 | } |
| 1105 | 1139 | |
| 1106 | pub fn makeDirWindows(allocator: *Allocator, dir_path: []const u8) !void { | |
| 1107 | const path_buf = try cstr.addNullByte(allocator, dir_path); | |
| 1108 | defer allocator.free(path_buf); | |
| 1140 | pub fn makeDirWindows(dir_path: []const u8) !void { | |
| 1141 | const dir_path_w = try windows_util.sliceToPrefixedFileW(dir_path); | |
| 1109 | 1142 | |
| 1110 | if (windows.CreateDirectoryA(path_buf.ptr, null) == 0) { | |
| 1143 | if (windows.CreateDirectoryW(&dir_path_w, null) == 0) { | |
| 1111 | 1144 | const err = windows.GetLastError(); |
| 1112 | 1145 | return switch (err) { |
| 1113 | 1146 | windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists, |
| ... | ... | @@ -1117,39 +1150,42 @@ pub fn makeDirWindows(allocator: *Allocator, dir_path: []const u8) !void { |
| 1117 | 1150 | } |
| 1118 | 1151 | } |
| 1119 | 1152 | |
| 1120 | pub fn makeDirPosix(allocator: *Allocator, dir_path: []const u8) !void { | |
| 1121 | const path_buf = try cstr.addNullByte(allocator, dir_path); | |
| 1122 | defer allocator.free(path_buf); | |
| 1123 | ||
| 1124 | const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755)); | |
| 1125 | if (err > 0) { | |
| 1126 | return switch (err) { | |
| 1127 | posix.EACCES, posix.EPERM => error.AccessDenied, | |
| 1128 | posix.EDQUOT => error.DiskQuota, | |
| 1129 | posix.EEXIST => error.PathAlreadyExists, | |
| 1130 | posix.EFAULT => unreachable, | |
| 1131 | posix.ELOOP => error.SymLinkLoop, | |
| 1132 | posix.EMLINK => error.LinkQuotaExceeded, | |
| 1133 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 1134 | posix.ENOENT => error.FileNotFound, | |
| 1135 | posix.ENOMEM => error.SystemResources, | |
| 1136 | posix.ENOSPC => error.NoSpaceLeft, | |
| 1137 | posix.ENOTDIR => error.NotDir, | |
| 1138 | posix.EROFS => error.ReadOnlyFileSystem, | |
| 1139 | else => unexpectedErrorPosix(err), | |
| 1140 | }; | |
| 1153 | pub fn makeDirPosixC(dir_path: [*]const u8) !void { | |
| 1154 | const err = posix.getErrno(posix.mkdir(dir_path, 0o755)); | |
| 1155 | switch (err) { | |
| 1156 | 0 => return, | |
| 1157 | posix.EACCES => return error.AccessDenied, | |
| 1158 | posix.EPERM => return error.AccessDenied, | |
| 1159 | posix.EDQUOT => return error.DiskQuota, | |
| 1160 | posix.EEXIST => return error.PathAlreadyExists, | |
| 1161 | posix.EFAULT => unreachable, | |
| 1162 | posix.ELOOP => return error.SymLinkLoop, | |
| 1163 | posix.EMLINK => return error.LinkQuotaExceeded, | |
| 1164 | posix.ENAMETOOLONG => return error.NameTooLong, | |
| 1165 | posix.ENOENT => return error.FileNotFound, | |
| 1166 | posix.ENOMEM => return error.SystemResources, | |
| 1167 | posix.ENOSPC => return error.NoSpaceLeft, | |
| 1168 | posix.ENOTDIR => return error.NotDir, | |
| 1169 | posix.EROFS => return error.ReadOnlyFileSystem, | |
| 1170 | else => return unexpectedErrorPosix(err), | |
| 1141 | 1171 | } |
| 1142 | 1172 | } |
| 1143 | 1173 | |
| 1174 | pub fn makeDirPosix(dir_path: []const u8) !void { | |
| 1175 | const dir_path_c = try toPosixPath(dir_path); | |
| 1176 | return makeDirPosixC(&dir_path_c); | |
| 1177 | } | |
| 1178 | ||
| 1144 | 1179 | /// Calls makeDir recursively to make an entire path. Returns success if the path |
| 1145 | 1180 | /// already exists and is a directory. |
| 1181 | /// TODO determine if we can remove the allocator requirement from this function | |
| 1146 | 1182 | pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { |
| 1147 | 1183 | const resolved_path = try path.resolve(allocator, full_path); |
| 1148 | 1184 | defer allocator.free(resolved_path); |
| 1149 | 1185 | |
| 1150 | 1186 | var end_index: usize = resolved_path.len; |
| 1151 | 1187 | while (true) { |
| 1152 | makeDir(allocator, resolved_path[0..end_index]) catch |err| switch (err) { | |
| 1188 | makeDir(resolved_path[0..end_index]) catch |err| switch (err) { | |
| 1153 | 1189 | error.PathAlreadyExists => { |
| 1154 | 1190 | // TODO stat the file and return an error if it's not a directory |
| 1155 | 1191 | // this is important because otherwise a dangling symlink |
| ... | ... | @@ -1187,6 +1223,7 @@ pub const DeleteDirError = error{ |
| 1187 | 1223 | ReadOnlyFileSystem, |
| 1188 | 1224 | OutOfMemory, |
| 1189 | 1225 | |
| 1226 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 1190 | 1227 | Unexpected, |
| 1191 | 1228 | }; |
| 1192 | 1229 | |
| ... | ... | @@ -1245,7 +1282,6 @@ const DeleteTreeError = error{ |
| 1245 | 1282 | NameTooLong, |
| 1246 | 1283 | SystemFdQuotaExceeded, |
| 1247 | 1284 | NoDevice, |
| 1248 | PathNotFound, | |
| 1249 | 1285 | SystemResources, |
| 1250 | 1286 | NoSpaceLeft, |
| 1251 | 1287 | PathAlreadyExists, |
| ... | ... | @@ -1255,20 +1291,30 @@ const DeleteTreeError = error{ |
| 1255 | 1291 | FileSystem, |
| 1256 | 1292 | FileBusy, |
| 1257 | 1293 | DirNotEmpty, |
| 1294 | ||
| 1295 | /// On Windows, file paths must be valid Unicode. | |
| 1296 | InvalidUtf8, | |
| 1297 | ||
| 1298 | /// On Windows, file paths cannot contain these characters: | |
| 1299 | /// '/', '*', '?', '"', '<', '>', '|' | |
| 1300 | BadPathName, | |
| 1301 | ||
| 1302 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 1258 | 1303 | Unexpected, |
| 1259 | 1304 | }; |
| 1305 | ||
| 1306 | /// TODO determine if we can remove the allocator requirement | |
| 1260 | 1307 | pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!void { |
| 1261 | 1308 | start_over: while (true) { |
| 1262 | 1309 | var got_access_denied = false; |
| 1263 | 1310 | // First, try deleting the item as a file. This way we don't follow sym links. |
| 1264 | if (deleteFile(allocator, full_path)) { | |
| 1311 | if (deleteFile(full_path)) { | |
| 1265 | 1312 | return; |
| 1266 | 1313 | } else |err| switch (err) { |
| 1267 | 1314 | error.FileNotFound => return, |
| 1268 | 1315 | error.IsDir => {}, |
| 1269 | 1316 | error.AccessDenied => got_access_denied = true, |
| 1270 | 1317 | |
| 1271 | error.OutOfMemory, | |
| 1272 | 1318 | error.SymLinkLoop, |
| 1273 | 1319 | error.NameTooLong, |
| 1274 | 1320 | error.SystemResources, |
| ... | ... | @@ -1276,6 +1322,8 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError! |
| 1276 | 1322 | error.NotDir, |
| 1277 | 1323 | error.FileSystem, |
| 1278 | 1324 | error.FileBusy, |
| 1325 | error.InvalidUtf8, | |
| 1326 | error.BadPathName, | |
| 1279 | 1327 | error.Unexpected, |
| 1280 | 1328 | => return err, |
| 1281 | 1329 | } |
| ... | ... | @@ -1297,7 +1345,7 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError! |
| 1297 | 1345 | error.NameTooLong, |
| 1298 | 1346 | error.SystemFdQuotaExceeded, |
| 1299 | 1347 | error.NoDevice, |
| 1300 | error.PathNotFound, | |
| 1348 | error.FileNotFound, | |
| 1301 | 1349 | error.SystemResources, |
| 1302 | 1350 | error.NoSpaceLeft, |
| 1303 | 1351 | error.PathAlreadyExists, |
| ... | ... | @@ -1367,7 +1415,7 @@ pub const Dir = struct { |
| 1367 | 1415 | }; |
| 1368 | 1416 | |
| 1369 | 1417 | pub const OpenError = error{ |
| 1370 | PathNotFound, | |
| 1418 | FileNotFound, | |
| 1371 | 1419 | NotDir, |
| 1372 | 1420 | AccessDenied, |
| 1373 | 1421 | FileTooBig, |
| ... | ... | @@ -1382,9 +1430,11 @@ pub const Dir = struct { |
| 1382 | 1430 | PathAlreadyExists, |
| 1383 | 1431 | OutOfMemory, |
| 1384 | 1432 | |
| 1433 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 1385 | 1434 | Unexpected, |
| 1386 | 1435 | }; |
| 1387 | 1436 | |
| 1437 | /// TODO remove the allocator requirement from this API | |
| 1388 | 1438 | pub fn open(allocator: *Allocator, dir_path: []const u8) OpenError!Dir { |
| 1389 | 1439 | return Dir{ |
| 1390 | 1440 | .allocator = allocator, |
| ... | ... | @@ -1400,7 +1450,6 @@ pub const Dir = struct { |
| 1400 | 1450 | }, |
| 1401 | 1451 | Os.macosx, Os.ios => Handle{ |
| 1402 | 1452 | .fd = try posixOpen( |
| 1403 | allocator, | |
| 1404 | 1453 | dir_path, |
| 1405 | 1454 | posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, |
| 1406 | 1455 | 0, |
| ... | ... | @@ -1412,7 +1461,6 @@ pub const Dir = struct { |
| 1412 | 1461 | }, |
| 1413 | 1462 | Os.linux => Handle{ |
| 1414 | 1463 | .fd = try posixOpen( |
| 1415 | allocator, | |
| 1416 | 1464 | dir_path, |
| 1417 | 1465 | posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, |
| 1418 | 1466 | 0, |
| ... | ... | @@ -1609,39 +1657,32 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void { |
| 1609 | 1657 | } |
| 1610 | 1658 | |
| 1611 | 1659 | /// Read value of a symbolic link. |
| 1612 | pub fn readLink(allocator: *Allocator, pathname: []const u8) ![]u8 { | |
| 1613 | const path_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1614 | defer allocator.free(path_buf); | |
| 1615 | ||
| 1616 | mem.copy(u8, path_buf, pathname); | |
| 1617 | path_buf[pathname.len] = 0; | |
| 1618 | ||
| 1619 | var result_buf = try allocator.alloc(u8, 1024); | |
| 1620 | errdefer allocator.free(result_buf); | |
| 1621 | while (true) { | |
| 1622 | const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len); | |
| 1623 | const err = posix.getErrno(ret_val); | |
| 1624 | if (err > 0) { | |
| 1625 | return switch (err) { | |
| 1626 | posix.EACCES => error.AccessDenied, | |
| 1627 | posix.EFAULT, posix.EINVAL => unreachable, | |
| 1628 | posix.EIO => error.FileSystem, | |
| 1629 | posix.ELOOP => error.SymLinkLoop, | |
| 1630 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 1631 | posix.ENOENT => error.FileNotFound, | |
| 1632 | posix.ENOMEM => error.SystemResources, | |
| 1633 | posix.ENOTDIR => error.NotDir, | |
| 1634 | else => unexpectedErrorPosix(err), | |
| 1635 | }; | |
| 1636 | } | |
| 1637 | if (ret_val == result_buf.len) { | |
| 1638 | result_buf = try allocator.realloc(u8, result_buf, result_buf.len * 2); | |
| 1639 | continue; | |
| 1640 | } | |
| 1641 | return allocator.shrink(u8, result_buf, ret_val); | |
| 1660 | /// The return value is a slice of out_buffer. | |
| 1661 | pub fn readLinkC(out_buffer: *[posix.PATH_MAX]u8, pathname: [*]const u8) ![]u8 { | |
| 1662 | const rc = posix.readlink(pathname, out_buffer, out_buffer.len); | |
| 1663 | const err = posix.getErrno(rc); | |
| 1664 | switch (err) { | |
| 1665 | 0 => return out_buffer[0..rc], | |
| 1666 | posix.EACCES => return error.AccessDenied, | |
| 1667 | posix.EFAULT => unreachable, | |
| 1668 | posix.EINVAL => unreachable, | |
| 1669 | posix.EIO => return error.FileSystem, | |
| 1670 | posix.ELOOP => return error.SymLinkLoop, | |
| 1671 | posix.ENAMETOOLONG => unreachable, // out_buffer is at least PATH_MAX | |
| 1672 | posix.ENOENT => return error.FileNotFound, | |
| 1673 | posix.ENOMEM => return error.SystemResources, | |
| 1674 | posix.ENOTDIR => return error.NotDir, | |
| 1675 | else => return unexpectedErrorPosix(err), | |
| 1642 | 1676 | } |
| 1643 | 1677 | } |
| 1644 | 1678 | |
| 1679 | /// Read value of a symbolic link. | |
| 1680 | /// The return value is a slice of out_buffer. | |
| 1681 | pub fn readLink(out_buffer: *[posix.PATH_MAX]u8, file_path: []const u8) ![]u8 { | |
| 1682 | const file_path_c = try toPosixPath(file_path); | |
| 1683 | return readLinkC(out_buffer, &file_path_c); | |
| 1684 | } | |
| 1685 | ||
| 1645 | 1686 | pub fn posix_setuid(uid: u32) !void { |
| 1646 | 1687 | const err = posix.getErrno(posix.setuid(uid)); |
| 1647 | 1688 | if (err == 0) return; |
| ... | ... | @@ -1688,6 +1729,8 @@ pub fn posix_setregid(rgid: u32, egid: u32) !void { |
| 1688 | 1729 | |
| 1689 | 1730 | pub const WindowsGetStdHandleErrs = error{ |
| 1690 | 1731 | NoStdHandles, |
| 1732 | ||
| 1733 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 1691 | 1734 | Unexpected, |
| 1692 | 1735 | }; |
| 1693 | 1736 | |
| ... | ... | @@ -2015,7 +2058,7 @@ pub fn unexpectedErrorPosix(errno: usize) UnexpectedError { |
| 2015 | 2058 | /// Call this when you made a windows DLL call or something that does SetLastError |
| 2016 | 2059 | /// and you get an unexpected error. |
| 2017 | 2060 | pub fn unexpectedErrorWindows(err: windows.DWORD) UnexpectedError { |
| 2018 | if (unexpected_error_tracing) { | |
| 2061 | if (true) { | |
| 2019 | 2062 | debug.warn("unexpected GetLastError(): {}\n", err); |
| 2020 | 2063 | debug.dumpCurrentStackTrace(null); |
| 2021 | 2064 | } |
| ... | ... | @@ -2024,17 +2067,12 @@ pub fn unexpectedErrorWindows(err: windows.DWORD) UnexpectedError { |
| 2024 | 2067 | |
| 2025 | 2068 | pub fn openSelfExe() !os.File { |
| 2026 | 2069 | switch (builtin.os) { |
| 2027 | Os.linux => { | |
| 2028 | const proc_file_path = "/proc/self/exe"; | |
| 2029 | var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined; | |
| 2030 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 2031 | return os.File.openRead(&fixed_allocator.allocator, proc_file_path); | |
| 2032 | }, | |
| 2070 | Os.linux => return os.File.openReadC(c"/proc/self/exe"), | |
| 2033 | 2071 | Os.macosx, Os.ios => { |
| 2034 | var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined; | |
| 2035 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 2036 | const self_exe_path = try selfExePath(&fixed_allocator.allocator); | |
| 2037 | return os.File.openRead(&fixed_allocator.allocator, self_exe_path); | |
| 2072 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 2073 | const self_exe_path = try selfExePath(&buf); | |
| 2074 | buf[self_exe_path.len] = 0; | |
| 2075 | return os.File.openReadC(self_exe_path.ptr); | |
| 2038 | 2076 | }, |
| 2039 | 2077 | else => @compileError("Unsupported OS"), |
| 2040 | 2078 | } |
| ... | ... | @@ -2043,7 +2081,7 @@ pub fn openSelfExe() !os.File { |
| 2043 | 2081 | test "openSelfExe" { |
| 2044 | 2082 | switch (builtin.os) { |
| 2045 | 2083 | Os.linux, Os.macosx, Os.ios => (try openSelfExe()).close(), |
| 2046 | else => return, // Unsupported OS. | |
| 2084 | else => return error.SkipZigTest, // Unsupported OS | |
| 2047 | 2085 | } |
| 2048 | 2086 | } |
| 2049 | 2087 | |
| ... | ... | @@ -2052,69 +2090,68 @@ test "openSelfExe" { |
| 2052 | 2090 | /// If you only want an open file handle, use openSelfExe. |
| 2053 | 2091 | /// This function may return an error if the current executable |
| 2054 | 2092 | /// was deleted after spawning. |
| 2055 | /// Caller owns returned memory. | |
| 2056 | pub fn selfExePath(allocator: *mem.Allocator) ![]u8 { | |
| 2093 | /// Returned value is a slice of out_buffer. | |
| 2094 | /// | |
| 2095 | /// On Linux, depends on procfs being mounted. If the currently executing binary has | |
| 2096 | /// been deleted, the file path looks something like `/a/b/c/exe (deleted)`. | |
| 2097 | /// TODO make the return type of this a null terminated pointer | |
| 2098 | pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { | |
| 2057 | 2099 | switch (builtin.os) { |
| 2058 | Os.linux => { | |
| 2059 | // If the currently executing binary has been deleted, | |
| 2060 | // the file path looks something like `/a/b/c/exe (deleted)` | |
| 2061 | return readLink(allocator, "/proc/self/exe"); | |
| 2062 | }, | |
| 2100 | Os.linux => return readLink(out_buffer, "/proc/self/exe"), | |
| 2063 | 2101 | Os.windows => { |
| 2064 | var out_path = try Buffer.initSize(allocator, 0xff); | |
| 2065 | errdefer out_path.deinit(); | |
| 2066 | while (true) { | |
| 2067 | const dword_len = try math.cast(windows.DWORD, out_path.len()); | |
| 2068 | const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len); | |
| 2069 | if (copied_amt <= 0) { | |
| 2070 | const err = windows.GetLastError(); | |
| 2071 | return switch (err) { | |
| 2072 | else => unexpectedErrorWindows(err), | |
| 2073 | }; | |
| 2074 | } | |
| 2075 | if (copied_amt < out_path.len()) { | |
| 2076 | out_path.shrink(copied_amt); | |
| 2077 | return out_path.toOwnedSlice(); | |
| 2102 | var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; | |
| 2103 | const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast | |
| 2104 | const rc = windows.GetModuleFileNameW(null, &utf16le_buf, casted_len); | |
| 2105 | assert(rc <= utf16le_buf.len); | |
| 2106 | if (rc == 0) { | |
| 2107 | const err = windows.GetLastError(); | |
| 2108 | switch (err) { | |
| 2109 | else => return unexpectedErrorWindows(err), | |
| 2078 | 2110 | } |
| 2079 | const new_len = (out_path.len() << 1) | 0b1; | |
| 2080 | try out_path.resize(new_len); | |
| 2081 | 2111 | } |
| 2112 | const utf16le_slice = utf16le_buf[0..rc]; | |
| 2113 | // Trust that Windows gives us valid UTF-16LE. | |
| 2114 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; | |
| 2115 | return out_buffer[0..end_index]; | |
| 2082 | 2116 | }, |
| 2083 | 2117 | Os.macosx, Os.ios => { |
| 2084 | var u32_len: u32 = 0; | |
| 2085 | const ret1 = c._NSGetExecutablePath(undefined, &u32_len); | |
| 2086 | assert(ret1 != 0); | |
| 2087 | const bytes = try allocator.alloc(u8, u32_len); | |
| 2088 | errdefer allocator.free(bytes); | |
| 2089 | const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len); | |
| 2090 | assert(ret2 == 0); | |
| 2091 | return bytes; | |
| 2118 | var u32_len: u32 = @intCast(u32, out_buffer.len); // TODO shouldn't need this cast | |
| 2119 | const rc = c._NSGetExecutablePath(out_buffer, &u32_len); | |
| 2120 | if (rc != 0) return error.NameTooLong; | |
| 2121 | return mem.toSlice(u8, out_buffer); | |
| 2092 | 2122 | }, |
| 2093 | 2123 | else => @compileError("Unsupported OS"), |
| 2094 | 2124 | } |
| 2095 | 2125 | } |
| 2096 | 2126 | |
| 2097 | /// Get the directory path that contains the current executable. | |
| 2127 | /// `selfExeDirPath` except allocates the result on the heap. | |
| 2098 | 2128 | /// Caller owns returned memory. |
| 2099 | pub fn selfExeDirPath(allocator: *mem.Allocator) ![]u8 { | |
| 2129 | pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { | |
| 2130 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 2131 | return mem.dupe(allocator, u8, try selfExeDirPath(&buf)); | |
| 2132 | } | |
| 2133 | ||
| 2134 | /// Get the directory path that contains the current executable. | |
| 2135 | /// Returned value is a slice of out_buffer. | |
| 2136 | pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) ![]const u8 { | |
| 2100 | 2137 | switch (builtin.os) { |
| 2101 | 2138 | Os.linux => { |
| 2102 | 2139 | // If the currently executing binary has been deleted, |
| 2103 | 2140 | // the file path looks something like `/a/b/c/exe (deleted)` |
| 2104 | 2141 | // This path cannot be opened, but it's valid for determining the directory |
| 2105 | 2142 | // the executable was in when it was run. |
| 2106 | const full_exe_path = try readLink(allocator, "/proc/self/exe"); | |
| 2107 | errdefer allocator.free(full_exe_path); | |
| 2108 | const dir = path.dirname(full_exe_path) orelse "."; | |
| 2109 | return allocator.shrink(u8, full_exe_path, dir.len); | |
| 2143 | const full_exe_path = try readLinkC(out_buffer, c"/proc/self/exe"); | |
| 2144 | // Assume that /proc/self/exe has an absolute path, and therefore dirname | |
| 2145 | // will not return null. | |
| 2146 | return path.dirname(full_exe_path).?; | |
| 2110 | 2147 | }, |
| 2111 | 2148 | Os.windows, Os.macosx, Os.ios => { |
| 2112 | const self_exe_path = try selfExePath(allocator); | |
| 2113 | errdefer allocator.free(self_exe_path); | |
| 2114 | const dirname = os.path.dirname(self_exe_path) orelse "."; | |
| 2115 | return allocator.shrink(u8, self_exe_path, dirname.len); | |
| 2149 | const self_exe_path = try selfExePath(out_buffer); | |
| 2150 | // Assume that the OS APIs return absolute paths, and therefore dirname | |
| 2151 | // will not return null. | |
| 2152 | return path.dirname(self_exe_path).?; | |
| 2116 | 2153 | }, |
| 2117 | else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)), | |
| 2154 | else => @compileError("Unsupported OS"), | |
| 2118 | 2155 | } |
| 2119 | 2156 | } |
| 2120 | 2157 | |
| ... | ... | @@ -2218,6 +2255,7 @@ pub const PosixBindError = error{ |
| 2218 | 2255 | /// The socket inode would reside on a read-only filesystem. |
| 2219 | 2256 | ReadOnlyFileSystem, |
| 2220 | 2257 | |
| 2258 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2221 | 2259 | Unexpected, |
| 2222 | 2260 | }; |
| 2223 | 2261 | |
| ... | ... | @@ -2261,6 +2299,7 @@ const PosixListenError = error{ |
| 2261 | 2299 | /// The socket is not of a type that supports the listen() operation. |
| 2262 | 2300 | OperationNotSupported, |
| 2263 | 2301 | |
| 2302 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2264 | 2303 | Unexpected, |
| 2265 | 2304 | }; |
| 2266 | 2305 | |
| ... | ... | @@ -2314,6 +2353,7 @@ pub const PosixAcceptError = error{ |
| 2314 | 2353 | /// Firewall rules forbid connection. |
| 2315 | 2354 | BlockedByFirewall, |
| 2316 | 2355 | |
| 2356 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2317 | 2357 | Unexpected, |
| 2318 | 2358 | }; |
| 2319 | 2359 | |
| ... | ... | @@ -2359,6 +2399,7 @@ pub const LinuxEpollCreateError = error{ |
| 2359 | 2399 | /// There was insufficient memory to create the kernel object. |
| 2360 | 2400 | SystemResources, |
| 2361 | 2401 | |
| 2402 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2362 | 2403 | Unexpected, |
| 2363 | 2404 | }; |
| 2364 | 2405 | |
| ... | ... | @@ -2413,6 +2454,7 @@ pub const LinuxEpollCtlError = error{ |
| 2413 | 2454 | /// for example, a regular file or a directory. |
| 2414 | 2455 | FileDescriptorIncompatibleWithEpoll, |
| 2415 | 2456 | |
| 2457 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2416 | 2458 | Unexpected, |
| 2417 | 2459 | }; |
| 2418 | 2460 | |
| ... | ... | @@ -2455,6 +2497,7 @@ pub const LinuxEventFdError = error{ |
| 2455 | 2497 | ProcessFdQuotaExceeded, |
| 2456 | 2498 | SystemFdQuotaExceeded, |
| 2457 | 2499 | |
| 2500 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2458 | 2501 | Unexpected, |
| 2459 | 2502 | }; |
| 2460 | 2503 | |
| ... | ... | @@ -2477,6 +2520,7 @@ pub const PosixGetSockNameError = error{ |
| 2477 | 2520 | /// Insufficient resources were available in the system to perform the operation. |
| 2478 | 2521 | SystemResources, |
| 2479 | 2522 | |
| 2523 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2480 | 2524 | Unexpected, |
| 2481 | 2525 | }; |
| 2482 | 2526 | |
| ... | ... | @@ -2530,6 +2574,7 @@ pub const PosixConnectError = error{ |
| 2530 | 2574 | /// that for IP sockets the timeout may be very long when syncookies are enabled on the server. |
| 2531 | 2575 | ConnectionTimedOut, |
| 2532 | 2576 | |
| 2577 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2533 | 2578 | Unexpected, |
| 2534 | 2579 | }; |
| 2535 | 2580 | |
| ... | ... | @@ -2751,6 +2796,7 @@ pub const SpawnThreadError = error{ |
| 2751 | 2796 | /// Not enough userland memory to spawn the thread. |
| 2752 | 2797 | OutOfMemory, |
| 2753 | 2798 | |
| 2799 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2754 | 2800 | Unexpected, |
| 2755 | 2801 | }; |
| 2756 | 2802 | |
| ... | ... | @@ -2926,7 +2972,9 @@ pub fn posixFStat(fd: i32) !posix.Stat { |
| 2926 | 2972 | const err = posix.getErrno(posix.fstat(fd, &stat)); |
| 2927 | 2973 | if (err > 0) { |
| 2928 | 2974 | return switch (err) { |
| 2929 | posix.EBADF => error.BadFd, | |
| 2975 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 2976 | // since the fd could have been reused. | |
| 2977 | posix.EBADF => unreachable, | |
| 2930 | 2978 | posix.ENOMEM => error.SystemResources, |
| 2931 | 2979 | else => os.unexpectedErrorPosix(err), |
| 2932 | 2980 | }; |
| ... | ... | @@ -2938,6 +2986,8 @@ pub fn posixFStat(fd: i32) !posix.Stat { |
| 2938 | 2986 | pub const CpuCountError = error{ |
| 2939 | 2987 | OutOfMemory, |
| 2940 | 2988 | PermissionDenied, |
| 2989 | ||
| 2990 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 2941 | 2991 | Unexpected, |
| 2942 | 2992 | }; |
| 2943 | 2993 | |
| ... | ... | @@ -3008,6 +3058,7 @@ pub const BsdKQueueError = error{ |
| 3008 | 3058 | /// The system-wide limit on the total number of open files has been reached. |
| 3009 | 3059 | SystemFdQuotaExceeded, |
| 3010 | 3060 | |
| 3061 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 3011 | 3062 | Unexpected, |
| 3012 | 3063 | }; |
| 3013 | 3064 |
std/os/path.zig+137-98| ... | ... | @@ -11,11 +11,14 @@ const math = std.math; |
| 11 | 11 | const posix = os.posix; |
| 12 | 12 | const windows = os.windows; |
| 13 | 13 | const cstr = std.cstr; |
| 14 | const windows_util = @import("windows/util.zig"); | |
| 14 | 15 | |
| 15 | 16 | pub const sep_windows = '\\'; |
| 16 | 17 | pub const sep_posix = '/'; |
| 17 | 18 | pub const sep = if (is_windows) sep_windows else sep_posix; |
| 18 | 19 | |
| 20 | pub const sep_str = [1]u8{sep}; | |
| 21 | ||
| 19 | 22 | pub const delimiter_windows = ';'; |
| 20 | 23 | pub const delimiter_posix = ':'; |
| 21 | 24 | pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix; |
| ... | ... | @@ -337,7 +340,7 @@ pub fn resolveSlice(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 337 | 340 | pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 338 | 341 | if (paths.len == 0) { |
| 339 | 342 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 340 | return os.getCwd(allocator); | |
| 343 | return os.getCwdAlloc(allocator); | |
| 341 | 344 | } |
| 342 | 345 | |
| 343 | 346 | // determine which disk designator we will result with, if any |
| ... | ... | @@ -432,7 +435,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 432 | 435 | }, |
| 433 | 436 | WindowsPath.Kind.None => { |
| 434 | 437 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 435 | const cwd = try os.getCwd(allocator); | |
| 438 | const cwd = try os.getCwdAlloc(allocator); | |
| 436 | 439 | defer allocator.free(cwd); |
| 437 | 440 | const parsed_cwd = windowsParsePath(cwd); |
| 438 | 441 | result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); |
| ... | ... | @@ -448,7 +451,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 448 | 451 | } else { |
| 449 | 452 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 450 | 453 | // TODO call get cwd for the result_disk_designator instead of the global one |
| 451 | const cwd = try os.getCwd(allocator); | |
| 454 | const cwd = try os.getCwdAlloc(allocator); | |
| 452 | 455 | defer allocator.free(cwd); |
| 453 | 456 | |
| 454 | 457 | result = try allocator.alloc(u8, max_size + cwd.len + 1); |
| ... | ... | @@ -516,7 +519,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 516 | 519 | pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 517 | 520 | if (paths.len == 0) { |
| 518 | 521 | assert(!is_windows); // resolvePosix called on windows can't use getCwd |
| 519 | return os.getCwd(allocator); | |
| 522 | return os.getCwdAlloc(allocator); | |
| 520 | 523 | } |
| 521 | 524 | |
| 522 | 525 | var first_index: usize = 0; |
| ... | ... | @@ -538,7 +541,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 538 | 541 | result = try allocator.alloc(u8, max_size); |
| 539 | 542 | } else { |
| 540 | 543 | assert(!is_windows); // resolvePosix called on windows can't use getCwd |
| 541 | const cwd = try os.getCwd(allocator); | |
| 544 | const cwd = try os.getCwdAlloc(allocator); | |
| 542 | 545 | defer allocator.free(cwd); |
| 543 | 546 | result = try allocator.alloc(u8, max_size + cwd.len + 1); |
| 544 | 547 | mem.copy(u8, result, cwd); |
| ... | ... | @@ -573,11 +576,11 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { |
| 573 | 576 | result_index += 1; |
| 574 | 577 | } |
| 575 | 578 | |
| 576 | return result[0..result_index]; | |
| 579 | return allocator.shrink(u8, result, result_index); | |
| 577 | 580 | } |
| 578 | 581 | |
| 579 | 582 | test "os.path.resolve" { |
| 580 | const cwd = try os.getCwd(debug.global_allocator); | |
| 583 | const cwd = try os.getCwdAlloc(debug.global_allocator); | |
| 581 | 584 | if (is_windows) { |
| 582 | 585 | if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { |
| 583 | 586 | cwd[0] = asciiUpper(cwd[0]); |
| ... | ... | @@ -591,7 +594,7 @@ test "os.path.resolve" { |
| 591 | 594 | |
| 592 | 595 | test "os.path.resolveWindows" { |
| 593 | 596 | if (is_windows) { |
| 594 | const cwd = try os.getCwd(debug.global_allocator); | |
| 597 | const cwd = try os.getCwdAlloc(debug.global_allocator); | |
| 595 | 598 | const parsed_cwd = windowsParsePath(cwd); |
| 596 | 599 | { |
| 597 | 600 | const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }); |
| ... | ... | @@ -1073,112 +1076,148 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons |
| 1073 | 1076 | assert(mem.eql(u8, result, expected_output)); |
| 1074 | 1077 | } |
| 1075 | 1078 | |
| 1076 | /// Return the canonicalized absolute pathname. | |
| 1077 | /// Expands all symbolic links and resolves references to `.`, `..`, and | |
| 1078 | /// extra `/` characters in ::pathname. | |
| 1079 | /// Caller must deallocate result. | |
| 1080 | pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 { | |
| 1081 | switch (builtin.os) { | |
| 1082 | Os.windows => { | |
| 1083 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1084 | defer allocator.free(pathname_buf); | |
| 1085 | ||
| 1086 | mem.copy(u8, pathname_buf, pathname); | |
| 1087 | pathname_buf[pathname.len] = 0; | |
| 1088 | ||
| 1089 | const h_file = windows.CreateFileA(pathname_buf.ptr, windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null); | |
| 1090 | if (h_file == windows.INVALID_HANDLE_VALUE) { | |
| 1091 | const err = windows.GetLastError(); | |
| 1092 | return switch (err) { | |
| 1093 | windows.ERROR.FILE_NOT_FOUND => error.FileNotFound, | |
| 1094 | windows.ERROR.ACCESS_DENIED => error.AccessDenied, | |
| 1095 | windows.ERROR.FILENAME_EXCED_RANGE => error.NameTooLong, | |
| 1096 | else => os.unexpectedErrorWindows(err), | |
| 1097 | }; | |
| 1098 | } | |
| 1099 | defer os.close(h_file); | |
| 1100 | var buf = try allocator.alloc(u8, 256); | |
| 1101 | errdefer allocator.free(buf); | |
| 1102 | while (true) { | |
| 1103 | const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong; | |
| 1104 | const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS); | |
| 1105 | ||
| 1106 | if (result == 0) { | |
| 1107 | const err = windows.GetLastError(); | |
| 1108 | return switch (err) { | |
| 1109 | windows.ERROR.PATH_NOT_FOUND => error.FileNotFound, | |
| 1110 | windows.ERROR.NOT_ENOUGH_MEMORY => error.OutOfMemory, | |
| 1111 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 1112 | else => os.unexpectedErrorWindows(err), | |
| 1113 | }; | |
| 1114 | } | |
| 1079 | pub const RealError = error{ | |
| 1080 | FileNotFound, | |
| 1081 | AccessDenied, | |
| 1082 | NameTooLong, | |
| 1083 | NotSupported, | |
| 1084 | NotDir, | |
| 1085 | SymLinkLoop, | |
| 1086 | InputOutput, | |
| 1087 | FileTooBig, | |
| 1088 | IsDir, | |
| 1089 | ProcessFdQuotaExceeded, | |
| 1090 | SystemFdQuotaExceeded, | |
| 1091 | NoDevice, | |
| 1092 | SystemResources, | |
| 1093 | NoSpaceLeft, | |
| 1094 | FileSystem, | |
| 1095 | BadPathName, | |
| 1096 | ||
| 1097 | /// On Windows, file paths must be valid Unicode. | |
| 1098 | InvalidUtf8, | |
| 1099 | ||
| 1100 | /// TODO remove this possibility | |
| 1101 | PathAlreadyExists, | |
| 1102 | ||
| 1103 | /// TODO remove this possibility | |
| 1104 | Unexpected, | |
| 1105 | }; | |
| 1115 | 1106 | |
| 1116 | if (result > buf.len) { | |
| 1117 | buf = try allocator.realloc(u8, buf, result); | |
| 1118 | continue; | |
| 1119 | } | |
| 1107 | /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. | |
| 1108 | /// Otherwise use `real` or `realC`. | |
| 1109 | pub fn realW(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u16) RealError![]u8 { | |
| 1110 | const h_file = windows.CreateFileW( | |
| 1111 | pathname, | |
| 1112 | windows.GENERIC_READ, | |
| 1113 | windows.FILE_SHARE_READ, | |
| 1114 | null, | |
| 1115 | windows.OPEN_EXISTING, | |
| 1116 | windows.FILE_ATTRIBUTE_NORMAL, | |
| 1117 | null, | |
| 1118 | ); | |
| 1119 | if (h_file == windows.INVALID_HANDLE_VALUE) { | |
| 1120 | const err = windows.GetLastError(); | |
| 1121 | switch (err) { | |
| 1122 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1123 | windows.ERROR.ACCESS_DENIED => return error.AccessDenied, | |
| 1124 | windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, | |
| 1125 | else => return os.unexpectedErrorWindows(err), | |
| 1126 | } | |
| 1127 | } | |
| 1128 | defer os.close(h_file); | |
| 1129 | var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; | |
| 1130 | const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast | |
| 1131 | const result = windows.GetFinalPathNameByHandleW(h_file, &utf16le_buf, casted_len, windows.VOLUME_NAME_DOS); | |
| 1132 | assert(result <= utf16le_buf.len); | |
| 1133 | if (result == 0) { | |
| 1134 | const err = windows.GetLastError(); | |
| 1135 | switch (err) { | |
| 1136 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1137 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 1138 | windows.ERROR.NOT_ENOUGH_MEMORY => return error.SystemResources, | |
| 1139 | windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, | |
| 1140 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 1141 | else => return os.unexpectedErrorWindows(err), | |
| 1142 | } | |
| 1143 | } | |
| 1144 | const utf16le_slice = utf16le_buf[0..result]; | |
| 1120 | 1145 | |
| 1121 | // windows returns \\?\ prepended to the path | |
| 1122 | // we strip it because nobody wants \\?\ prepended to their path | |
| 1123 | const final_len = x: { | |
| 1124 | if (result > 4 and mem.startsWith(u8, buf, "\\\\?\\")) { | |
| 1125 | var i: usize = 4; | |
| 1126 | while (i < result) : (i += 1) { | |
| 1127 | buf[i - 4] = buf[i]; | |
| 1128 | } | |
| 1129 | break :x result - 4; | |
| 1130 | } else { | |
| 1131 | break :x result; | |
| 1132 | } | |
| 1133 | }; | |
| 1134 | ||
| 1135 | return allocator.shrink(u8, buf, final_len); | |
| 1136 | } | |
| 1146 | // windows returns \\?\ prepended to the path | |
| 1147 | // we strip it because nobody wants \\?\ prepended to their path | |
| 1148 | const prefix = []u16{ '\\', '\\', '?', '\\' }; | |
| 1149 | const start_index = if (mem.startsWith(u16, utf16le_slice, prefix)) prefix.len else 0; | |
| 1150 | ||
| 1151 | // Trust that Windows gives us valid UTF-16LE. | |
| 1152 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice[start_index..]) catch unreachable; | |
| 1153 | return out_buffer[0..end_index]; | |
| 1154 | } | |
| 1155 | ||
| 1156 | /// See `real` | |
| 1157 | /// Use this when you have a null terminated pointer path. | |
| 1158 | pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealError![]u8 { | |
| 1159 | switch (builtin.os) { | |
| 1160 | Os.windows => { | |
| 1161 | const pathname_w = try windows_util.cStrToPrefixedFileW(pathname); | |
| 1162 | return realW(out_buffer, pathname_w); | |
| 1137 | 1163 | }, |
| 1138 | 1164 | Os.macosx, Os.ios => { |
| 1139 | // TODO instead of calling the libc function here, port the implementation | |
| 1140 | // to Zig, and then remove the NameTooLong error possibility. | |
| 1141 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1142 | defer allocator.free(pathname_buf); | |
| 1143 | ||
| 1144 | const result_buf = try allocator.alloc(u8, posix.PATH_MAX); | |
| 1145 | errdefer allocator.free(result_buf); | |
| 1146 | ||
| 1147 | mem.copy(u8, pathname_buf, pathname); | |
| 1148 | pathname_buf[pathname.len] = 0; | |
| 1149 | ||
| 1150 | const err = posix.getErrno(posix.realpath(pathname_buf.ptr, result_buf.ptr)); | |
| 1151 | if (err > 0) { | |
| 1152 | return switch (err) { | |
| 1153 | posix.EINVAL => unreachable, | |
| 1154 | posix.EBADF => unreachable, | |
| 1155 | posix.EFAULT => unreachable, | |
| 1156 | posix.EACCES => error.AccessDenied, | |
| 1157 | posix.ENOENT => error.FileNotFound, | |
| 1158 | posix.ENOTSUP => error.NotSupported, | |
| 1159 | posix.ENOTDIR => error.NotDir, | |
| 1160 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 1161 | posix.ELOOP => error.SymLinkLoop, | |
| 1162 | posix.EIO => error.InputOutput, | |
| 1163 | else => os.unexpectedErrorPosix(err), | |
| 1164 | }; | |
| 1165 | // TODO instead of calling the libc function here, port the implementation to Zig | |
| 1166 | const err = posix.getErrno(posix.realpath(pathname, out_buffer)); | |
| 1167 | switch (err) { | |
| 1168 | 0 => return mem.toSlice(u8, out_buffer), | |
| 1169 | posix.EINVAL => unreachable, | |
| 1170 | posix.EBADF => unreachable, | |
| 1171 | posix.EFAULT => unreachable, | |
| 1172 | posix.EACCES => return error.AccessDenied, | |
| 1173 | posix.ENOENT => return error.FileNotFound, | |
| 1174 | posix.ENOTSUP => return error.NotSupported, | |
| 1175 | posix.ENOTDIR => return error.NotDir, | |
| 1176 | posix.ENAMETOOLONG => return error.NameTooLong, | |
| 1177 | posix.ELOOP => return error.SymLinkLoop, | |
| 1178 | posix.EIO => return error.InputOutput, | |
| 1179 | else => return os.unexpectedErrorPosix(err), | |
| 1165 | 1180 | } |
| 1166 | return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); | |
| 1167 | 1181 | }, |
| 1168 | 1182 | Os.linux => { |
| 1169 | const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); | |
| 1183 | const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); | |
| 1170 | 1184 | defer os.close(fd); |
| 1171 | 1185 | |
| 1172 | 1186 | var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; |
| 1173 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}", fd) catch unreachable; | |
| 1187 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable; | |
| 1174 | 1188 | |
| 1175 | return os.readLink(allocator, proc_path); | |
| 1189 | return os.readLinkC(out_buffer, proc_path.ptr); | |
| 1176 | 1190 | }, |
| 1177 | 1191 | else => @compileError("TODO implement os.path.real for " ++ @tagName(builtin.os)), |
| 1178 | 1192 | } |
| 1179 | 1193 | } |
| 1180 | 1194 | |
| 1195 | /// Return the canonicalized absolute pathname. | |
| 1196 | /// Expands all symbolic links and resolves references to `.`, `..`, and | |
| 1197 | /// extra `/` characters in ::pathname. | |
| 1198 | /// The return value is a slice of out_buffer, and not necessarily from the beginning. | |
| 1199 | pub fn real(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: []const u8) RealError![]u8 { | |
| 1200 | switch (builtin.os) { | |
| 1201 | Os.windows => { | |
| 1202 | const pathname_w = try windows_util.sliceToPrefixedFileW(pathname); | |
| 1203 | return realW(out_buffer, &pathname_w); | |
| 1204 | }, | |
| 1205 | Os.macosx, Os.ios, Os.linux => { | |
| 1206 | const pathname_c = try os.toPosixPath(pathname); | |
| 1207 | return realC(out_buffer, &pathname_c); | |
| 1208 | }, | |
| 1209 | else => @compileError("Unsupported OS"), | |
| 1210 | } | |
| 1211 | } | |
| 1212 | ||
| 1213 | /// `real`, except caller must free the returned memory. | |
| 1214 | pub fn realAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { | |
| 1215 | var buf: [os.MAX_PATH_BYTES]u8 = undefined; | |
| 1216 | return mem.dupe(allocator, u8, try real(&buf, pathname)); | |
| 1217 | } | |
| 1218 | ||
| 1181 | 1219 | test "os.path.real" { |
| 1182 | 1220 | // at least call it so it gets compiled |
| 1183 | _ = real(debug.global_allocator, "some_path"); | |
| 1221 | var buf: [os.MAX_PATH_BYTES]u8 = undefined; | |
| 1222 | std.debug.assertError(real(&buf, "definitely_bogus_does_not_exist1234"), error.FileNotFound); | |
| 1184 | 1223 | } |
std/os/test.zig+8-8| ... | ... | @@ -10,27 +10,27 @@ const AtomicRmwOp = builtin.AtomicRmwOp; |
| 10 | 10 | const AtomicOrder = builtin.AtomicOrder; |
| 11 | 11 | |
| 12 | 12 | test "makePath, put some files in it, deleteTree" { |
| 13 | try os.makePath(a, "os_test_tmp/b/c"); | |
| 14 | try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense"); | |
| 15 | try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah"); | |
| 13 | try os.makePath(a, "os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c"); | |
| 14 | try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c" ++ os.path.sep_str ++ "file.txt", "nonsense"); | |
| 15 | try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "file2.txt", "blah"); | |
| 16 | 16 | try os.deleteTree(a, "os_test_tmp"); |
| 17 | 17 | if (os.Dir.open(a, "os_test_tmp")) |dir| { |
| 18 | 18 | @panic("expected error"); |
| 19 | 19 | } else |err| { |
| 20 | assert(err == error.PathNotFound); | |
| 20 | assert(err == error.FileNotFound); | |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | test "access file" { |
| 25 | 25 | try os.makePath(a, "os_test_tmp"); |
| 26 | if (os.File.access(a, "os_test_tmp/file.txt")) |ok| { | |
| 26 | if (os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { | |
| 27 | 27 | @panic("expected error"); |
| 28 | 28 | } else |err| { |
| 29 | assert(err == error.NotFound); | |
| 29 | assert(err == error.FileNotFound); | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | try io.writeFile(a, "os_test_tmp/file.txt", ""); | |
| 33 | try os.File.access(a, "os_test_tmp/file.txt"); | |
| 32 | try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", ""); | |
| 33 | try os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt"); | |
| 34 | 34 | try os.deleteTree(a, "os_test_tmp"); |
| 35 | 35 | } |
| 36 | 36 |
std/os/windows/kernel32.zig+25-13| ... | ... | @@ -1,14 +1,11 @@ |
| 1 | 1 | use @import("index.zig"); |
| 2 | 2 | |
| 3 | ||
| 4 | 3 | pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVERLAPPED) BOOL; |
| 5 | 4 | |
| 6 | 5 | pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL; |
| 7 | 6 | |
| 8 | pub extern "kernel32" stdcallcc fn CreateDirectoryA( | |
| 9 | lpPathName: LPCSTR, | |
| 10 | lpSecurityAttributes: ?*SECURITY_ATTRIBUTES, | |
| 11 | ) BOOL; | |
| 7 | pub extern "kernel32" stdcallcc fn CreateDirectoryA(lpPathName: [*]const u8, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 8 | pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 12 | 9 | |
| 13 | 10 | pub extern "kernel32" stdcallcc fn CreateFileA( |
| 14 | 11 | lpFileName: [*]const u8, // TODO null terminated pointer type |
| ... | ... | @@ -60,7 +57,8 @@ pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, Ex |
| 60 | 57 | |
| 61 | 58 | pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE; |
| 62 | 59 | |
| 63 | pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) BOOL; | |
| 60 | pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: [*]const u8) BOOL; | |
| 61 | pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: [*]const u16) BOOL; | |
| 64 | 62 | |
| 65 | 63 | pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn; |
| 66 | 64 | |
| ... | ... | @@ -74,7 +72,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR; |
| 74 | 72 | |
| 75 | 73 | pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) BOOL; |
| 76 | 74 | |
| 77 | pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD; | |
| 75 | pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: DWORD, lpBuffer: ?[*]CHAR) DWORD; | |
| 76 | pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: ?[*]WCHAR) DWORD; | |
| 78 | 77 | |
| 79 | 78 | pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE; |
| 80 | 79 | pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD; |
| ... | ... | @@ -87,9 +86,11 @@ pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCo |
| 87 | 86 | |
| 88 | 87 | pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LARGE_INTEGER) BOOL; |
| 89 | 88 | |
| 90 | pub extern "kernel32" stdcallcc fn GetFileAttributesA(lpFileName: LPCSTR) DWORD; | |
| 89 | pub extern "kernel32" stdcallcc fn GetFileAttributesA(lpFileName: [*]const CHAR) DWORD; | |
| 90 | pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD; | |
| 91 | 91 | |
| 92 | pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) DWORD; | |
| 92 | pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: [*]u8, nSize: DWORD) DWORD; | |
| 93 | pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) DWORD; | |
| 93 | 94 | |
| 94 | 95 | pub extern "kernel32" stdcallcc fn GetLastError() DWORD; |
| 95 | 96 | |
| ... | ... | @@ -107,6 +108,12 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA( |
| 107 | 108 | dwFlags: DWORD, |
| 108 | 109 | ) DWORD; |
| 109 | 110 | |
| 111 | pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW( | |
| 112 | hFile: HANDLE, | |
| 113 | lpszFilePath: [*]u16, | |
| 114 | cchFilePath: DWORD, | |
| 115 | dwFlags: DWORD, | |
| 116 | ) DWORD; | |
| 110 | 117 | |
| 111 | 118 | pub extern "kernel32" stdcallcc fn GetOverlappedResult(hFile: HANDLE, lpOverlapped: *OVERLAPPED, lpNumberOfBytesTransferred: *DWORD, bWait: BOOL) BOOL; |
| 112 | 119 | |
| ... | ... | @@ -132,8 +139,14 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem |
| 132 | 139 | pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL; |
| 133 | 140 | |
| 134 | 141 | pub extern "kernel32" stdcallcc fn MoveFileExA( |
| 135 | lpExistingFileName: LPCSTR, | |
| 136 | lpNewFileName: LPCSTR, | |
| 142 | lpExistingFileName: [*]const u8, | |
| 143 | lpNewFileName: [*]const u8, | |
| 144 | dwFlags: DWORD, | |
| 145 | ) BOOL; | |
| 146 | ||
| 147 | pub extern "kernel32" stdcallcc fn MoveFileExW( | |
| 148 | lpExistingFileName: [*]const u16, | |
| 149 | lpNewFileName: [*]const u16, | |
| 137 | 150 | dwFlags: DWORD, |
| 138 | 151 | ) BOOL; |
| 139 | 152 | |
| ... | ... | @@ -194,7 +207,6 @@ pub extern "kernel32" stdcallcc fn LoadLibraryA(lpLibFileName: LPCSTR) ?HMODULE; |
| 194 | 207 | |
| 195 | 208 | pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL; |
| 196 | 209 | |
| 197 | ||
| 198 | 210 | pub const FILE_NOTIFY_INFORMATION = extern struct { |
| 199 | 211 | NextEntryOffset: DWORD, |
| 200 | 212 | Action: DWORD, |
| ... | ... | @@ -208,7 +220,7 @@ pub const FILE_ACTION_MODIFIED = 0x00000003; |
| 208 | 220 | pub const FILE_ACTION_RENAMED_OLD_NAME = 0x00000004; |
| 209 | 221 | pub const FILE_ACTION_RENAMED_NEW_NAME = 0x00000005; |
| 210 | 222 | |
| 211 | pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn(DWORD, DWORD, *OVERLAPPED) void; | |
| 223 | pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn (DWORD, DWORD, *OVERLAPPED) void; | |
| 212 | 224 | |
| 213 | 225 | pub const FILE_LIST_DIRECTORY = 1; |
| 214 | 226 |
std/os/windows/util.zig+73-19| ... | ... | @@ -7,9 +7,17 @@ const mem = std.mem; |
| 7 | 7 | const BufMap = std.BufMap; |
| 8 | 8 | const cstr = std.cstr; |
| 9 | 9 | |
| 10 | // > The maximum path of 32,767 characters is approximate, because the "\\?\" | |
| 11 | // > prefix may be expanded to a longer string by the system at run time, and | |
| 12 | // > this expansion applies to the total length. | |
| 13 | // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation | |
| 14 | pub const PATH_MAX_WIDE = 32767; | |
| 15 | ||
| 10 | 16 | pub const WaitError = error{ |
| 11 | 17 | WaitAbandoned, |
| 12 | 18 | WaitTimeOut, |
| 19 | ||
| 20 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 13 | 21 | Unexpected, |
| 14 | 22 | }; |
| 15 | 23 | |
| ... | ... | @@ -37,6 +45,8 @@ pub const WriteError = error{ |
| 37 | 45 | SystemResources, |
| 38 | 46 | OperationAborted, |
| 39 | 47 | BrokenPipe, |
| 48 | ||
| 49 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 40 | 50 | Unexpected, |
| 41 | 51 | }; |
| 42 | 52 | |
| ... | ... | @@ -86,37 +96,51 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool { |
| 86 | 96 | pub const OpenError = error{ |
| 87 | 97 | SharingViolation, |
| 88 | 98 | PathAlreadyExists, |
| 99 | ||
| 100 | /// When any of the path components can not be found or the file component can not | |
| 101 | /// be found. Some operating systems distinguish between path components not found and | |
| 102 | /// file components not found, but they are collapsed into FileNotFound to gain | |
| 103 | /// consistency across operating systems. | |
| 89 | 104 | FileNotFound, |
| 105 | ||
| 90 | 106 | AccessDenied, |
| 91 | 107 | PipeBusy, |
| 108 | NameTooLong, | |
| 109 | ||
| 110 | /// On Windows, file paths must be valid Unicode. | |
| 111 | InvalidUtf8, | |
| 112 | ||
| 113 | /// On Windows, file paths cannot contain these characters: | |
| 114 | /// '/', '*', '?', '"', '<', '>', '|' | |
| 115 | BadPathName, | |
| 116 | ||
| 117 | /// See https://github.com/ziglang/zig/issues/1396 | |
| 92 | 118 | Unexpected, |
| 93 | OutOfMemory, | |
| 94 | 119 | }; |
| 95 | 120 | |
| 96 | /// `file_path` needs to be copied in memory to add a null terminating byte, hence the allocator. | |
| 97 | 121 | pub fn windowsOpen( |
| 98 | allocator: *mem.Allocator, | |
| 99 | 122 | file_path: []const u8, |
| 100 | 123 | desired_access: windows.DWORD, |
| 101 | 124 | share_mode: windows.DWORD, |
| 102 | 125 | creation_disposition: windows.DWORD, |
| 103 | 126 | flags_and_attrs: windows.DWORD, |
| 104 | 127 | ) OpenError!windows.HANDLE { |
| 105 | const path_with_null = try cstr.addNullByte(allocator, file_path); | |
| 106 | defer allocator.free(path_with_null); | |
| 128 | const file_path_w = try sliceToPrefixedFileW(file_path); | |
| 107 | 129 | |
| 108 | const result = windows.CreateFileA(path_with_null.ptr, desired_access, share_mode, null, creation_disposition, flags_and_attrs, null); | |
| 130 | const result = windows.CreateFileW(&file_path_w, desired_access, share_mode, null, creation_disposition, flags_and_attrs, null); | |
| 109 | 131 | |
| 110 | 132 | if (result == windows.INVALID_HANDLE_VALUE) { |
| 111 | 133 | const err = windows.GetLastError(); |
| 112 | return switch (err) { | |
| 113 | windows.ERROR.SHARING_VIOLATION => OpenError.SharingViolation, | |
| 114 | windows.ERROR.ALREADY_EXISTS, windows.ERROR.FILE_EXISTS => OpenError.PathAlreadyExists, | |
| 115 | windows.ERROR.FILE_NOT_FOUND => OpenError.FileNotFound, | |
| 116 | windows.ERROR.ACCESS_DENIED => OpenError.AccessDenied, | |
| 117 | windows.ERROR.PIPE_BUSY => OpenError.PipeBusy, | |
| 118 | else => os.unexpectedErrorWindows(err), | |
| 119 | }; | |
| 134 | switch (err) { | |
| 135 | windows.ERROR.SHARING_VIOLATION => return OpenError.SharingViolation, | |
| 136 | windows.ERROR.ALREADY_EXISTS => return OpenError.PathAlreadyExists, | |
| 137 | windows.ERROR.FILE_EXISTS => return OpenError.PathAlreadyExists, | |
| 138 | windows.ERROR.FILE_NOT_FOUND => return OpenError.FileNotFound, | |
| 139 | windows.ERROR.PATH_NOT_FOUND => return OpenError.FileNotFound, | |
| 140 | windows.ERROR.ACCESS_DENIED => return OpenError.AccessDenied, | |
| 141 | windows.ERROR.PIPE_BUSY => return OpenError.PipeBusy, | |
| 142 | else => return os.unexpectedErrorWindows(err), | |
| 143 | } | |
| 120 | 144 | } |
| 121 | 145 | |
| 122 | 146 | return result; |
| ... | ... | @@ -192,9 +216,8 @@ pub fn windowsFindFirstFile( |
| 192 | 216 | if (handle == windows.INVALID_HANDLE_VALUE) { |
| 193 | 217 | const err = windows.GetLastError(); |
| 194 | 218 | switch (err) { |
| 195 | windows.ERROR.FILE_NOT_FOUND, | |
| 196 | windows.ERROR.PATH_NOT_FOUND, | |
| 197 | => return error.PathNotFound, | |
| 219 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 220 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 198 | 221 | else => return os.unexpectedErrorWindows(err), |
| 199 | 222 | } |
| 200 | 223 | } |
| ... | ... | @@ -238,7 +261,7 @@ pub fn windowsPostQueuedCompletionStatus(completion_port: windows.HANDLE, bytes_ |
| 238 | 261 | } |
| 239 | 262 | } |
| 240 | 263 | |
| 241 | pub const WindowsWaitResult = enum{ | |
| 264 | pub const WindowsWaitResult = enum { | |
| 242 | 265 | Normal, |
| 243 | 266 | Aborted, |
| 244 | 267 | Cancelled, |
| ... | ... | @@ -254,8 +277,39 @@ pub fn windowsGetQueuedCompletionStatus(completion_port: windows.HANDLE, bytes_t |
| 254 | 277 | if (std.debug.runtime_safety) { |
| 255 | 278 | std.debug.panic("unexpected error: {}\n", err); |
| 256 | 279 | } |
| 257 | } | |
| 280 | }, | |
| 258 | 281 | } |
| 259 | 282 | } |
| 260 | 283 | return WindowsWaitResult.Normal; |
| 261 | 284 | } |
| 285 | ||
| 286 | pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { | |
| 287 | return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); | |
| 288 | } | |
| 289 | ||
| 290 | pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE + 1]u16 { | |
| 291 | // TODO well defined copy elision | |
| 292 | var result: [PATH_MAX_WIDE + 1]u16 = undefined; | |
| 293 | ||
| 294 | // > File I/O functions in the Windows API convert "/" to "\" as part of | |
| 295 | // > converting the name to an NT-style name, except when using the "\\?\" | |
| 296 | // > prefix as detailed in the following sections. | |
| 297 | // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation | |
| 298 | // Because we want the larger maximum path length for absolute paths, we | |
| 299 | // disallow forward slashes in zig std lib file functions on Windows. | |
| 300 | for (s) |byte| | |
| 301 | switch (byte) { | |
| 302 | '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName, | |
| 303 | else => {}, | |
| 304 | }; | |
| 305 | const start_index = if (mem.startsWith(u8, s, "\\\\") or !os.path.isAbsolute(s)) 0 else blk: { | |
| 306 | const prefix = []u16{ '\\', '\\', '?', '\\' }; | |
| 307 | mem.copy(u16, result[0..], prefix); | |
| 308 | break :blk prefix.len; | |
| 309 | }; | |
| 310 | const end_index = start_index + try std.unicode.utf8ToUtf16Le(result[start_index..], s); | |
| 311 | assert(end_index <= result.len); | |
| 312 | if (end_index == result.len) return error.NameTooLong; | |
| 313 | result[end_index] = 0; | |
| 314 | return result; | |
| 315 | } |
std/unicode.zig+71-31| ... | ... | @@ -218,7 +218,6 @@ const Utf8Iterator = struct { |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | const cp_len = utf8ByteSequenceLength(it.bytes[it.i]) catch unreachable; |
| 221 | ||
| 222 | 221 | it.i += cp_len; |
| 223 | 222 | return it.bytes[it.i - cp_len .. it.i]; |
| 224 | 223 | } |
| ... | ... | @@ -236,6 +235,38 @@ const Utf8Iterator = struct { |
| 236 | 235 | } |
| 237 | 236 | }; |
| 238 | 237 | |
| 238 | pub const Utf16LeIterator = struct { | |
| 239 | bytes: []const u8, | |
| 240 | i: usize, | |
| 241 | ||
| 242 | pub fn init(s: []const u16) Utf16LeIterator { | |
| 243 | return Utf16LeIterator{ | |
| 244 | .bytes = @sliceToBytes(s), | |
| 245 | .i = 0, | |
| 246 | }; | |
| 247 | } | |
| 248 | ||
| 249 | pub fn nextCodepoint(it: *Utf16LeIterator) !?u32 { | |
| 250 | assert(it.i <= it.bytes.len); | |
| 251 | if (it.i == it.bytes.len) return null; | |
| 252 | const c0: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]); | |
| 253 | if (c0 & ~u32(0x03ff) == 0xd800) { | |
| 254 | // surrogate pair | |
| 255 | it.i += 2; | |
| 256 | if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf; | |
| 257 | const c1: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]); | |
| 258 | if (c1 & ~u32(0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf; | |
| 259 | it.i += 2; | |
| 260 | return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff)); | |
| 261 | } else if (c0 & ~u32(0x03ff) == 0xdc00) { | |
| 262 | return error.UnexpectedSecondSurrogateHalf; | |
| 263 | } else { | |
| 264 | it.i += 2; | |
| 265 | return c0; | |
| 266 | } | |
| 267 | } | |
| 268 | }; | |
| 269 | ||
| 239 | 270 | test "utf8 encode" { |
| 240 | 271 | comptime testUtf8Encode() catch unreachable; |
| 241 | 272 | try testUtf8Encode(); |
| ... | ... | @@ -446,42 +477,34 @@ fn testDecode(bytes: []const u8) !u32 { |
| 446 | 477 | return utf8Decode(bytes); |
| 447 | 478 | } |
| 448 | 479 | |
| 449 | // TODO: make this API on top of a non-allocating Utf16LeView | |
| 450 | pub fn utf16leToUtf8(allocator: *mem.Allocator, utf16le: []const u16) ![]u8 { | |
| 480 | /// Caller must free returned memory. | |
| 481 | pub fn utf16leToUtf8Alloc(allocator: *mem.Allocator, utf16le: []const u16) ![]u8 { | |
| 451 | 482 | var result = std.ArrayList(u8).init(allocator); |
| 452 | 483 | // optimistically guess that it will all be ascii. |
| 453 | 484 | try result.ensureCapacity(utf16le.len); |
| 454 | ||
| 455 | const utf16le_as_bytes = @sliceToBytes(utf16le); | |
| 456 | var i: usize = 0; | |
| 457 | 485 | var out_index: usize = 0; |
| 458 | while (i < utf16le_as_bytes.len) : (i += 2) { | |
| 459 | // decode | |
| 460 | const c0: u32 = mem.readIntLE(u16, utf16le_as_bytes[i..i + 2]); | |
| 461 | var codepoint: u32 = undefined; | |
| 462 | if (c0 & ~u32(0x03ff) == 0xd800) { | |
| 463 | // surrogate pair | |
| 464 | i += 2; | |
| 465 | if (i >= utf16le_as_bytes.len) return error.DanglingSurrogateHalf; | |
| 466 | const c1: u32 = mem.readIntLE(u16, utf16le_as_bytes[i..i + 2]); | |
| 467 | if (c1 & ~u32(0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf; | |
| 468 | codepoint = 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff)); | |
| 469 | } else if (c0 & ~u32(0x03ff) == 0xdc00) { | |
| 470 | return error.UnexpectedSecondSurrogateHalf; | |
| 471 | } else { | |
| 472 | codepoint = c0; | |
| 473 | } | |
| 474 | ||
| 475 | // encode | |
| 486 | var it = Utf16LeIterator.init(utf16le); | |
| 487 | while (try it.nextCodepoint()) |codepoint| { | |
| 476 | 488 | const utf8_len = utf8CodepointSequenceLength(codepoint) catch unreachable; |
| 477 | 489 | try result.resize(result.len + utf8_len); |
| 478 | _ = utf8Encode(codepoint, result.items[out_index..]) catch unreachable; | |
| 490 | assert((utf8Encode(codepoint, result.items[out_index..]) catch unreachable) == utf8_len); | |
| 479 | 491 | out_index += utf8_len; |
| 480 | 492 | } |
| 481 | 493 | |
| 482 | 494 | return result.toOwnedSlice(); |
| 483 | 495 | } |
| 484 | 496 | |
| 497 | /// Asserts that the output buffer is big enough. | |
| 498 | /// Returns end byte index into utf8. | |
| 499 | pub fn utf16leToUtf8(utf8: []u8, utf16le: []const u16) !usize { | |
| 500 | var end_index: usize = 0; | |
| 501 | var it = Utf16LeIterator.init(utf16le); | |
| 502 | while (try it.nextCodepoint()) |codepoint| { | |
| 503 | end_index += try utf8Encode(codepoint, utf8[end_index..]); | |
| 504 | } | |
| 505 | return end_index; | |
| 506 | } | |
| 507 | ||
| 485 | 508 | test "utf16leToUtf8" { |
| 486 | 509 | var utf16le: [2]u16 = undefined; |
| 487 | 510 | const utf16le_as_bytes = @sliceToBytes(utf16le[0..]); |
| ... | ... | @@ -489,14 +512,14 @@ test "utf16leToUtf8" { |
| 489 | 512 | { |
| 490 | 513 | mem.writeInt(utf16le_as_bytes[0..], u16('A'), builtin.Endian.Little); |
| 491 | 514 | mem.writeInt(utf16le_as_bytes[2..], u16('a'), builtin.Endian.Little); |
| 492 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 515 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 493 | 516 | assert(mem.eql(u8, utf8, "Aa")); |
| 494 | 517 | } |
| 495 | 518 | |
| 496 | 519 | { |
| 497 | 520 | mem.writeInt(utf16le_as_bytes[0..], u16(0x80), builtin.Endian.Little); |
| 498 | 521 | mem.writeInt(utf16le_as_bytes[2..], u16(0xffff), builtin.Endian.Little); |
| 499 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 522 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 500 | 523 | assert(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf")); |
| 501 | 524 | } |
| 502 | 525 | |
| ... | ... | @@ -504,7 +527,7 @@ test "utf16leToUtf8" { |
| 504 | 527 | // the values just outside the surrogate half range |
| 505 | 528 | mem.writeInt(utf16le_as_bytes[0..], u16(0xd7ff), builtin.Endian.Little); |
| 506 | 529 | mem.writeInt(utf16le_as_bytes[2..], u16(0xe000), builtin.Endian.Little); |
| 507 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 530 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 508 | 531 | assert(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80")); |
| 509 | 532 | } |
| 510 | 533 | |
| ... | ... | @@ -512,7 +535,7 @@ test "utf16leToUtf8" { |
| 512 | 535 | // smallest surrogate pair |
| 513 | 536 | mem.writeInt(utf16le_as_bytes[0..], u16(0xd800), builtin.Endian.Little); |
| 514 | 537 | mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little); |
| 515 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 538 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 516 | 539 | assert(mem.eql(u8, utf8, "\xf0\x90\x80\x80")); |
| 517 | 540 | } |
| 518 | 541 | |
| ... | ... | @@ -520,14 +543,14 @@ test "utf16leToUtf8" { |
| 520 | 543 | // largest surrogate pair |
| 521 | 544 | mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little); |
| 522 | 545 | mem.writeInt(utf16le_as_bytes[2..], u16(0xdfff), builtin.Endian.Little); |
| 523 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 546 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 524 | 547 | assert(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf")); |
| 525 | 548 | } |
| 526 | 549 | |
| 527 | 550 | { |
| 528 | 551 | mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little); |
| 529 | 552 | mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little); |
| 530 | const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le); | |
| 553 | const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); | |
| 531 | 554 | assert(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); |
| 532 | 555 | } |
| 533 | 556 | } |
| ... | ... | @@ -548,3 +571,20 @@ pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![]u16 |
| 548 | 571 | try result.append(0); |
| 549 | 572 | return result.toOwnedSlice(); |
| 550 | 573 | } |
| 574 | ||
| 575 | /// Returns index of next character. If exact fit, returned index equals output slice length. | |
| 576 | /// If ran out of room, returned index equals output slice length + 1. | |
| 577 | /// TODO support codepoints bigger than 16 bits | |
| 578 | pub fn utf8ToUtf16Le(utf16le: []u16, utf8: []const u8) !usize { | |
| 579 | const utf16le_as_bytes = @sliceToBytes(utf16le[0..]); | |
| 580 | var end_index: usize = 0; | |
| 581 | ||
| 582 | var it = (try Utf8View.init(utf8)).iterator(); | |
| 583 | while (it.nextCodepoint()) |codepoint| { | |
| 584 | if (end_index == utf16le_as_bytes.len) return (end_index / 2) + 1; | |
| 585 | // TODO surrogate pairs | |
| 586 | mem.writeInt(utf16le_as_bytes[end_index..], @intCast(u16, codepoint), builtin.Endian.Little); | |
| 587 | end_index += 2; | |
| 588 | } | |
| 589 | return end_index / 2; | |
| 590 | } |
test/cases/merge_error_sets.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const A = error{ |
| 2 | PathNotFound, | |
| 2 | FileNotFound, | |
| 3 | 3 | NotDir, |
| 4 | 4 | }; |
| 5 | 5 | const B = error{OutOfMemory}; |
| ... | ... | @@ -15,7 +15,7 @@ test "merge error sets" { |
| 15 | 15 | @panic("unexpected"); |
| 16 | 16 | } else |err| switch (err) { |
| 17 | 17 | error.OutOfMemory => @panic("unexpected"), |
| 18 | error.PathNotFound => @panic("unexpected"), | |
| 18 | error.FileNotFound => @panic("unexpected"), | |
| 19 | 19 | error.NotDir => {}, |
| 20 | 20 | } |
| 21 | 21 | } |