From: Michael Tremer Date: Wed, 7 Jul 2021 16:37:28 +0000 (+0000) Subject: keys: Change type from PakfireKey to struct pakfire_key X-Git-Tag: 0.9.28~1091 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf7aa31bd3f5f8a79d6d1557a8d904c952d5630c;p=pakfire.git keys: Change type from PakfireKey to struct pakfire_key Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/key.c b/src/_pakfire/key.c index 457d34c38..a5e561865 100644 --- a/src/_pakfire/key.c +++ b/src/_pakfire/key.c @@ -26,7 +26,7 @@ #include "key.h" #include "pakfire.h" -PyObject* new_key(PyTypeObject* type, PakfireKey key) { +PyObject* new_key(PyTypeObject* type, struct pakfire_key* key) { KeyObject* self = (KeyObject *)type->tp_alloc(type, 0); if (self) { self->key = pakfire_key_ref(key); diff --git a/src/_pakfire/key.h b/src/_pakfire/key.h index fa6875acc..fe637f301 100644 --- a/src/_pakfire/key.h +++ b/src/_pakfire/key.h @@ -27,11 +27,11 @@ typedef struct { PyObject_HEAD - PakfireKey key; + struct pakfire_key* key; } KeyObject; extern PyTypeObject KeyType; -PyObject* new_key(PyTypeObject* type, PakfireKey key); +PyObject* new_key(PyTypeObject* type, struct pakfire_key* key); #endif /* PYTHON_PAKFIRE_KEY_H */ diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 7a52b2549..a3e307980 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -405,11 +405,11 @@ static PyObject* Pakfire_update(PakfireObject* self, PyObject* args, PyObject* k Py_RETURN_NONE; } -static PyObject* _import_keylist(PakfireObject* pakfire, PakfireKey* keys) { +static PyObject* _import_keylist(PakfireObject* pakfire, struct pakfire_key** keys) { PyObject* list = PyList_New(0); while (keys && *keys) { - PakfireKey key = *keys++; + struct pakfire_key* key = *keys++; PyObject* object = new_key(&KeyType, key); PyList_Append(list, object); @@ -425,7 +425,7 @@ static PyObject* _import_keylist(PakfireObject* pakfire, PakfireKey* keys) { } static PyObject* Pakfire_get_keys(PakfireObject* self) { - PakfireKey* keys = pakfire_key_list(self->pakfire); + struct pakfire_key** keys = pakfire_key_list(self->pakfire); return _import_keylist(self, keys); } @@ -436,7 +436,7 @@ static PyObject* Pakfire_get_key(PakfireObject* self, PyObject* args) { if (!PyArg_ParseTuple(args, "s", &pattern)) return NULL; - PakfireKey key = pakfire_key_get(self->pakfire, pattern); + struct pakfire_key* key = pakfire_key_get(self->pakfire, pattern); if (!key) Py_RETURN_NONE; @@ -449,7 +449,7 @@ static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args) { if (!PyArg_ParseTuple(args, "s", &userid)) return NULL; - PakfireKey key = pakfire_key_generate(self->pakfire, userid); + struct pakfire_key* key = pakfire_key_generate(self->pakfire, userid); assert(key); return new_key(&KeyType, key); @@ -461,7 +461,7 @@ static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) { if (!PyArg_ParseTuple(args, "s", &data)) return NULL; - PakfireKey* keys = pakfire_key_import(self->pakfire, data); + struct pakfire_key** keys = pakfire_key_import(self->pakfire, data); if (!keys) return NULL; // TODO Raise error from errno diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index cf61f932f..bb1901092 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -89,7 +89,7 @@ struct _PakfireArchive { struct _PakfireArchiveSignature { Pakfire pakfire; - PakfireKey key; + struct pakfire_key* key; char* sigdata; int nrefs; }; diff --git a/src/libpakfire/include/pakfire/key.h b/src/libpakfire/include/pakfire/key.h index 36111c319..f9d73d03b 100644 --- a/src/libpakfire/include/pakfire/key.h +++ b/src/libpakfire/include/pakfire/key.h @@ -24,6 +24,8 @@ #include #include +struct pakfire_key; + #include typedef enum pakfire_key_export_mode { @@ -31,42 +33,36 @@ typedef enum pakfire_key_export_mode { PAKFIRE_KEY_EXPORT_MODE_SECRET, } pakfire_key_export_mode_t; -PakfireKey* pakfire_key_list(Pakfire pakfire); +struct pakfire_key** pakfire_key_list(Pakfire pakfire); -PakfireKey pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey); -PakfireKey pakfire_key_ref(PakfireKey key); -void pakfire_key_unref(PakfireKey key); +struct pakfire_key* pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey); +struct pakfire_key* pakfire_key_ref(struct pakfire_key* key); +void pakfire_key_unref(struct pakfire_key* key); -PakfireKey pakfire_key_get(Pakfire pakfire, const char* fingerprint); -int pakfire_key_delete(PakfireKey key); +struct pakfire_key* pakfire_key_get(Pakfire pakfire, const char* fingerprint); +int pakfire_key_delete(struct pakfire_key* key); // Access key properties -const char* pakfire_key_get_fingerprint(PakfireKey key); -const char* pakfire_key_get_uid(PakfireKey key); -const char* pakfire_key_get_name(PakfireKey key); -const char* pakfire_key_get_email(PakfireKey key); -const char* pakfire_key_get_pubkey_algo(PakfireKey key); -size_t pakfire_key_get_pubkey_length(PakfireKey key); -time_t pakfire_key_get_created(PakfireKey key); -time_t pakfire_key_get_expires(PakfireKey key); -int pakfire_key_is_revoked(PakfireKey key); +const char* pakfire_key_get_fingerprint(struct pakfire_key* key); +const char* pakfire_key_get_uid(struct pakfire_key* key); +const char* pakfire_key_get_name(struct pakfire_key* key); +const char* pakfire_key_get_email(struct pakfire_key* key); +const char* pakfire_key_get_pubkey_algo(struct pakfire_key* key); +size_t pakfire_key_get_pubkey_length(struct pakfire_key* key); +time_t pakfire_key_get_created(struct pakfire_key* key); +time_t pakfire_key_get_expires(struct pakfire_key* key); +int pakfire_key_is_revoked(struct pakfire_key* key); -PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid); -char* pakfire_key_export(PakfireKey key, pakfire_key_export_mode_t mode); -PakfireKey* pakfire_key_import(Pakfire pakfire, const char* data); +struct pakfire_key* pakfire_key_generate(Pakfire pakfire, const char* userid); +char* pakfire_key_export(struct pakfire_key* key, pakfire_key_export_mode_t mode); +struct pakfire_key** pakfire_key_import(Pakfire pakfire, const char* data); -char* pakfire_key_dump(PakfireKey key); +char* pakfire_key_dump(struct pakfire_key* key); #ifdef PAKFIRE_PRIVATE gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire); -struct _PakfireKey { - Pakfire pakfire; - gpgme_key_t gpgkey; - int nrefs; -}; - #endif #endif /* PAKFIRE_KEY_H */ diff --git a/src/libpakfire/include/pakfire/types.h b/src/libpakfire/include/pakfire/types.h index a450143b9..97332dc41 100644 --- a/src/libpakfire/include/pakfire/types.h +++ b/src/libpakfire/include/pakfire/types.h @@ -25,6 +25,5 @@ typedef struct _Pakfire* Pakfire; typedef struct _PakfireArchive* PakfireArchive; typedef struct _PakfireArchiveSignature* PakfireArchiveSignature; typedef struct _PakfireFilelist* PakfireFilelist; -typedef struct _PakfireKey* PakfireKey; #endif /* PAKFIRE_TYPES_H */ diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index 64a16f7f2..9ac9d0a55 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -36,6 +36,14 @@ #define DEFAULT_KEY_SIZE "rsa4096" + +struct pakfire_key { + Pakfire pakfire; + int nrefs; + + gpgme_key_t gpgkey; +}; + gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire) { static int gpg_initialized = 0; gpgme_error_t error; @@ -123,15 +131,15 @@ static size_t pakfire_count_keys(Pakfire pakfire) { return count; } -PAKFIRE_EXPORT PakfireKey* pakfire_key_list(Pakfire pakfire) { +PAKFIRE_EXPORT struct pakfire_key** 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); - PakfireKey* first = calloc(count + 1, sizeof(PakfireKey)); - PakfireKey* list = first; + 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); @@ -154,8 +162,8 @@ PAKFIRE_EXPORT PakfireKey* pakfire_key_list(Pakfire pakfire) { return first; } -PAKFIRE_EXPORT PakfireKey pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey) { - PakfireKey key = calloc(1, sizeof(*key)); +PAKFIRE_EXPORT struct pakfire_key* pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey) { + struct pakfire_key* key = calloc(1, sizeof(*key)); if (key) { key->nrefs = 1; @@ -168,30 +176,30 @@ PAKFIRE_EXPORT PakfireKey pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey return key; } -static void pakfire_key_free(PakfireKey key) { +static void pakfire_key_free(struct pakfire_key* key) { pakfire_unref(key->pakfire); gpgme_key_unref(key->gpgkey); free(key); } -PAKFIRE_EXPORT PakfireKey pakfire_key_ref(PakfireKey key) { +PAKFIRE_EXPORT struct pakfire_key* pakfire_key_ref(struct pakfire_key* key) { ++key->nrefs; return key; } -PAKFIRE_EXPORT void pakfire_key_unref(PakfireKey key) { +PAKFIRE_EXPORT void pakfire_key_unref(struct pakfire_key* key) { if (--key->nrefs > 0) return; pakfire_key_free(key); } -static PakfireKey __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx, const char* fingerprint) { +static struct pakfire_key* __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx, const char* fingerprint) { DEBUG(pakfire, "Seaching for key with fingerprint %s\n", fingerprint); - PakfireKey key = NULL; + struct pakfire_key* key = NULL; gpgme_key_t gpgkey = NULL; gpgme_error_t error = gpgme_get_key(gpgctx, fingerprint, &gpgkey, 0); @@ -213,16 +221,16 @@ static PakfireKey __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx, const c return key; } -PAKFIRE_EXPORT PakfireKey pakfire_key_get(Pakfire pakfire, const char* fingerprint) { +PAKFIRE_EXPORT struct pakfire_key* pakfire_key_get(Pakfire pakfire, const char* fingerprint) { gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); - PakfireKey key = __pakfire_get_key(pakfire, gpgctx, fingerprint); + struct pakfire_key* key = __pakfire_get_key(pakfire, gpgctx, fingerprint); gpgme_release(gpgctx); return key; } -PAKFIRE_EXPORT int pakfire_key_delete(PakfireKey key) { +PAKFIRE_EXPORT int pakfire_key_delete(struct pakfire_key* key) { gpgme_ctx_t gpgctx = pakfire_get_gpgctx(key->pakfire); int r = 0; @@ -235,23 +243,23 @@ PAKFIRE_EXPORT int pakfire_key_delete(PakfireKey key) { return r; } -PAKFIRE_EXPORT const char* pakfire_key_get_fingerprint(PakfireKey key) { +PAKFIRE_EXPORT const char* pakfire_key_get_fingerprint(struct pakfire_key* key) { return key->gpgkey->fpr; } -PAKFIRE_EXPORT const char* pakfire_key_get_uid(PakfireKey key) { +PAKFIRE_EXPORT const char* pakfire_key_get_uid(struct pakfire_key* key) { return key->gpgkey->uids->uid; } -PAKFIRE_EXPORT const char* pakfire_key_get_name(PakfireKey key) { +PAKFIRE_EXPORT const char* pakfire_key_get_name(struct pakfire_key* key) { return key->gpgkey->uids->name; } -PAKFIRE_EXPORT const char* pakfire_key_get_email(PakfireKey key) { +PAKFIRE_EXPORT const char* pakfire_key_get_email(struct pakfire_key* key) { return key->gpgkey->uids->email; } -PAKFIRE_EXPORT const char* pakfire_key_get_pubkey_algo(PakfireKey key) { +PAKFIRE_EXPORT const char* pakfire_key_get_pubkey_algo(struct pakfire_key* key) { switch (key->gpgkey->subkeys->pubkey_algo) { case GPGME_PK_RSA: case GPGME_PK_RSA_E: @@ -281,23 +289,23 @@ PAKFIRE_EXPORT const char* pakfire_key_get_pubkey_algo(PakfireKey key) { return NULL; } -PAKFIRE_EXPORT size_t pakfire_key_get_pubkey_length(PakfireKey key) { +PAKFIRE_EXPORT size_t pakfire_key_get_pubkey_length(struct pakfire_key* key) { return key->gpgkey->subkeys->length; } -PAKFIRE_EXPORT time_t pakfire_key_get_created(PakfireKey key) { +PAKFIRE_EXPORT time_t pakfire_key_get_created(struct pakfire_key* key) { return key->gpgkey->subkeys->timestamp; } -PAKFIRE_EXPORT time_t pakfire_key_get_expires(PakfireKey key) { +PAKFIRE_EXPORT time_t pakfire_key_get_expires(struct pakfire_key* key) { return key->gpgkey->subkeys->expires; } -PAKFIRE_EXPORT int pakfire_key_is_revoked(PakfireKey key) { +PAKFIRE_EXPORT int pakfire_key_is_revoked(struct pakfire_key* key) { return key->gpgkey->subkeys->revoked; } -PAKFIRE_EXPORT PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid) { +PAKFIRE_EXPORT struct pakfire_key* pakfire_key_generate(Pakfire pakfire, const char* userid) { gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); unsigned int flags = 0; @@ -328,7 +336,7 @@ PAKFIRE_EXPORT PakfireKey pakfire_key_generate(Pakfire pakfire, const char* user return pakfire_key_get(pakfire, result->fpr); } -PAKFIRE_EXPORT char* pakfire_key_export(PakfireKey key, pakfire_key_export_mode_t mode) { +PAKFIRE_EXPORT char* pakfire_key_export(struct pakfire_key* key, pakfire_key_export_mode_t mode) { gpgme_ctx_t gpgctx = pakfire_get_gpgctx(key->pakfire); gpgme_export_mode_t gpgmode = 0; @@ -382,7 +390,7 @@ FAIL: return NULL; } -PAKFIRE_EXPORT PakfireKey* pakfire_key_import(Pakfire pakfire, const char* data) { +PAKFIRE_EXPORT struct pakfire_key** pakfire_key_import(Pakfire pakfire, const char* data) { gpgme_error_t error; gpgme_data_t keydata; @@ -411,12 +419,12 @@ PAKFIRE_EXPORT PakfireKey* pakfire_key_import(Pakfire pakfire, const char* data) if (!status) return NULL; - PakfireKey* head = calloc(result->imported + 1, sizeof(*head)); - PakfireKey* list = head; + struct pakfire_key** head = calloc(result->imported + 1, sizeof(*head)); + struct pakfire_key** list = head; // Retrieve all imported keys while (status) { - PakfireKey key = __pakfire_get_key(pakfire, gpgctx, status->fpr); + 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); @@ -454,7 +462,7 @@ FAIL: return NULL; } -PAKFIRE_EXPORT char* pakfire_key_dump(PakfireKey key) { +PAKFIRE_EXPORT char* pakfire_key_dump(struct pakfire_key* key) { char* s = ""; time_t created = pakfire_key_get_created(key); diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 3905f0fac..b740dd07c 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -29,9 +29,9 @@ static int test_init(const struct test* t) { // Try loading any keys & delete them all - PakfireKey* keys = pakfire_key_list(t->pakfire); + struct pakfire_key** keys = pakfire_key_list(t->pakfire); while (keys && *keys) { - PakfireKey key = *keys++; + struct pakfire_key* key = *keys++; pakfire_key_delete(key); pakfire_key_unref(key); @@ -49,14 +49,14 @@ static int test_init(const struct test* t) { static int test_import_export(const struct test* t) { // Try to delete the key just in case it // has been imported before - PakfireKey key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT); + struct pakfire_key* key = pakfire_key_get(t->pakfire, TEST_KEY_FINGERPRINT); if (key) { pakfire_key_delete(key); pakfire_key_unref(key); } // Import a key - PakfireKey* keys = pakfire_key_import(t->pakfire, TEST_KEY_DATA); + struct pakfire_key** keys = pakfire_key_import(t->pakfire, TEST_KEY_DATA); // We should have a list with precisely one key object ASSERT(keys);