diff --git a/fs/hash.go b/fs/hash.go index 132b54868..e8d9d8e1e 100644 --- a/fs/hash.go +++ b/fs/hash.go @@ -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 diff --git a/local/local.go b/local/local.go index 9abc1bb03..6f4e617d3 100644 --- a/local/local.go +++ b/local/local.go @@ -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 }