Artifact 6e4acae274efebf179c500d1d526414e14fd3035032ab7096724022812cd2002:
- File
src/ledger.c
— part of check-in
[45a7e1e117]
at
2026-11-13 07:44:25
on branch trunk
— Phase 4b-2: your own assets placed inside each project that matches
An outfit worn at personal scope for a project's key is now linked into every project with that key, and into each of its linked worktrees: skills, agents, commands, instructions and files, under the folders the harness names for a project. The machine limit and joined instructions work there as they do at the User scope.
The links are hidden from version control per project, never globally. For a git project suit keeps one marked block in that project's local exclude file, leaves every other byte of the file as it was, and removes only the block on suit off. A Fossil or zit project has no such file, so its links are placed and suit status says they are visible.
Suit never runs git and never stages or commits anything. A file already in the project at a destination is a clash, left as it is and reported, so a committed file always wins.
1006 unit checks, 227 shell cases, 146 store cases, 612 placement cases and 310 project cases, under clang and under gcc 16. 100% coverage in all 19 source files. The no-change run costs about 0.43 ms over an empty program. (user: dmestas, size: 5585) [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> const char LEDGER_CREATED[] = "created"; 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', 'file DEST' or 'block 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; bool is_block = strcmp(kind, "block") == 0; if (!is_link && !is_dir && !is_block && strcmp(kind, "file") != 0) return fail(e, line, "unknown record '%.40s' (link, dir, file or block)", kind); if (is_block && src != nullptr && strcmp(src, LEDGER_CREATED) != 0) return fail(e, line, "block takes DEST, and 'created' after it if suit made the file"); if (is_link && src == nullptr) return fail(e, line, "link needs DEST and SOURCE"); if (!is_link && !is_block && src != nullptr) return fail(e, line, "%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 : (is_block ? ENTRY_BLOCK : 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; if (en->kind == ENTRY_LINK) n = snprintf(out_buf + len, room, "link %s %s\n", en->dest, en->src); else if (en->kind == ENTRY_BLOCK) n = snprintf(out_buf + len, room, "block %s%s%s\n", en->dest, en->src != nullptr ? " " : "", en->src != nullptr ? en->src : ""); else n = 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; }