Artifact 2cea17e9f1d80e9021b34700f547ccdd47a8e17962d149b438e6364fb3515c76:
- File
src/store_fs.c
— 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: 2087) [annotate] [blame] [check-ins using]
#include "store_fs.h" #include "platform.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> constexpr size_t TMP_PATH_MAX = 4096 + 64; static bool write_temp(const char *tmp, mode_t mode, const void *src, size_t n) { int fd = sys_open(tmp, O_WRONLY | O_CREAT | O_EXCL, mode); if (fd < 0) return false; bool written = sys_write_all(fd, src, n) && sys_fsync(fd) == 0; bool closed = sys_close(fd) == 0; return written && closed; } // Write N bytes to ZNAME by way of a temporary file beside it. An executable // file stays executable. static int fs_write(void *state, char const *zName, void const *src, fsl_size_t n) { static unsigned counter; const char *slash = strrchr(zName, '/'); ensure(slash != nullptr); char tmp[TMP_PATH_MAX]; [[maybe_unused]] int len = snprintf(tmp, sizeof tmp, "%.*s/" STORE_TMP_PREFIX "%d.%u", (int)(slash - zName), zName, (int)getpid(), counter++); ensure(len > 0 && (size_t)len < sizeof tmp); struct stat st; bool executable = sys_stat(zName, &st) == 0 && (st.st_mode & 0100) != 0; if (!write_temp(tmp, executable ? 0755 : 0644, src, n) || sys_rename(tmp, zName) != 0) { int err = errno; (void)sys_unlink(tmp); return fsl_cx_err_set((fsl_cx *)state, FSL_RC_IO, "cannot write %s: %s", zName, strerror(err)); } return 0; } typedef struct { fsl_ckout_fs_dir_f cb; void *state; } DirFilter; static int dir_filter(void *p, char const *entry) { const DirFilter *d = p; bool ours = strncmp(entry, STORE_TMP_PREFIX, strlen(STORE_TMP_PREFIX)) == 0; return ours ? 0 : d->cb(d->state, entry); } static int fs_dirlist(void *state, char const *zDir, fsl_ckout_fs_dir_f cb, void *cbState) { DirFilter d = {cb, cbState}; return fsl_ckout_fs_os.xDirList(state, zDir, dir_filter, &d); } int store_fs_install(fsl_cx *f) { fsl_ckout_fs fs = fsl_ckout_fs_os; fs.xWrite = fs_write; fs.xDirList = fs_dirlist; fs.state = f; return fsl_cx_ckout_fs(f, &fs, nullptr); }