/
archive_util.go
363 lines (315 loc) · 11.2 KB
/
archive_util.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
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
package command
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/evergreen-ci/utility"
"github.com/klauspost/pgzip"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
// buildArchive reads the rootPath directory into the tar.Writer,
// taking included and excluded strings into account.
// Returns the number of files that were added to the archive
func buildArchive(ctx context.Context, tarWriter *tar.Writer, rootPath string, pathsToAdd []archiveContentFile,
excludes []string, logger grip.Journaler) (int, error) {
numFilesArchived := 0
processed := map[string]bool{}
logger.Infof("Beginning to build archive.")
FileLoop:
for _, file := range pathsToAdd {
if err := ctx.Err(); err != nil {
return numFilesArchived, errors.Wrap(err, "building archive was canceled")
}
var intarball string
// Tarring symlinks doesn't work reliably right now, so if the file is
// a symlink, leave intarball path intact but write from the file
// underlying the symlink.
if file.info.Mode()&os.ModeSymlink > 0 {
symlinkPath, err := filepath.EvalSymlinks(file.path)
if err != nil {
logger.Warningf("Could not follow symlink '%s', ignoring.", file.path)
continue
} else {
logger.Infof("Following symlink '%s', got path '%s'.", file.path, symlinkPath)
symlinkFileInfo, err := os.Stat(symlinkPath)
if err != nil {
logger.Warningf("Failed to get underlying file for symlink '%s', ignoring.", file.path)
continue
}
intarball = strings.Replace(file.path, "\\", "/", -1)
file.path = symlinkPath
file.info = symlinkFileInfo
}
} else {
intarball = strings.Replace(file.path, "\\", "/", -1)
}
rootPathPrefix := strings.Replace(rootPath, "\\", "/", -1)
intarball = strings.Replace(intarball, "\\", "/", -1)
intarball = strings.Replace(intarball, rootPathPrefix, "", 1)
intarball = filepath.Clean(intarball)
intarball = strings.Replace(intarball, "\\", "/", -1)
//strip any leading slash from the tarball header path
intarball = strings.TrimLeft(intarball, "/")
logger.Infof("Adding file to tarball: '%s'.", intarball)
if _, hasKey := processed[intarball]; hasKey {
continue
} else {
processed[intarball] = true
}
if file.info.IsDir() {
continue
}
_, fileName := filepath.Split(file.path)
for _, ignore := range excludes {
if match, _ := filepath.Match(ignore, fileName); match {
continue FileLoop
}
}
hdr := new(tar.Header)
hdr.Name = strings.TrimPrefix(intarball, rootPathPrefix)
hdr.Mode = int64(file.info.Mode())
hdr.Size = file.info.Size()
hdr.ModTime = file.info.ModTime()
numFilesArchived++
err := tarWriter.WriteHeader(hdr)
if err != nil {
return numFilesArchived, errors.Wrapf(err, "writing tarball header for file '%s'", intarball)
}
in, err := os.Open(file.path)
if err != nil {
return numFilesArchived, errors.Wrapf(err, "opening file '%s'", file.path)
}
amountWrote, err := io.Copy(tarWriter, in)
if err != nil {
logger.Debug(errors.Wrapf(in.Close(), "closing file '%s'", file.path))
return numFilesArchived, errors.Wrapf(err, "copying file '%s' into tarball", file.path)
}
if amountWrote != hdr.Size {
logger.Debug(errors.Wrapf(in.Close(), "closing file '%s'", file.path))
return numFilesArchived, errors.Errorf("tarball header size is %d but actually wrote %d", hdr.Size, amountWrote)
}
logger.Debug(errors.Wrapf(in.Close(), "closing file '%s'", file.path))
logger.Warning(errors.Wrap(tarWriter.Flush(), "flushing tar writer"))
}
return numFilesArchived, nil
}
func extractTarball(ctx context.Context, reader io.Reader, rootPath string, excludes []string) error {
// wrap the reader in a gzip reader and a tar reader
gzipReader, err := pgzip.NewReader(reader)
if err != nil {
return errors.Wrap(err, "creating gzip reader")
}
tarReader := tar.NewReader(gzipReader)
err = extractTarArchive(ctx, tarReader, rootPath, excludes)
if err != nil {
return errors.Wrapf(err, "extracting path '%s'", rootPath)
}
return nil
}
// extractTarArchive unpacks the tar.Reader into rootPath.
func extractTarArchive(ctx context.Context, tarReader *tar.Reader, rootPath string, excludes []string) error {
for {
hdr, err := tarReader.Next()
if err == io.EOF {
return nil //reached end of archive, we are done.
}
if err != nil {
return errors.WithStack(err)
}
if err := ctx.Err(); err != nil {
return errors.Wrap(err, "extraction operation canceled")
}
if hdr.Typeflag == tar.TypeDir {
// this tar entry is a directory - need to mkdir it
localDir := fmt.Sprintf("%v/%v", rootPath, hdr.Name)
if err = os.MkdirAll(localDir, 0755); err != nil {
return errors.WithStack(err)
}
} else if hdr.Typeflag == tar.TypeLink {
if err = os.Link(hdr.Name, hdr.Linkname); err != nil {
return errors.WithStack(err)
}
} else if hdr.Typeflag == tar.TypeSymlink {
if err = os.Symlink(hdr.Name, hdr.Linkname); err != nil {
return errors.WithStack(err)
}
} else if hdr.Typeflag == tar.TypeReg || hdr.Typeflag == tar.TypeRegA {
// this tar entry is a regular file (not a dir or link)
// first, ensure the file's parent directory exists
localFile := fmt.Sprintf("%v/%v", rootPath, hdr.Name)
for _, ignore := range excludes {
if match, _ := filepath.Match(ignore, localFile); match {
continue
}
}
dir := filepath.Dir(localFile)
if err = os.MkdirAll(dir, 0755); err != nil {
return errors.WithStack(err)
}
// Now create the file itself, and write in the contents.
// Not using 'defer f.Close()' because this is in a loop,
// and we don't want to wait for the whole archive to finish to
// close the files - so each is closed explicitly.
f, err := os.Create(localFile)
if err != nil {
return errors.WithStack(err)
}
if _, err = io.Copy(f, tarReader); err != nil {
grip.Error(errors.Wrapf(f.Close(), "closing file '%s'", localFile))
return errors.Wrap(err, "copying tar contents to local file '%s'")
}
// File's permissions should match what was in the archive
if err = os.Chmod(f.Name(), os.FileMode(int32(hdr.Mode))); err != nil {
grip.Error(errors.Wrapf(f.Close(), "closing file '%s'", localFile))
return errors.Wrapf(err, "changing file '%s' mode to %d", f.Name(), hdr.Mode)
}
grip.Error(errors.Wrapf(f.Close(), "closing file '%s'", localFile))
} else {
return errors.Errorf("unknown file type '%c' in archive", hdr.Typeflag)
}
}
}
// tarGzReader returns a file, gzip reader, and tar reader for the given path.
// The tar reader wraps the gzip reader, which wraps the file.
func tarGzReader(path string) (f, gz io.ReadCloser, tarReader *tar.Reader, err error) {
f, err = os.Open(path)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "opening file '%s'", path)
}
gz, err = pgzip.NewReader(f)
if err != nil {
defer f.Close()
return nil, nil, nil, errors.Wrap(err, "initializing gzip reader")
}
tarReader = tar.NewReader(gz)
return f, gz, tarReader, nil
}
// tarGzWriter returns a file, gzip writer, and tarWriter for the path.
// The tar writer wraps the gzip writer, which wraps the file. If
// useParallelGzip is true, then it will use the parallel gzip algorithm;
// otherwise, it'll use the standard single-threaded gzip algorithm.
func tarGzWriter(path string, useParallelGzip bool) (f, gz io.WriteCloser, tarWriter *tar.Writer, err error) {
f, err = os.Create(path)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "creating file '%s'", path)
}
if useParallelGzip {
gz = pgzip.NewWriter(f)
} else {
gz = gzip.NewWriter(f)
}
tarWriter = tar.NewWriter(gz)
return f, gz, tarWriter, nil
}
// archiveContentFile represents a tar file on disk.
type archiveContentFile struct {
path string
info os.FileInfo
err error
}
// findContentsToArchive finds all files starting from the rootPath with the
// given inclusion and exclusion patterns.
func findContentsToArchive(ctx context.Context, rootPath string, includes, excludes []string) (files []archiveContentFile, totalSize int, err error) {
out := []archiveContentFile{}
catcher := grip.NewBasicCatcher()
archiveContents, totalSize, err := findArchiveContents(ctx, rootPath, includes, excludes)
if err != nil {
return nil, 0, errors.Wrap(err, "getting archive contents")
}
for _, fn := range archiveContents {
if fn.err != nil {
catcher.Add(fn.err)
continue
}
out = append(out, fn)
}
if catcher.HasErrors() {
return nil, 0, catcher.Resolve()
}
return out, totalSize, nil
}
// findArchiveContents returns files to be archived starting at the rootPath. It
// matches all files that match any of the include filters and are not excluded
// by one of the exclude filters. At least one include filter must be given for
// this to return any files.
func findArchiveContents(ctx context.Context, rootPath string, includes, excludes []string) (files []archiveContentFile, totalSize int, err error) {
archiveContents := []archiveContentFile{}
seen := map[string]archiveContentFile{}
catcher := grip.NewCatcher()
addUniqueFile := func(path string, info fs.FileInfo) {
if _, ok := seen[path]; ok {
return
}
acf := archiveContentFile{path: path, info: info}
seen[path] = acf
archiveContents = append(archiveContents, acf)
if info.Mode().IsRegular() {
totalSize += int(info.Size())
}
}
for _, includePattern := range includes {
dir, filematch := filepath.Split(includePattern)
dir = filepath.Join(rootPath, dir)
if !utility.FileExists(dir) {
continue
}
if err := ctx.Err(); err != nil {
return nil, 0, errors.Wrapf(err, "canceled while streaming archive for include pattern '%s'", includePattern)
}
var walk filepath.WalkFunc
if filematch == "**" {
walk = func(path string, info os.FileInfo, err error) error {
for _, ignore := range excludes {
if match, _ := filepath.Match(ignore, path); match {
return nil
}
}
addUniqueFile(path, info)
return nil
}
catcher.Wrapf(filepath.Walk(dir, walk), "matching files included in filter '%s' for path '%s'", filematch, dir)
} else if strings.Contains(filematch, "**") {
globSuffix := filematch[2:]
walk = func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(filepath.Base(path), globSuffix) {
for _, ignore := range excludes {
if match, _ := filepath.Match(ignore, path); match {
return nil
}
}
addUniqueFile(path, info)
}
return nil
}
catcher.Wrapf(filepath.Walk(dir, walk), "matching files included in filter '%s' for path '%s'", filematch, dir)
} else {
walk = func(path string, info os.FileInfo, err error) error {
a, b := filepath.Split(path)
if filepath.Clean(a) == filepath.Clean(dir) {
match, err := filepath.Match(filematch, b)
if err != nil {
archiveContents = append(archiveContents, archiveContentFile{err: err})
}
if match {
for _, ignore := range excludes {
if exmatch, _ := filepath.Match(ignore, path); exmatch {
return nil
}
}
addUniqueFile(path, info)
}
}
return nil
}
catcher.Wrapf(filepath.Walk(rootPath, walk), "matching files included in filter '%s' for patch '%s'", filematch, rootPath)
}
}
return archiveContents, totalSize, catcher.Resolve()
}