Artifact dc239600aa1b8c71c37833489862ab5edbcaf434a982c47df03a80c527a98116:
- File
src/ledger.c
— part of check-in
[f96cbc3342]
at
2026-11-13 03:51:02
on branch trunk
— Phase 4a: outfits limited to a machine, and instruction files joined
suit machine shows or sets this machine's name. An outfit worn with machine=NAME applies on that machine and nowhere else, and where it and an unlimited outfit supply the same destination the machine's wins.
A harness has one instruction file per scope. When the unlimited and the machine-limited outfit both supply instructions, suit writes one file with the unlimited text first and the machine's after it, and links the harness to that. It is rewritten when either source changes and removed when no longer needed. This is the one case where an edit in the store reaches a harness only after suit's next run.
The ledger gains an entry for a file suit wrote, so suit off removes the joined file too.
420 unit checks, 227 shell cases, 146 store cases, 612 placement cases, under clang and under gcc 16. 100% coverage in all 16 source files. The no-change run costs about 0.46 ms over an empty program quiet and 0.66 to 0.79 ms loaded.
The checked-in fuzz corpus now includes inputs grown for the ledger reader (61) and more for the stamp reader (108), beside the config parser's 300. The replay tool's input table grew, because fuzzing had filled the old one on the development machine. (user: dmestas, size: 4987) [annotate] [blame] [check-ins using]
#include "ledger.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> static char out_buf[LEDGER_FILE_MAX + 1]; __attribute__((format(printf, 3, 4))) static bool fail(LedgerError *e, int line, const char *fmt, ...) { va_list ap; e->line = line; va_start(ap, fmt); vsnprintf(e->msg, sizeof e->msg, fmt, ap); va_end(ap); return false; } // True if S is one absolute path: it starts with '/' and has no space or tab. static bool path_ok(const char *s) { return s[0] == '/' && strpbrk(s, " \t") == nullptr; } static bool parse_line(Ledger *l, LedgerError *e, int line, char *s) { char *kind = s; char *dest = strchr(s, ' '); if (dest == nullptr) return fail(e, line, "expected 'link DEST SOURCE', 'dir DEST' or 'file DEST'"); *dest++ = '\0'; char *src = strchr(dest, ' '); if (src != nullptr) *src++ = '\0'; bool is_link = strcmp(kind, "link") == 0; bool is_dir = strcmp(kind, "dir") == 0; if (!is_link && !is_dir && strcmp(kind, "file") != 0) return fail(e, line, "unknown record '%.40s' (link, dir or file)", kind); if ((src != nullptr) != is_link) return fail(e, line, is_link ? "link needs DEST and SOURCE" : "%s takes only DEST", kind); if (!path_ok(dest) || (is_link && !path_ok(src))) return fail(e, line, "paths must be absolute and have no spaces"); EntryKind ek = is_link ? ENTRY_LINK : (is_dir ? ENTRY_DIR : ENTRY_FILE); for (uint32_t i = 0; i < l->n; i++) if (l->e[i].kind == ek && strcmp(l->e[i].dest, dest) == 0) return fail(e, line, "duplicate %s %.80s", kind, dest); if (l->n == LEDGER_ENTRY_MAX) return fail(e, line, "too many entries (limit %u)", LEDGER_ENTRY_MAX); l->e[l->n++] = (LedgerEntry){ek, dest, src}; return true; } static bool parse_in_place(Ledger *l, size_t len, LedgerError *e) { l->n = 0; l->buf[len] = '\0'; char *p = l->buf; char *end = l->buf + len; int line = 0; while (p < end) { char *nl = memchr(p, '\n', (size_t)(end - p)); line++; if (nl == nullptr) return fail(e, line, "line has no newline"); size_t n = (size_t)(nl - p); if (n > LEDGER_LINE_MAX) return fail(e, line, "line is %zu bytes, limit is %zu", n, LEDGER_LINE_MAX); if (memchr(p, '\0', n) != nullptr) return fail(e, line, "NUL byte in line"); *nl = '\0'; char *s = p; p = nl + 1; if (!parse_line(l, e, line, s)) return false; } return true; } bool ledger_parse(Ledger *l, const char *data, size_t len, LedgerError *e) { l->n = 0; if (len > LEDGER_FILE_MAX) return fail(e, 0, "file is %zu bytes, limit is %zu", len, LEDGER_FILE_MAX); memcpy(l->buf, data, len); bool ok = parse_in_place(l, len, e); l->n = ok ? l->n : 0; return ok; } bool ledger_load(Ledger *l, const char *path, LedgerError *e) { l->n = 0; int fd = sys_open(path, O_RDONLY, 0); if (fd < 0 && errno == ENOENT) return true; if (fd < 0) return fail(e, 0, "cannot open: %s", strerror(errno)); size_t len = 0; ssize_t n = 1; while (len <= LEDGER_FILE_MAX && n > 0) { n = sys_read(fd, l->buf + len, LEDGER_FILE_MAX + 1 - len); if (n < 0 && errno == EINTR) { n = 1; continue; } len += n > 0 ? (size_t)n : 0; } int err = errno; (void)sys_close(fd); if (n < 0) return fail(e, 0, "cannot read: %s", strerror(err)); if (len > LEDGER_FILE_MAX) return fail(e, 0, "file is over the limit of %zu bytes", LEDGER_FILE_MAX); bool ok = parse_in_place(l, len, e); l->n = ok ? l->n : 0; return ok; } static int cmp_entry(const void *pa, const void *pb) { const LedgerEntry *a = pa; const LedgerEntry *b = pb; if (a->kind != b->kind) return a->kind < b->kind ? -1 : 1; return strcmp(a->dest, b->dest); } int ledger_write(const Ledger *l, const char *path) { static LedgerEntry sorted[LEDGER_ENTRY_MAX]; ensure(l->n <= LEDGER_ENTRY_MAX); memcpy(sorted, l->e, l->n * sizeof sorted[0]); qsort(sorted, l->n, sizeof sorted[0], cmp_entry); size_t len = 0; for (uint32_t i = 0; i < l->n; i++) { const LedgerEntry *en = &sorted[i]; size_t room = sizeof out_buf - len; int n = en->kind == ENTRY_LINK ? snprintf(out_buf + len, room, "link %s %s\n", en->dest, en->src) : snprintf(out_buf + len, room, "%s %s\n", en->kind == ENTRY_DIR ? "dir" : "file", en->dest); ensure(n >= 0); if ((size_t)n >= room) return EFBIG; len += (size_t)n; } return sys_write_atomic(path, out_buf, len); } const LedgerEntry *ledger_find_link(const Ledger *l, const char *dest) { for (uint32_t i = 0; i < l->n; i++) if (l->e[i].kind == ENTRY_LINK && strcmp(l->e[i].dest, dest) == 0) return &l->e[i]; return nullptr; }