Skip to content

Commit 557d769

Browse files
committed
lib: add ability to template repo urls with {arch}
{arch} will be replaced with XBPS_TARGET_ARCH (if set) or XBPS_ARCH
1 parent ae11e15 commit 557d769

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

include/xbps.h.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,16 @@ bool xbps_repo_store(struct xbps_handle *xhp, const char *url);
16011601
*/
16021602
bool xbps_repo_remove(struct xbps_handle *xhp, const char *url);
16031603

1604+
/**
1605+
* Converts \a {arch} in a repository URI to the correct architecture.
1606+
*
1607+
* @param[in] xhp Pointer to the xbps_handle struct.
1608+
* @param[in] url Repository URI to format.
1609+
*
1610+
* @return The formatted repository URI.
1611+
*/
1612+
char *xbps_repo_format(struct xbps_handle *xhp, const char *url);
1613+
16041614
/**
16051615
* Creates a lock for a local repository to obtain exclusive access (write).
16061616
*

lib/initend.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ xbps_init(struct xbps_handle *xhp)
163163
xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
164164
}
165165

166+
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
167+
const char *url = NULL;
168+
xbps_array_get_cstring_nocopy(xhp->repositories, i, &url);
169+
xbps_array_set_cstring_nocopy(xhp->repositories, i, xbps_repo_format(xhp, url));
170+
}
171+
172+
xbps_dbg_printf("Native architecture is %s\n", xhp->native_arch);
173+
xbps_dbg_printf("Target architecture is %s\n", xhp->target_arch);
174+
166175
if (xhp->flags & XBPS_FLAG_DEBUG) {
167176
const char *repodir;
168177
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {

lib/repo.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <openssl/ssl.h>
3838
#include <openssl/pem.h>
3939

40+
#include "xbps/fmt.h"
4041
#include "xbps_api_impl.h"
4142

4243
/**
@@ -193,6 +194,65 @@ repo_open_remote(struct xbps_repo *repo)
193194
return rv;
194195
}
195196

197+
static int
198+
repo_fmt(FILE *fp, const struct xbps_fmt_var *var, void *data)
199+
{
200+
struct xbps_handle *xhp = data;
201+
const char *arch;
202+
203+
if (xhp->target_arch)
204+
arch = xhp->target_arch;
205+
else
206+
arch = xhp->native_arch;
207+
208+
if (strcmp(var->name, "arch") == 0) {
209+
return xbps_fmt_print_string(var, arch, 0, fp);
210+
}
211+
return 0;
212+
}
213+
214+
char *
215+
xbps_repo_format(struct xbps_handle *xhp, const char *url)
216+
{
217+
struct xbps_fmt *fmt = NULL;
218+
FILE *fmt_stream;
219+
char *fmt_buf;
220+
size_t len;
221+
int r;
222+
223+
assert(xhp);
224+
assert(url);
225+
226+
if (!strstr(url, "{arch}"))
227+
return strdup(url);
228+
229+
xbps_dbg_printf("Processing templated repository: %s\n", url);
230+
231+
fmt_stream = open_memstream(&fmt_buf, &len);
232+
if (!fmt_stream) {
233+
xbps_error_printf("failed to open buffer: %s\n", strerror(errno));
234+
goto fmtout;
235+
}
236+
fmt = xbps_fmt_parse(url);
237+
if (!fmt) {
238+
xbps_error_printf("failed to parse format for repo '%s': %s\n", url, strerror(errno));
239+
goto fmtout;
240+
}
241+
r = xbps_fmt(fmt, repo_fmt, xhp, fmt_stream);
242+
if (r < 0) {
243+
xbps_error_printf("failed to format repo '%s': %s\n", url, strerror(-r));
244+
goto fmtout;
245+
}
246+
fflush(fmt_stream);
247+
return fmt_buf;
248+
249+
fmtout:
250+
fclose(fmt_stream);
251+
xbps_fmt_free(fmt);
252+
free(fmt_buf);
253+
return NULL;
254+
}
255+
196256
static struct xbps_repo *
197257
repo_open_with_type(struct xbps_handle *xhp, const char *url, const char *name)
198258
{

0 commit comments

Comments
 (0)