pub trait CapStdExtDirExt {
    fn open_optional(&self, path: impl AsRef<Path>) -> Result<Option<File>>;
    fn open_dir_optional(&self, path: impl AsRef<Path>) -> Result<Option<Dir>>;
    fn ensure_dir_with(
        &self,
        p: impl AsRef<Path>,
        builder: &DirBuilder
    ) -> Result<bool>; fn metadata_optional(
        &self,
        path: impl AsRef<Path>
    ) -> Result<Option<Metadata>>; fn symlink_metadata_optional(
        &self,
        path: impl AsRef<Path>
    ) -> Result<Option<Metadata>>; fn remove_file_optional(&self, path: impl AsRef<Path>) -> Result<bool>; fn atomic_replace_with<F, T, E>(
        &self,
        destname: impl AsRef<Path>,
        f: F
    ) -> Result<T, E>
    where
        F: FnOnce(&mut BufWriter<TempFile<'_>>) -> Result<T, E>,
        E: From<Error>
; fn atomic_write(
        &self,
        destname: impl AsRef<Path>,
        contents: impl AsRef<[u8]>
    ) -> Result<()>; fn atomic_write_with_perms(
        &self,
        destname: impl AsRef<Path>,
        contents: impl AsRef<[u8]>,
        perms: Permissions
    ) -> Result<()>; }
Expand description

Extension trait for [cap_std::fs::Dir]

Required Methods

Open a file read-only, but return Ok(None) if it does not exist.

Open a directory, but return Ok(None) if it does not exist.

Create the target directory, but do nothing if a directory already exists at that path. The return value will be true if the directory was created. An error will be returned if the path is a non-directory. Symbolic links will be followed.

Gather metadata, but return Ok(None) if it does not exist.

Gather metadata (but do not follow symlinks), but return Ok(None) if it does not exist.

Remove (delete) a file, but return Ok(false) if the file does not exist.

Atomically write a file by calling the provided closure.

This uses cap_tempfile::TempFile, which is wrapped in a std::io::BufWriter and passed to the closure.

The closure may also perform other file operations beyond writing, such as changing file permissions:

use cap_std_ext::prelude::*;
let contents = b"hello world\n";
somedir.atomic_replace_with("somefilename", |f| -> io::Result<_> {
    f.write_all(contents)?;
    f.flush()?;
    use std::os::unix::prelude::PermissionsExt;
    let perms = cap_std::fs::Permissions::from_mode(0o600);
    f.get_mut().as_file_mut().set_permissions(perms)?;
    Ok(())
})

Any existing file will be replaced.

Atomically write the provided contents to a file.

Atomically write the provided contents to a file, using specified permissions.

Implementations on Foreign Types

Implementors