struct MetadataUnix [src]
Fields
stat: posix.Stat
Members
Source
pub const MetadataUnix = struct {
stat: posix.Stat,
const Self = @This();
/// Returns the size of the file
pub fn size(self: Self) u64 {
return @intCast(self.stat.size);
}
/// Returns a `Permissions` struct, representing the permissions on the file
pub fn permissions(self: Self) Permissions {
return .{ .inner = .{ .mode = self.stat.mode } };
}
/// Returns the `Kind` of the file
pub fn kind(self: Self) Kind {
if (builtin.os.tag == .wasi and !builtin.link_libc) return switch (self.stat.filetype) {
.BLOCK_DEVICE => .block_device,
.CHARACTER_DEVICE => .character_device,
.DIRECTORY => .directory,
.SYMBOLIC_LINK => .sym_link,
.REGULAR_FILE => .file,
.SOCKET_STREAM, .SOCKET_DGRAM => .unix_domain_socket,
else => .unknown,
};
const m = self.stat.mode & posix.S.IFMT;
switch (m) {
posix.S.IFBLK => return .block_device,
posix.S.IFCHR => return .character_device,
posix.S.IFDIR => return .directory,
posix.S.IFIFO => return .named_pipe,
posix.S.IFLNK => return .sym_link,
posix.S.IFREG => return .file,
posix.S.IFSOCK => return .unix_domain_socket,
else => {},
}
if (builtin.os.tag.isSolarish()) switch (m) {
posix.S.IFDOOR => return .door,
posix.S.IFPORT => return .event_port,
else => {},
};
return .unknown;
}
/// Returns the last time the file was accessed in nanoseconds since UTC 1970-01-01
pub fn accessed(self: Self) i128 {
const atime = self.stat.atime();
return @as(i128, atime.sec) * std.time.ns_per_s + atime.nsec;
}
/// Returns the last time the file was modified in nanoseconds since UTC 1970-01-01
pub fn modified(self: Self) i128 {
const mtime = self.stat.mtime();
return @as(i128, mtime.sec) * std.time.ns_per_s + mtime.nsec;
}
/// Returns the time the file was created in nanoseconds since UTC 1970-01-01.
/// Returns null if this is not supported by the OS or filesystem
pub fn created(self: Self) ?i128 {
if (!@hasDecl(@TypeOf(self.stat), "birthtime")) return null;
const birthtime = self.stat.birthtime();
// If the filesystem doesn't support this the value *should* be:
// On FreeBSD: nsec = 0, sec = -1
// On NetBSD and OpenBSD: nsec = 0, sec = 0
// On MacOS, it is set to ctime -- we cannot detect this!!
switch (builtin.os.tag) {
.freebsd => if (birthtime.sec == -1 and birthtime.nsec == 0) return null,
.netbsd, .openbsd => if (birthtime.sec == 0 and birthtime.nsec == 0) return null,
.macos => {},
else => @compileError("Creation time detection not implemented for OS"),
}
return @as(i128, birthtime.sec) * std.time.ns_per_s + birthtime.nsec;
}
}