XSystem storage: a save folder that cannot be read reports 0 used and the full quota free, and every space query re-walks the whole folder #30

Closed
opened 2026-11-26 03:31:28 +00:00 by github-bot · 1 comment
Owner

@⁠itsonewi on GitHub · 2026-11-25 23:52 UTC · original

XSystem's storage space has one reading that can be wrong in the dangerous direction (0 used, full quota free) and one cost that is paid on every query.

1. A storage folder that cannot be read reports "nothing used"

crates/victory-hle/src/modules/storage.rs:90-102:

fn used_bytes(dir: &Path) -> u64 {
    let Ok(entries) = std::fs::read_dir(dir) else { return 0; };
    entries.flatten().map(|e| match e.metadata() {
        Ok(m) if m.is_dir() => used_bytes(&e.path()),
        Ok(m) => m.len(),
        Err(_) => 0,
    }).sum()
}

and space() at :129-140 turns that into what the title sees:

let used = used_bytes(&title.storage);
let free = title.storage_bytes.saturating_sub(used);

So a missing folder, a folder the process cannot enumerate (permissions, an AV scanner holding it, a removed drive letter), or a subdirectory that cannot be walked all report used = 0, total_free = quota. prompt_for_space (:150-157) then grants any request, and the title writes until the host disk is full — with no log line anywhere saying the accounting failed. The unusable case and the empty case are the same value.

A wrong reading in the other direction is safe (free saturates at 0), so the only failure is the one that lets a title write into a full disk believing it has the whole quota.

Fix: keep "cannot read" distinct — return the HRESULT (E_FAIL) or count the unreadable entries as the quota's worth of used, and log once with the path and error.

2. Every query walks the whole save folder

space() is called by space_info (slot 0x28, :142-144) and by prompt_for_space (:150), and the file's own comment (:29-30) says the title "calls 0x20 (path) and 0x28 (space) with the same arguments", i.e. repeated calls are the expected use. Every one of them stats every file under the storage folder, recursively, on the title's own thread.

A save folder of a few thousand entries turns a cheap status query into thousands of FindFirstFile/GetFileAttributes calls. The used total only changes through our own file functions (files.rs already intercepts CreateFileW/CreateFileA/CreateFile2 and the write/close paths), so it can be kept as a running total and refreshed when the folder is created or when a handle closes, instead of recounted per query.

Not checked

Source reading at 0a43566. I did not run the title and did not measure the walk; the number of entries in the title's storage folder is not known to me, so the cost is stated from the code shape, not from a measurement.

AI use

AI-assisted review (source reading). No build, no run, no game files.

**@⁠itsonewi** on GitHub · 2026-11-25 23:52 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/30) `XSystem`'s storage space has one reading that can be wrong in the dangerous direction (0 used, full quota free) and one cost that is paid on every query. ## 1. A storage folder that cannot be read reports "nothing used" `crates/victory-hle/src/modules/storage.rs:90-102`: ```rust fn used_bytes(dir: &Path) -> u64 { let Ok(entries) = std::fs::read_dir(dir) else { return 0; }; entries.flatten().map(|e| match e.metadata() { Ok(m) if m.is_dir() => used_bytes(&e.path()), Ok(m) => m.len(), Err(_) => 0, }).sum() } ``` and `space()` at `:129-140` turns that into what the title sees: ```rust let used = used_bytes(&title.storage); let free = title.storage_bytes.saturating_sub(used); ``` So a missing folder, a folder the process cannot enumerate (permissions, an AV scanner holding it, a removed drive letter), or a subdirectory that cannot be walked all report `used = 0`, `total_free = quota`. `prompt_for_space` (`:150-157`) then grants any request, and the title writes until the host disk is full — with no log line anywhere saying the accounting failed. The unusable case and the empty case are the same value. A wrong reading in the other direction is safe (`free` saturates at 0), so the only failure is the one that lets a title write into a full disk believing it has the whole quota. Fix: keep "cannot read" distinct — return the HRESULT (`E_FAIL`) or count the unreadable entries as the quota's worth of used, and log once with the path and error. ## 2. Every query walks the whole save folder `space()` is called by `space_info` (slot 0x28, `:142-144`) and by `prompt_for_space` (`:150`), and the file's own comment (`:29-30`) says the title "calls 0x20 (path) and 0x28 (space) with the same arguments", i.e. repeated calls are the expected use. Every one of them stats every file under the storage folder, recursively, on the title's own thread. A save folder of a few thousand entries turns a cheap status query into thousands of `FindFirstFile`/`GetFileAttributes` calls. The used total only changes through our own file functions (`files.rs` already intercepts `CreateFileW`/`CreateFileA`/`CreateFile2` and the write/close paths), so it can be kept as a running total and refreshed when the folder is created or when a handle closes, instead of recounted per query. ## Not checked Source reading at `0a43566`. I did not run the title and did not measure the walk; the number of entries in the title's storage folder is not known to me, so the cost is stated from the code shape, not from a measurement. ## AI use AI-assisted review (source reading). No build, no run, no game files.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:31:28 +00:00
github-bot 2026-11-26 08:24:50 +00:00
Author
Owner

@⁠avatarmanz on GitHub · 2026-11-26 08:19 UTC · original

Fixed by #123, now in main (0ce46d5a).

**@⁠avatarmanz** on GitHub · 2026-11-26 08:19 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/30#issuecomment-7186450073) Fixed by #123, now in main (0ce46d5a).
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/avatarmanz--VIctory-7900XTX#30
No description provided.