From: Michael Tremer Date: Mon, 19 Apr 2021 14:49:37 +0000 (+0000) Subject: repo: Replace variables in URLs X-Git-Tag: 0.9.28~1285^2~332 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c23520eaf0cb3ca5f7741902b70f266b687e90e0;p=pakfire.git repo: Replace variables in URLs Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 67e8d0b6a..7e805ac6a 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -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; }