Commit b8df1182 authored by Ivar Derksen's avatar Ivar Derksen Committed by Sietse Ringers

Fixed error in deleting attributes from bbolt

parent 1b9835ad
...@@ -330,11 +330,13 @@ func (client *Client) RemoveAllCredentials() error { ...@@ -330,11 +330,13 @@ func (client *Client) RemoveAllCredentials() error {
if attrs.CredentialType() != nil { if attrs.CredentialType() != nil {
removed[attrs.CredentialType().Identifier()] = attrs.Strings() removed[attrs.CredentialType().Identifier()] = attrs.Strings()
} }
_ = client.storage.DeleteSignature(attrs)
} }
} }
client.attributes = map[irma.CredentialTypeIdentifier][]*irma.AttributeList{} client.attributes = map[irma.CredentialTypeIdentifier][]*irma.AttributeList{}
if err := client.storage.StoreAllAttributes(client.attributes); err != nil { if err := client.storage.DeleteAllAttributes(); err != nil {
return err
}
if err := client.storage.DeleteAllSignatures(); err != nil {
return err return err
} }
...@@ -1026,6 +1028,9 @@ func (client *Client) ConfigurationUpdated(downloaded *irma.IrmaIdentifierSet) e ...@@ -1026,6 +1028,9 @@ func (client *Client) ConfigurationUpdated(downloaded *irma.IrmaIdentifierSet) e
attrs[j] = big.NewInt(0) attrs[j] = big.NewInt(0)
} }
client.attributes[id][i].Ints = attrs client.attributes[id][i].Ints = attrs
if err := client.storage.StoreAttributes(id, client.attributes[id]); err != nil {
return err
}
if _, contains = client.credentialsCache[id]; !contains { if _, contains = client.credentialsCache[id]; !contains {
continue continue
...@@ -1041,5 +1046,5 @@ func (client *Client) ConfigurationUpdated(downloaded *irma.IrmaIdentifierSet) e ...@@ -1041,5 +1046,5 @@ func (client *Client) ConfigurationUpdated(downloaded *irma.IrmaIdentifierSet) e
} }
} }
return client.storage.StoreAllAttributes(client.attributes) return nil
} }
...@@ -69,6 +69,15 @@ func (s *storage) txStore(tx *bbolt.Tx, key string, value interface{}, bucketNam ...@@ -69,6 +69,15 @@ func (s *storage) txStore(tx *bbolt.Tx, key string, value interface{}, bucketNam
return b.Put([]byte(key), btsValue) return b.Put([]byte(key), btsValue)
} }
func (s *storage) txDelete(tx *bbolt.Tx, key string, bucketName string) error {
b, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
return err
}
return b.Delete([]byte(key))
}
func (s *storage) txLoad(tx *bbolt.Tx, key string, dest interface{}, bucketName string) (found bool, err error) { func (s *storage) txLoad(tx *bbolt.Tx, key string, dest interface{}, bucketName string) (found bool, err error) {
b := tx.Bucket([]byte(bucketName)) b := tx.Bucket([]byte(bucketName))
if b == nil { if b == nil {
...@@ -91,11 +100,13 @@ func (s *storage) load(key string, dest interface{}, bucketName string) (found b ...@@ -91,11 +100,13 @@ func (s *storage) load(key string, dest interface{}, bucketName string) (found b
func (s *storage) DeleteSignature(attrs *irma.AttributeList) error { func (s *storage) DeleteSignature(attrs *irma.AttributeList) error {
return s.db.Update(func(tx *bbolt.Tx) error { return s.db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte(signaturesBucket)) return s.txDelete(tx, attrs.Hash(), signaturesBucket)
if err != nil { })
return err }
}
return b.Delete([]byte(attrs.Hash())) func (s *storage) DeleteAllSignatures() error {
return s.db.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket([]byte(signaturesBucket))
}) })
} }
...@@ -133,29 +144,19 @@ func (s *storage) StoreAttributes(credTypeID irma.CredentialTypeIdentifier, attr ...@@ -133,29 +144,19 @@ func (s *storage) StoreAttributes(credTypeID irma.CredentialTypeIdentifier, attr
func (s *storage) TxStoreAttributes(tx *bbolt.Tx, credTypeID irma.CredentialTypeIdentifier, func (s *storage) TxStoreAttributes(tx *bbolt.Tx, credTypeID irma.CredentialTypeIdentifier,
attrlistlist []*irma.AttributeList) error { attrlistlist []*irma.AttributeList) error {
// If no credentials are left of a certain type, the full entry can be deleted.
if len(attrlistlist) == 0 {
return s.txDelete(tx, credTypeID.String(), attributesBucket)
}
return s.txStore(tx, credTypeID.String(), attrlistlist, attributesBucket) return s.txStore(tx, credTypeID.String(), attrlistlist, attributesBucket)
} }
func (s *storage) StoreAllAttributes( func (s *storage) DeleteAllAttributes() error {
attributes map[irma.CredentialTypeIdentifier][]*irma.AttributeList) error {
return s.db.Update(func(tx *bbolt.Tx) error { return s.db.Update(func(tx *bbolt.Tx) error {
return s.TxStoreAllAttributes(tx, attributes) return tx.DeleteBucket([]byte(attributesBucket))
}) })
} }
func (s *storage) TxStoreAllAttributes(tx *bbolt.Tx,
attrs map[irma.CredentialTypeIdentifier][]*irma.AttributeList) error {
for credTypeID, attrlistlist := range attrs {
err := s.TxStoreAttributes(tx, credTypeID, attrlistlist)
if err != nil {
return err
}
}
return nil
}
func (s *storage) StoreKeyshareServers(keyshareServers map[irma.SchemeManagerIdentifier]*keyshareServer) error { func (s *storage) StoreKeyshareServers(keyshareServers map[irma.SchemeManagerIdentifier]*keyshareServer) error {
return s.db.Update(func(tx *bbolt.Tx) error { return s.db.Update(func(tx *bbolt.Tx) error {
return s.TxStoreKeyshareServers(tx, keyshareServers) return s.TxStoreKeyshareServers(tx, keyshareServers)
...@@ -163,7 +164,7 @@ func (s *storage) StoreKeyshareServers(keyshareServers map[irma.SchemeManagerIde ...@@ -163,7 +164,7 @@ func (s *storage) StoreKeyshareServers(keyshareServers map[irma.SchemeManagerIde
} }
func (s *storage) TxStoreKeyshareServers(tx *bbolt.Tx, keyshareServers map[irma.SchemeManagerIdentifier]*keyshareServer) error { func (s *storage) TxStoreKeyshareServers(tx *bbolt.Tx, keyshareServers map[irma.SchemeManagerIdentifier]*keyshareServer) error {
return s.txStore(tx, kssKey, &keyshareServers, userdataBucket) return s.txStore(tx, kssKey, keyshareServers, userdataBucket)
} }
func (s *storage) AddLogEntry(entry *LogEntry) error { func (s *storage) AddLogEntry(entry *LogEntry) error {
...@@ -276,7 +277,7 @@ func (s *storage) LoadAttributes() (list map[irma.CredentialTypeIdentifier][]*ir ...@@ -276,7 +277,7 @@ func (s *storage) LoadAttributes() (list map[irma.CredentialTypeIdentifier][]*ir
func (s *storage) LoadKeyshareServers() (ksses map[irma.SchemeManagerIdentifier]*keyshareServer, err error) { func (s *storage) LoadKeyshareServers() (ksses map[irma.SchemeManagerIdentifier]*keyshareServer, err error) {
ksses = make(map[irma.SchemeManagerIdentifier]*keyshareServer) ksses = make(map[irma.SchemeManagerIdentifier]*keyshareServer)
_, err = s.load(kssKey, &ksses, userdataBucket) _, err = s.load(kssKey, &ksses, userdataBucket)
return ksses, err return
} }
// Returns all logs stored before log with ID 'index' sorted from new to old with // Returns all logs stored before log with ID 'index' sorted from new to old with
......
...@@ -121,13 +121,13 @@ var clientUpdates = []func(client *Client) error{ ...@@ -121,13 +121,13 @@ var clientUpdates = []func(client *Client) error{
} }
return client.storage.db.Update(func(tx *bbolt.Tx) error { return client.storage.db.Update(func(tx *bbolt.Tx) error {
err = client.storage.TxStoreSecretKey(tx, sk) if err = client.storage.TxStoreSecretKey(tx, sk); err != nil {
if err != nil {
return err return err
} }
err := client.storage.TxStoreAllAttributes(tx, attrs) for credTypeID, attrslistlist := range attrs {
if err != nil { if err = client.storage.TxStoreAttributes(tx, credTypeID, attrslistlist); err != nil {
return err return err
}
} }
for hash, sig := range sigs { for hash, sig := range sigs {
err = client.storage.TxStoreSignature(tx, hash, sig) err = client.storage.TxStoreSignature(tx, hash, sig)
...@@ -135,12 +135,10 @@ var clientUpdates = []func(client *Client) error{ ...@@ -135,12 +135,10 @@ var clientUpdates = []func(client *Client) error{
return err return err
} }
} }
err = client.storage.TxStoreKeyshareServers(tx, ksses) if err = client.storage.TxStoreKeyshareServers(tx, ksses); err != nil {
if err != nil {
return err return err
} }
err = client.storage.TxStorePreferences(tx, prefs) if err = client.storage.TxStorePreferences(tx, prefs); err != nil {
if err != nil {
return err return err
} }
return client.storage.TxStoreUpdates(tx, updates) return client.storage.TxStoreUpdates(tx, updates)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment