X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/060d38d627bd1e51dd2b3c6e7de9af6aa7d7b6f3..21e13deb6b38f6bea48923306755f648acd2d794:/sdk/go/keepclient/hashcheck.go diff --git a/sdk/go/keepclient/hashcheck.go b/sdk/go/keepclient/hashcheck.go index 4a5c09ba79..9295c14cc2 100644 --- a/sdk/go/keepclient/hashcheck.go +++ b/sdk/go/keepclient/hashcheck.go @@ -35,7 +35,7 @@ func (this HashCheckingReader) Read(p []byte) (n int, err error) { this.Hash.Write(p[:n]) } if err == io.EOF { - sum := this.Hash.Sum(make([]byte, 0, this.Hash.Size())) + sum := this.Hash.Sum(nil) if fmt.Sprintf("%x", sum) != this.Check { err = BadChecksum } @@ -43,8 +43,9 @@ func (this HashCheckingReader) Read(p []byte) (n int, err error) { return n, err } -// WriteTo writes the entire contents of this.Reader to dest. Returns -// BadChecksum if the checksum doesn't match. +// WriteTo writes the entire contents of this.Reader to dest. Returns +// BadChecksum if writing is successful but 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)) @@ -52,13 +53,16 @@ func (this HashCheckingReader) WriteTo(dest io.Writer) (written int64, err error written, err = io.Copy(io.MultiWriter(dest, this.Hash), this.Reader) } - sum := this.Hash.Sum(make([]byte, 0, this.Hash.Size())) + if err != nil { + return written, err + } + sum := this.Hash.Sum(nil) if fmt.Sprintf("%x", sum) != this.Check { - err = BadChecksum + return written, BadChecksum } - return written, err + return written, nil } // Close reads all remaining data from the underlying Reader and @@ -68,19 +72,16 @@ func (this HashCheckingReader) Close() (err error) { _, err = io.Copy(this.Hash, this.Reader) if closer, ok := this.Reader.(io.Closer); ok { - err2 := closer.Close() - if err2 != nil && err == nil { - return err2 + 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", this.Hash.Sum(nil)) != this.Check { + return BadChecksum } - - return err + return nil }