From fe8c12c798e8502c098d838b859980c3dc7cbf65 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 3 Sep 2016 20:11:10 +0200 Subject: [PATCH] Replace repolitoy.SaveAndEncrypt to SaveBlob() --- src/restic/archiver/archive_reader.go | 5 +++-- src/restic/archiver/archiver.go | 2 +- src/restic/repository.go | 3 ++- src/restic/repository/repack.go | 2 +- src/restic/repository/repack_test.go | 2 +- src/restic/repository/repository.go | 10 ++++++++++ src/restic/repository/repository_test.go | 8 ++++---- src/restic/testing.go | 2 +- 8 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/restic/archiver/archive_reader.go b/src/restic/archiver/archive_reader.go index 2a184ee4b..1868ee0c1 100644 --- a/src/restic/archiver/archive_reader.go +++ b/src/restic/archiver/archive_reader.go @@ -7,8 +7,9 @@ import ( "restic/debug" "time" - "github.com/restic/chunker" "restic/errors" + + "github.com/restic/chunker" ) // saveTreeJSON stores a tree in the repository. @@ -58,7 +59,7 @@ func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, nam id := restic.Hash(chunk.Data) if !repo.Index().Has(id, restic.DataBlob) { - _, err := repo.SaveAndEncrypt(restic.DataBlob, chunk.Data, nil) + _, err := repo.SaveBlob(restic.DataBlob, chunk.Data, id) if err != nil { return nil, restic.ID{}, err } diff --git a/src/restic/archiver/archiver.go b/src/restic/archiver/archiver.go index 30decd1d9..6decc5fd5 100644 --- a/src/restic/archiver/archiver.go +++ b/src/restic/archiver/archiver.go @@ -98,7 +98,7 @@ func (arch *Archiver) Save(t restic.BlobType, data []byte, id restic.ID) error { return nil } - _, err := arch.repo.SaveAndEncrypt(t, data, &id) + _, err := arch.repo.SaveBlob(t, data, id) if err != nil { debug.Log("Archiver.Save", "Save(%v, %v): error %v\n", t, id.Str(), err) return err diff --git a/src/restic/repository.go b/src/restic/repository.go index c7e8ae170..bf8d31453 100644 --- a/src/restic/repository.go +++ b/src/restic/repository.go @@ -29,7 +29,6 @@ type Repository interface { SaveJSON(BlobType, interface{}) (ID, error) SaveUnpacked(FileType, []byte) (ID, error) - SaveAndEncrypt(BlobType, []byte, *ID) (ID, error) SaveJSONUnpacked(FileType, interface{}) (ID, error) LoadJSONUnpacked(FileType, ID, interface{}) error @@ -37,6 +36,8 @@ type Repository interface { LoadTree(id ID) (*Tree, error) LoadDataBlob(id ID, buf []byte) (int, error) + + SaveBlob(BlobType, []byte, ID) (ID, error) } // Deleter removes all data stored in a backend/repo. diff --git a/src/restic/repository/repack.go b/src/restic/repository/repack.go index 2ce701ad9..edb717efb 100644 --- a/src/restic/repository/repack.go +++ b/src/restic/repository/repack.go @@ -64,7 +64,7 @@ func Repack(repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet } plaintext = plaintext[:n] - _, err = repo.SaveAndEncrypt(entry.Type, plaintext, &entry.ID) + _, err = repo.SaveBlob(entry.Type, plaintext, entry.ID) if err != nil { return err } diff --git a/src/restic/repository/repack_test.go b/src/restic/repository/repack_test.go index 9b118d7b4..6d910c97b 100644 --- a/src/restic/repository/repack_test.go +++ b/src/restic/repository/repack_test.go @@ -46,7 +46,7 @@ func createRandomBlobs(t testing.TB, repo restic.Repository, blobs int, pData fl continue } - _, err := repo.SaveAndEncrypt(tpe, buf, &id) + _, err := repo.SaveBlob(tpe, buf, id) if err != nil { t.Fatalf("SaveFrom() error %v", err) } diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index 5a6ee64f4..55b9ea1be 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -614,3 +614,13 @@ func (r *Repository) LoadDataBlob(id restic.ID, buf []byte) (int, error) { 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. +func (r *Repository) SaveBlob(t restic.BlobType, buf []byte, id restic.ID) (restic.ID, error) { + var i *restic.ID + if !id.IsNull() { + i = &id + } + return r.SaveAndEncrypt(t, buf, i) +} diff --git a/src/restic/repository/repository_test.go b/src/restic/repository/repository_test.go index 644650c30..1b93dc8b0 100644 --- a/src/restic/repository/repository_test.go +++ b/src/restic/repository/repository_test.go @@ -81,7 +81,7 @@ func TestSave(t *testing.T) { id := restic.Hash(data) // save - sid, err := repo.SaveAndEncrypt(restic.DataBlob, data, nil) + sid, err := repo.SaveBlob(restic.DataBlob, data, restic.ID{}) OK(t, err) Equals(t, id, sid) @@ -117,7 +117,7 @@ func TestSaveFrom(t *testing.T) { id := restic.Hash(data) // save - id2, err := repo.SaveAndEncrypt(restic.DataBlob, data, &id) + id2, err := repo.SaveBlob(restic.DataBlob, data, id) OK(t, err) Equals(t, id, id2) @@ -156,7 +156,7 @@ func BenchmarkSaveAndEncrypt(t *testing.B) { for i := 0; i < t.N; i++ { // save - _, err = repo.SaveAndEncrypt(restic.DataBlob, data, &id) + _, err = repo.SaveBlob(restic.DataBlob, data, id) OK(t, err) } } @@ -253,7 +253,7 @@ func saveRandomDataBlobs(t testing.TB, repo restic.Repository, num int, sizeMax _, err := io.ReadFull(rand.Reader, buf) OK(t, err) - _, err = repo.SaveAndEncrypt(restic.DataBlob, buf, nil) + _, err = repo.SaveBlob(restic.DataBlob, buf, restic.ID{}) OK(t, err) } } diff --git a/src/restic/testing.go b/src/restic/testing.go index 9b6b627c8..85cab0d69 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -43,7 +43,7 @@ func (fs fakeFileSystem) saveFile(rd io.Reader) (blobs IDs) { id := Hash(chunk.Data) if !fs.blobIsKnown(id, DataBlob) { - _, err := fs.repo.SaveAndEncrypt(DataBlob, chunk.Data, &id) + _, err := fs.repo.SaveBlob(DataBlob, chunk.Data, id) if err != nil { fs.t.Fatalf("error saving chunk: %v", err) }