15370: Fix flaky test.
[arvados.git] / sdk / go / keepclient / hashcheck.go
index b97df78d96da1e8e196e40adfa7773b1eb467b5c..0966e072eae6d354ad8664d935ce290fb35f7649 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 package keepclient
 
 import (
@@ -25,58 +29,59 @@ type HashCheckingReader struct {
 // Reads from the underlying reader, update the hashing function, and
 // pass the results through. Returns BadChecksum (instead of EOF) on
 // the last read if the checksum doesn't match.
-func (this HashCheckingReader) Read(p []byte) (n int, err error) {
-       n, err = this.Reader.Read(p)
+func (hcr HashCheckingReader) Read(p []byte) (n int, err error) {
+       n, err = hcr.Reader.Read(p)
        if n > 0 {
-               this.Hash.Write(p[:n])
+               hcr.Hash.Write(p[:n])
        }
        if err == io.EOF {
-               sum := this.Hash.Sum(make([]byte, 0, this.Hash.Size()))
-               if fmt.Sprintf("%x", sum) != this.Check {
+               sum := hcr.Hash.Sum(nil)
+               if fmt.Sprintf("%x", sum) != hcr.Check {
                        err = BadChecksum
                }
        }
        return n, err
 }
 
-// WriteTo writes the entire contents of this.Reader to dest.  Returns
-// BadChecksum if the checksum doesn't match.
-func (this HashCheckingReader) WriteTo(dest io.Writer) (written int64, err error) {
-       if writeto, ok := this.Reader.(io.WriterTo); ok {
-               written, err = writeto.WriteTo(io.MultiWriter(dest, this.Hash))
+// WriteTo writes the entire contents of hcr.Reader to dest. Returns
+// BadChecksum if writing is successful but the checksum doesn't
+// match.
+func (hcr HashCheckingReader) WriteTo(dest io.Writer) (written int64, err error) {
+       if writeto, ok := hcr.Reader.(io.WriterTo); ok {
+               written, err = writeto.WriteTo(io.MultiWriter(dest, hcr.Hash))
        } else {
-               written, err = io.Copy(io.MultiWriter(dest, this.Hash), this.Reader)
+               written, err = io.Copy(io.MultiWriter(dest, hcr.Hash), hcr.Reader)
        }
 
-       sum := this.Hash.Sum(make([]byte, 0, this.Hash.Size()))
+       if err != nil {
+               return written, err
+       }
 
-       if fmt.Sprintf("%x", sum) != this.Check {
-               err = BadChecksum
+       sum := hcr.Hash.Sum(nil)
+       if fmt.Sprintf("%x", sum) != hcr.Check {
+               return written, BadChecksum
        }
 
-       return written, err
+       return written, nil
 }
 
 // Close reads all remaining data from the underlying Reader and
 // returns BadChecksum if the checksum doesn't match. It also closes
 // the underlying Reader if it implements io.ReadCloser.
-func (this HashCheckingReader) Close() (err error) {
-       _, err = io.Copy(this.Hash, this.Reader)
+func (hcr HashCheckingReader) Close() (err error) {
+       _, err = io.Copy(hcr.Hash, hcr.Reader)
 
-       if closer, ok := this.Reader.(io.Closer); ok {
-               err2 := closer.Close()
-               if err2 != nil && err == nil {
-                       return err2
+       if closer, ok := hcr.Reader.(io.Closer); ok {
+               closeErr := closer.Close()
+               if err == nil {
+                       err = closeErr
                }
        }
        if err != nil {
                return err
        }
-
-       sum := this.Hash.Sum(make([]byte, 0, this.Hash.Size()))
-       if fmt.Sprintf("%x", sum) != this.Check {
-               err = BadChecksum
+       if fmt.Sprintf("%x", hcr.Hash.Sum(nil)) != hcr.Check {
+               return BadChecksum
        }
-
-       return err
+       return nil
 }