Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

filer_pb: walk the whole tree when the BFS start path ends in a slash (#11099)

Chris Lu23a6b8feb548
diff --git a/weed/pb/filer_pb/filer_client_bfs.go b/weed/pb/filer_pb/filer_client_bfs.go
index 451b58511c..e4ec78d675 100644
--- a/weed/pb/filer_pb/filer_client_bfs.go
+++ b/weed/pb/filer_pb/filer_client_bfs.go
@@ -15,6 +15,10 @@ import (
func TraverseBfs(ctx context.Context, filerClient FilerClient, parentPath util.FullPath, fn func(parentPath util.FullPath, entry *Entry) error) (err error) {
K := 5
+ // callers hand in user-typed paths, and every entry is reported relative
+ // to this one, so a trailing slash must not reach the callback
+ parentPath = util.NormalizePath(string(parentPath))
+
ctx, cancel := context.WithCancel(ctx)
defer cancel()
@@ -102,11 +106,7 @@ func processOneDirectory(ctx context.Context, filerClient FilerClient, parentPat
}
if entry.IsDirectory {
- subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
- if parentPath == "/" {
- subDir = "/" + entry.Name
- }
- if !enqueue(util.FullPath(subDir)) {
+ if !enqueue(parentPath.Child(entry.Name)) {
return ctx.Err()
}
}
diff --git a/weed/pb/filer_pb/filer_client_bfs_test.go b/weed/pb/filer_pb/filer_client_bfs_test.go
index c9ec79e144..2395623df0 100644
--- a/weed/pb/filer_pb/filer_client_bfs_test.go
+++ b/weed/pb/filer_pb/filer_client_bfs_test.go
@@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net"
+ "reflect"
+ "sort"
"strings"
"sync"
"testing"
@@ -150,3 +152,40 @@ func TestReadDirAllEntriesStuckPagination(t *testing.T) {
t.Fatal("listing loops forever on a non-advancing store")
}
}
+
+// A start path with a trailing slash must still descend into subdirectories.
+func TestTraverseBfsTrailingSlashRoot(t *testing.T) {
+ server := &stubFiler{}
+ server.listings = func(req *ListEntriesRequest, send func(*Entry) error) error {
+ if req.StartFromFileName != "" {
+ return nil
+ }
+ // the filer trims a single trailing slash, nothing more
+ switch strings.TrimSuffix(req.Directory, "/") {
+ case "/buckets/data":
+ return send(&Entry{Name: "sub", IsDirectory: true})
+ case "/buckets/data/sub":
+ return send(&Entry{Name: "a.txt"})
+ }
+ return nil
+ }
+ filerClient := startStubFiler(t, server)
+
+ var mu sync.Mutex
+ var seen []string
+ err := TraverseBfs(context.Background(), filerClient, "/buckets/data/", func(parentPath util.FullPath, entry *Entry) error {
+ mu.Lock()
+ seen = append(seen, fmt.Sprintf("%s -> %s", parentPath, entry.Name))
+ mu.Unlock()
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ sort.Strings(seen)
+ want := []string{"/buckets/data -> sub", "/buckets/data/sub -> a.txt"}
+ if !reflect.DeepEqual(seen, want) {
+ t.Fatalf("visited %v, want %v", seen, want)
+ }
+}