2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2013-11-01 01:06:35 -04:00
|
|
|
|
|
|
|
import (
|
2015-05-01 18:01:10 -04:00
|
|
|
"archive/tar"
|
2014-02-17 07:41:52 -05:00
|
|
|
"fmt"
|
2013-12-13 09:43:50 -05:00
|
|
|
"io"
|
2013-11-01 01:06:35 -04:00
|
|
|
"os"
|
2013-11-07 18:58:03 -05:00
|
|
|
"path/filepath"
|
2015-06-01 19:42:27 -04:00
|
|
|
"runtime"
|
2013-11-01 01:06:35 -04:00
|
|
|
"strings"
|
2014-05-20 20:10:35 -04:00
|
|
|
|
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"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-11-01 01:06:35 -04:00
|
|
|
)
|
|
|
|
|
2015-08-03 21:52:54 -04: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.
|
2016-10-20 19:40:59 -04:00
|
|
|
func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (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
|
2015-11-09 02:00:01 -05:00
|
|
|
unpackedPaths := make(map[string]struct{})
|
2015-10-13 17:01:36 -04:00
|
|
|
|
|
|
|
if options == nil {
|
|
|
|
options = &TarOptions{}
|
|
|
|
}
|
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
|
|
|
}
|
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)
|
|
|
|
|
2015-06-01 19:42:27 -04: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 18:24:18 -04:00
|
|
|
// image but have it tagged as Windows inadvertently.
|
2015-06-01 19:42:27 -04: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)) {
|
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)
|
2015-06-01 19:42:27 -04:00
|
|
|
|
2013-12-16 08:35:43 -05:00
|
|
|
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
|
2019-08-08 05:51:00 -04:00
|
|
|
err = system.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
|
2015-09-29 13:18:28 -04:00
|
|
|
if strings.HasPrefix(hdr.Name, WhiteoutMetaPrefix) {
|
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.
|
2015-09-29 13:18:28 -04:00
|
|
|
if strings.HasPrefix(hdr.Name, WhiteoutLinkDir) && hdr.Typeflag == tar.TypeReg {
|
2014-02-17 07:41:52 -05:00
|
|
|
basename := filepath.Base(hdr.Name)
|
|
|
|
aufsHardlinks[basename] = hdr
|
|
|
|
if aufsTempdir == "" {
|
2021-08-24 06:10:50 -04:00
|
|
|
if aufsTempdir, err = os.MkdirTemp("", "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)
|
|
|
|
}
|
2016-02-12 19:05:50 -05:00
|
|
|
if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil, options.InUserNS); err != nil {
|
2014-12-17 21:26:03 -05:00
|
|
|
return 0, err
|
2014-02-17 07:41:52 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-21 17:36:35 -04:00
|
|
|
|
2015-09-29 13:18:28 -04:00
|
|
|
if hdr.Name != WhiteoutOpaqueDir {
|
2015-09-21 17:36:35 -04:00
|
|
|
continue
|
|
|
|
}
|
2013-11-01 01:06:35 -04:00
|
|
|
}
|
2022-02-18 15:30:24 -05:00
|
|
|
//#nosec G305 -- The joined path is guarded against path traversal.
|
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
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
|
|
|
// Note as these operations are platform specific, so must the slash be.
|
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
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
|
|
|
|
2015-09-29 13:18:28 -04:00
|
|
|
if strings.HasPrefix(base, WhiteoutPrefix) {
|
2015-09-21 17:36:35 -04:00
|
|
|
dir := filepath.Dir(path)
|
2015-09-29 13:18:28 -04:00
|
|
|
if base == WhiteoutOpaqueDir {
|
2015-11-09 02:00:01 -05:00
|
|
|
_, err := os.Lstat(dir)
|
|
|
|
if err != nil {
|
2015-09-21 17:36:35 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2015-11-09 02:00:01 -05:00
|
|
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil // parent was deleted
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path == dir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, exists := unpackedPaths[path]; !exists {
|
|
|
|
err := os.RemoveAll(path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-09-21 17:36:35 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
2015-09-29 13:18:28 -04:00
|
|
|
originalBase := base[len(WhiteoutPrefix):]
|
2015-09-21 17:36:35 -04:00
|
|
|
originalPath := filepath.Join(dir, originalBase)
|
|
|
|
if err := os.RemoveAll(originalPath); err != nil {
|
|
|
|
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
|
2015-09-29 13:18:28 -04:00
|
|
|
if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), WhiteoutLinkDir) {
|
2014-02-17 07:41:52 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-14 15:24:29 -04:00
|
|
|
if err := remapIDs(options.IDMap, srcHdr); err != nil {
|
2017-05-31 17:18:04 -04:00
|
|
|
return 0, err
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2017-05-31 17:18:04 -04:00
|
|
|
|
2019-08-15 18:06:56 -04:00
|
|
|
if err := createTarFile(path, dest, srcHdr, srcData, !options.NoLchown, nil, options.InUserNS); 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
|
|
|
}
|
2015-11-09 02:00:01 -05:00
|
|
|
unpackedPaths[path] = struct{}{}
|
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 {
|
2022-02-18 15:30:24 -05:00
|
|
|
//#nosec G305 -- The header was checked for path traversal before it was appended to the dirs slice.
|
2013-12-13 09:43:50 -05:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2015-10-01 13:45:32 -04:00
|
|
|
if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); 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
|
|
|
|
2015-07-27 09:46:20 -04: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.
|
2016-10-20 19:40:59 -04:00
|
|
|
func ApplyLayer(dest string, layer io.Reader) (int64, error) {
|
2015-10-08 11:51:41 -04:00
|
|
|
return applyLayerHandler(dest, layer, &TarOptions{}, true)
|
2015-07-27 09:46:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-10-20 19:40:59 -04:00
|
|
|
func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (int64, error) {
|
2015-10-08 11:51:41 -04:00
|
|
|
return applyLayerHandler(dest, layer, options, false)
|
2015-07-27 09:46:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// do the bulk load of ApplyLayer, but allow for not calling DecompressStream
|
2016-10-20 19:40:59 -04:00
|
|
|
func applyLayerHandler(dest string, layer io.Reader, options *TarOptions, decompress bool) (int64, error) {
|
2014-12-08 16:14:56 -05:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
|
|
|
|
// We need to be able to set any perms
|
2018-10-05 18:46:59 -04:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
oldmask, err := system.Umask(0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer system.Umask(oldmask)
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-27 09:46:20 -04:00
|
|
|
if decompress {
|
2017-07-21 20:47:15 -04:00
|
|
|
decompLayer, err := DecompressStream(layer)
|
2015-07-27 09:46:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-07-21 20:47:15 -04:00
|
|
|
defer decompLayer.Close()
|
|
|
|
layer = decompLayer
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
return UnpackLayer(dest, layer, options)
|
2014-12-08 16:14:56 -05:00
|
|
|
}
|