2015-07-13 22:08:13 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2015-07-17 20:42:47 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
2015-07-13 22:08:13 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3/reference"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2015-07-13 22:08:13 +02:00
|
|
|
)
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
// Returns a list, or partial list, of repositories in the registry.
|
2015-07-13 22:08:13 +02:00
|
|
|
// Because it's a quite expensive operation, it should only be used when building up
|
|
|
|
// an initial set of repositories.
|
2016-07-14 22:28:08 +02:00
|
|
|
func (reg *registry) Repositories(ctx context.Context, repos []string, last string) (n int, err error) {
|
2017-11-29 21:26:28 +01:00
|
|
|
var finishedWalk bool
|
2015-07-17 20:42:47 +02:00
|
|
|
var foundRepos []string
|
|
|
|
|
|
|
|
if len(repos) == 0 {
|
|
|
|
return 0, errors.New("no space in slice")
|
|
|
|
}
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2016-07-14 22:28:08 +02:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:26:28 +01:00
|
|
|
err = reg.blobStore.driver.Walk(ctx, root, func(fileInfo driver.FileInfo) error {
|
2016-08-10 02:42:26 +02:00
|
|
|
err := handleRepository(fileInfo, root, last, func(repoPath string) error {
|
|
|
|
foundRepos = append(foundRepos, repoPath)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-12-06 23:41:38 +01:00
|
|
|
// if we've filled our array, no need to walk any further
|
|
|
|
if len(foundRepos) == len(repos) {
|
2017-11-29 21:26:28 +01:00
|
|
|
finishedWalk = true
|
|
|
|
return driver.ErrSkipDir
|
2015-12-06 23:41:38 +01:00
|
|
|
}
|
|
|
|
|
2015-07-13 22:08:13 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
n = copy(repos, foundRepos)
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2017-11-29 21:26:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
} else if !finishedWalk {
|
|
|
|
// We didn't fill buffer. No more records are available.
|
|
|
|
return n, io.EOF
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 22:28:08 +02:00
|
|
|
return n, err
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
|
|
|
|
// Enumerate applies ingester to each repository
|
|
|
|
func (reg *registry) Enumerate(ctx context.Context, ingester func(string) error) error {
|
2016-08-10 02:42:26 +02:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 21:10:20 +01:00
|
|
|
err = reg.blobStore.driver.Walk(ctx, root, func(fileInfo driver.FileInfo) error {
|
2016-08-10 02:42:26 +02:00
|
|
|
return handleRepository(fileInfo, root, "", ingester)
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
2016-07-21 16:38:42 +02:00
|
|
|
|
2018-08-03 07:58:52 +02:00
|
|
|
// Remove removes a repository from storage
|
2018-08-06 18:46:42 +02:00
|
|
|
func (reg *registry) Remove(ctx context.Context, name reference.Named) error {
|
2018-08-03 07:58:52 +02:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
repoDir := path.Join(root, name.Name())
|
2018-08-06 18:46:42 +02:00
|
|
|
return reg.driver.Delete(ctx, repoDir)
|
2018-08-03 07:58:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:18:43 +02: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 02:09:49 +02: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 02:18:43 +02: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 16:38:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:18:43 +02:00
|
|
|
if c1 < c2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c1 > c2 {
|
|
|
|
return +1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s1) < len(s2) {
|
|
|
|
return -1
|
|
|
|
}
|
2016-07-21 16:38:42 +02:00
|
|
|
|
2016-08-06 02:18:43 +02:00
|
|
|
if len(s1) > len(s2) {
|
|
|
|
return +1
|
2016-07-21 16:38:42 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:18:43 +02:00
|
|
|
return 0
|
2016-07-21 16:38:42 +02:00
|
|
|
}
|
2016-08-10 02:42:26 +02:00
|
|
|
|
|
|
|
// handleRepository calls function fn with a repository path if fileInfo
|
|
|
|
// has a path of a repository under root and that it is lexographically
|
|
|
|
// after last. Otherwise, it will return ErrSkipDir. This should be used
|
|
|
|
// with Walk to do handling with repositories in a storage.
|
|
|
|
func handleRepository(fileInfo driver.FileInfo, root, last string, fn func(repoPath string) error) error {
|
|
|
|
filePath := fileInfo.Path()
|
|
|
|
|
|
|
|
// lop the base path off
|
|
|
|
repo := filePath[len(root)+1:]
|
|
|
|
|
|
|
|
_, file := path.Split(repo)
|
2020-05-28 21:42:43 +02:00
|
|
|
if file == "_manifests" {
|
|
|
|
repo = strings.TrimSuffix(repo, "/_manifests")
|
2016-08-10 02:42:26 +02:00
|
|
|
if lessPath(last, repo) {
|
|
|
|
if err := fn(repo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 20:17:39 +01:00
|
|
|
return driver.ErrSkipDir
|
2016-08-10 02:42:26 +02:00
|
|
|
} else if strings.HasPrefix(file, "_") {
|
2017-11-29 20:17:39 +01:00
|
|
|
return driver.ErrSkipDir
|
2016-08-10 02:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|