2013-10-31 19:57:45 -04:00
|
|
|
package archive
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
import (
|
2015-05-01 18:01:10 -04:00
|
|
|
"archive/tar"
|
2014-05-09 18:49:02 -04:00
|
|
|
"bufio"
|
2014-05-16 19:26:28 -04:00
|
|
|
"bytes"
|
2013-12-17 08:19:48 -05:00
|
|
|
"compress/bzip2"
|
2014-01-06 20:37:49 -05:00
|
|
|
"compress/gzip"
|
2014-02-06 17:13:03 -05:00
|
|
|
"errors"
|
2013-05-28 16:37:49 -04:00
|
|
|
"fmt"
|
2013-03-11 08:42:36 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-04-21 17:23:55 -04:00
|
|
|
"os"
|
2013-03-11 08:42:36 -04:00
|
|
|
"os/exec"
|
2013-06-20 23:20:16 -04:00
|
|
|
"path/filepath"
|
2015-05-07 18:14:11 -04:00
|
|
|
"runtime"
|
2013-12-20 06:08:34 -05:00
|
|
|
"strings"
|
2013-12-20 05:30:45 -05:00
|
|
|
"syscall"
|
2014-05-09 18:49:02 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-24 18:11:48 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2014-08-12 12:29:46 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
type (
|
2015-08-03 21:52:54 -04:00
|
|
|
// Archive is a type of io.ReadCloser which has two interfaces Read and Closer.
|
|
|
|
Archive io.ReadCloser
|
|
|
|
// Reader is a type of io.Reader.
|
|
|
|
Reader io.Reader
|
|
|
|
// Compression is the state represtents if compressed or not.
|
|
|
|
Compression int
|
|
|
|
// TarChownOptions wraps the chown options UID and GID.
|
2015-01-19 15:15:36 -05:00
|
|
|
TarChownOptions struct {
|
|
|
|
UID, GID int
|
|
|
|
}
|
2015-08-03 21:52:54 -04:00
|
|
|
// TarOptions wraps the tar options.
|
2015-01-19 15:15:36 -05:00
|
|
|
TarOptions struct {
|
|
|
|
IncludeFiles []string
|
|
|
|
ExcludePatterns []string
|
|
|
|
Compression Compression
|
|
|
|
NoLchown bool
|
|
|
|
ChownOpts *TarChownOptions
|
|
|
|
IncludeSourceDir bool
|
|
|
|
// When unpacking, specifies whether overwriting a directory with a
|
|
|
|
// non-directory is allowed and vice versa.
|
|
|
|
NoOverwriteDirNonDir bool
|
2015-07-24 17:12:55 -04:00
|
|
|
// For each include when creating an archive, the included name will be
|
|
|
|
// replaced with the matching name from this map.
|
|
|
|
RebaseNames map[string]string
|
2014-02-06 17:13:03 -05:00
|
|
|
}
|
2014-11-06 13:01:37 -05:00
|
|
|
|
|
|
|
// Archiver allows the reuse of most utility functions of this package
|
|
|
|
// with a pluggable Untar function.
|
|
|
|
Archiver struct {
|
|
|
|
Untar func(io.Reader, string, *TarOptions) error
|
|
|
|
}
|
2014-10-20 15:35:48 -04:00
|
|
|
|
|
|
|
// breakoutError is used to differentiate errors related to breaking out
|
|
|
|
// When testing archive breakout in the unit tests, this error is expected
|
|
|
|
// in order for the test to pass.
|
|
|
|
breakoutError error
|
2014-02-06 17:13:03 -05:00
|
|
|
)
|
2013-11-11 17:30:38 -05:00
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
var (
|
2015-08-03 21:52:54 -04:00
|
|
|
// ErrNotImplemented is the error message of function not implemented.
|
2014-02-06 17:13:03 -05:00
|
|
|
ErrNotImplemented = errors.New("Function not implemented")
|
2014-11-06 13:01:37 -05:00
|
|
|
defaultArchiver = &Archiver{Untar}
|
2014-02-06 17:13:03 -05:00
|
|
|
)
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
const (
|
2015-08-03 21:52:54 -04:00
|
|
|
// Uncompressed represents the uncompressed.
|
2013-03-11 08:42:36 -04:00
|
|
|
Uncompressed Compression = iota
|
2015-08-03 21:52:54 -04:00
|
|
|
// Bzip2 is bzip2 compression algorithm.
|
2013-03-11 08:42:36 -04:00
|
|
|
Bzip2
|
2015-08-03 21:52:54 -04:00
|
|
|
// Gzip is gzip compression algorithm.
|
2013-03-11 08:42:36 -04:00
|
|
|
Gzip
|
2015-08-03 21:52:54 -04:00
|
|
|
// Xz is xz compression algorithm.
|
2013-04-01 19:16:28 -04:00
|
|
|
Xz
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// IsArchive checks if it is a archive by the header.
|
2014-05-09 18:26:41 -04:00
|
|
|
func IsArchive(header []byte) bool {
|
|
|
|
compression := DetectCompression(header)
|
|
|
|
if compression != Uncompressed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
r := tar.NewReader(bytes.NewBuffer(header))
|
|
|
|
_, err := r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// DetectCompression detects the compression algorithm of the source.
|
2013-06-13 20:53:38 -04:00
|
|
|
func DetectCompression(source []byte) Compression {
|
|
|
|
for compression, m := range map[Compression][]byte{
|
|
|
|
Bzip2: {0x42, 0x5A, 0x68},
|
|
|
|
Gzip: {0x1F, 0x8B, 0x08},
|
|
|
|
Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
|
|
|
|
} {
|
2014-05-16 15:47:50 -04:00
|
|
|
if len(source) < len(m) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Len too short")
|
2014-05-16 15:47:50 -04:00
|
|
|
continue
|
|
|
|
}
|
2014-05-16 15:15:21 -04:00
|
|
|
if bytes.Compare(m, source[:len(m)]) == 0 {
|
2013-06-13 20:53:38 -04:00
|
|
|
return compression
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Uncompressed
|
|
|
|
}
|
|
|
|
|
2014-02-14 05:12:49 -05:00
|
|
|
func xzDecompress(archive io.Reader) (io.ReadCloser, error) {
|
2013-12-18 04:50:22 -05:00
|
|
|
args := []string{"xz", "-d", "-c", "-q"}
|
|
|
|
|
2014-01-17 05:09:39 -05:00
|
|
|
return CmdStream(exec.Command(args[0], args[1:]...), archive)
|
2013-12-18 04:50:22 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// DecompressStream decompress the archive and returns a ReaderCloser with the decompressed archive.
|
2014-02-14 05:12:49 -05:00
|
|
|
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
|
2014-08-12 12:29:46 -04:00
|
|
|
p := pools.BufioReader32KPool
|
|
|
|
buf := p.Get(archive)
|
2014-05-09 18:49:02 -04:00
|
|
|
bs, err := buf.Peek(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-12-17 08:19:48 -05:00
|
|
|
}
|
2014-05-09 18:49:02 -04:00
|
|
|
|
|
|
|
compression := DetectCompression(bs)
|
2013-12-17 08:19:48 -05:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 12:29:46 -04:00
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, buf)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 08:19:48 -05:00
|
|
|
case Gzip:
|
2014-08-12 12:29:46 -04:00
|
|
|
gzReader, err := gzip.NewReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 08:19:48 -05:00
|
|
|
case Bzip2:
|
2014-08-12 12:29:46 -04:00
|
|
|
bz2Reader := bzip2.NewReader(buf)
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-18 04:50:22 -05:00
|
|
|
case Xz:
|
2014-08-12 12:29:46 -04:00
|
|
|
xzReader, err := xzDecompress(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 08:19:48 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// CompressStream compresses the dest with specified compression algorithm.
|
2014-01-20 06:07:34 -05:00
|
|
|
func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) {
|
2014-08-12 12:29:46 -04:00
|
|
|
p := pools.BufioWriter32KPool
|
|
|
|
buf := p.Get(dest)
|
2014-01-20 06:07:34 -05:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 12:29:46 -04:00
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
|
|
|
|
return writeBufWrapper, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
case Gzip:
|
2014-08-12 12:29:46 -04:00
|
|
|
gzWriter := gzip.NewWriter(dest)
|
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
|
|
|
|
return writeBufWrapper, nil
|
2014-01-20 06:07:34 -05:00
|
|
|
case Bzip2, Xz:
|
|
|
|
// archive/bzip2 does not support writing, and there is no xz support at all
|
|
|
|
// However, this is not a problem as docker only currently generates gzipped tars
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// Extension returns the extension of a file that uses the specified compression algorithm.
|
2013-05-28 16:37:49 -04:00
|
|
|
func (compression *Compression) Extension() string {
|
|
|
|
switch *compression {
|
|
|
|
case Uncompressed:
|
|
|
|
return "tar"
|
|
|
|
case Bzip2:
|
|
|
|
return "tar.bz2"
|
|
|
|
case Gzip:
|
|
|
|
return "tar.gz"
|
|
|
|
case Xz:
|
|
|
|
return "tar.xz"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
type tarAppender struct {
|
|
|
|
TarWriter *tar.Writer
|
|
|
|
Buffer *bufio.Writer
|
|
|
|
|
|
|
|
// for hardlink mapping
|
|
|
|
SeenFiles map[uint64]string
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:27:07 -05:00
|
|
|
// canonicalTarName provides a platform-independent and consistent posix-style
|
|
|
|
//path for files and directories to be archived regardless of the platform.
|
|
|
|
func canonicalTarName(name string, isDir bool) (string, error) {
|
2015-02-24 22:43:29 -05:00
|
|
|
name, err := CanonicalTarNameForPath(name)
|
2015-02-17 15:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// suffix with '/' for directories
|
|
|
|
if isDir && !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
func (ta *tarAppender) addTarFile(path, name string) error {
|
2014-01-20 05:48:47 -05:00
|
|
|
fi, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:48:47 -05:00
|
|
|
link := ""
|
2014-01-20 06:07:34 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
2014-01-20 05:48:47 -05:00
|
|
|
if link, err = os.Readlink(path); err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-01-20 05:48:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr, err := tar.FileInfoHeader(fi, link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-03 21:40:16 -05:00
|
|
|
hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
|
2014-01-20 05:48:47 -05:00
|
|
|
|
2015-02-17 15:27:07 -05:00
|
|
|
name, err = canonicalTarName(name, fi.IsDir())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tar: cannot canonicalize path: %v", err)
|
2014-01-21 06:52:05 -05:00
|
|
|
}
|
2014-01-20 05:48:47 -05:00
|
|
|
hdr.Name = name
|
|
|
|
|
2014-11-13 15:36:05 -05:00
|
|
|
nlink, inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-20 06:26:08 -05:00
|
|
|
}
|
2014-11-13 15:36:05 -05:00
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
// if it's a regular file and has more than 1 link,
|
|
|
|
// it's hardlinked, so set the type flag accordingly
|
2014-10-30 13:42:57 -04:00
|
|
|
if fi.Mode().IsRegular() && nlink > 1 {
|
2014-09-15 14:45:53 -04:00
|
|
|
// a link should have a name that it links too
|
|
|
|
// and that linked name should be first in the tar archive
|
2014-10-30 13:42:57 -04:00
|
|
|
if oldpath, ok := ta.SeenFiles[inode]; ok {
|
2014-09-15 14:45:53 -04:00
|
|
|
hdr.Typeflag = tar.TypeLink
|
|
|
|
hdr.Linkname = oldpath
|
|
|
|
hdr.Size = 0 // This Must be here for the writer math to add up!
|
|
|
|
} else {
|
2014-10-30 13:42:57 -04:00
|
|
|
ta.SeenFiles[inode] = name
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:12:25 -05:00
|
|
|
capability, _ := system.Lgetxattr(path, "security.capability")
|
2014-01-20 06:26:08 -05:00
|
|
|
if capability != nil {
|
|
|
|
hdr.Xattrs = make(map[string]string)
|
|
|
|
hdr.Xattrs["security.capability"] = string(capability)
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.TarWriter.WriteHeader(hdr); err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hdr.Typeflag == tar.TypeReg {
|
2014-07-14 13:11:48 -04:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
2014-07-14 13:11:48 -04:00
|
|
|
}
|
2014-07-14 12:31:19 -04:00
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
ta.Buffer.Reset(ta.TarWriter)
|
2014-10-28 17:01:10 -04:00
|
|
|
defer ta.Buffer.Reset(nil)
|
2014-09-15 14:45:53 -04:00
|
|
|
_, err = io.Copy(ta.Buffer, file)
|
2014-07-14 12:31:19 -04:00
|
|
|
file.Close()
|
|
|
|
if err != nil {
|
2014-07-14 13:11:48 -04:00
|
|
|
return err
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
2014-09-15 14:45:53 -04:00
|
|
|
err = ta.Buffer.Flush()
|
2014-07-14 12:31:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *TarChownOptions) error {
|
2014-02-18 03:53:30 -05:00
|
|
|
// hdr.Mode is in linux format, which we can use for sycalls,
|
|
|
|
// but for os.Foo() calls we need the mode converted to os.FileMode,
|
|
|
|
// so use hdrInfo.Mode() (they differ for e.g. setuid bits)
|
|
|
|
hdrInfo := hdr.FileInfo()
|
|
|
|
|
2013-12-20 05:30:45 -05:00
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
// Create directory unless it exists as a directory already.
|
|
|
|
// In that case we just want to merge the two
|
|
|
|
if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
|
2014-02-18 03:53:30 -05:00
|
|
|
if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeReg, tar.TypeRegA:
|
|
|
|
// Source is regular file
|
2014-02-18 03:53:30 -05:00
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
|
2013-12-20 05:30:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(file, reader); err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
|
2015-05-07 18:14:11 -04:00
|
|
|
// Handle this is an OS-specific way
|
|
|
|
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeLink:
|
2014-10-20 15:36:28 -04:00
|
|
|
targetPath := filepath.Join(extractDir, hdr.Linkname)
|
|
|
|
// check for hardlink breakout
|
|
|
|
if !strings.HasPrefix(targetPath, extractDir) {
|
|
|
|
return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
|
|
|
|
}
|
|
|
|
if err := os.Link(targetPath, path); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeSymlink:
|
2014-11-19 11:27:34 -05:00
|
|
|
// path -> hdr.Linkname = targetPath
|
|
|
|
// e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
|
|
|
|
targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname)
|
|
|
|
|
|
|
|
// the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
|
|
|
|
// that symlink would first have to be created, which would be caught earlier, at this very check:
|
|
|
|
if !strings.HasPrefix(targetPath, extractDir) {
|
|
|
|
return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
|
2014-10-20 15:36:28 -04:00
|
|
|
}
|
2013-12-20 05:30:45 -05:00
|
|
|
if err := os.Symlink(hdr.Linkname, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:50:07 -05:00
|
|
|
case tar.TypeXGlobalHeader:
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("PAX Global Extended Headers found and ignored")
|
2014-01-31 08:50:07 -05:00
|
|
|
return nil
|
|
|
|
|
2013-12-20 05:30:45 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
// Lchown is not supported on Windows.
|
|
|
|
if Lchown && runtime.GOOS != "windows" {
|
|
|
|
if chownOpts == nil {
|
|
|
|
chownOpts = &TarChownOptions{UID: hdr.Uid, GID: hdr.Gid}
|
|
|
|
}
|
|
|
|
if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
|
2015-05-07 18:14:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-12-20 05:30:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 09:05:30 -05:00
|
|
|
for key, value := range hdr.Xattrs {
|
2014-02-21 04:12:25 -05:00
|
|
|
if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
|
2014-01-15 09:05:30 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 05:30:45 -05:00
|
|
|
// There is no LChmod, so ignore mode for symlink. Also, this
|
|
|
|
// must happen after chown, as that can modify the file mode
|
2015-05-07 18:14:11 -04:00
|
|
|
if err := handleLChmod(hdr, path, hdrInfo); err != nil {
|
|
|
|
return err
|
2013-12-20 05:30:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
2015-05-07 18:14:11 -04:00
|
|
|
// syscall.UtimesNano doesn't support a NOFOLLOW flag atm
|
2015-03-01 18:55:28 -05:00
|
|
|
if hdr.Typeflag == tar.TypeLink {
|
|
|
|
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
|
|
|
|
if err := system.UtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if hdr.Typeflag != tar.TypeSymlink {
|
2014-06-18 19:42:22 -04:00
|
|
|
if err := system.UtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-18 19:42:22 -04:00
|
|
|
if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-18 23:28:49 -04:00
|
|
|
// Tar creates an archive from the directory at `path`, and returns it as a
|
|
|
|
// stream of bytes.
|
2014-02-14 06:41:46 -05:00
|
|
|
func Tar(path string, compression Compression) (io.ReadCloser, error) {
|
2014-02-13 19:05:36 -05:00
|
|
|
return TarWithOptions(path, &TarOptions{Compression: compression})
|
2013-09-05 16:10:29 -04:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:05:36 -05:00
|
|
|
// TarWithOptions creates an archive from the directory at `path`, only including files whose relative
|
2014-10-23 17:30:11 -04:00
|
|
|
// paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
|
2014-02-13 19:05:36 -05:00
|
|
|
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
2015-08-10 18:21:30 -04:00
|
|
|
// Fix the source path to work with long path names. This is a no-op
|
|
|
|
// on platforms other than Windows.
|
|
|
|
srcPath = fixVolumePathPrefix(srcPath)
|
|
|
|
|
2015-04-09 15:07:06 -04:00
|
|
|
patterns, patDirs, exceptions, err := fileutils.CleanPatterns(options.ExcludePatterns)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
2013-11-11 17:30:38 -05:00
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
compressWriter, err := CompressStream(pipeWriter, options.Compression)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
2013-09-05 16:10:29 -04:00
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
go func() {
|
2014-09-15 14:45:53 -04:00
|
|
|
ta := &tarAppender{
|
|
|
|
TarWriter: tar.NewWriter(compressWriter),
|
|
|
|
Buffer: pools.BufioWriter32KPool.Get(nil),
|
|
|
|
SeenFiles: make(map[uint64]string),
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
if err := ta.TarWriter.Close(); err != nil {
|
|
|
|
logrus.Debugf("Can't close tar writer: %s", err)
|
|
|
|
}
|
|
|
|
if err := compressWriter.Close(); err != nil {
|
|
|
|
logrus.Debugf("Can't close compress writer: %s", err)
|
|
|
|
}
|
|
|
|
if err := pipeWriter.Close(); err != nil {
|
|
|
|
logrus.Debugf("Can't close pipe writer: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
// this buffer is needed for the duration of this piped stream
|
|
|
|
defer pools.BufioWriter32KPool.Put(ta.Buffer)
|
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
// In general we log errors here but ignore them because
|
|
|
|
// during e.g. a diff operation the container can continue
|
|
|
|
// mutating the filesystem and we can see transient errors
|
|
|
|
// from this
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
stat, err := os.Lstat(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
|
|
|
// We can't later join a non-dir with any includes because the
|
|
|
|
// 'walk' will error if "file/." is stat-ed and "file" is not a
|
|
|
|
// directory. So, we must split the source path and use the
|
|
|
|
// basename as the include.
|
|
|
|
if len(options.IncludeFiles) > 0 {
|
|
|
|
logrus.Warn("Tar: Can't archive a file with includes")
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, base := SplitPathDirEntry(srcPath)
|
|
|
|
srcPath = dir
|
|
|
|
options.IncludeFiles = []string{base}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(options.IncludeFiles) == 0 {
|
2014-10-23 17:30:11 -04:00
|
|
|
options.IncludeFiles = []string{"."}
|
2014-01-20 06:07:34 -05:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, include := range options.IncludeFiles {
|
2015-07-24 17:12:55 -04:00
|
|
|
rebaseName := options.RebaseNames[include]
|
|
|
|
|
2015-08-10 18:21:30 -04:00
|
|
|
walkRoot := getWalkRoot(srcPath, include)
|
2015-01-19 15:15:36 -05:00
|
|
|
filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
|
2014-01-20 06:07:34 -05:00
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Tar: Can't stat file %s to tar: %s", srcPath, err)
|
2014-01-20 06:07:34 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath, err := filepath.Rel(srcPath, filePath)
|
2015-01-19 15:15:36 -05:00
|
|
|
if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
|
2014-10-27 20:23:50 -04:00
|
|
|
// Error getting relative path OR we are looking
|
2015-01-19 15:15:36 -05:00
|
|
|
// at the source directory path. Skip in both situations.
|
2014-01-20 06:07:34 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
if options.IncludeSourceDir && include == "." && relFilePath != "." {
|
|
|
|
relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
|
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
skip := false
|
|
|
|
|
|
|
|
// If "include" is an exact match for the current file
|
|
|
|
// then even if there's an "excludePatterns" pattern that
|
|
|
|
// matches it, don't skip it. IOW, assume an explicit 'include'
|
|
|
|
// is asking for that file no matter what - which is true
|
|
|
|
// for some files, like .dockerignore and Dockerfile (sometimes)
|
|
|
|
if include != relFilePath {
|
2015-04-09 15:07:06 -04:00
|
|
|
skip, err = fileutils.OptimizedMatches(relFilePath, patterns, patDirs)
|
2014-10-23 17:30:11 -04:00
|
|
|
if err != nil {
|
2015-07-15 15:25:50 -04:00
|
|
|
logrus.Debugf("Error matching %s: %v", relFilePath, err)
|
2014-10-23 17:30:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-07-07 08:23:07 -04:00
|
|
|
}
|
2014-07-31 18:11:15 -04:00
|
|
|
|
2014-07-07 08:23:07 -04:00
|
|
|
if skip {
|
2015-04-09 15:07:06 -04:00
|
|
|
if !exceptions && f.IsDir() {
|
2014-07-07 08:23:07 -04:00
|
|
|
return filepath.SkipDir
|
2014-02-13 19:05:36 -05:00
|
|
|
}
|
2014-07-07 08:23:07 -04:00
|
|
|
return nil
|
2014-02-13 19:05:36 -05:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
if seen[relFilePath] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
seen[relFilePath] = true
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// Rename the base resource.
|
|
|
|
if rebaseName != "" {
|
|
|
|
var replacement string
|
|
|
|
if rebaseName != string(filepath.Separator) {
|
|
|
|
// Special case the root directory to replace with an
|
|
|
|
// empty string instead so that we don't end up with
|
|
|
|
// double slashes in the paths.
|
|
|
|
replacement = rebaseName
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath = strings.Replace(relFilePath, include, replacement, 1)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.addTarFile(filePath, relFilePath); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Can't add file %s to tar: %s", filePath, err)
|
2014-01-20 06:07:34 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// Unpack unpacks the decompressedArchive to dest with options.
|
2014-12-08 16:19:24 -05:00
|
|
|
func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
|
2014-02-14 05:12:49 -05:00
|
|
|
tr := tar.NewReader(decompressedArchive)
|
2014-08-12 12:29:46 -04:00
|
|
|
trBuf := pools.BufioReader32KPool.Get(nil)
|
|
|
|
defer pools.BufioReader32KPool.Put(trBuf)
|
2013-12-20 06:08:34 -05:00
|
|
|
|
2014-07-08 15:26:59 -04:00
|
|
|
var dirs []*tar.Header
|
2013-12-20 06:08:34 -05:00
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
2014-07-31 03:33:59 -04:00
|
|
|
loop:
|
2013-12-20 06:08:34 -05:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
if err != nil {
|
2013-07-08 16:30:03 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
// Normalize name, for safety and for a simple is-root check
|
2015-06-01 19:42:27 -04:00
|
|
|
// This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
|
|
|
|
// This keeps "..\" as-is, but normalizes "\..\" to "\".
|
2013-12-20 06:08:34 -05:00
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
for _, exclude := range options.ExcludePatterns {
|
2014-07-31 03:33:59 -04:00
|
|
|
if strings.HasPrefix(hdr.Name, exclude) {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
// After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in
|
|
|
|
// the filepath format for the OS on which the daemon is running. Hence
|
|
|
|
// the check for a slash-suffix MUST be done in an OS-agnostic way.
|
|
|
|
if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
|
2013-12-20 06:08:34 -05:00
|
|
|
// Not the root directory, ensure that the parent directory exists
|
|
|
|
parent := filepath.Dir(hdr.Name)
|
|
|
|
parentPath := filepath.Join(dest, parent)
|
|
|
|
if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
|
2015-05-07 18:14:11 -04:00
|
|
|
err = system.MkdirAll(parentPath, 0777)
|
2013-12-20 06:08:34 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 15:26:59 -04:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2014-11-27 02:00:13 -05:00
|
|
|
rel, err := filepath.Rel(dest, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
2014-11-27 02:00:13 -05:00
|
|
|
return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
|
2014-10-20 15:36:28 -04:00
|
|
|
}
|
2013-12-20 06:08:34 -05:00
|
|
|
|
|
|
|
// If path exits we almost always just want to remove and replace it
|
|
|
|
// The only exception is when it is a directory *and* the file from
|
|
|
|
// the layer is also a directory. Then we want to merge them (i.e.
|
|
|
|
// just apply the metadata from the layer).
|
|
|
|
if fi, err := os.Lstat(path); err == nil {
|
2015-01-19 15:15:36 -05:00
|
|
|
if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
|
|
|
|
// If NoOverwriteDirNonDir is true then we cannot replace
|
|
|
|
// an existing directory with a non-directory from the archive.
|
|
|
|
return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
|
|
|
|
// If NoOverwriteDirNonDir is true then we cannot replace
|
|
|
|
// an existing non-directory with a directory from the archive.
|
|
|
|
return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:21:22 -04:00
|
|
|
if fi.IsDir() && hdr.Name == "." {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 12:31:19 -04:00
|
|
|
trBuf.Reset(tr)
|
2015-01-19 15:15:36 -05:00
|
|
|
|
|
|
|
if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts); err != nil {
|
2013-12-20 06:08:34 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-06-13 20:53:38 -04:00
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
// Directory mtimes must be handled at the end to avoid further
|
|
|
|
// file creation in them to modify the directory mtime
|
|
|
|
if hdr.Typeflag == tar.TypeDir {
|
|
|
|
dirs = append(dirs, hdr)
|
2013-11-11 20:17:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
for _, hdr := range dirs {
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
|
|
|
if err := syscall.UtimesNano(path, ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:19:24 -05:00
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive may be compressed with one of the following algorithms:
|
|
|
|
// identity (uncompressed), gzip, bzip2, xz.
|
|
|
|
// FIXME: specify behavior when target path exists vs. doesn't exist.
|
2015-07-27 09:46:20 -04:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, true)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
2015-07-27 09:46:20 -04:00
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive must be an uncompressed stream.
|
|
|
|
func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for teasing out the automatic decompression
|
|
|
|
func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
|
|
|
|
if tarArchive == nil {
|
2014-12-08 16:19:24 -05:00
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
if options == nil {
|
|
|
|
options = &TarOptions{}
|
|
|
|
}
|
2014-10-23 17:30:11 -04:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
2015-07-27 09:46:20 -04:00
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
r := tarArchive
|
2015-07-27 09:46:20 -04:00
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
2015-07-27 09:46:20 -04:00
|
|
|
|
|
|
|
return Unpack(r, dest, options)
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
|
|
|
|
// If either Tar or Untar fails, TarUntar aborts and returns the error.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) TarUntar(src, dst string) error {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("TarUntar(%s %s)", src, dst)
|
2014-02-13 19:05:36 -05:00
|
|
|
archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
|
2013-06-18 23:28:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 06:41:46 -05:00
|
|
|
defer archive.Close()
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.Untar(archive, dst, nil)
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05:00
|
|
|
// TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
|
|
|
|
// If either Tar or Untar fails, TarUntar aborts and returns the error.
|
|
|
|
func TarUntar(src, dst string) error {
|
|
|
|
return defaultArchiver.TarUntar(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// UntarPath untar a file from path to a destination, src is the source tar file path.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) UntarPath(src, dst string) error {
|
2014-02-14 06:41:46 -05:00
|
|
|
archive, err := os.Open(src)
|
|
|
|
if err != nil {
|
2013-06-14 19:43:39 -04:00
|
|
|
return err
|
2014-02-14 06:41:46 -05:00
|
|
|
}
|
|
|
|
defer archive.Close()
|
2014-11-06 13:01:37 -05:00
|
|
|
if err := archiver.Untar(archive, dst, nil); err != nil {
|
2013-06-14 19:43:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05:00
|
|
|
// UntarPath is a convenience function which looks for an archive
|
|
|
|
// at filesystem path `src`, and unpacks it at `dst`.
|
|
|
|
func UntarPath(src, dst string) error {
|
|
|
|
return defaultArchiver.UntarPath(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// CopyWithTar creates a tar archive of filesystem path `src`, and
|
|
|
|
// unpacks it at filesystem path `dst`.
|
|
|
|
// The archive is streamed directly with fixed buffering and no
|
|
|
|
// intermediary disk IO.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) CopyWithTar(src, dst string) error {
|
2013-06-18 23:28:49 -04:00
|
|
|
srcSt, err := os.Stat(src)
|
2013-06-14 19:43:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-20 23:20:16 -04:00
|
|
|
if !srcSt.IsDir() {
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.CopyFileWithTar(src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
}
|
|
|
|
// Create dst, copy src's content into it
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Creating dest directory: %s", dst)
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 19:49:05 -04:00
|
|
|
if err := system.MkdirAll(dst, 0755); err != nil {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.TarUntar(src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05:00
|
|
|
// CopyWithTar creates a tar archive of filesystem path `src`, and
|
|
|
|
// unpacks it at filesystem path `dst`.
|
|
|
|
// The archive is streamed directly with fixed buffering and no
|
|
|
|
// intermediary disk IO.
|
|
|
|
func CopyWithTar(src, dst string) error {
|
|
|
|
return defaultArchiver.CopyWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
|
|
|
// for a single file. It copies a regular file from path `src` to
|
|
|
|
// path `dst`, and preserves all its metadata.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
srcSt, err := os.Stat(src)
|
2013-06-18 23:28:49 -04:00
|
|
|
if err != nil {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
2013-06-18 23:28:49 -04:00
|
|
|
if srcSt.IsDir() {
|
2013-06-20 23:20:16 -04:00
|
|
|
return fmt.Errorf("Can't copy a directory")
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
|
|
|
// Clean up the trailing slash. This must be done in an operating
|
|
|
|
// system specific manner.
|
2015-05-07 18:14:11 -04:00
|
|
|
if dst[len(dst)-1] == os.PathSeparator {
|
|
|
|
dst = filepath.Join(dst, filepath.Base(src))
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
2013-06-20 23:20:16 -04:00
|
|
|
// Create the holding directory if necessary
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 19:49:05 -04:00
|
|
|
if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-01-06 20:37:49 -05:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
2014-09-30 02:23:36 -04:00
|
|
|
errC := promise.Go(func() error {
|
2014-01-06 20:37:49 -05:00
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
srcF, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
|
|
|
|
|
|
|
hdr, err := tar.FileInfoHeader(srcSt, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Name = filepath.Base(dst)
|
2015-03-03 21:40:16 -05:00
|
|
|
hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
|
|
|
|
|
2014-07-14 13:11:48 -04:00
|
|
|
tw := tar.NewWriter(w)
|
|
|
|
defer tw.Close()
|
2014-01-06 20:37:49 -05:00
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tw, srcF); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer func() {
|
|
|
|
if er := <-errC; err != nil {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
}()
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.Untar(r, filepath.Dir(dst), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
|
|
|
// for a single file. It copies a regular file from path `src` to
|
|
|
|
// path `dst`, and preserves all its metadata.
|
|
|
|
//
|
2015-06-01 19:42:27 -04:00
|
|
|
// Destination handling is in an operating specific manner depending
|
|
|
|
// where the daemon is running. If `dst` ends with a trailing slash
|
|
|
|
// the final destination path will be `dst/base(src)` (Linux) or
|
|
|
|
// `dst\base(src)` (Windows).
|
2014-11-06 13:01:37 -05:00
|
|
|
func CopyFileWithTar(src, dst string) (err error) {
|
|
|
|
return defaultArchiver.CopyFileWithTar(src, dst)
|
2013-06-14 19:43:39 -04:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:18:59 -04:00
|
|
|
// CmdStream executes a command, and returns its stdout as a stream.
|
|
|
|
// If the command fails to run or doesn't complete successfully, an error
|
|
|
|
// will be returned, including anything written on stderr.
|
2014-02-14 05:12:49 -05:00
|
|
|
func CmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
|
2013-09-05 16:10:29 -04:00
|
|
|
if input != nil {
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Write stdin if any
|
|
|
|
go func() {
|
2013-12-18 04:50:22 -05:00
|
|
|
io.Copy(stdin, input)
|
2013-09-05 16:10:29 -04:00
|
|
|
stdin.Close()
|
|
|
|
}()
|
|
|
|
}
|
2013-03-11 08:42:36 -04:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pipeR, pipeW := io.Pipe()
|
2013-03-29 07:42:17 -04:00
|
|
|
errChan := make(chan []byte)
|
2013-03-29 16:26:02 -04:00
|
|
|
// Collect stderr, we will use it in case of an error
|
2013-03-11 08:42:36 -04:00
|
|
|
go func() {
|
|
|
|
errText, e := ioutil.ReadAll(stderr)
|
|
|
|
if e != nil {
|
|
|
|
errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
|
|
|
|
}
|
2013-03-29 07:42:17 -04:00
|
|
|
errChan <- errText
|
|
|
|
}()
|
2013-03-29 16:26:02 -04:00
|
|
|
// Copy stdout to the returned pipe
|
2013-03-29 07:42:17 -04:00
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(pipeW, stdout)
|
|
|
|
if err != nil {
|
|
|
|
pipeW.CloseWithError(err)
|
|
|
|
}
|
|
|
|
errText := <-errChan
|
2013-03-11 08:42:36 -04:00
|
|
|
if err := cmd.Wait(); err != nil {
|
2013-07-02 17:47:58 -04:00
|
|
|
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errText))
|
2013-03-11 08:42:36 -04:00
|
|
|
} else {
|
|
|
|
pipeW.Close()
|
|
|
|
}
|
|
|
|
}()
|
2013-03-29 16:26:02 -04:00
|
|
|
// Run the command and return the pipe
|
2013-03-11 08:42:36 -04:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pipeR, nil
|
|
|
|
}
|
2013-04-21 17:23:55 -04:00
|
|
|
|
|
|
|
// NewTempArchive reads the content of src into a temporary file, and returns the contents
|
|
|
|
// of that file as an archive. The archive can only be read once - as soon as reading completes,
|
|
|
|
// the file will be deleted.
|
|
|
|
func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
|
|
|
|
f, err := ioutil.TempFile(dir, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(f, src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
st, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
size := st.Size()
|
2014-12-03 15:36:57 -05:00
|
|
|
return &TempArchive{File: f, Size: size}, nil
|
2013-04-21 17:23:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes,
|
|
|
|
// the file will be deleted.
|
2013-04-21 17:23:55 -04:00
|
|
|
type TempArchive struct {
|
|
|
|
*os.File
|
2014-12-03 15:36:57 -05:00
|
|
|
Size int64 // Pre-computed from Stat().Size() as a convenience
|
|
|
|
read int64
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying file if it's still open, or does a no-op
|
|
|
|
// to allow callers to try to close the TempArchive multiple times safely.
|
|
|
|
func (archive *TempArchive) Close() error {
|
|
|
|
if archive.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
archive.closed = true
|
|
|
|
|
|
|
|
return archive.File.Close()
|
2013-04-21 17:23:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (archive *TempArchive) Read(data []byte) (int, error) {
|
|
|
|
n, err := archive.File.Read(data)
|
2014-11-20 12:34:35 -05:00
|
|
|
archive.read += int64(n)
|
|
|
|
if err != nil || archive.read == archive.Size {
|
2014-12-03 15:36:57 -05:00
|
|
|
archive.Close()
|
2013-04-21 17:23:55 -04:00
|
|
|
os.Remove(archive.File.Name())
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|