mirror of
https://code.blicky.net/yorhel/ncdu.git
synced 2026-01-12 17:08:39 -09:00
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:
parent
d97a7f73dd
commit
beac59fb12
2 changed files with 21 additions and 23 deletions
|
|
@ -10,9 +10,6 @@ pub const c = @cImport({
|
|||
@cInclude("locale.h"); // setlocale() and localeconv()
|
||||
@cInclude("fnmatch.h"); // fnmatch()
|
||||
@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("pwd.h"); // getpwnam(), getpwuid()
|
||||
if (@import("builtin").os.tag == .linux) {
|
||||
|
|
|
|||
41
src/scan.zig
41
src/scan.zig
|
|
@ -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 {
|
||||
// std.posix.fstatatZ() is currently unusable due to https://github.com/ziglang/zig/issues/23463
|
||||
var stat: c.struct_stat = undefined;
|
||||
if (c.fstatat(parent.fd, name, &stat, if (follow) 0 else c.AT_SYMLINK_NOFOLLOW) != 0)
|
||||
// std.posix.fstatatZ() in Zig 0.14 is not suitable due to https://github.com/ziglang/zig/issues/23463
|
||||
var stat: std.c.Stat = undefined;
|
||||
if (std.c.fstatat(parent.fd, name, &stat, if (follow) 0 else std.c.AT.SYMLINK_NOFOLLOW) != 0) {
|
||||
return switch (std.c._errno().*) {
|
||||
c.ENOENT => error.FileNotFound,
|
||||
c.ENAMETOOLONG => error.NameTooLong,
|
||||
c.ENOMEM => error.OutOfMemory,
|
||||
c.EACCES => error.AccessDenied,
|
||||
@intFromEnum(std.c.E.NOENT) => error.FileNotFound,
|
||||
@intFromEnum(std.c.E.NAMETOOLONG) => error.NameTooLong,
|
||||
@intFromEnum(std.c.E.NOMEM) => error.OutOfMemory,
|
||||
@intFromEnum(std.c.E.ACCES) => error.AccessDenied,
|
||||
else => error.Unexpected,
|
||||
};
|
||||
symlink.* = c.S_ISLNK(stat.st_mode);
|
||||
}
|
||||
symlink.* = std.c.S.ISLNK(stat.mode);
|
||||
return sink.Stat{
|
||||
.etype =
|
||||
if (c.S_ISDIR(stat.st_mode)) .dir
|
||||
else if (stat.st_nlink > 1) .link
|
||||
else if (!c.S_ISREG(stat.st_mode)) .nonreg
|
||||
if (std.c.S.ISDIR(stat.mode)) .dir
|
||||
else if (stat.nlink > 1) .link
|
||||
else if (!std.c.S.ISREG(stat.mode)) .nonreg
|
||||
else .reg,
|
||||
.blocks = clamp(sink.Stat, .blocks, stat.st_blocks),
|
||||
.size = clamp(sink.Stat, .size, stat.st_size),
|
||||
.dev = truncate(sink.Stat, .dev, stat.st_dev),
|
||||
.ino = truncate(sink.Stat, .ino, stat.st_ino),
|
||||
.nlink = clamp(sink.Stat, .nlink, stat.st_nlink),
|
||||
.blocks = clamp(sink.Stat, .blocks, stat.blocks),
|
||||
.size = clamp(sink.Stat, .size, stat.size),
|
||||
.dev = truncate(sink.Stat, .dev, stat.dev),
|
||||
.ino = truncate(sink.Stat, .ino, stat.ino),
|
||||
.nlink = clamp(sink.Stat, .nlink, stat.nlink),
|
||||
.ext = .{
|
||||
.pack = .{
|
||||
.hasmtime = true,
|
||||
|
|
@ -76,10 +77,10 @@ fn statAt(parent: std.fs.Dir, name: [:0]const u8, follow: bool, symlink: *bool)
|
|||
.hasgid = true,
|
||||
.hasmode = true,
|
||||
},
|
||||
.mtime = clamp(model.Ext, .mtime, stat.st_mtim.tv_sec),
|
||||
.uid = truncate(model.Ext, .uid, stat.st_uid),
|
||||
.gid = truncate(model.Ext, .gid, stat.st_gid),
|
||||
.mode = truncate(model.Ext, .mode, stat.st_mode),
|
||||
.mtime = clamp(model.Ext, .mtime, stat.mtim.sec),
|
||||
.uid = truncate(model.Ext, .uid, stat.uid),
|
||||
.gid = truncate(model.Ext, .gid, stat.gid),
|
||||
.mode = truncate(model.Ext, .mode, stat.mode),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue