Skip to content

Commit 402dd7c

Browse files
committed
fix(artifact): create .gitattributes through the UTF-8 path layer
Follow-up to the DeusData#1171 fix. Exporting an artifact from a repository under a CJK path on Windows wrote graph.db.zst and artifact.json, but the .codebase-memory/.gitattributes file carrying the merge=ours protection was silently missing. Root cause: ensure_gitattributes() created the file with the raw POSIX open(O_WRONLY | O_CREAT | O_EXCL). On Windows that is the ANSI CRT, so a repo path with characters outside the active code page fails with ENOENT. The failure was only logged, and the warning itself was malformed: it passed printf-style arguments to the key/value logger, which printed the literal "msg=artifact.gitattributes.open_path=%s_err=%s <path>=<error>". Fix: create the file with cbm_fopen(path, "wbx") (_wfopen on Windows). The "x" mode keeps the O_CREAT | O_EXCL create-only-if-absent semantics, so an existing (possibly user-edited) .gitattributes is never rewritten. Binary mode writes the same LF bytes on every platform. The warning now uses the logger's key/value form (path=..., err=...). Test: artifact::artifact_roundtrip_non_ascii_paths now reads .gitattributes back from the CJK repo through cbm_fopen and asserts the merge=ours line. It also replaces the file with user content, exports again, and asserts the content is unchanged. On the Windows VM the assertion failed 3 of 3 without the fix and passes 3 of 3 with it. The no-overwrite check fails if the mode loses its "x". Refs DeusData#1171 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 7658954 commit 402dd7c

2 files changed

Lines changed: 59 additions & 23 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -772,30 +772,31 @@ static void ensure_gitattributes(const char *repo_path) {
772772
char ga_path[CBM_SZ_4K];
773773
artifact_path(ga_path, sizeof(ga_path), repo_path, ".gitattributes");
774774

775-
/* Atomic create-only-if-absent: O_EXCL closes the TOCTOU window
776-
* between checking existence and writing. If the file exists, open
777-
* fails with EEXIST and we leave it untouched. */
778-
int fd = open(ga_path, O_WRONLY | O_CREAT | O_EXCL, 0644);
779-
if (fd < 0) {
780-
if (errno != EEXIST) {
781-
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));
782788
}
783789
/* fall through to merge driver setup either way */
784790
} else {
785-
FILE *fp = fdopen(fd, "w");
786-
if (fp) {
787-
/* Order matters: attributes apply left to right and the `binary`
788-
* macro expands to `-diff -merge -text`, so a trailing `binary`
789-
* unsets `merge=ours` and the conflict prevention this file
790-
* exists for never engages. The macro must come first. */
791-
(void)fputs("# Auto-generated by codebase-memory-mcp\n"
792-
"# Prevent merge conflicts on compressed artifact\n" CBM_ARTIFACT_FILENAME
793-
" binary merge=ours\n",
794-
fp);
795-
(void)fclose(fp);
796-
} else {
797-
(void)close(fd);
798-
}
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);
799800
}
800801

801802
/* Best-effort: configure merge driver */

‎tests/test_artifact.c‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,30 @@ 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+
337352
/* #1171: a repository (and a cache directory) under a non-ASCII path must
338353
* export, report the artifact present, and import — also onto an existing
339354
* cache db. On Windows the atomic writer used the ANSI CRT fopen + MoveFileExA
340355
* 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). */
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). */
343361
#define ART_CJK_REPO "\xe6\x8c\x81\xe4\xb9\x85\xe5\x8c\x96\xe5\xa4\x8d\xe7\x8e\xb0"
344362
#define ART_CJK_CACHE "\xe5\xaf\xbc\xe5\x85\xa5"
345363
TEST(artifact_roundtrip_non_ascii_paths) {
@@ -356,6 +374,23 @@ TEST(artifact_roundtrip_non_ascii_paths) {
356374
ASSERT_TRUE(cbm_file_exists(zst));
357375
ASSERT_TRUE(cbm_artifact_exists(repo));
358376

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+
359394
char cache_dir[1024];
360395
snprintf(cache_dir, sizeof(cache_dir), "%s/" ART_CJK_CACHE, g_tmpdir);
361396
ASSERT_TRUE(cbm_mkdir_p(cache_dir, 0755));

0 commit comments

Comments
 (0)