Merge pull request #3353 from creack/improve_add_cache

Improve add cache
This commit is contained in:
Guillaume J. Charmes
2014-01-02 16:07:33 -08:00
3 changed files with 76 additions and 119 deletions
+26 -25
View File
@@ -1,38 +1,30 @@
package utils
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"archive/tar"
"hash"
"io"
"sort"
"strconv"
"strings"
)
type verboseHash struct {
hash.Hash
}
func (h verboseHash) Write(buf []byte) (int, error) {
Debugf("--->%s<---", buf)
return h.Hash.Write(buf)
}
type TarSum struct {
io.Reader
tarR *tar.Reader
tarW *tar.Writer
gz *gzip.Writer
bufTar *bytes.Buffer
bufGz *bytes.Buffer
h hash.Hash
h2 verboseHash
sums []string
finished bool
first bool
tarR *tar.Reader
tarW *tar.Writer
gz *gzip.Writer
bufTar *bytes.Buffer
bufGz *bytes.Buffer
h hash.Hash
sums map[string]string
currentFile string
finished bool
first bool
}
func (ts *TarSum) encodeHeader(h *tar.Header) error {
@@ -52,7 +44,6 @@ func (ts *TarSum) encodeHeader(h *tar.Header) error {
// {"atime", strconv.Itoa(int(h.AccessTime.UTC().Unix()))},
// {"ctime", strconv.Itoa(int(h.ChangeTime.UTC().Unix()))},
} {
// Debugf("-->%s<-- -->%s<--", elem[0], elem[1])
if _, err := ts.h.Write([]byte(elem[0] + elem[1])); err != nil {
return err
}
@@ -68,9 +59,9 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
ts.tarW = tar.NewWriter(ts.bufTar)
ts.gz = gzip.NewWriter(ts.bufGz)
ts.h = sha256.New()
// ts.h = verboseHash{sha256.New()}
ts.h.Reset()
ts.first = true
ts.sums = make(map[string]string)
}
if ts.finished {
@@ -85,7 +76,7 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
return 0, err
}
if !ts.first {
ts.sums = append(ts.sums, hex.EncodeToString(ts.h.Sum(nil)))
ts.sums[ts.currentFile] = hex.EncodeToString(ts.h.Sum(nil))
ts.h.Reset()
} else {
ts.first = false
@@ -102,6 +93,7 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
}
return n, err
}
ts.currentFile = strings.TrimSuffix(strings.TrimPrefix(currentHeader.Name, "./"), "/")
if err := ts.encodeHeader(currentHeader); err != nil {
return 0, err
}
@@ -143,12 +135,17 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
}
func (ts *TarSum) Sum(extra []byte) string {
sort.Strings(ts.sums)
var sums []string
for _, sum := range ts.sums {
sums = append(sums, sum)
}
sort.Strings(sums)
h := sha256.New()
if extra != nil {
h.Write(extra)
}
for _, sum := range ts.sums {
for _, sum := range sums {
Debugf("-->%s<--", sum)
h.Write([]byte(sum))
}
@@ -156,3 +153,7 @@ func (ts *TarSum) Sum(extra []byte) string {
Debugf("checksum processed: %s", checksum)
return checksum
}
func (ts *TarSum) GetSums() map[string]string {
return ts.sums
}