pub struct Move<'a> { /* private fields */ }
Expand description
Moves a file from the given path to the specified destination.
source
and dest
must be on the same filesystem.
If replace_using_temp
is specified, the destination file will be
replaced using the given temporary path.
- Errors:
- Io - copying / renaming
Implementations§
source§impl<'a> Move<'a>
impl<'a> Move<'a>
sourcepub fn from_source(source: &'a Path) -> Move<'a>
pub fn from_source(source: &'a Path) -> Move<'a>
Specify source file
Examples found in repository?
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
fn copy_files_and_run<R: Read + Seek>(archive_buffer: R, extract_path: &Path) -> Result {
let mut extracted_files: Vec<PathBuf> = Vec::new();
// extract the buffer to the tmp_dir
// we extract our signed archive into our final directory without any temp file
let mut extractor =
Extract::from_cursor(archive_buffer, ArchiveFormat::Tar(Some(Compression::Gz)));
// the first file in the tar.gz will always be
// <app_name>/Contents
let tmp_dir = tempfile::Builder::new()
.prefix("tauri_current_app")
.tempdir()?;
// create backup of our current app
Move::from_source(extract_path).to_dest(tmp_dir.path())?;
// extract all the files
extractor.with_files(|entry| {
let path = entry.path()?;
// skip the first folder (should be the app name)
let collected_path: PathBuf = path.iter().skip(1).collect();
let extraction_path = extract_path.join(collected_path);
// if something went wrong during the extraction, we should restore previous app
if let Err(err) = entry.extract(&extraction_path) {
for file in &extracted_files {
// delete all the files we extracted
if file.is_dir() {
std::fs::remove_dir(file)?;
} else {
std::fs::remove_file(file)?;
}
}
Move::from_source(tmp_dir.path()).to_dest(extract_path)?;
return Err(crate::api::Error::Extract(err.to_string()));
}
extracted_files.push(extraction_path);
Ok(false)
})?;
let _ = std::process::Command::new("touch")
.arg(&extract_path)
.status();
Ok(())
}
sourcepub fn replace_using_temp(&mut self, temp: &'a Path) -> &mut Self
pub fn replace_using_temp(&mut self, temp: &'a Path) -> &mut Self
If specified and the destination file already exists, the “destination” file will be moved to the given temporary location before the “source” file is moved to the “destination” file.
In the event of an io
error while renaming “source” to “destination”,
the temporary file will be moved back to “destination”.
The temp
dir must be explicitly provided since rename
operations require
files to live on the same filesystem.
sourcepub fn to_dest(&self, dest: &Path) -> Result<()>
pub fn to_dest(&self, dest: &Path) -> Result<()>
Move source file to specified destination (replace whole directory)
Examples found in repository?
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
fn copy_files_and_run<R: Read + Seek>(archive_buffer: R, extract_path: &Path) -> Result {
let mut extracted_files: Vec<PathBuf> = Vec::new();
// extract the buffer to the tmp_dir
// we extract our signed archive into our final directory without any temp file
let mut extractor =
Extract::from_cursor(archive_buffer, ArchiveFormat::Tar(Some(Compression::Gz)));
// the first file in the tar.gz will always be
// <app_name>/Contents
let tmp_dir = tempfile::Builder::new()
.prefix("tauri_current_app")
.tempdir()?;
// create backup of our current app
Move::from_source(extract_path).to_dest(tmp_dir.path())?;
// extract all the files
extractor.with_files(|entry| {
let path = entry.path()?;
// skip the first folder (should be the app name)
let collected_path: PathBuf = path.iter().skip(1).collect();
let extraction_path = extract_path.join(collected_path);
// if something went wrong during the extraction, we should restore previous app
if let Err(err) = entry.extract(&extraction_path) {
for file in &extracted_files {
// delete all the files we extracted
if file.is_dir() {
std::fs::remove_dir(file)?;
} else {
std::fs::remove_file(file)?;
}
}
Move::from_source(tmp_dir.path()).to_dest(extract_path)?;
return Err(crate::api::Error::Extract(err.to_string()));
}
extracted_files.push(extraction_path);
Ok(false)
})?;
let _ = std::process::Command::new("touch")
.arg(&extract_path)
.status();
Ok(())
}
sourcepub fn walk_to_dest(&self, dest: &Path) -> Result<()>
pub fn walk_to_dest(&self, dest: &Path) -> Result<()>
Walk in the source and copy all files and create directories if needed by replacing existing elements. (equivalent to a cp -R)