b2: simplify object iteration

Blazer is moving to a simpler object list interface, so I'm changing
this here as well.
This commit is contained in:
Toby Burress 2018-05-25 14:53:49 -07:00
parent 233596f4bc
commit 8ceda538ef
1 changed files with 25 additions and 50 deletions

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"net/http" "net/http"
"path" "path"
"strings"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
@ -257,43 +256,25 @@ func (be *b2Backend) Remove(ctx context.Context, h restic.Handle) error {
return errors.Wrap(obj.Delete(ctx), "Delete") return errors.Wrap(obj.Delete(ctx), "Delete")
} }
// List returns a channel that yields all names of blobs of type t. A type semLocker struct {
// goroutine is started for this. If the channel done is closed, sending *backend.Semaphore
// stops. }
func (sm semLocker) Lock() { sm.GetToken() }
func (sm semLocker) Unlock() { sm.ReleaseToken() }
// List returns a channel that yields all names of blobs of type t.
func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error { func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
debug.Log("List %v", t) debug.Log("List %v", t)
prefix, _ := be.Basedir(t)
cur := &b2.Cursor{Prefix: prefix}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
for { prefix, _ := be.Basedir(t)
be.sem.GetToken() iter := be.bucket.List(ctx, b2.ListPrefix(prefix), b2.ListPageSize(be.listMaxItems), b2.ListLocker(semLocker{be.sem}))
objs, c, err := be.bucket.ListCurrentObjects(ctx, be.listMaxItems, cur)
be.sem.ReleaseToken()
if err != nil && err != io.EOF { for iter.Next() {
debug.Log("List: %v", err) obj := iter.Object()
return err
}
debug.Log("returned %v items", len(objs))
for _, obj := range objs {
// Skip objects returned that do not have the specified prefix.
if !strings.HasPrefix(obj.Name(), prefix) {
continue
}
m := path.Base(obj.Name())
if m == "" {
continue
}
if ctx.Err() != nil {
return ctx.Err()
}
attrs, err := obj.Attrs(ctx) attrs, err := obj.Attrs(ctx)
if err != nil { if err != nil {
@ -301,25 +282,19 @@ func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic
} }
fi := restic.FileInfo{ fi := restic.FileInfo{
Name: m, Name: path.Base(obj.Name()),
Size: attrs.Size, Size: attrs.Size,
} }
err = fn(fi) if err := fn(fi); err != nil {
if err != nil {
return err return err
} }
if ctx.Err() != nil {
return ctx.Err()
} }
if err := iter.Err(); err != nil {
debug.Log("List: %v", err)
return err
} }
return nil
if err == io.EOF {
return ctx.Err()
}
cur = c
}
} }
// Remove keys for a specified backend type. // Remove keys for a specified backend type.