restic/internal/repository/repository.go

628 lines
16 KiB
Go
Raw Normal View History

package repository
2014-12-21 16:02:49 +00:00
import (
"bytes"
2017-06-04 09:16:55 +00:00
"context"
"encoding/json"
"fmt"
2015-07-26 19:58:03 +00:00
"os"
2014-12-21 16:02:49 +00:00
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/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
key *crypto.Key
keyName string
2015-10-12 20:34:12 +00:00
idx *MasterIndex
restic.Cache
treePM *packerManager
dataPM *packerManager
2014-12-21 16:02:49 +00:00
}
// New returns a new repository with backend be.
2016-08-31 18:29:54 +00:00
func New(be restic.Backend) *Repository {
repo := &Repository{
be: be,
idx: NewMasterIndex(),
dataPM: newPackerManager(be, nil),
treePM: newPackerManager(be, nil),
}
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
}
// UseCache replaces the backend with the wrapped cache.
func (r *Repository) UseCache(c restic.Cache) {
if c == nil {
return
}
debug.Log("using cache")
r.Cache = c
r.be = c.Wrap(r.be)
}
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
}
// LoadAndDecrypt loads and decrypts data identified by t and id from the
// backend.
func (r *Repository) LoadAndDecrypt(ctx context.Context, t restic.FileType, id restic.ID) (buf []byte, err error) {
2016-09-27 20:35:08 +00:00
debug.Log("load %v with id %v", t, id.Str())
2016-09-01 19:19:30 +00:00
h := restic.Handle{Type: t, Name: id.String()}
buf, err = backend.LoadAll(ctx, r.be, h)
2015-03-28 10:50:23 +00:00
if err != nil {
2017-02-11 13:24:11 +00:00
debug.Log("error loading %v: %v", h, 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) {
2017-02-11 13:28:15 +00:00
return nil, errors.Errorf("load %v: invalid data returned", h)
2015-03-28 10:50:23 +00:00
}
// decrypt
2017-01-13 20:23:47 +00:00
n, err := r.decryptTo(buf, buf)
if err != nil {
return nil, err
}
2017-01-13 20:23:47 +00:00
return buf[:n], 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.
2017-06-04 09:16:55 +00:00
func (r *Repository) loadBlob(ctx context.Context, id restic.ID, t restic.BlobType, plaintextBuf []byte) (int, error) {
debug.Log("load %v with id %v (buf len %v, cap %d)", t, id.Str(), len(plaintextBuf), cap(plaintextBuf))
// lookup packs
blobs, err := r.idx.Lookup(id, t)
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("id %v not found in index: %v", id.Str(), err)
return 0, err
}
2016-08-28 20:18:02 +00:00
var lastError error
for _, blob := range blobs {
2016-09-27 20:35:08 +00:00
debug.Log("id %v found: %v", id.Str(), blob)
if blob.Type != t {
2016-09-27 20:35:08 +00:00
debug.Log("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()}
if uint(cap(plaintextBuf)) < blob.Length {
return 0, errors.Errorf("buffer is too small: %v < %v", cap(plaintextBuf), blob.Length)
}
plaintextBuf = plaintextBuf[:blob.Length]
2017-06-04 09:16:55 +00:00
n, err := restic.ReadAt(ctx, r.be, h, int64(blob.Offset), plaintextBuf)
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("error loading blob %v: %v", blob, err)
2016-08-28 20:18:02 +00:00
lastError = err
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))
2016-09-27 20:35:08 +00:00
debug.Log("lastError: %v", lastError)
continue
}
// decrypt
n, err = r.decryptTo(plaintextBuf, plaintextBuf)
if err != nil {
2016-08-28 20:18:02 +00:00
lastError = errors.Errorf("decrypting blob %v failed: %v", id, err)
continue
}
plaintextBuf = plaintextBuf[:n]
// 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)
continue
}
return len(plaintextBuf), nil
}
2016-08-28 20:18:02 +00:00
if lastError != nil {
return 0, lastError
2016-08-28 20:18:02 +00:00
}
return 0, errors.Errorf("loading blob %v from %v packs failed", id.Str(), len(blobs))
}
2015-05-04 18:39:45 +00:00
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
// the item.
2017-06-04 09:16:55 +00:00
func (r *Repository) LoadJSONUnpacked(ctx context.Context, t restic.FileType, id restic.ID, item interface{}) (err error) {
buf, err := r.LoadAndDecrypt(ctx, t, id)
if err != nil {
return err
}
return json.Unmarshal(buf, item)
}
// 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) {
return r.idx.LookupSize(id, tpe)
}
// 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.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveAndEncrypt(ctx context.Context, t restic.BlobType, data []byte, id *restic.ID) (restic.ID, error) {
if id == nil {
// compute plaintext hash
2016-08-31 18:29:54 +00:00
hashedID := restic.Hash(data)
id = &hashedID
}
2016-09-27 20:35:08 +00:00
debug.Log("save id %v (%v, %d bytes)", id.Str(), t, len(data))
// get buf from the pool
2015-04-26 12:46:15 +00:00
ciphertext := getBuf()
defer freeBuf(ciphertext)
// encrypt blob
ciphertext, err := r.Encrypt(ciphertext, data)
if err != nil {
2016-08-31 18:29:54 +00:00
return restic.ID{}, err
}
// find suitable packer and add blob
var pm *packerManager
switch t {
case restic.TreeBlob:
pm = r.treePM
case restic.DataBlob:
pm = r.dataPM
default:
panic(fmt.Sprintf("invalid type: %v", t))
}
packer, err := pm.findPacker()
if err != nil {
2016-08-31 18:29:54 +00:00
return restic.ID{}, err
}
// save ciphertext
_, 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
}
// if the pack is not full enough, put back to the list
if packer.Size() < minPackSize {
2016-09-27 20:35:08 +00:00
debug.Log("pack is not full enough (%d bytes)", packer.Size())
pm.insertPacker(packer)
return *id, nil
}
// else write the pack to the backend
return *id, r.savePacker(t, packer)
}
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
// backend as type t, without a pack. It returns the storage hash.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveJSONUnpacked(ctx context.Context, t restic.FileType, item interface{}) (restic.ID, error) {
2016-09-27 20:35:08 +00:00
debug.Log("save new blob %v", t)
2016-01-24 17:50:41 +00:00
plaintext, err := json.Marshal(item)
if err != nil {
2016-08-31 18:29:54 +00:00
return restic.ID{}, errors.Wrap(err, "json.Marshal")
}
2017-06-04 09:16:55 +00:00
return r.SaveUnpacked(ctx, t, plaintext)
2016-01-24 17:52:11 +00:00
}
// SaveUnpacked encrypts data and stores it in the backend. Returned is the
// storage hash.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveUnpacked(ctx context.Context, t restic.FileType, p []byte) (id restic.ID, err error) {
ciphertext := restic.NewBlobBuffer(len(p))
2016-01-24 17:52:11 +00:00
ciphertext, err = r.Encrypt(ciphertext, p)
if err != nil {
2016-08-31 18:29:54 +00:00
return restic.ID{}, err
}
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()}
2017-06-04 09:16:55 +00:00
err = r.be.Save(ctx, h, bytes.NewReader(ciphertext))
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("error saving blob %v: %v", h, err)
2016-08-31 18:29:54 +00:00
return restic.ID{}, err
}
2016-09-27 20:35:08 +00:00
debug.Log("blob %v saved", h)
2016-01-24 17:50:41 +00:00
return id, nil
}
// Flush saves all remaining packs.
func (r *Repository) Flush() error {
pms := []struct {
t restic.BlobType
pm *packerManager
}{
{restic.DataBlob, r.dataPM},
{restic.TreeBlob, r.treePM},
}
for _, p := range pms {
p.pm.pm.Lock()
debug.Log("manually flushing %d packs", len(p.pm.packers))
for _, packer := range p.pm.packers {
err := r.savePacker(p.t, packer)
if err != nil {
p.pm.pm.Unlock()
return err
}
}
p.pm.packers = p.pm.packers[:0]
p.pm.pm.Unlock()
}
return nil
}
// Backend returns the backend for the repository.
2016-08-31 18:29:54 +00:00
func (r *Repository) Backend() restic.Backend {
return r.be
}
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 {
return r.idx
}
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)
}
2016-01-24 17:52:11 +00:00
// SaveIndex saves an index in the repository.
2017-06-04 09:16:55 +00:00
func SaveIndex(ctx context.Context, 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
}
2017-06-04 09:16:55 +00:00
return repo.SaveUnpacked(ctx, 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.
2017-06-04 09:16:55 +00:00
func (r *Repository) saveIndex(ctx context.Context, indexes ...*Index) error {
2015-10-12 21:59:17 +00:00
for i, idx := range indexes {
2016-09-27 20:35:08 +00:00
debug.Log("Saving index %d", i)
2017-06-04 09:16:55 +00:00
sid, err := SaveIndex(ctx, r, idx)
2015-10-12 20:34:12 +00:00
if err != nil {
return err
}
2015-02-15 23:24:58 +00:00
2016-09-27 20:35:08 +00:00
debug.Log("Saved index %d as %v", i, sid.Str())
2015-10-12 20:34:12 +00:00
}
2015-10-12 20:34:12 +00:00
return nil
}
2015-10-12 21:59:17 +00:00
// SaveIndex saves all new indexes in the backend.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveIndex(ctx context.Context) error {
return r.saveIndex(ctx, r.idx.NotFinalIndexes()...)
2015-10-12 21:59:17 +00:00
}
// SaveFullIndex saves all full indexes in the backend.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveFullIndex(ctx context.Context) error {
return r.saveIndex(ctx, r.idx.FullIndexes()...)
2015-10-12 21:59:17 +00:00
}
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.
2017-06-04 09:16:55 +00:00
func (r *Repository) LoadIndex(ctx context.Context) error {
2016-09-27 20:35:08 +00:00
debug.Log("Loading index")
2015-07-04 16:38:32 +00:00
errCh := make(chan error, 1)
indexes := make(chan *Index)
2017-06-04 09:16:55 +00:00
worker := func(ctx context.Context, id restic.ID) error {
idx, err := LoadIndex(ctx, r, id)
if err != nil {
return err
}
2015-07-04 16:38:32 +00:00
select {
case indexes <- idx:
2017-06-04 09:16:55 +00:00
case <-ctx.Done():
2015-07-04 16:38:32 +00:00
}
return nil
}
go func() {
defer close(indexes)
2017-06-04 09:16:55 +00:00
errCh <- FilesInParallel(ctx, r.be, restic.IndexFile, loadIndexParallelism,
ParallelWorkFuncParseID(worker))
2015-07-04 16:38:32 +00:00
}()
2017-09-24 09:25:45 +00:00
validIndex := restic.NewIDSet()
2015-07-04 16:38:32 +00:00
for idx := range indexes {
2017-09-24 09:25:45 +00:00
id, err := idx.ID()
if err == nil {
validIndex.Insert(id)
}
2015-10-12 20:34:12 +00:00
r.idx.Insert(idx)
2015-07-04 16:38:32 +00:00
}
2017-09-24 09:25:45 +00:00
if r.Cache != nil {
err := r.Cache.Clear(restic.IndexFile, validIndex)
if err != nil {
fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err)
}
}
2015-07-04 16:38:32 +00:00
if err := <-errCh; err != nil {
return err
}
2015-07-04 16:38:32 +00:00
return nil
}
2015-07-04 16:38:32 +00:00
// LoadIndex loads the index id from backend and returns it.
2017-06-04 09:16:55 +00:00
func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Index, error) {
idx, err := LoadIndexWithDecoder(ctx, repo, id, DecodeIndex)
2015-07-26 19:58:03 +00:00
if err == nil {
return idx, nil
}
if errors.Cause(err) == ErrOldIndexFormat {
fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str())
2017-06-04 09:16:55 +00:00
return LoadIndexWithDecoder(ctx, 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
// read and parsed. It tries at most maxKeys key files in the repo.
2017-06-04 09:16:55 +00:00
func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int) error {
key, err := SearchKey(ctx, r, password, maxKeys)
2014-12-21 17:10:19 +00:00
if err != nil {
return err
}
r.key = key.master
r.dataPM.key = key.master
r.treePM.key = key.master
r.keyName = key.Name()
2017-06-04 09:16:55 +00:00
r.cfg, err = restic.LoadConfig(ctx, 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.
2017-06-04 09:16:55 +00:00
func (r *Repository) Init(ctx context.Context, password string) error {
has, err := r.be.Test(ctx, restic.Handle{Type: 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()
if err != nil {
return err
}
2017-06-04 09:16:55 +00:00
return r.init(ctx, password, cfg)
}
// init creates a new master key with the supplied password and uses it to save
// the config into the repo.
2017-06-04 09:16:55 +00:00
func (r *Repository) init(ctx context.Context, password string, cfg restic.Config) error {
key, err := createMasterKey(r, password)
2015-05-03 14:36:52 +00:00
if err != nil {
return err
}
r.key = key.master
r.dataPM.key = key.master
r.treePM.key = key.master
r.keyName = key.Name()
2016-08-31 20:39:36 +00:00
r.cfg = cfg
2017-06-04 09:16:55 +00:00
_, err = r.SaveJSONUnpacked(ctx, restic.ConfigFile, cfg)
2015-07-02 20:36:31 +00:00
return err
2014-12-21 17:10:19 +00:00
}
// decrypt authenticates and decrypts ciphertext and stores the result in
// plaintext.
func (r *Repository) decryptTo(plaintext, ciphertext []byte) (int, error) {
if r.key == nil {
return 0, errors.New("key for repository not set")
2014-12-21 17:10:19 +00:00
}
return r.key.Decrypt(plaintext, ciphertext)
2014-12-21 17:10:19 +00:00
}
// Encrypt encrypts and authenticates the plaintext and saves the result in
// ciphertext.
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
}
return r.key.Encrypt(ciphertext, plaintext)
2014-12-21 17:10:19 +00:00
}
// Key returns the current master key.
func (r *Repository) Key() *crypto.Key {
return r.key
2014-12-21 17:10:19 +00:00
}
// KeyName returns the name of the current key in the backend.
func (r *Repository) KeyName() string {
return r.keyName
}
// List returns a channel that yields all IDs of type t in the backend.
2017-06-04 09:16:55 +00:00
func (r *Repository) List(ctx context.Context, t restic.FileType) <-chan restic.ID {
2017-03-06 21:19:38 +00:00
out := make(chan restic.ID)
go func() {
defer close(out)
2017-06-04 09:16:55 +00:00
for strID := range r.be.List(ctx, t) {
2017-03-06 21:19:38 +00:00
if id, err := restic.ParseID(strID); err == nil {
select {
case out <- id:
2017-06-04 09:16:55 +00:00
case <-ctx.Done():
2017-03-06 21:19:38 +00:00
return
}
}
}
}()
return out
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.
2017-06-04 09:16:55 +00:00
func (r *Repository) ListPack(ctx context.Context, 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
2017-06-04 09:16:55 +00:00
blobInfo, err := r.Backend().Stat(ctx, h)
2016-08-07 19:56:42 +00:00
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
}
// Delete calls backend.Delete() if implemented, and returns an error
// otherwise.
2017-06-04 09:16:55 +00:00
func (r *Repository) Delete(ctx context.Context) error {
2016-08-31 20:51:35 +00:00
if b, ok := r.be.(restic.Deleter); ok {
2017-06-04 09:16:55 +00:00
return b.Delete(ctx)
2014-12-21 16:02:49 +00:00
}
return errors.New("Delete() called for backend that does not implement this method")
}
// Close closes the repository by closing the backend.
func (r *Repository) Close() error {
return r.be.Close()
2015-03-28 10:50:23 +00:00
}
// LoadBlob loads a blob of type t from the repository to the buffer. buf must
// be large enough to hold the encrypted blob, since it is used as scratch
// space.
2017-06-04 09:16:55 +00:00
func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.ID, buf []byte) (int, error) {
2017-01-24 08:03:33 +00:00
debug.Log("load blob %v into buf (len %v, cap %v)", id.Str(), len(buf), cap(buf))
2016-09-03 18:55:22 +00:00
size, err := r.idx.LookupSize(id, t)
if err != nil {
return 0, err
}
if cap(buf) < restic.CiphertextLength(int(size)) {
return 0, errors.Errorf("buffer is too small for data blob (%d < %d)", cap(buf), restic.CiphertextLength(int(size)))
2016-09-03 18:55:22 +00:00
}
2017-06-04 09:16:55 +00:00
n, err := r.loadBlob(ctx, id, t, buf)
2016-09-03 18:55:22 +00:00
if err != nil {
return 0, err
}
buf = buf[:n]
2016-09-27 20:35:08 +00:00
debug.Log("loaded %d bytes into buf %p", len(buf), buf)
2016-09-03 18:55:22 +00:00
return len(buf), err
}
// SaveBlob saves a blob of type t into the repository. If id is the null id, it
// will be computed and returned.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID) (restic.ID, error) {
2016-09-03 18:55:22 +00:00
var i *restic.ID
if !id.IsNull() {
i = &id
}
2017-06-04 09:16:55 +00:00
return r.SaveAndEncrypt(ctx, t, buf, i)
2016-09-03 18:55:22 +00:00
}
// LoadTree loads a tree from the repository.
2017-06-04 09:16:55 +00:00
func (r *Repository) LoadTree(ctx context.Context, id restic.ID) (*restic.Tree, error) {
2016-09-27 20:35:08 +00:00
debug.Log("load tree %v", id.Str())
size, err := r.idx.LookupSize(id, restic.TreeBlob)
if err != nil {
return nil, err
}
2016-09-27 20:35:08 +00:00
debug.Log("size is %d, create buffer", size)
buf := restic.NewBlobBuffer(int(size))
2017-06-04 09:16:55 +00:00
n, err := r.loadBlob(ctx, id, restic.TreeBlob, buf)
if err != nil {
return nil, err
}
buf = buf[:n]
t := &restic.Tree{}
err = json.Unmarshal(buf, t)
if err != nil {
return nil, err
}
return t, nil
}
2016-09-03 18:55:22 +00:00
// SaveTree stores a tree into the repository and returns the ID. The ID is
// checked against the index. The tree is only stored when the index does not
// contain the ID.
2017-06-04 09:16:55 +00:00
func (r *Repository) SaveTree(ctx context.Context, t *restic.Tree) (restic.ID, error) {
2016-09-03 18:55:22 +00:00
buf, err := json.Marshal(t)
if err != nil {
2016-09-03 18:55:22 +00:00
return restic.ID{}, errors.Wrap(err, "MarshalJSON")
}
2016-09-03 18:55:22 +00:00
// append a newline so that the data is always consistent (json.Encoder
// adds a newline after each object)
buf = append(buf, '\n')
2016-09-03 18:55:22 +00:00
id := restic.Hash(buf)
if r.idx.Has(id, restic.TreeBlob) {
return id, nil
}
2017-06-04 09:16:55 +00:00
_, err = r.SaveBlob(ctx, restic.TreeBlob, buf, id)
2016-09-03 18:55:22 +00:00
return id, err
}