From bb5ebe69844246c8ff8d41223bdbe2e8cb7b685d Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani Date: Thu, 29 Aug 2019 13:39:32 -0700 Subject: [PATCH] issue #14: fix repo path walk --- pkg/api/routes_test.go | 18 +++++++++++ pkg/storage/storage.go | 61 ++++++++++++++++++------------------- pkg/storage/storage_test.go | 6 ---- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/pkg/api/routes_test.go b/pkg/api/routes_test.go index 9cad2bdd..a214ec2c 100644 --- a/pkg/api/routes_test.go +++ b/pkg/api/routes_test.go @@ -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() { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index a2186899..2577fec6 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -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) { diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index d2840a99..4aff1ae1 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -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) - }) }) }