Rename catalog funcs and update their godocs.

Signed-off-by: Milos Gajdos <milosgajdos83@gmail.com>
This commit is contained in:
Milos Gajdos 2021-05-28 11:23:15 +01:00
parent 27bd92bd51
commit 733c0f0323
No known key found for this signature in database
GPG key ID: 8B31058CC55DFD4F

View file

@ -3,6 +3,7 @@ package storage
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"path" "path"
"sort" "sort"
@ -13,10 +14,12 @@ import (
) )
var ( var (
ErrStopRec = errors.New("Stopped the recursion for getting repositories") // ErrStopReposWalk is used as a return value to indicate that the repository path walk
// should be stopped. It's not returned as an error by any function.
ErrStopReposWalk = errors.New("stop repos walk")
) )
// Returns a list, or partial list, of repositories in the registry. // Returns a list or a partial list of repositories in the registry.
// Because it's a quite expensive operation, it should only be used when building up // Because it's a quite expensive operation, it should only be used when building up
// an initial set of repositories. // an initial set of repositories.
func (reg *registry) Repositories(ctx context.Context, repos []string, last string) (n int, err error) { func (reg *registry) Repositories(ctx context.Context, repos []string, last string) (n int, err error) {
@ -24,7 +27,7 @@ func (reg *registry) Repositories(ctx context.Context, repos []string, last stri
var foundRepos []string var foundRepos []string
if len(repos) == 0 { if len(repos) == 0 {
return 0, errors.New("no space in slice") return -1, errors.New("no repos requested")
} }
root, err := pathFor(repositoriesRootPathSpec{}) root, err := pathFor(repositoriesRootPathSpec{})
@ -32,14 +35,14 @@ func (reg *registry) Repositories(ctx context.Context, repos []string, last stri
return 0, err return 0, err
} }
err = reg.getRepositories(ctx, root, last, func(repoPath string) error { err = reg.walkRepos(ctx, root, last, func(repoPath string) error {
// this is placed before the append, // this is placed before the append,
// so that we will get a extra repo if // so that we will get an extra repo if
// any. This assures that we do not return // any. This ensures that we do not return
// io.EOF without it being the last record. // io.EOF without it being the last record.
if len(foundRepos) == len(repos) { if len(foundRepos) == len(repos) {
finishedWalk = true finishedWalk = true
return ErrStopRec return ErrStopReposWalk
} }
foundRepos = append(foundRepos, repoPath) foundRepos = append(foundRepos, repoPath)
@ -66,7 +69,7 @@ func (reg *registry) Enumerate(ctx context.Context, ingester func(string) error)
return err return err
} }
return reg.getRepositories(ctx, root, "", ingester) return reg.walkRepos(ctx, root, "", ingester)
} }
// Remove removes a repository from storage // Remove removes a repository from storage
@ -79,12 +82,11 @@ func (reg *registry) Remove(ctx context.Context, name reference.Named) error {
return reg.driver.Delete(ctx, repoDir) return reg.driver.Delete(ctx, repoDir)
} }
// getRepositories is a helper function for getRepositoriesRec calls // walkRepos walks paths rooted in root calling fn for each found repo path.
// the function fn with a repository path, if the current path looked // Paths are walked in a lexical order which makes the output deterministic.
// at is a repository and is lexicographically after last. It is possible // If last is not an empty string it walks all repo paths. Otherwise
// to return driver.ErrSkipDir, if there is no interest in any repositories // it returns when last repoPath is found.
// under the given `repoPath`, or call ErrStopRec if the recursion should stop. func (reg *registry) walkRepos(ctx context.Context, root, last string, fn func(repoPath string) error) error {
func (reg *registry) getRepositories(ctx context.Context, root, last string, fn func(repoPath string) error) error {
midFn := fn midFn := fn
// middleware func to exclude the `last` repo // middleware func to exclude the `last` repo
@ -100,16 +102,16 @@ func (reg *registry) getRepositories(ctx context.Context, root, last string, fn
// call our recursive func, with the midFn and the start path // call our recursive func, with the midFn and the start path
// of where we want to find repositories. // of where we want to find repositories.
err := reg.getRepositoriesRec(ctx, root, root, last, midFn) err := reg.walkReposPath(ctx, root, root, last, midFn)
if err == ErrStopRec { if err == ErrStopReposWalk {
return nil return nil
} }
return err return err
} }
// getRepositoriesRec recurse through all folders it the `lookPath`, // walkReposPath walks through all folders in `lookPath`,
// there it will try to find repositories. See getRepositories for more. // looking for repositories. See walkRepos for more detailed description.
func (reg *registry) getRepositoriesRec(ctx context.Context, root, lookPath, last string, fn func(repoPath string) error) error { func (reg *registry) walkReposPath(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 // 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 f, err := reg.blobStore.driver.Stat(ctx, lookPath); err != nil || !f.IsDir() {
if err != nil { if err != nil {
@ -128,25 +130,25 @@ func (reg *registry) getRepositoriesRec(ctx context.Context, root, lookPath, las
sort.Strings(children) sort.Strings(children)
if last != "" { if last != "" {
splitLasts := strings.Split(last, "/") splitLast := strings.Split(last, "/")
// call the next iteration of getRepositoriesRec if any, but // call the next iteration of walkReposPath if any, but
// exclude the current one. // exclude the current one.
if len(splitLasts) > 1 { if len(splitLast) > 1 {
if err := reg.getRepositoriesRec(ctx, root, lookPath+"/"+splitLasts[0], strings.Join(splitLasts[1:], "/"), fn); err != nil { if err := reg.walkReposPath(ctx, root, lookPath+"/"+splitLast[0], strings.Join(splitLast[1:], "/"), fn); err != nil {
return err return err
} }
} }
// find current last path in our children // find current last path in our children
n := sort.SearchStrings(children, lookPath+"/"+splitLasts[0]) n := sort.SearchStrings(children, lookPath+"/"+splitLast[0])
if n == len(children) || children[n] != lookPath+"/"+splitLasts[0] { if n == len(children) || children[n] != lookPath+"/"+splitLast[0] {
return errors.New("the provided 'last' repositories does not exists") return fmt.Errorf("%q repository not found", last)
} }
// if this is not a final `last` (there are more `/` left) // if this is not a final `last` (there are more `/` left)
// then exclude the current index, else include it // then exclude the current index, else include it
if len(splitLasts) > 1 { if len(splitLast) > 1 {
children = children[n+1:] children = children[n+1:]
} else { } else {
children = children[n:] children = children[n:]
@ -156,15 +158,15 @@ func (reg *registry) getRepositoriesRec(ctx context.Context, root, lookPath, las
for _, child := range children { for _, child := range children {
_, file := path.Split(child) _, file := path.Split(child)
if file == "_manifest" { if file == "_manifests" {
if err := fn(strings.TrimPrefix(lookPath, root+"/")); err != nil { if err := fn(strings.TrimPrefix(lookPath, root+"/")); err != nil {
if err == driver.ErrSkipDir { if err == driver.ErrSkipDir {
break break
} }
return err return err
} }
} else if file[0] != '_' { } else if !strings.HasPrefix(file, "_") {
if err := reg.getRepositoriesRec(ctx, root, child, "", fn); err != nil { if err := reg.walkReposPath(ctx, root, child, "", fn); err != nil {
return err return err
} }
} }