]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Make keys belong to Pakfire instead of PakfirePool
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 23 Nov 2017 17:34:05 +0000 (18:34 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 23 Nov 2017 17:34:05 +0000 (18:34 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/key.c
src/_pakfire/key.h
src/_pakfire/pakfire.c
src/_pakfire/pool.c
src/libpakfire/include/pakfire/key.h
src/libpakfire/key.c
src/pakfire/cli.py

index 67ca5a46243354189293dc7dce70b2855b1e7f0d..5486e487cf71be1ec14e4bf6ae0bd4e8cc2a3f5b 100644 (file)
 #include <pakfire/util.h>
 
 #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;
 
index 9ffbe9a4e66474a3be53e2ee6fe80eafe52ca4bb..2485159da0f4a6f32136bde2155fbe75af81e99a 100644 (file)
 
 #include <pakfire/key.h>
 
-#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 */
index b3d907c7d932d9c47e390375b7fa6b106efee493..6449e6b4f89294793c7a5bda513e967ee26fde00 100644 (file)
@@ -21,7 +21,9 @@
 #include <Python.h>
 
 #include <pakfire/pakfire.h>
+#include <pakfire/key.h>
 
+#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,
index 2fc42dbd881759843a5f260af80b4db7d0e17654..3c0fc820698fbc6f63ad724b923b75025f21b469 100644 (file)
 #include <solv/solver.h>
 
 #include <pakfire/errno.h>
-#include <pakfire/key.h>
 #include <pakfire/pool.h>
 #include <pakfire/repo.h>
 
 #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 }
 };
 
index ee86118c3cccc26d7549bed2da5bfc6f00654315..47be3866da7f773e193d80432f1607d51b7ad444 100644 (file)
@@ -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;
 };
index fa45d7104a5199318148223ec9ed3fe87e7801eb..f0d6d8e530c2685ffe4ba685f5969718fcfed08e 100644 (file)
 #include <gpgme.h>
 
 #include <pakfire/key.h>
-#include <pakfire/pool.h>
+#include <pakfire/pakfire.h>
 #include <pakfire/util.h>
 
-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;
index 3475fac5a21986d9bdbc6561dceeedf03cd01183..caa124606bb3257a50c1f195f6e5ef1c52b57091 100644 (file)
@@ -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