From: Michael Tremer Date: Wed, 29 Nov 2017 14:57:02 +0000 (+0100) Subject: key: Count keys before generating list with all of them X-Git-Tag: 0.9.28~1285^2~1255 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=87dbfd1a7d1dcfd796a2f440cb09a2c195d3e5ef;p=pakfire.git key: Count keys before generating list with all of them Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index 0118c83aa..6f105a8e0 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -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; }