Storage: do not follow directory symlinks or junctions when computing used bytes #1928

Closed
github-bot wants to merge 1 commit from gh-pr/1928/head into gh-pr/1928/base
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 01:07 UTC · original
ashrafsdrop:storage/symlink-recursion-040 → main · +21 −6 · 2 files · 1 commits · closed without merging

Resolves VIC-BT-040 from #1249:

Summary

When calculating disk space consumption via \used_bytes\ in \crates/victory-hle/src/modules/storage.rs\ and \crates/victory-hle/src/modules/xpackage.rs:

  • Using \entry.metadata()\ follows filesystem symlinks and Windows NTFS directory junctions.
  • If a title creates recursive junctions or cyclic directory links within its save / package storage, traversal hangs in an infinite recursion loop until stack exhaustion.
  • Replaces \e.metadata()\ with \std::fs::symlink_metadata(e.path()), matching:
    • \Ok(m) if m.is_dir() => used_bytes(&e.path())\ (true physical directories only; symlinks/junctions report \ alse\ for \is_dir())
    • \Ok(m) if m.is_file() => m.len()\
    • _ => 0\
  • Adds unit test \used_bytes_does_not_follow_symlinks\ in \storage.rs.

Verification

  • \cargo test -p victory-hle: 144 tests passed, 0 failed.
  • \cargo clippy -p victory-hle --all-targets --all-features -- -D warnings: clean, 0 warnings.
  • \cargo run -p xtask -- policy: 182 files ok.
**@⁠ashrafsdrop** on GitHub · 2026-11-25 01:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1928) `ashrafsdrop:storage/symlink-recursion-040` → `main` · +21 −6 · 2 files · 1 commits · closed without merging Resolves VIC-BT-040 from #1249: ### Summary When calculating disk space consumption via \used_bytes\ in \crates/victory-hle/src/modules/storage.rs\ and \crates/victory-hle/src/modules/xpackage.rs\: - Using \entry.metadata()\ follows filesystem symlinks and Windows NTFS directory junctions. - If a title creates recursive junctions or cyclic directory links within its save / package storage, traversal hangs in an infinite recursion loop until stack exhaustion. - Replaces \e.metadata()\ with \std::fs::symlink_metadata(e.path())\, matching: - \Ok(m) if m.is_dir() => used_bytes(&e.path())\ (true physical directories only; symlinks/junctions report \ alse\ for \is_dir()\) - \Ok(m) if m.is_file() => m.len()\ - \_ => 0\ - Adds unit test \used_bytes_does_not_follow_symlinks\ in \storage.rs\. ### Verification - \cargo test -p victory-hle\: 144 tests passed, 0 failed. - \cargo clippy -p victory-hle --all-targets --all-features -- -D warnings\: clean, 0 warnings. - \cargo run -p xtask -- policy\: 182 files ok.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:02 +00:00
github-bot closed this pull request 2026-11-26 04:49:02 +00:00
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-25 02:26 UTC · original

Checked this on Windows 11 26300 (NTFS, Rust 1.98.1): main doesn't follow junctions or directory symlinks here, so the hang in VIC-BT-040 doesn't happen on Windows.

On Windows, DirEntry::metadata() comes from the FindNextFileW data and doesn't traverse links (the std docs say so for Windows). A standalone probe with main's used_bytes body, a 1,000-byte file, and a junction (mklink /J) from a subfolder back to the root:

"loop": DirEntry::metadata is_dir=false is_symlink=true  fs::metadata (follows) .is_dir=Ok(true)
used_bytes = 1000 bytes, 2 directory visits

So e.metadata() already behaves like symlink_metadata, and the junction counts as a non-directory entry of length 0.

This PR's test doesn't tell the two apart either. With only its test module put on main's storage.rs, used_bytes_does_not_follow_symlinks passes, and with the PR it passes too. That's on a machine with developer mode on, so symlink_dir really made the link. Where symlinks need privileges, let _ = symlink_dir(...) fails silently and the test checks nothing.

What the PR does change:

  • Links now count 0 rather than their entry's length. That's 0 for junctions anyway; I didn't check file symlinks.
  • One more symlink_metadata call per entry, where DirEntry::metadata is free on Windows.

If the aim is Unix builds, where DirEntry::metadata does follow links, that's worth saying in the description. A test there would need to fail on the old code, for example by assert!ing that the link was made and counting visits.

AI use: Claude Code (Claude Opus 5.5) wrote the probe, ran the checks and wrote this; the results are from the runs described.

**@⁠AgneyaK** on GitHub · 2026-11-25 02:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/1928#issuecomment-7150732115) Checked this on Windows 11 26300 (NTFS, Rust 1.98.1): **`main` doesn't follow junctions or directory symlinks here, so the hang in VIC-BT-040 doesn't happen on Windows.** On Windows, `DirEntry::metadata()` comes from the `FindNextFileW` data and doesn't traverse links (the std docs say so for Windows). A standalone probe with `main`'s `used_bytes` body, a 1,000-byte file, and a junction (`mklink /J`) from a subfolder back to the root: ``` "loop": DirEntry::metadata is_dir=false is_symlink=true fs::metadata (follows) .is_dir=Ok(true) used_bytes = 1000 bytes, 2 directory visits ``` So `e.metadata()` already behaves like `symlink_metadata`, and the junction counts as a non-directory entry of length 0. This PR's test doesn't tell the two apart either. With only its test module put on `main`'s `storage.rs`, `used_bytes_does_not_follow_symlinks` passes, and with the PR it passes too. That's on a machine with developer mode on, so `symlink_dir` really made the link. Where symlinks need privileges, `let _ = symlink_dir(...)` fails silently and the test checks nothing. What the PR does change: - **Links now count 0 rather than their entry's length.** That's 0 for junctions anyway; I didn't check file symlinks. - **One more `symlink_metadata` call per entry,** where `DirEntry::metadata` is free on Windows. If the aim is Unix builds, where `DirEntry::metadata` does follow links, that's worth saying in the description. A test there would need to fail on the old code, for example by `assert!`ing that the link was made and counting visits. AI use: Claude Code (Claude Opus 5.5) wrote the probe, ran the checks and wrote this; the results are from the runs described.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:16 UTC · original

Automated triage for the maintainers.

Proposed close.

The bug this fixes is not present on main. used_bytes in crates/victory-hle/src/modules/storage.rs (and size in package_bytes, xpackage.rs) uses DirEntry::metadata(), and std documents that this does not traverse symlinks. On Windows it comes straight from the FindNextFileW data, so a junction or directory symlink already reports is_dir() == false and is never recursed into. AgneyaK's comment above confirms this with a junction probe on Windows. The new test passes on main as well. It also ignores the result of symlink_dir (let _ =), so where creating the link fails it checks nothing. The only real change is that a link entry now counts as 0 instead of its own length, plus an extra symlink_metadata call per entry.

CI: check failing.

This PR will be closed in 48 hours unless the author pushes a change (for example, a test that fails on main) or a maintainer removes the label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1928#issuecomment-7156833164) <!-- victory-triage v1 head=d8c8ae6b7562e1b1118a305112f4fe3c7f950876 verdict=proposed-close at=2026-11-25T08:16:45Z --> Automated triage for the maintainers. **Proposed close.** The bug this fixes is not present on `main`. `used_bytes` in `crates/victory-hle/src/modules/storage.rs` (and `size` in `package_bytes`, `xpackage.rs`) uses `DirEntry::metadata()`, and std documents that this does not traverse symlinks. On Windows it comes straight from the `FindNextFileW` data, so a junction or directory symlink already reports `is_dir() == false` and is never recursed into. AgneyaK's comment above confirms this with a junction probe on Windows. The new test passes on `main` as well. It also ignores the result of `symlink_dir` (`let _ =`), so where creating the link fails it checks nothing. The only real change is that a link entry now counts as 0 instead of its own length, plus an extra `symlink_metadata` call per entry. CI: `check` failing. This PR will be closed in 48 hours unless the author pushes a change (for example, a test that fails on `main`) or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠ashrafsdrop on GitHub · 2026-11-25 11:05 UTC · original

Closing this pull request based on the triage verification. On Windows, std::fs::DirEntry::metadata() is read directly from FindNextFileW data without following reparse points or junctions (is_dir() == false), so directory cycle recursion cannot occur in used_bytes. Thank you for the detailed probe and review.

**@⁠ashrafsdrop** on GitHub · 2026-11-25 11:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1928#issuecomment-7160584611) Closing this pull request based on the triage verification. On Windows, `std::fs::DirEntry::metadata()` is read directly from `FindNextFileW` data without following reparse points or junctions (`is_dir() == false`), so directory cycle recursion cannot occur in `used_bytes`. Thank you for the detailed probe and review.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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/Marvisak--VIctory!1928
No description provided.