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

Commit 2ac0a10

Browse files
authored
Fix some unpack errors encountered when diffing gcr.io/gcp-runtimes/ubuntu (#159)
1 parent 3d6d468 commit 2ac0a10

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pkg/util/tar_utils.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ func unpackTar(tr *tar.Reader, path string) error {
4040

4141
if strings.Contains(header.Name, ".wh.") {
4242
rmPath := filepath.Join(path, header.Name)
43-
newName := strings.Replace(rmPath, ".wh.", "", 1)
44-
if err := os.Remove(rmPath); err != nil {
45-
logrus.Error(err)
43+
// Remove the .wh file if it was extracted.
44+
if _, err := os.Stat(rmPath); !os.IsNotExist(err) {
45+
if err := os.Remove(rmPath); err != nil {
46+
logrus.Error(err)
47+
}
4648
}
49+
50+
// Remove the whited-out path.
51+
newName := strings.Replace(rmPath, ".wh.", "", 1)
4752
if err = os.RemoveAll(newName); err != nil {
4853
logrus.Error(err)
4954
}
@@ -71,13 +76,24 @@ func unpackTar(tr *tar.Reader, path string) error {
7176
// It's possible for a file to be included before the directory it's in is created.
7277
baseDir := filepath.Dir(target)
7378
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
79+
logrus.Debugf("baseDir %s for file %s does not exist. Creating.", baseDir, target)
7480
if err := os.MkdirAll(baseDir, 0755); err != nil {
7581
return err
7682
}
7783
}
78-
currFile, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
84+
85+
// It's possible we end up creating files that can't be overwritten based on their permissions.
86+
// Explicitly delete an existing file before continuing.
87+
if _, err := os.Stat(target); !os.IsNotExist(err) {
88+
logrus.Debugf("Removing %s for overwrite.", target)
89+
if err := os.Remove(target); err != nil {
90+
return err
91+
}
92+
}
93+
94+
currFile, err := os.Create(target)
7995
if err != nil {
80-
logrus.Errorf("Error opening file %s", target)
96+
logrus.Errorf("Error creating file %s %s", target, err)
8197
return err
8298
}
8399
// manually set permissions on file, since the default umask (022) will interfere

0 commit comments

Comments
 (0)