2014-10-29 19:06:51 +00:00
|
|
|
package chrootarchive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-07-27 13:46:20 +00:00
|
|
|
"io/ioutil"
|
2014-10-29 19:06:51 +00:00
|
|
|
"os"
|
2014-12-08 21:19:24 +00:00
|
|
|
"path/filepath"
|
2014-10-29 19:06:51 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-05-14 22:08:00 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-10-29 19:06:51 +00:00
|
|
|
)
|
|
|
|
|
2014-12-12 18:38:48 +00:00
|
|
|
var chrootArchiver = &archive.Archiver{Untar: Untar}
|
2014-12-08 21:14:56 +00:00
|
|
|
|
2015-06-16 09:51:27 +00: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.
|
2014-12-08 21:14:56 +00:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
2015-07-27 13:46:20 +00:00
|
|
|
return untarHandler(tarArchive, dest, options, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive must be an uncompressed stream.
|
|
|
|
func UntarUncompressed(tarArchive io.Reader, dest string, options *archive.TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for teasing out the automatic decompression
|
|
|
|
func untarHandler(tarArchive io.Reader, dest string, options *archive.TarOptions, decompress bool) error {
|
2015-06-01 23:42:27 +00:00
|
|
|
|
2014-12-08 21:14:56 +00:00
|
|
|
if tarArchive == nil {
|
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
if options == nil {
|
|
|
|
options = &archive.TarOptions{}
|
|
|
|
}
|
2014-10-23 21:30:11 +00:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 21:14:56 +00:00
|
|
|
}
|
2014-10-29 19:06:51 +00:00
|
|
|
|
2015-01-29 15:28:44 +00:00
|
|
|
dest = filepath.Clean(dest)
|
2014-10-29 19:06:51 +00:00
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
2015-05-14 22:08:00 +00:00
|
|
|
if err := system.MkdirAll(dest, 0777); err != nil {
|
2014-10-29 19:06:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 15:28:44 +00:00
|
|
|
|
2015-07-27 13:46:20 +00:00
|
|
|
r := ioutil.NopCloser(tarArchive)
|
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := archive.DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 21:19:24 +00:00
|
|
|
}
|
2014-11-08 15:38:42 +00:00
|
|
|
|
2015-07-27 13:46:20 +00:00
|
|
|
return invokeUnpack(r, dest, options)
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 09:51:27 +00: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-10-29 19:06:51 +00:00
|
|
|
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.
|
|
|
|
//
|
2015-06-01 23:42:27 +00:00
|
|
|
// If `dst` ends with a trailing slash '/' ('\' on Windows), the final
|
|
|
|
// destination path will be `dst/base(src)` or `dst\base(src)`
|
2014-10-29 19:06:51 +00:00
|
|
|
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)
|
|
|
|
}
|