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-01-10 22:40:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-02-08 21:54:45 +00:00
|
|
|
"io"
|
2015-07-26 19:58:03 +00:00
|
|
|
"os"
|
2016-08-31 18:29:54 +00:00
|
|
|
"restic"
|
2014-12-21 16:02:49 +00:00
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend"
|
|
|
|
"restic/crypto"
|
|
|
|
"restic/debug"
|
|
|
|
"restic/pack"
|
2014-12-21 16:02:49 +00:00
|
|
|
)
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
// Repository is used to access a repository in a backend.
|
|
|
|
type Repository struct {
|
2016-08-31 18:29:54 +00:00
|
|
|
be restic.Backend
|
2016-08-31 20:39:36 +00:00
|
|
|
cfg restic.Config
|
2015-05-03 16:04:13 +00:00
|
|
|
key *crypto.Key
|
|
|
|
keyName string
|
2015-10-12 20:34:12 +00:00
|
|
|
idx *MasterIndex
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
*packerManager
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// New returns a new repository with backend be.
|
2016-08-31 18:29:54 +00:00
|
|
|
func New(be restic.Backend) *Repository {
|
2016-03-06 11:26:25 +00:00
|
|
|
repo := &Repository{
|
|
|
|
be: be,
|
|
|
|
idx: NewMasterIndex(),
|
2016-03-06 13:20:48 +00:00
|
|
|
packerManager: newPackerManager(be, nil),
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:14:06 +00:00
|
|
|
return repo
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 20:51:35 +00:00
|
|
|
// Config returns the repository configuration.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (r *Repository) Config() restic.Config {
|
|
|
|
return r.cfg
|
|
|
|
}
|
|
|
|
|
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.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) Find(t restic.FileType, prefix string) (string, error) {
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.Find(r.be, t, prefix)
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixLength returns the number of bytes required so that all prefixes of
|
|
|
|
// all IDs of type t are unique.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) PrefixLength(t restic.FileType) (int, error) {
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.PrefixLength(r.be, t)
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:44:09 +00:00
|
|
|
// LoadAndDecrypt loads and decrypts data identified by t and id from the
|
2015-04-26 15:44:38 +00:00
|
|
|
// backend.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) LoadAndDecrypt(t restic.FileType, id restic.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
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: t, Name: id.String()}
|
2016-01-23 23:12:09 +00:00
|
|
|
buf, err := backend.LoadAll(r.be, h, nil)
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
if t != restic.ConfigFile && !restic.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
|
2015-07-02 19:52:57 +00:00
|
|
|
plain, err := r.Decrypt(buf)
|
2015-04-26 15:44:38 +00:00
|
|
|
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
|
2015-07-26 16:00:53 +00:00
|
|
|
// pack from the backend, the result is stored in plaintextBuf, which must be
|
|
|
|
// large enough to hold the complete blob.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (r *Repository) LoadBlob(id restic.ID, t restic.BlobType, plaintextBuf []byte) ([]byte, error) {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "load %v with id %v", t, id.Str())
|
2016-08-03 20:38:05 +00:00
|
|
|
|
|
|
|
// lookup plaintext size of blob
|
|
|
|
size, err := r.idx.LookupSize(id, t)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-03 20:38:05 +00:00
|
|
|
// make sure the plaintext buffer is large enough, extend otherwise
|
2015-10-31 13:47:42 +00:00
|
|
|
plaintextBufSize := uint(cap(plaintextBuf))
|
2016-08-03 20:38:05 +00:00
|
|
|
if size > plaintextBufSize {
|
2016-01-23 23:12:09 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "need to expand buffer: want %d bytes, got %d",
|
2016-08-03 20:38:05 +00:00
|
|
|
size, plaintextBufSize)
|
|
|
|
plaintextBuf = make([]byte, size)
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 20:38:05 +00:00
|
|
|
// lookup packs
|
|
|
|
blobs, err := r.idx.Lookup(id, t)
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
2016-08-03 20:38:05 +00:00
|
|
|
debug.Log("Repo.LoadBlob", "id %v not found in index: %v", id.Str(), err)
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-28 20:18:02 +00:00
|
|
|
var lastError error
|
2016-08-03 20:38:05 +00:00
|
|
|
for _, blob := range blobs {
|
|
|
|
debug.Log("Repo.LoadBlob", "id %v found: %v", id.Str(), blob)
|
|
|
|
|
|
|
|
if blob.Type != t {
|
|
|
|
debug.Log("Repo.LoadBlob", "blob %v has wrong block type, want %v", blob, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// load blob from pack
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: blob.PackID.String()}
|
2016-08-03 20:38:05 +00:00
|
|
|
ciphertextBuf := make([]byte, blob.Length)
|
|
|
|
n, err := r.be.Load(h, ciphertextBuf, int64(blob.Offset))
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("Repo.LoadBlob", "error loading blob %v: %v", blob, err)
|
2016-08-28 20:18:02 +00:00
|
|
|
lastError = err
|
2016-08-03 20:38:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint(n) != blob.Length {
|
2016-08-28 20:18:02 +00:00
|
|
|
lastError = errors.Errorf("error loading blob %v: wrong length returned, want %d, got %d",
|
|
|
|
id.Str(), blob.Length, uint(n))
|
|
|
|
debug.Log("Repo.LoadBlob", "lastError: %v", lastError)
|
2016-08-03 20:38:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
|
|
|
plaintextBuf, err = r.decryptTo(plaintextBuf, ciphertextBuf)
|
|
|
|
if err != nil {
|
2016-08-28 20:18:02 +00:00
|
|
|
lastError = errors.Errorf("decrypting blob %v failed: %v", id, err)
|
2016-08-03 20:38:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check hash
|
2016-08-31 18:29:54 +00:00
|
|
|
if !restic.Hash(plaintextBuf).Equal(id) {
|
2016-08-28 20:18:02 +00:00
|
|
|
lastError = errors.Errorf("blob %v returned invalid hash", id)
|
2016-08-03 20:38:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return plaintextBuf, nil
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2016-08-28 20:18:02 +00:00
|
|
|
if lastError != nil {
|
|
|
|
return nil, lastError
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:48:36 +00:00
|
|
|
return nil, errors.Errorf("loading blob %v from %v packs failed", id.Str(), len(blobs))
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:33:20 +00:00
|
|
|
// closeOrErr calls cl.Close() and sets err to the returned error value if
|
|
|
|
// itself is not yet set.
|
|
|
|
func closeOrErr(cl io.Closer, err *error) {
|
|
|
|
e := cl.Close()
|
|
|
|
if *err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*err = e
|
|
|
|
}
|
|
|
|
|
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.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) LoadJSONUnpacked(t restic.FileType, id restic.ID, item interface{}) (err error) {
|
2016-01-23 23:12:09 +00:00
|
|
|
buf, err := r.LoadAndDecrypt(t, id)
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-23 23:12:09 +00:00
|
|
|
return json.Unmarshal(buf, item)
|
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.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (r *Repository) LoadJSONPack(t restic.BlobType, id restic.ID, item interface{}) (err error) {
|
2016-08-03 20:44:45 +00:00
|
|
|
buf, err := r.LoadBlob(id, t, nil)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-23 23:12:09 +00:00
|
|
|
return json.Unmarshal(buf, item)
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-26 14:43:42 +00:00
|
|
|
// LookupBlobSize returns the size of blob id.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (r *Repository) LookupBlobSize(id restic.ID, tpe restic.BlobType) (uint, error) {
|
2016-08-03 20:38:05 +00:00
|
|
|
return r.idx.LookupSize(id, tpe)
|
2015-07-26 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 22:50:56 +00:00
|
|
|
// SaveAndEncrypt 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.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (r *Repository) SaveAndEncrypt(t restic.BlobType, data []byte, id *restic.ID) (restic.ID, error) {
|
2015-01-10 22:40:10 +00:00
|
|
|
if id == nil {
|
|
|
|
// compute plaintext hash
|
2016-08-31 18:29:54 +00:00
|
|
|
hashedID := restic.Hash(data)
|
2015-07-25 15:05:45 +00:00
|
|
|
id = &hashedID
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
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-07-02 19:52:57 +00:00
|
|
|
ciphertext, err := r.Encrypt(ciphertext, data)
|
2015-01-10 22:40:10 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// find suitable packer and add blob
|
2015-07-02 19:52:57 +00:00
|
|
|
packer, err := r.findPacker(uint(len(ciphertext)))
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// save ciphertext
|
2016-03-06 11:26:25 +00:00
|
|
|
_, err = packer.Add(t, *id, ciphertext)
|
2015-10-12 20:34:12 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-10-12 20:34:12 +00:00
|
|
|
}
|
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
|
2015-07-02 19:52:57 +00:00
|
|
|
if packer.Size() < minPackSize && r.countPacker() < maxPackers {
|
2015-05-09 15:41:28 +00:00
|
|
|
debug.Log("Repo.Save", "pack is not full enough (%d bytes)", packer.Size())
|
2015-07-02 19:52:57 +00:00
|
|
|
r.insertPacker(packer)
|
2015-07-25 15:05:45 +00:00
|
|
|
return *id, nil
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// else write the pack to the backend
|
2015-07-25 15:05:45 +00:00
|
|
|
return *id, r.savePacker(packer)
|
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.
|
2016-08-31 18:58:57 +00:00
|
|
|
func (r *Repository) SaveJSON(t restic.BlobType, item interface{}) (restic.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 {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, errors.Errorf("json.Encode: %v", err)
|
2015-02-08 21:54:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
buf = wr.Bytes()
|
2016-02-01 22:50:56 +00:00
|
|
|
return r.SaveAndEncrypt(t, buf, nil)
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
|
|
|
|
// backend as type t, without a pack. It returns the storage hash.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) SaveJSONUnpacked(t restic.FileType, item interface{}) (restic.ID, error) {
|
2016-01-24 17:50:41 +00:00
|
|
|
debug.Log("Repo.SaveJSONUnpacked", "save new blob %v", t)
|
|
|
|
plaintext, err := json.Marshal(item)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, errors.Wrap(err, "json.Marshal")
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 17:52:11 +00:00
|
|
|
return r.SaveUnpacked(t, plaintext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveUnpacked encrypts data and stores it in the backend. Returned is the
|
|
|
|
// storage hash.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) SaveUnpacked(t restic.FileType, p []byte) (id restic.ID, err error) {
|
2016-01-24 17:52:11 +00:00
|
|
|
ciphertext := make([]byte, len(p)+crypto.Extension)
|
|
|
|
ciphertext, err = r.Encrypt(ciphertext, p)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
id = restic.Hash(ciphertext)
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: t, Name: id.String()}
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2016-01-24 17:50:41 +00:00
|
|
|
err = r.be.Save(h, ciphertext)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
2016-01-24 17:50:41 +00:00
|
|
|
debug.Log("Repo.SaveJSONUnpacked", "error saving blob %v: %v", h, err)
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 17:50:41 +00:00
|
|
|
debug.Log("Repo.SaveJSONUnpacked", "blob %v saved", h)
|
|
|
|
return id, nil
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush saves all remaining packs.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Flush() error {
|
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2015-07-02 19:52:57 +00:00
|
|
|
debug.Log("Repo.Flush", "manually flushing %d packs", len(r.packs))
|
2015-04-26 15:44:38 +00:00
|
|
|
|
2015-07-02 19:52:57 +00:00
|
|
|
for _, p := range r.packs {
|
|
|
|
err := r.savePacker(p)
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 19:52:57 +00:00
|
|
|
r.packs = r.packs[:0]
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// Backend returns the backend for the repository.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) Backend() restic.Backend {
|
2015-07-02 19:52:57 +00:00
|
|
|
return r.be
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
// Index returns the currently used MasterIndex.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (r *Repository) Index() restic.Index {
|
2015-07-02 19:52:57 +00:00
|
|
|
return r.idx
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 11:25:52 +00:00
|
|
|
// SetIndex instructs the repository to use the given index.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (r *Repository) SetIndex(i restic.Index) {
|
|
|
|
r.idx = i.(*MasterIndex)
|
2015-02-08 21:54:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 17:52:11 +00:00
|
|
|
// SaveIndex saves an index in the repository.
|
2016-08-31 18:58:57 +00:00
|
|
|
func SaveIndex(repo restic.Repository, index *Index) (restic.ID, error) {
|
2016-01-24 17:52:11 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2015-10-25 16:05:54 +00:00
|
|
|
|
2016-01-24 17:52:11 +00:00
|
|
|
err := index.Finalize(buf)
|
2015-10-25 16:05:54 +00:00
|
|
|
if err != nil {
|
2016-08-31 18:29:54 +00:00
|
|
|
return restic.ID{}, err
|
2015-10-25 16:05:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
return repo.SaveUnpacked(restic.IndexFile, buf.Bytes())
|
2015-10-25 16:05:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 21:59:17 +00:00
|
|
|
// saveIndex saves all indexes in the backend.
|
|
|
|
func (r *Repository) saveIndex(indexes ...*Index) error {
|
|
|
|
for i, idx := range indexes {
|
2015-10-12 20:34:12 +00:00
|
|
|
debug.Log("Repo.SaveIndex", "Saving index %d", i)
|
2015-08-08 10:40:37 +00:00
|
|
|
|
2015-10-25 16:05:54 +00:00
|
|
|
sid, err := SaveIndex(r, idx)
|
2015-10-12 20:34:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-15 23:24:58 +00:00
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
debug.Log("Repo.SaveIndex", "Saved index %d as %v", i, sid.Str())
|
|
|
|
}
|
2015-01-10 22:40:10 +00:00
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
return nil
|
2015-01-10 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 21:59:17 +00:00
|
|
|
// SaveIndex saves all new indexes in the backend.
|
|
|
|
func (r *Repository) SaveIndex() error {
|
|
|
|
return r.saveIndex(r.idx.NotFinalIndexes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveFullIndex saves all full indexes in the backend.
|
|
|
|
func (r *Repository) SaveFullIndex() error {
|
|
|
|
return r.saveIndex(r.idx.FullIndexes()...)
|
|
|
|
}
|
|
|
|
|
2015-07-04 16:38:32 +00:00
|
|
|
const loadIndexParallelism = 20
|
|
|
|
|
2015-10-12 20:34:12 +00:00
|
|
|
// LoadIndex loads all index files from the backend in parallel and stores them
|
|
|
|
// in the master index. The first error that occurred is returned.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *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
|
|
|
|
2015-07-04 16:38:32 +00:00
|
|
|
errCh := make(chan error, 1)
|
|
|
|
indexes := make(chan *Index)
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
worker := func(id restic.ID, done <-chan struct{}) error {
|
2015-07-04 16:38:32 +00:00
|
|
|
idx, err := LoadIndex(r, id)
|
2015-04-26 15:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-04 16:38:32 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case indexes <- idx:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(indexes)
|
2016-08-31 18:29:54 +00:00
|
|
|
errCh <- FilesInParallel(r.be, restic.IndexFile, loadIndexParallelism,
|
2016-02-24 21:41:32 +00:00
|
|
|
ParallelWorkFuncParseID(worker))
|
2015-07-04 16:38:32 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for idx := range indexes {
|
2015-10-12 20:34:12 +00:00
|
|
|
r.idx.Insert(idx)
|
2015-07-04 16:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
return err
|
2015-04-26 15:44:38 +00:00
|
|
|
}
|
2015-07-04 16:38:32 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-04 16:38:32 +00:00
|
|
|
// LoadIndex loads the index id from backend and returns it.
|
2016-08-31 18:29:54 +00:00
|
|
|
func LoadIndex(repo *Repository, id restic.ID) (*Index, error) {
|
2015-08-08 10:22:17 +00:00
|
|
|
idx, err := LoadIndexWithDecoder(repo, id, DecodeIndex)
|
2015-07-26 19:58:03 +00:00
|
|
|
if err == nil {
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == ErrOldIndexFormat {
|
2016-02-24 21:41:32 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str())
|
2015-08-08 10:22:17 +00:00
|
|
|
return LoadIndexWithDecoder(repo, id, DecodeOldIndex)
|
2015-07-26 19:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-04 18:39:45 +00:00
|
|
|
// SearchKey finds a key with the supplied password, afterwards the config is
|
2016-08-21 11:09:31 +00:00
|
|
|
// read and parsed. It tries at most maxKeys key files in the repo.
|
|
|
|
func (r *Repository) SearchKey(password string, maxKeys int) error {
|
|
|
|
key, err := SearchKey(r, password, maxKeys)
|
2014-12-21 17:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-02 19:52:57 +00:00
|
|
|
r.key = key.master
|
2015-11-18 19:20:25 +00:00
|
|
|
r.packerManager.key = key.master
|
2015-07-02 19:52:57 +00:00
|
|
|
r.keyName = key.Name()
|
2016-08-31 20:39:36 +00:00
|
|
|
r.cfg, err = restic.LoadConfig(r)
|
2015-07-02 20:36:31 +00:00
|
|
|
return err
|
2015-05-03 14:36:52 +00:00
|
|
|
}
|
2014-12-21 17:10:19 +00:00
|
|
|
|
2015-07-02 20:36:31 +00:00
|
|
|
// Init creates a new master key with the supplied password, initializes and
|
|
|
|
// saves the repository config.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Init(password string) error {
|
2016-08-31 18:29:54 +00:00
|
|
|
has, err := r.be.Test(restic.ConfigFile, "")
|
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")
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
cfg, err := restic.CreateConfig()
|
2016-07-31 14:27:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.init(password, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// init creates a new master key with the supplied password and uses it to save
|
|
|
|
// the config into the repo.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (r *Repository) init(password string, cfg restic.Config) error {
|
2015-07-02 19:52:57 +00:00
|
|
|
key, err := createMasterKey(r, password)
|
2015-05-03 14:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-02 19:52:57 +00:00
|
|
|
r.key = key.master
|
2015-11-18 19:20:25 +00:00
|
|
|
r.packerManager.key = key.master
|
2015-07-02 19:52:57 +00:00
|
|
|
r.keyName = key.Name()
|
2016-08-31 20:39:36 +00:00
|
|
|
r.cfg = cfg
|
2016-08-31 18:29:54 +00:00
|
|
|
_, err = r.SaveJSONUnpacked(restic.ConfigFile, cfg)
|
2015-07-02 20:36:31 +00:00
|
|
|
return err
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// Decrypt authenticates and decrypts ciphertext and returns the plaintext.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Decrypt(ciphertext []byte) ([]byte, error) {
|
2015-07-26 12:25:01 +00:00
|
|
|
return r.decryptTo(nil, ciphertext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt authenticates and decrypts ciphertext and stores the result in
|
|
|
|
// plaintext.
|
|
|
|
func (r *Repository) decryptTo(plaintext, ciphertext []byte) ([]byte, error) {
|
2015-07-02 19:52:57 +00:00
|
|
|
if r.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-07-02 19:52:57 +00:00
|
|
|
return crypto.Decrypt(r.key, nil, ciphertext)
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// Encrypt encrypts and authenticates the plaintext and saves the result in
|
|
|
|
// ciphertext.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Encrypt(ciphertext, plaintext []byte) ([]byte, error) {
|
|
|
|
if r.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-07-02 19:52:57 +00:00
|
|
|
return crypto.Encrypt(r.key, ciphertext, plaintext)
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// Key returns the current master key.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Key() *crypto.Key {
|
|
|
|
return r.key
|
2014-12-21 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// KeyName returns the name of the current key in the backend.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) KeyName() string {
|
|
|
|
return r.keyName
|
2015-05-03 16:04:13 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Count returns the number of blobs of a given type in the backend.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) Count(t restic.FileType) (n uint) {
|
2015-07-02 19:52:57 +00:00
|
|
|
for _ = range r.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
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) list(t restic.FileType, done <-chan struct{}, out chan<- restic.ID) {
|
2015-05-17 18:48:59 +00:00
|
|
|
defer close(out)
|
2015-07-02 19:52:57 +00:00
|
|
|
in := r.be.List(t, done)
|
2015-05-17 18:48:59 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// disable sending on the outCh until we received a job
|
2016-08-31 18:29:54 +00:00
|
|
|
outCh chan<- restic.ID
|
2015-05-17 18:48:59 +00:00
|
|
|
// enable receiving from in
|
|
|
|
inCh = in
|
2016-08-31 18:29:54 +00:00
|
|
|
id restic.ID
|
2015-05-17 18:48:59 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case strID, ok := <-inCh:
|
|
|
|
if !ok {
|
|
|
|
// input channel closed, we're done
|
|
|
|
return
|
|
|
|
}
|
2016-08-31 20:39:36 +00:00
|
|
|
id, err = restic.ParseID(strID)
|
2015-05-17 18:48:59 +00:00
|
|
|
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-07-02 20:53:03 +00:00
|
|
|
// List returns a channel that yields all IDs of type t in the backend.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (r *Repository) List(t restic.FileType, done <-chan struct{}) <-chan restic.ID {
|
|
|
|
outCh := make(chan restic.ID)
|
2015-05-17 18:48:59 +00:00
|
|
|
|
2015-07-02 19:52:57 +00:00
|
|
|
go r.list(t, done, outCh)
|
2015-05-17 18:48:59 +00:00
|
|
|
|
|
|
|
return outCh
|
2014-12-21 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 19:56:42 +00:00
|
|
|
// ListPack returns the list of blobs saved in the pack id and the length of
|
|
|
|
// the file as stored in the backend.
|
2016-08-31 20:39:36 +00:00
|
|
|
func (r *Repository) ListPack(id restic.ID) ([]restic.Blob, int64, error) {
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
2016-08-07 19:56:42 +00:00
|
|
|
|
|
|
|
blobInfo, err := r.Backend().Stat(h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
blobs, err := pack.List(r.Key(), restic.ReaderAt(r.Backend(), h), blobInfo.Size)
|
2016-05-08 11:51:21 +00:00
|
|
|
if err != nil {
|
2016-08-07 19:56:42 +00:00
|
|
|
return nil, 0, err
|
2016-05-08 11:51:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 19:08:16 +00:00
|
|
|
return blobs, blobInfo.Size, nil
|
2016-05-08 11:51:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 20:53:03 +00:00
|
|
|
// Delete calls backend.Delete() if implemented, and returns an error
|
|
|
|
// otherwise.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Delete() error {
|
2016-08-31 20:51:35 +00:00
|
|
|
if b, ok := r.be.(restic.Deleter); ok {
|
2014-12-21 16:02:49 +00:00
|
|
|
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-07-02 20:53:03 +00:00
|
|
|
// Close closes the repository by closing the backend.
|
2015-07-02 19:52:57 +00:00
|
|
|
func (r *Repository) Close() error {
|
|
|
|
return r.be.Close()
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
2016-09-03 09:22:01 +00:00
|
|
|
|
|
|
|
// LoadTree loads a tree from the repository.
|
|
|
|
func (r *Repository) LoadTree(id restic.ID) (*restic.Tree, error) {
|
|
|
|
size, err := r.idx.LookupSize(id, restic.TreeBlob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, size)
|
|
|
|
|
|
|
|
buf, err = r.LoadBlob(id, restic.TreeBlob, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := &restic.Tree{}
|
|
|
|
err = json.Unmarshal(buf, t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadDataBlob loads a data blob from the repository to the buffer.
|
|
|
|
func (r *Repository) LoadDataBlob(id restic.ID, buf []byte) (int, error) {
|
|
|
|
size, err := r.idx.LookupSize(id, restic.DataBlob)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) < int(size) {
|
|
|
|
return 0, errors.Errorf("buffer is too small for data blob (%d < %d)", len(buf), size)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err = r.LoadBlob(id, restic.DataBlob, buf)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(buf), err
|
|
|
|
}
|