Add error checking

This commit is contained in:
Alexander Neumann 2015-11-18 20:33:20 +01:00
parent 2c95772a6a
commit 5b601f00b1
4 changed files with 25 additions and 15 deletions

View file

@ -556,7 +556,7 @@ func DecodeOldIndex(rd io.Reader) (idx *Index, err error) {
}
// LoadIndexWithDecoder loads the index and decodes it with fn.
func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Index, error)) (*Index, error) {
func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Index, error)) (idx *Index, err error) {
debug.Log("LoadIndexWithDecoder", "Loading index %v", id[:8])
idxID, err := backend.ParseID(id)
@ -568,9 +568,9 @@ func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Inde
if err != nil {
return nil, err
}
defer rd.Close()
defer closeOrErr(rd, &err)
idx, err := fn(rd)
idx, err = fn(rd)
if err != nil {
debug.Log("LoadIndexWithDecoder", "error while decoding index %v: %v", id, err)
return nil, err

View file

@ -129,7 +129,7 @@ func TestIndexSerialize(t *testing.T) {
"index not final after encoding")
id := randomID()
idx.SetID(id)
OK(t, idx.SetID(id))
id2, err := idx.ID()
Assert(t, id2.Equal(id),
"wrong ID returned: want %v, got %v", id, id2)

View file

@ -112,23 +112,23 @@ func SearchKey(s *Repository, password string) (*Key, error) {
}
// LoadKey loads a key from the backend.
func LoadKey(s *Repository, name string) (*Key, error) {
func LoadKey(s *Repository, name string) (k *Key, err error) {
// extract data from repo
rd, err := s.be.Get(backend.Key, name)
if err != nil {
return nil, err
}
defer rd.Close()
defer closeOrErr(rd, &err)
// restore json
dec := json.NewDecoder(rd)
k := Key{}
err = dec.Decode(&k)
k = new(Key)
err = dec.Decode(k)
if err != nil {
return nil, err
}
return &k, nil
return k, nil
}
// AddKey adds a new key to an already existing repository.

View file

@ -143,19 +143,29 @@ func (r *Repository) LoadBlob(t pack.BlobType, id backend.ID, plaintextBuf []byt
return plaintextBuf, nil
}
// closeOrErr calls cl.Close() and sets err to the returned error value if
// itself is not yet set.
func closeOrErr(cl io.Closer, err *error) {
e := cl.Close()
if *err != nil {
return
}
*err = e
}
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
// the item.
func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) error {
func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) (err error) {
// load blob from backend
rd, err := r.be.Get(t, id.String())
if err != nil {
return err
}
defer rd.Close()
defer closeOrErr(rd, &err)
// decrypt
decryptRd, err := crypto.DecryptFrom(r.key, rd)
defer decryptRd.Close()
defer closeOrErr(decryptRd, &err)
if err != nil {
return err
}
@ -172,7 +182,7 @@ func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interf
// LoadJSONPack calls LoadBlob() to load a blob from the backend, decrypt the
// data and afterwards call json.Unmarshal on the item.
func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) error {
func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) (err error) {
// lookup pack
blob, err := r.idx.Lookup(id)
if err != nil {
@ -184,11 +194,11 @@ func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface
if err != nil {
return err
}
defer rd.Close()
defer closeOrErr(rd, &err)
// decrypt
decryptRd, err := crypto.DecryptFrom(r.key, rd)
defer decryptRd.Close()
defer closeOrErr(decryptRd, &err)
if err != nil {
return err
}