2015-07-13 20:08:13 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-07-17 18:42:47 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2015-07-13 20:08:13 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/context"
|
2015-07-17 18:42:47 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
2015-07-13 20:08:13 +00:00
|
|
|
)
|
|
|
|
|
2015-12-06 22:41:38 +00:00
|
|
|
// ErrFinishedWalk is used when the called walk function no longer wants
|
|
|
|
// to accept any more values. This is used for pagination when the
|
|
|
|
// required number of repos have been found.
|
|
|
|
var ErrFinishedWalk = errors.New("finished walk")
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
// Returns a list, or partial list, of repositories in the registry.
|
2015-07-13 20:08:13 +00:00
|
|
|
// Because it's a quite expensive operation, it should only be used when building up
|
|
|
|
// an initial set of repositories.
|
2016-07-14 20:28:08 +00:00
|
|
|
func (reg *registry) Repositories(ctx context.Context, repos []string, last string) (n int, err error) {
|
2015-07-17 18:42:47 +00:00
|
|
|
var foundRepos []string
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return 0, errors.New("no space in slice")
|
|
|
|
}
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2016-07-14 20:28:08 +00:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:28:08 +00:00
|
|
|
err = Walk(ctx, reg.blobStore.driver, root, func(fileInfo driver.FileInfo) error {
|
2015-07-13 20:08:13 +00:00
|
|
|
filePath := fileInfo.Path()
|
|
|
|
|
|
|
|
// lop the base path off
|
|
|
|
repoPath := filePath[len(root)+1:]
|
|
|
|
|
|
|
|
_, file := path.Split(repoPath)
|
|
|
|
if file == "_layers" {
|
|
|
|
repoPath = strings.TrimSuffix(repoPath, "/_layers")
|
2016-08-06 00:18:43 +00:00
|
|
|
if lessPath(last, repoPath) {
|
2015-07-17 18:42:47 +00:00
|
|
|
foundRepos = append(foundRepos, repoPath)
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
return ErrSkipDir
|
|
|
|
} else if strings.HasPrefix(file, "_") {
|
|
|
|
return ErrSkipDir
|
|
|
|
}
|
|
|
|
|
2015-12-06 22:41:38 +00:00
|
|
|
// if we've filled our array, no need to walk any further
|
|
|
|
if len(foundRepos) == len(repos) {
|
|
|
|
return ErrFinishedWalk
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:08:13 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
n = copy(repos, foundRepos)
|
2015-07-13 20:08:13 +00:00
|
|
|
|
2015-07-17 18:42:47 +00:00
|
|
|
// Signal that we have no more entries by setting EOF
|
2016-07-14 20:28:08 +00:00
|
|
|
if len(foundRepos) <= len(repos) && (err == nil || err == ErrSkipDir) {
|
|
|
|
err = io.EOF
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:28:08 +00:00
|
|
|
return n, err
|
2015-07-13 20:08:13 +00:00
|
|
|
}
|
2016-01-19 22:26:15 +00:00
|
|
|
|
|
|
|
// Enumerate applies ingester to each repository
|
|
|
|
func (reg *registry) Enumerate(ctx context.Context, ingester func(string) error) error {
|
|
|
|
repoNameBuffer := make([]string, 100)
|
|
|
|
var last string
|
|
|
|
for {
|
|
|
|
n, err := reg.Repositories(ctx, repoNameBuffer, last)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
last = repoNameBuffer[n-1]
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
repoName := repoNameBuffer[i]
|
|
|
|
err = ingester(repoName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2016-08-06 00:18:43 +00:00
|
|
|
// lessPath returns true if one path a is less than path b.
|
|
|
|
//
|
|
|
|
// A component-wise comparison is done, rather than the lexical comparison of
|
|
|
|
// strings.
|
|
|
|
func lessPath(a, b string) bool {
|
|
|
|
// we provide this behavior by making separator always sort first.
|
|
|
|
return compareReplaceInline(a, b, '/', '\x00') < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// compareReplaceInline modifies runtime.cmpstring to replace old with new
|
|
|
|
// during a byte-wise comparison.
|
|
|
|
func compareReplaceInline(s1, s2 string, old, new byte) int {
|
2016-08-09 00:09:49 +00:00
|
|
|
// TODO(stevvooe): We are missing an optimization when the s1 and s2 have
|
|
|
|
// the exact same slice header. It will make the code unsafe but can
|
|
|
|
// provide some extra performance.
|
|
|
|
|
2016-08-06 00:18:43 +00:00
|
|
|
l := len(s1)
|
|
|
|
if len(s2) < l {
|
|
|
|
l = len(s2)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
c1, c2 := s1[i], s2[i]
|
|
|
|
if c1 == old {
|
|
|
|
c1 = new
|
|
|
|
}
|
|
|
|
|
|
|
|
if c2 == old {
|
|
|
|
c2 = new
|
2016-07-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 00:18:43 +00:00
|
|
|
if c1 < c2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c1 > c2 {
|
|
|
|
return +1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s1) < len(s2) {
|
|
|
|
return -1
|
|
|
|
}
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2016-08-06 00:18:43 +00:00
|
|
|
if len(s1) > len(s2) {
|
|
|
|
return +1
|
2016-07-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 00:18:43 +00:00
|
|
|
return 0
|
2016-07-21 14:38:42 +00:00
|
|
|
}
|