2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

backend: use HashingWriter

This commit is contained in:
Alexander Neumann 2015-02-15 23:46:21 +01:00
parent 3dbe02182b
commit 907d401e32
3 changed files with 24 additions and 16 deletions

View File

@ -26,6 +26,7 @@ var (
type Blob interface {
io.WriteCloser
ID() (ID, error)
Size() uint
}
type Lister interface {

View File

@ -3,7 +3,6 @@ package backend
import (
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
@ -183,11 +182,11 @@ func (b *Local) dirname(t Type, id ID) string {
type localBlob struct {
f *os.File
h hash.Hash
tw io.Writer
hw *HashingWriter
backend *Local
tpe Type
id ID
size uint
}
func (lb *localBlob) Close() error {
@ -197,7 +196,7 @@ func (lb *localBlob) Close() error {
}
// get ID
lb.id = ID(lb.h.Sum(nil))
lb.id = ID(lb.hw.Sum(nil))
// check for duplicate ID
res, err := lb.backend.Test(lb.tpe, lb.id)
@ -219,7 +218,9 @@ func (lb *localBlob) Close() error {
}
func (lb *localBlob) Write(p []byte) (int, error) {
return lb.tw.Write(p)
n, err := lb.hw.Write(p)
lb.size += uint(n)
return n, err
}
func (lb *localBlob) ID() (ID, error) {
@ -230,6 +231,10 @@ func (lb *localBlob) ID() (ID, error) {
return lb.id, nil
}
func (lb *localBlob) Size() uint {
return lb.size
}
// Create creates a new blob of type t. Blob implements io.WriteCloser. Once
// Close() has been called, ID() can be used to retrieve the ID. If the blob is
// already present, Close() returns ErrAlreadyPresent.
@ -242,10 +247,9 @@ func (b *Local) Create(t Type) (Blob, error) {
return nil, err
}
h := newHash()
hw := NewHashingWriter(file, newHash())
blob := localBlob{
h: h,
tw: io.MultiWriter(h, file),
hw: hw,
f: file,
backend: b,
tpe: t,

View File

@ -5,7 +5,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"log"
@ -293,11 +292,11 @@ func (r *SFTP) dirname(t Type, id ID) string {
type sftpBlob struct {
f *sftp.File
name string
h hash.Hash
tw io.Writer
hw *HashingWriter
backend *SFTP
tpe Type
id ID
size uint
}
func (sb *sftpBlob) Close() error {
@ -307,7 +306,7 @@ func (sb *sftpBlob) Close() error {
}
// get ID
sb.id = ID(sb.h.Sum(nil))
sb.id = ID(sb.hw.Sum(nil))
// check for duplicate ID
res, err := sb.backend.Test(sb.tpe, sb.id)
@ -329,7 +328,13 @@ func (sb *sftpBlob) Close() error {
}
func (sb *sftpBlob) Write(p []byte) (int, error) {
return sb.tw.Write(p)
n, err := sb.hw.Write(p)
sb.size += uint(n)
return n, err
}
func (sb *sftpBlob) Size() uint {
return sb.size
}
func (sb *sftpBlob) ID() (ID, error) {
@ -352,10 +357,8 @@ func (r *SFTP) Create(t Type) (Blob, error) {
return nil, err
}
h := newHash()
blob := sftpBlob{
h: h,
tw: io.MultiWriter(h, file),
hw: NewHashingWriter(file, newHash()),
f: file,
name: filename,
backend: r,