-
Notifications
You must be signed in to change notification settings - Fork 781
/
digester.go
272 lines (246 loc) · 6.62 KB
/
digester.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
package buildah
import (
"archive/tar"
"errors"
"fmt"
"hash"
"io"
"sync"
"time"
digest "github.com/opencontainers/go-digest"
)
type digester interface {
io.WriteCloser
ContentType() string
Digest() digest.Digest
}
// A simple digester just digests its content as-is.
type simpleDigester struct {
digester digest.Digester
hasher hash.Hash
contentType string
}
func newSimpleDigester(contentType string) digester {
finalDigester := digest.Canonical.Digester()
return &simpleDigester{
digester: finalDigester,
hasher: finalDigester.Hash(),
contentType: contentType,
}
}
func (s *simpleDigester) ContentType() string {
return s.contentType
}
func (s *simpleDigester) Write(p []byte) (int, error) {
return s.hasher.Write(p)
}
func (s *simpleDigester) Close() error {
return nil
}
func (s *simpleDigester) Digest() digest.Digest {
return s.digester.Digest()
}
// A tarFilterer passes a tarball through to an io.WriteCloser, potentially
// modifying headers as it goes.
type tarFilterer struct {
wg sync.WaitGroup
pipeWriter *io.PipeWriter
closedLock sync.Mutex
closed bool
err error
}
func (t *tarFilterer) Write(p []byte) (int, error) {
return t.pipeWriter.Write(p)
}
func (t *tarFilterer) Close() error {
t.closedLock.Lock()
if t.closed {
t.closedLock.Unlock()
return errors.New("tar filter is already closed")
}
t.closed = true
t.closedLock.Unlock()
err := t.pipeWriter.Close()
t.wg.Wait()
if err != nil {
return fmt.Errorf("closing filter pipe: %w", err)
}
return t.err
}
// newTarFilterer passes one or more tar archives through to an io.WriteCloser
// as a single archive, potentially calling filter to modify headers and
// contents as it goes.
//
// Note: if "filter" indicates that a given item should be skipped, there is no
// guarantee that there will not be a subsequent item of type TypeLink, which
// is a hard link, which points to the skipped item as the link target.
func newTarFilterer(writeCloser io.WriteCloser, filter func(hdr *tar.Header) (skip, replaceContents bool, replacementContents io.Reader)) io.WriteCloser {
pipeReader, pipeWriter := io.Pipe()
tarWriter := tar.NewWriter(writeCloser)
filterer := &tarFilterer{
pipeWriter: pipeWriter,
}
filterer.wg.Add(1)
go func() {
filterer.closedLock.Lock()
closed := filterer.closed
filterer.closedLock.Unlock()
for !closed {
tarReader := tar.NewReader(pipeReader)
hdr, err := tarReader.Next()
for err == nil {
var skip, replaceContents bool
var replacementContents io.Reader
if filter != nil {
skip, replaceContents, replacementContents = filter(hdr)
}
if !skip {
err = tarWriter.WriteHeader(hdr)
if err != nil {
err = fmt.Errorf("filtering tar header for %q: %w", hdr.Name, err)
break
}
if hdr.Size != 0 {
var n int64
var copyErr error
if replaceContents {
n, copyErr = io.CopyN(tarWriter, replacementContents, hdr.Size)
} else {
n, copyErr = io.Copy(tarWriter, tarReader)
}
if copyErr != nil {
err = fmt.Errorf("copying content for %q: %w", hdr.Name, copyErr)
break
}
if n != hdr.Size {
err = fmt.Errorf("filtering content for %q: expected %d bytes, got %d bytes", hdr.Name, hdr.Size, n)
break
}
}
}
hdr, err = tarReader.Next()
}
if err != io.EOF {
filterer.err = fmt.Errorf("reading tar archive: %w", err)
break
}
filterer.closedLock.Lock()
closed = filterer.closed
filterer.closedLock.Unlock()
}
err1 := tarWriter.Close()
err := writeCloser.Close()
if err == nil {
err = err1
}
pipeReader.CloseWithError(err)
filterer.wg.Done()
}()
return filterer
}
// A tar digester digests an archive, modifying the headers it digests by
// calling a specified function to potentially modify the header that it's
// about to write.
type tarDigester struct {
isOpen bool
nested digester
tarFilterer io.WriteCloser
}
func modifyTarHeaderForDigesting(hdr *tar.Header) (skip, replaceContents bool, replacementContents io.Reader) {
zeroTime := time.Time{}
hdr.ModTime = zeroTime
hdr.AccessTime = zeroTime
hdr.ChangeTime = zeroTime
return false, false, nil
}
func newTarDigester(contentType string) digester {
nested := newSimpleDigester(contentType)
digester := &tarDigester{
isOpen: true,
nested: nested,
tarFilterer: newTarFilterer(nested, modifyTarHeaderForDigesting),
}
return digester
}
func (t *tarDigester) ContentType() string {
return t.nested.ContentType()
}
func (t *tarDigester) Digest() digest.Digest {
return t.nested.Digest()
}
func (t *tarDigester) Write(p []byte) (int, error) {
return t.tarFilterer.Write(p)
}
func (t *tarDigester) Close() error {
if t.isOpen {
t.isOpen = false
return t.tarFilterer.Close()
}
return nil
}
// CompositeDigester can compute a digest over multiple items.
type CompositeDigester struct {
digesters []digester
closer io.Closer
}
// closeOpenDigester closes an open sub-digester, if we have one.
func (c *CompositeDigester) closeOpenDigester() {
if c.closer != nil {
c.closer.Close()
c.closer = nil
}
}
// Restart clears all state, so that the composite digester can start over.
func (c *CompositeDigester) Restart() {
c.closeOpenDigester()
c.digesters = nil
}
// Start starts recording the digest for a new item ("", "file", or "dir").
// The caller should call Hash() immediately after to retrieve the new
// io.WriteCloser.
func (c *CompositeDigester) Start(contentType string) {
c.closeOpenDigester()
switch contentType {
case "":
c.digesters = append(c.digesters, newSimpleDigester(""))
case "file", "dir":
digester := newTarDigester(contentType)
c.closer = digester
c.digesters = append(c.digesters, digester)
default:
panic(fmt.Sprintf(`unrecognized content type: expected "", "file", or "dir", got %q`, contentType))
}
}
// Hash returns the hasher for the current item.
func (c *CompositeDigester) Hash() io.WriteCloser {
num := len(c.digesters)
if num == 0 {
return nil
}
return c.digesters[num-1]
}
// Digest returns the content type and a composite digest over everything
// that's been digested.
func (c *CompositeDigester) Digest() (string, digest.Digest) {
c.closeOpenDigester()
num := len(c.digesters)
switch num {
case 0:
return "", ""
case 1:
return c.digesters[0].ContentType(), c.digesters[0].Digest()
default:
content := ""
for i, digester := range c.digesters {
if i > 0 {
content += ","
}
contentType := digester.ContentType()
if contentType != "" {
contentType += ":"
}
content += contentType + digester.Digest().Encoded()
}
return "multi", digest.Canonical.FromString(content)
}
}