Skip to content
CryoCryo home

metadata

import std::fs::metadata; · source

Types

Metadata

type struct Metadata {
    raw_mode:  u32;
    byte_size: u64;
    mtime_sec: i64;

    file_type(&this) -> FileType;
    is_file(&this) -> boolean;
    is_dir(&this) -> boolean;
    is_symlink(&this) -> boolean;
    len(&this) -> u64;
    mode(&this) -> u32;
    permissions(&this) -> u32;
    modified_secs(&this) -> i64;
}

A POD snapshot of one stat — freely copied, nothing to drop.

mode() is the full st_mode — type bits plus permission bits — and permissions() the permission bits alone. len() is meaningful for regular files; modified_secs() is seconds since the Unix epoch.

Properties

PropertyType
raw_modeu32
byte_sizeu64
mtime_seci64

Methods

file_type(&this) -> FileType;
is_file(&this) -> boolean;
is_dir(&this) -> boolean;
is_symlink(&this) -> boolean;
len(&this) -> u64;

Size in bytes (meaningful for regular files).

mode(&this) -> u32;

Full st_mode (type bits + permission bits). On Windows the permission bits are synthesized from the read-only attribute (see the module header), not real ACLs.

permissions(&this) -> u32;

Unix permission bits (the low 12 bits of st_mode). Synthesized on Windows; see the module header.

modified_secs(&this) -> i64;

Last-modification time in whole seconds since the Unix epoch.

FileType

type enum FileType {
    File;
    Dir;
    Symlink;
    Other;
}

implement enum FileType {
    static from_mode(mode: u32) -> FileType;
}

The type bits of mode, decoded. Other covers everything that is not a regular file, a directory, or a symlink — sockets, pipes, devices, and an entry whose type the filesystem would not report.

Variants
Variant
File
Dir
Symlink
Other
Constructors
static from_mode(mode: u32) -> FileType;

Classify from a st_mode value (the S_IFMT bits).

Free Functions

Querying the filesystem

metadata(p) follows symlinks like stat(2); symlink_metadata(p) reports the link itself like lstat(2). The predicates exists, is_file, and is_dir return a bare boolean.

function metadata(p: Path) -> Result<Metadata, IoError>;

Metadata for p, following symlinks (stat(2)).

Metadata for p without following a final symlink. On POSIX this is lstat(2); on Windows it is the handle-free attribute query, which reports the reparse point itself (see attr_metadata).

function exists(p: Path) -> boolean;

True if p exists (any type). Uses access(2) with F_OK (0). A non-existence answer is not distinguished from a permission error on an intermediate directory; use metadata if you need the reason.

function is_file(p: Path) -> boolean;

True if p exists and is a regular file.

function is_dir(p: Path) -> boolean;

True if p exists and is a directory.

Platform notes

On POSIX this is stat(2) and lstat(2). struct stat is opaque across glibc versions, so it is read as a fixed-size buffer with the needed fields pulled out by their Linux x86-64 offsets.

On Windows it uses the Win32 file API rather than msvcrt's stat shims, which lay the struct out completely differently — decoding it with the POSIX offsets would return garbage. metadata opens an attribute-only handle so directories open too, and symlink_metadata reads the reparse point directly. Windows has no POSIX permission bits, so mode() is synthesized there: type bits from the file attributes plus 0644/0444 for files (by the read-only attribute) or 0755/0555 for directories. It is not a view of the real ACL. modified_secs converts the FILETIME to Unix seconds.

Constants

const STAT_OFF_MODE:          u64 = 24;
const STAT_OFF_SIZE:          u64 = 48;
const STAT_OFF_MTIME_SEC:     u64 = 88;
const WFAD_OFF_ATTRS:         u64 = 0;
const WFAD_OFF_WTIME:         u64 = 20;
const WFAD_OFF_SIZE_HIGH:     u64 = 28;
const WFAD_OFF_SIZE_LOW:      u64 = 32;
const BHFI_OFF_ATTRS:         u64 = 0;
const BHFI_OFF_WTIME:         u64 = 20;
const BHFI_OFF_SIZE_HIGH:     u64 = 32;
const BHFI_OFF_SIZE_LOW:      u64 = 36;
const FILETIME_TO_UNIX_TICKS: u64 = 116444736000000000;
const FILETIME_TICKS_PER_SEC: u64 = 10000000;
const WIN_PERM_FILE_RW:       u32 = 420;
const WIN_PERM_FILE_RO:       u32 = 292;
const WIN_PERM_DIR_RW:        u32 = 493;
const WIN_PERM_DIR_RO:        u32 = 365;
const WIN_PERM_LINK:          u32 = 511;