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

Commit cb5b89e

Browse files
dlorencdlorenc
authored and
dlorenc
committed
Handle the case where files appear in the tar before their directories.
1 parent 46ffad9 commit cb5b89e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pkg/util/tar_utils.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,25 @@ func unpackTar(tr *tar.Reader, path string) error {
5858

5959
// if its a dir and it doesn't exist create it
6060
case tar.TypeDir:
61-
if _, err := os.Stat(target); err != nil {
61+
if _, err := os.Stat(target); os.IsNotExist(err) {
6262
if err := os.MkdirAll(target, mode); err != nil {
63-
glog.Errorf("Error creating directory %s while untarring", target)
6463
return err
6564
}
66-
continue
65+
} else {
66+
if err := os.Chmod(target, mode); err != nil {
67+
return err
68+
}
6769
}
6870

6971
// if it's a file create it
7072
case tar.TypeReg:
71-
73+
// It's possible for a file to be included before the directory it's in is created.
74+
baseDir := filepath.Dir(target)
75+
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
76+
if err := os.MkdirAll(baseDir, 0755); err != nil {
77+
return err
78+
}
79+
}
7280
currFile, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode)
7381
if err != nil {
7482
glog.Errorf("Error opening file %s", target)

0 commit comments

Comments
 (0)