0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

issue #14: fix repo path walk

This commit is contained in:
Ramkumar Chinchani 2019-08-29 13:39:32 -07:00
parent 8bf6beaf2d
commit bb5ebe6984
3 changed files with 47 additions and 38 deletions

View file

@ -41,6 +41,24 @@ func TestAPI(t *testing.T) {
err = json.Unmarshal(resp.Body(), &repoList)
So(err, ShouldBeNil)
So(len(repoList.Repositories), ShouldEqual, 0)
// after newly created upload should succeed
resp, err = resty.R().Post(BaseURL + "/v2/z/blobs/uploads/")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202)
// after newly created upload should succeed
resp, err = resty.R().Post(BaseURL + "/v2/a/b/c/d/blobs/uploads/")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 202)
resp, err = resty.R().SetResult(&api.RepositoryList{}).Get(BaseURL + "/v2/_catalog")
So(err, ShouldBeNil)
So(resp.StatusCode(), ShouldEqual, 200)
So(resp.String(), ShouldNotBeEmpty)
r := resp.Result().(*api.RepositoryList)
So(r.Repositories[0], ShouldEqual, "a/b/c/d")
So(r.Repositories[1], ShouldEqual, "z")
})
Convey("Get images in a repository", func() {

View file

@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"sync"
"github.com/anuvu/zot/errors"
@ -39,35 +40,11 @@ func NewImageStore(rootDir string, log zerolog.Logger) *ImageStore {
}
if _, err := os.Stat(rootDir); os.IsNotExist(err) {
_ = os.MkdirAll(rootDir, 0700)
} else if _, err := is.Validate(); err != nil {
panic(err)
}
return is
}
func (is *ImageStore) Validate() (bool, error) {
dir := is.rootDir
files, err := ioutil.ReadDir(dir)
if err != nil {
is.log.Error().Err(err).Str("dir", dir).Msg("unable to read directory")
return false, errors.ErrRepoNotFound
}
for _, file := range files {
if !file.IsDir() {
is.log.Error().Err(err).Str("file", file.Name()).Msg("not a directory")
return false, errors.ErrRepoIsNotDir
}
v, err := is.ValidateRepo(file.Name())
if !v {
return v, err
}
}
return true, nil
}
func (is *ImageStore) InitRepo(name string) error {
repoDir := path.Join(is.rootDir, name)
@ -176,19 +153,39 @@ func (is *ImageStore) ValidateRepo(name string) (bool, error) {
func (is *ImageStore) GetRepositories() ([]string, error) {
dir := is.rootDir
files, err := ioutil.ReadDir(dir)
_, err := ioutil.ReadDir(dir)
if err != nil {
is.log.Error().Err(err).Msg("failure walking storage root-dir")
return nil, err
}
stores := make([]string, 0)
for _, file := range files {
p := path.Join(dir, file.Name())
is.log.Debug().Str("dir", p).Str("name", file.Name()).Msg("found image store")
stores = append(stores, file.Name())
}
return stores, nil
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
rel, err := filepath.Rel(is.rootDir, path)
if err != nil {
return nil
}
if ok, err := is.ValidateRepo(rel); !ok || err != nil {
return nil
}
is.log.Debug().Str("dir", path).Str("name", info.Name()).Msg("found image store")
stores = append(stores, rel)
return nil
})
return stores, err
}
func (is *ImageStore) GetImageTags(repo string) ([]string, error) {

View file

@ -38,11 +38,5 @@ func TestRepoLayout(t *testing.T) {
So(v, ShouldEqual, true)
So(err, ShouldBeNil)
})
Convey("Validate all repos", func() {
v, err := il.Validate()
So(v, ShouldEqual, true)
So(err, ShouldBeNil)
})
})
}