-
Notifications
You must be signed in to change notification settings - Fork 86
/
layer.go
348 lines (316 loc) · 10.1 KB
/
layer.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
package claircore
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"github.com/quay/claircore/pkg/tarfs"
)
// LayerDescription is a description of a container layer. It should contain
// enough information to fetch the layer.
//
// Unlike the [Layer] type, this type does not have any extra state or access to
// the contents of the layer.
type LayerDescription struct {
// Digest is a content addressable checksum uniquely identifying this layer.
Digest string
// URI is a URI that can be used to fetch layer contents.
URI string
// MediaType is the [OCI Layer media type] for this layer. Any [Indexer]
// instance will support the OCI-defined media types, and may support others
// based on its configuration.
//
// [OCI Layer media type]: https://github.com/opencontainers/image-spec/blob/main/layer.md
MediaType string
// Headers is additional request headers for fetching layer contents.
Headers map[string][]string
}
// Layer is an internal representation of a container image file system layer.
// Layers are stacked on top of each other to create the final file system of
// the container image.
//
// This type being in the external API of the
// [github.com/quay/claircore/libindex.Libindex] type is a historical accident.
//
// Previously, it was OK to use Layer literals. This is no longer allowed and
// the [Layer.Init] method must be called. Any methods besides [Layer.Init]
// called on an uninitialized Layer will report errors and may panic.
type Layer struct {
noCopy noCopy
// Final is used to implement the panicking Finalizer.
//
// Without a unique allocation, we cannot set a Finalizer (so, when filling
// in a Layer in a slice). A pointer to a zero-sized type (like a *struct{})
// is not unique. So this camps on a unique heap allocation made for the
// purpose of tracking the Closed state of this Layer.
final *string
// Hash is a content addressable hash uniquely identifying this layer.
// Libindex will treat layers with this same hash as identical.
Hash Digest `json:"hash"`
// URI is a URI that can be used to fetch layer contents.
//
// Deprecated: This is exported for historical reasons and may stop being
// populated in the future.
URI string `json:"uri"`
// Headers is additional request headers for fetching layer contents.
//
// Deprecated: This is exported for historical reasons and may stop being
// populated in the future.
Headers map[string][]string `json:"headers"`
cleanup []io.Closer
sys fs.FS
rd io.ReaderAt
closed bool // Used to catch double-closes.
init bool // Used to track initialization.
}
// Init initializes a Layer in-place. This is provided for flexibility when
// constructing a slice of Layers.
func (l *Layer) Init(ctx context.Context, desc *LayerDescription, r io.ReaderAt) error {
if l.init {
return fmt.Errorf("claircore: Init called on already initialized Layer")
}
var err error
l.Hash, err = ParseDigest(desc.Digest)
if err != nil {
return err
}
l.URI = desc.URI
l.Headers = desc.Headers
l.rd = r
defer func() {
if l.init {
return
}
for _, f := range l.cleanup {
f.Close()
}
}()
switch desc.MediaType {
case `application/vnd.oci.image.layer.v1.tar`,
`application/vnd.oci.image.layer.v1.tar+gzip`,
`application/vnd.oci.image.layer.v1.tar+zstd`,
`application/vnd.oci.image.layer.nondistributable.v1.tar`,
`application/vnd.oci.image.layer.nondistributable.v1.tar+gzip`,
`application/vnd.oci.image.layer.nondistributable.v1.tar+zstd`:
sys, err := tarfs.New(r)
switch {
case errors.Is(err, nil):
default:
return fmt.Errorf("claircore: layer %v: unable to create fs.FS: %w", desc.Digest, err)
}
l.sys = sys
case `application/vnd.claircore.filesystem`:
if desc.URI == "" {
return fmt.Errorf("claircore: layer %v: unable to create fs.FS: no URI provided", desc.Digest)
}
sys := os.DirFS(desc.URI)
l.sys = sys
l.rd = nil // The reader cannot be used for a filesystem
default:
return fmt.Errorf("claircore: layer %v: unknown MediaType %q", desc.Digest, desc.MediaType)
}
_, file, line, _ := runtime.Caller(1)
fmsg := fmt.Sprintf("%s:%d: Layer not closed", file, line)
l.final = &fmsg
runtime.SetFinalizer(l.final, func(msg *string) { panic(*msg) })
l.init = true
return nil
}
// Close releases held resources by this Layer.
//
// Not calling Close may cause the program to panic.
func (l *Layer) Close() error {
if !l.init {
return errors.New("claircore: Close: uninitialized Layer")
}
if l.closed {
_, file, line, _ := runtime.Caller(1)
panic(fmt.Sprintf("%s:%d: Layer closed twice", file, line))
}
runtime.SetFinalizer(l.final, nil)
l.closed = true
errs := make([]error, len(l.cleanup))
for i, c := range l.cleanup {
errs[i] = c.Close()
}
return errors.Join(errs...)
}
// SetLocal is a namespacing wart.
//
// Deprecated: This function unconditionally errors and does nothing. Use the
// [Layer.Init] method instead.
func (l *Layer) SetLocal(_ string) error {
// TODO(hank) Just wrap errors.ErrUnsupported when updating to go1.21
return errUnsupported
}
type unsupported struct{}
var errUnsupported = &unsupported{}
func (*unsupported) Error() string {
return "unsupported operation"
}
func (*unsupported) Is(tgt error) bool {
// Hack for forwards compatibility: In go1.21, [errors.ErrUnsupported] was
// added and ideally we'd just use that. However, we're supporting go1.20
// until it's out of upstream support. This hack will make constructions
// like:
//
// errors.Is(err, errors.ErrUnsupported)
//
// work as soon as a main module is built with go1.21.
return tgt.Error() == "unsupported operation"
}
// Fetched reports whether the layer blob has been fetched locally.
//
// Deprecated: Layers should now only be constructed by the code that does the
// fetching. That is, merely having a valid Layer indicates that the blob has
// been fetched.
func (l *Layer) Fetched() bool {
return l.init
}
// FS returns an [fs.FS] reading from an initialized layer.
func (l *Layer) FS() (fs.FS, error) {
if !l.init {
return nil, errors.New("claircore: unable to return FS: uninitialized Layer")
}
return l.sys, nil
}
// Reader returns a [ReadAtCloser] of the layer.
//
// It should also implement [io.Seeker], and should be a tar stream.
func (l *Layer) Reader() (ReadAtCloser, error) {
if !l.init {
return nil, errors.New("claircore: unable to return Reader: uninitialized Layer")
}
if l.rd == nil {
return nil, errors.New("claircore: unable to return Reader: ReaderAt is nil")
}
// Some hacks for making the returned ReadAtCloser implements as many
// interfaces as possible.
switch rd := l.rd.(type) {
case *os.File:
fi, err := rd.Stat()
if err != nil {
return nil, fmt.Errorf("claircore: unable to stat file: %w", err)
}
return &fileAdapter{
SectionReader: io.NewSectionReader(rd, 0, fi.Size()),
File: rd,
}, nil
default:
}
// Doing this with no size breaks the "seek to the end trick".
//
// This could do additional interface testing to support the various sizing
// tricks we do elsewhere.
return &rac{io.NewSectionReader(l.rd, 0, -1)}, nil
}
// Rac implements [io.Closer] on an [io.SectionReader].
type rac struct {
*io.SectionReader
}
// Close implements [io.Closer].
func (*rac) Close() error {
return nil
}
// FileAdapter implements [ReadAtCloser] in such a way that most of the File's
// methods are promoted, but the [io.ReaderAt] and [io.ReadSeeker] interfaces
// are dispatched to the [io.SectionReader] so that there's an independent
// cursor.
type fileAdapter struct {
*io.SectionReader
*os.File
}
// Read implements [io.Reader].
func (a *fileAdapter) Read(p []byte) (n int, err error) {
return a.SectionReader.Read(p)
}
// ReadAt implements [io.ReaderAt].
func (a *fileAdapter) ReadAt(p []byte, off int64) (n int, err error) {
return a.SectionReader.ReadAt(p, off)
}
// Seek implements [io.Seeker].
func (a *fileAdapter) Seek(offset int64, whence int) (int64, error) {
return a.SectionReader.Seek(offset, whence)
}
// Close implements [io.Closer].
func (*fileAdapter) Close() error {
return nil
}
// ReadAtCloser is an [io.ReadCloser] and also an [io.ReaderAt].
type ReadAtCloser interface {
io.ReadCloser
io.ReaderAt
}
// NormalizeIn is used to make sure paths are tar-root relative.
func normalizeIn(in, p string) string {
p = filepath.Clean(p)
if !filepath.IsAbs(p) {
p = filepath.Join(in, p)
}
if filepath.IsAbs(p) {
p = p[1:]
}
return p
}
// ErrNotFound is returned by [Layer.Files] if none of the requested files are
// found.
//
// Deprecated: The [Layer.Files] method is deprecated.
var ErrNotFound = errors.New("claircore: unable to find any requested files")
// Files retrieves specific files from the layer's tar archive.
//
// An error is returned only if none of the requested files are found.
//
// The returned map may contain more entries than the number of paths requested.
// All entries in the map are keyed by paths that are relative to the tar-root.
// For example, requesting paths of "/etc/os-release", "./etc/os-release", and
// "etc/os-release" will all result in any found content being stored with the
// key "etc/os-release".
//
// Deprecated: Callers should instead use [fs.WalkDir] with the [fs.FS] returned
// by [Layer.FS].
func (l *Layer) Files(paths ...string) (map[string]*bytes.Buffer, error) {
// Clean the input paths.
want := make(map[string]struct{})
for i, p := range paths {
p := normalizeIn("/", p)
paths[i] = p
want[p] = struct{}{}
}
f := make(map[string]*bytes.Buffer)
// Walk the fs. ReadFile will handle symlink resolution.
if err := fs.WalkDir(l.sys, ".", func(p string, d fs.DirEntry, err error) error {
switch {
case err != nil:
return err
case d.IsDir():
return nil
}
if _, ok := want[p]; !ok {
return nil
}
delete(want, p)
b, err := fs.ReadFile(l.sys, p)
if err != nil {
return err
}
f[p] = bytes.NewBuffer(b)
return nil
}); err != nil {
return nil, err
}
// If there's nothing in the "f" map, we didn't find anything.
if len(f) == 0 {
return nil, ErrNotFound
}
return f, nil
}
// NoCopy is a marker to get `go vet` to complain about copying.
type noCopy struct{}
func (noCopy) Lock() {}
func (noCopy) Unlock() {}