Skip to content

Commit

Permalink
backends/b2: Utilizing SHA1 hash metadata for uploads now.
Browse files Browse the repository at this point in the history
  • Loading branch information
someone1 committed Jul 25, 2018
1 parent 9b059e1 commit 583158a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backends/backblaze_b2_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (b *B2Backend) Upload(ctx context.Context, vol *helpers.VolumeInfo) error {
w.ChunkSize = b.conf.UploadChunkSize
w.Resume = true

sha1Opt := b2.WithAttrsOption(&b2.Attrs{SHA1: vol.SHA1Sum})
sha1Opt(w)

if _, err := io.Copy(w, vol); err != nil {
w.Close()
helpers.AppLogger.Debugf("b2 backend: Error while uploading volume %s - %v", vol.ObjectName, err)
Expand Down
11 changes: 10 additions & 1 deletion helpers/volumeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"bufio"
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
Expand Down Expand Up @@ -68,6 +69,8 @@ type VolumeInfo struct {
SHA256 hash.Hash `json:"-"`
MD5 hash.Hash `json:"-"`
CRC32C hash.Hash32 `json:"-"`
SHA1 hash.Hash `json:"-"`
SHA1Sum string `json:"-"`
SHA256Sum string
MD5Sum string
CRC32CSum32 uint32
Expand Down Expand Up @@ -366,6 +369,11 @@ func (v *VolumeInfo) Close() error {
v.MD5 = nil
}

if v.SHA1 != nil {
v.SHA1Sum = fmt.Sprintf("%x", v.SHA1.Sum(nil))
v.SHA1 = nil
}

v.w = nil
if v.pr == nil {
v.r = nil
Expand Down Expand Up @@ -527,6 +535,7 @@ func CreateSimpleVolume(ctx context.Context, pipe bool) (*VolumeInfo, error) {
SHA256: sha256.New(),
CRC32C: crc32.New(crc32.MakeTable(crc32.Castagnoli)),
MD5: md5.New(),
SHA1: sha1.New(),
CreateTime: time.Now(),
}

Expand Down Expand Up @@ -554,7 +563,7 @@ func CreateSimpleVolume(ctx context.Context, pipe bool) (*VolumeInfo, error) {
v.w = v.bufw

// Compute hashes
v.w = io.MultiWriter(v.w, v.SHA256, v.CRC32C, v.MD5)
v.w = io.MultiWriter(v.w, v.SHA256, v.CRC32C, v.MD5, v.SHA1)

// Add a writer that counts how many bytes have been written
v.counter = datacounter.NewWriterCounter(v.w)
Expand Down

0 comments on commit 583158a

Please sign in to comment.