Skip to content

Commit 44d5174

Browse files
committed
fix: address review feedback on artifact export error surfacing
- Declare cbm_artifact_export_last_error() in artifact.h and drop the ad-hoc forward declarations in pipeline.c and test_artifact.c so there is a single shared declaration. - Document that dump_and_persist_hashes now intentionally propagates a failed artifact export as a pipeline error (previously silently ignored), so the behavior change is recorded in-tree. - Use MoveFileExA(MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) for the Windows replace in write_file_atomic instead of unlink+rename; it replaces the destination atomically (credit @Ayush7Ranjan, from DeusData#492). Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent d4aae7e commit 44d5174

4 files changed

Lines changed: 17 additions & 7 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ enum {
3636
#include <sys/stat.h>
3737
#include <time.h>
3838
#include <unistd.h>
39+
#ifdef _WIN32
40+
#include <windows.h>
41+
#endif
3942

4043
/* ── Helpers ──────────────────────────────────────────────────────── */
4144

@@ -180,15 +183,21 @@ static int write_file_atomic(const char *path, const char *data, size_t len,
180183
}
181184

182185
#ifdef _WIN32
183-
/* C rename() does not reliably replace an existing destination on Windows. */
184-
cbm_unlink(path);
185-
#endif
186+
/* MoveFileEx replace approach suggested by @Ayush7Ranjan in #492. */
187+
if (!MoveFileExA(tmp, path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) {
188+
DWORD saved_error = GetLastError();
189+
cbm_unlink(tmp);
190+
file_error_set(out_err, "rename_temp", (int)saved_error);
191+
return CBM_NOT_FOUND;
192+
}
193+
#else
186194
if (rename(tmp, path) != 0) {
187195
int saved_errno = errno;
188196
cbm_unlink(tmp);
189197
file_error_set(out_err, "rename_temp", saved_errno);
190198
return CBM_NOT_FOUND;
191199
}
200+
#endif
192201
return 0;
193202
}
194203

‎src/pipeline/artifact.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ enum {
3232
int cbm_artifact_export(const char *db_path, const char *repo_path, const char *project_name,
3333
int quality);
3434

35+
/* Get details for the most recent export failure on this thread.
36+
* Returns NULL if no export error is recorded. */
37+
const char *cbm_artifact_export_last_error(void);
38+
3539
/* Import artifact from .codebase-memory/graph.db.zst to cache_db_path.
3640
* Decompresses, runs integrity check, recreates indexes.
3741
* Returns 0 on success, -1 on error. */

‎src/pipeline/pipeline.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ static inline void *intptr_to_ptr(intptr_t v) {
4646
return p;
4747
}
4848

49-
const char *cbm_artifact_export_last_error(void);
50-
5149
/* ── Global index lock ─────────────────────────────────────────── */
5250
/* Prevents concurrent pipeline runs on the same DB file.
5351
* Atomic spinlock: 0 = free, 1 = locked. */
@@ -853,6 +851,7 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil
853851
if (arc != 0) {
854852
const char *err = cbm_artifact_export_last_error();
855853
cbm_log_error("pipeline.err", "phase", "artifact_export", "err", err ? err : "unknown");
854+
/* A failed persistence export intentionally fails the run; this used to be ignored. */
856855
return arc;
857856
}
858857
}

‎tests/test_artifact.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ enum { ART_TEST_LOG_BUF = 32768 };
2121
static char g_log_capture[ART_TEST_LOG_BUF];
2222
static CBMLogLevel g_prev_log_level;
2323

24-
const char *cbm_artifact_export_last_error(void);
25-
2624
static void setup_artifact_test(void) {
2725
snprintf(g_tmpdir, sizeof(g_tmpdir), "%s/cbm_test_artifact_XXXXXX", cbm_tmpdir());
2826
cbm_mkdtemp(g_tmpdir);

0 commit comments

Comments
 (0)