2015-05-09 21:52:03 +00:00
|
|
|
package repository
|
2014-12-21 16:02:49 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-26 15:44:38 +00:00
|
|
|
"bytes"
|
2015-05-03 15:37:12 +00:00
|
|
|
"crypto/rand"
|
2015-02-15 23:24:58 +00:00
|
|
|
"crypto/sha256"
|
2015-05-03 15:37:12 +00:00
|
|
|
"encoding/hex"
|
2015-01-10 22:40:10 +00:00
|
|
|
"encoding/json"
|
2014-12-21 16:02:49 +00:00
|
|
|
"errors"
|
2015-01-10 22:40:10 +00:00
|
|
|
"fmt"
|
2015-02-08 21:54:45 +00:00
|
|
|
"io"
|
2015-03-28 10:50:23 +00:00
|
|
|
"io/ioutil"
|
2015-04-26 15:44:38 +00:00
|
|
|
"sync"
|
2014-12-21 16:02:49 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
2015-04-05 22:22:19 +00:00
|
|
|
"github.com/restic/restic/chunker"
|
2015-05-03 16:04:13 +00:00
|
|
|
"github.com/restic/restic/crypto"
|
2015-04-26 15:44:38 +00:00
|
|
|
"github.com/restic/restic/debug"
|
|
|
|
"github.com/restic/restic/pack"
|
2014-12-21 16:02:49 +00:00
|
|
|
)
|
|
|
|
|
2015-05-03 15:37:12 +00:00
|
|
|
// Config contains the configuration for a repository.
|
|
|
|
type Config struct {
|
|
|
|
Version uint `json:"version"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"`
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
// Repository is used to access a repository in a backend.
|
|
|
|
type Repository struct {
|
2015-05-03 16:04:13 +00:00
|
|
|
be backend.Backend
|
|
|
|
Config Config
|
|
|
|
key *crypto.Key
|
|
|
|
keyName string
|
|
|
|
idx *Index
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
pm sync.Mutex
|
|
|
|
packs []*pack.Packer
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func New(be backend.Backend) *Repository {
|
|
|
|
return &Repository{
|
2015-04-26 15:44:38 +00:00
|
|
|
be: be,
|
|
|
|
idx: NewIndex(),
|
|
|
|
}
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Find loads the list of all blobs of type t and searches for names which start
|
2014-12-21 16:02:49 +00:00
|
|
|
// with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. If
|
|
|
|
// more than one is found, nil and ErrMultipleIDMatches is returned.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Find(t backend.Type, prefix string) (string, error) {
|
2014-12-21 16:02:49 +00:00
|
|
|
return backend.Find(s.be, t, prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixLength returns the number of bytes required so that all prefixes of
|
|
|
|
// all IDs of type t are unique.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) PrefixLength(t backend.Type) (int, error) {
|
2014-12-21 16:02:49 +00:00
|
|
|
return backend.PrefixLength(s.be, t)
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// Load tries to load and decrypt content identified by t and id from the
|
|
|
|
// backend.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Load(t backend.Type, id backend.ID) ([]byte, error) {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Load", "load %v with id %v", t, id.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// load blob from pack
|
|
|
|
rd, err := s.be.Get(t, id.String())
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Load", "error loading %v: %v", id.Str(), err)
|
2015-03-28 10:50:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(rd)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
err = rd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// check hash
|
2015-04-26 15:44:38 +00:00
|
|
|
if !backend.Hash(buf).Equal(id) {
|
2015-03-28 10:50:23 +00:00
|
|
|
return nil, errors.New("invalid data returned")
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// decrypt
|
|
|
|
plain, err := s.Decrypt(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return plain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBlob tries to load and decrypt content identified by t and id from a
|
|
|
|
// pack from the backend.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) LoadBlob(t pack.BlobType, id backend.ID) ([]byte, error) {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "load %v with id %v", t, id.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
// lookup pack
|
|
|
|
packID, tpe, offset, length, err := s.idx.Lookup(id)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "id %v not found in index: %v", id.Str(), err)
|
2015-01-10 22:40:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
if tpe != t {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "wrong type returned for %v: wanted %v, got %v", id.Str(), t, tpe)
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, fmt.Errorf("blob has wrong type %v (wanted: %v)", tpe, t)
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "id %v found in pack %v at offset %v (length %d)", id.Str(), packID.Str(), offset, length)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// load blob from pack
|
|
|
|
rd, err := s.be.GetReader(backend.Data, packID.String(), offset, length)
|
|
|
|
if err != nil {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "error loading pack %v for %v: %v", packID.Str(), id.Str(), err)
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
buf, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
|
|
|
plain, err := s.Decrypt(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check hash
|
|
|
|
if !backend.Hash(plain).Equal(id) {
|
|
|
|
return nil, errors.New("invalid data returned")
|
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return plain, nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 18:39:45 +00:00
|
|
|
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
|
2015-04-26 15:44:38 +00:00
|
|
|
// the item.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) error {
|
2015-04-26 15:44:38 +00:00
|
|
|
// load blob from backend
|
|
|
|
rd, err := s.be.Get(t, id.String())
|
2015-03-28 14:07:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-26 15:44:38 +00:00
|
|
|
defer rd.Close()
|
2015-03-28 14:07:08 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// decrypt
|
2015-05-03 16:04:13 +00:00
|
|
|
decryptRd, err := crypto.DecryptFrom(s.key, rd)
|
2015-04-26 15:44:38 +00:00
|
|
|
defer decryptRd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode
|
|
|
|
decoder := json.NewDecoder(decryptRd)
|
|
|
|
err = decoder.Decode(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// LoadJSONPack calls LoadBlob() to load a blob from the backend, decrypt the
|
|
|
|
// data and afterwards call json.Unmarshal on the item.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) error {
|
2015-04-26 15:44:38 +00:00
|
|
|
// lookup pack
|
|
|
|
packID, _, offset, length, err := s.idx.Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load blob from pack
|
|
|
|
rd, err := s.be.GetReader(backend.Data, packID.String(), offset, length)
|
2015-02-15 22:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-21 23:21:56 +00:00
|
|
|
defer rd.Close()
|
2015-02-15 22:48:59 +00:00
|
|
|
|
|
|
|
// decrypt
|
2015-05-03 16:04:13 +00:00
|
|
|
decryptRd, err := crypto.DecryptFrom(s.key, rd)
|
2015-02-17 22:37:45 +00:00
|
|
|
defer decryptRd.Close()
|
2015-02-15 22:48:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode
|
2015-03-02 08:56:56 +00:00
|
|
|
decoder := json.NewDecoder(decryptRd)
|
2015-02-15 22:48:59 +00:00
|
|
|
err = decoder.Decode(item)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-15 22:48:59 +00:00
|
|
|
return nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
const minPackSize = 4 * chunker.MiB
|
|
|
|
const maxPackSize = 16 * chunker.MiB
|
|
|
|
const maxPackers = 200
|
|
|
|
|
|
|
|
// findPacker returns a packer for a new blob of size bytes. Either a new one is
|
|
|
|
// created or one is returned that already has some blobs.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) findPacker(size uint) (*pack.Packer, error) {
|
2015-04-26 15:44:38 +00:00
|
|
|
s.pm.Lock()
|
|
|
|
defer s.pm.Unlock()
|
|
|
|
|
|
|
|
// search for a suitable packer
|
|
|
|
if len(s.packs) > 0 {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.findPacker", "searching packer for %d bytes\n", size)
|
2015-04-26 15:44:38 +00:00
|
|
|
for i, p := range s.packs {
|
|
|
|
if p.Size()+size < maxPackSize {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.findPacker", "found packer %v", p)
|
2015-04-26 15:44:38 +00:00
|
|
|
// remove from list
|
|
|
|
s.packs = append(s.packs[:i], s.packs[i+1:]...)
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no suitable packer found, return new
|
|
|
|
blob, err := s.be.Create()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.findPacker", "create new pack %p", blob)
|
2015-05-03 16:04:13 +00:00
|
|
|
return pack.NewPacker(s.key, blob), nil
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertPacker appends p to s.packs.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) insertPacker(p *pack.Packer) {
|
2015-04-26 15:44:38 +00:00
|
|
|
s.pm.Lock()
|
|
|
|
defer s.pm.Unlock()
|
|
|
|
|
|
|
|
s.packs = append(s.packs, p)
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.insertPacker", "%d packers\n", len(s.packs))
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// savePacker stores p in the backend.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) savePacker(p *pack.Packer) error {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.savePacker", "save packer with %d blobs\n", p.Count())
|
2015-04-26 15:44:38 +00:00
|
|
|
_, err := p.Finalize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// move file to the final location
|
|
|
|
sid := p.ID()
|
|
|
|
err = p.Writer().(backend.Blob).Finalize(backend.Data, sid.String())
|
|
|
|
if err != nil {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.savePacker", "blob Finalize() error: %v", err)
|
2015-04-26 15:44:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.savePacker", "saved as %v", sid.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// update blobs in the index
|
|
|
|
for _, b := range p.Blobs() {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.savePacker", " updating blob %v to pack %v", b.ID.Str(), sid.Str())
|
2015-04-26 15:44:38 +00:00
|
|
|
s.idx.Store(b.Type, b.ID, sid, b.Offset, uint(b.Length))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// countPacker returns the number of open (unfinished) packers.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) countPacker() int {
|
2015-04-26 15:44:38 +00:00
|
|
|
s.pm.Lock()
|
|
|
|
defer s.pm.Unlock()
|
|
|
|
|
|
|
|
return len(s.packs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save encrypts data and stores it to the backend as type t. If data is small
|
|
|
|
// enough, it will be packed together with other small blobs.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Save(t pack.BlobType, data []byte, id backend.ID) (backend.ID, error) {
|
2015-01-10 22:40:10 +00:00
|
|
|
if id == nil {
|
|
|
|
// compute plaintext hash
|
|
|
|
id = backend.Hash(data)
|
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Save", "save id %v (%v, %d bytes)", id.Str(), t, len(data))
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get buf from the pool
|
2015-04-26 12:46:15 +00:00
|
|
|
ciphertext := getBuf()
|
|
|
|
defer freeBuf(ciphertext)
|
2015-01-10 22:40:10 +00:00
|
|
|
|
|
|
|
// encrypt blob
|
2015-04-12 18:58:41 +00:00
|
|
|
ciphertext, err := s.Encrypt(ciphertext, data)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// find suitable packer and add blob
|
|
|
|
packer, err := s.findPacker(uint(len(ciphertext)))
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// save ciphertext
|
|
|
|
packer.Add(t, id, bytes.NewReader(ciphertext))
|
|
|
|
|
|
|
|
// add this id to the index, although we don't know yet in which pack it
|
|
|
|
// will be saved, the entry will be updated when the pack is written.
|
|
|
|
s.idx.Store(t, id, nil, 0, 0)
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Save", "saving stub for %v (%v) in index", id.Str, t)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// if the pack is not full enough and there are less than maxPackers
|
|
|
|
// packers, put back to the list
|
|
|
|
if packer.Size() < minPackSize && s.countPacker() < maxPackers {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Save", "pack is not full enough (%d bytes)", packer.Size())
|
2015-04-26 15:44:38 +00:00
|
|
|
s.insertPacker(packer)
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// else write the pack to the backend
|
|
|
|
return id, s.savePacker(packer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveFrom encrypts data read from rd and stores it in a pack in the backend as type t.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SaveFrom(t pack.BlobType, id backend.ID, length uint, rd io.Reader) error {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveFrom", "save id %v (%v, %d bytes)", id.Str(), t, length)
|
2015-04-26 15:44:38 +00:00
|
|
|
if id == nil {
|
|
|
|
return errors.New("id is nil")
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
buf, err := ioutil.ReadAll(rd)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return err
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
_, err = s.Save(t, buf, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// SaveJSON serialises item as JSON and encrypts and saves it in a pack in the
|
|
|
|
// backend as type t.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SaveJSON(t pack.BlobType, item interface{}) (backend.ID, error) {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveJSON", "save %v blob", t)
|
2015-04-26 15:44:38 +00:00
|
|
|
buf := getBuf()[:0]
|
|
|
|
defer freeBuf(buf)
|
|
|
|
|
|
|
|
wr := bytes.NewBuffer(buf)
|
|
|
|
|
|
|
|
enc := json.NewEncoder(wr)
|
|
|
|
err := enc.Encode(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("json.Encode: %v", err)
|
2015-02-08 21:54:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
buf = wr.Bytes()
|
|
|
|
return s.Save(t, buf, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
|
|
|
|
// backend as type t, without a pack. It returns the storage hash.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend.ID, error) {
|
2015-05-03 15:37:12 +00:00
|
|
|
// create file
|
2015-04-26 15:44:38 +00:00
|
|
|
blob, err := s.be.Create()
|
2015-02-08 21:54:45 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-08 21:54:45 +00:00
|
|
|
}
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveJSONUnpacked", "create new file %p", blob)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// hash
|
|
|
|
hw := backend.NewHashingWriter(blob, sha256.New())
|
2015-02-08 21:54:45 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// encrypt blob
|
2015-05-03 16:04:13 +00:00
|
|
|
ewr := crypto.EncryptTo(s.key, hw)
|
2015-02-08 21:54:45 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
enc := json.NewEncoder(ewr)
|
|
|
|
err = enc.Encode(item)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, fmt.Errorf("json.Encode: %v", err)
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
err = ewr.Close()
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// finalize blob in the backend
|
2015-03-28 10:50:23 +00:00
|
|
|
sid := backend.ID(hw.Sum(nil))
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
err = blob.Finalize(t, sid.String())
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return sid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush saves all remaining packs.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Flush() error {
|
2015-04-26 15:44:38 +00:00
|
|
|
s.pm.Lock()
|
|
|
|
defer s.pm.Unlock()
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Flush", "manually flushing %d packs", len(s.packs))
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
for _, p := range s.packs {
|
|
|
|
err := s.savePacker(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.packs = s.packs[:0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Backend() backend.Backend {
|
2015-04-26 15:44:38 +00:00
|
|
|
return s.be
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Index() *Index {
|
2015-04-26 15:44:38 +00:00
|
|
|
return s.idx
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:25:52 +00:00
|
|
|
// SetIndex instructs the repository to use the given index.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SetIndex(i *Index) {
|
2015-04-26 15:44:38 +00:00
|
|
|
s.idx = i
|
2015-02-08 21:54:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// SaveIndex saves all new packs in the index in the backend, returned is the
|
|
|
|
// storage ID.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SaveIndex() (backend.ID, error) {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveIndex", "Saving index")
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
// create blob
|
|
|
|
blob, err := s.be.Create()
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 23:24:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveIndex", "create new pack %p", blob)
|
2015-02-15 23:24:58 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// hash
|
|
|
|
hw := backend.NewHashingWriter(blob, sha256.New())
|
|
|
|
|
|
|
|
// encrypt blob
|
2015-05-03 16:04:13 +00:00
|
|
|
ewr := crypto.EncryptTo(s.key, hw)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
err = s.idx.Encode(ewr)
|
2015-02-15 23:24:58 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 23:24:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
err = ewr.Close()
|
2015-02-15 23:24:58 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 23:24:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// finalize blob in the backend
|
|
|
|
sid := backend.ID(hw.Sum(nil))
|
|
|
|
|
|
|
|
err = blob.Finalize(backend.Index, sid.String())
|
2015-02-15 23:24:58 +00:00
|
|
|
if err != nil {
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
2015-02-15 23:24:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.SaveIndex", "Saved index as %v", sid.Str())
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return sid, nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// LoadIndex loads all index files from the backend and merges them with the
|
|
|
|
// current index.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) LoadIndex() error {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadIndex", "Loading index")
|
2015-04-26 15:44:38 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
for id := range s.be.List(backend.Index, done) {
|
|
|
|
err := s.loadIndex(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadIndex loads the index id and merges it with the currently used index.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) loadIndex(id string) error {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.loadIndex", "Loading index %v", id[:8])
|
2015-04-26 15:44:38 +00:00
|
|
|
before := len(s.idx.pack)
|
|
|
|
|
|
|
|
rd, err := s.be.Get(backend.Index, id)
|
|
|
|
defer rd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
2015-05-03 16:04:13 +00:00
|
|
|
decryptRd, err := crypto.DecryptFrom(s.key, rd)
|
2015-04-26 15:44:38 +00:00
|
|
|
defer decryptRd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, err := DecodeIndex(decryptRd)
|
|
|
|
if err != nil {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.loadIndex", "error while decoding index %v: %v", id, err)
|
2015-04-26 15:44:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.idx.Merge(idx)
|
|
|
|
|
|
|
|
after := len(s.idx.pack)
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.loadIndex", "Loaded index %v, added %v blobs", id[:8], after-before)
|
2015-04-26 15:44:38 +00:00
|
|
|
|
|
|
|
return nil
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 15:37:12 +00:00
|
|
|
const repositoryIDSize = sha256.Size
|
2015-05-09 15:41:28 +00:00
|
|
|
const RepoVersion = 1
|
2015-05-03 15:37:12 +00:00
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func createConfig(s *Repository) (err error) {
|
2015-05-03 15:37:12 +00:00
|
|
|
s.Config.ChunkerPolynomial, err = chunker.RandomPolynomial()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newID := make([]byte, repositoryIDSize)
|
|
|
|
_, err = io.ReadFull(rand.Reader, newID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Config.ID = hex.EncodeToString(newID)
|
2015-05-09 15:41:28 +00:00
|
|
|
s.Config.Version = RepoVersion
|
2015-05-03 15:37:12 +00:00
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.createConfig", "New config: %#v", s.Config)
|
2015-05-03 15:37:12 +00:00
|
|
|
|
|
|
|
_, err = s.SaveJSONUnpacked(backend.Config, s.Config)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) loadConfig(cfg *Config) error {
|
2015-05-03 15:42:01 +00:00
|
|
|
err := s.LoadJSONUnpacked(backend.Config, nil, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 15:41:28 +00:00
|
|
|
if cfg.Version != RepoVersion {
|
2015-05-04 18:40:02 +00:00
|
|
|
return errors.New("unsupported repository version")
|
|
|
|
}
|
|
|
|
|
2015-05-03 15:42:01 +00:00
|
|
|
if !cfg.ChunkerPolynomial.Irreducible() {
|
|
|
|
return errors.New("invalid chunker polynomial")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-05-03 15:37:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 18:39:45 +00:00
|
|
|
// SearchKey finds a key with the supplied password, afterwards the config is
|
|
|
|
// read and parsed.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) SearchKey(password string) error {
|
2015-04-26 12:46:15 +00:00
|
|
|
key, err := SearchKey(s, password)
|
2014-12-21 17:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:07:21 +00:00
|
|
|
s.key = key.master
|
2015-05-03 16:04:13 +00:00
|
|
|
s.keyName = key.Name()
|
2015-05-03 15:37:12 +00:00
|
|
|
return s.loadConfig(&s.Config)
|
2015-05-03 14:36:52 +00:00
|
|
|
}
|
2014-12-21 17:10:19 +00:00
|
|
|
|
2015-05-04 18:40:17 +00:00
|
|
|
// Init creates a new master key with the supplied password and initializes the
|
|
|
|
// repository config.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Init(password string) error {
|
2015-05-17 18:51:32 +00:00
|
|
|
has, err := s.be.Test(backend.Config, "")
|
2015-05-03 15:46:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return errors.New("repository master key and config already initialized")
|
|
|
|
}
|
|
|
|
|
2015-05-03 15:37:12 +00:00
|
|
|
key, err := createMasterKey(s, password)
|
2015-05-03 14:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:07:21 +00:00
|
|
|
s.key = key.master
|
2015-05-03 16:04:13 +00:00
|
|
|
s.keyName = key.Name()
|
2015-05-04 18:40:17 +00:00
|
|
|
return createConfig(s)
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Decrypt(ciphertext []byte) ([]byte, error) {
|
2014-12-21 17:10:19 +00:00
|
|
|
if s.key == nil {
|
2015-05-09 11:25:52 +00:00
|
|
|
return nil, errors.New("key for repository not set")
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:04:13 +00:00
|
|
|
return crypto.Decrypt(s.key, nil, ciphertext)
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Encrypt(ciphertext, plaintext []byte) ([]byte, error) {
|
2014-12-21 17:10:19 +00:00
|
|
|
if s.key == nil {
|
2015-05-09 11:25:52 +00:00
|
|
|
return nil, errors.New("key for repository not set")
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:04:13 +00:00
|
|
|
return crypto.Encrypt(s.key, ciphertext, plaintext)
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Key() *crypto.Key {
|
2014-12-21 17:10:19 +00:00
|
|
|
return s.key
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) KeyName() string {
|
2015-05-03 16:04:13 +00:00
|
|
|
return s.keyName
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Count returns the number of blobs of a given type in the backend.
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Count(t backend.Type) (n uint) {
|
2015-04-26 15:44:38 +00:00
|
|
|
for _ = range s.be.List(t, nil) {
|
2015-03-28 10:50:23 +00:00
|
|
|
n++
|
2015-02-21 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
return
|
2015-02-21 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 18:48:59 +00:00
|
|
|
func (s *Repository) list(t backend.Type, done <-chan struct{}, out chan<- backend.ID) {
|
|
|
|
defer close(out)
|
|
|
|
in := s.be.List(t, done)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// disable sending on the outCh until we received a job
|
|
|
|
outCh chan<- backend.ID
|
|
|
|
// enable receiving from in
|
|
|
|
inCh = in
|
|
|
|
id backend.ID
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case strID, ok := <-inCh:
|
|
|
|
if !ok {
|
|
|
|
// input channel closed, we're done
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err = backend.ParseID(strID)
|
|
|
|
if err != nil {
|
|
|
|
// ignore invalid IDs
|
|
|
|
continue
|
|
|
|
}
|
2014-12-21 16:02:49 +00:00
|
|
|
|
2015-05-17 18:48:59 +00:00
|
|
|
inCh = nil
|
|
|
|
outCh = out
|
|
|
|
case outCh <- id:
|
|
|
|
outCh = nil
|
|
|
|
inCh = in
|
|
|
|
}
|
|
|
|
}
|
2015-04-26 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 18:48:59 +00:00
|
|
|
func (s *Repository) List(t backend.Type, done <-chan struct{}) <-chan backend.ID {
|
|
|
|
outCh := make(chan backend.ID)
|
|
|
|
|
|
|
|
go s.list(t, done, outCh)
|
|
|
|
|
|
|
|
return outCh
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func (s *Repository) Delete() error {
|
2014-12-21 16:02:49 +00:00
|
|
|
if b, ok := s.be.(backend.Deleter); ok {
|
|
|
|
return b.Delete()
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Delete() called for backend that does not implement this method")
|
|
|
|
}
|
2015-03-14 10:56:45 +00:00
|
|
|
|
2015-05-17 18:55:24 +00:00
|
|
|
func (s *Repository) Close() error {
|
|
|
|
return s.be.Close()
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|