mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
mem: calculate md5 content hash for uploads
The mem backend is primarily used for testing. This ensures that the upload hash calculation gets appropriate test coverage.
This commit is contained in:
parent
a009b39e4c
commit
51b7e3119b
@ -3,6 +3,8 @@ package mem
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/base64"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -87,6 +89,16 @@ func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Re
|
|||||||
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", len(buf), rd.Length())
|
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", len(buf), rd.Length())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
beHash := be.Hasher()
|
||||||
|
// must never fail according to interface
|
||||||
|
_, _ = beHash.Write(buf)
|
||||||
|
if !bytes.Equal(beHash.Sum(nil), rd.Hash()) {
|
||||||
|
return errors.Errorf("invalid file hash or content, got %s expected %s",
|
||||||
|
base64.RawStdEncoding.EncodeToString(beHash.Sum(nil)),
|
||||||
|
base64.RawStdEncoding.EncodeToString(rd.Hash()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
be.data[h] = buf
|
be.data[h] = buf
|
||||||
debug.Log("saved %v bytes at %v", len(buf), h)
|
debug.Log("saved %v bytes at %v", len(buf), h)
|
||||||
|
|
||||||
@ -217,7 +229,7 @@ func (be *MemoryBackend) Location() string {
|
|||||||
|
|
||||||
// Hasher may return a hash function for calculating a content hash for the backend
|
// Hasher may return a hash function for calculating a content hash for the backend
|
||||||
func (be *MemoryBackend) Hasher() hash.Hash {
|
func (be *MemoryBackend) Hasher() hash.Hash {
|
||||||
return nil
|
return md5.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes all data in the backend.
|
// Delete removes all data in the backend.
|
||||||
|
Loading…
Reference in New Issue
Block a user