delete_project: just erase the .db file (and WAL/SHM). */
| 2176 | |
| 2177 | /* delete_project: just erase the .db file (and WAL/SHM). */ |
| 2178 | static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) { |
| 2179 | char *name = get_project_arg(args); |
| 2180 | if (!name) { |
| 2181 | return cbm_mcp_text_result("project is required", true); |
| 2182 | } |
| 2183 | |
| 2184 | /* Close store if it's the project being deleted */ |
| 2185 | if (srv->current_project && strcmp(srv->current_project, name) == 0) { |
| 2186 | if (srv->owns_store && srv->store) { |
| 2187 | cbm_store_close(srv->store); |
| 2188 | srv->store = NULL; |
| 2189 | } |
| 2190 | free(srv->current_project); |
| 2191 | srv->current_project = NULL; |
| 2192 | } |
| 2193 | |
| 2194 | /* Wait for any in-progress pipeline to finish before deleting */ |
| 2195 | cbm_pipeline_lock(); |
| 2196 | |
| 2197 | /* Delete the .db file + WAL/SHM */ |
| 2198 | char path[CBM_SZ_1K]; |
| 2199 | project_db_path(name, path, sizeof(path)); |
| 2200 | |
| 2201 | char wal[CBM_SZ_1K]; |
| 2202 | char shm[CBM_SZ_1K]; |
| 2203 | snprintf(wal, sizeof(wal), "%s-wal", path); |
| 2204 | snprintf(shm, sizeof(shm), "%s-shm", path); |
| 2205 | |
| 2206 | bool exists = (access(path, F_OK) == 0); |
| 2207 | const char *status = "not_found"; |
| 2208 | const char *error_detail = NULL; |
| 2209 | bool is_error = false; |
| 2210 | |
| 2211 | if (exists) { |
| 2212 | int rc = cbm_unlink(path); |
| 2213 | (void)cbm_unlink(wal); |
| 2214 | (void)cbm_unlink(shm); |
| 2215 | if (rc == 0) { |
| 2216 | status = "deleted"; |
| 2217 | } else { |
| 2218 | status = "delete_failed"; |
| 2219 | error_detail = strerror(errno); |
| 2220 | is_error = true; |
| 2221 | } |
| 2222 | } else { |
| 2223 | is_error = true; |
| 2224 | } |
| 2225 | |
| 2226 | cbm_pipeline_unlock(); |
| 2227 | |
| 2228 | if (srv->watcher) { |
| 2229 | cbm_watcher_unwatch(srv->watcher, name); |
| 2230 | } |
| 2231 | |
| 2232 | cbm_mem_collect(); /* return freed pages to OS after closing database */ |
| 2233 | |
| 2234 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 2235 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
no test coverage detected