Skip to content

Commit d19cab8

Browse files
authored
Merge pull request DeusData#2461 from DeusData/fix/issue-1171
fix(artifact): write and detect artifacts through wide paths on Windows (DeusData#1171)
2 parents 731f624 + 4cea83d commit d19cab8

2 files changed

Lines changed: 116 additions & 36 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎tests/test_artifact.c‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,82 @@ TEST(artifact_gitattributes_created) {
334334
PASS();
335335
}
336336

337+
/* Read a small file through the UTF-8 path layer (wide on Windows), so a
338+
* non-ASCII path is never misread by the test's own ANSI CRT call. Returns the
339+
* byte count, or 0 when the file cannot be opened or is empty. */
340+
static size_t read_small_file_utf8(const char *path, char *buf, size_t cap) {
341+
buf[0] = '\0';
342+
FILE *fp = cbm_fopen(path, "rb");
343+
if (!fp) {
344+
return 0;
345+
}
346+
size_t rd = fread(buf, 1, cap - 1, fp);
347+
(void)fclose(fp);
348+
buf[rd] = '\0';
349+
return rd;
350+
}
351+
352+
/* #1171: a repository (and a cache directory) under a non-ASCII path must
353+
* export, report the artifact present, and import — also onto an existing
354+
* cache db. On Windows the atomic writer used the ANSI CRT fopen + MoveFileExA
355+
* and the existence check the ANSI stat, so CJK characters outside the active
356+
* code page made every step fail. The same export also writes the
357+
* .gitattributes merge=ours protection, which ensure_gitattributes created
358+
* with the ANSI CRT open(): on Windows it was silently missing. The bytes are
359+
* escaped UTF-8 for a CJK repo directory name and a CJK cache directory name
360+
* (kept ASCII in source). */
361+
#define ART_CJK_REPO "\xe6\x8c\x81\xe4\xb9\x85\xe5\x8c\x96\xe5\xa4\x8d\xe7\x8e\xb0"
362+
#define ART_CJK_CACHE "\xe5\xaf\xbc\xe5\x85\xa5"
363+
TEST(artifact_roundtrip_non_ascii_paths) {
364+
setup_artifact_test();
365+
create_test_db(g_db);
366+
367+
char repo[1024];
368+
snprintf(repo, sizeof(repo), "%s/cbm-" ART_CJK_REPO, g_tmpdir);
369+
ASSERT_TRUE(cbm_mkdir_p(repo, 0755));
370+
371+
ASSERT_EQ(cbm_artifact_export(g_db, repo, "test-proj", CBM_ARTIFACT_FAST), 0);
372+
char zst[1024];
373+
snprintf(zst, sizeof(zst), "%s/.codebase-memory/graph.db.zst", repo);
374+
ASSERT_TRUE(cbm_file_exists(zst));
375+
ASSERT_TRUE(cbm_artifact_exists(repo));
376+
377+
char ga[1024];
378+
snprintf(ga, sizeof(ga), "%s/.codebase-memory/.gitattributes", repo);
379+
char ga_content[512];
380+
ASSERT_TRUE(read_small_file_utf8(ga, ga_content, sizeof(ga_content)) > 0);
381+
ASSERT_NOT_NULL(strstr(ga_content, CBM_ARTIFACT_FILENAME " binary merge=ours"));
382+
383+
/* Create-only-if-absent: a re-export never rewrites an existing
384+
* .gitattributes the user may have edited. */
385+
static const char user_ga[] = "# user-owned\n";
386+
FILE *uga = cbm_fopen(ga, "wb");
387+
ASSERT_NOT_NULL(uga);
388+
ASSERT_TRUE(fputs(user_ga, uga) >= 0);
389+
ASSERT_EQ(fclose(uga), 0);
390+
ASSERT_EQ(cbm_artifact_export(g_db, repo, "test-proj", CBM_ARTIFACT_FAST), 0);
391+
ASSERT_EQ(read_small_file_utf8(ga, ga_content, sizeof(ga_content)), strlen(user_ga));
392+
ASSERT_STR_EQ(ga_content, user_ga);
393+
394+
char cache_dir[1024];
395+
snprintf(cache_dir, sizeof(cache_dir), "%s/" ART_CJK_CACHE, g_tmpdir);
396+
ASSERT_TRUE(cbm_mkdir_p(cache_dir, 0755));
397+
char import_db[1024];
398+
snprintf(import_db, sizeof(import_db), "%s/imported.db", cache_dir);
399+
ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);
400+
/* Re-import replaces the existing cache db (the second-session path). */
401+
ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);
402+
403+
cbm_store_t *s = cbm_store_open_path(import_db);
404+
ASSERT_NOT_NULL(s);
405+
ASSERT_EQ(cbm_store_count_nodes(s, "test-proj"), 2);
406+
ASSERT_EQ(cbm_store_count_edges(s, "test-proj"), 1);
407+
cbm_store_close(s);
408+
409+
cleanup_dir(g_tmpdir);
410+
PASS();
411+
}
412+
337413
TEST(artifact_export_rename_failure_logs_specific_error) {
338414
setup_artifact_test();
339415
create_test_db(g_db);
@@ -1102,6 +1178,7 @@ SUITE(artifact) {
11021178
RUN_TEST(artifact_schema_version_mismatch);
11031179
RUN_TEST(artifact_import_missing);
11041180
RUN_TEST(artifact_gitattributes_created);
1181+
RUN_TEST(artifact_roundtrip_non_ascii_paths);
11051182
RUN_TEST(artifact_export_rename_failure_logs_specific_error);
11061183
RUN_TEST(pipeline_persistence_export_failure_returns_error);
11071184
RUN_TEST(artifact_import_rejects_size_mismatch);

0 commit comments

Comments
 (0)