2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 06:46:34 +00:00

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
writerTo io.WriterTo
}
// NewReader returns a new ReadSummer that uses the hash h. If the underlying
// reader supports WriteTo then the returned reader will do so too. // reader supports WriteTo then the returned reader will do so too.
func NewReader(r io.Reader, h hash.Hash) ReadSumer { func NewReader(r io.Reader, h hash.Hash) *Reader {
rs := reader{ return &Reader{r: r, h: h}
Reader: io.TeeReader(r, h),
h: h,
} }
if _, ok := r.(io.WriterTo); ok { func (h *Reader) Read(p []byte) (int, error) {
return &readWriterTo{ n, err := h.r.Read(p)
reader: rs, _, _ = h.h.Write(p[:n]) // Never returns an error.
writerTo: r.(io.WriterTo), return n, err
}
}
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,25 +21,8 @@ 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}} {
// test both code paths in WriteTo
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())
// test both Read and WriteTo
outerSrc := io.Reader(rd)
if !test.outerWriteTo {
outerSrc = &onlyReader{Reader: outerSrc}
}
n, err := io.Copy(ioutil.Discard, outerSrc)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -73,11 +38,6 @@ func TestReader(t *testing.T) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x", t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash) expectedHash, resultingHash)
} }
rtest.Assert(t, rawSrc.Traced == (test.innerWriteTo && test.outerWriteTo),
"unexpected/missing writeTo call innerWriteTo %v outerWriteTo %v",
test.innerWriteTo, test.outerWriteTo)
}
} }
} }