github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/catalog.go (about)

     1  package storage
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/docker/distribution/context"
    10  	"github.com/docker/distribution/registry/storage/driver"
    11  )
    12  
    13  // ErrFinishedWalk is used when the called walk function no longer wants
    14  // to accept any more values.  This is used for pagination when the
    15  // required number of repos have been found.
    16  var ErrFinishedWalk = errors.New("finished walk")
    17  
    18  // Returns a list, or partial list, of repositories in the registry.
    19  // Because it's a quite expensive operation, it should only be used when building up
    20  // an initial set of repositories.
    21  func (reg *registry) Repositories(ctx context.Context, repos []string, last string) (n int, errVal error) {
    22  	var foundRepos []string
    23  
    24  	if len(repos) == 0 {
    25  		return 0, errors.New("no space in slice")
    26  	}
    27  
    28  	root, err := pathFor(repositoriesRootPathSpec{})
    29  	if err != nil {
    30  		return 0, err
    31  	}
    32  
    33  	err = Walk(ctx, reg.blobStore.driver, root, func(fileInfo driver.FileInfo) error {
    34  		filePath := fileInfo.Path()
    35  
    36  		// lop the base path off
    37  		repoPath := filePath[len(root)+1:]
    38  
    39  		_, file := path.Split(repoPath)
    40  		if file == "_layers" {
    41  			repoPath = strings.TrimSuffix(repoPath, "/_layers")
    42  			if repoPath > last {
    43  				foundRepos = append(foundRepos, repoPath)
    44  			}
    45  			return ErrSkipDir
    46  		} else if strings.HasPrefix(file, "_") {
    47  			return ErrSkipDir
    48  		}
    49  
    50  		// if we've filled our array, no need to walk any further
    51  		if len(foundRepos) == len(repos) {
    52  			return ErrFinishedWalk
    53  		}
    54  
    55  		return nil
    56  	})
    57  
    58  	n = copy(repos, foundRepos)
    59  
    60  	// Signal that we have no more entries by setting EOF
    61  	if len(foundRepos) <= len(repos) && err != ErrFinishedWalk {
    62  		errVal = io.EOF
    63  	}
    64  
    65  	return n, errVal
    66  }