2014-09-30 06:23:36 +00:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2015-05-01 22:01:10 +00:00
|
|
|
"archive/tar"
|
2014-09-30 06:23:36 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-01 23:42:27 +00:00
|
|
|
"runtime"
|
2014-09-30 06:23:36 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-11-13 20:36:05 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-09-30 06:23:36 +00:00
|
|
|
)
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// UnpackLayer unpack `layer` to a `dest`. The stream `layer` can be
|
|
|
|
// compressed or uncompressed.
|
|
|
|
// Returns the size in bytes of the contents of the layer.
|
|
|
|
func UnpackLayer(dest string, layer Reader) (size int64, err error) {
|
2014-09-30 06:23:36 +00:00
|
|
|
tr := tar.NewReader(layer)
|
|
|
|
trBuf := pools.BufioReader32KPool.Get(tr)
|
|
|
|
defer pools.BufioReader32KPool.Put(trBuf)
|
|
|
|
|
|
|
|
var dirs []*tar.Header
|
|
|
|
|
|
|
|
aufsTempdir := ""
|
|
|
|
aufsHardlinks := make(map[string]*tar.Header)
|
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:26:03 +00:00
|
|
|
size += hdr.Size
|
|
|
|
|
2014-09-30 06:23:36 +00:00
|
|
|
// Normalize name, for safety and for a simple is-root check
|
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
// Windows does not support filenames with colons in them. Ignore
|
|
|
|
// these files. This is not a problem though (although it might
|
|
|
|
// appear that it is). Let's suppose a client is running docker pull.
|
|
|
|
// The daemon it points to is Windows. Would it make sense for the
|
|
|
|
// client to be doing a docker pull Ubuntu for example (which has files
|
|
|
|
// with colons in the name under /usr/share/man/man3)? No, absolutely
|
|
|
|
// not as it would really only make sense that they were pulling a
|
|
|
|
// Windows image. However, for development, it is necessary to be able
|
|
|
|
// to pull Linux images which are in the repository.
|
|
|
|
//
|
|
|
|
// TODO Windows. Once the registry is aware of what images are Windows-
|
|
|
|
// specific or Linux-specific, this warning should be changed to an error
|
|
|
|
// to cater for the situation where someone does manage to upload a Linux
|
2015-08-07 22:24:18 +00:00
|
|
|
// image but have it tagged as Windows inadvertently.
|
2015-06-01 23:42:27 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if strings.Contains(hdr.Name, ":") {
|
|
|
|
logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note as these operations are platform specific, so must the slash be.
|
|
|
|
if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
|
2014-09-30 06:23:36 +00:00
|
|
|
// Not the root directory, ensure that the parent directory exists.
|
|
|
|
// This happened in some tests where an image had a tarfile without any
|
|
|
|
// parent directories.
|
|
|
|
parent := filepath.Dir(hdr.Name)
|
|
|
|
parentPath := filepath.Join(dest, parent)
|
2015-06-01 23:42:27 +00:00
|
|
|
|
2014-09-30 06:23:36 +00:00
|
|
|
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
|
2015-05-07 22:14:11 +00:00
|
|
|
err = system.MkdirAll(parentPath, 0600)
|
2014-09-30 06:23:36 +00:00
|
|
|
if err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip AUFS metadata dirs
|
|
|
|
if strings.HasPrefix(hdr.Name, ".wh..wh.") {
|
|
|
|
// Regular files inside /.wh..wh.plnk can be used as hardlink targets
|
|
|
|
// We don't want this directory, but we need the files in them so that
|
|
|
|
// such hardlinks can be resolved.
|
|
|
|
if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
|
|
|
|
basename := filepath.Base(hdr.Name)
|
|
|
|
aufsHardlinks[basename] = hdr
|
|
|
|
if aufsTempdir == "" {
|
|
|
|
if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(aufsTempdir)
|
|
|
|
}
|
2015-01-19 20:15:36 +00:00
|
|
|
if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil); err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-21 21:36:35 +00:00
|
|
|
|
|
|
|
if hdr.Name != ".wh..wh..opq" {
|
|
|
|
continue
|
|
|
|
}
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2014-11-27 07:00:13 +00:00
|
|
|
rel, err := filepath.Rel(dest, path)
|
|
|
|
if err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-11-27 07:00:13 +00:00
|
|
|
}
|
2015-06-01 23:42:27 +00:00
|
|
|
|
|
|
|
// Note as these operations are platform specific, so must the slash be.
|
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
|
2014-10-31 17:18:39 +00:00
|
|
|
}
|
2014-11-27 07:00:13 +00:00
|
|
|
base := filepath.Base(path)
|
2014-10-31 17:18:39 +00:00
|
|
|
|
2014-09-30 06:23:36 +00:00
|
|
|
if strings.HasPrefix(base, ".wh.") {
|
|
|
|
originalBase := base[len(".wh."):]
|
2015-09-21 21:36:35 +00:00
|
|
|
dir := filepath.Dir(path)
|
|
|
|
if originalBase == ".wh..opq" {
|
|
|
|
fi, err := os.Lstat(dir)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(dir, fi.Mode()&os.ModePerm); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
originalPath := filepath.Join(dir, originalBase)
|
|
|
|
if err := os.RemoveAll(originalPath); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If path exits we almost always just want to remove and replace it.
|
|
|
|
// The only exception is when it is a directory *and* the file from
|
|
|
|
// the layer is also a directory. Then we want to merge them (i.e.
|
|
|
|
// just apply the metadata from the layer).
|
|
|
|
if fi, err := os.Lstat(path); err == nil {
|
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trBuf.Reset(tr)
|
|
|
|
srcData := io.Reader(trBuf)
|
|
|
|
srcHdr := hdr
|
|
|
|
|
|
|
|
// Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
|
|
|
|
// we manually retarget these into the temporary files we extracted them into
|
|
|
|
if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
|
|
|
|
linkBasename := filepath.Base(hdr.Linkname)
|
|
|
|
srcHdr = aufsHardlinks[linkBasename]
|
|
|
|
if srcHdr == nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, fmt.Errorf("Invalid aufs hardlink")
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
|
|
|
|
if err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
srcData = tmpFile
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:15:36 +00:00
|
|
|
if err := createTarFile(path, dest, srcHdr, srcData, true, nil); err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directory mtimes must be handled at the end to avoid further
|
|
|
|
// file creation in them to modify the directory mtime
|
|
|
|
if hdr.Typeflag == tar.TypeDir {
|
|
|
|
dirs = append(dirs, hdr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hdr := range dirs {
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
|
|
|
if err := syscall.UtimesNano(path, ts); err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
|
|
|
return size, nil
|
2014-09-30 06:23:36 +00:00
|
|
|
}
|
2014-12-08 21:14:56 +00:00
|
|
|
|
2015-07-27 13:46:20 +00:00
|
|
|
// ApplyLayer parses a diff in the standard layer format from `layer`,
|
|
|
|
// and applies it to the directory `dest`. The stream `layer` can be
|
|
|
|
// compressed or uncompressed.
|
|
|
|
// Returns the size in bytes of the contents of the layer.
|
2015-08-04 01:52:54 +00:00
|
|
|
func ApplyLayer(dest string, layer Reader) (int64, error) {
|
2015-07-27 13:46:20 +00:00
|
|
|
return applyLayerHandler(dest, layer, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyUncompressedLayer parses a diff in the standard layer format from
|
|
|
|
// `layer`, and applies it to the directory `dest`. The stream `layer`
|
|
|
|
// can only be uncompressed.
|
|
|
|
// Returns the size in bytes of the contents of the layer.
|
2015-08-04 01:52:54 +00:00
|
|
|
func ApplyUncompressedLayer(dest string, layer Reader) (int64, error) {
|
2015-07-27 13:46:20 +00:00
|
|
|
return applyLayerHandler(dest, layer, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do the bulk load of ApplyLayer, but allow for not calling DecompressStream
|
2015-08-04 01:52:54 +00:00
|
|
|
func applyLayerHandler(dest string, layer Reader, decompress bool) (int64, error) {
|
2014-12-08 21:14:56 +00:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
|
|
|
|
// We need to be able to set any perms
|
|
|
|
oldmask, err := system.Umask(0)
|
|
|
|
if err != nil {
|
2014-12-18 02:26:03 +00:00
|
|
|
return 0, err
|
2014-12-08 21:14:56 +00:00
|
|
|
}
|
|
|
|
defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
|
|
|
|
|
2015-07-27 13:46:20 +00:00
|
|
|
if decompress {
|
|
|
|
layer, err = DecompressStream(layer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-12-08 21:14:56 +00:00
|
|
|
}
|
|
|
|
return UnpackLayer(dest, layer)
|
|
|
|
}
|