]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Replace variables in URLs
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 19 Apr 2021 14:49:37 +0000 (14:49 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 19 Apr 2021 14:49:37 +0000 (14:49 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/repo.c

index 67e8d0b6a4b00dfc3545a49394835db6eb5ce233..7e805ac6ad42ab054bf7a88f14a128c30c117d28 100644 (file)
@@ -622,14 +622,58 @@ PAKFIRE_EXPORT const char* pakfire_repo_get_baseurl(PakfireRepo repo) {
        return repo->appdata->baseurl;
 }
 
+static char* pakfire_repo_url_replace(PakfireRepo repo, const char* url) {
+       if (!url)
+               return NULL;
+
+       const char* arch = pakfire_get_arch(repo->pakfire);
+       const char* name = pakfire_repo_get_name(repo);
+
+       const struct replacement {
+               const char* pattern;
+               const char* replacement;
+       } replacements[] = {
+               { "%{name}", name },
+               { "%{arch}", arch },
+               { NULL, NULL },
+       };
+
+       char* buffer = strdup(url);
+       if (!buffer)
+               return NULL;
+
+       for (const struct replacement* repl = replacements; repl->pattern; repl++) {
+               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(PakfireRepo repo, const char* baseurl) {
        if (repo->appdata->baseurl)
                free(repo->appdata->baseurl);
 
-       if (baseurl)
-               repo->appdata->baseurl = strdup(baseurl);
-       else
-               repo->appdata->baseurl = NULL;
+       // Store URL
+       repo->appdata->baseurl = pakfire_repo_url_replace(repo, baseurl);
+       if (!repo->appdata->baseurl)
+               return 1;
 
        return 0;
 }
@@ -670,10 +714,10 @@ PAKFIRE_EXPORT int pakfire_repo_set_mirrorlist_url(PakfireRepo repo, const char*
        if (repo->appdata->mirrorlist_url)
                free(repo->appdata->mirrorlist_url);
 
-       if (url)
-               repo->appdata->mirrorlist_url = strdup(url);
-       else
-               repo->appdata->mirrorlist_url = NULL;
+       // Store URL
+       repo->appdata->mirrorlist_url = pakfire_repo_url_replace(repo, url);
+       if (!repo->appdata->mirrorlist_url)
+               return 1;
 
        return 0;
 }