archive: use bufio for compression detection

Docker-DCO-1.1-Signed-off-by: Johan Euphrosine <proppy@google.com> (github: proppy)
This commit is contained in:
Johan Euphrosine 2014-05-09 15:49:02 -07:00
parent 5dfe7c43d4
commit 77c2b76e44
1 changed files with 16 additions and 22 deletions

View File

@ -1,14 +1,11 @@
package archive package archive
import ( import (
"bytes" "bufio"
"compress/bzip2" "compress/bzip2"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt" "fmt"
"github.com/dotcloud/docker/pkg/system"
"github.com/dotcloud/docker/utils"
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -17,6 +14,10 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"syscall" "syscall"
"github.com/dotcloud/docker/pkg/system"
"github.com/dotcloud/docker/utils"
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
) )
type ( type (
@ -74,31 +75,24 @@ func xzDecompress(archive io.Reader) (io.ReadCloser, error) {
} }
func DecompressStream(archive io.Reader) (io.ReadCloser, error) { func DecompressStream(archive io.Reader) (io.ReadCloser, error) {
buf := make([]byte, 10) buf := bufio.NewReader(archive)
totalN := 0 bs, err := buf.Peek(10)
for totalN < 10 { if err != nil {
n, err := archive.Read(buf[totalN:]) return nil, err
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("Tarball too short")
}
return nil, err
}
totalN += n
utils.Debugf("[tar autodetect] n: %d", n)
} }
compression := DetectCompression(buf) utils.Debugf("[tar autodetect] n: %v", bs)
wrap := io.MultiReader(bytes.NewReader(buf), archive)
compression := DetectCompression(bs)
switch compression { switch compression {
case Uncompressed: case Uncompressed:
return ioutil.NopCloser(wrap), nil return ioutil.NopCloser(buf), nil
case Gzip: case Gzip:
return gzip.NewReader(wrap) return gzip.NewReader(buf)
case Bzip2: case Bzip2:
return ioutil.NopCloser(bzip2.NewReader(wrap)), nil return ioutil.NopCloser(bzip2.NewReader(buf)), nil
case Xz: case Xz:
return xzDecompress(wrap) return xzDecompress(buf)
default: default:
return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension()) return nil, fmt.Errorf("Unsupported compression format %s", (&compression).Extension())
} }