Merge pull request #2702 from caervs/fix_path_enumeration

Ignore missing paths during enumeration
This commit is contained in:
Olivier Gambier 2018-09-05 15:44:52 -07:00 committed by GitHub
commit efa4c3bb5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"errors" "errors"
"sort" "sort"
"github.com/sirupsen/logrus"
) )
// ErrSkipDir is used as a return value from onFileFunc to indicate that // ErrSkipDir is used as a return value from onFileFunc to indicate that
@ -32,8 +34,15 @@ func WalkFallback(ctx context.Context, driver StorageDriver, from string, f Walk
// performance bottleneck. // performance bottleneck.
fileInfo, err := driver.Stat(ctx, child) fileInfo, err := driver.Stat(ctx, child)
if err != nil { if err != nil {
switch err.(type) {
case PathNotFoundError:
// repository was removed in between listing and enumeration. Ignore it.
logrus.WithField("path", child).Infof("ignoring deleted path")
continue
default:
return err return err
} }
}
err = f(fileInfo) err = f(fileInfo)
if err == nil && fileInfo.IsDir() { if err == nil && fileInfo.IsDir() {
if err := WalkFallback(ctx, driver, child, f); err != nil { if err := WalkFallback(ctx, driver, child, f); err != nil {

View file

@ -0,0 +1,47 @@
package driver
import (
"context"
"fmt"
"testing"
)
type changingFileSystem struct {
StorageDriver
fileset []string
keptFiles map[string]bool
}
func (cfs *changingFileSystem) List(ctx context.Context, path string) ([]string, error) {
return cfs.fileset, nil
}
func (cfs *changingFileSystem) Stat(ctx context.Context, path string) (FileInfo, error) {
kept, ok := cfs.keptFiles[path]
if ok && kept {
return &FileInfoInternal{
FileInfoFields: FileInfoFields{
Path: path,
},
}, nil
}
return nil, PathNotFoundError{}
}
func TestWalkFileRemoved(t *testing.T) {
d := &changingFileSystem{
fileset: []string{"zoidberg", "bender"},
keptFiles: map[string]bool{
"zoidberg": true,
},
}
infos := []FileInfo{}
err := WalkFallback(context.Background(), d, "", func(fileInfo FileInfo) error {
infos = append(infos, fileInfo)
return nil
})
if len(infos) != 1 || infos[0].Path() != "zoidberg" {
t.Errorf(fmt.Sprintf("unexpected path set during walk: %s", infos))
}
if err != nil {
t.Fatalf(err.Error())
}
}