2017-03-15 18:28:06 -04:00
|
|
|
package remotecontext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-03-18 15:29:56 -04:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-03-15 18:28:06 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
|
|
|
"github.com/docker/docker/pkg/pools"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
// NewLazySource creates a new LazyContext. LazyContext defines a hashed build
|
2017-03-15 18:28:06 -04:00
|
|
|
// context based on a root directory. Individual files are hashed first time
|
|
|
|
// they are asked. It is not safe to call methods of LazyContext concurrently.
|
2017-05-25 16:02:29 -04:00
|
|
|
func NewLazySource(root string) (builder.Source, error) {
|
|
|
|
return &lazySource{
|
2017-03-15 18:28:06 -04:00
|
|
|
root: root,
|
|
|
|
sums: make(map[string]string),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
type lazySource struct {
|
2017-03-15 18:28:06 -04:00
|
|
|
root string
|
|
|
|
sums map[string]string
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
func (c *lazySource) Root() string {
|
2017-03-20 18:22:29 -04:00
|
|
|
return c.root
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
func (c *lazySource) Close() error {
|
2017-03-20 18:22:29 -04:00
|
|
|
return nil
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
func (c *lazySource) Hash(path string) (string, error) {
|
2017-03-20 18:22:29 -04:00
|
|
|
cleanPath, fullPath, err := normalize(path, c.root)
|
2017-03-15 18:28:06 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return "", err
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
fi, err := os.Lstat(fullPath)
|
2017-03-15 18:28:06 -04:00
|
|
|
if err != nil {
|
2017-05-15 17:54:27 -04:00
|
|
|
return "", errors.WithStack(err)
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
relPath, err := Rel(c.root, fullPath)
|
2017-03-15 18:28:06 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return "", errors.WithStack(convertPathError(err, cleanPath))
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sum, ok := c.sums[relPath]
|
|
|
|
if !ok {
|
2017-03-20 18:22:29 -04:00
|
|
|
sum, err = c.prepareHash(relPath, fi)
|
2017-03-15 18:28:06 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return "", err
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
return sum, nil
|
2017-03-15 18:28:06 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 16:02:29 -04:00
|
|
|
func (c *lazySource) prepareHash(relPath string, fi os.FileInfo) (string, error) {
|
2017-03-15 18:28:06 -04:00
|
|
|
p := filepath.Join(c.root, relPath)
|
|
|
|
h, err := NewFileHash(p, relPath, fi)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to create hash for %s", relPath)
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() && fi.Size() > 0 {
|
|
|
|
f, err := os.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to open %s", relPath)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := pools.Copy(h, f); err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to copy file data for %s", relPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum := hex.EncodeToString(h.Sum(nil))
|
|
|
|
c.sums[relPath] = sum
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
// Rel makes a path relative to base path. Same as `filepath.Rel` but can also
|
|
|
|
// handle UUID paths in windows.
|
|
|
|
func Rel(basepath, targpath string) (string, error) {
|
2017-03-18 15:29:56 -04:00
|
|
|
// filepath.Rel can't handle UUID paths in windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
pfx := basepath + `\`
|
|
|
|
if strings.HasPrefix(targpath, pfx) {
|
|
|
|
p := strings.TrimPrefix(targpath, pfx)
|
|
|
|
if p == "" {
|
|
|
|
p = "."
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filepath.Rel(basepath, targpath)
|
|
|
|
}
|