Skip to content

fix: update parent directory nlink on mkdir/rmdir/rename#374

Draft
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-parent-nlink-on-mkdir-rmdir
Draft

fix: update parent directory nlink on mkdir/rmdir/rename#374
toddr-bot wants to merge 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-parent-nlink-on-mkdir-rmdir

Conversation

@toddr-bot
Copy link
Copy Markdown
Collaborator

What

Maintain accurate nlink on parent directories when subdirectories are created, removed, or renamed across parents.

Why

POSIX mandates that a directory's nlink equals 2 + number of immediate subdirectories (each child's .. entry is a hard link to the parent). MockFile was not updating parent nlink on mkdir(), rmdir(), or cross-parent rename(), causing (stat $dir)[3] to return stale values. Code that relies on nlink to detect subdirectory presence (e.g., find optimizations, File::Find) would see incorrect results.

How

  • __mkdir: increment parent dir's nlink after creating the subdirectory
  • __rmdir: decrement parent dir's nlink after removing the subdirectory (floored at 2)
  • __rename: when a directory moves between parents, decrement old parent and increment new parent; same-parent rename is a no-op for nlink

Testing

New t/parent_nlink.t with 8 subtests covering: single/multiple mkdir, rmdir, floor guard, file unlink (no effect), cross-parent rename, same-parent rename, file rename (no effect). Full test suite passes (only pre-existing fh-ref-leak.t failure).

🤖 Generated with Claude Code

POSIX mandates that a directory's nlink reflects the count of its
subdirectories (each subdirectory's ".." entry is a hard link back to
the parent).  Previously, mkdir() and rmdir() did not update the
parent directory's nlink, and rename() of a directory between parents
also left both parents' nlink unchanged.

Now:
- mkdir increments parent dir nlink by 1
- rmdir decrements parent dir nlink by 1 (floored at 2)
- rename of a directory across parents decrements old parent, increments new

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant