metadata
import std::fs::metadata; · source
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.
| Method | Returns | Notes |
|---|---|---|
file_type() | FileType | |
is_file() / is_dir() / is_symlink() | boolean | |
len() | u64 | Size in bytes, meaningful for regular files. |
mode() | u32 | Full st_mode — type bits plus permission bits. |
permissions() | u32 | The permission bits alone. |
modified_secs() | i64 | Last modification as 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.
Querying the filesystem
function metadata(p: Path) -> Result<Metadata, IoError>;
function symlink_metadata(p: Path) -> Result<Metadata, IoError>;
function exists(p: Path) -> boolean;
function is_file(p: Path) -> boolean;
function is_dir(p: Path) -> boolean;
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.
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.