Skip to content

Commit 6a88c7b

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 fa46567 commit 6a88c7b

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

data/xbps.d.5

Lines changed: 8 additions & 0 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
118125
.Bl -tag -compact -width repository=https://repo-default.voidlinux.org/current
119126
.It Sy repository=https://repo-default.voidlinux.org/current
127+
.It Sy repository=https://repo-default.voidlinux.org/current/{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
@@ -99,6 +99,7 @@ int HIDDEN xbps_transaction_fetch(struct xbps_handle *,
9999
int HIDDEN xbps_transaction_pkg_deps(struct xbps_handle *, xbps_array_t, xbps_dictionary_t);
100100
int HIDDEN xbps_transaction_internalize(struct xbps_handle *, xbps_object_iterator_t);
101101

102+
char HIDDEN *repo_format(struct xbps_handle *, const char *);
102103
char HIDDEN *xbps_get_remote_repo_string(const char *);
103104
int HIDDEN xbps_repo_sync(struct xbps_handle *, const char *);
104105
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
@@ -164,6 +164,15 @@ xbps_init(struct xbps_handle *xhp)
164164
xhp->flags |= XBPS_FLAG_DISABLE_SYSLOG;
165165
}
166166

167+
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
168+
const char *url = NULL;
169+
xbps_array_get_cstring_nocopy(xhp->repositories, i, &url);
170+
xbps_array_set_cstring_nocopy(xhp->repositories, i, repo_format(xhp, url));
171+
}
172+
173+
xbps_dbg_printf("Native architecture is %s\n", xhp->native_arch);
174+
xbps_dbg_printf("Target architecture is %s\n", xhp->target_arch);
175+
167176
if (xhp->flags & XBPS_FLAG_DEBUG) {
168177
const char *repodir;
169178
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
@@ -40,6 +40,7 @@
4040
#include <openssl/ssl.h>
4141
#include <openssl/pem.h>
4242

43+
#include "xbps/fmt.h"
4344
#include "xbps_api_impl.h"
4445

4546
/**
@@ -196,6 +197,65 @@ repo_open_remote(struct xbps_repo *repo)
196197
return rv;
197198
}
198199

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

0 commit comments

Comments
 (0)