Skip to content

Commit 7b6ae32

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 7b6ae32

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

data/xbps.d.5

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,20 @@ argument accepts local and remote repositories.
111111
A complete url or absolute path to the directory that stores the
112112
.Em <arch>-repodata
113113
archive is expected.
114+
If the
115+
.Ar url
116+
contains
117+
.Ar {arch} ,
118+
.Ar {arch}
119+
will be replaced by the current target XBPS architecture at runtime.
120+
If the target architecture is not set, the native architecture will be used.
114121
Note that remote repositories must be signed using
115122
.Xr xbps-rindex 1 ,
116123
example:
117124
.Pp
118-
.Bl -tag -compact -width repository=https://a-hel-fi.m.voidlinux.org/current
119-
.It Sy repository=https://a-hel-fi.m.voidlinux.org/current
125+
.Bl -tag -compact -width repository=https://repo-default.voidlinux.org/current
126+
.It Sy repository=https://repo-default.voidlinux.org/current
127+
.It Sy repository=https://repo-default.voidlinux.org/{arch}
120128
.It Sy repository=/hostdir/binpkgs
121129
.El
122130
.It Sy rootdir=path

include/xbps_api_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ int HIDDEN xbps_transaction_fetch(struct xbps_handle *,
104104
int HIDDEN xbps_transaction_pkg_deps(struct xbps_handle *, xbps_array_t, xbps_dictionary_t);
105105
int HIDDEN xbps_transaction_internalize(struct xbps_handle *, xbps_object_iterator_t);
106106

107+
char HIDDEN *repo_format(struct xbps_handle *, const char *);
107108
char HIDDEN *xbps_get_remote_repo_string(const char *);
108109
int HIDDEN xbps_repo_sync(struct xbps_handle *, const char *);
109110
int HIDDEN xbps_file_hash_check_dictionary(struct xbps_handle *,

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, 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+
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)