From ba1701d7d6c165464a03e00d71a04a657f583b35 Mon Sep 17 00:00:00 2001 From: Juan Bustamante Date: Tue, 9 Apr 2024 12:07:49 -0500 Subject: [PATCH] restoring imgutil.MakeFileSafeName method to convert the image name to a valid directory name Signed-off-by: Juan Bustamante --- index.go | 19 ++++++++++++++----- index/new.go | 2 +- index_test.go | 2 +- layout/new.go | 2 +- local/new.go | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/index.go b/index.go index 0a2210c1..a85fe70d 100644 --- a/index.go +++ b/index.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "sync" "github.com/google/go-containerregistry/pkg/name" @@ -894,7 +895,7 @@ func (h *ManifestHandler) Add(ref name.Reference, ops ...IndexAddOption) error { op(addOps) } - layoutPath := filepath.Join(h.Options.XdgPath, h.Options.Reponame) + layoutPath := filepath.Join(h.Options.XdgPath, MakeFileSafeName(h.Options.Reponame)) path, pathErr := layout.FromPath(layoutPath) if addOps.Local { if pathErr != nil { @@ -1245,7 +1246,7 @@ func (h *ManifestHandler) addPlatformSpecificImages(ref name.Reference, platform h.Images[digest] = *config - layoutPath := filepath.Join(h.Options.XdgPath, h.Options.Reponame) + layoutPath := filepath.Join(h.Options.XdgPath, MakeFileSafeName(h.Options.Reponame)) path, err := layout.FromPath(layoutPath) if err != nil { if path, err = layout.Write(layoutPath, h.ImageIndex); err != nil { @@ -1293,7 +1294,7 @@ func (h *ManifestHandler) save(layoutPath string) (path layout.Path, err error) // Save will locally save the given ImageIndex. func (h *ManifestHandler) Save() error { - layoutPath := filepath.Join(h.Options.XdgPath, h.Options.Reponame) + layoutPath := filepath.Join(h.Options.XdgPath, MakeFileSafeName(h.Options.Reponame)) path, err := layout.FromPath(layoutPath) if err != nil { if path, err = h.save(layoutPath); err != nil { @@ -1399,7 +1400,7 @@ func (h *ManifestHandler) Push(ops ...IndexPushOption) error { } } - layoutPath := filepath.Join(h.Options.XdgPath, h.Options.Reponame) + layoutPath := filepath.Join(h.Options.XdgPath, MakeFileSafeName(h.Options.Reponame)) path, err := layout.FromPath(layoutPath) if err != nil { return err @@ -1501,7 +1502,7 @@ func (h *ManifestHandler) Remove(ref name.Reference) (err error) { // Remove ImageIndex from local filesystem if exists. func (h *ManifestHandler) Delete() error { - layoutPath := filepath.Join(h.Options.XdgPath, h.Options.Reponame) + layoutPath := filepath.Join(h.Options.XdgPath, MakeFileSafeName(h.Options.Reponame)) if _, err := os.Stat(layoutPath); err != nil { return err } @@ -1581,3 +1582,11 @@ func (h *ManifestHandler) getIndexManifest(digest name.Digest) (mfest *v1.IndexM return nil, ErrNoImageOrIndexFoundWithGivenDigest(hash.String()) } + +// Change a reference name string into a valid file name +// Ex: cnbs/sample-package:hello-multiarch-universe +// to cnbs_sample-package-hello-multiarch-universe +func MakeFileSafeName(ref string) string { + fileName := strings.ReplaceAll(ref, ":", "-") + return strings.ReplaceAll(fileName, "/", "_") +} diff --git a/index/new.go b/index/new.go index 251d2056..ff6da556 100644 --- a/index/new.go +++ b/index/new.go @@ -28,7 +28,7 @@ func NewIndex(repoName string, ops ...Option) (idx imgutil.ImageIndex, err error InsecureRegistry: idxOps.insecure, } - layoutPath := filepath.Join(idxOps.xdgPath, idxOps.repoName) + layoutPath := filepath.Join(idxOps.xdgPath, imgutil.MakeFileSafeName(idxOps.repoName)) switch idxOps.format { case types.DockerManifestList: idx = imgutil.NewManifestHandler(imgutil.NewEmptyDockerIndex(), idxOptions) diff --git a/index_test.go b/index_test.go index ae6ee7a9..3fb558a5 100644 --- a/index_test.go +++ b/index_test.go @@ -3726,7 +3726,7 @@ func testIndex(t *testing.T, when spec.G, it spec.S) { h.AssertNil(t, err) err = idx.Delete() - localPath := filepath.Join(xdgPath, "busybox:1.36-musl") + localPath := filepath.Join(xdgPath, imgutil.MakeFileSafeName("busybox:1.36-musl")) h.AssertEq(t, err.Error(), fmt.Sprintf("stat %s: no such file or directory", localPath)) }) }) diff --git a/layout/new.go b/layout/new.go index a3fd2a4f..71f1c533 100644 --- a/layout/new.go +++ b/layout/new.go @@ -25,7 +25,7 @@ func NewIndex(repoName string, ops ...index.Option) (idx imgutil.ImageIndex, err } } - path, err := layout.FromPath(filepath.Join(idxOps.XDGRuntimePath(), idxOps.RepoName())) + path, err := layout.FromPath(filepath.Join(idxOps.XDGRuntimePath(), imgutil.MakeFileSafeName(idxOps.RepoName()))) if err != nil { return idx, err } diff --git a/local/new.go b/local/new.go index 86af792c..de1e6366 100644 --- a/local/new.go +++ b/local/new.go @@ -81,7 +81,7 @@ func NewIndex(repoName string, ops ...index.Option) (idx imgutil.ImageIndex, err } } - path, err := layout.FromPath(filepath.Join(idxOps.XDGRuntimePath(), idxOps.RepoName())) + path, err := layout.FromPath(filepath.Join(idxOps.XDGRuntimePath(), imgutil.MakeFileSafeName(idxOps.RepoName()))) if err != nil { return idx, err }