plat/guarded.rs#
Source: lib/plat/guarded.rs
guarded -- the public file functions: a name with a scheme goes to its vfs provider
(vfs.rs), any other name is checked against the app's permissions (policy.rs) and then
handed to the OS call (win_sys.rs, linux_sys.rs, web_files.rs: the *_raw functions, which
the platform layer uses for its own system files)
trash(path) moves into the trash (trash.rs) and counts as writing the path.
A refused path looks like a missing or unreadable one: 0 / None / false.
File and FileLock hold an OS file (also behind a folder mount: os_path) or a stream of a
mount (Provider::open: MemFs, Bundle, ZipFs, Overlay, Sub; other providers: the whole file,
read at the start and written back by sync and drop); a FileLock on a mount without OS
files holds inside this process only.
File::create_new and AtomicFile's sibling on a mount: Provider::create_new (the check and the
creation in one step: MemFs, Overlay, Sub, content://), else the OS's exclusive create of
os_path; a provider with neither has no File::create_new.
AtomicFile is write_file in pieces: a sibling (OS: OsAtomic of win_sys / linux_sys /
web_files / android_files; mounts: "NAME.tmp-.." in the mount) put in place by commit,
removed when dropped; content:// (no rename over a document): the bytes in memory, written
in place by commit (aio.rs builds the async writes and copies on it).
fn file_exists(path: &str) -> u320 missing, 1 file, 2 directory [plat_file_exists]fn file_exists_checked(path: &str) -> Option<u32>Some(0) only for a missing path, Some(1/2) for file/directory, None for other errors. A folder mount answers for its OS file; other mounts have only their kind (0 for missing and for errors alike).fn mkdir(path: &str) -> boolcreate a directory and its missing parents; an existing directory is fine [plat_mkdir]fn delete(path: &str) -> boola file or an empty directory [plat_delete]fn trash(path: &str) -> boolmove a file or folder into the trash (Windows: the recycle bin; Linux: the freedesktop trash) -> false: it stays where it was, never deleted for good (trash.rs). Counts as writing the path; a vfs name needs a folder mount.fn rename(old: &str, new: &str) -> boolrename, replacing an existing target (both names on the OS or in the same mount) [plat_rename]fn dir_list(path: &str, f: &mut dyn FnMut(&str, bool, u32)) -> boolf(name, is_dir, size_lo) for every entry except "." and ".." [plat_dir_list]fn read_file(path: &str) -> Option<Vec<u8>>the contents of a file [plat_file_read]fn write_file(path: &str, data: &[u8]) -> boolWrite an exclusively created sibling, flush it, then replace path (a mount: its own way of replacing a file; missing folders are created). [plat_file_write]struct File(Inner)an open file (closed when dropped): an OS file, or a stream of a vfs mount
impl File
fn open(path: &str, mode: u32) -> Option<File>FO_READ (existing, read only), FO_WRITE (create / truncate), FO_RW (open or create); on every mount (vfs.rs)fn create_new(path: &str) -> Option<File>Create a named file exclusively; never open an existing path. A mount: the provider's own check-and-create (Provider::create_new: mem://, content://, overlays) or the OS's of a folder mount; None on a provider with neither.fn open_temp(dir: Option<&str>) -> Option<File>a new file without a lasting name in dir (None = temp_dir) that disappears when it is closed (also after a crash); a mount without OS files: a file in memory [plat_file_open_temp]fn read_at(&self, off: u64, buf: &mut [u8]) -> Option<usize>bytes read at off (fewer at the end of the file), None on an error before any bytefn write_at(&self, off: u64, buf: &[u8]) -> Option<usize>bytes written at off (the file grows as needed), None on an error before any bytefn size(&self) -> Option<u64>fn sync(&self) -> boolwrite cached data to the disk (a mount: where its readers find it)fn set_len(&self, len: u64) -> boolcut or extend the file tolenbytesstruct AtomicFile(Option<AtomicInner>)The streaming form of write_file: the bytes go into a sibling of the target, commit puts it in place in one step (readers see the old file or all of the new one); dropped without commit, the sibling is removed and the target is unchanged. Checked and routed like write_file when it is created. OS paths: an exclusive "NAME.tmp-PID-SEQ-TIME" beside the file, fsync and rename (Linux keeps the old file's access bits; the browser: in memory until commit). Mounts: a "NAME.tmp-.." file of the mount, renamed by the provider (one without rename gets the whole file written); a provider whose rename cannot replace a file (atomic_rename() == false, content://) gets the bytes written in place at commit.
impl AtomicFile
fn create(path: &str) -> Option<AtomicFile>a writer of path (None: refused, a folder, an invalid name, or the sibling could not be created)fn write_at(&self, off: u64, data: &[u8]) -> Option<usize>bytes written at off (the file grows as needed), None on an errorfn commit(mut self) -> boolput the written file in place -> false: the target is unchanged, the sibling gonestruct FileLock(LockInner)Nonblocking exclusive sidecar lock; drop releases it. All cooperating writers must lock the same path before checking and replacing data. OS files: across processes (Windows: an exclusive handle, Linux: flock); a mount without OS files: inside this process.
impl FileLock
fn acquire(path: &str) -> Option<Self>Open or create without truncating. None means busy, invalid path, refused or I/O failure.