From 96244fd9caaa7e150437de74d6b2557375a793d1 Mon Sep 17 00:00:00 2001 From: tofarr Date: Thu, 3 Dec 2026 12:28:49 +0000 Subject: [PATCH] fix(index): make rename onto the same inode a no-op 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 --- internal/index/index.go | 6 +++ internal/index/rename_test.go | 92 +++++++++++++++++++++++++++++++++ internal/journal/rename_test.go | 31 +++++++++++ internal/journal/wrap.go | 6 +++ 4 files changed, 135 insertions(+) create mode 100644 internal/index/rename_test.go create mode 100644 internal/journal/rename_test.go diff --git a/internal/index/index.go b/internal/index/index.go index f5fd9b6..0c1b1b3 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -558,6 +558,12 @@ func (idx *Index) Rename(srcParent uint64, srcName string, dstParent uint64, dst } srcChild := idx.inodes[srcChildIno] + // POSIX: if both names refer to the same inode (including src == dst), + // rename does nothing. Falling through would free the inode we are moving. + if dstChildIno, exists := idx.dirIndex[dstParent][dstName]; exists && dstChildIno == srcChildIno { + return nil + } + // If destination exists, remove it first (must be empty if a directory). if dstChildIno, exists := idx.dirIndex[dstParent][dstName]; exists { dstChild := idx.inodes[dstChildIno] diff --git a/internal/index/rename_test.go b/internal/index/rename_test.go new file mode 100644 index 0000000..cc5e7b9 --- /dev/null +++ b/internal/index/rename_test.go @@ -0,0 +1,92 @@ +package index + +import "testing" + +func TestRenameOntoSelfIsNoop(t *testing.T) { + idx := New(4096) + n := now() + ino, err := idx.MakeFile(RootIno, "f", 0o644, 0, 0, 0, n) + if err != nil { + t.Fatal(err) + } + if err := idx.WriteExtent(ino, Extent{ChunkID: "chunk-00000000", Length: 10}, n); err != nil { + t.Fatal(err) + } + + if err := idx.Rename(RootIno, "f", RootIno, "f", n+1); err != nil { + t.Fatalf("rename onto itself: %v", err) + } + + got, ok := idx.Lookup(RootIno, "f") + if !ok || got != ino { + t.Fatalf("entry f: got %d, %v; want %d, true", got, ok, ino) + } + inode, ok := idx.GetInode(ino) + if !ok { + t.Fatal("inode was freed by renaming onto itself") + } + if inode.Nlink != 1 || inode.Size != 10 { + t.Errorf("inode changed: nlink=%d size=%d, want 1, 10", inode.Nlink, inode.Size) + } + if exts, _ := idx.GetExtents(ino); len(exts) != 1 { + t.Errorf("extents lost: %+v", exts) + } + checkInvariants(t, idx) + + // The entry must still be usable afterwards. + if err := idx.Rename(RootIno, "f", RootIno, "g", n+2); err != nil { + t.Fatalf("rename after no-op: %v", err) + } + checkInvariants(t, idx) +} + +func TestRenameBetweenHardLinksKeepsBothNames(t *testing.T) { + idx := New(4096) + n := now() + ino, _ := idx.MakeFile(RootIno, "a", 0o644, 0, 0, 0, n) + if err := idx.AddDirEntry(RootIno, "b", ino, n); err != nil { + t.Fatal(err) + } + + if err := idx.Rename(RootIno, "a", RootIno, "b", n+1); err != nil { + t.Fatalf("rename between hard links: %v", err) + } + + for _, name := range []string{"a", "b"} { + if got, ok := idx.Lookup(RootIno, name); !ok || got != ino { + t.Errorf("entry %q: got %d, %v; want %d, true", name, got, ok, ino) + } + } + if inode, _ := idx.GetInode(ino); inode.Nlink != 2 { + t.Errorf("nlink=%d, want 2", inode.Nlink) + } + checkInvariants(t, idx) +} + +func TestRenameDirectoryOntoSelfIsNoop(t *testing.T) { + idx := New(4096) + n := now() + dir, _ := idx.MakeDir(RootIno, "d", 0o755, 0, 0, n) + if _, err := idx.MakeFile(dir, "child", 0o644, 0, 0, 0, n); err != nil { + t.Fatal(err) + } + + if err := idx.Rename(RootIno, "d", RootIno, "d", n+1); err != nil { + t.Fatalf("rename non-empty directory onto itself: %v", err) + } + + if got, ok := idx.Lookup(RootIno, "d"); !ok || got != dir { + t.Errorf("entry d: got %d, %v; want %d, true", got, ok, dir) + } + if _, ok := idx.Lookup(dir, "child"); !ok { + t.Error("directory contents lost") + } + checkInvariants(t, idx) +} + +func TestRenameMissingSourceStillFails(t *testing.T) { + idx := New(4096) + if err := idx.Rename(RootIno, "nope", RootIno, "nope", now()); err == nil { + t.Error("want error renaming a nonexistent source onto itself") + } +} diff --git a/internal/journal/rename_test.go b/internal/journal/rename_test.go new file mode 100644 index 0000000..d8aed4c --- /dev/null +++ b/internal/journal/rename_test.go @@ -0,0 +1,31 @@ +package journal + +import ( + "testing" + "time" + + "github.com/tofarr/fusey/internal/index" +) + +func TestJournaledIndexRenameOntoSelfRecordsNothing(t *testing.T) { + idx := index.New(4096) + j := New(true) + ji := Wrap(idx, j) + now := time.Now().UnixNano() + + ino, _ := ji.CreateInode(index.Regular, 0o644, 0, 0, 0, now) + _ = ji.AddDirEntry(1, "f", ino, now+1) + before := j.BufferLen() + + if err := ji.Rename(1, "f", 1, "f", now+2); err != nil { + t.Fatalf("rename onto itself: %v", err) + } + + // A DeleteInode here would make replaying the journal delete the file. + if got := j.BufferLen() - before; got != 0 { + t.Errorf("recorded %d ops for a no-op rename: %+v", got, j.buffer[before:]) + } + if got, ok := ji.Lookup(1, "f"); !ok || got != ino { + t.Errorf("entry f: got %d, %v; want %d, true", got, ok, ino) + } +} diff --git a/internal/journal/wrap.go b/internal/journal/wrap.go index 7af6947..5333187 100644 --- a/internal/journal/wrap.go +++ b/internal/journal/wrap.go @@ -162,7 +162,13 @@ func (ji *JournaledIndex) Rename(srcParent uint64, srcName string, dstParent uin // replacement and potential deletion. var replacedIno uint64 var replacedNlink1 bool + srcIno, srcFound := ji.Index.Lookup(srcParent, srcName) if ino, ok := ji.Index.Lookup(dstParent, dstName); ok { + if srcFound && ino == srcIno { + // Both names are the same inode: Index.Rename is a no-op, so there + // is nothing to record (and nothing is replaced or freed). + return ji.Index.Rename(srcParent, srcName, dstParent, dstName, now) + } replacedIno = ino if inode, ok := ji.Index.GetInode(ino); ok && inode.Nlink == 1 { replacedNlink1 = true