Browse Source

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

Guillaume J. Charmes 11 years ago
parent
commit
fc9f4d8bad
1 changed files with 23 additions and 12 deletions
  1. 23 12
      utils/tarsum.go

+ 23 - 12
utils/tarsum.go

@@ -14,15 +14,16 @@ import (
 
 type TarSum struct {
 	io.Reader
-	tarR     *tar.Reader
-	tarW     *tar.Writer
-	gz       *gzip.Writer
-	bufTar   *bytes.Buffer
-	bufGz    *bytes.Buffer
-	h        hash.Hash
-	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 {
@@ -59,6 +60,7 @@ func (ts *TarSum) Read(buf []byte) (int, error) {
 		ts.h = sha256.New()
 		ts.h.Reset()
 		ts.first = true
+		ts.sums = make(map[string]string)
 	}
 
 	if ts.finished {
@@ -73,7 +75,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
@@ -131,12 +133,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))
 	}
@@ -144,3 +151,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
+}