]> git.ipfire.org Git - pakfire.git/commitdiff
key: Refactor listing all keys
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 17:39:09 +0000 (17:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 17:39:09 +0000 (17:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/key.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/key.c
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
tests/libpakfire/key.c

index a3e307980422a33b568bf149658f9ad9dc56b2aa..d38cebb68c749a47643a566abbae17e787be91e0 100644 (file)
@@ -425,8 +425,15 @@ static PyObject* _import_keylist(PakfireObject* pakfire, struct pakfire_key** ke
 }
 
 static PyObject* Pakfire_get_keys(PakfireObject* self) {
-       struct pakfire_key** keys = pakfire_key_list(self->pakfire);
+       struct pakfire_key** keys = NULL;
 
+       int r = pakfire_list_keys(self->pakfire, &keys);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+#warning THIS LEAKS MEMORY
        return _import_keylist(self, keys);
 }
 
index 9c3a4908d67948f7f8b9591523f6f1763fc418e3..4a93e31feba211aac0572151913b2154f452270f 100644 (file)
@@ -32,8 +32,6 @@ typedef enum pakfire_key_export_mode {
        PAKFIRE_KEY_EXPORT_MODE_SECRET,
 } pakfire_key_export_mode_t;
 
-struct pakfire_key** pakfire_key_list(Pakfire pakfire);
-
 struct pakfire_key* pakfire_key_ref(struct pakfire_key* key);
 void pakfire_key_unref(struct pakfire_key* key);
 
index d519bc961bc23807d4593aad7ca9bc590d6badef..88e74031be1e5c34e58b70be018672801cb097ce 100644 (file)
@@ -27,6 +27,7 @@
 #include <sys/stat.h>
 #include <time.h>
 
+#include <pakfire/key.h>
 #include <pakfire/parser.h>
 #include <pakfire/repo.h>
 #include <pakfire/repolist.h>
@@ -57,6 +58,8 @@ int pakfire_refresh(Pakfire pakfire, int flags);
 
 int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags);
 
+int pakfire_list_keys(Pakfire pakfire, struct pakfire_key*** keys);
+
 int pakfire_copy_in(Pakfire pakfire, const char* src, const char* dst);
 int pakfire_copy_out(Pakfire pakfire, const char* src, const char* dst);
 
index ced2c7af538c83f7c00dec463c0c3e623f0b897e..7fc7586404ff222e7e74f88c9044f7b47ea29c45 100644 (file)
@@ -43,61 +43,6 @@ struct pakfire_key {
        gpgme_key_t gpgkey;
 };
 
-static size_t pakfire_count_keys(Pakfire pakfire) {
-       gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
-
-       size_t count = 0;
-
-       gpgme_key_t key = NULL;
-       gpgme_error_t error = gpgme_op_keylist_start(gpgctx, NULL, 0);
-       while (!error) {
-               error = gpgme_op_keylist_next(gpgctx, &key);
-               if (error)
-                       break;
-
-               count++;
-
-               gpgme_key_release(key);
-       }
-
-       DEBUG(pakfire, "%zu key(s) in keystore\n", count);
-
-       return count;
-}
-
-PAKFIRE_EXPORT struct pakfire_key** pakfire_key_list(Pakfire pakfire) {
-       size_t count = pakfire_count_keys(pakfire);
-       if (count == 0)
-               return NULL;
-
-       struct pakfire_key* key = NULL;
-
-       gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
-
-       struct pakfire_key** first = calloc(count + 1, sizeof(struct pakfire_key*));
-       struct pakfire_key** list = first;
-
-       gpgme_key_t gpgkey = NULL;
-       gpgme_error_t error = gpgme_op_keylist_start(gpgctx, NULL, 0);
-       while (!error) {
-               error = gpgme_op_keylist_next(gpgctx, &gpgkey);
-               if (error)
-                       break;
-
-               pakfire_key_create(&key, pakfire, gpgkey);
-
-               // Add key to the list
-               *list++ = key;
-
-               gpgme_key_release(gpgkey);
-       }
-
-       // Last list item must be NULL
-       *list = NULL;
-
-       return first;
-}
-
 int pakfire_key_create(struct pakfire_key** key, Pakfire pakfire, gpgme_key_t gpgkey) {
        struct pakfire_key* k = calloc(1, sizeof(*k));
        if (!k)
index 3f3e6a04e2d37db74fc3cbb0c25fb4cb52e63cb1..a2b5b86308f0fc10f0aefdc36b66bb977146abc2 100644 (file)
@@ -36,6 +36,7 @@ global:
        pakfire_get_repo;
        pakfire_get_repos;
        pakfire_install;
+       pakfire_list_keys;
        pakfire_ref;
        pakfire_refresh;
        pakfire_search;
@@ -121,7 +122,6 @@ global:
        pakfire_key_get_uid;
        pakfire_key_import;
        pakfire_key_is_revoked;
-       pakfire_key_list;
        pakfire_key_ref;
        pakfire_key_unref;
 
index 7b0026ec4980fa1fe2c597752eaee9882e80713f..9010445314b45e4313ca48e805b8bd7ff1640c4c 100644 (file)
@@ -1136,6 +1136,64 @@ ERROR:
        return NULL;
 }
 
+PAKFIRE_EXPORT int pakfire_list_keys(Pakfire pakfire, struct pakfire_key*** keys) {
+       // Reset keys
+       *keys = NULL;
+
+       // Fetch GPGME context
+       gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
+       if (!gpgctx)
+               return 1;
+
+       struct pakfire_key* key = NULL;
+       gpgme_key_t gpgkey = NULL;
+       size_t length = 0;
+       int r = 1;
+
+       // Iterate over all keys and import them to the list
+       gpgme_error_t e = gpgme_op_keylist_start(gpgctx, NULL, 0);
+       if (e)
+               goto ERROR;
+
+       while (!e) {
+               e = gpgme_op_keylist_next(gpgctx, &gpgkey);
+               if (e)
+                       break;
+
+               // Create key
+               r = pakfire_key_create(&key, pakfire, gpgkey);
+               gpgme_key_unref(gpgkey);
+               if (r)
+                       goto ERROR;
+
+               // Append to keys
+               *keys = reallocarray(*keys, length + 2, sizeof(**keys));
+               if (!*keys) {
+                       r = 1;
+                       goto ERROR;
+               }
+
+               // Store key in array
+               (*keys)[length++] = key;
+
+               // Terminate the array
+               (*keys)[length] = NULL;
+       }
+
+       // Done
+       return 0;
+
+ERROR:
+       if (*keys) {
+               for (struct pakfire_key** key = *keys; *key; key++)
+                       pakfire_key_unref(*key);
+               free(*keys);
+               keys = NULL;
+       }
+
+       return r;
+}
+
 static int pakfire_foreach_repo(Pakfire pakfire,
                int (*func)(struct pakfire_repo* repo, int flags), int flags) {
        struct pakfire_repo* repo;
index b740dd07c8f017f10ce04a825f87e61212c182ca..dc797056742441d2d356ec6efe76549718cf2000 100644 (file)
 #include "key.h"
 
 static int test_init(const struct test* t) {
+       struct pakfire_key** keys = NULL;
+
        // Try loading any keys & delete them all
-       struct pakfire_key** keys = pakfire_key_list(t->pakfire);
+       ASSERT_SUCCESS(pakfire_list_keys(t->pakfire, &keys));
        while (keys && *keys) {
                struct pakfire_key* key = *keys++;
 
@@ -38,7 +40,7 @@ static int test_init(const struct test* t) {
        }
 
        // Load list of keys again
-       keys = pakfire_key_list(t->pakfire);
+       ASSERT_SUCCESS(pakfire_list_keys(t->pakfire, &keys));
 
        // Must be empty now
        ASSERT(keys == NULL);