]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Keep all appdata in the same struct
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 09:09:16 +0000 (09:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Sep 2023 09:09:16 +0000 (09:09 +0000)
This is mainly to make the static analyzer happy which fails to track
any strings allocated and stored in the appdata struct.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/repo.h
src/libpakfire/repo.c
src/libpakfire/transaction.c

index 982e51ca428815e783f321278ece4cb0cff80f3e..9afb4bffd141a45f1e61a6825f7b53a2fab95fb7 100644 (file)
@@ -112,6 +112,8 @@ int pakfire_repo_name_equals(struct pakfire_repo* repo, const char* name);
 int pakfire_repo_is_internal(struct pakfire_repo* repo);
 int pakfire_repo_is_local(struct pakfire_repo* repo);
 
+char* pakfire_repo_url_replace(struct pakfire_repo* repo, const char* url);
+
 int pakfire_repo_import(struct pakfire* pakfire, struct pakfire_config* config);
 const char* pakfire_repo_get_path(struct pakfire_repo* repo);
 
index eb948a3cdd4606e3c276e6209ed4c969fed9558b..b670bcee92b6d9ed71dc6907f11376d998a3d2bc 100644 (file)
 // Refresh repository metadata once every 2 hours
 #define REFRESH_AGE_METADATA                   2 * 3600
 
+#define MAX_DESCRIPTION 4096
+#define MAX_KEY         4096
+
 struct pakfire_repo_appdata {
-       char* description;
-       char* baseurl;
+       // Description
+       char description[MAX_DESCRIPTION];
+
+       // Base URL
+       char baseurl[PATH_MAX];
 
        // Refresh Interval
        time_t refresh;
 
        // Key
-       char* key;
+       char key[MAX_KEY];
 
        // Mirrorlist
-       char* mirrorlist_url;
+       char mirrorlist_url[PATH_MAX];
        char mirrorlist[PATH_MAX];
 
        // Markers
@@ -101,9 +107,6 @@ int pakfire_repo_is_local(struct pakfire_repo* repo) {
        if (pakfire_repo_is_commandline(repo))
                return 1;
 
-       if (!repo->appdata->baseurl)
-               return 0;
-
        return pakfire_string_startswith(repo->appdata->baseurl, "file://");
 }
 
@@ -137,6 +140,53 @@ int pakfire_repo_name_equals(struct pakfire_repo* repo, const char* name) {
        return strcmp(n, name) == 0;
 }
 
+char* pakfire_repo_url_replace(struct pakfire_repo* repo, const char* url) {
+       if (!url)
+               return NULL;
+
+       const struct replacement {
+               const char* pattern;
+               const char* replacement;
+       } replacements[] = {
+               { "%{name}",    pakfire_repo_get_name(repo) },
+               { "%{arch}",    pakfire_get_effective_arch(repo->pakfire) },
+               { "%{distro}",  pakfire_get_distro_id(repo->pakfire) },
+               { "%{version}", pakfire_get_distro_version_id(repo->pakfire) },
+               { NULL },
+       };
+
+       char* buffer = strdup(url);
+       if (!buffer)
+               return NULL;
+
+       for (const struct replacement* repl = replacements; repl->pattern; repl++) {
+               // Skip if there is no replacement
+               if (!repl->replacement)
+                       continue;
+
+               char* r = pakfire_string_replace(
+                       buffer, repl->pattern, repl->replacement);
+               free(buffer);
+
+               // Break on any errors
+               if (!r)
+                       return NULL;
+
+               // Free the old buffer and continue working with the new data
+               buffer = r;
+       }
+
+#ifdef ENABLE_DEBUG
+       if (strcmp(url, buffer) != 0) {
+               DEBUG(repo->pakfire, "Repository URL updated:");
+               DEBUG(repo->pakfire, "  From: %s\n", url);
+               DEBUG(repo->pakfire, "  To  : %s\n", buffer);
+       }
+#endif
+
+       return buffer;
+}
+
 #define pakfire_repo_path(repo, path, format, ...) \
        __pakfire_repo_path(repo, path, sizeof(path), format, __VA_ARGS__)
 
@@ -173,10 +223,21 @@ static int pakfire_repo_retrieve(
                const unsigned char* expected_digest,
                const size_t expected_digest_length,
                enum pakfire_transfer_flags flags) {
-       struct pakfire_downloader* downloader;
-       int r = pakfire_downloader_create(&downloader, repo->pakfire);
+       struct pakfire_downloader* downloader = NULL;
+       char* baseurl = NULL;
+       int r;
+
+       // Create downloader
+       r = pakfire_downloader_create(&downloader, repo->pakfire);
        if (r)
-               return r;
+               goto ERROR;
+
+       // Prepare the baseurl
+       baseurl = pakfire_repo_url_replace(repo, repo->appdata->baseurl);
+       if (!baseurl) {
+               r = -errno;
+               goto ERROR;
+       }
 
 #if 0
        // Fetch mirrorlist
@@ -184,20 +245,24 @@ static int pakfire_repo_retrieve(
 #endif
 
        // Retrieve the database file
-       r = pakfire_downloader_retrieve(downloader, repo->appdata->baseurl, NULL,
+       r = pakfire_downloader_retrieve(downloader, baseurl, NULL,
                title, url, path, md, expected_digest, expected_digest_length, flags);
 
 #if 0
        if (mirrorlist)
                pakfire_mirrorlist_unref(mirrorlist);
 #endif
-       pakfire_downloader_unref(downloader);
+
+ERROR:
+       if (downloader)
+               pakfire_downloader_unref(downloader);
+       if (baseurl)
+               free(baseurl);
 
        return r;
 }
 
 static int pakfire_repo_import_key(struct pakfire_repo* repo, const char* data) {
-       struct pakfire_key* key = NULL;
        int r;
 
        // Free any formerly imported keys (this should not happen)
@@ -215,17 +280,13 @@ static int pakfire_repo_import_key(struct pakfire_repo* repo, const char* data)
        }
 
        // If the key could be successfully imported, we will store it in the appdata
-       repo->appdata->key = strdup(data);
-       if (!repo->appdata->key) {
+       r = pakfire_string_set(repo->appdata->key, data);
+       if (r) {
                ERROR(repo->pakfire, "Could not copy the key to appdata: %m\n");
-               r = 1;
                goto ERROR;
        }
 
 ERROR:
-       if (key)
-               pakfire_key_unref(key);
-
        return r;
 }
 
@@ -479,6 +540,7 @@ ERROR:
 }
 
 static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int force) {
+       char* url = NULL;
        int r;
 
        // Local repositories don't need a mirrorlist
@@ -490,14 +552,14 @@ static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int
                return 0;
 
        // This repository does not have a mirrorlist
-       if (!repo->appdata->mirrorlist_url)
+       if (!*repo->appdata->mirrorlist_url)
                return 0;
 
        // Make path to mirrorlist
        r = pakfire_cache_path(repo->pakfire, repo->appdata->mirrorlist,
                "repodata/%s/mirrorlist", pakfire_repo_get_name(repo));
        if (r)
-               return r;
+               goto ERROR;
 
        // Check if this needs to be refreshed
        if (!force) {
@@ -509,9 +571,22 @@ static int pakfire_repo_refresh_mirrorlist(struct pakfire_repo* repo, const int
                }
        }
 
-       return pakfire_repo_retrieve(repo, NULL,
-               repo->appdata->mirrorlist_url, repo->appdata->mirrorlist,
+       // Replace any variables in the URL
+       url = pakfire_repo_url_replace(repo, repo->appdata->mirrorlist_url);
+       if (!url) {
+               ERROR(repo->pakfire, "Could not expend the mirror list URL: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
+
+       r = pakfire_repo_retrieve(repo, NULL, url, repo->appdata->mirrorlist,
                0, NULL, 0, PAKFIRE_TRANSFER_NOPROGRESS);
+
+ERROR:
+       if (url)
+               free(url);
+
+       return r;
 }
 
 static int pakfire_repo_download_metadata(struct pakfire_repo* repo, const char* path, int force) {
@@ -567,19 +642,8 @@ static int pakfire_repo_refresh_metadata(struct pakfire_repo* repo, const int fo
 }
 
 static void free_repo_appdata(struct pakfire_repo_appdata* appdata) {
-       if (!appdata)
-               return;
-
-       if (appdata->key)
-               free(appdata->key);
-
-       if (appdata->description)
-               free(appdata->description);
-
-       if (appdata->mirrorlist_url)
-               free(appdata->mirrorlist_url);
-
-       free(appdata);
+       if (appdata)
+               free(appdata);
 }
 
 static void pakfire_repo_free(struct pakfire_repo* repo, const int free_repo) {
@@ -790,15 +854,7 @@ PAKFIRE_EXPORT const char* pakfire_repo_get_description(struct pakfire_repo* rep
 }
 
 PAKFIRE_EXPORT int pakfire_repo_set_description(struct pakfire_repo* repo, const char* description) {
-       if (repo->appdata->description)
-               free(repo->appdata->description);
-
-       if (description)
-               repo->appdata->description = strdup(description);
-       else
-               repo->appdata->description = NULL;
-
-       return 0;
+       return pakfire_string_set(repo->appdata->description, description);
 }
 
 PAKFIRE_EXPORT int pakfire_repo_get_enabled(struct pakfire_repo* repo) {
@@ -823,61 +879,13 @@ PAKFIRE_EXPORT const char* pakfire_repo_get_baseurl(struct pakfire_repo* repo) {
        return repo->appdata->baseurl;
 }
 
-static char* pakfire_repo_url_replace(struct pakfire_repo* repo, const char* url) {
-       if (!url)
-               return NULL;
-
-       const struct replacement {
-               const char* pattern;
-               const char* replacement;
-       } replacements[] = {
-               { "%{name}",    pakfire_repo_get_name(repo) },
-               { "%{arch}",    pakfire_get_effective_arch(repo->pakfire) },
-               { "%{distro}",  pakfire_get_distro_id(repo->pakfire) },
-               { "%{version}", pakfire_get_distro_version_id(repo->pakfire) },
-               { NULL },
-       };
-
-       char* buffer = strdup(url);
-       if (!buffer)
-               return NULL;
-
-       for (const struct replacement* repl = replacements; repl->pattern; repl++) {
-               // Skip if there is no replacement
-               if (!repl->replacement)
-                       continue;
-
-               char* r = pakfire_string_replace(
-                       buffer, repl->pattern, repl->replacement);
-               free(buffer);
-
-               // Break on any errors
-               if (!r)
-                       return NULL;
-
-               // Free the old buffer and continue working with the new data
-               buffer = r;
-       }
-
-#ifdef ENABLE_DEBUG
-       if (strcmp(url, buffer) != 0) {
-               DEBUG(repo->pakfire, "Repository URL updated:");
-               DEBUG(repo->pakfire, "  From: %s\n", url);
-               DEBUG(repo->pakfire, "  To  : %s\n", buffer);
-       }
-#endif
-
-       return buffer;
-}
-
 PAKFIRE_EXPORT int pakfire_repo_set_baseurl(struct pakfire_repo* repo, const char* baseurl) {
-       if (repo->appdata->baseurl)
-               free(repo->appdata->baseurl);
+       int r;
 
-       // Store URL
-       repo->appdata->baseurl = pakfire_repo_url_replace(repo, baseurl);
-       if (!repo->appdata->baseurl)
-               return 1;
+       // Store the URL
+       r = pakfire_string_set(repo->appdata->baseurl, baseurl);
+       if (r)
+               return r;
 
        // Update sub-priority
        pakfire_repo_update_subpriority(repo);
@@ -895,18 +903,19 @@ const char* pakfire_repo_get_path(struct pakfire_repo* repo) {
 PAKFIRE_EXPORT struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo) {
        int r;
 
-       if (!repo->key && repo->appdata->key) {
+       // It looks like no key has been imported
+       if (!*repo->appdata->key)
+               return NULL;
+
+       // Import the key
+       if (!repo->key) {
                r = pakfire_key_import_from_string(&repo->key, repo->pakfire,
                        repo->appdata->key, strlen(repo->appdata->key));
                if (r)
-                       goto ERROR;
+                       return NULL;
        }
 
-       if (repo->key)
-               return pakfire_key_ref(repo->key);
-
-ERROR:
-       return NULL;
+       return pakfire_key_ref(repo->key);
 }
 
 PAKFIRE_EXPORT const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo) {
@@ -914,15 +923,7 @@ PAKFIRE_EXPORT const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo*
 }
 
 PAKFIRE_EXPORT int pakfire_repo_set_mirrorlist_url(struct pakfire_repo* repo, const char* url) {
-       if (repo->appdata->mirrorlist_url)
-               free(repo->appdata->mirrorlist_url);
-
-       // Store URL
-       repo->appdata->mirrorlist_url = pakfire_repo_url_replace(repo, url);
-       if (!repo->appdata->mirrorlist_url)
-               return 1;
-
-       return 0;
+       return pakfire_string_set(repo->appdata->mirrorlist_url, url);
 }
 
 PAKFIRE_EXPORT int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f) {
@@ -1726,13 +1727,13 @@ PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* pat
        // Prefix path with file:// to form baseurl
        r = pakfire_string_format(baseurl, "file://%s", realpath);
        if (r)
-               return 1;
+               goto ERROR;
 
        // Create a new temporary repository at path
        r = pakfire_repo_create(&repo, pakfire, "tmp");
        if (r) {
                ERROR(pakfire, "Could not create a temporary repository: %m\n");
-               return r;
+               goto ERROR;
        }
 
        // Set baseurl to path
@@ -1837,8 +1838,10 @@ OUT:
        r = 0;
 
 ERROR:
-       pakfire_repo_clear(repo);
-       pakfire_repo_unref(repo);
+       if (repo) {
+               pakfire_repo_clear(repo);
+               pakfire_repo_unref(repo);
+       }
 
        return r;
 }
index b2e7d54efe9c28ecf2fb06f7df46e433efca7996..4dbfdc397f3a5c34b6dca685adb7fccd92b9b13d 100644 (file)
@@ -1811,6 +1811,7 @@ static int pakfire_transaction_download_package(struct pakfire_transaction* tran
        int r = 1;
        struct pakfire_repo* repo = NULL;
        struct pakfire_mirrorlist* mirrorlist = NULL;
+       char* __baseurl = NULL;
 
        // Fetch the repository to download from
        repo = pakfire_package_get_repo(pkg);
@@ -1820,6 +1821,13 @@ static int pakfire_transaction_download_package(struct pakfire_transaction* tran
        // Fetch baseurl
        const char* baseurl = pakfire_repo_get_baseurl(repo);
 
+       // Prepare the base url
+       __baseurl = pakfire_repo_url_replace(repo, baseurl);
+       if (!__baseurl) {
+               r = -errno;
+               goto ERROR;
+       }
+
        // Fetch mirrorlist
        mirrorlist = pakfire_repo_get_mirrorlist(repo);
 
@@ -1852,7 +1860,7 @@ static int pakfire_transaction_download_package(struct pakfire_transaction* tran
        }
 
        // Add transfer to downloader
-       r = pakfire_downloader_add_transfer(downloader, baseurl, mirrorlist,
+       r = pakfire_downloader_add_transfer(downloader, __baseurl, mirrorlist,
                nevra, path, cache_path, digest_type, digest, digest_length, 0);
 
 ERROR:
@@ -1860,6 +1868,8 @@ ERROR:
                pakfire_mirrorlist_unref(mirrorlist);
        if (repo)
                pakfire_repo_unref(repo);
+       if (__baseurl)
+               free(__baseurl);
 
        return r;
 }