diff --git a/weed/s3api/s3_objectlock/object_lock_check.go b/weed/s3api/s3_objectlock/object_lock_check.go index 99c6453c3d..4282036227 100644 --- a/weed/s3api/s3_objectlock/object_lock_check.go +++ b/weed/s3api/s3_objectlock/object_lock_check.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "path" "strconv" "strings" "time" @@ -80,35 +81,22 @@ func paginateEntries(ctx context.Context, client filer_pb.SeaweedFilerClient, di processEntry func(entry *filer_pb.Entry) (stop bool, err error)) error { lastFileName := "" for { - resp, err := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{ - Directory: dir, - StartFromFileName: lastFileName, - InclusiveStartFrom: false, - Limit: 10000, - }) + // Read the page to its end before processing it: processing may list a + // subdirectory, and the filer can hold a store connection while a + // listing stays open. + page, err := listEntriesPage(ctx, client, dir, lastFileName) if err != nil { - return fmt.Errorf("failed to list directory %s: %w", dir, err) + return err } + if len(page) == 0 { + return nil + } + lastFileName = page[len(page)-1].Name - entriesReceived := false - for { - entryResp, recvErr := resp.Recv() - if recvErr != nil { - if errors.Is(recvErr, io.EOF) { - break // Normal end of stream - } - return fmt.Errorf("failed to receive entry from %s: %w", dir, recvErr) - } - entry := entryResp.Entry - if entry == nil { - continue - } - entriesReceived = true - lastFileName = entry.Name - + for _, entry := range page { // Skip invalid entry names to prevent path traversal if entry.Name == "" || entry.Name == "." || entry.Name == ".." || - strings.ContainsAny(entry.Name, "/\\") { + strings.Contains(entry.Name, "/") { glog.V(2).Infof("Skipping invalid entry name: %q in %s", entry.Name, dir) continue } @@ -121,12 +109,35 @@ func paginateEntries(ctx context.Context, client filer_pb.SeaweedFilerClient, di return nil } } + } +} - if !entriesReceived { - break +func listEntriesPage(ctx context.Context, client filer_pb.SeaweedFilerClient, dir, startFrom string) ([]*filer_pb.Entry, error) { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + resp, err := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{ + Directory: dir, + StartFromFileName: startFrom, + InclusiveStartFrom: false, + Limit: 10000, + OmitChunks: true, + }) + if err != nil { + return nil, fmt.Errorf("failed to list directory %s: %w", dir, err) + } + var page []*filer_pb.Entry + for { + entryResp, recvErr := resp.Recv() + if recvErr != nil { + if errors.Is(recvErr, io.EOF) { + return page, nil + } + return nil, fmt.Errorf("failed to receive entry from %s: %w", dir, recvErr) + } + if e := entryResp.Entry; e != nil { + page = append(page, &filer_pb.Entry{Name: e.Name, IsDirectory: e.IsDirectory, Extended: e.Extended}) } } - return nil } // recursivelyCheckLocksWithClient recursively checks all objects and versions for active locks @@ -140,15 +151,13 @@ func recursivelyCheckLocksWithClient(ctx context.Context, client filer_pb.Seawee return true, nil // Stop iteration } - // Skip special directories - if entry.Name == s3_constants.MultipartUploadsFolder { - return false, nil // Continue - } - if entry.IsDirectory { // A directory-marker object (key ending in "/") is stored as a // directory entry and can carry a lock, so check it before recursing. - if EntryHasActiveLock(entry, currentTime) { + // A multipart upload's directory holds the lock settings for an object + // that does not exist yet. + uploadDir := path.Base(dir) == s3_constants.MultipartUploadsFolder && len(entry.Extended[s3_constants.ExtMultipartObjectKey]) > 0 + if !uploadDir && EntryHasActiveLock(entry, currentTime) { *hasLocks = true glog.V(2).Infof("Found directory marker with active lock: %s/%s", dir, entry.Name) return true, nil diff --git a/weed/s3api/s3_objectlock/object_lock_check_test.go b/weed/s3api/s3_objectlock/object_lock_check_test.go index 718ba3c6bc..8a2b441096 100644 --- a/weed/s3api/s3_objectlock/object_lock_check_test.go +++ b/weed/s3api/s3_objectlock/object_lock_check_test.go @@ -2,6 +2,7 @@ package s3_objectlock import ( "context" + "fmt" "io" "strconv" "testing" @@ -16,10 +17,15 @@ import ( type fakeListStream struct { entries []*filer_pb.Entry index int + done func() } func (s *fakeListStream) Recv() (*filer_pb.ListEntriesResponse, error) { if s.index >= len(s.entries) { + if s.done != nil { + s.done() + s.done = nil + } return nil, io.EOF } e := s.entries[s.index] @@ -38,15 +44,24 @@ func (s *fakeListStream) RecvMsg(any) error { return nil } type fakeFilerClient struct { filer_pb.SeaweedFilerClient dirs map[string][]*filer_pb.Entry + // oneListing refuses a listing while another is still open, like a store + // with a single connection. + oneListing bool + open int } func (c *fakeFilerClient) ListEntries(ctx context.Context, in *filer_pb.ListEntriesRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[filer_pb.ListEntriesResponse], error) { + if c.oneListing && c.open > 0 { + return nil, fmt.Errorf("listing %s while another listing is open", in.Directory) + } + c.open++ + done := func() { c.open-- } entries := c.dirs[in.Directory] if in.StartFromFileName != "" { // Already returned everything on the first page; the second call gets nothing. - return &fakeListStream{}, nil + return &fakeListStream{done: done}, nil } - return &fakeListStream{entries: entries}, nil + return &fakeListStream{entries: entries, done: done}, nil } func lockedDirMarker(name string) *filer_pb.Entry { @@ -107,3 +122,82 @@ func TestHasObjectsWithActiveLocksBelowLiteralVersionsDirectory(t *testing.T) { t.Fatalf("expected the held object below a literal .versions directory to be reported") } } + +func TestHasObjectsWithActiveLocksReadsEachPageBeforeSubdirectories(t *testing.T) { + client := &fakeFilerClient{oneListing: true, dirs: map[string][]*filer_pb.Entry{ + "/buckets/b": {{Name: "a", IsDirectory: true}, {Name: "z"}}, + "/buckets/b/a": {{Name: "held", Extended: map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)}}}, + }} + + has, err := HasObjectsWithActiveLocks(context.Background(), client, "/buckets/b") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !has { + t.Fatalf("expected the held object in the subdirectory to be reported") + } +} + +func TestHasObjectsWithActiveLocksNamesWithBackslashes(t *testing.T) { + held := map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)} + for name, dirs := range map[string]map[string][]*filer_pb.Entry{ + "object": {"/buckets/b": {{Name: `a\b`, Extended: held}}}, + "prefix": {"/buckets/b": {{Name: `a\b`, IsDirectory: true}}, `/buckets/b/a\b`: {{Name: "c", Extended: held}}}, + } { + has, err := HasObjectsWithActiveLocks(context.Background(), &fakeFilerClient{dirs: dirs}, "/buckets/b") + if err != nil { + t.Fatalf("%s: unexpected error: %v", name, err) + } + if !has { + t.Fatalf("%s: expected the held object with a backslash in its key to be reported", name) + } + } +} + +func TestHasObjectsWithActiveLocksUploadsDirectories(t *testing.T) { + held := map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)} + upload := lockedDirMarker("upload-id") + upload.Extended[s3_constants.ExtMultipartObjectKey] = []byte("k") + for _, tt := range []struct { + name string + dirs map[string][]*filer_pb.Entry + want bool + }{ + {"pending upload", map[string][]*filer_pb.Entry{ + "/buckets/b": {{Name: s3_constants.MultipartUploadsFolder, IsDirectory: true}}, + "/buckets/b/.uploads": {upload}, + }, false}, + {"object below a pending upload", map[string][]*filer_pb.Entry{ + "/buckets/b": {{Name: s3_constants.MultipartUploadsFolder, IsDirectory: true}}, + "/buckets/b/.uploads": {upload}, + "/buckets/b/.uploads/upload-id": {{Name: "0001_x.part"}, {Name: "c", Extended: held}}, + }, true}, + {"upload directory renamed out of .uploads", map[string][]*filer_pb.Entry{ + "/buckets/b": {upload}, + }, true}, + {"object below a .uploads prefix", map[string][]*filer_pb.Entry{ + "/buckets/b": {{Name: "a", IsDirectory: true}}, + "/buckets/b/a": {{Name: s3_constants.MultipartUploadsFolder, IsDirectory: true}}, + "/buckets/b/a/.uploads": {{Name: "c", Extended: held}}, + }, true}, + } { + has, err := HasObjectsWithActiveLocks(context.Background(), &fakeFilerClient{dirs: tt.dirs}, "/buckets/b") + if err != nil || has != tt.want { + t.Fatalf("%s: got %v, %v, want %v", tt.name, has, err, tt.want) + } + } +} + +func TestListEntriesPageDropsInlineContent(t *testing.T) { + held := map[string][]byte{s3_constants.ExtLegalHoldKey: []byte(s3_constants.LegalHoldOn)} + client := &fakeFilerClient{dirs: map[string][]*filer_pb.Entry{ + "/buckets/b": {{Name: "a", Content: make([]byte, 1<<20), Extended: held}}, + }} + page, err := listEntriesPage(context.Background(), client, "/buckets/b", "") + if err != nil || len(page) != 1 { + t.Fatalf("got %v, %v", page, err) + } + if page[0].Content != nil || page[0].Name != "a" || !EntryHasActiveLock(page[0], time.Now()) { + t.Fatalf("page entry %+v", page[0]) + } +}