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

Function store_open_internal

src/store/store.c:678–735  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

676}
677
678static cbm_store_t *store_open_internal(const char *path, bool in_memory, bool create) {
679 cbm_store_t *s = calloc(CBM_ALLOC_ONE, sizeof(cbm_store_t));
680 if (!s) {
681 return NULL;
682 }
683
684 int flags = SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0);
685 if (in_memory) {
686 flags |= SQLITE_OPEN_MEMORY;
687 }
688
689 char open_path[4096];
690 const char *effective_path = path;
691 if (path && !in_memory) {
692 if (!cbm_path_for_file_api(path, open_path, sizeof(open_path))) {
693 free(s);
694 return NULL;
695 }
696 effective_path = open_path;
697 }
698 int rc = sqlite3_open_v2(effective_path, &s->db, flags, NULL);
699 if (rc != SQLITE_OK) {
700 sqlite3_close(s->db);
701 free(s);
702 return NULL;
703 }
704
705 if (path && !in_memory) {
706 s->db_path = heap_strdup(path);
707 }
708
709 /* Security: block ATTACH/DETACH to prevent file creation via SQL injection.
710 * The authorizer runs inside SQLite's query planner — no string-level bypass. */
711 sqlite3_set_authorizer(s->db, store_authorizer, NULL);
712
713 /* Register REGEXP function (SQLite doesn't have one built-in) */
714 sqlite3_create_function(s->db, "regexp", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
715 sqlite_regexp, NULL, NULL);
716 /* Case-insensitive variant for search with case_sensitive=false */
717 sqlite3_create_function(s->db, "iregexp", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
718 sqlite_iregexp, NULL, NULL);
719 /* Int8 cosine similarity for vector search */
720 sqlite3_create_function(s->db, "cbm_cosine_i8", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
721 NULL, sqlite_cosine_i8, NULL, NULL);
722 /* camelCase splitter for FTS5 BM25 indexing */
723 sqlite3_create_function(s->db, "cbm_camel_split", SKIP_ONE, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
724 NULL, sqlite_camel_split, NULL, NULL);
725
726 if (configure_pragmas(s, in_memory, false) != CBM_STORE_OK || init_schema(s) != CBM_STORE_OK ||
727 create_user_indexes(s) != CBM_STORE_OK) {
728 sqlite3_close(s->db);
729 safe_str_free(&s->db_path);
730 free(s);
731 return NULL;
732 }
733
734 return s;
735}

Callers 4

cbm_store_open_memoryFunction · 0.85
cbm_store_open_pathFunction · 0.85
cbm_store_openFunction · 0.85

Calls 6

cbm_path_for_file_apiFunction · 0.85
configure_pragmasFunction · 0.85
init_schemaFunction · 0.85
create_user_indexesFunction · 0.85
safe_str_freeFunction · 0.85
heap_strdupFunction · 0.70

Tested by

no test coverage detected