Skip to content

Commit

Permalink
restoring imgutil.MakeFileSafeName method to convert the image name t…
Browse files Browse the repository at this point in the history
…o a valid directory name

Signed-off-by: Juan Bustamante <[email protected]>
  • Loading branch information
jjbustamante committed Apr 9, 2024
1 parent b85e83c commit ba1701d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
19 changes: 14 additions & 5 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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, "/", "_")
}
2 changes: 1 addition & 1 deletion index/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
Expand Down
2 changes: 1 addition & 1 deletion layout/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion local/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ba1701d

Please sign in to comment.