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

Function prune_missing_project

src/watcher/watcher.c:1507–1570  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1505} poll_ctx_t;
1506
1507static void prune_missing_project(cbm_watcher_t *w, project_state_t *s) {
1508 if (!w || !s || !s->project_name) {
1509 return;
1510 }
1511
1512 char *project_name = cbm_strdup(s->project_name);
1513 char *root_path = cbm_strdup(s->root_path);
1514 if (!project_name || !root_path) {
1515 free(project_name);
1516 free(root_path);
1517 return;
1518 }
1519
1520 bool removed = false;
1521 cbm_mutex_lock(&w->coordination_lock);
1522 bool mutation_acquired =
1523 !w->mutation_begin || w->mutation_begin(w->mutation_context, project_name);
1524 if (!mutation_acquired) {
1525 cbm_mutex_unlock(&w->coordination_lock);
1526 free(project_name);
1527 free(root_path);
1528 return;
1529 }
1530 cbm_mutex_lock(&w->projects_lock);
1531 project_state_t *current = cbm_ht_get(w->projects, project_name);
1532 /* Deferred free (same discipline as cbm_watcher_unwatch): this state
1533 * is referenced by the poll_once snapshot iterating us. On OOM the
1534 * watch stays registered and pruning retries on the next cycle. Re-stat
1535 * under the mutation lease so a root restored after the poll snapshot is
1536 * never pruned. */
1537 int stat_errno = 0;
1538 uint64_t now_ms = cbm_now_ms();
1539 bool still_eligible =
1540 current == s && strcmp(current->root_path, root_path) == 0 &&
1541 current->missing_root_count >= MISSING_ROOT_DELETE_AFTER && current->first_missing_ms > 0 &&
1542 now_ms - current->first_missing_ms >= (uint64_t)prune_grace_s() * CBM_MSEC_PER_SEC &&
1543 root_status(root_path, &stat_errno) == ROOT_MISSING;
1544 if (still_eligible && defer_state_free(w, s)) {
1545 if (delete_cached_project_db(project_name)) {
1546 atomic_store_explicit(&s->registered, false, memory_order_release);
1547 cbm_ht_delete(w->projects, project_name);
1548 removed = true;
1549 } else {
1550 /* The state stays registered; undo the just-added deferred-free
1551 * entry so the next poll can safely retry the failed deletion. */
1552 w->pending_free[--w->pending_free_count] = NULL;
1553 }
1554 }
1555 cbm_mutex_unlock(&w->projects_lock);
1556
1557 if (removed && w->project_pruned) {
1558 w->project_pruned(w->mutation_context, project_name);
1559 }
1560 if (w->mutation_begin) {
1561 w->mutation_end(w->mutation_context, project_name);
1562 }
1563 cbm_mutex_unlock(&w->coordination_lock);
1564

Callers 1

poll_projectFunction · 0.85

Calls 9

cbm_mutex_lockFunction · 0.85
cbm_mutex_unlockFunction · 0.85
cbm_ht_getFunction · 0.85
cbm_now_msFunction · 0.85
prune_grace_sFunction · 0.85
root_statusFunction · 0.85
defer_state_freeFunction · 0.85
delete_cached_project_dbFunction · 0.85
cbm_ht_deleteFunction · 0.85

Tested by

no test coverage detected