@@ -185,7 +185,10 @@ static int write_file_atomic(const char *path, const char *data, size_t len,
185185 return CBM_NOT_FOUND ;
186186 }
187187
188- FILE * fp = fopen (tmp , "wb" );
188+ /* cbm_fopen / cbm_rename_replace: wide paths on Windows. The ANSI CRT fopen
189+ * and MoveFileExA mangled a repo path outside the active code page (a CJK
190+ * checkout), so every persistent export failed there (#1171). */
191+ FILE * fp = cbm_fopen (tmp , "wb" );
189192 if (!fp ) {
190193 file_error_set (out_err , "open_temp" , errno );
191194 return CBM_NOT_FOUND ;
@@ -207,22 +210,14 @@ static int write_file_atomic(const char *path, const char *data, size_t len,
207210 return CBM_NOT_FOUND ;
208211 }
209212
210- #ifdef _WIN32
211- /* MoveFileEx replace approach suggested by @Ayush7Ranjan in #492. */
212- if (!MoveFileExA (tmp , path , MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH )) {
213- DWORD saved_error = GetLastError ();
214- cbm_unlink (tmp );
215- file_error_set (out_err , "rename_temp" , (int )saved_error );
216- return CBM_NOT_FOUND ;
217- }
218- #else
219- if (rename (tmp , path ) != 0 ) {
213+ /* Replace-existing rename (MoveFileExW write-through on Windows, the
214+ * approach suggested by @Ayush7Ranjan in #492); errno carries the cause. */
215+ if (cbm_rename_replace (tmp , path ) != 0 ) {
220216 int saved_errno = errno ;
221217 cbm_unlink (tmp );
222218 file_error_set (out_err , "rename_temp" , saved_errno );
223219 return CBM_NOT_FOUND ;
224220 }
225- #endif
226221 return 0 ;
227222}
228223
@@ -777,30 +772,31 @@ static void ensure_gitattributes(const char *repo_path) {
777772 char ga_path [CBM_SZ_4K ];
778773 artifact_path (ga_path , sizeof (ga_path ), repo_path , ".gitattributes" );
779774
780- /* Atomic create-only-if-absent: O_EXCL closes the TOCTOU window
781- * between checking existence and writing. If the file exists, open
782- * fails with EEXIST and we leave it untouched. */
783- int fd = open (ga_path , O_WRONLY | O_CREAT | O_EXCL , 0644 );
784- if (fd < 0 ) {
785- if (errno != EEXIST ) {
786- cbm_log_warn ("artifact.gitattributes.open path=%s err=%s" , ga_path , strerror (errno ));
775+ /* Atomic create-only-if-absent: the "x" mode (O_CREAT|O_EXCL) closes the
776+ * TOCTOU window between checking existence and writing. If the file
777+ * exists, the open fails with EEXIST and we leave it untouched.
778+ * cbm_fopen, not open(): the ANSI CRT open() cannot create the file under
779+ * a repo path outside the active code page on Windows, which silently
780+ * dropped the merge=ours protection there (#1171). */
781+ errno = 0 ;
782+ FILE * fp = cbm_fopen (ga_path , "wbx" );
783+ if (!fp ) {
784+ int open_errno = errno ;
785+ if (open_errno != EEXIST ) {
786+ cbm_log_warn ("artifact.gitattributes.open" , "path" , ga_path , "err" ,
787+ strerror (open_errno ));
787788 }
788789 /* fall through to merge driver setup either way */
789790 } else {
790- FILE * fp = fdopen (fd , "w" );
791- if (fp ) {
792- /* Order matters: attributes apply left to right and the `binary`
793- * macro expands to `-diff -merge -text`, so a trailing `binary`
794- * unsets `merge=ours` and the conflict prevention this file
795- * exists for never engages. The macro must come first. */
796- (void )fputs ("# Auto-generated by codebase-memory-mcp\n"
797- "# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME
798- " binary merge=ours\n" ,
799- fp );
800- (void )fclose (fp );
801- } else {
802- (void )close (fd );
803- }
791+ /* Order matters: attributes apply left to right and the `binary`
792+ * macro expands to `-diff -merge -text`, so a trailing `binary`
793+ * unsets `merge=ours` and the conflict prevention this file
794+ * exists for never engages. The macro must come first. */
795+ (void )fputs ("# Auto-generated by codebase-memory-mcp\n"
796+ "# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME
797+ " binary merge=ours\n" ,
798+ fp );
799+ (void )fclose (fp );
804800 }
805801
806802 /* Best-effort: configure merge driver */
@@ -1188,7 +1184,7 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
11881184 * stale WAL next to the cache path would be replayed on top of the
11891185 * imported file at the next open (#897). */
11901186 cbm_remove_db_sidecars (cache_db_path );
1191- if (rename (tmp_path , cache_db_path ) != 0 ) {
1187+ if (cbm_rename_replace (tmp_path , cache_db_path ) != 0 ) {
11921188 cbm_log_error ("artifact.import" , "err" , "rename_to_cache" );
11931189 cbm_unlink (tmp_path );
11941190 return CBM_NOT_FOUND ;
@@ -1218,8 +1214,15 @@ bool cbm_artifact_exists(const char *repo_path) {
12181214 char zst_path [CBM_SZ_4K ];
12191215 artifact_path (zst_path , sizeof (zst_path ), repo_path , CBM_ARTIFACT_FILENAME );
12201216
1221- struct stat st ;
1222- if (stat (zst_path , & st ) != 0 || st .st_size == 0 ) {
1217+ /* Present and non-empty. cbm_fopen, not stat(): the ANSI CRT stat misses a
1218+ * repo path outside the active Windows code page (#1171). */
1219+ FILE * fp = cbm_fopen (zst_path , "rb" );
1220+ if (!fp ) {
1221+ return false;
1222+ }
1223+ int first = fgetc (fp );
1224+ (void )fclose (fp );
1225+ if (first == EOF ) {
12231226 return false;
12241227 }
12251228
0 commit comments