2017-07-27 21:46:51 +00:00
|
|
|
package contenthash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
2018-02-02 00:05:24 +00:00
|
|
|
"io"
|
2017-07-27 21:46:51 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
"github.com/BurntSushi/locker"
|
2017-12-17 23:45:12 +00:00
|
|
|
"github.com/containerd/containerd/fs"
|
2017-07-27 21:46:51 +00:00
|
|
|
iradix "github.com/hashicorp/go-immutable-radix"
|
2017-07-31 20:22:35 +00:00
|
|
|
"github.com/hashicorp/golang-lru/simplelru"
|
2017-07-27 21:46:51 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-07-31 20:22:35 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2017-07-27 21:46:51 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tonistiigi/fsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errNotFound = errors.Errorf("not found")
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
var defaultManager *cacheManager
|
|
|
|
var defaultManagerOnce sync.Once
|
|
|
|
|
|
|
|
const keyContentHash = "buildkit.contenthash.v0"
|
|
|
|
|
|
|
|
func getDefaultManager() *cacheManager {
|
|
|
|
defaultManagerOnce.Do(func() {
|
|
|
|
lru, _ := simplelru.NewLRU(20, nil) // error is impossible on positive size
|
|
|
|
defaultManager = &cacheManager{lru: lru, locker: locker.NewLocker()}
|
|
|
|
})
|
|
|
|
return defaultManager
|
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
|
|
|
|
// Layout in the radix tree: Every path is saved by cleaned absolute unix path.
|
|
|
|
// Directories have 2 records, one contains digest for directory header, other
|
|
|
|
// the recursive digest for directory contents. "/dir/" is the record for
|
|
|
|
// header, "/dir" is for contents. For the root node "" (empty string) is the
|
|
|
|
// key for root, "/" for the root header
|
|
|
|
|
|
|
|
func Checksum(ctx context.Context, ref cache.ImmutableRef, path string) (digest.Digest, error) {
|
2017-07-31 20:22:35 +00:00
|
|
|
return getDefaultManager().Checksum(ctx, ref, path)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 22:06:59 +00:00
|
|
|
func GetCacheContext(ctx context.Context, md *metadata.StorageItem) (CacheContext, error) {
|
|
|
|
return getDefaultManager().GetCacheContext(ctx, md)
|
2017-07-31 20:22:35 +00:00
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
|
2017-07-31 22:06:59 +00:00
|
|
|
func SetCacheContext(ctx context.Context, md *metadata.StorageItem, cc CacheContext) error {
|
|
|
|
return getDefaultManager().SetCacheContext(ctx, md, cc)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
type CacheContext interface {
|
|
|
|
Checksum(ctx context.Context, ref cache.Mountable, p string) (digest.Digest, error)
|
|
|
|
HandleChange(kind fsutil.ChangeKind, p string, fi os.FileInfo, err error) error
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Hashed interface {
|
|
|
|
Digest() digest.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheManager struct {
|
2017-07-31 20:22:35 +00:00
|
|
|
locker *locker.Locker
|
|
|
|
lru *simplelru.LRU
|
2017-07-31 22:06:59 +00:00
|
|
|
lruMu sync.Mutex
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) Checksum(ctx context.Context, ref cache.ImmutableRef, p string) (digest.Digest, error) {
|
2017-07-31 22:06:59 +00:00
|
|
|
cc, err := cm.GetCacheContext(ctx, ensureOriginMetadata(ref.Metadata()))
|
2017-07-31 20:22:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
return cc.Checksum(ctx, ref, p)
|
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
|
2017-07-31 22:06:59 +00:00
|
|
|
func (cm *cacheManager) GetCacheContext(ctx context.Context, md *metadata.StorageItem) (CacheContext, error) {
|
|
|
|
cm.locker.Lock(md.ID())
|
|
|
|
cm.lruMu.Lock()
|
|
|
|
v, ok := cm.lru.Get(md.ID())
|
|
|
|
cm.lruMu.Unlock()
|
2017-07-31 20:22:35 +00:00
|
|
|
if ok {
|
2017-07-31 22:06:59 +00:00
|
|
|
cm.locker.Unlock(md.ID())
|
2017-07-31 20:22:35 +00:00
|
|
|
return v.(*cacheContext), nil
|
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
cc, err := newCacheContext(md)
|
2017-07-31 20:22:35 +00:00
|
|
|
if err != nil {
|
2017-07-31 22:06:59 +00:00
|
|
|
cm.locker.Unlock(md.ID())
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
cm.lruMu.Lock()
|
|
|
|
cm.lru.Add(md.ID(), cc)
|
|
|
|
cm.lruMu.Unlock()
|
|
|
|
cm.locker.Unlock(md.ID())
|
2017-07-31 20:22:35 +00:00
|
|
|
return cc, nil
|
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
|
2017-07-31 22:06:59 +00:00
|
|
|
func (cm *cacheManager) SetCacheContext(ctx context.Context, md *metadata.StorageItem, cci CacheContext) error {
|
2017-07-31 20:22:35 +00:00
|
|
|
cc, ok := cci.(*cacheContext)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("invalid cachecontext: %T", cc)
|
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
if md.ID() != cc.md.ID() {
|
|
|
|
cc = &cacheContext{
|
|
|
|
md: md,
|
|
|
|
tree: cci.(*cacheContext).tree,
|
|
|
|
dirtyMap: map[string]struct{}{},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := cc.save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
cm.lruMu.Lock()
|
|
|
|
cm.lru.Add(md.ID(), cc)
|
|
|
|
cm.lruMu.Unlock()
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cacheContext struct {
|
2017-07-31 20:22:35 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
md *metadata.StorageItem
|
|
|
|
tree *iradix.Tree
|
|
|
|
dirty bool // needs to be persisted to disk
|
2017-07-27 21:46:51 +00:00
|
|
|
|
|
|
|
// used in HandleChange
|
|
|
|
txn *iradix.Txn
|
|
|
|
node *iradix.Node
|
|
|
|
dirtyMap map[string]struct{}
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
type mount struct {
|
|
|
|
mountable cache.Mountable
|
|
|
|
mountPath string
|
|
|
|
unmount func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mount) mount(ctx context.Context) (string, error) {
|
|
|
|
if m.mountPath != "" {
|
|
|
|
return m.mountPath, nil
|
|
|
|
}
|
|
|
|
mounts, err := m.mountable.Mount(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mounts)
|
|
|
|
|
|
|
|
mp, err := lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mountPath = mp
|
|
|
|
m.unmount = lm.Unmount
|
|
|
|
return mp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mount) clean() error {
|
|
|
|
if m.mountPath != "" {
|
|
|
|
if err := m.unmount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.mountPath = ""
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCacheContext(md *metadata.StorageItem) (*cacheContext, error) {
|
2017-07-27 21:46:51 +00:00
|
|
|
cc := &cacheContext{
|
2017-07-31 20:22:35 +00:00
|
|
|
md: md,
|
2017-07-27 21:46:51 +00:00
|
|
|
tree: iradix.New(),
|
|
|
|
dirtyMap: map[string]struct{}{},
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
if err := cc.load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cc, nil
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) load() error {
|
|
|
|
dt, err := cc.md.GetExternal(keyContentHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var l CacheRecords
|
|
|
|
if err := l.Unmarshal(dt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txn := cc.tree.Txn()
|
|
|
|
for _, p := range l.Paths {
|
|
|
|
txn.Insert([]byte(p.Path), p.Record)
|
|
|
|
}
|
|
|
|
cc.tree = txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *cacheContext) save() error {
|
|
|
|
cc.mu.Lock()
|
|
|
|
defer cc.mu.Unlock()
|
|
|
|
|
2017-07-31 22:06:59 +00:00
|
|
|
if cc.txn != nil {
|
|
|
|
cc.commitActiveTransaction()
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
|
|
|
|
var l CacheRecords
|
|
|
|
node := cc.tree.Root()
|
|
|
|
node.Walk(func(k []byte, v interface{}) bool {
|
|
|
|
l.Paths = append(l.Paths, &CacheRecordWithPath{
|
|
|
|
Path: string(k),
|
|
|
|
Record: v.(*CacheRecord),
|
|
|
|
})
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
dt, err := l.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cc.md.SetExternal(keyContentHash, dt)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleChange notifies the source about a modification operation
|
|
|
|
func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.FileInfo, err error) (retErr error) {
|
|
|
|
p = path.Join("/", filepath.ToSlash(p))
|
|
|
|
if p == "/" {
|
|
|
|
p = ""
|
|
|
|
}
|
|
|
|
k := []byte(p)
|
|
|
|
|
|
|
|
deleteDir := func(cr *CacheRecord) {
|
2017-07-31 20:22:35 +00:00
|
|
|
if cr.Type == CacheRecordTypeDir {
|
2017-07-27 21:46:51 +00:00
|
|
|
cc.node.WalkPrefix(append(k, []byte("/")...), func(k []byte, v interface{}) bool {
|
|
|
|
cc.txn.Delete(k)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.mu.Lock()
|
2017-07-31 22:06:59 +00:00
|
|
|
defer cc.mu.Unlock()
|
2017-07-27 21:46:51 +00:00
|
|
|
if cc.txn == nil {
|
|
|
|
cc.txn = cc.tree.Txn()
|
|
|
|
cc.node = cc.tree.Root()
|
2017-07-31 22:06:59 +00:00
|
|
|
|
|
|
|
// root is not called by HandleChange. need to fake it
|
|
|
|
if _, ok := cc.node.Get([]byte("/")); !ok {
|
|
|
|
cc.txn.Insert([]byte("/"), &CacheRecord{
|
|
|
|
Type: CacheRecordTypeDirHeader,
|
|
|
|
Digest: digest.FromBytes(nil),
|
|
|
|
})
|
|
|
|
cc.txn.Insert([]byte(""), &CacheRecord{
|
|
|
|
Type: CacheRecordTypeDir,
|
|
|
|
})
|
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
|
2017-07-27 21:46:51 +00:00
|
|
|
if kind == fsutil.ChangeKindDelete {
|
|
|
|
v, ok := cc.txn.Delete(k)
|
|
|
|
if ok {
|
|
|
|
deleteDir(v.(*CacheRecord))
|
|
|
|
}
|
|
|
|
d := path.Dir(string(k))
|
|
|
|
if d == "/" {
|
|
|
|
d = ""
|
|
|
|
}
|
|
|
|
cc.dirtyMap[d] = struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, ok := fi.Sys().(*fsutil.Stat)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("%s invalid change without stat information", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
h, ok := fi.(Hashed)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("invalid fileinfo: %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := cc.node.Get(k)
|
|
|
|
if ok {
|
|
|
|
deleteDir(v.(*CacheRecord))
|
|
|
|
}
|
|
|
|
|
|
|
|
cr := &CacheRecord{
|
2017-07-31 20:22:35 +00:00
|
|
|
Type: CacheRecordTypeFile,
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
2017-07-31 20:22:35 +00:00
|
|
|
cr.Type = CacheRecordTypeSymlink
|
|
|
|
cr.Linkname = filepath.ToSlash(stat.Linkname)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2017-07-31 20:22:35 +00:00
|
|
|
cr.Type = CacheRecordTypeDirHeader
|
2017-07-27 21:46:51 +00:00
|
|
|
cr2 := &CacheRecord{
|
2017-07-31 20:22:35 +00:00
|
|
|
Type: CacheRecordTypeDir,
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
cc.txn.Insert(k, cr2)
|
|
|
|
k = append(k, []byte("/")...)
|
|
|
|
}
|
|
|
|
cr.Digest = h.Digest()
|
|
|
|
cc.txn.Insert(k, cr)
|
|
|
|
d := path.Dir(string(k))
|
|
|
|
if d == "/" {
|
|
|
|
d = ""
|
|
|
|
}
|
|
|
|
cc.dirtyMap[d] = struct{}{}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) Checksum(ctx context.Context, mountable cache.Mountable, p string) (digest.Digest, error) {
|
|
|
|
m := &mount{mountable: mountable}
|
|
|
|
defer m.clean()
|
|
|
|
|
2017-07-27 21:46:51 +00:00
|
|
|
const maxSymlinkLimit = 255
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
if i > maxSymlinkLimit {
|
|
|
|
return "", errors.Errorf("too many symlinks: %s", p)
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
cr, err := cc.checksumNoFollow(ctx, m, p)
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
if cr.Type == CacheRecordTypeSymlink {
|
|
|
|
link := cr.Linkname
|
|
|
|
if !path.IsAbs(cr.Linkname) {
|
2017-07-27 21:46:51 +00:00
|
|
|
link = path.Join(path.Dir(p), link)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
p = link
|
|
|
|
} else {
|
|
|
|
return cr.Digest, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) checksumNoFollow(ctx context.Context, m *mount, p string) (*CacheRecord, error) {
|
2017-07-27 21:46:51 +00:00
|
|
|
p = path.Join("/", filepath.ToSlash(p))
|
|
|
|
if p == "/" {
|
|
|
|
p = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.mu.RLock()
|
|
|
|
if cc.txn == nil {
|
|
|
|
root := cc.tree.Root()
|
|
|
|
cc.mu.RUnlock()
|
|
|
|
v, ok := root.Get([]byte(p))
|
|
|
|
if ok {
|
|
|
|
cr := v.(*CacheRecord)
|
|
|
|
if cr.Digest != "" {
|
|
|
|
return cr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cc.mu.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.mu.Lock()
|
|
|
|
defer cc.mu.Unlock()
|
|
|
|
|
|
|
|
if cc.txn != nil {
|
|
|
|
cc.commitActiveTransaction()
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
defer func() {
|
|
|
|
if cc.dirty {
|
|
|
|
go cc.save()
|
|
|
|
cc.dirty = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cc.lazyChecksum(ctx, m, p)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *cacheContext) commitActiveTransaction() {
|
|
|
|
for d := range cc.dirtyMap {
|
|
|
|
addParentToMap(d, cc.dirtyMap)
|
|
|
|
}
|
|
|
|
for d := range cc.dirtyMap {
|
2017-11-16 21:30:19 +00:00
|
|
|
if _, ok := cc.txn.Get([]byte(d)); ok {
|
|
|
|
cc.txn.Insert([]byte(d), &CacheRecord{Type: CacheRecordTypeDir})
|
|
|
|
}
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
cc.tree = cc.txn.Commit()
|
|
|
|
cc.node = nil
|
|
|
|
cc.dirtyMap = map[string]struct{}{}
|
|
|
|
cc.txn = nil
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) lazyChecksum(ctx context.Context, m *mount, p string) (*CacheRecord, error) {
|
2017-07-27 21:46:51 +00:00
|
|
|
root := cc.tree.Root()
|
|
|
|
if cc.needsScan(root, p) {
|
2017-07-31 20:22:35 +00:00
|
|
|
if err := cc.scanPath(ctx, m, p); err != nil {
|
2017-07-27 21:46:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k := []byte(p)
|
|
|
|
txn := cc.tree.Txn()
|
2017-11-16 21:30:19 +00:00
|
|
|
root = txn.Root()
|
2017-07-31 20:22:35 +00:00
|
|
|
cr, updated, err := cc.checksum(ctx, root, txn, m, k)
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cc.tree = txn.Commit()
|
2017-07-31 20:22:35 +00:00
|
|
|
cc.dirty = updated
|
2017-07-27 21:46:51 +00:00
|
|
|
return cr, err
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node, txn *iradix.Txn, m *mount, k []byte) (*CacheRecord, bool, error) {
|
2017-07-27 21:46:51 +00:00
|
|
|
v, ok := root.Get(k)
|
|
|
|
|
|
|
|
if !ok {
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, false, errors.Wrapf(errNotFound, "%s not found", string(k))
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
cr := v.(*CacheRecord)
|
|
|
|
|
|
|
|
if cr.Digest != "" {
|
2017-07-31 20:22:35 +00:00
|
|
|
return cr, false, nil
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
var dgst digest.Digest
|
|
|
|
|
|
|
|
switch cr.Type {
|
2017-07-31 20:22:35 +00:00
|
|
|
case CacheRecordTypeDir:
|
2017-07-27 21:46:51 +00:00
|
|
|
h := sha256.New()
|
|
|
|
next := append(k, []byte("/")...)
|
2017-07-31 22:06:59 +00:00
|
|
|
iter := root.Seek(next)
|
|
|
|
subk := next
|
|
|
|
ok := true
|
2017-07-27 21:46:51 +00:00
|
|
|
for {
|
2017-07-31 22:06:59 +00:00
|
|
|
if !ok || !bytes.HasPrefix(subk, next) {
|
2017-07-27 21:46:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
h.Write(bytes.TrimPrefix(subk, k))
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
subcr, _, err := cc.checksum(ctx, root, txn, m, subk)
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, false, err
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.Write([]byte(subcr.Digest))
|
2017-07-31 20:22:35 +00:00
|
|
|
if subcr.Type == CacheRecordTypeDir { // skip subfiles
|
2017-07-31 22:06:59 +00:00
|
|
|
next := append(subk, []byte("/\xff")...)
|
|
|
|
iter = root.Seek(next)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
subk, _, ok = iter.Next()
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
dgst = digest.NewDigest(digest.SHA256, h)
|
|
|
|
default:
|
|
|
|
p := string(bytes.TrimSuffix(k, []byte("/")))
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
target, err := m.mount(ctx)
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, false, err
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no FollowSymlinkInScope because invalid paths should not be inserted
|
|
|
|
fp := filepath.Join(target, filepath.FromSlash(p))
|
|
|
|
|
|
|
|
fi, err := os.Lstat(fp)
|
|
|
|
if err != nil {
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, false, err
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dgst, err = prepareDigest(fp, p, fi)
|
|
|
|
if err != nil {
|
2017-07-31 20:22:35 +00:00
|
|
|
return nil, false, err
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cr2 := &CacheRecord{
|
2017-07-31 20:22:35 +00:00
|
|
|
Digest: dgst,
|
|
|
|
Type: cr.Type,
|
|
|
|
Linkname: cr.Linkname,
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txn.Insert(k, cr2)
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
return cr2, true, nil
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *cacheContext) needsScan(root *iradix.Node, p string) bool {
|
|
|
|
if p == "/" {
|
|
|
|
p = ""
|
|
|
|
}
|
|
|
|
if _, ok := root.Get([]byte(p)); !ok {
|
|
|
|
if p == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return cc.needsScan(root, path.Clean(path.Dir(p)))
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string) (retErr error) {
|
2017-07-27 21:46:51 +00:00
|
|
|
p = path.Join("/", p)
|
|
|
|
d, _ := path.Split(p)
|
|
|
|
|
2017-07-31 20:22:35 +00:00
|
|
|
mp, err := m.mount(ctx)
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:45:12 +00:00
|
|
|
parentPath, err := fs.RootPath(mp, filepath.FromSlash(d))
|
2017-07-27 21:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := cc.tree.Root()
|
|
|
|
txn := cc.tree.Txn()
|
|
|
|
|
|
|
|
err = filepath.Walk(parentPath, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to walk %s", path)
|
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(mp, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k := []byte(filepath.Join("/", filepath.ToSlash(rel)))
|
|
|
|
if string(k) == "/" {
|
|
|
|
k = []byte{}
|
|
|
|
}
|
|
|
|
if _, ok := n.Get(k); !ok {
|
|
|
|
cr := &CacheRecord{
|
2017-07-31 20:22:35 +00:00
|
|
|
Type: CacheRecordTypeFile,
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
2017-07-31 20:22:35 +00:00
|
|
|
cr.Type = CacheRecordTypeSymlink
|
2017-07-27 21:46:51 +00:00
|
|
|
link, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-31 20:22:35 +00:00
|
|
|
cr.Linkname = filepath.ToSlash(link)
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2017-07-31 20:22:35 +00:00
|
|
|
cr.Type = CacheRecordTypeDirHeader
|
2017-07-27 21:46:51 +00:00
|
|
|
cr2 := &CacheRecord{
|
2017-07-31 20:22:35 +00:00
|
|
|
Type: CacheRecordTypeDir,
|
2017-07-27 21:46:51 +00:00
|
|
|
}
|
|
|
|
txn.Insert(k, cr2)
|
|
|
|
k = append(k, []byte("/")...)
|
|
|
|
}
|
|
|
|
txn.Insert(k, cr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cc.tree = txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareDigest(fp, p string, fi os.FileInfo) (digest.Digest, error) {
|
|
|
|
h, err := NewFileHash(fp, fi)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to create hash for %s", p)
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() && fi.Size() > 0 {
|
|
|
|
// TODO: would be nice to put the contents to separate hash first
|
|
|
|
// so it can be cached for hardlinks
|
|
|
|
f, err := os.Open(fp)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to open %s", p)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2018-02-02 00:05:24 +00:00
|
|
|
if _, err := poolsCopy(h, f); err != nil {
|
2017-07-27 21:46:51 +00:00
|
|
|
return "", errors.Wrapf(err, "failed to copy file data for %s", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return digest.NewDigest(digest.SHA256, h), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addParentToMap(d string, m map[string]struct{}) {
|
|
|
|
if d == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d = path.Dir(d)
|
|
|
|
if d == "/" {
|
|
|
|
d = ""
|
|
|
|
}
|
|
|
|
m[d] = struct{}{}
|
|
|
|
addParentToMap(d, m)
|
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
|
|
|
|
func ensureOriginMetadata(md *metadata.StorageItem) *metadata.StorageItem {
|
|
|
|
v := md.Get("cache.equalMutable") // TODO: const
|
|
|
|
if v == nil {
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
var mutable string
|
|
|
|
if err := v.Unmarshal(&mutable); err != nil {
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
si, ok := md.Storage().Get(mutable)
|
|
|
|
if ok {
|
2017-08-10 01:20:33 +00:00
|
|
|
return si
|
2017-07-31 22:06:59 +00:00
|
|
|
}
|
|
|
|
return md
|
|
|
|
}
|
2018-02-02 00:05:24 +00:00
|
|
|
|
|
|
|
var pool32K = sync.Pool{
|
|
|
|
New: func() interface{} { return make([]byte, 32*1024) }, // 32K
|
|
|
|
}
|
|
|
|
|
|
|
|
func poolsCopy(dst io.Writer, src io.Reader) (written int64, err error) {
|
|
|
|
buf := pool32K.Get().([]byte)
|
|
|
|
written, err = io.CopyBuffer(dst, src, buf)
|
|
|
|
pool32K.Put(buf)
|
|
|
|
return
|
|
|
|
}
|