xpackage: the package-byte walk does not follow links, explicitly #581

Open
qnjk wants to merge 1 commit from qnjk/Darner:xpackage-reparse-recursion into Main
Contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The reported problem does not reproduce on main, and this description says so first. The finding (H-5) claimed package_bytes recursed without bound through a directory reparse point. It does not: the walk recursed on DirEntry::metadata, and std's DirEntry::metadata does not traverse symlinks ("This function will not traverse symlinks"). Measured on this machine with rustc 1.98.1 and an NTFS junction: e.metadata() gives is_symlink = true, is_dir = false, len = 0, while Path::metadata/fs::metadata gives is_dir = true. So the old walk counted a junction as zero bytes and finished at once; the hazard is real but latent — one metadata accessor away.

  • What this change does, and why it is still worth a review: it lifts the walk out of package_bytes into fn folder_bytes(dir: &Path) -> u64, and gives it the iterative, symlink_metadata-skipping shape that storage::used_bytes already uses for the same class of hazard ("Links can escape this quota tree or point back to an ancestor", storage.rs:109-112), with saturating_add instead of sum(). The skip becomes explicit rather than resting on which accessor the loop happens to use, and the extracted function is testable. The count is unchanged for every real package: a junction contributes 0 both before and after.

How it was tested

  • Tested revision: f1074ca (one commit on c8258f9).
  • Commands and results:
    • cargo test --locked -p darner-hle --lib -- xpackage — 7 passed, 0 failed, including the new test.
    • New modules::xpackage::tests::folder_bytes_does_not_follow_a_link_to_an_ancestor: builds a temp folder with nested/file.bin of 100 bytes and a junction nested/loop -> root, then asserts folder_bytes(root) == 100. Junctions are used rather than symlink_dir because a directory symlink needs SeCreateSymbolicLinkPrivilege (measured here: symlinks denied, mklink /J succeeds); the test skips with a printed line if cmd/mklink cannot run.
    • Fail-before: the original code passes this test, which is the point of the paragraph above. The assertion's teeth were shown by running it against a deliberately link-following walk (the old code with std::fs::metadata(e.path()) instead of e.metadata(), which is what the finding believed the code did): that version fails the test.
    • cargo clippy --locked -p darner-hle --all-targets -- -D warnings passes.
  • Changes a default: no. The count is identical for every real package.
  • A/B result: not applicable — no default changes and no speed claim. The walk runs once, in a OnceLock, on its own thread.
  • Gates run on that revision:
    • Gate 1, cargo clippy --workspace --all-targets --locked -- -D warnings: passes.
    • Gate 2, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: passes.
    • Gate 3, cargo test --workspace --locked: not run to completion here; the pre-existing darner-gpu test deadlock (#224) and darner-shader waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. darner-hle's suite is green.
  • Gameplay, when relevant: no gameplay run; the package byte count is read from the title's own folder, and no title behaviour changes.
  • Shader wave sizes tested, when relevant: not applicable.
  • Hardware dependence: none.
  • Coverage gaps: only a junction to an ancestor is exercised; a junction to a different tree, and a file symlink inside the folder, are not.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in gameplay: the change cannot alter behaviour (the count is identical). The evidence is the test plus the measured fact that the old walk already skipped junctions.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md. (cargo xtask policy: 576 files ok.)
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found. No open pull request changes crates/darner-hle/src/modules/xpackage.rs (checked against the file list of all 189 open pull requests), and no open or closed issue mentions xpackage, package_bytes or a reparse point.
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The reported problem does **not** reproduce on `main`, and this description says so first. The finding (H-5) claimed `package_bytes` recursed without bound through a directory reparse point. It does not: the walk recursed on `DirEntry::metadata`, and std's `DirEntry::metadata` does not traverse symlinks ("This function will not traverse symlinks"). Measured on this machine with rustc 1.98.1 and an NTFS junction: `e.metadata()` gives `is_symlink = true, is_dir = false, len = 0`, while `Path::metadata`/`fs::metadata` gives `is_dir = true`. So the old walk counted a junction as zero bytes and finished at once; the hazard is real but latent — one metadata accessor away. - What this change does, and why it is still worth a review: it lifts the walk out of `package_bytes` into `fn folder_bytes(dir: &Path) -> u64`, and gives it the iterative, `symlink_metadata`-skipping shape that `storage::used_bytes` already uses for the same class of hazard ("Links can escape this quota tree or point back to an ancestor", `storage.rs:109-112`), with `saturating_add` instead of `sum()`. The skip becomes explicit rather than resting on which accessor the loop happens to use, and the extracted function is testable. The count is unchanged for every real package: a junction contributes 0 both before and after. ## How it was tested - Tested revision: `f1074ca` (one commit on `c8258f9`). - Commands and results: - `cargo test --locked -p darner-hle --lib -- xpackage` — 7 passed, 0 failed, including the new test. - New `modules::xpackage::tests::folder_bytes_does_not_follow_a_link_to_an_ancestor`: builds a temp folder with `nested/file.bin` of 100 bytes and a junction `nested/loop -> root`, then asserts `folder_bytes(root) == 100`. Junctions are used rather than `symlink_dir` because a directory symlink needs `SeCreateSymbolicLinkPrivilege` (measured here: symlinks denied, `mklink /J` succeeds); the test skips with a printed line if `cmd`/`mklink` cannot run. - Fail-before: **the original code passes this test**, which is the point of the paragraph above. The assertion's teeth were shown by running it against a deliberately link-*following* walk (the old code with `std::fs::metadata(e.path())` instead of `e.metadata()`, which is what the finding believed the code did): that version fails the test. - `cargo clippy --locked -p darner-hle --all-targets -- -D warnings` passes. - Changes a default: no. The count is identical for every real package. - A/B result: not applicable — no default changes and no speed claim. The walk runs once, in a `OnceLock`, on its own thread. - Gates run on that revision: - Gate 1, `cargo clippy --workspace --all-targets --locked -- -D warnings`: passes. - Gate 2, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: passes. - Gate 3, `cargo test --workspace --locked`: not run to completion here; the pre-existing `darner-gpu` test deadlock (#224) and `darner-shader` waterfall fixture failure (#227) make the whole-workspace run non-green on this machine. `darner-hle`'s suite is green. - Gameplay, when relevant: no gameplay run; the package byte count is read from the title's own folder, and no title behaviour changes. - Shader wave sizes tested, when relevant: not applicable. - Hardware dependence: none. - Coverage gaps: only a junction to an ancestor is exercised; a junction to a different tree, and a file symlink inside the folder, are not. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in gameplay: the change cannot alter behaviour (the count is identical). The evidence is the test plus the measured fact that the old walk already skipped junctions. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. (`cargo xtask policy`: 576 files ok.) - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found. No open pull request changes `crates/darner-hle/src/modules/xpackage.rs` (checked against the file list of all 189 open pull requests), and no open or closed issue mentions `xpackage`, `package_bytes` or a reparse point. - Replaces or overlaps: none found
`package_bytes` walked the title's folder with a recursive `size` that
recurred into anything `DirEntry::metadata` reported as a directory. A
junction inside the package can point at one of its own ancestors — what a
hand-made `mklink /J` leaves behind when an install is moved to another
drive — and a walk that followed it would count the same files for ever on
a thread whose stack it would exhaust: `STATUS_STACK_OVERFLOW`, an abort
rather than a panic the title could survive.

`DirEntry::metadata` does not follow a link on the platforms we build for,
so the old walk counted a junction as zero bytes instead of recurring; the
hazard is one metadata accessor away, and `storage::used_bytes` already
spells the skip out for the same reason ("Links can escape this quota tree
or point back to an ancestor"). The walk is now that same iterative,
link-skipping shape, and `folder_bytes` is lifted out of `package_bytes` so
a real junction built under a temp directory holds it: a walk that followed
the link overflows the test thread's stack, the walk as written adds
nothing to the count.
TrustNo1 changed title from [pr-refactor] xpackage: the package-byte walk does not follow links, explicitly to xpackage: the package-byte walk does not follow links, explicitly 2026-12-06 03:26:52 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'xpackage-reparse-recursion:qnjk-xpackage-reparse-recursion'
git switch 'qnjk-xpackage-reparse-recursion'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'Main'
git merge --no-ff 'qnjk-xpackage-reparse-recursion'
git switch 'qnjk-xpackage-reparse-recursion'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'qnjk-xpackage-reparse-recursion'
git switch 'qnjk-xpackage-reparse-recursion'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'qnjk-xpackage-reparse-recursion'
git switch 'Main'
git merge --squash 'qnjk-xpackage-reparse-recursion'
git switch 'Main'
git merge --ff-only 'qnjk-xpackage-reparse-recursion'
git switch 'Main'
git merge 'qnjk-xpackage-reparse-recursion'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
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
TrustNo1/Darner!581
No description provided.