-
Notifications
You must be signed in to change notification settings - Fork 300
/
tar.go
355 lines (311 loc) · 9.55 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
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
package build
import (
"archive/tar"
"bytes"
"context"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"github.com/tilt-dev/tilt/internal/build/moby"
"github.com/tilt-dev/tilt/internal/dockerfile"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
)
type ArchiveBuilder struct {
tw *tar.Writer
filter model.PathMatcher
paths []string // local paths archived
// A shared I/O buffer to help with file copying.
copyBuf *bytes.Buffer
}
func NewArchiveBuilder(writer io.Writer, filter model.PathMatcher) *ArchiveBuilder {
tw := tar.NewWriter(writer)
if filter == nil {
filter = model.EmptyMatcher
}
return &ArchiveBuilder{tw: tw, filter: filter, copyBuf: bytes.NewBuffer(nil)}
}
func (a *ArchiveBuilder) Close() error {
return a.tw.Close()
}
// NOTE(dmiller) sometimes users will have very large UID/GIDs that will cause
// archive/tar to switch to PAX format, which will trip this Docker bug:
// https://github.com/docker/cli/issues/1459
// To prevent this, simply clear these out before adding to tar.
func clearUIDAndGID(h *tar.Header) {
h.Uid = 0
h.Gid = 0
}
func (a *ArchiveBuilder) archiveDf(ctx context.Context, df dockerfile.Dockerfile) error {
tarHeader := &tar.Header{
Name: "Dockerfile",
Typeflag: tar.TypeReg,
Size: int64(len(df)),
Mode: 0644,
ModTime: time.Now(),
AccessTime: time.Now(),
ChangeTime: time.Now(),
}
clearUIDAndGID(tarHeader)
err := a.tw.WriteHeader(tarHeader)
if err != nil {
return err
}
_, err = a.tw.Write([]byte(df))
if err != nil {
return err
}
return nil
}
// ArchivePathsIfExist creates a tar archive of all local files in `paths`. It quietly skips any paths that don't exist.
func (a *ArchiveBuilder) ArchivePathsIfExist(ctx context.Context, paths []PathMapping) error {
// In order to handle overlapping syncs, we
// 1) collect all the entries,
// 2) de-dupe them, with last-one-wins semantics
// 3) write all the entries
//
// It's not obvious that this is the correct behavior. A better approach
// (that's more in-line with how syncs work) might ignore files in earlier
// path mappings when we know they're going to be "synced" over.
// There's a bunch of subtle product decisions about how overlapping path
// mappings work that we're not sure about.
entries := []archiveEntry{}
for _, p := range paths {
newEntries, err := a.entriesForPath(ctx, p.LocalPath, p.ContainerPath)
if err != nil {
return errors.Wrapf(err, "tarPath '%s'", p.LocalPath)
}
entries = append(entries, newEntries...)
}
entries = dedupeEntries(entries)
for _, entry := range entries {
err := a.writeEntry(entry)
if err != nil {
return errors.Wrapf(err, "tarPath '%s'", entry.path)
}
a.paths = append(a.paths, entry.path)
}
return nil
}
// Local paths that were archived
func (a *ArchiveBuilder) Paths() []string {
return a.paths
}
type archiveEntry struct {
path string
info os.FileInfo
header *tar.Header
}
// tarPath writes the given source path into tarWriter at the given dest (recursively for directories).
// e.g. tarring my_dir --> dest d: d/file_a, d/file_b
// If source path does not exist, quietly skips it and returns no err
func (a *ArchiveBuilder) entriesForPath(ctx context.Context, localPath, containerPath string) ([]archiveEntry, error) {
localInfo, err := os.Stat(localPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrapf(err, "%s: stat", localPath)
}
localPathIsDir := localInfo.IsDir()
if localPathIsDir {
// Make sure we can trim this off filenames to get valid relative filepaths
if !strings.HasSuffix(localPath, string(filepath.Separator)) {
localPath += string(filepath.Separator)
}
}
containerPath = strings.TrimPrefix(containerPath, "/")
result := make([]archiveEntry, 0)
err = filepath.Walk(localPath, func(curLocalPath string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "error walking to %s", curLocalPath)
}
matches, err := a.filter.Matches(curLocalPath)
if err != nil {
return err
}
if matches {
if info.IsDir() && curLocalPath != localPath {
shouldSkip, err := a.filter.MatchesEntireDir(curLocalPath)
if err != nil {
return err
}
if shouldSkip {
return filepath.SkipDir
}
}
return nil
}
linkname := ""
if info.Mode()&os.ModeSymlink != 0 {
var err error
linkname, err = os.Readlink(curLocalPath)
if err != nil {
return err
}
}
header, err := tar.FileInfoHeader(info, linkname)
if err != nil {
// Not all types of files are allowed in a tarball. That's OK.
// Mimic the Docker behavior and just skip the file.
logger.Get(ctx).Debugf("Skipping file %s: %v", curLocalPath, err)
return nil
}
header.Mode = int64(moby.ChmodTarEntry(os.FileMode(header.Mode)))
clearUIDAndGID(header)
if localPathIsDir {
// Name of file in tar should be relative to source directory...
tmp, err := filepath.Rel(localPath, curLocalPath)
if err != nil {
return errors.Wrapf(err, "making rel path source:%s path:%s", localPath, curLocalPath)
}
// ...and live inside `dest`
header.Name = path.Join(containerPath, filepath.ToSlash(tmp))
} else if strings.HasSuffix(containerPath, "/") {
header.Name = containerPath + filepath.Base(curLocalPath)
} else {
header.Name = containerPath
}
header.Name = path.Clean(header.Name)
result = append(result, archiveEntry{
path: curLocalPath,
info: info,
header: header,
})
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
func (a *ArchiveBuilder) writeEntry(entry archiveEntry) error {
path := entry.path
header := entry.header
if header.Typeflag != tar.TypeReg {
// anything other than a regular file (e.g. dir, symlink) just needs the header
if err := a.tw.WriteHeader(header); err != nil {
return errors.Wrapf(err, "%s: writing header", path)
}
return nil
}
file, err := os.Open(path)
if err != nil {
// In case the file has been deleted since we last looked at it.
if os.IsNotExist(err) {
return nil
}
return errors.Wrapf(err, "%s: open", path)
}
defer func() {
_ = file.Close()
}()
// The size header must match the number of contents bytes.
//
// There is room for a race condition here if something writes to the file
// after we've read the file size.
//
// For small files, we avoid this by first copying the file into a buffer,
// and using the size of the buffer to populate the header.
//
// For larger files, we don't want to copy the whole thing into a buffer,
// because that would blow up heap size. There is some danger that this
// will lead to a spurious error when the tar writer validates the sizes.
// That error will be disruptive but will be handled as best as we
// can downstream.
useBuf := header.Size < 5000000
if useBuf {
a.copyBuf.Reset()
_, err = io.Copy(a.copyBuf, file)
if err != nil && err != io.EOF {
return errors.Wrapf(err, "%s: copying Contents", path)
}
header.Size = int64(len(a.copyBuf.Bytes()))
}
// wait to write the header until _after_ the file is successfully opened
// to avoid generating an invalid tar entry that has a header but no contents
// in the case the file has been deleted
err = a.tw.WriteHeader(header)
if err != nil {
return errors.Wrapf(err, "%s: writing header", path)
}
if useBuf {
_, err = io.Copy(a.tw, a.copyBuf)
} else {
_, err = io.Copy(a.tw, file)
}
if err != nil && err != io.EOF {
return errors.Wrapf(err, "%s: copying Contents", path)
}
// explicitly flush so that if the entry is invalid we will detect it now and
// provide a more meaningful error
if err := a.tw.Flush(); err != nil {
return errors.Wrapf(err, "%s: flush", path)
}
return nil
}
func tarContextAndUpdateDf(ctx context.Context, writer io.Writer, df dockerfile.Dockerfile, paths []PathMapping, filter model.PathMatcher) error {
ab := NewArchiveBuilder(writer, filter)
err := ab.ArchivePathsIfExist(ctx, paths)
if err != nil {
return errors.Wrap(err, "archivePaths")
}
err = ab.archiveDf(ctx, df)
if err != nil {
_ = ab.Close()
return errors.Wrap(err, "archiveDf")
}
return ab.Close()
}
func TarPath(ctx context.Context, writer io.Writer, path string) error {
ab := NewArchiveBuilder(writer, model.EmptyMatcher)
err := ab.ArchivePathsIfExist(ctx, []PathMapping{
{
LocalPath: path,
ContainerPath: ".",
},
})
if err != nil {
_ = ab.Close()
return errors.Wrap(err, "TarPath")
}
return ab.Close()
}
func TarArchiveForPaths(ctx context.Context, toArchive []PathMapping, filter model.PathMatcher) io.ReadCloser {
pr, pw := io.Pipe()
go tarArchiveForPaths(ctx, pw, toArchive, filter)
return pr
}
func tarArchiveForPaths(ctx context.Context, pw *io.PipeWriter, toArchive []PathMapping, filter model.PathMatcher) {
ab := NewArchiveBuilder(pw, filter)
err := ab.ArchivePathsIfExist(ctx, toArchive)
if err != nil {
_ = pw.CloseWithError(errors.Wrap(err, "archivePathsIfExists"))
} else {
// propagate errors from the TarWriter::Close() because it performs a final
// Flush() and any errors mean the tar is invalid
if err := ab.Close(); err != nil {
_ = pw.CloseWithError(errors.Wrap(err, "tar close"))
} else {
_ = pw.Close()
}
}
}
// Dedupe the entries with last-entry-wins semantics.
func dedupeEntries(entries []archiveEntry) []archiveEntry {
seenIndex := make(map[string]int, len(entries))
result := make([]archiveEntry, 0, len(entries))
for i, entry := range entries {
seenIndex[entry.header.Name] = i
}
for i, entry := range entries {
if seenIndex[entry.header.Name] == i {
result = append(result, entry)
}
}
return result
}