forked from TrueCloudLab/distribution
32ac467992
Move the Walk types into registry/storage/driver, and add a Walk method to each storage driver. Although this is yet another API to implement, there is a fall back implementation that relies on List and Stat. For some filesystems this is very slow. Also, this WalkDir Method conforms better do a traditional WalkDir (a la filepath). This change is in preparation for refactoring. Signed-off-by: Sargun Dhillon <sargun@sargun.me>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
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
|
|
// 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 {
|
|
children, err := driver.List(ctx, from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sort.Stable(sort.StringSlice(children))
|
|
for _, child := range children {
|
|
// 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.
|
|
fileInfo, err := driver.Stat(ctx, child)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = f(fileInfo)
|
|
skipDir := (err == storageDriver.ErrSkipDir)
|
|
if err != nil && !skipDir {
|
|
return err
|
|
}
|
|
|
|
if fileInfo.IsDir() && !skipDir {
|
|
if err := Walk(ctx, driver, child, f); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
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))
|
|
}
|