2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-05-13 21:37:11 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-03-24 23:57:23 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-08-26 19:39:16 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
type copyDirection int
|
|
|
|
|
|
|
|
const (
|
|
|
|
fromContainer copyDirection = (1 << iota)
|
|
|
|
toContainer
|
|
|
|
acrossContainers = fromContainer | toContainer
|
|
|
|
)
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
type cpConfig struct {
|
|
|
|
followLink bool
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
// CmdCp copies files/folders to or from a path in a container.
|
2015-03-25 13:34:41 -04:00
|
|
|
//
|
2015-10-01 03:56:39 -04:00
|
|
|
// When copying from a container, if DEST_PATH is '-' the data is written as a
|
2015-05-13 21:37:11 -04:00
|
|
|
// tar archive file to STDOUT.
|
2015-03-25 13:34:41 -04:00
|
|
|
//
|
2015-10-01 03:56:39 -04:00
|
|
|
// When copying to a container, if SRC_PATH is '-' the data is read as a tar
|
|
|
|
// archive file from STDIN, and the destination CONTAINER:DEST_PATH, must specify
|
2015-05-13 21:37:11 -04:00
|
|
|
// a directory.
|
|
|
|
//
|
|
|
|
// Usage:
|
2015-10-01 03:56:39 -04:00
|
|
|
// docker cp CONTAINER:SRC_PATH DEST_PATH|-
|
|
|
|
// docker cp SRC_PATH|- CONTAINER:DEST_PATH
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdCp(args ...string) error {
|
2015-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd(
|
2015-05-13 21:37:11 -04:00
|
|
|
"cp",
|
2015-10-01 03:56:39 -04:00
|
|
|
[]string{"CONTAINER:SRC_PATH DEST_PATH|-", "SRC_PATH|- CONTAINER:DEST_PATH"},
|
2015-05-13 21:37:11 -04:00
|
|
|
strings.Join([]string{
|
2015-10-08 08:46:21 -04:00
|
|
|
Cli.DockerCommands["cp"].Description,
|
|
|
|
"\nUse '-' as the source to read a tar archive from stdin\n",
|
2015-05-13 21:37:11 -04:00
|
|
|
"and extract it to a directory destination in a container.\n",
|
|
|
|
"Use '-' as the destination to stream a tar archive of a\n",
|
|
|
|
"container source to stdout.",
|
|
|
|
}, ""),
|
|
|
|
true,
|
|
|
|
)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
followLink := cmd.Bool([]string{"L", "-follow-link"}, false, "Always follow symbol link in SRC_PATH")
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
cmd.Require(flag.Exact, 2)
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
if cmd.Arg(0) == "" {
|
|
|
|
return fmt.Errorf("source can not be empty")
|
|
|
|
}
|
|
|
|
if cmd.Arg(1) == "" {
|
|
|
|
return fmt.Errorf("destination can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
srcContainer, srcPath := splitCpArg(cmd.Arg(0))
|
|
|
|
dstContainer, dstPath := splitCpArg(cmd.Arg(1))
|
|
|
|
|
|
|
|
var direction copyDirection
|
|
|
|
if srcContainer != "" {
|
|
|
|
direction |= fromContainer
|
|
|
|
}
|
|
|
|
if dstContainer != "" {
|
|
|
|
direction |= toContainer
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
cpParam := &cpConfig{
|
|
|
|
followLink: *followLink,
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
switch direction {
|
|
|
|
case fromContainer:
|
2015-10-01 03:56:39 -04:00
|
|
|
return cli.copyFromContainer(srcContainer, srcPath, dstPath, cpParam)
|
2015-05-13 21:37:11 -04:00
|
|
|
case toContainer:
|
2015-10-01 03:56:39 -04:00
|
|
|
return cli.copyToContainer(srcPath, dstContainer, dstPath, cpParam)
|
2015-05-13 21:37:11 -04:00
|
|
|
case acrossContainers:
|
|
|
|
// Copying between containers isn't supported.
|
|
|
|
return fmt.Errorf("copying between containers is not supported")
|
|
|
|
default:
|
|
|
|
// User didn't specify any container.
|
|
|
|
return fmt.Errorf("must specify at least one container source")
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
// We use `:` as a delimiter between CONTAINER and PATH, but `:` could also be
|
|
|
|
// in a valid LOCALPATH, like `file:name.txt`. We can resolve this ambiguity by
|
|
|
|
// requiring a LOCALPATH with a `:` to be made explicit with a relative or
|
|
|
|
// absolute path:
|
|
|
|
// `/path/to/file:name.txt` or `./file:name.txt`
|
|
|
|
//
|
|
|
|
// This is apparently how `scp` handles this as well:
|
|
|
|
// http://www.cyberciti.biz/faq/rsync-scp-file-name-with-colon-punctuation-in-it/
|
|
|
|
//
|
|
|
|
// We can't simply check for a filepath separator because container names may
|
|
|
|
// have a separator, e.g., "host0/cname1" if container is in a Docker cluster,
|
|
|
|
// so we have to check for a `/` or `.` prefix. Also, in the case of a Windows
|
|
|
|
// client, a `:` could be part of an absolute Windows path, in which case it
|
|
|
|
// is immediately proceeded by a backslash.
|
|
|
|
func splitCpArg(arg string) (container, path string) {
|
2015-08-26 19:39:16 -04:00
|
|
|
if system.IsAbs(arg) {
|
2015-05-13 21:37:11 -04:00
|
|
|
// Explicit local absolute path, e.g., `C:\foo` or `/foo`.
|
|
|
|
return "", arg
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
parts := strings.SplitN(arg, ":", 2)
|
|
|
|
|
|
|
|
if len(parts) == 1 || strings.HasPrefix(parts[0], ".") {
|
|
|
|
// Either there's no `:` in the arg
|
|
|
|
// OR it's an explicit local relative path like `./file:name.txt`.
|
|
|
|
return "", arg
|
2015-04-09 16:05:31 -04:00
|
|
|
}
|
2015-05-13 21:37:11 -04:00
|
|
|
|
|
|
|
return parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) statContainerPath(containerName, path string) (types.ContainerPathStat, error) {
|
2015-12-04 17:02:06 -05:00
|
|
|
return cli.client.ContainerStatPath(containerName, path)
|
2015-05-13 21:37:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolveLocalPath(localPath string) (absPath string, err error) {
|
|
|
|
if absPath, err = filepath.Abs(localPath); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
func (cli *DockerCli) copyFromContainer(srcContainer, srcPath, dstPath string, cpParam *cpConfig) (err error) {
|
2015-05-13 21:37:11 -04:00
|
|
|
if dstPath != "-" {
|
|
|
|
// Get an absolute destination path.
|
|
|
|
dstPath, err = resolveLocalPath(dstPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
// if client requests to follow symbol link, then must decide target file to be copied
|
|
|
|
var rebaseName string
|
|
|
|
if cpParam.followLink {
|
|
|
|
srcStat, err := cli.statContainerPath(srcContainer, srcPath)
|
|
|
|
|
|
|
|
// If the destination is a symbolic link, we should follow it.
|
|
|
|
if err == nil && srcStat.Mode&os.ModeSymlink != 0 {
|
|
|
|
linkTarget := srcStat.LinkTarget
|
|
|
|
if !system.IsAbs(linkTarget) {
|
|
|
|
// Join with the parent directory.
|
|
|
|
srcParent, _ := archive.SplitPathDirEntry(srcPath)
|
|
|
|
linkTarget = filepath.Join(srcParent, linkTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkTarget, rebaseName = archive.GetRebaseName(srcPath, linkTarget)
|
|
|
|
srcPath = linkTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
content, stat, err := cli.client.CopyFromContainer(context.Background(), srcContainer, srcPath)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-02 23:56:03 -05:00
|
|
|
defer content.Close()
|
2015-05-13 21:37:11 -04:00
|
|
|
|
|
|
|
if dstPath == "-" {
|
|
|
|
// Send the response to STDOUT.
|
2015-12-02 23:56:03 -05:00
|
|
|
_, err = io.Copy(os.Stdout, content)
|
2015-05-13 21:37:11 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare source copy info.
|
|
|
|
srcInfo := archive.CopyInfo{
|
2015-10-01 03:56:39 -04:00
|
|
|
Path: srcPath,
|
|
|
|
Exists: true,
|
|
|
|
IsDir: stat.Mode.IsDir(),
|
|
|
|
RebaseName: rebaseName,
|
2015-05-13 21:37:11 -04:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-02 23:56:03 -05:00
|
|
|
preArchive := content
|
2015-10-01 03:56:39 -04:00
|
|
|
if len(srcInfo.RebaseName) != 0 {
|
|
|
|
_, srcBase := archive.SplitPathDirEntry(srcInfo.Path)
|
2015-12-02 23:56:03 -05:00
|
|
|
preArchive = archive.RebaseArchiveEntries(content, srcBase, srcInfo.RebaseName)
|
2015-10-01 03:56:39 -04:00
|
|
|
}
|
2015-05-13 21:37:11 -04:00
|
|
|
// See comments in the implementation of `archive.CopyTo` for exactly what
|
|
|
|
// goes into deciding how and whether the source archive needs to be
|
|
|
|
// altered for the correct copy behavior.
|
2015-10-01 03:56:39 -04:00
|
|
|
return archive.CopyTo(preArchive, srcInfo, dstPath)
|
2015-05-13 21:37:11 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string, cpParam *cpConfig) (err error) {
|
2015-05-13 21:37:11 -04:00
|
|
|
if srcPath != "-" {
|
|
|
|
// Get an absolute source path.
|
|
|
|
srcPath, err = resolveLocalPath(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-05-13 21:37:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to get the copy behavior right, we need to know information
|
|
|
|
// about both the source and destination. The API is a simple tar
|
|
|
|
// archive/extract API but we can use the stat info header about the
|
|
|
|
// destination to be more informed about exactly what the destination is.
|
|
|
|
|
|
|
|
// Prepare destination copy info by stat-ing the container path.
|
|
|
|
dstInfo := archive.CopyInfo{Path: dstPath}
|
|
|
|
dstStat, err := cli.statContainerPath(dstContainer, dstPath)
|
2015-07-24 17:12:55 -04:00
|
|
|
|
|
|
|
// If the destination is a symbolic link, we should evaluate it.
|
|
|
|
if err == nil && dstStat.Mode&os.ModeSymlink != 0 {
|
|
|
|
linkTarget := dstStat.LinkTarget
|
2015-08-26 19:39:16 -04:00
|
|
|
if !system.IsAbs(linkTarget) {
|
2015-07-24 17:12:55 -04:00
|
|
|
// Join with the parent directory.
|
|
|
|
dstParent, _ := archive.SplitPathDirEntry(dstPath)
|
|
|
|
linkTarget = filepath.Join(dstParent, linkTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstInfo.Path = linkTarget
|
|
|
|
dstStat, err = cli.statContainerPath(dstContainer, linkTarget)
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
// Ignore any error and assume that the parent directory of the destination
|
|
|
|
// path exists, in which case the copy may still succeed. If there is any
|
|
|
|
// type of conflict (e.g., non-directory overwriting an existing directory
|
2015-12-13 11:00:39 -05:00
|
|
|
// or vice versa) the extraction will fail. If the destination simply did
|
2015-05-13 21:37:11 -04:00
|
|
|
// not exist, but the parent directory does, the extraction will still
|
|
|
|
// succeed.
|
|
|
|
if err == nil {
|
|
|
|
dstInfo.Exists, dstInfo.IsDir = true, dstStat.Mode.IsDir()
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
var (
|
|
|
|
content io.Reader
|
|
|
|
resolvedDstPath string
|
|
|
|
)
|
|
|
|
|
2015-05-13 21:37:11 -04:00
|
|
|
if srcPath == "-" {
|
|
|
|
// Use STDIN.
|
|
|
|
content = os.Stdin
|
2015-07-24 17:12:55 -04:00
|
|
|
resolvedDstPath = dstInfo.Path
|
2015-05-13 21:37:11 -04:00
|
|
|
if !dstInfo.IsDir {
|
|
|
|
return fmt.Errorf("destination %q must be a directory", fmt.Sprintf("%s:%s", dstContainer, dstPath))
|
|
|
|
}
|
|
|
|
} else {
|
2015-07-24 17:12:55 -04:00
|
|
|
// Prepare source copy info.
|
2015-10-01 03:56:39 -04:00
|
|
|
srcInfo, err := archive.CopyInfoSourcePath(srcPath, cpParam.followLink)
|
2015-07-24 17:12:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcArchive, err := archive.TarResource(srcInfo)
|
2015-05-13 21:37:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcArchive.Close()
|
|
|
|
|
|
|
|
// With the stat info about the local source as well as the
|
|
|
|
// destination, we have enough information to know whether we need to
|
|
|
|
// alter the archive that we upload so that when the server extracts
|
2015-12-13 11:00:39 -05:00
|
|
|
// it to the specified directory in the container we get the desired
|
2015-05-13 21:37:11 -04:00
|
|
|
// copy behavior.
|
|
|
|
|
|
|
|
// See comments in the implementation of `archive.PrepareArchiveCopy`
|
|
|
|
// for exactly what goes into deciding how and whether the source
|
|
|
|
// archive needs to be altered for the correct copy behavior when it is
|
|
|
|
// extracted. This function also infers from the source and destination
|
|
|
|
// info which directory to extract to, which may be the parent of the
|
|
|
|
// destination that the user specified.
|
|
|
|
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-13 21:37:11 -04:00
|
|
|
defer preparedArchive.Close()
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
resolvedDstPath = dstDir
|
2015-05-13 21:37:11 -04:00
|
|
|
content = preparedArchive
|
|
|
|
}
|
|
|
|
|
2015-12-04 17:02:06 -05:00
|
|
|
options := types.CopyToContainerOptions{
|
2015-12-02 23:56:03 -05:00
|
|
|
ContainerID: dstContainer,
|
|
|
|
Path: resolvedDstPath,
|
|
|
|
Content: content,
|
|
|
|
AllowOverwriteDirWithFile: false,
|
2015-05-13 21:37:11 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
return cli.client.CopyToContainer(context.Background(), options)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|