gd_plugins::files ================= .. rust:module:: gd_plugins::files :synopsis: 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. 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``. :rust:fn:`lock_exclusive ` serializes the commands that change such state. .. code-block:: rust 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)?; .. rubric:: Structs .. rust:struct:: pub struct 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. .. rubric:: Trait Implementations .. rust:impl:: impl Drop for FileLock :noindex: :for: FileLock :trait: Drop .. rust:method:: fn drop(&mut self) :noindex: .. rubric:: Enums .. rust:enum:: pub enum LockError Why :rust:fn:`lock_exclusive ` took no lock. .. rubric:: Variants .. rust:variant:: 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. .. rust:variant:: Io(io::Error) The lock file could not be opened or locked. .. rubric:: Trait Implementations .. rust:impl:: impl fmt::Display for LockError :noindex: :for: LockError :trait: fmt::Display .. rust:method:: fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result :noindex: .. rust:impl:: impl std::error::Error for LockError :noindex: :for: LockError :trait: std::error::Error .. rust:method:: fn source(&self) -> Option<&(dyn std::error::Error + 'static)> :noindex: .. rust:impl:: impl From for LockError :noindex: :for: LockError :trait: From .. rust:method:: fn from(e: io::Error) -> LockError :noindex: .. rubric:: Functions .. rust:function:: pub fn lock_exclusive(path: &Path) -> Result 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 :rust:variant:`LockError::Held ` when the lock is held, and with :rust:variant:`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. .. rust:function:: pub fn lock_exclusive_private(path: &Path) -> Result As :rust:fn:`lock_exclusive `, and a lock file it creates is readable and writable only by the user, where the system has permissions. .. rust:function:: pub fn 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. .. rust:function:: pub fn make_temp_dir(parent: &Path, prefix: &str) -> io::Result 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. .. rust:function:: pub fn create_private(path: &Path) -> io::Result Creates a new file readable and writable only by the user and opens it for writing; fails if it exists. .. rust:function:: pub fn 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 :rust:struct:`FileLock ` of that state while reading it and writing it back.