Artifact 03f7813c993f8aef9c9ecd87924619ec245a61624b265c32b6b8a9068f216380:
- File
src/attention.c
— part of check-in
[6516a0e4da]
at
2026-11-13 12:54:25
on branch trunk
— Phase 7a: a lint guards every check-in, and edits made in place are checked in
suit lint and suit commit refuse a change that holds a token-shaped value, tool-attribution text, a symlink, or a skill folder with no header. A suit.lint file in the wardrobe can exempt a path or forbid an extra phrase. The lint never changes a file.
suit tend is the hourly entry point. For now it checks in edits made in the store, after the lint. A change that deletes more than five files and more than a fifth of the wardrobe is held until it is confirmed. Anything that needs a person is written to an attention file, which suit status shows under 'needs you'.
A symlink refusal now comes from the lint and exits 3, where it exited 16 before.
Also fixed, found by the forge's runner: a file suit rewrote came back with default permissions, keeping only its executable bit. On the runner a project's local exclude file went from rw-rw---- to rw-r----- across apply and off. A rewritten file now keeps exactly the permissions it had. Tests rewrite files of four different modes under two different defaults, so the development machine catches it too. Three tests that restored a folder to a fixed mode now put back only the bits they took away.
1204 unit checks, 227 shell cases, 323 store cases, 612 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask. 100% coverage in all 21 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 4465) [annotate] [blame] [check-ins using]
#include "attention.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> static char cur[ATTENTION_FILE_MAX + 1]; static char out[ATTENTION_FILE_MAX + 1]; // Read the file into cur. A file that is not there is empty. static int load(const char *path, size_t *len) { *len = 0; int fd = sys_open(path, O_RDONLY, 0); if (fd < 0 && errno == ENOENT) return 0; if (fd < 0) { say(STDERR_FILENO, "suit: cannot open %s: %s\n", path, strerror(errno)); return 1; } ssize_t n = 1; while (*len <= ATTENTION_FILE_MAX && n > 0) { n = sys_read(fd, cur + *len, ATTENTION_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) { say(STDERR_FILENO, "suit: cannot read %s: %s\n", path, strerror(err)); return 1; } if (*len > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s is over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } return 0; } // Whether the header line LINE ("KIND NAME" or "KIND NAME: ...") is the item // of that kind (any if KIND is null) for NAME. static bool item_is(const char *line, size_t n, const char *kind, const char *name) { const char *sp = memchr(line, ' ', n); if (sp == nullptr) return false; size_t kl = (size_t)(sp - line); if (kind != nullptr && (strlen(kind) != kl || memcmp(line, kind, kl) != 0)) return false; const char *w = sp + 1; size_t room = n - kl - 1; size_t wl = 0; while (wl < room && w[wl] != ':') wl++; return wl == strlen(name) && memcmp(w, name, wl) == 0; } // Copy cur to out without the items of KIND and NAME. Returns the length of // out, or SIZE_MAX... cannot overflow: out is as big as cur. static size_t filter(size_t len, const char *kind, const char *name) { size_t o = 0; size_t p = 0; bool keep = true; while (p < len) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; if (cur[p] != ' ') keep = !item_is(cur + p, end - p, kind, name); if (keep) { memcpy(out + o, cur + p, end - p); o += end - p; out[o++] = '\n'; } p = end + 1; } return o; } // Write OUT, or remove the file when OUT is empty. CHANGED says whether OUT // differs from what was read. static int store_out(const char *path, size_t o, size_t len) { if (o == len && memcmp(out, cur, o) == 0) return 0; if (o == 0) { if (sys_unlink(path) != 0 && errno != ENOENT) { say(STDERR_FILENO, "suit: cannot remove %s: %s\n", path, strerror(errno)); return 1; } return 0; } int err = sys_write_atomic(path, out, o); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int attention_remove(const char *path, const char *kind, const char *name) { size_t len; if (load(path, &len) != 0) return 1; return store_out(path, filter(len, kind, name), len); } int attention_add(const char *path, const char *kind, const char *name, const char *header, const char *details) { size_t len; if (load(path, &len) != 0) return 1; size_t o = filter(len, kind, name); size_t more = strlen(header) + 1 + (details != nullptr ? strlen(details) * 3 : 0); if (o + more > ATTENTION_FILE_MAX) { say(STDERR_FILENO, "suit: %s would be over %zu bytes\n", path, ATTENTION_FILE_MAX); return 1; } o += (size_t)snprintf(out + o, sizeof out - o, "%s\n", header); for (const char *d = details; d != nullptr && *d != '\0';) { size_t n = strcspn(d, "\n"); o += (size_t)snprintf(out + o, sizeof out - o, " %.*s\n", (int)n, d); ensure(d[n] == '\n'); d += n + 1; } return store_out(path, o, len); } int attention_print(const char *path) { size_t len; if (load(path, &len) != 0) return 1; if (len == 0) return 0; say(STDOUT_FILENO, "needs you:\n"); for (size_t p = 0; p < len;) { const char *nl = memchr(cur + p, '\n', len - p); size_t end = nl != nullptr ? (size_t)(nl - cur) : len; say(STDOUT_FILENO, "%.*s\n", (int)(end - p), cur + p); p = end + 1; } return 0; }