pub struct AtomicWrite<'a> { /* private fields */ }Expand description
One atomic file write and its options: write_atomic is the default form.
let secret = AtomicWrite::new(b"token_secret = \"...\"\n");
#[cfg(unix)]
let secret = secret.mode(0o600);
match secret.new_only().write(Path::new("credentials.toml")) {
Err(error) if error.target_exists() => { /* keep the file that is there */ }
other => other?,
}By default the write replaces a regular file and keeps that file’s own permissions; a new file starts owner-only on Unix. The options:
modesets explicit permission bits on the new file, over either default. Unix only: Windows has no mode bits, so the method does not exist there and a caller that needs one writes thecfg(unix)itself rather than have the request silently ignored. A new Windows file takes the directory’s inherited ACL.new_onlyrefuses an existing target of any kind. The target is never touched; the refusal isAtomicError::target_exists.
Every form keeps the full sequence: private same-directory temporary file, write, permissions, file fsync, atomic install, directory fsync.
Implementations§
Source§impl<'a> AtomicWrite<'a>
impl<'a> AtomicWrite<'a>
Sourcepub fn new(bytes: &'a [u8]) -> Self
pub fn new(bytes: &'a [u8]) -> Self
A write of the complete new contents with the default behavior.
Sourcepub fn mode(self, bits: u32) -> Self
pub fn mode(self, bits: u32) -> Self
Give the new file these permission bits (0o600), overriding both the
replaced file’s permissions and the owner-only default. Unix only.
Sourcepub fn new_only(self) -> Self
pub fn new_only(self) -> Self
Refuse an existing target instead of replacing it. The refusal is
reported by AtomicError::target_exists, including when the name is
taken between the check and the install.
Sourcepub fn write(self, path: &Path) -> Result<(), AtomicError>
pub fn write(self, path: &Path) -> Result<(), AtomicError>
Write self to path. Every failure before the install leaves the
original untouched and removes the temporary file. A failure at the
final parent directory fsync — the one AtomicError::commit_uncertain
reports — means the new file may already be in place without confirmed
durability.