Skip to content

Commit 6935df2

Browse files
authored
Merge pull request DeusData#1816 from z2250389/codex-z2250389/fix-windows-unicode-git-root
fix(windows): support Unicode repository paths
2 parents f7cfccf + 3e67838 commit 6935df2

5 files changed

Lines changed: 138 additions & 16 deletions

File tree

‎src/git/git_context.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#include "foundation/compat_fs.h"
44
#include "foundation/constants.h"
5+
#include "foundation/platform.h"
56
#include "foundation/str_util.h"
67

78
#include <ctype.h>
89
#include <stdarg.h>
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <string.h>
12-
#include <sys/stat.h>
1313

1414
enum {
1515
GIT_CMD_MAX = 1024,
@@ -254,8 +254,7 @@ int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
254254
return CBM_NOT_FOUND;
255255
}
256256

257-
struct stat st;
258-
out->root_exists = (stat(path, &st) == 0);
257+
out->root_exists = cbm_is_dir(path);
259258
if (!out->root_exists) {
260259
return 0;
261260
}

‎src/ui/http_server.c‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ static void handle_processes(cbm_http_conn_t *c) {
715715

716716
/* ── Directory browser ────────────────────────────────────────── */
717717

718-
#include <dirent.h>
719718

720719
static void append_roots_json(char *buf, size_t bufsz, int *pos) {
721720
http_appendf(buf, bufsz, pos, ",\"roots\":[");
@@ -770,7 +769,7 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
770769
return;
771770
}
772771

773-
DIR *dir = opendir(path);
772+
cbm_dir_t *dir = cbm_opendir(path);
774773
if (!dir) {
775774
cbm_http_replyf(c, 403, g_cors_json, "{\"error\":\"cannot open directory\"}");
776775
return;
@@ -781,16 +780,16 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
781780
int pos = 0;
782781
http_appendf(buf, sizeof(buf), &pos, "{\"path\":\"%s\",\"dirs\":[", path);
783782

784-
struct dirent *ent;
783+
cbm_dirent_t *ent;
785784
int count = 0;
786-
while ((ent = readdir(dir)) != NULL) {
785+
while ((ent = cbm_readdir(dir)) != NULL) {
787786
/* Skip hidden dirs and . / .. */
788-
if (ent->d_name[0] == '.')
787+
if (ent->name[0] == '.')
789788
continue;
790789

791790
/* Check if it's actually a directory */
792791
char full[2048];
793-
snprintf(full, sizeof(full), "%s/%s", path, ent->d_name);
792+
snprintf(full, sizeof(full), "%s/%s", path, ent->name);
794793
if (!cbm_is_dir(full))
795794
continue;
796795

@@ -799,7 +798,7 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
799798
/* Escape directory name to prevent XSS (e.g., names with quotes/angle brackets) */
800799
{
801800
char esc[512];
802-
cbm_json_escape(esc, (int)sizeof(esc), ent->d_name);
801+
cbm_json_escape(esc, (int)sizeof(esc), ent->name);
803802
http_appendf(buf, sizeof(buf), &pos, "\"%s\"", esc);
804803
}
805804
if (pos >= (int)sizeof(buf)) {
@@ -810,7 +809,7 @@ static void handle_browse(cbm_http_conn_t *c, const cbm_http_req_t *req) {
810809
if (count >= 200)
811810
break; /* safety limit */
812811
}
813-
closedir(dir);
812+
cbm_closedir(dir);
814813

815814
/* Parent path — escape to prevent injection */
816815
char parent[1024];

‎tests/test_daemon_application.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,53 @@ static bool app_env_backup_restore(app_env_backup_t *backup) {
18591859
return status == 0;
18601860
}
18611861

1862+
/* The Graph UI enters through the daemon's background coordinator rather than
1863+
* the MCP tool path. Keep that path UTF-8-safe as well. */
1864+
TEST(daemon_application_ui_index_accepts_non_ascii_directory) {
1865+
app_env_backup_t cache_environment;
1866+
bool cache_saved = app_env_backup_capture(&cache_environment, "CBM_CACHE_DIR");
1867+
char root[APP_TEST_PATH_CAP];
1868+
char cache[APP_TEST_PATH_CAP];
1869+
(void)snprintf(root, sizeof(root),
1870+
"%s/cbm-app-ui-index-\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E-XXXXXX", cbm_tmpdir());
1871+
(void)snprintf(cache, sizeof(cache), "%s/cbm-app-ui-index-cache-XXXXXX", cbm_tmpdir());
1872+
bool dirs_created = cbm_mkdtemp(root) != NULL && cbm_mkdtemp(cache) != NULL;
1873+
bool cache_set = cache_saved && dirs_created && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0;
1874+
1875+
app_fake_worker_context_t fake;
1876+
app_fake_worker_context_init(&fake);
1877+
atomic_store(&fake.allow_completion, true);
1878+
cbm_daemon_application_worker_ops_t worker_ops = {
1879+
.context = &fake,
1880+
.start = app_fake_worker_start,
1881+
.poll = app_fake_worker_poll,
1882+
.cancel = app_fake_worker_cancel,
1883+
.log_path = app_fake_worker_log_path,
1884+
.destroy = app_fake_worker_destroy,
1885+
};
1886+
cbm_daemon_application_config_t config = {.worker_ops = &worker_ops};
1887+
cbm_daemon_application_t *application = cache_set ? cbm_daemon_application_new(&config) : NULL;
1888+
int result = application ? cbm_daemon_application_index(application, "", root) : -1;
1889+
bool stopped = application && cbm_daemon_application_shutdown(application, APP_TEST_TIMEOUT_MS);
1890+
cbm_daemon_application_free(application);
1891+
bool root_cleaned = th_rmtree(root) == 0;
1892+
bool cache_cleaned = th_rmtree(cache) == 0;
1893+
bool cache_restored = app_env_backup_restore(&cache_environment);
1894+
1895+
ASSERT_TRUE(cache_saved);
1896+
ASSERT_TRUE(dirs_created);
1897+
ASSERT_TRUE(cache_set);
1898+
ASSERT_NOT_NULL(application);
1899+
ASSERT_EQ(result, 0);
1900+
ASSERT_EQ(atomic_load(&fake.starts), 1);
1901+
ASSERT_EQ(atomic_load(&fake.destroys), 1);
1902+
ASSERT_TRUE(stopped);
1903+
ASSERT_TRUE(root_cleaned);
1904+
ASSERT_TRUE(cache_cleaned);
1905+
ASSERT_TRUE(cache_restored);
1906+
PASS();
1907+
}
1908+
18621909
typedef struct {
18631910
atomic_int starts;
18641911
atomic_int start_failures_remaining;
@@ -5652,6 +5699,7 @@ SUITE(daemon_application) {
56525699
RUN_TEST(daemon_application_initialize_coalesces_auto_index_for_full_sessions);
56535700
RUN_TEST(daemon_application_sensitive_root_blocks_auto_index_but_preserves_controls);
56545701
RUN_TEST(daemon_application_sensitive_root_blocks_watch_but_preserves_controls);
5702+
RUN_TEST(daemon_application_ui_index_accepts_non_ascii_directory);
56555703
RUN_TEST(daemon_application_index_args_compare_repo_path_separator_equivalently);
56565704
RUN_TEST(daemon_application_programmatic_index_injects_resource_policy);
56575705
RUN_TEST(daemon_application_auto_index_honors_tracked_file_limit);

‎tests/test_git_context.c‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* (input_path), not to worktree_root. Joining it with worktree_root and then
99
* string-stripping "/.git" left unresolved ".." components in the result.
1010
*
11-
* These tests shell out to `git`, so they SKIP_PLATFORM on Windows (the CI
12-
* shell there cannot init a repo via system()).
11+
* The canonical_root tests shell out to `git`, so they SKIP_PLATFORM on
12+
* Windows (the CI shell there cannot init a repo via system()). The root-path
13+
* existence test runs on every platform.
1314
*
1415
* Reproduce-first guard: canonical_root_subdir is the genuine RED-without-the-fix
1516
* guard — a repo indexed from a subdirectory yields a relative --git-common-dir
@@ -34,9 +35,29 @@
3435
#include <limits.h>
3536
#endif
3637

37-
/* These helpers shell out to git and are only used by the non-Windows test
38-
* bodies below; on Windows every test SKIP_PLATFORMs, so guard them here too or
39-
* they'd be unused-static functions and fail the -Werror build. */
38+
/* The Windows narrow stat() API interprets UTF-8 paths through the active ANSI
39+
* code page. That made an existing Unicode repository root appear absent and
40+
* stopped Git discovery before git was invoked (#1240). */
41+
TEST(root_exists_non_ascii_directory) {
42+
/* UTF-8 for "Japanese language", written as bytes to keep the source ASCII. */
43+
char *tmp = th_mktempdir("cbm_gitctx_\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E");
44+
if (!tmp)
45+
FAIL("th_mktempdir returned NULL for non-ASCII path");
46+
47+
cbm_git_context_t ctx = {0};
48+
int rc = cbm_git_context_resolve(tmp, &ctx);
49+
bool root_exists = ctx.root_exists;
50+
cbm_git_context_free(&ctx);
51+
th_rmtree(tmp);
52+
53+
ASSERT_EQ(rc, 0);
54+
ASSERT_TRUE(root_exists);
55+
PASS();
56+
}
57+
58+
/* These helpers shell out to git and are only used by the non-Windows
59+
* canonical_root test bodies below; guard them here too or they'd be
60+
* unused-static functions and fail the -Werror build on Windows. */
4061
#ifndef _WIN32
4162
/* Run a git command inside dir, return 0 on success. */
4263
static int git_run(const char *dir, const char *args) {
@@ -235,6 +256,7 @@ TEST(canonical_root_linked_worktree) {
235256
/* ── Suite ──────────────────────────────────────────────────────── */
236257

237258
SUITE(git_context) {
259+
RUN_TEST(root_exists_non_ascii_directory);
238260
RUN_TEST(canonical_root_repo_root);
239261
RUN_TEST(canonical_root_subdir);
240262
RUN_TEST(canonical_root_linked_worktree);

‎tests/test_httpd.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,59 @@ TEST(ui_server_rejects_non_loopback_host) {
21502150
PASS();
21512151
}
21522152

2153+
/* The browser sends UTF-8 repository paths in a percent-encoded query. On
2154+
* Windows, the handler's narrow opendir/readdir calls accepted the ASCII parent
2155+
* but could neither list nor open a non-ASCII child, even though cbm_is_dir's
2156+
* wide-path check had already accepted it. Exercise both user-visible steps:
2157+
* discover the Unicode directory from its parent, then browse into it. */
2158+
TEST(ui_server_browse_non_ascii_directory) {
2159+
static const char utf8_name[] = "\xE9\x81\x93\xE5\x85\xB7\xE7\xAE\xB1"; /* toolbox */
2160+
char *created = th_mktempdir("cbm_browse_utf8");
2161+
if (!created)
2162+
FAIL("mktempdir");
2163+
2164+
char base[512];
2165+
snprintf(base, sizeof(base), "%s", created);
2166+
cbm_normalize_path_sep(base);
2167+
2168+
char unicode_dir[768];
2169+
char child[1024];
2170+
int unicode_len = snprintf(unicode_dir, sizeof(unicode_dir), "%s/%s", base, utf8_name);
2171+
int child_len = snprintf(child, sizeof(child), "%s/Percy", unicode_dir);
2172+
if (unicode_len <= 0 || (size_t)unicode_len >= sizeof(unicode_dir) || child_len <= 0 ||
2173+
(size_t)child_len >= sizeof(child) || th_mkdir_p(child) != 0) {
2174+
th_cleanup(base);
2175+
FAIL("failed to create non-ASCII browse fixture");
2176+
}
2177+
2178+
th_server_t ts;
2179+
if (th_server_start(&ts) != 0) {
2180+
th_cleanup(base);
2181+
FAIL("server start");
2182+
}
2183+
2184+
int port = cbm_http_server_port(ts.srv);
2185+
char request[1536];
2186+
char response[8192];
2187+
snprintf(request, sizeof(request),
2188+
"GET /api/browse?path=%s/%%E9%%81%%93%%E5%%85%%B7%%E7%%AE%%B1 HTTP/1.1\r\n\r\n", base);
2189+
int n = th_http(port, request, response, sizeof(response));
2190+
bool child_browse_ok =
2191+
n > 0 && th_status(response) == 200 && strstr(response, "\"Percy\"") != NULL;
2192+
2193+
snprintf(request, sizeof(request), "GET /api/browse?path=%s HTTP/1.1\r\n\r\n", base);
2194+
n = th_http(port, request, response, sizeof(response));
2195+
bool parent_lists_unicode =
2196+
n > 0 && th_status(response) == 200 && strstr(response, utf8_name) != NULL;
2197+
2198+
th_server_stop(&ts);
2199+
th_cleanup(base);
2200+
2201+
ASSERT_TRUE(parent_lists_unicode);
2202+
ASSERT_TRUE(child_browse_ok);
2203+
PASS();
2204+
}
2205+
21532206
/* The directory browser formats readdir() entries into a fixed 32 KB response
21542207
* buffer. The per-entry loop is clamped, but the trailing "parent"/"roots"
21552208
* appends were not — once the entries filled the buffer, pos ran past the end
@@ -2367,6 +2420,7 @@ TEST(ui_server_index_status_long_paths_no_overflow) {
23672420
/* ── Suite ────────────────────────────────────────────────────── */
23682421

23692422
SUITE(httpd) {
2423+
RUN_TEST(ui_server_browse_non_ascii_directory);
23702424
RUN_TEST(ui_server_browse_wide_dir_no_overflow);
23712425
RUN_TEST(ui_server_logs_escape_dense_no_overflow);
23722426
RUN_TEST(ui_server_index_status_long_paths_no_overflow);

0 commit comments

Comments
 (0)