}
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);
}
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);
#include <sys/stat.h>
#include <time.h>
+#include <pakfire/key.h>
#include <pakfire/parser.h>
#include <pakfire/repo.h>
#include <pakfire/repolist.h>
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);
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)
pakfire_get_repo;
pakfire_get_repos;
pakfire_install;
+ pakfire_list_keys;
pakfire_ref;
pakfire_refresh;
pakfire_search;
pakfire_key_get_uid;
pakfire_key_import;
pakfire_key_is_revoked;
- pakfire_key_list;
pakfire_key_ref;
pakfire_key_unref;
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;
#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++;
}
// 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);