]> git.ipfire.org Git - pakfire.git/commitdiff
key: Count keys before generating list with all of them
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Nov 2017 14:57:02 +0000 (15:57 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 29 Nov 2017 14:57:56 +0000 (15:57 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/key.c

index 0118c83aa8de863e1fa5ae163c07d1854866e677..6f105a8e057b94c3a55e866c8f94186ec7af3a17 100644 (file)
@@ -84,11 +84,37 @@ FAIL:
        return NULL;
 }
 
+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("%zu key(s) in keystore\n", count);
+       gpgme_release(gpgctx);
+
+       return count;
+}
+
 PakfireKey* pakfire_key_list(Pakfire pakfire) {
+       size_t count = pakfire_count_keys(pakfire);
+       if (count == 0)
+               return NULL;
+
        gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire);
-       assert(gpgctx);
 
-       PakfireKey* first = pakfire_calloc(1, sizeof(PakfireKey));
+       PakfireKey* first = pakfire_calloc(count + 1, sizeof(PakfireKey));
        PakfireKey* list = first;
 
        gpgme_key_t gpgkey = NULL;
@@ -107,6 +133,8 @@ PakfireKey* pakfire_key_list(Pakfire pakfire) {
        // Last list item must be NULL
        *list = NULL;
 
+       gpgme_release(gpgctx);
+
        return first;
 }