-
Notifications
You must be signed in to change notification settings - Fork 11
/
files.go
472 lines (393 loc) · 14.7 KB
/
files.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package xtractr
/* Code to find, write, move and delete files. */
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
// ArchiveList is the value returned when searchying for compressed files.
// The map is directory to list of archives in that directory.
type ArchiveList map[string][]string
type archive struct {
// Extension is passed to strings.HasSuffix.
Extension string
// Extract function for this extension.
Extract Interface
}
// Interface is a common interface for extracting compressed or non-compressed files or archives.
type Interface func(*XFile) (int64, []string, []string, error)
// https://github.com/golift/xtractr/issues/44
//
//nolint:gochecknoglobals
var extension2function = []archive{
{Extension: ".tar.bz2", Extract: ChngInt(ExtractTarBzip)},
{Extension: ".tar.gz", Extract: ChngInt(ExtractTarGzip)},
{Extension: ".tar.xz", Extract: ChngInt(ExtractTarXZ)},
{Extension: ".tar.z", Extract: ChngInt(ExtractTarZ)},
// The ones with double extensions that match a single (below) need to come first.
{Extension: ".7z", Extract: Extract7z},
{Extension: ".7z.001", Extract: Extract7z},
{Extension: ".z", Extract: ChngInt(ExtractLZW)}, // everything is lowercase...
{Extension: ".br", Extract: ChngInt(ExtractBrotli)},
{Extension: ".brotli", Extract: ChngInt(ExtractBrotli)},
{Extension: ".bz2", Extract: ChngInt(ExtractBzip)},
{Extension: ".gz", Extract: ChngInt(ExtractGzip)},
{Extension: ".gzip", Extract: ChngInt(ExtractGzip)},
{Extension: ".iso", Extract: ChngInt(ExtractISO)},
{Extension: ".lz4", Extract: ChngInt(ExtractLZ4)},
{Extension: ".r00", Extract: ExtractRAR},
{Extension: ".rar", Extract: ExtractRAR},
{Extension: ".s2", Extract: ChngInt(ExtractS2)},
{Extension: ".snappy", Extract: ChngInt(ExtractSnappy)},
{Extension: ".sz", Extract: ChngInt(ExtractSnappy)},
{Extension: ".tar", Extract: ChngInt(ExtractTar)},
{Extension: ".tbz", Extract: ChngInt(ExtractTarBzip)},
{Extension: ".tbz2", Extract: ChngInt(ExtractTarBzip)},
{Extension: ".tgz", Extract: ChngInt(ExtractTarGzip)},
{Extension: ".txz", Extract: ChngInt(ExtractTarXZ)},
{Extension: ".tz", Extract: ChngInt(ExtractTarZ)},
{Extension: ".xz", Extract: ChngInt(ExtractXZ)},
{Extension: ".zip", Extract: ChngInt(ExtractZIP)},
{Extension: ".zlib", Extract: ChngInt(ExtractZlib)},
{Extension: ".zst", Extract: ChngInt(ExtractZstandard)},
{Extension: ".zstd", Extract: ChngInt(ExtractZstandard)},
{Extension: ".zz", Extract: ChngInt(ExtractZlib)},
}
// ChngInt converts the smaller return interface into an ExtractInterface.
// Functions with multi-part archive files return four values. Other functions return only 3.
// This ChngInt function makes both interfaces compatible.
func ChngInt(smallFn func(*XFile) (int64, []string, error)) Interface {
return func(xFile *XFile) (int64, []string, []string, error) {
size, files, err := smallFn(xFile)
return size, files, []string{xFile.FilePath}, err
}
}
// SupportedExtensions returns a slice of file extensions this library recognizes.
func SupportedExtensions() []string {
exts := make([]string, len(extension2function))
for idx, ext := range extension2function {
exts[idx] = ext.Extension
}
return exts
}
// XFile defines the data needed to extract an archive.
type XFile struct {
// Path to archive being extracted.
FilePath string
// Folder to extract archive into.
OutputDir string
// Write files with this mode.
FileMode os.FileMode
// Write folders with this mode.
DirMode os.FileMode
// (RAR/7z) Archive password. Blank for none. Gets prepended to Passwords, below.
Password string
// (RAR/7z) Archive passwords (to try multiple).
Passwords []string
}
// Filter is the input to find compressed files.
type Filter struct {
// This is the path to search in for archives.
Path string
// Any files with this suffix are ignored. ie. ".7z" or ".iso"
// Use the AllExcept func to create an inclusion list instead.
ExcludeSuffix Exclude
// Count of folder depth allowed when finding archives. 1 = root
MaxDepth int
// Only find archives this many child-folders deep. 0 and 1 are equal.
MinDepth int
}
// Exclude represents an exclusion list.
type Exclude []string
// GetFileList returns all the files in a path.
// This is non-recursive and only returns files _in_ the base path provided.
// This is a helper method and only exposed for convenience. You do not have to call this.
func (x *Xtractr) GetFileList(path string) ([]string, error) {
if stat, err := os.Stat(path); err == nil && !stat.IsDir() {
return []string{path}, nil
}
fileList, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("reading path %s: %w", path, err)
}
files := make([]string, len(fileList))
for idx, file := range fileList {
files[idx] = filepath.Join(path, file.Name())
}
return files, nil
}
// Difference returns all the strings that are in slice2 but not in slice1.
// Used to find new files in a file list from a path. ie. those we extracted.
// This is a helper method and only exposed for convenience. You do not have to call this.
func Difference(slice1 []string, slice2 []string) []string {
diff := []string{}
for _, s2p := range slice2 {
var found bool
for _, s1 := range slice1 {
if s1 == s2p {
found = true
break
}
}
if !found { // String not found, so it's a new string, add it to the diff.
diff = append(diff, s2p)
}
}
return diff
}
// Has returns true if the test has an excluded suffix.
func (e Exclude) Has(test string) bool {
for _, exclude := range e {
if strings.HasSuffix(test, strings.ToLower(exclude)) {
return true
}
}
return false
}
// FindCompressedFiles returns all the compressed archive files in a path. This attempts to grab
// only the first file in a multi-part rar or 7zip archive. Sometimes there are multiple archives,
// so if the rar archive does not have "part" followed by a number in the name, then it will be
// considered an independent archive. Some packagers seem to use different naming schemes,
// so this may need to be updated as time progresses. Use the input to Filter to adjust the output.
func FindCompressedFiles(filter Filter) ArchiveList {
return findCompressedFiles(filter.Path, &filter, 0)
}
func findCompressedFiles(path string, filter *Filter, depth int) ArchiveList {
if filter.MaxDepth > 0 && filter.MaxDepth < depth {
return nil
}
dir, err := os.Open(path)
if err != nil {
return nil
}
defer dir.Close()
if info, err := dir.Stat(); err != nil {
return nil // unreadable folder?
} else if !info.IsDir() && IsArchiveFile(path) {
return ArchiveList{path: {path}} // passed in an archive file; send it back out.
}
fileList, err := dir.Readdir(-1)
if err != nil {
return nil
}
return getCompressedFiles(path, filter, fileList, depth)
}
// IsArchiveFile returns true if the provided path has an archive file extension.
// This is not picky about extensions, and will match any that are known as an archive.
// In the future, it may use file magic to figure out if the file is an archive without
// relying on the extension.
func IsArchiveFile(path string) bool {
path = strings.ToLower(path)
for _, ext := range extension2function {
if strings.HasSuffix(path, ext.Extension) {
return true
}
}
return false
}
// CheckR00ForRarFile scans the file list to determine if a .rar file with the same name as .r00 exists.
// Returns true if the r00 files has an accompanying rar file in the fileList.
func CheckR00ForRarFile(fileList []os.FileInfo, r00file string) bool {
findFile := strings.TrimSuffix(strings.TrimSuffix(r00file, ".R00"), ".r00") + ".rar"
for _, file := range fileList {
if strings.EqualFold(file.Name(), findFile) {
return true
}
}
return false
}
// getCompressedFiles checks file suffixes to find archives to decompress.
// This pays special attention to the widely accepted variance of rar formats.
func getCompressedFiles(path string, filter *Filter, fileList []os.FileInfo, depth int) ArchiveList { //nolint:cyclop
files := ArchiveList{}
for _, file := range fileList {
switch lowerName := strings.ToLower(file.Name()); {
case !file.IsDir() &&
(filter.ExcludeSuffix.Has(lowerName) || depth < filter.MinDepth):
continue // file suffix is excluded or we are not deep enough.
case lowerName == "" || lowerName[0] == '.':
continue // ignore empty names and dot files/folders.
case file.IsDir(): // Recurse.
for k, v := range findCompressedFiles(filepath.Join(path, file.Name()), filter, depth+1) {
files[k] = v
}
case strings.HasSuffix(lowerName, ".rar"):
hasParts := regexp.MustCompile(`.*\.part[0-9]+\.rar$`)
partOne := regexp.MustCompile(`.*\.part0*1\.rar$`)
// Some archives are named poorly. Only return part01 or part001, not all.
if !hasParts.MatchString(lowerName) || partOne.MatchString(lowerName) {
files[path] = append(files[path], filepath.Join(path, file.Name()))
}
case strings.HasSuffix(lowerName, ".r00") && !CheckR00ForRarFile(fileList, lowerName):
// Accept .r00 as the first archive file if no .rar files are present in the path.
files[path] = append(files[path], filepath.Join(path, file.Name()))
case !strings.HasSuffix(lowerName, ".r00") && IsArchiveFile(lowerName):
files[path] = append(files[path], filepath.Join(path, file.Name()))
}
}
return files
}
// Extract calls the correct procedure for the type of file being extracted.
// Returns size of extracted data, list of extracted files, and/or error.
func (x *XFile) Extract() (int64, []string, []string, error) {
return ExtractFile(x)
}
// ExtractFile calls the correct procedure for the type of file being extracted.
// Returns size of extracted data, list of extracted files, list of archives processed, and/or error.
func ExtractFile(xFile *XFile) (int64, []string, []string, error) {
sName := strings.ToLower(xFile.FilePath)
for _, ext := range extension2function {
if strings.HasSuffix(sName, ext.Extension) {
return ext.Extract(xFile)
}
}
return 0, nil, nil, fmt.Errorf("%w: %s", ErrUnknownArchiveType, xFile.FilePath)
}
// MoveFiles relocates files then removes the folder they were in.
// Returns the new file paths.
// This is a helper method and only exposed for convenience. You do not have to call this.
func (x *Xtractr) MoveFiles(fromPath string, toPath string, overwrite bool) ([]string, error) { //nolint:cyclop
var (
newFiles = []string{}
keepErr error
)
files, err := x.GetFileList(fromPath)
if err != nil {
return nil, err
}
// If the "to path" is an existing archive file, remove the suffix to make a directory.
if _, err := os.Stat(toPath); err == nil && IsArchiveFile(toPath) {
toPath = strings.TrimSuffix(toPath, filepath.Ext(toPath))
}
if err := os.MkdirAll(toPath, x.config.DirMode); err != nil {
return nil, fmt.Errorf("os.MkdirAll: %w", err)
}
for _, file := range files {
var (
newFile = filepath.Join(toPath, filepath.Base(file))
_, err = os.Stat(newFile)
exists = !os.IsNotExist(err)
)
if exists && !overwrite {
x.config.Printf("Error: Renaming Temp File: %v to %v: (refusing to overwrite existing file)", file, newFile)
// keep trying.
continue
}
switch err = x.Rename(file, newFile); {
case err != nil:
keepErr = err
x.config.Printf("Error: Renaming Temp File: %v to %v: %v", file, newFile, err)
case exists:
newFiles = append(newFiles, newFile)
x.config.Debugf("Renamed Temp File: %v -> %v (overwrote existing file)", file, newFile)
default:
newFiles = append(newFiles, newFile)
x.config.Debugf("Renamed Temp File: %v -> %v", file, newFile)
}
}
x.DeleteFiles(fromPath)
// Since this is the last step, we tried to rename all the files, bubble the
// os.Rename error up, so it gets flagged as failed. It may have worked, but
// it should get attention.
return newFiles, keepErr
}
// DeleteFiles obliterates things and logs. Use with caution.
func (x *Xtractr) DeleteFiles(files ...string) {
for _, file := range files {
if err := os.RemoveAll(file); err != nil {
x.config.Printf("Error: Deleting %v: %v", file, err)
continue
}
x.config.Printf("Deleted (recursively): %s", file)
}
}
// writeFile writes a file from an io reader, making sure all parent directories exist.
func writeFile(fpath string, fdata io.Reader, fMode, dMode os.FileMode) (int64, error) {
if err := os.MkdirAll(filepath.Dir(fpath), dMode); err != nil {
return 0, fmt.Errorf("os.MkdirAll: %w", err)
}
fout, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fMode)
if err != nil {
return 0, fmt.Errorf("os.OpenFile: %w", err)
}
defer fout.Close()
s, err := io.Copy(fout, fdata)
if err != nil {
return s, fmt.Errorf("copying io: %w", err)
}
return s, nil
}
// Rename is an attempt to deal with "invalid cross link device" on weird file systems.
func (x *Xtractr) Rename(oldpath, newpath string) error {
if err := os.Rename(oldpath, newpath); err == nil {
return nil
}
/* Rename failed, try copy. */
oldFile, err := os.Open(oldpath) // do not forget to close this!
if err != nil {
return fmt.Errorf("os.Open(): %w", err)
}
newFile, err := os.OpenFile(newpath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, x.config.FileMode)
if err != nil {
oldFile.Close()
return fmt.Errorf("os.OpenFile(): %w", err)
}
defer newFile.Close()
_, err = io.Copy(newFile, oldFile)
oldFile.Close()
if err != nil {
return fmt.Errorf("io.Copy(): %w", err)
}
// The copy was successful, so now delete the original file
_ = os.Remove(oldpath)
return nil
}
// clean returns an absolute path for a file inside the OutputDir.
// If trim length is > 0, then the suffixes are trimmed, and filepath removed.
func (x *XFile) clean(filePath string, trim ...string) string {
if len(trim) != 0 {
filePath = filepath.Base(filePath)
for _, suffix := range trim {
filePath = strings.TrimSuffix(filePath, suffix)
}
}
return filepath.Clean(filepath.Join(x.OutputDir, filePath))
}
// AllExcept can be used as an input to ExcludeSuffix in a Filter.
// Returns a list of supported extensions minus the ones provided.
// Extensions for like-types such as .rar and .r00 need to both be provided.
// Same for .tar.gz and .tgz variants.
func AllExcept(onlyThese []string) Exclude {
output := SupportedExtensions()
for _, str := range onlyThese {
idx := 0
for _, ext := range output {
if !strings.EqualFold(ext, str) {
output[idx] = ext
idx++
}
}
output = output[:idx]
}
return output
}
// Count returns the number of unique archives in the archive list.
func (a ArchiveList) Count() int {
var count int
for _, files := range a {
count += len(files)
}
return count
}
// Random returns a random file listing from the archive list.
// If the list only contains one directory, then that is the one returned.
// If the archive list is empty or nil, returns nil.
func (a ArchiveList) Random() []string {
for _, files := range a {
return files
}
return nil
}