daemon/archive.go
c32dde5b
 package daemon
 
 import (
 	"errors"
 	"io"
 	"os"
 	"path/filepath"
f950de57
 	"strings"
c32dde5b
 
9c332b16
 	"github.com/docker/docker/builder"
6bb0d181
 	"github.com/docker/docker/container"
c32dde5b
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/chrootarchive"
9c332b16
 	"github.com/docker/docker/pkg/idtools"
c32dde5b
 	"github.com/docker/docker/pkg/ioutils"
7f665985
 	"github.com/docker/docker/pkg/system"
907407d0
 	"github.com/docker/engine-api/types"
c32dde5b
 )
 
 // ErrExtractPointNotDirectory is used to convey that the operation to extract
 // a tar archive to a directory in a container has failed because the specified
 // path does not refer to a directory.
 var ErrExtractPointNotDirectory = errors.New("extraction point is not a directory")
 
47c56e43
 // ContainerCopy performs a deprecated operation of archiving the resource at
927b334e
 // the specified path in the container identified by the given name.
b08f071e
 func (daemon *Daemon) ContainerCopy(name string, res string) (io.ReadCloser, error) {
d7d512bb
 	container, err := daemon.GetContainer(name)
c32dde5b
 	if err != nil {
 		return nil, err
 	}
 
47c56e43
 	if res[0] == '/' || res[0] == '\\' {
c32dde5b
 		res = res[1:]
 	}
 
3a497650
 	return daemon.containerCopy(container, res)
c32dde5b
 }
 
 // ContainerStatPath stats the filesystem resource at the specified path in the
 // container identified by the given name.
b08f071e
 func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *types.ContainerPathStat, err error) {
d7d512bb
 	container, err := daemon.GetContainer(name)
c32dde5b
 	if err != nil {
 		return nil, err
 	}
 
3a497650
 	return daemon.containerStatPath(container, path)
c32dde5b
 }
 
 // ContainerArchivePath creates an archive of the filesystem resource at the
 // specified path in the container identified by the given name. Returns a
 // tar archive of the resource and whether it was a directory or a single file.
b08f071e
 func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
d7d512bb
 	container, err := daemon.GetContainer(name)
c32dde5b
 	if err != nil {
 		return nil, nil, err
 	}
 
3a497650
 	return daemon.containerArchivePath(container, path)
c32dde5b
 }
 
 // ContainerExtractToDir extracts the given archive to the specified location
 // in the filesystem of the container identified by the given name. The given
 // path must be of a directory in the container. If it is not, the error will
 // be ErrExtractPointNotDirectory. If noOverwriteDirNonDir is true then it will
 // be an error if unpacking the given content would cause an existing directory
 // to be replaced with a non-directory and vice versa.
b08f071e
 func (daemon *Daemon) ContainerExtractToDir(name, path string, noOverwriteDirNonDir bool, content io.Reader) error {
d7d512bb
 	container, err := daemon.GetContainer(name)
c32dde5b
 	if err != nil {
 		return err
 	}
 
3a497650
 	return daemon.containerExtractToDir(container, path, noOverwriteDirNonDir, content)
c32dde5b
 }
 
3a497650
 // containerStatPath stats the filesystem resource at the specified path in this
c32dde5b
 // container. Returns stat info about the resource.
6bb0d181
 func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *types.ContainerPathStat, err error) {
c32dde5b
 	container.Lock()
 	defer container.Unlock()
 
3a497650
 	if err = daemon.Mount(container); err != nil {
c32dde5b
 		return nil, err
 	}
3a497650
 	defer daemon.Unmount(container)
c32dde5b
 
63efc120
 	err = daemon.mountVolumes(container)
9d12d093
 	defer container.UnmountVolumes(true, daemon.LogVolumeEvent)
c32dde5b
 	if err != nil {
 		return nil, err
 	}
 
6bb0d181
 	resolvedPath, absPath, err := container.ResolvePath(path)
c32dde5b
 	if err != nil {
 		return nil, err
 	}
 
6bb0d181
 	return container.StatPath(resolvedPath, absPath)
c32dde5b
 }
 
3a497650
 // containerArchivePath creates an archive of the filesystem resource at the specified
c32dde5b
 // path in this container. Returns a tar archive of the resource and stat info
 // about the resource.
6bb0d181
 func (daemon *Daemon) containerArchivePath(container *container.Container, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
c32dde5b
 	container.Lock()
 
 	defer func() {
 		if err != nil {
 			// Wait to unlock the container until the archive is fully read
 			// (see the ReadCloseWrapper func below) or if there is an error
 			// before that occurs.
 			container.Unlock()
 		}
 	}()
 
3a497650
 	if err = daemon.Mount(container); err != nil {
c32dde5b
 		return nil, nil, err
 	}
 
 	defer func() {
 		if err != nil {
 			// unmount any volumes
9d12d093
 			container.UnmountVolumes(true, daemon.LogVolumeEvent)
c32dde5b
 			// unmount the container's rootfs
3a497650
 			daemon.Unmount(container)
c32dde5b
 		}
 	}()
 
63efc120
 	if err = daemon.mountVolumes(container); err != nil {
c32dde5b
 		return nil, nil, err
 	}
 
6bb0d181
 	resolvedPath, absPath, err := container.ResolvePath(path)
c32dde5b
 	if err != nil {
 		return nil, nil, err
 	}
 
6bb0d181
 	stat, err = container.StatPath(resolvedPath, absPath)
c32dde5b
 	if err != nil {
 		return nil, nil, err
 	}
 
75f6929b
 	// We need to rebase the archive entries if the last element of the
 	// resolved path was a symlink that was evaluated and is now different
 	// than the requested path. For example, if the given path was "/foo/bar/",
 	// but it resolved to "/var/lib/docker/containers/{id}/foo/baz/", we want
 	// to ensure that the archive entries start with "bar" and not "baz". This
 	// also catches the case when the root directory of the container is
 	// requested: we want the archive entries to start with "/" and not the
 	// container ID.
 	data, err := archive.TarResourceRebase(resolvedPath, filepath.Base(absPath))
c32dde5b
 	if err != nil {
 		return nil, nil, err
 	}
 
 	content = ioutils.NewReadCloserWrapper(data, func() error {
 		err := data.Close()
9d12d093
 		container.UnmountVolumes(true, daemon.LogVolumeEvent)
3a497650
 		daemon.Unmount(container)
c32dde5b
 		container.Unlock()
 		return err
 	})
 
ca5ede2d
 	daemon.LogContainerEvent(container, "archive-path")
c32dde5b
 
 	return content, stat, nil
 }
 
3a497650
 // containerExtractToDir extracts the given tar archive to the specified location in the
c32dde5b
 // filesystem of this container. The given path must be of a directory in the
 // container. If it is not, the error will be ErrExtractPointNotDirectory. If
 // noOverwriteDirNonDir is true then it will be an error if unpacking the
 // given content would cause an existing directory to be replaced with a non-
 // directory and vice versa.
6bb0d181
 func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, noOverwriteDirNonDir bool, content io.Reader) (err error) {
c32dde5b
 	container.Lock()
 	defer container.Unlock()
 
3a497650
 	if err = daemon.Mount(container); err != nil {
c32dde5b
 		return err
 	}
3a497650
 	defer daemon.Unmount(container)
c32dde5b
 
63efc120
 	err = daemon.mountVolumes(container)
9d12d093
 	defer container.UnmountVolumes(true, daemon.LogVolumeEvent)
c32dde5b
 	if err != nil {
 		return err
 	}
 
7f665985
 	// Check if a drive letter supplied, it must be the system drive. No-op except on Windows
 	path, err = system.CheckSystemDriveAndRemoveDriveLetter(path)
 	if err != nil {
 		return err
 	}
 
75f6929b
 	// The destination path needs to be resolved to a host path, with all
 	// symbolic links followed in the scope of the container's rootfs. Note
6bb0d181
 	// that we do not use `container.ResolvePath(path)` here because we need
75f6929b
 	// to also evaluate the last path element if it is a symlink. This is so
 	// that you can extract an archive to a symlink that points to a directory.
 
c32dde5b
 	// Consider the given path as an absolute path in the container.
75f6929b
 	absPath := archive.PreserveTrailingDotOrSeparator(filepath.Join(string(filepath.Separator), path), path)
c32dde5b
 
75f6929b
 	// This will evaluate the last path element if it is a symlink.
c32dde5b
 	resolvedPath, err := container.GetResourcePath(absPath)
 	if err != nil {
 		return err
 	}
 
 	stat, err := os.Lstat(resolvedPath)
 	if err != nil {
 		return err
 	}
 
 	if !stat.IsDir() {
 		return ErrExtractPointNotDirectory
 	}
 
75f6929b
 	// Need to check if the path is in a volume. If it is, it cannot be in a
 	// read-only volume. If it is not in a volume, the container cannot be
 	// configured with a read-only rootfs.
 
 	// Use the resolved path relative to the container rootfs as the new
 	// absPath. This way we fully follow any symlinks in a volume that may
 	// lead back outside the volume.
f950de57
 	//
 	// The Windows implementation of filepath.Rel in golang 1.4 does not
 	// support volume style file path semantics. On Windows when using the
 	// filter driver, we are guaranteed that the path will always be
 	// a volume file path.
 	var baseRel string
 	if strings.HasPrefix(resolvedPath, `\\?\Volume{`) {
6bb0d181
 		if strings.HasPrefix(resolvedPath, container.BaseFS) {
 			baseRel = resolvedPath[len(container.BaseFS):]
f950de57
 			if baseRel[:1] == `\` {
 				baseRel = baseRel[1:]
 			}
 		}
 	} else {
6bb0d181
 		baseRel, err = filepath.Rel(container.BaseFS, resolvedPath)
f950de57
 	}
c32dde5b
 	if err != nil {
 		return err
 	}
75f6929b
 	// Make it an absolute path.
 	absPath = filepath.Join(string(filepath.Separator), baseRel)
c32dde5b
 
47c56e43
 	toVolume, err := checkIfPathIsInAVolume(container, absPath)
 	if err != nil {
 		return err
c32dde5b
 	}
 
6bb0d181
 	if !toVolume && container.HostConfig.ReadonlyRootfs {
abd72d40
 		return ErrRootFSReadOnly
c32dde5b
 	}
 
40be5dba
 	uid, gid := daemon.GetRemappedUIDGID()
c32dde5b
 	options := &archive.TarOptions{
40be5dba
 		NoOverwriteDirNonDir: noOverwriteDirNonDir,
c32dde5b
 		ChownOpts: &archive.TarChownOptions{
40be5dba
 			UID: uid, GID: gid, // TODO: should all ownership be set to root (either real or remapped)?
c32dde5b
 		},
 	}
 	if err := chrootarchive.Untar(content, resolvedPath, options); err != nil {
 		return err
 	}
 
ca5ede2d
 	daemon.LogContainerEvent(container, "extract-to-dir")
c32dde5b
 
 	return nil
 }
ebf707ec
 
6bb0d181
 func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
ebf707ec
 	container.Lock()
 
 	defer func() {
 		if err != nil {
 			// Wait to unlock the container until the archive is fully read
 			// (see the ReadCloseWrapper func below) or if there is an error
 			// before that occurs.
 			container.Unlock()
 		}
 	}()
 
 	if err := daemon.Mount(container); err != nil {
 		return nil, err
 	}
 
 	defer func() {
 		if err != nil {
 			// unmount any volumes
9d12d093
 			container.UnmountVolumes(true, daemon.LogVolumeEvent)
ebf707ec
 			// unmount the container's rootfs
 			daemon.Unmount(container)
 		}
 	}()
 
63efc120
 	if err := daemon.mountVolumes(container); err != nil {
ebf707ec
 		return nil, err
 	}
 
 	basePath, err := container.GetResourcePath(resource)
 	if err != nil {
 		return nil, err
 	}
 	stat, err := os.Stat(basePath)
 	if err != nil {
 		return nil, err
 	}
 	var filter []string
 	if !stat.IsDir() {
 		d, f := filepath.Split(basePath)
 		basePath = d
 		filter = []string{f}
 	} else {
 		filter = []string{filepath.Base(basePath)}
 		basePath = filepath.Dir(basePath)
 	}
 	archive, err := archive.TarWithOptions(basePath, &archive.TarOptions{
 		Compression:  archive.Uncompressed,
 		IncludeFiles: filter,
 	})
 	if err != nil {
 		return nil, err
 	}
 
 	reader := ioutils.NewReadCloserWrapper(archive, func() error {
 		err := archive.Close()
9d12d093
 		container.UnmountVolumes(true, daemon.LogVolumeEvent)
ebf707ec
 		daemon.Unmount(container)
 		container.Unlock()
 		return err
 	})
ca5ede2d
 	daemon.LogContainerEvent(container, "copy")
ebf707ec
 	return reader, nil
 }
9c332b16
 
 // CopyOnBuild copies/extracts a source FileInfo to a destination path inside a container
 // specified by a container object.
 // TODO: make sure callers don't unnecessarily convert destPath with filepath.FromSlash (Copy does it already).
 // CopyOnBuild should take in abstract paths (with slashes) and the implementation should convert it to OS-specific paths.
 func (daemon *Daemon) CopyOnBuild(cID string, destPath string, src builder.FileInfo, decompress bool) error {
 	srcPath := src.Path()
 	destExists := true
 	destDir := false
 	rootUID, rootGID := daemon.GetRemappedUIDGID()
 
 	// Work in daemon-local OS specific file paths
 	destPath = filepath.FromSlash(destPath)
 
 	c, err := daemon.GetContainer(cID)
 	if err != nil {
 		return err
 	}
 	err = daemon.Mount(c)
 	if err != nil {
 		return err
 	}
 	defer daemon.Unmount(c)
 
 	dest, err := c.GetResourcePath(destPath)
 	if err != nil {
 		return err
 	}
 
 	// Preserve the trailing slash
 	// TODO: why are we appending another path separator if there was already one?
 	if strings.HasSuffix(destPath, string(os.PathSeparator)) || destPath == "." {
 		destDir = true
 		dest += string(os.PathSeparator)
 	}
 
 	destPath = dest
 
 	destStat, err := os.Stat(destPath)
 	if err != nil {
 		if !os.IsNotExist(err) {
 			//logrus.Errorf("Error performing os.Stat on %s. %s", destPath, err)
 			return err
 		}
 		destExists = false
 	}
 
 	uidMaps, gidMaps := daemon.GetUIDGIDMaps()
 	archiver := &archive.Archiver{
 		Untar:   chrootarchive.Untar,
 		UIDMaps: uidMaps,
 		GIDMaps: gidMaps,
 	}
 
 	if src.IsDir() {
 		// copy as directory
 		if err := archiver.CopyWithTar(srcPath, destPath); err != nil {
 			return err
 		}
 		return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
 	}
 	if decompress && archive.IsArchivePath(srcPath) {
 		// Only try to untar if it is a file and that we've been told to decompress (when ADD-ing a remote file)
 
 		// First try to unpack the source as an archive
 		// to support the untar feature we need to clean up the path a little bit
 		// because tar is very forgiving.  First we need to strip off the archive's
 		// filename from the path but this is only added if it does not end in slash
 		tarDest := destPath
 		if strings.HasSuffix(tarDest, string(os.PathSeparator)) {
 			tarDest = filepath.Dir(destPath)
 		}
 
 		// try to successfully untar the orig
 		err := archiver.UntarPath(srcPath, tarDest)
 		/*
 			if err != nil {
 				logrus.Errorf("Couldn't untar to %s: %v", tarDest, err)
 			}
 		*/
 		return err
 	}
 
 	// only needed for fixPermissions, but might as well put it before CopyFileWithTar
 	if destDir || (destExists && destStat.IsDir()) {
 		destPath = filepath.Join(destPath, src.Name())
 	}
 
 	if err := idtools.MkdirAllNewAs(filepath.Dir(destPath), 0755, rootUID, rootGID); err != nil {
 		return err
 	}
 	if err := archiver.CopyFileWithTar(srcPath, destPath); err != nil {
 		return err
 	}
 
 	return fixPermissions(srcPath, destPath, rootUID, rootGID, destExists)
 }