delete_project: just erase the .db file (and WAL/SHM). */
| 4517 | |
| 4518 | /* delete_project: just erase the .db file (and WAL/SHM). */ |
| 4519 | static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) { |
| 4520 | char *name = get_project_arg(args); |
| 4521 | if (!name) { |
| 4522 | return cbm_mcp_text_result("project is required", true); |
| 4523 | } |
| 4524 | if (!mcp_project_mutation_begin(srv, name)) { |
| 4525 | free(name); |
| 4526 | return cbm_mcp_text_result("project operation cancelled or blocked by an active index", |
| 4527 | true); |
| 4528 | } |
| 4529 | |
| 4530 | /* Close store if it's the project being deleted */ |
| 4531 | if (srv->current_project && strcmp(srv->current_project, name) == 0) { |
| 4532 | if (srv->owns_store && srv->store) { |
| 4533 | cbm_store_close(srv->store); |
| 4534 | srv->store = NULL; |
| 4535 | } |
| 4536 | free(srv->current_project); |
| 4537 | srv->current_project = NULL; |
| 4538 | } |
| 4539 | |
| 4540 | /* Wait for any in-progress pipeline to finish before deleting */ |
| 4541 | cbm_pipeline_lock(); |
| 4542 | |
| 4543 | /* Delete the .db file + WAL/SHM */ |
| 4544 | char path[CBM_SZ_1K]; |
| 4545 | project_db_path(name, path, sizeof(path)); |
| 4546 | |
| 4547 | char wal[CBM_SZ_1K]; |
| 4548 | char shm[CBM_SZ_1K]; |
| 4549 | snprintf(wal, sizeof(wal), "%s-wal", path); |
| 4550 | snprintf(shm, sizeof(shm), "%s-shm", path); |
| 4551 | |
| 4552 | bool exists = (access(path, F_OK) == 0); |
| 4553 | const char *status = "not_found"; |
| 4554 | const char *error_detail = NULL; |
| 4555 | bool is_error = false; |
| 4556 | |
| 4557 | if (exists) { |
| 4558 | int rc = cbm_unlink(path); |
| 4559 | (void)cbm_unlink(wal); |
| 4560 | (void)cbm_unlink(shm); |
| 4561 | if (rc == 0) { |
| 4562 | status = "deleted"; |
| 4563 | } else { |
| 4564 | status = "delete_failed"; |
| 4565 | error_detail = strerror(errno); |
| 4566 | is_error = true; |
| 4567 | } |
| 4568 | } else { |
| 4569 | is_error = true; |
| 4570 | } |
| 4571 | |
| 4572 | cbm_pipeline_unlock(); |
| 4573 | |
| 4574 | if (srv->watcher) { |
| 4575 | cbm_watcher_unwatch(srv->watcher, name); |
| 4576 | } |
no test coverage detected