rename files across devices

This commit is contained in:
Brandon Thomas committed 2023-07-20 23:50:19 -04:00
1 parent 0534db0033
commit 8017fef05a
3 files changed
+35 -3

No files matched your search

+1
View File
@@ -24,3 +24,4 @@ pub mod file_exists;
pub mod file_size;
pub mod filename_concat;
pub mod path_to_string;
pub mod rename_across_devices;
@@ -0,0 +1,31 @@
use std::path::Path;
/// Rename a file.
/// Ordinary rename will fail in Linux if it is across physical devices.
/// This function will perform a copy followed by delete in that case.
pub fn rename_across_devices<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> std::io::Result<()> {
let result = std::fs::rename(&from, &to);
let err = match result.as_ref() {
Ok(_) => return Ok(()), // Rename succeeded.
Err(err) => err,
};
match err.raw_os_error() {
Some(18) => {
// NO-OP: pass-through
// NB: We can clean this up in the future when `err.kind() == ErrorKind::CrossDevices`
// stabilizes. For now we can assume code "18" means we're trying to rename across devices.
}
_ => {
// Something else happened. Return original error.
return result;
}
}
let _num_bytes = std::fs::copy(&from, &to)?;
std::fs::remove_file(&from)?;
Ok(())
}
@@ -1,13 +1,13 @@
use anyhow::anyhow;
use cloud_storage::bucket_client::BucketClient;
use container_common::filesystem::safe_delete_temp_directory::safe_delete_temp_directory;
use crate::util::scoped_temp_dir_creator::ScopedTempDirCreator;
use errors::AnyhowResult;
use filesys::create_dir_all_if_missing::create_dir_all_if_missing;
use filesys::file_exists::file_exists;
use log::{error, info};
use std::path::PathBuf;
use tempdir::TempDir;
use crate::util::scoped_temp_dir_creator::ScopedTempDirCreator;
use filesys::rename_across_devices::rename_across_devices;
#[derive(Clone)]
pub struct PretrainedHubertModel {
@@ -71,7 +71,7 @@ impl PretrainedHubertModel {
info!("Renaming hubert file from {:?} to {:?}!", &temp_path, &self.filesystem_path);
std::fs::rename(&temp_path, &self.filesystem_path)
rename_across_devices(&temp_path, &self.filesystem_path)
.map_err(|e| {
error!("could rename hubert model on disk: {:?}", e);
safe_delete_temp_directory(&temp_dir);