-
Notifications
You must be signed in to change notification settings - Fork 7
/
targz.go
319 lines (266 loc) · 6.73 KB
/
targz.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
// Package targz contains methods to create and extract tar gz archives.
//
// Usage (discarding potential errors):
// targz.Compress("path/to/the/directory/to/compress", "my_archive.tar.gz")
// targz.Extract("my_archive.tar.gz", "directory/to/extract/to")
// This creates an archive in ./my_archive.tar.gz with the folder "compress" (last in the path).
// And extracts the folder "compress" to "directory/to/extract/to/". The folder structure is created if it doesn't exist.
package targz
import (
"archive/tar"
"bufio"
"compress/gzip"
"errors"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"syscall"
)
// Compress creates a archive from the folder inputFilePath points to in the file outputFilePath points to.
// Only adds the last directory in inputFilePath to the archive, not the whole path.
// It tries to create the directory structure outputFilePath contains if it doesn't exist.
// It returns potential errors to be checked or nil if everything works.
func Compress(inputFilePath, outputFilePath string) (err error) {
inputFilePath = stripTrailingSlashes(inputFilePath)
inputFilePath, outputFilePath, err = makeAbsolute(inputFilePath, outputFilePath)
if err != nil {
return err
}
undoDir, err := mkdirAll(filepath.Dir(outputFilePath), 0755)
if err != nil {
return err
}
defer func() {
if err != nil {
undoDir()
}
}()
err = compress(inputFilePath, outputFilePath, filepath.Dir(inputFilePath))
if err != nil {
return err
}
return nil
}
// Extract extracts a archive from the file inputFilePath points to in the directory outputFilePath points to.
// It tries to create the directory structure outputFilePath contains if it doesn't exist.
// It returns potential errors to be checked or nil if everything works.
func Extract(inputFilePath, outputFilePath string) (err error) {
outputFilePath = stripTrailingSlashes(outputFilePath)
inputFilePath, outputFilePath, err = makeAbsolute(inputFilePath, outputFilePath)
if err != nil {
return err
}
undoDir, err := mkdirAll(outputFilePath, 0755)
if err != nil {
return err
}
defer func() {
if err != nil {
undoDir()
}
}()
return extract(inputFilePath, outputFilePath)
}
// Creates all directories with os.MakedirAll and returns a function to remove the first created directory so cleanup is possible.
func mkdirAll(dirPath string, perm os.FileMode) (func(), error) {
var undoDir string
for p := dirPath; ; p = path.Dir(p) {
finfo, err := os.Stat(p)
if err == nil {
if finfo.IsDir() {
break
}
finfo, err = os.Lstat(p)
if err != nil {
return nil, err
}
if finfo.IsDir() {
break
}
return nil, &os.PathError{"mkdirAll", p, syscall.ENOTDIR}
}
if os.IsNotExist(err) {
undoDir = p
} else {
return nil, err
}
}
if undoDir == "" {
return func() {}, nil
}
if err := os.MkdirAll(dirPath, perm); err != nil {
return nil, err
}
return func() { os.RemoveAll(undoDir) }, nil
}
// Remove trailing slash if any.
func stripTrailingSlashes(path string) string {
if len(path) > 0 && path[len(path)-1] == '/' {
path = path[0 : len(path)-1]
}
return path
}
// Make input and output paths absolute.
func makeAbsolute(inputFilePath, outputFilePath string) (string, string, error) {
inputFilePath, err := filepath.Abs(inputFilePath)
if err == nil {
outputFilePath, err = filepath.Abs(outputFilePath)
}
return inputFilePath, outputFilePath, err
}
// The main interaction with tar and gzip. Creates a archive and recursivly adds all files in the directory.
// The finished archive contains just the directory added, not any parents.
// This is possible by giving the whole path exept the final directory in subPath.
func compress(inPath, outFilePath, subPath string) (err error) {
files, err := ioutil.ReadDir(inPath)
if err != nil {
return err
}
if len(files) == 0 {
return errors.New("targz: input directory is empty")
}
file, err := os.Create(outFilePath)
if err != nil {
return err
}
defer func() {
if err != nil {
os.Remove(outFilePath)
}
}()
gzipWriter := gzip.NewWriter(file)
tarWriter := tar.NewWriter(gzipWriter)
err = writeDirectory(inPath, tarWriter, subPath)
if err != nil {
return err
}
err = tarWriter.Close()
if err != nil {
return err
}
err = gzipWriter.Close()
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
return nil
}
// Read a directy and write it to the tar writer. Recursive function that writes all sub folders.
func writeDirectory(directory string, tarWriter *tar.Writer, subPath string) error {
files, err := ioutil.ReadDir(directory)
if err != nil {
return err
}
for _, file := range files {
currentPath := filepath.Join(directory, file.Name())
if file.IsDir() {
err := writeDirectory(currentPath, tarWriter, subPath)
if err != nil {
return err
}
} else {
err = writeTarGz(currentPath, tarWriter, file, subPath)
if err != nil {
return err
}
}
}
return nil
}
// Write path without the prefix in subPath to tar writer.
func writeTarGz(path string, tarWriter *tar.Writer, fileInfo os.FileInfo, subPath string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
evaledPath, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}
subPath, err = filepath.EvalSymlinks(subPath)
if err != nil {
return err
}
link := ""
if evaledPath != path {
link = evaledPath
}
header, err := tar.FileInfoHeader(fileInfo, link)
if err != nil {
return err
}
header.Name = evaledPath[len(subPath):]
err = tarWriter.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(tarWriter, file)
if err != nil {
return err
}
return err
}
// Extract the file in filePath to directory.
func extract(filePath string, directory string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
gzipReader, err := gzip.NewReader(bufio.NewReader(file))
if err != nil {
return err
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
fileInfo := header.FileInfo()
dir := filepath.Join(directory, filepath.Dir(header.Name))
filename := filepath.Join(dir, fileInfo.Name())
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
file, err := os.Create(filename)
if err != nil {
return err
}
writer := bufio.NewWriter(file)
buffer := make([]byte, 4096)
for {
n, err := tarReader.Read(buffer)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
_, err = writer.Write(buffer[:n])
if err != nil {
return err
}
}
err = writer.Flush()
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
}
return nil
}