LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
Package engine: follow apt's preferences (/etc/apt/preferences.d) and apt's candidate rules, so the Store picks the versions apt picks (the Pi image no longer takes Raspberry Pi's +rpt builds of Debian packages)
Federico Pereira2b93ff5328d2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex adfda6c..3909c81 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -40,6 +40,10 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),### Fixed+- **The Store now follows apt's priorities** (`/etc/apt/preferences.d`), choosing the same versions+ as `apt`. On the Raspberry Pi image, "Update all" could take Raspberry Pi's builds of Chromium,+ BlueZ and other Debian packages instead of Debian's; it no longer does, and Raspberry Pi's kernel+ packages stay blocked as the image intends.- **LordOS Imager offered 0.8.3 as the newest Raspberry Pi image.** It only accepted release tagswith three numbers, so it skipped 0.8.4.1; point releases (`v0.8.4.1-beta`) are now listed anddownloaded, in LordOS and in the desktop app, and the desktop app's update check reads them too.diff --git a/docs/ISO.md b/docs/ISO.mdindex 41e3949..4583390 100644--- a/docs/ISO.md+++ b/docs/ISO.md@@ -486,9 +486,9 @@ Found in this test:nothing happens after the countdown; the board was restarted with `systemctl reboot`. The sameholds for Store → Updates' Restart Now; to fix in a later version (`canPower()` should be falsethere).-- **The Store's package engine ignores `/etc/apt/preferences.d`**, so on the Pi image its "Update all"+- **The Store's package engine ignored `/etc/apt/preferences.d`**, so on the Pi image its "Update all"would take Raspberry Pi's `+rpt` builds (Chromium, BlueZ…) over Debian's; `apt` follows the- priorities (release notes, Known issues).+ priorities (release notes, Known issues). Fixed for 0.8.5 (`PACKAGES.md`, "How it resolves").## Release test (0.8.4 Beta, October 19–25)diff --git a/docs/PACKAGES.md b/docs/PACKAGES.mdindex e9ce26c..98cbcc5 100644--- a/docs/PACKAGES.md+++ b/docs/PACKAGES.md@@ -286,12 +286,23 @@ await admin.run('pkg-install', 'featherpad', (line) => …) // line: "progres## How it resolves-- **Candidate.** It is the newest version from the highest-priority source:- - priority 500 for normal ones;- - 100 for `NotAutomatic` + `ButAutomaticUpgrades`, like backports;- - 1 for `NotAutomatic` only, like experimental.-- What was installed from backports keeps being upgraded from there. It never downgrades.+- **Candidate.** The same version apt would choose (`server/src/pkg/pin.c`, `pkg_candidate` in+ `resolve.c`; since 0.8.5). Each version gets a priority:+ - from **apt's preferences** (`/etc/apt/preferences`, then `/etc/apt/preferences.d/*` in C sort+ order, only names of `[A-Za-z0-9_.-]` with no extension or `.pref`): the first *specific* pin+ naming the package (globs, `/regex/` or `src:name`) decides for the versions its `Pin:` matches+ (`origin <host>`, `release a=,n=,c=,o=,l=`, `version <glob>`); otherwise the first *general*+ pin (`Package: *`) matching its source. A malformed stanza is skipped; a `release` key we can't+ check (`v=`, `b=`) never matches;+ - otherwise from its source: 500 for normal ones, 100 for `NotAutomatic` + `ButAutomaticUpgrades`+ (backports), 1 for `NotAutomatic` only (experimental).++ Then, as apt does, the versions are taken from the newest down and only a strictly higher priority+ replaces the one chosen; the installed version counts at least 100; an older version than the+ installed one only with 1000 or more; a negative priority never. So what was installed from+ backports keeps being upgraded from there, and on the Raspberry Pi image Raspberry Pi's `+rpt`+ builds of Debian packages are not taken. The preferences are re-read when they change, without a+ new index (`idx_refresh`). Tested in `pkg-test` with the Pi image's own pin files.- **Dependencies.** It walks those of what changes: `Pre-Depends` and `Depends`, and `Recommends`with `--recommends`.- **Choices.** Alternatives (`a | b`) and virtual packages are tried in order. Among providers ofdiff --git a/docs/RASPBERRY.md b/docs/RASPBERRY.mdindex 96ca542..7dcadec 100644--- a/docs/RASPBERRY.md+++ b/docs/RASPBERRY.md@@ -82,9 +82,9 @@ SD card or an NVMe, and the same image boots on the Pi 5 and the Pi 4.Everything else comes from Debian, Mesa included. Since 0.8.4.1 `rpi-eeprom` and what it needsare also in the LordOS repository (`UPDATES.md`, "Raspberry Pi firmware packages"): the samefiles, so either source updates the bootloader images; with both, `apt` takes the Raspberry Pi- archive's copy (priority 990) and the result is the same `.deb`. The Store's package engine does- not read `/etc/apt/preferences.d` yet, so its "Update all" may take Raspberry Pi's builds of- packages Debian also has (Chromium, BlueZ…); `apt` follows the priorities.+ archive's copy (priority 990) and the result is the same `.deb`. Since 0.8.5 the Store's package+ engine follows `/etc/apt/preferences.d` like `apt` (`PACKAGES.md`, "How it resolves"); until then+ its "Update all" could take Raspberry Pi's builds of packages Debian also has (Chromium, BlueZ…).- **The bootloader** (in the board's EEPROM) is updated from **Settings → Board firmware**(`SETTINGS.md`, "Board firmware"), not `raspi-config`: `rpi-eeprom-update`'s hint to run`raspi-config` doesn't apply on LordOS.diff --git a/docs/UPDATES.md b/docs/UPDATES.mdindex 18423a0..ebc3ebc 100644--- a/docs/UPDATES.md+++ b/docs/UPDATES.md@@ -249,8 +249,8 @@ system by LordOS's packages.priority 990 for these packages (`/etc/apt/preferences.d/raspberrypi`), apt takes the newestversion among the 990 sources; ours mirrors the same files, so it only matters when a newer versionis in one of them. No pin is needed for our copy: on a board without Raspberry Pi's archive it is- the only candidate (priority 500), and our package engine takes the highest version of any- source. A newer `rpi-eeprom` reaches a board through Store → Updates or `apt upgrade` like any+ the only candidate (priority 500), and our package engine (which follows apt's preferences since+ 0.8.5) takes the highest version among equal priorities. A newer `rpi-eeprom` reaches a board through Store → Updates or `apt upgrade` like anyother package.## The Raspberry Pi kerneldiff --git a/server/Makefile b/server/Makefileindex fc55def..c16a7ed 100644--- a/server/Makefile+++ b/server/Makefile@@ -61,7 +61,7 @@ SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/admin.c ssrc/ui/unicode.c src/ui/bidi.c src/ui/shape.c src/ui/shape_indic.c src/ui/shape_syl.c src/ui/shape_khmer.c src/ui/shape_myanmar.c src/ui/shape_use.c src/ui/thbrk.c src/ui/textlayout.c \src/term/vt.c src/term/pty.c src/term/term.c src/term/term_paint.c src/term/hold.c \src/net/nl.c src/net/crypto.c src/net/state.c src/net/sys.c src/net/dhcp.c src/net/wifi.c src/net/wg.c src/net/ovpn.c src/net/vpn.c src/net/vici.c src/net/ipsec.c src/net/vpnproc.c src/net/actions.c \- src/pkg/crypto.c src/pkg/pgp.c src/pkg/xz.c src/pkg/deb.c src/pkg/idx.c src/pkg/status.c src/pkg/resolve.c src/pkg/http.c src/pkg/pkg.c src/ui/js_pkg.c src/ui/js_sec.c \+ src/pkg/crypto.c src/pkg/pgp.c src/pkg/xz.c src/pkg/deb.c src/pkg/idx.c src/pkg/pin.c src/pkg/status.c src/pkg/resolve.c src/pkg/http.c src/pkg/pkg.c src/ui/js_pkg.c src/ui/js_sec.c \$(SEC_SRC) $(APP_SRC) $(MON_SRC) $(MEDIA_SRC)# Documents and printing (M9): lord-doc helper sessions and our own IPP/mDNS/eSCL client# (src/doc, src/print; see docs/DOCUMENTS.md and docs/PRINTING.md). No Poppler or libarchive here.@@ -225,11 +225,11 @@ build/wasm-js-test: tests/wasm_js_test.c src/ui/js_wasm.c src/ui/js_wasm.h $(WASbuild/wasm-test: tests/wasm_test.c $(WASM_SRC) src/wasm/wasm.h src/wasm/wasm_int.h@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/wasm_test.c $(WASM_SRC) -lm-PKG_SRC := src/pkg/crypto.c src/pkg/pgp.c src/pkg/xz.c src/pkg/deb.c src/pkg/idx.c src/pkg/status.c src/pkg/resolve.c src/pkg/http.c src/pkg/pkg.c+PKG_SRC := src/pkg/crypto.c src/pkg/pgp.c src/pkg/xz.c src/pkg/deb.c src/pkg/idx.c src/pkg/pin.c src/pkg/status.c src/pkg/resolve.c src/pkg/http.c src/pkg/pkg.cbuild/pkg-test: tests/pkg_test.c $(PKG_SRC) src/sec/cgroup.c build/obj/ui/stb.o src/admin.h $(wildcard src/pkg/*.h)@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pkg_test.c $(PKG_SRC) src/sec/cgroup.c build/obj/ui/stb.o -lm-PKG_READ_SRC := src/pkg/deb.c src/pkg/idx.c src/pkg/status.c src/pkg/resolve.c src/pkg/crypto.c+PKG_READ_SRC := src/pkg/deb.c src/pkg/idx.c src/pkg/pin.c src/pkg/status.c src/pkg/resolve.c src/pkg/crypto.cbuild/pkg-js-test: tests/pkg_js_test.c src/ui/js_pkg.c $(PKG_READ_SRC) $(QJS_OBJ) $(wildcard src/pkg/*.h)$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pkg_js_test.c src/ui/js_pkg.c $(PKG_READ_SRC) $(QJS_OBJ) -lm -lpthreadbuild/thbrk-test: tests/thbrk_test.c src/ui/thbrk.c src/ui/thbrk.hdiff --git a/server/src/pkg/idx.c b/server/src/pkg/idx.cindex b5431f8..6c5cd49 100644--- a/server/src/pkg/idx.c+++ b/server/src/pkg/idx.c@@ -3,6 +3,7 @@#include "../errcode.h"#include "deb.h"#include "crypto.h"+#include "pin.h"#include <errno.h>#include <fcntl.h>@@ -16,6 +17,19 @@/* ---- reading ---- */+/* Each record's priority under apt's preferences (none: the index's source priorities). */+static void idx_pins(Idx *x) {+ free(x->prio);+ x->prio = NULL;+ x->pin_stamp = pin_stamp();+ PinSet *set = pin_load();+ if (!set) return;+ x->prio = malloc(sizeof *x->prio * (x->h->npkg ? x->h->npkg : 1));+ if (x->prio)+ for (long i = 0; i < (long)x->h->npkg; i++) x->prio[i] = (int16_t)pin_priority(set, x, i);+ pin_free(set);+}+int idx_open(Idx *x, const char *dir) {memset(x, 0, sizeof *x);char path[512];@@ -51,11 +65,13 @@ int idx_open(Idx *x, const char *dir) {x->desc_len = h->len_desc;x->mtime = (long)st.st_mtime;x->ino = (long)st.st_ino;+ idx_pins(x);return 0;}void idx_close(Idx *x) {if (x->map) munmap(x->map, x->map_len);+ free(x->prio);memset(x, 0, sizeof *x);}@@ -64,7 +80,11 @@ int idx_refresh(Idx *x, const char *dir) {snprintf(path, sizeof path, "%s/index.bin", dir);struct stat st;if (stat(path, &st)) { if (x->map) { idx_close(x); return 1; } return 0; }- if (x->map && (long)st.st_ino == x->ino && (long)st.st_mtime == x->mtime) return 0;+ if (x->map && (long)st.st_ino == x->ino && (long)st.st_mtime == x->mtime) {+ if (pin_stamp() == x->pin_stamp) return 0;+ idx_pins(x); /* the preferences changed: same index, new priorities */+ return 1;+ }Idx n;if (idx_open(&n, dir)) return 0;idx_close(x);diff --git a/server/src/pkg/idx.h b/server/src/pkg/idx.hindex 8bcc31c..39cf60e 100644--- a/server/src/pkg/idx.h+++ b/server/src/pkg/idx.h@@ -64,12 +64,16 @@ typedef struct {size_t str_len, desc_len, map_len;void *map;long mtime, ino;+ int16_t *prio; /* each record's priority after apt's preferences (pin.c) */+ unsigned long pin_stamp;} Idx;int idx_open(Idx *x, const char *dir); /* 0 ok */void idx_close(Idx *x);int idx_refresh(Idx *x, const char *dir); /* reopens if it changed on disk; 1 if it changed */static inline const char *idx_s(const Idx *x, uint32_t off) { return off < x->str_len ? x->str + off : ""; }+/* A record's priority (apt's preferences applied; the source's own priority without them). */+static inline int idx_prio(const Idx *x, long rec) { return x->prio ? x->prio[rec] : (int)x->src[x->pkg[rec].src].prio; }/* First package with that name (or -1); the next ones with the same name follow it. */long idx_find(const Idx *x, const char *name, size_t n);/* Range [*lo, *hi) of those that provide name. */diff --git a/server/src/pkg/pin.c b/server/src/pkg/pin.cnew file mode 100644index 0000000..6773622--- /dev/null+++ b/server/src/pkg/pin.c@@ -0,0 +1,321 @@+/* pin.c — apt's preferences for the package engine (see pin.h). */+#include "pin.h"++#include <ctype.h>+#include <dirent.h>+#include <fcntl.h>+#include <fnmatch.h>+#include <regex.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <strings.h>+#include <sys/stat.h>+#include <unistd.h>++#define PIN_FILES 64+#define PIN_FILE_MAX (256 * 1024)+#define PIN_MAX 512+#define PAT_MAX 32++const char *pin_root = "";++enum { PT_ORIGIN, PT_RELEASE, PT_VERSION };++typedef struct {+ char *text; /* glob, regex body or source name */+ regex_t re;+ unsigned char kind; /* 0 glob, 1 regex, 2 src:name */+} Pat;++typedef struct {+ Pat pat[PAT_MAX];+ int npat, general, type, prio;+ char *value; /* origin host or version glob */+ char *rel_a, *rel_n, *rel_c, *rel_o, *rel_l; /* release fields (NULL: any) */+ int rel_other; /* a release key we can't check (v=, b=…): never matches */+} Pin;++struct PinSet { Pin pin[PIN_MAX]; int n; };++/* ---- reading ---- */++static char *read_file(const char *path, size_t *len) {+ int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);+ if (fd < 0) return NULL;+ struct stat st;+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_size > PIN_FILE_MAX) { close(fd); return NULL; }+ char *buf = malloc((size_t)st.st_size + 1);+ size_t n = 0;+ while (buf && n < (size_t)st.st_size) {+ ssize_t r = read(fd, buf + n, (size_t)st.st_size - n);+ if (r <= 0) break;+ n += (size_t)r;+ }+ close(fd);+ if (!buf) return NULL;+ buf[n] = 0;+ *len = n;+ return buf;+}++/* apt's rule for preferences.d names: [A-Za-z0-9_.-] only, no extension or ".pref". */+static int good_name(const char *n) {+ if (!n[0] || n[0] == '.') return 0;+ for (const char *p = n; *p; p++)+ if (!isalnum((unsigned char)*p) && *p != '_' && *p != '-' && *p != '.') return 0;+ const char *dot = strrchr(n, '.');+ return !dot || !strcmp(dot, ".pref");+}++static int cmp_str(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }++/* The files in apt's order; returns how many paths were stored. */+static int list_files(char paths[][512]) {+ int n = 0;+ snprintf(paths[n++], 512, "%s/etc/apt/preferences", pin_root);+ char dir[300];+ snprintf(dir, sizeof dir, "%s/etc/apt/preferences.d", pin_root);+ DIR *d = opendir(dir);+ if (!d) return n;+ char *names[PIN_FILES];+ int nn = 0;+ struct dirent *e;+ while ((e = readdir(d)) && nn < PIN_FILES)+ if (good_name(e->d_name) && strlen(e->d_name) < 200) names[nn++] = strdup(e->d_name);+ closedir(d);+ qsort(names, (size_t)nn, sizeof *names, cmp_str);+ for (int i = 0; i < nn; i++) {+ if (names[i] && n < PIN_FILES + 1) snprintf(paths[n++], 512, "%s/%s", dir, names[i]);+ free(names[i]);+ }+ return n;+}++unsigned long pin_stamp(void) {+ char paths[PIN_FILES + 1][512];+ int n = list_files(paths);+ unsigned long h = 1469598103u;+ for (int i = 0; i < n; i++) {+ struct stat st;+ if (stat(paths[i], &st)) continue;+ for (const char *p = paths[i]; *p; p++) h = (h ^ (unsigned char)*p) * 1099511628211u;+ h = (h ^ (unsigned long)st.st_size) * 1099511628211u;+ h = (h ^ (unsigned long)st.st_mtime) * 1099511628211u;+ h = (h ^ (unsigned long)st.st_mtim.tv_nsec) * 1099511628211u;+ }+ return h;+}++/* ---- parsing ---- */++static char *trim(char *s) {+ while (*s == ' ' || *s == '\t') s++;+ char *e = s + strlen(s);+ while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\r')) *--e = 0;+ return s;+}++static void pin_clear(Pin *p) {+ for (int i = 0; i < p->npat; i++) {+ if (p->pat[i].kind == 1) regfree(&p->pat[i].re);+ free(p->pat[i].text);+ }+ free(p->value); free(p->rel_a); free(p->rel_n); free(p->rel_c); free(p->rel_o); free(p->rel_l);+ memset(p, 0, sizeof *p);+}++static int parse_packages(Pin *p, char *v) {+ if (!strcmp(v, "*")) { p->general = 1; return 0; }+ for (char *tok = strtok(v, " \t"); tok; tok = strtok(NULL, " \t")) {+ if (p->npat == PAT_MAX) return -1;+ Pat *t = &p->pat[p->npat];+ size_t l = strlen(tok);+ if (l > 2 && tok[0] == '/' && tok[l - 1] == '/') {+ tok[l - 1] = 0;+ if (regcomp(&t->re, tok + 1, REG_EXTENDED | REG_NOSUB)) return -1;+ t->kind = 1;+ t->text = strdup(tok + 1);+ } else if (!strncmp(tok, "src:", 4) && tok[4]) {+ t->kind = 2;+ t->text = strdup(tok + 4);+ } else {+ t->kind = 0;+ t->text = strdup(tok);+ }+ if (!t->text) { if (t->kind == 1) regfree(&t->re); return -1; }+ p->npat++;+ }+ return p->npat ? 0 : -1;+}++static char *unquote(const char *s) {+ size_t l = strlen(s);+ if (l >= 2 && s[0] == '"' && s[l - 1] == '"') return strndup(s + 1, l - 2);+ return strdup(s);+}++static int parse_pin(Pin *p, char *v) {+ if (!strncmp(v, "origin", 6) && (v[6] == ' ' || v[6] == '\t')) {+ p->type = PT_ORIGIN;+ p->value = unquote(trim(v + 7));+ return p->value ? 0 : -1;+ }+ if (!strncmp(v, "version", 7) && (v[7] == ' ' || v[7] == '\t')) {+ p->type = PT_VERSION;+ p->value = strdup(trim(v + 8));+ return p->value && p->value[0] ? 0 : -1;+ }+ if (!strncmp(v, "release", 7) && (v[7] == ' ' || v[7] == '\t' || !v[7])) {+ p->type = PT_RELEASE;+ char *rest = v + 7;+ for (char *tok = strtok(rest, ","); tok; tok = strtok(NULL, ",")) {+ tok = trim(tok);+ if (!*tok) continue;+ char *eq = strchr(tok, '=');+ if (!eq) { p->rel_other = 1; continue; } /* a bare version number */+ *eq = 0;+ char *key = trim(tok), *val = unquote(trim(eq + 1)), **slot = NULL;+ if (!val) return -1;+ if (!strcmp(key, "a")) slot = &p->rel_a;+ else if (!strcmp(key, "n")) slot = &p->rel_n;+ else if (!strcmp(key, "c")) slot = &p->rel_c;+ else if (!strcmp(key, "o")) slot = &p->rel_o;+ else if (!strcmp(key, "l")) slot = &p->rel_l;+ if (slot && !*slot) *slot = val;+ else { free(val); p->rel_other = 1; }+ }+ return 0;+ }+ return -1;+}++static void parse_text(PinSet *set, char *text) {+ Pin cur;+ memset(&cur, 0, sizeof cur);+ int have_pkg = 0, have_pin = 0, have_prio = 0, bad = 0;+ char *next = text;+ for (;;) { /* line by line: blank lines end a stanza */+ char *line = next;+ if (line) {+ char *nl = strchr(line, '\n');+ if (nl) { *nl = 0; next = nl + 1; } else next = NULL;+ if (!nl && !*line) line = NULL;+ }+ int end = !line;+ char *l = end ? NULL : trim(line);+ if (end || !*l) { /* blank line or end: close the stanza */+ if (have_pkg && have_pin && have_prio && !bad && set->n < PIN_MAX) set->pin[set->n++] = cur;+ else pin_clear(&cur);+ memset(&cur, 0, sizeof cur);+ have_pkg = have_pin = have_prio = bad = 0;+ if (end) break;+ continue;+ }+ if (*l == '#') continue;+ char *colon = strchr(l, ':');+ if (!colon) { bad = 1; continue; }+ *colon = 0;+ char *key = trim(l), *val = trim(colon + 1);+ if (!strcasecmp(key, "Package")) { if (have_pkg || parse_packages(&cur, val)) bad = 1; have_pkg = 1; }+ else if (!strcasecmp(key, "Pin")) { if (have_pin || parse_pin(&cur, val)) bad = 1; have_pin = 1; }+ else if (!strcasecmp(key, "Pin-Priority")) {+ char *e;+ long v = strtol(val, &e, 10);+ if (have_prio || e == val || *trim(e) || v < -32768 || v > 32767) bad = 1;+ cur.prio = (int)v;+ have_prio = 1;+ } else if (strcasecmp(key, "Explanation")) bad = 1;+ }+}++PinSet *pin_load(void) {+ char paths[PIN_FILES + 1][512];+ int n = list_files(paths);+ PinSet *set = calloc(1, sizeof *set);+ if (!set) return NULL;+ for (int i = 0; i < n; i++) {+ size_t len;+ char *text = read_file(paths[i], &len);+ if (!text) continue;+ parse_text(set, text);+ free(text);+ }+ if (!set->n) { free(set); return NULL; }+ return set;+}++void pin_free(PinSet *p) {+ if (!p) return;+ for (int i = 0; i < p->n; i++) pin_clear(&p->pin[i]);+ free(p);+}++/* ---- matching ---- */++static int pkg_matches(const Pin *p, const char *name, const char *source) {+ for (int i = 0; i < p->npat; i++) {+ const Pat *t = &p->pat[i];+ if (t->kind == 0 && !fnmatch(t->text, name, 0)) return 1;+ if (t->kind == 1 && !regexec(&t->re, name, 0, NULL, 0)) return 1;+ if (t->kind == 2 && !strcmp(t->text, source[0] ? source : name)) return 1;+ }+ return 0;+}++/* The host of a source URI ("http://archive.raspberrypi.com/debian" → archive.raspberrypi.com). */+static void uri_host(const char *uri, char *out, size_t n) {+ out[0] = 0;+ const char *p = strstr(uri, "://");+ if (!p) return; /* file: and other local sources have the origin "" */+ p += 3;+ const char *at = strpbrk(p, "@/");+ if (at && *at == '@') p = at + 1;+ size_t l = strcspn(p, ":/");+ if (l >= n) l = n - 1;+ memcpy(out, p, l);+ out[l] = 0;+}++static int field_ok(const char *want, const char *have) {+ return !want || !fnmatch(want, have, 0);+}++static int pin_matches(const Pin *p, const Idx *x, long rec) {+ const IdxPkg *k = &x->pkg[rec];+ const IdxSrc *s = &x->src[k->src];+ switch (p->type) {+ case PT_ORIGIN: {+ char host[256];+ uri_host(idx_s(x, s->uri), host, sizeof host);+ return !strcasecmp(p->value, host);+ }+ case PT_VERSION:+ return !fnmatch(p->value, idx_s(x, k->version), 0);+ case PT_RELEASE:+ return !p->rel_other && field_ok(p->rel_a, idx_s(x, s->suite)) && field_ok(p->rel_n, idx_s(x, s->codename)) &&+ field_ok(p->rel_c, idx_s(x, s->component)) && field_ok(p->rel_o, idx_s(x, s->origin)) &&+ field_ok(p->rel_l, idx_s(x, s->label));+ }+ return 0;+}++int pin_priority(const PinSet *set, const Idx *x, long rec) {+ const IdxPkg *k = &x->pkg[rec];+ int dflt = (int)x->src[k->src].prio;+ if (!set) return dflt;+ const char *name = idx_s(x, k->name), *source = idx_s(x, k->source);+ /* the first specific pin naming the package decides, for the versions it matches */+ for (int i = 0; i < set->n; i++) {+ const Pin *p = &set->pin[i];+ if (p->general || !pkg_matches(p, name, source)) continue;+ if (pin_matches(p, x, rec)) return p->prio;+ break;+ }+ /* then the first general pin matching the version's source */+ for (int i = 0; i < set->n; i++) {+ const Pin *p = &set->pin[i];+ if (p->general && pin_matches(p, x, rec)) return p->prio;+ }+ return dflt;+}diff --git a/server/src/pkg/pin.h b/server/src/pkg/pin.hnew file mode 100644index 0000000..b199ee6--- /dev/null+++ b/server/src/pkg/pin.h@@ -0,0 +1,31 @@+/* pin.h — apt's preferences (/etc/apt/preferences and /etc/apt/preferences.d), so the package engine+ * picks the same candidate versions as apt (docs/PACKAGES.md, "Priorities").+ *+ * Read the way apt reads them: /etc/apt/preferences first, then the files of preferences.d in C+ * sort order whose names are only [A-Za-z0-9_.-] and have no extension or ".pref". A stanza has+ * `Package:` (glob patterns, /regex/ or src:name, separated by spaces; exactly "*" makes it a+ * general pin), `Pin:` (`origin <host>`, `release a=,n=,c=,o=,l=` or `version <glob>`) and+ * `Pin-Priority:`; `Explanation:` and comments are ignored, a malformed stanza is skipped.+ *+ * A version's priority, as in apt: the first specific pin that names its package decides, for the+ * versions it matches; otherwise the first general pin matching its source; otherwise the source's+ * own priority (500, 100 for backports, 1 for experimental). Nothing here is written or executed;+ * the files are world-readable configuration owned by root. */+#ifndef LORD_PKG_PIN_H+#define LORD_PKG_PIN_H++#include "idx.h"++/* Prefix for /etc/apt (tests point it at a fake tree; "" for the real system). */+extern const char *pin_root;++typedef struct PinSet PinSet;++PinSet *pin_load(void); /* NULL: no preferences (or no memory) */+void pin_free(PinSet *p);+/* A cheap fingerprint of the preference files (names, sizes, dates), to notice a change. */+unsigned long pin_stamp(void);+/* Priority of index record `rec` (a version from one source). */+int pin_priority(const PinSet *p, const Idx *x, long rec);++#endifdiff --git a/server/src/pkg/resolve.c b/server/src/pkg/resolve.cindex 93564ed..626fb8f 100644--- a/server/src/pkg/resolve.c+++ b/server/src/pkg/resolve.c@@ -65,23 +65,30 @@ static uint32_t hash(const char *s, size_t n) {return h;}+/* apt's choice (pkgPolicy::GetCandidateVer): versions from the newest down, and only a strictly+ * higher priority replaces the one chosen, so among equals the newest wins; the installed version+ * counts at least 100 (dpkg's status, as in apt), and versions older than it only with 1000 or+ * more; a negative priority never. -1 when nothing is available or the installed version is the+ * best (it may not be in the index). */long pkg_candidate(const Idx *x, const char *name, size_t n, const StPkg *inst) {long first = idx_find(x, name, n);if (first < 0) return -1;- long best500 = -1, best100 = -1;- int inst_in_100 = 0;+ long best = -1;+ int max = 0, inst_seen = !inst;for (long i = first; i < (long)x->h->npkg && !strcmp(idx_s(x, x->pkg[i].name), idx_s(x, x->pkg[first].name)); i++) {- uint32_t prio = x->src[x->pkg[i].src].prio;- if (prio >= 500 && best500 < 0) best500 = i;- if (prio >= 100 && best100 < 0) best100 = i;- if (inst && prio >= 100 && prio < 500 && !strcmp(idx_s(x, x->pkg[i].version), inst->version)) inst_in_100 = 1;- }- if (!inst) return best500 >= 0 ? best500 : best100;- /* installed from backports (or newer than normal): stays on the highest version */- long c = best500;- if (inst_in_100 || c < 0 || ver_cmp(idx_s(x, x->pkg[c].version), inst->version) < 0) c = best100;- if (c >= 0 && ver_cmp(idx_s(x, x->pkg[c].version), inst->version) < 0) return -1; /* never downgrade */- return c;+ int c = inst ? ver_cmp(idx_s(x, x->pkg[i].version), inst->version) : 1;+ if (!inst_seen && c < 0) { /* past the installed version, which no source carries */+ inst_seen = 1;+ if (100 > max) { max = 100; best = -1; }+ }+ int p = idx_prio(x, i);+ if (p < 0) continue;+ if (c == 0) { inst_seen = 1; if (p < 100) p = 100; }+ if (c < 0 && p < 1000) continue;+ if (p > max) { max = p; best = i; }+ }+ if (!inst_seen && 100 > max) best = -1;+ return best;}static int node_get(S *s, const char *name, size_t n) {diff --git a/server/tests/pkg_test.c b/server/tests/pkg_test.cindex abeae1c..cce46c2 100644--- a/server/tests/pkg_test.c+++ b/server/tests/pkg_test.c@@ -11,6 +11,7 @@#include "pkg/deb.h"#include "pkg/idx.h"#include "pkg/pgp.h"+#include "pkg/pin.h"#include "pkg/pkg.h"#include "pkg/resolve.h"#include "pkg/status.h"@@ -315,6 +316,102 @@ static void test_resolver(const char *dir) {idx_close(&x);}+/* apt's preferences: the Raspberry Pi image's pins (tools/rpi/rootfs/etc/apt/preferences.d/raspberrypi+ * and build-img.sh's lordos-kernel), checked against what apt chooses. */+static const char *PI_DEBIAN =+ "Package: chromium\nVersion: 140.0-1~deb13u1\nArchitecture: arm64\nFilename: pool/c/chromium.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: c\n\n"+ "Package: bluez\nVersion: 5.82-1\nArchitecture: arm64\nFilename: pool/b/bluez.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: b\n\n"+ "Package: mesa\nVersion: 25.0-1\nArchitecture: arm64\nFilename: pool/m/mesa.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: m\n\n";+static const char *PI_RPI =+ "Package: chromium\nVersion: 1:141.0-1+rpt1\nArchitecture: arm64\nFilename: pool/c/chromium-rpt.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: c\n\n"+ "Package: bluez\nVersion: 5.82-1+rpt2\nArchitecture: arm64\nFilename: pool/b/bluez-rpt.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: b\n\n"+ "Package: rpi-eeprom\nVersion: 28.4-1\nArchitecture: all\nFilename: pool/r/rpi-eeprom.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: r\n\n"+ "Package: raspi-config\nVersion: 2025\nArchitecture: all\nFilename: pool/r/raspi-config.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: r\n\n"+ "Package: linux-image-rpi-2712\nVersion: 1:6.12.47-1+rpt1\nArchitecture: arm64\nFilename: pool/l/k.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: k\n\n";+static const char *PI_LORDOS =+ "Package: rpi-eeprom\nVersion: 28.3-1\nArchitecture: all\nFilename: pool/rpi/r/rpi-eeprom.deb\nSize: 9\nSHA256: 0000000000000000000000000000000000000000000000000000000000000000\nDescription: r\n\n";+static const char *PI_PREFS =+ "# LordOS on Raspberry Pi\n"+ "Package: *\nPin: origin archive.raspberrypi.com\nPin-Priority: 100\n\n"+ "Package: raspi-firmware linux-image-rpi-* linux-image-*-rpi-* rpi-eeprom raspi-utils* raspberrypi-archive-keyring\n"+ "Pin: origin archive.raspberrypi.com\nPin-Priority: 990\n";+static const char *PI_KERNEL =+ "Explanation: LordOS's own kernel\nPackage: linux-image-rpi-2712 linux-image-*+rpt-rpi-2712\nPin: version *\nPin-Priority: -1\n";++static void write_text(const char *path, const char *text) {+ FILE *f = fopen(path, "w");+ if (f) { fputs(text, f); fclose(f); }+}++static const char *cand_ver(const Idx *x, const char *name, const StPkg *inst) {+ long c = pkg_candidate(x, name, strlen(name), inst);+ return c < 0 ? "-" : idx_s(x, x->pkg[c].version);+}++static void test_pins(const char *dir) {+ char root[600], path[700];+ snprintf(root, sizeof root, "%s/pins", dir);+ snprintf(path, sizeof path, "%s/etc", root); mkdir(root, 0755); mkdir(path, 0755);+ snprintf(path, sizeof path, "%s/etc/apt", root); mkdir(path, 0755);+ snprintf(path, sizeof path, "%s/etc/apt/preferences.d", root); mkdir(path, 0755);+ pin_root = root;+ IdxBuilder *b = idxb_new();+ int deb = idxb_source(b, "http://deb.debian.org/debian", "stable", "main", "trixie", "Debian", "Debian", 0, 0, 500);+ int rpi = idxb_source(b, "http://archive.raspberrypi.com/debian", "trixie", "main", "trixie", "Raspberry Pi Foundation", "Raspberry Pi Foundation", 0, 0, 500);+ int lord = idxb_source(b, "https://lordos.org/apt", "stable", "rpi", "lordos", "LordOS", "LordOS", 0, 0, 500);+ idxb_packages(b, deb, PI_DEBIAN, strlen(PI_DEBIAN), "arm64");+ idxb_packages(b, rpi, PI_RPI, strlen(PI_RPI), "arm64");+ idxb_packages(b, lord, PI_LORDOS, strlen(PI_LORDOS), "arm64");+ char idir[700], err[200];+ snprintf(idir, sizeof idir, "%s/pinidx", dir);+ mkdir(idir, 0755);+ CHECK(!idxb_write(b, idir, "arm64", err, sizeof err), "write pinned index: %s", err);+ idxb_free(b);+ Idx x;+ CHECK(!idx_open(&x, idir) && !x.prio, "no preferences: source priorities only");+ CHECK(!strcmp(cand_ver(&x, "chromium", NULL), "1:141.0-1+rpt1"), "without pins the newest wins (the old bug): %s", cand_ver(&x, "chromium", NULL));+ idx_close(&x);++ snprintf(path, sizeof path, "%s/etc/apt/preferences.d/raspberrypi", root); write_text(path, PI_PREFS);+ snprintf(path, sizeof path, "%s/etc/apt/preferences.d/lordos-kernel", root); write_text(path, PI_KERNEL);+ snprintf(path, sizeof path, "%s/etc/apt/preferences.d/ignored.list", root); write_text(path, "Package: *\nPin: version *\nPin-Priority: -1\n");+ CHECK(!idx_open(&x, idir) && x.prio, "preferences read");+ CHECK(!strcmp(cand_ver(&x, "chromium", NULL), "140.0-1~deb13u1"), "chromium from Debian: %s", cand_ver(&x, "chromium", NULL));+ CHECK(!strcmp(cand_ver(&x, "bluez", NULL), "5.82-1"), "bluez from Debian: %s", cand_ver(&x, "bluez", NULL));+ CHECK(!strcmp(cand_ver(&x, "rpi-eeprom", NULL), "28.4-1"), "rpi-eeprom pinned 990 to Raspberry Pi: %s", cand_ver(&x, "rpi-eeprom", NULL));+ CHECK(!strcmp(cand_ver(&x, "raspi-config", NULL), "2025"), "only in Raspberry Pi's archive (100): still installable");+ CHECK(!strcmp(cand_ver(&x, "linux-image-rpi-2712", NULL), "-"), "Raspberry Pi's kernel never (-1)");+ CHECK(!strcmp(cand_ver(&x, "mesa", NULL), "25.0-1"), "untouched package");+ /* installed versions */+ StPkg deb_chr = { .name = "chromium", .version = "140.0-1~deb13u1" };+ StPkg rpt_chr = { .name = "chromium", .version = "1:141.0-1+rpt1" };+ StPkg old_chr = { .name = "chromium", .version = "139.0-1" };+ CHECK(!strcmp(cand_ver(&x, "chromium", &deb_chr), "140.0-1~deb13u1"), "Debian's chromium installed: stays (no switch to +rpt)");+ CHECK(!strcmp(cand_ver(&x, "chromium", &rpt_chr), "1:141.0-1+rpt1"), "an installed +rpt build is not downgraded below 1000");+ CHECK(!strcmp(cand_ver(&x, "chromium", &old_chr), "140.0-1~deb13u1"), "an older chromium upgrades to Debian's");+ StPkg rpt_cfg = { .name = "raspi-config", .version = "2024" };+ CHECK(!strcmp(cand_ver(&x, "raspi-config", &rpt_cfg), "2025"), "same priority (100): the newer one upgrades");+ /* a change in preferences.d is noticed without a new index */+ snprintf(path, sizeof path, "%s/etc/apt/preferences.d/zz-rpt.pref", root);+ write_text(path, "Package: chromium\nPin: release o=Raspberry Pi Foundation\nPin-Priority: 1001\n\nbroken stanza\n\nPackage: bluez\nPin: release v=1\nPin-Priority: 2000\n");+ struct timespec ts = { 0, 20000000 };+ nanosleep(&ts, NULL);+ CHECK(idx_refresh(&x, idir) == 1, "preferences changed: priorities recomputed");+ CHECK(!strcmp(cand_ver(&x, "chromium", &deb_chr), "1:141.0-1+rpt1"), "chromium pinned 1001 to Raspberry Pi: %s", cand_ver(&x, "chromium", &deb_chr));+ StPkg new_chr = { .name = "chromium", .version = "1:150" };+ CHECK(!strcmp(cand_ver(&x, "chromium", &new_chr), "1:141.0-1+rpt1"), "priority 1000 or more downgrades, as in apt");+ CHECK(!strcmp(cand_ver(&x, "bluez", NULL), "5.82-1"), "release v= is not checked: never matches");+ idx_close(&x);+ /* the first specific pin naming a package decides; later ones for it are ignored */+ snprintf(path, sizeof path, "%s/etc/apt/preferences.d/0-first", root);+ write_text(path, "Package: /^chrom/\nPin: origin deb.debian.org\nPin-Priority: 600\n");+ CHECK(!idx_open(&x, idir), "reopened");+ CHECK(!strcmp(cand_ver(&x, "chromium", &deb_chr), "140.0-1~deb13u1"), "a /regex/ pin read first wins over zz-rpt.pref: %s", cand_ver(&x, "chromium", &deb_chr));+ CHECK(!strcmp(cand_ver(&x, "chromium", &new_chr), "-"), "nothing newer than the installed one: no candidate");+ idx_close(&x);+ pin_root = dir; /* the other tests: no preferences */+}+static int cmd_plan(int argc, char **argv) {Idx x;Status st = { 0 };@@ -397,7 +494,9 @@ int main(int argc, char **argv) {test_notes();test_crypto();test_compress();+ pin_root = dir; /* never the machine's own /etc/apt/preferences */test_resolver(dir);+ test_pins(dir);char cmd[256];snprintf(cmd, sizeof cmd, "rm -rf %s", dir);if (system(cmd)) {}