/* * SQLite and its recovery extension in one translation unit, with a few * helpers for johnmcm.com's /sqlite-repair. Everything else the page needs * (queries, PRAGMA integrity_check, VACUUM INTO) goes through SQLite's own C * API, exported as it is. * * The recovery extension is included here, not built separately, so that * jm_recover_state() and jm_recover_table() can report which stage it is at * and which table it is writing, for the progress line. * * Long work is bounded by a progress handler (jm_watch) that asks the page's * worker, every few thousand SQLite instructions, whether its time limit has * passed or Stop was pressed; a non-zero answer makes the statement fail * with SQLITE_INTERRUPT. It is set on every connection as it opens (an * automatic extension), including the ones the recovery extension opens * itself, so SQL from the file's schema that the recovery runs in the new * database (a CREATE TABLE ... AS SELECT, say) is bounded too. * * The SQL download is meant to be run with the sqlite3 shell, which reads * its input a line at a time and takes a line starting with a dot as one of * its own commands (.shell runs a program). The schema text comes from the * file, so each statement is rewritten (jm_safe_stmt) to be exactly one * statement with no line break outside quotes before it reaches the worker. * * Written for johnmcm.com (2026); published at /source/sqlite-recover/. * SQLite itself is in the public domain. */ #include "sqlite3.c" #include "dbdata.c" #include "sqlite3recover.c" #include /* Asks the worker whether to stop (non-zero) and lets it post progress. */ EM_JS(int, jm_tick, (void), { return Module['jmTick'] ? Module['jmTick']() : 0; }); /* Hands one statement of an SQL dump to the worker. */ EM_JS(void, jm_sql_out, (const char *sql), { if (Module['jmSql']) Module['jmSql'](UTF8ToString(sql)); }); static int jm_progress(void *unused) { (void)unused; return jm_tick(); } /* Turns the time limit on (on != 0) or off for one connection. */ EMSCRIPTEN_KEEPALIVE void jm_watch(sqlite3 *db, int on) { if (db) sqlite3_progress_handler(db, on ? 4000 : 0, on ? jm_progress : 0, 0); } /* * Run for every connection as it opens: the time limit, and views and * triggers limited to functions without side effects. */ static int jm_auto(sqlite3 *db, char **err, const sqlite3_api_routines *api) { (void)err; (void)api; sqlite3_db_config(db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, 0, 0); jm_watch(db, 1); return SQLITE_OK; } EMSCRIPTEN_KEEPALIVE int jm_init(void) { int rc = sqlite3_initialize(); if (rc == SQLITE_OK) rc = sqlite3_auto_extension((void (*)(void))jm_auto); return rc; } /* * Opens a database: flags are SQLite's SQLITE_OPEN_* bits (the page passes * READONLY|URI for the file it was given, READWRITE|CREATE|URI otherwise). * Returns the handle (even on failure, so the worker can read * sqlite3_errmsg before closing it) and SQLite's result code in *rc. */ EMSCRIPTEN_KEEPALIVE sqlite3 *jm_open(const char *uri, int flags, int *rc) { sqlite3 *db = 0; *rc = sqlite3_open_v2(uri, &db, flags, 0); if (db) { sqlite3_extended_result_codes(db, 1); /* Views and triggers in the file may only use functions that have no side effects. (Defensive mode is left off: the recovery extension needs PRAGMA writable_schema.) */ sqlite3_db_config(db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, 0, 0); } return db; } /* * Starts recovering database db into a new database file at out. Rows that * cannot be placed in a table go to a table named laf (none if laf is 0). * Rowids are kept, so the recovered rows line up with the original's. */ EMSCRIPTEN_KEEPALIVE sqlite3_recover *jm_recover_db(sqlite3 *db, const char *out, const char *laf, int freelistCorrupt) { sqlite3_recover *p = sqlite3_recover_init(db, "main", out); if (!p) return 0; if (laf) sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void *)laf); int on = 1, fl = freelistCorrupt ? 1 : 0; sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, &on); sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT, &fl); return p; } /* * One statement of the SQL download, made safe to feed to the sqlite3 shell * a line at a time: cut after the first complete statement (the recovery * runs only the first one, but hands over the text as the file had it, so a * schema entry can carry more), with line breaks and comments outside quotes * turned into spaces, so that the shell can neither end the statement early * (a line holding only "go" or "/" does that in some versions) nor see a * line starting with a dot as one of its commands. Line breaks inside quoted * strings and names are kept: the shell reads them as part of the statement. * Returns the statement without its final semicolon, or 0 to leave it out * (text SQLite cannot read as one complete statement). */ static char *jm_safe_stmt(const char *z) { sqlite3_str *s = sqlite3_str_new(0); int complete = 0, bad = 0, semis = 0; i64 i = 0; while (z[i] && !complete && !bad) { int tt = 0; i64 n = sqlite3GetToken((const unsigned char *)z + i, &tt); if (n <= 0) { bad = 1; break; } if (tt == TK_SPACE) { int nl = 0; for (i64 k = i; k < i + n; k++) if (z[k] == '\n' || z[k] == '\r') nl = 1; if (nl) sqlite3_str_appendchar(s, 1, ' '); else sqlite3_str_append(s, z + i, (int)n); } else if (tt == TK_COMMENT) { sqlite3_str_appendchar(s, 1, ' '); } else if (tt == TK_ILLEGAL) { bad = 1; } else { sqlite3_str_append(s, z + i, (int)n); if (tt == TK_SEMI) { if (++semis > 1000) bad = 1; else if (sqlite3_complete(sqlite3_str_value(s))) complete = 1; } } i += n; } if (!bad && !complete) { sqlite3_str_appendchar(s, 1, ';'); complete = sqlite3_complete(sqlite3_str_value(s)); } if (bad || !complete || sqlite3_str_errcode(s)) { sqlite3_free(sqlite3_str_finish(s)); return 0; } char *out = sqlite3_str_finish(s); /* Drop the final semicolon and any spaces before it; the worker adds ";\n". */ i64 len = (i64)strlen(out); if (len && out[len - 1] == ';') out[--len] = 0; while (len && out[len - 1] == ' ') out[--len] = 0; if (!len) { sqlite3_free(out); return 0; } return out; } static int jm_sql_cb(void *ctx, const char *sql) { (void)ctx; char *safe = jm_safe_stmt(sql); if (safe) jm_sql_out(safe); sqlite3_free(safe); return SQLITE_OK; } /* Starts writing database db out as SQL statements, handed to the worker one at a time. */ EMSCRIPTEN_KEEPALIVE sqlite3_recover *jm_recover_sql(sqlite3 *db, const char *laf) { sqlite3_recover *p = sqlite3_recover_init_sql(db, "main", jm_sql_cb, 0); if (!p) return 0; if (laf) sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void *)laf); int on = 1; sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, &on); return p; } /* * One step of the recovery. The output connection exists after the first * step; it gets the same time limit as the input. */ EMSCRIPTEN_KEEPALIVE int jm_recover_step(sqlite3_recover *p) { int rc = sqlite3_recover_step(p); if (p->dbOut) jm_watch(p->dbOut, 1); return rc; } /* The stage it is at: 0 starting, 1 copying tables, 2-4 lost and found, 5 indexes, 6 done. */ EMSCRIPTEN_KEEPALIVE int jm_recover_state(sqlite3_recover *p) { return p ? p->eState : -1; } /* The table being copied, while the stage is 1. */ EMSCRIPTEN_KEEPALIVE const char *jm_recover_table(sqlite3_recover *p) { return (p && p->eState == RECOVER_STATE_WRITING && p->w1.pTab) ? p->w1.pTab->zTab : 0; } EMSCRIPTEN_KEEPALIVE sqlite3 *jm_recover_out(sqlite3_recover *p) { return p ? p->dbOut : 0; }