Log files name along with their checksum in TarSum + add a Method to retrieve the checksum map

This commit is contained in:
Guillaume J. Charmes 2013-12-26 16:01:36 -08:00
parent 1d4b7d8fa1
commit fc9f4d8bad
No known key found for this signature in database
GPG Key ID: B33E4642CB6E3FF3
1 changed files with 23 additions and 12 deletions

View File

@ -20,7 +20,8 @@ type TarSum struct {
bufTar *bytes.Buffer bufTar *bytes.Buffer
bufGz *bytes.Buffer bufGz *bytes.Buffer
h hash.Hash h hash.Hash
sums []string sums map[string]string
currentFile string
finished bool finished bool
first bool first bool
} }
@ -59,6 +60,7 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
ts.h = sha256.New() ts.h = sha256.New()
ts.h.Reset() ts.h.Reset()
ts.first = true ts.first = true
ts.sums = make(map[string]string)
} }
if ts.finished { if ts.finished {
@ -73,7 +75,7 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
return 0, err return 0, err
} }
if !ts.first { 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() ts.h.Reset()
} else { } else {
ts.first = false ts.first = false
@ -131,12 +133,17 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
} }
func (ts *TarSum) Sum(extra []byte) string { 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() h := sha256.New()
if extra != nil { if extra != nil {
h.Write(extra) h.Write(extra)
} }
for _, sum := range ts.sums { for _, sum := range sums {
Debugf("-->%s<--", sum) Debugf("-->%s<--", sum)
h.Write([]byte(sum)) h.Write([]byte(sum))
} }
@ -144,3 +151,7 @@ func (ts *TarSum) Sum(extra []byte) string {
Debugf("checksum processed: %s", checksum) Debugf("checksum processed: %s", checksum)
return checksum return checksum
} }
func (ts *TarSum) GetSums() map[string]string {
return ts.sums
}