-
Notifications
You must be signed in to change notification settings - Fork 63
/
tar_writer.go
344 lines (278 loc) · 6.96 KB
/
tar_writer.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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package imagetar
import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"sort"
"time"
"carvel.dev/imgpkg/pkg/imgpkg/imagedesc"
"carvel.dev/imgpkg/pkg/imgpkg/internal/util"
regv1 "github.com/google/go-containerregistry/pkg/v1"
)
type Logger interface {
Logf(str string, args ...interface{})
}
type TarWriterOpts struct {
Concurrency int
}
type TarWriter struct {
ids *imagedesc.ImageRefDescriptors
dstOpener func() (io.WriteCloser, error)
dst io.WriteCloser
tf *tar.Writer
layersToWrite []imagedesc.ImageLayerDescriptor
opts TarWriterOpts
logger Logger
imageLayerWriterCheck ImageLayerWriterFilter
layersFromOtherSource []regv1.Layer
}
// NewTarWriter constructor returning a mechanism to write image refs / layers to a tarball on disk.
func NewTarWriter(ids *imagedesc.ImageRefDescriptors, dstOpener func() (io.WriteCloser, error),
opts TarWriterOpts, logger Logger, imageLayerWriterCheck ImageLayerWriterFilter,
layersFromOtherSource []regv1.Layer) *TarWriter {
return &TarWriter{
ids: ids,
dstOpener: dstOpener,
opts: opts,
logger: logger,
imageLayerWriterCheck: imageLayerWriterCheck,
layersFromOtherSource: layersFromOtherSource,
}
}
func (w *TarWriter) Write() error {
var err error
w.dst, err = w.dstOpener()
if err != nil {
return err
}
defer w.dst.Close()
w.tf = tar.NewWriter(w.dst)
defer w.tf.Close()
idsBytes, err := w.ids.AsBytes()
if err != nil {
return err
}
err = w.writeTarEntry(w.tf, "manifest.json", bytes.NewReader(idsBytes), int64(len(idsBytes)))
if err != nil {
return err
}
for _, td := range w.ids.Descriptors() {
switch {
case td.Image != nil:
err := w.writeImage(*td.Image)
if err != nil {
return err
}
case td.ImageIndex != nil:
err := w.writeImageIndex(*td.ImageIndex)
if err != nil {
return err
}
default:
panic("Unknown item")
}
}
return w.writeLayers()
}
func (w *TarWriter) writeImageIndex(td imagedesc.ImageIndexDescriptor) error {
for _, idx := range td.Indexes {
err := w.writeImageIndex(idx)
if err != nil {
return err
}
}
for _, img := range td.Images {
err := w.writeImage(img)
if err != nil {
return err
}
}
return nil
}
func (w *TarWriter) writeImage(td imagedesc.ImageDescriptor) error {
for _, imgLayer := range td.Layers {
shouldLayerBeIncluded, err := w.imageLayerWriterCheck.ShouldLayerBeIncluded(imagedesc.NewDescribedCompressedLayer(imgLayer, nil))
if err != nil {
return err
}
if shouldLayerBeIncluded {
w.layersToWrite = append(w.layersToWrite, imgLayer)
}
}
return nil
}
type writtenLayer struct {
Name string
Offset int64
Layer imagedesc.ImageLayerDescriptor
}
func (w *TarWriter) writeLayers() error {
// Sort layers by digest to have deterministic archive
sort.Slice(w.layersToWrite, func(i, j int) bool {
return w.layersToWrite[i].Digest < w.layersToWrite[j].Digest
})
seekableDst, isSeekable := w.dst.(*os.File)
isInflatable := (w.opts.Concurrency > 1) && isSeekable
writtenLayers := map[string]writtenLayer{}
// Inflate tar file so that multiple writes can happen in parallel
for _, imgLayer := range w.layersToWrite {
digest, err := regv1.NewHash(imgLayer.Digest)
if err != nil {
return err
}
name := digest.Algorithm + "-" + digest.Hex + ".tar.gz"
// Dedup layers
if _, found := writtenLayers[name]; found {
continue
}
err = w.tf.Flush()
if err != nil {
return err
}
var stream io.Reader
var currPos int64
if isSeekable {
currPos, err = seekableDst.Seek(0, 1)
if err != nil {
return fmt.Errorf("Find current pos: %s", err)
}
}
if isInflatable {
stream = nil
} else {
for _, layer := range w.layersFromOtherSource {
d, err := layer.Digest()
if err != nil {
return fmt.Errorf("Retrieving digest: %s", err)
}
if d.String() == imgLayer.Digest {
stream, err = layer.Compressed()
if err != nil {
return fmt.Errorf("Retrieve layer from file: %s", err)
}
break
}
}
if stream == nil {
foundLayer, err := w.ids.FindLayer(imgLayer)
if err != nil {
return err
}
stream, err = foundLayer.Open()
if err != nil {
return err
}
}
}
err = w.writeTarEntry(w.tf, name, stream, imgLayer.Size)
if err != nil {
return fmt.Errorf("Writing tar entry: %s", err)
}
writtenLayers[name] = writtenLayer{
Name: name,
Layer: imgLayer,
Offset: currPos,
}
}
err := w.tf.Flush()
if err != nil {
return err
}
if isInflatable {
return w.fillInLayers(writtenLayers)
}
return nil
}
func (w *TarWriter) fillInLayers(writtenLayers map[string]writtenLayer) error {
var sortedWrittenLayers []writtenLayer
for _, writtenLayer := range writtenLayers {
sortedWrittenLayers = append(sortedWrittenLayers, writtenLayer)
}
// Prefer larger sizes first
sort.Slice(sortedWrittenLayers, func(i, j int) bool {
return sortedWrittenLayers[i].Layer.Size >= sortedWrittenLayers[j].Layer.Size
})
errCh := make(chan error, len(writtenLayers))
writeThrottle := util.NewThrottle(w.opts.Concurrency)
// Fill in actual data
for _, writtenLayer := range sortedWrittenLayers {
writtenLayer := writtenLayer // copy
go func() {
writeThrottle.Take()
defer writeThrottle.Done()
errCh <- util.Retry(func() error {
return w.fillInLayer(writtenLayer)
})
}()
}
for i := 0; i < len(writtenLayers); i++ {
err := <-errCh
if err != nil {
return fmt.Errorf("Filling in a layer: %s", err)
}
}
return nil
}
func (w *TarWriter) fillInLayer(wl writtenLayer) error {
file, err := w.dstOpener()
if err != nil {
return err
}
defer file.Close()
_, err = file.(*os.File).Seek(wl.Offset, 0)
if err != nil {
return fmt.Errorf("Seeking to offset: %s", err)
}
tw := tar.NewWriter(file)
// Do not close tar writer as it would add unwanted footer
foundLayer, err := w.ids.FindLayer(wl.Layer)
if err != nil {
return err
}
stream, err := foundLayer.Open()
if err != nil {
return err
}
err = w.writeTarEntry(tw, wl.Name, stream, wl.Layer.Size)
if err != nil {
return fmt.Errorf("Rewriting tar entry (%s): %s", wl.Name, err)
}
return tw.Flush()
}
func (w *TarWriter) writeTarEntry(tw *tar.Writer, path string, r io.Reader, size int64) error {
var zerosFill bool
if r == nil {
zerosFill = true
r = io.LimitReader(zeroReader{}, size)
}
hdr := &tar.Header{
Mode: 0644,
Typeflag: tar.TypeReg,
Size: size,
Name: path,
}
err := tw.WriteHeader(hdr)
if err != nil {
return fmt.Errorf("Writing header: %s", err)
}
t1 := time.Now()
_, err = io.Copy(tw, r)
if err != nil {
return fmt.Errorf("Copying data: %s", err)
}
if !zerosFill {
w.logger.Logf("done: file '%s' (%s)\n", path, time.Now().Sub(t1))
}
return nil
}
type zeroReader struct{}
func (r zeroReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = 0
}
return len(p), nil
}