4242/* Thread-local rotating buffers for small int→string conversions (logging).
4343 * Rotating allows multiple itoa_buf() calls in a single log statement. */
4444enum { ART_RING = 4 , ART_RING_MASK = 3 };
45+ static _Thread_local char g_export_error [CBM_SZ_512 ];
46+
4547static const char * itoa_buf (int v ) {
4648 static _Thread_local char bufs [ART_RING ][CBM_SZ_32 ];
4749 static _Thread_local int idx = 0 ;
@@ -51,9 +53,68 @@ static const char *itoa_buf(int v) {
5153 return bufs [i ];
5254}
5355
56+ const char * cbm_artifact_export_last_error (void ) {
57+ return g_export_error [0 ] ? g_export_error : NULL ;
58+ }
59+
60+ static void clear_export_error (void ) {
61+ g_export_error [0 ] = '\0' ;
62+ }
63+
64+ static int artifact_export_fail (const char * stage , const char * path , const char * err , int err_no ) {
65+ const char * safe_stage = stage ? stage : "unknown" ;
66+ const char * safe_err = err ? err : "unknown" ;
67+
68+ if (path && err_no != 0 ) {
69+ snprintf (g_export_error , sizeof (g_export_error ), "%s: %s errno=%d path=%s" , safe_stage ,
70+ safe_err , err_no , path );
71+ } else if (path ) {
72+ snprintf (g_export_error , sizeof (g_export_error ), "%s: %s path=%s" , safe_stage , safe_err ,
73+ path );
74+ } else if (err_no != 0 ) {
75+ snprintf (g_export_error , sizeof (g_export_error ), "%s: %s errno=%d" , safe_stage , safe_err ,
76+ err_no );
77+ } else {
78+ snprintf (g_export_error , sizeof (g_export_error ), "%s: %s" , safe_stage , safe_err );
79+ }
80+
81+ if (path && err_no != 0 ) {
82+ cbm_log_error ("artifact.export" , "stage" , safe_stage , "err" , safe_err , "errno" ,
83+ itoa_buf (err_no ), "path" , path );
84+ } else if (path ) {
85+ cbm_log_error ("artifact.export" , "stage" , safe_stage , "err" , safe_err , "path" , path );
86+ } else if (err_no != 0 ) {
87+ cbm_log_error ("artifact.export" , "stage" , safe_stage , "err" , safe_err , "errno" ,
88+ itoa_buf (err_no ));
89+ } else {
90+ cbm_log_error ("artifact.export" , "stage" , safe_stage , "err" , safe_err );
91+ }
92+ return CBM_NOT_FOUND ;
93+ }
94+
95+ typedef struct {
96+ const char * err ;
97+ int err_no ;
98+ } artifact_file_error_t ;
99+
100+ static void file_error_clear (artifact_file_error_t * out ) {
101+ if (out ) {
102+ out -> err = NULL ;
103+ out -> err_no = 0 ;
104+ }
105+ }
106+
107+ static void file_error_set (artifact_file_error_t * out , const char * err , int err_no ) {
108+ if (out ) {
109+ out -> err = err ;
110+ out -> err_no = err_no ;
111+ }
112+ }
113+
54114/* 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 );
115+ static bool artifact_path (char * buf , size_t bufsz , const char * repo_path , const char * name ) {
116+ int n = snprintf (buf , bufsz , "%s/%s/%s" , repo_path , CBM_ARTIFACT_DIR , name );
117+ return n >= 0 && (size_t )n < bufsz ;
57118}
58119
59120/* Read entire file into malloc'd buffer. Sets *out_len. Returns NULL on error. */
@@ -85,21 +146,47 @@ static char *read_file_alloc(const char *path, size_t *out_len) {
85146}
86147
87148/* 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 ) {
149+ static int write_file_atomic (const char * path , const char * data , size_t len ,
150+ artifact_file_error_t * out_err ) {
151+ file_error_clear (out_err );
152+
89153 char tmp [CBM_SZ_4K ];
90- snprintf (tmp , sizeof (tmp ), "%s.tmp" , path );
154+ int n = snprintf (tmp , sizeof (tmp ), "%s.tmp" , path );
155+ if (n < 0 || (size_t )n >= sizeof (tmp )) {
156+ file_error_set (out_err , "path_too_long" , 0 );
157+ return CBM_NOT_FOUND ;
158+ }
159+
91160 FILE * fp = fopen (tmp , "wb" );
92161 if (!fp ) {
162+ file_error_set (out_err , "open_temp" , errno );
93163 return CBM_NOT_FOUND ;
94164 }
165+
95166 size_t wr = fwrite (data , ART_NUL , len , fp );
96- (void )fclose (fp );
97167 if (wr != len ) {
168+ int saved_errno = ferror (fp ) ? errno : 0 ;
169+ (void )fclose (fp );
98170 cbm_unlink (tmp );
171+ file_error_set (out_err , "write_temp" , saved_errno );
99172 return CBM_NOT_FOUND ;
100173 }
174+
175+ if (fclose (fp ) != 0 ) {
176+ int saved_errno = errno ;
177+ cbm_unlink (tmp );
178+ file_error_set (out_err , "close_temp" , saved_errno );
179+ return CBM_NOT_FOUND ;
180+ }
181+
182+ #ifdef _WIN32
183+ /* C rename() does not reliably replace an existing destination on Windows. */
184+ cbm_unlink (path );
185+ #endif
101186 if (rename (tmp , path ) != 0 ) {
187+ int saved_errno = errno ;
102188 cbm_unlink (tmp );
189+ file_error_set (out_err , "rename_temp" , saved_errno );
103190 return CBM_NOT_FOUND ;
104191 }
105192 return 0 ;
@@ -215,13 +302,20 @@ static int write_metadata(const char *repo_path, const char *project_name, int n
215302 char * json = yyjson_mut_write (doc , YYJSON_WRITE_PRETTY , & json_len );
216303 yyjson_mut_doc_free (doc );
217304 if (!json ) {
218- return CBM_NOT_FOUND ;
305+ return artifact_export_fail ( "write_metadata" , NULL , "json_encode" , 0 ) ;
219306 }
220307
221308 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 );
309+ if (!artifact_path (meta_path , sizeof (meta_path ), repo_path , CBM_ARTIFACT_META )) {
310+ free (json );
311+ return artifact_export_fail ("write_metadata" , repo_path , "path_too_long" , 0 );
312+ }
313+ artifact_file_error_t ioerr ;
314+ int rc = write_file_atomic (meta_path , json , json_len , & ioerr );
224315 free (json );
316+ if (rc != 0 ) {
317+ return artifact_export_fail ("write_metadata" , meta_path , ioerr .err , ioerr .err_no );
318+ }
225319 return rc ;
226320}
227321
@@ -288,7 +382,8 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
288382 * (which blocks ATTACH, used internally by VACUUM INTO). */
289383 sqlite3 * raw_db = NULL ;
290384 if (sqlite3_open_v2 (db_path , & raw_db , SQLITE_OPEN_READWRITE , NULL ) != SQLITE_OK ) {
291- cbm_log_error ("artifact.export" , "err" , "open_source_db" );
385+ const char * err = raw_db ? sqlite3_errmsg (raw_db ) : "sqlite_open" ;
386+ artifact_export_fail ("open_source_db" , db_path , err , 0 );
292387 sqlite3_close (raw_db );
293388 return NULL ;
294389 }
@@ -300,7 +395,7 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
300395 sqlite3_close (raw_db );
301396
302397 if (vrc != SQLITE_OK ) {
303- cbm_log_error ( "artifact.export " , "err" , "vacuum_into" );
398+ artifact_export_fail ( "vacuum_into " , tmp_path , errmsg ? errmsg : sqlite3_errstr ( vrc ), 0 );
304399 sqlite3_free (errmsg );
305400 cbm_unlink (tmp_path );
306401 return NULL ;
@@ -315,6 +410,9 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
315410 }
316411
317412 char * data = read_file_alloc (tmp_path , out_size );
413+ if (!data || * out_size == 0 ) {
414+ artifact_export_fail ("read_stripped_db" , tmp_path , "empty_or_unreadable" , errno );
415+ }
318416 cbm_unlink (tmp_path );
319417
320418 /* Clean up WAL/SHM from temp */
@@ -331,14 +429,26 @@ static char *prepare_stripped_db(const char *db_path, size_t *out_size) {
331429
332430int cbm_artifact_export (const char * db_path , const char * repo_path , const char * project_name ,
333431 int quality ) {
432+ clear_export_error ();
433+
334434 if (!db_path || !repo_path || !project_name ) {
335- return CBM_NOT_FOUND ;
435+ return artifact_export_fail ( "validate_args" , NULL , "missing_argument" , 0 ) ;
336436 }
337437
338438 /* Ensure .codebase-memory/ directory exists */
339439 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 );
440+ int dir_len = snprintf (art_dir , sizeof (art_dir ), "%s/%s" , repo_path , CBM_ARTIFACT_DIR );
441+ if (dir_len < 0 || (size_t )dir_len >= sizeof (art_dir )) {
442+ return artifact_export_fail ("prepare_artifact_dir" , repo_path , "path_too_long" , 0 );
443+ }
444+ errno = 0 ;
445+ if (!cbm_mkdir_p (art_dir , ART_DIR_PERMS )) {
446+ return artifact_export_fail ("prepare_artifact_dir" , art_dir , "mkdir_or_not_directory" ,
447+ errno );
448+ }
449+ if (!cbm_is_dir (art_dir )) {
450+ return artifact_export_fail ("prepare_artifact_dir" , art_dir , "not_directory" , 0 );
451+ }
342452
343453 size_t db_size = 0 ;
344454 char * db_data = NULL ;
@@ -353,36 +463,40 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
353463
354464 if (!db_data || db_size == 0 ) {
355465 free (db_data );
356- cbm_log_error ("artifact.export" , "err" , "read_db" );
357- return CBM_NOT_FOUND ;
466+ if (cbm_artifact_export_last_error ()) {
467+ return CBM_NOT_FOUND ;
468+ }
469+ return artifact_export_fail ("read_db" , db_path , "empty_or_unreadable" , errno );
358470 }
359471
360472 /* Compress with zstd */
361473 size_t bound = cbm_zstd_compress_bound ((int )db_size );
362474 char * compressed = malloc (bound );
363475 if (!compressed ) {
364476 free (db_data );
365- return CBM_NOT_FOUND ;
477+ return artifact_export_fail ( "compress" , NULL , "alloc_compressed_buffer" , 0 ) ;
366478 }
367479
368480 int clen = cbm_zstd_compress (db_data , (int )db_size , compressed , (int )bound , compression_level );
369481 free (db_data );
370482
371483 if (clen <= 0 ) {
372484 free (compressed );
373- cbm_log_error ("artifact.export" , "err" , "zstd_compress" );
374- return CBM_NOT_FOUND ;
485+ return artifact_export_fail ("compress" , NULL , "zstd_compress" , 0 );
375486 }
376487
377488 /* Write compressed artifact */
378489 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 );
490+ if (!artifact_path (zst_path , sizeof (zst_path ), repo_path , CBM_ARTIFACT_FILENAME )) {
491+ free (compressed );
492+ return artifact_export_fail ("write_artifact" , repo_path , "path_too_long" , 0 );
493+ }
494+ artifact_file_error_t ioerr ;
495+ int wrc = write_file_atomic (zst_path , compressed , (size_t )clen , & ioerr );
381496 free (compressed );
382497
383498 if (wrc != 0 ) {
384- cbm_log_error ("artifact.export" , "err" , "write_artifact" );
385- return CBM_NOT_FOUND ;
499+ return artifact_export_fail ("write_artifact" , zst_path , ioerr .err , ioerr .err_no );
386500 }
387501
388502 /* Get node/edge counts for metadata */
@@ -396,7 +510,11 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
396510 }
397511
398512 /* Write metadata */
399- write_metadata (repo_path , project_name , nodes , edges , db_size , (size_t )clen , compression_level );
513+ if (write_metadata (repo_path , project_name , nodes , edges , db_size , (size_t )clen ,
514+ compression_level ) != 0 ) {
515+ cbm_unlink (zst_path );
516+ return CBM_NOT_FOUND ;
517+ }
400518
401519 /* Ensure .gitattributes for merge conflict prevention */
402520 ensure_gitattributes (repo_path );
@@ -472,11 +590,18 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
472590 cbm_mkdir_p (cache_dir , ART_DIR_PERMS );
473591 }
474592
475- int wrc = write_file_atomic (tmp_path , decompressed , (size_t )dlen );
593+ artifact_file_error_t ioerr ;
594+ int wrc = write_file_atomic (tmp_path , decompressed , (size_t )dlen , & ioerr );
476595 free (decompressed );
477596
478597 if (wrc != 0 ) {
479- cbm_log_error ("artifact.import" , "err" , "write_temp_db" );
598+ if (ioerr .err_no != 0 ) {
599+ cbm_log_error ("artifact.import" , "err" , "write_temp_db" , "detail" , ioerr .err , "errno" ,
600+ itoa_buf (ioerr .err_no ), "path" , tmp_path );
601+ } else {
602+ cbm_log_error ("artifact.import" , "err" , "write_temp_db" , "detail" , ioerr .err , "path" ,
603+ tmp_path );
604+ }
480605 return CBM_NOT_FOUND ;
481606 }
482607
0 commit comments