Skip to content

Commit ea1b21d

Browse files
committed
fix linux
* error.BadFd is not a valid error code. it would always be a bug to get this error code. * merge error.Io with existing error.InputOutput * merge error.PathNotFound with existing error.FileNotFound. Not all OS's support both. * add os.File.openReadC * add error.BadPathName for windows file operations with invalid characters * add os.toPosixPath to help stack allocate a null terminating byte * add some TODOs for other functions to investigate removing the allocator requirement * optimize some implementations to use the alternate functions when a null byte is already available * add a missing error.SkipZigTest * os.selfExePath uses a non-allocating API * os.selfExeDirPath uses a non-allocating API * os.path.real uses a non-allocating API * add os.path.realAlloc and os.path.realC * convert many windows syscalls to use the W versions (See #534)
1 parent 51852d2 commit ea1b21d

17 files changed

+319
-288
lines changed

doc/docgen.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ pub fn main() !void {
3434
const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg"));
3535
defer allocator.free(out_file_name);
3636

37-
var in_file = try os.File.openRead(allocator, in_file_name);
37+
var in_file = try os.File.openRead(in_file_name);
3838
defer in_file.close();
3939

40-
var out_file = try os.File.openWrite(allocator, out_file_name);
40+
var out_file = try os.File.openWrite(out_file_name);
4141
defer out_file.close();
4242

4343
var file_in_stream = io.FileInStream.init(&in_file);
@@ -738,7 +738,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
738738
try out.print("<pre><code class=\"zig\">{}</code></pre>", escaped_source);
739739
const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name);
740740
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);
742742

743743
switch (code.id) {
744744
Code.Id.Exe => |expected_outcome| {

example/cat/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn main() !void {
2020
} else if (arg[0] == '-') {
2121
return usage(exe);
2222
} else {
23-
var file = os.File.openRead(allocator, arg) catch |err| {
23+
var file = os.File.openRead(arg) catch |err| {
2424
warn("Unable to open file: {}\n", @errorName(err));
2525
return err;
2626
};

src-self-hosted/compilation.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ pub const Compilation = struct {
257257
pub const BuildError = error{
258258
OutOfMemory,
259259
EndOfStream,
260-
BadFd,
261-
Io,
262260
IsDir,
263261
Unexpected,
264262
SystemResources,
@@ -273,7 +271,6 @@ pub const Compilation = struct {
273271
NameTooLong,
274272
SystemFdQuotaExceeded,
275273
NoDevice,
276-
PathNotFound,
277274
NoSpaceLeft,
278275
NotDir,
279276
FileSystem,
@@ -962,7 +959,7 @@ pub const Compilation = struct {
962959
if (self.root_src_path) |root_src_path| {
963960
const root_scope = blk: {
964961
// TODO async/await os.path.real
965-
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| {
966963
try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err));
967964
return;
968965
};

src-self-hosted/introspect.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![
2222

2323
/// Caller must free result
2424
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);
2626
defer allocator.free(self_exe_path);
2727

2828
var cur_path: []const u8 = self_exe_path;

src-self-hosted/libc_installation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ fn fileExists(path: []const u8) !bool {
453453
if (std.os.File.access(path)) |_| {
454454
return true;
455455
} else |err| switch (err) {
456-
error.FileNotFound, error.PathNotFound, error.PermissionDenied => return false,
456+
error.FileNotFound, error.PermissionDenied => return false,
457457
else => return error.FileSystem,
458458
}
459459
}

std/build.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,11 +1491,14 @@ pub const LibExeObjStep = struct {
14911491
}
14921492

14931493
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+
));
14951498
defer builder.allocator.free(rpath_arg);
1496-
cc_args.append(rpath_arg) catch unreachable;
1499+
try cc_args.append(rpath_arg);
14971500

1498-
cc_args.append("-rdynamic") catch unreachable;
1501+
try cc_args.append("-rdynamic");
14991502
}
15001503

15011504
for (self.full_path_libs.toSliceConst()) |full_path_lib| {
@@ -1566,11 +1569,14 @@ pub const LibExeObjStep = struct {
15661569
cc_args.append("-o") catch unreachable;
15671570
cc_args.append(output_path) catch unreachable;
15681571

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+
));
15701576
defer builder.allocator.free(rpath_arg);
1571-
cc_args.append(rpath_arg) catch unreachable;
1577+
try cc_args.append(rpath_arg);
15721578

1573-
cc_args.append("-rdynamic") catch unreachable;
1579+
try cc_args.append("-rdynamic");
15741580

15751581
{
15761582
var it = self.link_libs.iterator();

std/debug/index.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address
255255
address,
256256
compile_unit_name,
257257
);
258-
if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) {
258+
if (printLineFromFile(out_stream, line_info)) {
259259
if (line_info.column == 0) {
260260
try out_stream.write("\n");
261261
} else {

std/event/fs.zig

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, o
7878
builtin.Os.macosx,
7979
builtin.Os.linux,
8080
=> 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),
8382
else => @compileError("Unsupported OS"),
8483
}
8584
}
@@ -147,7 +146,6 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off
147146
}
148147
}
149148

150-
151149
/// data - just the inner references - must live until pwritev promise completes.
152150
pub async fn pwritevPosix(loop: *Loop, fd: os.FileHandle, data: []const []const u8, offset: usize) !void {
153151
// 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:
203201
builtin.Os.macosx,
204202
builtin.Os.linux,
205203
=> 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),
208205
else => @compileError("Unsupported OS"),
209206
}
210207
}
@@ -222,7 +219,7 @@ pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, o
222219
var inner_off: usize = 0;
223220
while (true) {
224221
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);
226223
off += amt_read;
227224
inner_off += amt_read;
228225
if (inner_off == v.len) {
@@ -340,16 +337,15 @@ pub async fn openPosix(
340337
resume @handle();
341338
}
342339

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);
345341

346342
var req_node = RequestNode{
347343
.prev = null,
348344
.next = null,
349345
.data = Request{
350346
.msg = Request.Msg{
351347
.Open = Request.Msg.Open{
352-
.path = path_with_null[0..path.len],
348+
.path = path_c[0..path.len],
353349
.flags = flags,
354350
.mode = mode,
355351
.result = undefined,
@@ -408,8 +404,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os
408404
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
409405
return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable);
410406
},
411-
builtin.Os.windows,
412-
=> return os.windowsOpen(
407+
builtin.Os.windows => return os.windowsOpen(
413408
path,
414409
windows.GENERIC_WRITE,
415410
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
@@ -434,7 +429,7 @@ pub async fn openReadWrite(
434429

435430
builtin.Os.windows => return os.windowsOpen(
436431
path,
437-
windows.GENERIC_WRITE|windows.GENERIC_READ,
432+
windows.GENERIC_WRITE | windows.GENERIC_READ,
438433
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
439434
windows.OPEN_ALWAYS,
440435
windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_OVERLAPPED,
@@ -510,8 +505,7 @@ pub const CloseOperation = struct {
510505
self.loop.allocator.destroy(self);
511506
}
512507
},
513-
builtin.Os.windows,
514-
=> {
508+
builtin.Os.windows => {
515509
if (self.os_data.handle) |handle| {
516510
os.close(handle);
517511
}
@@ -529,8 +523,7 @@ pub const CloseOperation = struct {
529523
self.os_data.close_req_node.data.msg.Close.fd = handle;
530524
self.os_data.have_fd = true;
531525
},
532-
builtin.Os.windows,
533-
=> {
526+
builtin.Os.windows => {
534527
self.os_data.handle = handle;
535528
},
536529
else => @compileError("Unsupported OS"),
@@ -545,8 +538,7 @@ pub const CloseOperation = struct {
545538
=> {
546539
self.os_data.have_fd = false;
547540
},
548-
builtin.Os.windows,
549-
=> {
541+
builtin.Os.windows => {
550542
self.os_data.handle = null;
551543
},
552544
else => @compileError("Unsupported OS"),
@@ -561,8 +553,7 @@ pub const CloseOperation = struct {
561553
assert(self.os_data.have_fd);
562554
return self.os_data.close_req_node.data.msg.Close.fd;
563555
},
564-
builtin.Os.windows,
565-
=> {
556+
builtin.Os.windows => {
566557
return self.os_data.handle.?;
567558
},
568559
else => @compileError("Unsupported OS"),
@@ -582,8 +573,7 @@ pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8,
582573
builtin.Os.linux,
583574
builtin.Os.macosx,
584575
=> return await (async writeFileModeThread(loop, path, contents, mode) catch unreachable),
585-
builtin.Os.windows,
586-
=> return await (async writeFileWindows(loop, path, contents) catch unreachable),
576+
builtin.Os.windows => return await (async writeFileWindows(loop, path, contents) catch unreachable),
587577
else => @compileError("Unsupported OS"),
588578
}
589579
}
@@ -1000,7 +990,7 @@ pub fn Watch(comptime V: type) type {
1000990
const basename_utf16le_null = try std.unicode.utf8ToUtf16LeWithNull(self.channel.loop.allocator, basename);
1001991
var basename_utf16le_null_consumed = false;
1002992
defer if (!basename_utf16le_null_consumed) self.channel.loop.allocator.free(basename_utf16le_null);
1003-
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];
1004994

1005995
const dir_handle = windows.CreateFileW(
1006996
dirname_utf16le.ptr,
@@ -1014,9 +1004,8 @@ pub fn Watch(comptime V: type) type {
10141004
if (dir_handle == windows.INVALID_HANDLE_VALUE) {
10151005
const err = windows.GetLastError();
10161006
switch (err) {
1017-
windows.ERROR.FILE_NOT_FOUND,
1018-
windows.ERROR.PATH_NOT_FOUND,
1019-
=> return error.PathNotFound,
1007+
windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
1008+
windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
10201009
else => return os.unexpectedErrorWindows(err),
10211010
}
10221011
}
@@ -1102,7 +1091,10 @@ pub fn Watch(comptime V: type) type {
11021091

11031092
// TODO handle this error not in the channel but in the setup
11041093
_ = os.windowsCreateIoCompletionPort(
1105-
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,
11061098
) catch |err| {
11071099
await (async self.channel.put(err) catch unreachable);
11081100
return;
@@ -1122,10 +1114,10 @@ pub fn Watch(comptime V: type) type {
11221114
&event_buf,
11231115
@intCast(windows.DWORD, event_buf.len),
11241116
windows.FALSE, // watch subtree
1125-
windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME |
1126-
windows.FILE_NOTIFY_CHANGE_ATTRIBUTES | windows.FILE_NOTIFY_CHANGE_SIZE |
1127-
windows.FILE_NOTIFY_CHANGE_LAST_WRITE | windows.FILE_NOTIFY_CHANGE_LAST_ACCESS |
1128-
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,
11291121
null, // number of bytes transferred (unused for async)
11301122
&overlapped,
11311123
null, // completion routine - unused because we use IOCP
@@ -1152,7 +1144,7 @@ pub fn Watch(comptime V: type) type {
11521144
else => null,
11531145
};
11541146
if (emit) |id| {
1155-
const basename_utf16le = ([*]u16)(&ev.FileName)[0..ev.FileNameLength/2];
1147+
const basename_utf16le = ([*]u16)(&ev.FileName)[0 .. ev.FileNameLength / 2];
11561148
const user_value = blk: {
11571149
const held = await (async dir.table_lock.acquire() catch unreachable);
11581150
defer held.release();

std/os/child_process.zig

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,7 @@ pub const ChildProcess = struct {
349349
};
350350

351351
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;
360353
defer {
361354
if (any_ignore) os.close(dev_null_fd);
362355
}

0 commit comments

Comments
 (0)