2013-10-31 19:57:45 -04:00
|
|
|
package archive
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-20 23:20:16 -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-06-18 23:28:49 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2014-02-27 11:43:44 -05:00
|
|
|
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
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-18 23:28:49 -04:00
|
|
|
"path"
|
2013-06-20 23:20:16 -04:00
|
|
|
"path/filepath"
|
2013-12-20 06:08:34 -05:00
|
|
|
"strings"
|
2013-12-20 05:30:45 -05:00
|
|
|
"syscall"
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
type (
|
2014-02-14 06:41:46 -05:00
|
|
|
Archive io.ReadCloser
|
|
|
|
ArchiveReader io.Reader
|
|
|
|
Compression int
|
|
|
|
TarOptions struct {
|
2014-02-06 17:13:03 -05:00
|
|
|
Includes []string
|
|
|
|
Compression Compression
|
|
|
|
}
|
|
|
|
)
|
2013-11-11 17:30:38 -05:00
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
var (
|
|
|
|
ErrNotImplemented = errors.New("Function not implemented")
|
|
|
|
)
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
Uncompressed Compression = iota
|
|
|
|
Bzip2
|
|
|
|
Gzip
|
2013-04-01 19:16:28 -04:00
|
|
|
Xz
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2013-06-13 20:53:38 -04:00
|
|
|
func DetectCompression(source []byte) Compression {
|
|
|
|
sourceLen := len(source)
|
|
|
|
for compression, m := range map[Compression][]byte{
|
|
|
|
Bzip2: {0x42, 0x5A, 0x68},
|
|
|
|
Gzip: {0x1F, 0x8B, 0x08},
|
|
|
|
Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
|
|
|
|
} {
|
|
|
|
fail := false
|
|
|
|
if len(m) > sourceLen {
|
|
|
|
utils.Debugf("Len too short")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for _, b := range m {
|
|
|
|
if b != source[i] {
|
|
|
|
fail = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if !fail {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-14 05:12:49 -05:00
|
|
|
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
|
2013-12-17 08:19:48 -05:00
|
|
|
buf := make([]byte, 10)
|
|
|
|
totalN := 0
|
|
|
|
for totalN < 10 {
|
|
|
|
n, err := archive.Read(buf[totalN:])
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil, fmt.Errorf("Tarball too short")
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
totalN += n
|
|
|
|
utils.Debugf("[tar autodetect] n: %d", n)
|
|
|
|
}
|
|
|
|
compression := DetectCompression(buf)
|
|
|
|
wrap := io.MultiReader(bytes.NewReader(buf), archive)
|
|
|
|
|
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-02-14 05:12:49 -05:00
|
|
|
return ioutil.NopCloser(wrap), nil
|
2013-12-17 08:19:48 -05:00
|
|
|
case Gzip:
|
|
|
|
return gzip.NewReader(wrap)
|
|
|
|
case Bzip2:
|
2014-02-14 05:12:49 -05:00
|
|
|
return ioutil.NopCloser(bzip2.NewReader(wrap)), nil
|
2013-12-18 04:50:22 -05:00
|
|
|
case Xz:
|
|
|
|
return xzDecompress(wrap)
|
2013-12-17 08:19:48 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) {
|
|
|
|
|
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-02-14 05:33:40 -05:00
|
|
|
return utils.NopWriteCloser(dest), nil
|
2013-03-11 08:42:36 -04:00
|
|
|
case Gzip:
|
2014-01-20 06:07:34 -05:00
|
|
|
return gzip.NewWriter(dest), nil
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-17 05:21:36 -05:00
|
|
|
func addTarFile(path, name string, tw *tar.Writer) 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
|
|
|
|
}
|
|
|
|
|
2014-01-21 06:52:05 -05:00
|
|
|
if fi.IsDir() && !strings.HasSuffix(name, "/") {
|
|
|
|
name = name + "/"
|
|
|
|
}
|
|
|
|
|
2014-01-20 05:48:47 -05:00
|
|
|
hdr.Name = name
|
|
|
|
|
|
|
|
stat, ok := fi.Sys().(*syscall.Stat_t)
|
|
|
|
if ok {
|
|
|
|
// Currently go does not fill in the major/minors
|
|
|
|
if stat.Mode&syscall.S_IFBLK == syscall.S_IFBLK ||
|
|
|
|
stat.Mode&syscall.S_IFCHR == syscall.S_IFCHR {
|
|
|
|
hdr.Devmajor = int64(major(uint64(stat.Rdev)))
|
|
|
|
hdr.Devminor = int64(minor(uint64(stat.Rdev)))
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
2014-01-20 06:26:08 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
capability, _ := Lgetxattr(path, "security.capability")
|
|
|
|
if capability != nil {
|
|
|
|
hdr.Xattrs = make(map[string]string)
|
|
|
|
hdr.Xattrs["security.capability"] = string(capability)
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hdr.Typeflag == tar.TypeReg {
|
|
|
|
if file, err := os.Open(path); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_, err := io.Copy(tw, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:41:52 -05:00
|
|
|
func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader) 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:
|
|
|
|
mode := uint32(hdr.Mode & 07777)
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeBlock:
|
|
|
|
mode |= syscall.S_IFBLK
|
|
|
|
case tar.TypeChar:
|
|
|
|
mode |= syscall.S_IFCHR
|
|
|
|
case tar.TypeFifo:
|
|
|
|
mode |= syscall.S_IFIFO
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeLink:
|
|
|
|
if err := os.Link(filepath.Join(extractDir, hdr.Linkname), path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeSymlink:
|
|
|
|
if err := os.Symlink(hdr.Linkname, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:50:07 -05:00
|
|
|
case tar.TypeXGlobalHeader:
|
|
|
|
utils.Debugf("PAX Global Extended Headers found and ignored")
|
|
|
|
return nil
|
|
|
|
|
2013-12-20 05:30:45 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
|
|
|
|
}
|
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:05:30 -05:00
|
|
|
for key, value := range hdr.Xattrs {
|
|
|
|
if err := Lsetxattr(path, key, []byte(value), 0); err != nil {
|
|
|
|
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
|
|
|
|
if hdr.Typeflag != tar.TypeSymlink {
|
2014-02-18 03:53:30 -05:00
|
|
|
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
|
|
|
|
// syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
|
|
|
|
if hdr.Typeflag != tar.TypeSymlink {
|
2014-02-06 17:13:03 -05:00
|
|
|
if err := UtimesNano(path, ts); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := LUtimesNano(path, ts); err != nil {
|
|
|
|
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-01-17 05:09:39 -05:00
|
|
|
return TarFilter(path, &TarOptions{Compression: compression})
|
2013-09-05 16:10:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func escapeName(name string) string {
|
2013-09-26 11:40:13 -04:00
|
|
|
escaped := make([]byte, 0)
|
2013-09-05 16:10:29 -04:00
|
|
|
for i, c := range []byte(name) {
|
|
|
|
if i == 0 && c == '/' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// all printable chars except "-" which is 0x2d
|
2013-09-26 11:40:13 -04:00
|
|
|
if (0x20 <= c && c <= 0x7E) && c != 0x2d {
|
2013-09-05 16:10:29 -04:00
|
|
|
escaped = append(escaped, c)
|
|
|
|
} else {
|
|
|
|
escaped = append(escaped, fmt.Sprintf("\\%03o", c)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(escaped)
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tar creates an archive from the directory at `path`, only including files whose relative
|
|
|
|
// paths are included in `filter`. If `filter` is nil, then all files are included.
|
2014-02-14 06:41:46 -05:00
|
|
|
func TarFilter(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
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
|
|
|
tw := tar.NewWriter(compressWriter)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// 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
|
|
|
|
|
|
|
|
if options.Includes == nil {
|
|
|
|
options.Includes = []string{"."}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, include := range options.Includes {
|
|
|
|
filepath.Walk(filepath.Join(srcPath, include), func(filePath string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
utils.Debugf("Tar: Can't stat file %s to tar: %s\n", srcPath, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath, err := filepath.Rel(srcPath, filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addTarFile(filePath, relFilePath, tw); err != nil {
|
|
|
|
utils.Debugf("Can't add file %s to tar: %s\n", srcPath, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
utils.Debugf("Can't close tar writer: %s\n", err)
|
|
|
|
}
|
|
|
|
if err := compressWriter.Close(); err != nil {
|
|
|
|
utils.Debugf("Can't close compress writer: %s\n", err)
|
|
|
|
}
|
2014-02-14 05:33:40 -05:00
|
|
|
if err := pipeWriter.Close(); err != nil {
|
|
|
|
utils.Debugf("Can't close pipe writer: %s\n", err)
|
|
|
|
}
|
2014-01-20 06:07:34 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 23:28:49 -04:00
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `path`.
|
2013-08-12 13:53:06 -04:00
|
|
|
// The archive may be compressed with one of the following algorithms:
|
2013-06-18 23:28:49 -04:00
|
|
|
// identity (uncompressed), gzip, bzip2, xz.
|
2013-06-14 19:43:39 -04:00
|
|
|
// FIXME: specify behavior when target path exists vs. doesn't exist.
|
2013-12-20 06:08:34 -05:00
|
|
|
func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
2013-06-22 15:29:42 -04:00
|
|
|
if archive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
2013-07-08 16:30:03 -04:00
|
|
|
|
2014-02-14 05:12:49 -05:00
|
|
|
decompressedArchive, err := DecompressStream(archive)
|
2013-12-20 06:08:34 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 05:12:49 -05:00
|
|
|
defer decompressedArchive.Close()
|
2013-12-20 06:08:34 -05:00
|
|
|
|
2014-02-14 05:12:49 -05:00
|
|
|
tr := tar.NewReader(decompressedArchive)
|
2013-12-20 06:08:34 -05:00
|
|
|
|
|
|
|
var dirs []*tar.Header
|
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
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
|
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
|
|
|
if !strings.HasSuffix(hdr.Name, "/") {
|
|
|
|
// 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) {
|
|
|
|
err = os.MkdirAll(parentPath, 600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 20:53:38 -04:00
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
if err := createTarFile(path, dest, hdr, tr); err != nil {
|
|
|
|
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
|
|
|
}
|
2013-12-20 06:08:34 -05:00
|
|
|
|
2013-03-11 08:42:36 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-18 23:28:49 -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-01-17 05:09:39 -05:00
|
|
|
func TarUntar(src string, dst string) error {
|
|
|
|
utils.Debugf("TarUntar(%s %s)", src, dst)
|
|
|
|
archive, err := TarFilter(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()
|
2013-11-11 20:17:38 -05:00
|
|
|
return Untar(archive, dst, nil)
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 19:43:39 -04: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 {
|
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()
|
|
|
|
if err := Untar(archive, dst, nil); err != nil {
|
2013-06-14 19:43:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
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() {
|
|
|
|
return CopyFileWithTar(src, dst)
|
|
|
|
}
|
|
|
|
// Create dst, copy src's content into it
|
|
|
|
utils.Debugf("Creating dest directory: %s", dst)
|
2013-07-29 14:03:09 -04:00
|
|
|
if err := os.MkdirAll(dst, 0755); err != nil && !os.IsExist(err) {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
2014-01-17 05:09:39 -05:00
|
|
|
return TarUntar(src, dst)
|
2013-06-20 23:20:16 -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.
|
|
|
|
//
|
|
|
|
// If `dst` ends with a trailing slash '/', the final destination path
|
|
|
|
// will be `dst/base(src)`.
|
2014-01-06 20:37:49 -05:00
|
|
|
func CopyFileWithTar(src, dst string) (err error) {
|
2013-06-20 23:20:16 -04:00
|
|
|
utils.Debugf("CopyFileWithTar(%s, %s)", src, dst)
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2013-06-20 23:20:16 -04:00
|
|
|
// Clean up the trailing /
|
|
|
|
if dst[len(dst)-1] == '/' {
|
|
|
|
dst = path.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
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-06 20:37:49 -05:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
errC := utils.Go(func() error {
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
srcF, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
|
|
|
|
|
|
|
tw := tar.NewWriter(w)
|
|
|
|
hdr, err := tar.FileInfoHeader(srcSt, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Name = filepath.Base(dst)
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tw, srcF); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tw.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer func() {
|
|
|
|
if er := <-errC; err != nil {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return Untar(r, filepath.Dir(dst), nil)
|
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()
|
|
|
|
return &TempArchive{f, size}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TempArchive struct {
|
|
|
|
*os.File
|
|
|
|
Size int64 // Pre-computed from Stat().Size() as a convenience
|
|
|
|
}
|
|
|
|
|
|
|
|
func (archive *TempArchive) Read(data []byte) (int, error) {
|
|
|
|
n, err := archive.File.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(archive.File.Name())
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|