Artifact ad8fa50da404166795a559ccf732a5f7cfc7e8cd5a0c72784f804c7d238cf509:
- File
src/ledger.c
— part of check-in
[c3b2afcdc0]
at
2026-11-13 02:12:51
on branch trunk
— Phase 3: the User scope, placed by links that suit owns and nothing else
suit reads suit.conf from the store, works out which links should exist for the User scope, and makes each harness's folders match. suit plan, apply, status and off. suit run now applies when something changed.
The ledger is a text file listing every link suit made. A link is made under a temporary name and renamed into place. A real file, a real folder or someone else's link at a destination is left byte-identical and reported as a clash. suit off leaves a home folder exactly as it was before the first apply.
Proven with a real harness: Claude Code, run against a scratch home through a local proxy on a DeepSeek model, returned the marker word from the instruction file suit placed, listed the skill suit placed, returned a new marker after the file was edited in the store with no suit command run, and no longer saw it after suit off. make live runs this on purpose; it is not part of the automatic gates.
385 unit checks, 227 shell cases, 146 store cases, 157 placement cases. 100% of functions, lines, regions and branches in all 14 source files. The no-change run costs 0.45 to 0.72 ms over an empty program with the stamp now watching 128 paths.
Not yet done in this phase: live proof with Codex and pi, and the document of each harness's paths. (user: dmestas, size: 4868) [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' or 'dir DEST'"); *dest++ = '\0'; char *src = strchr(dest, ' '); if (src != nullptr) *src++ = '\0'; bool is_link = strcmp(kind, "link") == 0; if (!is_link && strcmp(kind, "dir") != 0) return fail(e, line, "unknown record '%.40s' (link or dir)", kind); if ((src != nullptr) != is_link) return fail(e, line, is_link ? "link needs DEST and SOURCE" : "dir takes only DEST"); if (!path_ok(dest) || (is_link && !path_ok(src))) return fail(e, line, "paths must be absolute and have no spaces"); for (uint32_t i = 0; i < l->n; i++) if (l->e[i].kind == (is_link ? ENTRY_LINK : ENTRY_DIR) && 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){is_link ? ENTRY_LINK : ENTRY_DIR, 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, "dir %s\n", 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; }