Skip to content

Commit 4a42285

Browse files
authored
Merge pull request DeusData#719 from DeusData/fix/readonly-query-open
fix(store): open query stores READONLY so queries never mutate dbs
2 parents ad422bc + 8ec7523 commit 4a42285

4 files changed

Lines changed: 432 additions & 13 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,14 +4843,35 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
48434843
/* ADRs are stored in the SQLite store (project_summaries), the SAME
48444844
* backend the UI /api/adr endpoints use — so writes via the MCP tool and
48454845
* the UI are visible to each other (#256). */
4846-
cbm_store_t *store = resolve_store(srv, project);
4847-
if (!store) {
4846+
cbm_store_t *resolved = resolve_store(srv, project);
4847+
if (!resolved) {
48484848
free(project);
48494849
free(mode_str);
48504850
free(content);
48514851
return cbm_mcp_text_result("project not found", true);
48524852
}
48534853

4854+
/* resolve_store opens file-backed projects READ-ONLY (query stores must
4855+
* not mutate the DB). manage_adr is the only resolve_store caller that
4856+
* WRITES, so it needs a writable handle. For a file-backed project open a
4857+
* dedicated read-write handle to the same DB file (the project is verified
4858+
* to exist via resolve_store, so cbm_store_open_path won't create a ghost
4859+
* DB). For an in-memory / embedded store (db_path == NULL) the resolved
4860+
* store is already writable — use it directly. */
4861+
cbm_store_t *store = resolved;
4862+
cbm_store_t *owned_rw = NULL;
4863+
const char *resolved_db_path = cbm_store_db_path(resolved);
4864+
if (resolved_db_path) {
4865+
owned_rw = cbm_store_open_path(resolved_db_path);
4866+
if (!owned_rw) {
4867+
free(project);
4868+
free(mode_str);
4869+
free(content);
4870+
return cbm_mcp_text_result("project not found", true);
4871+
}
4872+
store = owned_rw;
4873+
}
4874+
48544875
/* One-time migration: older versions wrote ADRs to a file at
48554876
* <root>/.codebase-memory/adr.md. If the store has no ADR yet but that
48564877
* legacy file exists, import it so nothing is lost on upgrade. */
@@ -4898,6 +4919,9 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
48984919
if (have_adr) {
48994920
cbm_store_adr_free(&adr);
49004921
}
4922+
if (owned_rw) {
4923+
cbm_store_close(owned_rw);
4924+
}
49014925
free(project);
49024926
free(mode_str);
49034927
free(content);

‎src/store/store.c‎

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ enum {
2626
ST_FOUND = -1,
2727
ST_BUF_16 = 16,
2828
ST_BUF_64 = 64,
29+
/* file: URI for the immutable read-only fallback. A path is at most
30+
* CBM_SZ_1K; percent-encoding can triple it, plus the "file://" prefix,
31+
* the "?immutable=1" suffix and a leading '/'. */
32+
ST_QUERY_URI_MAX = 3 * 1024 + 64,
2933
ST_GROWTH = 2,
3034
ST_MAX_DEGREE = 8192,
3135
ST_HALF_SEC = 500000,
@@ -322,7 +326,16 @@ int64_t cbm_store_resolve_mmap_size(void) {
322326
return (int64_t)parsed;
323327
}
324328

325-
static int configure_pragmas(cbm_store_t *s, bool in_memory) {
329+
/* Configure connection pragmas.
330+
* in_memory — :memory: DB (synchronous OFF, no journal file).
331+
* read_only — query-only connection opened SQLITE_OPEN_READONLY. Runs
332+
* ONLY non-writing pragmas (foreign_keys, temp_store,
333+
* busy_timeout, mmap_size) and SKIPS journal_mode=WAL,
334+
* wal_checkpoint and synchronous — those WRITE to the DB/WAL
335+
* and would (a) mutate the DB on every read query and (b)
336+
* fail on a read-only DB file / filesystem.
337+
* in_memory and read_only are mutually exclusive. */
338+
static int configure_pragmas(cbm_store_t *s, bool in_memory, bool read_only) {
326339
int rc;
327340
rc = exec_sql(s, "PRAGMA foreign_keys = ON;");
328341
if (rc != CBM_STORE_OK) {
@@ -335,6 +348,16 @@ static int configure_pragmas(cbm_store_t *s, bool in_memory) {
335348

336349
if (in_memory) {
337350
rc = exec_sql(s, "PRAGMA synchronous = OFF;");
351+
} else if (read_only) {
352+
/* Non-writing pragmas only — see the function comment. */
353+
rc = exec_sql(s, "PRAGMA busy_timeout = 10000;");
354+
if (rc != CBM_STORE_OK) {
355+
return rc;
356+
}
357+
char mmap_sql[ST_BUF_64];
358+
snprintf(mmap_sql, sizeof(mmap_sql), "PRAGMA mmap_size = %lld;",
359+
(long long)cbm_store_resolve_mmap_size());
360+
rc = exec_sql(s, mmap_sql);
338361
} else {
339362
rc = exec_sql(s, "PRAGMA busy_timeout = 10000;");
340363
if (rc != CBM_STORE_OK) {
@@ -593,7 +616,7 @@ static cbm_store_t *store_open_internal(const char *path, bool in_memory) {
593616
sqlite3_create_function(s->db, "cbm_camel_split", SKIP_ONE, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
594617
NULL, sqlite_camel_split, NULL, NULL);
595618

596-
if (configure_pragmas(s, in_memory) != CBM_STORE_OK || init_schema(s) != CBM_STORE_OK ||
619+
if (configure_pragmas(s, in_memory, false) != CBM_STORE_OK || init_schema(s) != CBM_STORE_OK ||
597620
create_user_indexes(s) != CBM_STORE_OK) {
598621
sqlite3_close(s->db);
599622
safe_str_free(&s->db_path);
@@ -615,6 +638,69 @@ cbm_store_t *cbm_store_open_path(const char *db_path) {
615638
return store_open_internal(db_path, false);
616639
}
617640

641+
const char *cbm_store_db_path(const cbm_store_t *s) {
642+
return s ? s->db_path : NULL;
643+
}
644+
645+
/* Build a SQLite "file:" URI with immutable=1 from a filesystem path.
646+
* immutable=1 bypasses WAL and locking and reads the main DB file directly —
647+
* used only as a fallback for read-only filesystems where the wal-index
648+
* (-shm) cannot be created. URI-special characters are percent-encoded.
649+
* Windows backslashes are normalized to '/', and a leading '/' is inserted
650+
* for drive-letter paths so the drive is not parsed as a URI authority.
651+
* Returns false if the output buffer is too small. */
652+
static bool build_immutable_uri(const char *path, char *out, size_t out_sz) {
653+
static const char PREFIX[] = "file://";
654+
static const char SUFFIX[] = "?immutable=1";
655+
static const char HEX[] = "0123456789ABCDEF";
656+
size_t prefix_len = sizeof(PREFIX) - 1;
657+
size_t suffix_len = sizeof(SUFFIX) - 1;
658+
if (prefix_len + 1 > out_sz) {
659+
return false;
660+
}
661+
memcpy(out, PREFIX, prefix_len);
662+
size_t pos = prefix_len;
663+
664+
/* Ensure the path component begins with '/' (POSIX absolute paths already
665+
* do; Windows "C:\..." gets a leading '/' -> "/C:/..."). */
666+
if (path[0] != '/') {
667+
if (pos + 1 >= out_sz) {
668+
return false;
669+
}
670+
out[pos++] = '/';
671+
}
672+
673+
for (const unsigned char *p = (const unsigned char *)path; *p != '\0'; p++) {
674+
unsigned char c = *p;
675+
if (c == '\\') {
676+
c = '/'; /* normalize Windows separators */
677+
}
678+
bool safe = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
679+
c == '/' || c == '.' || c == '-' || c == '_' || c == '~' || c == ':';
680+
if (safe) {
681+
if (pos + 1 >= out_sz) {
682+
return false;
683+
}
684+
out[pos++] = (char)c;
685+
} else {
686+
if (pos + 3 >= out_sz) {
687+
return false;
688+
}
689+
out[pos++] = '%';
690+
out[pos++] = HEX[(c >> 4) & 0xF];
691+
out[pos++] = HEX[c & 0xF];
692+
}
693+
}
694+
695+
if (pos + suffix_len + 1 > out_sz) {
696+
return false;
697+
}
698+
memcpy(out + pos, SUFFIX, suffix_len);
699+
pos += suffix_len;
700+
out[pos] = '\0';
701+
return true;
702+
}
703+
618704
cbm_store_t *cbm_store_open_path_query(const char *db_path) {
619705
if (!db_path) {
620706
return NULL;
@@ -625,13 +711,52 @@ cbm_store_t *cbm_store_open_path_query(const char *db_path) {
625711
return NULL;
626712
}
627713

628-
/* Open read-write but do NOT create — returns SQLITE_CANTOPEN if absent. */
629-
int rc = sqlite3_open_v2(db_path, &s->db, SQLITE_OPEN_READWRITE, NULL);
714+
/* Query tools open the project DB READ-ONLY: a read query must never
715+
* mutate the DB (the previous READWRITE open + WAL write-pragmas did),
716+
* and must work on a read-only DB file / filesystem.
717+
*
718+
* Try a plain READONLY open first — on a normal writable filesystem this
719+
* reads WAL frames correctly via the -shm wal-index. SQLite opens lazily,
720+
* so a read-only-filesystem failure (cannot create -shm for a WAL-mode
721+
* DB) surfaces on first access, not at open time; we probe with a trivial
722+
* read to force it. If the probe fails, retry once with an immutable URI
723+
* that bypasses WAL and reads the main DB file directly.
724+
*
725+
* No SQLITE_OPEN_CREATE on either path — a missing DB must return NULL
726+
* (no ghost .db for unknown/unindexed projects). */
727+
int rc = sqlite3_open_v2(db_path, &s->db, SQLITE_OPEN_READONLY, NULL);
728+
if (rc == SQLITE_OK) {
729+
/* Force first DB access so a read-only-FS WAL failure surfaces now. */
730+
if (sqlite3_exec(s->db, "SELECT 1 FROM sqlite_master LIMIT 1;", NULL, NULL, NULL) !=
731+
SQLITE_OK) {
732+
sqlite3_close(s->db);
733+
s->db = NULL;
734+
rc = SQLITE_CANTOPEN; /* trigger immutable fallback */
735+
}
736+
}
630737
if (rc != SQLITE_OK) {
631-
/* sqlite3_open_v2 allocates a handle even on failure — must close it. */
632-
sqlite3_close(s->db);
633-
free(s);
634-
return NULL;
738+
sqlite3_close(s->db); /* no-op if already NULL */
739+
s->db = NULL;
740+
/* A genuinely missing DB must return NULL without creating anything —
741+
* only retry with the immutable URI when the file exists but could not
742+
* be opened (the read-only-filesystem case). This also keeps the
743+
* common "project not found" path to a single open attempt. */
744+
if (!cbm_file_exists(db_path)) {
745+
free(s);
746+
return NULL;
747+
}
748+
char uri[ST_QUERY_URI_MAX];
749+
if (!build_immutable_uri(db_path, uri, sizeof(uri))) {
750+
free(s);
751+
return NULL;
752+
}
753+
rc = sqlite3_open_v2(uri, &s->db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, NULL);
754+
if (rc != SQLITE_OK) {
755+
/* sqlite3_open_v2 allocates a handle even on failure — must close it. */
756+
sqlite3_close(s->db);
757+
free(s);
758+
return NULL;
759+
}
635760
}
636761

637762
s->db_path = heap_strdup(db_path);
@@ -649,7 +774,7 @@ cbm_store_t *cbm_store_open_path_query(const char *db_path) {
649774
sqlite3_create_function(s->db, "cbm_camel_split", SKIP_ONE, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
650775
NULL, sqlite_camel_split, NULL, NULL);
651776

652-
if (configure_pragmas(s, false) != CBM_STORE_OK) {
777+
if (configure_pragmas(s, false, true) != CBM_STORE_OK) {
653778
sqlite3_close(s->db);
654779
safe_str_free(&s->db_path);
655780
free(s);

‎src/store/store.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,16 @@ cbm_store_t *cbm_store_open_memory(void);
201201
/* Open a file-backed database at the given path. Creates if needed. */
202202
cbm_store_t *cbm_store_open_path(const char *db_path);
203203

204-
/* Open an existing file-backed database for querying only (no SQLITE_OPEN_CREATE).
205-
* Returns NULL if the file does not exist — never creates a new .db file. */
204+
/* Open an existing file-backed database for querying only. Opened READ-ONLY
205+
* (no SQLITE_OPEN_CREATE, no write pragmas) so queries never mutate the DB and
206+
* work on a read-only file / filesystem. Returns NULL if the file does not
207+
* exist — never creates a new .db file. */
206208
cbm_store_t *cbm_store_open_path_query(const char *db_path);
207209

210+
/* On-disk path of a file-backed store, or NULL for an in-memory (:memory:)
211+
* store. The returned pointer is owned by the store. */
212+
const char *cbm_store_db_path(const cbm_store_t *s);
213+
208214
/* Check database integrity. Returns true if the DB passes basic sanity checks
209215
* (projects table has correct types, no corruption indicators).
210216
* Returns false if corruption is detected — caller should delete and re-index. */

0 commit comments

Comments
 (0)