Function resymgen::persist_named_temp_file_safe
source · pub fn persist_named_temp_file_safe<P: AsRef<Path>>(
file: NamedTempFile,
new_path: P
) -> Result<(), PersistError>
Expand description
Persist the temporary file at the target path.
This wraps NamedTempFile::persist()
with fallback to manual copying.
On some systems, the normal persist()
might not always work. For example, on Unix
systems persist()
ultimately relies on std::fs::rename()
:
https://github.com/Stebalien/tempfile/blob/66aa57f7d3a38234fcd393077366b61d36171e42/src/file/imp/unix.rs#L131
which will fail if the tempfile and target path are not on the same filesystem:
https://doc.rust-lang.org/std/fs/fn.rename.html. Since /tmp
(the normal return value of
std::env::temp_dir()
on Linux) can sometimes be mounted on a separate filesystem, this is
a fairly common case.
If the normal persist()
method fails, try manually copying the tempfile to the destination.
Unlike std::fs::rename()
, std::fs::copy()
works even across filesystems.