2017-03-15 18:28:06 -04:00
|
|
|
package remotecontext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
2017-03-18 15:29:56 -04:00
|
|
|
"strings"
|
2017-03-15 18:28:06 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/builder"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2017-03-15 18:28:06 -04:00
|
|
|
"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-08-03 20:22:00 -04:00
|
|
|
func NewLazySource(root containerfs.ContainerFS) (builder.Source, error) {
|
2017-05-25 16:02:29 -04:00
|
|
|
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-08-03 20:22:00 -04:00
|
|
|
root containerfs.ContainerFS
|
2017-03-15 18:28:06 -04:00
|
|
|
sums map[string]string
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
func (c *lazySource) Root() containerfs.ContainerFS {
|
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-08-03 20:22:00 -04:00
|
|
|
fi, err := c.root.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-08-03 20:22:00 -04:00
|
|
|
p := c.root.Join(c.root.Path(), relPath)
|
2017-03-15 18:28:06 -04:00
|
|
|
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 {
|
2017-08-03 20:22:00 -04:00
|
|
|
f, err := c.root.Open(p)
|
2017-03-15 18:28:06 -04:00
|
|
|
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.
|
2017-08-03 20:22:00 -04:00
|
|
|
func Rel(basepath containerfs.ContainerFS, targpath string) (string, error) {
|
2017-03-18 15:29:56 -04:00
|
|
|
// filepath.Rel can't handle UUID paths in windows
|
2017-08-03 20:22:00 -04:00
|
|
|
if basepath.OS() == "windows" {
|
|
|
|
pfx := basepath.Path() + `\`
|
2017-03-18 15:29:56 -04:00
|
|
|
if strings.HasPrefix(targpath, pfx) {
|
|
|
|
p := strings.TrimPrefix(targpath, pfx)
|
|
|
|
if p == "" {
|
|
|
|
p = "."
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
return basepath.Rel(basepath.Path(), targpath)
|
2017-03-18 15:29:56 -04:00
|
|
|
}
|