Skip to content

Commit 402cc81

Browse files
authored
Merge pull request DeusData#2639 from H4M4CHi-ttr/fix/windows-utf8-watcher-paths
Thank you for the Windows path fix and the careful pre-fix investigation. This uses the existing UTF-8 filesystem helpers so valid repository roots and cached graph databases are not mistaken for missing paths. Contributor history and DCO are preserved.
2 parents d5bb016 + 1b3ca8c commit 402cc81

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎src/daemon/application.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ static bool application_regular_db_exists(const char *project) {
449449
if (written <= 0 || (size_t)written >= sizeof(path)) {
450450
return false;
451451
}
452-
struct stat status;
453-
return stat(path, &status) == 0 && S_ISREG(status.st_mode);
452+
cbm_path_info_t status;
453+
return cbm_path_info_utf8(path, &status) == CBM_PATH_INFO_OK && status.is_regular;
454454
}
455455

456456
static bool application_canonical_directory_exists(const char *path) {

‎src/watcher/watcher.c‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,28 @@ static watcher_git_status_t git_repo_status(cbm_watcher_t *w, project_state_t *s
522522
* a `.git` file (the gitlink form used by linked worktrees and initialized
523523
* submodules). Deliberately a filesystem check, not a git invocation — the whole
524524
* point is to learn something `rev-parse` cannot tell us, because it walks up. */
525+
/* Preserve stat's link-following behavior while inspecting UTF-8 paths. */
526+
static int watcher_path_info(const char *path, cbm_path_info_t *info) {
527+
int status = cbm_path_info_utf8(path, info);
528+
if (status == CBM_PATH_INFO_OK && info->is_symlink) {
529+
char canonical[CBM_SZ_4K];
530+
if (!cbm_canonical_path(path, canonical, sizeof(canonical))) {
531+
return CBM_PATH_INFO_UNAVAILABLE;
532+
}
533+
status = cbm_path_info_utf8(canonical, info);
534+
}
535+
return status;
536+
}
537+
525538
static bool git_has_own_dot_git(const char *root_path) {
526539
char path[CBM_SZ_4K];
527540
int written = snprintf(path, sizeof(path), "%s/.git", root_path);
528541
if (written <= 0 || (size_t)written >= sizeof(path)) {
529542
return false;
530543
}
531-
struct stat st;
532-
return stat(path, &st) == 0 && (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode));
544+
cbm_path_info_t info;
545+
return watcher_path_info(path, &info) == CBM_PATH_INFO_OK &&
546+
(info.is_directory || info.is_regular);
533547
}
534548

535549
/* Does the ancestor repository actually track anything inside this directory?
@@ -990,13 +1004,17 @@ static root_status_t root_status(const char *root_path, int *out_errno) {
9901004
if (!root_path) {
9911005
return ROOT_UNCERTAIN;
9921006
}
993-
struct stat st;
994-
if (stat(root_path, &st) == 0) {
995-
/* Exists but is no longer a directory → the root directory is gone. */
996-
return S_ISDIR(st.st_mode) ? ROOT_PRESENT : ROOT_MISSING;
1007+
cbm_path_info_t info;
1008+
int status = watcher_path_info(root_path, &info);
1009+
if (status == CBM_PATH_INFO_OK) {
1010+
return info.is_directory ? ROOT_PRESENT : ROOT_MISSING;
9971011
}
1012+
#ifdef _WIN32
1013+
*out_errno = status == CBM_PATH_INFO_ABSENT ? ENOENT : EIO;
1014+
#else
9981015
*out_errno = errno;
999-
return cbm_watcher_root_missing_errno(errno) ? ROOT_MISSING : ROOT_UNCERTAIN;
1016+
#endif
1017+
return status == CBM_PATH_INFO_ABSENT ? ROOT_MISSING : ROOT_UNCERTAIN;
10001018
}
10011019

10021020
/* Sustained-absence window (seconds) before a missing root may be pruned.
@@ -1338,8 +1356,8 @@ static void publish_status(project_state_t *s, bool scanned) {
13381356

13391357
/* Init baseline for a project: check if git, get HEAD, count files */
13401358
static bool init_baseline(cbm_watcher_t *w, project_state_t *s) {
1341-
struct stat st;
1342-
if (stat(s->root_path, &st) != 0) {
1359+
cbm_path_info_t info;
1360+
if (watcher_path_info(s->root_path, &info) != CBM_PATH_INFO_OK || !info.is_directory) {
13431361
cbm_log_warn("watcher.root_gone", "project", s->project_name, "path", s->root_path);
13441362
s->baseline_done = true;
13451363
s->is_git = false;

‎tests/test_watcher.c‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,8 @@ TEST(watcher_stop_and_unwatch_cancel_blocked_git_without_backstop) {
11031103
watcher_windows_image_identity_t expected_git;
11041104
ASSERT_TRUE(watcher_windows_capture_image_identity(fake_git, &expected_git));
11051105

1106-
const char *old_path_raw = getenv("PATH");
1107-
const char *old_marker_raw = getenv(WATCHER_TEST_BLOCKING_GIT_MARKER_ENV);
1106+
const char *old_path_raw = cbm_runtime_getenv("PATH");
1107+
const char *old_marker_raw = cbm_runtime_getenv(WATCHER_TEST_BLOCKING_GIT_MARKER_ENV);
11081108
char *old_path = old_path_raw ? cbm_strdup(old_path_raw) : NULL;
11091109
char *old_marker = old_marker_raw ? cbm_strdup(old_marker_raw) : NULL;
11101110
size_t path_size = strlen(bin) + 2 + (old_path ? strlen(old_path) : 0);
@@ -1238,8 +1238,8 @@ TEST(watcher_stop_and_unwatch_cancel_blocked_git_without_backstop) {
12381238
ASSERT_TRUE(watcher_test_self_image(self));
12391239
ASSERT_EQ(symlink(self, fake_git), 0);
12401240

1241-
const char *old_path_raw = getenv("PATH");
1242-
const char *old_marker_raw = getenv(WATCHER_TEST_BLOCKING_GIT_MARKER_ENV);
1241+
const char *old_path_raw = cbm_runtime_getenv("PATH");
1242+
const char *old_marker_raw = cbm_runtime_getenv(WATCHER_TEST_BLOCKING_GIT_MARKER_ENV);
12431243
char *old_path = old_path_raw ? strdup(old_path_raw) : NULL;
12441244
char *old_marker = old_marker_raw ? strdup(old_marker_raw) : NULL;
12451245
size_t path_size = strlen(bin) + 2 + (old_path ? strlen(old_path) : 0);
@@ -3995,7 +3995,41 @@ TEST(watcher_null_watch_count) {
39953995
* SUITE
39963996
* ══════════════════════════════════════════════════════════════════ */
39973997

3998+
TEST(watcher_unicode_root_is_not_pruned_as_missing) {
3999+
char *parent = th_mktempdir("cbm_watch_utf8");
4000+
ASSERT_NOT_NULL(parent);
4001+
char root[CBM_SZ_4K];
4002+
snprintf(root, sizeof(root), "%s/\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E", parent);
4003+
ASSERT_EQ(th_mkdir_p(root), 0);
4004+
const char *saved = cbm_runtime_getenv("CBM_WATCHER_PRUNE_GRACE_S");
4005+
char *saved_copy = saved ? strdup(saved) : NULL;
4006+
cbm_setenv("CBM_WATCHER_PRUNE_GRACE_S", "0", 1);
4007+
cbm_store_t *store = cbm_store_open_memory();
4008+
cbm_store_upsert_project(store, "unicode-root", root);
4009+
cbm_watcher_t *watcher = cbm_watcher_new(store, NULL, NULL);
4010+
cbm_watcher_watch(watcher, "unicode-root", root);
4011+
for (int i = 0; i < 8; i++) {
4012+
cbm_watcher_poll_once(watcher);
4013+
}
4014+
cbm_project_t info = {0};
4015+
bool kept = cbm_watcher_watch_count(watcher) == 1 &&
4016+
cbm_store_get_project(store, "unicode-root", &info) == CBM_STORE_OK;
4017+
cbm_project_free_fields(&info);
4018+
cbm_watcher_free(watcher);
4019+
cbm_store_close(store);
4020+
if (saved_copy) {
4021+
cbm_setenv("CBM_WATCHER_PRUNE_GRACE_S", saved_copy, 1);
4022+
} else {
4023+
cbm_unsetenv("CBM_WATCHER_PRUNE_GRACE_S");
4024+
}
4025+
free(saved_copy);
4026+
ASSERT_EQ(th_rmtree(parent), 0);
4027+
ASSERT_TRUE(kept);
4028+
PASS();
4029+
}
4030+
39984031
SUITE(watcher) {
4032+
RUN_TEST(watcher_unicode_root_is_not_pruned_as_missing);
39994033
/* Adaptive interval */
40004034
RUN_TEST(poll_interval_base);
40014035
RUN_TEST(poll_interval_scaling);

0 commit comments

Comments
 (0)