b2: Fix reading metadata for all files when using a subdir - fixes #356

Also fix some confusion with Metadata prefix/root.
This commit is contained in:
Nick Craig-Wood 2016-02-19 12:09:11 +00:00
parent 84b00b362f
commit 85a0f25b95

View file

@ -256,6 +256,10 @@ func (f *Fs) NewFsObject(remote string) fs.Object {
// listFn is called from list to handle an object // listFn is called from list to handle an object
type listFn func(string, *api.File) error type listFn func(string, *api.File) error
// errEndList is a sentinel used to end the list iteration now.
// listFn should return it to end the iteration with no errors.
var errEndList = errors.New("end list")
// list lists the objects into the function supplied from // list lists the objects into the function supplied from
// the bucket and root supplied // the bucket and root supplied
// //
@ -294,6 +298,9 @@ func (f *Fs) list(prefix string, limit int, hidden bool, fn listFn) error {
for { for {
_, err = f.srv.CallJSON(&opts, &request, &response) _, err = f.srv.CallJSON(&opts, &request, &response)
if err != nil { if err != nil {
if err == errEndList {
return nil
}
return err return err
} }
for i := range response.Files { for i := range response.Files {
@ -302,7 +309,7 @@ func (f *Fs) list(prefix string, limit int, hidden bool, fn listFn) error {
if !strings.HasPrefix(file.Name, prefix) { if !strings.HasPrefix(file.Name, prefix) {
return nil return nil
} }
err = fn(file.Name[len(prefix):], file) err = fn(file.Name[len(f.root):], file)
if err != nil { if err != nil {
return err return err
} }
@ -637,10 +644,10 @@ func (o *Object) readMetaData() (err error) {
return nil return nil
} }
err = o.fs.list(o.remote, 1, false, func(remote string, object *api.File) error { err = o.fs.list(o.remote, 1, false, func(remote string, object *api.File) error {
if remote == "" { if remote == o.remote {
o.info = *object o.info = *object
} }
return nil return errEndList // read only 1 item
}) })
if o.info.ID != "" { if o.info.ID != "" {
return nil return nil