Skip to content

Commit 7658954

Browse files
committed
fix(artifact): write and detect artifacts through wide paths on Windows (DeusData#1171)
With persistence=true, indexing a repository under a CJK path on Windows made the worker exit nonzero and no .codebase-memory/graph.db.zst was written, while an ASCII junction to the same directory worked. Root cause: write_file_atomic() -- used for artifact.json, graph.db.zst and the import temp db -- opened its temp file with the raw CRT fopen() and published it with MoveFileExA(). Both go through the ANSI code page, so a repo path containing characters outside it cannot be opened. The same file had two more ANSI calls on the same flow: cbm_artifact_exists() used stat() on the repo path, and the import published the cache db with CRT rename(), which is ANSI and also fails when the destination exists. Fix: use the repo's UTF-8 helpers -- cbm_fopen() for the temp file, cbm_rename_replace() (MoveFileExW, write-through, replace-existing) for both publishes, and a cbm_fopen() probe for the non-empty existence check. rename_temp now reports errno on every platform (cbm_rename_replace translates the Win32 error). Test: artifact::artifact_roundtrip_non_ascii_paths exports from a CJK repo directory, checks cbm_artifact_exists(), and imports twice into a CJK cache directory (the second import replaces the existing db). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 64c23fa commit 7658954

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 17 additions & 15 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

@@ -1188,7 +1183,7 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
11881183
* stale WAL next to the cache path would be replayed on top of the
11891184
* imported file at the next open (#897). */
11901185
cbm_remove_db_sidecars(cache_db_path);
1191-
if (rename(tmp_path, cache_db_path) != 0) {
1186+
if (cbm_rename_replace(tmp_path, cache_db_path) != 0) {
11921187
cbm_log_error("artifact.import", "err", "rename_to_cache");
11931188
cbm_unlink(tmp_path);
11941189
return CBM_NOT_FOUND;
@@ -1218,8 +1213,15 @@ bool cbm_artifact_exists(const char *repo_path) {
12181213
char zst_path[CBM_SZ_4K];
12191214
artifact_path(zst_path, sizeof(zst_path), repo_path, CBM_ARTIFACT_FILENAME);
12201215

1221-
struct stat st;
1222-
if (stat(zst_path, &st) != 0 || st.st_size == 0) {
1216+
/* Present and non-empty. cbm_fopen, not stat(): the ANSI CRT stat misses a
1217+
* repo path outside the active Windows code page (#1171). */
1218+
FILE *fp = cbm_fopen(zst_path, "rb");
1219+
if (!fp) {
1220+
return false;
1221+
}
1222+
int first = fgetc(fp);
1223+
(void)fclose(fp);
1224+
if (first == EOF) {
12231225
return false;
12241226
}
12251227

‎tests/test_artifact.c‎

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

337+
/* #1171: a repository (and a cache directory) under a non-ASCII path must
338+
* export, report the artifact present, and import — also onto an existing
339+
* cache db. On Windows the atomic writer used the ANSI CRT fopen + MoveFileExA
340+
* and the existence check the ANSI stat, so CJK characters outside the active
341+
* code page made every step fail. The bytes are escaped UTF-8 for a CJK repo
342+
* directory name and a CJK cache directory name (kept ASCII in source). */
343+
#define ART_CJK_REPO "\xe6\x8c\x81\xe4\xb9\x85\xe5\x8c\x96\xe5\xa4\x8d\xe7\x8e\xb0"
344+
#define ART_CJK_CACHE "\xe5\xaf\xbc\xe5\x85\xa5"
345+
TEST(artifact_roundtrip_non_ascii_paths) {
346+
setup_artifact_test();
347+
create_test_db(g_db);
348+
349+
char repo[1024];
350+
snprintf(repo, sizeof(repo), "%s/cbm-" ART_CJK_REPO, g_tmpdir);
351+
ASSERT_TRUE(cbm_mkdir_p(repo, 0755));
352+
353+
ASSERT_EQ(cbm_artifact_export(g_db, repo, "test-proj", CBM_ARTIFACT_FAST), 0);
354+
char zst[1024];
355+
snprintf(zst, sizeof(zst), "%s/.codebase-memory/graph.db.zst", repo);
356+
ASSERT_TRUE(cbm_file_exists(zst));
357+
ASSERT_TRUE(cbm_artifact_exists(repo));
358+
359+
char cache_dir[1024];
360+
snprintf(cache_dir, sizeof(cache_dir), "%s/" ART_CJK_CACHE, g_tmpdir);
361+
ASSERT_TRUE(cbm_mkdir_p(cache_dir, 0755));
362+
char import_db[1024];
363+
snprintf(import_db, sizeof(import_db), "%s/imported.db", cache_dir);
364+
ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);
365+
/* Re-import replaces the existing cache db (the second-session path). */
366+
ASSERT_EQ(cbm_artifact_import(repo, import_db), 0);
367+
368+
cbm_store_t *s = cbm_store_open_path(import_db);
369+
ASSERT_NOT_NULL(s);
370+
ASSERT_EQ(cbm_store_count_nodes(s, "test-proj"), 2);
371+
ASSERT_EQ(cbm_store_count_edges(s, "test-proj"), 1);
372+
cbm_store_close(s);
373+
374+
cleanup_dir(g_tmpdir);
375+
PASS();
376+
}
377+
337378
TEST(artifact_export_rename_failure_logs_specific_error) {
338379
setup_artifact_test();
339380
create_test_db(g_db);
@@ -1102,6 +1143,7 @@ SUITE(artifact) {
11021143
RUN_TEST(artifact_schema_version_mismatch);
11031144
RUN_TEST(artifact_import_missing);
11041145
RUN_TEST(artifact_gitattributes_created);
1146+
RUN_TEST(artifact_roundtrip_non_ascii_paths);
11051147
RUN_TEST(artifact_export_rename_failure_logs_specific_error);
11061148
RUN_TEST(pipeline_persistence_export_failure_returns_error);
11071149
RUN_TEST(artifact_import_rejects_size_mismatch);

0 commit comments

Comments
 (0)