-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.go
202 lines (169 loc) · 4.8 KB
/
tar.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
package compressor
import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
)
const tarBlockSize int = 512
type tarFormat struct{}
func (tarFormat) Match(filename string) bool {
return strings.HasSuffix(strings.ToLower(filename), ".tar") || istarFormat(filename)
}
// MakeBytes makes a buffer of bytes that is a tar file
func (tarFormat) MakeBytes(filePaths []string, writer io.Writer, skipperFn func(string) bool) error {
const tarPath = "/1111111122222222223333333"
tarWriter := tar.NewWriter(writer)
defer tarWriter.Close()
return tarball(filePaths, tarWriter, tarPath, skipperFn)
}
// Open untars source and puts the contents into destination.
func (tarFormat) OpenBytes(source io.Reader, destination string) error {
return untar(tar.NewReader(source), destination)
}
// untar un-tarballs the contents of tr into destination.
func untar(tr *tar.Reader, destination string) error {
for {
header, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
if err := untarFile(tr, header, destination); err != nil {
return err
}
}
return nil
}
// untarFile untars a single file from tr with header header into destination.
func untarFile(tr *tar.Reader, header *tar.Header, destination string) error {
switch header.Typeflag {
case tar.TypeDir:
return mkdir(filepath.Join(destination, header.Name))
case tar.TypeReg, tar.TypeRegA, tar.TypeChar, tar.TypeBlock, tar.TypeFifo:
return writeNewFile(filepath.Join(destination, header.Name), tr, header.FileInfo().Mode())
case tar.TypeSymlink:
return writeNewSymbolicLink(filepath.Join(destination, header.Name), header.Linkname)
case tar.TypeLink:
return writeNewHardLink(filepath.Join(destination, header.Name), filepath.Join(destination, header.Linkname))
default:
return fmt.Errorf("%s: unknown type flag: %c", header.Name, header.Typeflag)
}
}
// istarFormat checks the file has the tarFormat format header by reading its beginning
// block.
func istarFormat(tarPath string) bool {
f, err := os.Open(tarPath)
if err != nil {
return false
}
defer f.Close()
buf := make([]byte, tarBlockSize)
if _, err = io.ReadFull(f, buf); err != nil {
return false
}
return hasTarHeader(buf)
}
func hasTarHeader(buf []byte) bool {
if len(buf) < tarBlockSize {
return false
}
b := buf[148:156]
b = bytes.Trim(b, " \x00") // clean up all spaces and null bytes
if len(b) == 0 {
return false // unknown format
}
hdrSum, err := strconv.ParseUint(string(b), 8, 64)
if err != nil {
return false
}
// According to the go official archive/tar, Sun tar uses signed byte
// values so this calcs both signed and unsigned
var usum uint64
var sum int64
for i, c := range buf {
if 148 <= i && i < 156 {
c = ' ' // checksum field itself is counted as branks
}
usum += uint64(uint8(c))
sum += int64(int8(c))
}
if hdrSum != usum && int64(hdrSum) != sum {
return false // invalid checksum
}
return true
}
// tarball writes all files listed in filePaths into tarWriter, which is
// writing into a file located at dest.
func tarball(filePaths []string, tarWriter *tar.Writer, dest string, skipperFn func(string) bool) error {
for _, fpath := range filePaths {
err := tarFile(tarWriter, fpath, dest, skipperFn)
if err != nil {
return err
}
}
return nil
}
// tarFile writes the file at source into tarWriter. It does so
// recursively for directories.
func tarFile(tarWriter *tar.Writer, source, dest string, skipperFn func(string) bool) error {
sourceInfo, err := os.Stat(source)
if err != nil {
return fmt.Errorf("%s: stat: %v", source, err)
}
var baseDir string
if sourceInfo.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking to %s: %v", path, err)
}
if skipperFn != nil {
if skipperFn(path) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
}
header, err := tar.FileInfoHeader(info, path)
if err != nil {
return fmt.Errorf("%s: making header: %v", path, err)
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if header.Name == dest {
// our new tar file is inside the directory being archived; skip it
return nil
}
if info.IsDir() {
header.Name += "/"
}
err = tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf("%s: writing header: %v", path, err)
}
if info.IsDir() {
return nil
}
if header.Typeflag == tar.TypeReg {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("%s: open: %v", path, err)
}
defer file.Close()
_, err = io.CopyN(tarWriter, file, info.Size())
if err != nil && err != io.EOF {
return fmt.Errorf("%s: copying contents: %v", path, err)
}
}
return nil
})
}