@@ -40,10 +40,15 @@ func unpackTar(tr *tar.Reader, path string) error {
40
40
41
41
if strings .Contains (header .Name , ".wh." ) {
42
42
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
+ }
46
48
}
49
+
50
+ // Remove the whited-out path.
51
+ newName := strings .Replace (rmPath , ".wh." , "" , 1 )
47
52
if err = os .RemoveAll (newName ); err != nil {
48
53
logrus .Error (err )
49
54
}
@@ -71,13 +76,24 @@ func unpackTar(tr *tar.Reader, path string) error {
71
76
// It's possible for a file to be included before the directory it's in is created.
72
77
baseDir := filepath .Dir (target )
73
78
if _ , err := os .Stat (baseDir ); os .IsNotExist (err ) {
79
+ logrus .Debugf ("baseDir %s for file %s does not exist. Creating." , baseDir , target )
74
80
if err := os .MkdirAll (baseDir , 0755 ); err != nil {
75
81
return err
76
82
}
77
83
}
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 )
79
95
if err != nil {
80
- logrus .Errorf ("Error opening file %s" , target )
96
+ logrus .Errorf ("Error creating file %s %s " , target , err )
81
97
return err
82
98
}
83
99
// manually set permissions on file, since the default umask (022) will interfere
0 commit comments