From a009b39e4c2577f033d51efa866d92889aa44320 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 19 Dec 2020 12:49:58 +0100 Subject: [PATCH] gs/swift: calculate md5 content hash for upload --- internal/backend/gs/gs.go | 4 +++- internal/backend/swift/swift.go | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 30bb0b4ee..130c86112 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -3,6 +3,7 @@ package gs import ( "context" + "crypto/md5" "hash" "io" "net/http" @@ -191,7 +192,7 @@ func (be *Backend) Location() string { // Hasher may return a hash function for calculating a content hash for the backend func (be *Backend) Hasher() hash.Hash { - return nil + return md5.New() } // Path returns the path in the bucket that is used for this backend. @@ -240,6 +241,7 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe // uploads are not providing significant benefit anyways. w := be.bucket.Object(objName).NewWriter(ctx) w.ChunkSize = 0 + w.MD5 = rd.Hash() wbytes, err := io.Copy(w, rd) cerr := w.Close() if err == nil { diff --git a/internal/backend/swift/swift.go b/internal/backend/swift/swift.go index 02a246203..1fc1bcc26 100644 --- a/internal/backend/swift/swift.go +++ b/internal/backend/swift/swift.go @@ -2,6 +2,8 @@ package swift import ( "context" + "crypto/md5" + "encoding/hex" "fmt" "hash" "io" @@ -118,7 +120,7 @@ func (be *beSwift) Location() string { // Hasher may return a hash function for calculating a content hash for the backend func (be *beSwift) Hasher() hash.Hash { - return nil + return md5.New() } // Load runs fn with a reader that yields the contents of the file at h at the @@ -184,7 +186,7 @@ func (be *beSwift) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe debug.Log("PutObject(%v, %v, %v)", be.container, objName, encoding) hdr := swift.Headers{"Content-Length": strconv.FormatInt(rd.Length(), 10)} - _, err := be.conn.ObjectPut(be.container, objName, rd, true, "", encoding, hdr) + _, err := be.conn.ObjectPut(be.container, objName, rd, true, hex.EncodeToString(rd.Hash()), encoding, hdr) // swift does not return the upload length debug.Log("%v, err %#v", objName, err)