docs

std::fs

On this page

files and directories over the openat family, so a Dir handle is a capability.

Dir::root() resolves relative paths against the working directory; a Dir opened from it refuses absolute paths and ...

copy reflinks where the OS can (clonefileat, CopyFileExW, copy_file_range), else copies bytes. Windows: a Dir is a directory handle and permissions are the read-only attribute.

Opening: one function per way of opening (open, create, create_new, open_or_create, append), so no invalid combination can be written; each passes its fixed choice to the runtime as an OpenHow. The raw open(2) / CreateFile2 live in os::unix::fs / os::windows::fs (DirExt).

*_async: kqueue / epoll cannot wait on regular files, so each call runs its sync twin on the blocking lane (task::spawn_blocking). A cancelled task leaves the call to finish in the background (a read_async still fills its buffer). read_dir_async streams entries in batches.

Structs

DirSource

pub struct Dir

A directory capability. fd < 0 is root(): the whole namespace, relative paths from the working directory.

  • pub async fn current_async() throws (IoError) -> Dir§

    Source

    Dir::current on the blocking lane: the task waits, its thread does not.

  • pub async fn home_async() throws (IoError) -> Dir§

    Source

    Dir::home on the blocking lane: the task waits, its thread does not.

  • pub async fn temp_async() throws (IoError) -> Dir§

    Source

    Dir::temp on the blocking lane: the task waits, its thread does not.

  • pub async fn open_async<P: AsPath>(ref self, path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File§

    Source

    open on the blocking lane: the task waits, its thread does not.

  • pub async fn create_async<P: AsPath>(ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    create on the blocking lane: the task waits, its thread does not.

  • pub async fn create_new_async<P: AsPath>(ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    create_new on the blocking lane: the task waits, its thread does not.

  • pub async fn open_or_create_async<P: AsPath>(ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    open_or_create on the blocking lane: the task waits, its thread does not.

  • pub async fn append_async<P: AsPath>(ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    append on the blocking lane: the task waits, its thread does not.

  • pub async fn open_dir_async<P: AsPath>(ref self, path: P) throws (IoError) -> Dir§

    Source

    open_dir on the blocking lane: the task waits, its thread does not.

  • pub async fn read_dir_async(ref self) throws (IoError) -> ReadDirStream§

    Source

    This directory's entries as a TryStream (try for await e in try await d.read_dir_async() { .. }), read in batches on a blocking thread.

  • pub async fn set_permissions_async<P: AsPath>(ref self, path: P, perm: Permissions) throws (IoError)§

    Source

    set_permissions on the blocking lane: the task waits, its thread does not.

  • pub async fn metadata_async<P: AsPath>(ref self, path: P) throws (IoError) -> Metadata§

    Source

    metadata on the blocking lane: the task waits, its thread does not.

  • Source

    symlink_metadata on the blocking lane: the task waits, its thread does not.

  • pub async fn exists_async<P: AsPath>(ref self, path: P) -> bool§

    Source

    exists on the blocking lane: the task waits, its thread does not.

  • pub async fn create_dir_async<P: AsPath>(ref self, path: P) throws (IoError)§

    Source

    create_dir on the blocking lane: the task waits, its thread does not.

  • pub async fn create_dir_all_async<P: AsPath>(ref self, path: P) throws (IoError)§

    Source

    create_dir_all on the blocking lane: the task waits, its thread does not.

  • pub async fn remove_file_async<P: AsPath>(ref self, path: P) throws (IoError)§

    Source

    remove_file on the blocking lane: the task waits, its thread does not.

  • pub async fn remove_dir_async<P: AsPath>(ref self, path: P) throws (IoError)§

    Source

    remove_dir on the blocking lane: the task waits, its thread does not.

  • pub async fn remove_dir_all_async<P: AsPath>(ref self, path: P) throws (IoError)§

    Source

    remove_dir_all on the blocking lane: the task waits, its thread does not.

  • pub async fn rename_async<P: AsPath, Q: AsPath>(ref self, from: P, to: Q) throws (IoError)§

    Source

    rename on the blocking lane: the task waits, its thread does not.

  • pub async fn copy_async<P: AsPath, Q: AsPath>(ref self, from: P, to: Q) throws (IoError) -> u64§

    Source

    copy on the blocking lane: the task waits, its thread does not.

  • Source

    symlink on the blocking lane: the task waits, its thread does not.

  • Source

    hardlink on the blocking lane: the task waits, its thread does not.

  • pub async fn close_async(self) throws (IoError)§

    Source

    close on the blocking lane: the task waits, its thread does not.

  • pub const fn root() -> Dir§

    Source

    The whole namespace: absolute paths as given, relative ones from the working directory.

  • pub fn current() throws (IoError) -> Dir§

    Source

    The working directory.

  • pub fn home() throws (IoError) -> Dir§

    Source

    The home directory (env::home_dir); NotFound when it is not set.

  • pub fn temp() throws (IoError) -> Dir§

    Source

    The temp directory (env::temp_dir).

  • pub fn open<P: AsPath>(self, path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File§

    Source

    An existing file, opened for access; missing: NotFound.

  • pub fn create<P: AsPath>(self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created, or emptied when it exists; open for writing (and reading with flags.read).

  • pub fn create_new<P: AsPath>(self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created; AlreadyExists when anything is at path (a dangling symbolic link too).

  • pub fn open_or_create<P: AsPath>(self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created when missing; the contents are kept, the position is 0.

  • pub fn append<P: AsPath>(self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created when missing; every write goes to the end.

  • pub fn open_dir<P: AsPath>(self, path: P) throws (IoError) -> Dir§

    Source

    Open the subdirectory path as a new capability; NotADirectory if it is not one.

  • pub fn set_permissions<P: AsPath>(self, path: P, perm: Permissions) throws (IoError)§

    Source

    Set the permissions of path (a final symbolic link is followed). Windows: only the read-only attribute.

  • pub fn read_dir(self) throws (IoError) -> ReadDir§

    Source

    The entries of this directory.

  • pub fn metadata<P: AsPath>(self, path: P) throws (IoError) -> Metadata§

    Source

    Follows symlinks.

  • Source

    Does not follow a final symlink.

  • pub fn exists<P: AsPath>(self, path: P) -> bool§

    Source

    Whether path exists (symlinks followed); any error reads as false.

  • pub fn create_dir<P: AsPath>(self, path: P) throws (IoError)§

    Source

    Create the directory path; its parent must exist (NotFound), and AlreadyExists if it is there.

  • pub fn create_dir_all<P: AsPath>(self, path: P) throws (IoError)§

    Source

    path and any missing parents; fine if it already is a directory.

  • pub fn remove_file<P: AsPath>(self, path: P) throws (IoError)§

    Source

    Remove the file or symlink path (not a directory).

  • pub fn remove_dir<P: AsPath>(self, path: P) throws (IoError)§

    Source

    An empty directory.

  • pub fn remove_dir_all<P: AsPath>(self, path: P) throws (IoError)§

    Source

    A directory and everything under it (symlinks are removed, not followed).

  • pub fn rename<P: AsPath, Q: AsPath>(self, from: P, to: Q) throws (IoError)§

    Source

    Rename from to to, replacing a file at to.

  • pub fn copy<P: AsPath, Q: AsPath>(self, from: P, to: Q) throws (IoError) -> u64§

    Source

    Copy contents and permissions; returns the byte count.

  • Source

    link -> target (a symbolic link; target is stored verbatim).

  • Source

    link as a second name for the file target (a hard link).

  • pub fn close(self) throws (IoError)§

    Source

    Release the handle (root() has none).

Trait implementations 2

DirEntrySource

pub struct DirEntry

One directory entry, with its lstat metadata.

  • pub const fn path(self) -> Path§

    Source

    The listed directory's path joined with the name.

  • pub const fn file_name(self) -> str§

    Source

    The entry's name within its directory.

  • pub const fn metadata(self) -> Metadata§

    Source

    Metadata of the entry itself (a symlink is not followed).

  • pub const fn file_type(self) -> FileType§

    Source

    What the entry is (the symlink itself, not its target).

  • pub const fn is_dir(self) -> bool§

    Source

    true for a directory (a symlink to one is not).

  • pub const fn is_file(self) -> bool§

    Source

    true for a regular file.

  • Source

    true for a symbolic link.

Trait implementations 1

ReadDirSource

pub struct ReadDir

The entries of one directory, in the order the OS returns them (. / .. skipped): a TryIterator (try for e in fs::read_dir(p) { .. }).

Trait implementations 3

ReadDirStreamSource

pub struct ReadDirStream

read_dir_async: a ReadDir as a TryStream. Each refill reads up to DIR_BATCH entries on a blocking thread while the task suspends. Closed at its end or where its owner leaves scope; a cancelled task's refill finishes first.

Trait implementations 3

WalkSource

pub struct Walk

Recursive walk, depth first in pre-order (a directory before its entries), the root first and each directory's entries sorted by name (Go filepath.WalkDir). Symlinks are listed, not followed. Each directory is read whole when reached, so a Walk never holds a stream open (stopping early leaks nothing). An unreadable directory throws its error from next; calling next again goes on with the rest.

Trait implementations 2

CreateFlagsSource

pub struct CreateFlags

Knobs of the functions that may create the file.

  • pub read: bool = false§

    Source

    Open for reading too.

  • pub permissions: Permissions = Permissions::default()§

    Source

    A created file's permissions, before the umask; an existing file keeps its own. Windows: only the read-only attribute.

  • Source

    false: a final symbolic link is FilesystemLoop, not followed.

  • pub os: OsCreateFlags = OsCreateFlags {}§

    Source

    The target's own knobs (os::unix::fs::OsCreateFlags, os::windows::fs::OsCreateFlags; empty elsewhere).

OpenFlagsSource

pub struct OpenFlags

Knobs of open.

  • Source

    false: a final symbolic link is FilesystemLoop, not followed.

  • pub os: OsOpenFlags = OsOpenFlags {}§

    Source

    The target's own knobs (os::unix::fs::OsOpenFlags, os::windows::fs::OsOpenFlags; empty elsewhere).

OsCreateFlagsSource

pub struct OsCreateFlags

No target knobs here.

OsOpenFlagsSource

pub struct OsOpenFlags

No target knobs here.

MetadataSource

pub struct Metadata

stat of a file.

  • pub const fn len(self) -> u64§

    Source

    Size in bytes.

  • pub const fn file_type(self) -> FileType§

    Source

    What the path is: file, directory, symlink or other.

  • pub const fn is_file(self) -> bool§

    Source

    true for a regular file.

  • pub const fn is_dir(self) -> bool§

    Source

    true for a directory.

  • Source

    true for a symbolic link (only from symlink_metadata).

  • pub const fn permissions(self) -> Permissions§

    Source

    The permission bits.

  • pub const fn modified(self) throws (IoError) -> SystemTime§

    Source

    Last modification as a SystemTime; never throws on Unix.

  • pub const fn accessed(self) throws (IoError) -> SystemTime§

    Source

    Last access as a SystemTime.

  • pub const fn status_changed(self) throws (IoError) -> SystemTime§

    Source

    Last change of the contents or the metadata (permissions, links, a rename) as a SystemTime.

  • pub const fn created(self) throws (IoError) -> SystemTime§

    Source

    Creation as a SystemTime; Unsupported where the OS or file system keeps none (Linux before 4.11, some file systems).

Trait implementations 1

PermissionsSource

pub struct Permissions

A file's permissions: Unix permission bits (Windows: the read-only attribute; PermissionsExt in std::os::unix::fs reads and sets the bits).

  • pub const fn readonly(self) -> bool§

    Source

    No write bit for anyone.

  • pub const fn set_readonly(ref self, readonly: bool)§

    Source

    true clears every write bit; false adds the owner's write bit only (group and others stay as they are).

Trait implementations 2

FileSource

pub struct File

An open file. It has one owner: let b = a moves it, and try_clone opens an independent handle. Close it with try f.close() or using.

  • pub fn open<P: AsPath>(path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File§

    Source

    An existing file, opened for access; missing: NotFound.

  • pub fn create<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created, or emptied when it exists; open for writing (and reading with flags.read).

  • pub fn create_new<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created; AlreadyExists when anything is at path.

  • pub fn open_or_create<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created when missing; the contents are kept.

  • pub fn append<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    Created when missing; every write goes to the end.

  • pub fn close(self) throws (IoError)§

    Source

    Close now; later calls fail with InvalidInput. Closing twice is a no-op. With calls in flight through other copies, the last of them releases the fd.

  • pub fn sync_all(self) throws (IoError)§

    Source

    Flush data and metadata to the device (fsync).

  • pub fn sync_data(self) throws (IoError)§

    Source

    Flush data to the device, metadata only as needed to read it back (fdatasync).

  • pub fn set_len(self, size: u64) throws (IoError)§

    Source

    Truncate or zero-extend the file to size bytes.

  • pub fn metadata(self) throws (IoError) -> Metadata§

    Source

    The open file's metadata (fstat).

  • pub fn permissions(self) throws (IoError) -> Permissions§

    Source

    The open file's permissions.

  • pub fn set_permissions(self, perm: Permissions) throws (IoError)§

    Source

    Set the open file's permissions (fchmod).

  • pub fn try_clone(self) throws (IoError) -> File§

    Source

    A second handle on the same open file (dup).

  • pub fn lock_exclusive(self) throws (IoError)§

    Source

    Take an exclusive advisory lock, blocking until it is free (flock).

  • pub fn lock_shared(self) throws (IoError)§

    Source

    Take a shared advisory lock, blocking while an exclusive one is held.

  • pub fn try_lock_exclusive(self) throws (IoError) -> bool§

    Source

    false if another holder has it.

  • pub fn unlock(self) throws (IoError)§

    Source

    Release this handle's advisory lock.

  • pub unsafe fn mmap(self, offset: u64, len: usize) throws (IoError) -> Mmap§

    Source

    Map len bytes at offset (a multiple of the page size) read-only and shared. Unsafe: another writer of the file changes the bytes under the mapping, and truncating it makes reads fault.

  • pub async fn open_async<P: AsPath>(path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File§

    Source

    File::open on the blocking lane: the task waits, its thread does not.

  • pub async fn create_async<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    File::create on the blocking lane: the task waits, its thread does not.

  • pub async fn create_new_async<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    File::create_new on the blocking lane: the task waits, its thread does not.

  • pub async fn open_or_create_async<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    File::open_or_create on the blocking lane: the task waits, its thread does not.

  • pub async fn append_async<P: AsPath>(path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File§

    Source

    File::append on the blocking lane: the task waits, its thread does not.

  • pub async fn sync_all_async(ref self) throws (IoError)§

    Source

    sync_all on the blocking lane: the task waits, its thread does not.

  • pub async fn sync_data_async(ref self) throws (IoError)§

    Source

    sync_data on the blocking lane: the task waits, its thread does not.

  • pub async fn set_len_async(ref self, size: u64) throws (IoError)§

    Source

    set_len on the blocking lane: the task waits, its thread does not.

  • pub async fn metadata_async(ref self) throws (IoError) -> Metadata§

    Source

    metadata on the blocking lane: the task waits, its thread does not.

  • pub async fn lock_exclusive_async(ref self) throws (IoError)§

    Source

    lock_exclusive on the blocking lane: the task waits, its thread does not.

  • pub async fn lock_shared_async(ref self) throws (IoError)§

    Source

    lock_shared on the blocking lane: the task waits, its thread does not.

Trait implementations 16

MmapSource

pub struct Mmap

A read-only shared mapping from File::mmap. Unmapped by unmap or where its owner leaves scope; reads after that see an empty mapping.

  • pub const fn len(self) -> usize§

    Source

    Bytes mapped; 0 once unmapped.

  • pub const fn is_empty(self) -> bool§

    Source

    true when no bytes are mapped.

  • pub fn get(self, i: usize) -> u8 | none§

    Source

    Byte i, or none past the end.

  • pub fn as_bytes(self) -> Slice<u8>§

    Source

    The mapped bytes, not copied (m.as_bytes()[a:b] for a part). The view reads the mapping itself: use it only while the mapping is live (empty once unmapped).

  • pub fn to_list(self) -> List<u8>§

    Source

    Every byte as a new list.

  • pub fn unmap(self)§

    Source

    Unmap now; later reads see an empty mapping. Unmapping twice is a no-op.

Trait implementations 2

Enums

AccessSource

pub enum Access

What open opens an existing file for.

FileTypeSource

pub enum FileType

What a directory entry is.

Trait implementations 1

Functions

globSource

pub fn glob(pattern: str) throws (IoError) -> List<Path>

The paths matching pattern, sorted. In each /-separated component * matches any run of characters, ? one character and [abc] / [a-z] / [!a] one of a set; a ** component matches any number of directories. Wildcards do not match a leading . unless the component starts with one. A relative pattern gives relative paths. Unreadable directories throw. Python: glob.glob(pattern, recursive=True).

openSource

pub fn open<P: AsPath>(path: P) throws (IoError) -> File

File::open(path): an existing file, for reading. The other ways of opening are on File and Dir.

createSource

pub fn create<P: AsPath>(path: P) throws (IoError) -> File

File::create(path): created or emptied, for writing.

readSource

pub fn read<P: AsPath>(path: P) throws (IoError) -> List<u8>

The whole file as bytes.

read_to_strSource

pub fn read_to_str<P: AsPath>(path: P) throws (IoError) -> str

The whole file as text; InvalidData if it is not UTF-8.

writeSource

pub fn write<P: AsPath, D: AsBytes>(path: P, data: D) throws (IoError)

Create or truncate path and write data (a str or List<u8>).

write_atomicSource

pub fn write_atomic<P: AsPath, D: AsBytes>(path: P, data: D) throws (IoError)

Write via a temp file in the same directory, fsync, rename, then fsync the directory: readers see the old or the new file, never a torn one.

metadataSource

pub fn metadata<P: AsPath>(path: P) throws (IoError) -> Metadata

path's metadata, symlinks followed.

existsSource

pub fn exists<P: AsPath>(path: P) -> bool

Whether path exists (symlinks followed); any error reads as false.

create_dirSource

pub fn create_dir<P: AsPath>(path: P) throws (IoError)

Create the directory path; its parent must exist.

create_dir_allSource

pub fn create_dir_all<P: AsPath>(path: P) throws (IoError)

Create path and any missing parents; fine if it already is a directory.

remove_fileSource

pub fn remove_file<P: AsPath>(path: P) throws (IoError)

Remove the file or symlink path.

remove_dirSource

pub fn remove_dir<P: AsPath>(path: P) throws (IoError)

Remove the empty directory path.

remove_dir_allSource

pub fn remove_dir_all<P: AsPath>(path: P) throws (IoError)

Remove the directory path and everything under it (symlinks are removed, not followed).

renameSource

pub fn rename<P: AsPath, Q: AsPath>(from: P, to: Q) throws (IoError)

Rename from to to, replacing a file at to.

copySource

pub fn copy<P: AsPath, Q: AsPath>(from: P, to: Q) throws (IoError) -> u64

Copy contents and permissions of from to to; returns the byte count.

read_dirSource

pub fn read_dir<P: AsPath>(path: P) throws (IoError) -> ReadDir

The entries of the directory path.

set_permissionsSource

pub fn set_permissions<P: AsPath>(path: P, perm: Permissions) throws (IoError)

Set the permissions of path (a final symbolic link is followed). Windows: only the read-only attribute.

walkSource

pub fn walk<P: AsPath>(root: P) -> Walk

Everything under root, root included (see Walk).

open_asyncSource

pub async fn open_async<P: AsPath>(path: P) throws (IoError) -> File

fs::open on the blocking lane: the task waits, its thread does not.

create_asyncSource

pub async fn create_async<P: AsPath>(path: P) throws (IoError) -> File

fs::create on the blocking lane: the task waits, its thread does not.

read_asyncSource

pub async fn read_async<P: AsPath>(path: P) throws (IoError) -> List<u8>

fs::read on the blocking lane: the task waits, its thread does not.

read_to_str_asyncSource

pub async fn read_to_str_async<P: AsPath>(path: P) throws (IoError) -> str

fs::read_to_str on the blocking lane: the task waits, its thread does not.

write_asyncSource

pub async fn write_async<P: AsPath, D: AsBytes>(path: P, data: D) throws (IoError)

fs::write on the blocking lane: the task waits, its thread does not.

write_atomic_asyncSource

pub async fn write_atomic_async<P: AsPath, D: AsBytes>(path: P, data: D) throws (IoError)

fs::write_atomic on the blocking lane: the task waits, its thread does not.

metadata_asyncSource

pub async fn metadata_async<P: AsPath>(path: P) throws (IoError) -> Metadata

fs::metadata on the blocking lane: the task waits, its thread does not.

exists_asyncSource

pub async fn exists_async<P: AsPath>(path: P) -> bool

fs::exists on the blocking lane: the task waits, its thread does not.

create_dir_asyncSource

pub async fn create_dir_async<P: AsPath>(path: P) throws (IoError)

fs::create_dir on the blocking lane: the task waits, its thread does not.

create_dir_all_asyncSource

pub async fn create_dir_all_async<P: AsPath>(path: P) throws (IoError)

fs::create_dir_all on the blocking lane: the task waits, its thread does not.

remove_file_asyncSource

pub async fn remove_file_async<P: AsPath>(path: P) throws (IoError)

fs::remove_file on the blocking lane: the task waits, its thread does not.

remove_dir_asyncSource

pub async fn remove_dir_async<P: AsPath>(path: P) throws (IoError)

fs::remove_dir on the blocking lane: the task waits, its thread does not.

remove_dir_all_asyncSource

pub async fn remove_dir_all_async<P: AsPath>(path: P) throws (IoError)

fs::remove_dir_all on the blocking lane: the task waits, its thread does not.

rename_asyncSource

pub async fn rename_async<P: AsPath, Q: AsPath>(from: P, to: Q) throws (IoError)

fs::rename on the blocking lane: the task waits, its thread does not.

copy_asyncSource

pub async fn copy_async<P: AsPath, Q: AsPath>(from: P, to: Q) throws (IoError) -> u64

fs::copy on the blocking lane: the task waits, its thread does not.

read_dir_asyncSource

pub async fn read_dir_async<P: AsPath>(path: P) throws (IoError) -> ReadDirStream

The entries of path as a TryStream, read in batches on a blocking thread (Dir::read_dir_async).

set_permissions_asyncSource

pub async fn set_permissions_async<P: AsPath>(path: P, perm: Permissions) throws (IoError)

fs::set_permissions on the blocking lane: the task waits, its thread does not.