Fix incomplete local hashes.

Fixes #533
This commit is contained in:
Klaus Post 2016-06-19 14:49:34 +02:00
parent ab0947ee37
commit e0aa4bb492
2 changed files with 19 additions and 7 deletions

View file

@ -117,7 +117,8 @@ func hashToMultiWriter(h map[HashType]hash.Hash) io.Writer {
// A MultiHasher will construct various hashes on // A MultiHasher will construct various hashes on
// all incoming writes. // all incoming writes.
type MultiHasher struct { type MultiHasher struct {
io.Writer w io.Writer
size int64
h map[HashType]hash.Hash // Hashes h map[HashType]hash.Hash // Hashes
} }
@ -138,10 +139,16 @@ func NewMultiHasherTypes(set HashSet) (*MultiHasher, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
m := MultiHasher{h: hashers, Writer: hashToMultiWriter(hashers)} m := MultiHasher{h: hashers, w: hashToMultiWriter(hashers)}
return &m, nil return &m, nil
} }
func (m *MultiHasher) Write(p []byte) (n int, err error) {
n, err = m.w.Write(p)
m.size += int64(n)
return n, err
}
// Sums returns the sums of all accumulated hashes as hex encoded // Sums returns the sums of all accumulated hashes as hex encoded
// strings. // strings.
func (m *MultiHasher) Sums() map[HashType]string { func (m *MultiHasher) Sums() map[HashType]string {
@ -152,6 +159,11 @@ func (m *MultiHasher) Sums() map[HashType]string {
return dst return dst
} }
// Size returns the number of bytes written
func (m *MultiHasher) Size() int64 {
return m.size
}
// A HashSet Indicates one or more hash types. // A HashSet Indicates one or more hash types.
type HashSet int type HashSet int

View file

@ -566,13 +566,13 @@ func (file *localOpenFile) Read(p []byte) (n int, err error) {
return return
} }
// Close the object and update the md5sum // Close the object and update the hashes
func (file *localOpenFile) Close() (err error) { func (file *localOpenFile) Close() (err error) {
err = file.in.Close() err = file.in.Close()
if err == nil { if err == nil {
if file.hash.Size() == file.o.Size() {
file.o.hashes = file.hash.Sums() file.o.hashes = file.hash.Sums()
} else { }
file.o.hashes = nil
} }
return err return err
} }