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

Function cbm_store_open_path_query

src/store/store.c:837–923  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

835}
836
837cbm_store_t *cbm_store_open_path_query(const char *db_path) {
838 if (!db_path) {
839 return NULL;
840 }
841
842 cbm_store_t *s = calloc(CBM_ALLOC_ONE, sizeof(cbm_store_t));
843 if (!s) {
844 return NULL;
845 }
846
847 /* Query tools open the project DB READ-ONLY: a read query must never
848 * mutate the DB (the previous READWRITE open + WAL write-pragmas did),
849 * and must work on a read-only DB file / filesystem.
850 *
851 * Try a plain READONLY open first — on a normal writable filesystem this
852 * reads WAL frames correctly via the -shm wal-index. SQLite opens lazily,
853 * so a read-only-filesystem failure (cannot create -shm for a WAL-mode
854 * DB) surfaces on first access, not at open time; we probe with a trivial
855 * read to force it. If the probe fails, retry once with an immutable URI
856 * that bypasses WAL and reads the main DB file directly.
857 *
858 * No SQLITE_OPEN_CREATE on either path — a missing DB must return NULL
859 * (no ghost .db for unknown/unindexed projects). */
860 char open_path[4096];
861 if (!cbm_path_for_file_api(db_path, open_path, sizeof(open_path))) {
862 free(s);
863 return NULL;
864 }
865 int rc = sqlite3_open_v2(open_path, &s->db, SQLITE_OPEN_READONLY, NULL);
866 if (rc == SQLITE_OK) {
867 /* Force first DB access so a read-only-FS WAL failure surfaces now. */
868 if (sqlite3_exec(s->db, "SELECT 1 FROM sqlite_master LIMIT 1;", NULL, NULL, NULL) !=
869 SQLITE_OK) {
870 sqlite3_close(s->db);
871 s->db = NULL;
872 rc = SQLITE_CANTOPEN; /* trigger immutable fallback */
873 }
874 }
875 if (rc != SQLITE_OK) {
876 sqlite3_close(s->db); /* no-op if already NULL */
877 s->db = NULL;
878 /* A genuinely missing DB must return NULL without creating anything —
879 * only retry with the immutable URI when the file exists but could not
880 * be opened (the read-only-filesystem case). This also keeps the
881 * common "project not found" path to a single open attempt. */
882 if (!cbm_file_exists(db_path)) {
883 free(s);
884 return NULL;
885 }
886 char uri[ST_QUERY_URI_MAX];
887 if (!build_immutable_uri(db_path, uri, sizeof(uri))) {
888 free(s);
889 return NULL;
890 }
891 rc = sqlite3_open_v2(uri, &s->db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, NULL);
892 if (rc != SQLITE_OK) {
893 /* sqlite3_open_v2 allocates a handle even on failure — must close it. */
894 sqlite3_close(s->db);

Callers 15

quarantine_corrupt_storeFunction · 0.85
resolve_store_internalFunction · 0.85
db_internal_project_nameFunction · 0.85
handle_repo_infoFunction · 0.85
handle_adr_getFunction · 0.85
handle_project_healthFunction · 0.85
handle_layoutFunction · 0.85
cr_project_existsFunction · 0.85
cbm_artifact_exportFunction · 0.85
capture_existing_adrFunction · 0.85

Calls 6

cbm_path_for_file_apiFunction · 0.85
cbm_file_existsFunction · 0.85
build_immutable_uriFunction · 0.85
configure_pragmasFunction · 0.85
safe_str_freeFunction · 0.85
heap_strdupFunction · 0.70

Tested by 3

ui_adr_equalsFunction · 0.68
dv_persisted_nodesFunction · 0.68
cross_repo_count_edgesFunction · 0.68