Attempt atomic renames when moving top-level files and folders, fixes #607

This commit is contained in:
Jeremy Soller 2025-04-29 19:12:33 -06:00
parent dd5fa29e9a
commit 6fa890e3f3
No known key found for this signature in database
GPG key ID: 670FDFB5428E05CA

View file

@ -285,7 +285,7 @@ async fn copy_or_move(
);
// Handle duplicate file names by renaming paths
let from_to_pairs: Vec<(PathBuf, PathBuf)> = paths
let mut from_to_pairs: Vec<(PathBuf, PathBuf)> = paths
.into_iter()
.zip(std::iter::repeat(to.as_path()))
.filter_map(|(from, to)| {
@ -306,6 +306,24 @@ async fn copy_or_move(
})
.collect();
// Attempt quick and simple renames
//TODO: allow rename to be used for directories in recursive context?
if matches!(method, Method::Move { .. }) {
from_to_pairs.retain(|(from, to)| {
//TODO: use compio::fs::rename?
match fs::rename(from, to) {
Ok(()) => {
log::info!("renamed {from:?} to {to:?}");
false
},
Err(err) => {
log::info!("failed to rename {from:?} to {to:?}, fallback to recursive move: {err}");
true
}
}
});
}
let mut context = Context::new(controller.clone());
{