2014-10-29 22:06:51 +03:00
|
|
|
package chrootarchive
|
|
|
|
|
|
|
|
import (
|
2014-11-08 18:38:42 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-10-29 22:06:51 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2014-12-09 00:19:24 +03:00
|
|
|
"path/filepath"
|
2014-10-29 22:06:51 +03:00
|
|
|
"runtime"
|
2014-11-08 18:38:42 +03:00
|
|
|
"strings"
|
2014-10-29 22:06:51 +03:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
|
|
)
|
|
|
|
|
2014-12-12 21:38:48 +03:00
|
|
|
var chrootArchiver = &archive.Archiver{Untar: Untar}
|
2014-12-09 00:14:56 +03:00
|
|
|
|
|
|
|
func chroot(path string) error {
|
|
|
|
if err := syscall.Chroot(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return syscall.Chdir("/")
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:06:51 +03:00
|
|
|
func untar() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
flag.Parse()
|
2014-12-09 00:19:24 +03:00
|
|
|
if err := chroot(flag.Arg(0)); err != nil {
|
2014-10-29 22:06:51 +03:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-09 00:19:24 +03:00
|
|
|
var options *archive.TarOptions
|
|
|
|
if err := json.NewDecoder(strings.NewReader(flag.Arg(1))).Decode(&options); err != nil {
|
2014-11-08 18:38:42 +03:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-09 00:19:24 +03:00
|
|
|
if err := archive.Unpack(os.Stdin, "/", options); err != nil {
|
2014-10-29 22:06:51 +03:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-06 05:30:03 +03:00
|
|
|
// fully consume stdin in case it is zero padded
|
2014-12-09 02:04:34 +03:00
|
|
|
flush(os.Stdin)
|
2014-10-29 22:06:51 +03:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-12-09 00:14:56 +03:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
|
|
|
if tarArchive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
2014-10-24 01:30:11 +04:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-09 00:14:56 +03:00
|
|
|
}
|
2014-10-29 22:06:51 +03:00
|
|
|
|
2014-12-09 00:19:24 +03:00
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
enc = json.NewEncoder(&buf)
|
|
|
|
)
|
2014-11-08 18:38:42 +03:00
|
|
|
if err := enc.Encode(options); err != nil {
|
|
|
|
return fmt.Errorf("Untar json encode: %v", err)
|
|
|
|
}
|
2014-10-29 22:06:51 +03:00
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(dest, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 00:19:24 +03:00
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
decompressedArchive, err := archive.DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
2014-11-08 18:38:42 +03:00
|
|
|
|
|
|
|
cmd := reexec.Command("docker-untar", dest, buf.String())
|
2014-12-09 00:19:24 +03:00
|
|
|
cmd.Stdin = decompressedArchive
|
2014-10-29 22:06:51 +03:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Untar %s %s", err, out)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TarUntar(src, dst string) error {
|
|
|
|
return chrootArchiver.TarUntar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 chrootArchiver.CopyWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)`.
|
|
|
|
func CopyFileWithTar(src, dst string) (err error) {
|
|
|
|
return chrootArchiver.CopyFileWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 chrootArchiver.UntarPath(src, dst)
|
|
|
|
}
|