Fixes: file over folder with Overwrite, CRLF .directory files, stray blank line after removing an added group. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
702 lines
23 KiB
Rust
702 lines
23 KiB
Rust
//! The job engine on real files in temp dirs: conflicts, errors, cancel, cross-device moves.
|
|
|
|
use std::fs;
|
|
use std::os::unix::fs::{MetadataExt, PermissionsExt};
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::{Arc, mpsc};
|
|
use std::time::Duration;
|
|
|
|
use tunal_core::ops::{
|
|
ConflictAction, ConflictChoice, ErrorAction, JobEvent, JobId, JobKind, Jobs, Report,
|
|
auto_rename, is_inside, numbered,
|
|
};
|
|
use tunal_core::{LocalLocation, Locations, Uri};
|
|
|
|
fn uri(p: &Path) -> Uri {
|
|
Uri::from_path(p)
|
|
}
|
|
|
|
type ErrorHook = Box<dyn FnMut(&Uri)>;
|
|
|
|
/// What the "UI" answers while a job runs.
|
|
struct Policy {
|
|
conflict: ConflictChoice,
|
|
error: ErrorAction,
|
|
/// Called with the failing path before the error answer is sent.
|
|
on_error: Option<ErrorHook>,
|
|
/// Cancel at the first conflict instead of answering.
|
|
cancel_on_conflict: bool,
|
|
/// Cancel once bytes were copied.
|
|
cancel_on_bytes: bool,
|
|
}
|
|
|
|
impl Policy {
|
|
fn new(action: ConflictAction) -> Self {
|
|
Self {
|
|
conflict: ConflictChoice {
|
|
action,
|
|
apply_to_all: false,
|
|
},
|
|
error: ErrorAction::Skip,
|
|
on_error: None,
|
|
cancel_on_conflict: false,
|
|
cancel_on_bytes: false,
|
|
}
|
|
}
|
|
|
|
fn all(mut self) -> Self {
|
|
self.conflict.apply_to_all = true;
|
|
self
|
|
}
|
|
}
|
|
|
|
struct Outcome {
|
|
report: Report,
|
|
conflicts: usize,
|
|
errors: usize,
|
|
}
|
|
|
|
struct Engine {
|
|
jobs: Jobs,
|
|
events: mpsc::Receiver<(JobId, JobEvent)>,
|
|
slow: Arc<AtomicBool>,
|
|
}
|
|
|
|
fn engine() -> Engine {
|
|
let (tx, events) = mpsc::channel();
|
|
let slow = Arc::new(AtomicBool::new(false));
|
|
let slow_sink = slow.clone();
|
|
let jobs = Jobs::new(Locations::with_local(), move |id, event| {
|
|
let progress = matches!(event, JobEvent::Progress(_));
|
|
let _ = tx.send((id, event));
|
|
// A busy UI: lets a cancel land in the middle of a big file.
|
|
if progress && slow_sink.load(Ordering::Relaxed) {
|
|
std::thread::sleep(Duration::from_millis(150));
|
|
}
|
|
});
|
|
Engine { jobs, events, slow }
|
|
}
|
|
|
|
impl Engine {
|
|
fn run(&self, kind: JobKind, mut policy: Policy) -> Outcome {
|
|
let handle = self.jobs.submit(kind);
|
|
let (mut conflicts, mut errors) = (0, 0);
|
|
for (id, event) in &self.events {
|
|
if id != handle.id() {
|
|
continue;
|
|
}
|
|
match event {
|
|
JobEvent::Progress(p) if policy.cancel_on_bytes && p.done_bytes > 0 => {
|
|
handle.cancel();
|
|
}
|
|
JobEvent::Progress(_) => {}
|
|
JobEvent::Conflict(request) => {
|
|
conflicts += 1;
|
|
if policy.cancel_on_conflict {
|
|
handle.cancel();
|
|
continue;
|
|
}
|
|
request.reply.send(policy.conflict.clone());
|
|
}
|
|
JobEvent::Error(request) => {
|
|
errors += 1;
|
|
if let Some(f) = policy.on_error.as_mut() {
|
|
f(&request.uri);
|
|
}
|
|
request.reply.send(policy.error);
|
|
}
|
|
JobEvent::Finished(report) => {
|
|
return Outcome {
|
|
report,
|
|
conflicts,
|
|
errors,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
panic!("job worker stopped");
|
|
}
|
|
}
|
|
|
|
fn read(p: impl AsRef<Path>) -> String {
|
|
fs::read_to_string(p).unwrap()
|
|
}
|
|
|
|
fn names(dir: &Path) -> Vec<String> {
|
|
let mut v: Vec<String> = fs::read_dir(dir)
|
|
.unwrap()
|
|
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
|
.collect();
|
|
v.sort();
|
|
v
|
|
}
|
|
|
|
/// `src/{a,b,c}.txt = "src <name>"` and `dst/{a,b,c}.txt = "dst <name>"`.
|
|
fn conflicted() -> (tempfile::TempDir, PathBuf, PathBuf) {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir(&src).unwrap();
|
|
fs::create_dir(&dst).unwrap();
|
|
for n in ["a", "b", "c"] {
|
|
fs::write(src.join(format!("{n}.txt")), format!("src {n}")).unwrap();
|
|
fs::write(dst.join(format!("{n}.txt")), format!("dst {n}")).unwrap();
|
|
}
|
|
(dir, src, dst)
|
|
}
|
|
|
|
fn copy_all(src: &Path, dst: &Path) -> JobKind {
|
|
JobKind::Copy {
|
|
sources: ["a", "b", "c"]
|
|
.iter()
|
|
.map(|n| uri(&src.join(format!("{n}.txt"))))
|
|
.collect(),
|
|
dest: uri(dst),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn copy_without_conflict_reports_totals_and_keeps_mtime() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(src.join("sub")).unwrap();
|
|
fs::create_dir(&dst).unwrap();
|
|
fs::write(src.join("a.txt"), "hello").unwrap();
|
|
fs::write(src.join("sub/b.txt"), "abc").unwrap();
|
|
std::os::unix::fs::symlink("b.txt", src.join("sub/link")).unwrap();
|
|
let old = std::time::SystemTime::now() - Duration::from_secs(86_400 * 30);
|
|
fs::File::options()
|
|
.write(true)
|
|
.open(src.join("a.txt"))
|
|
.unwrap()
|
|
.set_modified(old)
|
|
.unwrap();
|
|
let e = engine();
|
|
let out = e.run(
|
|
JobKind::Copy {
|
|
sources: vec![uri(&src.join("a.txt")), uri(&src.join("sub"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert!(out.report.failures.is_empty() && !out.report.cancelled);
|
|
assert_eq!(out.conflicts, 0);
|
|
assert_eq!(out.report.done_bytes, 8);
|
|
assert_eq!(read(dst.join("a.txt")), "hello");
|
|
assert_eq!(read(dst.join("sub/b.txt")), "abc");
|
|
assert_eq!(
|
|
fs::read_link(dst.join("sub/link")).unwrap(),
|
|
Path::new("b.txt")
|
|
);
|
|
assert_eq!(
|
|
fs::metadata(dst.join("a.txt")).unwrap().modified().unwrap(),
|
|
old
|
|
);
|
|
// The source is untouched.
|
|
assert!(src.join("a.txt").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn conflict_skip() {
|
|
let (_dir, src, dst) = conflicted();
|
|
let out = engine().run(copy_all(&src, &dst), Policy::new(ConflictAction::Skip));
|
|
assert_eq!(out.conflicts, 3);
|
|
assert_eq!(read(dst.join("a.txt")), "dst a");
|
|
assert_eq!(names(&dst).len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn conflict_overwrite() {
|
|
let (_dir, src, dst) = conflicted();
|
|
let out = engine().run(copy_all(&src, &dst), Policy::new(ConflictAction::Overwrite));
|
|
assert_eq!(out.conflicts, 3);
|
|
assert_eq!(read(dst.join("b.txt")), "src b");
|
|
assert_eq!(names(&dst).len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn conflict_auto_rename() {
|
|
let (_dir, src, dst) = conflicted();
|
|
let out = engine().run(
|
|
copy_all(&src, &dst),
|
|
Policy::new(ConflictAction::AutoRename),
|
|
);
|
|
assert_eq!(out.conflicts, 3);
|
|
assert_eq!(read(dst.join("a.txt")), "dst a");
|
|
assert_eq!(read(dst.join("a (2).txt")), "src a");
|
|
assert_eq!(read(dst.join("c (2).txt")), "src c");
|
|
}
|
|
|
|
#[test]
|
|
fn conflict_rename_asks_again_when_the_new_name_exists() {
|
|
let (_dir, src, dst) = conflicted();
|
|
// "b.txt" is taken too, so the engine asks a second time.
|
|
let kind = JobKind::Copy {
|
|
sources: vec![uri(&src.join("a.txt"))],
|
|
dest: uri(&dst),
|
|
};
|
|
let e = engine();
|
|
let handle = e.jobs.submit(kind);
|
|
let mut answers = vec![b"b.txt".to_vec(), b"fresh.txt".to_vec()].into_iter();
|
|
for (id, event) in &e.events {
|
|
assert_eq!(id, handle.id());
|
|
match event {
|
|
JobEvent::Conflict(request) => request.reply.send(ConflictChoice {
|
|
action: ConflictAction::Rename(answers.next().expect("asked twice")),
|
|
apply_to_all: true,
|
|
}),
|
|
JobEvent::Finished(report) => {
|
|
assert!(report.failures.is_empty());
|
|
break;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
assert!(answers.next().is_none());
|
|
assert_eq!(read(dst.join("fresh.txt")), "src a");
|
|
assert_eq!(read(dst.join("b.txt")), "dst b");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_to_all_asks_once() {
|
|
let (_dir, src, dst) = conflicted();
|
|
let out = engine().run(
|
|
copy_all(&src, &dst),
|
|
Policy::new(ConflictAction::Overwrite).all(),
|
|
);
|
|
assert_eq!(out.conflicts, 1);
|
|
for n in ["a", "b", "c"] {
|
|
assert_eq!(read(dst.join(format!("{n}.txt"))), format!("src {n}"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unanswered_conflict_means_skip() {
|
|
let (_dir, src, dst) = conflicted();
|
|
let e = engine();
|
|
let handle = e.jobs.submit(copy_all(&src, &dst));
|
|
for (id, event) in &e.events {
|
|
assert_eq!(id, handle.id());
|
|
match event {
|
|
JobEvent::Conflict(request) => drop(request.reply),
|
|
JobEvent::Finished(report) => {
|
|
assert!(!report.cancelled);
|
|
break;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
assert_eq!(read(dst.join("a.txt")), "dst a");
|
|
}
|
|
|
|
#[test]
|
|
fn folder_onto_folder_merges() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(src.join("f")).unwrap();
|
|
fs::create_dir_all(dst.join("f")).unwrap();
|
|
fs::write(src.join("f/new.txt"), "new").unwrap();
|
|
fs::write(src.join("f/both.txt"), "src").unwrap();
|
|
fs::write(dst.join("f/both.txt"), "dst").unwrap();
|
|
fs::write(dst.join("f/only-dst.txt"), "keep").unwrap();
|
|
let out = engine().run(
|
|
JobKind::Copy {
|
|
sources: vec![uri(&src.join("f"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Overwrite).all(),
|
|
);
|
|
assert!(out.report.failures.is_empty());
|
|
assert_eq!(
|
|
names(&dst.join("f")),
|
|
["both.txt", "new.txt", "only-dst.txt"]
|
|
);
|
|
assert_eq!(read(dst.join("f/both.txt")), "src");
|
|
}
|
|
|
|
#[test]
|
|
fn file_over_folder_overwrite_replaces_it() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(&src).unwrap();
|
|
fs::create_dir_all(dst.join("x/inner")).unwrap();
|
|
fs::write(src.join("x"), "file").unwrap();
|
|
engine().run(
|
|
JobKind::Copy {
|
|
sources: vec![uri(&src.join("x"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Overwrite),
|
|
);
|
|
assert_eq!(read(dst.join("x")), "file");
|
|
}
|
|
|
|
#[test]
|
|
fn copy_into_own_folder_makes_numbered_copies_without_asking() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
fs::write(dir.path().join("a.txt"), "a").unwrap();
|
|
let kind = || JobKind::Copy {
|
|
sources: vec![uri(&dir.path().join("a.txt"))],
|
|
dest: uri(dir.path()),
|
|
};
|
|
let e = engine();
|
|
let first = e.run(kind(), Policy::new(ConflictAction::Skip));
|
|
let second = e.run(kind(), Policy::new(ConflictAction::Skip));
|
|
assert_eq!(first.conflicts + second.conflicts, 0);
|
|
assert_eq!(names(dir.path()), ["a (2).txt", "a (3).txt", "a.txt"]);
|
|
}
|
|
|
|
#[test]
|
|
fn copy_folder_into_itself_is_refused() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let src = dir.path().join("src");
|
|
fs::create_dir_all(src.join("sub")).unwrap();
|
|
fs::write(src.join("a.txt"), "a").unwrap();
|
|
for dest in [src.clone(), src.join("sub")] {
|
|
for mv in [false, true] {
|
|
let sources = vec![uri(&src)];
|
|
let kind = if mv {
|
|
JobKind::Move {
|
|
sources,
|
|
dest: uri(&dest),
|
|
}
|
|
} else {
|
|
JobKind::Copy {
|
|
sources,
|
|
dest: uri(&dest),
|
|
}
|
|
};
|
|
let out = engine().run(kind, Policy::new(ConflictAction::Skip));
|
|
// Moving a folder onto its own parent is a no-op, not an error.
|
|
if !(mv && dest == src.parent().unwrap()) {
|
|
assert_eq!(out.report.failures.len(), 1, "dest {dest:?} move {mv}");
|
|
}
|
|
}
|
|
}
|
|
assert_eq!(names(&src), ["a.txt", "sub"]);
|
|
assert!(names(&src.join("sub")).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn move_removes_the_source_and_merges() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(src.join("f")).unwrap();
|
|
fs::create_dir_all(dst.join("f")).unwrap();
|
|
fs::write(src.join("f/a.txt"), "src").unwrap();
|
|
fs::write(src.join("f/b.txt"), "b").unwrap();
|
|
fs::write(dst.join("f/a.txt"), "dst").unwrap();
|
|
fs::write(src.join("solo.txt"), "solo").unwrap();
|
|
let out = engine().run(
|
|
JobKind::Move {
|
|
sources: vec![uri(&src.join("f")), uri(&src.join("solo.txt"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Overwrite).all(),
|
|
);
|
|
assert!(out.report.failures.is_empty());
|
|
assert_eq!(read(dst.join("f/a.txt")), "src");
|
|
assert_eq!(read(dst.join("f/b.txt")), "b");
|
|
assert_eq!(read(dst.join("solo.txt")), "solo");
|
|
assert!(!src.join("f").exists() && !src.join("solo.txt").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn skipped_folder_conflict_leaves_both_sides_alone() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(src.join("f")).unwrap();
|
|
fs::create_dir_all(dst.join("f")).unwrap();
|
|
fs::write(src.join("f/a.txt"), "src").unwrap();
|
|
fs::write(dst.join("f/b.txt"), "dst").unwrap();
|
|
let out = engine().run(
|
|
JobKind::Move {
|
|
sources: vec![uri(&src.join("f"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert_eq!((out.conflicts, out.report.failures.len()), (1, 0));
|
|
assert_eq!(names(&src.join("f")), ["a.txt"]);
|
|
assert_eq!(names(&dst.join("f")), ["b.txt"]);
|
|
}
|
|
|
|
#[test]
|
|
fn move_to_same_folder_is_a_no_op() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
fs::write(dir.path().join("a.txt"), "a").unwrap();
|
|
let out = engine().run(
|
|
JobKind::Move {
|
|
sources: vec![uri(&dir.path().join("a.txt"))],
|
|
dest: uri(dir.path()),
|
|
},
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert_eq!((out.conflicts, out.report.failures.len()), (0, 0));
|
|
assert_eq!(read(dir.path().join("a.txt")), "a");
|
|
}
|
|
|
|
#[test]
|
|
fn cross_device_move_copies_then_deletes() {
|
|
let other = Path::new("/dev/shm");
|
|
let here = tempfile::tempdir().unwrap();
|
|
let Ok(far) = tempfile::tempdir_in(other) else {
|
|
eprintln!("skipped: no /dev/shm");
|
|
return;
|
|
};
|
|
if fs::metadata(here.path()).unwrap().dev() == fs::metadata(far.path()).unwrap().dev() {
|
|
eprintln!("skipped: temp dir and /dev/shm share a device");
|
|
return;
|
|
}
|
|
let src = here.path().join("tree");
|
|
fs::create_dir_all(src.join("sub")).unwrap();
|
|
fs::write(src.join("sub/big.bin"), vec![9u8; 3 << 20]).unwrap();
|
|
fs::write(src.join("a.txt"), "a").unwrap();
|
|
let out = engine().run(
|
|
JobKind::Move {
|
|
sources: vec![uri(&src)],
|
|
dest: uri(far.path()),
|
|
},
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert!(out.report.failures.is_empty(), "{:?}", out.report.failures);
|
|
assert!(!src.exists());
|
|
assert_eq!(
|
|
fs::read(far.path().join("tree/sub/big.bin")).unwrap().len(),
|
|
3 << 20
|
|
);
|
|
assert_eq!(read(far.path().join("tree/a.txt")), "a");
|
|
}
|
|
|
|
#[test]
|
|
fn delete_removes_trees_and_reports_missing() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
fs::create_dir_all(dir.path().join("t/deep/er")).unwrap();
|
|
fs::write(dir.path().join("t/deep/er/f"), "x").unwrap();
|
|
std::os::unix::fs::symlink("/", dir.path().join("t/link-to-root")).unwrap();
|
|
fs::write(dir.path().join("keep"), "x").unwrap();
|
|
let e = engine();
|
|
let out = e.run(
|
|
JobKind::Delete(vec![uri(&dir.path().join("t"))]),
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert!(out.report.failures.is_empty());
|
|
assert_eq!(names(dir.path()), ["keep"]);
|
|
let out = e.run(
|
|
JobKind::Delete(vec![uri(&dir.path().join("t"))]),
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert_eq!(out.report.failures.len(), 1);
|
|
assert_eq!(out.errors, 1);
|
|
}
|
|
|
|
#[test]
|
|
fn mkdir_rename_and_link_jobs() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let e = engine();
|
|
let p = dir.path();
|
|
let skip = || Policy::new(ConflictAction::Skip);
|
|
assert!(
|
|
e.run(JobKind::Mkdir(uri(&p.join("d"))), skip())
|
|
.report
|
|
.failures
|
|
.is_empty()
|
|
);
|
|
assert!(p.join("d").is_dir());
|
|
assert_eq!(
|
|
e.run(JobKind::Mkdir(uri(&p.join("d"))), skip())
|
|
.report
|
|
.failures
|
|
.len(),
|
|
1
|
|
);
|
|
fs::write(p.join("f"), "x").unwrap();
|
|
e.run(
|
|
JobKind::Rename {
|
|
from: uri(&p.join("f")),
|
|
to: uri(&p.join("g")),
|
|
},
|
|
skip(),
|
|
);
|
|
assert!(!p.join("f").exists() && p.join("g").exists());
|
|
// Renaming onto an existing name fails and changes nothing.
|
|
fs::write(p.join("h"), "keep").unwrap();
|
|
let out = e.run(
|
|
JobKind::Rename {
|
|
from: uri(&p.join("g")),
|
|
to: uri(&p.join("h")),
|
|
},
|
|
skip(),
|
|
);
|
|
assert_eq!(out.report.failures.len(), 1);
|
|
assert_eq!(read(p.join("h")), "keep");
|
|
e.run(
|
|
JobKind::Link {
|
|
sources: vec![uri(&p.join("g"))],
|
|
dest: uri(&p.join("d")),
|
|
},
|
|
skip(),
|
|
);
|
|
assert_eq!(fs::read_link(p.join("d/g")).unwrap(), p.join("g"));
|
|
}
|
|
|
|
#[test]
|
|
fn partial_failure_skips_and_continues() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(&src).unwrap();
|
|
fs::create_dir(&dst).unwrap();
|
|
fs::write(src.join("locked.txt"), "secret").unwrap();
|
|
fs::write(src.join("ok.txt"), "ok").unwrap();
|
|
fs::set_permissions(src.join("locked.txt"), fs::Permissions::from_mode(0o000)).unwrap();
|
|
if fs::File::open(src.join("locked.txt")).is_ok() {
|
|
eprintln!("skipped: modes are not enforced (running as root)");
|
|
return;
|
|
}
|
|
let out = engine().run(
|
|
JobKind::Copy {
|
|
sources: vec![uri(&src.join("locked.txt")), uri(&src.join("ok.txt"))],
|
|
dest: uri(&dst),
|
|
},
|
|
Policy::new(ConflictAction::Skip),
|
|
);
|
|
assert_eq!(out.errors, 1);
|
|
assert_eq!(out.report.failures.len(), 1);
|
|
assert!(out.report.failures[0].1.needs_elevation());
|
|
assert!(!out.report.cancelled);
|
|
assert_eq!(names(&dst), ["ok.txt"]);
|
|
}
|
|
|
|
#[test]
|
|
fn error_actions_retry_skip_all_abort() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(&src).unwrap();
|
|
fs::create_dir(&dst).unwrap();
|
|
for n in ["l1", "l2"] {
|
|
fs::write(src.join(n), n).unwrap();
|
|
fs::set_permissions(src.join(n), fs::Permissions::from_mode(0o000)).unwrap();
|
|
}
|
|
fs::write(src.join("ok"), "ok").unwrap();
|
|
if fs::File::open(src.join("l1")).is_ok() {
|
|
eprintln!("skipped: modes are not enforced (running as root)");
|
|
return;
|
|
}
|
|
let kind = || JobKind::Copy {
|
|
sources: ["l1", "l2", "ok"]
|
|
.iter()
|
|
.map(|n| uri(&src.join(n)))
|
|
.collect(),
|
|
dest: uri(&dst),
|
|
};
|
|
let e = engine();
|
|
|
|
let mut policy = Policy::new(ConflictAction::Skip);
|
|
policy.error = ErrorAction::SkipAll;
|
|
let out = e.run(kind(), policy);
|
|
assert_eq!((out.errors, out.report.failures.len()), (1, 2));
|
|
assert_eq!(names(&dst), ["ok"]);
|
|
fs::remove_file(dst.join("ok")).unwrap();
|
|
|
|
let mut policy = Policy::new(ConflictAction::Skip);
|
|
policy.error = ErrorAction::Abort;
|
|
let out = e.run(kind(), policy);
|
|
assert!(out.report.cancelled);
|
|
assert_eq!(out.report.failures.len(), 1);
|
|
assert!(names(&dst).is_empty());
|
|
|
|
// Retry after the "user" fixed the permission.
|
|
let mut policy = Policy::new(ConflictAction::Skip);
|
|
policy.error = ErrorAction::Retry;
|
|
let fixed = src.clone();
|
|
policy.on_error = Some(Box::new(move |failing| {
|
|
let path = failing.to_path().unwrap();
|
|
assert!(path.starts_with(&fixed));
|
|
fs::set_permissions(path, fs::Permissions::from_mode(0o644)).unwrap();
|
|
}));
|
|
let out = e.run(kind(), policy);
|
|
assert_eq!((out.errors, out.report.failures.len()), (2, 0));
|
|
assert_eq!(names(&dst), ["l1", "l2", "ok"]);
|
|
}
|
|
|
|
#[test]
|
|
fn cancel_at_a_conflict_stops_the_job() {
|
|
let (_dir, src, dst) = conflicted();
|
|
fs::write(src.join("0.txt"), "first").unwrap();
|
|
let mut policy = Policy::new(ConflictAction::Skip);
|
|
policy.cancel_on_conflict = true;
|
|
let out = engine().run(
|
|
JobKind::Copy {
|
|
sources: ["0", "a", "b"]
|
|
.iter()
|
|
.map(|n| uri(&src.join(format!("{n}.txt"))))
|
|
.collect(),
|
|
dest: uri(&dst),
|
|
},
|
|
policy,
|
|
);
|
|
assert!(out.report.cancelled);
|
|
assert_eq!(read(dst.join("0.txt")), "first");
|
|
assert_eq!(read(dst.join("a.txt")), "dst a");
|
|
assert_eq!(read(dst.join("b.txt")), "dst b");
|
|
}
|
|
|
|
#[test]
|
|
fn cancel_in_the_middle_of_a_file_leaves_no_partial_copy() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let (src, dst) = (dir.path().join("src"), dir.path().join("dst"));
|
|
fs::create_dir_all(&src).unwrap();
|
|
fs::create_dir(&dst).unwrap();
|
|
fs::write(src.join("big.bin"), vec![7u8; 48 << 20]).unwrap();
|
|
let e = engine();
|
|
e.slow.store(true, Ordering::Relaxed);
|
|
let mut policy = Policy::new(ConflictAction::Skip);
|
|
policy.cancel_on_bytes = true;
|
|
let out = e.run(
|
|
JobKind::Copy {
|
|
sources: vec![uri(&src.join("big.bin"))],
|
|
dest: uri(&dst),
|
|
},
|
|
policy,
|
|
);
|
|
e.slow.store(false, Ordering::Relaxed);
|
|
assert!(out.report.cancelled);
|
|
assert!(!dst.join("big.bin").exists());
|
|
assert!(src.join("big.bin").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn naming_helpers() {
|
|
assert_eq!(numbered(b"report.pdf", 2), b"report (2).pdf");
|
|
assert_eq!(numbered(b"report (2).pdf", 3), b"report (3).pdf");
|
|
assert_eq!(numbered(b"photos", 4), b"photos (4)");
|
|
assert_eq!(numbered(b".bashrc", 2), b".bashrc (2)");
|
|
assert_eq!(numbered(b"b.TAR.GZ", 2), b"b (2).TAR.GZ");
|
|
assert_eq!(numbered(b"backup.tar.gz", 2), b"backup (2).tar.gz");
|
|
assert_eq!(numbered(b"x (a).txt", 2), b"x (a) (2).txt");
|
|
assert_eq!(numbered(b"odd \xff.txt", 2), b"odd \xff (2).txt");
|
|
|
|
let top = Uri::parse("file:///a/b").unwrap();
|
|
assert!(is_inside(&top, &top));
|
|
assert!(is_inside(&Uri::parse("file:///a/b/c/d").unwrap(), &top));
|
|
assert!(is_inside(
|
|
&Uri::parse("file:///a/b/c").unwrap(),
|
|
&Uri::parse("file:///a/b/").unwrap()
|
|
));
|
|
assert!(!is_inside(&Uri::parse("file:///a/bc").unwrap(), &top));
|
|
assert!(!is_inside(&Uri::parse("sftp:///a/b/c").unwrap(), &top));
|
|
assert!(is_inside(&top, &Uri::parse("file:///").unwrap()));
|
|
}
|
|
|
|
#[test]
|
|
fn auto_rename_picks_the_first_free_number() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
for n in ["a.txt", "a (2).txt", "a (4).txt"] {
|
|
fs::write(dir.path().join(n), "").unwrap();
|
|
}
|
|
let free = auto_rename(&LocalLocation, &uri(&dir.path().join("a.txt"))).unwrap();
|
|
assert_eq!(free.file_name(), Some(&b"a (3).txt"[..]));
|
|
// Also from an already numbered name.
|
|
let free = auto_rename(&LocalLocation, &uri(&dir.path().join("a (2).txt"))).unwrap();
|
|
assert_eq!(free.file_name(), Some(&b"a (3).txt"[..]));
|
|
}
|