Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
92 changes: 92 additions & 0 deletions internal/index/rename_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
31 changes: 31 additions & 0 deletions internal/journal/rename_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions internal/journal/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading