@@ -11,8 +11,11 @@ import (
11
11
)
12
12
13
13
type Extractor struct {
14
- Path string
15
- Progress func (int64 ) int64
14
+ Path string
15
+ Progress func (int64 ) int64
16
+ sanitizePaths bool
17
+ sanitizeLinks bool
18
+ stubLinks bool
16
19
}
17
20
18
21
func (te * Extractor ) Extract (reader io.Reader ) error {
@@ -61,13 +64,40 @@ func (te *Extractor) Extract(reader io.Reader) error {
61
64
return nil
62
65
}
63
66
67
+ // Sanitize toggles all output sanitation rules
68
+ func (te * Extractor ) Sanitize (toggle bool ) {
69
+ te .sanitizePaths = toggle
70
+ te .sanitizeLinks = toggle
71
+ }
72
+
73
+ // SanitizePaths toggles converting illegal paths to valid paths on Extract
74
+ func (te * Extractor ) SanitizePaths (toggle bool ) {
75
+ te .sanitizePaths = toggle
76
+
77
+ }
78
+
79
+ // SanitizeLinks toggles failure for links that are absolute or escape the output root on Extract
80
+ func (te * Extractor ) SanitizeLinks (toggle bool ) {
81
+ te .sanitizeLinks = toggle
82
+ }
83
+
84
+ // StubLinks toggles link stubbing. When enabled, all links are created as empty files instead of links
85
+ func (te * Extractor ) StubLinks (toggle bool ) {
86
+ te .stubLinks = toggle
87
+ }
88
+
64
89
// outputPath returns the path at which to place tarPath
65
90
func (te * Extractor ) outputPath (tarPath string ) string {
66
- elems := strings .Split (tarPath , "/" ) // break into elems
67
- elems = elems [1 :] // remove IPFS root
68
- safePath := platformSanitize (elems ) // sanitize IPFS path to be platform legal
69
- safePath = fp .Join (te .Path , safePath ) // rebase on to extraction target root
70
- return safePath
91
+ var outPath string
92
+ elems := strings .Split (tarPath , "/" ) // break into elems
93
+ elems = elems [1 :] // remove original root
94
+ if te .sanitizePaths {
95
+ outPath = platformSanitize (elems ) // sanitize base path elements to be platform legal
96
+ } else {
97
+ outPath = fp .Join (elems ... ) // join elems
98
+ }
99
+ outPath = fp .Join (te .Path , outPath ) // rebase on to extraction target root
100
+ return outPath
71
101
}
72
102
73
103
func (te * Extractor ) extractDir (h * tar.Header , depth int ) error {
@@ -82,6 +112,14 @@ func (te *Extractor) extractDir(h *tar.Header, depth int) error {
82
112
}
83
113
84
114
func (te * Extractor ) extractSymlink (h * tar.Header ) error {
115
+ if te .stubLinks {
116
+ f , err := os .Create (te .outputPath (h .Name ))
117
+ f .Close ()
118
+ return err
119
+ }
120
+ if te .sanitizeLinks {
121
+ return platformLink (te .Path , h .Linkname , te .outputPath (h .Name ))
122
+ }
85
123
return os .Symlink (h .Linkname , te .outputPath (h .Name ))
86
124
}
87
125
0 commit comments