diff --git a/atomic-repository/src/content_filter.rs b/atomic-repository/src/content_filter.rs index b29d40f5..311bd7dd 100644 --- a/atomic-repository/src/content_filter.rs +++ b/atomic-repository/src/content_filter.rs @@ -587,13 +587,24 @@ fn run_bounded_command( .spawn() .map_err(|error| error.to_string())?; #[cfg(windows)] - let mut child = Command::new("cmd") - .args(["/C", &command]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - .map_err(|error| error.to_string())?; + let mut child = { + use std::os::windows::process::CommandExt; + let mut shell = Command::new("cmd"); + shell.arg("/C"); + // The driver command reaches cmd.exe verbatim. Rust's standard + // argument escaping writes `\"` for every quote, which cmd.exe + // does not understand, so a driver command that quotes an + // argument — a path with spaces, or any `%f` substitution that + // needs them — fails with "The filename, directory name, or + // volume label syntax is incorrect". + shell.raw_arg(command); + shell + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .map_err(|error| error.to_string())? + }; let mut stdin = child.stdin.take().expect("piped stdin"); let owned_input = input.to_vec(); diff --git a/atomic-repository/src/repository/git_stream.rs b/atomic-repository/src/repository/git_stream.rs index af4f1a1d..25892715 100644 --- a/atomic-repository/src/repository/git_stream.rs +++ b/atomic-repository/src/repository/git_stream.rs @@ -2074,33 +2074,79 @@ mod tests { assert!(!dot.join("bridge.lock").exists()); } - /// Copy a repository while it is still open: the copy is what a killed - /// process leaves behind, a database redb must repair before reading. - fn copy_of_open_repository(source: &std::path::Path) -> tempfile::TempDir { - fn copy_dir(from: &std::path::Path, to: &std::path::Path) { - std::fs::create_dir_all(to).unwrap(); - for entry in std::fs::read_dir(from).unwrap() { - let entry = entry.unwrap(); - let target = to.join(entry.file_name()); - if entry.file_type().unwrap().is_dir() { - copy_dir(&entry.path(), &target); - } else { - std::fs::copy(entry.path(), &target).unwrap(); + /// Hold a repository database open in a child process, then kill the + /// child: the leftover database is what a killed process leaves + /// behind, one redb must repair before reading. + /// + /// This replaces a Unix fixture that copied the repository while it + /// was still open. Windows cannot reproduce that copy — redb holds a + /// byte-range lock over the open database, so every second-handle read + /// fails with a sharing violation (OS error 33) — but killing a real + /// holder produces the same unclosed state on every platform. + fn kill_a_database_holder(root: &std::path::Path) { + const HOLD_ENV: &str = "ATOMIC_TEST_HOLD_OPEN_DB"; + if let Some(held) = std::env::var_os(HOLD_ENV) { + let _repo = Repository::open(std::path::PathBuf::from(held)).unwrap(); + println!("ATOMIC_TEST_DB_HELD"); + loop { + std::thread::sleep(std::time::Duration::from_secs(60)); + } + } + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "repository::git_stream::tests::git_import_preflight_repairs_a_database_left_by_a_killed_process", + "--nocapture", + ]) + .env(HOLD_ENV, root) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let stdout = child.stdout.take().unwrap(); + let (send, receive) = std::sync::mpsc::channel(); + let reader = std::thread::spawn(move || { + use std::io::BufRead; + for line in std::io::BufReader::new(stdout).lines() { + match line { + Ok(line) if line.contains("ATOMIC_TEST_DB_HELD") => { + let _ = send.send(Ok(())); + return; + } + Ok(_) => {} + Err(error) => { + let _ = send.send(Err(error.to_string())); + return; + } } } + let _ = send.send(Err("child exited before holding the database".to_string())); + }); + let ready = receive.recv_timeout(std::time::Duration::from_secs(60)); + let killed = child.kill(); + let _ = child.wait(); + if ready.is_ok() { + reader.join().unwrap(); } - let copy = tempfile::tempdir().unwrap(); - copy_dir(source, copy.path()); - copy + ready + .expect("child timed out") + .expect("child did not hold the database open"); + killed.expect("child must be killed while the database is open"); } #[test] fn git_import_preflight_repairs_a_database_left_by_a_killed_process() { - let dir = tempfile::tempdir().unwrap(); - let repo = Repository::init(dir.path()).unwrap(); - let killed = copy_of_open_repository(dir.path()); - let dry_run = copy_of_open_repository(dir.path()); - drop(repo); + let killed = tempfile::tempdir().unwrap(); + let dry_run = tempfile::tempdir().unwrap(); + { + let repo = Repository::init(killed.path()).unwrap(); + drop(repo); + } + { + let repo = Repository::init(dry_run.path()).unwrap(); + drop(repo); + } + kill_a_database_holder(killed.path()); + kill_a_database_holder(dry_run.path()); let database = |root: &std::path::Path| root.join(".atomic").join(super::super::DATABASE_FILE); assert!( diff --git a/atomic-repository/src/repository/tests/conflict_surface_tests.rs b/atomic-repository/src/repository/tests/conflict_surface_tests.rs index f94b95fa..38a8b0b4 100644 --- a/atomic-repository/src/repository/tests/conflict_surface_tests.rs +++ b/atomic-repository/src/repository/tests/conflict_surface_tests.rs @@ -23,6 +23,21 @@ fn record_all(repo: &Repository, message: &str) -> Result String { + if cfg!(windows) { + format!("'type \"{}\"'", file.display()) + } else { + "'cat'".to_string() + } +} + /// Build a repo where feature and dev insert different lines at the same /// position, then insert feature → dev and materialize on dev. Returns the /// on-disk path of the conflicted file. @@ -61,7 +76,10 @@ fn make_conflicted_repo() -> (TempDir, TestRepository, std::path::PathBuf) { .unwrap(); std::fs::write( temp_dir.path().join(".atomic/config.toml"), - "[view]\ndefault = \"dev\"\n\n[filters.drivers.reject-conflict]\nclean = \"cat\"\nsmudge = \"exit 19\"\nrequired = true\n", + format!( + "[view]\ndefault = \"dev\"\n\n[filters.drivers.reject-conflict]\nclean = {}\nsmudge = \"exit 19\"\nrequired = true\n", + clean_pass_through(&file) + ), ) .unwrap(); repo.insert_from_view(CrossViewInsertOptions::new("feature", "dev"))