2018-02-05 16:05:59 -05:00
|
|
|
package remotecontext // import "github.com/docker/docker/builder/remotecontext"
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-05-15 17:54:27 -04:00
|
|
|
"sync"
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-09-22 01:07:45 -04:00
|
|
|
iradix "github.com/hashicorp/go-immutable-radix"
|
2018-05-19 07:38:54 -04:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-05-15 17:54:27 -04:00
|
|
|
"github.com/tonistiigi/fsutil"
|
2015-09-06 13:26:40 -04:00
|
|
|
)
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
type hashed interface {
|
2017-09-22 01:07:45 -04:00
|
|
|
Digest() digest.Digest
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// CachableSource is a source that contains cache records for its contents
|
|
|
|
type CachableSource struct {
|
|
|
|
mu sync.Mutex
|
2017-08-03 20:22:00 -04:00
|
|
|
root containerfs.ContainerFS
|
2017-05-15 17:54:27 -04:00
|
|
|
tree *iradix.Tree
|
|
|
|
txn *iradix.Txn
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// NewCachableSource creates new CachableSource
|
|
|
|
func NewCachableSource(root string) *CachableSource {
|
|
|
|
ts := &CachableSource{
|
|
|
|
tree: iradix.New(),
|
2017-08-03 20:22:00 -04:00
|
|
|
root: containerfs.NewLocalContainerFS(root),
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
return ts
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// MarshalBinary marshals current cache information to a byte array
|
|
|
|
func (cs *CachableSource) MarshalBinary() ([]byte, error) {
|
|
|
|
b := TarsumBackup{Hashes: make(map[string]string)}
|
|
|
|
root := cs.getRoot()
|
|
|
|
root.Walk(func(k []byte, v interface{}) bool {
|
|
|
|
b.Hashes[string(k)] = v.(*fileInfo).sum
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return b.Marshal()
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// UnmarshalBinary decodes cache information for presented byte array
|
|
|
|
func (cs *CachableSource) UnmarshalBinary(data []byte) error {
|
|
|
|
var b TarsumBackup
|
|
|
|
if err := b.Unmarshal(data); err != nil {
|
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
txn := iradix.New().Txn()
|
|
|
|
for p, v := range b.Hashes {
|
|
|
|
txn.Insert([]byte(p), &fileInfo{sum: v})
|
|
|
|
}
|
|
|
|
cs.mu.Lock()
|
|
|
|
defer cs.mu.Unlock()
|
|
|
|
cs.tree = txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// Scan rescans the cache information from the file system
|
|
|
|
func (cs *CachableSource) Scan() error {
|
|
|
|
lc, err := NewLazySource(cs.root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txn := iradix.New().Txn()
|
2017-08-03 20:22:00 -04:00
|
|
|
err = cs.root.Walk(cs.root.Path(), func(path string, info os.FileInfo, err error) error {
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
return errors.Wrapf(err, "failed to walk %s", path)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
rel, err := Rel(cs.root, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h, err := lc.Hash(rel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txn.Insert([]byte(rel), &fileInfo{sum: h})
|
|
|
|
return nil
|
|
|
|
})
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
cs.mu.Lock()
|
|
|
|
defer cs.mu.Unlock()
|
|
|
|
cs.tree = txn.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// HandleChange notifies the source about a modification operation
|
|
|
|
func (cs *CachableSource) HandleChange(kind fsutil.ChangeKind, p string, fi os.FileInfo, err error) (retErr error) {
|
|
|
|
cs.mu.Lock()
|
|
|
|
if cs.txn == nil {
|
|
|
|
cs.txn = cs.tree.Txn()
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-05-15 17:54:27 -04:00
|
|
|
if kind == fsutil.ChangeKindDelete {
|
|
|
|
cs.txn.Delete([]byte(p))
|
|
|
|
cs.mu.Unlock()
|
|
|
|
return
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
h, ok := fi.(hashed)
|
|
|
|
if !ok {
|
|
|
|
cs.mu.Unlock()
|
|
|
|
return errors.Errorf("invalid fileinfo: %s", p)
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
hfi := &fileInfo{
|
2017-09-22 01:07:45 -04:00
|
|
|
sum: h.Digest().Hex(),
|
2017-05-15 17:54:27 -04:00
|
|
|
}
|
|
|
|
cs.txn.Insert([]byte(p), hfi)
|
|
|
|
cs.mu.Unlock()
|
|
|
|
return nil
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
func (cs *CachableSource) getRoot() *iradix.Node {
|
|
|
|
cs.mu.Lock()
|
|
|
|
if cs.txn != nil {
|
|
|
|
cs.tree = cs.txn.Commit()
|
|
|
|
cs.txn = nil
|
|
|
|
}
|
|
|
|
t := cs.tree
|
|
|
|
cs.mu.Unlock()
|
|
|
|
return t.Root()
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// Close closes the source
|
|
|
|
func (cs *CachableSource) Close() error {
|
|
|
|
return nil
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// Hash returns a hash for a single file in the source
|
|
|
|
func (cs *CachableSource) Hash(path string) (string, error) {
|
|
|
|
n := cs.getRoot()
|
|
|
|
// TODO: check this for symlinks
|
|
|
|
v, ok := n.Get([]byte(path))
|
|
|
|
if !ok {
|
2017-09-08 18:00:14 -04:00
|
|
|
return path, nil
|
2017-03-20 18:22:29 -04:00
|
|
|
}
|
2017-09-08 18:00:14 -04:00
|
|
|
return v.(*fileInfo).sum, nil
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:54:27 -04:00
|
|
|
// Root returns a root directory for the source
|
2017-08-03 20:22:00 -04:00
|
|
|
func (cs *CachableSource) Root() containerfs.ContainerFS {
|
2017-05-15 17:54:27 -04:00
|
|
|
return cs.root
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileInfo struct {
|
|
|
|
sum string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fi *fileInfo) Hash() string {
|
|
|
|
return fi.sum
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|