Query tools open the project DB READ-ONLY: a read query must never * mutate the DB (the previous READWRITE open + WAL write-pragmas did), * and must work on a read-only DB file / filesystem. * * Try a plain READONLY open first — on a normal writable filesystem this * reads WAL frames correctly via the -shm wal-index. SQLite opens lazily, * so a read-only-filesystem failure (cannot create -sh
| 1122 | * (no ghost .db for unknown/unindexed projects). Returns false with s->db |
| 1123 | * closed when the DB cannot be opened. */ |
| 1124 | static bool query_open_first_access(cbm_store_t *s, const char *db_path) { |
| 1125 | char open_path[4096]; |
| 1126 | if (!cbm_path_for_file_api(db_path, open_path, sizeof(open_path))) { |
| 1127 | return false; |
| 1128 | } |
| 1129 | int rc = sqlite3_open_v2(open_path, &s->db, SQLITE_OPEN_READONLY, NULL); |
| 1130 | if (rc == SQLITE_OK) { |
| 1131 | /* Force first DB access so a read-only-FS WAL failure surfaces now. */ |
| 1132 | if (sqlite3_exec(s->db, "SELECT 1 FROM sqlite_master LIMIT 1;", NULL, NULL, NULL) != |
| 1133 | SQLITE_OK) { |
| 1134 | sqlite3_close(s->db); |
| 1135 | s->db = NULL; |
| 1136 | rc = SQLITE_CANTOPEN; /* trigger immutable fallback */ |
| 1137 | } |
| 1138 | } |
| 1139 | if (rc != SQLITE_OK) { |
| 1140 | sqlite3_close(s->db); /* no-op if already NULL */ |
| 1141 | s->db = NULL; |
| 1142 | /* A genuinely missing DB must return NULL without creating anything — |
| 1143 | * only retry with the immutable URI when the file exists but could not |
| 1144 | * be opened (the read-only-filesystem case). This also keeps the |
| 1145 | * common "project not found" path to a single open attempt. */ |
| 1146 | if (!cbm_file_exists(db_path)) { |
| 1147 | return false; |
| 1148 | } |
| 1149 | char uri[ST_QUERY_URI_MAX]; |
| 1150 | if (!build_immutable_uri(db_path, uri, sizeof(uri))) { |
| 1151 | return false; |
| 1152 | } |
| 1153 | rc = sqlite3_open_v2(uri, &s->db, SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, NULL); |
| 1154 | if (rc != SQLITE_OK) { |
| 1155 | /* sqlite3_open_v2 allocates a handle even on failure — must close it. */ |
| 1156 | sqlite3_close(s->db); |
| 1157 | s->db = NULL; |
| 1158 | return false; |
| 1159 | } |
| 1160 | } |
| 1161 | return true; |
| 1162 | } |
| 1163 | |
| 1164 | #ifdef CBM_ENABLE_TEST_SEAMS |
| 1165 | static void (*g_first_access_hook)(void *ctx) = NULL; |
no test coverage detected