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.

fix(store): replace-on-rename for dump installs on Windows

Martin Vogel43d84ee6f217
diff --git a/src/foundation/compat_fs.c b/src/foundation/compat_fs.c
index 135fac3b6..838c1cf96 100644
--- a/src/foundation/compat_fs.c
+++ b/src/foundation/compat_fs.c
@@ -714,6 +714,28 @@ int cbm_exec_no_shell(const char *const *argv) {
#endif /* _WIN32 */
+/* rename() with overwrite semantics on every platform: POSIX rename already
+ * replaces atomically; Windows rename fails with EEXIST when the target
+ * exists, so use MoveFileExW(MOVEFILE_REPLACE_EXISTING) there (wide paths —
+ * raw MoveFileExA would re-mangle non-ASCII cache paths). */
+int cbm_rename_replace(const char *src, const char *dst) {
+#ifdef _WIN32
+ wchar_t *wsrc = cbm_utf8_to_wide(src);
+ wchar_t *wdst = cbm_utf8_to_wide(dst);
+ int ret = CBM_NOT_FOUND;
+ if (wsrc && wdst) {
+ ret = MoveFileExW(wsrc, wdst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)
+ ? 0
+ : CBM_NOT_FOUND;
+ }
+ free(wsrc);
+ free(wdst);
+ return ret;
+#else
+ return rename(src, dst);
+#endif
+}
+
/* Remove a SQLite database's -wal/-shm sidecars (both platforms). Any code
* path that installs a FRESH database file at a path where a previous
* generation lived must call this first: SQLite decides whether to replay a
diff --git a/src/foundation/compat_fs.h b/src/foundation/compat_fs.h
index 7fc875cd0..05cae3dda 100644
--- a/src/foundation/compat_fs.h
+++ b/src/foundation/compat_fs.h
@@ -50,6 +50,9 @@ int cbm_unlink(const char *path);
* DB file where a previous generation lived — a leftover WAL is otherwise
* replayed on top of the new file at the next open (#897). */
void cbm_remove_db_sidecars(const char *db_path);
+/* rename() that replaces an existing destination on every platform
+ * (Windows rename fails with EEXIST; this uses MoveFileExW there). */
+int cbm_rename_replace(const char *src, const char *dst);
/* Delete an empty directory. Returns 0 on success. */
int cbm_rmdir(const char *path);
diff --git a/src/store/store.c b/src/store/store.c
index b4ec88406..4d506f58c 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -1101,7 +1101,7 @@ int cbm_store_dump_to_file(cbm_store_t *s, const char *dest_path) {
* fresh file at the next open — SQLite validates the WAL against its
* own header/checksums, not against the main file (#897). */
cbm_remove_db_sidecars(dest_path);
- if (rename(tmp_path, dest_path) != 0) {
+ if (cbm_rename_replace(tmp_path, dest_path) != 0) {
store_set_error(s, "dump: rename failed");
(void)unlink(tmp_path);
return CBM_STORE_ERR;