Skip to content

Commit

Permalink
Fix dirHandler: readOff and index offset are not comparable (#5242)
Browse files Browse the repository at this point in the history
Signed-off-by: Changxin Miao <[email protected]>
  • Loading branch information
polyrabbit authored Oct 22, 2024
1 parent 406c486 commit 4f8e211
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3032,7 +3032,7 @@ func (h *dirHandler) Delete(name string) {
return
}

if idx, ok := h.batch.indexes[name]; ok && idx >= h.readOff {
if idx, ok := h.batch.indexes[name]; ok && idx+h.batch.offset >= h.readOff {
delete(h.batch.indexes, name)
n := len(h.batch.entries)
if idx < n-1 {
Expand All @@ -3058,7 +3058,7 @@ func (h *dirHandler) Insert(inode Ino, name string, attr *Attr) {
}

func (h *dirHandler) Read(offset int) {
h.readOff = offset - len(h.initEntries)
h.readOff = offset - len(h.initEntries) // TODO: what if fuse only reads one entry?
}

func (h *dirHandler) Close() {
Expand Down
30 changes: 21 additions & 9 deletions pkg/vfs/vfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,12 +901,12 @@ func testReaddirCache(t *testing.T, metaUri string, typ string, batchNum int) {
}
parent := entry.Inode
for i := 0; i <= 100; i++ {
_, _ = v.Mkdir(ctx, parent, fmt.Sprintf("d%d", i), 0777, 022)
_, _ = v.Mkdir(ctx, parent, fmt.Sprintf("d%03d", i), 0777, 022)
}

defer func() {
for i := 0; i <= 120; i++ {
_ = v.Rmdir(ctx, parent, fmt.Sprintf("d%d", i))
_ = v.Rmdir(ctx, parent, fmt.Sprintf("d%03d", i))
}
_ = v.Rmdir(ctx, 1, "testdir")
}()
Expand All @@ -922,16 +922,28 @@ func testReaddirCache(t *testing.T, metaUri string, typ string, batchNum int) {
}

off := num + initNum
{
entries, _, _ = v.Readdir(ctx, parent, 20, off, fh, true) // read next 20
v.UpdateReaddirOffset(ctx, parent, fh, off+1) // but readdir buffer is too full to return all entries
name := fmt.Sprintf("d%03d", off+2)
_ = v.Rmdir(ctx, parent, name)
entries, _, _ = v.Readdir(ctx, parent, 20, off, fh, true) // should only get 19 entries
for _, e := range entries {
if string(e.Name) == name {
t.Fatalf("dir %s should be deleted", name)
}
}
}
v.UpdateReaddirOffset(ctx, parent, fh, off)
for i := 0; i < 100; i += 10 {
name := fmt.Sprintf("d%d", i)
name := fmt.Sprintf("d%03d", i)
_ = v.Rmdir(ctx, parent, name)
delete(files, name)
}
for i := 100; i < 110; i++ {
_, _ = v.Mkdir(ctx, parent, fmt.Sprintf("d%d", i), 0777, 022)
_ = v.Rename(ctx, parent, fmt.Sprintf("d%d", i), parent, fmt.Sprintf("d%d", i+10), 0)
delete(files, fmt.Sprintf("d%d", i))
_, _ = v.Mkdir(ctx, parent, fmt.Sprintf("d%03d", i), 0777, 022)
_ = v.Rename(ctx, parent, fmt.Sprintf("d%03d", i), parent, fmt.Sprintf("d%03d", i+10), 0)
delete(files, fmt.Sprintf("d%03d", i))
}
for {
entries, _, _ := v.Readdir(ctx, parent, 20, off, fh, true)
Expand All @@ -952,19 +964,19 @@ func testReaddirCache(t *testing.T, metaUri string, typ string, batchNum int) {
v.UpdateReaddirOffset(ctx, parent, fh, off)
}
for i := 0; i < 100; i += 10 {
name := fmt.Sprintf("d%d", i)
name := fmt.Sprintf("d%03d", i)
if _, ok := files[name]; ok {
t.Fatalf("dir %s should be deleted", name)
}
}
for i := 100; i < 110; i++ {
name := fmt.Sprintf("d%d", i)
name := fmt.Sprintf("d%03d", i)
if _, ok := files[name]; ok {
t.Fatalf("dir %s should be deleted", name)
}
}
for i := 110; i < 120; i++ {
name := fmt.Sprintf("d%d", i)
name := fmt.Sprintf("d%03d", i)
if _, ok := files[name]; !ok {
t.Fatalf("dir %s should be added", name)
}
Expand Down

0 comments on commit 4f8e211

Please sign in to comment.