Artifact 543c43251d5232c04efcf956fec1989f112bf5bc3fcc2d2471be0df824a60424:
- File
src/need.c
— part of check-in
[3598f9d78d]
at
2026-11-14 13:15:53
on branch trunk
— Phase 8a: needs are checked, listed, and shown until they are done
A need is a step a harness must do for itself, such as registering a slot in its own settings. suit never edits those settings; it says what is needed and can tell when it has been done.
The done test has two forms and nothing else: PATH exists, and PATH contains "TEXT". suit needs lists every need as done, not done or cannot tell, and changes nothing. suit need HARNESS NAME prints the prompt's exact bytes so it can be handed to the harness.
After each apply the attention file holds one item for each need not done and none for those that are. Other kinds of item are left alone and keep their order, and nothing is written when nothing changed. If that update fails, apply writes no stamp, so the next run tries again. A bad need stops apply before it places anything. suit off removes the items.
suit-run is untouched and the speed budget still passes.
An eighth fuzz target covers the done-test parser.
1355 unit checks, 479 shell cases, 669 store cases, 745 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (34 minutes). 100% coverage in all 24 source files. No suite has fewer cases than at the last check-in. (user: dmestas, size: 4160) [annotate] [blame] [check-ins using]
#include "need.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> static char file_buf[DONE_FILE_MAX + 1]; static const char CONTAINS[] = "contains \""; bool done_parse(DoneTest *t, const char *home, const char *s, char *why, size_t cap) { size_t pl = strcspn(s, " "); if (pl == 0 || s[pl] == '\0') { snprintf(why, cap, "expected PATH exists or PATH contains \"TEXT\""); return false; } const char *rest = s + pl + 1; size_t rl = strlen(rest); if (strcmp(rest, "exists") == 0) { t->kind = DONE_EXISTS; t->text = nullptr; t->text_len = 0; } else if (strncmp(rest, CONTAINS, sizeof CONTAINS - 1) == 0) { if (rl < sizeof CONTAINS || rest[rl - 1] != '"') { snprintf(why, cap, "TEXT must be in quotes, with the closing quote last on the line"); return false; } if (rl == sizeof CONTAINS) { snprintf(why, cap, "TEXT is empty"); return false; } t->kind = DONE_CONTAINS; t->text = rest + sizeof CONTAINS - 1; t->text_len = rl - sizeof CONTAINS; } else { snprintf(why, cap, "expected PATH exists or PATH contains \"TEXT\""); return false; } bool tilde = s[0] == '~' && (pl == 1 || s[1] == '/'); size_t hl = tilde ? strlen(home) : 0; const char *tail = tilde ? s + 1 : s; size_t tl = tilde ? pl - 1 : pl; if (hl + tl >= sizeof t->path) { snprintf(why, cap, "PATH is longer than %zu bytes", sizeof t->path - 1); return false; } memcpy(t->path, home, hl); memcpy(t->path + hl, tail, tl); t->path[hl + tl] = '\0'; if (t->path[0] != '/') { snprintf(why, cap, "PATH must be absolute, or start with ~/ for the home folder"); return false; } return true; } static bool has_text(const char *buf, size_t len, const char *text, size_t n) { for (size_t at = 0; at + n <= len; at++) { const char *hit = memchr(buf + at, text[0], len - at - n + 1); if (hit == nullptr) return false; at = (size_t)(hit - buf); if (memcmp(hit, text, n) == 0) return true; } return false; } static NeedState unknown(char *why, size_t cap, const char *what, int err) { if (err != 0) snprintf(why, cap, "%s: %s", what, strerror(err)); else snprintf(why, cap, "%s", what); return NEED_UNKNOWN; } // A stat or lstat that failed: a file that is not there is not done. static NeedState look_failed(char *why, size_t cap) { bool missing = errno == ENOENT || errno == ENOTDIR; return missing ? NEED_NOT_DONE : unknown(why, cap, "cannot look at the file", errno); } static NeedState eval_contains(const DoneTest *t, char *why, size_t cap) { struct stat st; if (sys_stat(t->path, &st) != 0) { return look_failed(why, cap); } if (!S_ISREG(st.st_mode)) return unknown(why, cap, "not a regular file", 0); int fd = sys_open(t->path, O_RDONLY, 0); if (fd < 0) return unknown(why, cap, "cannot open the file", errno); size_t len = 0; ssize_t n = 1; while (n > 0 && len <= DONE_FILE_MAX) { n = sys_read(fd, file_buf + len, DONE_FILE_MAX + 1 - len); if (n < 0 && errno == EINTR) n = 1; else if (n > 0) len += (size_t)n; } int err = errno; (void)sys_close(fd); if (n < 0) return unknown(why, cap, "cannot read the file", err); if (len > DONE_FILE_MAX) return unknown(why, cap, "file is over 4 MiB", 0); return has_text(file_buf, len, t->text, t->text_len) ? NEED_DONE : NEED_NOT_DONE; } NeedState done_eval(const DoneTest *t, char *why, size_t cap) { if (t->kind == DONE_CONTAINS) return eval_contains(t, why, cap); struct stat st; if (sys_lstat(t->path, &st) == 0) return NEED_DONE; return look_failed(why, cap); } bool need_prompt_ok(const char *p) { if (p[0] == '\0' || p[0] == '/') return false; for (const char *c = p; *c != '\0';) { size_t n = strcspn(c, "/"); if (n == 2 && c[0] == '.' && c[1] == '.') return false; c += n + (c[n] == '/' ? 1 : 0); } return true; }