Guidedog マニュアル 1.0.0-beta.16
言語
このページの内容
Guidedog / ドキュメント 1.0.0-beta.16

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_exclusive took 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
impl std::error::Error for LockError
fn source(&self) -> Option<&(dyn std::error::Error + 'static)>
impl From<io::Error> for LockError
fn from(e: io::Error) -> LockError

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 of path must exist. Returns the lock, which borrows nothing; fails with LockError::Held when the lock is held, and with LockError::Io when 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 dir and 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, named prefix- and a suffix unique to this process and call, and returns its path. parent must 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 data to a private file beside path (its name and .next), syncs it and renames it over path, 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, path when the rename did.

Two writers of one path must not run at once, since they share the file beside: hold the FileLock of that state while reading it and writing it back.