Struct tempfile_fast::Sponge [−][src]
pub struct Sponge { /* fields omitted */ }
Expand description
A safer abstraction for atomic overwrites of files.
A Sponge
will “soak up” writes, and eventually, when you’re ready, write them to the destination file.
This is atomic, so the destination file will never be left in an intermediate state. This is
error, panic, and crash safe.
Ownership and permission is preserved, where appropriate for the platform.
Space is needed to soak up these writes: If you are overwriting a large file, you may need disk space for the entire file to be stored twice.
For performance and correctness reasons, many of the things that can go wrong will go wrong at
commit()
time, not on creation. This might not be what you want if you are doing a very
expensive operation. Most of the failures are permissions errors, however. If you are operating
as a single user inside the user’s directory, the chance of failure (except for disk space) is
negligible.
Example
let mut temp = tempfile_fast::Sponge::new_for("example.txt").unwrap();
temp.write_all(b"hello").unwrap();
temp.commit().unwrap();
Implementations
Create a Sponge
which will eventually overwrite the named file.
The file does not have to exist.
This will be resolved to an absolute path relative to the current directory immediately.
The path is not run through fs::canonicalize
, so other oddities will resolve
at commit()
time. Notably, a symlink
(or hardlink
, or reflink
) will be converted
into a regular file, using the target’s fs::metadata
.
Intermediate directories will be created using the platform defaults (e.g. permissions), if this is not what you want, create them in advance.
Write the Sponge
out to the destination file.
Ownership and permission is preserved, where appropriate for the platform. The permissions
and ownership are resolved now, using the (absolute) path provided. i.e. changes to the
destination’s file’s permissions since the creation of the Sponge
will be included.
The aim is to transfer all ownership and permission information, but not timestamps. The implementation, and what information is transferred, is subject to change in minor versions.
The file is flush()
ed correctly, but not fsync()
’d. The update is atomic against
anything that happens to the current process, including erroring, panicking, or crashing.
If you need the update to survive power loss, or OS/kernel issues, you should additionally
follow the platform recommendations for fsync()
, which may involve calling fsync()
on
at least the new file, and probably on the parent directory. Note that this is the same as
every other file API, but is being called out here as a reminder, if you are building
certain types of application.
Platform-specific behavior
Metadata:
unix
(includinglinux
): At leastchown(uid, gid)
andchmod(mode_t)
windows
: At least thereadonly
flag.- all: See
fs::set_permissions
Error
If any underlying operation fails the system error will be returned directly. This method
consumes self
, so these errors are not recoverable. Failing to set the ownership
information on the temporary file is an error, not ignored, unlike in many implementations.
Trait Implementations
A Sponge
is a BufWriter
.
write
to the intermediate file, without touching the destination.
flush
to the intermediate file, without touching the destination.
This has no real purpose, as these writes should not be observable.
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
Attempts to write an entire buffer into this writer. Read more
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more