std::fs
On this page
- Structs
Dir49appendappend_asynccloseclose_asynccopycopy_asynccreatecreate_asynccreate_dircreate_dir_allcreate_dir_all_asynccreate_dir_asynccreate_newcreate_new_asynccurrentcurrent_asyncexistsexists_asynchardlinkhardlink_asynchomehome_asyncmetadatametadata_asyncopenopen_asyncopen_diropen_dir_asyncopen_or_createopen_or_create_asyncread_dirread_dir_asyncremove_dirremove_dir_allremove_dir_all_asyncremove_dir_asyncremove_fileremove_file_asyncrenamerename_asyncrootset_permissionsset_permissions_asyncsymlinksymlink_asyncsymlink_metadatasymlink_metadata_asynctemptemp_async
Permissions2File29appendappend_asyncclosecreatecreate_asynccreate_newcreate_new_asynclock_exclusivelock_exclusive_asynclock_sharedlock_shared_asyncmetadatametadata_asyncmmapopenopen_asyncopen_or_createopen_or_create_asyncpermissionsset_lenset_len_asyncset_permissionssync_allsync_all_asyncsync_datasync_data_asynctry_clonetry_lock_exclusiveunlock
- Enums
FileType3- Functions
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 DirA directory capability. fd < 0 is root(): the whole namespace,
relative paths from the working directory.
Dir::currenton the blocking lane: the task waits, its thread does not.Dir::homeon the blocking lane: the task waits, its thread does not.Dir::tempon the blocking lane: the task waits, its thread does not.
Sourcepub async fn open_async<P: AsPath>(§ref self, path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File openon the blocking lane: the task waits, its thread does not.
Sourcepub async fn create_async<P: AsPath>(§ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File createon the blocking lane: the task waits, its thread does not.
Sourcepub async fn create_new_async<P: AsPath>(§ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File create_newon the blocking lane: the task waits, its thread does not.
Sourcepub async fn open_or_create_async<P: AsPath>(§ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File open_or_createon the blocking lane: the task waits, its thread does not.
Sourcepub async fn append_async<P: AsPath>(§ref self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File appendon the blocking lane: the task waits, its thread does not.open_diron the blocking lane: the task waits, its thread does not.
Sourcepub async fn read_dir_async(§ref self) throws (IoError) -> ReadDirStream 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.
Sourcepub async fn set_permissions_async<P: AsPath>(§ref self, path: P, perm: Permissions) throws (IoError) set_permissionson the blocking lane: the task waits, its thread does not.metadataon the blocking lane: the task waits, its thread does not.
Sourcepub async fn symlink_metadata_async<P: AsPath>(§ref self, path: P) throws (IoError) -> Metadata symlink_metadataon the blocking lane: the task waits, its thread does not.existson the blocking lane: the task waits, its thread does not.create_diron the blocking lane: the task waits, its thread does not.create_dir_allon the blocking lane: the task waits, its thread does not.remove_fileon the blocking lane: the task waits, its thread does not.remove_diron the blocking lane: the task waits, its thread does not.remove_dir_allon the blocking lane: the task waits, its thread does not.renameon the blocking lane: the task waits, its thread does not.
Sourcepub async fn copy_async<P: AsPath, Q: AsPath>(§ref self, from: P, to: Q) throws (IoError) -> u64 copyon the blocking lane: the task waits, its thread does not.
Sourcepub async fn symlink_async<P: AsPath, Q: AsPath>(§ref self, target: P, link: Q) throws (IoError) symlinkon the blocking lane: the task waits, its thread does not.
Sourcepub async fn hardlink_async<P: AsPath, Q: AsPath>(§ref self, target: P, link: Q) throws (IoError) hardlinkon the blocking lane: the task waits, its thread does not.closeon the blocking lane: the task waits, its thread does not.The whole namespace: absolute paths as given, relative ones from the working directory.
The working directory.
The home directory (
env::home_dir);NotFoundwhen it is not set.The temp directory (
env::temp_dir).
Sourcepub fn open<P: AsPath>(§self, path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File An existing file, opened for
access; missing:NotFound.
Sourcepub fn create<P: AsPath>(§self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created, or emptied when it exists; open for writing (and reading with
flags.read).
Sourcepub fn create_new<P: AsPath>(§self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created;
AlreadyExistswhen anything is atpath(a dangling symbolic link too).
Sourcepub fn open_or_create<P: AsPath>(§self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created when missing; the contents are kept, the position is 0.
Sourcepub fn append<P: AsPath>(§self, path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created when missing; every write goes to the end.
Open the subdirectory
pathas a new capability;NotADirectoryif it is not one.
Sourcepub fn set_permissions<P: AsPath>(§self, path: P, perm: Permissions) throws (IoError) Set the permissions of
path(a final symbolic link is followed). Windows: only the read-only attribute.The entries of this directory.
Follows symlinks.
Does not follow a final symlink.
Whether
pathexists (symlinks followed); any error reads asfalse.Create the directory
path; its parent must exist (NotFound), andAlreadyExistsif it is there.pathand any missing parents; fine if it already is a directory.Remove the file or symlink
path(not a directory).An empty directory.
A directory and everything under it (symlinks are removed, not followed).
Rename
fromtoto, replacing a file atto.Copy contents and permissions; returns the byte count.
link->target(a symbolic link;targetis stored verbatim).linkas a second name for the filetarget(a hard link).Release the handle (
root()has none).
DirEntrySource
pub struct DirEntryOne directory entry, with its lstat metadata.
The listed directory's path joined with the name.
The entry's name within its directory.
Metadata of the entry itself (a symlink is not followed).
What the entry is (the symlink itself, not its target).
truefor a directory (a symlink to one is not).truefor a regular file.truefor a symbolic link.
Trait implementations 1
impl Debug for DirEntry
ReadDirSource
pub struct ReadDirThe 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
impl Debug for ReadDirimpl TryIterator for ReadDirimpl Drop for ReadDir
ReadDirStreamSource
pub struct ReadDirStreamread_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.
WalkSource
pub struct WalkRecursive 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
impl Debug for Walkimpl TryIterator for Walk
CreateFlagsSource
pub struct CreateFlagsKnobs of the functions that may create the file.
Open for reading too.
Sourcepub permissions: Permissions = Permissions::default()§A created file's permissions, before the umask; an existing file keeps its own. Windows: only the read-only attribute.
false: a final symbolic link isFilesystemLoop, not followed.The target's own knobs (
os::unix::fs::OsCreateFlags,os::windows::fs::OsCreateFlags; empty elsewhere).
OpenFlagsSource
pub struct OpenFlagsKnobs of open.
false: a final symbolic link isFilesystemLoop, not followed.The target's own knobs (
os::unix::fs::OsOpenFlags,os::windows::fs::OsOpenFlags; empty elsewhere).
OsCreateFlagsSource
pub struct OsCreateFlagsNo target knobs here.
OsOpenFlagsSource
pub struct OsOpenFlagsNo target knobs here.
MetadataSource
pub struct Metadatastat of a file.
Size in bytes.
What the path is: file, directory, symlink or other.
truefor a regular file.truefor a directory.truefor a symbolic link (only fromsymlink_metadata).
Sourcepub const fn permissions(§self) -> Permissions The permission bits.
Sourcepub const fn modified(§self) throws (IoError) -> SystemTime Last modification as a
SystemTime; never throws on Unix.
Sourcepub const fn accessed(§self) throws (IoError) -> SystemTime Last access as a
SystemTime.
Sourcepub const fn status_changed(§self) throws (IoError) -> SystemTime Last change of the contents or the metadata (permissions, links, a rename) as a
SystemTime.
Sourcepub const fn created(§self) throws (IoError) -> SystemTime Creation as a
SystemTime;Unsupportedwhere the OS or file system keeps none (Linux before 4.11, some file systems).
Trait implementations 1
impl Debug for Metadata
PermissionsSource
pub struct PermissionsA file's permissions: Unix permission bits (Windows: the read-only
attribute; PermissionsExt in std::os::unix::fs reads and sets
the bits).
No write bit for anyone.
trueclears every write bit;falseadds the owner's write bit only (group and others stay as they are).
FileSource
pub struct FileAn 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.
Sourcepub fn open<P: AsPath>(§path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File An existing file, opened for
access; missing:NotFound.
Sourcepub fn create<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created, or emptied when it exists; open for writing (and reading with
flags.read).
Sourcepub fn create_new<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created;
AlreadyExistswhen anything is atpath.
Sourcepub fn open_or_create<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created when missing; the contents are kept.
Sourcepub fn append<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File Created when missing; every write goes to the end.
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.Flush data and metadata to the device (
fsync).Flush data to the device, metadata only as needed to read it back (
fdatasync).Truncate or zero-extend the file to
sizebytes.The open file's metadata (
fstat).
Sourcepub fn permissions(§self) throws (IoError) -> Permissions The open file's permissions.
Sourcepub fn set_permissions(§self, perm: Permissions) throws (IoError) Set the open file's permissions (
fchmod).A second handle on the same open file (
dup).Take an exclusive advisory lock, blocking until it is free (
flock).Take a shared advisory lock, blocking while an exclusive one is held.
falseif another holder has it.Release this handle's advisory lock.
Map
lenbytes atoffset(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.
Sourcepub async fn open_async<P: AsPath>(§path: P, access: Access = Access::ReadOnly, flags: OpenFlags = OpenFlags {}) throws (IoError) -> File File::openon the blocking lane: the task waits, its thread does not.
Sourcepub async fn create_async<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File File::createon the blocking lane: the task waits, its thread does not.
Sourcepub async fn create_new_async<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File File::create_newon the blocking lane: the task waits, its thread does not.
Sourcepub async fn open_or_create_async<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File File::open_or_createon the blocking lane: the task waits, its thread does not.
Sourcepub async fn append_async<P: AsPath>(§path: P, flags: CreateFlags = CreateFlags {}) throws (IoError) -> File File::appendon the blocking lane: the task waits, its thread does not.sync_allon the blocking lane: the task waits, its thread does not.sync_dataon the blocking lane: the task waits, its thread does not.set_lenon the blocking lane: the task waits, its thread does not.metadataon the blocking lane: the task waits, its thread does not.lock_exclusiveon the blocking lane: the task waits, its thread does not.lock_sharedon the blocking lane: the task waits, its thread does not.
Trait implementations 16
impl Debug for Fileimpl AsRawFd for Fileimpl IntoRawFd for Fileimpl FromRawFd for Fileimpl AsRawHandle for Fileimpl IntoRawHandle for Fileimpl FromRawHandle for Fileimpl Read for Fileimpl Write for Fileimpl Seek for Fileimpl AsyncRead for Fileimpl AsyncWrite for Fileimpl AsyncSeek for Fileimpl Close for Fileimpl AsyncClose for Fileimpl Drop for File
MmapSource
pub struct MmapA read-only shared mapping from File::mmap. Unmapped by unmap or
where its owner leaves scope; reads after that see an empty mapping.
Bytes mapped; 0 once unmapped.
truewhen no bytes are mapped.Byte
i, ornonepast the end.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).Every byte as a new list.
Unmap now; later reads see an empty mapping. Unmapping twice is a no-op.
Enums
AccessSource
pub enum AccessWhat open opens an existing file for.
Write, every write at the end.
Read, and every write at the end.
FileTypeSource
pub enum FileTypeWhat a directory entry is.
A device, pipe, socket, ...
truefor a regular file.truefor a directory.truefor a symbolic link.
Trait implementations 1
impl Debug for FileType
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.
symlink_metadataSource
pub fn symlink_metadata<P: AsPath>(path: P) throws (IoError) -> Metadata path's metadata; a final symlink is described, not 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.
symlinkSource
pub fn symlink<P: AsPath, Q: AsPath>(target: P, link: Q) throws (IoError) link -> target (a symbolic link; target is stored verbatim).
hardlinkSource
pub fn hardlink<P: AsPath, Q: AsPath>(target: P, link: Q) throws (IoError) link as a second name for the file target (a hard link).
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.
symlink_metadata_asyncSource
pub async fn symlink_metadata_async<P: AsPath>(path: P) throws (IoError) -> Metadata fs::symlink_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.
symlink_asyncSource
pub async fn symlink_async<P: AsPath, Q: AsPath>(target: P, link: Q) throws (IoError) fs::symlink on the blocking lane: the task waits, its thread does not.
hardlink_asyncSource
pub async fn hardlink_async<P: AsPath, Q: AsPath>(target: P, link: Q) throws (IoError) fs::hardlink 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.