Artifact f2d7472c171c3eca750a30a820bf968caa5fb6b5f07629f6026a67c8880d4cba:
- File
src/curate.c
— part of check-in
[cef6a0ad45]
at
2026-11-17 07:37:01
on branch trunk
— suit curate writes a page for deciding what each asset is worth keeping
suit curate [--since DAYS] [--out FILE] runs a usage scan and writes one self-contained HTML page, mode 0600, atomically, to curate.html in suit's home folder or to FILE. It changes no wardrobe file, config, stamp or ledger. The page names projects and stays on this machine; it loads nothing from the network and has no inline handlers.
Each skill, agent and command in the wardrobes is a row: uses in the window and in each of the last four weeks, last used, the projects it was used in, its description and what that costs every session, where it is worn, whether it is in the attic, and whether it is the owner's or an upstream's. Origin comes from the entries of the check-in the upstream's record names, so an upstream's skills beside the owner's own in skills/ are told apart. A name in two wardrobes is tagged with its wardrobe.
Each row starts from a suggestion with its reason: retire when unused in the window, unless its files changed within it; limit when three or more uses all came from one known project; otherwise keep. A folder that is not a known project cannot be limited to, and the page says how to make it one. The owner picks keep, limit or retire per row, with keys or in bulk on what is shown, and copies or saves the choices as a short text file. Applying it is a separate step, not in this check-in. The format is in docs/curate.md: one line per asset, `keep KIND WARDROBE NAME`, `retire KIND WARDROBE NAME`, or `personal KIND WARDROBE NAME PROJECT`.
The page is src/curate.html, turned into a C array at build time with od and awk, so the release build needs no other tool. Values are written as JSON with markup characters, U+2028 and U+2029 escaped and invalid UTF-8 replaced. The page script has its own test, run under node where node is installed and reported as skipped elsewhere. A new fuzz target covers the front-matter reader and the JSON writer.
Page sizes: 37 KB for the test fixture, 252 KB for 500 assets, 805 KB at the 4096-asset bound.
A new suite, test/curate.sh, with 288 cases, is part of the gate.
2993 unit checks (was 2855), 620 shell cases, 1153 store cases, 846 placement cases, 378 project cases, 236 usage cases and 288 curate cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (66 minutes, on a loaded machine; one earlier run failed only the loaded speed measurement, at 1.06 to 1.11 ms, and this run passed it at 0.99 ms). 100% coverage in all 31 source files. No existing case was changed. (user: dmestas, size: 25751) [annotate] [blame] [check-ins using]
#include "curate.h" #include "assets.h" #include "conf.h" #include "platform.h" #include "resolve.h" #include "stamp.h" #include "store.h" #include "tally.h" #include "upstream.h" #include "curate_page.inc" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> constexpr size_t PATH_CAP = 4096; constexpr int64_t DAY = 86400; constexpr uint32_t WEAR_WORDS = (CONF_SECTION_MAX + 63) / 64; constexpr uint32_t NO_ITEM = 0xffffffffu; constexpr uint32_t UNMATCHED = CURATE_ASSETS_MAX; // a record's key from here on: a usage name that matched no asset constexpr char MARKER[] = "@@DATA@@"; constexpr char ATTIC[] = "attic"; static_assert((int)USE_SKILL == (int)AK_SKILL && (int)USE_AGENT == (int)AK_AGENT && (int)USE_COMMAND == (int)AK_COMMAND); // What can be lowered in a test build, to reach the bounds without a great deal of data. static struct { size_t assets; size_t recs; size_t page; } lim; static void limits_init(void) { lim.assets = CURATE_ASSETS_MAX; lim.recs = CURATE_RECS_MAX; lim.page = CURATE_PAGE_MAX; #ifdef SUIT_TEST // SUIT_CURATE_LIMITS=NAME=N,...: assets recs page const char *v = sys_getenv("SUIT_CURATE_LIMITS"); if (v == nullptr) return; const struct { const char *name; size_t *at; } known[] = {{"assets=", &lim.assets}, {"recs=", &lim.recs}, {"page=", &lim.page}}; for (const char *item = v; *item != '\0';) { for (size_t i = 0; i < sizeof known / sizeof known[0]; i++) { size_t n = strlen(known[i].name); if (strncmp(item, known[i].name, n) == 0) *known[i].at = strtoul(item + n, nullptr, 10); } const char *comma = strchr(item, ','); item = comma != nullptr ? comma + 1 : item + strlen(item); } #endif } static void put(CurateBuf *b, const char *s) { curate_put(b, s, strlen(s)); } static void put_num(CurateBuf *b, uint64_t v) { char t[24]; [[maybe_unused]] int n = snprintf(t, sizeof t, "%llu", (unsigned long long)v); ensure(n > 0 && (size_t)n < sizeof t); put(b, t); } // ---- matching and time ---------------------------------------------------------------------- static int name_cmp(const char *name, const char *s, size_t len) { size_t nl = strlen(name); int c = memcmp(name, s, nl < len ? nl : len); return c != 0 ? c : (nl > len) - (nl < len); } uint32_t curate_matches(const char *const *names, uint32_t n, const char *usage, size_t len, void (*cb)(void *state, uint32_t index), void *state) { // An asset's name has no colon, so the only name a usage name can be is what follows its last one. const char *tail = usage; for (size_t i = 0; i < len; i++) if (usage[i] == ':') tail = usage + i + 1; size_t tlen = len - (size_t)(tail - usage); uint32_t lo = 0; uint32_t hi = n; while (lo < hi) { uint32_t mid = lo + (hi - lo) / 2; if (name_cmp(names[mid], tail, tlen) < 0) lo = mid + 1; else hi = mid; } uint32_t count = 0; for (; lo < n && name_cmp(names[lo], tail, tlen) == 0; lo++) { cb(state, lo); count++; } return count; } void curate_cuts(int64_t now, int days, CurateCuts *c) { tally_civil(now - days * DAY, c->window); tally_civil(now - 90 * DAY, c->d90); for (int i = 0; i < 4; i++) tally_civil(now - (4 - i) * 7 * DAY, c->week[i]); } int curate_week(const CurateCuts *c, const char *t) { for (int i = 3; i >= 0; i--) if (memcmp(t, c->week[i], TALLY_TIME_LEN) >= 0) return i; return -1; } // ---- the assets --------------------------------------------------------------------------- typedef struct { AssetKind kind; uint32_t aidx; // its place in the list of its kind const char *name; const char *file; uint8_t ward; uint64_t size; bool size_cut; int64_t newest; // the newest modification time of its files, -1 if it has none size_t desc_len; char desc[CURATE_DESC_KEEP + 1]; bool listed; // some outfit lists it uint64_t reach[WEAR_WORDS]; // the wears that reach it, as section numbers int upstream; // the section of the upstream that supplies it, -1 uint32_t uses; uint32_t uses90; char last[20]; uint32_t weeks[4]; uint32_t pfirst; // its projects are recs[pfirst .. pfirst + pcount) uint32_t pcount; } Item; static Item items[CURATE_ASSETS_MAX]; static uint32_t nitems; static uint32_t skipped_names; static const char *name_ptr[CURATE_ASSETS_MAX]; static uint32_t kstart[AK_COMMAND + 1]; static uint32_t kcount[AK_COMMAND + 1]; static uint32_t item_of[AK_COMMAND + 1][ASSET_MAX]; static Assets assets; static const char *const kind_word[AK_COMMAND + 1] = {"skill", "agent", "command"}; static const char *const kind_dir[AK_COMMAND + 1] = {"skills", "agents", "commands"}; // Byte order of the kind's word: agent, command, skill. static const int kind_rank[AK_COMMAND + 1] = {2, 0, 1}; static int item_cmp(const void *pa, const void *pb) { const Item *a = pa; const Item *b = pb; int c = kind_rank[a->kind] - kind_rank[b->kind]; c = c != 0 ? c : strcmp(a->name, b->name); return c != 0 ? c : (int)a->ward - (int)b->ward; } static int collect_items(const Wards *w) { int rc = assets_load(w, &assets); if (rc != 0) return rc; for (int k = AK_SKILL; k <= AK_COMMAND; k++) { const NameList *l = &assets.list[k]; for (uint32_t i = 0; i < l->n; i++) { item_of[k][i] = NO_ITEM; // A name suit cannot place, and the choices file cannot hold. if (!conf_name_ok(l->name[i], strlen(l->name[i]), false)) { skipped_names++; continue; } if (nitems == lim.assets) { say(STDERR_FILENO, "suit: more than %zu assets in the wardrobes: too many for one page\n", lim.assets); return 1; } items[nitems++] = (Item){.kind = (AssetKind)k, .aidx = i, .name = l->name[i], .file = l->file[i], .ward = l->ward[i], .newest = -1, .upstream = -1}; } } qsort(items, nitems, sizeof items[0], item_cmp); for (uint32_t i = 0; i < nitems; i++) { name_ptr[i] = items[i].name; item_of[items[i].kind][items[i].aidx] = i; if (kcount[items[i].kind]++ == 0) kstart[items[i].kind] = i; } return 0; } static void on_reach(void *state, const Section *wear, const Section *outfit, AssetKind kind, uint32_t index) { const Conf *c = *(const Conf *const *)state; if (item_of[kind][index] == NO_ITEM) return; Item *it = &items[item_of[kind][index]]; it->listed = true; if (wear == nullptr || strcmp(outfit->arg[0], ATTIC) == 0) return; uint32_t bit = (uint32_t)(wear - c->sections); it->reach[bit / 64] |= 1ull << (bit % 64); } static bool item_worn(const Item *it) { uint64_t any = 0; for (uint32_t i = 0; i < WEAR_WORDS; i++) any |= it->reach[i]; return any != 0; } static const char *entry_value(const Conf *c, const Section *s, const char *key) { for (uint32_t i = 0; i < s->count; i++) if (strcmp(c->entries[s->first + i].key, key) == 0) return c->entries[s->first + i].val; return nullptr; } // An asset is an upstream's when the wardrobe holds the record upstreams/NAME and the asset is // one of the top-level entries under the upstream's `into` in the check-in the record names. typedef struct { int section; AssetKind kind; uint8_t ward; } Origin; static void on_entry(void *state, const char *entry) { const Origin *o = state; for (uint32_t i = 0; i < nitems; i++) { Item *it = &items[i]; if (it->kind == o->kind && it->ward == o->ward && strcmp(it->file, entry) == 0) it->upstream = o->section; } } static int find_origins(const Conf *c, const Wards *w) { for (uint32_t i = 0; i < c->nsections; i++) { const Section *s = &c->sections[i]; if (s->kind != KIND_UPSTREAM) continue; char path[PATH_CAP]; [[maybe_unused]] int n = snprintf(path, sizeof path, "%s/%s/%s", w->w[s->ward].store, UPSTREAM_RECORD_DIR, s->arg[0]); ensure(n > 0 && (size_t)n < sizeof path); struct stat st; if (sys_stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue; // Only what lies directly in the folder of a kind can be an asset. const char *into = entry_value(c, s, "into"); if (into == nullptr) into = "skills"; int kind = -1; for (int k = AK_SKILL; k <= AK_COMMAND; k++) if (strcmp(into, kind_dir[k]) == 0) kind = k; if (kind < 0) continue; Origin o = {.section = (int)i, .kind = (AssetKind)kind, .ward = s->ward}; int rc = store_upstream_entries(w->w[s->ward].name, s->arg[0], into, on_entry, &o); if (rc != 0) return rc; } return 0; } // ---- the files of an asset --------------------------------------------------------------- typedef struct { char path[2 * PATH_CAP]; int depth; uint32_t files; uint64_t bytes; int64_t newest; bool cut; // something was not counted bool full; // the files are too many: stop bool failed; // a file or folder cannot be read: stop } Walk; static int walk_dir(Walk *w); static int walk_entry(void *state, const char *entry) { Walk *w = state; size_t len = strlen(w->path); [[maybe_unused]] int n = snprintf(w->path + len, sizeof w->path - len, "/%s", entry); ensure(n > 0 && (size_t)n < sizeof w->path - len); // CURATE_DEPTH_MAX names of at most 255 bytes fit struct stat st; if (sys_lstat(w->path, &st) != 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", w->path, strerror(errno)); w->failed = true; } else if (S_ISREG(st.st_mode)) { if (w->files == CURATE_FILES_MAX) { w->cut = true; w->full = true; } else { w->files++; w->bytes += (uint64_t)st.st_size; int64_t t = platform_mtime_sec(&st); if (t > w->newest) w->newest = t; } } else if (S_ISDIR(st.st_mode)) { if (w->depth == CURATE_DEPTH_MAX) { w->cut = true; } else { w->depth++; (void)walk_dir(w); w->depth--; } } w->path[len] = '\0'; return w->failed || w->full ? 1 : 0; } static int walk_dir(Walk *w) { if (sys_listdir(w->path, walk_entry, w) < 0) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", w->path, strerror(errno)); w->failed = true; } return w->failed || w->full ? 1 : 0; } // The first CAP bytes of PATH: 0, 1 if there is no such file, -1 with errno set. static int read_head(const char *path, char *buf, size_t cap, size_t *len) { int fd = sys_open(path, O_RDONLY, 0); if (fd < 0) return errno == ENOENT ? 1 : -1; size_t n = 0; while (n < cap) { ssize_t r = sys_read(fd, buf + n, cap - n); if (r < 0 && errno == EINTR) continue; if (r < 0) { int err = errno; (void)sys_close(fd); errno = err; return -1; } if (r == 0) break; n += (size_t)r; } (void)sys_close(fd); // a file only read: a failed close is not reported *len = n; return 0; } static Walk walk; static char head[CURATE_HEAD_MAX]; static char dtext[CURATE_HEAD_MAX + 1]; static int read_failed(const char *path) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(errno)); return 1; } // Its size on disk, its newest modification time and its description. static int fill_files(Item *it, const Wards *w) { char path[PATH_CAP]; [[maybe_unused]] int n = snprintf(path, sizeof path, "%s/%s/%s", w->w[it->ward].store, kind_dir[it->kind], it->file); ensure(n > 0 && (size_t)n < sizeof path); // the store's folder leaves room for a name of any length char text_path[PATH_CAP + 16]; if (it->kind == AK_SKILL) { walk = (Walk){.newest = -1}; memcpy(walk.path, path, strlen(path) + 1); (void)walk_dir(&walk); if (walk.failed) return 1; // the message was said it->size = walk.bytes; it->size_cut = walk.cut; it->newest = walk.newest; [[maybe_unused]] int m = snprintf(text_path, sizeof text_path, "%s/SKILL.md", path); ensure(m > 0 && (size_t)m < sizeof text_path); } else { struct stat st; if (sys_stat(path, &st) != 0) return read_failed(path); it->size = (uint64_t)st.st_size; it->newest = platform_mtime_sec(&st); memcpy(text_path, path, strlen(path) + 1); } size_t got = 0; int r = read_head(text_path, head, sizeof head, &got); if (r < 0) return read_failed(text_path); if (r == 0) { it->desc_len = curate_front_description(head, got, dtext); size_t kept = curate_cut(dtext, it->desc_len, CURATE_DESC_KEEP); memcpy(it->desc, dtext, kept); it->desc[kept] = '\0'; } return 0; } // ---- the uses ------------------------------------------------------------------------------ typedef struct { const char *ptr; // a project's key or a name, in the tally uint32_t len; uint32_t key; // the item, or UNMATCHED and the kind uint32_t count; } Rec; static Rec recs[CURATE_RECS_MAX]; static uint32_t nrecs; static uint32_t first_unmatched; typedef struct { CurateCuts cuts; uint64_t events; char oldest[20]; bool full; const TallyEvent *ev; bool in_window; AssetKind kind; bool hit; } Agg; static void add_rec(Agg *g, uint32_t key, const char *ptr, size_t len) { if (nrecs == lim.recs) { g->full = true; return; } recs[nrecs++] = (Rec){.ptr = ptr, .len = (uint32_t)len, .key = key, .count = 1}; } static void on_match(void *state, uint32_t index) { Agg *g = state; uint32_t at = kstart[g->kind] + index; Item *it = &items[at]; const TallyEvent *ev = g->ev; g->hit = true; if (it->last[0] == '\0' || memcmp(ev->time, it->last, TALLY_TIME_LEN) > 0) { memcpy(it->last, ev->time, TALLY_TIME_LEN); it->last[TALLY_TIME_LEN] = '\0'; } int week = curate_week(&g->cuts, ev->time); if (week >= 0) it->weeks[week]++; if (memcmp(ev->time, g->cuts.d90, TALLY_TIME_LEN) >= 0) it->uses90++; if (g->in_window) { it->uses++; add_rec(g, at, ev->project, ev->project_len); } } static void on_event(void *state, const TallyEvent *ev) { Agg *g = state; g->events++; if (g->oldest[0] == '\0' || memcmp(ev->time, g->oldest, TALLY_TIME_LEN) < 0) { memcpy(g->oldest, ev->time, TALLY_TIME_LEN); g->oldest[TALLY_TIME_LEN] = '\0'; } g->ev = ev; g->in_window = memcmp(ev->time, g->cuts.window, TALLY_TIME_LEN) >= 0; g->kind = (AssetKind)ev->kind; g->hit = false; (void)curate_matches(name_ptr + kstart[g->kind], kcount[g->kind], ev->name, ev->name_len, on_match, g); if (!g->hit && g->in_window) add_rec(g, UNMATCHED + (uint32_t)g->kind, ev->name, ev->name_len); } static int text_cmp(const Rec *a, const Rec *b) { int c = memcmp(a->ptr, b->ptr, a->len < b->len ? a->len : b->len); return c != 0 ? c : (a->len > b->len) - (a->len < b->len); } static int rec_cmp_bytes(const void *pa, const void *pb) { const Rec *a = pa; const Rec *b = pb; int c = (a->key > b->key) - (a->key < b->key); return c != 0 ? c : text_cmp(a, b); } // Most used first, then in byte order, then by kind. static int rec_cmp_count(const void *pa, const void *pb) { const Rec *a = pa; const Rec *b = pb; int c = (a->count < b->count) - (a->count > b->count); c = c != 0 ? c : text_cmp(a, b); return c != 0 ? c : (a->key > b->key) - (a->key < b->key); } // Sort the records, make one of each run that is the same project of the same asset (or the same // unmatched name), and put each asset's projects most used first. static void settle_recs(void) { qsort(recs, nrecs, sizeof recs[0], rec_cmp_bytes); uint32_t w = 0; for (uint32_t i = 0; i < nrecs;) { uint32_t j = i + 1; while (j < nrecs && rec_cmp_bytes(&recs[i], &recs[j]) == 0) j++; recs[w] = recs[i]; recs[w++].count = j - i; i = j; } nrecs = w; uint32_t i = 0; while (i < nrecs && recs[i].key < UNMATCHED) { uint32_t j = i; while (j < nrecs && recs[j].key == recs[i].key) j++; qsort(recs + i, j - i, sizeof recs[0], rec_cmp_count); items[recs[i].key].pfirst = i; items[recs[i].key].pcount = j - i; i = j; } first_unmatched = i; qsort(recs + i, nrecs - i, sizeof recs[0], rec_cmp_count); } // ---- the page ---------------------------------------------------------------------------------- static void put_json(CurateBuf *b, const char *s) { curate_json_string(b, s, strlen(s)); } // The first ten bytes of T, a time, as a date. static void day_of(const char *t, char out[11]) { memcpy(out, t, 10); out[10] = '\0'; } static void date_of(int64_t sec, char out[11]) { char t[20]; tally_civil(sec, t); day_of(t, out); } static void put_scope(CurateBuf *b, const Section *wear, bool target_only) { char t[2 * CONF_LINE_MAX]; [[maybe_unused]] int n = target_only ? snprintf(t, sizeof t, "%s", wear->arg[1]) : snprintf(t, sizeof t, "%s%s%s%s%s", wear->arg[0], wear->arg[1][0] != '\0' ? " " : "", wear->arg[1], wear->arg[2][0] != '\0' ? " machine=" : "", wear->arg[2]); ensure(n >= 0 && (size_t)n < sizeof t); // a line is at most CONF_LINE_MAX bytes put_json(b, t); } static bool reaches(const Item *it, uint32_t section) { return (it->reach[section / 64] >> (section % 64) & 1) != 0; } static void put_item(CurateBuf *b, const Item *it, const Conf *c, const Wards *w, int64_t now, int days) { bool worn = item_worn(it); put(b, "{\"k\":"); put_json(b, kind_word[it->kind]); put(b, ",\"n\":"); put_json(b, it->name); put(b, ",\"w\":"); put_json(b, w->w[it->ward].name); put(b, ",\"t\":"); put_json(b, it->desc); put(b, ",\"dl\":"); put_num(b, it->desc_len); put(b, ",\"tr\":"); put(b, it->desc_len > strlen(it->desc) ? "1" : "0"); put(b, ",\"b\":"); put_num(b, it->size); put(b, ",\"bt\":"); put(b, it->size_cut ? "1" : "0"); char day[11] = ""; if (it->newest >= 0) date_of(it->newest, day); put(b, ",\"m\":"); put_json(b, day); put(b, ",\"fr\":"); put(b, it->newest >= 0 && it->newest >= now - days * DAY ? "1" : "0"); put(b, ",\"s\":"); put_json(b, worn ? "worn" : it->listed ? "attic" : "loose"); put(b, ",\"wr\":["); bool all_personal = worn; bool first = true; for (uint32_t s = 0; s < c->nsections; s++) { if (!reaches(it, s)) continue; put(b, first ? "" : ","); first = false; put_scope(b, &c->sections[s], false); all_personal &= strcmp(c->sections[s].arg[0], "personal") == 0; } put(b, "],\"only\":["); first = true; for (uint32_t s = 0; all_personal && s < c->nsections; s++) { if (!reaches(it, s)) continue; put(b, first ? "" : ","); first = false; put_scope(b, &c->sections[s], true); } put(b, "],\"o\":"); if (it->upstream < 0) { put_json(b, "yours"); } else { char o[CONF_NAME_MAX + 16]; [[maybe_unused]] int n = snprintf(o, sizeof o, "upstream %s", c->sections[it->upstream].arg[0]); ensure(n > 0 && (size_t)n < sizeof o); put_json(b, o); } put(b, ",\"u\":"); put_num(b, it->uses); put(b, ",\"u90\":"); put_num(b, it->uses90); char last[11] = ""; if (it->last[0] != '\0') day_of(it->last, last); put(b, ",\"l\":"); put_json(b, last); put(b, ",\"wk\":["); for (int i = 0; i < 4; i++) { put(b, i > 0 ? "," : ""); put_num(b, it->weeks[i]); } put(b, "],\"pn\":"); put_num(b, it->pcount); put(b, ",\"p\":["); uint32_t shown = it->pcount < CURATE_PROJECTS_MAX ? it->pcount : CURATE_PROJECTS_MAX; uint64_t other = 0; for (uint32_t i = 0; i < it->pcount; i++) { const Rec *r = &recs[it->pfirst + i]; if (i >= shown) { other += r->count; continue; } put(b, i > 0 ? ",[" : "["); curate_json_string(b, r->ptr, r->len); put(b, ","); put_num(b, r->count); put(b, "]"); } put(b, "],\"po\":"); put_num(b, other); put(b, "}"); } static void put_harnesses(CurateBuf *b, const Conf *c, bool with) { bool first = true; for (uint32_t i = 0; i < c->nsections; i++) { const Section *s = &c->sections[i]; if (s->kind != KIND_HARNESS || tally_scannable(s) != with) continue; put(b, first ? "" : ","); first = false; put_json(b, s->arg[0]); } } static void put_data(CurateBuf *b, const Conf *c, const Wards *w, const Agg *g, int64_t now, int days) { char day[11]; date_of(now, day); put(b, "{\"v\":1,\"made\":"); put_json(b, day); put(b, ",\"days\":"); put_num(b, (uint64_t)days); put(b, ",\"events\":"); put_num(b, g->events); put(b, ",\"oldest\":"); char oldest[11] = ""; if (g->oldest[0] != '\0') day_of(g->oldest, oldest); put_json(b, oldest); put(b, ",\"with\":["); put_harnesses(b, c, true); put(b, "],\"without\":["); put_harnesses(b, c, false); put(b, "],\"skipped\":"); put_num(b, skipped_names); put(b, ",\"unmatched\":{\"n\":"); put_num(b, nrecs - first_unmatched); put(b, ",\"top\":["); for (uint32_t i = first_unmatched; i < nrecs && i < first_unmatched + CURATE_TOP; i++) { put(b, i > first_unmatched ? ",[" : "["); put_json(b, kind_word[recs[i].key - UNMATCHED]); put(b, ","); curate_json_string(b, recs[i].ptr, recs[i].len); put(b, ","); put_num(b, recs[i].count); put(b, "]"); } put(b, "]},\"assets\":["); for (uint32_t i = 0; i < nitems; i++) { put(b, i > 0 ? "," : ""); put_item(b, &items[i], c, w, now, days); } put(b, "]}"); } static char page[CURATE_PAGE_MAX]; // The page: the template with the data where the marker is. False if it does not fit. static bool make_page(size_t *len, const Conf *c, const Wards *w, const Agg *g, int64_t now, int days) { size_t m = 0; size_t mlen = sizeof MARKER - 1; while (memcmp(curate_page + m, MARKER, mlen) != 0) { m++; ensure(m + mlen <= sizeof curate_page); // the template has its marker } CurateBuf b = {.p = page, .cap = lim.page}; curate_put(&b, (const char *)curate_page, m); put_data(&b, c, w, g, now, days); curate_put(&b, (const char *)curate_page + m + mlen, sizeof curate_page - m - mlen); *len = b.len; return !b.over; } // ---- the command ------------------------------------------------------------------------------- static int usage_error(const char *why) { say(STDERR_FILENO, "suit: usage: %s\nusage: suit curate [--since DAYS] [--out FILE]\n", why); return 2; } static char out_path[PATH_CAP]; int cmd_curate(int argc, char **argv) { int days = TALLY_WINDOW_DEFAULT; const char *given = nullptr; for (int i = 0; i < argc; i++) { const char *a = argv[i]; if (i + 1 == argc) return usage_error("an option needs a value, or is not one"); const char *v = argv[++i]; if (strcmp(a, "--since") == 0) { char *end; long n = strtol(v, &end, 10); if (*v == '\0' || *end != '\0' || n < 1 || n > TALLY_DAYS_KEPT) return usage_error("--since takes a number of days from 1 to 400"); days = (int)n; } else if (strcmp(a, "--out") == 0) { if (*v == '\0' || strlen(v) >= PATH_CAP - 4) return usage_error("--out takes a file name of at most 4091 bytes"); given = v; } else { return usage_error("an option needs a value, or is not one"); } } if (given == nullptr) { if (suit_home_path(out_path, sizeof out_path, "curate.html") == 0) return 1; given = out_path; } else if (strchr(given, '/') == nullptr) { snprintf(out_path, sizeof out_path, "./%s", given); // the writer wants a folder part } else { snprintf(out_path, sizeof out_path, "%s", given); } limits_init(); int rc = tally_open(); if (rc != 0) return rc; const Conf *conf = tally_conf(); const Wards *wards = tally_wards(); int64_t now = tally_now(); rc = collect_items(wards); if (rc != 0) return rc; resolve_reach(conf, &assets, on_reach, &conf); rc = find_origins(conf, wards); if (rc != 0) return rc; for (uint32_t i = 0; i < nitems; i++) { rc = fill_files(&items[i], wards); if (rc != 0) return rc; } Agg g = {}; curate_cuts(now, days, &g.cuts); tally_each(on_event, &g); if (g.full) { say(STDERR_FILENO, "suit: more than %zu uses to count in the window; try a shorter --since\n", lim.recs); return 1; } settle_recs(); size_t len = 0; if (!make_page(&len, conf, wards, &g, now, days)) { say(STDERR_FILENO, "suit: the page would be over %zu bytes\n", lim.page); return 1; } int err = sys_write_atomic_exact(out_path, page, len, 0600); if (err != 0) { say(STDERR_FILENO, "suit: cannot write %s: %s\n", out_path, strerror(err)); return 1; } say(STDOUT_FILENO, "%s\n", given); return 0; }