From 6fa890e3f32975bee46689eff5b23b409f53e637 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 29 Apr 2025 19:12:33 -0600 Subject: [PATCH] Attempt atomic renames when moving top-level files and folders, fixes #607 --- src/operation/mod.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/operation/mod.rs b/src/operation/mod.rs index e2f9352..db9d8df 100644 --- a/src/operation/mod.rs +++ b/src/operation/mod.rs @@ -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()); {