repo: Use Save() instead of Create()

This commit is contained in:
Alexander Neumann 2016-01-24 18:50:41 +01:00
parent 35f9eae6c3
commit 01e40e62bf
1 changed files with 10 additions and 25 deletions

View File

@ -265,44 +265,29 @@ func (r *Repository) SaveJSON(t pack.BlobType, item interface{}) (backend.ID, er
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the // SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
// backend as type t, without a pack. It returns the storage hash. // backend as type t, without a pack. It returns the storage hash.
func (r *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend.ID, error) { func (r *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend.ID, error) {
// create file debug.Log("Repo.SaveJSONUnpacked", "save new blob %v", t)
blob, err := r.be.Create() plaintext, err := json.Marshal(item)
if err != nil {
return backend.ID{}, err
}
debug.Log("Repo.SaveJSONUnpacked", "create new blob %v", t)
// hash
hw := backend.NewHashingWriter(blob, sha256.New())
// encrypt blob
ewr := crypto.EncryptTo(r.key, hw)
enc := json.NewEncoder(ewr)
err = enc.Encode(item)
if err != nil { if err != nil {
return backend.ID{}, fmt.Errorf("json.Encode: %v", err) return backend.ID{}, fmt.Errorf("json.Encode: %v", err)
} }
err = ewr.Close() ciphertext := make([]byte, len(plaintext)+crypto.Extension)
ciphertext, err = crypto.Encrypt(r.key, ciphertext, plaintext)
if err != nil { if err != nil {
return backend.ID{}, err return backend.ID{}, err
} }
// finalize blob in the backend id := backend.Hash(ciphertext)
hash := hw.Sum(nil) h := backend.Handle{Type: t, Name: id.String()}
sid := backend.ID{}
copy(sid[:], hash)
err = blob.Finalize(t, sid.String()) err = r.be.Save(h, ciphertext)
if err != nil { if err != nil {
debug.Log("Repo.SaveJSONUnpacked", "error saving blob %v as %v: %v", t, sid, err) debug.Log("Repo.SaveJSONUnpacked", "error saving blob %v: %v", h, err)
return backend.ID{}, err return backend.ID{}, err
} }
debug.Log("Repo.SaveJSONUnpacked", "new blob %v saved as %v", t, sid) debug.Log("Repo.SaveJSONUnpacked", "blob %v saved", h)
return id, nil
return sid, nil
} }
// Flush saves all remaining packs. // Flush saves all remaining packs.