From: Michael Tremer Date: Thu, 23 Nov 2017 17:34:05 +0000 (+0100) Subject: libpakfire: Make keys belong to Pakfire instead of PakfirePool X-Git-Tag: 0.9.28~1285^2~1294 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad62bb07d305e51f8d4ef87c108aeb663eb40929;p=pakfire.git libpakfire: Make keys belong to Pakfire instead of PakfirePool Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/key.c b/src/_pakfire/key.c index 67ca5a462..5486e487c 100644 --- a/src/_pakfire/key.c +++ b/src/_pakfire/key.c @@ -24,19 +24,20 @@ #include #include "key.h" +#include "pakfire.h" -static PyObject* Key_new_core(PyTypeObject* type, PoolObject* pool, PakfireKey key) { +static PyObject* Key_new_core(PyTypeObject* type, PakfireObject* pakfire, PakfireKey key) { KeyObject* self = (KeyObject *)type->tp_alloc(type, 0); if (self) { - self->pool = pool; + self->pakfire = pakfire; self->key = key; } return (PyObject *)self; } -PyObject* new_key(PoolObject* pool, PakfireKey key) { - return Key_new_core(&KeyType, pool, key); +PyObject* new_key(PakfireObject* pakfire, PakfireKey key) { + return Key_new_core(&KeyType, pakfire, key); } static PyObject* Key_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { @@ -47,20 +48,23 @@ static void Key_dealloc(KeyObject* self) { if (self->key) pakfire_key_free(self->key); + if (self->pakfire) + Py_DECREF(self->pakfire); + Py_TYPE(self)->tp_free((PyObject *)self); } static int Key_init(KeyObject* self, PyObject* args, PyObject* kwds) { - PyObject* pool; + PyObject* pakfire; const char* fingerprint = NULL; - if (!PyArg_ParseTuple(args, "O!s", &PoolType, &pool, &fingerprint)) + if (!PyArg_ParseTuple(args, "O!s", &PakfireType, &pakfire, &fingerprint)) return -1; - self->pool = (PoolObject *)pool; - Py_INCREF(self->pool); + self->pakfire = (PakfireObject *)pakfire; + Py_INCREF(self->pakfire); - self->key = pakfire_key_get(self->pool->pool, fingerprint); + self->key = pakfire_key_get(self->pakfire->pakfire, fingerprint); if (!self->key) return -1; diff --git a/src/_pakfire/key.h b/src/_pakfire/key.h index 9ffbe9a4e..2485159da 100644 --- a/src/_pakfire/key.h +++ b/src/_pakfire/key.h @@ -25,16 +25,16 @@ #include -#include "pool.h" +#include "pakfire.h" typedef struct { PyObject_HEAD - PoolObject* pool; + PakfireObject* pakfire; PakfireKey key; } KeyObject; extern PyTypeObject KeyType; -PyObject* new_key(PoolObject* pool, PakfireKey key); +PyObject* new_key(PakfireObject* pakfire, PakfireKey key); #endif /* PYTHON_PAKFIRE_KEY_H */ diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index b3d907c7d..6449e6b4f 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -21,7 +21,9 @@ #include #include +#include +#include "key.h" #include "pakfire.h" static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { @@ -73,7 +75,61 @@ static PyObject* Pakfire_get_arch(PakfireObject* self) { return PyUnicode_FromString(arch); } +static PyObject* Pakfire_get_keys(PakfireObject* self) { + PyObject* list = PyList_New(0); + + PakfireKey* keys = pakfire_key_list(self->pakfire); + while (keys && *keys) { + PakfireKey key = *keys++; + + PyObject* object = new_key(self, key); + PyList_Append(list, object); + Py_DECREF(object); + + pakfire_key_free(key); + } + + return list; +} + +static PyObject* Pakfire_get_key(PakfireObject* self, PyObject* args) { + const char* pattern = NULL; + + if (!PyArg_ParseTuple(args, "s", &pattern)) + return NULL; + + PakfireKey key = pakfire_key_get(self->pakfire, pattern); + if (!key) + Py_RETURN_NONE; + + return new_key(self, key); +} + +static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args) { + const char* userid = NULL; + + if (!PyArg_ParseTuple(args, "s", &userid)) + return NULL; + + PakfireKey key = pakfire_key_generate(self->pakfire, userid); + assert(key); + + return new_key(self, key); +} + static struct PyMethodDef Pakfire_methods[] = { + { + "generate_key", + (PyCFunction)Pakfire_generate_key, + METH_VARARGS, + NULL + }, + { + "get_key", + (PyCFunction)Pakfire_get_key, + METH_VARARGS, + NULL + }, { NULL }, }; @@ -85,6 +141,13 @@ static struct PyGetSetDef Pakfire_getsetters[] = { NULL, NULL }, + { + "keys", + (getter)Pakfire_get_keys, + NULL, + NULL, + NULL + }, { "path", (getter)Pakfire_get_path, diff --git a/src/_pakfire/pool.c b/src/_pakfire/pool.c index 2fc42dbd8..3c0fc8206 100644 --- a/src/_pakfire/pool.c +++ b/src/_pakfire/pool.c @@ -24,12 +24,10 @@ #include #include -#include #include #include #include "constants.h" -#include "key.h" #include "pool.h" #include "relation.h" #include "repo.h" @@ -204,61 +202,7 @@ static PyObject* Pool_search(PoolObject* self, PyObject* args) { return PyList_FromPackageList(self, list); } -static PyObject* Pool_get_keys(PoolObject* self) { - PyObject* list = PyList_New(0); - - PakfireKey* keys = pakfire_key_list(self->pool); - while (keys && *keys) { - PakfireKey key = *keys++; - - PyObject* object = new_key(self, key); - PyList_Append(list, object); - Py_DECREF(object); - - pakfire_key_free(key); - } - - return list; -} - -static PyObject* Pool_get_key(PoolObject* self, PyObject* args) { - const char* pattern = NULL; - - if (!PyArg_ParseTuple(args, "s", &pattern)) - return NULL; - - PakfireKey key = pakfire_key_get(self->pool, pattern); - if (!key) - Py_RETURN_NONE; - - return new_key(self, key); -} - -static PyObject* Pool_generate_key(PoolObject* self, PyObject* args) { - const char* userid = NULL; - - if (!PyArg_ParseTuple(args, "s", &userid)) - return NULL; - - PakfireKey key = pakfire_key_generate(self->pool, userid); - assert(key); - - return new_key(self, key); -} - static struct PyMethodDef Pool_methods[] = { - { - "generate_key", - (PyCFunction)Pool_generate_key, - METH_VARARGS, - NULL - }, - { - "get_key", - (PyCFunction)Pool_get_key, - METH_VARARGS, - NULL - }, { "search", (PyCFunction)Pool_search, @@ -302,13 +246,6 @@ static struct PyGetSetDef Pool_getsetters[] = { NULL, NULL }, - { - "keys", - (getter)Pool_get_keys, - NULL, - NULL, - NULL - }, { NULL } }; diff --git a/src/libpakfire/include/pakfire/key.h b/src/libpakfire/include/pakfire/key.h index ee86118c3..47be3866d 100644 --- a/src/libpakfire/include/pakfire/key.h +++ b/src/libpakfire/include/pakfire/key.h @@ -30,20 +30,20 @@ typedef enum pakfire_key_export_mode { PAKFIRE_KEY_EXPORT_MODE_SECRET, } pakfire_key_export_mode_t; -PakfireKey* pakfire_key_list(PakfirePool pool); +PakfireKey* pakfire_key_list(Pakfire pakfire); -PakfireKey pakfire_key_create(PakfirePool pool, gpgme_key_t gpgkey); +PakfireKey pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey); void pakfire_key_free(PakfireKey key); -PakfireKey pakfire_key_get(PakfirePool pool, const char* fingerprint); +PakfireKey pakfire_key_get(Pakfire pakfire, const char* fingerprint); const char* pakfire_key_get_fingerprint(PakfireKey key); -PakfireKey pakfire_key_generate(PakfirePool pool, const char* userid); +PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid); char* pakfire_key_export(PakfireKey key, pakfire_key_export_mode_t mode); #ifdef PAKFIRE_PRIVATE struct _PakfireKey { - PakfirePool pool; + Pakfire pakfire; gpgme_key_t gpgkey; int nrefs; }; diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index fa45d7104..f0d6d8e53 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -22,10 +22,10 @@ #include #include -#include +#include #include -static gpgme_ctx_t pakfire_get_gpgctx(PakfirePool pool) { +static gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire) { static int gpg_initialized = 0; gpgme_error_t error; const char* error_string; @@ -44,8 +44,9 @@ static gpgme_ctx_t pakfire_get_gpgctx(PakfirePool pool) { goto FAIL; // Use GPG - error = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, - NULL, "/etc/pakfire/gnupg"); + char* home = pakfire_path_join(pakfire->path, "/etc/pakfire/gnupg"); + error = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, NULL, home); + pakfire_free(home); if (gpg_err_code(error) != GPG_ERR_NO_ERROR) goto FAIL; @@ -81,8 +82,8 @@ FAIL: return NULL; } -PakfireKey* pakfire_key_list(PakfirePool pool) { - gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pool); +PakfireKey* pakfire_key_list(Pakfire pakfire) { + gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); assert(gpgctx); PakfireKey* first = pakfire_calloc(1, sizeof(PakfireKey)); @@ -96,7 +97,7 @@ PakfireKey* pakfire_key_list(PakfirePool pool) { break; // Add key to the list - *list++ = pakfire_key_create(pool, gpgkey); + *list++ = pakfire_key_create(pakfire, gpgkey); gpgme_key_release(gpgkey); } @@ -107,11 +108,11 @@ PakfireKey* pakfire_key_list(PakfirePool pool) { return first; } -PakfireKey pakfire_key_create(PakfirePool pool, gpgme_key_t gpgkey) { +PakfireKey pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey) { PakfireKey key = pakfire_calloc(1, sizeof(*key)); if (key) { - key->pool = pool; + key->pakfire = pakfire_ref(pakfire); key->gpgkey = gpgkey; gpgme_key_ref(key->gpgkey); @@ -121,13 +122,14 @@ PakfireKey pakfire_key_create(PakfirePool pool, gpgme_key_t gpgkey) { } void pakfire_key_free(PakfireKey key) { + pakfire_unref(key->pakfire); gpgme_key_unref(key->gpgkey); pakfire_free(key); } -PakfireKey pakfire_key_get(PakfirePool pool, const char* fingerprint) { - gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pool); +PakfireKey pakfire_key_get(Pakfire pakfire, const char* fingerprint) { + gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); assert(gpgctx); gpgme_key_t gpgkey = NULL; @@ -135,7 +137,7 @@ PakfireKey pakfire_key_get(PakfirePool pool, const char* fingerprint) { if (error != GPG_ERR_NO_ERROR) return NULL; - PakfireKey key = pakfire_key_create(pool, gpgkey); + PakfireKey key = pakfire_key_create(pakfire, gpgkey); gpgme_key_unref(gpgkey); gpgme_release(gpgctx); @@ -146,8 +148,8 @@ const char* pakfire_key_get_fingerprint(PakfireKey key) { return key->gpgkey->fpr; } -PakfireKey pakfire_key_generate(PakfirePool pool, const char* userid) { - gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pool); +PakfireKey pakfire_key_generate(Pakfire pakfire, const char* userid) { + gpgme_ctx_t gpgctx = pakfire_get_gpgctx(pakfire); assert(gpgctx); unsigned int flags = 0; @@ -175,11 +177,11 @@ PakfireKey pakfire_key_generate(PakfirePool pool, const char* userid) { gpgme_release(gpgctx); // Retrieve the key by its fingerprint - return pakfire_key_get(pool, result->fpr); + return pakfire_key_get(pakfire, result->fpr); } char* pakfire_key_export(PakfireKey key, pakfire_key_export_mode_t mode) { - gpgme_ctx_t gpgctx = pakfire_get_gpgctx(key->pool); + gpgme_ctx_t gpgctx = pakfire_get_gpgctx(key->pakfire); assert(gpgctx); gpgme_export_mode_t gpgmode = 0; diff --git a/src/pakfire/cli.py b/src/pakfire/cli.py index 3475fac5a..caa124606 100644 --- a/src/pakfire/cli.py +++ b/src/pakfire/cli.py @@ -850,19 +850,19 @@ class CliKey(Cli): print(_("Generating the key may take a moment...")) print() - key = p.pool.generate_key("%s <%s>" % (ns.realname.pop(), ns.email.pop())) + key = p.generate_key("%s <%s>" % (ns.realname.pop(), ns.email.pop())) def handle_list(self, ns): p = self.pakfire(ns) - for key in p.pool.keys: + for key in p.keys: print(key) def handle_export(self, ns): p = self.pakfire(ns) for fingerprint in ns.fingerprint: - key = p.pool.get_key(fingerprint) + key = p.get_key(fingerprint) if not key: print(_("Could not find key with fingerprint %s") % fingerprint) continue