2013-07-17 15:13:22 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2014-02-27 11:43:44 -05:00
|
|
|
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2013-07-17 15:13:22 -04:00
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2013-12-26 19:02:15 -05:00
|
|
|
"strings"
|
2013-07-17 15:13:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type TarSum struct {
|
|
|
|
io.Reader
|
2014-01-07 20:46:04 -05:00
|
|
|
tarR *tar.Reader
|
|
|
|
tarW *tar.Writer
|
|
|
|
gz writeCloseFlusher
|
|
|
|
bufTar *bytes.Buffer
|
|
|
|
bufGz *bytes.Buffer
|
|
|
|
h hash.Hash
|
|
|
|
sums map[string]string
|
|
|
|
currentFile string
|
|
|
|
finished bool
|
|
|
|
first bool
|
|
|
|
DisableCompression bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeCloseFlusher interface {
|
|
|
|
io.WriteCloser
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopCloseFlusher struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nopCloseFlusher) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nopCloseFlusher) Flush() error {
|
|
|
|
return nil
|
2013-07-17 15:13:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TarSum) encodeHeader(h *tar.Header) error {
|
|
|
|
for _, elem := range [][2]string{
|
|
|
|
{"name", h.Name},
|
|
|
|
{"mode", strconv.Itoa(int(h.Mode))},
|
|
|
|
{"uid", strconv.Itoa(h.Uid)},
|
|
|
|
{"gid", strconv.Itoa(h.Gid)},
|
|
|
|
{"size", strconv.Itoa(int(h.Size))},
|
|
|
|
{"mtime", strconv.Itoa(int(h.ModTime.UTC().Unix()))},
|
|
|
|
{"typeflag", string([]byte{h.Typeflag})},
|
|
|
|
{"linkname", h.Linkname},
|
|
|
|
{"uname", h.Uname},
|
|
|
|
{"gname", h.Gname},
|
|
|
|
{"devmajor", strconv.Itoa(int(h.Devmajor))},
|
|
|
|
{"devminor", strconv.Itoa(int(h.Devminor))},
|
|
|
|
// {"atime", strconv.Itoa(int(h.AccessTime.UTC().Unix()))},
|
|
|
|
// {"ctime", strconv.Itoa(int(h.ChangeTime.UTC().Unix()))},
|
|
|
|
} {
|
|
|
|
if _, err := ts.h.Write([]byte(elem[0] + elem[1])); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TarSum) Read(buf []byte) (int, error) {
|
|
|
|
if ts.gz == nil {
|
|
|
|
ts.bufTar = bytes.NewBuffer([]byte{})
|
|
|
|
ts.bufGz = bytes.NewBuffer([]byte{})
|
|
|
|
ts.tarR = tar.NewReader(ts.Reader)
|
|
|
|
ts.tarW = tar.NewWriter(ts.bufTar)
|
2014-01-07 20:46:04 -05:00
|
|
|
if !ts.DisableCompression {
|
|
|
|
ts.gz = gzip.NewWriter(ts.bufGz)
|
|
|
|
} else {
|
|
|
|
ts.gz = &nopCloseFlusher{Writer: ts.bufGz}
|
|
|
|
}
|
2013-07-17 15:13:22 -04:00
|
|
|
ts.h = sha256.New()
|
|
|
|
ts.h.Reset()
|
|
|
|
ts.first = true
|
2013-12-26 19:01:36 -05:00
|
|
|
ts.sums = make(map[string]string)
|
2013-07-17 15:13:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ts.finished {
|
|
|
|
return ts.bufGz.Read(buf)
|
|
|
|
}
|
|
|
|
buf2 := make([]byte, len(buf), cap(buf))
|
|
|
|
|
|
|
|
n, err := ts.tarR.Read(buf2)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if _, err := ts.h.Write(buf2[:n]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !ts.first {
|
2013-12-26 19:01:36 -05:00
|
|
|
ts.sums[ts.currentFile] = hex.EncodeToString(ts.h.Sum(nil))
|
2013-07-17 15:13:22 -04:00
|
|
|
ts.h.Reset()
|
|
|
|
} else {
|
|
|
|
ts.first = false
|
|
|
|
}
|
|
|
|
|
|
|
|
currentHeader, err := ts.tarR.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
if err := ts.gz.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
ts.finished = true
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
2013-12-26 19:02:15 -05:00
|
|
|
ts.currentFile = strings.TrimSuffix(strings.TrimPrefix(currentHeader.Name, "./"), "/")
|
2013-07-17 15:13:22 -04:00
|
|
|
if err := ts.encodeHeader(currentHeader); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := ts.tarW.WriteHeader(currentHeader); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if _, err := ts.tarW.Write(buf2[:n]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
ts.tarW.Flush()
|
|
|
|
if _, err := io.Copy(ts.gz, ts.bufTar); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
ts.gz.Flush()
|
|
|
|
|
|
|
|
return ts.bufGz.Read(buf)
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filling the hash buffer
|
|
|
|
if _, err = ts.h.Write(buf2[:n]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filling the tar writter
|
|
|
|
if _, err = ts.tarW.Write(buf2[:n]); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
ts.tarW.Flush()
|
|
|
|
|
|
|
|
// Filling the gz writter
|
|
|
|
if _, err = io.Copy(ts.gz, ts.bufTar); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
ts.gz.Flush()
|
|
|
|
|
|
|
|
return ts.bufGz.Read(buf)
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:50:32 -04:00
|
|
|
func (ts *TarSum) Sum(extra []byte) string {
|
2013-12-26 19:01:36 -05:00
|
|
|
var sums []string
|
|
|
|
|
|
|
|
for _, sum := range ts.sums {
|
|
|
|
sums = append(sums, sum)
|
|
|
|
}
|
|
|
|
sort.Strings(sums)
|
2013-07-17 15:13:22 -04:00
|
|
|
h := sha256.New()
|
2013-07-22 19:16:31 -04:00
|
|
|
if extra != nil {
|
|
|
|
h.Write(extra)
|
|
|
|
}
|
2013-12-26 19:01:36 -05:00
|
|
|
for _, sum := range sums {
|
2013-07-17 15:13:22 -04:00
|
|
|
Debugf("-->%s<--", sum)
|
|
|
|
h.Write([]byte(sum))
|
|
|
|
}
|
2013-07-22 17:50:32 -04:00
|
|
|
checksum := "tarsum+sha256:" + hex.EncodeToString(h.Sum(nil))
|
2013-07-17 15:13:22 -04:00
|
|
|
Debugf("checksum processed: %s", checksum)
|
|
|
|
return checksum
|
|
|
|
}
|
2013-12-26 19:01:36 -05:00
|
|
|
|
|
|
|
func (ts *TarSum) GetSums() map[string]string {
|
|
|
|
return ts.sums
|
|
|
|
}
|