@@ -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+
618704cbm_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 );
0 commit comments