Skip to content

Commit 43d84ee

Browse files
committed
fix(store): replace-on-rename for dump installs on Windows
The new stale-sidecar guard exposed a pre-existing Windows gap in the same code path: rename() fails with EEXIST when the destination exists, so cbm_store_dump_to_file could never install over a previous DB generation on Windows. New cbm_rename_replace uses MoveFileExW(MOVEFILE_REPLACE_EXISTING) with wide paths (raw MoveFileExA would re-mangle non-ASCII cache paths); POSIX keeps plain atomic rename. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 394198e commit 43d84ee

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/foundation/compat_fs.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,28 @@ int cbm_exec_no_shell(const char *const *argv) {
714714

715715
#endif /* _WIN32 */
716716

717+
/* rename() with overwrite semantics on every platform: POSIX rename already
718+
* replaces atomically; Windows rename fails with EEXIST when the target
719+
* exists, so use MoveFileExW(MOVEFILE_REPLACE_EXISTING) there (wide paths —
720+
* raw MoveFileExA would re-mangle non-ASCII cache paths). */
721+
int cbm_rename_replace(const char *src, const char *dst) {
722+
#ifdef _WIN32
723+
wchar_t *wsrc = cbm_utf8_to_wide(src);
724+
wchar_t *wdst = cbm_utf8_to_wide(dst);
725+
int ret = CBM_NOT_FOUND;
726+
if (wsrc && wdst) {
727+
ret = MoveFileExW(wsrc, wdst, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED)
728+
? 0
729+
: CBM_NOT_FOUND;
730+
}
731+
free(wsrc);
732+
free(wdst);
733+
return ret;
734+
#else
735+
return rename(src, dst);
736+
#endif
737+
}
738+
717739
/* Remove a SQLite database's -wal/-shm sidecars (both platforms). Any code
718740
* path that installs a FRESH database file at a path where a previous
719741
* generation lived must call this first: SQLite decides whether to replay a

‎src/foundation/compat_fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ int cbm_unlink(const char *path);
5050
* DB file where a previous generation lived — a leftover WAL is otherwise
5151
* replayed on top of the new file at the next open (#897). */
5252
void cbm_remove_db_sidecars(const char *db_path);
53+
/* rename() that replaces an existing destination on every platform
54+
* (Windows rename fails with EEXIST; this uses MoveFileExW there). */
55+
int cbm_rename_replace(const char *src, const char *dst);
5356

5457
/* Delete an empty directory. Returns 0 on success. */
5558
int cbm_rmdir(const char *path);

‎src/store/store.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ int cbm_store_dump_to_file(cbm_store_t *s, const char *dest_path) {
11011101
* fresh file at the next open — SQLite validates the WAL against its
11021102
* own header/checksums, not against the main file (#897). */
11031103
cbm_remove_db_sidecars(dest_path);
1104-
if (rename(tmp_path, dest_path) != 0) {
1104+
if (cbm_rename_replace(tmp_path, dest_path) != 0) {
11051105
store_set_error(s, "dump: rename failed");
11061106
(void)unlink(tmp_path);
11071107
return CBM_STORE_ERR;

0 commit comments

Comments
 (0)