2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-09-22 20:49:47 -04:00
|
|
|
"path/filepath"
|
2015-08-25 16:26:49 -04:00
|
|
|
"strings"
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
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"
|
2016-04-26 22:26:12 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-03-15 18:28:06 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-05-12 21:21:26 -04:00
|
|
|
)
|
|
|
|
|
2015-07-16 17:14:58 -04:00
|
|
|
// ContainerCopy performs a deprecated operation of archiving the resource at
|
2015-12-13 11:00:39 -05:00
|
|
|
// the specified path in the container identified by the given name.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerCopy(name string, res string) (io.ReadCloser, error) {
|
2019-08-09 08:10:07 -04:00
|
|
|
ctr, err := daemon.GetContainer(name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:29:12 -04:00
|
|
|
// Make sure an online file-system operation is permitted.
|
2019-08-09 08:10:07 -04:00
|
|
|
if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
2017-07-19 10:20:13 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:10:07 -04:00
|
|
|
data, err := daemon.containerCopy(ctr, res)
|
2017-07-19 10:20:13 -04:00
|
|
|
if err == nil {
|
|
|
|
return data, nil
|
2017-03-15 14:29:12 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, containerFileNotFound{res, name}
|
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
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-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *types.ContainerPathStat, err error) {
|
2019-08-09 08:10:07 -04:00
|
|
|
ctr, err := daemon.GetContainer(name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:29:12 -04:00
|
|
|
// Make sure an online file-system operation is permitted.
|
2019-08-09 08:10:07 -04:00
|
|
|
if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
2017-03-15 14:29:12 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:10:07 -04:00
|
|
|
stat, err = daemon.containerStatPath(ctr, path)
|
2017-07-19 10:20:13 -04:00
|
|
|
if err == nil {
|
|
|
|
return stat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, containerFileNotFound{path, name}
|
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, errdefs.System(err)
|
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-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io.ReadCloser, stat *types.ContainerPathStat, err error) {
|
2019-08-09 08:10:07 -04:00
|
|
|
ctr, err := daemon.GetContainer(name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:29:12 -04:00
|
|
|
// Make sure an online file-system operation is permitted.
|
2019-08-09 08:10:07 -04:00
|
|
|
if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, nil, errdefs.System(err)
|
2017-07-19 10:20:13 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:10:07 -04:00
|
|
|
content, stat, err = daemon.containerArchivePath(ctr, path)
|
2017-07-19 10:20:13 -04:00
|
|
|
if err == nil {
|
|
|
|
return content, stat, nil
|
2017-03-15 14:29:12 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil, containerFileNotFound{path, name}
|
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, nil, errdefs.System(err)
|
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
|
2022-09-27 16:24:51 -04:00
|
|
|
// be an errdefs.InvalidParameter. If noOverwriteDirNonDir is true then it will
|
2015-05-12 21:21:26 -04:00
|
|
|
// be an error if unpacking the given content would cause an existing directory
|
|
|
|
// to be replaced with a non-directory and vice versa.
|
2016-11-14 08:37:08 -05:00
|
|
|
func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) error {
|
2019-08-09 08:10:07 -04:00
|
|
|
ctr, err := daemon.GetContainer(name)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-15 14:29:12 -04:00
|
|
|
// Make sure an online file-system operation is permitted.
|
2019-08-09 08:10:07 -04:00
|
|
|
if err := daemon.isOnlineFSOperationPermitted(ctr); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.System(err)
|
2017-03-15 14:29:12 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 08:10:07 -04:00
|
|
|
err = daemon.containerExtractToDir(ctr, path, copyUIDGID, noOverwriteDirNonDir, content)
|
2017-07-19 10:20:13 -04:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return containerFileNotFound{path, name}
|
|
|
|
}
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.System(err)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
// containerStatPath stats the filesystem resource at the specified path in this
|
2015-05-12 21:21:26 -04:00
|
|
|
// container. Returns stat info about the resource.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) containerStatPath(container *container.Container, path string) (stat *types.ContainerPathStat, err error) {
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
if err = daemon.Mount(container); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
defer daemon.Unmount(container)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
err = daemon.mountVolumes(container)
|
2016-10-03 13:53:06 -04:00
|
|
|
defer container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// Normalize path before sending to rootfs
|
2022-09-22 20:49:47 -04:00
|
|
|
path = filepath.FromSlash(path)
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
resolvedPath, absPath, err := container.ResolvePath(path)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
return container.StatPath(resolvedPath, absPath)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
// containerArchivePath creates an archive of the filesystem resource at the specified
|
2015-05-12 21:21:26 -04:00
|
|
|
// path in this container. Returns a tar archive of the resource and stat info
|
|
|
|
// about the resource.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) containerArchivePath(container *container.Container, 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-11-02 20:06:09 -05:00
|
|
|
if err = daemon.Mount(container); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// unmount any volumes
|
2016-10-03 13:53:06 -04:00
|
|
|
container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-05-12 21:21:26 -04:00
|
|
|
// unmount the container's rootfs
|
2015-11-02 20:06:09 -05:00
|
|
|
daemon.Unmount(container)
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
if err = daemon.mountVolumes(container); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// Normalize path before sending to rootfs
|
2022-09-22 20:49:47 -04:00
|
|
|
path = filepath.FromSlash(path)
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
resolvedPath, absPath, err := container.ResolvePath(path)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05: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.
|
2017-08-03 20:22:00 -04:00
|
|
|
|
|
|
|
// Get the source and the base paths of the container resolved path in order
|
|
|
|
// to get the proper tar options for the rebase tar.
|
2022-09-22 20:49:47 -04:00
|
|
|
resolvedPath = filepath.Clean(resolvedPath)
|
|
|
|
if filepath.Base(resolvedPath) == "." {
|
|
|
|
resolvedPath += string(filepath.Separator) + "."
|
2017-08-03 20:22:00 -04:00
|
|
|
}
|
2019-06-10 21:55:38 -04:00
|
|
|
|
|
|
|
sourceDir := resolvedPath
|
|
|
|
sourceBase := "."
|
|
|
|
|
|
|
|
if stat.Mode&os.ModeDir == 0 { // not dir
|
2022-09-22 20:49:47 -04:00
|
|
|
sourceDir, sourceBase = filepath.Split(resolvedPath)
|
2019-06-10 21:55:38 -04:00
|
|
|
}
|
2022-09-22 20:49:47 -04:00
|
|
|
opts := archive.TarResourceRebaseOpts(sourceBase, filepath.Base(absPath))
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2022-09-23 15:51:35 -04:00
|
|
|
data, err := chrootarchive.Tar(sourceDir, opts, container.BaseFS)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content = ioutils.NewReadCloserWrapper(data, func() error {
|
|
|
|
err := data.Close()
|
2016-10-03 13:53:06 -04:00
|
|
|
container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-11-02 20:06:09 -05:00
|
|
|
daemon.Unmount(container)
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "archive-path")
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
return content, stat, nil
|
|
|
|
}
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
// containerExtractToDir extracts the given tar archive to the specified location in the
|
2015-05-12 21:21:26 -04:00
|
|
|
// filesystem of this container. The given path must be of a directory in the
|
2022-09-27 16:24:51 -04:00
|
|
|
// container. If it is not, the error will be an errdefs.InvalidParameter. If
|
2015-05-12 21:21:26 -04:00
|
|
|
// 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.
|
2016-11-14 08:37:08 -05:00
|
|
|
func (daemon *Daemon) containerExtractToDir(container *container.Container, path string, copyUIDGID, noOverwriteDirNonDir bool, content io.Reader) (err error) {
|
2015-05-12 21:21:26 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-11-02 20:06:09 -05:00
|
|
|
if err = daemon.Mount(container); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-11-02 20:06:09 -05:00
|
|
|
defer daemon.Unmount(container)
|
2015-05-12 21:21:26 -04:00
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
err = daemon.mountVolumes(container)
|
2016-10-03 13:53:06 -04:00
|
|
|
defer container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// Normalize path before sending to rootfs'
|
2022-09-22 20:49:47 -04:00
|
|
|
path = filepath.FromSlash(path)
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2016-04-26 22:26:12 -04:00
|
|
|
// Check if a drive letter supplied, it must be the system drive. No-op except on Windows
|
2022-09-22 20:49:47 -04:00
|
|
|
path, err = system.CheckSystemDriveAndRemoveDriveLetter(path)
|
2016-04-26 22:26:12 -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
|
2015-11-12 14:55:17 -05:00
|
|
|
// that we do not use `container.ResolvePath(path)` here because we need
|
2015-07-24 17:12:55 -04:00
|
|
|
// 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.
|
2022-09-22 20:49:47 -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
|
|
|
|
}
|
|
|
|
|
2022-09-22 20:59:58 -04:00
|
|
|
stat, err := os.Lstat(resolvedPath)
|
2015-05-12 21:21:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
2022-09-27 16:24:51 -04:00
|
|
|
return errdefs.InvalidParameter(errors.New("extraction point is not a directory"))
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
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{`) {
|
2022-09-23 15:51:35 -04:00
|
|
|
if strings.HasPrefix(resolvedPath, container.BaseFS) {
|
|
|
|
baseRel = resolvedPath[len(container.BaseFS):]
|
2015-08-25 16:26:49 -04:00
|
|
|
if baseRel[:1] == `\` {
|
|
|
|
baseRel = baseRel[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-23 15:51:35 -04:00
|
|
|
baseRel, err = filepath.Rel(container.BaseFS, resolvedPath)
|
2015-08-25 16:26:49 -04:00
|
|
|
}
|
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.
|
2022-09-22 20:49:47 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if !toVolume && container.HostConfig.ReadonlyRootfs {
|
2022-09-26 14:44:36 -04:00
|
|
|
return errdefs.InvalidParameter(errors.New("container rootfs is marked read-only"))
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 08:37:08 -05:00
|
|
|
options := daemon.defaultTarCopyOptions(noOverwriteDirNonDir)
|
|
|
|
|
|
|
|
if copyUIDGID {
|
|
|
|
var err error
|
|
|
|
// tarCopyOptions will appropriately pull in the right uid/gid for the
|
|
|
|
// user/group and will set the options.
|
|
|
|
options, err = daemon.tarCopyOptions(container, noOverwriteDirNonDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-12 21:21:26 -04:00
|
|
|
}
|
2016-11-14 08:37:08 -05:00
|
|
|
|
2022-09-23 15:51:35 -04:00
|
|
|
if err := chrootarchive.UntarWithRoot(content, resolvedPath, options, container.BaseFS); err != nil {
|
2015-05-12 21:21:26 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "extract-to-dir")
|
2015-05-12 21:21:26 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-02 20:19:17 -05:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) containerCopy(container *container.Container, resource string) (rc io.ReadCloser, err error) {
|
2017-07-19 10:20:13 -04:00
|
|
|
if resource[0] == '/' || resource[0] == '\\' {
|
|
|
|
resource = resource[1:]
|
|
|
|
}
|
2015-11-02 20:19:17 -05: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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// unmount any volumes
|
2016-10-03 13:53:06 -04:00
|
|
|
container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-11-02 20:19:17 -05:00
|
|
|
// unmount the container's rootfs
|
|
|
|
daemon.Unmount(container)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
if err := daemon.mountVolumes(container); err != nil {
|
2015-11-02 20:19:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// Normalize path before sending to rootfs
|
2022-09-22 20:49:47 -04:00
|
|
|
resource = filepath.FromSlash(resource)
|
2017-08-03 20:22:00 -04:00
|
|
|
|
2015-11-02 20:19:17 -05:00
|
|
|
basePath, err := container.GetResourcePath(resource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-22 20:59:58 -04:00
|
|
|
stat, err := os.Stat(basePath)
|
2015-11-02 20:19:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var filter []string
|
|
|
|
if !stat.IsDir() {
|
2022-09-22 20:49:47 -04:00
|
|
|
d, f := filepath.Split(basePath)
|
2015-11-02 20:19:17 -05:00
|
|
|
basePath = d
|
|
|
|
filter = []string{f}
|
|
|
|
}
|
2022-09-23 15:51:35 -04:00
|
|
|
archv, err := chrootarchive.Tar(basePath, &archive.TarOptions{
|
2015-11-02 20:19:17 -05:00
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
IncludeFiles: filter,
|
2022-09-23 14:09:51 -04:00
|
|
|
}, container.BaseFS)
|
2015-11-02 20:19:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-09 08:10:07 -04:00
|
|
|
reader := ioutils.NewReadCloserWrapper(archv, func() error {
|
|
|
|
err := archv.Close()
|
2016-10-03 13:53:06 -04:00
|
|
|
container.DetachAndUnmount(daemon.LogVolumeEvent)
|
2015-11-02 20:19:17 -05:00
|
|
|
daemon.Unmount(container)
|
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
})
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "copy")
|
2015-11-02 20:19:17 -05:00
|
|
|
return reader, nil
|
2017-05-25 17:03:29 -04:00
|
|
|
}
|