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: release notes, restart flag and file: sources
Federico Pereiraca51192ac62e
diff --git a/server/src/pkg/deb.c b/server/src/pkg/deb.cindex c5db626..330d69d 100644--- a/server/src/pkg/deb.c+++ b/server/src/pkg/deb.c@@ -4,6 +4,7 @@#include "deb.h"#include <ctype.h>+#include <stdlib.h>#include <string.h>#include <strings.h>@@ -207,3 +208,49 @@ int deb_next_group(const char **pp, const char *end, DebRel *alts, int max) {p = *pp;}}++/* Release notes (docs/UPDATES.md): deb822 stanzas "Package", "Version" and a multi-line "Notes"+ * whose continuation lines start with one space and use " ." for an empty line, like a Debian+ * "Description". Returns the Markdown text of the matching stanza (malloc'd), or NULL. */+char *deb_notes_find(const char *buf, size_t len, const char *pkg, const char *version, size_t *outlen) {+ const char *pos = buf, *end = buf + len, *st, *se;+ size_t pl = strlen(pkg), vl = strlen(version);+ while (d822_next_stanza(&pos, end, &st, &se)) {+ D822 it = { st, se };+ D822Field f;+ int name_ok = 0, ver_ok = 0;+ const char *notes = NULL;+ size_t nlen = 0;+ while (d822_next_field(&it, &f)) {+ if (d822_is(&f, "Package")) name_ok = f.vlen == pl && !memcmp(f.value, pkg, pl);+ else if (d822_is(&f, "Version")) ver_ok = f.vlen == vl && !memcmp(f.value, version, vl);+ else if (d822_is(&f, "Notes")) { notes = f.value; nlen = f.vlen; }+ }+ if (!name_ok || !ver_ok || !notes) continue;+ char *out = malloc(nlen + 1);+ if (!out) return NULL;+ size_t o = 0;+ const char *p = notes, *e = notes + nlen;+ int first = 1;+ while (p < e) {+ const char *nl = memchr(p, '\n', (size_t)(e - p));+ if (!nl) nl = e;+ const char *l = p;+ size_t ll = (size_t)(nl - p);+ if (ll && l[ll - 1] == '\r') ll--;+ if (!first && ll && *l == ' ') { l++; ll--; }+ if (!first && ll == 1 && *l == '.') ll = 0;+ if (!(first && !ll)) { /* the empty text after "Notes:" */+ if (o) out[o++] = '\n';+ memcpy(out + o, l, ll);+ o += ll;+ }+ first = 0;+ p = nl + 1;+ }+ out[o] = 0;+ if (outlen) *outlen = o;+ return out;+ }+ return NULL;+}diff --git a/server/src/pkg/deb.h b/server/src/pkg/deb.hindex 467ff8c..3ca5503 100644--- a/server/src/pkg/deb.h+++ b/server/src/pkg/deb.h@@ -40,4 +40,8 @@ typedef struct {/* Reads the next group of alternatives (up to the comma). Returns the count (0 = end). */int deb_next_group(const char **p, const char *end, DebRel *alts, int max);+/* Release notes of pkg at version from a ReleaseNotes file (deb822 Package, Version, Notes):+ * the Markdown text, malloc'd (the caller frees it), or NULL. See docs/UPDATES.md. */+char *deb_notes_find(const char *buf, size_t len, const char *pkg, const char *version, size_t *outlen);+#endifdiff --git a/server/src/pkg/http.c b/server/src/pkg/http.cindex a2c2a59..5fd25a4 100644--- a/server/src/pkg/http.c+++ b/server/src/pkg/http.c@@ -4,7 +4,7 @@* http://), Content-Length, chunked or until the connection closes, If-Modified-Since, and* a 30 s maximum time without data. It sends the parent messages over the pipe:* [type 1 byte][item 4][length 4][data] type H = "status date length", D = data, E = end- * (E with text = error). */+ * (E with text = error). A file: URL (a local repository) is read by the same child, as nobody. */#include "http.h"#include "../errcode.h"#include "crypto.h"@@ -188,8 +188,41 @@ static int parse_url(const char *url, char *host, size_t hn, char *port, size_treturn 0;}+/* A file: source (a repository on a local disk or a USB stick, as apt's file: method): read as+ * nobody too, so only world-readable files; sent like a download, and the parent checks it the same+ * way (size, SHA-256 against the signed index). Only from a configured source: an HTTP redirect+ * never leads here. */+static void get_file(Conn *c, const FetchItem *it, uint32_t idx) {+ char err[200] = "";+ const char *path = it->url + 5;+ while (path[0] == '/' && path[1] == '/') path++; /* file:///x and file:/x */+ int fd = path[0] == '/' && !strstr(path, "/../") ? open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK) : -1;+ struct stat st;+ if (fd < 0 || fstat(fd, &st) || !S_ISREG(st.st_mode)) {+ lerr_set(err, sizeof err, "pkg.http.status", "status", "404", NULL);+ } else if (it->ims > 0 && (int64_t)st.st_mtime <= it->ims) {+ send_msg(c->out, 'H', idx, "304 0 0", 7);+ } else if ((uint64_t)st.st_size > it->max) {+ lerr_set(err, sizeof err, "pkg.http.tooLarge", NULL);+ } else {+ char h[96];+ snprintf(h, sizeof h, "200 %lld %lld", (long long)st.st_mtime, (long long)st.st_size);+ if (send_msg(c->out, 'H', idx, h, (uint32_t)strlen(h))) _exit(1);+ for (;;) {+ ssize_t r = read(fd, c->buf, sizeof c->buf);+ if (r < 0 && errno == EINTR) continue;+ if (r < 0) { lerr_set(err, sizeof err, "pkg.http.connectionLost", NULL); break; }+ if (r == 0) break;+ if (send_msg(c->out, 'D', idx, c->buf, (uint32_t)r)) _exit(1);+ }+ }+ if (fd >= 0) close(fd);+ send_msg(c->out, 'E', idx, err, (uint32_t)strlen(err));+}+/* Downloads one item: sends H, D… and E. */static void get_one(Conn *c, const FetchItem *it, uint32_t idx) {+ if (!strncmp(it->url, "file:", 5)) { get_file(c, it, idx); return; }char url[1100], err[200] = "", line[4096];snprintf(url, sizeof url, "%s", it->url);for (int redirects = 0;; redirects++) {diff --git a/server/src/pkg/pkg.c b/server/src/pkg/pkg.cindex c007e89..b20ed62 100644--- a/server/src/pkg/pkg.c+++ b/server/src/pkg/pkg.c@@ -128,6 +128,7 @@ static void mirror_uri(const char *in, char *out, size_t n) {/* no TLS of our own: https is downloaded over http (security comes from the signature, as in apt) */if (!strncmp(in, "https://", 8)) snprintf(out, n, "http://%s", in + 8);else if (!strncmp(in, "http://", 7)) snprintf(out, n, "%s", in);+ else if (!strncmp(in, "file:/", 6)) snprintf(out, n, "%s", in); /* a local repository (http.c) */size_t l = strlen(out);while (l && out[l - 1] == '/') out[--l] = 0;}@@ -433,7 +434,20 @@ static void icons_cleanup(IdxBuilder *b) {/* ---- pkg-update ---- */-typedef struct { char path[PATH_MAX]; char ext[4]; int kind, src; char comp[48]; } Wanted; /* kind 0 Packages, 1 Translation, 2 DEP-11, 3 icons */+#define NOTES_MAX (4u << 20)++/* PKG_DIR/notes: the verified ReleaseNotes of every source (deb822: Package, Version, Notes), read+ * by lord.pkg.releaseNotes() without root. Replaced whole; removed when no source has any. */+static void write_notes(const char *notes, size_t len) {+ if (!len) { unlink(PKG_DIR "/notes"); return; }+ int fd = open(PKG_DIR "/.notes.new", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0644);+ if (fd < 0) return;+ int ok = write(fd, notes, len) == (ssize_t)len;+ if (close(fd)) ok = 0;+ if (!ok || rename(PKG_DIR "/.notes.new", PKG_DIR "/notes")) unlink(PKG_DIR "/.notes.new");+}++typedef struct { char path[PATH_MAX]; char ext[4]; int kind, src; char comp[48]; } Wanted; /* kind 0 Packages, 1 Translation, 2 DEP-11, 3 icons, 4 release notes */static int file_ok(const char *path, uint64_t size) {struct stat st;@@ -543,11 +557,12 @@ void pkg_act_update(const AdminReq *r, const char *arg, size_t len) {for (int c = 0; c < src[i].ncomp; c++) {char base[200];const char *ext;- for (int k = 0; k < 4; k++) {+ for (int k = 0; k < 5; k++) {if (k == 0) snprintf(base, sizeof base, "%s/binary-%s/Packages", src[i].comps[c], arch);else if (k == 1) snprintf(base, sizeof base, "%s/i18n/Translation-en", src[i].comps[c]);else if (k == 2) snprintf(base, sizeof base, "%s/dep11/Components-%s.yml", src[i].comps[c], arch);- else snprintf(base, sizeof base, "%s/dep11/icons-64x64.tar", src[i].comps[c]);+ else if (k == 3) snprintf(base, sizeof base, "%s/dep11/icons-64x64.tar", src[i].comps[c]);+ else snprintf(base, sizeof base, "%s/notes/ReleaseNotes", src[i].comps[c]); /* LordOS's release notes */const RelFile *f = rel_pick(&R[i], base, &ext);if (k == 2 && f && !strcmp(ext, "")) f = NULL; /* the uncompressed YAML is huge */if (!f) { if (k == 0) admin_log("pkg.sourceMissingFile", "uri", src[i].uri, "suite", src[i].suite, "file", base, NULL); continue; }@@ -584,6 +599,8 @@ void pkg_act_update(const AdminReq *r, const char *arg, size_t len) {for (int i = 0; i < ns; i++) if (rel[i].status != 304) changed = 1;free(rel);progress("lists", (uint64_t)ns, (uint64_t)ns);+ /* release notes listed but not stored yet (an index built before LordOS read them): rebuild */+ for (int k = 0; k < nw && !changed; k++) if (w[k].kind == 4 && access(PKG_DIR "/notes", F_OK)) changed = 1;Idx cur;if (!changed && !failures && !idx_open(&cur, PKG_DIR)) {/* no list changed since last time: the index is already up to date */@@ -655,16 +672,32 @@ void pkg_act_update(const AdminReq *r, const char *arg, size_t len) {if (!b || !srcid) { admin_err("sys.outOfMemory", NULL); return; }for (int i = 0; i < ns; i++) srcid[i] = -1;int pk_files = 0, pk_missing = 0;- for (int pass = 0; pass < 4; pass++)+ char *notes = NULL; /* every source's ReleaseNotes, one after the other */+ size_t notes_len = 0;+ for (int pass = 0; pass < 5; pass++)for (int k = 0; k < nw; k++) {Wanted *x = &w[k];if (x->kind != pass || !good[x->src]) continue;- progress("index", (uint64_t)(pass * nw + k), (uint64_t)(4 * nw));+ progress("index", (uint64_t)(pass * nw + k), (uint64_t)(5 * nw));if (srcid[x->src] < 0) {Release *rr = &R[x->src];int prio = rr->not_auto ? (rr->but_auto ? 100 : 1) : 500;srcid[x->src] = idxb_source(b, src[x->src].uri, src[x->src].suite, x->comp, rr->codename, rr->origin, rr->label, rr->date, rr->valid_until, prio);}+ if (pass == 4) {+ size_t tl;+ uint8_t *t = load_index_file(x->path, x->ext, &tl);+ char *nn = t && notes_len + tl + 2 <= NOTES_MAX ? realloc(notes, notes_len + tl + 2) : NULL;+ if (nn) {+ notes = nn;+ memcpy(notes + notes_len, t, tl);+ notes_len += tl;+ notes[notes_len++] = '\n';+ notes[notes_len++] = '\n';+ }+ free(t);+ continue;+ }if (pass == 3) {size_t tl;uint8_t *t = load_index_file(x->path, x->ext, &tl);@@ -689,6 +722,8 @@ void pkg_act_update(const AdminReq *r, const char *arg, size_t len) {char err[256];int wrote = pk_files && !idxb_write(b, PKG_DIR, arch, err, sizeof err);if (wrote) icons_cleanup(b);+ if (wrote) write_notes(notes, notes_len);+ free(notes);uint32_t count = idxb_count(b);idxb_free(b);if (wrote) gc_lists(w, nw, src, ns);diff --git a/server/src/ui/js_pkg.c b/server/src/ui/js_pkg.cindex 2f69a94..31e0e58 100644--- a/server/src/ui/js_pkg.c+++ b/server/src/ui/js_pkg.c@@ -11,6 +11,7 @@#include "../pkg/status.h"#include <ctype.h>+#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>@@ -547,7 +548,68 @@ static JSValue f_icon(JSContext *c, JSValueConst this_val, int argc, JSValueConsreturn icon[0] && !access(path, R_OK) ? JS_NewString(c, path) : JS_NULL;}+/* lord.pkg.releaseNotes(name, version) → Markdown text or null: the release notes a source publishes+ * for that version (ReleaseNotes in its signed Release, stored by pkg-update; docs/UPDATES.md). */+static JSValue f_release_notes(JSContext *c, JSValueConst this_val, int argc, JSValueConst *argv) {+ (void)this_val;+ char name[129], ver[129];+ if (argc < 2 || arg_name(c, argv[0], name, sizeof name)) return JS_NULL;+ const char *v = JS_ToCString(c, argv[1]);+ if (!v) return JS_NULL;+ int ok = strlen(v) < sizeof ver && deb_valid_version(v, strlen(v));+ if (ok) snprintf(ver, sizeof ver, "%s", v);+ JS_FreeCString(c, v);+ if (!ok) return JS_NULL;+ int fd = open(PKG_DIR "/notes", O_RDONLY | O_CLOEXEC | O_NOFOLLOW);+ if (fd < 0) return JS_NULL;+ struct stat st;+ char *buf = NULL;+ size_t got = 0;+ if (!fstat(fd, &st) && S_ISREG(st.st_mode) && st.st_size > 0 && st.st_size <= (4 << 20) && (buf = malloc((size_t)st.st_size))) {+ while (got < (size_t)st.st_size) {+ ssize_t r = read(fd, buf + got, (size_t)st.st_size - got);+ if (r <= 0) break;+ got += (size_t)r;+ }+ }+ close(fd);+ size_t tl = 0;+ char *text = buf ? deb_notes_find(buf, got, name, ver, &tl) : NULL;+ free(buf);+ JSValue r = text ? JS_NewStringLen(c, text, tl) : JS_NULL;+ free(text);+ return r;+}++/* lord.pkg.rebootRequired() → null, or the packages that asked for a restart ([] if none named):+ * the Debian convention /run/reboot-required(.pkgs), written by LordOS's postinst and its kernel hook. */+static JSValue f_reboot_required(JSContext *c, JSValueConst this_val, int argc, JSValueConst *argv) {+ (void)this_val; (void)argc; (void)argv;+ if (access("/run/reboot-required", F_OK)) return JS_NULL;+ JSValue arr = JS_NewArray(c);+ FILE *f = fopen("/run/reboot-required.pkgs", "re");+ char line[160];+ uint32_t n = 0;+ while (f && n < 64 && fgets(line, sizeof line, f)) {+ line[strcspn(line, "\r\n")] = 0;+ if (!deb_valid_name(line, strlen(line))) continue;+ int dup = 0;+ for (uint32_t i = 0; i < n && !dup; i++) {+ JSValue e = JS_GetPropertyUint32(c, arr, i);+ const char *es = JS_ToCString(c, e);+ dup = es && !strcmp(es, line);+ JS_FreeCString(c, es);+ JS_FreeValue(c, e);+ }+ if (!dup) JS_SetPropertyUint32(c, arr, n++, JS_NewString(c, line));+ }+ if (f) fclose(f);+ return arr;+}+static const JSCFunctionListEntry pkg_funcs[] = {+ JS_CFUNC_DEF("releaseNotes", 2, f_release_notes),+ JS_CFUNC_DEF("rebootRequired", 0, f_reboot_required),JS_CFUNC_DEF("available", 0, f_available),JS_CFUNC_DEF("lastUpdate", 0, f_last_update),JS_CFUNC_DEF("sources", 0, f_sources),diff --git a/server/tests/pkg_test.c b/server/tests/pkg_test.cindex 2ddc364..abeae1c 100644--- a/server/tests/pkg_test.c+++ b/server/tests/pkg_test.c@@ -339,6 +339,23 @@ static int cmd_plan(int argc, char **argv) {return 0;}+/* ReleaseNotes (docs/UPDATES.md): the stanza of that package and version, " ." as an empty line */+static void test_notes(void) {+ const char *t = "Package: lordos\nVersion: 0.8.1\nNotes:\n ### Added\n .\n - Updates from the Store\n\n"+ "Package: lordos\nVersion: 0.8.2~beta1\nNotes: one line\n\nPackage: other\nVersion: 0.8.1\nNotes:\n x\n";+ size_t n = 0;+ char *s = deb_notes_find(t, strlen(t), "lordos", "0.8.1", &n);+ CHECK(s && !strcmp(s, "### Added\n\n- Updates from the Store") && n == strlen(s), "notes 0.8.1: [%s]", s ? s : "(null)");+ free(s);+ s = deb_notes_find(t, strlen(t), "lordos", "0.8.2~beta1", &n);+ CHECK(s && !strcmp(s, "one line"), "notes on the field line: [%s]", s ? s : "(null)");+ free(s);+ CHECK(!deb_notes_find(t, strlen(t), "lordos", "0.8.3", &n), "no notes for another version");+ CHECK(!deb_notes_find(t, strlen(t), "lordo", "0.8.1", &n), "the package name must match whole");+ CHECK(ver_cmp("0.8.1~staging1", "0.8.0") > 0 && ver_cmp("0.8.1~staging1", "0.8.1") < 0 && ver_cmp("0.8.1~beta2", "0.8.1~beta10") < 0,+ "pre-release versions sort between releases");+}+int main(int argc, char **argv) {if (argc >= 4 && !strcmp(argv[1], "pgp")) {PgpKeyring kr = { 0 };@@ -377,6 +394,7 @@ int main(int argc, char **argv) {if (!mkdtemp(dir)) return 1;test_versions();test_deb822();+ test_notes();test_crypto();test_compress();test_resolver(dir);