]> git.ipfire.org Git - pakfire.git/commitdiff
key: Refactor finding keys in local keystore
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 20:24:49 +0000 (20:24 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 20:25:28 +0000 (20:25 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/key.c

index 5659efaa7d51ae754229d587404ee6ae6ca747a0..fc5e4c0ae7af3ddb325a69d44a138741502e0da7 100644 (file)
@@ -83,35 +83,46 @@ PAKFIRE_EXPORT void pakfire_key_unref(struct pakfire_key* key) {
        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) {
@@ -312,15 +323,17 @@ PAKFIRE_EXPORT struct pakfire_key** pakfire_key_import(Pakfire pakfire, const ch
                        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;
                        }