forked from probonopd/go-appimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archivereader.go
351 lines (330 loc) · 8.69 KB
/
archivereader.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
package goappimage
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"sort"
"strings"
"github.com/CalebQ42/squashfs"
)
type archiveReader interface {
//FileReader returns an io.ReadCloser for a file at the given path.
//If the given path is a symlink, it will return the link's reader.
//If the symlink is to an absolute path, an error is returned.
//Returns an error if the given path is a directory.
FileReader(path string) (io.ReadCloser, error)
//IsDir returns if the given path points to a directory.
IsDir(path string) bool
//ListFiles returns a list of filenames at the given directory.
//Returns nil if the given path is a symlink, file, or isn't contained.
ListFiles(path string) []string
//ExtractTo extracts the file/folder at path to the folder at destination.
ExtractTo(path, destination string, resolveSymlinks bool) error
}
func (ai *AppImage) populateReader(allowFallback, forceFallback bool) (err error) {
if ai.imageType == 1 {
ai.reader, err = newType1Reader(ai.Path)
return err
} else if ai.imageType == 2 {
ai.reader, err = newType2Reader(ai)
return err
}
return errors.New("invalid AppImage type")
}
//TODO: Implement command based fallback here.
type type2Reader struct {
rdr *squashfs.Reader
}
func newType2Reader(ai *AppImage) (*type2Reader, error) {
aiFil, err := os.Open(ai.Path)
if err != nil {
return nil, err
}
stat, _ := aiFil.Stat()
aiRdr := io.NewSectionReader(aiFil, ai.offset, stat.Size()-ai.offset)
squashRdr, err := squashfs.NewSquashfsReader(aiRdr)
if err != nil {
return nil, err
}
return &type2Reader{
rdr: squashRdr,
}, nil
}
// type anonymousCloser struct {
// close func() error
// }
// func (a anonymousCloser) Close() error {
// return a.close()
// }
func (r *type2Reader) FileReader(filepath string) (io.ReadCloser, error) {
//TODO: command fallback
fsFil, err := r.rdr.Open(filepath)
if err != nil {
return nil, err
}
fil := fsFil.(*squashfs.File)
for fil.IsSymlink() {
fil = fil.GetSymlinkFile()
if fil == nil {
return nil, errors.New("Can't resolve symlink at: " + filepath)
}
}
if fil.IsDir() {
return nil, errors.New("Path is a directory: " + filepath)
}
return fil, nil
}
func (r *type2Reader) IsDir(filepath string) bool {
fsFil, err := r.rdr.Open(filepath)
if err != nil {
return false
}
fil := fsFil.(*squashfs.File)
if fil.IsSymlink() {
fil = fil.GetSymlinkFile()
if fil == nil {
return false
}
}
return fil.IsDir()
}
func (r *type2Reader) ListFiles(path string) []string {
fsFil, err := r.rdr.Open(path)
if err != nil {
return nil
}
fil := fsFil.(*squashfs.File)
if fil.IsSymlink() {
fil = fil.GetSymlinkFile()
if fil == nil {
return nil
}
}
if !fil.IsDir() {
return nil
}
children, err := fil.ReadDir(0)
if err != nil {
return nil
}
out := make([]string, len(children))
for _, child := range children {
out = append(out, child.Name())
}
return out
}
func (r *type2Reader) ExtractTo(filepath, destination string, resolveSymlinks bool) error {
fsFil, err := r.rdr.Open(filepath)
if err != nil {
return err
}
options := squashfs.DefaultOptions()
options.DereferenceSymlink = true
err = fsFil.(*squashfs.File).ExtractWithOptions(destination, options)
return err
}
type type1Reader struct {
structure map[string][]string //[folder]File
path string
folders []string
}
func newType1Reader(filepath string) (*type1Reader, error) {
cmd := exec.Command("bsdtar", "-f", filepath, "-t")
wrt, err := runCommand(cmd)
if err != nil {
return nil, err
}
containedFiles := strings.Split(wrt.String(), "\n")
var rdr type1Reader
rdr.path = filepath
rdr.structure = make(map[string][]string)
for _, contained := range containedFiles {
contained = path.Clean(contained)
if contained == "" || contained == "." {
continue
}
fileName := path.Base(contained)
dir := path.Dir(contained)
dir = path.Clean(dir)
if !strings.Contains(contained, "/") {
dir = "/"
}
if rdr.structure[dir] == nil && dir != "/" {
rdr.folders = append(rdr.folders, dir)
}
rdr.structure[dir] = append(rdr.structure[dir], fileName)
}
sort.Strings(rdr.folders)
for folds := range rdr.structure {
sort.Strings(rdr.structure[folds])
}
return &rdr, nil
}
//makes sure that the path is nice and only points to ONE file, which is needed if there are wildcards.
//If you were to search for *.desktop, you will get both blender.desktop AND /usr/bin/blender.desktop.
//This could cause issues, especially for FileReader
//
//Probably a bit spagetti and can be cleaned up. Maybe add a rawPaths variable to type1reader to make
//it easier to find a match with wildcards.
func (r *type1Reader) cleanPath(filepath string) (string, error) {
filepath = strings.TrimPrefix(filepath, "/")
filepath = path.Clean(filepath)
if filepath == "" {
return "", nil
}
filepathDir := path.Dir(filepath)
if filepathDir != "." {
for _, dir := range r.folders {
match, _ := path.Match(filepathDir, dir)
if match {
filepathDir = dir
break
}
}
} else {
filepathDir = "/"
}
if filepathDir == "" {
return "", errors.New("file not found in the archive")
}
filepathName := path.Base(filepath)
for _, fil := range r.structure[filepathDir] {
match, _ := path.Match(filepathName, fil)
if match {
filepathName = fil
break
}
}
if filepathName == "" {
return "", errors.New("file not found in the archive")
}
if filepathDir == "/" {
filepath = filepathName
} else {
filepath = filepathDir + "/" + filepathName
}
return filepath, nil
}
func (r *type1Reader) FileReader(filepath string) (io.ReadCloser, error) {
//TODO: check size of file and if it's large, extract to a temp directory, read that, and delete it on close.
//This would make sure a huge file isn't completely held in memory via the byte buffer.
filepath, err := r.cleanPath(filepath)
if err != nil {
return nil, err
}
cmd := exec.Command("bsdtar", "-f", r.path, "-xO", filepath)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return nil, err
}
return ioutil.NopCloser(&out), nil
}
func (r *type1Reader) IsDir(filepath string) bool {
filepath, err := r.cleanPath(filepath)
if err != nil {
return false
}
return r.structure[filepath] != nil
}
func (r *type1Reader) SymlinkPath(filepath string) string {
filepath, err := r.cleanPath(filepath)
if err != nil {
return filepath
}
cmd := exec.Command("bsdtar", "-f", r.path, "-tv", filepath)
wrt, err := runCommand(cmd)
if err != nil {
return filepath
}
output := strings.TrimSuffix(wrt.String(), "\n")
output = strings.Split(output, "\n")[0] //Make sure we are only getting the first value that matches
if index := strings.Index(output, "->"); index != -1 { //signifies symlink
return output[index+3:]
}
return filepath
}
func (r *type1Reader) SymlinkPathRecursive(filepath string) string {
filepath, err := r.cleanPath(filepath)
if err != nil {
return filepath
}
cmd := exec.Command("bsdtar", "-f", r.path, "-tv", filepath)
wrt, err := runCommand(cmd)
if err != nil {
return filepath
}
output := strings.TrimSuffix(wrt.String(), "\n")
if index := strings.Index(output, "->"); index != -1 { //signifies symlink
symlinkedFile := output[index+3:]
if strings.HasPrefix(symlinkedFile, "/") {
return filepath //we can't help with absolute symlinks...
}
tmp := r.SymlinkPathRecursive(path.Dir(filepath) + "/" + symlinkedFile)
if tmp != path.Dir(filepath)+"/"+symlinkedFile {
return tmp
}
}
return filepath
}
func (r *type1Reader) Contains(filepath string) bool {
filepath, err := r.cleanPath(filepath)
if err != nil {
return false
}
dir := path.Dir(filepath)
name := path.Base(filepath)
if dir == "" {
dir = "/"
}
return sort.SearchStrings(r.structure[dir], name) != len(r.structure[dir])
}
func (r *type1Reader) ListFiles(filepath string) []string {
filepath, err := r.cleanPath(filepath)
if err != nil {
return nil
}
if filepath == "" {
return r.structure["/"]
}
if r.IsDir(filepath) {
return r.structure[filepath]
}
return nil
}
func (r *type1Reader) ExtractTo(filepath, destination string, resolveSymlinks bool) error {
filepath, err := r.cleanPath(filepath)
if err != nil {
return err
}
err = os.MkdirAll(destination, os.ModePerm)
if err != nil {
return err
}
name := path.Base(filepath)
destination = strings.TrimSuffix(destination, "/")
tmpDir := destination + "/" + ".tmp"
err = os.Mkdir(tmpDir, os.ModePerm)
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
if resolveSymlinks {
filepath = r.SymlinkPathRecursive(filepath)
}
cmd := exec.Command("bsdtar", "-C", tmpDir, "-f", r.path, "-x", filepath)
_, err = runCommand(cmd)
if err != nil {
return err
}
err = os.Rename(tmpDir+"/"+filepath, destination+"/"+name)
if err != nil {
return err
}
return err
}