| 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 memory |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"math" |
| 23 |
"os" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 27 |
"github.com/containerd/stargz-snapshotter/metadata" |
| 28 |
digest "github.com/opencontainers/go-digest" |
| 29 |
) |
| 30 |
|
| 31 |
type reader struct { |
| 32 |
r *estargz.Reader |
| 33 |
rootID uint32 |
| 34 |
|
| 35 |
idMap map[uint32]*estargz.TOCEntry |
| 36 |
idOfEntry map[string]uint32 |
| 37 |
|
| 38 |
estargzOpts []estargz.OpenOption |
| 39 |
} |
| 40 |
|
| 41 |
func newReader(er *estargz.Reader, rootID uint32, idMap map[uint32]*estargz.TOCEntry, idOfEntry map[string]uint32, estargzOpts []estargz.OpenOption) *reader { |
| 42 |
return &reader{r: er, rootID: rootID, idMap: idMap, idOfEntry: idOfEntry, estargzOpts: estargzOpts} |
| 43 |
} |
| 44 |
|
| 45 |
func NewReader(sr *io.SectionReader, opts ...metadata.Option) (metadata.Reader, error) { |
| 46 |
var rOpts metadata.Options |
| 47 |
for _, o := range opts { |
| 48 |
if err := o(&rOpts); err != nil { |
| 49 |
return nil, fmt.Errorf("failed to apply option: %w", err) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
telemetry := &estargz.Telemetry{} |
| 54 |
if rOpts.Telemetry != nil { |
| 55 |
telemetry.GetFooterLatency = estargz.MeasureLatencyHook(rOpts.Telemetry.GetFooterLatency) |
| 56 |
telemetry.GetTocLatency = estargz.MeasureLatencyHook(rOpts.Telemetry.GetTocLatency) |
| 57 |
telemetry.DeserializeTocLatency = estargz.MeasureLatencyHook(rOpts.Telemetry.DeserializeTocLatency) |
| 58 |
} |
| 59 |
var decompressors []estargz.Decompressor |
| 60 |
for _, d := range rOpts.Decompressors { |
| 61 |
decompressors = append(decompressors, d) |
| 62 |
} |
| 63 |
|
| 64 |
erOpts := []estargz.OpenOption{ |
| 65 |
estargz.WithTOCOffset(rOpts.TOCOffset), |
| 66 |
estargz.WithTelemetry(telemetry), |
| 67 |
estargz.WithDecompressors(decompressors...), |
| 68 |
} |
| 69 |
er, err := estargz.Open(sr, erOpts...) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
root, ok := er.Lookup("") |
| 74 |
if !ok { |
| 75 |
return nil, fmt.Errorf("failed to get root node") |
| 76 |
} |
| 77 |
rootID, idMap, idOfEntry, err := assignIDs(er, root) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
r := newReader(er, rootID, idMap, idOfEntry, erOpts) |
| 82 |
return r, nil |
| 83 |
} |
| 84 |
|
| 85 |
// assignIDs assigns an to each TOC item and returns a mapping from ID to entry and vice-versa. |
| 86 |
func assignIDs(er *estargz.Reader, e *estargz.TOCEntry) (rootID uint32, idMap map[uint32]*estargz.TOCEntry, idOfEntry map[string]uint32, err error) { |
| 87 |
idMap = make(map[uint32]*estargz.TOCEntry) |
| 88 |
idOfEntry = make(map[string]uint32) |
| 89 |
curID := uint32(0) |
| 90 |
|
| 91 |
nextID := func() (uint32, error) { |
| 92 |
if curID == math.MaxUint32 { |
| 93 |
return 0, fmt.Errorf("sequence id too large") |
| 94 |
} |
| 95 |
curID++ |
| 96 |
return curID, nil |
| 97 |
} |
| 98 |
|
| 99 |
var mapChildren func(e *estargz.TOCEntry) (uint32, error) |
| 100 |
mapChildren = func(e *estargz.TOCEntry) (uint32, error) { |
| 101 |
if e.Type == "hardlink" { |
| 102 |
return 0, fmt.Errorf("unexpected type \"hardlink\": this should be replaced to the destination entry") |
| 103 |
} |
| 104 |
|
| 105 |
var ok bool |
| 106 |
id, ok := idOfEntry[e.Name] |
| 107 |
if !ok { |
| 108 |
id, err = nextID() |
| 109 |
if err != nil { |
| 110 |
return 0, err |
| 111 |
} |
| 112 |
idMap[id] = e |
| 113 |
idOfEntry[e.Name] = id |
| 114 |
} |
| 115 |
|
| 116 |
e.ForeachChild(func(_ string, ent *estargz.TOCEntry) bool { |
| 117 |
_, err = mapChildren(ent) |
| 118 |
return err == nil |
| 119 |
}) |
| 120 |
if err != nil { |
| 121 |
return 0, err |
| 122 |
} |
| 123 |
return id, nil |
| 124 |
} |
| 125 |
|
| 126 |
rootID, err = mapChildren(e) |
| 127 |
if err != nil { |
| 128 |
return 0, nil, nil, err |
| 129 |
} |
| 130 |
|
| 131 |
return rootID, idMap, idOfEntry, nil |
| 132 |
} |
| 133 |
|
| 134 |
func (r *reader) RootID() uint32 { |
| 135 |
return r.rootID |
| 136 |
} |
| 137 |
|
| 138 |
func (r *reader) TOCDigest() digest.Digest { |
| 139 |
return r.r.TOCDigest() |
| 140 |
} |
| 141 |
|
| 142 |
func (r *reader) GetOffset(id uint32) (offset int64, err error) { |
| 143 |
e, ok := r.idMap[id] |
| 144 |
if !ok { |
| 145 |
return 0, fmt.Errorf("entry %d not found", id) |
| 146 |
} |
| 147 |
return e.Offset, nil |
| 148 |
} |
| 149 |
|
| 150 |
func (r *reader) GetAttr(id uint32) (attr metadata.Attr, err error) { |
| 151 |
e, ok := r.idMap[id] |
| 152 |
if !ok { |
| 153 |
err = fmt.Errorf("entry %d not found", id) |
| 154 |
return |
| 155 |
} |
| 156 |
// TODO: zero copy |
| 157 |
attrFromTOCEntry(e, &attr) |
| 158 |
return |
| 159 |
} |
| 160 |
|
| 161 |
func (r *reader) GetChild(pid uint32, base string) (id uint32, attr metadata.Attr, err error) { |
| 162 |
e, ok := r.idMap[pid] |
| 163 |
if !ok { |
| 164 |
err = fmt.Errorf("parent entry %d not found", pid) |
| 165 |
return |
| 166 |
} |
| 167 |
child, ok := e.LookupChild(base) |
| 168 |
if !ok { |
| 169 |
err = fmt.Errorf("child %q of entry %d not found", base, pid) |
| 170 |
return |
| 171 |
} |
| 172 |
cid, ok := r.idOfEntry[child.Name] |
| 173 |
if !ok { |
| 174 |
err = fmt.Errorf("id of entry %q not found", base) |
| 175 |
return |
| 176 |
} |
| 177 |
// TODO: zero copy |
| 178 |
attrFromTOCEntry(child, &attr) |
| 179 |
return cid, attr, nil |
| 180 |
} |
| 181 |
|
| 182 |
func (r *reader) ForeachChild(id uint32, f func(name string, id uint32, mode os.FileMode) bool) error { |
| 183 |
e, ok := r.idMap[id] |
| 184 |
if !ok { |
| 185 |
return fmt.Errorf("parent entry %d not found", id) |
| 186 |
} |
| 187 |
var err error |
| 188 |
e.ForeachChild(func(baseName string, ent *estargz.TOCEntry) bool { |
| 189 |
id, ok := r.idOfEntry[ent.Name] |
| 190 |
if !ok { |
| 191 |
err = fmt.Errorf("id of child entry %q not found", baseName) |
| 192 |
return false |
| 193 |
} |
| 194 |
return f(baseName, id, ent.Stat().Mode()) |
| 195 |
}) |
| 196 |
return err |
| 197 |
} |
| 198 |
|
| 199 |
func (r *reader) OpenFile(id uint32) (metadata.File, error) { |
| 200 |
e, ok := r.idMap[id] |
| 201 |
if !ok { |
| 202 |
return nil, fmt.Errorf("entry %d not found", id) |
| 203 |
} |
| 204 |
sr, err := r.r.OpenFile(e.Name) |
| 205 |
if err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
return &file{r, e, sr}, nil |
| 209 |
} |
| 210 |
|
| 211 |
func (r *reader) OpenFileWithPreReader(id uint32, preRead func(id uint32, chunkOffset, chunkSize int64, chunkDigest string, r io.Reader) error) (metadata.File, error) { |
| 212 |
e, ok := r.idMap[id] |
| 213 |
if !ok { |
| 214 |
return nil, fmt.Errorf("entry %d not found", id) |
| 215 |
} |
| 216 |
sr, err := r.r.OpenFileWithPreReader(e.Name, func(e *estargz.TOCEntry, chunkR io.Reader) error { |
| 217 |
cid, ok := r.idOfEntry[e.Name] |
| 218 |
if !ok { |
| 219 |
return fmt.Errorf("id of entry %q not found", e.Name) |
| 220 |
} |
| 221 |
return preRead(cid, e.ChunkOffset, e.ChunkSize, e.ChunkDigest, chunkR) |
| 222 |
}) |
| 223 |
if err != nil { |
| 224 |
return nil, err |
| 225 |
} |
| 226 |
return &file{r, e, sr}, nil |
| 227 |
} |
| 228 |
|
| 229 |
func (r *reader) Clone(sr *io.SectionReader) (metadata.Reader, error) { |
| 230 |
er, err := estargz.Open(sr, r.estargzOpts...) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
|
| 235 |
return newReader(er, r.rootID, r.idMap, r.idOfEntry, r.estargzOpts), nil |
| 236 |
} |
| 237 |
|
| 238 |
func (r *reader) Close() error { |
| 239 |
return nil |
| 240 |
} |
| 241 |
|
| 242 |
type file struct { |
| 243 |
r *reader |
| 244 |
e *estargz.TOCEntry |
| 245 |
sr *io.SectionReader |
| 246 |
} |
| 247 |
|
| 248 |
func (r *file) ChunkEntryForOffset(offset int64) (off int64, size int64, dgst string, ok bool) { |
| 249 |
e, ok := r.r.r.ChunkEntryForOffset(r.e.Name, offset) |
| 250 |
if !ok { |
| 251 |
return 0, 0, "", false |
| 252 |
} |
| 253 |
dgst = e.Digest |
| 254 |
if e.ChunkDigest != "" { |
| 255 |
// NOTE* "reg" also can contain ChunkDigest (e.g. when "reg" is the first entry of |
| 256 |
// chunked file) |
| 257 |
dgst = e.ChunkDigest |
| 258 |
} |
| 259 |
return e.ChunkOffset, e.ChunkSize, dgst, true |
| 260 |
} |
| 261 |
|
| 262 |
func (r *file) ReadAt(p []byte, off int64) (n int, err error) { |
| 263 |
return r.sr.ReadAt(p, off) |
| 264 |
} |
| 265 |
|
| 266 |
func (r *reader) NumOfNodes() (i int, _ error) { |
| 267 |
return len(r.idMap), nil |
| 268 |
} |
| 269 |
|
| 270 |
// TODO: share it with db pkg |
| 271 |
func attrFromTOCEntry(src *estargz.TOCEntry, dst *metadata.Attr) *metadata.Attr { |
| 272 |
dst.Size = src.Size |
| 273 |
dst.ModTime, _ = time.Parse(time.RFC3339, src.ModTime3339) |
| 274 |
dst.LinkName = src.LinkName |
| 275 |
dst.Mode = src.Stat().Mode() |
| 276 |
dst.UID = src.UID |
| 277 |
dst.GID = src.GID |
| 278 |
dst.DevMajor = src.DevMajor |
| 279 |
dst.DevMinor = src.DevMinor |
| 280 |
dst.Xattrs = src.Xattrs |
| 281 |
dst.NumLink = src.NumLink |
| 282 |
return dst |
| 283 |
} |