]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Anchor the key to the appdata
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 31 Jan 2025 13:05:47 +0000 (13:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 31 Jan 2025 13:05:47 +0000 (13:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/repo.c

index 486ab41dcae59badb398f8153cd824e95604a1c3..ca11683225cb32f2906769baa6c40709f7274ae6 100644 (file)
@@ -56,7 +56,6 @@
 #define REFRESH_AGE_METADATA                   2 * 3600
 
 #define MAX_DESCRIPTION 4096
-#define MAX_KEY         4096
 
 struct pakfire_repo_appdata {
        // Reference Counter
@@ -76,7 +75,7 @@ struct pakfire_repo_appdata {
        time_t refresh;
 
        // Key
-       char key[MAX_KEY];
+       struct pakfire_key* key;
 
        // Mirrorlist
        struct pakfire_mirrorlist* mirrorlist;
@@ -285,31 +284,31 @@ ERROR:
        return r;
 }
 
-static int pakfire_repo_import_key(struct pakfire_repo* repo, const char* data) {
+static int pakfire_repo_import_key(struct pakfire_repo* self, const char* data) {
+       struct pakfire_key* key = NULL;
        int r;
 
        // Free any formerly imported keys (this should not happen)
-       if (repo->key) {
-               pakfire_key_unref(repo->key);
-               repo->key = NULL;
+       if (self->key) {
+               pakfire_key_unref(self->key);
+               self->key = NULL;
        }
 
        // Import the key
-       r = pakfire_key_import_from_string(&repo->key, repo->ctx, data, strlen(data));
+       r = pakfire_key_import_from_string(&key, self->ctx, data, strlen(data));
        if (r < 0) {
-               ERROR(repo->ctx, "Could not import key for repository '%s': %s\n",
-                       pakfire_repo_get_name(repo), strerror(-r));
+               ERROR(self->ctx, "Could not import key for repository '%s': %s\n",
+                       pakfire_repo_get_name(self), strerror(-r));
                goto ERROR;
        }
 
        // If the key could be successfully imported, we will store it in the appdata
-       r = pakfire_string_set(repo->appdata->key, data);
-       if (r < 0) {
-               ERROR(repo->ctx, "Could not copy the key to appdata: %s\n", strerror(-r));
-               goto ERROR;
-       }
+       self->appdata->key = pakfire_key_ref(key);
 
 ERROR:
+       if (key)
+               pakfire_key_unref(key);
+
        return r;
 }
 
@@ -827,6 +826,8 @@ static void pakfire_repo_free_appdata(struct pakfire_repo_appdata* appdata) {
 
        if (appdata->mirrorlist)
                pakfire_mirrorlist_unref(appdata->mirrorlist);
+       if (appdata->key)
+               pakfire_key_unref(appdata->key);
        free(appdata);
 }
 
@@ -835,8 +836,6 @@ static void pakfire_repo_free(struct pakfire_repo* self) {
                pakfire_repo_free_appdata(self->appdata);
        if (self->pakfire)
                pakfire_unref(self->pakfire);
-       if (self->key)
-               pakfire_key_unref(self->key);
        if (self->ctx)
                pakfire_ctx_unref(self->ctx);
        free(self);
@@ -1208,22 +1207,11 @@ const char* pakfire_repo_get_path(struct pakfire_repo* repo) {
        return baseurl + strlen("file://");
 }
 
-struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo) {
-       int r;
-
-       // It looks like no key has been imported
-       if (!*repo->appdata->key)
-               return NULL;
+struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* self) {
+       if (self->appdata->key)
+               return pakfire_key_ref(self->appdata->key);
 
-       // Import the key
-       if (!repo->key) {
-               r = pakfire_key_import_from_string(&repo->key, repo->ctx,
-                       repo->appdata->key, strlen(repo->appdata->key));
-               if (r)
-                       return NULL;
-       }
-
-       return pakfire_key_ref(repo->key);
+       return NULL;
 }
 
 const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo) {