Skip to content

Commit 3d4236e

Browse files
authored
Merge pull request DeusData#486 from mvanhorn/fix/400-windows-persistence-artifact-silent-failure
fix: Surface a real error when persistence artifact export silently fails on Windows
2 parents 090a1a7 + 44d5174 commit 3d4236e

4 files changed

Lines changed: 273 additions & 26 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 159 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ 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

4245
/* Thread-local rotating buffers for small int→string conversions (logging).
4346
* Rotating allows multiple itoa_buf() calls in a single log statement. */
4447
enum { ART_RING = 4, ART_RING_MASK = 3 };
48+
static _Thread_local char g_export_error[CBM_SZ_512];
49+
4550
static const char *itoa_buf(int v) {
4651
static _Thread_local char bufs[ART_RING][CBM_SZ_32];
4752
static _Thread_local int idx = 0;
@@ -51,9 +56,68 @@ static const char *itoa_buf(int v) {
5156
return bufs[i];
5257
}
5358

59+
const char *cbm_artifact_export_last_error(void) {
60+
return g_export_error[0] ? g_export_error : NULL;
61+
}
62+
63+
static void clear_export_error(void) {
64+
g_export_error[0] = '\0';
65+
}
66+
67+
static int artifact_export_fail(const char *stage, const char *path, const char *err, int err_no) {
68+
const char *safe_stage = stage ? stage : "unknown";
69+
const char *safe_err = err ? err : "unknown";
70+
71+
if (path && err_no != 0) {
72+
snprintf(g_export_error, sizeof(g_export_error), "%s: %s errno=%d path=%s", safe_stage,
73+
safe_err, err_no, path);
74+
} else if (path) {
75+
snprintf(g_export_error, sizeof(g_export_error), "%s: %s path=%s", safe_stage, safe_err,
76+
path);
77+
} else if (err_no != 0) {
78+
snprintf(g_export_error, sizeof(g_export_error), "%s: %s errno=%d", safe_stage, safe_err,
79+
err_no);
80+
} else {
81+
snprintf(g_export_error, sizeof(g_export_error), "%s: %s", safe_stage, safe_err);
82+
}
83+
84+
if (path && err_no != 0) {
85+
cbm_log_error("artifact.export", "stage", safe_stage, "err", safe_err, "errno",
86+
itoa_buf(err_no), "path", path);
87+
} else if (path) {
88+
cbm_log_error("artifact.export", "stage", safe_stage, "err", safe_err, "path", path);
89+
} else if (err_no != 0) {
90+
cbm_log_error("artifact.export", "stage", safe_stage, "err", safe_err, "errno",
91+
itoa_buf(err_no));
92+
} else {
93+
cbm_log_error("artifact.export", "stage", safe_stage, "err", safe_err);
94+
}
95+
return CBM_NOT_FOUND;
96+
}
97+
98+
typedef struct {
99+
const char *err;
100+
int err_no;
101+
} artifact_file_error_t;
102+
103+
static void file_error_clear(artifact_file_error_t *out) {
104+
if (out) {
105+
out->err = NULL;
106+
out->err_no = 0;
107+
}
108+
}
109+
110+
static void file_error_set(artifact_file_error_t *out, const char *err, int err_no) {
111+
if (out) {
112+
out->err = err;
113+
out->err_no = err_no;
114+
}
115+
}
116+
54117
/* Build path: <repo>/.codebase-memory/<name> into caller-owned buf. */
55-
static void artifact_path(char *buf, size_t bufsz, const char *repo_path, const char *name) {
56-
snprintf(buf, bufsz, "%s/%s/%s", repo_path, CBM_ARTIFACT_DIR, name);
118+
static bool artifact_path(char *buf, size_t bufsz, const char *repo_path, const char *name) {
119+
int n = snprintf(buf, bufsz, "%s/%s/%s", repo_path, CBM_ARTIFACT_DIR, name);
120+
return n >= 0 && (size_t)n < bufsz;
57121
}
58122

59123
/* Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */
@@ -85,23 +149,55 @@ static char *read_file_alloc(const char *path, size_t *out_len) {
85149
}
86150

87151
/* Write buffer to file atomically (write to tmp, rename). Returns 0 on success. */
88-
static int write_file_atomic(const char *path, const char *data, size_t len) {
152+
static int write_file_atomic(const char *path, const char *data, size_t len,
153+
artifact_file_error_t *out_err) {
154+
file_error_clear(out_err);
155+
89156
char tmp[CBM_SZ_4K];
90-
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
157+
int n = snprintf(tmp, sizeof(tmp), "%s.tmp", path);
158+
if (n < 0 || (size_t)n >= sizeof(tmp)) {
159+
file_error_set(out_err, "path_too_long", 0);
160+
return CBM_NOT_FOUND;
161+
}
162+
91163
FILE *fp = fopen(tmp, "wb");
92164
if (!fp) {
165+
file_error_set(out_err, "open_temp", errno);
93166
return CBM_NOT_FOUND;
94167
}
168+
95169
size_t wr = fwrite(data, ART_NUL, len, fp);
96-
(void)fclose(fp);
97170
if (wr != len) {
171+
int saved_errno = ferror(fp) ? errno : 0;
172+
(void)fclose(fp);
98173
cbm_unlink(tmp);
174+
file_error_set(out_err, "write_temp", saved_errno);
99175
return CBM_NOT_FOUND;
100176
}
177+
178+
if (fclose(fp) != 0) {
179+
int saved_errno = errno;
180+
cbm_unlink(tmp);
181+
file_error_set(out_err, "close_temp", saved_errno);
182+
return CBM_NOT_FOUND;
183+
}
184+
185+
#ifdef _WIN32
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
101194
if (rename(tmp, path) != 0) {
195+
int saved_errno = errno;
102196
cbm_unlink(tmp);
197+
file_error_set(out_err, "rename_temp", saved_errno);
103198
return CBM_NOT_FOUND;
104199
}
200+
#endif
105201
return 0;
106202
}
107203

@@ -215,13 +311,20 @@ static int write_metadata(const char *repo_path, const char *project_name, int n
215311
char *json = yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, &json_len);
216312
yyjson_mut_doc_free(doc);
217313
if (!json) {
218-
return CBM_NOT_FOUND;
314+
return artifact_export_fail("write_metadata", NULL, "json_encode", 0);
219315
}
220316

221317
char meta_path[CBM_SZ_4K];
222-
artifact_path(meta_path, sizeof(meta_path), repo_path, CBM_ARTIFACT_META);
223-
int rc = write_file_atomic(meta_path, json, json_len);
318+
if (!artifact_path(meta_path, sizeof(meta_path), repo_path, CBM_ARTIFACT_META)) {
319+
free(json);
320+
return artifact_export_fail("write_metadata", repo_path, "path_too_long", 0);
321+
}
322+
artifact_file_error_t ioerr;
323+
int rc = write_file_atomic(meta_path, json, json_len, &ioerr);
224324
free(json);
325+
if (rc != 0) {
326+
return artifact_export_fail("write_metadata", meta_path, ioerr.err, ioerr.err_no);
327+
}
225328
return rc;
226329
}
227330

@@ -288,7 +391,8 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
288391
* (which blocks ATTACH, used internally by VACUUM INTO). */
289392
sqlite3 *raw_db = NULL;
290393
if (sqlite3_open_v2(db_path, &raw_db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
291-
cbm_log_error("artifact.export", "err", "open_source_db");
394+
const char *err = raw_db ? sqlite3_errmsg(raw_db) : "sqlite_open";
395+
artifact_export_fail("open_source_db", db_path, err, 0);
292396
sqlite3_close(raw_db);
293397
return NULL;
294398
}
@@ -300,7 +404,7 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
300404
sqlite3_close(raw_db);
301405

302406
if (vrc != SQLITE_OK) {
303-
cbm_log_error("artifact.export", "err", "vacuum_into");
407+
artifact_export_fail("vacuum_into", tmp_path, errmsg ? errmsg : sqlite3_errstr(vrc), 0);
304408
sqlite3_free(errmsg);
305409
cbm_unlink(tmp_path);
306410
return NULL;
@@ -315,6 +419,9 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
315419
}
316420

317421
char *data = read_file_alloc(tmp_path, out_size);
422+
if (!data || *out_size == 0) {
423+
artifact_export_fail("read_stripped_db", tmp_path, "empty_or_unreadable", errno);
424+
}
318425
cbm_unlink(tmp_path);
319426

320427
/* Clean up WAL/SHM from temp */
@@ -331,14 +438,26 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
331438

332439
int cbm_artifact_export(const char *db_path, const char *repo_path, const char *project_name,
333440
int quality) {
441+
clear_export_error();
442+
334443
if (!db_path || !repo_path || !project_name) {
335-
return CBM_NOT_FOUND;
444+
return artifact_export_fail("validate_args", NULL, "missing_argument", 0);
336445
}
337446

338447
/* Ensure .codebase-memory/ directory exists */
339448
char art_dir[CBM_SZ_4K];
340-
snprintf(art_dir, sizeof(art_dir), "%s/%s", repo_path, CBM_ARTIFACT_DIR);
341-
cbm_mkdir_p(art_dir, ART_DIR_PERMS);
449+
int dir_len = snprintf(art_dir, sizeof(art_dir), "%s/%s", repo_path, CBM_ARTIFACT_DIR);
450+
if (dir_len < 0 || (size_t)dir_len >= sizeof(art_dir)) {
451+
return artifact_export_fail("prepare_artifact_dir", repo_path, "path_too_long", 0);
452+
}
453+
errno = 0;
454+
if (!cbm_mkdir_p(art_dir, ART_DIR_PERMS)) {
455+
return artifact_export_fail("prepare_artifact_dir", art_dir, "mkdir_or_not_directory",
456+
errno);
457+
}
458+
if (!cbm_is_dir(art_dir)) {
459+
return artifact_export_fail("prepare_artifact_dir", art_dir, "not_directory", 0);
460+
}
342461

343462
size_t db_size = 0;
344463
char *db_data = NULL;
@@ -353,36 +472,40 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
353472

354473
if (!db_data || db_size == 0) {
355474
free(db_data);
356-
cbm_log_error("artifact.export", "err", "read_db");
357-
return CBM_NOT_FOUND;
475+
if (cbm_artifact_export_last_error()) {
476+
return CBM_NOT_FOUND;
477+
}
478+
return artifact_export_fail("read_db", db_path, "empty_or_unreadable", errno);
358479
}
359480

360481
/* Compress with zstd */
361482
size_t bound = cbm_zstd_compress_bound((int)db_size);
362483
char *compressed = malloc(bound);
363484
if (!compressed) {
364485
free(db_data);
365-
return CBM_NOT_FOUND;
486+
return artifact_export_fail("compress", NULL, "alloc_compressed_buffer", 0);
366487
}
367488

368489
int clen = cbm_zstd_compress(db_data, (int)db_size, compressed, (int)bound, compression_level);
369490
free(db_data);
370491

371492
if (clen <= 0) {
372493
free(compressed);
373-
cbm_log_error("artifact.export", "err", "zstd_compress");
374-
return CBM_NOT_FOUND;
494+
return artifact_export_fail("compress", NULL, "zstd_compress", 0);
375495
}
376496

377497
/* Write compressed artifact */
378498
char zst_path[CBM_SZ_4K];
379-
artifact_path(zst_path, sizeof(zst_path), repo_path, CBM_ARTIFACT_FILENAME);
380-
int wrc = write_file_atomic(zst_path, compressed, (size_t)clen);
499+
if (!artifact_path(zst_path, sizeof(zst_path), repo_path, CBM_ARTIFACT_FILENAME)) {
500+
free(compressed);
501+
return artifact_export_fail("write_artifact", repo_path, "path_too_long", 0);
502+
}
503+
artifact_file_error_t ioerr;
504+
int wrc = write_file_atomic(zst_path, compressed, (size_t)clen, &ioerr);
381505
free(compressed);
382506

383507
if (wrc != 0) {
384-
cbm_log_error("artifact.export", "err", "write_artifact");
385-
return CBM_NOT_FOUND;
508+
return artifact_export_fail("write_artifact", zst_path, ioerr.err, ioerr.err_no);
386509
}
387510

388511
/* Get node/edge counts for metadata */
@@ -396,7 +519,11 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
396519
}
397520

398521
/* Write metadata */
399-
write_metadata(repo_path, project_name, nodes, edges, db_size, (size_t)clen, compression_level);
522+
if (write_metadata(repo_path, project_name, nodes, edges, db_size, (size_t)clen,
523+
compression_level) != 0) {
524+
cbm_unlink(zst_path);
525+
return CBM_NOT_FOUND;
526+
}
400527

401528
/* Ensure .gitattributes for merge conflict prevention */
402529
ensure_gitattributes(repo_path);
@@ -472,11 +599,18 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
472599
cbm_mkdir_p(cache_dir, ART_DIR_PERMS);
473600
}
474601

475-
int wrc = write_file_atomic(tmp_path, decompressed, (size_t)dlen);
602+
artifact_file_error_t ioerr;
603+
int wrc = write_file_atomic(tmp_path, decompressed, (size_t)dlen, &ioerr);
476604
free(decompressed);
477605

478606
if (wrc != 0) {
479-
cbm_log_error("artifact.import", "err", "write_temp_db");
607+
if (ioerr.err_no != 0) {
608+
cbm_log_error("artifact.import", "err", "write_temp_db", "detail", ioerr.err, "errno",
609+
itoa_buf(ioerr.err_no), "path", tmp_path);
610+
} else {
611+
cbm_log_error("artifact.import", "err", "write_temp_db", "detail", ioerr.err, "path",
612+
tmp_path);
613+
}
480614
return CBM_NOT_FOUND;
481615
}
482616

‎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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,13 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil
898898

899899
/* Export persistent artifact if enabled */
900900
if (p->persistence) {
901-
cbm_artifact_export(db_path, p->repo_path, p->project_name, CBM_ARTIFACT_BEST);
901+
int arc = cbm_artifact_export(db_path, p->repo_path, p->project_name, CBM_ARTIFACT_BEST);
902+
if (arc != 0) {
903+
const char *err = cbm_artifact_export_last_error();
904+
cbm_log_error("pipeline.err", "phase", "artifact_export", "err", err ? err : "unknown");
905+
/* A failed persistence export intentionally fails the run; this used to be ignored. */
906+
return arc;
907+
}
902908
}
903909

904910
return 0;

0 commit comments

Comments
 (0)