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;...@@ -633,7 +633,11 @@ pub const ReadFileError = File.OpenError || File.Reader.Error;
633/// * On WASI, `file_path` should be encoded as valid UTF-8.633/// * On WASI, `file_path` should be encoded as valid UTF-8.
634/// * On other platforms, `file_path` is an opaque sequence of bytes with no particular encoding.634/// * On other platforms, `file_path` is an opaque sequence of bytes with no particular encoding.
635pub fn readFile(dir: Dir, io: Io, file_path: []const u8, buffer: []u8) ReadFileError![]u8 {635pub 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 });
637 defer file.close(io);641 defer file.close(io);
638642
639 var reader = file.reader(io, &.{});643 var reader = file.reader(io, &.{});
...@@ -1217,7 +1221,11 @@ pub fn readFileAllocOptions(...@@ -1217,7 +1221,11 @@ pub fn readFileAllocOptions(
1217 comptime alignment: std.mem.Alignment,1221 comptime alignment: std.mem.Alignment,
1218 comptime sentinel: ?u8,1222 comptime sentinel: ?u8,
1219) ReadFileAllocError!(if (sentinel) |s| [:s]align(alignment.toByteUnits()) u8 else []align(alignment.toByteUnits()) u8) {1223) 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 });
1221 defer file.close(io);1229 defer file.close(io);
1222 var file_reader = file.reader(io, &.{});1230 var file_reader = file.reader(io, &.{});
1223 return file_reader.interface.allocRemainingAlignedSentinel(gpa, limit, alignment, sentinel) catch |err| switch (err) {1231 return file_reader.interface.allocRemainingAlignedSentinel(gpa, limit, alignment, sentinel) catch |err| switch (err) {