This repository was archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathunpack.go
More file actions
399 lines (373 loc) · 11 KB
/
unpack.go
File metadata and controls
399 lines (373 loc) · 11 KB
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
// DBDeployer - The MySQL Sandbox
// Copyright © 2006-2020 Giuseppe Maxia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* Originally copyrighted as
// Copyright © 2011-12 Qtrac Ltd.
//
// This program or package and any associated files are licensed under the
// Apache License, Version 2.0 (the "License"); you may not use these files
// except in compliance with the License. You can get a copy of the License
// at: http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
/*
Code adapted and enhanced from examples to the book:
Programming in Go by Mark Summerfield
http://www.qtrac.eu/gobook.html
Original author: Mark Summerfield
Converted to package by Giuseppe Maxia in 2018
The original code was a stand-alone program, and it
had a few bugs:
* when extracting from a tar file: when there
isn't a separate item for each directory, the
extraction fails.
* The attributes of the files were not reproduced
in the extracted files.
This code fixes those problems and introduces a
destination directory and verbosity
levels for the extraction
*/
package unpack
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/xi2/xz"
"github.com/datacharmer/dbdeployer/common"
"github.com/datacharmer/dbdeployer/globals"
)
const (
SILENT = iota // No output
VERBOSE // Minimal feedback about extraction operations
CHATTY // Full details of what is being extracted
)
var Verbose int
func condPrint(s string, nl bool, level int) {
if Verbose >= level {
if nl {
fmt.Println(s)
} else {
fmt.Print(s)
}
}
}
func validSuffix(filename string) bool {
for _, suffix := range []string{globals.TgzExt, globals.TarExt, globals.TarGzExt, globals.TarXzExt} {
if strings.HasSuffix(filename, suffix) {
return true
}
}
return false
}
func UnpackXzTar(filename string, destination string, verbosityLevel int) (err error) {
Verbose = verbosityLevel
if !common.FileExists(filename) {
return fmt.Errorf("file %s not found", filename)
}
if !common.DirExists(destination) {
return fmt.Errorf("directory %s not found", destination)
}
filename, err = common.AbsolutePath(filename)
if err != nil {
return err
}
err = os.Chdir(destination)
if err != nil {
return errors.Wrapf(err, "error changing directory to %s", destination)
}
f, err := os.Open(filename) // #nosec G304
if err != nil {
return err
}
defer f.Close() // #nosec G307
// Create an xz Reader
r, err := xz.NewReader(f, 0)
if err != nil {
return err
}
// Create a tar Reader
tr := tar.NewReader(r)
return unpackTarFiles(tr, destination)
}
func UnpackTar(filename string, destination string, verbosityLevel int) (err error) {
Verbose = verbosityLevel
f, err := os.Stat(destination)
if os.IsNotExist(err) {
return fmt.Errorf("destination directory '%s' does not exist", destination)
}
filemode := f.Mode()
if !filemode.IsDir() {
return fmt.Errorf("destination '%s' is not a directory", destination)
}
if !validSuffix(filename) {
return fmt.Errorf("unrecognized archive suffix")
}
var file *os.File
// #nosec G304
if file, err = os.Open(filename); err != nil {
return err
}
defer file.Close() // #nosec G307
err = os.Chdir(destination)
if err != nil {
return errors.Wrapf(err, "error changing directory to %s", destination)
}
var fileReader io.Reader = file
var decompressor *gzip.Reader
if strings.HasSuffix(filename, globals.GzExt) {
if decompressor, err = gzip.NewReader(file); err != nil {
return err
}
defer decompressor.Close()
}
var reader *tar.Reader
if decompressor != nil {
reader = tar.NewReader(decompressor)
} else {
reader = tar.NewReader(fileReader)
}
return unpackTarFiles(reader, destination)
}
func unpackTarFiles(reader *tar.Reader, extractDir string) error {
const errLinkedDirectoryOutside = "linked directory '%s' is outside the extraction directory"
extractAbsDir, err := filepath.Abs(extractDir)
if err != nil {
return fmt.Errorf("error defining the absolute path of '%s': %s", extractDir, err)
}
var header *tar.Header
var count int = 0
var reSlash = regexp.MustCompile(`/.*`)
innerDir := ""
for {
if header, err = reader.Next(); err != nil {
if err == io.EOF {
condPrint("Files ", false, CHATTY)
condPrint(strconv.Itoa(count), true, 1)
return nil // OK
}
return err
}
// cond_print(fmt.Sprintf("%#v\n", header), true, CHATTY)
/*
tar.Header{
Typeflag:0x30,
Name:"mysql-8.0.11-macos10.13-x86_64/docs/INFO_SRC",
Linkname:"",
Size:185,
Mode:420,
Uid:7161,
Gid:10,
Uname:"pb2user",
Gname:"owner",
ModTime:time.Time{wall:0x0, ext:63658769207, loc:(*time.Location)(0x13730e0)},
AccessTime:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)},
ChangeTime:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)},
Devmajor:0, Devminor:0,
Xattrs:map[string]string(nil),
PAXRecords:map[string]string(nil),
Format:0}
tar.Header{
Typeflag:0x32,
Name:"mysql-8.0.11-macos10.13-x86_64/lib/libssl.dylib",
Linkname:"libssl.1.0.0.dylib",
Size:0,
Mode:493,
Uid:7161,
Gid:10,
Uname:"pb2user",
Gname:"owner",
ModTime:time.Time{wall:0x0, ext:63658772525, loc:(*time.Location)(0x13730e0)},
AccessTime:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)},
ChangeTime:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)},
Devmajor:0,
Devminor:0,
Xattrs:map[string]string(nil),
PAXRecords:map[string]string(nil),
Format:0}
*/
filemode := os.FileMode(header.Mode)
filename := sanitizedName(header.Name)
fileDir := path.Dir(filename)
upperDir := reSlash.ReplaceAllString(fileDir, "")
if innerDir != "" {
if upperDir != innerDir {
return fmt.Errorf("found more than one directory inside the tarball\n"+
"<%s> and <%s>", upperDir, innerDir)
}
} else {
innerDir = upperDir
}
if _, err = os.Stat(fileDir); os.IsNotExist(err) {
if err = os.MkdirAll(fileDir, globals.PublicDirectoryAttr); err != nil {
return err
}
condPrint(" + "+fileDir+" ", true, CHATTY)
}
if header.Typeflag == 0 {
header.Typeflag = tar.TypeReg
}
switch header.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(filename, globals.PublicDirectoryAttr); err != nil {
return err
}
case tar.TypeReg:
if err = unpackTarFile(filename, reader); err != nil {
return err
}
err = os.Chmod(filename, filemode)
if err != nil {
return err
}
count++
condPrint(filename, true, CHATTY)
if count%10 == 0 {
mark := "."
if count%100 == 0 {
mark = strconv.Itoa(count)
}
if Verbose < CHATTY {
condPrint(mark, false, 1)
}
}
case tar.TypeSymlink:
if header.Linkname != "" {
linkDepth := pathDepth(header.Linkname)
nameDepth := pathDepth(header.Name)
if linkDepth > nameDepth {
fmt.Println()
return fmt.Errorf(errLinkedDirectoryOutside, header.Linkname)
}
if common.FileExists(header.Linkname) {
absFile, err := filepath.Abs(header.Linkname)
if err != nil {
return fmt.Errorf("error retrieving absolute path of %s: %s", header.Linkname, err)
}
if !common.BeginsWith(absFile, extractAbsDir) {
return fmt.Errorf(errLinkedDirectoryOutside, header.Linkname)
}
} else {
if common.BeginsWith(header.Linkname, "/") {
if !common.BeginsWith(header.Linkname, extractAbsDir) {
return fmt.Errorf(errLinkedDirectoryOutside, header.Linkname)
}
}
}
condPrint(fmt.Sprintf("%s -> %s", filename, header.Linkname), true, CHATTY)
err = os.Symlink(header.Linkname, filename)
if err != nil {
return fmt.Errorf("%#v\n#ERROR: %s", header, err)
}
} else {
return fmt.Errorf("file %s is a symlink, but no link information was provided", filename)
}
}
}
// return nil
}
func pathDepth(s string) int {
reSlash := regexp.MustCompilePOSIX("(/)")
list := reSlash.FindAllStringIndex(s, -1)
return len(list)
}
func unpackTarFile(filename string,
reader *tar.Reader) (err error) {
var writer *os.File
// #nosec G304
if writer, err = os.Create(filename); err != nil {
return err
}
defer writer.Close() // #nosec G307
if _, err = io.Copy(writer, reader); err != nil {
return err
}
return nil
}
func sanitizedName(filename string) string {
if len(filename) > 1 && filename[1] == ':' {
filename = filename[2:]
}
filename = strings.TrimLeft(filename, "\\/.")
filename = strings.Replace(filename, "../", "", -1)
return strings.Replace(filename, "..\\", "", -1)
}
func VerifyTarFile(fileName string) error {
if !validSuffix(fileName) {
return fmt.Errorf("unrecognized archive suffix %s", fileName)
}
var file *os.File
var err error
// #nosec G304
if file, err = os.Open(fileName); err != nil {
return fmt.Errorf("[open file Validation] %s", err)
}
defer file.Close() // #nosec G307
var fileReader io.Reader = file
var decompressor *gzip.Reader
var xzDecompressor *xz.Reader
if strings.HasSuffix(fileName, globals.GzExt) {
if decompressor, err = gzip.NewReader(file); err != nil {
return fmt.Errorf("[gz Validation] %s", err)
}
defer decompressor.Close()
} else {
if strings.HasSuffix(fileName, globals.TarXzExt) {
if xzDecompressor, err = xz.NewReader(file, 0); err != nil {
return fmt.Errorf("[xz Validation] %s", err)
}
}
}
var reader *tar.Reader
if decompressor != nil {
reader = tar.NewReader(decompressor)
} else {
if xzDecompressor != nil {
reader = tar.NewReader(xzDecompressor)
} else {
reader = tar.NewReader(fileReader)
}
}
var header *tar.Header
expectedDirName := common.BaseName(fileName)
reExt := regexp.MustCompile(`\.(?:tar(?:\.gz|\.xz)?)$`)
expectedDirName = reExt.ReplaceAllString(expectedDirName, "")
if header, err = reader.Next(); err != nil {
if err == io.EOF {
return fmt.Errorf("[EOF Validation] file %s is empty", fileName)
}
return fmt.Errorf("[header validation] %s", err)
}
innerFileName := sanitizedName(header.Name)
fileDir := path.Dir(innerFileName)
reSlash := regexp.MustCompile(`/.*`)
fileDir = reSlash.ReplaceAllString(fileDir, "")
if fileDir != expectedDirName {
return fmt.Errorf("inner directory name different from tarball name\n"+
"Expected: %s - Found: %s", expectedDirName, fileDir)
}
return nil
}