|
@@ -23,10 +23,7 @@ type Compression int
|
|
|
|
|
|
type TarOptions struct {
|
|
|
Includes []string
|
|
|
- Excludes []string
|
|
|
- Recursive bool
|
|
|
Compression Compression
|
|
|
- CreateFiles []string
|
|
|
}
|
|
|
|
|
|
const (
|
|
@@ -66,7 +63,7 @@ func DetectCompression(source []byte) Compression {
|
|
|
func xzDecompress(archive io.Reader) (io.Reader, error) {
|
|
|
args := []string{"xz", "-d", "-c", "-q"}
|
|
|
|
|
|
- return CmdStream(exec.Command(args[0], args[1:]...), archive, nil)
|
|
|
+ return CmdStream(exec.Command(args[0], args[1:]...), archive)
|
|
|
}
|
|
|
|
|
|
func DecompressStream(archive io.Reader) (io.Reader, error) {
|
|
@@ -100,16 +97,20 @@ func DecompressStream(archive io.Reader) (io.Reader, error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (compression *Compression) Flag() string {
|
|
|
- switch *compression {
|
|
|
- case Bzip2:
|
|
|
- return "j"
|
|
|
+func CompressStream(dest io.WriteCloser, compression Compression) (io.WriteCloser, error) {
|
|
|
+
|
|
|
+ switch compression {
|
|
|
+ case Uncompressed:
|
|
|
+ return dest, nil
|
|
|
case Gzip:
|
|
|
- return "z"
|
|
|
- case Xz:
|
|
|
- return "J"
|
|
|
+ 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())
|
|
|
}
|
|
|
- return ""
|
|
|
}
|
|
|
|
|
|
func (compression *Compression) Extension() string {
|
|
@@ -126,6 +127,59 @@ func (compression *Compression) Extension() string {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
+func addTarFile(path, name string, tw *tar.Writer) error {
|
|
|
+ fi, err := os.Lstat(path)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ link := ""
|
|
|
+ if fi.Mode()&os.ModeSymlink != 0 {
|
|
|
+ if link, err = os.Readlink(path); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ hdr, err := tar.FileInfoHeader(fi, link)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if fi.IsDir() && !strings.HasSuffix(name, "/") {
|
|
|
+ name = name + "/"
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+}
|
|
|
+
|
|
|
func createTarFile(path, extractDir string, hdr *tar.Header, reader *tar.Reader) error {
|
|
|
switch hdr.Typeflag {
|
|
|
case tar.TypeDir:
|
|
@@ -207,7 +261,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader *tar.Reader)
|
|
|
// Tar creates an archive from the directory at `path`, and returns it as a
|
|
|
// stream of bytes.
|
|
|
func Tar(path string, compression Compression) (io.Reader, error) {
|
|
|
- return TarFilter(path, &TarOptions{Recursive: true, Compression: compression})
|
|
|
+ return TarFilter(path, &TarOptions{Compression: compression})
|
|
|
}
|
|
|
|
|
|
func escapeName(name string) string {
|
|
@@ -228,57 +282,55 @@ func escapeName(name string) string {
|
|
|
|
|
|
// 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, options *TarOptions) (io.Reader, error) {
|
|
|
- args := []string{"tar", "--numeric-owner", "-f", "-", "-C", path, "-T", "-"}
|
|
|
- if options.Includes == nil {
|
|
|
- options.Includes = []string{"."}
|
|
|
- }
|
|
|
- args = append(args, "-c"+options.Compression.Flag())
|
|
|
+func TarFilter(srcPath string, options *TarOptions) (io.Reader, error) {
|
|
|
+ pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
|
- for _, exclude := range options.Excludes {
|
|
|
- args = append(args, fmt.Sprintf("--exclude=%s", exclude))
|
|
|
+ compressWriter, err := CompressStream(pipeWriter, options.Compression)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- if !options.Recursive {
|
|
|
- args = append(args, "--no-recursion")
|
|
|
- }
|
|
|
+ tw := tar.NewWriter(compressWriter)
|
|
|
|
|
|
- files := ""
|
|
|
- for _, f := range options.Includes {
|
|
|
- files = files + escapeName(f) + "\n"
|
|
|
- }
|
|
|
-
|
|
|
- tmpDir := ""
|
|
|
+ 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.CreateFiles != nil {
|
|
|
- var err error // Can't use := here or we override the outer tmpDir
|
|
|
- tmpDir, err = ioutil.TempDir("", "docker-tar")
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ if options.Includes == nil {
|
|
|
+ options.Includes = []string{"."}
|
|
|
}
|
|
|
|
|
|
- files = files + "-C" + tmpDir + "\n"
|
|
|
- for _, f := range options.CreateFiles {
|
|
|
- path := filepath.Join(tmpDir, f)
|
|
|
- err := os.MkdirAll(filepath.Dir(path), 0600)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ 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
|
|
|
+ }
|
|
|
|
|
|
- if file, err := os.OpenFile(path, os.O_CREATE, 0600); err != nil {
|
|
|
- return nil, err
|
|
|
- } else {
|
|
|
- file.Close()
|
|
|
- }
|
|
|
- files = files + escapeName(f) + "\n"
|
|
|
+ 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
|
|
|
+ })
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return CmdStream(exec.Command(args[0], args[1:]...), bytes.NewBufferString(files), func() {
|
|
|
- if tmpDir != "" {
|
|
|
- _ = os.RemoveAll(tmpDir)
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ return pipeReader, nil
|
|
|
}
|
|
|
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
@@ -311,19 +363,6 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if options != nil {
|
|
|
- excludeFile := false
|
|
|
- for _, exclude := range options.Excludes {
|
|
|
- if strings.HasPrefix(hdr.Name, exclude) {
|
|
|
- excludeFile = true
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- if excludeFile {
|
|
|
- continue
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// Normalize name, for safety and for a simple is-root check
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
@@ -378,9 +417,9 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
|
|
// 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, &TarOptions{Compression: Uncompressed, Includes: filter, Recursive: true})
|
|
|
+func TarUntar(src string, dst string) error {
|
|
|
+ utils.Debugf("TarUntar(%s %s)", src, dst)
|
|
|
+ archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -417,7 +456,7 @@ func CopyWithTar(src, dst string) error {
|
|
|
return err
|
|
|
}
|
|
|
utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
|
|
- return TarUntar(src, nil, dst)
|
|
|
+ return TarUntar(src, dst)
|
|
|
}
|
|
|
|
|
|
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
|
@@ -480,13 +519,10 @@ func CopyFileWithTar(src, dst string) (err error) {
|
|
|
// 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.
|
|
|
-func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error) {
|
|
|
+func CmdStream(cmd *exec.Cmd, input io.Reader) (io.Reader, error) {
|
|
|
if input != nil {
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
if err != nil {
|
|
|
- if atEnd != nil {
|
|
|
- atEnd()
|
|
|
- }
|
|
|
return nil, err
|
|
|
}
|
|
|
// Write stdin if any
|
|
@@ -497,16 +533,10 @@ func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error)
|
|
|
}
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
if err != nil {
|
|
|
- if atEnd != nil {
|
|
|
- atEnd()
|
|
|
- }
|
|
|
return nil, err
|
|
|
}
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
if err != nil {
|
|
|
- if atEnd != nil {
|
|
|
- atEnd()
|
|
|
- }
|
|
|
return nil, err
|
|
|
}
|
|
|
pipeR, pipeW := io.Pipe()
|
|
@@ -531,9 +561,6 @@ func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error)
|
|
|
} else {
|
|
|
pipeW.Close()
|
|
|
}
|
|
|
- if atEnd != nil {
|
|
|
- atEnd()
|
|
|
- }
|
|
|
}()
|
|
|
// Run the command and return the pipe
|
|
|
if err := cmd.Start(); err != nil {
|