| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package metadata |
| 18 |
|
| 19 |
import ( |
| 20 |
"io" |
| 21 |
"os" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 25 |
digest "github.com/opencontainers/go-digest" |
| 26 |
) |
| 27 |
|
| 28 |
// Attr reprensents the attributes of a node. |
| 29 |
type Attr struct { |
| 30 |
// Size, for regular files, is the logical size of the file. |
| 31 |
Size int64 |
| 32 |
|
| 33 |
// ModTime is the modification time of the node. |
| 34 |
ModTime time.Time |
| 35 |
|
| 36 |
// LinkName, for symlinks, is the link target. |
| 37 |
LinkName string |
| 38 |
|
| 39 |
// Mode is the permission and mode bits. |
| 40 |
Mode os.FileMode |
| 41 |
|
| 42 |
// UID is the user ID of the owner. |
| 43 |
UID int |
| 44 |
|
| 45 |
// GID is the group ID of the owner. |
| 46 |
GID int |
| 47 |
|
| 48 |
// DevMajor is the major device number for device. |
| 49 |
DevMajor int |
| 50 |
|
| 51 |
// DevMinor is the major device number for device. |
| 52 |
DevMinor int |
| 53 |
|
| 54 |
// Xattrs are the extended attribute for the node. |
| 55 |
Xattrs map[string][]byte |
| 56 |
|
| 57 |
// NumLink is the number of names pointing to this node. |
| 58 |
NumLink int |
| 59 |
} |
| 60 |
|
| 61 |
// Store reads the provided eStargz blob and creates a metadata reader. |
| 62 |
type Store func(sr *io.SectionReader, opts ...Option) (Reader, error) |
| 63 |
|
| 64 |
// Reader provides access to file metadata of a blob. |
| 65 |
type Reader interface { |
| 66 |
RootID() uint32 |
| 67 |
TOCDigest() digest.Digest |
| 68 |
|
| 69 |
GetOffset(id uint32) (offset int64, err error) |
| 70 |
GetAttr(id uint32) (attr Attr, err error) |
| 71 |
GetChild(pid uint32, base string) (id uint32, attr Attr, err error) |
| 72 |
ForeachChild(id uint32, f func(name string, id uint32, mode os.FileMode) bool) error |
| 73 |
OpenFile(id uint32) (File, error) |
| 74 |
OpenFileWithPreReader(id uint32, preRead func(id uint32, chunkOffset, chunkSize int64, chunkDigest string, r io.Reader) error) (File, error) |
| 75 |
|
| 76 |
Clone(sr *io.SectionReader) (Reader, error) |
| 77 |
Close() error |
| 78 |
} |
| 79 |
|
| 80 |
type File interface { |
| 81 |
ChunkEntryForOffset(offset int64) (off int64, size int64, dgst string, ok bool) |
| 82 |
ReadAt(p []byte, off int64) (n int, err error) |
| 83 |
} |
| 84 |
|
| 85 |
type Decompressor interface { |
| 86 |
estargz.Decompressor |
| 87 |
|
| 88 |
// DecompressTOC decompresses the passed blob and returns a reader of TOC JSON. |
| 89 |
// |
| 90 |
// If tocOffset returned by ParseFooter is < 0, we assume that TOC isn't contained in the blob. |
| 91 |
// Pass nil reader to DecompressTOC then we expect that DecompressTOC acquire TOC from the external |
| 92 |
// location and return it. |
| 93 |
DecompressTOC(io.Reader) (tocJSON io.ReadCloser, err error) |
| 94 |
} |
| 95 |
|
| 96 |
type Options struct { |
| 97 |
TOCOffset int64 |
| 98 |
Telemetry *Telemetry |
| 99 |
Decompressors []Decompressor |
| 100 |
} |
| 101 |
|
| 102 |
// Option is an option to configure the behaviour of reader. |
| 103 |
type Option func(o *Options) error |
| 104 |
|
| 105 |
// WithTOCOffset option specifies the offset of TOC |
| 106 |
func WithTOCOffset(tocOffset int64) Option { |
| 107 |
return func(o *Options) error { |
| 108 |
o.TOCOffset = tocOffset |
| 109 |
return nil |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// WithTelemetry option specifies the telemetry hooks |
| 114 |
func WithTelemetry(telemetry *Telemetry) Option { |
| 115 |
return func(o *Options) error { |
| 116 |
o.Telemetry = telemetry |
| 117 |
return nil |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// WithDecompressors option specifies decompressors to use. |
| 122 |
// Default is gzip-based decompressor. |
| 123 |
func WithDecompressors(decompressors ...Decompressor) Option { |
| 124 |
return func(o *Options) error { |
| 125 |
o.Decompressors = decompressors |
| 126 |
return nil |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// A func which takes start time and records the diff |
| 131 |
type MeasureLatencyHook func(time.Time) |
| 132 |
|
| 133 |
// A struct which defines telemetry hooks. By implementing these hooks you should be able to record |
| 134 |
// the latency metrics of the respective steps of estargz open operation. |
| 135 |
type Telemetry struct { |
| 136 |
GetFooterLatency MeasureLatencyHook // measure time to get stargz footer (in milliseconds) |
| 137 |
GetTocLatency MeasureLatencyHook // measure time to GET TOC JSON (in milliseconds) |
| 138 |
DeserializeTocLatency MeasureLatencyHook // measure time to deserialize TOC JSON (in milliseconds) |
| 139 |
} |