From 08a87d4b3b3f76ac9d73309b1042aa75d6634ed9 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 8 Jul 2013 13:30:03 -0700 Subject: [PATCH] Fix #1162 - Remove bufio from Untar --- archive.go | 24 ++++++++++++++---------- image.go | 3 +++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/archive.go b/archive.go index c9a650cb8d..01af86006f 100644 --- a/archive.go +++ b/archive.go @@ -2,7 +2,6 @@ package docker import ( "archive/tar" - "bufio" "bytes" "fmt" "github.com/dotcloud/docker/utils" @@ -26,10 +25,6 @@ const ( ) func DetectCompression(source []byte) Compression { - for _, c := range source[:10] { - utils.Debugf("%x", c) - } - sourceLen := len(source) for compression, m := range map[Compression][]byte{ Bzip2: {0x42, 0x5A, 0x68}, @@ -110,17 +105,26 @@ func Untar(archive io.Reader, path string) error { if archive == nil { return fmt.Errorf("Empty archive") } - bufferedArchive := bufio.NewReaderSize(archive, 10) - buf, err := bufferedArchive.Peek(10) - if err != nil { - return err + + buf := make([]byte, 10) + totalN := 0 + for totalN < 10 { + if n, err := archive.Read(buf[totalN:]); err != nil { + if err == io.EOF { + return fmt.Errorf("Tarball too short") + } + return err + } else { + totalN += n + utils.Debugf("[tar autodetect] n: %d", n) + } } compression := DetectCompression(buf) utils.Debugf("Archive compression detected: %s", compression.Extension()) cmd := exec.Command("tar", "--numeric-owner", "-f", "-", "-C", path, "-x"+compression.Flag()) - cmd.Stdin = bufferedArchive + cmd.Stdin = io.MultiReader(bytes.NewReader(buf), archive) // Hardcode locale environment for predictable outcome regardless of host configuration. // (see https://github.com/dotcloud/docker/issues/355) cmd.Env = []string{"LANG=en_US.utf-8", "LC_ALL=en_US.utf-8"} diff --git a/image.go b/image.go index bb6598b262..e1b1ac0418 100644 --- a/image.go +++ b/image.go @@ -94,9 +94,12 @@ func StoreImage(img *Image, layerData Archive, root string, store bool) error { } // If layerData is not nil, unpack it into the new layer if layerData != nil { + start := time.Now() + utils.Debugf("Start untar layer") if err := Untar(layerData, layer); err != nil { return err } + utils.Debugf("Untar time: %vs\n", time.Now().Sub(start).Seconds()) } return StoreSize(img, root)