2013-10-31 19:57:45 -04:00
|
|
|
package archive
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
import (
|
2015-05-01 18:01:10 -04:00
|
|
|
"archive/tar"
|
2014-05-09 18:49:02 -04:00
|
|
|
"bufio"
|
2014-05-16 19:26:28 -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-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-20 23:20:16 -04:00
|
|
|
"path/filepath"
|
2015-05-07 18:14:11 -04:00
|
|
|
"runtime"
|
2013-12-20 06:08:34 -05:00
|
|
|
"strings"
|
2013-12-20 05:30:45 -05:00
|
|
|
"syscall"
|
2014-05-09 18:49:02 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-24 18:11:48 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-08-12 12:29:46 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
type (
|
2015-08-03 21:52:54 -04:00
|
|
|
// Archive is a type of io.ReadCloser which has two interfaces Read and Closer.
|
|
|
|
Archive io.ReadCloser
|
|
|
|
// Reader is a type of io.Reader.
|
|
|
|
Reader io.Reader
|
2015-12-13 11:00:39 -05:00
|
|
|
// Compression is the state represents if compressed or not.
|
2015-08-03 21:52:54 -04:00
|
|
|
Compression int
|
2016-04-06 18:07:29 -04:00
|
|
|
// WhiteoutFormat is the format of whiteouts unpacked
|
|
|
|
WhiteoutFormat int
|
2015-08-03 21:52:54 -04:00
|
|
|
// TarChownOptions wraps the chown options UID and GID.
|
2015-01-19 15:15:36 -05:00
|
|
|
TarChownOptions struct {
|
|
|
|
UID, GID int
|
|
|
|
}
|
2015-08-03 21:52:54 -04:00
|
|
|
// TarOptions wraps the tar options.
|
2015-01-19 15:15:36 -05:00
|
|
|
TarOptions struct {
|
|
|
|
IncludeFiles []string
|
|
|
|
ExcludePatterns []string
|
|
|
|
Compression Compression
|
|
|
|
NoLchown bool
|
2015-10-08 11:51:41 -04:00
|
|
|
UIDMaps []idtools.IDMap
|
|
|
|
GIDMaps []idtools.IDMap
|
2015-01-19 15:15:36 -05:00
|
|
|
ChownOpts *TarChownOptions
|
|
|
|
IncludeSourceDir bool
|
2016-04-06 18:07:29 -04:00
|
|
|
// WhiteoutFormat is the expected on disk format for whiteout files.
|
|
|
|
// This format will be converted to the standard format on pack
|
|
|
|
// and from the standard format on unpack.
|
|
|
|
WhiteoutFormat WhiteoutFormat
|
2015-01-19 15:15:36 -05:00
|
|
|
// When unpacking, specifies whether overwriting a directory with a
|
|
|
|
// non-directory is allowed and vice versa.
|
|
|
|
NoOverwriteDirNonDir bool
|
2015-07-24 17:12:55 -04:00
|
|
|
// For each include when creating an archive, the included name will be
|
|
|
|
// replaced with the matching name from this map.
|
|
|
|
RebaseNames map[string]string
|
2014-02-06 17:13:03 -05:00
|
|
|
}
|
2014-11-06 13:01:37 -05:00
|
|
|
|
|
|
|
// Archiver allows the reuse of most utility functions of this package
|
2015-10-08 11:51:41 -04:00
|
|
|
// with a pluggable Untar function. Also, to facilitate the passing of
|
|
|
|
// specific id mappings for untar, an archiver can be created with maps
|
|
|
|
// which will then be passed to Untar operations
|
2014-11-06 13:01:37 -05:00
|
|
|
Archiver struct {
|
2015-10-08 11:51:41 -04:00
|
|
|
Untar func(io.Reader, string, *TarOptions) error
|
|
|
|
UIDMaps []idtools.IDMap
|
|
|
|
GIDMaps []idtools.IDMap
|
2014-11-06 13:01:37 -05:00
|
|
|
}
|
2014-10-20 15:35:48 -04:00
|
|
|
|
|
|
|
// breakoutError is used to differentiate errors related to breaking out
|
|
|
|
// When testing archive breakout in the unit tests, this error is expected
|
|
|
|
// in order for the test to pass.
|
|
|
|
breakoutError error
|
2014-02-06 17:13:03 -05:00
|
|
|
)
|
2013-11-11 17:30:38 -05:00
|
|
|
|
2014-02-06 17:13:03 -05:00
|
|
|
var (
|
2015-08-03 21:52:54 -04:00
|
|
|
// ErrNotImplemented is the error message of function not implemented.
|
2014-02-06 17:13:03 -05:00
|
|
|
ErrNotImplemented = errors.New("Function not implemented")
|
2015-10-08 11:51:41 -04:00
|
|
|
defaultArchiver = &Archiver{Untar: Untar, UIDMaps: nil, GIDMaps: nil}
|
2014-02-06 17:13:03 -05:00
|
|
|
)
|
2013-03-11 08:42:36 -04:00
|
|
|
|
2015-11-20 11:49:33 -05:00
|
|
|
const (
|
|
|
|
// HeaderSize is the size in bytes of a tar header
|
|
|
|
HeaderSize = 512
|
|
|
|
)
|
|
|
|
|
2013-03-11 08:42:36 -04:00
|
|
|
const (
|
2015-08-03 21:52:54 -04:00
|
|
|
// Uncompressed represents the uncompressed.
|
2013-03-11 08:42:36 -04:00
|
|
|
Uncompressed Compression = iota
|
2015-08-03 21:52:54 -04:00
|
|
|
// Bzip2 is bzip2 compression algorithm.
|
2013-03-11 08:42:36 -04:00
|
|
|
Bzip2
|
2015-08-03 21:52:54 -04:00
|
|
|
// Gzip is gzip compression algorithm.
|
2013-03-11 08:42:36 -04:00
|
|
|
Gzip
|
2015-08-03 21:52:54 -04:00
|
|
|
// Xz is xz compression algorithm.
|
2013-04-01 19:16:28 -04:00
|
|
|
Xz
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
const (
|
2016-06-21 03:28:30 -04:00
|
|
|
// AUFSWhiteoutFormat is the default format for whiteouts
|
2016-04-06 18:07:29 -04:00
|
|
|
AUFSWhiteoutFormat WhiteoutFormat = iota
|
|
|
|
// OverlayWhiteoutFormat formats whiteout according to the overlay
|
|
|
|
// standard.
|
|
|
|
OverlayWhiteoutFormat
|
|
|
|
)
|
|
|
|
|
2015-11-20 11:49:33 -05:00
|
|
|
// IsArchive checks for the magic bytes of a tar or any supported compression
|
|
|
|
// algorithm.
|
2014-05-09 18:26:41 -04:00
|
|
|
func IsArchive(header []byte) bool {
|
|
|
|
compression := DetectCompression(header)
|
|
|
|
if compression != Uncompressed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
r := tar.NewReader(bytes.NewBuffer(header))
|
|
|
|
_, err := r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:49:33 -05:00
|
|
|
// IsArchivePath checks if the (possibly compressed) file at the given path
|
|
|
|
// starts with a tar file header.
|
|
|
|
func IsArchivePath(path string) bool {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
rdr, err := DecompressStream(file)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
r := tar.NewReader(rdr)
|
|
|
|
_, err = r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// DetectCompression detects the compression algorithm of the source.
|
2013-06-13 20:53:38 -04:00
|
|
|
func DetectCompression(source []byte) Compression {
|
|
|
|
for compression, m := range map[Compression][]byte{
|
|
|
|
Bzip2: {0x42, 0x5A, 0x68},
|
|
|
|
Gzip: {0x1F, 0x8B, 0x08},
|
|
|
|
Xz: {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00},
|
|
|
|
} {
|
2014-05-16 15:47:50 -04:00
|
|
|
if len(source) < len(m) {
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Debug("Len too short")
|
2014-05-16 15:47:50 -04:00
|
|
|
continue
|
|
|
|
}
|
2014-05-16 15:15:21 -04:00
|
|
|
if bytes.Compare(m, source[:len(m)]) == 0 {
|
2013-06-13 20:53:38 -04:00
|
|
|
return compression
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Uncompressed
|
|
|
|
}
|
|
|
|
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
func xzDecompress(archive io.Reader) (io.ReadCloser, <-chan struct{}, error) {
|
2013-12-18 04:50:22 -05:00
|
|
|
args := []string{"xz", "-d", "-c", "-q"}
|
|
|
|
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
return cmdStream(exec.Command(args[0], args[1:]...), archive)
|
2013-12-18 04:50:22 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 11:12:38 -04:00
|
|
|
// DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
|
2014-02-14 05:12:49 -05:00
|
|
|
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
|
2014-08-12 12:29:46 -04:00
|
|
|
p := pools.BufioReader32KPool
|
|
|
|
buf := p.Get(archive)
|
2014-05-09 18:49:02 -04:00
|
|
|
bs, err := buf.Peek(10)
|
2015-11-23 20:20:44 -05:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
// Note: we'll ignore any io.EOF error because there are some odd
|
|
|
|
// cases where the layer.tar file will be empty (zero bytes) and
|
|
|
|
// that results in an io.EOF from the Peek() call. So, in those
|
|
|
|
// cases we'll just treat it as a non-compressed stream and
|
|
|
|
// that means just create an empty layer.
|
|
|
|
// See Issue 18170
|
2014-05-09 18:49:02 -04:00
|
|
|
return nil, err
|
2013-12-17 08:19:48 -05:00
|
|
|
}
|
2014-05-09 18:49:02 -04:00
|
|
|
|
|
|
|
compression := DetectCompression(bs)
|
2013-12-17 08:19:48 -05:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 12:29:46 -04:00
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, buf)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 08:19:48 -05:00
|
|
|
case Gzip:
|
2014-08-12 12:29:46 -04:00
|
|
|
gzReader, err := gzip.NewReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 08:19:48 -05:00
|
|
|
case Bzip2:
|
2014-08-12 12:29:46 -04:00
|
|
|
bz2Reader := bzip2.NewReader(buf)
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-18 04:50:22 -05:00
|
|
|
case Xz:
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
xzReader, chdone, err := xzDecompress(buf)
|
2014-08-12 12:29:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
return ioutils.NewReadCloserWrapper(readBufWrapper, func() error {
|
|
|
|
<-chdone
|
|
|
|
return readBufWrapper.Close()
|
|
|
|
}), nil
|
2013-12-17 08:19:48 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:12:38 -04:00
|
|
|
// CompressStream compresseses the dest with specified compression algorithm.
|
2016-04-07 16:21:05 -04:00
|
|
|
func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) {
|
2014-08-12 12:29:46 -04:00
|
|
|
p := pools.BufioWriter32KPool
|
|
|
|
buf := p.Get(dest)
|
2014-01-20 06:07:34 -05:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 12:29:46 -04:00
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
|
|
|
|
return writeBufWrapper, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
case Gzip:
|
2014-08-12 12:29:46 -04:00
|
|
|
gzWriter := gzip.NewWriter(dest)
|
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
|
|
|
|
return writeBufWrapper, nil
|
2014-01-20 06:07:34 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// Extension returns the extension of a file that uses the specified compression algorithm.
|
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 ""
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
type tarWhiteoutConverter interface {
|
|
|
|
ConvertWrite(*tar.Header, string, os.FileInfo) error
|
|
|
|
ConvertRead(*tar.Header, string) (bool, error)
|
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
type tarAppender struct {
|
|
|
|
TarWriter *tar.Writer
|
|
|
|
Buffer *bufio.Writer
|
|
|
|
|
|
|
|
// for hardlink mapping
|
|
|
|
SeenFiles map[uint64]string
|
2015-10-08 11:51:41 -04:00
|
|
|
UIDMaps []idtools.IDMap
|
|
|
|
GIDMaps []idtools.IDMap
|
2015-12-14 00:42:52 -05:00
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
// For packing and unpacking whiteout files in the
|
|
|
|
// non standard format. The whiteout files defined
|
|
|
|
// by the AUFS standard are used as the tar whiteout
|
|
|
|
// standard.
|
|
|
|
WhiteoutConverter tarWhiteoutConverter
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:27:07 -05:00
|
|
|
// canonicalTarName provides a platform-independent and consistent posix-style
|
|
|
|
//path for files and directories to be archived regardless of the platform.
|
|
|
|
func canonicalTarName(name string, isDir bool) (string, error) {
|
2015-02-24 22:43:29 -05:00
|
|
|
name, err := CanonicalTarNameForPath(name)
|
2015-02-17 15:27:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// suffix with '/' for directories
|
|
|
|
if isDir && !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2015-12-14 00:42:52 -05:00
|
|
|
// addTarFile adds to the tar archive a file from `path` as `name`
|
2014-09-15 14:45:53 -04:00
|
|
|
func (ta *tarAppender) addTarFile(path, name string) 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
|
|
|
|
}
|
2015-03-03 21:40:16 -05:00
|
|
|
hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
|
2014-01-20 05:48:47 -05:00
|
|
|
|
2015-02-17 15:27:07 -05:00
|
|
|
name, err = canonicalTarName(name, fi.IsDir())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tar: cannot canonicalize path: %v", err)
|
2014-01-21 06:52:05 -05:00
|
|
|
}
|
2014-01-20 05:48:47 -05:00
|
|
|
hdr.Name = name
|
|
|
|
|
2015-10-12 15:11:22 -04:00
|
|
|
inode, err := setHeaderForSpecialDevice(hdr, ta, name, fi.Sys())
|
2014-11-13 15:36:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-20 06:26:08 -05:00
|
|
|
}
|
2014-11-13 15:36:05 -05:00
|
|
|
|
2015-10-12 15:11:22 -04:00
|
|
|
// if it's not a directory and has more than 1 link,
|
2014-09-15 14:45:53 -04:00
|
|
|
// it's hardlinked, so set the type flag accordingly
|
2015-10-12 15:11:22 -04:00
|
|
|
if !fi.IsDir() && hasHardlinks(fi) {
|
2014-09-15 14:45:53 -04:00
|
|
|
// a link should have a name that it links too
|
|
|
|
// and that linked name should be first in the tar archive
|
2014-10-30 13:42:57 -04:00
|
|
|
if oldpath, ok := ta.SeenFiles[inode]; ok {
|
2014-09-15 14:45:53 -04:00
|
|
|
hdr.Typeflag = tar.TypeLink
|
|
|
|
hdr.Linkname = oldpath
|
|
|
|
hdr.Size = 0 // This Must be here for the writer math to add up!
|
|
|
|
} else {
|
2014-10-30 13:42:57 -04:00
|
|
|
ta.SeenFiles[inode] = name
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 04:12:25 -05:00
|
|
|
capability, _ := system.Lgetxattr(path, "security.capability")
|
2014-01-20 06:26:08 -05:00
|
|
|
if capability != nil {
|
|
|
|
hdr.Xattrs = make(map[string]string)
|
|
|
|
hdr.Xattrs["security.capability"] = string(capability)
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
//handle re-mapping container ID mappings back to host ID mappings before
|
2015-12-09 10:11:32 -05:00
|
|
|
//writing tar headers/files. We skip whiteout files because they were written
|
|
|
|
//by the kernel and already have proper ownership relative to the host
|
|
|
|
if !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && (ta.UIDMaps != nil || ta.GIDMaps != nil) {
|
2015-10-08 11:51:41 -04:00
|
|
|
uid, gid, err := getFileUIDGID(fi.Sys())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
xUID, err := idtools.ToContainer(uid, ta.UIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
xGID, err := idtools.ToContainer(gid, ta.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Uid = xUID
|
|
|
|
hdr.Gid = xGID
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
if ta.WhiteoutConverter != nil {
|
|
|
|
if err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi); err != nil {
|
|
|
|
return err
|
2015-12-14 00:42:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.TarWriter.WriteHeader(hdr); err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-23 16:34:38 -04:00
|
|
|
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
|
2014-07-14 13:11:48 -04:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2014-01-17 05:21:36 -05:00
|
|
|
return err
|
2014-07-14 13:11:48 -04:00
|
|
|
}
|
2014-07-14 12:31:19 -04:00
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
ta.Buffer.Reset(ta.TarWriter)
|
2014-10-28 17:01:10 -04:00
|
|
|
defer ta.Buffer.Reset(nil)
|
2014-09-15 14:45:53 -04:00
|
|
|
_, err = io.Copy(ta.Buffer, file)
|
2014-07-14 12:31:19 -04:00
|
|
|
file.Close()
|
|
|
|
if err != nil {
|
2014-07-14 13:11:48 -04:00
|
|
|
return err
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
2014-09-15 14:45:53 -04:00
|
|
|
err = ta.Buffer.Flush()
|
2014-07-14 12:31:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-17 05:21:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *TarChownOptions) 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:
|
2015-05-07 18:14:11 -04:00
|
|
|
// Handle this is an OS-specific way
|
|
|
|
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeLink:
|
2014-10-20 15:36:28 -04:00
|
|
|
targetPath := filepath.Join(extractDir, hdr.Linkname)
|
|
|
|
// check for hardlink breakout
|
|
|
|
if !strings.HasPrefix(targetPath, extractDir) {
|
|
|
|
return breakoutError(fmt.Errorf("invalid hardlink %q -> %q", targetPath, hdr.Linkname))
|
|
|
|
}
|
|
|
|
if err := os.Link(targetPath, path); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeSymlink:
|
2014-11-19 11:27:34 -05:00
|
|
|
// path -> hdr.Linkname = targetPath
|
|
|
|
// e.g. /extractDir/path/to/symlink -> ../2/file = /extractDir/path/2/file
|
|
|
|
targetPath := filepath.Join(filepath.Dir(path), hdr.Linkname)
|
|
|
|
|
|
|
|
// the reason we don't need to check symlinks in the path (with FollowSymlinkInScope) is because
|
|
|
|
// that symlink would first have to be created, which would be caught earlier, at this very check:
|
|
|
|
if !strings.HasPrefix(targetPath, extractDir) {
|
|
|
|
return breakoutError(fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname))
|
2014-10-20 15:36:28 -04:00
|
|
|
}
|
2013-12-20 05:30:45 -05:00
|
|
|
if err := os.Symlink(hdr.Linkname, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-31 08:50:07 -05:00
|
|
|
case tar.TypeXGlobalHeader:
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Debug("PAX Global Extended Headers found and ignored")
|
2014-01-31 08:50:07 -05:00
|
|
|
return nil
|
|
|
|
|
2013-12-20 05:30:45 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unhandled tar header type %d\n", hdr.Typeflag)
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
// Lchown is not supported on Windows.
|
|
|
|
if Lchown && runtime.GOOS != "windows" {
|
|
|
|
if chownOpts == nil {
|
|
|
|
chownOpts = &TarChownOptions{UID: hdr.Uid, GID: hdr.Gid}
|
|
|
|
}
|
|
|
|
if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
|
2015-05-07 18:14:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-12-20 05:30:45 -05:00
|
|
|
}
|
|
|
|
|
2016-03-16 07:54:57 -04:00
|
|
|
var errors []string
|
2014-01-15 09:05:30 -05:00
|
|
|
for key, value := range hdr.Xattrs {
|
2014-02-21 04:12:25 -05:00
|
|
|
if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
|
2016-06-09 10:24:04 -04:00
|
|
|
if err == syscall.ENOTSUP {
|
|
|
|
// We ignore errors here because not all graphdrivers support
|
|
|
|
// xattrs *cough* old versions of AUFS *cough*. However only
|
|
|
|
// ENOTSUP should be emitted in that case, otherwise we still
|
|
|
|
// bail.
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
2014-01-15 09:05:30 -05:00
|
|
|
}
|
2016-03-16 07:54:57 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"errors": errors,
|
|
|
|
}).Warn("ignored xattrs in archive: underlying filesystem doesn't support them")
|
2014-01-15 09:05:30 -05:00
|
|
|
}
|
|
|
|
|
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
|
2015-05-07 18:14:11 -04:00
|
|
|
if err := handleLChmod(hdr, path, hdrInfo); err != nil {
|
|
|
|
return err
|
2013-12-20 05:30:45 -05:00
|
|
|
}
|
|
|
|
|
2015-11-05 14:47:18 -05:00
|
|
|
aTime := hdr.AccessTime
|
|
|
|
if aTime.Before(hdr.ModTime) {
|
|
|
|
// Last access time should never be before last modified time.
|
|
|
|
aTime = hdr.ModTime
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:45:32 -04:00
|
|
|
// system.Chtimes doesn't support a NOFOLLOW flag atm
|
2015-03-01 18:55:28 -05:00
|
|
|
if hdr.Typeflag == tar.TypeLink {
|
|
|
|
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
|
2015-11-05 14:47:18 -05:00
|
|
|
if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
|
2015-03-01 18:55:28 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if hdr.Typeflag != tar.TypeSymlink {
|
2015-11-05 14:47:18 -05:00
|
|
|
if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
|
2013-12-20 05:30:45 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-05 14:47:18 -05:00
|
|
|
ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)}
|
2014-06-18 19:42:22 -04:00
|
|
|
if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
|
2013-12-20 05:30:45 -05:00
|
|
|
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-02-13 19:05:36 -05:00
|
|
|
return TarWithOptions(path, &TarOptions{Compression: compression})
|
2013-09-05 16:10:29 -04:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:05:36 -05:00
|
|
|
// TarWithOptions creates an archive from the directory at `path`, only including files whose relative
|
2014-10-23 17:30:11 -04:00
|
|
|
// paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
|
2014-02-13 19:05:36 -05:00
|
|
|
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
2015-08-10 18:21:30 -04:00
|
|
|
// Fix the source path to work with long path names. This is a no-op
|
|
|
|
// on platforms other than Windows.
|
|
|
|
srcPath = fixVolumePathPrefix(srcPath)
|
|
|
|
|
2015-04-09 15:07:06 -04:00
|
|
|
patterns, patDirs, exceptions, err := fileutils.CleanPatterns(options.ExcludePatterns)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
go func() {
|
2014-09-15 14:45:53 -04:00
|
|
|
ta := &tarAppender{
|
2016-04-06 18:07:29 -04:00
|
|
|
TarWriter: tar.NewWriter(compressWriter),
|
|
|
|
Buffer: pools.BufioWriter32KPool.Get(nil),
|
|
|
|
SeenFiles: make(map[uint64]string),
|
|
|
|
UIDMaps: options.UIDMaps,
|
|
|
|
GIDMaps: options.GIDMaps,
|
|
|
|
WhiteoutConverter: getWhiteoutConverter(options.WhiteoutFormat),
|
2014-09-15 14:45:53 -04:00
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
if err := ta.TarWriter.Close(); err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Can't close tar writer: %s", err)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
if err := compressWriter.Close(); err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Can't close compress writer: %s", err)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
if err := pipeWriter.Close(); err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Can't close pipe writer: %s", err)
|
2015-01-19 15:15:36 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
// this buffer is needed for the duration of this piped stream
|
|
|
|
defer pools.BufioWriter32KPool.Put(ta.Buffer)
|
|
|
|
|
2014-01-20 06:07:34 -05:00
|
|
|
// 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
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
stat, err := os.Lstat(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
|
|
|
// We can't later join a non-dir with any includes because the
|
|
|
|
// 'walk' will error if "file/." is stat-ed and "file" is not a
|
|
|
|
// directory. So, we must split the source path and use the
|
|
|
|
// basename as the include.
|
|
|
|
if len(options.IncludeFiles) > 0 {
|
|
|
|
logrus.Warn("Tar: Can't archive a file with includes")
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, base := SplitPathDirEntry(srcPath)
|
|
|
|
srcPath = dir
|
|
|
|
options.IncludeFiles = []string{base}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(options.IncludeFiles) == 0 {
|
2014-10-23 17:30:11 -04:00
|
|
|
options.IncludeFiles = []string{"."}
|
2014-01-20 06:07:34 -05:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, include := range options.IncludeFiles {
|
2015-07-24 17:12:55 -04:00
|
|
|
rebaseName := options.RebaseNames[include]
|
|
|
|
|
2015-08-10 18:21:30 -04:00
|
|
|
walkRoot := getWalkRoot(srcPath, include)
|
2015-01-19 15:15:36 -05:00
|
|
|
filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
|
2014-01-20 06:07:34 -05:00
|
|
|
if err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err)
|
2014-01-20 06:07:34 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath, err := filepath.Rel(srcPath, filePath)
|
2015-01-19 15:15:36 -05:00
|
|
|
if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
|
2014-10-27 20:23:50 -04:00
|
|
|
// Error getting relative path OR we are looking
|
2015-01-19 15:15:36 -05:00
|
|
|
// at the source directory path. Skip in both situations.
|
2014-01-20 06:07:34 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
if options.IncludeSourceDir && include == "." && relFilePath != "." {
|
|
|
|
relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
|
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
skip := false
|
|
|
|
|
|
|
|
// If "include" is an exact match for the current file
|
|
|
|
// then even if there's an "excludePatterns" pattern that
|
|
|
|
// matches it, don't skip it. IOW, assume an explicit 'include'
|
|
|
|
// is asking for that file no matter what - which is true
|
|
|
|
// for some files, like .dockerignore and Dockerfile (sometimes)
|
|
|
|
if include != relFilePath {
|
2015-04-09 15:07:06 -04:00
|
|
|
skip, err = fileutils.OptimizedMatches(relFilePath, patterns, patDirs)
|
2014-10-23 17:30:11 -04:00
|
|
|
if err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Error matching %s: %v", relFilePath, err)
|
2014-10-23 17:30:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-07-07 08:23:07 -04:00
|
|
|
}
|
2014-07-31 18:11:15 -04:00
|
|
|
|
2014-07-07 08:23:07 -04:00
|
|
|
if skip {
|
2016-02-19 14:17:15 -05:00
|
|
|
// If we want to skip this file and its a directory
|
|
|
|
// then we should first check to see if there's an
|
|
|
|
// excludes pattern (eg !dir/file) that starts with this
|
|
|
|
// dir. If so then we can't skip this dir.
|
|
|
|
|
|
|
|
// Its not a dir then so we can just return/skip.
|
|
|
|
if !f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// No exceptions (!...) in patterns so just skip dir
|
|
|
|
if !exceptions {
|
2014-07-07 08:23:07 -04:00
|
|
|
return filepath.SkipDir
|
2014-02-13 19:05:36 -05:00
|
|
|
}
|
2016-02-19 14:17:15 -05:00
|
|
|
|
|
|
|
dirSlash := relFilePath + string(filepath.Separator)
|
|
|
|
|
|
|
|
for _, pat := range patterns {
|
|
|
|
if pat[0] != '!' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pat = pat[1:] + string(filepath.Separator)
|
|
|
|
if strings.HasPrefix(pat, dirSlash) {
|
|
|
|
// found a match - so can't skip this dir
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No matching exclusion dir so just skip dir
|
|
|
|
return filepath.SkipDir
|
2014-02-13 19:05:36 -05:00
|
|
|
}
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
if seen[relFilePath] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
seen[relFilePath] = true
|
|
|
|
|
2015-07-24 17:12:55 -04:00
|
|
|
// Rename the base resource.
|
|
|
|
if rebaseName != "" {
|
|
|
|
var replacement string
|
|
|
|
if rebaseName != string(filepath.Separator) {
|
|
|
|
// Special case the root directory to replace with an
|
|
|
|
// empty string instead so that we don't end up with
|
|
|
|
// double slashes in the paths.
|
|
|
|
replacement = rebaseName
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath = strings.Replace(relFilePath, include, replacement, 1)
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:45:53 -04:00
|
|
|
if err := ta.addTarFile(filePath, relFilePath); err != nil {
|
2016-02-19 05:50:11 -05:00
|
|
|
logrus.Errorf("Can't add file %s to tar: %s", filePath, err)
|
2016-03-28 09:17:21 -04:00
|
|
|
// if pipe is broken, stop writing tar stream to it
|
2016-02-19 05:50:11 -05:00
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-20 06:07:34 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// Unpack unpacks the decompressedArchive to dest with options.
|
2014-12-08 16:19:24 -05:00
|
|
|
func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
|
2014-02-14 05:12:49 -05:00
|
|
|
tr := tar.NewReader(decompressedArchive)
|
2014-08-12 12:29:46 -04:00
|
|
|
trBuf := pools.BufioReader32KPool.Get(nil)
|
|
|
|
defer pools.BufioReader32KPool.Put(trBuf)
|
2013-12-20 06:08:34 -05:00
|
|
|
|
2014-07-08 15:26:59 -04:00
|
|
|
var dirs []*tar.Header
|
2015-10-08 11:51:41 -04:00
|
|
|
remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-06 18:07:29 -04:00
|
|
|
whiteoutConverter := getWhiteoutConverter(options.WhiteoutFormat)
|
2013-12-20 06:08:34 -05:00
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
2014-07-31 03:33:59 -04:00
|
|
|
loop:
|
2013-12-20 06:08:34 -05:00
|
|
|
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
|
2015-06-01 19:42:27 -04:00
|
|
|
// This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
|
|
|
|
// This keeps "..\" as-is, but normalizes "\..\" to "\".
|
2013-12-20 06:08:34 -05:00
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
2014-10-23 17:30:11 -04:00
|
|
|
for _, exclude := range options.ExcludePatterns {
|
2014-07-31 03:33:59 -04:00
|
|
|
if strings.HasPrefix(hdr.Name, exclude) {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 19:42:27 -04:00
|
|
|
// After calling filepath.Clean(hdr.Name) above, hdr.Name will now be in
|
|
|
|
// the filepath format for the OS on which the daemon is running. Hence
|
|
|
|
// the check for a slash-suffix MUST be done in an OS-agnostic way.
|
|
|
|
if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
|
2013-12-20 06:08:34 -05:00
|
|
|
// 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) {
|
2016-02-26 21:50:50 -05:00
|
|
|
err = idtools.MkdirAllNewAs(parentPath, 0777, remappedRootUID, remappedRootGID)
|
2013-12-20 06:08:34 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 15:26:59 -04:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2014-11-27 02:00:13 -05:00
|
|
|
rel, err := filepath.Rel(dest, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
2014-11-27 02:00:13 -05:00
|
|
|
return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
|
2014-10-20 15:36:28 -04:00
|
|
|
}
|
2013-12-20 06:08:34 -05:00
|
|
|
|
|
|
|
// 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 {
|
2015-01-19 15:15:36 -05:00
|
|
|
if options.NoOverwriteDirNonDir && fi.IsDir() && hdr.Typeflag != tar.TypeDir {
|
|
|
|
// If NoOverwriteDirNonDir is true then we cannot replace
|
|
|
|
// an existing directory with a non-directory from the archive.
|
|
|
|
return fmt.Errorf("cannot overwrite directory %q with non-directory %q", path, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.NoOverwriteDirNonDir && !fi.IsDir() && hdr.Typeflag == tar.TypeDir {
|
|
|
|
// If NoOverwriteDirNonDir is true then we cannot replace
|
|
|
|
// an existing non-directory with a directory from the archive.
|
|
|
|
return fmt.Errorf("cannot overwrite non-directory %q with directory %q", path, dest)
|
|
|
|
}
|
|
|
|
|
2014-04-09 10:21:22 -04:00
|
|
|
if fi.IsDir() && hdr.Name == "." {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2013-12-20 06:08:34 -05:00
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 12:31:19 -04:00
|
|
|
trBuf.Reset(tr)
|
2015-01-19 15:15:36 -05:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
// if the options contain a uid & gid maps, convert header uid/gid
|
|
|
|
// entries using the maps such that lchown sets the proper mapped
|
|
|
|
// uid/gid after writing the file. We only perform this mapping if
|
|
|
|
// the file isn't already owned by the remapped root UID or GID, as
|
|
|
|
// that specific uid/gid has no mapping from container -> host, and
|
|
|
|
// those files already have the proper ownership for inside the
|
|
|
|
// container.
|
|
|
|
if hdr.Uid != remappedRootUID {
|
|
|
|
xUID, err := idtools.ToHost(hdr.Uid, options.UIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Uid = xUID
|
|
|
|
}
|
|
|
|
if hdr.Gid != remappedRootGID {
|
|
|
|
xGID, err := idtools.ToHost(hdr.Gid, options.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Gid = xGID
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:07:29 -04:00
|
|
|
if whiteoutConverter != nil {
|
|
|
|
writeFile, err := whiteoutConverter.ConvertRead(hdr, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-14 00:42:52 -05:00
|
|
|
}
|
2016-04-06 18:07:29 -04:00
|
|
|
if !writeFile {
|
2015-12-14 00:42:52 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:15:36 -05:00
|
|
|
if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts); err != nil {
|
2013-12-20 06:08:34 -05:00
|
|
|
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)
|
2015-10-01 13:45:32 -04:00
|
|
|
|
|
|
|
if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
|
2013-12-20 06:08:34 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-08 16:19:24 -05: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.
|
|
|
|
// FIXME: specify behavior when target path exists vs. doesn't exist.
|
2015-07-27 09:46:20 -04:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, true)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
2015-07-27 09:46:20 -04:00
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive must be an uncompressed stream.
|
|
|
|
func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for teasing out the automatic decompression
|
|
|
|
func untarHandler(tarArchive io.Reader, dest string, options *TarOptions, decompress bool) error {
|
|
|
|
if tarArchive == nil {
|
2014-12-08 16:19:24 -05:00
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
if options == nil {
|
|
|
|
options = &TarOptions{}
|
|
|
|
}
|
2014-10-23 17:30:11 -04:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
2015-07-27 09:46:20 -04:00
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
r := tarArchive
|
2015-07-27 09:46:20 -04:00
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
2015-07-27 09:46:20 -04:00
|
|
|
|
|
|
|
return Unpack(r, dest, options)
|
2014-12-08 16:19:24 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -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-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) TarUntar(src, dst string) error {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("TarUntar(%s %s)", src, dst)
|
2014-02-13 19:05:36 -05:00
|
|
|
archive, err := TarWithOptions(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()
|
2015-10-08 11:51:41 -04:00
|
|
|
|
|
|
|
var options *TarOptions
|
|
|
|
if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
|
|
|
|
options = &TarOptions{
|
|
|
|
UIDMaps: archiver.UIDMaps,
|
|
|
|
GIDMaps: archiver.GIDMaps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return archiver.Untar(archive, dst, options)
|
2013-06-18 23:28:49 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05: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, dst string) error {
|
|
|
|
return defaultArchiver.TarUntar(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// UntarPath untar a file from path to a destination, src is the source tar file path.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) 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()
|
2015-10-08 11:51:41 -04:00
|
|
|
var options *TarOptions
|
|
|
|
if archiver.UIDMaps != nil || archiver.GIDMaps != nil {
|
|
|
|
options = &TarOptions{
|
|
|
|
UIDMaps: archiver.UIDMaps,
|
|
|
|
GIDMaps: archiver.GIDMaps,
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 11:49:33 -05:00
|
|
|
return archiver.Untar(archive, dst, options)
|
2013-06-14 19:43:39 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05: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 {
|
|
|
|
return defaultArchiver.UntarPath(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// 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.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) 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() {
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.CopyFileWithTar(src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
}
|
2016-03-12 23:05:45 -05:00
|
|
|
|
|
|
|
// if this archiver is set up with ID mapping we need to create
|
|
|
|
// the new destination directory with the remapped root UID/GID pair
|
|
|
|
// as owner
|
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-20 23:20:16 -04:00
|
|
|
// Create dst, copy src's content into it
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Creating dest directory: %s", dst)
|
2016-03-12 23:05:45 -05:00
|
|
|
if err := idtools.MkdirAllNewAs(dst, 0755, rootUID, rootGID); err != nil {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
2014-11-06 13:01:37 -05:00
|
|
|
return archiver.TarUntar(src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
}
|
|
|
|
|
2014-11-06 13:01:37 -05:00
|
|
|
// 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 defaultArchiver.CopyWithTar(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -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.
|
2014-11-06 13:01:37 -05:00
|
|
|
func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst)
|
2013-06-20 23:20:16 -04:00
|
|
|
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
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
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
|
|
|
}
|
2015-06-01 19:42:27 -04:00
|
|
|
|
|
|
|
// Clean up the trailing slash. This must be done in an operating
|
|
|
|
// system specific manner.
|
2015-05-07 18:14:11 -04:00
|
|
|
if dst[len(dst)-1] == os.PathSeparator {
|
|
|
|
dst = filepath.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
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 19:49:05 -04:00
|
|
|
if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil {
|
2013-06-20 23:20:16 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-01-06 20:37:49 -05:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
2014-09-30 02:23:36 -04:00
|
|
|
errC := promise.Go(func() error {
|
2014-01-06 20:37:49 -05:00
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
srcF, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
|
|
|
|
|
|
|
hdr, err := tar.FileInfoHeader(srcSt, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Name = filepath.Base(dst)
|
2015-03-03 21:40:16 -05:00
|
|
|
hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
remappedRootUID, remappedRootGID, err := idtools.GetRootUIDGID(archiver.UIDMaps, archiver.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// only perform mapping if the file being copied isn't already owned by the
|
|
|
|
// uid or gid of the remapped root in the container
|
|
|
|
if remappedRootUID != hdr.Uid {
|
|
|
|
xUID, err := idtools.ToHost(hdr.Uid, archiver.UIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Uid = xUID
|
|
|
|
}
|
|
|
|
if remappedRootGID != hdr.Gid {
|
|
|
|
xGID, err := idtools.ToHost(hdr.Gid, archiver.GIDMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Gid = xGID
|
|
|
|
}
|
|
|
|
|
2014-07-14 13:11:48 -04:00
|
|
|
tw := tar.NewWriter(w)
|
|
|
|
defer tw.Close()
|
2014-01-06 20:37:49 -05:00
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tw, srcF); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer func() {
|
|
|
|
if er := <-errC; err != nil {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
}()
|
2015-10-08 11:51:41 -04:00
|
|
|
|
2015-10-14 19:25:03 -04:00
|
|
|
err = archiver.Untar(r, filepath.Dir(dst), nil)
|
|
|
|
if err != nil {
|
|
|
|
r.CloseWithError(err)
|
|
|
|
}
|
|
|
|
return err
|
2014-11-06 13:01:37 -05: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.
|
|
|
|
//
|
2015-06-01 19:42:27 -04:00
|
|
|
// Destination handling is in an operating specific manner depending
|
|
|
|
// where the daemon is running. If `dst` ends with a trailing slash
|
|
|
|
// the final destination path will be `dst/base(src)` (Linux) or
|
|
|
|
// `dst\base(src)` (Windows).
|
2014-11-06 13:01:37 -05:00
|
|
|
func CopyFileWithTar(src, dst string) (err error) {
|
|
|
|
return defaultArchiver.CopyFileWithTar(src, dst)
|
2013-06-14 19:43:39 -04:00
|
|
|
}
|
|
|
|
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
// cmdStream executes a command, and returns its stdout as a stream.
|
2013-03-29 16:18:59 -04:00
|
|
|
// If the command fails to run or doesn't complete successfully, an error
|
|
|
|
// will be returned, including anything written on stderr.
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, <-chan struct{}, error) {
|
|
|
|
chdone := make(chan struct{})
|
|
|
|
cmd.Stdin = input
|
2013-03-11 08:42:36 -04:00
|
|
|
pipeR, pipeW := io.Pipe()
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
cmd.Stdout = pipeW
|
|
|
|
var errBuf bytes.Buffer
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
|
|
|
|
// Run the command and return the pipe
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2013-03-29 16:26:02 -04:00
|
|
|
// Copy stdout to the returned pipe
|
2013-03-29 07:42:17 -04:00
|
|
|
go func() {
|
2013-03-11 08:42:36 -04:00
|
|
|
if err := cmd.Wait(); err != nil {
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
|
2013-03-11 08:42:36 -04:00
|
|
|
} else {
|
|
|
|
pipeW.Close()
|
|
|
|
}
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
close(chdone)
|
2013-03-11 08:42:36 -04:00
|
|
|
}()
|
fix a race crash when building with "ADD some-broken.tar.xz ..."
The race is between pools.Put which calls buf.Reset and exec.Cmd
doing io.Copy from the buffer; it caused a runtime crash, as
described in #16924:
``` docker-daemon cat the-tarball.xz | xz -d -c -q | docker-untar /path/to/... (aufs ) ```
When docker-untar side fails (like try to set xattr on aufs, or a broken
tar), invokeUnpack will be responsible to exhaust all input, otherwise
`xz` will be write pending for ever.
this change add a receive only channel to cmdStream, and will close it
to notify it's now safe to close the input stream;
in CmdStream the change to use Stdin / Stdout / Stderr keeps the
code simple, os/exec.Cmd will spawn goroutines and call io.Copy automatically.
the CmdStream is actually called in the same file only, change it
lowercase to mark as private.
[...]
INFO[0000] Docker daemon commit=0a8c2e3 execdriver=native-0.2 graphdriver=aufs version=1.8.2
DEBU[0006] Calling POST /build
INFO[0006] POST /v1.20/build?cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&memory=0&memswap=0&rm=1&t=gentoo-x32&ulimits=null
DEBU[0008] [BUILDER] Cache miss
DEBU[0009] Couldn't untar /home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz to /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537: Untar re-exec error: exit status 1: output: operation not supported
DEBU[0009] CopyFileWithTar(/home/lib-docker-v1.8.2-tmp/tmp/docker-build316710953/stage3-x32-20151004.tar.xz, /home/lib-docker-v1.8.2-tmp/aufs/mnt/d909abb87150463939c13e8a349b889a72d9b14f0cfcab42a8711979be285537/)
panic: runtime error: slice bounds out of range
goroutine 42 [running]:
bufio.(*Reader).fill(0xc208187800)
/usr/local/go/src/bufio/bufio.go:86 +0x2db
bufio.(*Reader).WriteTo(0xc208187800, 0x7ff39602d150, 0xc2083f11a0, 0x508000, 0x0, 0x0)
/usr/local/go/src/bufio/bufio.go:449 +0x27e
io.Copy(0x7ff39602d150, 0xc2083f11a0, 0x7ff3960261f8, 0xc208187800, 0x0, 0x0, 0x0)
/usr/local/go/src/io/io.go:354 +0xb2
github.com/docker/docker/pkg/archive.func·006()
/go/src/github.com/docker/docker/pkg/archive/archive.go:817 +0x71
created by github.com/docker/docker/pkg/archive.CmdStream
/go/src/github.com/docker/docker/pkg/archive/archive.go:819 +0x1ec
goroutine 1 [chan receive]:
main.(*DaemonCli).CmdDaemon(0xc20809da30, 0xc20800a020, 0xd, 0xd, 0x0, 0x0)
/go/src/github.com/docker/docker/docker/daemon.go:289 +0x1781
reflect.callMethod(0xc208140090, 0xc20828fce0)
/usr/local/go/src/reflect/value.go:605 +0x179
reflect.methodValueCall(0xc20800a020, 0xd, 0xd, 0x1, 0xc208140090, 0x0, 0x0, 0xc208140090, 0x0, 0x45343f, ...)
/usr/local/go/src/reflect/asm_amd64.s:29 +0x36
github.com/docker/docker/cli.(*Cli).Run(0xc208129fb0, 0xc20800a010, 0xe, 0xe, 0x0, 0x0)
/go/src/github.com/docker/docker/cli/cli.go:89 +0x38e
main.main()
/go/src/github.com/docker/docker/docker/docker.go:69 +0x428
goroutine 5 [syscall]:
os/signal.loop()
/usr/local/go/src/os/signal/signal_unix.go:21 +0x1f
created by os/signal.init·1
/usr/local/go/src/os/signal/signal_unix.go:27 +0x35
Signed-off-by: Derek Ch <denc716@gmail.com>
2015-10-09 15:54:21 -04:00
|
|
|
|
|
|
|
return pipeR, chdone, nil
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
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()
|
2014-12-03 15:36:57 -05:00
|
|
|
return &TempArchive{File: f, Size: size}, nil
|
2013-04-21 17:23:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 21:52:54 -04:00
|
|
|
// TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes,
|
|
|
|
// the file will be deleted.
|
2013-04-21 17:23:55 -04:00
|
|
|
type TempArchive struct {
|
|
|
|
*os.File
|
2014-12-03 15:36:57 -05:00
|
|
|
Size int64 // Pre-computed from Stat().Size() as a convenience
|
|
|
|
read int64
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying file if it's still open, or does a no-op
|
|
|
|
// to allow callers to try to close the TempArchive multiple times safely.
|
|
|
|
func (archive *TempArchive) Close() error {
|
|
|
|
if archive.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
archive.closed = true
|
|
|
|
|
|
|
|
return archive.File.Close()
|
2013-04-21 17:23:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (archive *TempArchive) Read(data []byte) (int, error) {
|
|
|
|
n, err := archive.File.Read(data)
|
2014-11-20 12:34:35 -05:00
|
|
|
archive.read += int64(n)
|
|
|
|
if err != nil || archive.read == archive.Size {
|
2014-12-03 15:36:57 -05:00
|
|
|
archive.Close()
|
2013-04-21 17:23:55 -04:00
|
|
|
os.Remove(archive.File.Name())
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|