Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

FastCopy: fix cancelled undo, resume false-skip/dirs, and Full-verify short reads (issue 864)

Ruchir3178f3c6d57385e
diff --git a/crates/rf-win/src/fastcopy.rs b/crates/rf-win/src/fastcopy.rs
index fd5e56cf..ff7499a4 100644
--- a/crates/rf-win/src/fastcopy.rs
+++ b/crates/rf-win/src/fastcopy.rs
@@ -630,6 +630,7 @@ fn run_job_inner(j: &Job, run: &Run) -> windows::core::Result<()> {
let _ = std::fs::remove_file(&tmp);
}
copy_or_resume(j, &tmp, run)?;
+ drop_wsl_zone(&tmp);
move_into_place(&tmp, &j.dst, true).inspect_err(|_| {
let _ = std::fs::remove_file(&tmp);
})
@@ -751,9 +752,23 @@ fn run_jobs(mut p: Plan, tracker: &Tracker, threads: usize, journal: Option<(&Jr
.into_iter()
.filter_map(|i| jobs[i].dst.parent().map(|d| (jobs[i].src.clone(), d.to_path_buf())))
.collect();
+ let done_indices = done.into_inner().unwrap();
+ let done_pairs: HashSet<(&Path, &Path)> = done_indices.iter().map(|&i| (jobs[i].src.as_path(), jobs[i].dst.as_path())).collect();
+ let job_pairs: HashSet<(&Path, &Path)> = jobs.iter().map(|j| (j.src.as_path(), j.dst.as_path())).collect();
+ let undo: Vec<(PathBuf, PathBuf)> = p
+ .undo
+ .into_iter()
+ .filter(|(s, d)| {
+ if job_pairs.contains(&(s.as_path(), d.as_path())) {
+ done_pairs.contains(&(s.as_path(), d.as_path()))
+ } else {
+ !cancelled || done_indices.iter().any(|&i| jobs[i].dst.starts_with(d))
+ }
+ })
+ .collect();
let mut copied = already;
- copied.extend(done.into_inner().unwrap().into_iter().filter(|&i| jobs[i].op == Op::Copy).map(|i| (jobs[i].src.clone(), jobs[i].dst.clone())));
- Outcome { undo: p.undo, failed, whole: p.whole, copied, src_dirs: p.src_dirs, cancelled }
+ copied.extend(done_indices.into_iter().filter(|&i| jobs[i].op == Op::Copy).map(|i| (jobs[i].src.clone(), jobs[i].dst.clone())));
+ Outcome { undo, failed, whole: p.whole, copied, src_dirs: p.src_dirs, cancelled }
}
/// Copies `sources` into `dest_dir` in parallel (conflicts as `tracker.ask` says; without it
@@ -1043,9 +1058,8 @@ pub fn discard(journal: &Path) {
}
/// Carries an interrupted job on with its original plan: no conflict is asked again. A file is
-/// skipped only when the journal says it finished, or its copy matches the source (size and
-/// time; byte for byte when the job verifies fully: that happens in `finish` for every file,
-/// skipped or not). Anything else at a destination is a partial copy from the cut, and is
+/// skipped only when the journal says it finished (and its copy is checked in `finish` for every
+/// file, skipped or not). Anything else at a destination is a partial copy from the cut, and is
/// replaced (through a temporary name, as always). Moves delete sources only after the check.
pub fn resume(journal: &Path, tracker: Arc<Tracker>) -> anyhow::Result<bool> {
let Some(l) = load(journal) else {
@@ -1095,7 +1109,7 @@ pub fn resume(journal: &Path, tracker: Arc<Tracker>) -> anyhow::Result<bool> {
let _ = std::fs::remove_file(&tmp);
continue; // a move that already deleted it: its copy checked out then
}
- if dst_there && (finished.contains(&i) || same_meta(&j.src, &j.dst)) {
+ if dst_there && finished.contains(&i) {
let _ = std::fs::remove_file(&tmp);
already.push((j.src.clone(), j.dst.clone()));
continue;
@@ -1113,6 +1127,13 @@ pub fn resume(journal: &Path, tracker: Arc<Tracker>) -> anyhow::Result<bool> {
index.push(i);
p.jobs.push(j);
}
+ if !l.rename && l.info.kind == Transfer::Move {
+ for s in &l.info.sources {
+ if s.is_dir() && !p.whole.contains(s) {
+ walk_dirs(s, &mut p.src_dirs);
+ }
+ }
+ }
// a clean journal (no torn tail) to go on recording into
let w = crate::journal::rewrite(journal, &l.records)?;
let jr = Jr { w: Mutex::new((w, Instant::now())), path: journal.to_path_buf() };
@@ -1203,10 +1224,29 @@ fn same_meta(a: &Path, b: &Path) -> bool {
}
}
+fn same_readers(fa: &mut impl std::io::Read, fb: &mut impl std::io::Read, ba: &mut [u8], bb: &mut [u8]) -> std::io::Result<bool> {
+ loop {
+ let n = fb.read(bb)?;
+ if n == 0 {
+ return Ok(fa.read(&mut ba[..1])? == 0);
+ }
+ let mut m = 0;
+ while m < n {
+ let k = fa.read(&mut ba[m..n])?;
+ if k == 0 {
+ return Ok(false);
+ }
+ m += k;
+ }
+ if ba[..n] != bb[..n] {
+ return Ok(false);
+ }
+ }
+}
+
/// Same bytes; the copy is read past the cache (what the disk holds, not what was just
/// written to memory).
fn same_bytes(a: &Path, b: &Path) -> std::io::Result<bool> {
- use std::io::Read;
use std::os::windows::fs::OpenOptionsExt;
const CHUNK: usize = 1 << 20;
const FILE_FLAG_NO_BUFFERING: u32 = 0x2000_0000;
@@ -1227,24 +1267,7 @@ fn same_bytes(a: &Path, b: &Path) -> std::io::Result<bool> {
}
let bb = unsafe { std::slice::from_raw_parts_mut(buf.0, CHUNK) };
let mut ba = vec![0u8; CHUNK];
- loop {
- let n = fb.read(bb)?;
- let mut m = 0;
- while m < n {
- let k = fa.read(&mut ba[m..n])?;
- if k == 0 {
- return Ok(false);
- }
- m += k;
- }
- if ba[..n] != bb[..n] {
- return Ok(false);
- }
- if n < CHUNK {
- // the copy ended: the source must too
- return Ok(fa.read(&mut ba[..1])? == 0);
- }
- }
+ same_readers(&mut fa, &mut fb, &mut ba, bb)
}
/// `inner` is `outer` or inside it, comparing like Windows does (case-insensitive, either
@@ -1863,4 +1886,95 @@ mod tests {
assert!(!absorb_no_shell(Err(crate::engine_guard::NoShellEngine { op: crate::engine_guard::Op::Copy }.into()), 3, &mut left).unwrap());
assert_eq!(left, 3);
}
+
+ #[test]
+ fn cancelled_replace_does_not_record_untouched_undo() {
+ let base = base("fastcopy-cancel-replace-undo");
+ let (src, dst) = (base.join("kaynak"), base.join("hedef"));
+ std::fs::create_dir_all(&src).unwrap();
+ std::fs::create_dir_all(&dst).unwrap();
+ std::fs::write(src.join("a.txt"), b"new").unwrap();
+ std::fs::write(dst.join("a.txt"), b"old").unwrap();
+ let mut t = answering(Resolve::Replace);
+ t.cancel.store(true, Ordering::Relaxed);
+ let t = Arc::new(t);
+ let ok = transfer(Transfer::Copy, &[src.join("a.txt")], &dst, t.clone()).unwrap();
+ assert!(!ok);
+ assert!(t.results.lock().unwrap().is_empty(), "cancelled replace must not record untouched undo pairs");
+ assert_eq!(std::fs::read(dst.join("a.txt")).unwrap(), b"old");
+ let _ = std::fs::remove_dir_all(&base);
+ }
+
+ #[test]
+ fn resume_does_not_skip_unrecorded_replace_with_same_meta() {
+ let base = base("fastcopy-resume-replace");
+ let (src, dst) = (base.join("kaynak"), base.join("hedef"));
+ std::fs::create_dir_all(&src).unwrap();
+ std::fs::create_dir_all(&dst).unwrap();
+ let src_file = src.join("a.txt");
+ let dst_file = dst.join("a.txt");
+ std::fs::write(&src_file, b"AAAA").unwrap();
+ std::fs::write(&dst_file, b"BBBB").unwrap();
+ let mtime = std::fs::metadata(&src_file).unwrap().modified().unwrap();
+ std::fs::File::options().write(true).open(&dst_file).unwrap().set_modified(mtime).unwrap();
+
+ set_journal_dir(std::env::temp_dir().join("rf-journals-test"));
+ let mut p = plan(std::slice::from_ref(&src_file), &dst, false);
+ let len = p.conflicts.len();
+ p.resolve(&vec![Resolve::Replace; len]);
+ let jr = journal_start(Transfer::Move, false, Verify::Quick, std::slice::from_ref(&src_file), &dst, &p).expect("journal");
+ let journal = jr.path.clone();
+ drop(jr);
+
+ assert!(resume(&journal, Arc::new(tracker())).unwrap());
+ assert_eq!(std::fs::read(&dst_file).unwrap(), b"AAAA", "unrecorded replace with same size+mtime must not be skipped");
+ assert!(!src_file.exists(), "move deletes verified source after replacing");
+ let _ = std::fs::remove_dir_all(&base);
+ }
+
+ #[test]
+ fn resume_cross_volume_move_removes_empty_source_dirs() {
+ let base = base("fastcopy-resume-dirs");
+ let (src, dst) = (base.join("kaynak"), base.join("hedef"));
+ std::fs::create_dir_all(src.join("sub")).unwrap();
+ std::fs::create_dir_all(&dst).unwrap();
+ std::fs::write(src.join("sub/f.txt"), b"data").unwrap();
+
+ set_journal_dir(std::env::temp_dir().join("rf-journals-test"));
+ let p = plan(std::slice::from_ref(&src), &dst, false);
+ let jr = journal_start(Transfer::Move, false, Verify::Quick, std::slice::from_ref(&src), &dst, &p).expect("journal");
+ let journal = jr.path.clone();
+ drop(jr);
+
+ assert!(resume(&journal, Arc::new(tracker())).unwrap());
+ assert_eq!(std::fs::read(dst.join("kaynak/sub/f.txt")).unwrap(), b"data");
+ assert!(!src.exists(), "resumed cross-volume move must remove empty source folders");
+ let _ = std::fs::remove_dir_all(&base);
+ }
+
+ #[test]
+ fn same_readers_handles_short_reads() {
+ struct ShortReader<'a> {
+ data: &'a [u8],
+ step: usize,
+ }
+ impl std::io::Read for ShortReader<'_> {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ let n = self.data.len().min(buf.len()).min(self.step);
+ buf[..n].copy_from_slice(&self.data[..n]);
+ self.data = &self.data[n..];
+ Ok(n)
+ }
+ }
+ let a: Vec<u8> = (0..100u8).collect();
+ let mut b_diff = a.clone();
+ b_diff[95] ^= 0xFF;
+ let mut ba = [0u8; 64];
+ let mut bb = [0u8; 64];
+
+ assert!(same_readers(&mut ShortReader { data: &a, step: 7 }, &mut ShortReader { data: &a, step: 13 }, &mut ba, &mut bb).unwrap());
+ assert!(!same_readers(&mut ShortReader { data: &a, step: 7 }, &mut ShortReader { data: &b_diff, step: 13 }, &mut ba, &mut bb).unwrap());
+ assert!(!same_readers(&mut ShortReader { data: &a, step: 7 }, &mut ShortReader { data: &a[..99], step: 13 }, &mut ba, &mut bb).unwrap());
+ assert!(!same_readers(&mut ShortReader { data: &a[..99], step: 7 }, &mut ShortReader { data: &a, step: 13 }, &mut ba, &mut bb).unwrap());
+ }
}