2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2015-01-19 15:15:36 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2015-08-26 19:39:16 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-01-19 15:15:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Errors used or returned by this file.
|
|
|
|
var (
|
|
|
|
ErrNotDirectory = errors.New("not a directory")
|
|
|
|
ErrDirNotExists = errors.New("no such directory")
|
|
|
|
ErrCannotCopyDir = errors.New("cannot copy directory")
|
|
|
|
ErrInvalidCopySource = errors.New("invalid copy source content")
|
|
|
|
)
|
|
|
|
|
|
|
|
// PreserveTrailingDotOrSeparator returns the given cleaned path (after
|
|
|
|
// processing using any utility functions from the path or filepath stdlib
|
|
|
|
// packages) and appends a trailing `/.` or `/` if its corresponding original
|
|
|
|
// path (from before being processed by utility functions from the path or
|
|
|
|
// filepath stdlib packages) ends with a trailing `/.` or `/`. If the cleaned
|
|
|
|
// path already ends in a `.` path segment, then another is not added. If the
|
2017-08-03 20:22:00 -04:00
|
|
|
// clean path already ends in the separator, then another is not added.
|
|
|
|
func PreserveTrailingDotOrSeparator(cleanedPath string, originalPath string, sep byte) string {
|
2015-07-28 22:55:14 -04:00
|
|
|
// Ensure paths are in platform semantics
|
2017-08-03 20:22:00 -04:00
|
|
|
cleanedPath = strings.Replace(cleanedPath, "/", string(sep), -1)
|
|
|
|
originalPath = strings.Replace(originalPath, "/", string(sep), -1)
|
2015-07-28 22:55:14 -04:00
|
|
|
|
|
|
|
if !specifiesCurrentDir(cleanedPath) && specifiesCurrentDir(originalPath) {
|
2017-08-03 20:22:00 -04:00
|
|
|
if !hasTrailingPathSeparator(cleanedPath, sep) {
|
2015-01-19 15:15:36 -05:00
|
|
|
// Add a separator if it doesn't already end with one (a cleaned
|
|
|
|
// path would only end in a separator if it is the root).
|
2017-08-03 20:22:00 -04:00
|
|
|
cleanedPath += string(sep)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
cleanedPath += "."
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
if !hasTrailingPathSeparator(cleanedPath, sep) && hasTrailingPathSeparator(originalPath, sep) {
|
|
|
|
cleanedPath += string(sep)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return cleanedPath
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:55:14 -04:00
|
|
|
// assertsDirectory returns whether the given path is
|
2015-01-19 15:15:36 -05:00
|
|
|
// asserted to be a directory, i.e., the path ends with
|
|
|
|
// a trailing '/' or `/.`, assuming a path separator of `/`.
|
2017-08-03 20:22:00 -04:00
|
|
|
func assertsDirectory(path string, sep byte) bool {
|
|
|
|
return hasTrailingPathSeparator(path, sep) || specifiesCurrentDir(path)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 22:55:14 -04:00
|
|
|
// hasTrailingPathSeparator returns whether the given
|
2015-01-19 15:15:36 -05:00
|
|
|
// path ends with the system's path separator character.
|
2017-08-03 20:22:00 -04:00
|
|
|
func hasTrailingPathSeparator(path string, sep byte) bool {
|
|
|
|
return len(path) > 0 && path[len(path)-1] == sep
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 22:55:14 -04:00
|
|
|
// specifiesCurrentDir returns whether the given path specifies
|
2015-01-19 15:15:36 -05:00
|
|
|
// a "current directory", i.e., the last path segment is `.`.
|
2015-07-28 22:55:14 -04:00
|
|
|
func specifiesCurrentDir(path string) bool {
|
2015-01-19 15:15:36 -05:00
|
|
|
return filepath.Base(path) == "."
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// SplitPathDirEntry splits the given path between its directory name and its
|
|
|
|
// basename by first cleaning the path but preserves a trailing "." if the
|
|
|
|
// original path specified the current directory.
|
|
|
|
func SplitPathDirEntry(path string) (dir, base string) {
|
2017-08-03 20:22:00 -04:00
|
|
|
cleanedPath := filepath.Clean(filepath.FromSlash(path))
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2015-07-28 22:55:14 -04:00
|
|
|
if specifiesCurrentDir(path) {
|
2017-08-03 20:22:00 -04:00
|
|
|
cleanedPath += string(os.PathSeparator) + "."
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
return filepath.Dir(cleanedPath), filepath.Base(cleanedPath)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// TarResource archives the resource described by the given CopyInfo to a Tar
|
2015-01-19 15:15:36 -05:00
|
|
|
// archive. A non-nil error is returned if sourcePath does not exist or is
|
|
|
|
// asserted to be a directory but exists as another type of file.
|
|
|
|
//
|
|
|
|
// This function acts as a convenient wrapper around TarWithOptions, which
|
|
|
|
// requires a directory as the source path. TarResource accepts either a
|
|
|
|
// directory or a file path and correctly sets the Tar options.
|
2016-10-20 19:40:59 -04:00
|
|
|
func TarResource(sourceInfo CopyInfo) (content io.ReadCloser, err error) {
|
2015-07-24 17:12:55 -04:00
|
|
|
return TarResourceRebase(sourceInfo.Path, sourceInfo.RebaseName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TarResourceRebase is like TarResource but renames the first path element of
|
|
|
|
// items in the resulting tar archive to match the given rebaseName if not "".
|
2016-10-20 19:40:59 -04:00
|
|
|
func TarResourceRebase(sourcePath, rebaseName string) (content io.ReadCloser, err error) {
|
2015-07-28 22:55:14 -04:00
|
|
|
sourcePath = normalizePath(sourcePath)
|
2015-01-19 15:15:36 -05:00
|
|
|
if _, err = os.Lstat(sourcePath); err != nil {
|
|
|
|
// Catches the case where the source does not exist or is not a
|
|
|
|
// directory if asserted to be a directory, as this also causes an
|
|
|
|
// error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-21 06:03:37 -04:00
|
|
|
// Separate the source path between its directory and
|
2015-01-19 15:15:36 -05:00
|
|
|
// the entry in that directory which we are archiving.
|
|
|
|
sourceDir, sourceBase := SplitPathDirEntry(sourcePath)
|
2017-08-03 20:22:00 -04:00
|
|
|
opts := TarResourceRebaseOpts(sourceBase, rebaseName)
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf("copying %q from %q", sourceBase, sourceDir)
|
2017-08-03 20:22:00 -04:00
|
|
|
return TarWithOptions(sourceDir, opts)
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// TarResourceRebaseOpts does not preform the Tar, but instead just creates the rebase
|
|
|
|
// parameters to be sent to TarWithOptions (the TarOptions struct)
|
|
|
|
func TarResourceRebaseOpts(sourceBase string, rebaseName string) *TarOptions {
|
|
|
|
filter := []string{sourceBase}
|
|
|
|
return &TarOptions{
|
2015-01-19 15:15:36 -05:00
|
|
|
Compression: Uncompressed,
|
|
|
|
IncludeFiles: filter,
|
|
|
|
IncludeSourceDir: true,
|
2015-07-24 17:12:55 -04:00
|
|
|
RebaseNames: map[string]string{
|
|
|
|
sourceBase: rebaseName,
|
|
|
|
},
|
2017-08-03 20:22:00 -04:00
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// CopyInfo holds basic info about the source
|
|
|
|
// or destination path of a copy operation.
|
|
|
|
type CopyInfo struct {
|
2015-07-24 17:12:55 -04:00
|
|
|
Path string
|
|
|
|
Exists bool
|
|
|
|
IsDir bool
|
|
|
|
RebaseName string
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// CopyInfoSourcePath stats the given path to create a CopyInfo
|
|
|
|
// struct representing that resource for the source of an archive copy
|
|
|
|
// operation. The given path should be an absolute local path. A source path
|
|
|
|
// has all symlinks evaluated that appear before the last path separator ("/"
|
|
|
|
// on Unix). As it is to be a copy source, the path must exist.
|
2015-10-01 03:56:39 -04:00
|
|
|
func CopyInfoSourcePath(path string, followLink bool) (CopyInfo, error) {
|
|
|
|
// normalize the file path and then evaluate the symbol link
|
|
|
|
// we will use the target file instead of the symbol link if
|
|
|
|
// followLink is set
|
2015-07-28 22:55:14 -04:00
|
|
|
path = normalizePath(path)
|
2015-07-24 17:12:55 -04:00
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
resolvedPath, rebaseName, err := ResolveHostSourcePath(path, followLink)
|
2015-07-24 17:12:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return CopyInfo{}, err
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
stat, err := os.Lstat(resolvedPath)
|
|
|
|
if err != nil {
|
|
|
|
return CopyInfo{}, err
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
return CopyInfo{
|
|
|
|
Path: resolvedPath,
|
|
|
|
Exists: true,
|
|
|
|
IsDir: stat.IsDir(),
|
|
|
|
RebaseName: rebaseName,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyInfoDestinationPath stats the given path to create a CopyInfo
|
|
|
|
// struct representing that resource for the destination of an archive copy
|
|
|
|
// operation. The given path should be an absolute local path.
|
|
|
|
func CopyInfoDestinationPath(path string) (info CopyInfo, err error) {
|
|
|
|
maxSymlinkIter := 10 // filepath.EvalSymlinks uses 255, but 10 already seems like a lot.
|
2015-07-28 22:55:14 -04:00
|
|
|
path = normalizePath(path)
|
2015-07-24 17:12:55 -04:00
|
|
|
originalPath := path
|
|
|
|
|
|
|
|
stat, err := os.Lstat(path)
|
|
|
|
|
|
|
|
if err == nil && stat.Mode()&os.ModeSymlink == 0 {
|
|
|
|
// The path exists and is not a symlink.
|
|
|
|
return CopyInfo{
|
|
|
|
Path: path,
|
|
|
|
Exists: true,
|
|
|
|
IsDir: stat.IsDir(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// While the path is a symlink.
|
|
|
|
for n := 0; err == nil && stat.Mode()&os.ModeSymlink != 0; n++ {
|
|
|
|
if n > maxSymlinkIter {
|
|
|
|
// Don't follow symlinks more than this arbitrary number of times.
|
|
|
|
return CopyInfo{}, errors.New("too many symlinks in " + originalPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The path is a symbolic link. We need to evaluate it so that the
|
|
|
|
// destination of the copy operation is the link target and not the
|
|
|
|
// link itself. This is notably different than CopyInfoSourcePath which
|
|
|
|
// only evaluates symlinks before the last appearing path separator.
|
|
|
|
// Also note that it is okay if the last path element is a broken
|
|
|
|
// symlink as the copy operation should create the target.
|
|
|
|
var linkTarget string
|
|
|
|
|
|
|
|
linkTarget, err = os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return CopyInfo{}, err
|
|
|
|
}
|
|
|
|
|
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, _ := SplitPathDirEntry(path)
|
|
|
|
linkTarget = filepath.Join(dstParent, linkTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
path = linkTarget
|
|
|
|
stat, err = os.Lstat(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// It's okay if the destination path doesn't exist. We can still
|
|
|
|
// continue the copy operation if the parent directory exists.
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return CopyInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure destination parent dir exists.
|
|
|
|
dstParent, _ := SplitPathDirEntry(path)
|
|
|
|
|
2017-03-19 22:39:40 -04:00
|
|
|
parentDirStat, err := os.Stat(dstParent)
|
2015-07-24 17:12:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return CopyInfo{}, err
|
|
|
|
}
|
|
|
|
if !parentDirStat.IsDir() {
|
|
|
|
return CopyInfo{}, ErrNotDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
return CopyInfo{Path: path}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The path exists after resolving symlinks.
|
|
|
|
return CopyInfo{
|
|
|
|
Path: path,
|
|
|
|
Exists: true,
|
|
|
|
IsDir: stat.IsDir(),
|
|
|
|
}, nil
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareArchiveCopy prepares the given srcContent archive, which should
|
|
|
|
// contain the archived resource described by srcInfo, to the destination
|
|
|
|
// described by dstInfo. Returns the possibly modified content archive along
|
|
|
|
// with the path to the destination directory which it should be extracted to.
|
2016-10-20 19:40:59 -04:00
|
|
|
func PrepareArchiveCopy(srcContent io.Reader, srcInfo, dstInfo CopyInfo) (dstDir string, content io.ReadCloser, err error) {
|
2015-07-28 22:55:14 -04:00
|
|
|
// Ensure in platform semantics
|
|
|
|
srcInfo.Path = normalizePath(srcInfo.Path)
|
|
|
|
dstInfo.Path = normalizePath(dstInfo.Path)
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
// Separate the destination path between its directory and base
|
|
|
|
// components in case the source archive contents need to be rebased.
|
|
|
|
dstDir, dstBase := SplitPathDirEntry(dstInfo.Path)
|
|
|
|
_, srcBase := SplitPathDirEntry(srcInfo.Path)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case dstInfo.Exists && dstInfo.IsDir:
|
|
|
|
// The destination exists as a directory. No alteration
|
|
|
|
// to srcContent is needed as its contents can be
|
|
|
|
// simply extracted to the destination directory.
|
|
|
|
return dstInfo.Path, ioutil.NopCloser(srcContent), nil
|
|
|
|
case dstInfo.Exists && srcInfo.IsDir:
|
|
|
|
// The destination exists as some type of file and the source
|
|
|
|
// content is a directory. This is an error condition since
|
|
|
|
// you cannot copy a directory to an existing file location.
|
|
|
|
return "", nil, ErrCannotCopyDir
|
|
|
|
case dstInfo.Exists:
|
|
|
|
// The destination exists as some type of file and the source content
|
|
|
|
// is also a file. The source content entry will have to be renamed to
|
|
|
|
// have a basename which matches the destination path's basename.
|
2015-10-01 03:56:39 -04:00
|
|
|
if len(srcInfo.RebaseName) != 0 {
|
|
|
|
srcBase = srcInfo.RebaseName
|
|
|
|
}
|
|
|
|
return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
|
2015-01-19 15:15:36 -05:00
|
|
|
case srcInfo.IsDir:
|
|
|
|
// The destination does not exist and the source content is an archive
|
|
|
|
// of a directory. The archive should be extracted to the parent of
|
|
|
|
// the destination path instead, and when it is, the directory that is
|
|
|
|
// created as a result should take the name of the destination path.
|
|
|
|
// The source content entries will have to be renamed to have a
|
|
|
|
// basename which matches the destination path's basename.
|
2015-10-01 03:56:39 -04:00
|
|
|
if len(srcInfo.RebaseName) != 0 {
|
|
|
|
srcBase = srcInfo.RebaseName
|
|
|
|
}
|
|
|
|
return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
|
2017-08-03 20:22:00 -04:00
|
|
|
case assertsDirectory(dstInfo.Path, os.PathSeparator):
|
2015-01-19 15:15:36 -05:00
|
|
|
// The destination does not exist and is asserted to be created as a
|
|
|
|
// directory, but the source content is not a directory. This is an
|
|
|
|
// error condition since you cannot create a directory from a file
|
|
|
|
// source.
|
|
|
|
return "", nil, ErrDirNotExists
|
|
|
|
default:
|
|
|
|
// The last remaining case is when the destination does not exist, is
|
|
|
|
// not asserted to be a directory, and the source content is not an
|
|
|
|
// archive of a directory. It this case, the destination file will need
|
|
|
|
// to be created when the archive is extracted and the source content
|
|
|
|
// entry will have to be renamed to have a basename which matches the
|
|
|
|
// destination path's basename.
|
2015-10-01 03:56:39 -04:00
|
|
|
if len(srcInfo.RebaseName) != 0 {
|
|
|
|
srcBase = srcInfo.RebaseName
|
|
|
|
}
|
|
|
|
return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
// RebaseArchiveEntries rewrites the given srcContent archive replacing
|
2015-08-07 18:24:18 -04:00
|
|
|
// an occurrence of oldBase with newBase at the beginning of entry names.
|
2016-10-20 19:40:59 -04:00
|
|
|
func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.ReadCloser {
|
2015-07-28 22:55:14 -04:00
|
|
|
if oldBase == string(os.PathSeparator) {
|
2015-07-24 17:12:55 -04:00
|
|
|
// If oldBase specifies the root directory, use an empty string as
|
|
|
|
// oldBase instead so that newBase doesn't replace the path separator
|
|
|
|
// that all paths will start with.
|
|
|
|
oldBase = ""
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
rebased, w := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
srcTar := tar.NewReader(srcContent)
|
|
|
|
rebasedTar := tar.NewWriter(w)
|
|
|
|
|
|
|
|
for {
|
|
|
|
hdr, err := srcTar.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// Signals end of archive.
|
|
|
|
rebasedTar.Close()
|
|
|
|
w.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
w.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr.Name = strings.Replace(hdr.Name, oldBase, newBase, 1)
|
2017-07-06 05:03:38 -04:00
|
|
|
if hdr.Typeflag == tar.TypeLink {
|
|
|
|
hdr.Linkname = strings.Replace(hdr.Linkname, oldBase, newBase, 1)
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
|
|
|
if err = rebasedTar.WriteHeader(hdr); err != nil {
|
|
|
|
w.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.Copy(rebasedTar, srcTar); err != nil {
|
|
|
|
w.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rebased
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
// TODO @gupta-ak. These might have to be changed in the future to be
|
|
|
|
// continuity driver aware as well to support LCOW.
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
// CopyResource performs an archive copy from the given source path to the
|
|
|
|
// given destination path. The source path MUST exist and the destination
|
|
|
|
// path's parent directory must exist.
|
2015-10-01 03:56:39 -04:00
|
|
|
func CopyResource(srcPath, dstPath string, followLink bool) error {
|
2015-01-19 15:15:36 -05:00
|
|
|
var (
|
|
|
|
srcInfo CopyInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-07-28 22:55:14 -04:00
|
|
|
// Ensure in platform semantics
|
|
|
|
srcPath = normalizePath(srcPath)
|
|
|
|
dstPath = normalizePath(dstPath)
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
// Clean the source and destination paths.
|
2017-08-03 20:22:00 -04:00
|
|
|
srcPath = PreserveTrailingDotOrSeparator(filepath.Clean(srcPath), srcPath, os.PathSeparator)
|
|
|
|
dstPath = PreserveTrailingDotOrSeparator(filepath.Clean(dstPath), dstPath, os.PathSeparator)
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2015-10-01 03:56:39 -04:00
|
|
|
if srcInfo, err = CopyInfoSourcePath(srcPath, followLink); err != nil {
|
2015-01-19 15:15:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
content, err := TarResource(srcInfo)
|
2015-01-19 15:15:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer content.Close()
|
|
|
|
|
|
|
|
return CopyTo(content, srcInfo, dstPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTo handles extracting the given content whose
|
|
|
|
// entries should be sourced from srcInfo to dstPath.
|
2016-10-20 19:40:59 -04:00
|
|
|
func CopyTo(content io.Reader, srcInfo CopyInfo, dstPath string) error {
|
2015-07-24 17:12:55 -04:00
|
|
|
// The destination path need not exist, but CopyInfoDestinationPath will
|
|
|
|
// ensure that at least the parent directory exists.
|
2015-07-28 22:55:14 -04:00
|
|
|
dstInfo, err := CopyInfoDestinationPath(normalizePath(dstPath))
|
2015-01-19 15:15:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstDir, copyArchive, err := PrepareArchiveCopy(content, srcInfo, dstInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer copyArchive.Close()
|
|
|
|
|
|
|
|
options := &TarOptions{
|
|
|
|
NoLchown: true,
|
|
|
|
NoOverwriteDirNonDir: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Untar(copyArchive, dstDir, options)
|
|
|
|
}
|
2015-10-01 03:56:39 -04:00
|
|
|
|
|
|
|
// ResolveHostSourcePath decides real path need to be copied with parameters such as
|
|
|
|
// whether to follow symbol link or not, if followLink is true, resolvedPath will return
|
|
|
|
// link target of any symbol link file, else it will only resolve symlink of directory
|
|
|
|
// but return symbol link file itself without resolving.
|
|
|
|
func ResolveHostSourcePath(path string, followLink bool) (resolvedPath, rebaseName string, err error) {
|
|
|
|
if followLink {
|
|
|
|
resolvedPath, err = filepath.EvalSymlinks(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvedPath, rebaseName = GetRebaseName(path, resolvedPath)
|
|
|
|
} else {
|
|
|
|
dirPath, basePath := filepath.Split(path)
|
|
|
|
|
|
|
|
// if not follow symbol link, then resolve symbol link of parent dir
|
|
|
|
var resolvedDirPath string
|
|
|
|
resolvedDirPath, err = filepath.EvalSymlinks(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 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
|
2017-08-03 20:22:00 -04:00
|
|
|
if hasTrailingPathSeparator(path, os.PathSeparator) &&
|
|
|
|
filepath.Base(path) != filepath.Base(resolvedPath) {
|
2015-10-01 03:56:39 -04:00
|
|
|
rebaseName = filepath.Base(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolvedPath, rebaseName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRebaseName normalizes and compares path and resolvedPath,
|
|
|
|
// return completed resolved path and rebased file name
|
|
|
|
func GetRebaseName(path, resolvedPath string) (string, string) {
|
|
|
|
// linkTarget will have been cleaned (no trailing path separators and dot) so
|
|
|
|
// we can manually join it with them
|
|
|
|
var rebaseName string
|
2017-08-03 20:22:00 -04:00
|
|
|
if specifiesCurrentDir(path) &&
|
|
|
|
!specifiesCurrentDir(resolvedPath) {
|
2015-10-01 03:56:39 -04:00
|
|
|
resolvedPath += string(filepath.Separator) + "."
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
if hasTrailingPathSeparator(path, os.PathSeparator) &&
|
|
|
|
!hasTrailingPathSeparator(resolvedPath, os.PathSeparator) {
|
2015-10-01 03:56:39 -04:00
|
|
|
resolvedPath += string(filepath.Separator)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Base(path) != filepath.Base(resolvedPath) {
|
|
|
|
// In the case where the path had a trailing separator and a symlink
|
|
|
|
// evaluation has changed the last path component, we will need to
|
|
|
|
// rebase the name in the archive that is being copied to match the
|
|
|
|
// originally requested name.
|
|
|
|
rebaseName = filepath.Base(path)
|
|
|
|
}
|
|
|
|
return resolvedPath, rebaseName
|
|
|
|
}
|