Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124/* A test-only VFS: preserve SQLite's file layout and replace only xSync. * While <gate>.<main|wal>.block exists, a sync of that kind of file writes * <gate>.<kind>.entered and waits, so a test can hold a checkpoint or a * commit inside fsync. test/sync_gate.gleam builds and loads it. */#define _POSIX_C_SOURCE 200809L#include <dlfcn.h>#include <erl_nif.h>#include <fcntl.h>#include <limits.h>#include <sqlite3.h>#include <stdio.h>#include <string.h>#include <time.h>#include <unistd.h>
typedef struct { sqlite3_io_methods methods; int (*sync)(sqlite3_file *, int); int flags;} SyncFile;
static sqlite3_vfs *parent;static sqlite3_vfs wrapper;static size_t file_offset;static char gate[PATH_MAX];
static SyncFile *state(sqlite3_file *file) { return (SyncFile *)((char *)file + file_offset);}
static int gated_sync(sqlite3_file *file, int flags) { SyncFile *saved = state(file); const char *kind = (saved->flags & SQLITE_OPEN_MAIN_DB) ? "main" : (saved->flags & SQLITE_OPEN_WAL) ? "wal" : NULL; char block[PATH_MAX + 32], entered[PATH_MAX + 32]; if (kind) { snprintf(block, sizeof(block), "%s.%s.block", gate, kind); snprintf(entered, sizeof(entered), "%s.%s.entered", gate, kind); if (access(block, F_OK) == 0) { int marker = open(entered, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (marker < 0) return SQLITE_IOERR_FSYNC; close(marker); struct timespec start, now, pause = {0, 10000000}; clock_gettime(CLOCK_MONOTONIC, &start); while (access(block, F_OK) == 0) { clock_gettime(CLOCK_MONOTONIC, &now); if (now.tv_sec - start.tv_sec >= 60) { unlink(entered); return SQLITE_IOERR_FSYNC; } nanosleep(&pause, NULL); } unlink(entered); } } return saved->sync(file, flags);}
static int gated_open(sqlite3_vfs *vfs, const char *name, sqlite3_file *file, int flags, int *out_flags) { (void)vfs; int result = parent->xOpen(parent, name, file, flags, out_flags); if (result == SQLITE_OK && file->pMethods) { SyncFile *saved = state(file); saved->methods = *file->pMethods; saved->sync = file->pMethods->xSync; saved->flags = flags; saved->methods.xSync = gated_sync; file->pMethods = &saved->methods; } return result;}
static int path_arg(ErlNifEnv *env, ERL_NIF_TERM arg, char *out, size_t size) { ErlNifBinary value; if (!enif_inspect_binary(env, arg, &value) || value.size >= size || memchr(value.data, 0, value.size)) return 0; memcpy(out, value.data, value.size); out[value.size] = 0; return 1;}
static int load(ErlNifEnv *env, void **priv, ERL_NIF_TERM info) { (void)priv; int arity; const ERL_NIF_TERM *args; char library[PATH_MAX]; /* Leave room for the marker suffixes in gated_sync. */ if (!enif_get_tuple(env, info, &arity, &args) || arity != 2 || !path_arg(env, args[0], library, sizeof(library)) || !path_arg(env, args[1], gate, sizeof(gate) - 32)) return -1; void *sqlite = dlopen(library, RTLD_NOW | RTLD_LOCAL); if (!sqlite) { fprintf(stderr, "storage sync fixture dlopen: %s\n", dlerror()); return -1; } sqlite3_vfs *(*find_vfs)(const char *) = dlsym(sqlite, "sqlite3_vfs_find"); int (*register_vfs)(sqlite3_vfs *, int) = dlsym(sqlite, "sqlite3_vfs_register"); if (!find_vfs || !register_vfs || !(parent = find_vfs(NULL))) return -1; file_offset = ((size_t)parent->szOsFile + sizeof(void *) - 1) & ~(sizeof(void *) - 1); wrapper = *parent; wrapper.zName = "albedo-storage-sync"; wrapper.szOsFile = (int)(file_offset + sizeof(SyncFile)); wrapper.xOpen = gated_open; return register_vfs(&wrapper, 1) == SQLITE_OK ? 0 : -1;}
static ERL_NIF_TERM installed(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { (void)argc; (void)argv; return enif_make_atom(env, "true");}
static ErlNifFunc functions[] = {{"installed", 0, installed, 0}};ERL_NIF_INIT(sync_gate, functions, load, NULL, NULL, NULL)