Skip to content

fix(index): make rename onto the same inode a no-op - #31

Merged
tofarr merged 1 commit into
mainfrom
fix-rename-onto-self
Dec 3, 2026
Merged

tofarr merged 1 commit into
mainfrom
fix-rename-onto-self

Conversation

@tofarr

@tofarr tofarr commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Why

Index.Rename assumed that an existing destination entry was always a different file to replace. When the destination was the same inode as the source, it lowered the link count to zero and deleted the inode. This happened for rename("x", "x") and for two hard links to one file. It then added a directory entry that pointed at the deleted inode. The next operation on that entry hit a nil inode and crashed.

POSIX says these renames do nothing. Index.Rename now returns early when the destination entry is the source inode, and both names stay. The journal wrapper had the same problem. It treated the file as a replaced destination and recorded a DeleteInode op, so replaying the journal would delete the file. JournaledIndex.Rename now records nothing in this case.

Validation

  • New index tests — a file renamed onto itself keeps its inode, extents, link count and entry. Two hard links keep both names. A non-empty directory renamed onto itself keeps its contents. A missing source still returns an error.
  • New journal test — a rename onto itself adds no entries to the journal.
  • Without the fix — three of the new index tests and the new journal test fail. A random-operation test that renames entries crashed with a nil pointer on the unfixed code.

This PR was drafted by an AI agent on behalf of the user.

@tofarr can click here to continue refining the PR

Rename(p, "x", p, "x") treated the source as its own replaced destination:
it dropped nlink to zero and freed the inode, then re-added a directory entry
pointing at it, leaving a dangling entry (nil inode on the next access). The
same happened for two hard links to one file. POSIX specifies a no-op.

Index.Rename now returns early when the destination entry is the source inode.
JournaledIndex.Rename records nothing in that case; it would otherwise emit a
DeleteInode that makes journal replay delete the file.

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr marked this pull request as ready for review December 3, 2026 13:29

@hieptl hieptl left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙏

@tofarr
tofarr merged commit 98ede30 into main Dec 3, 2026
1 check passed
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.

2 participants