pakfire_key_free(key);
}
-static struct pakfire_key* __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx, const char* fingerprint) {
+static int pakfire_find_key(struct pakfire_key** key, Pakfire pakfire, const char* fingerprint) {
+ // Reset key
+ *key = NULL;
+
+ // Fetch GPGME context
+ gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
+ if (!gpgctx)
+ return 1;
+
DEBUG(pakfire, "Seaching for key with fingerprint %s\n", fingerprint);
- struct pakfire_key* key = NULL;
gpgme_key_t gpgkey = NULL;
+ int r;
gpgme_error_t error = gpgme_get_key(gpgctx, fingerprint, &gpgkey, 0);
switch (gpg_error(error)) {
+ // Create a key object if we found something
case GPG_ERR_NO_ERROR:
- pakfire_key_create(&key, pakfire, gpgkey);
+ r = pakfire_key_create(key, pakfire, gpgkey);
gpgme_key_unref(gpgkey);
+ if (r)
+ return r;
break;
+ // Nothing found
case GPG_ERR_EOF:
- DEBUG(pakfire, "Nothing found\n");
- break;
-
- default:
- DEBUG(pakfire, "Could not find key: %s\n", gpgme_strerror(error));
break;
}
- return key;
+ return 0;
}
PAKFIRE_EXPORT struct pakfire_key* pakfire_key_get(Pakfire pakfire, const char* fingerprint) {
- gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
+ struct pakfire_key* key = NULL;
+
+ int r = pakfire_find_key(&key, pakfire, fingerprint);
+ if (r)
+ return NULL;
- return __pakfire_get_key(pakfire, gpgctx, fingerprint);
+ return key;
}
PAKFIRE_EXPORT int pakfire_key_delete(struct pakfire_key* key) {
struct pakfire_key** list = head;
// Retrieve all imported keys
+ struct pakfire_key* key;
while (status) {
- struct pakfire_key* key = __pakfire_get_key(pakfire, gpgctx, status->fpr);
- if (key) {
- const char* fingerprint = pakfire_key_get_fingerprint(key);
- INFO(pakfire, "Imported key %s\n", fingerprint);
-
- // Append key to list
- *list++ = key;
- }
+ int r = pakfire_find_key(&key, pakfire, status->fpr);
+ if (r)
+ goto FAIL;
+
+ const char* fingerprint = pakfire_key_get_fingerprint(key);
+ INFO(pakfire, "Imported key %s\n", fingerprint);
+
+ // Append key to list
+ *list++ = key;
status = status->next;
}