Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 124 additions & 25 deletions crates/xcl/src/hle/xgameruntime/gamesave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,30 @@ fn replace_file(tmp: &Path, dest: &Path) -> std::io::Result<()> {
}
}

/// Write `data` to a unique sibling temp file, flush to disk, and atomically replace `dest`.
fn write_atomic(dest: &Path, data: &[u8]) -> std::io::Result<()> {
use std::io::Write;
/// A unique sibling temp file of `dest` (`blobs_of` skips what a crash leaves behind).
fn temp_path(dest: &Path) -> std::io::Result<PathBuf> {
let parent = dest
.parent()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "missing parent directory"))?;
let id = NEXT_TMP.fetch_add(1, Ordering::Relaxed);
let tmp = parent.join(format!("{TMP_PREFIX}{}.{id}", std::process::id()));
Ok(parent.join(format!("{TMP_PREFIX}{}.{id}", std::process::id())))
}

/// Write `data` to a unique sibling temp file of `dest`, flushed to disk, for the caller to
/// move into place (`replace_file`); nothing is left behind when the write fails.
fn write_temp(dest: &Path, data: &[u8]) -> std::io::Result<PathBuf> {
use std::io::Write;
let tmp = temp_path(dest)?;
let res = (|| -> std::io::Result<()> {
let mut f = std::fs::File::create(&tmp)?;
f.write_all(data)?;
f.sync_all()?;
drop(f);
replace_file(&tmp, dest)?;
Ok(())
f.sync_all()
})();
if res.is_err() {
if let Err(e) = res {
let _ = std::fs::remove_file(&tmp);
return Err(e);
}
res
Ok(tmp)
}

unsafe fn provider<'a>(p: *mut Provider) -> Option<&'a Provider> {
Expand Down Expand Up @@ -567,24 +571,60 @@ fn apply(dir: &Path, display: Option<&str>, ops: &[(String, Option<Vec<u8>>)]) -
if std::fs::create_dir_all(&blobs).is_err() {
return crate::util::E_FAIL;
}
if let Some(d) = display {
if write_atomic(&dir.join("display"), d.as_bytes()).is_err() {
return crate::util::E_FAIL;
// All or nothing (#324): every write goes to a temp file first; then the files the update
// replaces or deletes are moved aside and the temps moved into place. A failure anywhere
// puts the files moved aside back and removes the temps, so the container stays as it was.
let mut writes: Vec<(PathBuf, PathBuf)> = Vec::new(); // (temp, destination)
let mut deletes: Vec<PathBuf> = Vec::new();
let mut aside: Vec<(PathBuf, PathBuf)> = Vec::new(); // (where it went, where it was)
let mut placed = 0;
let done = (|| -> std::io::Result<()> {
if let Some(d) = display {
let dest = dir.join("display");
writes.push((write_temp(&dest, d.as_bytes())?, dest));
}
}
for (name, data) in ops {
let path = blobs.join(enc(name));
match data {
Some(d) => {
// write to a unique sibling temp file, sync, then replace atomically
if write_atomic(&path, d).is_err() {
return crate::util::E_FAIL;
}
// the last op on a blob is the one that counts, as when they ran in order (names
// compare without case: one file on NTFS)
let mut seen: Vec<&str> = Vec::new();
let last: Vec<&(String, Option<Vec<u8>>)> =
ops.iter().rev().filter(|(name, _)| !seen.iter().any(|s| s.eq_ignore_ascii_case(name)) && { seen.push(name); true }).collect();
for (name, data) in last.into_iter().rev() {
let path = blobs.join(enc(name));
match data {
Some(d) => writes.push((write_temp(&path, d)?, path)),
None => deletes.push(path),
}
}
for dest in writes.iter().map(|(_, dest)| dest).chain(&deletes) {
if dest.is_file() {
let away = temp_path(dest)?;
replace_file(dest, &away)?;
aside.push((away, dest.clone()));
}
}
for (tmp, dest) in &writes {
replace_file(tmp, dest)?;
placed += 1;
}
Ok(())
})();
if done.is_err() {
for (tmp, dest) in &writes {
let _ = std::fs::remove_file(tmp);
if placed > 0 {
let _ = std::fs::remove_file(dest);
placed -= 1;
}
None => {
let _ = std::fs::remove_file(&path);
}
for (away, dest) in &aside {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the regression also cover rollback of a delete when a later write fails? This branch restores every file moved aside, including deleted blobs, but the current failed-update test only verifies restoration of a replaced blob and the display name. A focused update that deletes an existing blob and then fails while installing a later write would assert the deleted blob is restored and the container remains unchanged. AI-assisted review (OpenAI GPT-6): I inspected the diff and tests; I did not run tests.

if let Err(e) = replace_file(away, dest) {
crate::xlog_warn!("XGameSave: a failed update could not put {} back from {}: {e}", dest.display(), away.display());
}
}
return crate::util::E_FAIL;
}
for (away, _) in &aside {
let _ = std::fs::remove_file(away);
}
S_OK
}
Expand Down Expand Up @@ -926,6 +966,64 @@ mod tests {
}
}

/// An update is all or nothing (#324): when one of its blobs cannot be written, the blobs
/// and the display name it wrote before stay as they were, and no temp file is left.
#[test]
fn a_failed_update_leaves_the_container_as_it_was() {
let _g = crate::hle::xgameruntime::xasync::ASYNC_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
unsafe {
let cfg = format!("xcl-test-324-{}\0", std::process::id());
let mut p = std::ptr::null_mut();
assert_eq!(XGameSaveInitializeProvider(std::ptr::null_mut(), cfg.as_ptr(), false, &mut p), S_OK);
let mut c = std::ptr::null_mut();
assert_eq!(XGameSaveCreateContainer(p, b"slot/324\0".as_ptr(), &mut c), S_OK);
let mut u = std::ptr::null_mut();
assert_eq!(XGameSaveCreateUpdate(c, b"Old\0".as_ptr(), &mut u), S_OK);
assert_eq!(XGameSaveSubmitBlobWrite(u, b"a\0".as_ptr(), b"old".as_ptr(), 3), S_OK);
assert_eq!(XGameSaveSubmitUpdate(u), S_OK);
XGameSaveCloseUpdate(u);
// the second blob of the next update cannot be written: a folder is in its place
let dir = (*p).root.join(enc("slot/324"));
std::fs::create_dir_all(dir.join("b").join(enc("b"))).unwrap();
let mut u = std::ptr::null_mut();
assert_eq!(XGameSaveCreateUpdate(c, b"New\0".as_ptr(), &mut u), S_OK);
assert_eq!(XGameSaveSubmitBlobWrite(u, b"a\0".as_ptr(), b"new".as_ptr(), 3), S_OK);
assert_eq!(XGameSaveSubmitBlobWrite(u, b"b\0".as_ptr(), b"x".as_ptr(), 1), S_OK);
assert_eq!(XGameSaveSubmitUpdate(u), crate::util::E_FAIL);
XGameSaveCloseUpdate(u);
assert_eq!(std::fs::read(dir.join("b").join(enc("a"))).unwrap(), b"old");
assert_eq!(std::fs::read_to_string(dir.join("display")).unwrap(), "Old");
let temps: Vec<String> = [dir.clone(), dir.join("b")]
.iter()
.flat_map(|d| std::fs::read_dir(d).unwrap().flatten())
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| n.starts_with(TMP_PREFIX))
.collect();
assert!(temps.is_empty(), "temp files left behind: {temps:?}");
// ops on one blob count in their order: write then delete, delete then write
std::fs::remove_dir(dir.join("b").join(enc("b"))).unwrap();
let mut u = std::ptr::null_mut();
assert_eq!(XGameSaveCreateUpdate(c, std::ptr::null(), &mut u), S_OK);
assert_eq!(XGameSaveSubmitBlobWrite(u, b"b\0".as_ptr(), b"x".as_ptr(), 1), S_OK);
assert_eq!(XGameSaveSubmitBlobDelete(u, b"b\0".as_ptr()), S_OK);
assert_eq!(XGameSaveSubmitBlobDelete(u, b"a\0".as_ptr()), S_OK);
assert_eq!(XGameSaveSubmitBlobWrite(u, b"a\0".as_ptr(), b"z".as_ptr(), 1), S_OK);
// one file on NTFS whatever the case, so the delete of META is the last op on Meta
assert_eq!(XGameSaveSubmitBlobWrite(u, b"Meta\0".as_ptr(), b"m".as_ptr(), 1), S_OK);
assert_eq!(XGameSaveSubmitBlobDelete(u, b"META\0".as_ptr()), S_OK);
assert_eq!(XGameSaveSubmitUpdate(u), S_OK);
XGameSaveCloseUpdate(u);
assert!(!dir.join("b").join(enc("b")).exists());
assert!(!dir.join("b").join(enc("Meta")).exists());
assert_eq!(std::fs::read(dir.join("b").join(enc("a"))).unwrap(), b"z");
assert_eq!(std::fs::read_to_string(dir.join("display")).unwrap(), "Old");
XGameSaveCloseContainer(c);
let root = (*p).root.clone();
XGameSaveCloseProvider(p);
let _ = std::fs::remove_dir_all(root);
}
}

#[test]
fn atomic_writes_preserve_xcltmp_blobs_and_skip_leftover_temps() {
let _g = crate::hle::xgameruntime::xasync::ASYNC_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
Expand Down Expand Up @@ -987,7 +1085,8 @@ mod tests {
let _ = std::fs::remove_file(&leftover);
let blocker = blobs_dir.join("dir_blocker");
std::fs::create_dir_all(&blocker).unwrap();
assert!(write_atomic(&blocker, b"cannot-replace-dir").is_err());
let ops = [("dir_blocker".to_string(), Some(b"cannot-replace-dir".to_vec()))];
assert_eq!(apply(blobs_dir.parent().unwrap(), None, &ops), crate::util::E_FAIL);
let remaining_tmps: Vec<_> = std::fs::read_dir(&blobs_dir)
.unwrap()
.flatten()
Expand Down
Loading