structstd.fs.File.MetadataUnix[src]

Fields

stat: posix.Stat

Functions

Functionsize[src]

pub fn size(self: Self) u64

Returns the size of the file

Parameters

self: Self

Source Code

Source code
pub fn size(self: Self) u64 {
    return @intCast(self.stat.size);
}

Functionpermissions[src]

pub fn permissions(self: Self) Permissions

Returns a Permissions struct, representing the permissions on the file

Parameters

self: Self

Source Code

Source code
pub fn permissions(self: Self) Permissions {
    return .{ .inner = .{ .mode = self.stat.mode } };
}

Functionkind[src]

pub fn kind(self: Self) Kind

Returns the Kind of the file

Parameters

self: Self

Source Code

Source code
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;
}

Functionaccessed[src]

pub fn accessed(self: Self) i128

Returns the last time the file was accessed in nanoseconds since UTC 1970-01-01

Parameters

self: Self

Source Code

Source code
pub fn accessed(self: Self) i128 {
    const atime = self.stat.atime();
    return @as(i128, atime.sec) * std.time.ns_per_s + atime.nsec;
}

Functionmodified[src]

pub fn modified(self: Self) i128

Returns the last time the file was modified in nanoseconds since UTC 1970-01-01

Parameters

self: Self

Source Code

Source code
pub fn modified(self: Self) i128 {
    const mtime = self.stat.mtime();
    return @as(i128, mtime.sec) * std.time.ns_per_s + mtime.nsec;
}

Functioncreated[src]

pub fn created(self: Self) ?i128

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

Parameters

self: Self

Source Code

Source code
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;
}

Source Code

Source code
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;
    }
}