From f46008c1d8bce5dd8d1d0155fc3bd1c865870c79 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Thu, 31 Aug 2023 14:05:43 -0700 Subject: [PATCH 1/2] Fix `Dir.statFile` for WASI when linking libc `statFile` now only uses `os.fstatatWasi` when not linking libc, matching the pattern used throughout other `Dir` functions. This fixes the compilation error: `error: struct 'c.wasi.Stat' has no member named 'fromFilestat'` (which the added test would have failed with) --- lib/std/fs.zig | 24 ++++++++++-------------- lib/std/fs/test.zig | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 4a0f37b855..6e91f3d591 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -2653,21 +2653,17 @@ pub const Dir = struct { /// /// `sub_path` may be absolute, in which case `self` is ignored. pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat { - switch (builtin.os.tag) { - .windows => { - var file = try self.openFile(sub_path, .{}); - defer file.close(); - return file.stat(); - }, - .wasi => { - const st = try os.fstatatWasi(self.fd, sub_path, os.wasi.LOOKUP_SYMLINK_FOLLOW); - return Stat.fromSystem(st); - }, - else => { - const st = try os.fstatat(self.fd, sub_path, 0); - return Stat.fromSystem(st); - }, + if (builtin.os.tag == .windows) { + var file = try self.openFile(sub_path, .{}); + defer file.close(); + return file.stat(); } + if (builtin.os.tag == .wasi and !builtin.link_libc) { + const st = try os.fstatatWasi(self.fd, sub_path, os.wasi.LOOKUP_SYMLINK_FOLLOW); + return Stat.fromSystem(st); + } + const st = try os.fstatat(self.fd, sub_path, 0); + return Stat.fromSystem(st); } const Permissions = File.Permissions; diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index ade3fc55ab..4b86bfd668 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -569,6 +569,21 @@ test "readAllAlloc" { try testing.expectError(error.FileTooBig, file.readToEndAlloc(testing.allocator, write_buf.len - 1)); } +test "Dir.statFile" { + try testWithAllSupportedPathTypes(struct { + fn impl(ctx: *TestContext) !void { + const test_file_name = try ctx.transformPath("test_file"); + + try testing.expectError(error.FileNotFound, ctx.dir.statFile(test_file_name)); + + try ctx.dir.writeFile(test_file_name, ""); + + const stat = try ctx.dir.statFile(test_file_name); + try testing.expectEqual(File.Kind.file, stat.kind); + } + }.impl); +} + test "directory operations on files" { try testWithAllSupportedPathTypes(struct { fn impl(ctx: *TestContext) !void { From 01f9cdd21a8f18941636c061a5985a040e4a4bcd Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Thu, 31 Aug 2023 18:01:15 -0700 Subject: [PATCH 2/2] Temporarily disable Dir.statFile test when linking glibc See https://github.com/ziglang/zig/issues/17034 --- lib/std/fs/test.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 4b86bfd668..2c3de6b405 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -570,6 +570,9 @@ test "readAllAlloc" { } test "Dir.statFile" { + // TODO: Re-enable once https://github.com/ziglang/zig/issues/17034 is solved + if (builtin.os.tag == .linux and builtin.link_libc and builtin.abi == .gnu) return error.SkipZigTest; + try testWithAllSupportedPathTypes(struct { fn impl(ctx: *TestContext) !void { const test_file_name = try ctx.transformPath("test_file");