hashing: Remove io.WriterTo implementation

This functionality has gone unused since
4b3dc415ef changed hashing.Reader's only
client to use ioutil.ReadAll on a bufio.Reader wrapping the hashing
Reader.

Reverts bcb852a8d0.
This commit is contained in:
greatroar 2022-05-10 22:35:57 +02:00
parent ab49c14621
commit 54b8337813
2 changed files with 25 additions and 87 deletions

View File

@ -5,47 +5,25 @@ import (
"io" "io"
) )
// ReadSumer hashes all data read from the underlying reader. // Reader hashes all data read from the underlying reader.
type ReadSumer interface { type Reader struct {
io.Reader r io.Reader
// Sum returns the hash of the data read so far.
Sum(d []byte) []byte
}
type reader struct {
io.Reader
h hash.Hash h hash.Hash
} }
type readWriterTo struct { // NewReader returns a new Reader that uses the hash h. If the underlying
reader // reader supports WriteTo then the returned reader will do so too.
writerTo io.WriterTo func NewReader(r io.Reader, h hash.Hash) *Reader {
return &Reader{r: r, h: h}
} }
// NewReader returns a new ReadSummer that uses the hash h. If the underlying func (h *Reader) Read(p []byte) (int, error) {
// reader supports WriteTo then the returned reader will do so too. n, err := h.r.Read(p)
func NewReader(r io.Reader, h hash.Hash) ReadSumer { _, _ = h.h.Write(p[:n]) // Never returns an error.
rs := reader{ return n, err
Reader: io.TeeReader(r, h),
h: h,
}
if _, ok := r.(io.WriterTo); ok {
return &readWriterTo{
reader: rs,
writerTo: r.(io.WriterTo),
}
}
return &rs
} }
// Sum returns the hash of the data read so far. // Sum returns the hash of the data read so far.
func (h *reader) Sum(d []byte) []byte { func (h *Reader) Sum(d []byte) []byte {
return h.h.Sum(d) return h.h.Sum(d)
} }
// WriteTo reads all data into the passed writer
func (h *readWriterTo) WriteTo(w io.Writer) (int64, error) {
return h.writerTo.WriteTo(NewWriter(w, h.h))
}

View File

@ -7,26 +7,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"testing" "testing"
rtest "github.com/restic/restic/internal/test"
) )
// only expose Read method
type onlyReader struct {
io.Reader
}
type traceWriterTo struct {
io.Reader
writerTo io.WriterTo
Traced bool
}
func (r *traceWriterTo) WriteTo(w io.Writer) (n int64, err error) {
r.Traced = true
return r.writerTo.WriteTo(w)
}
func TestReader(t *testing.T) { func TestReader(t *testing.T) {
tests := []int{5, 23, 2<<18 + 23, 1 << 20} tests := []int{5, 23, 2<<18 + 23, 1 << 20}
@ -39,44 +21,22 @@ func TestReader(t *testing.T) {
expectedHash := sha256.Sum256(data) expectedHash := sha256.Sum256(data)
for _, test := range []struct { rd := NewReader(bytes.NewReader(data), sha256.New())
innerWriteTo, outerWriteTo bool n, err := io.Copy(ioutil.Discard, rd)
}{{false, false}, {false, true}, {true, false}, {true, true}} { if err != nil {
// test both code paths in WriteTo t.Fatal(err)
src := bytes.NewReader(data) }
rawSrc := &traceWriterTo{Reader: src, writerTo: src}
innerSrc := io.Reader(rawSrc)
if !test.innerWriteTo {
innerSrc = &onlyReader{Reader: rawSrc}
}
rd := NewReader(innerSrc, sha256.New()) if n != int64(size) {
// test both Read and WriteTo t.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
outerSrc := io.Reader(rd) n, size)
if !test.outerWriteTo { }
outerSrc = &onlyReader{Reader: outerSrc}
}
n, err := io.Copy(ioutil.Discard, outerSrc) resultingHash := rd.Sum(nil)
if err != nil {
t.Fatal(err)
}
if n != int64(size) { if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Reader: invalid number of bytes written: got %d, expected %d", t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
n, size) expectedHash, resultingHash)
}
resultingHash := rd.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
rtest.Assert(t, rawSrc.Traced == (test.innerWriteTo && test.outerWriteTo),
"unexpected/missing writeTo call innerWriteTo %v outerWriteTo %v",
test.innerWriteTo, test.outerWriteTo)
} }
} }
} }