/
extractor.go
73 lines (66 loc) · 1.5 KB
/
extractor.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
// FIXME: golangci-lint
// nolint:govet,revive
package files
import (
"archive/tar"
"io"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
)
// Extractor defines methods to extract files to path
type Extractor interface {
Extract(rc io.ReadCloser, dst string) error
}
// NewExtractor returns the main extractor used by EdgeAPI
func NewExtractor(log log.FieldLogger) Extractor {
return &TARFileExtractor{log: log}
}
// TARFileExtractor implements a method to extract TAR files into a path
type TARFileExtractor struct {
log log.FieldLogger
}
// Extract extracts file to destination path
func (f *TARFileExtractor) Extract(rc io.ReadCloser, dst string) error {
defer rc.Close()
tarReader := tar.NewReader(rc)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
path, err := sanitizePath(dst, header.Name)
if err != nil {
f.log.WithField("error", err.Error()).Error("Error sanitizing path")
}
info := header.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(path, info.Mode()); err != nil {
return err
}
continue
}
file, err := os.OpenFile(filepath.Clean(path), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
for {
_, err = io.CopyN(file, tarReader, 1024*1024)
if err != nil {
if err == io.EOF {
break
}
if err := file.Close(); err != nil {
return err
}
return err
}
}
if err := file.Close(); err != nil {
return err
}
}
return nil
}