authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-12-20 20:18:07-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:11-08:00
loge442a0ecc23d315f99f62684c6240e7c1cb6e9ce
tree2e034d8953310f01d219f7f65e6cc2db3eea73f8
parentad08117e9daf5d2a34be1ca71a23366f39079990

Dir.readFile/readFileAlloc: take advantage of `.allow_directory = false` on Windows

Since we know the read will fail for directories, we can take advantage of Windows being able to fail with IsDir during open to avoid needing to wait until the read to find out about the directory-ness of the file.

1 files changed, 10 insertions(+), 2 deletions(-)

lib/std/Io/Dir.zig+10-2
......@@ -633,7 +633,11 @@ pub const ReadFileError = File.OpenError || File.Reader.Error;
633633/// * On WASI, `file_path` should be encoded as valid UTF-8.
634634/// * On other platforms, `file_path` is an opaque sequence of bytes with no particular encoding.
635635pub fn readFile(dir: Dir, io: Io, file_path: []const u8, buffer: []u8) ReadFileError![]u8 {
636 var file = try dir.openFile(io, file_path, .{});
636 var file = try dir.openFile(io, file_path, .{
637 // We can take advantage of this on Windows since it doesn't involve any extra syscalls,
638 // so we can get error.IsDir during open rather than during the read.
639 .allow_directory = if (native_os == .windows) false else true,
640 });
637641 defer file.close(io);
638642
639643 var reader = file.reader(io, &.{});
......@@ -1217,7 +1221,11 @@ pub fn readFileAllocOptions(
12171221 comptime alignment: std.mem.Alignment,
12181222 comptime sentinel: ?u8,
12191223) ReadFileAllocError!(if (sentinel) |s| [:s]align(alignment.toByteUnits()) u8 else []align(alignment.toByteUnits()) u8) {
1220 var file = try dir.openFile(io, sub_path, .{});
1224 var file = try dir.openFile(io, sub_path, .{
1225 // We can take advantage of this on Windows since it doesn't involve any extra syscalls,
1226 // so we can get error.IsDir during open rather than during the read.
1227 .allow_directory = if (native_os == .windows) false else true,
1228 });
12211229 defer file.close(io);
12221230 var file_reader = file.reader(io, &.{});
12231231 return file_reader.interface.allocRemainingAlignedSentinel(gpa, limit, alignment, sentinel) catch |err| switch (err) {