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"
|
2023-06-27 10:26:56 +00:00
|
|
|
"fmt"
|
2015-07-17 18:42:47 +00:00
|
|
|
"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 (
|
2023-06-27 10:27:05 +00:00
|
|
|
// ErrStopReposWalk is a sentinel error to indicate that the repository path walk was stopped.
|
2023-06-27 10:26:56 +00:00
|
|
|
ErrStopReposWalk = errors.New("stop repos walk")
|
2023-06-27 10:26:51 +00:00
|
|
|
)
|
|
|
|
|
2023-06-27 10:27:05 +00:00
|
|
|
// Repositories populates the passed passed repos slice with repositories in the
|
|
|
|
// registry up to the capacity of the slice. Returns the number of repos returned and
|
|
|
|
// `io.EOF` if no more repositories are available.
|
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
|
|
|
|
|
2023-06-27 10:27:05 +00:00
|
|
|
if len(repos) == 0 && cap(repos) == 0 {
|
|
|
|
return 0, errors.New("no repos requested")
|
2015-07-17 18:42:47 +00:00
|
|
|
}
|
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:56 +00:00
|
|
|
err = reg.walkRepos(ctx, root, last, func(repoPath string) error {
|
2023-06-27 10:26:51 +00:00
|
|
|
// this is placed before the append,
|
2023-06-27 10:26:56 +00:00
|
|
|
// so that we will get an extra repo if
|
|
|
|
// any. This ensures that we do not return
|
2023-06-27 10:26:51 +00:00
|
|
|
// 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:56 +00:00
|
|
|
return ErrStopReposWalk
|
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:56 +00:00
|
|
|
return reg.walkRepos(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:56 +00:00
|
|
|
// walkRepos walks paths rooted in root calling fn for each found repo path.
|
|
|
|
// Paths are walked in a lexical order which makes the output deterministic.
|
|
|
|
// If last is not an empty string it walks all repo paths. Otherwise
|
|
|
|
// it returns when last repoPath is found.
|
|
|
|
func (reg *registry) walkRepos(ctx context.Context, root, last string, fn func(repoPath string) error) error {
|
2023-06-27 10:26:51 +00:00
|
|
|
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.
|
2023-06-27 10:26:56 +00:00
|
|
|
err := reg.walkReposPath(ctx, root, root, last, midFn)
|
|
|
|
if err == ErrStopReposWalk {
|
2023-06-27 10:26:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2016-08-06 00:18:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:26:56 +00:00
|
|
|
// walkReposPath walks through all folders in `lookPath`,
|
|
|
|
// looking for repositories. See walkRepos for more detailed description.
|
|
|
|
func (reg *registry) walkReposPath(ctx context.Context, root, lookPath, last string, fn func(repoPath string) error) error {
|
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 != "" {
|
2023-06-27 10:26:56 +00:00
|
|
|
splitLast := strings.Split(last, "/")
|
2016-07-21 14:38:42 +00:00
|
|
|
|
2023-06-27 10:26:56 +00:00
|
|
|
// call the next iteration of walkReposPath if any, but
|
2023-06-27 10:26:51 +00:00
|
|
|
// exclude the current one.
|
2023-06-27 10:26:56 +00:00
|
|
|
if len(splitLast) > 1 {
|
|
|
|
if err := reg.walkReposPath(ctx, root, lookPath+"/"+splitLast[0], strings.Join(splitLast[1:], "/"), fn); err != nil {
|
2023-06-27 10:26:51 +00:00
|
|
|
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
|
2023-06-27 10:26:56 +00:00
|
|
|
n := sort.SearchStrings(children, lookPath+"/"+splitLast[0])
|
|
|
|
if n == len(children) || children[n] != lookPath+"/"+splitLast[0] {
|
|
|
|
return fmt.Errorf("%q repository not found", last)
|
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
|
2023-06-27 10:26:56 +00:00
|
|
|
if len(splitLast) > 1 {
|
2023-06-27 10:26:51 +00:00
|
|
|
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:56 +00:00
|
|
|
if file == "_manifests" {
|
2023-06-27 10:26:51 +00:00
|
|
|
if err := fn(strings.TrimPrefix(lookPath, root+"/")); err != nil {
|
|
|
|
if err == driver.ErrSkipDir {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-06-27 10:26:56 +00:00
|
|
|
} else if !strings.HasPrefix(file, "_") {
|
|
|
|
if err := reg.walkReposPath(ctx, root, child, "", fn); err != nil {
|
2016-08-10 00:42:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|