Debian Codegraph @

ext/recover/sqlite3recover.c

raw
1 /*
2 ** 2022-08-27
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 */
14
15
16 #include "sqlite3recover.h"
17 #include <assert.h>
18 #include <string.h>
19
20 #ifndef SQLITE_OMIT_VIRTUALTABLE
21
22 /*
23 ** Declaration for public API function in file dbdata.c. This may be called
24 ** with NULL as the final two arguments to register the sqlite_dbptr and
25 ** sqlite_dbdata virtual tables with a database handle.
26 */
27 #ifdef _WIN32
28 __declspec(dllexport)
29 #endif
30 int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*);
31
32 typedef unsigned int u32;
33 typedef unsigned char u8;
34 typedef sqlite3_int64 i64;
35
36 /*
37 ** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
38 ** to avoid complaints from -fsanitize=strict-bounds.
39 */
40 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
41 # define FLEXARRAY
42 #else
43 # define FLEXARRAY 1
44 #endif
45
46 typedef struct RecoverTable RecoverTable;
47 typedef struct RecoverColumn RecoverColumn;
48
49 /*
50 ** When recovering rows of data that can be associated with table
51 ** definitions recovered from the sqlite_schema table, each table is
52 ** represented by an instance of the following object.
53 **
54 ** iRoot:
55 ** The root page in the original database. Not necessarily (and usually
56 ** not) the same in the recovered database.
57 **
58 ** zTab:
59 ** Name of the table.
60 **
61 ** nCol/aCol[]:
62 ** aCol[] is an array of nCol columns. In the order in which they appear
63 ** in the table.
64 **
65 ** bIntkey:
66 ** Set to true for intkey tables, false for WITHOUT ROWID.
67 **
68 ** iRowidBind:
69 ** Each column in the aCol[] array has associated with it the index of
70 ** the bind parameter its values will be bound to in the INSERT statement
71 ** used to construct the output database. If the table does has a rowid
72 ** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the
73 ** index of the bind paramater to which the rowid value should be bound.
74 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
75 ** KEY column, then the rowid value should be bound to the index associated
76 ** with the column.
77 **
78 ** pNext:
79 ** All RecoverTable objects used by the recovery operation are allocated
80 ** and populated as part of creating the recovered database schema in
81 ** the output database, before any non-schema data are recovered. They
82 ** are then stored in a singly-linked list linked by this variable beginning
83 ** at sqlite3_recover.pTblList.
84 */
85 struct RecoverTable {
86 u32 iRoot; /* Root page in original database */
87 char *zTab; /* Name of table */
88 int nCol; /* Number of columns in table */
89 RecoverColumn *aCol; /* Array of columns */
90 int bIntkey; /* True for intkey, false for without rowid */
91 int iRowidBind; /* If >0, bind rowid to INSERT here */
92 RecoverTable *pNext;
93 };
94
95 /*
96 ** Each database column is represented by an instance of the following object
97 ** stored in the RecoverTable.aCol[] array of the associated table.
98 **
99 ** iField:
100 ** The index of the associated field within database records. Or -1 if
101 ** there is no associated field (e.g. for virtual generated columns).
102 **
103 ** iBind:
104 ** The bind index of the INSERT statement to bind this columns values
105 ** to. Or 0 if there is no such index (iff (iField<0)).
106 **
107 ** bIPK:
108 ** True if this is the INTEGER PRIMARY KEY column.
109 **
110 ** zCol:
111 ** Name of column.
112 **
113 ** eHidden:
114 ** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each).
115 */
116 struct RecoverColumn {
117 int iField; /* Field in record on disk */
118 int iBind; /* Binding to use in INSERT */
119 int bIPK; /* True for IPK column */
120 char *zCol;
121 int eHidden;
122 };
123
124 #define RECOVER_EHIDDEN_NONE 0 /* Normal database column */
125 #define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */
126 #define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */
127 #define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */
128
129 /*
130 ** Bitmap object used to track pages in the input database. Allocated
131 ** and manipulated only by the following functions:
132 **
133 ** recoverBitmapAlloc()
134 ** recoverBitmapFree()
135 ** recoverBitmapSet()
136 ** recoverBitmapQuery()
137 **
138 ** nPg:
139 ** Largest page number that may be stored in the bitmap. The range
140 ** of valid keys is 1 to nPg, inclusive.
141 **
142 ** aElem[]:
143 ** Array large enough to contain a bit for each key. For key value
144 ** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32].
145 ** In other words, the following is true if bit iKey is set, or
146 ** false if it is clear:
147 **
148 ** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0
149 */
150 typedef struct RecoverBitmap RecoverBitmap;
151 struct RecoverBitmap {
152 i64 nPg; /* Size of bitmap */
153 u32 aElem[FLEXARRAY]; /* Array of 32-bit bitmasks */
154 };
155
156 /* Size in bytes of a RecoverBitmap object sufficient to cover 32 pages */
157 #define SZ_RECOVERBITMAP_32 (16)
158
159 /*
160 ** State variables (part of the sqlite3_recover structure) used while
161 ** recovering data for tables identified in the recovered schema (state
162 ** RECOVER_STATE_WRITING).
163 */
164 typedef struct RecoverStateW1 RecoverStateW1;
165 struct RecoverStateW1 {
166 sqlite3_stmt *pTbls;
167 sqlite3_stmt *pSel;
168 sqlite3_stmt *pInsert;
169 int nInsert;
170
171 RecoverTable *pTab; /* Table currently being written */
172 int nMax; /* Max column count in any schema table */
173 sqlite3_value **apVal; /* Array of nMax values */
174 int nVal; /* Number of valid entries in apVal[] */
175 int bHaveRowid;
176 i64 iRowid;
177 i64 iPrevPage;
178 int iPrevCell;
179 };
180
181 /*
182 ** State variables (part of the sqlite3_recover structure) used while
183 ** recovering data destined for the lost and found table (states
184 ** RECOVER_STATE_LOSTANDFOUND[123]).
185 */
186 typedef struct RecoverStateLAF RecoverStateLAF;
187 struct RecoverStateLAF {
188 RecoverBitmap *pUsed;
189 i64 nPg; /* Size of db in pages */
190 sqlite3_stmt *pAllAndParent;
191 sqlite3_stmt *pMapInsert;
192 sqlite3_stmt *pMaxField;
193 sqlite3_stmt *pUsedPages;
194 sqlite3_stmt *pFindRoot;
195 sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */
196 sqlite3_stmt *pAllPage;
197 sqlite3_stmt *pPageData;
198 sqlite3_value **apVal;
199 int nMaxField;
200 };
201
202 /*
203 ** Main recover handle structure.
204 */
205 struct sqlite3_recover {
206 /* Copies of sqlite3_recover_init[_sql]() parameters */
207 sqlite3 *dbIn; /* Input database */
208 char *zDb; /* Name of input db ("main" etc.) */
209 char *zUri; /* URI for output database */
210 void *pSqlCtx; /* SQL callback context */
211 int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
212
213 /* Values configured by sqlite3_recover_config() */
214 char *zStateDb; /* State database to use (or NULL) */
215 char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
216 int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */
217 int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */
218 int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
219
220 int pgsz;
221 int detected_pgsz;
222 int nReserve;
223 u8 *pPage1Disk;
224 u8 *pPage1Cache;
225
226 /* Error code and error message */
227 int errCode; /* For sqlite3_recover_errcode() */
228 char *zErrMsg; /* For sqlite3_recover_errmsg() */
229
230 int eState;
231 int bCloseTransaction;
232
233 /* Variables used with eState==RECOVER_STATE_WRITING */
234 RecoverStateW1 w1;
235
236 /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */
237 RecoverStateLAF laf;
238
239 /* Fields used within sqlite3_recover_run() */
240 sqlite3 *dbOut; /* Output database */
241 sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */
242 RecoverTable *pTblList; /* List of tables recovered from schema */
243 };
244
245 /*
246 ** The various states in which an sqlite3_recover object may exist:
247 **
248 ** RECOVER_STATE_INIT:
249 ** The object is initially created in this state. sqlite3_recover_step()
250 ** has yet to be called. This is the only state in which it is permitted
251 ** to call sqlite3_recover_config().
252 **
253 ** RECOVER_STATE_WRITING:
254 **
255 ** RECOVER_STATE_LOSTANDFOUND1:
256 ** State to populate the bitmap of pages used by other tables or the
257 ** database freelist.
258 **
259 ** RECOVER_STATE_LOSTANDFOUND2:
260 ** Populate the recovery.map table - used to figure out a "root" page
261 ** for each lost page from in the database from which records are
262 ** extracted.
263 **
264 ** RECOVER_STATE_LOSTANDFOUND3:
265 ** Populate the lost-and-found table itself.
266 */
267 #define RECOVER_STATE_INIT 0
268 #define RECOVER_STATE_WRITING 1
269 #define RECOVER_STATE_LOSTANDFOUND1 2
270 #define RECOVER_STATE_LOSTANDFOUND2 3
271 #define RECOVER_STATE_LOSTANDFOUND3 4
272 #define RECOVER_STATE_SCHEMA2 5
273 #define RECOVER_STATE_DONE 6
274
275
276 /*
277 ** Global variables used by this extension.
278 */
279 typedef struct RecoverGlobal RecoverGlobal;
280 struct RecoverGlobal {
281 const sqlite3_io_methods *pMethods;
282 sqlite3_recover *p;
283 };
284 static RecoverGlobal recover_g;
285
286 /*
287 ** Use this static SQLite mutex to protect the globals during the
288 ** first call to sqlite3_recover_step().
289 */
290 #define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2
291
292
293 /*
294 ** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid).
295 */
296 #define RECOVER_ROWID_DEFAULT 1
297
298 /*
299 ** Mutex handling:
300 **
301 ** recoverEnterMutex() - Enter the recovery mutex
302 ** recoverLeaveMutex() - Leave the recovery mutex
303 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
304 */
305 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
306 # define recoverEnterMutex()
307 # define recoverLeaveMutex()
308 #else
309 static void recoverEnterMutex(void){
310 sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
311 }
312 static void recoverLeaveMutex(void){
313 sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID));
314 }
315 #endif
316 #if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG)
317 static void recoverAssertMutexHeld(void){
318 assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) );
319 }
320 #else
321 # define recoverAssertMutexHeld()
322 #endif
323
324
325 /*
326 ** Like strlen(). But handles NULL pointer arguments.
327 */
328 static int recoverStrlen(const char *zStr){
329 if( zStr==0 ) return 0;
330 return (int)(strlen(zStr)&0x7fffffff);
331 }
332
333 /*
334 ** This function is a no-op if the recover handle passed as the first
335 ** argument already contains an error (if p->errCode!=SQLITE_OK).
336 **
337 ** Otherwise, an attempt is made to allocate, zero and return a buffer nByte
338 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
339 ** if an OOM error occurs, NULL is returned and the handle error code
340 ** (p->errCode) set to SQLITE_NOMEM.
341 */
342 static void *recoverMalloc(sqlite3_recover *p, i64 nByte){
343 void *pRet = 0;
344 assert( nByte>0 );
345 if( p->errCode==SQLITE_OK ){
346 pRet = sqlite3_malloc64(nByte);
347 if( pRet ){
348 memset(pRet, 0, nByte);
349 }else{
350 p->errCode = SQLITE_NOMEM;
351 }
352 }
353 return pRet;
354 }
355
356 /*
357 ** Set the error code and error message for the recover handle passed as
358 ** the first argument. The error code is set to the value of parameter
359 ** errCode.
360 **
361 ** Parameter zFmt must be a printf() style formatting string. The handle
362 ** error message is set to the result of using any trailing arguments for
363 ** parameter substitutions in the formatting string.
364 **
365 ** For example:
366 **
367 ** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename);
368 */
369 static int recoverError(
370 sqlite3_recover *p,
371 int errCode,
372 const char *zFmt, ...
373 ){
374 char *z = 0;
375 va_list ap;
376 va_start(ap, zFmt);
377 if( zFmt ){
378 z = sqlite3_vmprintf(zFmt, ap);
379 }
380 va_end(ap);
381 sqlite3_free(p->zErrMsg);
382 p->zErrMsg = z;
383 p->errCode = errCode;
384 return errCode;
385 }
386
387
388 /*
389 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
390 ** In this case it returns NULL.
391 **
392 ** Otherwise, an attempt is made to allocate and return a bitmap object
393 ** large enough to store a bit for all page numbers between 1 and nPg,
394 ** inclusive. The bitmap is initially zeroed.
395 */
396 static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){
397 int nElem = (nPg+1+31) / 32;
398 int nByte = SZ_RECOVERBITMAP_32 + nElem*sizeof(u32);
399 RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte);
400
401 if( pRet ){
402 pRet->nPg = nPg;
403 }
404 return pRet;
405 }
406
407 /*
408 ** Free a bitmap object allocated by recoverBitmapAlloc().
409 */
410 static void recoverBitmapFree(RecoverBitmap *pMap){
411 sqlite3_free(pMap);
412 }
413
414 /*
415 ** Set the bit associated with page iPg in bitvec pMap.
416 */
417 static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){
418 if( iPg<=pMap->nPg ){
419 int iElem = (iPg / 32);
420 int iBit = (iPg % 32);
421 pMap->aElem[iElem] |= (((u32)1) << iBit);
422 }
423 }
424
425 /*
426 ** Query bitmap object pMap for the state of the bit associated with page
427 ** iPg. Return 1 if it is set, or 0 otherwise.
428 */
429 static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){
430 int ret = 1;
431 if( iPg<=pMap->nPg && iPg>0 ){
432 int iElem = (iPg / 32);
433 int iBit = (iPg % 32);
434 ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0;
435 }
436 return ret;
437 }
438
439 /*
440 ** Set the recover handle error to the error code and message returned by
441 ** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database
442 ** handle db.
443 */
444 static int recoverDbError(sqlite3_recover *p, sqlite3 *db){
445 return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db));
446 }
447
448 /*
449 ** This function is a no-op if recover handle p already contains an error
450 ** (if p->errCode!=SQLITE_OK).
451 **
452 ** Otherwise, it attempts to prepare the SQL statement in zSql against
453 ** database handle db. If successful, the statement handle is returned.
454 ** Or, if an error occurs, NULL is returned and an error left in the
455 ** recover handle.
456 */
457 static sqlite3_stmt *recoverPrepare(
458 sqlite3_recover *p,
459 sqlite3 *db,
460 const char *zSql
461 ){
462 sqlite3_stmt *pStmt = 0;
463 if( p->errCode==SQLITE_OK ){
464 if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){
465 recoverDbError(p, db);
466 }
467 }
468 return pStmt;
469 }
470
471 /*
472 ** This function is a no-op if recover handle p already contains an error
473 ** (if p->errCode!=SQLITE_OK).
474 **
475 ** Otherwise, argument zFmt is used as a printf() style format string,
476 ** along with any trailing arguments, to create an SQL statement. This
477 ** SQL statement is prepared against database handle db and, if successful,
478 ** the statment handle returned. Or, if an error occurs - either during
479 ** the printf() formatting or when preparing the resulting SQL - an
480 ** error code and message are left in the recover handle.
481 */
482 static sqlite3_stmt *recoverPreparePrintf(
483 sqlite3_recover *p,
484 sqlite3 *db,
485 const char *zFmt, ...
486 ){
487 sqlite3_stmt *pStmt = 0;
488 if( p->errCode==SQLITE_OK ){
489 va_list ap;
490 char *z;
491 va_start(ap, zFmt);
492 z = sqlite3_vmprintf(zFmt, ap);
493 va_end(ap);
494 if( z==0 ){
495 p->errCode = SQLITE_NOMEM;
496 }else{
497 pStmt = recoverPrepare(p, db, z);
498 sqlite3_free(z);
499 }
500 }
501 return pStmt;
502 }
503
504 /*
505 ** Reset SQLite statement handle pStmt. If the call to sqlite3_reset()
506 ** indicates that an error occurred, and there is not already an error
507 ** in the recover handle passed as the first argument, set the error
508 ** code and error message appropriately.
509 **
510 ** This function returns a copy of the statement handle pointer passed
511 ** as the second argument.
512 */
513 static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){
514 int rc = sqlite3_reset(pStmt);
515 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){
516 recoverDbError(p, sqlite3_db_handle(pStmt));
517 }
518 return pStmt;
519 }
520
521 /*
522 ** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset()
523 ** indicates that an error occurred, and there is not already an error
524 ** in the recover handle passed as the first argument, set the error
525 ** code and error message appropriately.
526 */
527 static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){
528 sqlite3 *db = sqlite3_db_handle(pStmt);
529 int rc = sqlite3_finalize(pStmt);
530 if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){
531 recoverDbError(p, db);
532 }
533 }
534
535 /*
536 ** Run a single SQL statement in zSql. If zSql contains two or more
537 ** SQL statements separated by ';', only the first is run.
538 **
539 ** Return the sqlite3_finalizer() or sqlite3_prepare() result code
540 ** from running the zSql statement.
541 */
542 static int recoverOneStmt(sqlite3 *db, const char *zSql){
543 sqlite3_stmt *pStmt = 0;
544 int rc;
545 if( zSql==0 ) return SQLITE_OK;
546 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
547 if( rc ){
548 sqlite3_finalize(pStmt);
549 return rc;
550 }
551 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
552 return sqlite3_finalize(pStmt);
553 }
554
555 /*
556 ** This function is a no-op if recover handle p already contains an error
557 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
558 ** case.
559 **
560 ** Otherwise, execute a single SQL statment in zSql. Even if zSql contains
561 ** two or more SQL statements separated by ';', only execute the first one.
562 ** If successful, return SQLITE_OK. Or, if an error occurs, leave an error
563 ** code and message in the recover handle and return a copy of the error code.
564 */
565 static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){
566 if( p->errCode==SQLITE_OK ){
567 int rc = recoverOneStmt(db, zSql);
568 if( rc ){
569 recoverDbError(p, db);
570 }
571 }
572 return p->errCode;
573 }
574
575 /*
576 ** Bind the value pVal to parameter iBind of statement pStmt. Leave an
577 ** error in the recover handle passed as the first argument if an error
578 ** (e.g. an OOM) occurs.
579 */
580 static void recoverBindValue(
581 sqlite3_recover *p,
582 sqlite3_stmt *pStmt,
583 int iBind,
584 sqlite3_value *pVal
585 ){
586 if( p->errCode==SQLITE_OK ){
587 int rc = sqlite3_bind_value(pStmt, iBind, pVal);
588 if( rc ) recoverError(p, rc, 0);
589 }
590 }
591
592 /*
593 ** This function is a no-op if recover handle p already contains an error
594 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
595 **
596 ** Otherwise, an attempt is made to interpret zFmt as a printf() style
597 ** formatting string and the result of using the trailing arguments for
598 ** parameter substitution with it written into a buffer obtained from
599 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
600 ** It is the responsibility of the caller to eventually free the buffer
601 ** using sqlite3_free().
602 **
603 ** Or, if an error occurs, an error code and message is left in the recover
604 ** handle and NULL returned.
605 */
606 static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){
607 va_list ap;
608 char *z;
609 va_start(ap, zFmt);
610 z = sqlite3_vmprintf(zFmt, ap);
611 va_end(ap);
612 if( p->errCode==SQLITE_OK ){
613 if( z==0 ) p->errCode = SQLITE_NOMEM;
614 }else{
615 sqlite3_free(z);
616 z = 0;
617 }
618 return z;
619 }
620
621 /*
622 ** This function is a no-op if recover handle p already contains an error
623 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
624 **
625 ** Otherwise, execute "PRAGMA page_count" against the input database. If
626 ** successful, return the integer result. Or, if an error occurs, leave an
627 ** error code and error message in the sqlite3_recover handle and return
628 ** zero.
629 */
630 static i64 recoverPageCount(sqlite3_recover *p){
631 i64 nPg = 0;
632 if( p->errCode==SQLITE_OK ){
633 sqlite3_stmt *pStmt = 0;
634 pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb);
635 if( pStmt ){
636 sqlite3_step(pStmt);
637 nPg = sqlite3_column_int64(pStmt, 0);
638 }
639 recoverFinalize(p, pStmt);
640 }
641 return nPg;
642 }
643
644 /*
645 ** Implementation of SQL scalar function "read_i32". The first argument to
646 ** this function must be a blob. The second a non-negative integer. This
647 ** function reads and returns a 32-bit big-endian integer from byte
648 ** offset (4*<arg2>) of the blob.
649 **
650 ** SELECT read_i32(<blob>, <idx>)
651 */
652 static void recoverReadI32(
653 sqlite3_context *context,
654 int argc,
655 sqlite3_value **argv
656 ){
657 const unsigned char *pBlob;
658 int nBlob;
659 int iInt;
660
661 assert( argc==2 );
662 nBlob = sqlite3_value_bytes(argv[0]);
663 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
664 iInt = sqlite3_value_int(argv[1]) & 0xFFFF;
665
666 if( (iInt+1)*4<=nBlob ){
667 const unsigned char *a = &pBlob[iInt*4];
668 i64 iVal = ((i64)a[0]<<24)
669 + ((i64)a[1]<<16)
670 + ((i64)a[2]<< 8)
671 + ((i64)a[3]<< 0);
672 sqlite3_result_int64(context, iVal);
673 }
674 }
675
676 /*
677 ** Implementation of SQL scalar function "page_is_used". This function
678 ** is used as part of the procedure for locating orphan rows for the
679 ** lost-and-found table, and it depends on those routines having populated
680 ** the sqlite3_recover.laf.pUsed variable.
681 **
682 ** The only argument to this function is a page-number. It returns true
683 ** if the page has already been used somehow during data recovery, or false
684 ** otherwise.
685 **
686 ** SELECT page_is_used(<pgno>);
687 */
688 static void recoverPageIsUsed(
689 sqlite3_context *pCtx,
690 int nArg,
691 sqlite3_value **apArg
692 ){
693 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
694 i64 pgno = sqlite3_value_int64(apArg[0]);
695 assert( nArg==1 );
696 sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno));
697 }
698
699 /*
700 ** The implementation of a user-defined SQL function invoked by the
701 ** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages
702 ** of the database being recovered.
703 **
704 ** This function always takes a single integer argument. If the argument
705 ** is zero, then the value returned is the number of pages in the db being
706 ** recovered. If the argument is greater than zero, it is a page number.
707 ** The value returned in this case is an SQL blob containing the data for
708 ** the identified page of the db being recovered. e.g.
709 **
710 ** SELECT getpage(0); -- return number of pages in db
711 ** SELECT getpage(4); -- return page 4 of db as a blob of data
712 */
713 static void recoverGetPage(
714 sqlite3_context *pCtx,
715 int nArg,
716 sqlite3_value **apArg
717 ){
718 sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx);
719 i64 pgno = sqlite3_value_int64(apArg[0]);
720 sqlite3_stmt *pStmt = 0;
721
722 assert( nArg==1 );
723 if( pgno==0 ){
724 i64 nPg = recoverPageCount(p);
725 sqlite3_result_int64(pCtx, nPg);
726 return;
727 }else{
728 if( p->pGetPage==0 ){
729 pStmt = p->pGetPage = recoverPreparePrintf(
730 p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb
731 );
732 }else if( p->errCode==SQLITE_OK ){
733 pStmt = p->pGetPage;
734 }
735
736 if( pStmt ){
737 sqlite3_bind_int64(pStmt, 1, pgno);
738 if( SQLITE_ROW==sqlite3_step(pStmt) ){
739 const u8 *aPg;
740 int nPg;
741 assert( p->errCode==SQLITE_OK );
742 aPg = sqlite3_column_blob(pStmt, 0);
743 nPg = sqlite3_column_bytes(pStmt, 0);
744 if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
745 aPg = p->pPage1Disk;
746 }
747 sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
748 }
749 recoverReset(p, pStmt);
750 }
751 }
752
753 if( p->errCode ){
754 if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1);
755 sqlite3_result_error_code(pCtx, p->errCode);
756 }
757 }
758
759 /*
760 ** Find a string that is not found anywhere in z[]. Return a pointer
761 ** to that string.
762 **
763 ** Try to use zA and zB first. If both of those are already found in z[]
764 ** then make up some string and store it in the buffer zBuf.
765 */
766 static const char *recoverUnusedString(
767 const char *z, /* Result must not appear anywhere in z */
768 const char *zA, const char *zB, /* Try these first */
769 char *zBuf /* Space to store a generated string */
770 ){
771 unsigned i = 0;
772 if( strstr(z, zA)==0 ) return zA;
773 if( strstr(z, zB)==0 ) return zB;
774 do{
775 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
776 }while( strstr(z,zBuf)!=0 );
777 return zBuf;
778 }
779
780 /*
781 ** Implementation of scalar SQL function "escape_crlf". The argument passed to
782 ** this function is the output of built-in function quote(). If the first
783 ** character of the input is "'", indicating that the value passed to quote()
784 ** was a text value, then this function searches the input for "\n" and "\r"
785 ** characters and adds a wrapper similar to the following:
786 **
787 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
788 **
789 ** Or, if the first character of the input is not "'", then a copy of the input
790 ** is returned.
791 */
792 static void recoverEscapeCrlf(
793 sqlite3_context *context,
794 int argc,
795 sqlite3_value **argv
796 ){
797 const char *zText = (const char*)sqlite3_value_text(argv[0]);
798 (void)argc;
799 if( zText && zText[0]=='\'' ){
800 int nText = sqlite3_value_bytes(argv[0]);
801 int i;
802 char zBuf1[20];
803 char zBuf2[20];
804 const char *zNL = 0;
805 const char *zCR = 0;
806 int nCR = 0;
807 int nNL = 0;
808
809 for(i=0; zText[i]; i++){
810 if( zNL==0 && zText[i]=='\n' ){
811 zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1);
812 nNL = (int)strlen(zNL);
813 }
814 if( zCR==0 && zText[i]=='\r' ){
815 zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2);
816 nCR = (int)strlen(zCR);
817 }
818 }
819
820 if( zNL || zCR ){
821 int iOut = 0;
822 i64 nMax = (nNL > nCR) ? nNL : nCR;
823 i64 nAlloc = nMax * nText + (nMax+64)*2;
824 char *zOut = (char*)sqlite3_malloc64(nAlloc);
825 if( zOut==0 ){
826 sqlite3_result_error_nomem(context);
827 return;
828 }
829
830 if( zNL && zCR ){
831 memcpy(&zOut[iOut], "replace(replace(", 16);
832 iOut += 16;
833 }else{
834 memcpy(&zOut[iOut], "replace(", 8);
835 iOut += 8;
836 }
837 for(i=0; zText[i]; i++){
838 if( zText[i]=='\n' ){
839 memcpy(&zOut[iOut], zNL, nNL);
840 iOut += nNL;
841 }else if( zText[i]=='\r' ){
842 memcpy(&zOut[iOut], zCR, nCR);
843 iOut += nCR;
844 }else{
845 zOut[iOut] = zText[i];
846 iOut++;
847 }
848 }
849
850 if( zNL ){
851 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
852 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
853 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
854 }
855 if( zCR ){
856 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
857 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
858 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
859 }
860
861 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
862 sqlite3_free(zOut);
863 return;
864 }
865 }
866
867 sqlite3_result_value(context, argv[0]);
868 }
869
870 /*
871 ** This function is a no-op if recover handle p already contains an error
872 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
873 ** this case.
874 **
875 ** Otherwise, attempt to populate temporary table "recovery.schema" with the
876 ** parts of the database schema that can be extracted from the input database.
877 **
878 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
879 ** and error message are left in the recover handle and a copy of the
880 ** error code returned. It is not considered an error if part of all of
881 ** the database schema cannot be recovered due to corruption.
882 */
883 static int recoverCacheSchema(sqlite3_recover *p){
884 return recoverExec(p, p->dbOut,
885 "WITH RECURSIVE pages(p) AS ("
886 " SELECT 1"
887 " UNION"
888 " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p"
889 ")"
890 "INSERT INTO recovery.schema SELECT"
891 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
892 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
893 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
894 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
895 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
896 "FROM sqlite_dbdata('getpage()') WHERE pgno IN ("
897 " SELECT p FROM pages"
898 ") GROUP BY pgno, cell"
899 );
900 }
901
902 /*
903 ** If this recover handle is not in SQL callback mode (i.e. was not created
904 ** using sqlite3_recover_init_sql()) of if an error has already occurred,
905 ** this function is a no-op. Otherwise, issue a callback with SQL statement
906 ** zSql as the parameter.
907 **
908 ** If the callback returns non-zero, set the recover handle error code to
909 ** the value returned (so that the caller will abandon processing).
910 */
911 static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){
912 if( p->errCode==SQLITE_OK && p->xSql ){
913 int res = p->xSql(p->pSqlCtx, zSql);
914 if( res ){
915 recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res);
916 }
917 }
918 }
919
920 /*
921 ** Transfer the following settings from the input database to the output
922 ** database:
923 **
924 ** + page-size,
925 ** + auto-vacuum settings,
926 ** + database encoding,
927 ** + user-version (PRAGMA user_version), and
928 ** + application-id (PRAGMA application_id), and
929 */
930 static void recoverTransferSettings(sqlite3_recover *p){
931 const char *aPragma[] = {
932 "encoding",
933 "page_size",
934 "auto_vacuum",
935 "user_version",
936 "application_id"
937 };
938 int ii;
939
940 /* Truncate the output database to 0 pages in size. This is done by
941 ** opening a new, empty, temp db, then using the backup API to clobber
942 ** any existing output db with a copy of it. */
943 if( p->errCode==SQLITE_OK ){
944 sqlite3 *db2 = 0;
945 int rc = sqlite3_open("", &db2);
946 if( rc!=SQLITE_OK ){
947 recoverDbError(p, db2);
948 return;
949 }
950
951 for(ii=0; ii<(int)(sizeof(aPragma)/sizeof(aPragma[0])); ii++){
952 const char *zPrag = aPragma[ii];
953 sqlite3_stmt *p1 = 0;
954 p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag);
955 if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){
956 const char *zArg = (const char*)sqlite3_column_text(p1, 0);
957 char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg);
958 recoverSqlCallback(p, z2);
959 recoverExec(p, db2, z2);
960 sqlite3_free(z2);
961 if( zArg==0 ){
962 recoverError(p, SQLITE_NOMEM, 0);
963 }
964 }
965 recoverFinalize(p, p1);
966 }
967 recoverExec(p, db2, "CREATE TABLE t1(a)");
968 recoverExec(p, db2, "DROP TABLE t1");
969
970 if( p->errCode==SQLITE_OK ){
971 sqlite3 *db = p->dbOut;
972 sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main");
973 if( pBackup ){
974 sqlite3_backup_step(pBackup, -1);
975 p->errCode = sqlite3_backup_finish(pBackup);
976 }else{
977 recoverDbError(p, db);
978 }
979 }
980
981 sqlite3_close(db2);
982 }
983 }
984
985 /*
986 ** This function is a no-op if recover handle p already contains an error
987 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
988 ** this case.
989 **
990 ** Otherwise, an attempt is made to open the output database, attach
991 ** and create the schema of the temporary database used to store
992 ** intermediate data, and to register all required user functions and
993 ** virtual table modules with the output handle.
994 **
995 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
996 ** and error message are left in the recover handle and a copy of the
997 ** error code returned.
998 */
999 static int recoverOpenOutput(sqlite3_recover *p){
1000 struct Func {
1001 const char *zName;
1002 int nArg;
1003 void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
1004 } aFunc[] = {
1005 { "getpage", 1, recoverGetPage },
1006 { "page_is_used", 1, recoverPageIsUsed },
1007 { "read_i32", 2, recoverReadI32 },
1008 { "escape_crlf", 1, recoverEscapeCrlf },
1009 };
1010
1011 const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
1012 sqlite3 *db = 0; /* New database handle */
1013 int ii; /* For iterating through aFunc[] */
1014
1015 assert( p->dbOut==0 );
1016
1017 if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){
1018 recoverDbError(p, db);
1019 }
1020
1021 /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules.
1022 ** These two are registered with the output database handle - this
1023 ** module depends on the input handle supporting the sqlite_dbpage
1024 ** virtual table only. */
1025 if( p->errCode==SQLITE_OK ){
1026 p->errCode = sqlite3_dbdata_init(db, 0, 0);
1027 }
1028
1029 /* Register the custom user-functions with the output handle. */
1030 for(ii=0;
1031 p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0]));
1032 ii++){
1033 p->errCode = sqlite3_create_function(db, aFunc[ii].zName,
1034 aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0
1035 );
1036 }
1037
1038 p->dbOut = db;
1039 return p->errCode;
1040 }
1041
1042 /*
1043 ** Attach the auxiliary database 'recovery' to the output database handle.
1044 ** This temporary database is used during the recovery process and then
1045 ** discarded.
1046 */
1047 static void recoverOpenRecovery(sqlite3_recover *p){
1048 char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb);
1049 recoverExec(p, p->dbOut, zSql);
1050 sqlite3_free(zSql);
1051 recoverExec(p, p->dbOut, "PRAGMA writable_schema = 1");
1052 recoverExec(p, p->dbOut,
1053 "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT)");
1054 recoverExec(p, p->dbOut,
1055 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql)");
1056 }
1057
1058
1059 /*
1060 ** This function is a no-op if recover handle p already contains an error
1061 ** (if p->errCode!=SQLITE_OK).
1062 **
1063 ** Otherwise, argument zName must be the name of a table that has just been
1064 ** created in the output database. This function queries the output db
1065 ** for the schema of said table, and creates a RecoverTable object to
1066 ** store the schema in memory. The new RecoverTable object is linked into
1067 ** the list at sqlite3_recover.pTblList.
1068 **
1069 ** Parameter iRoot must be the root page of table zName in the INPUT
1070 ** database.
1071 */
1072 static void recoverAddTable(
1073 sqlite3_recover *p,
1074 const char *zName, /* Name of table created in output db */
1075 i64 iRoot /* Root page of same table in INPUT db */
1076 ){
1077 sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut,
1078 "PRAGMA table_xinfo(%Q)", zName
1079 );
1080
1081 if( pStmt ){
1082 int iPk = -1;
1083 int iBind = 1;
1084 RecoverTable *pNew = 0;
1085 int nCol = 0;
1086 int nName = recoverStrlen(zName);
1087 int nByte = 0;
1088 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1089 nCol++;
1090 nByte += (sqlite3_column_bytes(pStmt, 1)+1);
1091 }
1092 nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1;
1093 recoverReset(p, pStmt);
1094
1095 pNew = recoverMalloc(p, nByte);
1096 if( pNew ){
1097 int i = 0;
1098 int iField = 0;
1099 char *csr = 0;
1100 pNew->aCol = (RecoverColumn*)&pNew[1];
1101 pNew->zTab = csr = (char*)&pNew->aCol[nCol];
1102 pNew->nCol = nCol;
1103 pNew->iRoot = iRoot;
1104 memcpy(csr, zName, nName);
1105 csr += nName+1;
1106
1107 for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){
1108 int iPKF = sqlite3_column_int(pStmt, 5);
1109 int n = sqlite3_column_bytes(pStmt, 1);
1110 const char *z = (const char*)sqlite3_column_text(pStmt, 1);
1111 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
1112 int eHidden = sqlite3_column_int(pStmt, 6);
1113
1114 if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i;
1115 if( iPKF>1 ) iPk = -2;
1116 pNew->aCol[i].zCol = csr;
1117 pNew->aCol[i].eHidden = eHidden;
1118 if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){
1119 pNew->aCol[i].iField = -1;
1120 }else{
1121 pNew->aCol[i].iField = iField++;
1122 }
1123 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
1124 && eHidden!=RECOVER_EHIDDEN_STORED
1125 ){
1126 pNew->aCol[i].iBind = iBind++;
1127 }
1128 memcpy(csr, z, n);
1129 csr += (n+1);
1130 }
1131
1132 pNew->pNext = p->pTblList;
1133 p->pTblList = pNew;
1134 pNew->bIntkey = 1;
1135 }
1136
1137 recoverFinalize(p, pStmt);
1138
1139 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName);
1140 while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
1141 int iField = sqlite3_column_int(pStmt, 0);
1142 int iCol = sqlite3_column_int(pStmt, 1);
1143
1144 assert( iCol<pNew->nCol );
1145 pNew->aCol[iCol].iField = iField;
1146
1147 pNew->bIntkey = 0;
1148 iPk = -2;
1149 }
1150 recoverFinalize(p, pStmt);
1151
1152 if( p->errCode==SQLITE_OK ){
1153 if( iPk>=0 ){
1154 pNew->aCol[iPk].bIPK = 1;
1155 }else if( pNew->bIntkey ){
1156 pNew->iRowidBind = iBind++;
1157 }
1158 }
1159 }
1160 }
1161
1162 /*
1163 ** This function is called after recoverCacheSchema() has cached those parts
1164 ** of the input database schema that could be recovered in temporary table
1165 ** "recovery.schema". This function creates in the output database copies
1166 ** of all parts of that schema that must be created before the tables can
1167 ** be populated. Specifically, this means:
1168 **
1169 ** * all tables that are not VIRTUAL, and
1170 ** * UNIQUE indexes.
1171 **
1172 ** If the recovery handle uses SQL callbacks, then callbacks containing
1173 ** the associated "CREATE TABLE" and "CREATE INDEX" statements are made.
1174 **
1175 ** Additionally, records are added to the sqlite_schema table of the
1176 ** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE
1177 ** records are written directly to sqlite_schema, not actually executed.
1178 ** If the handle is in SQL callback mode, then callbacks are invoked
1179 ** with equivalent SQL statements.
1180 */
1181 static int recoverWriteSchema1(sqlite3_recover *p){
1182 sqlite3_stmt *pSelect = 0;
1183 sqlite3_stmt *pTblname = 0;
1184
1185 pSelect = recoverPrepare(p, p->dbOut,
1186 "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS ("
1187 " SELECT rootpage, name, sql, "
1188 " type='table', "
1189 " sql LIKE 'create virtual%',"
1190 " (type='index' AND (sql LIKE '%unique%' OR ?1))"
1191 " FROM recovery.schema"
1192 ")"
1193 "SELECT rootpage, tbl, isVirtual, name, sql"
1194 " FROM dbschema "
1195 " WHERE (tbl OR isIndex) AND sql GLOB 'CREATE *'"
1196 " ORDER BY tbl DESC, name=='sqlite_sequence' DESC"
1197 );
1198
1199 pTblname = recoverPrepare(p, p->dbOut,
1200 "SELECT name FROM sqlite_schema "
1201 "WHERE type='table' ORDER BY rowid DESC LIMIT 1"
1202 );
1203
1204 if( pSelect ){
1205 sqlite3_bind_int(pSelect, 1, p->bSlowIndexes);
1206 while( sqlite3_step(pSelect)==SQLITE_ROW ){
1207 i64 iRoot = sqlite3_column_int64(pSelect, 0);
1208 int bTable = sqlite3_column_int(pSelect, 1);
1209 int bVirtual = sqlite3_column_int(pSelect, 2);
1210 const char *zName = (const char*)sqlite3_column_text(pSelect, 3);
1211 const char *zSql = (const char*)sqlite3_column_text(pSelect, 4);
1212 char *zFree = 0;
1213 int rc = SQLITE_OK;
1214
1215 if( bVirtual ){
1216 zSql = (const char*)(zFree = recoverMPrintf(p,
1217 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
1218 zName, zName, zSql
1219 ));
1220 }
1221 rc = recoverOneStmt(p->dbOut, zSql);
1222 if( rc==SQLITE_OK ){
1223 recoverSqlCallback(p, zSql);
1224 if( bTable && !bVirtual ){
1225 if( SQLITE_ROW==sqlite3_step(pTblname) ){
1226 const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
1227 if( zTbl ) recoverAddTable(p, zTbl, iRoot);
1228 }
1229 recoverReset(p, pTblname);
1230 }
1231 }else if( rc!=SQLITE_ERROR ){
1232 recoverDbError(p, p->dbOut);
1233 }
1234 sqlite3_free(zFree);
1235 }
1236 }
1237 recoverFinalize(p, pSelect);
1238 recoverFinalize(p, pTblname);
1239
1240 return p->errCode;
1241 }
1242
1243 /*
1244 ** This function is called after the output database has been populated. It
1245 ** adds all recovered schema elements that were not created in the output
1246 ** database by recoverWriteSchema1() - everything except for tables and
1247 ** UNIQUE indexes. Specifically:
1248 **
1249 ** * views,
1250 ** * triggers,
1251 ** * non-UNIQUE indexes.
1252 **
1253 ** If the recover handle is in SQL callback mode, then equivalent callbacks
1254 ** are issued to create the schema elements.
1255 */
1256 static int recoverWriteSchema2(sqlite3_recover *p){
1257 sqlite3_stmt *pSelect = 0;
1258
1259 pSelect = recoverPrepare(p, p->dbOut,
1260 p->bSlowIndexes ?
1261 "SELECT rootpage, sql FROM recovery.schema "
1262 " WHERE type!='table' AND type!='index'"
1263 " AND sql GLOB 'CREATE *'"
1264 :
1265 "SELECT rootpage, sql FROM recovery.schema "
1266 " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')"
1267 " AND sql GLOB 'CREATE *'"
1268 );
1269
1270 if( pSelect ){
1271 while( sqlite3_step(pSelect)==SQLITE_ROW ){
1272 const char *zSql = (const char*)sqlite3_column_text(pSelect, 1);
1273 int rc = recoverOneStmt(p->dbOut, zSql);
1274 if( rc==SQLITE_OK ){
1275 recoverSqlCallback(p, zSql);
1276 }else if( rc!=SQLITE_ERROR ){
1277 recoverDbError(p, p->dbOut);
1278 }
1279 }
1280 }
1281 recoverFinalize(p, pSelect);
1282
1283 return p->errCode;
1284 }
1285
1286 /*
1287 ** This function is a no-op if recover handle p already contains an error
1288 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
1289 **
1290 ** Otherwise, if the recover handle is configured to create an output
1291 ** database (was created by sqlite3_recover_init()), then this function
1292 ** prepares and returns an SQL statement to INSERT a new record into table
1293 ** pTab, assuming the first nField fields of a record extracted from disk
1294 ** are valid.
1295 **
1296 ** For example, if table pTab is:
1297 **
1298 ** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e);
1299 **
1300 ** And nField is 4, then the SQL statement prepared and returned is:
1301 **
1302 ** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3);
1303 **
1304 ** In this case even though 4 values were extracted from the input db,
1305 ** only 3 are written to the output, as the generated STORED column
1306 ** cannot be written.
1307 **
1308 ** If the recover handle is in SQL callback mode, then the SQL statement
1309 ** prepared is such that evaluating it returns a single row containing
1310 ** a single text value - itself an SQL statement similar to the above,
1311 ** except with SQL literals in place of the variables. For example:
1312 **
1313 ** SELECT 'INSERT INTO (a, c, d) VALUES ('
1314 ** || quote(?1) || ', '
1315 ** || quote(?2) || ', '
1316 ** || quote(?3) || ')';
1317 **
1318 ** In either case, it is the responsibility of the caller to eventually
1319 ** free the statement handle using sqlite3_finalize().
1320 */
1321 static sqlite3_stmt *recoverInsertStmt(
1322 sqlite3_recover *p,
1323 RecoverTable *pTab,
1324 int nField
1325 ){
1326 sqlite3_stmt *pRet = 0;
1327 const char *zSep = "";
1328 const char *zSqlSep = "";
1329 char *zSql = 0;
1330 char *zFinal = 0;
1331 char *zBind = 0;
1332 int ii;
1333 int bSql = p->xSql ? 1 : 0;
1334
1335 if( nField<=0 ) return 0;
1336
1337 assert( nField<=pTab->nCol );
1338
1339 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab);
1340
1341 if( pTab->iRowidBind ){
1342 assert( pTab->bIntkey );
1343 zSql = recoverMPrintf(p, "%z_rowid_", zSql);
1344 if( bSql ){
1345 zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind);
1346 }else{
1347 zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind);
1348 }
1349 zSqlSep = "||', '||";
1350 zSep = ", ";
1351 }
1352
1353 for(ii=0; ii<nField; ii++){
1354 int eHidden = pTab->aCol[ii].eHidden;
1355 if( eHidden!=RECOVER_EHIDDEN_VIRTUAL
1356 && eHidden!=RECOVER_EHIDDEN_STORED
1357 ){
1358 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 );
1359 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol);
1360
1361 if( bSql ){
1362 zBind = recoverMPrintf(p,
1363 "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind
1364 );
1365 zSqlSep = "||', '||";
1366 }else{
1367 zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind);
1368 }
1369 zSep = ", ";
1370 }
1371 }
1372
1373 if( bSql ){
1374 zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'",
1375 zSql, zBind
1376 );
1377 }else{
1378 zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind);
1379 }
1380
1381 pRet = recoverPrepare(p, p->dbOut, zFinal);
1382 sqlite3_free(zSql);
1383 sqlite3_free(zBind);
1384 sqlite3_free(zFinal);
1385
1386 return pRet;
1387 }
1388
1389
1390 /*
1391 ** Search the list of RecoverTable objects at p->pTblList for one that
1392 ** has root page iRoot in the input database. If such an object is found,
1393 ** return a pointer to it. Otherwise, return NULL.
1394 */
1395 static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){
1396 RecoverTable *pRet = 0;
1397 for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext);
1398 return pRet;
1399 }
1400
1401 /*
1402 ** This function attempts to create a lost and found table within the
1403 ** output db. If successful, it returns a pointer to a buffer containing
1404 ** the name of the new table. It is the responsibility of the caller to
1405 ** eventually free this buffer using sqlite3_free().
1406 **
1407 ** If an error occurs, NULL is returned and an error code and error
1408 ** message left in the recover handle.
1409 */
1410 static char *recoverLostAndFoundCreate(
1411 sqlite3_recover *p, /* Recover object */
1412 int nField /* Number of column fields in new table */
1413 ){
1414 char *zTbl = 0;
1415 sqlite3_stmt *pProbe = 0;
1416 int ii = 0;
1417
1418 pProbe = recoverPrepare(p, p->dbOut,
1419 "SELECT 1 FROM sqlite_schema WHERE name=?"
1420 );
1421 for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){
1422 int bFail = 0;
1423 if( ii<0 ){
1424 zTbl = recoverMPrintf(p, "%s", p->zLostAndFound);
1425 }else{
1426 zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii);
1427 }
1428
1429 if( p->errCode==SQLITE_OK ){
1430 sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC);
1431 if( SQLITE_ROW==sqlite3_step(pProbe) ){
1432 bFail = 1;
1433 }
1434 recoverReset(p, pProbe);
1435 }
1436
1437 if( bFail ){
1438 sqlite3_clear_bindings(pProbe);
1439 sqlite3_free(zTbl);
1440 zTbl = 0;
1441 }
1442 }
1443 recoverFinalize(p, pProbe);
1444
1445 if( zTbl ){
1446 const char *zSep = 0;
1447 char *zField = 0;
1448 char *zSql = 0;
1449
1450 zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, ";
1451 for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){
1452 zField = recoverMPrintf(p, "%z%sc%d", zField, zSep, ii);
1453 zSep = ", ";
1454 }
1455
1456 zSql = recoverMPrintf(p, "CREATE TABLE %s(%s)", zTbl, zField);
1457 sqlite3_free(zField);
1458
1459 recoverExec(p, p->dbOut, zSql);
1460 recoverSqlCallback(p, zSql);
1461 sqlite3_free(zSql);
1462 }else if( p->errCode==SQLITE_OK ){
1463 recoverError(
1464 p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound
1465 );
1466 }
1467
1468 return zTbl;
1469 }
1470
1471 /*
1472 ** Synthesize and prepare an INSERT statement to write to the lost_and_found
1473 ** table in the output database. The name of the table is zTab, and it has
1474 ** nField c* fields.
1475 */
1476 static sqlite3_stmt *recoverLostAndFoundInsert(
1477 sqlite3_recover *p,
1478 const char *zTab,
1479 int nField
1480 ){
1481 int nTotal = nField + 4;
1482 int ii;
1483 char *zBind = 0;
1484 sqlite3_stmt *pRet = 0;
1485
1486 if( p->xSql==0 ){
1487 for(ii=0; ii<nTotal; ii++){
1488 zBind = recoverMPrintf(p, "%z%s?", zBind, zBind?", ":"", ii);
1489 }
1490 pRet = recoverPreparePrintf(
1491 p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind
1492 );
1493 }else{
1494 const char *zSep = "";
1495 for(ii=0; ii<nTotal; ii++){
1496 zBind = recoverMPrintf(p, "%z%squote(?)", zBind, zSep);
1497 zSep = "|| ', ' ||";
1498 }
1499 pRet = recoverPreparePrintf(
1500 p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind
1501 );
1502 }
1503
1504 sqlite3_free(zBind);
1505 return pRet;
1506 }
1507
1508 /*
1509 ** Input database page iPg contains data that will be written to the
1510 ** lost-and-found table of the output database. This function attempts
1511 ** to identify the root page of the tree that page iPg belonged to.
1512 ** If successful, it sets output variable (*piRoot) to the page number
1513 ** of the root page and returns SQLITE_OK. Otherwise, if an error occurs,
1514 ** an SQLite error code is returned and the final value of *piRoot
1515 ** undefined.
1516 */
1517 static int recoverLostAndFoundFindRoot(
1518 sqlite3_recover *p,
1519 i64 iPg,
1520 i64 *piRoot
1521 ){
1522 RecoverStateLAF *pLaf = &p->laf;
1523
1524 if( pLaf->pFindRoot==0 ){
1525 pLaf->pFindRoot = recoverPrepare(p, p->dbOut,
1526 "WITH RECURSIVE p(pgno) AS ("
1527 " SELECT ?"
1528 " UNION"
1529 " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno"
1530 ") "
1531 "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno "
1532 " AND m.parent IS NULL"
1533 );
1534 }
1535 if( p->errCode==SQLITE_OK ){
1536 sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg);
1537 if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){
1538 *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0);
1539 }else{
1540 *piRoot = iPg;
1541 }
1542 recoverReset(p, pLaf->pFindRoot);
1543 }
1544 return p->errCode;
1545 }
1546
1547 /*
1548 ** Recover data from page iPage of the input database and write it to
1549 ** the lost-and-found table in the output database.
1550 */
1551 static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){
1552 RecoverStateLAF *pLaf = &p->laf;
1553 sqlite3_value **apVal = pLaf->apVal;
1554 sqlite3_stmt *pPageData = pLaf->pPageData;
1555 sqlite3_stmt *pInsert = pLaf->pInsert;
1556
1557 int nVal = -1;
1558 int iPrevCell = 0;
1559 i64 iRoot = 0;
1560 int bHaveRowid = 0;
1561 i64 iRowid = 0;
1562 int ii = 0;
1563
1564 if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return;
1565 sqlite3_bind_int64(pPageData, 1, iPage);
1566 while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){
1567 int iCell = sqlite3_column_int64(pPageData, 0);
1568 int iField = sqlite3_column_int64(pPageData, 1);
1569
1570 if( iPrevCell!=iCell && nVal>=0 ){
1571 /* Insert the new row */
1572 sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */
1573 sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */
1574 sqlite3_bind_int(pInsert, 3, nVal); /* nfield */
1575 if( bHaveRowid ){
1576 sqlite3_bind_int64(pInsert, 4, iRowid); /* id */
1577 }
1578 for(ii=0; ii<nVal; ii++){
1579 recoverBindValue(p, pInsert, 5+ii, apVal[ii]);
1580 }
1581 if( sqlite3_step(pInsert)==SQLITE_ROW ){
1582 recoverSqlCallback(p, (const char*)sqlite3_column_text(pInsert, 0));
1583 }
1584 recoverReset(p, pInsert);
1585
1586 /* Discard the accumulated row data */
1587 for(ii=0; ii<nVal; ii++){
1588 sqlite3_value_free(apVal[ii]);
1589 apVal[ii] = 0;
1590 }
1591 sqlite3_clear_bindings(pInsert);
1592 bHaveRowid = 0;
1593 nVal = -1;
1594 }
1595
1596 if( iCell<0 ) break;
1597
1598 if( iField<0 ){
1599 assert( nVal==-1 );
1600 iRowid = sqlite3_column_int64(pPageData, 2);
1601 bHaveRowid = 1;
1602 nVal = 0;
1603 }else if( iField<pLaf->nMaxField ){
1604 sqlite3_value *pVal = sqlite3_column_value(pPageData, 2);
1605 apVal[iField] = sqlite3_value_dup(pVal);
1606 assert( iField==nVal || (nVal==-1 && iField==0) );
1607 nVal = iField+1;
1608 if( apVal[iField]==0 ){
1609 recoverError(p, SQLITE_NOMEM, 0);
1610 }
1611 }
1612
1613 iPrevCell = iCell;
1614 }
1615 recoverReset(p, pPageData);
1616
1617 for(ii=0; ii<nVal; ii++){
1618 sqlite3_value_free(apVal[ii]);
1619 apVal[ii] = 0;
1620 }
1621 }
1622
1623 /*
1624 ** Perform one step (sqlite3_recover_step()) of work for the connection
1625 ** passed as the only argument, which is guaranteed to be in
1626 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
1627 ** table of the output database is populated with recovered data that can
1628 ** not be assigned to any recovered schema object.
1629 */
1630 static int recoverLostAndFound3Step(sqlite3_recover *p){
1631 RecoverStateLAF *pLaf = &p->laf;
1632 if( p->errCode==SQLITE_OK ){
1633 if( pLaf->pInsert==0 ){
1634 return SQLITE_DONE;
1635 }else{
1636 if( p->errCode==SQLITE_OK ){
1637 int res = sqlite3_step(pLaf->pAllPage);
1638 if( res==SQLITE_ROW ){
1639 i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0);
1640 if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){
1641 recoverLostAndFoundOnePage(p, iPage);
1642 }
1643 }else{
1644 recoverReset(p, pLaf->pAllPage);
1645 return SQLITE_DONE;
1646 }
1647 }
1648 }
1649 }
1650 return SQLITE_OK;
1651 }
1652
1653 /*
1654 ** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3
1655 ** state - during which the lost-and-found table of the output database
1656 ** is populated with recovered data that can not be assigned to any
1657 ** recovered schema object.
1658 */
1659 static void recoverLostAndFound3Init(sqlite3_recover *p){
1660 RecoverStateLAF *pLaf = &p->laf;
1661
1662 if( pLaf->nMaxField>0 ){
1663 char *zTab = 0; /* Name of lost_and_found table */
1664
1665 zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField);
1666 pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField);
1667 sqlite3_free(zTab);
1668
1669 pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut,
1670 "WITH RECURSIVE seq(ii) AS ("
1671 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
1672 ")"
1673 "SELECT ii FROM seq" , p->laf.nPg
1674 );
1675 pLaf->pPageData = recoverPrepare(p, p->dbOut,
1676 "SELECT cell, field, value "
1677 "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? "
1678 "UNION ALL "
1679 "SELECT -1, -1, -1"
1680 );
1681
1682 pLaf->apVal = (sqlite3_value**)recoverMalloc(p,
1683 pLaf->nMaxField*sizeof(sqlite3_value*)
1684 );
1685 }
1686 }
1687
1688 /*
1689 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
1690 ** tables recovered from the schema of the input database are populated with
1691 ** recovered data.
1692 */
1693 static int recoverWriteDataInit(sqlite3_recover *p){
1694 RecoverStateW1 *p1 = &p->w1;
1695 RecoverTable *pTbl = 0;
1696 int nByte = 0;
1697
1698 /* Figure out the maximum number of columns for any table in the schema */
1699 assert( p1->nMax==0 );
1700 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){
1701 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol;
1702 }
1703
1704 /* Allocate an array of (sqlite3_value*) in which to accumulate the values
1705 ** that will be written to the output database in a single row. */
1706 nByte = sizeof(sqlite3_value*) * (p1->nMax+1);
1707 p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte);
1708 if( p1->apVal==0 ) return p->errCode;
1709
1710 /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT
1711 ** to loop through cells that appear to belong to a single table (pSel). */
1712 p1->pTbls = recoverPrepare(p, p->dbOut,
1713 "SELECT rootpage FROM recovery.schema "
1714 " WHERE type='table' AND (sql NOT LIKE 'create virtual%')"
1715 " ORDER BY (tbl_name='sqlite_sequence') ASC"
1716 );
1717 p1->pSel = recoverPrepare(p, p->dbOut,
1718 "WITH RECURSIVE pages(page) AS ("
1719 " SELECT ?1"
1720 " UNION"
1721 " SELECT child FROM sqlite_dbptr('getpage()'), pages "
1722 " WHERE pgno=page"
1723 ") "
1724 "SELECT page, cell, field, value "
1725 "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno "
1726 "UNION ALL "
1727 "SELECT 0, 0, 0, 0"
1728 );
1729
1730 return p->errCode;
1731 }
1732
1733 /*
1734 ** Clean up resources allocated by recoverWriteDataInit() (stuff in
1735 ** sqlite3_recover.w1).
1736 */
1737 static void recoverWriteDataCleanup(sqlite3_recover *p){
1738 RecoverStateW1 *p1 = &p->w1;
1739 int ii;
1740 for(ii=0; ii<p1->nVal; ii++){
1741 sqlite3_value_free(p1->apVal[ii]);
1742 }
1743 sqlite3_free(p1->apVal);
1744 recoverFinalize(p, p1->pInsert);
1745 recoverFinalize(p, p1->pTbls);
1746 recoverFinalize(p, p1->pSel);
1747 memset(p1, 0, sizeof(*p1));
1748 }
1749
1750 /*
1751 ** Perform one step (sqlite3_recover_step()) of work for the connection
1752 ** passed as the only argument, which is guaranteed to be in
1753 ** RECOVER_STATE_WRITING state - during which tables recovered from the
1754 ** schema of the input database are populated with recovered data.
1755 */
1756 static int recoverWriteDataStep(sqlite3_recover *p){
1757 RecoverStateW1 *p1 = &p->w1;
1758 sqlite3_stmt *pSel = p1->pSel;
1759 sqlite3_value **apVal = p1->apVal;
1760
1761 if( p->errCode==SQLITE_OK && p1->pTab==0 ){
1762 if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){
1763 i64 iRoot = sqlite3_column_int64(p1->pTbls, 0);
1764 p1->pTab = recoverFindTable(p, iRoot);
1765
1766 recoverFinalize(p, p1->pInsert);
1767 p1->pInsert = 0;
1768
1769 /* If this table is unknown, return early. The caller will invoke this
1770 ** function again and it will move on to the next table. */
1771 if( p1->pTab==0 ) return p->errCode;
1772
1773 /* If this is the sqlite_sequence table, delete any rows added by
1774 ** earlier INSERT statements on tables with AUTOINCREMENT primary
1775 ** keys before recovering its contents. The p1->pTbls SELECT statement
1776 ** is rigged to deliver "sqlite_sequence" last of all, so we don't
1777 ** worry about it being modified after it is recovered. */
1778 if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){
1779 recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence");
1780 recoverSqlCallback(p, "DELETE FROM sqlite_sequence");
1781 }
1782
1783 /* Bind the root page of this table within the original database to
1784 ** SELECT statement p1->pSel. The SELECT statement will then iterate
1785 ** through cells that look like they belong to table pTab. */
1786 sqlite3_bind_int64(pSel, 1, iRoot);
1787
1788 p1->nVal = 0;
1789 p1->bHaveRowid = 0;
1790 p1->iPrevPage = -1;
1791 p1->iPrevCell = -1;
1792 }else{
1793 return SQLITE_DONE;
1794 }
1795 }
1796 assert( p->errCode!=SQLITE_OK || p1->pTab );
1797
1798 if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){
1799 RecoverTable *pTab = p1->pTab;
1800
1801 i64 iPage = sqlite3_column_int64(pSel, 0);
1802 int iCell = sqlite3_column_int(pSel, 1);
1803 int iField = sqlite3_column_int(pSel, 2);
1804 sqlite3_value *pVal = sqlite3_column_value(pSel, 3);
1805 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell);
1806
1807 assert( bNewCell==0 || (iField==-1 || iField==0) );
1808 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol );
1809
1810 if( bNewCell ){
1811 int ii = 0;
1812 if( p1->nVal>=0 ){
1813 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){
1814 recoverFinalize(p, p1->pInsert);
1815 p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal);
1816 p1->nInsert = p1->nVal;
1817 }
1818 if( p1->nVal>0 ){
1819 sqlite3_stmt *pInsert = p1->pInsert;
1820 for(ii=0; ii<pTab->nCol; ii++){
1821 RecoverColumn *pCol = &pTab->aCol[ii];
1822 int iBind = pCol->iBind;
1823 if( iBind>0 ){
1824 if( pCol->bIPK ){
1825 sqlite3_bind_int64(pInsert, iBind, p1->iRowid);
1826 }else if( pCol->iField<p1->nVal ){
1827 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]);
1828 }
1829 }
1830 }
1831 if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){
1832 sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid);
1833 }
1834 if( SQLITE_ROW==sqlite3_step(pInsert) ){
1835 const char *z = (const char*)sqlite3_column_text(pInsert, 0);
1836 recoverSqlCallback(p, z);
1837 }
1838 recoverReset(p, pInsert);
1839 assert( p->errCode || pInsert );
1840 if( pInsert ) sqlite3_clear_bindings(pInsert);
1841 }
1842 }
1843
1844 for(ii=0; ii<p1->nVal; ii++){
1845 sqlite3_value_free(apVal[ii]);
1846 apVal[ii] = 0;
1847 }
1848 p1->nVal = -1;
1849 p1->bHaveRowid = 0;
1850 }
1851
1852 if( iPage!=0 ){
1853 if( iField<0 ){
1854 p1->iRowid = sqlite3_column_int64(pSel, 3);
1855 assert( p1->nVal==-1 );
1856 p1->nVal = 0;
1857 p1->bHaveRowid = 1;
1858 }else if( iField<pTab->nCol ){
1859 assert( apVal[iField]==0 );
1860 apVal[iField] = sqlite3_value_dup( pVal );
1861 if( apVal[iField]==0 ){
1862 recoverError(p, SQLITE_NOMEM, 0);
1863 }
1864 p1->nVal = iField+1;
1865 }else if( pTab->nCol==0 ){
1866 p1->nVal = pTab->nCol;
1867 }
1868 p1->iPrevCell = iCell;
1869 p1->iPrevPage = iPage;
1870 }
1871 }else{
1872 recoverReset(p, pSel);
1873 p1->pTab = 0;
1874 }
1875
1876 return p->errCode;
1877 }
1878
1879 /*
1880 ** Initialize resources required by sqlite3_recover_step() in
1881 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
1882 ** already allocated to a recovered schema element is determined.
1883 */
1884 static void recoverLostAndFound1Init(sqlite3_recover *p){
1885 RecoverStateLAF *pLaf = &p->laf;
1886 sqlite3_stmt *pStmt = 0;
1887
1888 assert( p->laf.pUsed==0 );
1889 pLaf->nPg = recoverPageCount(p);
1890 pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg);
1891
1892 /* Prepare a statement to iterate through all pages that are part of any tree
1893 ** in the recoverable part of the input database schema to the bitmap. And,
1894 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the
1895 ** freelist. */
1896 pStmt = recoverPrepare(
1897 p, p->dbOut,
1898 "WITH trunk(pgno) AS ("
1899 " SELECT read_i32(getpage(1), 8) AS x WHERE x>0"
1900 " UNION"
1901 " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0"
1902 "),"
1903 "trunkdata(pgno, data) AS ("
1904 " SELECT pgno, getpage(pgno) FROM trunk"
1905 "),"
1906 "freelist(data, n, freepgno) AS ("
1907 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata"
1908 " UNION ALL"
1909 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0"
1910 "),"
1911 ""
1912 "roots(r) AS ("
1913 " SELECT 1 UNION ALL"
1914 " SELECT rootpage FROM recovery.schema WHERE rootpage>0"
1915 "),"
1916 "used(page) AS ("
1917 " SELECT r FROM roots"
1918 " UNION"
1919 " SELECT child FROM sqlite_dbptr('getpage()'), used "
1920 " WHERE pgno=page"
1921 ") "
1922 "SELECT page FROM used"
1923 " UNION ALL "
1924 "SELECT freepgno FROM freelist WHERE NOT ?"
1925 );
1926 if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt);
1927 pLaf->pUsedPages = pStmt;
1928 }
1929
1930 /*
1931 ** Perform one step (sqlite3_recover_step()) of work for the connection
1932 ** passed as the only argument, which is guaranteed to be in
1933 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
1934 ** already allocated to a recovered schema element is determined.
1935 */
1936 static int recoverLostAndFound1Step(sqlite3_recover *p){
1937 RecoverStateLAF *pLaf = &p->laf;
1938 int rc = p->errCode;
1939 if( rc==SQLITE_OK ){
1940 rc = sqlite3_step(pLaf->pUsedPages);
1941 if( rc==SQLITE_ROW ){
1942 i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0);
1943 recoverBitmapSet(pLaf->pUsed, iPg);
1944 rc = SQLITE_OK;
1945 }else{
1946 recoverFinalize(p, pLaf->pUsedPages);
1947 pLaf->pUsedPages = 0;
1948 }
1949 }
1950 return rc;
1951 }
1952
1953 /*
1954 ** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2
1955 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
1956 ** are sorted into sets that likely belonged to the same database tree.
1957 */
1958 static void recoverLostAndFound2Init(sqlite3_recover *p){
1959 RecoverStateLAF *pLaf = &p->laf;
1960
1961 assert( p->laf.pAllAndParent==0 );
1962 assert( p->laf.pMapInsert==0 );
1963 assert( p->laf.pMaxField==0 );
1964 assert( p->laf.nMaxField==0 );
1965
1966 pLaf->pMapInsert = recoverPrepare(p, p->dbOut,
1967 "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)"
1968 );
1969 pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut,
1970 "WITH RECURSIVE seq(ii) AS ("
1971 " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld"
1972 ")"
1973 "SELECT pgno, child FROM sqlite_dbptr('getpage()') "
1974 " UNION ALL "
1975 "SELECT NULL, ii FROM seq", p->laf.nPg
1976 );
1977 pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut,
1978 "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?"
1979 );
1980 }
1981
1982 /*
1983 ** Perform one step (sqlite3_recover_step()) of work for the connection
1984 ** passed as the only argument, which is guaranteed to be in
1985 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
1986 ** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged
1987 ** to the same database tree.
1988 */
1989 static int recoverLostAndFound2Step(sqlite3_recover *p){
1990 RecoverStateLAF *pLaf = &p->laf;
1991 if( p->errCode==SQLITE_OK ){
1992 int res = sqlite3_step(pLaf->pAllAndParent);
1993 if( res==SQLITE_ROW ){
1994 i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1);
1995 if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){
1996 sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild);
1997 sqlite3_bind_value(pLaf->pMapInsert, 2,
1998 sqlite3_column_value(pLaf->pAllAndParent, 0)
1999 );
2000 sqlite3_step(pLaf->pMapInsert);
2001 recoverReset(p, pLaf->pMapInsert);
2002 sqlite3_bind_int64(pLaf->pMaxField, 1, iChild);
2003 if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){
2004 int nMax = sqlite3_column_int(pLaf->pMaxField, 0);
2005 if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax;
2006 }
2007 recoverReset(p, pLaf->pMaxField);
2008 }
2009 }else{
2010 recoverFinalize(p, pLaf->pAllAndParent);
2011 pLaf->pAllAndParent =0;
2012 return SQLITE_DONE;
2013 }
2014 }
2015 return p->errCode;
2016 }
2017
2018 /*
2019 ** Free all resources allocated as part of sqlite3_recover_step() calls
2020 ** in one of the RECOVER_STATE_LOSTANDFOUND[123] states.
2021 */
2022 static void recoverLostAndFoundCleanup(sqlite3_recover *p){
2023 recoverBitmapFree(p->laf.pUsed);
2024 p->laf.pUsed = 0;
2025 sqlite3_finalize(p->laf.pUsedPages);
2026 sqlite3_finalize(p->laf.pAllAndParent);
2027 sqlite3_finalize(p->laf.pMapInsert);
2028 sqlite3_finalize(p->laf.pMaxField);
2029 sqlite3_finalize(p->laf.pFindRoot);
2030 sqlite3_finalize(p->laf.pInsert);
2031 sqlite3_finalize(p->laf.pAllPage);
2032 sqlite3_finalize(p->laf.pPageData);
2033 p->laf.pUsedPages = 0;
2034 p->laf.pAllAndParent = 0;
2035 p->laf.pMapInsert = 0;
2036 p->laf.pMaxField = 0;
2037 p->laf.pFindRoot = 0;
2038 p->laf.pInsert = 0;
2039 p->laf.pAllPage = 0;
2040 p->laf.pPageData = 0;
2041 sqlite3_free(p->laf.apVal);
2042 p->laf.apVal = 0;
2043 }
2044
2045 /*
2046 ** Free all resources allocated as part of sqlite3_recover_step() calls.
2047 */
2048 static void recoverFinalCleanup(sqlite3_recover *p){
2049 RecoverTable *pTab = 0;
2050 RecoverTable *pNext = 0;
2051
2052 recoverWriteDataCleanup(p);
2053 recoverLostAndFoundCleanup(p);
2054
2055 for(pTab=p->pTblList; pTab; pTab=pNext){
2056 pNext = pTab->pNext;
2057 sqlite3_free(pTab);
2058 }
2059 p->pTblList = 0;
2060 sqlite3_finalize(p->pGetPage);
2061 p->pGetPage = 0;
2062 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
2063
2064 {
2065 #ifndef NDEBUG
2066 int res =
2067 #endif
2068 sqlite3_close(p->dbOut);
2069 assert( res==SQLITE_OK );
2070 }
2071 p->dbOut = 0;
2072 }
2073
2074 /*
2075 ** Decode and return an unsigned 16-bit big-endian integer value from
2076 ** buffer a[].
2077 */
2078 static u32 recoverGetU16(const u8 *a){
2079 return (((u32)a[0])<<8) + ((u32)a[1]);
2080 }
2081
2082 /*
2083 ** Decode and return an unsigned 32-bit big-endian integer value from
2084 ** buffer a[].
2085 */
2086 static u32 recoverGetU32(const u8 *a){
2087 return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]);
2088 }
2089
2090 /*
2091 ** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal)
2092 ** and return the number of bytes consumed.
2093 */
2094 static int recoverGetVarint(const u8 *a, i64 *pVal){
2095 sqlite3_uint64 u = 0;
2096 int i;
2097 for(i=0; i<8; i++){
2098 u = (u<<7) + (a[i]&0x7f);
2099 if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; }
2100 }
2101 u = (u<<8) + (a[i]&0xff);
2102 *pVal = (sqlite3_int64)u;
2103 return 9;
2104 }
2105
2106 /*
2107 ** The second argument points to a buffer n bytes in size. If this buffer
2108 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
2109 ** return the page-size in bytes. Otherwise, if the buffer does not
2110 ** appear to contain a well-formed b-tree page, return 0.
2111 */
2112 static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
2113 u8 *aUsed = aTmp;
2114 int nFrag = 0;
2115 int nActual = 0;
2116 int iFree = 0;
2117 int nCell = 0; /* Number of cells on page */
2118 int iCellOff = 0; /* Offset of cell array in page */
2119 int iContent = 0;
2120 int eType = 0;
2121 int ii = 0;
2122
2123 eType = (int)a[0];
2124 if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0;
2125
2126 iFree = (int)recoverGetU16(&a[1]);
2127 nCell = (int)recoverGetU16(&a[3]);
2128 iContent = (int)recoverGetU16(&a[5]);
2129 if( iContent==0 ) iContent = 65536;
2130 nFrag = (int)a[7];
2131
2132 if( iContent>n ) return 0;
2133
2134 memset(aUsed, 0, n);
2135 memset(aUsed, 0xFF, iContent);
2136
2137 /* Follow the free-list. This is the same format for all b-tree pages. */
2138 if( iFree && iFree<=iContent ) return 0;
2139 while( iFree ){
2140 int iNext = 0;
2141 int nByte = 0;
2142 if( iFree>(n-4) ) return 0;
2143 iNext = recoverGetU16(&a[iFree]);
2144 nByte = recoverGetU16(&a[iFree+2]);
2145 if( iFree+nByte>n || nByte<4 ) return 0;
2146 if( iNext && iNext<iFree+nByte ) return 0;
2147 memset(&aUsed[iFree], 0xFF, nByte);
2148 iFree = iNext;
2149 }
2150
2151 /* Run through the cells */
2152 if( eType==0x02 || eType==0x05 ){
2153 iCellOff = 12;
2154 }else{
2155 iCellOff = 8;
2156 }
2157 if( (iCellOff + 2*nCell)>iContent ) return 0;
2158 for(ii=0; ii<nCell; ii++){
2159 int iByte;
2160 i64 nPayload = 0;
2161 int nByte = 0;
2162 int iOff = recoverGetU16(&a[iCellOff + 2*ii]);
2163 if( iOff<iContent || iOff>n ){
2164 return 0;
2165 }
2166 if( eType==0x05 || eType==0x02 ) nByte += 4;
2167 nByte += recoverGetVarint(&a[iOff+nByte], &nPayload);
2168 if( eType==0x0D ){
2169 i64 dummy = 0;
2170 nByte += recoverGetVarint(&a[iOff+nByte], &dummy);
2171 }
2172 if( eType!=0x05 ){
2173 int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23);
2174 int M = ((n-12)*32/255)-23;
2175 int K = M+((nPayload-M)%(n-4));
2176
2177 if( nPayload<X ){
2178 nByte += nPayload;
2179 }else if( K<=X ){
2180 nByte += K+4;
2181 }else{
2182 nByte += M+4;
2183 }
2184 }
2185
2186 if( iOff+nByte>n ){
2187 return 0;
2188 }
2189 for(iByte=iOff; iByte<(iOff+nByte); iByte++){
2190 if( aUsed[iByte]!=0 ){
2191 return 0;
2192 }
2193 aUsed[iByte] = 0xFF;
2194 }
2195 }
2196
2197 nActual = 0;
2198 for(ii=0; ii<n; ii++){
2199 if( aUsed[ii]==0 ) nActual++;
2200 }
2201 return (nActual==nFrag);
2202 }
2203
2204
2205 static int recoverVfsClose(sqlite3_file*);
2206 static int recoverVfsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
2207 static int recoverVfsWrite(sqlite3_file*, const void*, int, sqlite3_int64);
2208 static int recoverVfsTruncate(sqlite3_file*, sqlite3_int64 size);
2209 static int recoverVfsSync(sqlite3_file*, int flags);
2210 static int recoverVfsFileSize(sqlite3_file*, sqlite3_int64 *pSize);
2211 static int recoverVfsLock(sqlite3_file*, int);
2212 static int recoverVfsUnlock(sqlite3_file*, int);
2213 static int recoverVfsCheckReservedLock(sqlite3_file*, int *pResOut);
2214 static int recoverVfsFileControl(sqlite3_file*, int op, void *pArg);
2215 static int recoverVfsSectorSize(sqlite3_file*);
2216 static int recoverVfsDeviceCharacteristics(sqlite3_file*);
2217 static int recoverVfsShmMap(sqlite3_file*, int, int, int, void volatile**);
2218 static int recoverVfsShmLock(sqlite3_file*, int offset, int n, int flags);
2219 static void recoverVfsShmBarrier(sqlite3_file*);
2220 static int recoverVfsShmUnmap(sqlite3_file*, int deleteFlag);
2221 static int recoverVfsFetch(sqlite3_file*, sqlite3_int64, int, void**);
2222 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p);
2223
2224 static sqlite3_io_methods recover_methods = {
2225 2, /* iVersion */
2226 recoverVfsClose,
2227 recoverVfsRead,
2228 recoverVfsWrite,
2229 recoverVfsTruncate,
2230 recoverVfsSync,
2231 recoverVfsFileSize,
2232 recoverVfsLock,
2233 recoverVfsUnlock,
2234 recoverVfsCheckReservedLock,
2235 recoverVfsFileControl,
2236 recoverVfsSectorSize,
2237 recoverVfsDeviceCharacteristics,
2238 recoverVfsShmMap,
2239 recoverVfsShmLock,
2240 recoverVfsShmBarrier,
2241 recoverVfsShmUnmap,
2242 recoverVfsFetch,
2243 recoverVfsUnfetch
2244 };
2245
2246 static int recoverVfsClose(sqlite3_file *pFd){
2247 assert( pFd->pMethods!=&recover_methods );
2248 return pFd->pMethods->xClose(pFd);
2249 }
2250
2251 /*
2252 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
2253 */
2254 static void recoverPutU16(u8 *a, u32 v){
2255 a[0] = (v>>8) & 0x00FF;
2256 a[1] = (v>>0) & 0x00FF;
2257 }
2258
2259 /*
2260 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
2261 */
2262 static void recoverPutU32(u8 *a, u32 v){
2263 a[0] = (v>>24) & 0x00FF;
2264 a[1] = (v>>16) & 0x00FF;
2265 a[2] = (v>>8) & 0x00FF;
2266 a[3] = (v>>0) & 0x00FF;
2267 }
2268
2269 /*
2270 ** Detect the page-size of the database opened by file-handle pFd by
2271 ** searching the first part of the file for a well-formed SQLite b-tree
2272 ** page. If parameter nReserve is non-zero, then as well as searching for
2273 ** a b-tree page with zero reserved bytes, this function searches for one
2274 ** with nReserve reserved bytes at the end of it.
2275 **
2276 ** If successful, set variable p->detected_pgsz to the detected page-size
2277 ** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page
2278 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
2279 ** if an error occurs (e.g. an IO or OOM error), then an SQLite error code
2280 ** is returned. The final value of p->detected_pgsz is undefined in this
2281 ** case.
2282 */
2283 static int recoverVfsDetectPagesize(
2284 sqlite3_recover *p, /* Recover handle */
2285 sqlite3_file *pFd, /* File-handle open on input database */
2286 u32 nReserve, /* Possible nReserve value */
2287 i64 nSz /* Size of database file in bytes */
2288 ){
2289 int rc = SQLITE_OK;
2290 const int nMin = 512;
2291 const int nMax = 65536;
2292 const int nMaxBlk = 4;
2293 u32 pgsz = 0;
2294 int iBlk = 0;
2295 u8 *aPg = 0;
2296 u8 *aTmp = 0;
2297 int nBlk = 0;
2298
2299 aPg = (u8*)sqlite3_malloc(2*nMax);
2300 if( aPg==0 ) return SQLITE_NOMEM;
2301 aTmp = &aPg[nMax];
2302
2303 nBlk = (nSz+nMax-1)/nMax;
2304 if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
2305
2306 do {
2307 for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
2308 int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
2309 memset(aPg, 0, nMax);
2310 rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
2311 if( rc==SQLITE_OK ){
2312 int pgsz2;
2313 for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
2314 int iOff;
2315 for(iOff=0; iOff<nMax; iOff+=pgsz2){
2316 if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
2317 pgsz = pgsz2;
2318 break;
2319 }
2320 }
2321 }
2322 }
2323 }
2324 if( pgsz>(u32)p->detected_pgsz ){
2325 p->detected_pgsz = pgsz;
2326 p->nReserve = nReserve;
2327 }
2328 if( nReserve==0 ) break;
2329 nReserve = 0;
2330 }while( 1 );
2331
2332 p->detected_pgsz = pgsz;
2333 sqlite3_free(aPg);
2334 return rc;
2335 }
2336
2337 /*
2338 ** The xRead() method of the wrapper VFS. This is used to intercept calls
2339 ** to read page 1 of the input database.
2340 */
2341 static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
2342 int rc = SQLITE_OK;
2343 if( pFd->pMethods==&recover_methods ){
2344 pFd->pMethods = recover_g.pMethods;
2345 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
2346 if( nByte==16 ){
2347 sqlite3_randomness(16, aBuf);
2348 }else
2349 if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){
2350 /* Ensure that the database has a valid header file. The only fields
2351 ** that really matter to recovery are:
2352 **
2353 ** + Database page size (16-bits at offset 16)
2354 ** + Size of db in pages (32-bits at offset 28)
2355 ** + Database encoding (32-bits at offset 56)
2356 **
2357 ** Also preserved are:
2358 **
2359 ** + first freelist page (32-bits at offset 32)
2360 ** + size of freelist (32-bits at offset 36)
2361 ** + the wal-mode flags (16-bits at offset 18)
2362 **
2363 ** We also try to preserve the auto-vacuum, incr-value, user-version
2364 ** and application-id fields - all 32 bit quantities at offsets
2365 ** 52, 60, 64 and 68. All other fields are set to known good values.
2366 **
2367 ** Byte offset 105 should also contain the page-size as a 16-bit
2368 ** integer.
2369 */
2370 const int aPreserve[] = {32, 36, 52, 60, 64, 68};
2371 u8 aHdr[108] = {
2372 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
2373 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00,
2374 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20,
2375 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
2376 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2377 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
2378 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
2379 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2380 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2381 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2382 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2383 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2384 0x00, 0x2e, 0x5b, 0x30,
2385
2386 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00
2387 };
2388 u8 *a = (u8*)aBuf;
2389
2390 u32 pgsz = recoverGetU16(&a[16]);
2391 u32 nReserve = a[20];
2392 u32 enc = recoverGetU32(&a[56]);
2393 u32 dbsz = 0;
2394 i64 dbFileSize = 0;
2395 int ii;
2396 sqlite3_recover *p = recover_g.p;
2397
2398 if( pgsz==0x01 ) pgsz = 65536;
2399 rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
2400
2401 if( rc==SQLITE_OK && p->detected_pgsz==0 ){
2402 rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
2403 }
2404 if( p->detected_pgsz ){
2405 pgsz = p->detected_pgsz;
2406 nReserve = p->nReserve;
2407 }
2408
2409 if( pgsz ){
2410 dbsz = dbFileSize / pgsz;
2411 }
2412 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){
2413 enc = SQLITE_UTF8;
2414 }
2415
2416 sqlite3_free(p->pPage1Cache);
2417 p->pPage1Cache = 0;
2418 p->pPage1Disk = 0;
2419
2420 p->pgsz = nByte;
2421 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2);
2422 if( p->pPage1Cache ){
2423 p->pPage1Disk = &p->pPage1Cache[nByte];
2424 memcpy(p->pPage1Disk, aBuf, nByte);
2425 aHdr[18] = a[18];
2426 aHdr[19] = a[19];
2427 recoverPutU32(&aHdr[28], dbsz);
2428 recoverPutU32(&aHdr[56], enc);
2429 recoverPutU16(&aHdr[105], pgsz-nReserve);
2430 if( pgsz==65536 ) pgsz = 1;
2431 recoverPutU16(&aHdr[16], pgsz);
2432 aHdr[20] = nReserve;
2433 for(ii=0; ii<(int)(sizeof(aPreserve)/sizeof(aPreserve[0])); ii++){
2434 memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
2435 }
2436 memcpy(aBuf, aHdr, sizeof(aHdr));
2437 memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr));
2438
2439 memcpy(p->pPage1Cache, aBuf, nByte);
2440 }else{
2441 rc = p->errCode;
2442 }
2443
2444 }
2445 pFd->pMethods = &recover_methods;
2446 }else{
2447 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff);
2448 }
2449 return rc;
2450 }
2451
2452 /*
2453 ** Used to make sqlite3_io_methods wrapper methods less verbose.
2454 */
2455 #define RECOVER_VFS_WRAPPER(code) \
2456 int rc = SQLITE_OK; \
2457 if( pFd->pMethods==&recover_methods ){ \
2458 pFd->pMethods = recover_g.pMethods; \
2459 rc = code; \
2460 pFd->pMethods = &recover_methods; \
2461 }else{ \
2462 rc = code; \
2463 } \
2464 return rc;
2465
2466 /*
2467 ** Methods of the wrapper VFS. All methods except for xRead() and xClose()
2468 ** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent
2469 ** method on the lower level VFS, then reinstall the wrapper before returning.
2470 ** Those that return an integer value use the RECOVER_VFS_WRAPPER macro.
2471 */
2472 static int recoverVfsWrite(
2473 sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff
2474 ){
2475 RECOVER_VFS_WRAPPER (
2476 pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff)
2477 );
2478 }
2479 static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){
2480 RECOVER_VFS_WRAPPER (
2481 pFd->pMethods->xTruncate(pFd, size)
2482 );
2483 }
2484 static int recoverVfsSync(sqlite3_file *pFd, int flags){
2485 RECOVER_VFS_WRAPPER (
2486 pFd->pMethods->xSync(pFd, flags)
2487 );
2488 }
2489 static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){
2490 RECOVER_VFS_WRAPPER (
2491 pFd->pMethods->xFileSize(pFd, pSize)
2492 );
2493 }
2494 static int recoverVfsLock(sqlite3_file *pFd, int eLock){
2495 RECOVER_VFS_WRAPPER (
2496 pFd->pMethods->xLock(pFd, eLock)
2497 );
2498 }
2499 static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){
2500 RECOVER_VFS_WRAPPER (
2501 pFd->pMethods->xUnlock(pFd, eLock)
2502 );
2503 }
2504 static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){
2505 RECOVER_VFS_WRAPPER (
2506 pFd->pMethods->xCheckReservedLock(pFd, pResOut)
2507 );
2508 }
2509 static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){
2510 RECOVER_VFS_WRAPPER (
2511 (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND)
2512 );
2513 }
2514 static int recoverVfsSectorSize(sqlite3_file *pFd){
2515 RECOVER_VFS_WRAPPER (
2516 pFd->pMethods->xSectorSize(pFd)
2517 );
2518 }
2519 static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){
2520 RECOVER_VFS_WRAPPER (
2521 pFd->pMethods->xDeviceCharacteristics(pFd)
2522 );
2523 }
2524 static int recoverVfsShmMap(
2525 sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp
2526 ){
2527 RECOVER_VFS_WRAPPER (
2528 pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp)
2529 );
2530 }
2531 static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){
2532 RECOVER_VFS_WRAPPER (
2533 pFd->pMethods->xShmLock(pFd, offset, n, flags)
2534 );
2535 }
2536 static void recoverVfsShmBarrier(sqlite3_file *pFd){
2537 if( pFd->pMethods==&recover_methods ){
2538 pFd->pMethods = recover_g.pMethods;
2539 pFd->pMethods->xShmBarrier(pFd);
2540 pFd->pMethods = &recover_methods;
2541 }else{
2542 pFd->pMethods->xShmBarrier(pFd);
2543 }
2544 }
2545 static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){
2546 RECOVER_VFS_WRAPPER (
2547 pFd->pMethods->xShmUnmap(pFd, deleteFlag)
2548 );
2549 }
2550
2551 static int recoverVfsFetch(
2552 sqlite3_file *pFd,
2553 sqlite3_int64 iOff,
2554 int iAmt,
2555 void **pp
2556 ){
2557 (void)pFd;
2558 (void)iOff;
2559 (void)iAmt;
2560 *pp = 0;
2561 return SQLITE_OK;
2562 }
2563 static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){
2564 (void)pFd;
2565 (void)iOff;
2566 (void)p;
2567 return SQLITE_OK;
2568 }
2569
2570 /*
2571 ** Install the VFS wrapper around the file-descriptor open on the input
2572 ** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held
2573 ** when this function is called.
2574 */
2575 static void recoverInstallWrapper(sqlite3_recover *p){
2576 sqlite3_file *pFd = 0;
2577 assert( recover_g.pMethods==0 );
2578 recoverAssertMutexHeld();
2579 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd);
2580 assert( pFd==0 || pFd->pMethods!=&recover_methods );
2581 if( pFd && pFd->pMethods ){
2582 int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0);
2583 recover_g.pMethods = pFd->pMethods;
2584 recover_g.p = p;
2585 recover_methods.iVersion = iVersion;
2586 pFd->pMethods = &recover_methods;
2587 }
2588 }
2589
2590 /*
2591 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
2592 ** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be
2593 ** held when this function is called.
2594 */
2595 static void recoverUninstallWrapper(sqlite3_recover *p){
2596 sqlite3_file *pFd = 0;
2597 recoverAssertMutexHeld();
2598 sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd);
2599 if( pFd && pFd->pMethods ){
2600 pFd->pMethods = recover_g.pMethods;
2601 recover_g.pMethods = 0;
2602 recover_g.p = 0;
2603 }
2604 }
2605
2606 /*
2607 ** This function does the work of a single sqlite3_recover_step() call. It
2608 ** is guaranteed that the handle is not in an error state when this
2609 ** function is called.
2610 */
2611 static void recoverStep(sqlite3_recover *p){
2612 assert( p && p->errCode==SQLITE_OK );
2613 switch( p->eState ){
2614 case RECOVER_STATE_INIT: {
2615 int bUseWrapper = 1;
2616 /* This is the very first call to sqlite3_recover_step() on this object.
2617 */
2618 recoverSqlCallback(p, "BEGIN");
2619 recoverSqlCallback(p, "PRAGMA writable_schema = on");
2620 recoverSqlCallback(p, "PRAGMA foreign_keys = off");
2621
2622 recoverEnterMutex();
2623
2624 /* Open the output database. And register required virtual tables and
2625 ** user functions with the new handle. */
2626 recoverOpenOutput(p);
2627
2628 /* Attempt to open a transaction and read page 1 of the input database.
2629 ** Two attempts may be made - one with a wrapper installed to ensure
2630 ** that the database header is sane, and then if that attempt returns
2631 ** SQLITE_NOTADB, then again with no wrapper. The second attempt is
2632 ** required for encrypted databases. */
2633 if( p->errCode==SQLITE_OK ){
2634 do{
2635 p->errCode = SQLITE_OK;
2636 if( bUseWrapper ) recoverInstallWrapper(p);
2637
2638 /* Open a transaction on the input database. */
2639 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0);
2640 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on");
2641 recoverExec(p, p->dbIn, "BEGIN");
2642 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1;
2643 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema");
2644 recoverTransferSettings(p);
2645 recoverOpenRecovery(p);
2646 recoverCacheSchema(p);
2647
2648 if( bUseWrapper ) recoverUninstallWrapper(p);
2649 }while( p->errCode==SQLITE_NOTADB
2650 && (bUseWrapper--)
2651 && SQLITE_OK==recoverOneStmt(p->dbIn, "ROLLBACK")
2652 );
2653 }
2654
2655 recoverLeaveMutex();
2656 recoverExec(p, p->dbOut, "BEGIN");
2657 recoverWriteSchema1(p);
2658 p->eState = RECOVER_STATE_WRITING;
2659 break;
2660 }
2661
2662 case RECOVER_STATE_WRITING: {
2663 if( p->w1.pTbls==0 ){
2664 recoverWriteDataInit(p);
2665 }
2666 if( SQLITE_DONE==recoverWriteDataStep(p) ){
2667 recoverWriteDataCleanup(p);
2668 if( p->zLostAndFound ){
2669 p->eState = RECOVER_STATE_LOSTANDFOUND1;
2670 }else{
2671 p->eState = RECOVER_STATE_SCHEMA2;
2672 }
2673 }
2674 break;
2675 }
2676
2677 case RECOVER_STATE_LOSTANDFOUND1: {
2678 if( p->laf.pUsed==0 ){
2679 recoverLostAndFound1Init(p);
2680 }
2681 if( SQLITE_DONE==recoverLostAndFound1Step(p) ){
2682 p->eState = RECOVER_STATE_LOSTANDFOUND2;
2683 }
2684 break;
2685 }
2686 case RECOVER_STATE_LOSTANDFOUND2: {
2687 if( p->laf.pAllAndParent==0 ){
2688 recoverLostAndFound2Init(p);
2689 }
2690 if( SQLITE_DONE==recoverLostAndFound2Step(p) ){
2691 p->eState = RECOVER_STATE_LOSTANDFOUND3;
2692 }
2693 break;
2694 }
2695
2696 case RECOVER_STATE_LOSTANDFOUND3: {
2697 if( p->laf.pInsert==0 ){
2698 recoverLostAndFound3Init(p);
2699 }
2700 if( SQLITE_DONE==recoverLostAndFound3Step(p) ){
2701 p->eState = RECOVER_STATE_SCHEMA2;
2702 }
2703 break;
2704 }
2705
2706 case RECOVER_STATE_SCHEMA2: {
2707 int rc = SQLITE_OK;
2708
2709 recoverWriteSchema2(p);
2710 p->eState = RECOVER_STATE_DONE;
2711
2712 /* If no error has occurred, commit the write transaction on the output
2713 ** database. Regardless of whether or not an error has occurred, make
2714 ** an attempt to end the read transaction on the input database. */
2715 recoverExec(p, p->dbOut, "COMMIT");
2716 rc = recoverOneStmt(p->dbIn, "END");
2717 if( p->errCode==SQLITE_OK ) p->errCode = rc;
2718
2719 recoverSqlCallback(p, "PRAGMA writable_schema = off");
2720 recoverSqlCallback(p, "COMMIT");
2721 p->eState = RECOVER_STATE_DONE;
2722 recoverFinalCleanup(p);
2723 break;
2724 };
2725
2726 case RECOVER_STATE_DONE: {
2727 /* no-op */
2728 break;
2729 };
2730 }
2731 }
2732
2733
2734 /*
2735 ** This is a worker function that does the heavy lifting for both init
2736 ** functions:
2737 **
2738 ** sqlite3_recover_init()
2739 ** sqlite3_recover_init_sql()
2740 **
2741 ** All this function does is allocate space for the recover handle and
2742 ** take copies of the input parameters. All the real work is done within
2743 ** sqlite3_recover_run().
2744 */
2745 sqlite3_recover *recoverInit(
2746 sqlite3* db,
2747 const char *zDb,
2748 const char *zUri, /* Output URI for _recover_init() */
2749 int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */
2750 void *pSqlCtx /* Context arg for _recover_init_sql() */
2751 ){
2752 sqlite3_recover *pRet = 0;
2753 int nDb = 0;
2754 int nUri = 0;
2755 int nByte = 0;
2756
2757 if( zDb==0 ){ zDb = "main"; }
2758
2759 nDb = recoverStrlen(zDb);
2760 nUri = recoverStrlen(zUri);
2761
2762 nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1;
2763 pRet = (sqlite3_recover*)sqlite3_malloc(nByte);
2764 if( pRet ){
2765 memset(pRet, 0, nByte);
2766 pRet->dbIn = db;
2767 pRet->zDb = (char*)&pRet[1];
2768 pRet->zUri = &pRet->zDb[nDb+1];
2769 memcpy(pRet->zDb, zDb, nDb);
2770 if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri);
2771 pRet->xSql = xSql;
2772 pRet->pSqlCtx = pSqlCtx;
2773 pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT;
2774 }
2775
2776 return pRet;
2777 }
2778
2779 /*
2780 ** Initialize a recovery handle that creates a new database containing
2781 ** the recovered data.
2782 */
2783 sqlite3_recover *sqlite3_recover_init(
2784 sqlite3* db,
2785 const char *zDb,
2786 const char *zUri
2787 ){
2788 return recoverInit(db, zDb, zUri, 0, 0);
2789 }
2790
2791 /*
2792 ** Initialize a recovery handle that returns recovered data in the
2793 ** form of SQL statements via a callback.
2794 */
2795 sqlite3_recover *sqlite3_recover_init_sql(
2796 sqlite3* db,
2797 const char *zDb,
2798 int (*xSql)(void*, const char*),
2799 void *pSqlCtx
2800 ){
2801 return recoverInit(db, zDb, 0, xSql, pSqlCtx);
2802 }
2803
2804 /*
2805 ** Return the handle error message, if any.
2806 */
2807 const char *sqlite3_recover_errmsg(sqlite3_recover *p){
2808 return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory";
2809 }
2810
2811 /*
2812 ** Return the handle error code.
2813 */
2814 int sqlite3_recover_errcode(sqlite3_recover *p){
2815 return p ? p->errCode : SQLITE_NOMEM;
2816 }
2817
2818 /*
2819 ** Configure the handle.
2820 */
2821 int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){
2822 int rc = SQLITE_OK;
2823 if( p==0 ){
2824 rc = SQLITE_NOMEM;
2825 }else if( p->eState!=RECOVER_STATE_INIT ){
2826 rc = SQLITE_MISUSE;
2827 }else{
2828 switch( op ){
2829 case 789:
2830 /* This undocumented magic configuration option is used to set the
2831 ** name of the auxiliary database that is ATTACH-ed to the database
2832 ** connection and used to hold state information during the
2833 ** recovery process. This option is for debugging use only and
2834 ** is subject to change or removal at any time. */
2835 sqlite3_free(p->zStateDb);
2836 p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg);
2837 break;
2838
2839 case SQLITE_RECOVER_LOST_AND_FOUND: {
2840 const char *zArg = (const char*)pArg;
2841 sqlite3_free(p->zLostAndFound);
2842 if( zArg ){
2843 p->zLostAndFound = recoverMPrintf(p, "%s", zArg);
2844 }else{
2845 p->zLostAndFound = 0;
2846 }
2847 break;
2848 }
2849
2850 case SQLITE_RECOVER_FREELIST_CORRUPT:
2851 p->bFreelistCorrupt = *(int*)pArg;
2852 break;
2853
2854 case SQLITE_RECOVER_ROWIDS:
2855 p->bRecoverRowid = *(int*)pArg;
2856 break;
2857
2858 case SQLITE_RECOVER_SLOWINDEXES:
2859 p->bSlowIndexes = *(int*)pArg;
2860 break;
2861
2862 default:
2863 rc = SQLITE_NOTFOUND;
2864 break;
2865 }
2866 }
2867
2868 return rc;
2869 }
2870
2871 /*
2872 ** Do a unit of work towards the recovery job. Return SQLITE_OK if
2873 ** no error has occurred but database recovery is not finished, SQLITE_DONE
2874 ** if database recovery has been successfully completed, or an SQLite
2875 ** error code if an error has occurred.
2876 */
2877 int sqlite3_recover_step(sqlite3_recover *p){
2878 if( p==0 ) return SQLITE_NOMEM;
2879 if( p->errCode==SQLITE_OK ) recoverStep(p);
2880 if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){
2881 return SQLITE_DONE;
2882 }
2883 return p->errCode;
2884 }
2885
2886 /*
2887 ** Do the configured recovery operation. Return SQLITE_OK if successful, or
2888 ** else an SQLite error code.
2889 */
2890 int sqlite3_recover_run(sqlite3_recover *p){
2891 while( SQLITE_OK==sqlite3_recover_step(p) );
2892 return sqlite3_recover_errcode(p);
2893 }
2894
2895
2896 /*
2897 ** Free all resources associated with the recover handle passed as the only
2898 ** argument. The results of using a handle with any sqlite3_recover_**
2899 ** API function after it has been passed to this function are undefined.
2900 **
2901 ** A copy of the value returned by the first call made to sqlite3_recover_run()
2902 ** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has
2903 ** not been called on this handle.
2904 */
2905 int sqlite3_recover_finish(sqlite3_recover *p){
2906 int rc;
2907 if( p==0 ){
2908 rc = SQLITE_NOMEM;
2909 }else{
2910 recoverFinalCleanup(p);
2911 if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){
2912 rc = recoverOneStmt(p->dbIn, "END");
2913 if( p->errCode==SQLITE_OK ) p->errCode = rc;
2914 }
2915 rc = p->errCode;
2916 sqlite3_free(p->zErrMsg);
2917 sqlite3_free(p->zStateDb);
2918 sqlite3_free(p->zLostAndFound);
2919 sqlite3_free(p->pPage1Cache);
2920 sqlite3_free(p);
2921 }
2922 return rc;
2923 }
2924
2925 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */