Remove HashAppendWriter
This commit is contained in:
parent
4fd7676e92
commit
0d5731383f
2 changed files with 0 additions and 77 deletions
|
@ -1,51 +1,10 @@
|
||||||
package backend
|
package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HashAppendWriter struct {
|
|
||||||
w io.Writer
|
|
||||||
origWr io.Writer
|
|
||||||
h hash.Hash
|
|
||||||
sum []byte
|
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHashAppendWriter(w io.Writer, h hash.Hash) *HashAppendWriter {
|
|
||||||
return &HashAppendWriter{
|
|
||||||
h: h,
|
|
||||||
w: io.MultiWriter(w, h),
|
|
||||||
origWr: w,
|
|
||||||
sum: make([]byte, 0, h.Size()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HashAppendWriter) Close() error {
|
|
||||||
if h == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !h.closed {
|
|
||||||
h.closed = true
|
|
||||||
|
|
||||||
_, err := h.origWr.Write(h.h.Sum(nil))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HashAppendWriter) Write(p []byte) (n int, err error) {
|
|
||||||
if !h.closed {
|
|
||||||
return h.w.Write(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, errors.New("Write() called on closed HashAppendWriter")
|
|
||||||
}
|
|
||||||
|
|
||||||
// HashingWriter wraps an io.Writer to hashes all data that is written to it.
|
// HashingWriter wraps an io.Writer to hashes all data that is written to it.
|
||||||
type HashingWriter struct {
|
type HashingWriter struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
|
|
@ -12,42 +12,6 @@ import (
|
||||||
. "github.com/restic/restic/test"
|
. "github.com/restic/restic/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHashAppendWriter(t *testing.T) {
|
|
||||||
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
|
|
||||||
|
|
||||||
for _, size := range tests {
|
|
||||||
data := make([]byte, size)
|
|
||||||
_, err := io.ReadFull(rand.Reader, data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ReadFull: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedHash := sha256.Sum256(data)
|
|
||||||
|
|
||||||
target := bytes.NewBuffer(nil)
|
|
||||||
wr := backend.NewHashAppendWriter(target, sha256.New())
|
|
||||||
|
|
||||||
_, err = wr.Write(data)
|
|
||||||
OK(t, err)
|
|
||||||
OK(t, wr.Close())
|
|
||||||
|
|
||||||
Assert(t, len(target.Bytes()) == size+len(expectedHash),
|
|
||||||
"HashAppendWriter: invalid number of bytes written: got %d, expected %d",
|
|
||||||
len(target.Bytes()), size+len(expectedHash))
|
|
||||||
|
|
||||||
r := target.Bytes()
|
|
||||||
resultingHash := r[len(r)-len(expectedHash):]
|
|
||||||
Assert(t, bytes.Equal(expectedHash[:], resultingHash),
|
|
||||||
"HashAppendWriter: hashes do not match: expected %02x, got %02x",
|
|
||||||
expectedHash, resultingHash)
|
|
||||||
|
|
||||||
// write again, this must return an error
|
|
||||||
_, err = wr.Write([]byte{23})
|
|
||||||
Assert(t, err != nil,
|
|
||||||
"HashAppendWriter: Write() after Close() did not return an error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHashingWriter(t *testing.T) {
|
func TestHashingWriter(t *testing.T) {
|
||||||
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
|
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue