-
Notifications
You must be signed in to change notification settings - Fork 12
/
rar.go
122 lines (99 loc) · 3.12 KB
/
rar.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
package xtractr
/* How to extract a RAR file. */
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/nwaples/rardecode"
)
func ExtractRAR(xFile *XFile) (int64, []string, []string, error) {
if len(xFile.Passwords) == 0 && xFile.Password == "" {
return extractRAR(xFile)
}
// Try all the passwords.
passwords := xFile.Passwords
if xFile.Password != "" { // If a single password is provided, try it first.
passwords = append([]string{xFile.Password}, xFile.Passwords...)
}
for idx, password := range passwords {
size, files, archives, err := extractRAR(&XFile{
FilePath: xFile.FilePath,
OutputDir: xFile.OutputDir,
FileMode: xFile.FileMode,
DirMode: xFile.DirMode,
Password: password,
})
if err == nil {
return size, files, archives, nil
}
// https://github.com/nwaples/rardecode/issues/28
if strings.Contains(err.Error(), "incorrect password") {
continue
}
return size, files, archives, fmt.Errorf("used password %d of %d: %w", idx+1, len(passwords), err)
}
// No password worked, try without a password.
return extractRAR(&XFile{
FilePath: xFile.FilePath,
OutputDir: xFile.OutputDir,
FileMode: xFile.FileMode,
DirMode: xFile.DirMode,
})
}
// ExtractRAR extracts a rar file. to a destination. This wraps github.com/nwaples/rardecode.
func extractRAR(xFile *XFile) (int64, []string, []string, error) {
rarReader, err := rardecode.OpenReader(xFile.FilePath, xFile.Password)
if err != nil {
return 0, nil, nil, fmt.Errorf("rardecode.OpenReader: %w", err)
}
defer rarReader.Close()
size, files, err := xFile.unrar(rarReader)
if err != nil {
lastFile := xFile.FilePath
if volumes := rarReader.Volumes(); len(volumes) > 0 {
lastFile = volumes[len(volumes)-1]
}
return size, files, rarReader.Volumes(), fmt.Errorf("%s: %w", lastFile, err)
}
return size, files, rarReader.Volumes(), nil
}
func (x *XFile) unrar(rarReader *rardecode.ReadCloser) (int64, []string, error) {
files := []string{}
size := int64(0)
for {
header, err := rarReader.Next()
switch {
case errors.Is(err, io.EOF):
return size, files, nil
case err != nil:
return size, files, fmt.Errorf("rarReader.Next: %w", err)
case header == nil:
return size, files, fmt.Errorf("%w: %s", ErrInvalidHead, x.FilePath)
}
wfile := x.clean(header.Name)
//nolint:gocritic // this 1-argument filepath.Join removes a ./ prefix should there be one.
if !strings.HasPrefix(wfile, filepath.Join(x.OutputDir)) {
// The file being written is trying to write outside of our base path. Malicious archive?
return size, files, fmt.Errorf("%s: %w: %s != %s (from: %s)",
x.FilePath, ErrInvalidPath, wfile, x.OutputDir, header.Name)
}
if header.IsDir {
if err = os.MkdirAll(wfile, x.DirMode); err != nil {
return size, files, fmt.Errorf("os.MkdirAll: %w", err)
}
continue
}
if err = os.MkdirAll(filepath.Dir(wfile), x.DirMode); err != nil {
return size, files, fmt.Errorf("os.MkdirAll: %w", err)
}
fSize, err := writeFile(wfile, rarReader, x.FileMode, x.DirMode)
if err != nil {
return size, files, err
}
files = append(files, wfile)
size += fSize
}
}