mirror of
https://github.com/octoleo/restic.git
synced 2024-12-28 04:56:04 +00:00
HashingWriter: Track size
This commit is contained in:
parent
b816192d1c
commit
fb2cd7b485
@ -45,6 +45,7 @@ func (h *HashAppendWriter) Write(p []byte) (n int, err error) {
|
|||||||
type HashingWriter struct {
|
type HashingWriter struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
h hash.Hash
|
h hash.Hash
|
||||||
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter {
|
func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter {
|
||||||
@ -55,9 +56,15 @@ func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *HashingWriter) Write(p []byte) (int, error) {
|
func (h *HashingWriter) Write(p []byte) (int, error) {
|
||||||
return h.w.Write(p)
|
n, err := h.w.Write(p)
|
||||||
|
h.size += n
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HashingWriter) Sum(d []byte) []byte {
|
func (h *HashingWriter) Sum(d []byte) []byte {
|
||||||
return h.h.Sum(d)
|
return h.h.Sum(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *HashingWriter) Size() int {
|
||||||
|
return h.size
|
||||||
|
}
|
||||||
|
@ -68,6 +68,10 @@ func TestHashingWriter(t *testing.T) {
|
|||||||
"HashAppendWriter: invalid number of bytes written: got %d, expected %d",
|
"HashAppendWriter: invalid number of bytes written: got %d, expected %d",
|
||||||
n, size)
|
n, size)
|
||||||
|
|
||||||
|
assert(t, wr.Size() == size,
|
||||||
|
"HashAppendWriter: invalid number of bytes returned: got %d, expected %d",
|
||||||
|
wr.Size, size)
|
||||||
|
|
||||||
resultingHash := wr.Sum(nil)
|
resultingHash := wr.Sum(nil)
|
||||||
assert(t, bytes.Equal(expectedHash[:], resultingHash),
|
assert(t, bytes.Equal(expectedHash[:], resultingHash),
|
||||||
"HashAppendWriter: hashes do not match: expected %02x, got %02x",
|
"HashAppendWriter: hashes do not match: expected %02x, got %02x",
|
||||||
|
Loading…
Reference in New Issue
Block a user