2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-16 01:57:10 +00:00
restic/contenthandler.go

193 lines
4.0 KiB
Go
Raw Normal View History

2014-09-23 20:39:12 +00:00
package khepri
import (
"encoding/json"
"errors"
2014-11-21 20:21:44 +00:00
"fmt"
2014-09-23 20:39:12 +00:00
"github.com/fd0/khepri/backend"
)
type ContentHandler struct {
be backend.Server
key *Key
2014-11-21 20:21:44 +00:00
bl *BlobList
2014-09-23 20:39:12 +00:00
}
// NewContentHandler creates a new content handler.
func NewContentHandler(be backend.Server, key *Key) (*ContentHandler, error) {
ch := &ContentHandler{
2014-11-21 20:21:44 +00:00
be: be,
key: key,
bl: NewBlobList(),
2014-09-23 20:39:12 +00:00
}
return ch, nil
}
2014-10-05 12:44:59 +00:00
// LoadSnapshot adds all blobs from a snapshot into the content handler and returns the snapshot.
2014-09-23 20:39:12 +00:00
func (ch *ContentHandler) LoadSnapshot(id backend.ID) (*Snapshot, error) {
sn, err := LoadSnapshot(ch, id)
if err != nil {
return nil, err
}
2014-11-21 20:21:44 +00:00
ch.bl.Merge(sn.BlobList)
2014-09-23 20:39:12 +00:00
return sn, nil
}
// LoadAllSnapshots adds all blobs from all snapshots that can be decrypted
// into the content handler.
func (ch *ContentHandler) LoadAllSnapshots() error {
// add all maps from all snapshots that can be decrypted to the storage map
err := backend.EachID(ch.be, backend.Snapshot, func(id backend.ID) {
sn, err := LoadSnapshot(ch, id)
if err != nil {
return
}
2014-11-16 21:50:20 +00:00
2014-11-21 20:21:44 +00:00
ch.bl.Merge(sn.BlobList)
2014-09-23 20:39:12 +00:00
})
if err != nil {
return err
}
return nil
}
// Save encrypts data and stores it to the backend as type t. If the data was
// already saved before, the blob is returned.
2014-11-21 20:21:44 +00:00
func (ch *ContentHandler) Save(t backend.Type, data []byte) (Blob, error) {
2014-09-23 20:39:12 +00:00
// compute plaintext hash
id := backend.Hash(data)
// test if the hash is already in the backend
2014-11-21 20:21:44 +00:00
blob, err := ch.bl.Find(Blob{ID: id})
if err == nil {
2014-09-23 20:39:12 +00:00
return blob, nil
}
// else create a new blob
2014-11-21 20:21:44 +00:00
blob = Blob{
2014-09-23 20:39:12 +00:00
ID: id,
Size: uint64(len(data)),
}
// encrypt blob
ciphertext, err := ch.key.Encrypt(data)
if err != nil {
2014-11-21 20:21:44 +00:00
return Blob{}, err
2014-09-23 20:39:12 +00:00
}
// save blob
sid, err := ch.be.Create(t, ciphertext)
if err != nil {
2014-11-21 20:21:44 +00:00
return Blob{}, err
2014-09-23 20:39:12 +00:00
}
blob.Storage = sid
blob.StorageSize = uint64(len(ciphertext))
// insert blob into the storage map
2014-11-21 20:21:44 +00:00
ch.bl.Insert(blob)
2014-09-23 20:39:12 +00:00
return blob, nil
}
// SaveJSON serialises item as JSON and uses Save() to store it to the backend as type t.
2014-11-21 20:21:44 +00:00
func (ch *ContentHandler) SaveJSON(t backend.Type, item interface{}) (Blob, error) {
2014-09-23 20:39:12 +00:00
// convert to json
data, err := json.Marshal(item)
if err != nil {
2014-11-21 20:21:44 +00:00
return Blob{}, err
2014-09-23 20:39:12 +00:00
}
// compress and save data
return ch.Save(t, backend.Compress(data))
}
// Load tries to load and decrypt content identified by t and id from the backend.
func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
if t == backend.Snapshot {
// load data
buf, err := ch.be.Get(t, id)
if err != nil {
return nil, err
}
// decrypt
buf, err = ch.key.Decrypt(buf)
if err != nil {
return nil, err
}
return buf, nil
}
// lookup storage hash
2014-11-21 20:21:44 +00:00
blob, err := ch.bl.Find(Blob{ID: id})
if err != nil {
return nil, fmt.Errorf("Storage ID %s not found", id)
2014-09-23 20:39:12 +00:00
}
// load data
buf, err := ch.be.Get(t, blob.Storage)
if err != nil {
return nil, err
}
// check length
if len(buf) != int(blob.StorageSize) {
return nil, errors.New("Invalid storage length")
}
// decrypt
buf, err = ch.key.Decrypt(buf)
if err != nil {
return nil, err
}
// check length
if len(buf) != int(blob.Size) {
return nil, errors.New("Invalid length")
}
return buf, nil
}
// LoadJSON calls Load() to get content from the backend and afterwards calls
// json.Unmarshal on the item.
func (ch *ContentHandler) LoadJSON(t backend.Type, id backend.ID, item interface{}) error {
// load from backend
buf, err := ch.Load(t, id)
if err != nil {
return err
}
// inflate and unmarshal
err = json.Unmarshal(backend.Uncompress(buf), item)
return err
}
// LoadJSONRaw loads data with the given storage id and type from the backend,
// decrypts it and calls json.Unmarshal on the item.
func (ch *ContentHandler) LoadJSONRaw(t backend.Type, id backend.ID, item interface{}) error {
// load data
buf, err := ch.be.Get(t, id)
if err != nil {
return err
}
// decrypt
buf, err = ch.key.Decrypt(buf)
if err != nil {
return err
}
// inflate and unmarshal
err = json.Unmarshal(backend.Uncompress(buf), item)
return err
}