diff --git a/repository/index.go b/repository/index.go index 093667336..87a53c8ac 100644 --- a/repository/index.go +++ b/repository/index.go @@ -594,33 +594,14 @@ func ConvertIndex(repo *Repository, id backend.ID) (backend.ID, error) { return id, err } - blob, err := repo.CreateEncryptedBlob(backend.Index) - if err != nil { - return id, err - } - + buf := bytes.NewBuffer(nil) idx.supersedes = backend.IDs{id} - err = idx.Encode(blob) + err = idx.Encode(buf) if err != nil { debug.Log("ConvertIndex", "oldIdx.Encode() returned error: %v", err) return id, err } - err = blob.Close() - if err != nil { - debug.Log("ConvertIndex", "blob.Close() returned error: %v", err) - return id, err - } - - newID := blob.ID() - debug.Log("ConvertIndex", "index %v converted to new format as %v", id.Str(), newID.Str()) - - err = repo.be.Remove(backend.Index, id.String()) - if err != nil { - debug.Log("ConvertIndex", "backend.Remove(%v) returned error: %v", id.Str(), err) - return id, err - } - - return newID, nil + return repo.SaveUnpacked(backend.Index, buf.Bytes()) } diff --git a/repository/repository.go b/repository/repository.go index 275d6fdee..1469bee3d 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -271,13 +271,19 @@ func (r *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend return backend.ID{}, fmt.Errorf("json.Encode: %v", err) } - ciphertext := make([]byte, len(plaintext)+crypto.Extension) - ciphertext, err = crypto.Encrypt(r.key, ciphertext, plaintext) + return r.SaveUnpacked(t, plaintext) +} + +// SaveUnpacked encrypts data and stores it in the backend. Returned is the +// storage hash. +func (r *Repository) SaveUnpacked(t backend.Type, p []byte) (id backend.ID, err error) { + ciphertext := make([]byte, len(p)+crypto.Extension) + ciphertext, err = r.Encrypt(ciphertext, p) if err != nil { return backend.ID{}, err } - id := backend.Hash(ciphertext) + id = backend.Hash(ciphertext) h := backend.Handle{Type: t, Name: id.String()} err = r.be.Save(h, ciphertext) @@ -377,26 +383,16 @@ func (bw *BlobWriter) ID() backend.ID { return bw.id } -// SaveIndex saves an index to repo's backend. +// SaveIndex saves an index in the repository. func SaveIndex(repo *Repository, index *Index) (backend.ID, error) { - blob, err := repo.CreateEncryptedBlob(backend.Index) + buf := bytes.NewBuffer(nil) + + err := index.Finalize(buf) if err != nil { return backend.ID{}, err } - err = index.Finalize(blob) - if err != nil { - return backend.ID{}, err - } - - err = blob.Close() - if err != nil { - return backend.ID{}, err - } - - sid := blob.ID() - err = index.SetID(sid) - return sid, err + return repo.SaveUnpacked(backend.Index, buf.Bytes()) } // saveIndex saves all indexes in the backend.