-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathiso.go
More file actions
183 lines (141 loc) · 4.4 KB
/
Copy pathiso.go
File metadata and controls
183 lines (141 loc) · 4.4 KB
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
package xtractr
import (
"fmt"
"os"
"path/filepath"
"github.com/Unpackerr/iso9660"
)
// ExtractISO writes an ISO's contents to disk.
// It tries UDF first (which preserves full filenames), then falls back
// to ISO9660 (with Joliet support) if UDF parsing fails.
func ExtractISO(xFile *XFile) (size uint64, filesList []string, err error) {
openISO, err := os.Open(xFile.FilePath) // os.Open on purpose.
if err != nil {
return 0, nil, fmt.Errorf("os.Open: %w", err)
}
defer openISO.Close()
// Try UDF first — it preserves full-length filenames.
size, filesList, udfErr := extractUDF(xFile, openISO)
if udfErr == nil {
xFile.Debugf("Extracted %s via UDF path", xFile.FilePath)
return size, filesList, nil
}
if IsLimitError(udfErr) {
if xFile.prog != nil {
xFile.prog.done()
}
return size, filesList, udfErr
}
xFile.Debugf("UDF extraction failed for %s, falling back to ISO9660: %v", xFile.FilePath, udfErr)
return extractISO9660(xFile, openISO)
}
func extractISO9660(xFile *XFile, openISO *os.File) (uint64, []string, error) {
image, isoErr := iso9660.OpenImage(openISO)
if isoErr != nil {
if xFile.prog != nil {
xFile.prog.done()
}
return 0, nil, fmt.Errorf("failed to open iso image: %s: %w", xFile.FilePath, isoErr)
}
tracker, headerErr := xFile.archiveProgress(getUncompressedIsoSize(image))
defer tracker.done()
if headerErr != nil {
return 0, nil, headerErr
}
iso, err := iso9660.OpenImage(xFile.prog.readAter(openISO))
if err != nil {
return 0, nil, fmt.Errorf("failed to open iso image: %s: %w", xFile.FilePath, err)
}
root, err := iso.RootDir()
if err != nil {
return 0, nil, fmt.Errorf("failed to open iso root: %s: %w", xFile.FilePath, err)
}
size, files, err := xFile.uniso(root, "")
if err != nil {
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err)
}
return size, files, nil
}
//nolint:unparam // so we can pass it in.
func getUncompressedIsoSize(image *iso9660.Image) (total, _ uint64, count int) {
if image == nil {
return total, 0, count
}
var loop func(isoFile *iso9660.File)
loop = func(isoFile *iso9660.File) {
children, err := isoFile.GetChildren()
if err != nil {
return
}
for _, child := range children {
count++
if child.IsDir() {
loop(child)
continue
}
total += uint64(child.Size())
}
}
root, err := image.RootDir()
if err != nil {
return total, 0, count
}
loop(root)
return total, 0, count
}
func (x *XFile) uniso(isoFile *iso9660.File, parent string) (uint64, []string, error) {
itemName := filepath.Join(parent, isoFile.Name())
if isoFile.Name() == string([]byte{0}) { // root directory - extract to output dir directly.
itemName = ""
}
if !isoFile.IsDir() { // it's a file
return x.unisofile(isoFile, itemName)
}
if itemName != "" {
dirPath := x.clean(itemName)
if !x.pathWithinOutput(dirPath) {
// The directory is trying to land outside of our base path. Malicious ISO?
return 0, nil, fmt.Errorf("%s: %w: %s (from: %s)",
x.FilePath, ErrInvalidPath, dirPath, isoFile.Name())
}
err := x.mkDir(dirPath, isoFile.Mode(), isoFile.ModTime())
if err != nil {
return 0, nil, fmt.Errorf("making iso directory %s: %w", isoFile.Name(), err)
}
}
children, err := isoFile.GetChildren()
if err != nil {
return 0, nil, fmt.Errorf("getting children for %s: %w", isoFile.Name(), err)
}
files := []string{}
size := uint64(0)
for _, child := range children {
childSize, childFiles, err := x.uniso(child, itemName)
if err != nil {
return size + childSize, files, err
}
size += childSize
files = append(files, childFiles...)
}
files, err = x.cleanup(files)
return size, files, err
}
func (x *XFile) unisofile(isoFile *iso9660.File, wfile string) (uint64, []string, error) {
file := &file{
Path: x.clean(wfile),
Data: isoFile.Reader(),
FileMode: isoFile.Mode(),
DirMode: x.DirMode,
Mtime: isoFile.ModTime(),
}
if !x.pathWithinOutput(file.Path) {
// The file being written is trying to write outside of our base path. Malicious ISO?
return 0, nil, fmt.Errorf("%s: %w: %s != %s (from: %s)",
x.FilePath, ErrInvalidPath, file.Path, x.OutputDir, isoFile.Name())
}
x.Debugf("Writing archived file: %s (bytes: %d)", file.Path, isoFile.Size())
size, err := x.write(file)
x.Debugf("Wrote archived file: %s (%d bytes), total: %d files and %d bytes",
file.Path, size, x.prog.Files, int64(x.prog.Wrote))
return size, []string{file.Path}, err
}