2015-05-12 21:21:26 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-08-25 16:26:49 -04:00
|
|
|
"strings"
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2015-09-10 18:01:18 -04:00
|
|
|
"github.com/docker/docker/context"
|
2015-05-12 21:21:26 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
// ContainerCopy performs a deprecated operation of archiving the resource at
|
2015-05-12 21:21:26 -04:00
|
|
|
// the specified path in the conatiner identified by the given name.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (daemon *Daemon) ContainerCopy(ctx context.Context, name string, res string) (io.ReadCloser, error) {
|
|
|
|
container, err := daemon.Get(ctx, name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
if res[0] == '/' || res[0] == '\\' {
|
2015-05-12 21:21:26 -04:00
|
|
|
res = res[1:]
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
return container.copy(ctx, res)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatPath stats the filesystem resource at the specified path in the
|
|
|
|
// container identified by the given name.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (daemon *Daemon) ContainerStatPath(ctx context.Context, name string, path string) (stat *types.ContainerPathStat, err error) {
|
|
|
|
container, err := daemon.Get(ctx, name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
return container.StatPath(ctx, path)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (daemon *Daemon) ContainerArchivePath(ctx context.Context, name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
|
|
|
|
container, err := daemon.Get(ctx, name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
return container.ArchivePath(ctx, path)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (daemon *Daemon) ContainerExtractToDir(ctx context.Context, name, path string, noOverwriteDirNonDir bool, content io.Reader) error {
|
|
|
|
container, err := daemon.Get(ctx, name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
return container.ExtractToDir(ctx, path, noOverwriteDirNonDir, content)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// resolvePath resolves the given path in the container to a resource on the
|
|
|
|
// host. Returns a resolved path (absolute path to the resource on the host),
|
|
|
|
// the absolute path to the resource relative to the container's rootfs, and
|
|
|
|
// a error if the path points to outside the container's rootfs.
|
|
|
|
func (container *Container) resolvePath(path string) (resolvedPath, absPath string, err error) {
|
|
|
|
// Consider the given path as an absolute path in the container.
|
|
|
|
absPath = archive.PreserveTrailingDotOrSeparator(filepath.Join(string(filepath.Separator), path), path)
|
|
|
|
|
|
|
|
// Split the absPath into its Directory and Base components. We will
|
|
|
|
// resolve the dir in the scope of the container then append the base.
|
|
|
|
dirPath, basePath := filepath.Split(absPath)
|
|
|
|
|
|
|
|
resolvedDirPath, err := container.GetResourcePath(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolvedDirPath will have been cleaned (no trailing path separators) so
|
|
|
|
// we can manually join it with the base path element.
|
|
|
|
resolvedPath = resolvedDirPath + string(filepath.Separator) + basePath
|
|
|
|
|
|
|
|
return resolvedPath, absPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// statPath is the unexported version of StatPath. Locks and mounts should
|
2015-08-07 18:24:18 -04:00
|
|
|
// be acquired before calling this method and the given path should be fully
|
2015-07-24 17:12:55 -04:00
|
|
|
// resolved to a path on the host corresponding to the given absolute path
|
|
|
|
// inside the container.
|
|
|
|
func (container *Container) statPath(resolvedPath, absPath string) (stat *types.ContainerPathStat, err error) {
|
|
|
|
lstat, err := os.Lstat(resolvedPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var linkTarget string
|
|
|
|
if lstat.Mode()&os.ModeSymlink != 0 {
|
|
|
|
// Fully evaluate the symlink in the scope of the container rootfs.
|
|
|
|
hostPath, err := container.GetResourcePath(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
linkTarget, err = filepath.Rel(container.basefs, hostPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it an absolute path.
|
|
|
|
linkTarget = filepath.Join(string(filepath.Separator), linkTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.ContainerPathStat{
|
|
|
|
Name: filepath.Base(absPath),
|
|
|
|
Size: lstat.Size(),
|
|
|
|
Mode: lstat.Mode(),
|
|
|
|
Mtime: lstat.ModTime(),
|
|
|
|
LinkTarget: linkTarget,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-12 21:21:26 -04:00
|
|
|
// StatPath stats the filesystem resource at the specified path in this
|
|
|
|
// container. Returns stat info about the resource.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (container *Container) StatPath(ctx context.Context, path string) (stat *types.ContainerPathStat, err error) {
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
if err = container.Mount(ctx); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-10 18:01:18 -04:00
|
|
|
defer container.Unmount(ctx)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
err = container.mountVolumes()
|
2015-07-30 17:01:53 -04:00
|
|
|
defer container.unmountVolumes(true)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
resolvedPath, absPath, err := container.resolvePath(path)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
return container.statPath(resolvedPath, absPath)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ArchivePath creates an archive of the filesystem resource at the specified
|
|
|
|
// path in this container. Returns a tar archive of the resource and stat info
|
|
|
|
// about the resource.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (container *Container) ArchivePath(ctx context.Context, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
|
2015-05-12 21:21:26 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
if err = container.Mount(ctx); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// unmount any volumes
|
2015-07-30 17:01:53 -04:00
|
|
|
container.unmountVolumes(true)
|
2015-05-12 21:21:26 -04:00
|
|
|
// unmount the container's rootfs
|
2015-09-10 18:01:18 -04:00
|
|
|
container.Unmount(ctx)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = container.mountVolumes(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
resolvedPath, absPath, err := container.resolvePath(path)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
stat, err = container.statPath(resolvedPath, absPath)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// 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))
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = ioutils.NewReadCloserWrapper(data, func() error {
|
|
|
|
err := data.Close()
|
2015-07-30 17:01:53 -04:00
|
|
|
container.unmountVolumes(true)
|
2015-09-10 18:01:18 -04:00
|
|
|
container.Unmount(ctx)
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
container.logEvent(ctx, "archive-path")
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
return content, stat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractToDir extracts the given tar archive to the specified location in the
|
|
|
|
// 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.
|
2015-09-10 18:01:18 -04:00
|
|
|
func (container *Container) ExtractToDir(ctx context.Context, path string, noOverwriteDirNonDir bool, content io.Reader) (err error) {
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
if err = container.Mount(ctx); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-10 18:01:18 -04:00
|
|
|
defer container.Unmount(ctx)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
err = container.mountVolumes()
|
2015-07-30 17:01:53 -04:00
|
|
|
defer container.unmountVolumes(true)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// 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
|
|
|
|
// that we do not use `container.resolvePath(path)` here because we need
|
|
|
|
// 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.
|
|
|
|
|
2015-05-12 21:21:26 -04:00
|
|
|
// Consider the given path as an absolute path in the container.
|
2015-07-24 17:12:55 -04:00
|
|
|
absPath := archive.PreserveTrailingDotOrSeparator(filepath.Join(string(filepath.Separator), path), path)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// This will evaluate the last path element if it is a symlink.
|
2015-05-12 21:21:26 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// 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.
|
2015-08-25 16:26:49 -04:00
|
|
|
//
|
|
|
|
// 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{`) {
|
|
|
|
if strings.HasPrefix(resolvedPath, container.basefs) {
|
|
|
|
baseRel = resolvedPath[len(container.basefs):]
|
|
|
|
if baseRel[:1] == `\` {
|
|
|
|
baseRel = baseRel[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
baseRel, err = filepath.Rel(container.basefs, resolvedPath)
|
|
|
|
}
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-24 17:12:55 -04:00
|
|
|
// Make it an absolute path.
|
|
|
|
absPath = filepath.Join(string(filepath.Separator), baseRel)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
toVolume, err := checkIfPathIsInAVolume(container, absPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !toVolume && container.hostConfig.ReadonlyRootfs {
|
2015-07-30 17:01:53 -04:00
|
|
|
return ErrRootFSReadOnly
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
options := &archive.TarOptions{
|
|
|
|
ChownOpts: &archive.TarChownOptions{
|
|
|
|
UID: 0, GID: 0, // TODO: use config.User? Remap to userns root?
|
|
|
|
},
|
|
|
|
NoOverwriteDirNonDir: noOverwriteDirNonDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chrootarchive.Untar(content, resolvedPath, options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:01:18 -04:00
|
|
|
container.logEvent(ctx, "extract-to-dir")
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|