2013-11-01 01:06:35 -04:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2014-02-17 07:41:52 -05:00
|
|
|
"fmt"
|
2013-12-13 09:43:50 -05:00
|
|
|
"io"
|
2014-02-17 07:41:52 -05:00
|
|
|
"io/ioutil"
|
2013-11-01 01:06:35 -04:00
|
|
|
"os"
|
2013-11-07 18:58:03 -05:00
|
|
|
"path/filepath"
|
2013-11-01 01:06:35 -04:00
|
|
|
"strings"
|
2013-11-19 03:35:03 -05:00
|
|
|
"syscall"
|
2014-05-20 20:10:35 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2014-08-12 12:29:46 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-11-13 15:36:05 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2013-11-01 01:06:35 -04:00
|
|
|
)
|
|
|
|
|
2014-12-17 21:26:03 -05:00
|
|
|
func UnpackLayer(dest string, layer ArchiveReader) (size int64, err error) {
|
2013-12-13 09:43:50 -05:00
|
|
|
tr := tar.NewReader(layer)
|
2014-08-12 12:29:46 -04:00
|
|
|
trBuf := pools.BufioReader32KPool.Get(tr)
|
|
|
|
defer pools.BufioReader32KPool.Put(trBuf)
|
2013-11-01 01:06:35 -04:00
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
var dirs []*tar.Header
|
2013-11-19 03:35:03 -05:00
|
|
|
|
2014-02-17 07:41:52 -05:00
|
|
|
aufsTempdir := ""
|
|
|
|
aufsHardlinks := make(map[string]*tar.Header)
|
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
2013-11-01 01:06:35 -04:00
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-11-01 01:06:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-17 21:26:03 -05:00
|
|
|
size += hdr.Size
|
|
|
|
|
2013-12-16 08:35:43 -05:00
|
|
|
// Normalize name, for safety and for a simple is-root check
|
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
|
|
|
if !strings.HasSuffix(hdr.Name, "/") {
|
2013-12-24 19:37:00 -05:00
|
|
|
// Not the root directory, ensure that the parent directory exists.
|
2013-12-16 08:35:43 -05:00
|
|
|
// This happened in some tests where an image had a tarfile without any
|
2013-12-24 19:37:00 -05:00
|
|
|
// parent directories.
|
2013-12-16 08:35:43 -05:00
|
|
|
parent := filepath.Dir(hdr.Name)
|
|
|
|
parentPath := filepath.Join(dest, parent)
|
|
|
|
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
|
2014-04-16 11:16:07 -04:00
|
|
|
err = os.MkdirAll(parentPath, 0600)
|
2013-12-16 08:35:43 -05:00
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-12-16 08:35:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
// Skip AUFS metadata dirs
|
|
|
|
if strings.HasPrefix(hdr.Name, ".wh..wh.") {
|
2014-02-17 07:41:52 -05:00
|
|
|
// 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-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-02-17 07:41:52 -05:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(aufsTempdir)
|
|
|
|
}
|
2014-05-20 20:10:35 -04:00
|
|
|
if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true); err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-02-17 07:41:52 -05:00
|
|
|
}
|
|
|
|
}
|
2013-12-13 09:43:50 -05:00
|
|
|
continue
|
2013-11-01 01:06:35 -04:00
|
|
|
}
|
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2014-11-27 02:00:13 -05:00
|
|
|
rel, err := filepath.Rel(dest, path)
|
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-11-27 02:00:13 -05:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(rel, "..") {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
|
2014-10-31 13:18:39 -04:00
|
|
|
}
|
2014-11-27 02:00:13 -05:00
|
|
|
base := filepath.Base(path)
|
2014-10-31 13:18:39 -04:00
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
if strings.HasPrefix(base, ".wh.") {
|
|
|
|
originalBase := base[len(".wh."):]
|
|
|
|
originalPath := filepath.Join(filepath.Dir(path), originalBase)
|
|
|
|
if err := os.RemoveAll(originalPath); err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-11-19 03:35:03 -05:00
|
|
|
}
|
2013-12-13 09:43:50 -05:00
|
|
|
} else {
|
2013-12-24 19:37:00 -05:00
|
|
|
// If path exits we almost always just want to remove and replace it.
|
2013-12-13 09:43:50 -05:00
|
|
|
// 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 {
|
2013-12-20 05:30:45 -05:00
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
2013-12-13 09:43:50 -05:00
|
|
|
if err := os.RemoveAll(path); err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-12-13 09:43:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 01:06:35 -04:00
|
|
|
|
2014-07-14 12:31:19 -04:00
|
|
|
trBuf.Reset(tr)
|
|
|
|
srcData := io.Reader(trBuf)
|
2014-02-17 07:41:52 -05:00
|
|
|
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-17 21:26:03 -05:00
|
|
|
return 0, fmt.Errorf("Invalid aufs hardlink")
|
2014-02-17 07:41:52 -05:00
|
|
|
}
|
|
|
|
tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
|
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-02-17 07:41:52 -05:00
|
|
|
}
|
|
|
|
defer tmpFile.Close()
|
|
|
|
srcData = tmpFile
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:10:35 -04:00
|
|
|
if err := createTarFile(path, dest, srcHdr, srcData, true); err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-11-19 03:35:03 -05:00
|
|
|
}
|
2013-12-13 09:43:50 -05:00
|
|
|
|
2013-12-20 05:30:45 -05: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)
|
2013-12-13 09:43:50 -05:00
|
|
|
}
|
2013-11-01 01:06:35 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-19 03:35:03 -05:00
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
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-17 21:26:03 -05:00
|
|
|
return 0, err
|
2013-11-19 03:35:03 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 21:26:03 -05:00
|
|
|
|
|
|
|
return size, nil
|
2013-11-01 01:06:35 -04:00
|
|
|
}
|
2014-12-08 16:14:56 -05:00
|
|
|
|
|
|
|
// ApplyLayer parses a diff in the standard layer format from `layer`, and
|
2014-12-17 21:26:03 -05:00
|
|
|
// applies it to the directory `dest`. Returns the size in bytes of the
|
|
|
|
// contents of the layer.
|
|
|
|
func ApplyLayer(dest string, layer ArchiveReader) (int64, error) {
|
2014-12-08 16:14:56 -05:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
|
|
|
|
// We need to be able to set any perms
|
|
|
|
oldmask, err := system.Umask(0)
|
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
|
|
|
defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
|
|
|
|
|
|
|
|
layer, err = DecompressStream(layer)
|
|
|
|
if err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
|
|
|
return UnpackLayer(dest, layer)
|
|
|
|
}
|