Skip to content

Commit c1b9c45

Browse files
DeusDataliuchong
andcommitted
fix(daemon): keep one separator spelling for the session root on Windows
On Windows cbm_canonical_path answers in the platform's native backslash form, and the daemon stored that spelling verbatim for the client's session_root and allowed_root (cbm_mcp_server_set_session_context) and for the auto-index job's repo_path (application.c canonicalizes with the bare call at three sites). Every tool handler, however, normalizes separators after canonicalizing. One directory therefore had two names, and the exact comparison in application_index_args_equal never matched: an explicit index_repository request for the session root was refused as OPTIONS_CONFLICT instead of joining the auto-index job already running for it. Normalize the separator spelling of both session roots in set_session_context, and canonicalize plus normalize at the three application.c sites through one helper, application_canonical_root(). Separators fold on every platform, so the new test daemon_session_context_keeps_one_spelling_of_a_root binds wherever the suite runs; it fails on main with "C:\repos\cbm" != "C:/repos/cbm". Distilled from DeusData#1726 with co-author credit. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com> Co-authored-by: 刘冲 <mail@liuchong.dev>
1 parent fe85a6b commit c1b9c45

3 files changed

Lines changed: 53 additions & 5 deletions

File tree

‎src/daemon/application.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,20 @@ static bool application_canonical_directory_exists(const char *path) {
379379
return cbm_path_info_utf8(path, &info) == 0 && info.is_directory;
380380
}
381381

382+
/* The daemon's one spelling of a repository root. cbm_canonical_path answers in
383+
* the platform's native form (backslashes on Windows) while every tool handler
384+
* normalizes the separators of the paths it canonicalizes. A root kept native
385+
* is a second name for one directory, and the exact comparison downstream -
386+
* index-args equality when a request joins the job already running for that
387+
* root - never matches it. */
388+
static bool application_canonical_root(const char *path, char *out, size_t out_size) {
389+
if (!cbm_canonical_path(path, out, out_size)) {
390+
return false;
391+
}
392+
cbm_normalize_path_sep(out);
393+
return true;
394+
}
395+
382396
static cbm_daemon_application_watch_t *application_find_watch_locked(
383397
cbm_daemon_application_t *application, const char *project) {
384398
for (cbm_daemon_application_watch_t *watch = application->watches; watch; watch = watch->next) {
@@ -2355,9 +2369,10 @@ static cbm_daemon_runtime_application_status_t application_set_context(
23552369
}
23562370
char canonical_root[APPLICATION_PATH_CAP] = {0};
23572371
char canonical_allowed[APPLICATION_PATH_CAP] = {0};
2358-
bool canonical = cbm_canonical_path(root, canonical_root, sizeof(canonical_root));
2372+
bool canonical = application_canonical_root(root, canonical_root, sizeof(canonical_root));
23592373
if (canonical && allowed_present) {
2360-
canonical = cbm_canonical_path(allowed, canonical_allowed, sizeof(canonical_allowed));
2374+
canonical =
2375+
application_canonical_root(allowed, canonical_allowed, sizeof(canonical_allowed));
23612376
}
23622377
canonical = canonical && application_canonical_directory_exists(canonical_root);
23632378
bool set =
@@ -3372,7 +3387,7 @@ static int application_background_index(cbm_daemon_application_t *application,
33723387
return -1;
33733388
}
33743389
char canonical_root[APPLICATION_PATH_CAP];
3375-
if (!cbm_canonical_path(root_path, canonical_root, sizeof(canonical_root)) ||
3390+
if (!application_canonical_root(root_path, canonical_root, sizeof(canonical_root)) ||
33763391
!application_canonical_directory_exists(canonical_root)) {
33773392
return -1;
33783393
}

‎src/mcp/mcp.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,18 @@ bool cbm_mcp_server_set_session_context(cbm_mcp_server_t *srv, const char *sessi
17651765
return false;
17661766
}
17671767

1768-
char *project = cbm_project_name_from_path(session_root);
1768+
/* Keep both roots in the one spelling every tool handler produces. Each
1769+
* handler normalizes separators after canonicalizing, so a root stored in
1770+
* the platform's native form (backslashes on Windows) is a second name for
1771+
* the same directory - and the daemon compares roots exactly. An explicit
1772+
* index_repository request for this root was refused as an options
1773+
* conflict instead of joining the auto-index job already running for it,
1774+
* because the job's repo_path never matched the request's. */
1775+
char root[sizeof(srv->session_root)];
1776+
snprintf(root, sizeof(root), "%s", session_root);
1777+
cbm_normalize_path_sep(root);
1778+
1779+
char *project = cbm_project_name_from_path(root);
17691780
if (!project || project[0] == '\0' || strlen(project) >= sizeof(srv->session_project)) {
17701781
free(project);
17711782
return false;
@@ -1776,8 +1787,9 @@ bool cbm_mcp_server_set_session_context(cbm_mcp_server_t *srv, const char *sessi
17761787
free(project);
17771788
return false;
17781789
}
1790+
cbm_normalize_path_sep(allowed_copy);
17791791

1780-
snprintf(srv->session_root, sizeof(srv->session_root), "%s", session_root);
1792+
snprintf(srv->session_root, sizeof(srv->session_root), "%s", root);
17811793
snprintf(srv->session_project, sizeof(srv->session_project), "%s", project);
17821794
free(project);
17831795

‎tests/test_daemon.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,26 @@ TEST(daemon_sessions_keep_distinct_roots_and_allowed_root_policy) {
467467
PASS();
468468
}
469469

470+
/* The daemon canonicalizes a client's root through the platform API, which on
471+
* Windows answers in backslash form, while every tool handler normalizes the
472+
* separators of the paths it canonicalizes. Stored in its native form, one
473+
* directory had two names, and an explicit index_repository request was
474+
* refused as an options conflict instead of joining the auto-index job already
475+
* running for that root. Separators fold on every platform, so the check binds
476+
* wherever the suite runs. */
477+
TEST(daemon_session_context_keeps_one_spelling_of_a_root) {
478+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
479+
ASSERT_NOT_NULL(srv);
480+
cbm_mcp_server_set_background_tasks(srv, false);
481+
482+
ASSERT_TRUE(cbm_mcp_server_set_session_context(srv, "C:\\repos\\cbm", "C:\\repos"));
483+
ASSERT_STR_EQ(cbm_mcp_server_session_root(srv), "C:/repos/cbm");
484+
ASSERT_STR_EQ(cbm_mcp_server_allowed_root(srv), "C:/repos");
485+
486+
cbm_mcp_server_free(srv);
487+
PASS();
488+
}
489+
470490
SUITE(daemon) {
471491
RUN_TEST(daemon_client_ids_are_connection_bound);
472492
RUN_TEST(daemon_shared_job_survives_until_final_subscriber_disconnects);
@@ -480,4 +500,5 @@ SUITE(daemon) {
480500
RUN_TEST(daemon_bridge_rejects_embedded_nul_body);
481501
RUN_TEST(daemon_bridge_rejects_oversized_headers);
482502
RUN_TEST(daemon_sessions_keep_distinct_roots_and_allowed_root_policy);
503+
RUN_TEST(daemon_session_context_keeps_one_spelling_of_a_root);
483504
}

0 commit comments

Comments
 (0)