Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Remove characters from cache path that are invalid on Windows #285

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var imageDiffs = []imageDiff{
{"", "", true},
{"gcr.io/google-appengine/python", "gcr.io/google-appengine/debian9", false},
{"gcr.io/google-appengine/python", "cats", true},
{"mcr.microsoft.com/mcr/hello-world:latest", "mcr.microsoft.com/mcr/hello-world:latest", false},
}

func TestDiffImages(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func getCacheDir(imageName string) (string, error) {
}
rootDir := filepath.Join(cacheDir, ".container-diff", "cache")
imageName = strings.Replace(imageName, string(os.PathSeparator), "", -1)
return filepath.Join(rootDir, filepath.Clean(imageName)), nil
return filepath.Join(rootDir, pkgutil.CleanFilePath(imageName)), nil
}

func getWriter(outputFile string) (io.Writer, error) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/fs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,11 @@ func DirIsEmpty(path string) (bool, error) {
}
return false, err
}

// CleanFilePath removes characters from a given path that cannot be used
// in paths by the underlying platform (e.g. Windows)
func CleanFilePath(dirtyPath string) string {
var windowsReplacements = []string{"<", "_", ">", "_", ":", "_", "?", "_", "*", "_", "?", "_", "|", "_"}
replacer := strings.NewReplacer(windowsReplacements...)
return filepath.Clean(replacer.Replace(dirtyPath))
}
3 changes: 1 addition & 2 deletions pkg/util/image_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ func GetImage(imageName string, includeLayers bool, cacheDir string) (Image, err
if err != nil {
return Image{}, err
}
dirName := strings.Replace(RemoveTag(imageName)+"@"+imageDigest.String(), ":", "", -1)
path, err := getExtractPathForName(dirName, cacheDir)
path, err := getExtractPathForName(RemoveTag(imageName)+"@"+imageDigest.String(), cacheDir)
if err != nil {
return Image{}, err
}
Expand Down