forked from mholt/archiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archiver.go
119 lines (102 loc) · 2.95 KB
/
archiver.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
package archiver
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
// Archiver represent a archive format
type Archiver interface {
// Match checks supported files
Match(filename string) bool
// Make makes an archive file on disk.
Make(destination string, sources []string) error
// Open extracts an archive file on disk.
Open(source, destination string) error
// Write writes an archive to a Writer.
Write(output io.Writer, sources []string) error
// Read reads an archive from a Reader.
Read(input io.Reader, destination string) error
}
// SupportedFormats contains all supported archive formats
var SupportedFormats = map[string]Archiver{}
// RegisterFormat adds a supported archive format
func RegisterFormat(name string, format Archiver) {
if _, ok := SupportedFormats[name]; ok {
log.Printf("Format %s already exists, skip!\n", name)
return
}
SupportedFormats[name] = format
}
// MatchingFormat returns the first archive format that matches
// the given file, or nil if there is no match
func MatchingFormat(fpath string) Archiver {
for _, fmt := range SupportedFormats {
if fmt.Match(fpath) {
return fmt
}
}
return nil
}
func writeNewFile(fpath string, in io.Reader, fm os.FileMode) error {
err := os.MkdirAll(filepath.Dir(fpath), 0755)
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}
out, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("%s: creating new file: %v", fpath, err)
}
defer out.Close()
err = out.Chmod(fm)
if err != nil && runtime.GOOS != "windows" {
return fmt.Errorf("%s: changing file mode: %v", fpath, err)
}
_, err = io.Copy(out, in)
if err != nil {
return fmt.Errorf("%s: writing file: %v", fpath, err)
}
return nil
}
func writeNewSymbolicLink(fpath string, target string) error {
err := os.MkdirAll(filepath.Dir(fpath), 0755)
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}
err = os.Symlink(target, fpath)
if err != nil {
return fmt.Errorf("%s: making symbolic link for: %v", fpath, err)
}
return nil
}
func writeNewHardLink(fpath string, target string) error {
err := os.MkdirAll(filepath.Dir(fpath), 0755)
if err != nil {
return fmt.Errorf("%s: making directory for file: %v", fpath, err)
}
err = os.Link(target, fpath)
if err != nil {
return fmt.Errorf("%s: making hard link for: %v", fpath, err)
}
return nil
}
func mkdir(dirPath string) error {
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return fmt.Errorf("%s: making directory: %v", dirPath, err)
}
return nil
}
func sanitizeExtractPath(filePath string, destination string) error {
// to avoid zip slip (writing outside of the destination), we resolve
// the target path, and make sure it's nested in the intended
// destination, or bail otherwise.
destpath := filepath.Join(destination, filePath)
if !strings.HasPrefix(destpath, destination) {
return fmt.Errorf("%s: illegal file path", filePath)
}
return nil
}