2015-04-07 22:52:48 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-04-07 22:52:48 +00:00
|
|
|
"fmt"
|
2015-12-02 00:24:31 +00:00
|
|
|
"sort"
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
storageDriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
|
|
|
// from the given path, calling f on each file
|
2017-11-29 19:17:39 +00:00
|
|
|
// If the returned error from the WalkFn is ErrSkipDir and fileInfo refers
|
|
|
|
// to a directory, the directory will not be entered and Walk
|
|
|
|
// will continue the traversal. Otherwise Walk will return
|
|
|
|
// the error
|
|
|
|
func Walk(ctx context.Context, driver storageDriver.StorageDriver, from string, f storageDriver.WalkFn) error {
|
2015-04-27 22:58:58 +00:00
|
|
|
children, err := driver.List(ctx, from)
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-02 00:24:31 +00:00
|
|
|
sort.Stable(sort.StringSlice(children))
|
2015-04-07 22:52:48 +00:00
|
|
|
for _, child := range children {
|
2015-12-02 00:24:31 +00:00
|
|
|
// TODO(stevvooe): Calling driver.Stat for every entry is quite
|
|
|
|
// expensive when running against backends with a slow Stat
|
|
|
|
// implementation, such as s3. This is very likely a serious
|
|
|
|
// performance bottleneck.
|
2015-04-27 22:58:58 +00:00
|
|
|
fileInfo, err := driver.Stat(ctx, child)
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = f(fileInfo)
|
2017-11-29 19:17:39 +00:00
|
|
|
skipDir := (err == storageDriver.ErrSkipDir)
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil && !skipDir {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.IsDir() && !skipDir {
|
2015-12-02 00:24:07 +00:00
|
|
|
if err := Walk(ctx, driver, child, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushError formats an error type given a path and an error
|
|
|
|
// and pushes it to a slice of errors
|
|
|
|
func pushError(errors []error, path string, err error) []error {
|
|
|
|
return append(errors, fmt.Errorf("%s: %s", path, err))
|
|
|
|
}
|