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 { type Blob interface {
io.WriteCloser io.WriteCloser
ID() (ID, error) ID() (ID, error)
Size() uint
} }
type Lister interface { type Lister interface {

View File

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

View File

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