File stat in zig 0.16-dev

how do I get the uid and gid of a file? I’ve searched through the official documentation but still don’t quite understand how to do it.

You have to drop to platform specific APIs, Io doesn’t provide it because that information isn’t available on all supported platforms.

I think?? it might change in the future

Since std.posix has been nuked, you have to go even lower to std.os.your_os_here.

Assuming you’re on linux:

const linux = std.os.linux;

var statx: linux.Statx = undefined;
// there is a `BASIC_STATS` decl which includes uid, gid and other common stats
const errno = linux.errno(linux.statx(io_file.handle, "", linux.AT.EMPTY_PATH, .{ .GID = true, .UID = true }, &statx));
switch (errno) {
    .SUCCESS => {},
    .ACCES => {},
    .BADF => {},
    .FAULT => {},
    .INVAL => {},
    .LOOP => {},
    .NAMETOOLONG => {},
    .NOENT => {},
    .NOMEM => {},
    .NOTDIR => {},
    // no other errors are possible
    else => unreachable,
}
// only these are filled due to the mask passed
const uid, const gid = .{ statx.uid, statx.gid };
4 Likes

My guess is that you’ll need to go through std.os.linux.statx() (e.g. Zig Documentation which populates a StatX struct: Zig Documentation, and this has uid and gid).

It’s probably not part of the generic std.Io interface because uid and gid are not portable.

3 Likes

In case you need to get and set, there is nowstd.Io.File.Permissions. You have setPermissions() function as well.

Hi, @vulpesx @floooh .

I used std.os.linux.statx and it solved the problem; I can now correctly retrieve the required data on Linux.

On macOS, I use std.c.fstat.

Thank~