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

Commit 8fc5ae4

Browse files
authored
Merge pull request #285 from riverar/fix-windows-paths
Remove characters from cache path that are invalid on Windows
2 parents e3e53da + 26dc680 commit 8fc5ae4

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

cmd/diff_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var imageDiffs = []imageDiff{
4444
{"", "", true},
4545
{"gcr.io/google-appengine/python", "gcr.io/google-appengine/debian9", false},
4646
{"gcr.io/google-appengine/python", "cats", true},
47+
{"mcr.microsoft.com/mcr/hello-world:latest", "mcr.microsoft.com/mcr/hello-world:latest", false},
4748
}
4849

4950
func TestDiffImages(t *testing.T) {

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func getCacheDir(imageName string) (string, error) {
168168
}
169169
rootDir := filepath.Join(cacheDir, ".container-diff", "cache")
170170
imageName = strings.Replace(imageName, string(os.PathSeparator), "", -1)
171-
return filepath.Join(rootDir, filepath.Clean(imageName)), nil
171+
return filepath.Join(rootDir, pkgutil.CleanFilePath(imageName)), nil
172172
}
173173

174174
func getWriter(outputFile string) (io.Writer, error) {

pkg/util/fs_utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,11 @@ func DirIsEmpty(path string) (bool, error) {
207207
}
208208
return false, err
209209
}
210+
211+
// CleanFilePath removes characters from a given path that cannot be used
212+
// in paths by the underlying platform (e.g. Windows)
213+
func CleanFilePath(dirtyPath string) string {
214+
var windowsReplacements = []string{"<", "_", ">", "_", ":", "_", "?", "_", "*", "_", "?", "_", "|", "_"}
215+
replacer := strings.NewReplacer(windowsReplacements...)
216+
return filepath.Clean(replacer.Replace(dirtyPath))
217+
}

pkg/util/image_utils.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ func GetImage(imageName string, includeLayers bool, cacheDir string) (Image, err
161161
if err != nil {
162162
return Image{}, err
163163
}
164-
dirName := strings.Replace(RemoveTag(imageName)+"@"+imageDigest.String(), ":", "", -1)
165-
path, err := getExtractPathForName(dirName, cacheDir)
164+
path, err := getExtractPathForName(RemoveTag(imageName)+"@"+imageDigest.String(), cacheDir)
166165
if err != nil {
167166
return Image{}, err
168167
}

0 commit comments

Comments
 (0)