std::fs::metadata
File Metadata
Platform-independent file metadata and file type classification.
Fs::Metadata wraps the raw POSIX Stat record and exposes a
portable API. Fs::FileType is an enum that classifies entries
as regular files, directories, or symlinks without requiring
callers to know about S_IFMT masks or DT_* constants.
Types
| Type | Description |
|---|---|
Metadata |
File size, timestamps, permissions, and file type |
FileType |
Classification: FILE, DIRECTORY, SYMLINK, OTHER |
Construction
Users do not create Metadata directly. Instead, obtain it from:
Fs::metadata(path)(stat by path)file.metadata()(fstat on open file)
Example
import Fs from "std::fs";
import Io from "std::io";
function main(): i32 {
let result: Result<Fs::Metadata, Io::IoError> = Fs::metadata("/etc/hosts");
if (result.isError()) {
return 1;
}
let meta: Fs::Metadata = result.unwrap();
if (meta.isFile()) {
let size: i64 = meta.len();
return 0;
}
return 1;
}
namespace FsRecords
record MetadataPlatform-independent file metadata.
Wraps the raw POSIX Stat and provides portable accessors.
Obtained via Fs::metadata(path) or file.metadata().
size: i64File size in bytes.
mode: u32POSIX mode bits (permissions + file type mask).
atime: i64Last access time (seconds since Unix epoch).
mtime: i64Last modification time (seconds since Unix epoch).
ctime: i64Last status change time (seconds since Unix epoch).
fileType(&self): FileTypeReturns the classified file type.
accessedTime(&self): i64Returns the last access time in seconds since Unix epoch.
createdTime(&self): i64Returns the last status change time in seconds since Unix epoch.
fileType(&self): FileTypeReturns the classified file type.
isDir(&self): booleanReturns true if this is a directory.
isFile(&self): booleanReturns true if this is a regular file.
isSymlink(&self): booleanReturns true if this is a symbolic link.
len(&self): i64Returns the file size in bytes.
modifiedTime(&self): i64Returns the last modification time in seconds since Unix epoch.
permissions(&self): u32Returns the raw POSIX permission bits (lower 12 bits of mode).
Enums
enum FileTypeClassification of a filesystem entry.
FILEDIRECTORYSYMLINKOTHERFunctions
function fileTypeFromDirent(dtype: u8): FileTypeClassifies a dirent d_type value into a FileType.
function fileTypeFromMode(mode: u32): FileTypeClassifies a POSIX mode value into a FileType.
Uses the S_IFMT mask to extract the file type bits.
function metadataFromStat(st: &Stat): MetadataConverts a raw Stat into a high-level Metadata.