Use std.c.fstatat() instead of @cImport()ed version

Because translate-c can't handle struct stat as defined by musl.
(Should have done this in the first place, but wasn't aware fstatat()
had been properly wrapped in std.c)
This commit is contained in:
Yorhel 2025-04-28 13:11:54 +02:00
parent d97a7f73dd
commit beac59fb12
2 changed files with 21 additions and 23 deletions

View file

@ -10,9 +10,6 @@ pub const c = @cImport({
@cInclude("locale.h"); // setlocale() and localeconv() @cInclude("locale.h"); // setlocale() and localeconv()
@cInclude("fnmatch.h"); // fnmatch() @cInclude("fnmatch.h"); // fnmatch()
@cInclude("unistd.h"); // getuid() @cInclude("unistd.h"); // getuid()
@cInclude("errno.h"); // error codes
@cInclude("fcntl.h"); // AT_SYMLINK_NOFOLLOW
@cInclude("sys/stat.h"); // fstatat()
@cInclude("sys/types.h"); // struct passwd @cInclude("sys/types.h"); // struct passwd
@cInclude("pwd.h"); // getpwnam(), getpwuid() @cInclude("pwd.h"); // getpwnam(), getpwuid()
if (@import("builtin").os.tag == .linux) { if (@import("builtin").os.tag == .linux) {

View file

@ -47,28 +47,29 @@ fn truncate(comptime T: type, comptime field: anytype, x: anytype) std.meta.fiel
fn statAt(parent: std.fs.Dir, name: [:0]const u8, follow: bool, symlink: *bool) !sink.Stat { fn statAt(parent: std.fs.Dir, name: [:0]const u8, follow: bool, symlink: *bool) !sink.Stat {
// std.posix.fstatatZ() is currently unusable due to https://github.com/ziglang/zig/issues/23463 // std.posix.fstatatZ() in Zig 0.14 is not suitable due to https://github.com/ziglang/zig/issues/23463
var stat: c.struct_stat = undefined; var stat: std.c.Stat = undefined;
if (c.fstatat(parent.fd, name, &stat, if (follow) 0 else c.AT_SYMLINK_NOFOLLOW) != 0) if (std.c.fstatat(parent.fd, name, &stat, if (follow) 0 else std.c.AT.SYMLINK_NOFOLLOW) != 0) {
return switch (std.c._errno().*) { return switch (std.c._errno().*) {
c.ENOENT => error.FileNotFound, @intFromEnum(std.c.E.NOENT) => error.FileNotFound,
c.ENAMETOOLONG => error.NameTooLong, @intFromEnum(std.c.E.NAMETOOLONG) => error.NameTooLong,
c.ENOMEM => error.OutOfMemory, @intFromEnum(std.c.E.NOMEM) => error.OutOfMemory,
c.EACCES => error.AccessDenied, @intFromEnum(std.c.E.ACCES) => error.AccessDenied,
else => error.Unexpected, else => error.Unexpected,
}; };
symlink.* = c.S_ISLNK(stat.st_mode); }
symlink.* = std.c.S.ISLNK(stat.mode);
return sink.Stat{ return sink.Stat{
.etype = .etype =
if (c.S_ISDIR(stat.st_mode)) .dir if (std.c.S.ISDIR(stat.mode)) .dir
else if (stat.st_nlink > 1) .link else if (stat.nlink > 1) .link
else if (!c.S_ISREG(stat.st_mode)) .nonreg else if (!std.c.S.ISREG(stat.mode)) .nonreg
else .reg, else .reg,
.blocks = clamp(sink.Stat, .blocks, stat.st_blocks), .blocks = clamp(sink.Stat, .blocks, stat.blocks),
.size = clamp(sink.Stat, .size, stat.st_size), .size = clamp(sink.Stat, .size, stat.size),
.dev = truncate(sink.Stat, .dev, stat.st_dev), .dev = truncate(sink.Stat, .dev, stat.dev),
.ino = truncate(sink.Stat, .ino, stat.st_ino), .ino = truncate(sink.Stat, .ino, stat.ino),
.nlink = clamp(sink.Stat, .nlink, stat.st_nlink), .nlink = clamp(sink.Stat, .nlink, stat.nlink),
.ext = .{ .ext = .{
.pack = .{ .pack = .{
.hasmtime = true, .hasmtime = true,
@ -76,10 +77,10 @@ fn statAt(parent: std.fs.Dir, name: [:0]const u8, follow: bool, symlink: *bool)
.hasgid = true, .hasgid = true,
.hasmode = true, .hasmode = true,
}, },
.mtime = clamp(model.Ext, .mtime, stat.st_mtim.tv_sec), .mtime = clamp(model.Ext, .mtime, stat.mtim.sec),
.uid = truncate(model.Ext, .uid, stat.st_uid), .uid = truncate(model.Ext, .uid, stat.uid),
.gid = truncate(model.Ext, .gid, stat.st_gid), .gid = truncate(model.Ext, .gid, stat.gid),
.mode = truncate(model.Ext, .mode, stat.st_mode), .mode = truncate(model.Ext, .mode, stat.mode),
}, },
}; };
} }