HLE: a save version that cannot be removed does not fail the container

`current` removed unfinished and older save versions and propagated any
error. A file another program holds without letting it be deleted (a virus
scanner, a backup in progress) then failed the whole lookup: the container
read as unreadable and every save of it failed for as long as the file was
held, though the newest readable version was intact.

Both removals are now best-effort and logged; the leftovers go at a later
save, when the file is free.

Ported from fxceless/victory-fork 6a42ed27, GPL-3.0-or-later.

(cherry picked from commit 0438a1b7467cfa33bfe59336cb99326ae9c4561a)
This commit is contained in:
fxceless authored and trustno1-coder committed 2026-11-27 17:51:23 -08:00
1 parent d8b258a455
commit 879558a3a7
1 file changed
+61 -2
+61 -2
View File
@@ -213,7 +213,12 @@ fn current(dir: &Path) -> std::io::Result<Option<Version>> {
let file = entry.file_name();
let file = file.to_string_lossy();
if file.ends_with(".new") {
std::fs::remove_dir_all(entry.path())?;
// As far as it goes: an unfinished version is never read, and a
// file another program holds (a virus scanner, a backup) must
// not make the container unreadable.
if let Err(e) = std::fs::remove_dir_all(entry.path()) {
tracing::debug!(dir = %entry.path().display(), %e, "an unfinished save version stays for now");
}
} else if let Some(n) = version_number(&file) {
highest = highest.max(n);
if entry.file_type().is_ok_and(|t| t.is_dir()) {
@@ -230,8 +235,16 @@ fn current(dir: &Path) -> std::io::Result<Option<Version>> {
let Some((name, display, blobs)) = load(&version)? else {
continue;
};
// Older versions go as far as they can. The newest readable one is
// the container whatever is left below it, and a file another
// program holds without letting it be deleted fails the removal:
// returning that error made every save fail for as long as it was
// held (while a virus scanner or a backup had it open).
for old in &committed[at + 1..] {
std::fs::remove_dir_all(dir.join(format!("v{old}")))?;
let path = dir.join(format!("v{old}"));
if let Err(e) = std::fs::remove_dir_all(&path) {
tracing::info!(dir = %path.display(), %e, "an older save version stays for now; it goes at a later save");
}
}
return Ok(Some(Version {
dir: version,
@@ -1018,6 +1031,52 @@ mod tests {
assert_eq!(no_room(mb, 0, 0, mb), None);
}
/// A file of an older version held by another program (a virus scanner,
/// a backup copy in progress) keeps that version from being removed.
/// The saves after it must not fail for that: the newest readable
/// version is the container whatever is left below it, and what is left
/// goes at a later save.
#[cfg(windows)]
#[test]
fn an_older_version_that_cannot_be_removed_does_not_fail_the_saves_after_it() {
use std::os::windows::fs::OpenOptionsExt;
let root = tempfile::tempdir().unwrap();
let dir = container_dir(root.path(), "Slot");
let versions = || {
let entries = std::fs::read_dir(&dir).unwrap().flatten();
entries
.filter(|e| e.file_name().to_string_lossy().starts_with('v'))
.count()
};
assert_eq!(update(root.path(), "one", &[("a", Some("first a"))]), S_OK);
let first = current(&dir).unwrap().unwrap();
// Opened with no sharing: nobody else can delete it while it is held.
let held = std::fs::OpenOptions::new()
.read(true)
.share_mode(0)
.open(first.blob(0))
.unwrap();
assert_eq!(update(root.path(), "two", &[("a", Some("second a"))]), S_OK);
assert_eq!(
state(root.path()),
Some(("two".into(), vec![("a".into(), "second a".into())]))
);
assert_eq!(update(root.path(), "three", &[("a", Some("third a"))]), S_OK);
assert_eq!(
state(root.path()),
Some(("three".into(), vec![("a".into(), "third a".into())]))
);
assert!(first.blob(0).exists(), "the held file is still there");
// Let go of: the next look at the container removes what was left.
drop(held);
assert_eq!(update(root.path(), "four", &[("a", Some("fourth a"))]), S_OK);
assert_eq!(
state(root.path()),
Some(("four".into(), vec![("a".into(), "fourth a".into())]))
);
assert_eq!(versions(), 1);
}
fn update(root: &Path, display: &str, changes: &[(&str, Option<&str>)]) -> HRESULT {
Update {
name: "Slot".into(),