]> git.ipfire.org Git - pakfire.git/commitdiff
key: Refactor and make private pakfire_key_create
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 17:21:27 +0000 (17:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 7 Jul 2021 17:21:27 +0000 (17:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/key.h
src/libpakfire/key.c
src/libpakfire/libpakfire.sym

index 60a2dc9a4e7c5b97c720d8d8457dcbe2a56ee8d2..9c3a4908d67948f7f8b9591523f6f1763fc418e3 100644 (file)
@@ -21,7 +21,6 @@
 #ifndef PAKFIRE_KEY_H
 #define PAKFIRE_KEY_H
 
-#include <gpgme.h>
 #include <time.h>
 
 struct pakfire_key;
@@ -35,7 +34,6 @@ typedef enum pakfire_key_export_mode {
 
 struct pakfire_key** pakfire_key_list(Pakfire pakfire);
 
-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);
 
@@ -59,4 +57,12 @@ struct pakfire_key** pakfire_key_import(Pakfire pakfire, const char* data);
 
 char* pakfire_key_dump(struct pakfire_key* key);
 
+#ifdef PAKFIRE_PRIVATE
+
+#include <gpgme.h>
+
+int pakfire_key_create(struct pakfire_key** key, Pakfire pakfire, gpgme_key_t gpgkey);
+
+#endif
+
 #endif /* PAKFIRE_KEY_H */
index 60d70d680afe8afb710ae0c9b5660421ab3d3021..ced2c7af538c83f7c00dec463c0c3e623f0b897e 100644 (file)
@@ -70,6 +70,8 @@ PAKFIRE_EXPORT struct pakfire_key** pakfire_key_list(Pakfire 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*));
@@ -82,8 +84,10 @@ PAKFIRE_EXPORT struct pakfire_key** pakfire_key_list(Pakfire pakfire) {
                if (error)
                        break;
 
+               pakfire_key_create(&key, pakfire, gpgkey);
+
                // Add key to the list
-               *list++ = pakfire_key_create(pakfire, gpgkey);
+               *list++ = key;
 
                gpgme_key_release(gpgkey);
        }
@@ -94,18 +98,21 @@ PAKFIRE_EXPORT struct pakfire_key** pakfire_key_list(Pakfire pakfire) {
        return first;
 }
 
-PAKFIRE_EXPORT struct pakfire_key* pakfire_key_create(Pakfire pakfire, gpgme_key_t gpgkey) {
-       struct pakfire_key* key = calloc(1, sizeof(*key));
+int pakfire_key_create(struct pakfire_key** key, Pakfire pakfire, gpgme_key_t gpgkey) {
+       struct pakfire_key* k = calloc(1, sizeof(*k));
+       if (!k)
+               return 1;
 
-       if (key) {
-               key->nrefs = 1;
-               key->pakfire = pakfire_ref(pakfire);
+       // Initialize pakfire and reference counter
+       k->pakfire = pakfire_ref(pakfire);
+       k->nrefs = 1;
 
-               key->gpgkey = gpgkey;
-               gpgme_key_ref(key->gpgkey);
-       }
+       // Keep a reference to this key
+       gpgme_key_ref(gpgkey);
+       k->gpgkey = gpgkey;
 
-       return key;
+       *key = k;
+       return 0;
 }
 
 static void pakfire_key_free(struct pakfire_key* key) {
@@ -137,7 +144,7 @@ static struct pakfire_key* __pakfire_get_key(Pakfire pakfire, gpgme_ctx_t gpgctx
        gpgme_error_t error = gpgme_get_key(gpgctx, fingerprint, &gpgkey, 0);
        switch (gpg_error(error)) {
                case GPG_ERR_NO_ERROR:
-                       key = pakfire_key_create(pakfire, gpgkey);
+                       pakfire_key_create(&key, pakfire, gpgkey);
                        gpgme_key_unref(gpgkey);
                        break;
 
index 5ce449836c1fcc19df48aef696c465d283764f76..3f3e6a04e2d37db74fc3cbb0c25fb4cb52e63cb1 100644 (file)
@@ -108,7 +108,6 @@ global:
        pakfire_filelist_unref;
 
        # key
-       pakfire_key_create;
        pakfire_key_delete;
        pakfire_key_dump;
        pakfire_key_export;