forked from datacharmer/dbdeployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpack.go
288 lines (271 loc) · 7.64 KB
/
unpack.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
// DBDeployer - The MySQL Sandbox
// Copyright © 2006-2018 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"
"strconv"
"strings"
"github.com/datacharmer/dbdeployer/common"
"github.com/datacharmer/dbdeployer/globals"
"github.com/pkg/errors"
"github.com/xi2/xz"
)
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.Printf(s)
}
}
}
func validSuffix(filename string) bool {
for _, suffix := range []string{globals.TgzExt, globals.TarExt, globals.TarGzExt} {
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)
if err != nil {
return err
}
defer f.Close()
// 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)
}
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() == false {
return fmt.Errorf("destination '%s' is not a directory", destination)
}
if !validSuffix(filename) {
return fmt.Errorf("unrecognized archive suffix")
}
var file *os.File
if file, err = os.Open(filename); err != nil {
return err
}
defer file.Close()
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)
}
func unpackTarFiles(reader *tar.Reader) (err error) {
var header *tar.Header
var count int = 0
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)
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 != "" {
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\n", filename)
}
}
}
// return nil
}
func unpackTarFile(filename string,
reader *tar.Reader) (err error) {
var writer *os.File
if writer, err = os.Create(filename); err != nil {
return err
}
defer writer.Close()
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)
}