Преглед изворни кода

Merge pull request #429 from marquezinii/fix/xgamesave-documented-errors

XGameSave: the documented errors for a full disk and a small read buffer
Marvis пре 2 дана
родитељ
комит
f0894c2b5c
1 измењених фајлова са 38 додато и 2 уклоњено
  1. 38 2
      crates/victory-hle/src/modules/xgamesave.rs

+ 38 - 2
crates/victory-hle/src/modules/xgamesave.rs

@@ -396,6 +396,21 @@ fn remaining(root: &Path) -> u64 {
         .min(storage::disk_free(root))
         .min(storage::disk_free(root))
 }
 }
 
 
+/// Why an update writing `written` bytes, `replaced` of them over blobs it
+/// replaces, does not fit: past the per-user quota (`used` bytes kept), or
+/// past the host disk's `free` bytes, which is not the user's quota (the
+/// new version is written before the old one goes, so nothing it replaces
+/// counts there).
+fn no_room(written: u64, replaced: u64, used: u64, free: u64) -> Option<HRESULT> {
+    if written > QUOTA.saturating_sub(used) + replaced {
+        Some(QUOTA_EXCEEDED)
+    } else if written > free {
+        Some(OUT_OF_LOCAL_STORAGE)
+    } else {
+        None
+    }
+}
+
 fn io_error(dir: &Path, e: &std::io::Error) -> HRESULT {
 fn io_error(dir: &Path, e: &std::io::Error) -> HRESULT {
     tracing::warn!(dir = %dir.display(), %e, "game save");
     tracing::warn!(dir = %dir.display(), %e, "game save");
     if e.kind() == std::io::ErrorKind::StorageFull {
     if e.kind() == std::io::ErrorKind::StorageFull {
@@ -449,8 +464,9 @@ impl Update {
             changed.map(|(_, size)| size).sum()
             changed.map(|(_, size)| size).sum()
         });
         });
         let root = self.dir.parent().unwrap_or(&self.dir);
         let root = self.dir.parent().unwrap_or(&self.dir);
-        if written as u64 > remaining(root) + replaced {
-            return QUOTA_EXCEEDED;
+        let free = storage::disk_free(root);
+        if let Some(hr) = no_room(written as u64, replaced, used_bytes(root), free) {
+            return hr;
         }
         }
         let display = self.display.as_deref();
         let display = self.display.as_deref();
         let display = display.or(current.as_ref().map(|v| v.display.as_str()));
         let display = display.or(current.as_ref().map(|v| v.display.as_str()));
@@ -789,6 +805,11 @@ extern "system" fn read_result(
 ) -> HRESULT {
 ) -> HRESULT {
     let mut used = 0;
     let mut used = 0;
     let hr = xasync::XAsyncGetResult(block, (&raw const READ).cast(), size, out.cast(), &mut used);
     let hr = xasync::XAsyncGetResult(block, (&raw const READ).cast(), size, out.cast(), &mut used);
+    // The documented error for a small buffer; the result stays for a
+    // retry with a larger one.
+    if hr == xasync::E_NOT_SUFFICIENT_BUFFER {
+        return BUFFER_TOO_SMALL;
+    }
     if hr < 0 {
     if hr < 0 {
         return hr;
         return hr;
     }
     }
@@ -948,6 +969,17 @@ mod tests {
         }
         }
     }
     }
 
 
+    /// The per-user quota and the host disk fail with their own errors.
+    #[test]
+    fn a_full_disk_is_not_the_quota() {
+        let mb = 1 << 20;
+        assert_eq!(no_room(2 * mb, 0, 0, mb), Some(OUT_OF_LOCAL_STORAGE));
+        assert_eq!(no_room(2 * mb, 0, QUOTA - mb, 100 * mb), Some(QUOTA_EXCEEDED));
+        // Replacing a blob of the same size fits a full quota.
+        assert_eq!(no_room(mb, mb, QUOTA, 100 * mb), None);
+        assert_eq!(no_room(mb, 0, 0, mb), None);
+    }
+
     fn update(root: &Path, display: &str, changes: &[(&str, Option<&str>)]) -> HRESULT {
     fn update(root: &Path, display: &str, changes: &[(&str, Option<&str>)]) -> HRESULT {
         Update {
         Update {
             name: "Slot".into(),
             name: "Slot".into(),
@@ -1237,6 +1269,10 @@ mod tests {
         );
         );
         let mut buffer = vec![0u64; size.div_ceil(8)];
         let mut buffer = vec![0u64; size.div_ceil(8)];
         let mut count = 0;
         let mut count = 0;
+        assert_eq!(
+            read_result(this, &mut b, 8, buffer.as_mut_ptr().cast(), &mut count),
+            BUFFER_TOO_SMALL
+        );
         assert_eq!(
         assert_eq!(
             read_result(this, &mut b, size, buffer.as_mut_ptr().cast(), &mut count),
             read_result(this, &mut b, size, buffer.as_mut_ptr().cast(), &mut count),
             S_OK
             S_OK