MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_watcher_watch

Function cbm_watcher_watch

src/watcher/watcher.c:1160–1227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1158/* ── Watch list management ──────────────────────────────────────── */
1159
1160bool cbm_watcher_watch(cbm_watcher_t *w, const char *project_name, const char *root_path) {
1161 if (!w || !project_name || !project_name[0] || !root_path || !root_path[0] ||
1162 atomic_load_explicit(&w->stopped, memory_order_acquire)) {
1163 return false;
1164 }
1165
1166 project_state_t *s = state_new(project_name, root_path);
1167 if (!s) {
1168 cbm_log_warn("watcher.watch.oom", "project", project_name, "path", root_path);
1169 return false;
1170 }
1171
1172 cbm_mutex_lock(&w->projects_lock);
1173 if (atomic_load_explicit(&w->stopped, memory_order_acquire)) {
1174 cbm_mutex_unlock(&w->projects_lock);
1175 state_free(s);
1176 return false;
1177 }
1178 project_state_t *old = cbm_ht_get(w->projects, project_name);
1179 if (old && strcmp(old->root_path, s->root_path) == 0) {
1180 /* Multiple daemon sessions may subscribe to the same canonical
1181 * project/root. Re-registering that identical watch must not discard
1182 * its git baseline, pending dirty signature, or immediate-touch state. */
1183 cbm_mutex_unlock(&w->projects_lock);
1184 state_free(s);
1185 return true;
1186 }
1187 if (old) {
1188 /* A poll snapshot may still be using the old state, including while
1189 * its index callback calls back into this function. Queue it before
1190 * removing the table entry; on OOM, preserve the existing watch. */
1191 if (!defer_state_free(w, old)) {
1192 cbm_mutex_unlock(&w->projects_lock);
1193 state_free(s);
1194 return false;
1195 }
1196 cbm_ht_delete(w->projects, project_name);
1197 }
1198 cbm_ht_set(w->projects, s->project_name, s);
1199 bool registered = cbm_ht_get(w->projects, project_name) == s;
1200 if (!registered && old) {
1201 /* Restore the original table entry and remove the deferred-free slot.
1202 * The lock keeps this rollback invisible to poll/watch callers. */
1203 cbm_ht_set(w->projects, old->project_name, old);
1204 if (cbm_ht_get(w->projects, project_name) == old) {
1205 w->pending_free[--w->pending_free_count] = NULL;
1206 } else {
1207 atomic_store_explicit(&old->registered, false, memory_order_release);
1208 if (old->active_git) {
1209 (void)cbm_subprocess_request_cancel(old->active_git);
1210 }
1211 }
1212 }
1213 if (registered && old) {
1214 atomic_store_explicit(&old->registered, false, memory_order_release);
1215 if (old->active_git) {
1216 (void)cbm_subprocess_request_cancel(old->active_git);
1217 }

Callers 6

test_watcher.cFile · 0.85
test_httpd.cFile · 0.85

Calls 9

state_newFunction · 0.85
cbm_mutex_lockFunction · 0.85
cbm_mutex_unlockFunction · 0.85
state_freeFunction · 0.85
cbm_ht_getFunction · 0.85
defer_state_freeFunction · 0.85
cbm_ht_deleteFunction · 0.85
cbm_ht_setFunction · 0.85

Tested by 1