gd_plugins::files¶
Private folders and files, and files replaced in one step: what a host or a publisher needs to keep state (a registry, tokens, a frozen site) that only its user may read. Where the system has permissions, folders are made 0700 and files 0600. lock_exclusive serializes the commands that change such state.
let dir = gd_plugins::files::make_temp_dir(&std::env::temp_dir(), "doc-example")?;
gd_plugins::files::write_atomically(&dir.join("state.toml"), b"schema = 1\n")
.map_err(|(_, e)| e)?;
std::fs::remove_dir_all(&dir)?;
Structs
- pub struct files::FileLock¶
-
An exclusive lock on a lock file, held until the value is dropped. A second lock of the same file is refused while this one lives, in another process and in another thread of this one. When the process ends without dropping it, the system releases the lock.
Trait Implementations
- impl Drop for FileLock
-
- fn drop(&mut self)
Enums
- pub enum files::LockError¶
-
Why
lock_exclusivetook no lock.Variants
- Held¶
-
Another holder has the lock: a live one, or one that ended without releasing while a child it had just created had not yet started its program.
- Io(io::Error)¶
-
The lock file could not be opened or locked.
Trait Implementations
- impl fmt::Display for LockError
-
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
Functions
- pub fn files::lock_exclusive(path: &Path) -> Result<FileLock, LockError>¶
-
Takes the exclusive lock of the lock file
path, creating the file if it is missing, without waiting. The folder ofpathmust exist. Returns the lock, which borrows nothing; fails withLockError::Heldwhen the lock is held, and withLockError::Iowhen the file cannot be opened or locked.We keep the lock file after use: removing it would let two takers lock different files at one path.
- pub fn files::lock_exclusive_private(path: &Path) -> Result<FileLock, LockError>¶
-
As
lock_exclusive, and a lock file it creates is readable and writable only by the user, where the system has permissions.
- pub fn files::create_private_dir(dir: &Path) -> io::Result<()>¶
-
Creates
dirand its missing parents, readable only by the user. A folder that exists already is kept as it is. Fails with the error of the folder that could not be made.
- pub fn files::make_temp_dir(parent: &Path, prefix: &str) -> io::Result<PathBuf>¶
-
Makes a new folder in
parent, readable only by the user, namedprefix-and a suffix unique to this process and call, and returns its path.parentmust exist.
- pub fn files::create_private(path: &Path) -> io::Result<fs::File>¶
-
Creates a new file readable and writable only by the user and opens it for writing; fails if it exists.
- pub fn files::write_atomically(path: &Path, data: &[u8]) -> Result<(), (PathBuf, io::Error)>¶
-
Writes
datato a private file besidepath(its name and.next), syncs it and renames it overpath, so a reader sees the old file or the new one, never a part. On failure the file beside is removed, and the error comes with the path that failed: the file beside when writing it failed,pathwhen the rename did.Two writers of one
pathmust not run at once, since they share the file beside: hold theFileLockof that state while reading it and writing it back.