125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package librarysnapshot
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"math"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type Usage struct {
|
|
VersionID string `json:"version_id"`
|
|
LogicalBytes int64 `json:"logical_bytes"`
|
|
UniqueBytes int64 `json:"unique_bytes"`
|
|
SharedBytes int64 `json:"shared_bytes"`
|
|
ExclusiveBytes int64 `json:"exclusive_bytes"`
|
|
}
|
|
|
|
// OpenPart provides archive-owned recovery bytes, never a source-side locator.
|
|
// Verify and rewind the same descriptor, so path replacement cannot swap the
|
|
// verified file for an unchecked dependency between these two operations.
|
|
func (a *Archive) OpenPart(p Part) (io.ReadCloser, error) {
|
|
if !validDigest(p.SHA256) || p.Bytes < 0 || p.Bytes > a.maxBlobBytes {
|
|
return nil, errors.New("invalid recovery blob")
|
|
}
|
|
f, err := regular(a.blobs, p.SHA256, a.maxBlobBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
h := sha256.New()
|
|
n, err := io.Copy(h, io.LimitReader(f, a.maxBlobBytes+1))
|
|
if err != nil || n != p.Bytes || hex.EncodeToString(h.Sum(nil)) != p.SHA256 {
|
|
f.Close()
|
|
return nil, errors.New("corrupt recovery blob")
|
|
}
|
|
if _, err = f.Seek(0, io.SeekStart); err != nil {
|
|
f.Close()
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
// Usage reports content bytes, not filesystem allocation or reclaimed space.
|
|
// It is deliberately read-only: protection/retention and live-job references
|
|
// must be acquired from the coordinator before any future cleanup operation.
|
|
func (a *Archive) Usage() ([]Usage, error) {
|
|
ids, err := a.versionIDs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
type inventory struct {
|
|
usage Usage
|
|
blobs map[string]int64
|
|
}
|
|
inventories := make([]inventory, 0, len(ids))
|
|
refs := map[string]int{}
|
|
for _, id := range ids {
|
|
m, err := a.Load(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
v := inventory{usage: Usage{VersionID: id}, blobs: map[string]int64{}}
|
|
for _, p := range m.Parts {
|
|
if v.usage.LogicalBytes > math.MaxInt64-p.Bytes {
|
|
return nil, errors.New("snapshot usage overflow")
|
|
}
|
|
v.usage.LogicalBytes += p.Bytes
|
|
v.blobs[p.SHA256] = p.Bytes
|
|
}
|
|
for sha := range v.blobs {
|
|
refs[sha]++
|
|
}
|
|
inventories = append(inventories, v)
|
|
}
|
|
after, err := a.versionIDs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !slices.Equal(ids, after) {
|
|
return nil, errors.New("snapshot inventory changed; retry usage read")
|
|
}
|
|
result := make([]Usage, 0, len(inventories))
|
|
for _, v := range inventories {
|
|
for sha, size := range v.blobs {
|
|
v.usage.UniqueBytes += size
|
|
if refs[sha] > 1 {
|
|
v.usage.SharedBytes += size
|
|
} else {
|
|
v.usage.ExclusiveBytes += size
|
|
}
|
|
}
|
|
result = append(result, v.usage)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (a *Archive) versionIDs() ([]string, error) {
|
|
f, err := a.versions.Open(".")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
entries, err := f.ReadDir(-1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]string, 0, len(entries))
|
|
for _, entry := range entries {
|
|
if strings.HasPrefix(entry.Name(), ".pending-") {
|
|
continue
|
|
}
|
|
name := entry.Name()
|
|
id := strings.TrimSuffix(name, ".json")
|
|
if !strings.HasSuffix(name, ".json") || !validDigest(id) || entry.IsDir() {
|
|
return nil, errors.New("unexpected archive version entry")
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
sort.Strings(ids)
|
|
return ids, nil
|
|
}
|