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-25 23:52:16 +00:00 by itsonewi · 1 comment
itsonewi commented 2026-11-25 23:52:16 +00:00 (Migrated from github.com)
Contributor

Imported from avatarmanz/VIctory-7900XTX. Originally posted by itsonewi on 2026-11-25T23:52:16Z.


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.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6616204396 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/issues/30). Originally posted by [itsonewi](https://github.com/itsonewi) on 2026-11-25T23:52:16Z. --- `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.
avatarmanz commented 2026-11-26 08:19:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from avatarmanz/VIctory-7900XTX. Originally posted by avatarmanz on 2026-11-26T08:19:09Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7186450073 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/issues/30#issuecomment-7186450073). Originally posted by [avatarmanz](https://github.com/avatarmanz) on 2026-11-26T08:19:09Z. --- Fixed by #123, now in main (0ce46d5a).
Sign in to join this conversation.
No labels
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.

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