From 907d401e32eab1215dda49b4e7583bd15e17d4aa Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 15 Feb 2015 23:46:21 +0100 Subject: [PATCH] backend: use HashingWriter --- backend/interface.go | 1 + backend/local.go | 20 ++++++++++++-------- backend/sftp.go | 19 +++++++++++-------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/backend/interface.go b/backend/interface.go index b671609c7..bd2cb401c 100644 --- a/backend/interface.go +++ b/backend/interface.go @@ -26,6 +26,7 @@ var ( type Blob interface { io.WriteCloser ID() (ID, error) + Size() uint } type Lister interface { diff --git a/backend/local.go b/backend/local.go index 5509e034c..5af20e89f 100644 --- a/backend/local.go +++ b/backend/local.go @@ -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, diff --git a/backend/sftp.go b/backend/sftp.go index c98eff480..5961e43f5 100644 --- a/backend/sftp.go +++ b/backend/sftp.go @@ -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,