-
Notifications
You must be signed in to change notification settings - Fork 3
/
probe.go
308 lines (270 loc) · 7.71 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package boar
import (
"fmt"
"path/filepath"
"strings"
"github.com/itchio/dash"
"github.com/itchio/boar/rarextractor"
"github.com/itchio/boar/szextractor"
"github.com/itchio/boar/szextractor/xzsource"
"github.com/itchio/savior/bzip2source"
"github.com/itchio/savior/gzipsource"
"github.com/itchio/savior/seeksource"
"github.com/itchio/savior/tarextractor"
"github.com/itchio/savior/zipextractor"
"github.com/pkg/errors"
"github.com/itchio/headway/state"
"github.com/itchio/httpkit/eos"
"github.com/itchio/savior"
)
type Strategy int
const (
StrategyNone Strategy = 0
StrategyZip Strategy = 100
// linux binaries for example - they might be MojoSetup installers
// (.zip files), or they might not be.
StrategyZipUnsure Strategy = 101
StrategyTar Strategy = 200
StrategyTarGz Strategy = 201
StrategyTarBz2 Strategy = 202
StrategyTarXz Strategy = 203
StrategySevenZip Strategy = 300
// .exe files for example - might be self-extracting
// archives 7-zip can handle, or they might not.
StrategySevenZipUnsure Strategy = 301
// .dmg files can only be properly extracted on macOS.
// 7-zip struggles with ISO9660 disk images for example,
// and doesn't support APFS yet (as of 18.05)
StrategyDmg Strategy = 400
// .rar files we do *not* want to open while probing
StrategyRar Strategy = 500
)
func (as Strategy) String() string {
switch as {
case StrategyZip, StrategyZipUnsure:
return "zip"
case StrategyTar:
return "tar"
case StrategyTarGz:
return "tar.gz"
case StrategyTarBz2:
return "tar.bz2"
case StrategyTarXz:
return "tar.xz"
case StrategySevenZip, StrategySevenZipUnsure:
return "7-zip"
case StrategyDmg:
return "dmg"
case StrategyRar:
return "rar"
default:
return "<no strategy>"
}
}
type EntriesLister interface {
Entries() []*savior.Entry
}
type Info struct {
Strategy Strategy
Features savior.ExtractorFeatures
Format string
PostExtract []string
}
func (ai *Info) String() string {
res := ""
res += fmt.Sprintf("%s (via %s)", ai.Format, ai.Strategy)
res += fmt.Sprintf(", %s", ai.Features)
return res
}
// Probe attempts to determine the type of an archive
// Returns (nil, nil) if it is not a recognized archive type
// Returns (nil, non-nil) if it IS a recognized archive type, but something's
// wrong with it.
// Returns (non-nil, nil) if it is a recognized archive type and we
// are confident we can extract it correctly.
func Probe(params ProbeParams) (*Info, error) {
var strategy Strategy
file := params.File
consumer := params.Consumer
ext := getExt(file, consumer)
if params.Candidate != nil && params.Candidate.Flavor == dash.FlavorNativeLinux {
// might be a mojosetup installer - if not, we won't know what to do with it
strategy = StrategyZipUnsure
} else {
strategy = getStrategy(ext)
}
if strategy == StrategyNone {
return nil, nil
}
info := &Info{
Strategy: strategy,
}
if info.Strategy == StrategyDmg {
// There's nothing else we can do about DMG, we don't ship
// an extractor for it.
return info, nil
}
{
checkEarlyExit := true
switch info.Strategy {
case StrategySevenZip:
info.Features = szextractor.FeaturesByExtension(ext)
case StrategyRar:
info.Features = rarextractor.Features()
default:
checkEarlyExit = false
}
if checkEarlyExit && !info.Features.RandomAccess {
// no random access means the format isn't "htfs-friendly",
// ie. enumerating entries might end up downloading the whole
// file. since we're just probing right now, let's just return
// what little info we have.
return info, nil
}
}
// now actually try to open it
ex, err := info.GetExtractor(file, consumer)
if err != nil {
switch strategy {
case StrategySevenZipUnsure:
// we didn't know that one until we try, so it's just
// not a recognized archive format
consumer.Warnf("Tried opening archive with 7-zip but we got: %v", err)
consumer.Warnf("Ignoring...")
return nil, nil
case StrategyZipUnsure:
// we didn't know that one until we try, so it's just
// not a recognized archive format
consumer.Warnf("Tried opening as a zip but we got: %v", err)
consumer.Warnf("Ignoring...")
return nil, nil
default:
return nil, errors.Wrap(err, "opening archive")
}
}
if szex, ok := ex.(szextractor.SzExtractor); ok {
// this codepath runs when we did not have a .zip, .tar.gz etc. file
// extension, but 7-zip detected such a file anyway. in this case, we prefer
// "native" decompressors, implemented in pure Go.
info.Format = szex.GetFormat()
useNativeDecompressor := true
switch info.Format {
case "gzip":
info.Strategy = StrategyTarGz
case "bzip2":
info.Strategy = StrategyTarBz2
case "xz":
info.Strategy = StrategyTarXz
case "tar":
info.Strategy = StrategyTar
case "zip":
info.Strategy = StrategyZip
default:
useNativeDecompressor = false
}
if useNativeDecompressor {
ex, err = info.GetExtractor(file, consumer)
if err != nil {
return nil, errors.Wrap(err, "getting extractor for file")
}
info.Format = info.Strategy.String()
}
} else {
info.Format = info.Strategy.String()
}
info.Features = ex.Features()
var entries []*savior.Entry
// only try listing entries if we have random access support,
// otherwise, we risk downloading the whole archive just to list entries.
if info.Features.RandomAccess {
if el, ok := ex.(EntriesLister); ok {
entries = el.Entries()
if params.OnEntries != nil {
params.OnEntries(entries)
}
}
}
return info, nil
}
func getExt(file eos.File, consumer *state.Consumer) string {
stats, err := file.Stat()
if err != nil {
consumer.Warnf("archive: Could not stat file, going with blank extension")
return ""
}
lowerName := strings.ToLower(stats.Name())
ext := filepath.Ext(lowerName)
if strings.HasSuffix(lowerName, ".tar"+ext) {
ext = ".tar" + ext
}
return ext
}
func getStrategy(ext string) Strategy {
switch ext {
case ".zip":
return StrategyZip
case ".tar":
return StrategyTar
case ".tar.gz":
return StrategyTarGz
case ".tar.bz2":
return StrategyTarBz2
case ".tar.xz":
return StrategyTarXz
case ".dmg":
return StrategyDmg
case ".rar":
return StrategyRar
case ".7z":
return StrategySevenZip
case ".exe":
return StrategySevenZipUnsure
}
return StrategySevenZipUnsure
}
func (ai *Info) GetExtractor(file eos.File, consumer *state.Consumer) (savior.Extractor, error) {
switch ai.Strategy {
case StrategyZip, StrategyZipUnsure:
stats, err := file.Stat()
if err != nil {
return nil, errors.Wrap(err, "stat'ing file to open as zip archive")
}
ex, err := zipextractor.New(file, stats.Size())
if err != nil {
return nil, errors.Wrap(err, "creating zip extractor")
}
return ex, nil
case StrategyRar:
return rarextractor.New(file, consumer)
case StrategyTar:
return tarextractor.New(seeksource.FromFile(file)), nil
case StrategyTarGz:
return tarextractor.New(gzipsource.New(seeksource.FromFile(file))), nil
case StrategyTarBz2:
return tarextractor.New(bzip2source.New(seeksource.FromFile(file))), nil
case StrategyTarXz:
xs, err := xzsource.New(file, consumer)
if err != nil {
return nil, errors.Wrap(err, "creating xz extractor")
}
return tarextractor.New(xs), nil
case StrategySevenZip, StrategySevenZipUnsure:
szex, err := szextractor.New(file, consumer)
if err != nil {
return nil, errors.Wrap(err, "creating 7-zip extractor")
}
// apply blacklist
switch szex.GetFormat() {
// cf. https://github.com/itchio/itch/issues/1700
case "elf":
// won't extract ELF executables
return nil, errors.New("refusing to extract ELF file")
case "pe":
// won't extract PE executables
return nil, errors.New("refusing to extract PE file")
default:
return szex, nil
}
}
return nil, fmt.Errorf("unknown Strategy %d", ai.Strategy)
}