filer: ListEntriesRecursive RPC (#13262)

* filer: name the check that a listing expires an entry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: StreamListRecursiveEntries lists a subtree in path order

It pages through the store's RecursiveLister until the limit, leaves out
and deletes expired entries as a directory listing does, and declines a
subtree in or holding a remote mount.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer.proto: ListEntriesRecursive

A new RPC rather than a field on ListEntries: a filer that predates it
answers Unimplemented instead of listing one directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: serve ListEntriesRecursive

Unimplemented when the store cannot list the subtree in path order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: a scoped token is refused ListEntriesRecursive

Classified with TraverseBfsMetadata: it lists a whole subtree, past the
per-directory checks a scoped ListEntries gets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: trim a trailing slash before checking a recursive listing for remote mounts

An empty directory lists from the root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep the recursive listing cursor when a page comes back empty

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-12-02 12:04:27 -08:00
1 parent a9d4714aed
commit acb7728915
10 files changed
+1534 -569

No files matched your search

+16 -14
View File
@@ -1220,20 +1220,11 @@ func (f *Filer) doListDirectoryEntries(ctx context.Context, p util.FullPath, sta
glog.V(1).InfofCtx(ctx, "listing %q canceled: %v", p, ctx.Err())
return false, fmt.Errorf("context canceled: %w", ctx.Err())
default:
if entry.TtlSec > 0 && !entry.IsDirectory() {
if entry.IsExpireS3Enabled() {
if entry.GetS3ExpireTime().Before(time.Now()) && !entry.IsS3Versioning() {
// Collect for deletion after iteration completes to avoid DB deadlock
expiredEntries = append(expiredEntries, entry)
expiredCount++
return true, nil
}
} else if entry.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now()) {
// Collect for deletion after iteration completes to avoid DB deadlock
expiredEntries = append(expiredEntries, entry)
expiredCount++
return true, nil
}
if listingExpired(entry) {
// Collect for deletion after iteration completes to avoid DB deadlock
expiredEntries = append(expiredEntries, entry)
expiredCount++
return true, nil
}
// Track that we found at least one valid (non-expired) entry
hasValidEntries = true
@@ -1263,6 +1254,17 @@ func (f *Filer) doListDirectoryEntries(ctx context.Context, p util.FullPath, sta
return
}
// listingExpired reports whether a listing leaves the entry out and deletes it.
func listingExpired(entry *Entry) bool {
if entry.TtlSec <= 0 || entry.IsDirectory() {
return false
}
if entry.IsExpireS3Enabled() {
return entry.GetS3ExpireTime().Before(time.Now()) && !entry.IsS3Versioning()
}
return entry.Crtime.Add(time.Duration(entry.TtlSec) * time.Second).Before(time.Now())
}
// DeleteEmptyParentDirectories recursively checks and deletes parent directories if they become empty.
// It stops at root "/" or at stopAtPath (if provided).
// This is useful for cleaning up directories after deleting files or expired entries.
+64
View File
@@ -0,0 +1,64 @@
package filer
import (
"context"
"fmt"
"strings"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// StreamListRecursiveEntries lists up to limit entries below p in path order
// with the store's RecursiveLister, leaving out expired entries and deleting
// them as a directory listing does. A subtree in or holding a remote mount is
// declined: its directory listings fetch from the remote.
func (f *Filer) StreamListRecursiveEntries(ctx context.Context, p, startPath util.FullPath, prefix string, limit int64, eachEntryFunc ListEachEntryFunc) (lastPath util.FullPath, err error) {
lister, ok := f.Store.(RecursiveLister)
if !ok {
return "", ErrUnsupportedRecursiveListing
}
if p = util.FullPath(strings.TrimSuffix(string(p), "/")); p == "" {
p = "/"
}
if f.RemoteStorage != nil {
for _, mount := range f.RemoteStorage.MountedDirectories() {
if mount == p || mount.IsUnder(p) || p.IsUnder(mount) {
return "", fmt.Errorf("%w: %s is remote mounted", ErrUnsupportedRecursiveListing, mount)
}
}
}
lastPath = startPath
for limit > 0 {
var expiredEntries []*Entry
read, stopped := int64(0), false
page := min(limit, int64(PaginationSize))
var pageLast util.FullPath
pageLast, err = lister.ListRecursivePrefixedEntries(ctx, p, lastPath, prefix, page, func(entry *Entry) (bool, error) {
read++
if ctxErr := ctx.Err(); ctxErr != nil {
return false, ctxErr
}
if listingExpired(entry) {
expiredEntries = append(expiredEntries, entry)
return true, nil
}
limit--
more, eachErr := eachEntryFunc(entry)
stopped = !more || limit == 0
return !stopped, eachErr
})
if pageLast != "" {
lastPath = pageLast
}
for _, entry := range expiredEntries {
if delErr := f.deleteExpiredEntry(ctx, entry.FullPath); delErr != nil {
glog.ErrorfCtx(ctx, "list %s recursively: delete expired %s: %v", p, entry.FullPath, delErr)
}
}
if err != nil || stopped || read < page {
return lastPath, err
}
}
return lastPath, nil
}
+157
View File
@@ -0,0 +1,157 @@
package filer
import (
"context"
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/stretchr/testify/require"
)
// recursiveStubStore lists the stub's entries in path order.
type recursiveStubStore struct {
*stubFilerStore
calls int
}
func (s *recursiveStubStore) ListRecursivePrefixedEntries(ctx context.Context, dirPath, startPath util.FullPath, prefix string, limit int64, eachEntryFunc ListEachEntryFunc) (lastPath util.FullPath, err error) {
s.calls++
from := strings.TrimSuffix(string(dirPath), "/") + "/" + prefix
s.mu.Lock()
var paths []string
for p := range s.entries {
if strings.HasPrefix(p, from) && p > string(startPath) {
paths = append(paths, p)
}
}
s.mu.Unlock()
sort.Strings(paths)
for _, p := range paths {
if limit <= 0 {
break
}
limit--
lastPath = util.FullPath(p)
s.mu.Lock()
entry := s.entries[p]
s.mu.Unlock()
if more, err := eachEntryFunc(entry); err != nil || !more {
return lastPath, err
}
}
return lastPath, nil
}
func TestStreamListRecursiveEntries(t *testing.T) {
ctx := context.Background()
store := &recursiveStubStore{stubFilerStore: newStubFilerStore()}
f := &Filer{Store: NewFilerStoreWrapper(store)}
now := time.Now()
var want []string
for i := 0; i < 2*PaginationSize+5; i++ {
p := fmt.Sprintf("/buckets/b/d%02d/f%05d", i%7, i)
store.entries[p] = &Entry{FullPath: util.FullPath(p), Attr: Attr{Mode: 0644, Crtime: now, Mtime: now}}
want = append(want, p)
}
expired := "/buckets/b/d00/expired"
store.entries[expired] = &Entry{FullPath: util.FullPath(expired), Attr: Attr{Mode: 0644, Crtime: now.Add(-time.Hour), Mtime: now, TtlSec: 60}}
sort.Strings(want)
list := func(t *testing.T, start, prefix string, limit int64, stopAt int) ([]string, util.FullPath) {
t.Helper()
var got []string
last, err := f.StreamListRecursiveEntries(ctx, "/buckets/b", util.FullPath(start), prefix, limit, func(e *Entry) (bool, error) {
got = append(got, string(e.FullPath))
return len(got) != stopAt, nil
})
require.NoError(t, err)
return got, last
}
t.Run("pages through the store until the limit", func(t *testing.T) {
store.calls = 0
got, last := list(t, "", "", 1<<20, -1)
require.Equal(t, want, got)
require.Equal(t, util.FullPath(want[len(want)-1]), last)
require.Equal(t, 3, store.calls, "two full pages and a short one")
_, found := store.entries[expired]
require.False(t, found, "the expired entry is deleted")
got, last = list(t, "", "", 10, -1)
require.Equal(t, want[:10], got)
require.Equal(t, util.FullPath(want[9]), last)
})
t.Run("resumes after a path, within a prefix", func(t *testing.T) {
got, _ := list(t, want[100], "d03/", 1<<20, -1)
var expect []string
for _, p := range want[101:] {
if strings.HasPrefix(p, "/buckets/b/d03/") {
expect = append(expect, p)
}
}
require.Equal(t, expect, got)
})
t.Run("a trailing slash lists the same subtree", func(t *testing.T) {
got, _ := list(t, "", "", 1<<20, -1)
var withSlash []string
_, err := f.StreamListRecursiveEntries(ctx, "/buckets/b/", "", "", 1<<20, func(e *Entry) (bool, error) {
withSlash = append(withSlash, string(e.FullPath))
return true, nil
})
require.NoError(t, err)
require.Equal(t, got, withSlash)
})
t.Run("a callback that stops ends at its entry", func(t *testing.T) {
got, last := list(t, "", "", 1<<20, 5)
require.Equal(t, want[:5], got)
require.Equal(t, util.FullPath(want[4]), last)
})
t.Run("an empty last page keeps the cursor", func(t *testing.T) {
exact := &recursiveStubStore{stubFilerStore: newStubFilerStore()}
var last string
for i := 0; i < PaginationSize; i++ {
last = fmt.Sprintf("/buckets/e/f%05d", i)
exact.entries[last] = &Entry{FullPath: util.FullPath(last), Attr: Attr{Mode: 0644, Crtime: now, Mtime: now}}
}
count := 0
lastPath, err := (&Filer{Store: NewFilerStoreWrapper(exact)}).StreamListRecursiveEntries(ctx, "/buckets/e", "", "", 1<<20, func(*Entry) (bool, error) {
count++
return true, nil
})
require.NoError(t, err)
require.Equal(t, PaginationSize, count)
require.Equal(t, 2, exact.calls)
require.Equal(t, util.FullPath(last), lastPath)
})
t.Run("declined", func(t *testing.T) {
rs := NewFilerRemoteStorage()
rs.mapDirectoryToRemoteStorage("/buckets/b/d01", &remote_pb.RemoteStorageLocation{Name: "r"})
f.RemoteStorage = rs
_, err := f.StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below")
_, err = f.StreamListRecursiveEntries(ctx, "/buckets/b/d01/x", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "inside a remote mount")
_, err = f.StreamListRecursiveEntries(ctx, "/buckets/b/", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below a directory with a trailing slash")
_, err = f.StreamListRecursiveEntries(ctx, "", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below an empty directory")
f.RemoteStorage = nil
wrapper := NewFilerStoreWrapper(store)
wrapper.AddPathSpecificStore("/other/", "other", newStubFilerStore())
_, err = (&Filer{Store: wrapper}).StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "path-specific stores")
_, err = (&Filer{Store: NewFilerStoreWrapper(newStubFilerStore())}).StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a store without the capability")
})
}
+19
View File
@@ -19,6 +19,11 @@ service SeaweedFiler {
rpc ListEntries (ListEntriesRequest) returns (stream ListEntriesResponse) {
}
// Lists every entry below a directory in path order, for stores that hold
// a subtree in one ordered range. Unimplemented when the store cannot.
rpc ListEntriesRecursive (ListEntriesRecursiveRequest) returns (stream ListEntriesRecursiveResponse) {
}
rpc CreateEntry (CreateEntryRequest) returns (CreateEntryResponse) {
}
@@ -150,6 +155,20 @@ message ListEntriesResponse {
int64 snapshot_ts_ns = 2;
}
message ListEntriesRecursiveRequest {
string directory = 1;
// a path prefix below the directory, which may hold "/"
string prefix = 2;
// a full path; the entries after it are listed
string start_from = 3;
uint32 limit = 4;
}
message ListEntriesRecursiveResponse {
string directory = 1;
Entry entry = 2;
}
message RemoteEntry {
string storage_name = 1;
int64 last_local_sync_ts_ns = 2;
File diff suppressed because it is too large. Load diff
+50 -5
View File
@@ -22,6 +22,7 @@ const (
SeaweedFiler_LookupDirectoryEntry_FullMethodName = "/filer_pb.SeaweedFiler/LookupDirectoryEntry"
SeaweedFiler_LookupDirectoryEntries_FullMethodName = "/filer_pb.SeaweedFiler/LookupDirectoryEntries"
SeaweedFiler_ListEntries_FullMethodName = "/filer_pb.SeaweedFiler/ListEntries"
SeaweedFiler_ListEntriesRecursive_FullMethodName = "/filer_pb.SeaweedFiler/ListEntriesRecursive"
SeaweedFiler_CreateEntry_FullMethodName = "/filer_pb.SeaweedFiler/CreateEntry"
SeaweedFiler_UpdateEntry_FullMethodName = "/filer_pb.SeaweedFiler/UpdateEntry"
SeaweedFiler_TouchAccessTime_FullMethodName = "/filer_pb.SeaweedFiler/TouchAccessTime"
@@ -63,6 +64,9 @@ type SeaweedFilerClient interface {
LookupDirectoryEntry(ctx context.Context, in *LookupDirectoryEntryRequest, opts ...grpc.CallOption) (*LookupDirectoryEntryResponse, error)
LookupDirectoryEntries(ctx context.Context, in *LookupDirectoryEntriesRequest, opts ...grpc.CallOption) (*LookupDirectoryEntriesResponse, error)
ListEntries(ctx context.Context, in *ListEntriesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[ListEntriesResponse], error)
// Lists every entry below a directory in path order, for stores that hold
// a subtree in one ordered range. Unimplemented when the store cannot.
ListEntriesRecursive(ctx context.Context, in *ListEntriesRecursiveRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[ListEntriesRecursiveResponse], error)
CreateEntry(ctx context.Context, in *CreateEntryRequest, opts ...grpc.CallOption) (*CreateEntryResponse, error)
UpdateEntry(ctx context.Context, in *UpdateEntryRequest, opts ...grpc.CallOption) (*UpdateEntryResponse, error)
TouchAccessTime(ctx context.Context, in *TouchAccessTimeRequest, opts ...grpc.CallOption) (*TouchAccessTimeResponse, error)
@@ -149,6 +153,25 @@ func (c *seaweedFilerClient) ListEntries(ctx context.Context, in *ListEntriesReq
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type SeaweedFiler_ListEntriesClient = grpc.ServerStreamingClient[ListEntriesResponse]
func (c *seaweedFilerClient) ListEntriesRecursive(ctx context.Context, in *ListEntriesRecursiveRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[ListEntriesRecursiveResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[1], SeaweedFiler_ListEntriesRecursive_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
x := &grpc.GenericClientStream[ListEntriesRecursiveRequest, ListEntriesRecursiveResponse]{ClientStream: stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type SeaweedFiler_ListEntriesRecursiveClient = grpc.ServerStreamingClient[ListEntriesRecursiveResponse]
func (c *seaweedFilerClient) CreateEntry(ctx context.Context, in *CreateEntryRequest, opts ...grpc.CallOption) (*CreateEntryResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(CreateEntryResponse)
@@ -241,7 +264,7 @@ func (c *seaweedFilerClient) AtomicRenameEntry(ctx context.Context, in *AtomicRe
func (c *seaweedFilerClient) StreamRenameEntry(ctx context.Context, in *StreamRenameEntryRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[StreamRenameEntryResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[1], SeaweedFiler_StreamRenameEntry_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[2], SeaweedFiler_StreamRenameEntry_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -260,7 +283,7 @@ type SeaweedFiler_StreamRenameEntryClient = grpc.ServerStreamingClient[StreamRen
func (c *seaweedFilerClient) StreamMutateEntry(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[StreamMutateEntryRequest, StreamMutateEntryResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[2], SeaweedFiler_StreamMutateEntry_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[3], SeaweedFiler_StreamMutateEntry_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -343,7 +366,7 @@ func (c *seaweedFilerClient) GetFilerConfiguration(ctx context.Context, in *GetF
func (c *seaweedFilerClient) TraverseBfsMetadata(ctx context.Context, in *TraverseBfsMetadataRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[TraverseBfsMetadataResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[3], SeaweedFiler_TraverseBfsMetadata_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[4], SeaweedFiler_TraverseBfsMetadata_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -362,7 +385,7 @@ type SeaweedFiler_TraverseBfsMetadataClient = grpc.ServerStreamingClient[Travers
func (c *seaweedFilerClient) SubscribeMetadata(ctx context.Context, in *SubscribeMetadataRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[SubscribeMetadataResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[4], SeaweedFiler_SubscribeMetadata_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[5], SeaweedFiler_SubscribeMetadata_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -381,7 +404,7 @@ type SeaweedFiler_SubscribeMetadataClient = grpc.ServerStreamingClient[Subscribe
func (c *seaweedFilerClient) SubscribeLocalMetadata(ctx context.Context, in *SubscribeMetadataRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[SubscribeMetadataResponse], error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[5], SeaweedFiler_SubscribeLocalMetadata_FullMethodName, cOpts...)
stream, err := c.cc.NewStream(ctx, &SeaweedFiler_ServiceDesc.Streams[6], SeaweedFiler_SubscribeLocalMetadata_FullMethodName, cOpts...)
if err != nil {
return nil, err
}
@@ -515,6 +538,9 @@ type SeaweedFilerServer interface {
LookupDirectoryEntry(context.Context, *LookupDirectoryEntryRequest) (*LookupDirectoryEntryResponse, error)
LookupDirectoryEntries(context.Context, *LookupDirectoryEntriesRequest) (*LookupDirectoryEntriesResponse, error)
ListEntries(*ListEntriesRequest, grpc.ServerStreamingServer[ListEntriesResponse]) error
// Lists every entry below a directory in path order, for stores that hold
// a subtree in one ordered range. Unimplemented when the store cannot.
ListEntriesRecursive(*ListEntriesRecursiveRequest, grpc.ServerStreamingServer[ListEntriesRecursiveResponse]) error
CreateEntry(context.Context, *CreateEntryRequest) (*CreateEntryResponse, error)
UpdateEntry(context.Context, *UpdateEntryRequest) (*UpdateEntryResponse, error)
TouchAccessTime(context.Context, *TouchAccessTimeRequest) (*TouchAccessTimeResponse, error)
@@ -571,6 +597,9 @@ func (UnimplementedSeaweedFilerServer) LookupDirectoryEntries(context.Context, *
func (UnimplementedSeaweedFilerServer) ListEntries(*ListEntriesRequest, grpc.ServerStreamingServer[ListEntriesResponse]) error {
return status.Error(codes.Unimplemented, "method ListEntries not implemented")
}
func (UnimplementedSeaweedFilerServer) ListEntriesRecursive(*ListEntriesRecursiveRequest, grpc.ServerStreamingServer[ListEntriesRecursiveResponse]) error {
return status.Error(codes.Unimplemented, "method ListEntriesRecursive not implemented")
}
func (UnimplementedSeaweedFilerServer) CreateEntry(context.Context, *CreateEntryRequest) (*CreateEntryResponse, error) {
return nil, status.Error(codes.Unimplemented, "method CreateEntry not implemented")
}
@@ -735,6 +764,17 @@ func _SeaweedFiler_ListEntries_Handler(srv interface{}, stream grpc.ServerStream
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type SeaweedFiler_ListEntriesServer = grpc.ServerStreamingServer[ListEntriesResponse]
func _SeaweedFiler_ListEntriesRecursive_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(ListEntriesRecursiveRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(SeaweedFilerServer).ListEntriesRecursive(m, &grpc.GenericServerStream[ListEntriesRecursiveRequest, ListEntriesRecursiveResponse]{ServerStream: stream})
}
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
type SeaweedFiler_ListEntriesRecursiveServer = grpc.ServerStreamingServer[ListEntriesRecursiveResponse]
func _SeaweedFiler_CreateEntry_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(CreateEntryRequest)
if err := dec(in); err != nil {
@@ -1402,6 +1442,11 @@ var SeaweedFiler_ServiceDesc = grpc.ServiceDesc{
Handler: _SeaweedFiler_ListEntries_Handler,
ServerStreams: true,
},
{
StreamName: "ListEntriesRecursive",
Handler: _SeaweedFiler_ListEntriesRecursive_Handler,
ServerStreams: true,
},
{
StreamName: "StreamRenameEntry",
Handler: _SeaweedFiler_StreamRenameEntry_Handler,
+437
View File
@@ -251,6 +251,115 @@ func (m *ListEntriesResponse) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
return len(dAtA) - i, nil
}
func (m *ListEntriesRecursiveRequest) MarshalVT() (dAtA []byte, err error) {
if m == nil {
return nil, nil
}
size := m.SizeVT()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBufferVT(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *ListEntriesRecursiveRequest) MarshalToVT(dAtA []byte) (int, error) {
size := m.SizeVT()
return m.MarshalToSizedBufferVT(dAtA[:size])
}
func (m *ListEntriesRecursiveRequest) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
if m == nil {
return 0, nil
}
i := len(dAtA)
_ = i
var l int
_ = l
if m.unknownFields != nil {
i -= len(m.unknownFields)
copy(dAtA[i:], m.unknownFields)
}
if m.Limit != 0 {
i = protohelpers.EncodeVarint(dAtA, i, uint64(m.Limit))
i--
dAtA[i] = 0x20
}
if len(m.StartFrom) > 0 {
i -= len(m.StartFrom)
copy(dAtA[i:], m.StartFrom)
i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.StartFrom)))
i--
dAtA[i] = 0x1a
}
if len(m.Prefix) > 0 {
i -= len(m.Prefix)
copy(dAtA[i:], m.Prefix)
i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.Prefix)))
i--
dAtA[i] = 0x12
}
if len(m.Directory) > 0 {
i -= len(m.Directory)
copy(dAtA[i:], m.Directory)
i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.Directory)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *ListEntriesRecursiveResponse) MarshalVT() (dAtA []byte, err error) {
if m == nil {
return nil, nil
}
size := m.SizeVT()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBufferVT(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *ListEntriesRecursiveResponse) MarshalToVT(dAtA []byte) (int, error) {
size := m.SizeVT()
return m.MarshalToSizedBufferVT(dAtA[:size])
}
func (m *ListEntriesRecursiveResponse) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
if m == nil {
return 0, nil
}
i := len(dAtA)
_ = i
var l int
_ = l
if m.unknownFields != nil {
i -= len(m.unknownFields)
copy(dAtA[i:], m.unknownFields)
}
if m.Entry != nil {
size, err := m.Entry.MarshalToSizedBufferVT(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = protohelpers.EncodeVarint(dAtA, i, uint64(size))
i--
dAtA[i] = 0x12
}
if len(m.Directory) > 0 {
i -= len(m.Directory)
copy(dAtA[i:], m.Directory)
i = protohelpers.EncodeVarint(dAtA, i, uint64(len(m.Directory)))
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *RemoteEntry) MarshalVT() (dAtA []byte, err error) {
if m == nil {
return nil, nil
@@ -6856,6 +6965,49 @@ func (m *ListEntriesResponse) SizeVT() (n int) {
return n
}
func (m *ListEntriesRecursiveRequest) SizeVT() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Directory)
if l > 0 {
n += 1 + l + protohelpers.SizeOfVarint(uint64(l))
}
l = len(m.Prefix)
if l > 0 {
n += 1 + l + protohelpers.SizeOfVarint(uint64(l))
}
l = len(m.StartFrom)
if l > 0 {
n += 1 + l + protohelpers.SizeOfVarint(uint64(l))
}
if m.Limit != 0 {
n += 1 + protohelpers.SizeOfVarint(uint64(m.Limit))
}
n += len(m.unknownFields)
return n
}
func (m *ListEntriesRecursiveResponse) SizeVT() (n int) {
if m == nil {
return 0
}
var l int
_ = l
l = len(m.Directory)
if l > 0 {
n += 1 + l + protohelpers.SizeOfVarint(uint64(l))
}
if m.Entry != nil {
l = m.Entry.SizeVT()
n += 1 + l + protohelpers.SizeOfVarint(uint64(l))
}
n += len(m.unknownFields)
return n
}
func (m *RemoteEntry) SizeVT() (n int) {
if m == nil {
return 0
@@ -10060,6 +10212,291 @@ func (m *ListEntriesResponse) UnmarshalVT(dAtA []byte) error {
}
return nil
}
func (m *ListEntriesRecursiveRequest) UnmarshalVT(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: ListEntriesRecursiveRequest: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: ListEntriesRecursiveRequest: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Directory", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return protohelpers.ErrInvalidLength
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return protohelpers.ErrInvalidLength
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Directory = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Prefix", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return protohelpers.ErrInvalidLength
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return protohelpers.ErrInvalidLength
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Prefix = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 3:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field StartFrom", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return protohelpers.ErrInvalidLength
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return protohelpers.ErrInvalidLength
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.StartFrom = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 4:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Limit", wireType)
}
m.Limit = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Limit |= uint32(b&0x7F) << shift
if b < 0x80 {
break
}
}
default:
iNdEx = preIndex
skippy, err := protohelpers.Skip(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return protohelpers.ErrInvalidLength
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
m.unknownFields = append(m.unknownFields, dAtA[iNdEx:iNdEx+skippy]...)
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *ListEntriesRecursiveResponse) UnmarshalVT(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: ListEntriesRecursiveResponse: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: ListEntriesRecursiveResponse: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Directory", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
stringLen |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
intStringLen := int(stringLen)
if intStringLen < 0 {
return protohelpers.ErrInvalidLength
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return protohelpers.ErrInvalidLength
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Directory = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Entry", wireType)
}
var msglen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return protohelpers.ErrIntOverflow
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
msglen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if msglen < 0 {
return protohelpers.ErrInvalidLength
}
postIndex := iNdEx + msglen
if postIndex < 0 {
return protohelpers.ErrInvalidLength
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
if m.Entry == nil {
m.Entry = &Entry{}
}
if err := m.Entry.UnmarshalVT(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := protohelpers.Skip(dAtA[iNdEx:])
if err != nil {
return err
}
if (skippy < 0) || (iNdEx+skippy) < 0 {
return protohelpers.ErrInvalidLength
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
m.unknownFields = append(m.unknownFields, dAtA[iNdEx:iNdEx+skippy]...)
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *RemoteEntry) UnmarshalVT(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
@@ -49,7 +49,8 @@ func TestScopeDescriptorEveryFilerMethodIsAllowedOrExplicitlyRefused(t *testing.
"SubscribeLocalMetadata": true,
}
explicitlyRefusedStreams := map[string]bool{
"TraverseBfsMetadata": true,
"TraverseBfsMetadata": true,
"ListEntriesRecursive": true,
}
for _, method := range filer_pb.SeaweedFiler_ServiceDesc.Methods {
@@ -0,0 +1,31 @@
package weed_server
import (
"errors"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ListEntriesRecursive answers Unimplemented when the store cannot list the
// subtree in path order, so the caller can walk it directory by directory.
func (fs *FilerServer) ListEntriesRecursive(req *filer_pb.ListEntriesRecursiveRequest, stream filer_pb.SeaweedFiler_ListEntriesRecursiveServer) error {
limit := int64(req.Limit)
if limit == 0 {
limit = int64(fs.option.DirListingLimit)
}
_, err := fs.filer.StreamListRecursiveEntries(stream.Context(), util.FullPath(req.Directory), util.FullPath(req.StartFrom), req.Prefix, limit, func(entry *filer.Entry) (bool, error) {
dir, _ := entry.FullPath.DirAndName()
if err := stream.Send(&filer_pb.ListEntriesRecursiveResponse{Directory: dir, Entry: entry.ToProtoEntry()}); err != nil {
return false, err
}
return true, nil
})
if errors.Is(err, filer.ErrUnsupportedRecursiveListing) {
return status.Error(codes.Unimplemented, err.Error())
}
return err
}
@@ -0,0 +1,72 @@
package weed_server
import (
"context"
"sort"
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// recursiveListStore lists the rename test store's entries in path order.
type recursiveListStore struct{ *renameTestStore }
func (s recursiveListStore) ListRecursivePrefixedEntries(ctx context.Context, dirPath, startPath util.FullPath, prefix string, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastPath util.FullPath, err error) {
from := strings.TrimSuffix(string(dirPath), "/") + "/" + prefix
var paths []string
for p := range s.entries {
if strings.HasPrefix(p, from) && p > string(startPath) {
paths = append(paths, p)
}
}
sort.Strings(paths)
for _, p := range paths[:min(int64(len(paths)), limit)] {
lastPath = util.FullPath(p)
if more, err := eachEntryFunc(s.entries[p]); err != nil || !more {
return lastPath, err
}
}
return lastPath, nil
}
type captureRecursiveStream struct {
grpc.ServerStream
paths []string
}
func (s *captureRecursiveStream) Context() context.Context { return context.Background() }
func (s *captureRecursiveStream) Send(resp *filer_pb.ListEntriesRecursiveResponse) error {
s.paths = append(s.paths, string(util.NewFullPath(resp.Directory, resp.Entry.Name)))
return nil
}
func TestListEntriesRecursive(t *testing.T) {
store := newRenameTestStore()
for _, d := range []string{"/buckets/b", "/buckets/b/a", "/buckets/b/a-b"} {
store.entries[d] = newDirectoryEntry(d, 1)
}
for _, f := range []string{"/buckets/b/a/x", "/buckets/b/a+b", "/buckets/b/a-b/y", "/buckets/b/z"} {
store.entries[f] = newFileEntry(f, 2)
}
server := &FilerServer{filer: newRenameTestFiler(t, recursiveListStore{store})}
stream := &captureRecursiveStream{}
require.NoError(t, server.ListEntriesRecursive(&filer_pb.ListEntriesRecursiveRequest{Directory: "/buckets/b", Limit: 100}, stream))
require.Equal(t, []string{"/buckets/b/a", "/buckets/b/a+b", "/buckets/b/a-b", "/buckets/b/a-b/y", "/buckets/b/a/x", "/buckets/b/z"}, stream.paths)
stream = &captureRecursiveStream{}
require.NoError(t, server.ListEntriesRecursive(&filer_pb.ListEntriesRecursiveRequest{Directory: "/buckets/b", Prefix: "a", StartFrom: "/buckets/b/a-b", Limit: 2}, stream))
require.Equal(t, []string{"/buckets/b/a-b/y", "/buckets/b/a/x"}, stream.paths)
plain := &FilerServer{filer: newRenameTestFiler(t, store)}
err := plain.ListEntriesRecursive(&filer_pb.ListEntriesRecursiveRequest{Directory: "/buckets/b", Limit: 1}, &captureRecursiveStream{})
require.Equal(t, codes.Unimplemented, status.Code(err))
}