Artifact 2559ae4185fae7931e5d07a7fc139dfbcdb2b09f5ec0ab938b177fa21ef249a7:
- File
src/platform.h
— part of check-in
[2f274ce15b]
at
2026-11-12 22:28:09
on branch trunk
— Phase 2: the store, a working copy that is never half written
The store is a working copy of a wardrobe, kept through libfossil's C API with suit's own file writer: every file is written beside itself and renamed, so a reader sees the old file or the new one.
suit store open, status and update, suit commit and suit back. An update refuses when there are local changes, runs as a dry run first, and changes nothing if any file would conflict.
Proven here: the store is byte-identical to fossil's own checkout of a 600-file wardrobe at each version; a reader hashing files during a slowed update saw only old or new contents; an update that fails at any call leaves the old bytes and no temporary file. 100% of functions, lines, regions and branches in all ten source files.
Vendored, unedited: libfossil trunk f2983dcd78, SQLite 3.55.0, zlib 1.3.1. The release binary links only the system library.
Speed: with everything linked, suit costs 0.82 ms over an empty program quiet and up to 1.38 ms under load, which is over budget. The timer's entry point is now suit-run, a 35 KB program built from the no-change path alone, at 0.40 ms quiet and about 0.5 ms loaded. It runs suit only when something changed. The budget test is on suit-run.
Known limits, recorded in docs/decisions.md: libfossil's checkout writes symlinks as regular files, so wardrobes may not hold them; suit back undoes an update and not a check-in; and a store left half updated by a double failure has no repair command yet.
Not yet run: Fil-C and the Linux build of this phase. (user: dmestas, size: 3335) [annotate] [blame] [check-ins using]
#ifndef SUIT_PLATFORM_H #define SUIT_PLATFORM_H // The only file that knows which operating system this is, and the only one // that calls the operating system. Every call that can fail goes through a // wrapper here, so a test build can make the Nth call fail (docs/quality.md). #include <stddef.h> #include <stdint.h> #include <sys/resource.h> #include <sys/stat.h> #include <sys/types.h> #define PLATFORM_TRUE_PATH "/usr/bin/true" // ensure(cond): an invariant that cannot fail. Reports file and line and // aborts. On in every build except the coverage build, where it compiles to // nothing so the impossible side is not counted as a branch. COND must have // no side effects. #ifdef SUIT_COVER #define ensure(cond) ((void)0) #else [[noreturn]] void ensure_fail(const char *cond, const char *file, int line); #define ensure(cond) ((cond) ? (void)0 : ensure_fail(#cond, __FILE__, __LINE__)) #endif // ---- calls that can fail -------------------------------------------------- // Each returns what the libc call returns and sets errno the same way. int sys_open(const char *path, int flags, mode_t mode); ssize_t sys_read(int fd, void *buf, size_t n); ssize_t sys_write(int fd, const void *buf, size_t n); int sys_close(int fd); int sys_fsync(int fd); int sys_stat(const char *path, struct stat *st); int sys_fstatat(int dirfd, const char *name, struct stat *st); int sys_rename(const char *from, const char *to); int sys_mkdir(const char *path, mode_t mode); int sys_unlink(const char *path); const char *sys_getenv(const char *name); // Spawn PATH with the current environment. Returns 0 or an error number. int sys_spawn(pid_t *pid, const char *path, char *const argv[]); // Replace this process with PATH. Returns only on failure. int sys_execv(const char *path, char *const argv[]); pid_t sys_wait4(pid_t pid, int *status, struct rusage *ru); // CLOCK_MONOTONIC in nanoseconds. False on failure. bool sys_now_ns(uint64_t *ns); // Write all of BUF, retrying after interruption and short writes. bool sys_write_all(int fd, const char *buf, size_t len); #ifdef SUIT_TEST // Forget the parsed SUIT_FAULT and the call counts, so the next wrapper call // reads the variable afresh. For tests that change it in a child process. void fault_reset(void); #endif // Absolute path of the running program. False if it fails or does not fit. bool platform_self_path(char *buf, size_t cap); // ---- no failure to report ------------------------------------------------- int64_t platform_mtime_sec(const struct stat *st); int64_t platform_mtime_nsec(const struct stat *st); // Peak resident size from a wait4 result, in bytes. uint64_t platform_peak_resident(const struct rusage *ru); // Format and write one message to FD in one go. A failure to write is not // reported: there is nowhere left to report it. Messages over SAY_MAX bytes // are cut and end in a newline. constexpr size_t SAY_MAX = 8192; __attribute__((format(printf, 2, 3))) void say(int fd, const char *fmt, ...); // Buffered output to a descriptor. Write errors are kept until out_finish. typedef struct { int fd; int err; bool failed; size_t len; char buf[8192]; } Out; void out_init(Out *o, int fd); void out_str(Out *o, const char *s); // Flush. False if any write failed; errno is set to that error. bool out_finish(Out *o); #endif