pub struct AtomicFileWriter { /* private fields */ }Expand description
An atomic file writer that writes to a temporary file and renames on completion.
If the writer is dropped without calling finish(),
the temporary file is removed (best-effort cleanup).
Implementations§
Source§impl AtomicFileWriter
impl AtomicFileWriter
Sourcepub fn new(dest: impl AsRef<Path>) -> Result<Self>
pub fn new(dest: impl AsRef<Path>) -> Result<Self>
Create a new atomic writer targeting dest.
The temporary file is created with a random suffix in the same
directory as dest, using O_CREAT | O_EXCL to prevent
symlink-following attacks on shared filesystems.
§Errors
Returns an I/O error if the temporary file cannot be created.
Sourcepub fn new_private(dest: impl AsRef<Path>) -> Result<Self>
pub fn new_private(dest: impl AsRef<Path>) -> Result<Self>
Like new, but restricts the temp file (and therefore
the renamed destination) to owner-read/write (0600) on Unix.
Use this when writing files that contain sensitive material such as
plaintext secrets, so that the data is never world-readable — even
during the brief window between the initial open and the rename.
§Errors
Returns an error if the temporary file cannot be created or its permissions cannot be set.
Sourcepub fn finish(self) -> Result<()>
pub fn finish(self) -> Result<()>
Flush all buffers, fsync, and atomically rename to the final destination.
§Errors
Returns an I/O error if flush, sync, or rename fails. On error, the temporary file is cleaned up on a best-effort basis.
Trait Implementations§
Source§impl Drop for AtomicFileWriter
impl Drop for AtomicFileWriter
Source§impl Seek for AtomicFileWriter
impl Seek for AtomicFileWriter
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)Source§impl Write for AtomicFileWriter
impl Write for AtomicFileWriter
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)Auto Trait Implementations§
impl Freeze for AtomicFileWriter
impl RefUnwindSafe for AtomicFileWriter
impl Send for AtomicFileWriter
impl Sync for AtomicFileWriter
impl Unpin for AtomicFileWriter
impl UnsafeUnpin for AtomicFileWriter
impl UnwindSafe for AtomicFileWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more