2018-02-05 21:05:59 +00:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2013-03-11 12:42:36 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-01 22:01:10 +00:00
|
|
|
"archive/tar"
|
2014-05-09 22:49:02 +00:00
|
|
|
"bufio"
|
2014-05-16 23:26:28 +00:00
|
|
|
"bytes"
|
2013-12-17 13:19:48 +00:00
|
|
|
"compress/bzip2"
|
2014-01-07 01:37:49 +00:00
|
|
|
"compress/gzip"
|
2018-01-16 18:49:18 +00:00
|
|
|
"context"
|
2013-05-28 20:37:49 +00:00
|
|
|
"fmt"
|
2013-03-11 12:42:36 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-04-21 21:23:55 +00:00
|
|
|
"os"
|
2013-03-11 12:42:36 +00:00
|
|
|
"os/exec"
|
2013-06-21 03:20:16 +00:00
|
|
|
"path/filepath"
|
2015-05-07 22:14:11 +00:00
|
|
|
"runtime"
|
2018-01-16 18:49:18 +00:00
|
|
|
"strconv"
|
2013-12-20 11:08:34 +00:00
|
|
|
"strings"
|
2013-12-20 10:30:45 +00:00
|
|
|
"syscall"
|
2018-04-02 17:48:34 +00:00
|
|
|
"time"
|
2014-05-09 22:49:02 +00:00
|
|
|
|
2014-10-24 22:11:48 +00:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2015-10-08 15:51:41 +00: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 19:54:21 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-08-12 16:29:46 +00:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-03-11 12:42:36 +00:00
|
|
|
)
|
|
|
|
|
2018-01-16 18:49:18 +00:00
|
|
|
var unpigzPath string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if path, err := exec.LookPath("unpigz"); err != nil {
|
|
|
|
logrus.Debug("unpigz binary not found in PATH, falling back to go gzip library")
|
|
|
|
} else {
|
|
|
|
logrus.Debugf("Using unpigz binary found at path %s", path)
|
|
|
|
unpigzPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 22:13:03 +00:00
|
|
|
type (
|
2015-12-13 16:00:39 +00:00
|
|
|
// Compression is the state represents if compressed or not.
|
2015-08-04 01:52:54 +00:00
|
|
|
Compression int
|
2016-04-06 22:07:29 +00:00
|
|
|
// WhiteoutFormat is the format of whiteouts unpacked
|
|
|
|
WhiteoutFormat int
|
2016-10-20 23:40:59 +00:00
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// TarOptions wraps the tar options.
|
2015-01-19 20:15:36 +00:00
|
|
|
TarOptions struct {
|
|
|
|
IncludeFiles []string
|
|
|
|
ExcludePatterns []string
|
|
|
|
Compression Compression
|
|
|
|
NoLchown bool
|
2015-10-08 15:51:41 +00:00
|
|
|
UIDMaps []idtools.IDMap
|
|
|
|
GIDMaps []idtools.IDMap
|
2017-11-16 06:20:33 +00:00
|
|
|
ChownOpts *idtools.Identity
|
2015-01-19 20:15:36 +00:00
|
|
|
IncludeSourceDir bool
|
2016-04-06 22:07:29 +00: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 20:15:36 +00:00
|
|
|
// When unpacking, specifies whether overwriting a directory with a
|
|
|
|
// non-directory is allowed and vice versa.
|
|
|
|
NoOverwriteDirNonDir bool
|
2015-07-24 21:12:55 +00: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
|
2016-02-13 00:05:50 +00:00
|
|
|
InUserNS bool
|
2014-02-06 22:13:03 +00:00
|
|
|
}
|
|
|
|
)
|
2013-11-11 22:30:38 +00:00
|
|
|
|
2017-08-04 00:22:00 +00:00
|
|
|
// Archiver implements the Archiver interface and allows the reuse of most utility functions of
|
|
|
|
// this package 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.
|
2017-05-24 15:53:41 +00:00
|
|
|
type Archiver struct {
|
2017-11-16 06:20:33 +00:00
|
|
|
Untar func(io.Reader, string, *TarOptions) error
|
|
|
|
IDMapping *idtools.IdentityMapping
|
2017-05-24 15:53:41 +00:00
|
|
|
}
|
2013-03-11 12:42:36 +00:00
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
// NewDefaultArchiver returns a new Archiver without any IdentityMapping
|
2017-05-24 15:53:41 +00:00
|
|
|
func NewDefaultArchiver() *Archiver {
|
2017-11-16 06:20:33 +00:00
|
|
|
return &Archiver{Untar: Untar, IDMapping: &idtools.IdentityMapping{}}
|
2017-05-24 15:53:41 +00: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.
|
|
|
|
type breakoutError error
|
2015-11-20 16:49:33 +00:00
|
|
|
|
2013-03-11 12:42:36 +00:00
|
|
|
const (
|
2015-08-04 01:52:54 +00:00
|
|
|
// Uncompressed represents the uncompressed.
|
2013-03-11 12:42:36 +00:00
|
|
|
Uncompressed Compression = iota
|
2015-08-04 01:52:54 +00:00
|
|
|
// Bzip2 is bzip2 compression algorithm.
|
2013-03-11 12:42:36 +00:00
|
|
|
Bzip2
|
2015-08-04 01:52:54 +00:00
|
|
|
// Gzip is gzip compression algorithm.
|
2013-03-11 12:42:36 +00:00
|
|
|
Gzip
|
2015-08-04 01:52:54 +00:00
|
|
|
// Xz is xz compression algorithm.
|
2013-04-01 23:16:28 +00:00
|
|
|
Xz
|
2013-03-11 12:42:36 +00:00
|
|
|
)
|
|
|
|
|
2016-04-06 22:07:29 +00:00
|
|
|
const (
|
2016-06-21 07:28:30 +00:00
|
|
|
// AUFSWhiteoutFormat is the default format for whiteouts
|
2016-04-06 22:07:29 +00:00
|
|
|
AUFSWhiteoutFormat WhiteoutFormat = iota
|
|
|
|
// OverlayWhiteoutFormat formats whiteout according to the overlay
|
|
|
|
// standard.
|
|
|
|
OverlayWhiteoutFormat
|
|
|
|
)
|
|
|
|
|
2017-07-04 06:34:15 +00:00
|
|
|
const (
|
|
|
|
modeISDIR = 040000 // Directory
|
|
|
|
modeISFIFO = 010000 // FIFO
|
|
|
|
modeISREG = 0100000 // Regular file
|
|
|
|
modeISLNK = 0120000 // Symbolic link
|
|
|
|
modeISBLK = 060000 // Block special file
|
|
|
|
modeISCHR = 020000 // Character special file
|
|
|
|
modeISSOCK = 0140000 // Socket
|
|
|
|
)
|
|
|
|
|
2015-11-20 16:49:33 +00: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
|
|
|
|
}
|
2017-07-22 00:47:15 +00:00
|
|
|
defer rdr.Close()
|
2015-11-20 16:49:33 +00:00
|
|
|
r := tar.NewReader(rdr)
|
|
|
|
_, err = r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// DetectCompression detects the compression algorithm of the source.
|
2013-06-14 00:53:38 +00: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 19:47:50 +00:00
|
|
|
if len(source) < len(m) {
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("Len too short")
|
2014-05-16 19:47:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-03-30 09:04:42 +00:00
|
|
|
if bytes.Equal(m, source[:len(m)]) {
|
2013-06-14 00:53:38 +00:00
|
|
|
return compression
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Uncompressed
|
|
|
|
}
|
|
|
|
|
2018-01-16 18:49:18 +00:00
|
|
|
func xzDecompress(ctx context.Context, archive io.Reader) (io.ReadCloser, error) {
|
2013-12-18 09:50:22 +00:00
|
|
|
args := []string{"xz", "-d", "-c", "-q"}
|
|
|
|
|
2018-01-16 18:49:18 +00:00
|
|
|
return cmdStream(exec.CommandContext(ctx, args[0], args[1:]...), archive)
|
|
|
|
}
|
|
|
|
|
|
|
|
func gzDecompress(ctx context.Context, buf io.Reader) (io.ReadCloser, error) {
|
|
|
|
if unpigzPath == "" {
|
|
|
|
return gzip.NewReader(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
disablePigzEnv := os.Getenv("MOBY_DISABLE_PIGZ")
|
|
|
|
if disablePigzEnv != "" {
|
|
|
|
if disablePigz, err := strconv.ParseBool(disablePigzEnv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if disablePigz {
|
|
|
|
return gzip.NewReader(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdStream(exec.CommandContext(ctx, unpigzPath, "-d", "-c"), buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapReadCloser(readBuf io.ReadCloser, cancel context.CancelFunc) io.ReadCloser {
|
|
|
|
return ioutils.NewReadCloserWrapper(readBuf, func() error {
|
|
|
|
cancel()
|
|
|
|
return readBuf.Close()
|
|
|
|
})
|
2013-12-18 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 15:12:38 +00:00
|
|
|
// DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
|
2014-02-14 10:12:49 +00:00
|
|
|
func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
|
2014-08-12 16:29:46 +00:00
|
|
|
p := pools.BufioReader32KPool
|
|
|
|
buf := p.Get(archive)
|
2014-05-09 22:49:02 +00:00
|
|
|
bs, err := buf.Peek(10)
|
2015-11-24 01:20:44 +00: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 22:49:02 +00:00
|
|
|
return nil, err
|
2013-12-17 13:19:48 +00:00
|
|
|
}
|
2014-05-09 22:49:02 +00:00
|
|
|
|
|
|
|
compression := DetectCompression(bs)
|
2013-12-17 13:19:48 +00:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 16:29:46 +00:00
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, buf)
|
|
|
|
return readBufWrapper, nil
|
2013-12-17 13:19:48 +00:00
|
|
|
case Gzip:
|
2018-01-16 18:49:18 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
gzReader, err := gzDecompress(ctx, buf)
|
2014-08-12 16:29:46 +00:00
|
|
|
if err != nil {
|
2018-01-16 18:49:18 +00:00
|
|
|
cancel()
|
2014-08-12 16:29:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, gzReader)
|
2018-01-16 18:49:18 +00:00
|
|
|
return wrapReadCloser(readBufWrapper, cancel), nil
|
2013-12-17 13:19:48 +00:00
|
|
|
case Bzip2:
|
2014-08-12 16:29:46 +00:00
|
|
|
bz2Reader := bzip2.NewReader(buf)
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, bz2Reader)
|
|
|
|
return readBufWrapper, nil
|
2013-12-18 09:50:22 +00:00
|
|
|
case Xz:
|
2018-01-16 18:49:18 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
xzReader, err := xzDecompress(ctx, buf)
|
2014-08-12 16:29:46 +00:00
|
|
|
if err != nil {
|
2018-01-16 18:49:18 +00:00
|
|
|
cancel()
|
2014-08-12 16:29:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readBufWrapper := p.NewReadCloserWrapper(buf, xzReader)
|
2018-01-16 18:49:18 +00:00
|
|
|
return wrapReadCloser(readBufWrapper, cancel), nil
|
2013-12-17 13:19:48 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 23:24:07 +00:00
|
|
|
// CompressStream compresses the dest with specified compression algorithm.
|
2016-04-07 20:21:05 +00:00
|
|
|
func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error) {
|
2014-08-12 16:29:46 +00:00
|
|
|
p := pools.BufioWriter32KPool
|
|
|
|
buf := p.Get(dest)
|
2014-01-20 11:07:34 +00:00
|
|
|
switch compression {
|
|
|
|
case Uncompressed:
|
2014-08-12 16:29:46 +00:00
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, buf)
|
|
|
|
return writeBufWrapper, nil
|
2013-03-11 12:42:36 +00:00
|
|
|
case Gzip:
|
2014-08-12 16:29:46 +00:00
|
|
|
gzWriter := gzip.NewWriter(dest)
|
|
|
|
writeBufWrapper := p.NewWriteCloserWrapper(buf, gzWriter)
|
|
|
|
return writeBufWrapper, nil
|
2014-01-20 11:07:34 +00: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 12:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 20:07:45 +00:00
|
|
|
// TarModifierFunc is a function that can be passed to ReplaceFileTarWrapper to
|
2017-04-05 22:25:29 +00:00
|
|
|
// modify the contents or header of an entry in the archive. If the file already
|
|
|
|
// exists in the archive the TarModifierFunc will be called with the Header and
|
|
|
|
// a reader which will return the files content. If the file does not exist both
|
|
|
|
// header and content will be nil.
|
2017-02-21 20:07:45 +00:00
|
|
|
type TarModifierFunc func(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error)
|
|
|
|
|
2017-04-05 16:09:26 +00:00
|
|
|
// ReplaceFileTarWrapper converts inputTarStream to a new tar stream. Files in the
|
|
|
|
// tar stream are modified if they match any of the keys in mods.
|
2017-02-21 20:07:45 +00:00
|
|
|
func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModifierFunc) io.ReadCloser {
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
tarReader := tar.NewReader(inputTarStream)
|
|
|
|
tarWriter := tar.NewWriter(pipeWriter)
|
|
|
|
defer inputTarStream.Close()
|
2017-04-05 22:25:29 +00:00
|
|
|
defer tarWriter.Close()
|
|
|
|
|
|
|
|
modify := func(name string, original *tar.Header, modifier TarModifierFunc, tarReader io.Reader) error {
|
|
|
|
header, data, err := modifier(name, original, tarReader)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case header == nil:
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-21 20:07:45 +00:00
|
|
|
|
2017-04-05 22:25:29 +00:00
|
|
|
header.Name = name
|
|
|
|
header.Size = int64(len(data))
|
|
|
|
if err := tarWriter.WriteHeader(header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(data) != 0 {
|
|
|
|
if _, err := tarWriter.Write(data); err != nil {
|
|
|
|
return err
|
2017-02-21 20:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-05 22:25:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-21 20:07:45 +00:00
|
|
|
|
2017-04-05 22:25:29 +00:00
|
|
|
var err error
|
|
|
|
var originalHeader *tar.Header
|
|
|
|
for {
|
|
|
|
originalHeader, err = tarReader.Next()
|
2017-02-21 20:07:45 +00:00
|
|
|
if err == io.EOF {
|
2017-04-05 22:25:29 +00:00
|
|
|
break
|
2017-02-21 20:07:45 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-05 22:25:29 +00:00
|
|
|
modifier, ok := mods[originalHeader.Name]
|
|
|
|
if !ok {
|
|
|
|
// No modifiers for this file, copy the header and data
|
|
|
|
if err := tarWriter.WriteHeader(originalHeader); err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := pools.Copy(tarWriter, tarReader); err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(mods, originalHeader.Name)
|
|
|
|
|
|
|
|
if err := modify(originalHeader.Name, originalHeader, modifier, tarReader); err != nil {
|
2017-02-21 20:07:45 +00:00
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2017-04-05 22:25:29 +00:00
|
|
|
}
|
2017-02-21 20:07:45 +00:00
|
|
|
|
2017-04-05 22:25:29 +00:00
|
|
|
// Apply the modifiers that haven't matched any files in the archive
|
|
|
|
for name, modifier := range mods {
|
|
|
|
if err := modify(name, nil, modifier, nil); err != nil {
|
2017-02-21 20:07:45 +00:00
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 22:25:29 +00:00
|
|
|
|
|
|
|
pipeWriter.Close()
|
|
|
|
|
2017-02-21 20:07:45 +00:00
|
|
|
}()
|
|
|
|
return pipeReader
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// Extension returns the extension of a file that uses the specified compression algorithm.
|
2013-05-28 20:37:49 +00: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 ""
|
|
|
|
}
|
|
|
|
|
2017-03-15 22:28:06 +00:00
|
|
|
// FileInfoHeader creates a populated Header from fi.
|
|
|
|
// Compared to archive pkg this function fills in more information.
|
2017-07-04 06:34:15 +00:00
|
|
|
// Also, regardless of Go version, this function fills file type bits (e.g. hdr.Mode |= modeISDIR),
|
|
|
|
// which have been deleted since Go 1.9 archive/tar.
|
2017-05-15 21:54:27 +00:00
|
|
|
func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, error) {
|
2017-03-15 22:28:06 +00:00
|
|
|
hdr, err := tar.FileInfoHeader(fi, link)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-02 17:48:34 +00:00
|
|
|
hdr.Format = tar.FormatPAX
|
|
|
|
hdr.ModTime = hdr.ModTime.Truncate(time.Second)
|
|
|
|
hdr.AccessTime = time.Time{}
|
|
|
|
hdr.ChangeTime = time.Time{}
|
2017-07-04 06:34:15 +00:00
|
|
|
hdr.Mode = fillGo18FileTypeBits(int64(chmodTarEntry(os.FileMode(hdr.Mode))), fi)
|
2018-06-27 19:07:17 +00:00
|
|
|
hdr.Name = canonicalTarName(name, fi.IsDir())
|
2017-03-15 22:28:06 +00:00
|
|
|
if err := setHeaderForSpecialDevice(hdr, name, fi.Sys()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-15 21:54:27 +00:00
|
|
|
return hdr, nil
|
|
|
|
}
|
|
|
|
|
2017-07-04 06:34:15 +00:00
|
|
|
// fillGo18FileTypeBits fills type bits which have been removed on Go 1.9 archive/tar
|
|
|
|
// https://github.com/golang/go/commit/66b5a2f
|
|
|
|
func fillGo18FileTypeBits(mode int64, fi os.FileInfo) int64 {
|
|
|
|
fm := fi.Mode()
|
|
|
|
switch {
|
|
|
|
case fm.IsRegular():
|
|
|
|
mode |= modeISREG
|
|
|
|
case fi.IsDir():
|
|
|
|
mode |= modeISDIR
|
|
|
|
case fm&os.ModeSymlink != 0:
|
|
|
|
mode |= modeISLNK
|
|
|
|
case fm&os.ModeDevice != 0:
|
|
|
|
if fm&os.ModeCharDevice != 0 {
|
|
|
|
mode |= modeISCHR
|
|
|
|
} else {
|
|
|
|
mode |= modeISBLK
|
|
|
|
}
|
|
|
|
case fm&os.ModeNamedPipe != 0:
|
|
|
|
mode |= modeISFIFO
|
|
|
|
case fm&os.ModeSocket != 0:
|
|
|
|
mode |= modeISSOCK
|
|
|
|
}
|
|
|
|
return mode
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
// ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem
|
|
|
|
// to a tar header
|
|
|
|
func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error {
|
2017-03-15 22:28:06 +00:00
|
|
|
capability, _ := system.Lgetxattr(path, "security.capability")
|
|
|
|
if capability != nil {
|
|
|
|
hdr.Xattrs = make(map[string]string)
|
|
|
|
hdr.Xattrs["security.capability"] = string(capability)
|
|
|
|
}
|
2017-05-15 21:54:27 +00:00
|
|
|
return nil
|
2017-03-15 22:28:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 22:07:29 +00:00
|
|
|
type tarWhiteoutConverter interface {
|
2016-10-25 00:03:56 +00:00
|
|
|
ConvertWrite(*tar.Header, string, os.FileInfo) (*tar.Header, error)
|
2016-04-06 22:07:29 +00:00
|
|
|
ConvertRead(*tar.Header, string) (bool, error)
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
type tarAppender struct {
|
|
|
|
TarWriter *tar.Writer
|
|
|
|
Buffer *bufio.Writer
|
|
|
|
|
|
|
|
// for hardlink mapping
|
2017-11-16 06:20:33 +00:00
|
|
|
SeenFiles map[uint64]string
|
|
|
|
IdentityMapping *idtools.IdentityMapping
|
|
|
|
ChownOpts *idtools.Identity
|
2015-12-14 05:42:52 +00:00
|
|
|
|
2016-04-06 22:07:29 +00: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 18:45:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
func newTarAppender(idMapping *idtools.IdentityMapping, writer io.Writer, chownOpts *idtools.Identity) *tarAppender {
|
2017-05-24 18:10:15 +00:00
|
|
|
return &tarAppender{
|
2017-11-16 06:20:33 +00:00
|
|
|
SeenFiles: make(map[uint64]string),
|
|
|
|
TarWriter: tar.NewWriter(writer),
|
|
|
|
Buffer: pools.BufioWriter32KPool.Get(nil),
|
|
|
|
IdentityMapping: idMapping,
|
|
|
|
ChownOpts: chownOpts,
|
2017-05-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 20:27:07 +00:00
|
|
|
// canonicalTarName provides a platform-independent and consistent posix-style
|
|
|
|
//path for files and directories to be archived regardless of the platform.
|
2018-06-27 19:07:17 +00:00
|
|
|
func canonicalTarName(name string, isDir bool) string {
|
|
|
|
name = CanonicalTarNameForPath(name)
|
2015-02-17 20:27:07 +00:00
|
|
|
|
|
|
|
// suffix with '/' for directories
|
|
|
|
if isDir && !strings.HasSuffix(name, "/") {
|
|
|
|
name += "/"
|
|
|
|
}
|
2018-06-27 19:07:17 +00:00
|
|
|
return name
|
2015-02-17 20:27:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 05:42:52 +00:00
|
|
|
// addTarFile adds to the tar archive a file from `path` as `name`
|
2014-09-15 18:45:53 +00:00
|
|
|
func (ta *tarAppender) addTarFile(path, name string) error {
|
2014-01-20 10:48:47 +00:00
|
|
|
fi, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
2014-01-17 10:21:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
var link string
|
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
|
|
|
var err error
|
|
|
|
link, err = os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr, err := FileInfoHeader(name, fi, link)
|
2014-11-13 20:36:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-20 11:26:08 +00:00
|
|
|
}
|
2017-05-15 21:54:27 +00:00
|
|
|
if err := ReadSecurityXattrToTarHeader(path, hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-13 20:36:05 +00:00
|
|
|
|
2015-10-12 19:11:22 +00:00
|
|
|
// if it's not a directory and has more than 1 link,
|
2016-10-29 07:03:26 +00:00
|
|
|
// it's hard linked, so set the type flag accordingly
|
2015-10-12 19:11:22 +00:00
|
|
|
if !fi.IsDir() && hasHardlinks(fi) {
|
2017-03-15 22:28:06 +00:00
|
|
|
inode, err := getInodeFromStat(fi.Sys())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-15 18:45:53 +00: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 17:42:57 +00:00
|
|
|
if oldpath, ok := ta.SeenFiles[inode]; ok {
|
2014-09-15 18:45:53 +00: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 17:42:57 +00:00
|
|
|
ta.SeenFiles[inode] = name
|
2014-09-15 18:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 08:52:37 +00:00
|
|
|
//check whether the file is overlayfs whiteout
|
|
|
|
//if yes, skip re-mapping container ID mappings.
|
|
|
|
isOverlayWhiteout := fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0
|
|
|
|
|
2015-10-08 15:51:41 +00:00
|
|
|
//handle re-mapping container ID mappings back to host ID mappings before
|
2015-12-09 15:11:32 +00: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
|
2017-11-16 06:20:33 +00:00
|
|
|
if !isOverlayWhiteout && !strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) && !ta.IdentityMapping.Empty() {
|
2017-05-31 21:18:04 +00:00
|
|
|
fileIDPair, err := getFileUIDGID(fi.Sys())
|
2015-10-08 15:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-16 06:20:33 +00:00
|
|
|
hdr.Uid, hdr.Gid, err = ta.IdentityMapping.ToContainer(fileIDPair)
|
2015-10-08 15:51:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 00:17:46 +00:00
|
|
|
// explicitly override with ChownOpts
|
|
|
|
if ta.ChownOpts != nil {
|
|
|
|
hdr.Uid = ta.ChownOpts.UID
|
|
|
|
hdr.Gid = ta.ChownOpts.GID
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:07:29 +00:00
|
|
|
if ta.WhiteoutConverter != nil {
|
2016-10-25 00:03:56 +00:00
|
|
|
wo, err := ta.WhiteoutConverter.ConvertWrite(hdr, path, fi)
|
|
|
|
if err != nil {
|
2016-04-06 22:07:29 +00:00
|
|
|
return err
|
2015-12-14 05:42:52 +00:00
|
|
|
}
|
2016-10-25 00:03:56 +00:00
|
|
|
|
|
|
|
// If a new whiteout file exists, write original hdr, then
|
|
|
|
// replace hdr with wo to be written after. Whiteouts should
|
|
|
|
// always be written after the original. Note the original
|
|
|
|
// hdr may have been updated to be a whiteout with returning
|
|
|
|
// a whiteout header
|
|
|
|
if wo != nil {
|
|
|
|
if err := ta.TarWriter.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
|
|
|
|
return fmt.Errorf("tar: cannot use whiteout for non-empty file")
|
|
|
|
}
|
|
|
|
hdr = wo
|
|
|
|
}
|
2015-12-14 05:42:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
if err := ta.TarWriter.WriteHeader(hdr); err != nil {
|
2014-01-17 10:21:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:34:38 +00:00
|
|
|
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
|
2016-11-01 22:44:06 +00:00
|
|
|
// We use system.OpenSequential to ensure we use sequential file
|
|
|
|
// access on Windows to avoid depleting the standby list.
|
|
|
|
// On Linux, this equates to a regular os.Open.
|
|
|
|
file, err := system.OpenSequential(path)
|
2014-07-14 17:11:48 +00:00
|
|
|
if err != nil {
|
2014-01-17 10:21:36 +00:00
|
|
|
return err
|
2014-07-14 17:11:48 +00:00
|
|
|
}
|
2014-07-14 16:31:19 +00:00
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
ta.Buffer.Reset(ta.TarWriter)
|
2014-10-28 21:01:10 +00:00
|
|
|
defer ta.Buffer.Reset(nil)
|
2014-09-15 18:45:53 +00:00
|
|
|
_, err = io.Copy(ta.Buffer, file)
|
2014-07-14 16:31:19 +00:00
|
|
|
file.Close()
|
|
|
|
if err != nil {
|
2014-07-14 17:11:48 +00:00
|
|
|
return err
|
2014-01-17 10:21:36 +00:00
|
|
|
}
|
2014-09-15 18:45:53 +00:00
|
|
|
err = ta.Buffer.Flush()
|
2014-07-14 16:31:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-17 10:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, Lchown bool, chownOpts *idtools.Identity, inUserns bool) error {
|
2014-02-18 08:53:30 +00: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 10:30:45 +00: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 08:53:30 +00:00
|
|
|
if err := os.Mkdir(path, hdrInfo.Mode()); err != nil {
|
2013-12-20 10:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeReg, tar.TypeRegA:
|
2016-11-01 22:44:06 +00:00
|
|
|
// Source is regular file. We use system.OpenFileSequential to use sequential
|
|
|
|
// file access to avoid depleting the standby list on Windows.
|
|
|
|
// On Linux, this equates to a regular os.OpenFile
|
|
|
|
file, err := system.OpenFileSequential(path, os.O_CREATE|os.O_WRONLY, hdrInfo.Mode())
|
2013-12-20 10:30:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(file, reader); err != nil {
|
|
|
|
file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
2016-02-13 00:05:50 +00:00
|
|
|
case tar.TypeBlock, tar.TypeChar:
|
|
|
|
if inUserns { // cannot create devices in a userns
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Handle this is an OS-specific way
|
|
|
|
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeFifo:
|
2015-05-07 22:14:11 +00:00
|
|
|
// Handle this is an OS-specific way
|
|
|
|
if err := handleTarTypeBlockCharFifo(hdr, path); err != nil {
|
2013-12-20 10:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeLink:
|
2014-10-20 19:36:28 +00: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 10:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case tar.TypeSymlink:
|
2014-11-19 16:27:34 +00: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 19:36:28 +00:00
|
|
|
}
|
2013-12-20 10:30:45 +00:00
|
|
|
if err := os.Symlink(hdr.Linkname, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-31 13:50:07 +00:00
|
|
|
case tar.TypeXGlobalHeader:
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("PAX Global Extended Headers found and ignored")
|
2014-01-31 13:50:07 +00:00
|
|
|
return nil
|
|
|
|
|
2013-12-20 10:30:45 +00:00
|
|
|
default:
|
2017-08-17 19:16:30 +00:00
|
|
|
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
|
2013-12-20 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 20:15:36 +00:00
|
|
|
// Lchown is not supported on Windows.
|
|
|
|
if Lchown && runtime.GOOS != "windows" {
|
|
|
|
if chownOpts == nil {
|
2017-11-16 06:20:33 +00:00
|
|
|
chownOpts = &idtools.Identity{UID: hdr.Uid, GID: hdr.Gid}
|
2015-01-19 20:15:36 +00:00
|
|
|
}
|
|
|
|
if err := os.Lchown(path, chownOpts.UID, chownOpts.GID); err != nil {
|
2015-05-07 22:14:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-12-20 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 11:54:57 +00:00
|
|
|
var errors []string
|
2014-01-15 14:05:30 +00:00
|
|
|
for key, value := range hdr.Xattrs {
|
2014-02-21 09:12:25 +00:00
|
|
|
if err := system.Lsetxattr(path, key, []byte(value), 0); err != nil {
|
2018-10-04 05:19:09 +00:00
|
|
|
if err == syscall.ENOTSUP || err == syscall.EPERM {
|
2016-06-09 14:24:04 +00:00
|
|
|
// 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.
|
2018-10-04 05:19:09 +00:00
|
|
|
// EPERM occurs if modifying xattrs is not allowed. This can
|
|
|
|
// happen when running in userns with restrictions (ChromeOS).
|
2016-06-09 14:24:04 +00:00
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
2014-01-15 14:05:30 +00:00
|
|
|
}
|
2016-03-16 11:54:57 +00: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 14:05:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 10:30:45 +00: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 22:14:11 +00:00
|
|
|
if err := handleLChmod(hdr, path, hdrInfo); err != nil {
|
|
|
|
return err
|
2013-12-20 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 19:47:18 +00: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 17:45:32 +00:00
|
|
|
// system.Chtimes doesn't support a NOFOLLOW flag atm
|
2015-03-01 23:55:28 +00:00
|
|
|
if hdr.Typeflag == tar.TypeLink {
|
|
|
|
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
|
2015-11-05 19:47:18 +00:00
|
|
|
if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
|
2015-03-01 23:55:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if hdr.Typeflag != tar.TypeSymlink {
|
2015-11-05 19:47:18 +00:00
|
|
|
if err := system.Chtimes(path, aTime, hdr.ModTime); err != nil {
|
2013-12-20 10:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-05 19:47:18 +00:00
|
|
|
ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(hdr.ModTime)}
|
2014-06-18 23:42:22 +00:00
|
|
|
if err := system.LUtimesNano(path, ts); err != nil && err != system.ErrNotSupportedPlatform {
|
2013-12-20 10:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-19 03:28:49 +00:00
|
|
|
// Tar creates an archive from the directory at `path`, and returns it as a
|
|
|
|
// stream of bytes.
|
2014-02-14 11:41:46 +00:00
|
|
|
func Tar(path string, compression Compression) (io.ReadCloser, error) {
|
2014-02-14 00:05:36 +00:00
|
|
|
return TarWithOptions(path, &TarOptions{Compression: compression})
|
2013-09-05 20:10:29 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 00:05:36 +00:00
|
|
|
// TarWithOptions creates an archive from the directory at `path`, only including files whose relative
|
2014-10-23 21:30:11 +00:00
|
|
|
// paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
|
2014-02-14 00:05:36 +00:00
|
|
|
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
2015-04-09 19:07:06 +00:00
|
|
|
|
2015-08-10 22:21:30 +00:00
|
|
|
// Fix the source path to work with long path names. This is a no-op
|
|
|
|
// on platforms other than Windows.
|
|
|
|
srcPath = fixVolumePathPrefix(srcPath)
|
|
|
|
|
2017-03-08 22:23:25 +00:00
|
|
|
pm, err := fileutils.NewPatternMatcher(options.ExcludePatterns)
|
2015-04-09 19:07:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:07:34 +00:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
2013-11-11 22:30:38 +00:00
|
|
|
|
2014-01-20 11:07:34 +00:00
|
|
|
compressWriter, err := CompressStream(pipeWriter, options.Compression)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-06-19 03:28:49 +00:00
|
|
|
}
|
2013-09-05 20:10:29 +00:00
|
|
|
|
2014-01-20 11:07:34 +00:00
|
|
|
go func() {
|
2017-05-24 18:10:15 +00:00
|
|
|
ta := newTarAppender(
|
|
|
|
idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps),
|
|
|
|
compressWriter,
|
2017-08-22 00:17:46 +00:00
|
|
|
options.ChownOpts,
|
2017-05-24 18:10:15 +00:00
|
|
|
)
|
2018-10-15 07:47:02 +00:00
|
|
|
ta.WhiteoutConverter = getWhiteoutConverter(options.WhiteoutFormat, options.InUserNS)
|
2015-01-19 20:15:36 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Make sure to check the error on Close.
|
|
|
|
if err := ta.TarWriter.Close(); err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Can't close tar writer: %s", err)
|
2015-01-19 20:15:36 +00:00
|
|
|
}
|
|
|
|
if err := compressWriter.Close(); err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Can't close compress writer: %s", err)
|
2015-01-19 20:15:36 +00:00
|
|
|
}
|
|
|
|
if err := pipeWriter.Close(); err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Can't close pipe writer: %s", err)
|
2015-01-19 20:15:36 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
// this buffer is needed for the duration of this piped stream
|
|
|
|
defer pools.BufioWriter32KPool.Put(ta.Buffer)
|
|
|
|
|
2014-01-20 11:07:34 +00: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 20:15:36 +00: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 21:30:11 +00:00
|
|
|
options.IncludeFiles = []string{"."}
|
2014-01-20 11:07:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 21:30:11 +00:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, include := range options.IncludeFiles {
|
2015-07-24 21:12:55 +00:00
|
|
|
rebaseName := options.RebaseNames[include]
|
|
|
|
|
2015-08-10 22:21:30 +00:00
|
|
|
walkRoot := getWalkRoot(srcPath, include)
|
2015-01-19 20:15:36 +00:00
|
|
|
filepath.Walk(walkRoot, func(filePath string, f os.FileInfo, err error) error {
|
2014-01-20 11:07:34 +00:00
|
|
|
if err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Tar: Can't stat file %s to tar: %s", srcPath, err)
|
2014-01-20 11:07:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
relFilePath, err := filepath.Rel(srcPath, filePath)
|
2015-01-19 20:15:36 +00:00
|
|
|
if err != nil || (!options.IncludeSourceDir && relFilePath == "." && f.IsDir()) {
|
2014-10-28 00:23:50 +00:00
|
|
|
// Error getting relative path OR we are looking
|
2015-01-19 20:15:36 +00:00
|
|
|
// at the source directory path. Skip in both situations.
|
2014-01-20 11:07:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-19 20:15:36 +00:00
|
|
|
if options.IncludeSourceDir && include == "." && relFilePath != "." {
|
|
|
|
relFilePath = strings.Join([]string{".", relFilePath}, string(filepath.Separator))
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:30:11 +00: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 {
|
2017-03-08 22:23:25 +00:00
|
|
|
skip, err = pm.Matches(relFilePath)
|
2014-10-23 21:30:11 +00:00
|
|
|
if err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Error matching %s: %v", relFilePath, err)
|
2014-10-23 21:30:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-07-07 12:23:07 +00:00
|
|
|
}
|
2014-07-31 22:11:15 +00:00
|
|
|
|
2014-07-07 12:23:07 +00:00
|
|
|
if skip {
|
2016-02-19 19:17:15 +00:00
|
|
|
// If we want to skip this file and its a directory
|
|
|
|
// then we should first check to see if there's an
|
2016-12-08 17:35:32 +00:00
|
|
|
// excludes pattern (e.g. !dir/file) that starts with this
|
2016-02-19 19:17:15 +00:00
|
|
|
// 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
|
2017-03-08 22:23:25 +00:00
|
|
|
if !pm.Exclusions() {
|
2014-07-07 12:23:07 +00:00
|
|
|
return filepath.SkipDir
|
2014-02-14 00:05:36 +00:00
|
|
|
}
|
2016-02-19 19:17:15 +00:00
|
|
|
|
|
|
|
dirSlash := relFilePath + string(filepath.Separator)
|
|
|
|
|
2017-03-08 22:23:25 +00:00
|
|
|
for _, pat := range pm.Patterns() {
|
|
|
|
if !pat.Exclusion() {
|
2016-02-19 19:17:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-03-08 22:23:25 +00:00
|
|
|
if strings.HasPrefix(pat.String()+string(filepath.Separator), dirSlash) {
|
2016-02-19 19:17:15 +00:00
|
|
|
// found a match - so can't skip this dir
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No matching exclusion dir so just skip dir
|
|
|
|
return filepath.SkipDir
|
2014-02-14 00:05:36 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 21:30:11 +00:00
|
|
|
if seen[relFilePath] {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
seen[relFilePath] = true
|
|
|
|
|
2015-07-24 21:12:55 +00: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 13:07:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 18:45:53 +00:00
|
|
|
if err := ta.addTarFile(filePath, relFilePath); err != nil {
|
2016-02-19 10:50:11 +00:00
|
|
|
logrus.Errorf("Can't add file %s to tar: %s", filePath, err)
|
2016-03-28 13:17:21 +00:00
|
|
|
// if pipe is broken, stop writing tar stream to it
|
2016-02-19 10:50:11 +00:00
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-20 11:07:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, nil
|
2013-03-11 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// Unpack unpacks the decompressedArchive to dest with options.
|
2014-12-08 21:19:24 +00:00
|
|
|
func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error {
|
2014-02-14 10:12:49 +00:00
|
|
|
tr := tar.NewReader(decompressedArchive)
|
2014-08-12 16:29:46 +00:00
|
|
|
trBuf := pools.BufioReader32KPool.Get(nil)
|
|
|
|
defer pools.BufioReader32KPool.Put(trBuf)
|
2013-12-20 11:08:34 +00:00
|
|
|
|
2014-07-08 19:26:59 +00:00
|
|
|
var dirs []*tar.Header
|
2017-11-16 06:20:33 +00:00
|
|
|
idMapping := idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps)
|
|
|
|
rootIDs := idMapping.RootPair()
|
2018-10-15 07:47:02 +00:00
|
|
|
whiteoutConverter := getWhiteoutConverter(options.WhiteoutFormat, options.InUserNS)
|
2013-12-20 11:08:34 +00:00
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
2014-07-31 07:33:59 +00:00
|
|
|
loop:
|
2013-12-20 11:08:34 +00:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
2013-11-18 23:35:56 +00:00
|
|
|
if err != nil {
|
2013-07-08 20:30:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-11-18 23:35:56 +00:00
|
|
|
|
2013-12-20 11:08:34 +00:00
|
|
|
// Normalize name, for safety and for a simple is-root check
|
2015-06-01 23:42:27 +00:00
|
|
|
// This keeps "../" as-is, but normalizes "/../" to "/". Or Windows:
|
|
|
|
// This keeps "..\" as-is, but normalizes "\..\" to "\".
|
2013-12-20 11:08:34 +00:00
|
|
|
hdr.Name = filepath.Clean(hdr.Name)
|
|
|
|
|
2014-10-23 21:30:11 +00:00
|
|
|
for _, exclude := range options.ExcludePatterns {
|
2014-07-31 07:33:59 +00:00
|
|
|
if strings.HasPrefix(hdr.Name, exclude) {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:42:27 +00: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 11:08:34 +00: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) {
|
2017-05-31 21:18:04 +00:00
|
|
|
err = idtools.MkdirAllAndChownNew(parentPath, 0777, rootIDs)
|
2013-12-20 11:08:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 19:26:59 +00:00
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2014-11-27 07:00:13 +00:00
|
|
|
rel, err := filepath.Rel(dest, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-01 23:42:27 +00:00
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
2014-11-27 07:00:13 +00:00
|
|
|
return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
|
2014-10-20 19:36:28 +00:00
|
|
|
}
|
2013-12-20 11:08:34 +00: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 20:15:36 +00: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 14:21:22 +00:00
|
|
|
if fi.IsDir() && hdr.Name == "." {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-19 20:15:36 +00:00
|
|
|
|
2013-12-20 11:08:34 +00:00
|
|
|
if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 16:31:19 +00:00
|
|
|
trBuf.Reset(tr)
|
2015-01-19 20:15:36 +00:00
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
if err := remapIDs(idMapping, hdr); err != nil {
|
2017-05-31 21:18:04 +00:00
|
|
|
return err
|
2015-10-08 15:51:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 22:07:29 +00:00
|
|
|
if whiteoutConverter != nil {
|
|
|
|
writeFile, err := whiteoutConverter.ConvertRead(hdr, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-14 05:42:52 +00:00
|
|
|
}
|
2016-04-06 22:07:29 +00:00
|
|
|
if !writeFile {
|
2015-12-14 05:42:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 20:25:13 +00:00
|
|
|
if err := createTarFile(path, dest, hdr, trBuf, !options.NoLchown, options.ChownOpts, options.InUserNS); err != nil {
|
2013-12-20 11:08:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-06-14 00:53:38 +00:00
|
|
|
|
2013-12-20 11:08:34 +00: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-12 01:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 11:08:34 +00:00
|
|
|
for _, hdr := range dirs {
|
|
|
|
path := filepath.Join(dest, hdr.Name)
|
2015-10-01 17:45:32 +00:00
|
|
|
|
|
|
|
if err := system.Chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
|
2013-12-20 11:08:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-03-11 12:42:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-08 21:19:24 +00:00
|
|
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
|
|
|
// and unpacks it into the directory at `dest`.
|
|
|
|
// The archive may be compressed with one of the following algorithms:
|
|
|
|
// identity (uncompressed), gzip, bzip2, xz.
|
|
|
|
// FIXME: specify behavior when target path exists vs. doesn't exist.
|
2015-07-27 13:46:20 +00:00
|
|
|
func Untar(tarArchive io.Reader, dest string, options *TarOptions) error {
|
|
|
|
return untarHandler(tarArchive, dest, options, true)
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive,
|
2015-07-27 13:46:20 +00: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 21:19:24 +00:00
|
|
|
return fmt.Errorf("Empty archive")
|
|
|
|
}
|
|
|
|
dest = filepath.Clean(dest)
|
|
|
|
if options == nil {
|
|
|
|
options = &TarOptions{}
|
|
|
|
}
|
2014-10-23 21:30:11 +00:00
|
|
|
if options.ExcludePatterns == nil {
|
|
|
|
options.ExcludePatterns = []string{}
|
2014-12-08 21:19:24 +00:00
|
|
|
}
|
2015-07-27 13:46:20 +00:00
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
r := tarArchive
|
2015-07-27 13:46:20 +00:00
|
|
|
if decompress {
|
|
|
|
decompressedArchive, err := DecompressStream(tarArchive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer decompressedArchive.Close()
|
|
|
|
r = decompressedArchive
|
2014-12-08 21:19:24 +00:00
|
|
|
}
|
2015-07-27 13:46:20 +00:00
|
|
|
|
|
|
|
return Unpack(r, dest, options)
|
2014-12-08 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other.
|
|
|
|
// If either Tar or Untar fails, TarUntar aborts and returns the error.
|
2014-11-06 18:01:37 +00:00
|
|
|
func (archiver *Archiver) TarUntar(src, dst string) error {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("TarUntar(%s %s)", src, dst)
|
2014-02-14 00:05:36 +00:00
|
|
|
archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
|
2013-06-19 03:28:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-14 11:41:46 +00:00
|
|
|
defer archive.Close()
|
2017-05-24 15:53:41 +00:00
|
|
|
options := &TarOptions{
|
2017-11-16 06:20:33 +00:00
|
|
|
UIDMaps: archiver.IDMapping.UIDs(),
|
|
|
|
GIDMaps: archiver.IDMapping.GIDs(),
|
2015-10-08 15:51:41 +00:00
|
|
|
}
|
|
|
|
return archiver.Untar(archive, dst, options)
|
2013-06-19 03:28:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00:00
|
|
|
// UntarPath untar a file from path to a destination, src is the source tar file path.
|
2014-11-06 18:01:37 +00:00
|
|
|
func (archiver *Archiver) UntarPath(src, dst string) error {
|
2014-02-14 11:41:46 +00:00
|
|
|
archive, err := os.Open(src)
|
|
|
|
if err != nil {
|
2013-06-14 23:43:39 +00:00
|
|
|
return err
|
2014-02-14 11:41:46 +00:00
|
|
|
}
|
|
|
|
defer archive.Close()
|
2017-05-24 15:53:41 +00:00
|
|
|
options := &TarOptions{
|
2017-11-16 06:20:33 +00:00
|
|
|
UIDMaps: archiver.IDMapping.UIDs(),
|
|
|
|
GIDMaps: archiver.IDMapping.GIDs(),
|
2015-10-08 15:51:41 +00:00
|
|
|
}
|
2015-11-20 16:49:33 +00:00
|
|
|
return archiver.Untar(archive, dst, options)
|
2013-06-14 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00: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 18:01:37 +00:00
|
|
|
func (archiver *Archiver) CopyWithTar(src, dst string) error {
|
2013-06-19 03:28:49 +00:00
|
|
|
srcSt, err := os.Stat(src)
|
2013-06-14 23:43:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-06-21 03:20:16 +00:00
|
|
|
if !srcSt.IsDir() {
|
2014-11-06 18:01:37 +00:00
|
|
|
return archiver.CopyFileWithTar(src, dst)
|
2013-06-21 03:20:16 +00:00
|
|
|
}
|
2016-03-13 04:05:45 +00:00
|
|
|
|
2017-08-04 00:22:00 +00:00
|
|
|
// if this Archiver is set up with ID mapping we need to create
|
2016-03-13 04:05:45 +00:00
|
|
|
// the new destination directory with the remapped root UID/GID pair
|
|
|
|
// as owner
|
2017-11-16 06:20:33 +00:00
|
|
|
rootIDs := archiver.IDMapping.RootPair()
|
2013-06-21 03:20:16 +00:00
|
|
|
// Create dst, copy src's content into it
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Creating dest directory: %s", dst)
|
2017-05-24 15:53:41 +00:00
|
|
|
if err := idtools.MkdirAllAndChownNew(dst, 0755, rootIDs); err != nil {
|
2013-06-21 03:20:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
2014-11-06 18:01:37 +00:00
|
|
|
return archiver.TarUntar(src, dst)
|
2013-06-21 03:20:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00: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 18:01:37 +00:00
|
|
|
func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst)
|
2013-06-21 03:20:16 +00:00
|
|
|
srcSt, err := os.Stat(src)
|
2013-06-19 03:28:49 +00:00
|
|
|
if err != nil {
|
2013-06-21 03:20:16 +00:00
|
|
|
return err
|
2013-06-19 03:28:49 +00:00
|
|
|
}
|
2015-06-01 23:42:27 +00:00
|
|
|
|
2013-06-19 03:28:49 +00:00
|
|
|
if srcSt.IsDir() {
|
2013-06-21 03:20:16 +00:00
|
|
|
return fmt.Errorf("Can't copy a directory")
|
2013-06-19 03:28:49 +00:00
|
|
|
}
|
2015-06-01 23:42:27 +00:00
|
|
|
|
|
|
|
// Clean up the trailing slash. This must be done in an operating
|
|
|
|
// system specific manner.
|
2015-05-07 22:14:11 +00:00
|
|
|
if dst[len(dst)-1] == os.PathSeparator {
|
|
|
|
dst = filepath.Join(dst, filepath.Base(src))
|
2013-06-19 03:28:49 +00:00
|
|
|
}
|
2013-06-21 03:20:16 +00:00
|
|
|
// Create the holding directory if necessary
|
2017-06-02 01:59:11 +00:00
|
|
|
if err := system.MkdirAll(filepath.Dir(dst), 0700, ""); err != nil {
|
2013-06-21 03:20:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-01-07 01:37:49 +00:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
2017-09-22 00:56:45 +00:00
|
|
|
errC := make(chan error, 1)
|
2014-01-07 01:37:49 +00:00
|
|
|
|
2017-09-22 00:56:45 +00:00
|
|
|
go func() {
|
|
|
|
defer close(errC)
|
2014-01-07 01:37:49 +00:00
|
|
|
|
2017-09-22 00:56:45 +00:00
|
|
|
errC <- func() error {
|
|
|
|
defer w.Close()
|
2015-03-04 02:40:16 +00:00
|
|
|
|
2017-09-22 00:56:45 +00:00
|
|
|
srcF, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
2015-10-08 15:51:41 +00:00
|
|
|
|
2017-09-22 00:56:45 +00:00
|
|
|
hdr, err := tar.FileInfoHeader(srcSt, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-02 17:48:34 +00:00
|
|
|
hdr.Format = tar.FormatPAX
|
|
|
|
hdr.ModTime = hdr.ModTime.Truncate(time.Second)
|
|
|
|
hdr.AccessTime = time.Time{}
|
|
|
|
hdr.ChangeTime = time.Time{}
|
2017-09-22 00:56:45 +00:00
|
|
|
hdr.Name = filepath.Base(dst)
|
|
|
|
hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode)))
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
if err := remapIDs(archiver.IDMapping, hdr); err != nil {
|
2017-09-22 00:56:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := tar.NewWriter(w)
|
|
|
|
defer tw.Close()
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tw, srcF); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
}()
|
2014-01-07 01:37:49 +00:00
|
|
|
defer func() {
|
2016-09-30 18:46:37 +00:00
|
|
|
if er := <-errC; err == nil && er != nil {
|
2014-01-07 01:37:49 +00:00
|
|
|
err = er
|
|
|
|
}
|
|
|
|
}()
|
2015-10-08 15:51:41 +00:00
|
|
|
|
2015-10-14 23:25:03 +00:00
|
|
|
err = archiver.Untar(r, filepath.Dir(dst), nil)
|
|
|
|
if err != nil {
|
|
|
|
r.CloseWithError(err)
|
|
|
|
}
|
|
|
|
return err
|
2014-11-06 18:01:37 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
// IdentityMapping returns the IdentityMapping of the archiver.
|
|
|
|
func (archiver *Archiver) IdentityMapping() *idtools.IdentityMapping {
|
|
|
|
return archiver.IDMapping
|
2017-08-04 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
func remapIDs(idMapping *idtools.IdentityMapping, hdr *tar.Header) error {
|
|
|
|
ids, err := idMapping.ToHost(idtools.Identity{UID: hdr.Uid, GID: hdr.Gid})
|
2017-05-31 21:18:04 +00:00
|
|
|
hdr.Uid, hdr.Gid = ids.UID, ids.GID
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 19:54:21 +00:00
|
|
|
// cmdStream executes a command, and returns its stdout as a stream.
|
2013-03-29 20:18:59 +00:00
|
|
|
// If the command fails to run or doesn't complete successfully, an error
|
|
|
|
// will be returned, including anything written on stderr.
|
2018-01-16 18:49:18 +00:00
|
|
|
func cmdStream(cmd *exec.Cmd, input io.Reader) (io.ReadCloser, error) {
|
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 19:54:21 +00:00
|
|
|
cmd.Stdin = input
|
2013-03-11 12:42:36 +00: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 19:54:21 +00:00
|
|
|
cmd.Stdout = pipeW
|
|
|
|
var errBuf bytes.Buffer
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
|
|
|
|
// Run the command and return the pipe
|
|
|
|
if err := cmd.Start(); err != nil {
|
2018-01-16 18:49:18 +00:00
|
|
|
return nil, err
|
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 19:54:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 20:26:02 +00:00
|
|
|
// Copy stdout to the returned pipe
|
2013-03-29 11:42:17 +00:00
|
|
|
go func() {
|
2013-03-11 12:42:36 +00: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 19:54:21 +00:00
|
|
|
pipeW.CloseWithError(fmt.Errorf("%s: %s", err, errBuf.String()))
|
2013-03-11 12:42:36 +00: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 19:54:21 +00:00
|
|
|
|
2018-01-16 18:49:18 +00:00
|
|
|
return pipeR, nil
|
2013-03-11 12:42:36 +00:00
|
|
|
}
|
2013-04-21 21:23:55 +00: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.
|
2016-10-20 23:40:59 +00:00
|
|
|
func NewTempArchive(src io.Reader, dir string) (*TempArchive, error) {
|
2013-04-21 21:23:55 +00:00
|
|
|
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 20:36:57 +00:00
|
|
|
return &TempArchive{File: f, Size: size}, nil
|
2013-04-21 21:23:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 01:52:54 +00: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 21:23:55 +00:00
|
|
|
type TempArchive struct {
|
|
|
|
*os.File
|
2014-12-03 20:36:57 +00: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 21:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (archive *TempArchive) Read(data []byte) (int, error) {
|
|
|
|
n, err := archive.File.Read(data)
|
2014-11-20 17:34:35 +00:00
|
|
|
archive.read += int64(n)
|
|
|
|
if err != nil || archive.read == archive.Size {
|
2014-12-03 20:36:57 +00:00
|
|
|
archive.Close()
|
2013-04-21 21:23:55 +00:00
|
|
|
os.Remove(archive.File.Name())
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|