2017-03-20 18:22:29 -04:00
|
|
|
package remotecontext
|
2015-09-06 13:26:40 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
2017-03-20 18:22:29 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-09-06 13:26:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type tarSumContext struct {
|
|
|
|
root string
|
|
|
|
sums tarsum.FileInfoSums
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tarSumContext) Close() error {
|
|
|
|
return os.RemoveAll(c.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertPathError(err error, cleanpath string) error {
|
|
|
|
if err, ok := err.(*os.PathError); ok {
|
|
|
|
err.Path = cleanpath
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
type modifiableContext interface {
|
|
|
|
builder.Source
|
|
|
|
// Remove deletes the entry specified by `path`.
|
|
|
|
// It is usual for directory entries to delete all its subentries.
|
|
|
|
Remove(path string) error
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeTarSumContext returns a build Context from a tar stream.
|
|
|
|
//
|
|
|
|
// It extracts the tar stream to a temporary folder that is deleted as soon as
|
|
|
|
// the Context is closed.
|
|
|
|
// As the extraction happens, a tarsum is calculated for every file, and the set of
|
|
|
|
// all those sums then becomes the source of truth for all operations on this Context.
|
|
|
|
//
|
|
|
|
// Closing tarStream has to be done by the caller.
|
2017-03-20 18:22:29 -04:00
|
|
|
func MakeTarSumContext(tarStream io.Reader) (builder.Source, error) {
|
2015-09-06 13:26:40 -04:00
|
|
|
root, err := ioutils.TempDir("", "docker-builder")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tsc := &tarSumContext{root: root}
|
|
|
|
|
|
|
|
// Make sure we clean-up upon error. In the happy case the caller
|
|
|
|
// is expected to manage the clean-up
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
tsc.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
decompressedStream, err := archive.DecompressStream(tarStream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum, err := tarsum.NewTarSum(decompressedStream, true, tarsum.Version1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:11:53 -05:00
|
|
|
err = chrootarchive.Untar(sum, root, nil)
|
|
|
|
if err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tsc.sums = sum.GetSums()
|
|
|
|
|
|
|
|
return tsc, nil
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
func (c *tarSumContext) Root() string {
|
|
|
|
return c.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tarSumContext) Remove(path string) error {
|
|
|
|
_, fullpath, err := normalize(path, c.root)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-03-20 18:22:29 -04:00
|
|
|
return os.RemoveAll(fullpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tarSumContext) Hash(path string) (string, error) {
|
|
|
|
cleanpath, fullpath, err := normalize(path, c.root)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return "", err
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
rel, err := filepath.Rel(c.root, fullpath)
|
|
|
|
if err != nil {
|
|
|
|
return "", convertPathError(err, cleanpath)
|
|
|
|
}
|
2015-09-06 13:26:40 -04:00
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
// Use the checksum of the followed path(not the possible symlink) because
|
|
|
|
// this is the file that is actually copied.
|
|
|
|
if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil {
|
|
|
|
return tsInfo.Sum(), nil
|
|
|
|
}
|
|
|
|
// We set sum to path by default for the case where GetFile returns nil.
|
|
|
|
// The usual case is if relative path is empty.
|
|
|
|
return path, nil // backwards compat TODO: see if really needed
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 18:22:29 -04:00
|
|
|
func normalize(path, root string) (cleanPath, fullPath string, err error) {
|
|
|
|
cleanPath = filepath.Clean(string(os.PathSeparator) + path)[1:]
|
|
|
|
fullPath, err = symlink.FollowSymlinkInScope(filepath.Join(root, path), root)
|
2015-09-06 13:26:40 -04:00
|
|
|
if err != nil {
|
2017-03-20 18:22:29 -04:00
|
|
|
return "", "", errors.Wrapf(err, "forbidden path outside the build context: %s (%s)", path, cleanPath)
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|
2017-03-20 18:22:29 -04:00
|
|
|
if _, err := os.Lstat(fullPath); err != nil {
|
|
|
|
return "", "", convertPathError(err, path)
|
|
|
|
}
|
|
|
|
return
|
2015-09-06 13:26:40 -04:00
|
|
|
}
|