-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextract.go
418 lines (364 loc) · 10.5 KB
/
extract.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
package squashfs
import (
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"syscall"
"golang.org/x/sys/unix"
)
// DefaultFilePerm - files will be created with this perm by default.
const DefaultFilePerm = 0644
// DefaultDirPerm - Directories created with this perm
const DefaultDirPerm = 0755
// OpenDirPerm - Perm that current uid can write to
const OpenDirPerm = 0777
type Extractor struct {
Dir string
SquashFs SquashFs
Path string
WhiteOuts bool
Owners bool
Perms bool
Devs bool
Sockets bool
Logger Logger
Ops FsOps
cleanups []func() error
}
type FsOps interface {
Chmod(string, os.FileMode) error
Chown(string, int, int) error
Mknod(string, FileInfo) error
}
type GoFsOps struct{}
func (g GoFsOps) Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}
func (g GoFsOps) Chown(name string, uid, gid int) error {
return os.Lchown(name, uid, gid)
}
func (g GoFsOps) Mknod(path string, info FileInfo) error {
stat := info.Sys().(syscall.Stat_t)
return syscall.Mknod(path, DefaultFilePerm, int(stat.Rdev))
}
// Golang's os.Chown, os.Chmod, syscall.Mknod, make syscalls
// which are missed by fakeroot's LD_PRELOAD of those filesystem operations.
// In order to work with fakeroot, we execute the programs
// 'chown', 'chmod', 'mknod' which are expected to work with fakeroot.
type FakerootOps struct{}
func (f FakerootOps) Chmod(name string, mode os.FileMode) error {
return exec.Command("chmod", fmt.Sprintf("%04o", mode.Perm()), name).Run()
}
func (f FakerootOps) Chown(name string, uid, gid int) error {
return exec.Command("chown", "--no-dereference", fmt.Sprintf("%d:%d", uid, gid), name).Run()
}
func (f FakerootOps) Mknod(path string, info FileInfo) error {
// dtype is b (block), c (char), p (fifo)
var dtype string
stat := info.Sys().(syscall.Stat_t)
majMin := []string{fmt.Sprintf("%d", stat.Rdev/256), fmt.Sprintf("%d", stat.Rdev%256)}
if info.FMode&os.ModeCharDevice != 0 {
dtype = "c"
} else if info.FMode&os.ModeDevice != 0 {
dtype = "b"
} else if info.FMode&os.ModeNamedPipe != 0 {
dtype = "p"
majMin = []string{}
} else {
return fmt.Errorf("%s is not a char, block or fifo", path)
}
cmd := append(
[]string{"mknod", fmt.Sprintf("--mode=%o", info.FMode.Perm()),
path, dtype}, majMin...)
if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil {
if PathExists(path) {
return os.ErrExist
}
return err
}
return nil
}
// Extract - extract the
func (e *Extractor) Extract() error {
var walkErr, cleanErr error
if e.Ops == nil {
if isFakeroot() {
e.Ops = FakerootOps{}
} else {
e.Ops = GoFsOps{}
}
}
e.Logger.Debug("extractor: %#v", e)
walkErr = e.SquashFs.Walk(e.Path, e.extract)
for _, c := range e.cleanups {
if err := c(); err != nil {
e.Logger.Info("Cleanup failed: %s", err)
if cleanErr != nil {
cleanErr = err
}
}
}
if walkErr != nil {
return walkErr
}
return cleanErr
}
func (e *Extractor) extract(path string, info FileInfo, perr error) error {
if perr != nil {
e.Logger.Info("extract called with %s info=%s and %s", path, info.Filename, perr)
return perr
}
if whiteOut := getWhiteOut(info); whiteOut != "" {
if !e.WhiteOuts {
e.Logger.Debug("not extracting white-out file %s", path)
return nil
}
return e.applyWhiteOut(path, whiteOut)
}
mode := info.FMode
var err error
if mode&os.ModeDir != 0 {
err = e.extractDir(path, info)
} else if mode&os.ModeSymlink != 0 {
err = e.extractSymlink(path, info)
} else if mode&os.ModeSocket != 0 {
if !e.Sockets {
e.Logger.Debug("skipping socket %s", path)
return nil
}
err = e.extractSocket(path, info)
} else if mode&os.ModeNamedPipe != 0 {
err = e.extractNamedPipe(path, info)
} else if mode&os.ModeDevice != 0 {
if !e.Devs {
e.Logger.Debug("skipping block device node %s", path)
return nil
}
err = e.extractBlockDevice(path, info)
} else if mode&os.ModeCharDevice != 0 {
if !e.Devs {
e.Logger.Debug("skipping char device node %s", path)
return nil
}
err = e.extractCharDevice(path, info)
} else if mode&os.ModeIrregular != 0 {
err = e.extractIrregular(path, info)
} else if mode.IsRegular() {
err = e.extractRegular(path, info)
} else {
return fmt.Errorf("could not determine file type of '%s'", path)
}
if err != nil {
return err
}
fpath := filepath.Join(e.Dir, path)
if e.Owners {
stat := info.Sys().(syscall.Stat_t)
e.Logger.Debug("chown(%s, %d, %d)", path, stat.Uid, stat.Gid)
if err := e.Ops.Chown(fpath, int(stat.Uid), int(stat.Gid)); err != nil {
e.Logger.Info("chown(%s, %d, %d) failed: %s", path, stat.Uid, stat.Gid, err)
return err
}
}
if e.Perms {
mode := info.Mode()
// you can't chmod a symlink.
if mode&os.ModeSymlink == 0 {
modrw := ""
// if a dir does not have owner RW perms, then extract it with RW and add a cleanup to set it back
if mode&os.ModeDir != 0 && mode.Perm()&0600 == 0 {
oldMode := mode
mode |= 0600
modrw = "(+rw)"
e.cleanups = append(e.cleanups, func() error {
e.Logger.Debug("fixing %s back to %04o", path, oldMode.Perm())
return e.Ops.Chmod(fpath, oldMode)
})
}
e.Logger.Debug("chmod(%s, %04o)%s", path, mode.Perm(), modrw)
if err := e.Ops.Chmod(fpath, mode); err != nil {
e.Logger.Info("chmod(%s, %04o) failed: %s", path, mode.Perm(), err)
return err
}
}
}
return nil
}
func (e *Extractor) extractSymlink(path string, info FileInfo) error {
e.Logger.Debug("symlink: %s", path)
targetPath := filepath.Join(e.Dir, path)
return e.doCreate(targetPath, info,
func() error {
return os.Symlink(info.SymlinkTarget, targetPath)
})
}
func (e *Extractor) extractNamedPipe(path string, info FileInfo) error {
e.Logger.Debug("mkfifo: %s", path)
targetPath := filepath.Join(e.Dir, path)
return e.doCreate(targetPath, info,
func() error {
return syscall.Mkfifo(targetPath, DefaultFilePerm)
},
)
}
func (e *Extractor) extractSocket(path string, info FileInfo) error {
e.Logger.Debug("socket: %s", path)
targetPath := filepath.Join(e.Dir, path)
return e.doCreate(targetPath, info,
func() error {
_, err := net.Listen("unix", targetPath)
return err
})
}
func (e *Extractor) extractBlockDevice(path string, info FileInfo) error {
e.Logger.Debug("bdev: %s", path)
targetPath := filepath.Join(e.Dir, path)
return e.doCreate(targetPath, info,
func() error { return e.Ops.Mknod(targetPath, info) })
}
func (e *Extractor) extractCharDevice(path string, info FileInfo) error {
e.Logger.Debug("cdev: %s", path)
targetPath := filepath.Join(e.Dir, path)
return e.doCreate(targetPath, info,
func() error { return e.Ops.Mknod(targetPath, info) })
}
func (e *Extractor) extractRegular(path string, info FileInfo) error {
var finalError, cleanupError error
targetPath := filepath.Join(e.Dir, path)
cleanup, err := e.prepWrite(targetPath, info)
if err == nil {
if writeFp, err := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY, DefaultFilePerm); err == nil {
defer writeFp.Close()
if written, err := io.Copy(writeFp, info.File); err == nil {
if written != info.FSize {
finalError = fmt.Errorf("wrote %d bytes to %s. expected %d from %s",
written, targetPath, info.FSize, path)
}
}
} else {
finalError = err
}
} else {
finalError = err
}
cleanupError = cleanup()
if finalError != nil {
// there was an error before cleanup, so log the cleanup error, return the real error.
e.Logger.Info("prepWrite cleanup for %s failed: %s", targetPath, cleanupError)
} else {
finalError = cleanupError
}
return finalError
}
func (e *Extractor) extractIrregular(path string, info FileInfo) error {
return fmt.Errorf("cannot extract Irregular file %s", path)
}
func (e *Extractor) extractDir(path string, info FileInfo) error {
e.Logger.Debug("mkdir %s", path)
// we do not use doCreate here because we do not want to remove if exist.
fpath := filepath.Join(e.Dir, path)
if err := os.Mkdir(fpath, DefaultDirPerm); os.IsExist(err) {
return nil
} else if err != nil {
return err
}
return nil
}
func (e *Extractor) applyWhiteOut(path string, whiteOut string) error {
fp := filepath.Join(e.Dir, path)
if PathExists(fp) {
e.Logger.Debug("applying white-out '%s' by removing '%s'", whiteOut, path)
return os.RemoveAll(fp)
}
return nil
}
// prepWrite - prepare to write to path
// if dirname(path) is not a directory - return error
func (e *Extractor) prepWrite(path string, finfo FileInfo) (func() error, error) {
cleanup := func() error { return nil }
dir := filepath.Dir(path)
dirFinfo, err := os.Stat(dir)
if err != nil {
// could not stat parent directory.
return cleanup, err
}
if !dirFinfo.IsDir() {
return cleanup, fmt.Errorf("dirname(%s) = %s : not a directory", path, dir)
}
if unix.Access(dir, unix.W_OK) != nil {
oldPerms := dirFinfo.Mode()
setBack := func() error {
return e.Ops.Chmod(dir, oldPerms)
}
if err := e.Ops.Chmod(dir, OpenDirPerm); err != nil {
return cleanup, err
}
if unix.Access(dir, unix.W_OK) != nil {
if err := setBack(); err != nil {
return cleanup, fmt.Errorf("cannot make %s writable, failed setting back", dir)
}
return cleanup, fmt.Errorf("cannot make %s writable", dir)
}
cleanup = setBack
}
pathFinfo, err := os.Lstat(path)
if os.IsNotExist(err) {
// nothing to do
return cleanup, nil
} else if err != nil {
return cleanup, err
}
// path exists, so get rid of it.
if pathFinfo.IsDir() {
if finfo.IsDir() {
// path is already a dir, leave it.
// caller has to deal with mkdir failing with os.IsExist()
return cleanup, nil
}
// path is a dir, but we want something else there so purge.
if err := os.RemoveAll(path); err != nil {
return cleanup, err
}
return cleanup, nil
}
// path exists, but is not a dir.
return cleanup, os.Remove(path)
}
// doCreate - prep writing of fInfo to path, and then call creator.
func (e *Extractor) doCreate(path string, fInfo FileInfo, creator func() error) error {
var createError, cleanupError error
cleanup, err := e.prepWrite(path, fInfo)
if err == nil {
createError = creator()
}
cleanupError = cleanup()
if cleanupError != nil {
if createError == nil {
return cleanupError
}
}
return createError
}
func isFakeroot() bool {
return os.Getenv("FAKEROOTKEY") != ""
}
func getWhiteOut(info FileInfo) string {
// squashfs / overlayfs is a character device major/minor 0/0 with same name.
if info.FMode&os.ModeCharDevice != 0 {
stat := info.Sys().(syscall.Stat_t)
if stat.Rdev == 0 {
return info.Filename
}
}
return ""
}
func PathExists(d string) bool {
_, err := os.Stat(d)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}