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,8 +117,9 @@ func hashToMultiWriter(h map[HashType]hash.Hash) io.Writer {
// A MultiHasher will construct various hashes on
// all incoming writes.
type MultiHasher struct {
io.Writer
h map[HashType]hash.Hash // Hashes
w io.Writer
size int64
h map[HashType]hash.Hash // Hashes
}
// NewMultiHasher will return a hash writer that will write all
@ -138,10 +139,16 @@ func NewMultiHasherTypes(set HashSet) (*MultiHasher, error) {
if err != nil {
return nil, err
}
m := MultiHasher{h: hashers, Writer: hashToMultiWriter(hashers)}
m := MultiHasher{h: hashers, w: hashToMultiWriter(hashers)}
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
// strings.
func (m *MultiHasher) Sums() map[HashType]string {
@ -152,6 +159,11 @@ func (m *MultiHasher) Sums() map[HashType]string {
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.
type HashSet int

View file

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