Ensure skip applies to cleanup operations, fixes #854

This commit is contained in:
Jeremy Soller 2025-03-07 09:29:48 -07:00
parent 63ab36af8c
commit 40a02bb2fc
No known key found for this signature in database
GPG key ID: 670FDFB5428E05CA

View file

@ -1,9 +1,11 @@
use std::{ use std::{
cell::Cell,
error::Error, error::Error,
fs, fs,
io::{Read, Write}, io::{Read, Write},
ops::ControlFlow, ops::ControlFlow,
path::PathBuf, path::PathBuf,
rc::Rc,
}; };
use walkdir::WalkDir; use walkdir::WalkDir;
@ -88,7 +90,12 @@ impl Context {
//TODO: ensure to is inside of to_parent? //TODO: ensure to is inside of to_parent?
to_parent.join(relative) to_parent.join(relative)
}; };
let op = Op { kind, from, to }; let op = Op {
kind,
from,
to,
skipped: Rc::new(Cell::new(false)),
};
if moving { if moving {
if let Some(cleanup_op) = op.move_cleanup_op() { if let Some(cleanup_op) = op.move_cleanup_op() {
cleanup_ops.push(cleanup_op); cleanup_ops.push(cleanup_op);
@ -166,6 +173,7 @@ impl Context {
if apply_to_all { if apply_to_all {
self.replace_result_opt = Some(replace_result); self.replace_result_opt = Some(replace_result);
} }
op.skipped.set(true);
Ok(ControlFlow::Break(true)) Ok(ControlFlow::Break(true))
} }
ReplaceResult::Cancel => Ok(ControlFlow::Break(false)), ReplaceResult::Cancel => Ok(ControlFlow::Break(false)),
@ -196,6 +204,7 @@ pub struct Op {
pub kind: OpKind, pub kind: OpKind,
pub from: PathBuf, pub from: PathBuf,
pub to: PathBuf, pub to: PathBuf,
pub skipped: Rc<Cell<bool>>,
} }
impl Op { impl Op {
@ -210,10 +219,14 @@ impl Op {
from: self.from.clone(), from: self.from.clone(),
//TODO: it is strange to have `to` here //TODO: it is strange to have `to` here
to: self.to.clone(), to: self.to.clone(),
skipped: self.skipped.clone(),
}) })
} }
fn run(&mut self, ctx: &mut Context, mut progress: Progress) -> Result<bool, Box<dyn Error>> { fn run(&mut self, ctx: &mut Context, mut progress: Progress) -> Result<bool, Box<dyn Error>> {
if self.skipped.get() {
return Ok(true);
}
match self.kind { match self.kind {
OpKind::Copy => { OpKind::Copy => {
let mut from_file = fs::OpenOptions::new().read(true).open(&self.from)?; let mut from_file = fs::OpenOptions::new().read(true).open(&self.from)?;
@ -273,8 +286,9 @@ impl Op {
kind: OpKind::Copy, kind: OpKind::Copy,
from: self.from.clone(), from: self.from.clone(),
to: self.to.clone(), to: self.to.clone(),
skipped: self.skipped.clone(),
}; };
copy_op.run(ctx, progress)?; return copy_op.run(ctx, progress);
} else { } else {
return Err(err.into()); return Err(err.into());
} }