| 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 db |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"bytes" |
| 22 |
"encoding/binary" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"math" |
| 27 |
"os" |
| 28 |
"path" |
| 29 |
"path/filepath" |
| 30 |
"sort" |
| 31 |
"strings" |
| 32 |
"sync" |
| 33 |
"time" |
| 34 |
|
| 35 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 36 |
"github.com/containerd/stargz-snapshotter/metadata" |
| 37 |
"github.com/goccy/go-json" |
| 38 |
"github.com/hashicorp/go-multierror" |
| 39 |
digest "github.com/opencontainers/go-digest" |
| 40 |
"github.com/rs/xid" |
| 41 |
bolt "go.etcd.io/bbolt" |
| 42 |
"golang.org/x/sync/errgroup" |
| 43 |
) |
| 44 |
|
| 45 |
// reader stores filesystem metadata parsed from eStargz to metadata DB |
| 46 |
// and provides methods to read them. |
| 47 |
type reader struct { |
| 48 |
db *bolt.DB |
| 49 |
fsID string |
| 50 |
rootID uint32 |
| 51 |
tocDigest digest.Digest |
| 52 |
sr *io.SectionReader |
| 53 |
|
| 54 |
curID uint32 |
| 55 |
curIDMu sync.Mutex |
| 56 |
initG *errgroup.Group |
| 57 |
|
| 58 |
decompressor metadata.Decompressor |
| 59 |
} |
| 60 |
|
| 61 |
func (r *reader) nextID() (uint32, error) { |
| 62 |
r.curIDMu.Lock() |
| 63 |
defer r.curIDMu.Unlock() |
| 64 |
if r.curID == math.MaxUint32 { |
| 65 |
return 0, fmt.Errorf("sequence id too large") |
| 66 |
} |
| 67 |
r.curID++ |
| 68 |
return r.curID, nil |
| 69 |
} |
| 70 |
|
| 71 |
// NewReader parses an eStargz and stores filesystem metadata to |
| 72 |
// the provided DB. |
| 73 |
func NewReader(db *bolt.DB, sr *io.SectionReader, opts ...metadata.Option) (metadata.Reader, error) { |
| 74 |
var rOpts metadata.Options |
| 75 |
for _, o := range opts { |
| 76 |
if err := o(&rOpts); err != nil { |
| 77 |
return nil, fmt.Errorf("failed to apply option: %w", err) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
gzipCompressors := []metadata.Decompressor{new(estargz.GzipDecompressor), new(estargz.LegacyGzipDecompressor)} |
| 82 |
decompressors := append(gzipCompressors, rOpts.Decompressors...) |
| 83 |
|
| 84 |
// Determine the size to fetch. Try to fetch as many bytes as possible. |
| 85 |
fetchSize := maxFooterSize(sr.Size(), decompressors...) |
| 86 |
if maybeTocOffset := rOpts.TOCOffset; maybeTocOffset > fetchSize { |
| 87 |
if maybeTocOffset > sr.Size() { |
| 88 |
return nil, fmt.Errorf("blob size %d is smaller than the toc offset", sr.Size()) |
| 89 |
} |
| 90 |
fetchSize = sr.Size() - maybeTocOffset |
| 91 |
} |
| 92 |
|
| 93 |
start := time.Now() // before getting layer footer |
| 94 |
footer := make([]byte, fetchSize) |
| 95 |
if _, err := sr.ReadAt(footer, sr.Size()-fetchSize); err != nil { |
| 96 |
return nil, fmt.Errorf("error reading footer: %v", err) |
| 97 |
} |
| 98 |
if rOpts.Telemetry != nil && rOpts.Telemetry.GetFooterLatency != nil { |
| 99 |
rOpts.Telemetry.GetFooterLatency(start) |
| 100 |
} |
| 101 |
|
| 102 |
var allErr error |
| 103 |
var tocR io.ReadCloser |
| 104 |
var decompressor metadata.Decompressor |
| 105 |
for _, d := range decompressors { |
| 106 |
fSize := d.FooterSize() |
| 107 |
fOffset := positive(int64(len(footer)) - fSize) |
| 108 |
maybeTocBytes := footer[:fOffset] |
| 109 |
_, tocOffset, tocSize, err := d.ParseFooter(footer[fOffset:]) |
| 110 |
if err != nil { |
| 111 |
allErr = multierror.Append(allErr, err) |
| 112 |
continue |
| 113 |
} |
| 114 |
if tocOffset >= 0 && tocSize <= 0 { |
| 115 |
tocSize = sr.Size() - tocOffset - fSize |
| 116 |
} |
| 117 |
if tocOffset >= 0 && tocSize < int64(len(maybeTocBytes)) { |
| 118 |
maybeTocBytes = maybeTocBytes[:tocSize] |
| 119 |
} |
| 120 |
tocR, err = decompressTOC(d, sr, tocOffset, tocSize, maybeTocBytes, rOpts) |
| 121 |
if err != nil { |
| 122 |
allErr = multierror.Append(allErr, err) |
| 123 |
continue |
| 124 |
} |
| 125 |
decompressor = d |
| 126 |
break |
| 127 |
} |
| 128 |
if tocR == nil { |
| 129 |
if allErr == nil { |
| 130 |
return nil, fmt.Errorf("failed to get the reader of TOC: unknown") |
| 131 |
} |
| 132 |
return nil, fmt.Errorf("failed to get the reader of TOC: %w", allErr) |
| 133 |
} |
| 134 |
defer tocR.Close() |
| 135 |
r := &reader{sr: sr, db: db, initG: new(errgroup.Group), decompressor: decompressor} |
| 136 |
if err := r.init(tocR, rOpts); err != nil { |
| 137 |
return nil, fmt.Errorf("failed to initialize matadata: %w", err) |
| 138 |
} |
| 139 |
return r, nil |
| 140 |
} |
| 141 |
|
| 142 |
func maxFooterSize(blobSize int64, decompressors ...metadata.Decompressor) (res int64) { |
| 143 |
for _, d := range decompressors { |
| 144 |
if s := d.FooterSize(); res < s && s <= blobSize { |
| 145 |
res = s |
| 146 |
} |
| 147 |
} |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
func decompressTOC(d metadata.Decompressor, sr *io.SectionReader, tocOff, tocSize int64, tocBytes []byte, opts metadata.Options) (io.ReadCloser, error) { |
| 152 |
if tocOff < 0 { |
| 153 |
// This means that TOC isn't contained in the blob. |
| 154 |
// We pass nil reader to DecompressTOC and expect that it acquires TOC from |
| 155 |
// the external location. |
| 156 |
start := time.Now() |
| 157 |
tocR, err := d.DecompressTOC(nil) |
| 158 |
if err != nil { |
| 159 |
return nil, err |
| 160 |
} |
| 161 |
if opts.Telemetry != nil && opts.Telemetry.GetTocLatency != nil { |
| 162 |
opts.Telemetry.GetTocLatency(start) |
| 163 |
} |
| 164 |
return tocR, nil |
| 165 |
} |
| 166 |
if len(tocBytes) > 0 { |
| 167 |
start := time.Now() // before getting TOC |
| 168 |
tocR, err := d.DecompressTOC(bytes.NewReader(tocBytes)) |
| 169 |
if err == nil { |
| 170 |
if opts.Telemetry != nil && opts.Telemetry.GetTocLatency != nil { |
| 171 |
opts.Telemetry.GetTocLatency(start) |
| 172 |
} |
| 173 |
return tocR, nil |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
start := time.Now() // before getting TOC |
| 178 |
tocBytes = make([]byte, tocSize) |
| 179 |
if _, err := sr.ReadAt(tocBytes, tocOff); err != nil { |
| 180 |
return nil, fmt.Errorf("error reading %d byte TOC targz: %v", len(tocBytes), err) |
| 181 |
} |
| 182 |
r, err := d.DecompressTOC(bytes.NewReader(tocBytes)) |
| 183 |
if err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
if opts.Telemetry != nil && opts.Telemetry.GetTocLatency != nil { |
| 187 |
opts.Telemetry.GetTocLatency(start) |
| 188 |
} |
| 189 |
return r, nil |
| 190 |
} |
| 191 |
|
| 192 |
// RootID returns ID of the root node. |
| 193 |
func (r *reader) RootID() uint32 { |
| 194 |
return r.rootID |
| 195 |
} |
| 196 |
|
| 197 |
func (r *reader) TOCDigest() digest.Digest { |
| 198 |
return r.tocDigest |
| 199 |
} |
| 200 |
|
| 201 |
// Clone returns a new reader identical to the current reader |
| 202 |
// but uses the provided section reader for retrieving file paylaods. |
| 203 |
func (r *reader) Clone(sr *io.SectionReader) (metadata.Reader, error) { |
| 204 |
if err := r.waitInit(); err != nil { |
| 205 |
return nil, err |
| 206 |
} |
| 207 |
return &reader{ |
| 208 |
db: r.db, |
| 209 |
fsID: r.fsID, |
| 210 |
rootID: r.rootID, |
| 211 |
sr: sr, |
| 212 |
initG: new(errgroup.Group), |
| 213 |
decompressor: r.decompressor, |
| 214 |
}, nil |
| 215 |
} |
| 216 |
|
| 217 |
func (r *reader) init(decompressedR io.Reader, rOpts metadata.Options) (retErr error) { |
| 218 |
start := time.Now() // before parsing TOC JSON |
| 219 |
|
| 220 |
// Initialize root node |
| 221 |
var ok bool |
| 222 |
for i := 0; i < 100; i++ { |
| 223 |
fsID := xid.New().String() |
| 224 |
if err := r.initRootNode(fsID); err != nil { |
| 225 |
if errors.Is(err, bolt.ErrBucketExists) { |
| 226 |
continue // try with another id |
| 227 |
} |
| 228 |
return fmt.Errorf("failed to initialize root node %q: %w", fsID, err) |
| 229 |
} |
| 230 |
ok = true |
| 231 |
break |
| 232 |
} |
| 233 |
if !ok { |
| 234 |
return fmt.Errorf("failed to get a unique id for metadata reader") |
| 235 |
} |
| 236 |
|
| 237 |
f, err := os.CreateTemp("", "") |
| 238 |
if err != nil { |
| 239 |
return err |
| 240 |
} |
| 241 |
closeFunc := func() (closeErr error) { |
| 242 |
name := f.Name() |
| 243 |
if err := f.Close(); err != nil { |
| 244 |
closeErr = multierror.Append(closeErr, err) |
| 245 |
} |
| 246 |
if err := os.Remove(name); err != nil { |
| 247 |
closeErr = multierror.Append(closeErr, err) |
| 248 |
} |
| 249 |
return |
| 250 |
} |
| 251 |
defer func() { |
| 252 |
if retErr != nil { |
| 253 |
if err := closeFunc(); err != nil { |
| 254 |
retErr = multierror.Append(retErr, err) |
| 255 |
} |
| 256 |
} |
| 257 |
}() |
| 258 |
if err := f.Chmod(0600); err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
dgstr := digest.Canonical.Digester() |
| 262 |
if _, err := io.Copy(f, io.TeeReader(decompressedR, dgstr.Hash())); err != nil { |
| 263 |
return fmt.Errorf("failed to read TOC: %w", err) |
| 264 |
} |
| 265 |
r.tocDigest = dgstr.Digest() |
| 266 |
|
| 267 |
// Initialize file metadata in background. All operations refer to these metadata must wait |
| 268 |
// until this initialization ends. |
| 269 |
r.initG.Go(func() error { |
| 270 |
defer closeFunc() |
| 271 |
if _, err := f.Seek(0, io.SeekStart); err != nil { |
| 272 |
return err |
| 273 |
} |
| 274 |
if err := r.initNodes(f); err != nil { |
| 275 |
return err |
| 276 |
} |
| 277 |
if rOpts.Telemetry != nil && rOpts.Telemetry.DeserializeTocLatency != nil { |
| 278 |
rOpts.Telemetry.DeserializeTocLatency(start) |
| 279 |
} |
| 280 |
return nil |
| 281 |
}) |
| 282 |
return nil |
| 283 |
} |
| 284 |
|
| 285 |
func (r *reader) initRootNode(fsID string) error { |
| 286 |
return r.db.Batch(func(tx *bolt.Tx) (err error) { |
| 287 |
filesystems, err := tx.CreateBucketIfNotExists(bucketKeyFilesystems) |
| 288 |
if err != nil { |
| 289 |
return err |
| 290 |
} |
| 291 |
lbkt, err := filesystems.CreateBucket([]byte(fsID)) |
| 292 |
if err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
r.fsID = fsID |
| 296 |
if _, err := lbkt.CreateBucket(bucketKeyMetadata); err != nil { |
| 297 |
return err |
| 298 |
} |
| 299 |
if _, err := lbkt.CreateBucket(bucketKeyStream); err != nil { |
| 300 |
return err |
| 301 |
} |
| 302 |
nodes, err := lbkt.CreateBucket(bucketKeyNodes) |
| 303 |
if err != nil { |
| 304 |
return err |
| 305 |
} |
| 306 |
rootID, err := r.nextID() |
| 307 |
if err != nil { |
| 308 |
return err |
| 309 |
} |
| 310 |
rootBucket, err := nodes.CreateBucket(encodeID(rootID)) |
| 311 |
if err != nil { |
| 312 |
return err |
| 313 |
} |
| 314 |
if err := writeAttr(rootBucket, &metadata.Attr{ |
| 315 |
Mode: os.ModeDir | 0755, |
| 316 |
NumLink: 2, // The directory itself(.) and the parent link to this directory. |
| 317 |
}); err != nil { |
| 318 |
return err |
| 319 |
} |
| 320 |
r.rootID = rootID |
| 321 |
return err |
| 322 |
}) |
| 323 |
} |
| 324 |
|
| 325 |
func (r *reader) initNodes(tr io.Reader) error { |
| 326 |
dec := json.NewDecoder(tr) |
| 327 |
for { |
| 328 |
t, err := dec.Token() |
| 329 |
if err != nil { |
| 330 |
return fmt.Errorf("failed to get JSON token: %w", err) |
| 331 |
} |
| 332 |
if ele, ok := t.(string); ok { |
| 333 |
if ele == "version" { |
| 334 |
continue |
| 335 |
} |
| 336 |
if ele == "entries" { |
| 337 |
continue |
| 338 |
} |
| 339 |
} |
| 340 |
if de, ok := t.(json.Delim); ok { |
| 341 |
if de.String() == "[" { |
| 342 |
break |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
md := make(map[uint32]*metadataEntry) |
| 347 |
st := make(map[int64]map[int64]uint32) |
| 348 |
if err := r.db.Batch(func(tx *bolt.Tx) (err error) { |
| 349 |
nodes, err := getNodes(tx, r.fsID) |
| 350 |
if err != nil { |
| 351 |
return err |
| 352 |
} |
| 353 |
nodes.FillPercent = 1.0 // we only do sequential write to this bucket |
| 354 |
var wantNextOffsetID []uint32 |
| 355 |
var lastEntBucketID uint32 |
| 356 |
var lastEntSize int64 |
| 357 |
var attr metadata.Attr |
| 358 |
var ent estargz.TOCEntry |
| 359 |
for dec.More() { |
| 360 |
resetEnt(&ent) |
| 361 |
if err := dec.Decode(&ent); err != nil { |
| 362 |
return err |
| 363 |
} |
| 364 |
ent.Name = cleanEntryName(ent.Name) |
| 365 |
if ent.Type == "chunk" { |
| 366 |
if lastEntBucketID == 0 { |
| 367 |
return fmt.Errorf("chunk entry must not be the topmost") |
| 368 |
} |
| 369 |
if ent.ChunkSize == 0 { // last chunk in this file |
| 370 |
ent.ChunkSize = lastEntSize - ent.ChunkOffset |
| 371 |
} |
| 372 |
} |
| 373 |
if ent.ChunkSize == 0 && ent.Size != 0 { |
| 374 |
ent.ChunkSize = ent.Size |
| 375 |
} |
| 376 |
if ent.Type != "chunk" { |
| 377 |
var id uint32 |
| 378 |
var b *bolt.Bucket |
| 379 |
if ent.Type == "hardlink" { |
| 380 |
id, err = getIDByName(md, ent.LinkName, r.rootID) |
| 381 |
if err != nil { |
| 382 |
return fmt.Errorf("%q is a hardlink but cannot get link destination %q: %w", ent.Name, ent.LinkName, err) |
| 383 |
} |
| 384 |
b, err = getNodeBucketByID(nodes, id) |
| 385 |
if err != nil { |
| 386 |
return fmt.Errorf("cannot get hardlink destination %q ==> %q (%d): %w", ent.Name, ent.LinkName, id, err) |
| 387 |
} |
| 388 |
numLink, _ := binary.Varint(b.Get(bucketKeyNumLink)) |
| 389 |
if err := putInt(b, bucketKeyNumLink, numLink+1); err != nil { |
| 390 |
return fmt.Errorf("cannot put NumLink of %q ==> %q: %w", ent.Name, ent.LinkName, err) |
| 391 |
} |
| 392 |
} else { |
| 393 |
// Write node bucket |
| 394 |
var found bool |
| 395 |
if ent.Type == "dir" { |
| 396 |
// Check if this directory is already created, if so overwrite it. |
| 397 |
id, err = getIDByName(md, ent.Name, r.rootID) |
| 398 |
if err == nil { |
| 399 |
b, err = getNodeBucketByID(nodes, id) |
| 400 |
if err != nil { |
| 401 |
return fmt.Errorf("failed to get directory bucket %d: %w", id, err) |
| 402 |
} |
| 403 |
found = true |
| 404 |
ent.NumLink = readNumLink(b) |
| 405 |
} |
| 406 |
} |
| 407 |
if !found { |
| 408 |
// No existing node. Create a new one. |
| 409 |
id, err = r.nextID() |
| 410 |
if err != nil { |
| 411 |
return err |
| 412 |
} |
| 413 |
b, err = nodes.CreateBucket(encodeID(id)) |
| 414 |
if err != nil { |
| 415 |
return err |
| 416 |
} |
| 417 |
ent.NumLink = 1 // at least the parent dir references this directory. |
| 418 |
if ent.Type == "dir" { |
| 419 |
ent.NumLink++ // at least "." references this directory. |
| 420 |
} |
| 421 |
} |
| 422 |
if err := writeAttr(b, attrFromTOCEntry(&ent, &attr)); err != nil { |
| 423 |
return fmt.Errorf("failed to set attr to %d(%q): %w", id, ent.Name, err) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
pdirName := parentDir(ent.Name) |
| 428 |
pid, pb, err := r.getOrCreateDir(nodes, md, pdirName, r.rootID) |
| 429 |
if err != nil { |
| 430 |
return fmt.Errorf("failed to create parent directory %q of %q: %w", pdirName, ent.Name, err) |
| 431 |
} |
| 432 |
if err := setChild(md, pb, pid, path.Base(ent.Name), id, ent.Type == "dir"); err != nil { |
| 433 |
return err |
| 434 |
} |
| 435 |
|
| 436 |
if ent.Offset > 0 && ent.InnerOffset == 0 && len(wantNextOffsetID) > 0 { |
| 437 |
for _, i := range wantNextOffsetID { |
| 438 |
if md[i] == nil { |
| 439 |
md[i] = &metadataEntry{} |
| 440 |
} |
| 441 |
md[i].nextOffset = ent.Offset |
| 442 |
} |
| 443 |
wantNextOffsetID = nil |
| 444 |
} |
| 445 |
if ent.Type == "reg" && ent.Size > 0 { |
| 446 |
wantNextOffsetID = append(wantNextOffsetID, id) |
| 447 |
} |
| 448 |
|
| 449 |
lastEntSize = ent.Size |
| 450 |
lastEntBucketID = id |
| 451 |
} |
| 452 |
if (ent.Type == "reg" && ent.Size > 0) || (ent.Type == "chunk" && ent.ChunkSize > 0) { |
| 453 |
if md[lastEntBucketID] == nil { |
| 454 |
md[lastEntBucketID] = &metadataEntry{} |
| 455 |
} |
| 456 |
ce := chunkEntry{ent.Offset, ent.ChunkOffset, ent.ChunkSize, ent.ChunkDigest, ent.InnerOffset} |
| 457 |
md[lastEntBucketID].chunks = append(md[lastEntBucketID].chunks, ce) |
| 458 |
if _, ok := st[ent.Offset]; !ok { |
| 459 |
st[ent.Offset] = make(map[int64]uint32) |
| 460 |
} |
| 461 |
st[ent.Offset][ent.InnerOffset] = lastEntBucketID |
| 462 |
} |
| 463 |
} |
| 464 |
if len(wantNextOffsetID) > 0 { |
| 465 |
for _, i := range wantNextOffsetID { |
| 466 |
if md[i] == nil { |
| 467 |
md[i] = &metadataEntry{} |
| 468 |
} |
| 469 |
md[i].nextOffset = r.sr.Size() |
| 470 |
} |
| 471 |
} |
| 472 |
return nil |
| 473 |
}); err != nil { |
| 474 |
return err |
| 475 |
} |
| 476 |
|
| 477 |
for mdK, d := range md { |
| 478 |
for cK, ce := range d.chunks { |
| 479 |
if len(st[ce.offset]) == 1 { |
| 480 |
for ioff := range st[ce.offset] { |
| 481 |
if ioff == 0 { |
| 482 |
// This stream contains only 1 chunk with innerOffset=0. No need to record innerOffsets. |
| 483 |
md[mdK].chunks[cK].innerOffset = -1 // indicates no following chunks in this stream. |
| 484 |
} |
| 485 |
break |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
addendum := make([]struct { |
| 492 |
id []byte |
| 493 |
md *metadataEntry |
| 494 |
}, len(md)) |
| 495 |
i := 0 |
| 496 |
for id, d := range md { |
| 497 |
addendum[i].id, addendum[i].md = encodeID(id), d |
| 498 |
i++ |
| 499 |
} |
| 500 |
sort.Slice(addendum, func(i, j int) bool { |
| 501 |
return bytes.Compare(addendum[i].id, addendum[j].id) < 0 |
| 502 |
}) |
| 503 |
if err := r.db.Batch(func(tx *bolt.Tx) (err error) { |
| 504 |
meta, err := getMetadata(tx, r.fsID) |
| 505 |
if err != nil { |
| 506 |
return err |
| 507 |
} |
| 508 |
meta.FillPercent = 1.0 // we only do sequential write to this bucket |
| 509 |
for _, m := range addendum { |
| 510 |
md, err := meta.CreateBucket(m.id) |
| 511 |
if err != nil { |
| 512 |
return err |
| 513 |
} |
| 514 |
if err := writeMetadataEntry(md, m.md); err != nil { |
| 515 |
return err |
| 516 |
} |
| 517 |
} |
| 518 |
return nil |
| 519 |
}); err != nil { |
| 520 |
return err |
| 521 |
} |
| 522 |
|
| 523 |
addendumStream := make([]struct { |
| 524 |
offset []byte |
| 525 |
st map[int64]uint32 |
| 526 |
}, len(st)) |
| 527 |
i = 0 |
| 528 |
for off, s := range st { |
| 529 |
singleStream := false |
| 530 |
if len(s) == 1 { |
| 531 |
for ioff := range s { |
| 532 |
if ioff == 0 { |
| 533 |
singleStream = true |
| 534 |
} |
| 535 |
break |
| 536 |
} |
| 537 |
} |
| 538 |
if singleStream { |
| 539 |
continue // This stream contains only 1 chunk with innerOffset=0. No need to record. |
| 540 |
} |
| 541 |
offKey, err := encodeInt(off) |
| 542 |
if err != nil { |
| 543 |
return err |
| 544 |
} |
| 545 |
addendumStream[i].offset, addendumStream[i].st = offKey, s |
| 546 |
i++ |
| 547 |
} |
| 548 |
addendumStream = addendumStream[:i] |
| 549 |
if len(addendumStream) > 0 { |
| 550 |
sort.Slice(addendumStream, func(i, j int) bool { |
| 551 |
return bytes.Compare(addendumStream[i].offset, addendumStream[j].offset) < 0 |
| 552 |
}) |
| 553 |
if err := r.db.Batch(func(tx *bolt.Tx) (err error) { |
| 554 |
stream, err := getStream(tx, r.fsID) |
| 555 |
if err != nil { |
| 556 |
return err |
| 557 |
} |
| 558 |
stream.FillPercent = 1.0 // we only do sequential write to this bucket |
| 559 |
for _, s := range addendumStream { |
| 560 |
stbkt, err := stream.CreateBucket(s.offset) |
| 561 |
if err != nil { |
| 562 |
return err |
| 563 |
} |
| 564 |
for innerOffset, nodeid := range s.st { |
| 565 |
iOffKey, err := encodeInt(innerOffset) |
| 566 |
if err != nil { |
| 567 |
return err |
| 568 |
} |
| 569 |
if err := stbkt.Put(iOffKey, encodeID(nodeid)); err != nil { |
| 570 |
return fmt.Errorf("failed to put inner offset info of %d: %w", nodeid, err) |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
return nil |
| 575 |
}); err != nil { |
| 576 |
return err |
| 577 |
} |
| 578 |
} |
| 579 |
return nil |
| 580 |
} |
| 581 |
|
| 582 |
func (r *reader) getOrCreateDir(nodes *bolt.Bucket, md map[uint32]*metadataEntry, d string, rootID uint32) (id uint32, b *bolt.Bucket, err error) { |
| 583 |
id, err = getIDByName(md, d, rootID) |
| 584 |
if err != nil { |
| 585 |
id, err = r.nextID() |
| 586 |
if err != nil { |
| 587 |
return 0, nil, err |
| 588 |
} |
| 589 |
b, err = nodes.CreateBucket(encodeID(id)) |
| 590 |
if err != nil { |
| 591 |
return 0, nil, err |
| 592 |
} |
| 593 |
attr := &metadata.Attr{ |
| 594 |
Mode: os.ModeDir | 0755, |
| 595 |
NumLink: 2, // The directory itself(.) and the parent link to this directory. |
| 596 |
} |
| 597 |
if err := writeAttr(b, attr); err != nil { |
| 598 |
return 0, nil, err |
| 599 |
} |
| 600 |
if d != "" { |
| 601 |
pid, pb, err := r.getOrCreateDir(nodes, md, parentDir(d), rootID) |
| 602 |
if err != nil { |
| 603 |
return 0, nil, err |
| 604 |
} |
| 605 |
if err := setChild(md, pb, pid, path.Base(d), id, true); err != nil { |
| 606 |
return 0, nil, err |
| 607 |
} |
| 608 |
} |
| 609 |
} else { |
| 610 |
b, err = getNodeBucketByID(nodes, id) |
| 611 |
if err != nil { |
| 612 |
return 0, nil, fmt.Errorf("failed to get dir bucket %d: %w", id, err) |
| 613 |
} |
| 614 |
} |
| 615 |
return id, b, nil |
| 616 |
} |
| 617 |
|
| 618 |
func (r *reader) waitInit() error { |
| 619 |
// TODO: add timeout |
| 620 |
if err := r.initG.Wait(); err != nil { |
| 621 |
return fmt.Errorf("initialization failed: %w", err) |
| 622 |
} |
| 623 |
return nil |
| 624 |
} |
| 625 |
|
| 626 |
func (r *reader) view(fn func(tx *bolt.Tx) error) error { |
| 627 |
if err := r.waitInit(); err != nil { |
| 628 |
return err |
| 629 |
} |
| 630 |
return r.db.View(func(tx *bolt.Tx) error { |
| 631 |
return fn(tx) |
| 632 |
}) |
| 633 |
} |
| 634 |
|
| 635 |
func (r *reader) update(fn func(tx *bolt.Tx) error) error { |
| 636 |
if err := r.waitInit(); err != nil { |
| 637 |
return err |
| 638 |
} |
| 639 |
return r.db.Batch(func(tx *bolt.Tx) error { |
| 640 |
return fn(tx) |
| 641 |
}) |
| 642 |
} |
| 643 |
|
| 644 |
// Close closes this reader. This removes underlying filesystem metadata as well. |
| 645 |
func (r *reader) Close() error { |
| 646 |
return r.update(func(tx *bolt.Tx) (err error) { |
| 647 |
filesystems := tx.Bucket(bucketKeyFilesystems) |
| 648 |
if filesystems == nil { |
| 649 |
return nil |
| 650 |
} |
| 651 |
return filesystems.DeleteBucket([]byte(r.fsID)) |
| 652 |
}) |
| 653 |
} |
| 654 |
|
| 655 |
// GetOffset returns an offset of a node. |
| 656 |
func (r *reader) GetOffset(id uint32) (offset int64, _ error) { |
| 657 |
if err := r.view(func(tx *bolt.Tx) error { |
| 658 |
metadataEntries, err := getMetadata(tx, r.fsID) |
| 659 |
if err != nil { |
| 660 |
return fmt.Errorf("metadata bucket of %q not found for searching offset of %d: %w", r.fsID, id, err) |
| 661 |
} |
| 662 |
nodes, err := getNodes(tx, r.fsID) |
| 663 |
if err != nil { |
| 664 |
return err |
| 665 |
} |
| 666 |
b, err := getNodeBucketByID(nodes, id) |
| 667 |
if err != nil { |
| 668 |
return err |
| 669 |
} |
| 670 |
size, _ := binary.Varint(b.Get(bucketKeySize)) |
| 671 |
if md, err := getMetadataBucketByID(metadataEntries, id); err == nil { |
| 672 |
chunks, err := readChunks(md, size) |
| 673 |
if err != nil { |
| 674 |
return err |
| 675 |
} |
| 676 |
if len(chunks) > 0 { |
| 677 |
offset = chunks[0].offset |
| 678 |
} |
| 679 |
} |
| 680 |
return nil |
| 681 |
}); err != nil { |
| 682 |
return 0, err |
| 683 |
} |
| 684 |
return |
| 685 |
} |
| 686 |
|
| 687 |
// GetAttr returns file attribute of specified node. |
| 688 |
func (r *reader) GetAttr(id uint32) (attr metadata.Attr, _ error) { |
| 689 |
if r.rootID == id { // no need to wait for root dir |
| 690 |
if err := r.db.View(func(tx *bolt.Tx) error { |
| 691 |
nodes, err := getNodes(tx, r.fsID) |
| 692 |
if err != nil { |
| 693 |
return fmt.Errorf("nodes bucket of %q not found for sarching attr %d: %w", r.fsID, id, err) |
| 694 |
} |
| 695 |
b, err := getNodeBucketByID(nodes, id) |
| 696 |
if err != nil { |
| 697 |
return fmt.Errorf("failed to get attr bucket %d: %w", id, err) |
| 698 |
} |
| 699 |
return readAttr(b, &attr) |
| 700 |
}); err != nil { |
| 701 |
return metadata.Attr{}, err |
| 702 |
} |
| 703 |
return attr, nil |
| 704 |
} |
| 705 |
if err := r.view(func(tx *bolt.Tx) error { |
| 706 |
nodes, err := getNodes(tx, r.fsID) |
| 707 |
if err != nil { |
| 708 |
return fmt.Errorf("nodes bucket of %q not found for sarching attr %d: %w", r.fsID, id, err) |
| 709 |
} |
| 710 |
b, err := getNodeBucketByID(nodes, id) |
| 711 |
if err != nil { |
| 712 |
return fmt.Errorf("failed to get attr bucket %d: %w", id, err) |
| 713 |
} |
| 714 |
return readAttr(b, &attr) |
| 715 |
}); err != nil { |
| 716 |
return metadata.Attr{}, err |
| 717 |
} |
| 718 |
return |
| 719 |
} |
| 720 |
|
| 721 |
// GetChild returns a child node that has the specified base name. |
| 722 |
func (r *reader) GetChild(pid uint32, base string) (id uint32, attr metadata.Attr, _ error) { |
| 723 |
if err := r.view(func(tx *bolt.Tx) error { |
| 724 |
metadataEntries, err := getMetadata(tx, r.fsID) |
| 725 |
if err != nil { |
| 726 |
return fmt.Errorf("metadata bucket of %q not found for getting child of %d: %w", r.fsID, pid, err) |
| 727 |
} |
| 728 |
md, err := getMetadataBucketByID(metadataEntries, pid) |
| 729 |
if err != nil { |
| 730 |
return fmt.Errorf("failed to get parent metadata %d: %w", pid, err) |
| 731 |
} |
| 732 |
id, err = readChild(md, base) |
| 733 |
if err != nil { |
| 734 |
return fmt.Errorf("failed to read child %q of %d: %w", base, pid, err) |
| 735 |
} |
| 736 |
nodes, err := getNodes(tx, r.fsID) |
| 737 |
if err != nil { |
| 738 |
return fmt.Errorf("nodes bucket of %q not found for getting child of %d: %w", r.fsID, pid, err) |
| 739 |
} |
| 740 |
child, err := getNodeBucketByID(nodes, id) |
| 741 |
if err != nil { |
| 742 |
return fmt.Errorf("failed to get child bucket %d: %w", id, err) |
| 743 |
} |
| 744 |
return readAttr(child, &attr) |
| 745 |
}); err != nil { |
| 746 |
return 0, metadata.Attr{}, err |
| 747 |
} |
| 748 |
return |
| 749 |
} |
| 750 |
|
| 751 |
// ForeachChild calls the specified callback function for each child node. |
| 752 |
// When the callback returns non-nil error, this stops the iteration. |
| 753 |
func (r *reader) ForeachChild(id uint32, f func(name string, id uint32, mode os.FileMode) bool) error { |
| 754 |
type childInfo struct { |
| 755 |
id uint32 |
| 756 |
mode os.FileMode |
| 757 |
} |
| 758 |
children := make(map[string]childInfo) |
| 759 |
if err := r.view(func(tx *bolt.Tx) error { |
| 760 |
metadataEntries, err := getMetadata(tx, r.fsID) |
| 761 |
if err != nil { |
| 762 |
return fmt.Errorf("nodes bucket of %q not found for getting child of %d: %w", r.fsID, id, err) |
| 763 |
} |
| 764 |
md, err := getMetadataBucketByID(metadataEntries, id) |
| 765 |
if err != nil { |
| 766 |
return nil // no child |
| 767 |
} |
| 768 |
|
| 769 |
var nodes *bolt.Bucket |
| 770 |
firstName := md.Get(bucketKeyChildName) |
| 771 |
if len(firstName) != 0 { |
| 772 |
firstID := decodeID(md.Get(bucketKeyChildID)) |
| 773 |
if nodes == nil { |
| 774 |
nodes, err = getNodes(tx, r.fsID) |
| 775 |
if err != nil { |
| 776 |
return fmt.Errorf("nodes bucket of %q not found for getting children of %d: %w", r.fsID, id, err) |
| 777 |
} |
| 778 |
} |
| 779 |
firstChild, err := getNodeBucketByID(nodes, firstID) |
| 780 |
if err != nil { |
| 781 |
return fmt.Errorf("failed to get first child bucket %d: %w", firstID, err) |
| 782 |
} |
| 783 |
mode, _ := binary.Uvarint(firstChild.Get(bucketKeyMode)) |
| 784 |
children[string(firstName)] = childInfo{firstID, os.FileMode(uint32(mode))} |
| 785 |
} |
| 786 |
|
| 787 |
cbkt := md.Bucket(bucketKeyChildrenExtra) |
| 788 |
if cbkt == nil { |
| 789 |
return nil // no child |
| 790 |
} |
| 791 |
if nodes == nil { |
| 792 |
nodes, err = getNodes(tx, r.fsID) |
| 793 |
if err != nil { |
| 794 |
return fmt.Errorf("nodes bucket of %q not found for getting children of %d: %w", r.fsID, id, err) |
| 795 |
} |
| 796 |
} |
| 797 |
return cbkt.ForEach(func(k, v []byte) error { |
| 798 |
id := decodeID(v) |
| 799 |
child, err := getNodeBucketByID(nodes, id) |
| 800 |
if err != nil { |
| 801 |
return fmt.Errorf("failed to get child bucket %d: %w", id, err) |
| 802 |
} |
| 803 |
mode, _ := binary.Uvarint(child.Get(bucketKeyMode)) |
| 804 |
children[string(k)] = childInfo{id, os.FileMode(uint32(mode))} |
| 805 |
return nil |
| 806 |
}) |
| 807 |
}); err != nil { |
| 808 |
return err |
| 809 |
} |
| 810 |
for k, e := range children { |
| 811 |
if !f(k, e.id, e.mode) { |
| 812 |
break |
| 813 |
} |
| 814 |
} |
| 815 |
return nil |
| 816 |
} |
| 817 |
|
| 818 |
// OpenFileWithPreReader returns a section reader of the specified node. |
| 819 |
// When it reads other ranges than required by the returned reader (e.g. when the target range is located in |
| 820 |
// a large chunk with innerOffset), these chunks are passed to the callback so that it can be cached for futural use. |
| 821 |
func (r *reader) OpenFileWithPreReader(id uint32, preRead func(nid uint32, chunkOffset, chunkSize int64, chunkDigest string, r io.Reader) error) (metadata.File, error) { |
| 822 |
return r.openFile(id, preRead) |
| 823 |
} |
| 824 |
|
| 825 |
// OpenFile returns a section reader of the specified node. |
| 826 |
func (r *reader) OpenFile(id uint32) (metadata.File, error) { |
| 827 |
return r.openFile(id, nil) |
| 828 |
} |
| 829 |
|
| 830 |
func (r *reader) openFile(id uint32, preRead func(id uint32, chunkOffset, chunkSize int64, chunkDigest string, r io.Reader) error) (metadata.File, error) { |
| 831 |
var chunks []chunkEntry |
| 832 |
var size int64 |
| 833 |
|
| 834 |
var nextOffset int64 |
| 835 |
if err := r.view(func(tx *bolt.Tx) error { |
| 836 |
nodes, err := getNodes(tx, r.fsID) |
| 837 |
if err != nil { |
| 838 |
return fmt.Errorf("nodes bucket of %q not found for opening %d: %w", r.fsID, id, err) |
| 839 |
} |
| 840 |
b, err := getNodeBucketByID(nodes, id) |
| 841 |
if err != nil { |
| 842 |
return fmt.Errorf("failed to get file bucket %d: %w", id, err) |
| 843 |
} |
| 844 |
size, _ = binary.Varint(b.Get(bucketKeySize)) |
| 845 |
m, _ := binary.Uvarint(b.Get(bucketKeyMode)) |
| 846 |
if !os.FileMode(uint32(m)).IsRegular() { |
| 847 |
return fmt.Errorf("%q is not a regular file", id) |
| 848 |
} |
| 849 |
|
| 850 |
metadataEntries, err := getMetadata(tx, r.fsID) |
| 851 |
if err != nil { |
| 852 |
return fmt.Errorf("metadata bucket of %q not found for opening %d: %w", r.fsID, id, err) |
| 853 |
} |
| 854 |
if md, err := getMetadataBucketByID(metadataEntries, id); err == nil { |
| 855 |
chunks, err = readChunks(md, size) |
| 856 |
if err != nil { |
| 857 |
return fmt.Errorf("failed to get chunks: %w", err) |
| 858 |
} |
| 859 |
nextOffset, _ = binary.Varint(md.Get(bucketKeyNextOffset)) |
| 860 |
} |
| 861 |
return nil |
| 862 |
}); err != nil { |
| 863 |
return nil, err |
| 864 |
} |
| 865 |
fr := &fileReader{ |
| 866 |
r: r, |
| 867 |
size: size, |
| 868 |
ents: chunks, |
| 869 |
nextOffset: nextOffset, |
| 870 |
preRead: preRead, |
| 871 |
} |
| 872 |
return &file{io.NewSectionReader(fr, 0, size), chunks}, nil |
| 873 |
} |
| 874 |
|
| 875 |
type file struct { |
| 876 |
io.ReaderAt |
| 877 |
ents []chunkEntry |
| 878 |
} |
| 879 |
|
| 880 |
func (fr *file) ChunkEntryForOffset(offset int64) (off int64, size int64, dgst string, ok bool) { |
| 881 |
i := sort.Search(len(fr.ents), func(i int) bool { |
| 882 |
e := fr.ents[i] |
| 883 |
return e.chunkOffset >= offset || (offset > e.chunkOffset && offset < e.chunkOffset+e.chunkSize) |
| 884 |
}) |
| 885 |
if i == len(fr.ents) { |
| 886 |
return 0, 0, "", false |
| 887 |
} |
| 888 |
ci := fr.ents[i] |
| 889 |
return ci.chunkOffset, ci.chunkSize, ci.chunkDigest, true |
| 890 |
} |
| 891 |
|
| 892 |
type fileReader struct { |
| 893 |
r *reader |
| 894 |
size int64 |
| 895 |
ents []chunkEntry |
| 896 |
nextOffset int64 |
| 897 |
preRead func(id uint32, chunkOffset, chunkSize int64, chunkDigest string, r io.Reader) error |
| 898 |
} |
| 899 |
|
| 900 |
// ReadAt reads file payload of this file. |
| 901 |
func (fr *fileReader) ReadAt(p []byte, off int64) (n int, err error) { |
| 902 |
if off >= fr.size { |
| 903 |
return 0, io.EOF |
| 904 |
} |
| 905 |
if off < 0 { |
| 906 |
return 0, errors.New("invalid offset") |
| 907 |
} |
| 908 |
|
| 909 |
var ent chunkEntry |
| 910 |
switch len(fr.ents) { |
| 911 |
case 0: |
| 912 |
return 0, errors.New("no chunk is registered") |
| 913 |
case 1: |
| 914 |
ent = fr.ents[0] |
| 915 |
if ent.chunkOffset > off { |
| 916 |
return 0, fmt.Errorf("no chunk coveres offset %d", off) |
| 917 |
} |
| 918 |
default: |
| 919 |
i := sort.Search(len(fr.ents), func(i int) bool { |
| 920 |
return fr.ents[i].chunkOffset > off |
| 921 |
}) |
| 922 |
if i == 0 { |
| 923 |
return 0, fmt.Errorf("no chunk coveres offset %d", off) |
| 924 |
} |
| 925 |
ent = fr.ents[i-1] |
| 926 |
} |
| 927 |
|
| 928 |
compressedBytesRemain := fr.nextOffset - ent.offset |
| 929 |
bufSize := int(2 << 20) |
| 930 |
if bufSize > int(compressedBytesRemain) { |
| 931 |
bufSize = int(compressedBytesRemain) |
| 932 |
} |
| 933 |
|
| 934 |
br := bufio.NewReaderSize(io.NewSectionReader(fr.r.sr, ent.offset, compressedBytesRemain), bufSize) |
| 935 |
if _, err := br.Peek(bufSize); err != nil { |
| 936 |
return 0, fmt.Errorf("failed to peek read file payload: %v", err) |
| 937 |
} |
| 938 |
dr, err := fr.r.decompressor.Reader(br) |
| 939 |
if err != nil { |
| 940 |
return 0, fmt.Errorf("fileReader.ReadAt.decompressor.Reader: %v", err) |
| 941 |
} |
| 942 |
defer dr.Close() |
| 943 |
|
| 944 |
// Stream that doesn't contain multiple chunks is indicated as ent.innerOffset < 0. |
| 945 |
if fr.preRead == nil || ent.innerOffset < 0 { |
| 946 |
base := off - ent.chunkOffset |
| 947 |
if ent.innerOffset > 0 { |
| 948 |
base += ent.innerOffset |
| 949 |
} |
| 950 |
if n, err := io.CopyN(io.Discard, dr, base); n != base || err != nil { |
| 951 |
return 0, fmt.Errorf("discard of %d bytes = %v, %v", base, n, err) |
| 952 |
} |
| 953 |
return io.ReadFull(dr, p) |
| 954 |
} |
| 955 |
|
| 956 |
var innerChunks []chunkEntryWithID |
| 957 |
if err := fr.r.view(func(tx *bolt.Tx) error { |
| 958 |
innerChunks, err = readInnerChunks(tx, fr.r.fsID, ent.offset) |
| 959 |
return err |
| 960 |
}); err != nil { |
| 961 |
return 0, err |
| 962 |
} |
| 963 |
var found bool |
| 964 |
var nr int64 |
| 965 |
var retN int |
| 966 |
var retErr error |
| 967 |
for _, e := range innerChunks { |
| 968 |
// Fully read the previous chunk reader so that the seek position goes at the current chunk offset |
| 969 |
if in, err := io.CopyN(io.Discard, dr, e.innerOffset-nr); err != nil || in != e.innerOffset-nr { |
| 970 |
return 0, fmt.Errorf("discard of remaining %d bytes != %v, %v", e.innerOffset-nr, in, err) |
| 971 |
} |
| 972 |
nr += e.innerOffset - nr |
| 973 |
if e.innerOffset == ent.innerOffset { |
| 974 |
found = true |
| 975 |
base := off - ent.chunkOffset |
| 976 |
if n, err := io.CopyN(io.Discard, dr, base); n != base || err != nil { |
| 977 |
return 0, fmt.Errorf("discard of offset %d bytes != %v, %v", off, n, err) |
| 978 |
} |
| 979 |
retN, retErr = io.ReadFull(dr, p) |
| 980 |
nr += base + int64(retN) |
| 981 |
continue |
| 982 |
} |
| 983 |
cr := &countReader{r: io.LimitReader(dr, e.chunkSize)} |
| 984 |
if err := fr.preRead(e.id, e.chunkOffset, e.chunkSize, e.chunkDigest, cr); err != nil { |
| 985 |
return 0, fmt.Errorf("failed to pre read: %w", err) |
| 986 |
} |
| 987 |
nr += cr.n |
| 988 |
} |
| 989 |
if !found { |
| 990 |
return 0, fmt.Errorf("fileReader.ReadAt: target entry not found") |
| 991 |
} |
| 992 |
return retN, retErr |
| 993 |
} |
| 994 |
|
| 995 |
// TODO: share it with memory pkg |
| 996 |
func attrFromTOCEntry(src *estargz.TOCEntry, dst *metadata.Attr) *metadata.Attr { |
| 997 |
dst.Size = src.Size |
| 998 |
dst.ModTime, _ = time.Parse(time.RFC3339, src.ModTime3339) |
| 999 |
dst.LinkName = src.LinkName |
| 1000 |
dst.Mode = src.Stat().Mode() |
| 1001 |
dst.UID = src.UID |
| 1002 |
dst.GID = src.GID |
| 1003 |
dst.DevMajor = src.DevMajor |
| 1004 |
dst.DevMinor = src.DevMinor |
| 1005 |
dst.Xattrs = src.Xattrs |
| 1006 |
dst.NumLink = src.NumLink |
| 1007 |
return dst |
| 1008 |
} |
| 1009 |
|
| 1010 |
func getIDByName(md map[uint32]*metadataEntry, name string, rootID uint32) (uint32, error) { |
| 1011 |
name = cleanEntryName(name) |
| 1012 |
if name == "" { |
| 1013 |
return rootID, nil |
| 1014 |
} |
| 1015 |
dir, base := filepath.Split(name) |
| 1016 |
pid, err := getIDByName(md, dir, rootID) |
| 1017 |
if err != nil { |
| 1018 |
return 0, err |
| 1019 |
} |
| 1020 |
if md[pid] == nil { |
| 1021 |
return 0, fmt.Errorf("not found metadata of %d", pid) |
| 1022 |
} |
| 1023 |
if md[pid].children == nil { |
| 1024 |
return 0, fmt.Errorf("not found children of %q", pid) |
| 1025 |
} |
| 1026 |
c, ok := md[pid].children[base] |
| 1027 |
if !ok { |
| 1028 |
return 0, fmt.Errorf("not found child %q in %d", base, pid) |
| 1029 |
} |
| 1030 |
return c.id, nil |
| 1031 |
} |
| 1032 |
|
| 1033 |
func setChild(md map[uint32]*metadataEntry, pb *bolt.Bucket, pid uint32, base string, id uint32, isDir bool) error { |
| 1034 |
if md[pid] == nil { |
| 1035 |
md[pid] = &metadataEntry{} |
| 1036 |
} |
| 1037 |
if md[pid].children == nil { |
| 1038 |
md[pid].children = make(map[string]childEntry) |
| 1039 |
} |
| 1040 |
md[pid].children[base] = childEntry{base, id} |
| 1041 |
if isDir { |
| 1042 |
numLink, _ := binary.Varint(pb.Get(bucketKeyNumLink)) |
| 1043 |
if err := putInt(pb, bucketKeyNumLink, numLink+1); err != nil { |
| 1044 |
return fmt.Errorf("cannot add numlink for children: %w", err) |
| 1045 |
} |
| 1046 |
} |
| 1047 |
return nil |
| 1048 |
} |
| 1049 |
|
| 1050 |
func parentDir(p string) string { |
| 1051 |
dir, _ := path.Split(p) |
| 1052 |
return strings.TrimSuffix(dir, "/") |
| 1053 |
} |
| 1054 |
|
| 1055 |
func cleanEntryName(name string) string { |
| 1056 |
// Use path.Clean to consistently deal with path separators across platforms. |
| 1057 |
return strings.TrimPrefix(path.Clean("/"+name), "/") |
| 1058 |
} |
| 1059 |
|
| 1060 |
func resetEnt(ent *estargz.TOCEntry) { |
| 1061 |
ent.Name = "" |
| 1062 |
ent.Type = "" |
| 1063 |
ent.Size = 0 |
| 1064 |
ent.ModTime3339 = "" |
| 1065 |
ent.LinkName = "" |
| 1066 |
ent.Mode = 0 |
| 1067 |
ent.UID = 0 |
| 1068 |
ent.GID = 0 |
| 1069 |
ent.Uname = "" |
| 1070 |
ent.Gname = "" |
| 1071 |
ent.Offset = 0 |
| 1072 |
ent.DevMajor = 0 |
| 1073 |
ent.DevMinor = 0 |
| 1074 |
ent.NumLink = 0 |
| 1075 |
ent.Xattrs = nil |
| 1076 |
ent.Digest = "" |
| 1077 |
ent.ChunkOffset = 0 |
| 1078 |
ent.ChunkSize = 0 |
| 1079 |
ent.ChunkDigest = "" |
| 1080 |
ent.InnerOffset = 0 |
| 1081 |
} |
| 1082 |
|
| 1083 |
func positive(n int64) int64 { |
| 1084 |
if n < 0 { |
| 1085 |
return 0 |
| 1086 |
} |
| 1087 |
return n |
| 1088 |
} |
| 1089 |
|
| 1090 |
func (r *reader) NumOfNodes() (i int, _ error) { |
| 1091 |
if err := r.view(func(tx *bolt.Tx) error { |
| 1092 |
nodes, err := getNodes(tx, r.fsID) |
| 1093 |
if err != nil { |
| 1094 |
return err |
| 1095 |
} |
| 1096 |
return nodes.ForEach(func(k, v []byte) error { |
| 1097 |
b := nodes.Bucket(k) |
| 1098 |
if b == nil { |
| 1099 |
return fmt.Errorf("entry bucket for %q not found", string(k)) |
| 1100 |
} |
| 1101 |
var attr metadata.Attr |
| 1102 |
if err := readAttr(b, &attr); err != nil { |
| 1103 |
return err |
| 1104 |
} |
| 1105 |
i++ |
| 1106 |
return nil |
| 1107 |
}) |
| 1108 |
}); err != nil { |
| 1109 |
return 0, err |
| 1110 |
} |
| 1111 |
return |
| 1112 |
} |
| 1113 |
|
| 1114 |
func (r *reader) NumOfChunks(id uint32) (i int, _ error) { |
| 1115 |
if err := r.view(func(tx *bolt.Tx) error { |
| 1116 |
metadataEntries, err := getMetadata(tx, r.fsID) |
| 1117 |
if err != nil { |
| 1118 |
return err |
| 1119 |
} |
| 1120 |
md, err := getMetadataBucketByID(metadataEntries, id) |
| 1121 |
if err != nil { |
| 1122 |
return err |
| 1123 |
} |
| 1124 |
nodes, err := getNodes(tx, r.fsID) |
| 1125 |
if err != nil { |
| 1126 |
return err |
| 1127 |
} |
| 1128 |
b, err := getNodeBucketByID(nodes, id) |
| 1129 |
if err != nil { |
| 1130 |
return err |
| 1131 |
} |
| 1132 |
size, _ := binary.Varint(b.Get(bucketKeySize)) |
| 1133 |
chunks, err := readChunks(md, size) |
| 1134 |
if err != nil { |
| 1135 |
return err |
| 1136 |
} |
| 1137 |
i = len(chunks) |
| 1138 |
return nil |
| 1139 |
}); err != nil { |
| 1140 |
return 0, err |
| 1141 |
} |
| 1142 |
return |
| 1143 |
} |
| 1144 |
|
| 1145 |
type countReader struct { |
| 1146 |
r io.Reader |
| 1147 |
n int64 |
| 1148 |
} |
| 1149 |
|
| 1150 |
func (cr *countReader) Read(p []byte) (n int, err error) { |
| 1151 |
n, err = cr.r.Read(p) |
| 1152 |
cr.n += int64(n) |
| 1153 |
return |
| 1154 |
} |