Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(pipeline): delta-repair groundwork — CoW staging + shared finalizer

Martin Vogel5722231eadfc
diff --git a/src/foundation/compat_fs.c b/src/foundation/compat_fs.c
index 89a102a70..746d46781 100644
--- a/src/foundation/compat_fs.c
+++ b/src/foundation/compat_fs.c
@@ -1114,3 +1114,77 @@ int cbm_remove_db_sidecars(const char *db_path) {
}
return result;
}
+
+/* ── Clone-or-copy ───────────────────────────────────────────────── */
+
+#if defined(__APPLE__)
+#include <sys/clonefile.h>
+#elif defined(__linux__)
+#include <linux/fs.h>
+#include <sys/ioctl.h>
+#endif
+#include <fcntl.h>
+
+static int stream_copy_file(const char *src, const char *dst) {
+ FILE *in = cbm_fopen(src, "rb");
+ if (!in) {
+ return CBM_NOT_FOUND;
+ }
+ FILE *out = cbm_fopen(dst, "wb");
+ if (!out) {
+ (void)fclose(in);
+ return CBM_NOT_FOUND;
+ }
+ char buf[CBM_SZ_64K];
+ size_t n;
+ int rc = 0;
+ while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
+ if (fwrite(buf, 1, n, out) != n) {
+ rc = CBM_NOT_FOUND;
+ break;
+ }
+ }
+ if (ferror(in)) {
+ rc = CBM_NOT_FOUND;
+ }
+ (void)fclose(in);
+ if (fclose(out) != 0) {
+ rc = CBM_NOT_FOUND;
+ }
+ if (rc != 0) {
+ (void)cbm_unlink(dst);
+ }
+ return rc;
+}
+
+int cbm_clone_or_copy_file(const char *src, const char *dst) {
+ if (!src || !dst) {
+ return CBM_NOT_FOUND;
+ }
+#if defined(__APPLE__)
+ /* clonefile refuses to overwrite; the staging name is freshly minted by
+ * the caller, but clear any leftover defensively so the fast path is
+ * never abandoned for a stale artifact. */
+ (void)cbm_unlink(dst);
+ if (clonefile(src, dst, 0) == 0) {
+ return 0;
+ }
+#elif defined(__linux__)
+ int in_fd = open(src, O_RDONLY | O_CLOEXEC);
+ if (in_fd >= 0) {
+ int out_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
+ if (out_fd >= 0) {
+ int cloned = ioctl(out_fd, FICLONE, in_fd);
+ int close_rc = close(out_fd);
+ (void)close(in_fd);
+ if (cloned == 0 && close_rc == 0) {
+ return 0;
+ }
+ (void)cbm_unlink(dst);
+ } else {
+ (void)close(in_fd);
+ }
+ }
+#endif
+ return stream_copy_file(src, dst);
+}
diff --git a/src/foundation/compat_fs.h b/src/foundation/compat_fs.h
index 964e45e60..240ebceae 100644
--- a/src/foundation/compat_fs.h
+++ b/src/foundation/compat_fs.h
@@ -69,6 +69,14 @@ int cbm_remove_db_sidecars(const char *db_path);
/* rename() that replaces an existing destination on every platform
* (Windows rename fails with EEXIST; this uses write-through MoveFileExW). */
int cbm_rename_replace(const char *src, const char *dst);
+
+/* Copy src to dst (dst truncated/created), preferring an instant
+ * copy-on-write clone where the filesystem supports one: clonefile(2) on
+ * APFS, FICLONE on Linux reflink filesystems. Falls back to a streamed
+ * copy. Returns 0 on success. The delta-repair path stages a multi-GB
+ * database this way, so the clone fast path is the difference between
+ * milliseconds and seconds there. */
+int cbm_clone_or_copy_file(const char *src, const char *dst);
/* Move a regular file only when dst does not exist. Never overwrites dst.
* Used for collision-safe evidence quarantine beside a database. */
int cbm_rename_noreplace(const char *src, const char *dst);
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index ecaa0af06..82766dcd6 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1721,46 +1721,58 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
ok = false;
}
cbm_store_close(store);
- if (ok && cbm_remove_db_sidecars(stage_path) != 0) {
- ok = false;
- }
if (!ok) {
discard_generation_stage(stage_path);
free(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
- if (generation->cancelled && atomic_load(generation->cancelled)) {
+ int fin_rc = cbm_pipeline_finalize_staged_generation(stage_path, generation->final_db_path,
+ generation->cancelled);
+ free(stage_path);
+ return fin_rc;
+}
+
+void cbm_pipeline_discard_stage(const char *stage_path) {
+ discard_generation_stage(stage_path);
+}
+
+/* Shared final leg of every publication, dump-built or delta-patched: the
+ * staging file is complete and sealed; remove its sidecars, quarantine the
+ * previous generation, and atomically rename. Owns discarding the stage on
+ * every failure path. The store handle must already be CLOSED — sidecar
+ * removal and rename act on the bare file. */
+int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_db_path,
+ atomic_int *cancelled) {
+ if (cbm_remove_db_sidecars(stage_path) != 0) {
+ discard_generation_stage(stage_path);
+ return CBM_PIPELINE_PERSIST_FAILED;
+ }
+ if (cancelled && atomic_load(cancelled)) {
discard_generation_stage(stage_path);
- free(stage_path);
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
cbm_replacement_prepare_t prepared = {0};
/* The destination here is the caller's staging file, not the live database:
* the publishing wrapper owns quarantining that. */
- if (prepare_existing_generation_for_replace(generation->final_db_path, &prepared, false) != 0) {
+ if (prepare_existing_generation_for_replace(final_db_path, &prepared, false) != 0) {
discard_generation_stage(stage_path);
- free(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
- if (cbm_pipeline_persist_test_take_cancel_after_destination_prepare() &&
- generation->cancelled) {
- atomic_store(generation->cancelled, true);
+ if (cbm_pipeline_persist_test_take_cancel_after_destination_prepare() && cancelled) {
+ atomic_store(cancelled, true);
}
#endif
- if (generation->cancelled && atomic_load(generation->cancelled)) {
- int rollback_rc = rollback_quarantined_generation(generation->final_db_path, &prepared);
+ if (cancelled && atomic_load(cancelled)) {
+ int rollback_rc = rollback_quarantined_generation(final_db_path, &prepared);
discard_generation_stage(stage_path);
- free(stage_path);
return rollback_rc == 0 ? CBM_PIPELINE_ABORT_PRESERVE_DB : CBM_PIPELINE_PERSIST_FAILED;
}
- if (cbm_rename_replace(stage_path, generation->final_db_path) != 0) {
- (void)rollback_quarantined_generation(generation->final_db_path, &prepared);
+ if (cbm_rename_replace(stage_path, final_db_path) != 0) {
+ (void)rollback_quarantined_generation(final_db_path, &prepared);
discard_generation_stage(stage_path);
- free(stage_path);
return CBM_PIPELINE_PERSIST_FAILED;
}
- free(stage_path);
return 0;
}
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index d02e6433b..2c6c958bf 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -708,6 +708,14 @@ typedef struct {
* replace final_db_path. The old generation is untouched on every failure or
* cancellation observed before the rename commit point. */
int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation);
+/* Final leg shared by the dump-built and delta-patched publication paths:
+ * sidecar removal, previous-generation quarantine, atomic rename. The stage
+ * must be complete and sealed with its store handle closed. Discards the
+ * stage on every failure. Does NOT free stage_path. */
+int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_db_path,
+ atomic_int *cancelled);
+/* discard helper shared with the delta executor (unlink stage + sidecars). */
+void cbm_pipeline_discard_stage(const char *stage_path);
/* The SQLite generation is authoritative. An explicitly requested artifact is
* part of the caller-visible operation and its export error is returned;
* automatic refresh of an already-existing artifact remains best-effort. */