XGameSave: a stray or unreadable version folder never costs the save

Recovery took any name `v<digits>` as a version, so a leftover `v01`
next to a committed `v1` parsed as version 1, and the cleanup rebuilt
the name as `v1` and deleted the committed save (#870). A correctly
named but unreadable newer entry (an empty `v2`, a file, a broken
manifest) did the same: `v1` was pruned as older, the read failed, and
the next update committed a fresh `v1` holding only the changed blobs,
which the following read pruned again.

Now only `v<N>` exactly as `commit` writes it is a version. `current`
takes the newest version whose manifest reads, removes only versions
below it, and leaves unreadable newer entries alone. An update goes
above the highest version name present, so a stray entry neither hides
the save nor blocks writes. With no readable version at all, reads and
updates still fail and nothing is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit b8dbcba260d1012dca8f06ee572e5244bffba1ac)
This commit is contained in:
moe-miau authored and local committed 2026-11-25 01:03:03 -06:00
1 parent eea3b01ee8
commit 0618f88ebe
1 file changed
+129 -24
+129 -24
View File
@@ -135,7 +135,9 @@ fn valid_container(name: &str) -> bool {
/// A committed version of a container.
struct Version {
dir: PathBuf,
number: u64,
/// The highest version number any entry in the container's folder
/// carries, readable or not; the next commit goes above it.
highest: u64,
name: String,
display: String,
/// Blob names; blob `i` is the file `i`.
@@ -177,38 +179,73 @@ fn parse_manifest(bytes: &[u8]) -> Option<Vec<String>> {
(out.len() >= 2).then_some(out)
}
/// The container's newest committed version, if any. Removes what a crash
/// left: unfinished versions and older ones.
/// The number of a version folder exactly as [`commit`] names it (`v1`,
/// `v2`, ...); `None` for anything else, such as `v01`, `v+1` or `v0`.
fn version_number(file: &str) -> Option<u64> {
let n = file.strip_prefix('v')?.parse::<u64>().ok()?;
(n > 0 && format!("v{n}") == file).then_some(n)
}
/// The names in version folder `dir`: container, display, blobs; `None` if
/// its manifest is missing or does not parse.
fn load(dir: &Path) -> std::io::Result<Option<(String, String, Vec<String>)>> {
let bytes = match std::fs::read(dir.join(MANIFEST)) {
Ok(bytes) => bytes,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e),
};
let Some(mut names) = parse_manifest(&bytes) else {
return Ok(None);
};
let blobs = names.split_off(2);
let [name, display] = <[String; 2]>::try_from(names).expect("two names, checked by the parser");
Ok(Some((name, display, blobs)))
}
/// The container's newest readable committed version, if any. Removes what
/// a crash left: unfinished versions, and versions older than that one.
/// An unreadable newer folder (a stray, or a broken manifest) is neither
/// taken nor removed, and never costs the version below it.
fn current(dir: &Path) -> std::io::Result<Option<Version>> {
let mut committed = Vec::new();
let mut highest = 0;
for entry in std::fs::read_dir(dir)?.flatten() {
let file = entry.file_name();
let file = file.to_string_lossy();
if file.ends_with(".new") {
std::fs::remove_dir_all(entry.path())?;
} else if let Some(n) = file.strip_prefix('v').and_then(|n| n.parse::<u64>().ok()) {
committed.push(n);
} else if let Some(n) = version_number(&file) {
highest = highest.max(n);
if entry.file_type().is_ok_and(|t| t.is_dir()) {
committed.push(n);
}
}
}
committed.sort_unstable();
let Some(&number) = committed.last() else {
committed.sort_unstable_by(|a, b| b.cmp(a));
if committed.is_empty() {
return Ok(None);
};
for old in &committed[..committed.len() - 1] {
std::fs::remove_dir_all(dir.join(format!("v{old}")))?;
}
let version = dir.join(format!("v{number}"));
let bad = || std::io::Error::new(std::io::ErrorKind::InvalidData, "bad save manifest");
let mut names = parse_manifest(&std::fs::read(version.join(MANIFEST))?).ok_or_else(bad)?;
let blobs = names.split_off(2);
let [name, display] = <[String; 2]>::try_from(names).map_err(|_| bad())?;
Ok(Some(Version {
dir: version,
number,
name,
display,
blobs,
}))
for (at, &number) in committed.iter().enumerate() {
let version = dir.join(format!("v{number}"));
let Some((name, display, blobs)) = load(&version)? else {
continue;
};
for old in &committed[at + 1..] {
std::fs::remove_dir_all(dir.join(format!("v{old}")))?;
}
return Ok(Some(Version {
dir: version,
highest,
name,
display,
blobs,
}));
}
// Versions exist but none reads: keep them all, and refuse.
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"no save version has a readable manifest",
))
}
/// The version of container `name` in `dir`; none for a missing container
@@ -471,7 +508,7 @@ impl Update {
}
let display = self.display.as_deref();
let display = display.or(current.as_ref().map(|v| v.display.as_str()));
let number = current.as_ref().map_or(1, |v| v.number + 1);
let number = current.as_ref().map_or(1, |v| v.highest + 1);
if let Err(e) = commit(&self.dir, number, &self.name, display.unwrap_or(""), &blobs) {
return io_error(&self.dir, &e);
}
@@ -1172,7 +1209,7 @@ mod tests {
]
);
let v = current_named(&c.dir, "Player").unwrap().unwrap();
assert_eq!((v.number, v.display.as_str()), (1, "Chapter 1"));
assert_eq!((v.dir.file_name(), v.display.as_str()), (Some("v1".as_ref()), "Chapter 1"));
}
/// An empty configuration id names none. As a folder name it would be
@@ -1384,4 +1421,72 @@ mod tests {
close_container(this, container);
close_provider(this, provider);
}
/// Only `v<N>` as `commit` writes it is a version: `v01` and the like
/// are neither read, counted nor removed (#870).
#[test]
fn a_noncanonical_folder_does_not_remove_a_committed_save() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
assert_eq!(update(root, "one", &[("a", Some("old a")), ("b", Some("old b"))]), S_OK);
let slot = container_dir(root, "Slot");
for stray in ["v01", "v001", "v+1", "v0", "v02"] {
std::fs::create_dir(slot.join(stray)).unwrap();
}
assert_eq!(state(root), old_state());
assert!(slot.join("v1").is_dir());
for stray in ["v01", "v001", "v+1", "v0", "v02"] {
assert!(slot.join(stray).exists(), "{stray} was removed");
}
}
/// A newer version folder that does not read (an empty folder, a file,
/// a broken manifest) neither hides the committed save, nor removes it,
/// nor blocks the next update, which goes above it.
#[test]
fn an_unreadable_newer_version_does_not_cost_the_save() {
type Make = fn(&Path);
let strays: [(&str, Make); 3] = [
("empty folder", |p| std::fs::create_dir(p).unwrap()),
("file", |p| std::fs::write(p, b"not a version").unwrap()),
("broken manifest", |p| {
std::fs::create_dir(p).unwrap();
std::fs::write(p.join(MANIFEST), [9, 0, 0, 0, b'x']).unwrap();
}),
];
for (what, make) in strays {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
assert_eq!(update(root, "one", &[("a", Some("old a")), ("b", Some("old b"))]), S_OK);
let slot = container_dir(root, "Slot");
make(&slot.join("v2"));
assert_eq!(state(root), old_state(), "{what}: read");
assert!(slot.join("v1").is_dir(), "{what}: v1 was removed");
assert_eq!(update(root, "two", &[("a", Some("new a"))]), S_OK, "{what}: update");
let blobs = vec![("a".into(), "new a".into()), ("b".into(), "old b".into())];
let after = Some(("two".into(), blobs));
assert_eq!(state(root), after, "{what}: after the update");
assert!(slot.join("v3").is_dir(), "{what}: the update did not go above v2");
// Read again: the new version stays the container.
assert_eq!(state(root), after, "{what}: second read");
}
}
/// With no readable version at all, reads and updates fail and nothing
/// is removed.
#[test]
fn no_readable_version_is_refused_and_kept() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
assert_eq!(update(root, "one", &[("a", Some("old a"))]), S_OK);
let slot = container_dir(root, "Slot");
std::fs::write(slot.join("v1").join(MANIFEST), [1, 2, 3]).unwrap();
let c = Provider { root: root.into() }.container("Slot");
assert_eq!(read(&c, None).err(), Some(NO_ACCESS));
assert_eq!(update(root, "two", &[("a", Some("new a"))]), NO_ACCESS);
assert!(slot.join("v1").join("0").is_file());
assert!(!slot.join("v2").exists());
}
}