Recovery: replace a symlinked sidecar and do not sweep through symlinked bundle folders (#11230)

* Recovery: replace a symlinked sidecar and do not sweep through symlinked bundle folders

* address review: remove a directory symlink at the sidecar path with remove_dir on Windows
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-11 08:38:40 -05:00
1 parent e4baa2270e
commit 8ef148ace9
1 file changed
+49 -1
+49 -1
View File
@@ -189,6 +189,18 @@ fn worker(rx: Receiver<Job>, bundle: PathBuf, sidecar: PathBuf, last: Arc<Mutex<
/// bundle and listed it twice, or not at all (#9034). A rename swaps the name in one step, and
/// recovery data is private to the app, so there is no metadata to keep.
fn write_sidecar(path: &Path, bytes: &[u8]) -> Result<()> {
// The atomic writer follows a symlink at its destination; a link planted at the sidecar's
// name in the recovery folder must not redirect the write, so it is replaced, not followed.
if let Ok(meta) = std::fs::symlink_metadata(path)
&& meta.file_type().is_symlink()
{
// Windows removes a directory symlink with `remove_dir`, not `remove_file`.
#[cfg(windows)]
let dir_link = std::os::windows::fs::FileTypeExt::is_symlink_dir(&meta.file_type());
#[cfg(not(windows))]
let dir_link = false;
if dir_link { std::fs::remove_dir(path)? } else { std::fs::remove_file(path)? }
}
struct SidecarFs;
impl crate::atomic::AtomicFs for SidecarFs {
// As before: Windows refuses a read-only sidecar up front, Unix replaces it.
@@ -451,7 +463,13 @@ fn sweep_orphans(recovery_dir: &Path, min_age: std::time::Duration) {
}
// Anything at the sidecar's path (even a broken one) keeps the bundle.
let has_sidecar = std::fs::symlink_metadata(recovery_dir.join(format!("{key}.json"))).is_ok();
let parts = [path.clone(), path.join(craft_pcraft_format::TILES_DIR), path.join(craft_pcraft_format::BLOBS_DIR)];
// A linked `tiles/` or `blobs/` is left alone: sweeping it would delete files elsewhere.
let real_dir = |p: PathBuf| std::fs::symlink_metadata(&p).is_ok_and(|m| m.is_dir()).then_some(p);
let parts: Vec<PathBuf> =
[Some(path.clone()), real_dir(path.join(craft_pcraft_format::TILES_DIR)), real_dir(path.join(craft_pcraft_format::BLOBS_DIR))]
.into_iter()
.flatten()
.collect();
// A committed bundle is recoverable work: keep exactly what `list_recovery` lists.
if !has_sidecar && sidecarless_entry(recovery_dir, &path).is_none() && parts.iter().all(|p| untouched_for(p, min_age, true)) {
let _ = std::fs::remove_dir_all(&path);
@@ -631,4 +649,34 @@ mod tests {
assert!(dir.join("first.pcraft").is_dir());
let _ = std::fs::remove_dir_all(&dir);
}
#[cfg(unix)]
#[test]
fn symlinks_in_the_recovery_folder_are_replaced_or_skipped_never_followed() {
let dir = std::env::temp_dir().join(format!(
"pcraft-links-{}-{}",
std::process::id(),
SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0)
));
let outside = dir.join("outside");
std::fs::create_dir_all(&outside).unwrap();
// A sidecar name that links to a file elsewhere: the write replaces the link.
let victim = outside.join("victim.txt");
std::fs::write(&victim, b"keep").unwrap();
let sidecar = dir.join("k.json");
std::os::unix::fs::symlink(&victim, &sidecar).unwrap();
write_sidecar(&sidecar, b"{}").unwrap();
assert_eq!(std::fs::read(&victim).unwrap(), b"keep");
assert_eq!(std::fs::read(&sidecar).unwrap(), b"{}");
// A bundle whose `tiles` links elsewhere: the sweep leaves the linked folder's files.
let stale = outside.join(".x.4294967294-0.pcsave.tmp");
std::fs::write(&stale, b"tmp").unwrap();
let bundle = dir.join("doc-1.pcraft");
std::fs::create_dir_all(&bundle).unwrap();
std::os::unix::fs::symlink(&outside, bundle.join(craft_pcraft_format::TILES_DIR)).unwrap();
std::fs::write(dir.join("doc-1.json"), b"{}").unwrap();
sweep_orphans(&dir, std::time::Duration::ZERO);
assert!(stale.is_file(), "files behind a linked tiles folder are not swept");
let _ = std::fs::remove_dir_all(&dir);
}
}