From 6fc359083866a6dafd41323531c76f60e9ac1a91 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 8 May 2016 13:13:29 +0200 Subject: [PATCH] Remove repository.SaveFrom() --- src/restic/archiver.go | 7 +++---- src/restic/archiver_duplication_test.go | 3 +-- src/restic/archiver_test.go | 2 +- src/restic/repository/repository.go | 21 --------------------- src/restic/repository/repository_test.go | 7 ++++--- src/restic/testing.go | 4 +--- 6 files changed, 10 insertions(+), 34 deletions(-) diff --git a/src/restic/archiver.go b/src/restic/archiver.go index 9f9298f83..007bfa993 100644 --- a/src/restic/archiver.go +++ b/src/restic/archiver.go @@ -1,7 +1,6 @@ package restic import ( - "bytes" "encoding/json" "fmt" "io" @@ -92,7 +91,7 @@ func (arch *Archiver) isKnownBlob(id backend.ID) bool { } // Save stores a blob read from rd in the repository. -func (arch *Archiver) Save(t pack.BlobType, id backend.ID, length uint, rd io.Reader) error { +func (arch *Archiver) Save(t pack.BlobType, data []byte, id backend.ID) error { debug.Log("Archiver.Save", "Save(%v, %v)\n", t, id.Str()) if arch.isKnownBlob(id) { @@ -100,7 +99,7 @@ func (arch *Archiver) Save(t pack.BlobType, id backend.ID, length uint, rd io.Re return nil } - err := arch.repo.SaveFrom(t, &id, length, rd) + _, err := arch.repo.SaveAndEncrypt(t, data, &id) if err != nil { debug.Log("Archiver.Save", "Save(%v, %v): error %v\n", t, id.Str(), err) return err @@ -160,7 +159,7 @@ func (arch *Archiver) saveChunk(chunk chunker.Chunk, p *Progress, token struct{} defer freeBuf(chunk.Data) id := backend.Hash(chunk.Data) - err := arch.Save(pack.Data, id, chunk.Length, bytes.NewReader(chunk.Data)) + err := arch.Save(pack.Data, chunk.Data, id) // TODO handle error if err != nil { panic(err) diff --git a/src/restic/archiver_duplication_test.go b/src/restic/archiver_duplication_test.go index ffcbacee4..1c0193eab 100644 --- a/src/restic/archiver_duplication_test.go +++ b/src/restic/archiver_duplication_test.go @@ -1,7 +1,6 @@ package restic_test import ( - "bytes" "crypto/rand" "errors" "io" @@ -108,7 +107,7 @@ func testArchiverDuplication(t *testing.T) { buf := make([]byte, 50) - err := arch.Save(pack.Data, id, uint(len(buf)), bytes.NewReader(buf)) + err := arch.Save(pack.Data, buf, id) if err != nil { t.Fatal(err) } diff --git a/src/restic/archiver_test.go b/src/restic/archiver_test.go index 813cc3362..e42151a27 100644 --- a/src/restic/archiver_test.go +++ b/src/restic/archiver_test.go @@ -267,7 +267,7 @@ func testParallelSaveWithDuplication(t *testing.T, seed int) { id := backend.Hash(c.Data) time.Sleep(time.Duration(id[0])) - err := arch.Save(pack.Data, id, c.Length, bytes.NewReader(c.Data)) + err := arch.Save(pack.Data, c.Data, id) <-barrier errChan <- err }(c, errChan) diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index e7e2e5ada..6df3471ff 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "restic/backend" @@ -212,26 +211,6 @@ func (r *Repository) SaveAndEncrypt(t pack.BlobType, data []byte, id *backend.ID return *id, r.savePacker(packer) } -// SaveFrom encrypts data read from rd and stores it in a pack in the backend as type t. -func (r *Repository) SaveFrom(t pack.BlobType, id *backend.ID, length uint, rd io.Reader) error { - debug.Log("Repo.SaveFrom", "save id %v (%v, %d bytes)", id.Str(), t, length) - if id == nil { - return errors.New("id is nil") - } - - buf, err := ioutil.ReadAll(rd) - if err != nil { - return err - } - - _, err = r.SaveAndEncrypt(t, buf, id) - if err != nil { - return err - } - - return nil -} - // SaveJSON serialises item as JSON and encrypts and saves it in a pack in the // backend as type t. func (r *Repository) SaveJSON(t pack.BlobType, item interface{}) (backend.ID, error) { diff --git a/src/restic/repository/repository_test.go b/src/restic/repository/repository_test.go index 5bd595086..3df824771 100644 --- a/src/restic/repository/repository_test.go +++ b/src/restic/repository/repository_test.go @@ -117,8 +117,9 @@ func TestSaveFrom(t *testing.T) { id := backend.Hash(data) // save - err = repo.SaveFrom(pack.Data, &id, uint(size), bytes.NewReader(data)) + id2, err := repo.SaveAndEncrypt(pack.Data, data, &id) OK(t, err) + Equals(t, id, id2) OK(t, repo.Flush()) @@ -136,7 +137,7 @@ func TestSaveFrom(t *testing.T) { } } -func BenchmarkSaveFrom(t *testing.B) { +func BenchmarkSaveAndEncrypt(t *testing.B) { repo := SetupRepo() defer TeardownRepo(repo) @@ -153,7 +154,7 @@ func BenchmarkSaveFrom(t *testing.B) { for i := 0; i < t.N; i++ { // save - err = repo.SaveFrom(pack.Data, &id, uint(size), bytes.NewReader(data)) + _, err = repo.SaveAndEncrypt(pack.Data, data, &id) OK(t, err) } } diff --git a/src/restic/testing.go b/src/restic/testing.go index 7e54207cf..0703772a1 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -1,7 +1,6 @@ package restic import ( - "bytes" "fmt" "io" "math/rand" @@ -105,8 +104,7 @@ func saveFile(t testing.TB, repo *repository.Repository, rd io.Reader) (blobs ba t.Fatalf("unabel to save chunk in repo: %v", err) } - id := backend.Hash(chunk.Data) - err = repo.SaveFrom(pack.Data, &id, uint(len(chunk.Data)), bytes.NewReader(chunk.Data)) + id, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, nil) if err != nil { t.Fatalf("error saving chunk: %v", err) }