thirdparty/tar: clearer var names for recursion
License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
Juan Batiz-Benet committed
Aug 4, 2015 at 12:07 UTC
f46098459431ea399cb4b449e436963b86a6526d
1 file changed
+13
-17
thirdparty/tar/extractor.go
+13
-17
@@ -18,14 +18,14 @@ func (te *Extractor) Extract(reader io.Reader) error {
18
// Check if the output path already exists, so we know whether we should
19
// create our output with that name, or if we should put the output inside
20
// a preexisting directory
21
- exists := true
22
- pathIsDir := false
21
+ rootExists := true
22
+ rootIsDir := false
23
if stat, err := os.Stat(te.Path); err != nil && os.IsNotExist(err) {
24
- exists = false
24
+ rootExists = false
25
} else if err != nil {
26
return err
27
} else if stat.IsDir() {
28
- pathIsDir = true
28
+ rootIsDir = true
29
}
30
31
// files come recursively in order (i == 0 is root directory)
@@ -39,22 +39,22 @@ func (te *Extractor) Extract(reader io.Reader) error {
39
}
40
41
if header.Typeflag == tar.TypeDir {
42
- if err := te.extractDir(header, i, exists); err != nil {
42
+ if err := te.extractDir(header, i, rootExists); err != nil {
43
return err
44
}
45
continue
46
}
47
48
- if err := te.extractFile(header, tarReader, i, exists, pathIsDir); err != nil {
48
+ if err := te.extractFile(header, tarReader, i, rootExists, rootIsDir); err != nil {
49
return err
50
}
51
}
52
return nil
53
}
54
55
-func (te *Extractor) extractDir(h *tar.Header, depth int, exists bool) error {
55
+func (te *Extractor) extractDir(h *tar.Header, depth int, rootExists bool) error {
56
pathElements := strings.Split(h.Name, "/")
57
- if !exists {
57
+ if !rootExists {
58
pathElements = pathElements[1:]
59
}
60
path := fp.Join(pathElements...)
@@ -72,18 +72,14 @@ func (te *Extractor) extractDir(h *tar.Header, depth int, exists bool) error {
72
return nil
73
}
74
75
-func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, exists bool, pathIsDir bool) error {
76
- var path string
77
- if depth == 0 {
75
+func (te *Extractor) extractFile(h *tar.Header, r *tar.Reader, depth int, rootExists bool, rootIsDir bool) error {
76
+ path := te.Path
77
+ if depth == 0 && rootExists {
78
// if depth is 0, this is the only file (we aren't 'ipfs get'ing a directory)
79
- switch {
80
- case exists && !pathIsDir:
81
- return os.ErrExist
82
- case exists && pathIsDir:
79
+ if rootIsDir { // putting file inside of a root dir.
80
path = fp.Join(te.Path, h.Name)
84
- case !exists:
85
- path = te.Path
81
}
82
+ // else if the file exists, just overwrite it.
83
} else {
84
// we are outputting a directory, this file is inside of it
85
pathElements := strings.Split(h.Name, "/")[1:]