2013-03-21 20:47:23 -04:00
|
|
|
package docker
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2013-05-28 16:37:49 -04:00
|
|
|
"fmt"
|
2013-06-18 23:28:49 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
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-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
type Archive io.Reader
|
|
|
|
|
2013-03-11 08:42:36 -04:00
|
|
|
type Compression uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
Uncompressed Compression = iota
|
|
|
|
Bzip2
|
|
|
|
Gzip
|
2013-04-01 19:16:28 -04:00
|
|
|
Xz
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (compression *Compression) Flag() string {
|
|
|
|
switch *compression {
|
|
|
|
case Bzip2:
|
|
|
|
return "j"
|
|
|
|
case Gzip:
|
|
|
|
return "z"
|
2013-04-01 19:16:28 -04:00
|
|
|
case Xz:
|
|
|
|
return "J"
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
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.
|
2013-03-11 08:42:36 -04:00
|
|
|
func Tar(path string, compression Compression) (io.Reader, error) {
|
2013-06-18 23:28:49 -04:00
|
|
|
return TarFilter(path, compression, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func TarFilter(path string, compression Compression, filter []string) (io.Reader, error) {
|
|
|
|
args := []string{"bsdtar", "-f", "-", "-C", path}
|
|
|
|
if filter == nil {
|
|
|
|
filter = []string{"."}
|
|
|
|
}
|
|
|
|
for _, f := range filter {
|
|
|
|
args = append(args, "-c"+compression.Flag(), f)
|
|
|
|
}
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2013-03-11 08:42:36 -04:00
|
|
|
return CmdStream(cmd)
|
|
|
|
}
|
|
|
|
|
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`.
|
|
|
|
// The archive may be compressed with one of the following algorithgms:
|
|
|
|
// 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-03-11 08:42:36 -04:00
|
|
|
func Untar(archive io.Reader, path string) error {
|
|
|
|
cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-x")
|
|
|
|
cmd.Stdin = archive
|
2013-06-01 01:25:48 -04:00
|
|
|
// Hardcode locale environment for predictable outcome regardless of host configuration.
|
|
|
|
// (see https://github.com/dotcloud/docker/issues/355)
|
|
|
|
cmd.Env = []string{"LANG=en_US.utf-8", "LC_ALL=en_US.utf-8"}
|
2013-03-11 08:42:36 -04:00
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2013-05-28 16:37:49 -04:00
|
|
|
return fmt.Errorf("%s: %s", err, output)
|
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.
|
|
|
|
func TarUntar(src string, filter []string, dst string) error {
|
|
|
|
utils.Debugf("TarUntar(%s %s %s)", src, filter, dst)
|
|
|
|
archive, err := TarFilter(src, Uncompressed, filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return Untar(archive, dst)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if archive, err := os.Open(src); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err := Untar(archive, dst); err != nil {
|
|
|
|
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-18 23:28:49 -04:00
|
|
|
var dstExists bool
|
|
|
|
dstSt, err := os.Stat(dst)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dstExists = true
|
|
|
|
}
|
|
|
|
// Things that can go wrong if the source is a directory
|
|
|
|
if srcSt.IsDir() {
|
|
|
|
// The destination exists and is a regular file
|
|
|
|
if dstExists && !dstSt.IsDir() {
|
|
|
|
return fmt.Errorf("Can't copy a directory over a regular file")
|
|
|
|
}
|
|
|
|
// Things that can go wrong if the source is a regular file
|
|
|
|
} else {
|
|
|
|
utils.Debugf("The destination exists, it's a directory, and doesn't end in /")
|
|
|
|
// The destination exists, it's a directory, and doesn't end in /
|
|
|
|
if dstExists && dstSt.IsDir() && dst[len(dst)-1] != '/' {
|
|
|
|
return fmt.Errorf("Can't copy a regular file over a directory %s |%s|", dst, dst[len(dst)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create the destination
|
|
|
|
var dstDir string
|
|
|
|
if dst[len(dst)-1] == '/' {
|
|
|
|
// The destination ends in /
|
|
|
|
// --> dst is the holding directory
|
|
|
|
dstDir = dst
|
|
|
|
} else {
|
|
|
|
// The destination doesn't end in /
|
|
|
|
// --> dst is the file
|
|
|
|
dstDir = path.Dir(dst)
|
|
|
|
}
|
|
|
|
if !dstExists {
|
|
|
|
// Create the holding directory if necessary
|
|
|
|
utils.Debugf("Creating the holding directory %s", dstDir)
|
|
|
|
if err := os.MkdirAll(dstDir, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !srcSt.IsDir() {
|
|
|
|
return TarUntar(path.Dir(src), []string{path.Base(src)}, dstDir)
|
|
|
|
}
|
|
|
|
return TarUntar(src, nil, dstDir)
|
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.
|
2013-03-11 08:42:36 -04:00
|
|
|
func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
|
|
|
|
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 {
|
|
|
|
pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
|
|
|
|
} 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
|
|
|
|
}
|