2015-07-13 20:08:13 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-07-17 18:42:47 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2015-07-13 20:08:13 +00:00
|
|
|
"path"
|
2023-06-27 10:26:51 +00:00
|
|
|
"sort"
|
2015-07-13 20:08:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/reference"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2015-07-13 20:08:13 +00:00
|
|
|
)
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
var (
|
|
|
|
ErrStopRec = errors.New("Stopped the recursion for getting repositories")
|
|
|
|
)
|
|
|
|
|
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) {
|
2017-11-29 20:26:28 +00:00
|
|
|
var finishedWalk bool
|
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
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
err = reg.getRepositories(ctx, root, last, func(repoPath string) error {
|
|
|
|
// this is placed before the append,
|
|
|
|
// so that we will get a extra repo if
|
|
|
|
// any. This assures that we do not return
|
|
|
|
// io.EOF without it being the last record.
|
2015-12-06 22:41:38 +00:00
|
|
|
if len(foundRepos) == len(repos) {
|
2017-11-29 20:26:28 +00:00
|
|
|
finishedWalk = true
|
2023-06-27 10:26:51 +00:00
|
|
|
return ErrStopRec
|
2015-12-06 22:41:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
foundRepos = append(foundRepos, repoPath)
|
|
|
|
|
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
|
|
|
|
2017-11-29 20:26:28 +00: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 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 {
|
2016-08-10 00:42:26 +00:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
return reg.getRepositories(ctx, root, "", ingester)
|
2016-01-19 22:26:15 +00:00
|
|
|
}
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2018-08-03 05:58:52 +00:00
|
|
|
// Remove removes a repository from storage
|
2018-08-06 16:46:42 +00:00
|
|
|
func (reg *registry) Remove(ctx context.Context, name reference.Named) error {
|
2018-08-03 05:58:52 +00:00
|
|
|
root, err := pathFor(repositoriesRootPathSpec{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
repoDir := path.Join(root, name.Name())
|
2018-08-06 16:46:42 +00:00
|
|
|
return reg.driver.Delete(ctx, repoDir)
|
2018-08-03 05:58:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// getRepositories is a helper function for getRepositoriesRec calls
|
|
|
|
// the function fn with a repository path, if the current path looked
|
|
|
|
// at is a repository and is lexicographically after last. It is possible
|
|
|
|
// to return driver.ErrSkipDir, if there is no interest in any repositories
|
|
|
|
// under the given `repoPath`, or call ErrStopRec if the recursion should stop.
|
|
|
|
func (reg *registry) getRepositories(ctx context.Context, root, last string, fn func(repoPath string) error) error {
|
|
|
|
midFn := fn
|
|
|
|
|
|
|
|
// middleware func to exclude the `last` repo
|
|
|
|
// only use it, if there is set a last.
|
|
|
|
if last != "" {
|
|
|
|
midFn = func(repoPath string) error {
|
|
|
|
if repoPath != last {
|
|
|
|
return fn(repoPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// call our recursive func, with the midFn and the start path
|
|
|
|
// of where we want to find repositories.
|
|
|
|
err := reg.getRepositoriesRec(ctx, root, root, last, midFn)
|
|
|
|
if err == ErrStopRec {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2016-08-06 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// getRepositoriesRec recurse through all folders it the `lookPath`,
|
|
|
|
// there it will try to find repositories. See getRepositories for more.
|
|
|
|
func (reg *registry) getRepositoriesRec(ctx context.Context, root, lookPath, last string, fn func(repoPath string) error) error {
|
|
|
|
// ensure that the current path is a dir, otherwise we just return
|
|
|
|
if f, err := reg.blobStore.driver.Stat(ctx, lookPath); err != nil || !f.IsDir() {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-09 00:09:49 +00:00
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// get children in the current path
|
|
|
|
children, err := reg.blobStore.driver.List(ctx, lookPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-06 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// sort this, so that it will be added in the correct order
|
|
|
|
sort.Strings(children)
|
2016-08-06 00:18:43 +00:00
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
if last != "" {
|
|
|
|
splitLasts := strings.Split(last, "/")
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// call the next iteration of getRepositoriesRec if any, but
|
|
|
|
// exclude the current one.
|
|
|
|
if len(splitLasts) > 1 {
|
|
|
|
if err := reg.getRepositoriesRec(ctx, root, lookPath+"/"+splitLasts[0], strings.Join(splitLasts[1:], "/"), fn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-06 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// find current last path in our children
|
|
|
|
n := sort.SearchStrings(children, lookPath+"/"+splitLasts[0])
|
|
|
|
if n == len(children) || children[n] != lookPath+"/"+splitLasts[0] {
|
|
|
|
return errors.New("the provided 'last' repositories does not exists")
|
2016-08-06 00:18:43 +00:00
|
|
|
}
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
// if this is not a final `last` (there are more `/` left)
|
|
|
|
// then exclude the current index, else include it
|
|
|
|
if len(splitLasts) > 1 {
|
|
|
|
children = children[n+1:]
|
|
|
|
} else {
|
|
|
|
children = children[n:]
|
|
|
|
}
|
2016-07-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
for _, child := range children {
|
|
|
|
_, file := path.Split(child)
|
2016-08-10 00:42:26 +00:00
|
|
|
|
2023-06-27 10:26:51 +00:00
|
|
|
if file == "_manifest" {
|
|
|
|
if err := fn(strings.TrimPrefix(lookPath, root+"/")); err != nil {
|
|
|
|
if err == driver.ErrSkipDir {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if file[0] != '_' {
|
|
|
|
if err := reg.getRepositoriesRec(ctx, root, child, "", fn); err != nil {
|
2016-08-10 00:42:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|