Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1184)

Side by Side Diff: third_party/sqlite/patches/0005-Virtual-table-supporting-recovery-of-corrupted-datab.patch

Issue 2751253002: [sql] Import SQLite 3.17.0. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 From d176c774ba1a8b431400f38ca71459bf148f0c3a Mon Sep 17 00:00:00 2001
2 From: Scott Hess <shess@chromium.org>
3 Date: Sat, 20 Jul 2013 11:42:21 -0700
4 Subject: [PATCH 05/13] Virtual table supporting recovery of corrupted
5 databases.
6
7 "recover" implements a virtual table which uses the SQLite pager layer
8 to read table pages and pull out the data which is structurally sound
9 (at least at the storage layer).
10
11 BUG=109482
12
13 Since this implements a new feature for SQLite, the review URLs aren't
14 listed. This patch and the top of recover.c should be considered
15 authoritative. The history is mostly under
16 third_party/sqlite/src/src/{recover,recover-alt}.c .
17 ---
18 third_party/sqlite/src/main.mk | 6 +-
19 third_party/sqlite/src/src/main.c | 8 +
20 third_party/sqlite/src/src/recover.c | 2270 +++++++++++++++++++++++++++
21 third_party/sqlite/src/src/recover.h | 23 +
22 third_party/sqlite/src/src/recover_varint.c | 201 +++
23 third_party/sqlite/src/test/recover.test | 164 ++
24 third_party/sqlite/src/test/recover0.test | 532 +++++++
25 third_party/sqlite/src/test/recover1.test | 429 +++++
26 third_party/sqlite/src/test/recover2.test | 157 ++
27 9 files changed, 3789 insertions(+), 1 deletion(-)
28 create mode 100644 third_party/sqlite/src/src/recover.c
29 create mode 100644 third_party/sqlite/src/src/recover.h
30 create mode 100644 third_party/sqlite/src/src/recover_varint.c
31 create mode 100644 third_party/sqlite/src/test/recover.test
32 create mode 100644 third_party/sqlite/src/test/recover0.test
33 create mode 100644 third_party/sqlite/src/test/recover1.test
34 create mode 100644 third_party/sqlite/src/test/recover2.test
35
36 diff --git a/third_party/sqlite/src/main.mk b/third_party/sqlite/src/main.mk
37 index 6ff3bd4..26f9f15 100644
38 --- a/third_party/sqlite/src/main.mk
39 +++ b/third_party/sqlite/src/main.mk
40 @@ -67,7 +67,8 @@ LIBOBJ+= vdbe.o parse.o \
41 mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
42 notify.o opcodes.o os.o os_unix.o os_win.o \
43 pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
44 - random.o resolve.o rowset.o rtree.o select.o sqlite3rbu.o status.o \
45 + random.o recover.o recover_varint.o resolve.o rowset.o rtree.o \
46 + select.o sqlite3rbu.o status.o \
47 table.o threads.o tokenize.o treeview.o trigger.o \
48 update.o userauth.o util.o vacuum.o \
49 vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
50 @@ -360,6 +361,8 @@ TESTSRC2 = \
51 $(TOP)/src/prepare.c \
52 $(TOP)/src/printf.c \
53 $(TOP)/src/random.c \
54 + $(TOP)/src/recover.c \
55 + $(TOP)/src/recover_varint.c \
56 $(TOP)/src/pcache.c \
57 $(TOP)/src/pcache1.c \
58 $(TOP)/src/select.c \
59 @@ -720,6 +723,7 @@ sqlite3_analyzer$(EXE): sqlite3_analyzer.c
60 #
61 TESTFIXTURE_FLAGS = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
62 TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
63 +TESTFIXTURE_FLAGS += -DDEFAULT_ENABLE_RECOVER=1
64
65 testfixture$(EXE): $(TESTSRC2) libsqlite3.a $(TESTSRC) $(TOP)/src/tclsqlite.c
66 $(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS) \
67 diff --git a/third_party/sqlite/src/src/main.c b/third_party/sqlite/src/src/main .c
68 index 3be7c77..301808c 100644
69 --- a/third_party/sqlite/src/src/main.c
70 +++ b/third_party/sqlite/src/src/main.c
71 @@ -2927,6 +2927,14 @@ static int openDatabase(
72 }
73 #endif
74
75 +#ifdef DEFAULT_ENABLE_RECOVER
76 + /* Initialize recover virtual table for testing. */
77 + extern int recoverVtableInit(sqlite3 *db);
78 + if( !db->mallocFailed && rc==SQLITE_OK ){
79 + rc = recoverVtableInit(db);
80 + }
81 +#endif
82 +
83 #ifdef SQLITE_ENABLE_ICU
84 if( !db->mallocFailed && rc==SQLITE_OK ){
85 rc = sqlite3IcuInit(db);
86 diff --git a/third_party/sqlite/src/src/recover.c b/third_party/sqlite/src/src/r ecover.c
87 new file mode 100644
88 index 0000000..c22fd4d
89 --- /dev/null
90 +++ b/third_party/sqlite/src/src/recover.c
91 @@ -0,0 +1,2270 @@
92 +/*
93 +** 2012 Jan 11
94 +**
95 +** The author disclaims copyright to this source code. In place of
96 +** a legal notice, here is a blessing:
97 +**
98 +** May you do good and not evil.
99 +** May you find forgiveness for yourself and forgive others.
100 +** May you share freely, never taking more than you give.
101 +*/
102 +/* TODO(shess): THIS MODULE IS STILL EXPERIMENTAL. DO NOT USE IT. */
103 +/* Implements a virtual table "recover" which can be used to recover
104 + * data from a corrupt table. The table is walked manually, with
105 + * corrupt items skipped. Additionally, any errors while reading will
106 + * be skipped.
107 + *
108 + * Given a table with this definition:
109 + *
110 + * CREATE TABLE Stuff (
111 + * name TEXT PRIMARY KEY,
112 + * value TEXT NOT NULL
113 + * );
114 + *
115 + * to recover the data from teh table, you could do something like:
116 + *
117 + * -- Attach another database, the original is not trustworthy.
118 + * ATTACH DATABASE '/tmp/db.db' AS rdb;
119 + * -- Create a new version of the table.
120 + * CREATE TABLE rdb.Stuff (
121 + * name TEXT PRIMARY KEY,
122 + * value TEXT NOT NULL
123 + * );
124 + * -- This will read the original table's data.
125 + * CREATE VIRTUAL TABLE temp.recover_Stuff using recover(
126 + * main.Stuff,
127 + * name TEXT STRICT NOT NULL, -- only real TEXT data allowed
128 + * value TEXT STRICT NOT NULL
129 + * );
130 + * -- Corruption means the UNIQUE constraint may no longer hold for
131 + * -- Stuff, so either OR REPLACE or OR IGNORE must be used.
132 + * INSERT OR REPLACE INTO rdb.Stuff (rowid, name, value )
133 + * SELECT rowid, name, value FROM temp.recover_Stuff;
134 + * DROP TABLE temp.recover_Stuff;
135 + * DETACH DATABASE rdb;
136 + * -- Move db.db to replace original db in filesystem.
137 + *
138 + *
139 + * Usage
140 + *
141 + * Given the goal of dealing with corruption, it would not be safe to
142 + * create a recovery table in the database being recovered. So
143 + * recovery tables must be created in the temp database. They are not
144 + * appropriate to persist, in any case. [As a bonus, sqlite_master
145 + * tables can be recovered. Perhaps more cute than useful, though.]
146 + *
147 + * The parameters are a specifier for the table to read, and a column
148 + * definition for each bit of data stored in that table. The named
149 + * table must be convertable to a root page number by reading the
150 + * sqlite_master table. Bare table names are assumed to be in
151 + * database 0 ("main"), other databases can be specified in db.table
152 + * fashion.
153 + *
154 + * Column definitions are similar to BUT NOT THE SAME AS those
155 + * provided to CREATE statements:
156 + * column-def: column-name [type-name [STRICT] [NOT NULL]]
157 + * type-name: (ANY|ROWID|INTEGER|FLOAT|NUMERIC|TEXT|BLOB)
158 + *
159 + * Only those exact type names are accepted, there is no type
160 + * intuition. The only constraints accepted are STRICT (see below)
161 + * and NOT NULL. Anything unexpected will cause the create to fail.
162 + *
163 + * ANY is a convenience to indicate that manifest typing is desired.
164 + * It is equivalent to not specifying a type at all. The results for
165 + * such columns will have the type of the data's storage. The exposed
166 + * schema will contain no type for that column.
167 + *
168 + * ROWID is used for columns representing aliases to the rowid
169 + * (INTEGER PRIMARY KEY, with or without AUTOINCREMENT), to make the
170 + * concept explicit. Such columns are actually stored as NULL, so
171 + * they cannot be simply ignored. The exposed schema will be INTEGER
172 + * for that column.
173 + *
174 + * NOT NULL causes rows with a NULL in that column to be skipped. It
175 + * also adds NOT NULL to the column in the exposed schema. If the
176 + * table has ever had columns added using ALTER TABLE, then those
177 + * columns implicitly contain NULL for rows which have not been
178 + * updated. [Workaround using COALESCE() in your SELECT statement.]
179 + *
180 + * The created table is read-only, with no indices. Any SELECT will
181 + * be a full-table scan, returning each valid row read from the
182 + * storage of the backing table. The rowid will be the rowid of the
183 + * row from the backing table. "Valid" means:
184 + * - The cell metadata for the row is well-formed. Mainly this means that
185 + * the cell header info describes a payload of the size indicated by
186 + * the cell's payload size.
187 + * - The cell does not run off the page.
188 + * - The cell does not overlap any other cell on the page.
189 + * - The cell contains doesn't contain too many columns.
190 + * - The types of the serialized data match the indicated types (see below).
191 + *
192 + *
193 + * Type affinity versus type storage.
194 + *
195 + * http://www.sqlite.org/datatype3.html describes SQLite's type
196 + * affinity system. The system provides for automated coercion of
197 + * types in certain cases, transparently enough that many developers
198 + * do not realize that it is happening. Importantly, it implies that
199 + * the raw data stored in the database may not have the obvious type.
200 + *
201 + * Differences between the stored data types and the expected data
202 + * types may be a signal of corruption. This module makes some
203 + * allowances for automatic coercion. It is important to be concious
204 + * of the difference between the schema exposed by the module, and the
205 + * data types read from storage. The following table describes how
206 + * the module interprets things:
207 + *
208 + * type schema data STRICT
209 + * ---- ------ ---- ------
210 + * ANY <none> any any
211 + * ROWID INTEGER n/a n/a
212 + * INTEGER INTEGER integer integer
213 + * FLOAT FLOAT integer or float float
214 + * NUMERIC NUMERIC integer, float, or text integer or float
215 + * TEXT TEXT text or blob text
216 + * BLOB BLOB blob blob
217 + *
218 + * type is the type provided to the recover module, schema is the
219 + * schema exposed by the module, data is the acceptable types of data
220 + * decoded from storage, and STRICT is a modification of that.
221 + *
222 + * A very loose recovery system might use ANY for all columns, then
223 + * use the appropriate sqlite3_column_*() calls to coerce to expected
224 + * types. This doesn't provide much protection if a page from a
225 + * different table with the same column count is linked into an
226 + * inappropriate btree.
227 + *
228 + * A very tight recovery system might use STRICT to enforce typing on
229 + * all columns, preferring to skip rows which are valid at the storage
230 + * level but don't contain the right types. Note that FLOAT STRICT is
231 + * almost certainly not appropriate, since integral values are
232 + * transparently stored as integers, when that is more efficient.
233 + *
234 + * Another option is to use ANY for all columns and inspect each
235 + * result manually (using sqlite3_column_*). This should only be
236 + * necessary in cases where developers have used manifest typing (test
237 + * to make sure before you decide that you aren't using manifest
238 + * typing!).
239 + *
240 + *
241 + * Caveats
242 + *
243 + * Leaf pages not referenced by interior nodes will not be found.
244 + *
245 + * Leaf pages referenced from interior nodes of other tables will not
246 + * be resolved.
247 + *
248 + * Rows referencing invalid overflow pages will be skipped.
249 + *
250 + * SQlite rows have a header which describes how to interpret the rest
251 + * of the payload. The header can be valid in cases where the rest of
252 + * the record is actually corrupt (in the sense that the data is not
253 + * the intended data). This can especially happen WRT overflow pages,
254 + * as lack of atomic updates between pages is the primary form of
255 + * corruption I have seen in the wild.
256 + */
257 +/* The implementation is via a series of cursors. The cursor
258 + * implementations follow the pattern:
259 + *
260 + * // Creates the cursor using various initialization info.
261 + * int cursorCreate(...);
262 + *
263 + * // Returns 1 if there is no more data, 0 otherwise.
264 + * int cursorEOF(Cursor *pCursor);
265 + *
266 + * // Various accessors can be used if not at EOF.
267 + *
268 + * // Move to the next item.
269 + * int cursorNext(Cursor *pCursor);
270 + *
271 + * // Destroy the memory associated with the cursor.
272 + * void cursorDestroy(Cursor *pCursor);
273 + *
274 + * References in the following are to sections at
275 + * http://www.sqlite.org/fileformat2.html .
276 + *
277 + * RecoverLeafCursor iterates the records in a leaf table node
278 + * described in section 1.5 "B-tree Pages". When the node is
279 + * exhausted, an interior cursor is used to get the next leaf node,
280 + * and iteration continues there.
281 + *
282 + * RecoverInteriorCursor iterates the child pages in an interior table
283 + * node described in section 1.5 "B-tree Pages". When the node is
284 + * exhausted, a parent interior cursor is used to get the next
285 + * interior node at the same level, and iteration continues there.
286 + *
287 + * Together these record the path from the leaf level to the root of
288 + * the tree. Iteration happens from the leaves rather than the root
289 + * both for efficiency and putting the special case at the front of
290 + * the list is easier to implement.
291 + *
292 + * RecoverCursor uses a RecoverLeafCursor to iterate the rows of a
293 + * table, returning results via the SQLite virtual table interface.
294 + */
295 +/* TODO(shess): It might be useful to allow DEFAULT in types to
296 + * specify what to do for NULL when an ALTER TABLE case comes up.
297 + * Unfortunately, simply adding it to the exposed schema and using
298 + * sqlite3_result_null() does not cause the default to be generate.
299 + * Handling it ourselves seems hard, unfortunately.
300 + */
301 +
302 +#include <assert.h>
303 +#include <ctype.h>
304 +#include <stdint.h>
305 +#include <stdio.h>
306 +#include <string.h>
307 +
308 +#include "sqlite3.h"
309 +
310 +/* Some SQLite internals use, cribbed from fts5int.h. */
311 +#ifndef SQLITE_AMALGAMATION
312 +typedef uint8_t u8;
313 +typedef uint32_t u32;
314 +typedef sqlite3_int64 i64;
315 +typedef sqlite3_uint64 u64;
316 +
317 +#define ArraySize(x) (sizeof(x) / sizeof(x[0]))
318 +#endif
319 +
320 +/* From recover_varint.c. */
321 +u8 recoverGetVarint(const unsigned char *p, u64 *v);
322 +
323 +/* For debugging. */
324 +#if 0
325 +#define FNENTRY() fprintf(stderr, "In %s\n", __FUNCTION__)
326 +#else
327 +#define FNENTRY()
328 +#endif
329 +
330 +/* Generic constants and helper functions. */
331 +
332 +static const unsigned char kTableLeafPage = 0x0D;
333 +static const unsigned char kTableInteriorPage = 0x05;
334 +
335 +/* From section 1.2. */
336 +static const unsigned kiHeaderPageSizeOffset = 16;
337 +static const unsigned kiHeaderReservedSizeOffset = 20;
338 +static const unsigned kiHeaderEncodingOffset = 56;
339 +/* TODO(shess) |static const unsigned| fails creating the header in GetPager()
340 +** because |knHeaderSize| isn't |constexpr|. But this isn't C++, either.
341 +*/
342 +enum { knHeaderSize = 100};
343 +
344 +/* From section 1.5. */
345 +static const unsigned kiPageTypeOffset = 0;
346 +/* static const unsigned kiPageFreeBlockOffset = 1; */
347 +static const unsigned kiPageCellCountOffset = 3;
348 +/* static const unsigned kiPageCellContentOffset = 5; */
349 +/* static const unsigned kiPageFragmentedBytesOffset = 7; */
350 +static const unsigned knPageLeafHeaderBytes = 8;
351 +/* Interior pages contain an additional field. */
352 +static const unsigned kiPageRightChildOffset = 8;
353 +static const unsigned kiPageInteriorHeaderBytes = 12;
354 +
355 +/* Accepted types are specified by a mask. */
356 +#define MASK_ROWID (1<<0)
357 +#define MASK_INTEGER (1<<1)
358 +#define MASK_FLOAT (1<<2)
359 +#define MASK_TEXT (1<<3)
360 +#define MASK_BLOB (1<<4)
361 +#define MASK_NULL (1<<5)
362 +
363 +/* Helpers to decode fixed-size fields. */
364 +static u32 decodeUnsigned16(const unsigned char *pData){
365 + return (pData[0]<<8) + pData[1];
366 +}
367 +static u32 decodeUnsigned32(const unsigned char *pData){
368 + return (decodeUnsigned16(pData)<<16) + decodeUnsigned16(pData+2);
369 +}
370 +static i64 decodeSigned(const unsigned char *pData, unsigned nBytes){
371 + i64 r = (char)(*pData);
372 + while( --nBytes ){
373 + r <<= 8;
374 + r += *(++pData);
375 + }
376 + return r;
377 +}
378 +/* Derived from vdbeaux.c, sqlite3VdbeSerialGet(), case 7. */
379 +/* TODO(shess): Determine if swapMixedEndianFloat() applies. */
380 +static double decodeFloat64(const unsigned char *pData){
381 +#if !defined(NDEBUG)
382 + static const u64 t1 = ((u64)0x3ff00000)<<32;
383 + static const double r1 = 1.0;
384 + u64 t2 = t1;
385 + assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
386 +#endif
387 + i64 x = decodeSigned(pData, 8);
388 + double d;
389 + memcpy(&d, &x, sizeof(x));
390 + return d;
391 +}
392 +
393 +/* Return true if a varint can safely be read from pData/nData. */
394 +/* TODO(shess): DbPage points into the middle of a buffer which
395 + * contains the page data before DbPage. So code should always be
396 + * able to read a small number of varints safely. Consider whether to
397 + * trust that or not.
398 + */
399 +static int checkVarint(const unsigned char *pData, unsigned nData){
400 + unsigned i;
401 +
402 + /* In the worst case the decoder takes all 8 bits of the 9th byte. */
403 + if( nData>=9 ){
404 + return 1;
405 + }
406 +
407 + /* Look for a high-bit-clear byte in what's left. */
408 + for( i=0; i<nData; ++i ){
409 + if( !(pData[i]&0x80) ){
410 + return 1;
411 + }
412 + }
413 +
414 + /* Cannot decode in the space given. */
415 + return 0;
416 +}
417 +
418 +/* Return 1 if n varints can be read from pData/nData. */
419 +static int checkVarints(const unsigned char *pData, unsigned nData,
420 + unsigned n){
421 + unsigned nCur = 0; /* Byte offset within current varint. */
422 + unsigned nFound = 0; /* Number of varints found. */
423 + unsigned i;
424 +
425 + /* In the worst case the decoder takes all 8 bits of the 9th byte. */
426 + if( nData>=9*n ){
427 + return 1;
428 + }
429 +
430 + for( i=0; nFound<n && i<nData; ++i ){
431 + nCur++;
432 + if( nCur==9 || !(pData[i]&0x80) ){
433 + nFound++;
434 + nCur = 0;
435 + }
436 + }
437 +
438 + return nFound==n;
439 +}
440 +
441 +/* ctype and str[n]casecmp() can be affected by locale (eg, tr_TR).
442 + * These versions consider only the ASCII space.
443 + */
444 +/* TODO(shess): It may be reasonable to just remove the need for these
445 + * entirely. The module could require "TEXT STRICT NOT NULL", not
446 + * "Text Strict Not Null" or whatever the developer felt like typing
447 + * that day. Handling corrupt data is a PERFECT place to be pedantic.
448 + */
449 +static int ascii_isspace(char c){
450 + /* From fts3_expr.c */
451 + return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
452 +}
453 +static int ascii_isalnum(int x){
454 + /* From fts3_tokenizer1.c */
455 + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
456 +}
457 +static int ascii_tolower(int x){
458 + /* From fts3_tokenizer1.c */
459 + return (x>='A' && x<='Z') ? x-'A'+'a' : x;
460 +}
461 +/* TODO(shess): Consider sqlite3_strnicmp() */
462 +static int ascii_strncasecmp(const char *s1, const char *s2, size_t n){
463 + const unsigned char *us1 = (const unsigned char *)s1;
464 + const unsigned char *us2 = (const unsigned char *)s2;
465 + while( *us1 && *us2 && n && ascii_tolower(*us1)==ascii_tolower(*us2) ){
466 + us1++, us2++, n--;
467 + }
468 + return n ? ascii_tolower(*us1)-ascii_tolower(*us2) : 0;
469 +}
470 +static int ascii_strcasecmp(const char *s1, const char *s2){
471 + /* If s2 is equal through strlen(s1), will exit while() due to s1's
472 + * trailing NUL, and return NUL-s2[strlen(s1)].
473 + */
474 + return ascii_strncasecmp(s1, s2, strlen(s1)+1);
475 +}
476 +
477 +/* Provide access to the pages of a SQLite database in a way similar to SQLite' s
478 +** Pager.
479 +*/
480 +typedef struct RecoverPager RecoverPager;
481 +struct RecoverPager {
482 + sqlite3_file *pSqliteFile; /* Reference to database's file handle */
483 + u32 nPageSize; /* Size of pages in pSqliteFile */
484 +};
485 +
486 +static void pagerDestroy(RecoverPager *pPager){
487 + pPager->pSqliteFile->pMethods->xUnlock(pPager->pSqliteFile, SQLITE_LOCK_NONE) ;
488 + memset(pPager, 0xA5, sizeof(*pPager));
489 + sqlite3_free(pPager);
490 +}
491 +
492 +/* pSqliteFile should already have a SHARED lock. */
493 +static int pagerCreate(sqlite3_file *pSqliteFile, u32 nPageSize,
494 + RecoverPager **ppPager){
495 + RecoverPager *pPager = sqlite3_malloc(sizeof(RecoverPager));
496 + if( !pPager ){
497 + return SQLITE_NOMEM;
498 + }
499 +
500 + memset(pPager, 0, sizeof(*pPager));
501 + pPager->pSqliteFile = pSqliteFile;
502 + pPager->nPageSize = nPageSize;
503 + *ppPager = pPager;
504 + return SQLITE_OK;
505 +}
506 +
507 +/* Matches DbPage (aka PgHdr) from SQLite internals. */
508 +/* TODO(shess): SQLite by default allocates page metadata in a single allocatio n
509 +** such that the page's data and metadata are contiguous, see pcache1AllocPage
510 +** in pcache1.c. I believe this was intended to reduce malloc churn. It means
511 +** that Chromium's automated tooling would be unlikely to see page-buffer
512 +** overruns. I believe that this code is safe, but for now replicate SQLite's
513 +** approach with kExcessSpace.
514 +*/
515 +const int kExcessSpace = 128;
516 +typedef struct RecoverPage RecoverPage;
517 +struct RecoverPage {
518 + u32 pgno; /* Page number for this page */
519 + void *pData; /* Page data for pgno */
520 + RecoverPager *pPager; /* The pager this page is part of */
521 +};
522 +
523 +static void pageDestroy(RecoverPage *pPage){
524 + sqlite3_free(pPage->pData);
525 + memset(pPage, 0xA5, sizeof(*pPage));
526 + sqlite3_free(pPage);
527 +}
528 +
529 +static int pageCreate(RecoverPager *pPager, u32 pgno, RecoverPage **ppPage){
530 + RecoverPage *pPage = sqlite3_malloc(sizeof(RecoverPage));
531 + if( !pPage ){
532 + return SQLITE_NOMEM;
533 + }
534 +
535 + memset(pPage, 0, sizeof(*pPage));
536 + pPage->pPager = pPager;
537 + pPage->pgno = pgno;
538 + pPage->pData = sqlite3_malloc(pPager->nPageSize + kExcessSpace);
539 + if( pPage->pData==NULL ){
540 + pageDestroy(pPage);
541 + return SQLITE_NOMEM;
542 + }
543 + memset((u8 *)pPage->pData + pPager->nPageSize, 0, kExcessSpace);
544 +
545 + *ppPage = pPage;
546 + return SQLITE_OK;
547 +}
548 +
549 +static int pagerGetPage(RecoverPager *pPager, u32 iPage, RecoverPage **ppPage) {
550 + sqlite3_int64 iOfst;
551 + sqlite3_file *pFile = pPager->pSqliteFile;
552 + RecoverPage *pPage;
553 + int rc = pageCreate(pPager, iPage, &pPage);
554 + if( rc!=SQLITE_OK ){
555 + return rc;
556 + }
557 +
558 + /* xRead() can return SQLITE_IOERR_SHORT_READ, which should be treated as
559 + ** SQLITE_OK plus an EOF indicator. The excess space is zero-filled.
560 + */
561 + iOfst = ((sqlite3_int64)iPage - 1) * pPager->nPageSize;
562 + rc = pFile->pMethods->xRead(pFile, pPage->pData, pPager->nPageSize, iOfst);
563 + if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
564 + pageDestroy(pPage);
565 + return rc;
566 + }
567 +
568 + *ppPage = pPage;
569 + return SQLITE_OK;
570 +}
571 +
572 +/* For some reason I kept making mistakes with offset calculations. */
573 +static const unsigned char *PageData(RecoverPage *pPage, unsigned iOffset){
574 + assert( iOffset<=pPage->pPager->nPageSize );
575 + return (unsigned char *)pPage->pData + iOffset;
576 +}
577 +
578 +/* The first page in the file contains a file header in the first 100
579 + * bytes. The page's header information comes after that. Note that
580 + * the offsets in the page's header information are relative to the
581 + * beginning of the page, NOT the end of the page header.
582 + */
583 +static const unsigned char *PageHeader(RecoverPage *pPage){
584 + if( pPage->pgno==1 ){
585 + return PageData(pPage, knHeaderSize);
586 + }else{
587 + return PageData(pPage, 0);
588 + }
589 +}
590 +
591 +/* Helper to fetch the pager and page size for the named database. */
592 +static int GetPager(sqlite3 *db, const char *zName,
593 + RecoverPager **ppPager, unsigned *pnPageSize,
594 + int *piEncoding){
595 + int rc, iEncoding;
596 + unsigned nPageSize, nReservedSize;
597 + unsigned char header[knHeaderSize];
598 + sqlite3_file *pFile = NULL;
599 + RecoverPager *pPager;
600 +
601 + rc = sqlite3_file_control(db, zName, SQLITE_FCNTL_FILE_POINTER, &pFile);
602 + if( rc!=SQLITE_OK ) {
603 + return rc;
604 + } else if( pFile==NULL ){
605 + /* The documentation for sqlite3PagerFile() indicates it can return NULL if
606 + ** the file has not yet been opened. That should not be possible here...
607 + */
608 + return SQLITE_MISUSE;
609 + }
610 +
611 + /* Get a shared lock to make sure the on-disk version of the file is truth. * /
612 + rc = pFile->pMethods->xLock(pFile, SQLITE_LOCK_SHARED);
613 + if( rc != SQLITE_OK ){
614 + return rc;
615 + }
616 +
617 + /* Read the Initial header information. In case of SQLITE_IOERR_SHORT_READ,
618 + ** the header is incomplete, which means no data could be recovered anyhow.
619 + */
620 + rc = pFile->pMethods->xRead(pFile, header, sizeof(header), 0);
621 + if( rc != SQLITE_OK ){
622 + pFile->pMethods->xUnlock(pFile, SQLITE_LOCK_NONE);
623 + if( rc==SQLITE_IOERR_SHORT_READ ){
624 + return SQLITE_CORRUPT;
625 + }
626 + return rc;
627 + }
628 +
629 + /* Page size must be a power of two between 512 and 32768 inclusive. */
630 + nPageSize = decodeUnsigned16(header + kiHeaderPageSizeOffset);
631 + if( (nPageSize&(nPageSize-1)) || nPageSize>32768 || nPageSize<512 ){
632 + pFile->pMethods->xUnlock(pFile, SQLITE_LOCK_NONE);
633 + return rc;
634 + }
635 +
636 + /* Space reserved a the end of the page for extensions. Usually 0. */
637 + nReservedSize = header[kiHeaderReservedSizeOffset];
638 +
639 + /* 1 for UTF-8, 2 for UTF-16le, 3 for UTF-16be. */
640 + iEncoding = decodeUnsigned32(header + kiHeaderEncodingOffset);
641 + if( iEncoding==3 ){
642 + *piEncoding = SQLITE_UTF16BE;
643 + } else if( iEncoding==2 ){
644 + *piEncoding = SQLITE_UTF16LE;
645 + } else if( iEncoding==1 ){
646 + *piEncoding = SQLITE_UTF8;
647 + } else {
648 + /* This case should not be possible. */
649 + *piEncoding = SQLITE_UTF8;
650 + }
651 +
652 + rc = pagerCreate(pFile, nPageSize, &pPager);
653 + if( rc!=SQLITE_OK ){
654 + pFile->pMethods->xUnlock(pFile, SQLITE_LOCK_NONE);
655 + return rc;
656 + }
657 +
658 + *ppPager = pPager;
659 + *pnPageSize = nPageSize - nReservedSize;
660 + *piEncoding = iEncoding;
661 + return SQLITE_OK;
662 +}
663 +
664 +/* iSerialType is a type read from a record header. See "2.1 Record Format".
665 + */
666 +
667 +/* Storage size of iSerialType in bytes. My interpretation of SQLite
668 + * documentation is that text and blob fields can have 32-bit length.
669 + * Values past 2^31-12 will need more than 32 bits to encode, which is
670 + * why iSerialType is u64.
671 + */
672 +static u32 SerialTypeLength(u64 iSerialType){
673 + switch( iSerialType ){
674 + case 0 : return 0; /* NULL */
675 + case 1 : return 1; /* Various integers. */
676 + case 2 : return 2;
677 + case 3 : return 3;
678 + case 4 : return 4;
679 + case 5 : return 6;
680 + case 6 : return 8;
681 + case 7 : return 8; /* 64-bit float. */
682 + case 8 : return 0; /* Constant 0. */
683 + case 9 : return 0; /* Constant 1. */
684 + case 10 : case 11 : assert( "RESERVED TYPE"==NULL ); return 0;
685 + }
686 + return (u32)((iSerialType>>1) - 6);
687 +}
688 +
689 +/* True if iSerialType refers to a blob. */
690 +static int SerialTypeIsBlob(u64 iSerialType){
691 + assert( iSerialType>=12 );
692 + return (iSerialType%2)==0;
693 +}
694 +
695 +/* Returns true if the serialized type represented by iSerialType is
696 + * compatible with the given type mask.
697 + */
698 +static int SerialTypeIsCompatible(u64 iSerialType, unsigned char mask){
699 + switch( iSerialType ){
700 + case 0 : return (mask&MASK_NULL)!=0;
701 + case 1 : return (mask&MASK_INTEGER)!=0;
702 + case 2 : return (mask&MASK_INTEGER)!=0;
703 + case 3 : return (mask&MASK_INTEGER)!=0;
704 + case 4 : return (mask&MASK_INTEGER)!=0;
705 + case 5 : return (mask&MASK_INTEGER)!=0;
706 + case 6 : return (mask&MASK_INTEGER)!=0;
707 + case 7 : return (mask&MASK_FLOAT)!=0;
708 + case 8 : return (mask&MASK_INTEGER)!=0;
709 + case 9 : return (mask&MASK_INTEGER)!=0;
710 + case 10 : assert( "RESERVED TYPE"==NULL ); return 0;
711 + case 11 : assert( "RESERVED TYPE"==NULL ); return 0;
712 + }
713 + return (mask&(SerialTypeIsBlob(iSerialType) ? MASK_BLOB : MASK_TEXT));
714 +}
715 +
716 +/* Versions of strdup() with return values appropriate for
717 + * sqlite3_free(). malloc.c has sqlite3DbStrDup()/NDup(), but those
718 + * need sqlite3DbFree(), which seems intrusive.
719 + */
720 +static char *sqlite3_strndup(const char *z, unsigned n){
721 + char *zNew;
722 +
723 + if( z==NULL ){
724 + return NULL;
725 + }
726 +
727 + zNew = sqlite3_malloc(n+1);
728 + if( zNew!=NULL ){
729 + memcpy(zNew, z, n);
730 + zNew[n] = '\0';
731 + }
732 + return zNew;
733 +}
734 +static char *sqlite3_strdup(const char *z){
735 + if( z==NULL ){
736 + return NULL;
737 + }
738 + return sqlite3_strndup(z, strlen(z));
739 +}
740 +
741 +/* Fetch the page number of zTable in zDb from sqlite_master in zDb,
742 + * and put it in *piRootPage.
743 + */
744 +static int getRootPage(sqlite3 *db, const char *zDb, const char *zTable,
745 + u32 *piRootPage){
746 + char *zSql; /* SQL selecting root page of named element. */
747 + sqlite3_stmt *pStmt;
748 + int rc;
749 +
750 + if( strcmp(zTable, "sqlite_master")==0 ){
751 + *piRootPage = 1;
752 + return SQLITE_OK;
753 + }
754 +
755 + zSql = sqlite3_mprintf("SELECT rootpage FROM %s.sqlite_master "
756 + "WHERE type = 'table' AND tbl_name = %Q",
757 + zDb, zTable);
758 + if( !zSql ){
759 + return SQLITE_NOMEM;
760 + }
761 +
762 + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
763 + sqlite3_free(zSql);
764 + if( rc!=SQLITE_OK ){
765 + return rc;
766 + }
767 +
768 + /* Require a result. */
769 + rc = sqlite3_step(pStmt);
770 + if( rc==SQLITE_DONE ){
771 + rc = SQLITE_CORRUPT;
772 + }else if( rc==SQLITE_ROW ){
773 + *piRootPage = sqlite3_column_int(pStmt, 0);
774 +
775 + /* Require only one result. */
776 + rc = sqlite3_step(pStmt);
777 + if( rc==SQLITE_DONE ){
778 + rc = SQLITE_OK;
779 + }else if( rc==SQLITE_ROW ){
780 + rc = SQLITE_CORRUPT;
781 + }
782 + }
783 + sqlite3_finalize(pStmt);
784 + return rc;
785 +}
786 +
787 +/* Cursor for iterating interior nodes. Interior page cells contain a
788 + * child page number and a rowid. The child page contains items left
789 + * of the rowid (less than). The rightmost page of the subtree is
790 + * stored in the page header.
791 + *
792 + * interiorCursorDestroy - release all resources associated with the
793 + * cursor and any parent cursors.
794 + * interiorCursorCreate - create a cursor with the given parent and page.
795 + * interiorCursorNextPage - fetch the next child page from the cursor.
796 + *
797 + * Logically, interiorCursorNextPage() returns the next child page
798 + * number from the page the cursor is currently reading, calling the
799 + * parent cursor as necessary to get new pages to read, until done.
800 + * SQLITE_ROW if a page is returned, SQLITE_DONE if out of pages,
801 + * error otherwise. Unfortunately, if the table is corrupted
802 + * unexpected pages can be returned. If any unexpected page is found,
803 + * leaf or otherwise, it is returned to the caller for processing,
804 + * with the interior cursor left empty. The next call to
805 + * interiorCursorNextPage() will recurse to the parent cursor until an
806 + * interior page to iterate is returned.
807 + *
808 + * Note that while interiorCursorNextPage() will refuse to follow
809 + * loops, it does not keep track of pages returned for purposes of
810 + * preventing duplication.
811 + */
812 +typedef struct RecoverInteriorCursor RecoverInteriorCursor;
813 +struct RecoverInteriorCursor {
814 + RecoverInteriorCursor *pParent; /* Parent node to this node. */
815 + RecoverPage *pPage; /* Reference to leaf page. */
816 + unsigned nPageSize; /* Size of page. */
817 + unsigned nChildren; /* Number of children on the page. */
818 + unsigned iChild; /* Index of next child to return. */
819 +};
820 +
821 +static void interiorCursorDestroy(RecoverInteriorCursor *pCursor){
822 + /* Destroy all the cursors to the root. */
823 + while( pCursor ){
824 + RecoverInteriorCursor *p = pCursor;
825 + pCursor = pCursor->pParent;
826 +
827 + if( p->pPage ){
828 + pageDestroy(p->pPage);
829 + p->pPage = NULL;
830 + }
831 +
832 + memset(p, 0xA5, sizeof(*p));
833 + sqlite3_free(p);
834 + }
835 +}
836 +
837 +/* Internal helper. Reset storage in preparation for iterating pPage. */
838 +static void interiorCursorSetPage(RecoverInteriorCursor *pCursor,
839 + RecoverPage *pPage){
840 + const unsigned knMinCellLength = 2 + 4 + 1;
841 + unsigned nMaxChildren;
842 + assert( PageHeader(pPage)[kiPageTypeOffset]==kTableInteriorPage );
843 +
844 + if( pCursor->pPage ){
845 + pageDestroy(pCursor->pPage);
846 + pCursor->pPage = NULL;
847 + }
848 + pCursor->pPage = pPage;
849 + pCursor->iChild = 0;
850 +
851 + /* A child for each cell, plus one in the header. */
852 + pCursor->nChildren = decodeUnsigned16(PageHeader(pPage) +
853 + kiPageCellCountOffset) + 1;
854 +
855 + /* Each child requires a 16-bit offset from an array after the header,
856 + * and each child contains a 32-bit page number and at least a varint
857 + * (min size of one byte). The final child page is in the header. So
858 + * the maximum value for nChildren is:
859 + * (nPageSize - kiPageInteriorHeaderBytes) /
860 + * (sizeof(uint16) + sizeof(uint32) + 1) + 1
861 + */
862 + /* TODO(shess): This count is very unlikely to be corrupted in
863 + * isolation, so seeing this could signal to skip the page. OTOH, I
864 + * can't offhand think of how to get here unless this or the page-type
865 + * byte is corrupted. Could be an overflow page, but it would require
866 + * a very large database.
867 + */
868 + nMaxChildren =
869 + (pCursor->nPageSize - kiPageInteriorHeaderBytes) / knMinCellLength + 1;
870 + if (pCursor->nChildren > nMaxChildren) {
871 + pCursor->nChildren = nMaxChildren;
872 + }
873 +}
874 +
875 +static int interiorCursorCreate(RecoverInteriorCursor *pParent,
876 + RecoverPage *pPage, int nPageSize,
877 + RecoverInteriorCursor **ppCursor){
878 + RecoverInteriorCursor *pCursor =
879 + sqlite3_malloc(sizeof(RecoverInteriorCursor));
880 + if( !pCursor ){
881 + return SQLITE_NOMEM;
882 + }
883 +
884 + memset(pCursor, 0, sizeof(*pCursor));
885 + pCursor->pParent = pParent;
886 + pCursor->nPageSize = nPageSize;
887 + interiorCursorSetPage(pCursor, pPage);
888 + *ppCursor = pCursor;
889 + return SQLITE_OK;
890 +}
891 +
892 +/* Internal helper. Return the child page number at iChild. */
893 +static unsigned interiorCursorChildPage(RecoverInteriorCursor *pCursor){
894 + const unsigned char *pPageHeader; /* Header of the current page. */
895 + const unsigned char *pCellOffsets; /* Offset to page's cell offsets. */
896 + unsigned iCellOffset; /* Offset of target cell. */
897 +
898 + assert( pCursor->iChild<pCursor->nChildren );
899 +
900 + /* Rightmost child is in the header. */
901 + pPageHeader = PageHeader(pCursor->pPage);
902 + if( pCursor->iChild==pCursor->nChildren-1 ){
903 + return decodeUnsigned32(pPageHeader + kiPageRightChildOffset);
904 + }
905 +
906 + /* Each cell is a 4-byte integer page number and a varint rowid
907 + * which is greater than the rowid of items in that sub-tree (this
908 + * module ignores ordering). The offset is from the beginning of the
909 + * page, not from the page header.
910 + */
911 + pCellOffsets = pPageHeader + kiPageInteriorHeaderBytes;
912 + iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iChild*2);
913 + if( iCellOffset<=pCursor->nPageSize-4 ){
914 + return decodeUnsigned32(PageData(pCursor->pPage, iCellOffset));
915 + }
916 +
917 + /* TODO(shess): Check for cell overlaps? Cells require 4 bytes plus
918 + * a varint. Check could be identical to leaf check (or even a
919 + * shared helper testing for "Cells starting in this range"?).
920 + */
921 +
922 + /* If the offset is broken, return an invalid page number. */
923 + return 0;
924 +}
925 +
926 +/* Internal helper. Used to detect if iPage would cause a loop. */
927 +static int interiorCursorPageInUse(RecoverInteriorCursor *pCursor,
928 + unsigned iPage){
929 + /* Find any parent using the indicated page. */
930 + while( pCursor && pCursor->pPage->pgno!=iPage ){
931 + pCursor = pCursor->pParent;
932 + }
933 + return pCursor!=NULL;
934 +}
935 +
936 +/* Get the next page from the interior cursor at *ppCursor. Returns
937 + * SQLITE_ROW with the page in *ppPage, or SQLITE_DONE if out of
938 + * pages, or the error SQLite returned.
939 + *
940 + * If the tree is uneven, then when the cursor attempts to get a new
941 + * interior page from the parent cursor, it may get a non-interior
942 + * page. In that case, the new page is returned, and *ppCursor is
943 + * updated to point to the parent cursor (this cursor is freed).
944 + */
945 +/* TODO(shess): I've tried to avoid recursion in most of this code,
946 + * but this case is more challenging because the recursive call is in
947 + * the middle of operation. One option for converting it without
948 + * adding memory management would be to retain the head pointer and
949 + * use a helper to "back up" as needed. Another option would be to
950 + * reverse the list during traversal.
951 + */
952 +static int interiorCursorNextPage(RecoverInteriorCursor **ppCursor,
953 + RecoverPage **ppPage){
954 + RecoverInteriorCursor *pCursor = *ppCursor;
955 + while( 1 ){
956 + int rc;
957 + const unsigned char *pPageHeader; /* Header of found page. */
958 +
959 + /* Find a valid child page which isn't on the stack. */
960 + while( pCursor->iChild<pCursor->nChildren ){
961 + const unsigned iPage = interiorCursorChildPage(pCursor);
962 + pCursor->iChild++;
963 + if( interiorCursorPageInUse(pCursor, iPage) ){
964 + fprintf(stderr, "Loop detected at %d\n", iPage);
965 + }else{
966 + int rc = pagerGetPage(pCursor->pPage->pPager, iPage, ppPage);
967 + if( rc==SQLITE_OK ){
968 + return SQLITE_ROW;
969 + }
970 + }
971 + }
972 +
973 + /* This page has no more children. Get next page from parent. */
974 + if( !pCursor->pParent ){
975 + return SQLITE_DONE;
976 + }
977 + rc = interiorCursorNextPage(&pCursor->pParent, ppPage);
978 + if( rc!=SQLITE_ROW ){
979 + return rc;
980 + }
981 +
982 + /* If a non-interior page is received, that either means that the
983 + * tree is uneven, or that a child was re-used (say as an overflow
984 + * page). Remove this cursor and let the caller handle the page.
985 + */
986 + pPageHeader = PageHeader(*ppPage);
987 + if( pPageHeader[kiPageTypeOffset]!=kTableInteriorPage ){
988 + *ppCursor = pCursor->pParent;
989 + pCursor->pParent = NULL;
990 + interiorCursorDestroy(pCursor);
991 + return SQLITE_ROW;
992 + }
993 +
994 + /* Iterate the new page. */
995 + interiorCursorSetPage(pCursor, *ppPage);
996 + *ppPage = NULL;
997 + }
998 +
999 + assert(NULL); /* NOTREACHED() */
1000 + return SQLITE_CORRUPT;
1001 +}
1002 +
1003 +/* Large rows are spilled to overflow pages. The row's main page
1004 + * stores the overflow page number after the local payload, with a
1005 + * linked list forward from there as necessary. overflowMaybeCreate()
1006 + * and overflowGetSegment() provide an abstraction for accessing such
1007 + * data while centralizing the code.
1008 + *
1009 + * overflowDestroy - releases all resources associated with the structure.
1010 + * overflowMaybeCreate - create the overflow structure if it is needed
1011 + * to represent the given record. See function comment.
1012 + * overflowGetSegment - fetch a segment from the record, accounting
1013 + * for overflow pages. Segments which are not
1014 + * entirely contained with a page are constructed
1015 + * into a buffer which is returned. See function comment.
1016 + */
1017 +typedef struct RecoverOverflow RecoverOverflow;
1018 +struct RecoverOverflow {
1019 + RecoverOverflow *pNextOverflow;
1020 + RecoverPage *pPage;
1021 + unsigned nPageSize;
1022 +};
1023 +
1024 +static void overflowDestroy(RecoverOverflow *pOverflow){
1025 + while( pOverflow ){
1026 + RecoverOverflow *p = pOverflow;
1027 + pOverflow = p->pNextOverflow;
1028 +
1029 + if( p->pPage ){
1030 + pageDestroy(p->pPage);
1031 + p->pPage = NULL;
1032 + }
1033 +
1034 + memset(p, 0xA5, sizeof(*p));
1035 + sqlite3_free(p);
1036 + }
1037 +}
1038 +
1039 +/* Internal helper. Used to detect if iPage would cause a loop. */
1040 +static int overflowPageInUse(RecoverOverflow *pOverflow, unsigned iPage){
1041 + while( pOverflow && pOverflow->pPage->pgno!=iPage ){
1042 + pOverflow = pOverflow->pNextOverflow;
1043 + }
1044 + return pOverflow!=NULL;
1045 +}
1046 +
1047 +/* Setup to access an nRecordBytes record beginning at iRecordOffset
1048 + * in pPage. If nRecordBytes can be satisfied entirely from pPage,
1049 + * then no overflow pages are needed an *pnLocalRecordBytes is set to
1050 + * nRecordBytes. Otherwise, *ppOverflow is set to the head of a list
1051 + * of overflow pages, and *pnLocalRecordBytes is set to the number of
1052 + * bytes local to pPage.
1053 + *
1054 + * overflowGetSegment() will do the right thing regardless of whether
1055 + * those values are set to be in-page or not.
1056 + */
1057 +static int overflowMaybeCreate(RecoverPage *pPage, unsigned nPageSize,
1058 + unsigned iRecordOffset, unsigned nRecordBytes,
1059 + unsigned *pnLocalRecordBytes,
1060 + RecoverOverflow **ppOverflow){
1061 + unsigned nLocalRecordBytes; /* Record bytes in the leaf page. */
1062 + unsigned iNextPage; /* Next page number for record data. */
1063 + unsigned nBytes; /* Maximum record bytes as of current page. */
1064 + int rc;
1065 + RecoverOverflow *pFirstOverflow; /* First in linked list of pages. */
1066 + RecoverOverflow *pLastOverflow; /* End of linked list. */
1067 +
1068 + /* Calculations from the "Table B-Tree Leaf Cell" part of section
1069 + * 1.5 of http://www.sqlite.org/fileformat2.html . maxLocal and
1070 + * minLocal to match naming in btree.c.
1071 + */
1072 + const unsigned maxLocal = nPageSize - 35;
1073 + const unsigned minLocal = ((nPageSize-12)*32/255)-23; /* m */
1074 +
1075 + /* Always fit anything smaller than maxLocal. */
1076 + if( nRecordBytes<=maxLocal ){
1077 + *pnLocalRecordBytes = nRecordBytes;
1078 + *ppOverflow = NULL;
1079 + return SQLITE_OK;
1080 + }
1081 +
1082 + /* Calculate the remainder after accounting for minLocal on the leaf
1083 + * page and what packs evenly into overflow pages. If the remainder
1084 + * does not fit into maxLocal, then a partially-full overflow page
1085 + * will be required in any case, so store as little as possible locally.
1086 + */
1087 + nLocalRecordBytes = minLocal+((nRecordBytes-minLocal)%(nPageSize-4));
1088 + if( maxLocal<nLocalRecordBytes ){
1089 + nLocalRecordBytes = minLocal;
1090 + }
1091 +
1092 + /* Don't read off the end of the page. */
1093 + if( iRecordOffset+nLocalRecordBytes+4>nPageSize ){
1094 + return SQLITE_CORRUPT;
1095 + }
1096 +
1097 + /* First overflow page number is after the local bytes. */
1098 + iNextPage =
1099 + decodeUnsigned32(PageData(pPage, iRecordOffset + nLocalRecordBytes));
1100 + nBytes = nLocalRecordBytes;
1101 +
1102 + /* While there are more pages to read, and more bytes are needed,
1103 + * get another page.
1104 + */
1105 + pFirstOverflow = pLastOverflow = NULL;
1106 + rc = SQLITE_OK;
1107 + while( iNextPage && nBytes<nRecordBytes ){
1108 + RecoverOverflow *pOverflow; /* New overflow page for the list. */
1109 +
1110 + rc = pagerGetPage(pPage->pPager, iNextPage, &pPage);
1111 + if( rc!=SQLITE_OK ){
1112 + break;
1113 + }
1114 +
1115 + pOverflow = sqlite3_malloc(sizeof(RecoverOverflow));
1116 + if( !pOverflow ){
1117 + pageDestroy(pPage);
1118 + rc = SQLITE_NOMEM;
1119 + break;
1120 + }
1121 + memset(pOverflow, 0, sizeof(*pOverflow));
1122 + pOverflow->pPage = pPage;
1123 + pOverflow->nPageSize = nPageSize;
1124 +
1125 + if( !pFirstOverflow ){
1126 + pFirstOverflow = pOverflow;
1127 + }else{
1128 + pLastOverflow->pNextOverflow = pOverflow;
1129 + }
1130 + pLastOverflow = pOverflow;
1131 +
1132 + iNextPage = decodeUnsigned32(pPage->pData);
1133 + nBytes += nPageSize-4;
1134 +
1135 + /* Avoid loops. */
1136 + if( overflowPageInUse(pFirstOverflow, iNextPage) ){
1137 + fprintf(stderr, "Overflow loop detected at %d\n", iNextPage);
1138 + rc = SQLITE_CORRUPT;
1139 + break;
1140 + }
1141 + }
1142 +
1143 + /* If there were not enough pages, or too many, things are corrupt.
1144 + * Not having enough pages is an obvious problem, all the data
1145 + * cannot be read. Too many pages means that the contents of the
1146 + * row between the main page and the overflow page(s) is
1147 + * inconsistent (most likely one or more of the overflow pages does
1148 + * not really belong to this row).
1149 + */
1150 + if( rc==SQLITE_OK && (nBytes<nRecordBytes || iNextPage) ){
1151 + rc = SQLITE_CORRUPT;
1152 + }
1153 +
1154 + if( rc==SQLITE_OK ){
1155 + *ppOverflow = pFirstOverflow;
1156 + *pnLocalRecordBytes = nLocalRecordBytes;
1157 + }else if( pFirstOverflow ){
1158 + overflowDestroy(pFirstOverflow);
1159 + }
1160 + return rc;
1161 +}
1162 +
1163 +/* Use in concert with overflowMaybeCreate() to efficiently read parts
1164 + * of a potentially-overflowing record. pPage and iRecordOffset are
1165 + * the values passed into overflowMaybeCreate(), nLocalRecordBytes and
1166 + * pOverflow are the values returned by that call.
1167 + *
1168 + * On SQLITE_OK, *ppBase points to nRequestBytes of data at
1169 + * iRequestOffset within the record. If the data exists contiguously
1170 + * in a page, a direct pointer is returned, otherwise a buffer from
1171 + * sqlite3_malloc() is returned with the data. *pbFree is set true if
1172 + * sqlite3_free() should be called on *ppBase.
1173 + */
1174 +/* Operation of this function is subtle. At any time, pPage is the
1175 + * current page, with iRecordOffset and nLocalRecordBytes being record
1176 + * data within pPage, and pOverflow being the overflow page after
1177 + * pPage. This allows the code to handle both the initial leaf page
1178 + * and overflow pages consistently by adjusting the values
1179 + * appropriately.
1180 + */
1181 +static int overflowGetSegment(RecoverPage *pPage, unsigned iRecordOffset,
1182 + unsigned nLocalRecordBytes,
1183 + RecoverOverflow *pOverflow,
1184 + unsigned iRequestOffset, unsigned nRequestBytes,
1185 + unsigned char **ppBase, int *pbFree){
1186 + unsigned nBase; /* Amount of data currently collected. */
1187 + unsigned char *pBase; /* Buffer to collect record data into. */
1188 +
1189 + /* Skip to the page containing the start of the data. */
1190 + while( iRequestOffset>=nLocalRecordBytes && pOverflow ){
1191 + /* Factor out current page's contribution. */
1192 + iRequestOffset -= nLocalRecordBytes;
1193 +
1194 + /* Move forward to the next page in the list. */
1195 + pPage = pOverflow->pPage;
1196 + iRecordOffset = 4;
1197 + nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
1198 + pOverflow = pOverflow->pNextOverflow;
1199 + }
1200 +
1201 + /* If the requested data is entirely within this page, return a
1202 + * pointer into the page.
1203 + */
1204 + if( iRequestOffset+nRequestBytes<=nLocalRecordBytes ){
1205 + /* TODO(shess): "assignment discards qualifiers from pointer target type"
1206 + * Having ppBase be const makes sense, but sqlite3_free() takes non-const.
1207 + */
1208 + *ppBase = (unsigned char *)PageData(pPage, iRecordOffset + iRequestOffset);
1209 + *pbFree = 0;
1210 + return SQLITE_OK;
1211 + }
1212 +
1213 + /* The data range would require additional pages. */
1214 + if( !pOverflow ){
1215 + /* Should never happen, the range is outside the nRecordBytes
1216 + * passed to overflowMaybeCreate().
1217 + */
1218 + assert(NULL); /* NOTREACHED */
1219 + return SQLITE_ERROR;
1220 + }
1221 +
1222 + /* Get a buffer to construct into. */
1223 + nBase = 0;
1224 + pBase = sqlite3_malloc(nRequestBytes);
1225 + if( !pBase ){
1226 + return SQLITE_NOMEM;
1227 + }
1228 + while( nBase<nRequestBytes ){
1229 + /* Copy over data present on this page. */
1230 + unsigned nCopyBytes = nRequestBytes - nBase;
1231 + if( nLocalRecordBytes-iRequestOffset<nCopyBytes ){
1232 + nCopyBytes = nLocalRecordBytes - iRequestOffset;
1233 + }
1234 + memcpy(pBase + nBase, PageData(pPage, iRecordOffset + iRequestOffset),
1235 + nCopyBytes);
1236 + nBase += nCopyBytes;
1237 +
1238 + if( pOverflow ){
1239 + /* Copy from start of record data in future pages. */
1240 + iRequestOffset = 0;
1241 +
1242 + /* Move forward to the next page in the list. Should match
1243 + * first while() loop.
1244 + */
1245 + pPage = pOverflow->pPage;
1246 + iRecordOffset = 4;
1247 + nLocalRecordBytes = pOverflow->nPageSize - iRecordOffset;
1248 + pOverflow = pOverflow->pNextOverflow;
1249 + }else if( nBase<nRequestBytes ){
1250 + /* Ran out of overflow pages with data left to deliver. Not
1251 + * possible if the requested range fits within nRecordBytes
1252 + * passed to overflowMaybeCreate() when creating pOverflow.
1253 + */
1254 + assert(NULL); /* NOTREACHED */
1255 + sqlite3_free(pBase);
1256 + return SQLITE_ERROR;
1257 + }
1258 + }
1259 + assert( nBase==nRequestBytes );
1260 + *ppBase = pBase;
1261 + *pbFree = 1;
1262 + return SQLITE_OK;
1263 +}
1264 +
1265 +/* Primary structure for iterating the contents of a table.
1266 + *
1267 + * leafCursorDestroy - release all resources associated with the cursor.
1268 + * leafCursorCreate - create a cursor to iterate items from tree at
1269 + * the provided root page.
1270 + * leafCursorNextValidCell - get the cursor ready to access data from
1271 + * the next valid cell in the table.
1272 + * leafCursorCellRowid - get the current cell's rowid.
1273 + * leafCursorCellColumns - get current cell's column count.
1274 + * leafCursorCellColInfo - get type and data for a column in current cell.
1275 + *
1276 + * leafCursorNextValidCell skips cells which fail simple integrity
1277 + * checks, such as overlapping other cells, or being located at
1278 + * impossible offsets, or where header data doesn't correctly describe
1279 + * payload data. Returns SQLITE_ROW if a valid cell is found,
1280 + * SQLITE_DONE if all pages in the tree were exhausted.
1281 + *
1282 + * leafCursorCellColInfo() accounts for overflow pages in the style of
1283 + * overflowGetSegment().
1284 + */
1285 +typedef struct RecoverLeafCursor RecoverLeafCursor;
1286 +struct RecoverLeafCursor {
1287 + RecoverInteriorCursor *pParent; /* Parent node to this node. */
1288 + RecoverPager *pPager; /* Page provider. */
1289 + RecoverPage *pPage; /* Current leaf page. */
1290 + unsigned nPageSize; /* Size of pPage. */
1291 + unsigned nCells; /* Number of cells in pPage. */
1292 + unsigned iCell; /* Current cell. */
1293 +
1294 + /* Info parsed from data in iCell. */
1295 + i64 iRowid; /* rowid parsed. */
1296 + unsigned nRecordCols; /* how many items in the record. */
1297 + u64 iRecordOffset; /* offset to record data. */
1298 + /* TODO(shess): nRecordBytes and nRecordHeaderBytes are used in
1299 + * leafCursorCellColInfo() to prevent buffer overruns.
1300 + * leafCursorCellDecode() already verified that the cell is valid, so
1301 + * those checks should be redundant.
1302 + */
1303 + u64 nRecordBytes; /* Size of record data. */
1304 + unsigned nLocalRecordBytes; /* Amount of record data in-page. */
1305 + unsigned nRecordHeaderBytes; /* Size of record header data. */
1306 + unsigned char *pRecordHeader; /* Pointer to record header data. */
1307 + int bFreeRecordHeader; /* True if record header requires free. */
1308 + RecoverOverflow *pOverflow; /* Cell overflow info, if needed. */
1309 +};
1310 +
1311 +/* Internal helper shared between next-page and create-cursor. If
1312 + * pPage is a leaf page, it will be stored in the cursor and state
1313 + * initialized for reading cells.
1314 + *
1315 + * If pPage is an interior page, a new parent cursor is created and
1316 + * injected on the stack. This is necessary to handle trees with
1317 + * uneven depth, but also is used during initial setup.
1318 + *
1319 + * If pPage is not a table page at all, it is discarded.
1320 + *
1321 + * If SQLITE_OK is returned, the caller no longer owns pPage,
1322 + * otherwise the caller is responsible for discarding it.
1323 + */
1324 +static int leafCursorLoadPage(RecoverLeafCursor *pCursor, RecoverPage *pPage){
1325 + const unsigned char *pPageHeader; /* Header of *pPage */
1326 + unsigned nCells; /* Number of cells in the page */
1327 +
1328 + /* Release the current page. */
1329 + if( pCursor->pPage ){
1330 + pageDestroy(pCursor->pPage);
1331 + pCursor->pPage = NULL;
1332 + pCursor->iCell = pCursor->nCells = 0;
1333 + }
1334 +
1335 + /* If the page is an unexpected interior node, inject a new stack
1336 + * layer and try again from there.
1337 + */
1338 + pPageHeader = PageHeader(pPage);
1339 + if( pPageHeader[kiPageTypeOffset]==kTableInteriorPage ){
1340 + RecoverInteriorCursor *pParent;
1341 + int rc = interiorCursorCreate(pCursor->pParent, pPage, pCursor->nPageSize,
1342 + &pParent);
1343 + if( rc!=SQLITE_OK ){
1344 + return rc;
1345 + }
1346 + pCursor->pParent = pParent;
1347 + return SQLITE_OK;
1348 + }
1349 +
1350 + /* Not a leaf page, skip it. */
1351 + if( pPageHeader[kiPageTypeOffset]!=kTableLeafPage ){
1352 + pageDestroy(pPage);
1353 + return SQLITE_OK;
1354 + }
1355 +
1356 + /* Leaf contains no data, skip it. Empty tables, for instance. */
1357 + nCells = decodeUnsigned16(pPageHeader + kiPageCellCountOffset);;
1358 + if( nCells<1 ){
1359 + pageDestroy(pPage);
1360 + return SQLITE_OK;
1361 + }
1362 +
1363 + /* Take ownership of the page and start decoding. */
1364 + pCursor->pPage = pPage;
1365 + pCursor->iCell = 0;
1366 + pCursor->nCells = nCells;
1367 + return SQLITE_OK;
1368 +}
1369 +
1370 +/* Get the next leaf-level page in the tree. Returns SQLITE_ROW when
1371 + * a leaf page is found, SQLITE_DONE when no more leaves exist, or any
1372 + * error which occurred.
1373 + */
1374 +static int leafCursorNextPage(RecoverLeafCursor *pCursor){
1375 + if( !pCursor->pParent ){
1376 + return SQLITE_DONE;
1377 + }
1378 +
1379 + /* Repeatedly load the parent's next child page until a leaf is found. */
1380 + do {
1381 + RecoverPage *pNextPage;
1382 + int rc = interiorCursorNextPage(&pCursor->pParent, &pNextPage);
1383 + if( rc!=SQLITE_ROW ){
1384 + assert( rc==SQLITE_DONE );
1385 + return rc;
1386 + }
1387 +
1388 + rc = leafCursorLoadPage(pCursor, pNextPage);
1389 + if( rc!=SQLITE_OK ){
1390 + pageDestroy(pNextPage);
1391 + return rc;
1392 + }
1393 + } while( !pCursor->pPage );
1394 +
1395 + return SQLITE_ROW;
1396 +}
1397 +
1398 +static void leafCursorDestroyCellData(RecoverLeafCursor *pCursor){
1399 + if( pCursor->bFreeRecordHeader ){
1400 + sqlite3_free(pCursor->pRecordHeader);
1401 + }
1402 + pCursor->bFreeRecordHeader = 0;
1403 + pCursor->pRecordHeader = NULL;
1404 +
1405 + if( pCursor->pOverflow ){
1406 + overflowDestroy(pCursor->pOverflow);
1407 + pCursor->pOverflow = NULL;
1408 + }
1409 +}
1410 +
1411 +static void leafCursorDestroy(RecoverLeafCursor *pCursor){
1412 + leafCursorDestroyCellData(pCursor);
1413 +
1414 + if( pCursor->pParent ){
1415 + interiorCursorDestroy(pCursor->pParent);
1416 + pCursor->pParent = NULL;
1417 + }
1418 +
1419 + if( pCursor->pPage ){
1420 + pageDestroy(pCursor->pPage);
1421 + pCursor->pPage = NULL;
1422 + }
1423 +
1424 + if( pCursor->pPager ){
1425 + pagerDestroy(pCursor->pPager);
1426 + pCursor->pPager = NULL;
1427 + }
1428 +
1429 + memset(pCursor, 0xA5, sizeof(*pCursor));
1430 + sqlite3_free(pCursor);
1431 +}
1432 +
1433 +/* Create a cursor to iterate the rows from the leaf pages of a table
1434 + * rooted at iRootPage.
1435 + */
1436 +/* TODO(shess): recoverOpen() calls this to setup the cursor, and I
1437 + * think that recoverFilter() may make a hard assumption that the
1438 + * cursor returned will turn up at least one valid cell.
1439 + *
1440 + * The cases I can think of which break this assumption are:
1441 + * - pPage is a valid leaf page with no valid cells.
1442 + * - pPage is a valid interior page with no valid leaves.
1443 + * - pPage is a valid interior page who's leaves contain no valid cells.
1444 + * - pPage is not a valid leaf or interior page.
1445 + */
1446 +static int leafCursorCreate(RecoverPager *pPager, unsigned nPageSize,
1447 + u32 iRootPage, RecoverLeafCursor **ppCursor){
1448 + RecoverPage *pPage; /* Reference to page at iRootPage. */
1449 + RecoverLeafCursor *pCursor; /* Leaf cursor being constructed. */
1450 + int rc;
1451 +
1452 + /* Start out with the root page. */
1453 + rc = pagerGetPage(pPager, iRootPage, &pPage);
1454 + if( rc!=SQLITE_OK ){
1455 + return rc;
1456 + }
1457 +
1458 + pCursor = sqlite3_malloc(sizeof(RecoverLeafCursor));
1459 + if( !pCursor ){
1460 + pageDestroy(pPage);
1461 + return SQLITE_NOMEM;
1462 + }
1463 + memset(pCursor, 0, sizeof(*pCursor));
1464 +
1465 + pCursor->nPageSize = nPageSize;
1466 + pCursor->pPager = pPager;
1467 +
1468 + rc = leafCursorLoadPage(pCursor, pPage);
1469 + if( rc!=SQLITE_OK ){
1470 + pageDestroy(pPage);
1471 + leafCursorDestroy(pCursor);
1472 + return rc;
1473 + }
1474 +
1475 + /* pPage wasn't a leaf page, find the next leaf page. */
1476 + if( !pCursor->pPage ){
1477 + rc = leafCursorNextPage(pCursor);
1478 + if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ){
1479 + leafCursorDestroy(pCursor);
1480 + return rc;
1481 + }
1482 + }
1483 +
1484 + *ppCursor = pCursor;
1485 + return SQLITE_OK;
1486 +}
1487 +
1488 +/* Useful for setting breakpoints. */
1489 +static int ValidateError(){
1490 + return SQLITE_ERROR;
1491 +}
1492 +
1493 +/* Setup the cursor for reading the information from cell iCell. */
1494 +static int leafCursorCellDecode(RecoverLeafCursor *pCursor){
1495 + const unsigned char *pPageHeader; /* Header of current page. */
1496 + const unsigned char *pPageEnd; /* Byte after end of current page. */
1497 + const unsigned char *pCellOffsets; /* Pointer to page's cell offsets. */
1498 + unsigned iCellOffset; /* Offset of current cell (iCell). */
1499 + const unsigned char *pCell; /* Pointer to data at iCellOffset. */
1500 + unsigned nCellMaxBytes; /* Maximum local size of iCell. */
1501 + unsigned iEndOffset; /* End of iCell's in-page data. */
1502 + u64 nRecordBytes; /* Expected size of cell, w/overflow. */
1503 + u64 iRowid; /* iCell's rowid (in table). */
1504 + unsigned nRead; /* Amount of cell read. */
1505 + unsigned nRecordHeaderRead; /* Header data read. */
1506 + u64 nRecordHeaderBytes; /* Header size expected. */
1507 + unsigned nRecordCols; /* Columns read from header. */
1508 + u64 nRecordColBytes; /* Bytes in payload for those columns. */
1509 + unsigned i;
1510 + int rc;
1511 +
1512 + assert( pCursor->iCell<pCursor->nCells );
1513 +
1514 + leafCursorDestroyCellData(pCursor);
1515 +
1516 + /* Find the offset to the row. */
1517 + pPageHeader = PageHeader(pCursor->pPage);
1518 + pCellOffsets = pPageHeader + knPageLeafHeaderBytes;
1519 + pPageEnd = PageData(pCursor->pPage, pCursor->nPageSize);
1520 + if( pCellOffsets + pCursor->iCell*2 + 2 > pPageEnd ){
1521 + return ValidateError();
1522 + }
1523 + iCellOffset = decodeUnsigned16(pCellOffsets + pCursor->iCell*2);
1524 + if( iCellOffset>=pCursor->nPageSize ){
1525 + return ValidateError();
1526 + }
1527 +
1528 + pCell = PageData(pCursor->pPage, iCellOffset);
1529 + nCellMaxBytes = pCursor->nPageSize - iCellOffset;
1530 +
1531 + /* B-tree leaf cells lead with varint record size, varint rowid and
1532 + * varint header size.
1533 + */
1534 + /* TODO(shess): The smallest page size is 512 bytes, which has an m
1535 + * of 39. Three varints need at most 27 bytes to encode. I think.
1536 + */
1537 + if( !checkVarints(pCell, nCellMaxBytes, 3) ){
1538 + return ValidateError();
1539 + }
1540 +
1541 + nRead = recoverGetVarint(pCell, &nRecordBytes);
1542 + assert( iCellOffset+nRead<=pCursor->nPageSize );
1543 + pCursor->nRecordBytes = nRecordBytes;
1544 +
1545 + nRead += recoverGetVarint(pCell + nRead, &iRowid);
1546 + assert( iCellOffset+nRead<=pCursor->nPageSize );
1547 + pCursor->iRowid = (i64)iRowid;
1548 +
1549 + pCursor->iRecordOffset = iCellOffset + nRead;
1550 +
1551 + /* Start overflow setup here because nLocalRecordBytes is needed to
1552 + * check cell overlap.
1553 + */
1554 + rc = overflowMaybeCreate(pCursor->pPage, pCursor->nPageSize,
1555 + pCursor->iRecordOffset, pCursor->nRecordBytes,
1556 + &pCursor->nLocalRecordBytes,
1557 + &pCursor->pOverflow);
1558 + if( rc!=SQLITE_OK ){
1559 + return ValidateError();
1560 + }
1561 +
1562 + /* Check that no other cell starts within this cell. */
1563 + iEndOffset = pCursor->iRecordOffset + pCursor->nLocalRecordBytes;
1564 + for( i=0; i<pCursor->nCells && pCellOffsets + i*2 + 2 <= pPageEnd; ++i ){
1565 + const unsigned iOtherOffset = decodeUnsigned16(pCellOffsets + i*2);
1566 + if( iOtherOffset>iCellOffset && iOtherOffset<iEndOffset ){
1567 + return ValidateError();
1568 + }
1569 + }
1570 +
1571 + nRecordHeaderRead = recoverGetVarint(pCell + nRead, &nRecordHeaderBytes);
1572 + assert( nRecordHeaderBytes<=nRecordBytes );
1573 + pCursor->nRecordHeaderBytes = nRecordHeaderBytes;
1574 +
1575 + /* Large headers could overflow if pages are small. */
1576 + rc = overflowGetSegment(pCursor->pPage,
1577 + pCursor->iRecordOffset, pCursor->nLocalRecordBytes,
1578 + pCursor->pOverflow, 0, nRecordHeaderBytes,
1579 + &pCursor->pRecordHeader, &pCursor->bFreeRecordHeader) ;
1580 + if( rc!=SQLITE_OK ){
1581 + return ValidateError();
1582 + }
1583 +
1584 + /* Tally up the column count and size of data. */
1585 + nRecordCols = 0;
1586 + nRecordColBytes = 0;
1587 + while( nRecordHeaderRead<nRecordHeaderBytes ){
1588 + u64 iSerialType; /* Type descriptor for current column. */
1589 + if( !checkVarint(pCursor->pRecordHeader + nRecordHeaderRead,
1590 + nRecordHeaderBytes - nRecordHeaderRead) ){
1591 + return ValidateError();
1592 + }
1593 + nRecordHeaderRead += recoverGetVarint(
1594 + pCursor->pRecordHeader + nRecordHeaderRead, &iSerialType);
1595 + if( iSerialType==10 || iSerialType==11 ){
1596 + return ValidateError();
1597 + }
1598 + nRecordColBytes += SerialTypeLength(iSerialType);
1599 + nRecordCols++;
1600 + }
1601 + pCursor->nRecordCols = nRecordCols;
1602 +
1603 + /* Parsing the header used as many bytes as expected. */
1604 + if( nRecordHeaderRead!=nRecordHeaderBytes ){
1605 + return ValidateError();
1606 + }
1607 +
1608 + /* Calculated record is size of expected record. */
1609 + if( nRecordHeaderBytes+nRecordColBytes!=nRecordBytes ){
1610 + return ValidateError();
1611 + }
1612 +
1613 + return SQLITE_OK;
1614 +}
1615 +
1616 +static i64 leafCursorCellRowid(RecoverLeafCursor *pCursor){
1617 + return pCursor->iRowid;
1618 +}
1619 +
1620 +static unsigned leafCursorCellColumns(RecoverLeafCursor *pCursor){
1621 + return pCursor->nRecordCols;
1622 +}
1623 +
1624 +/* Get the column info for the cell. Pass NULL for ppBase to prevent
1625 + * retrieving the data segment. If *pbFree is true, *ppBase must be
1626 + * freed by the caller using sqlite3_free().
1627 + */
1628 +static int leafCursorCellColInfo(RecoverLeafCursor *pCursor,
1629 + unsigned iCol, u64 *piColType,
1630 + unsigned char **ppBase, int *pbFree){
1631 + const unsigned char *pRecordHeader; /* Current cell's header. */
1632 + u64 nRecordHeaderBytes; /* Bytes in pRecordHeader. */
1633 + unsigned nRead; /* Bytes read from header. */
1634 + u64 iColEndOffset; /* Offset to end of column in cell. */
1635 + unsigned nColsSkipped; /* Count columns as procesed. */
1636 + u64 iSerialType; /* Type descriptor for current column. * /
1637 +
1638 + /* Implicit NULL for columns past the end. This case happens when
1639 + * rows have not been updated since an ALTER TABLE added columns.
1640 + * It is more convenient to address here than in callers.
1641 + */
1642 + if( iCol>=pCursor->nRecordCols ){
1643 + *piColType = 0;
1644 + if( ppBase ){
1645 + *ppBase = 0;
1646 + *pbFree = 0;
1647 + }
1648 + return SQLITE_OK;
1649 + }
1650 +
1651 + /* Must be able to decode header size. */
1652 + pRecordHeader = pCursor->pRecordHeader;
1653 + if( !checkVarint(pRecordHeader, pCursor->nRecordHeaderBytes) ){
1654 + return SQLITE_CORRUPT;
1655 + }
1656 +
1657 + /* Rather than caching the header size and how many bytes it took,
1658 + * decode it every time.
1659 + */
1660 + nRead = recoverGetVarint(pRecordHeader, &nRecordHeaderBytes);
1661 + assert( nRecordHeaderBytes==pCursor->nRecordHeaderBytes );
1662 +
1663 + /* Scan forward to the indicated column. Scans to _after_ column
1664 + * for later range checking.
1665 + */
1666 + /* TODO(shess): This could get expensive for very wide tables. An
1667 + * array of iSerialType could be built in leafCursorCellDecode(), but
1668 + * the number of columns is dynamic per row, so it would add memory
1669 + * management complexity. Enough info to efficiently forward
1670 + * iterate could be kept, if all clients forward iterate
1671 + * (recoverColumn() may not).
1672 + */
1673 + iColEndOffset = 0;
1674 + nColsSkipped = 0;
1675 + while( nColsSkipped<=iCol && nRead<nRecordHeaderBytes ){
1676 + if( !checkVarint(pRecordHeader + nRead, nRecordHeaderBytes - nRead) ){
1677 + return SQLITE_CORRUPT;
1678 + }
1679 + nRead += recoverGetVarint(pRecordHeader + nRead, &iSerialType);
1680 + iColEndOffset += SerialTypeLength(iSerialType);
1681 + nColsSkipped++;
1682 + }
1683 +
1684 + /* Column's data extends past record's end. */
1685 + if( nRecordHeaderBytes+iColEndOffset>pCursor->nRecordBytes ){
1686 + return SQLITE_CORRUPT;
1687 + }
1688 +
1689 + *piColType = iSerialType;
1690 + if( ppBase ){
1691 + const u32 nColBytes = SerialTypeLength(iSerialType);
1692 +
1693 + /* Offset from start of record to beginning of column. */
1694 + const unsigned iColOffset = nRecordHeaderBytes+iColEndOffset-nColBytes;
1695 +
1696 + return overflowGetSegment(pCursor->pPage, pCursor->iRecordOffset,
1697 + pCursor->nLocalRecordBytes, pCursor->pOverflow,
1698 + iColOffset, nColBytes, ppBase, pbFree);
1699 + }
1700 + return SQLITE_OK;
1701 +}
1702 +
1703 +static int leafCursorNextValidCell(RecoverLeafCursor *pCursor){
1704 + while( 1 ){
1705 + int rc;
1706 +
1707 + /* Move to the next cell. */
1708 + pCursor->iCell++;
1709 +
1710 + /* No more cells, get the next leaf. */
1711 + if( pCursor->iCell>=pCursor->nCells ){
1712 + rc = leafCursorNextPage(pCursor);
1713 + if( rc!=SQLITE_ROW ){
1714 + return rc;
1715 + }
1716 + assert( pCursor->iCell==0 );
1717 + }
1718 +
1719 + /* If the cell is valid, indicate that a row is available. */
1720 + rc = leafCursorCellDecode(pCursor);
1721 + if( rc==SQLITE_OK ){
1722 + return SQLITE_ROW;
1723 + }
1724 +
1725 + /* Iterate until done or a valid row is found. */
1726 + /* TODO(shess): Remove debugging output. */
1727 + fprintf(stderr, "Skipping invalid cell\n");
1728 + }
1729 + return SQLITE_ERROR;
1730 +}
1731 +
1732 +typedef struct Recover Recover;
1733 +struct Recover {
1734 + sqlite3_vtab base;
1735 + sqlite3 *db; /* Host database connection */
1736 + char *zDb; /* Database containing target table */
1737 + char *zTable; /* Target table */
1738 + unsigned nCols; /* Number of columns in target table */
1739 + unsigned char *pTypes; /* Types of columns in target table */
1740 +};
1741 +
1742 +/* Internal helper for deleting the module. */
1743 +static void recoverRelease(Recover *pRecover){
1744 + sqlite3_free(pRecover->zDb);
1745 + sqlite3_free(pRecover->zTable);
1746 + sqlite3_free(pRecover->pTypes);
1747 + memset(pRecover, 0xA5, sizeof(*pRecover));
1748 + sqlite3_free(pRecover);
1749 +}
1750 +
1751 +/* Helper function for initializing the module. Forward-declared so
1752 + * recoverCreate() and recoverConnect() can see it.
1753 + */
1754 +static int recoverInit(
1755 + sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **
1756 +);
1757 +
1758 +static int recoverCreate(
1759 + sqlite3 *db,
1760 + void *pAux,
1761 + int argc, const char *const*argv,
1762 + sqlite3_vtab **ppVtab,
1763 + char **pzErr
1764 +){
1765 + FNENTRY();
1766 + return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
1767 +}
1768 +
1769 +/* This should never be called. */
1770 +static int recoverConnect(
1771 + sqlite3 *db,
1772 + void *pAux,
1773 + int argc, const char *const*argv,
1774 + sqlite3_vtab **ppVtab,
1775 + char **pzErr
1776 +){
1777 + FNENTRY();
1778 + return recoverInit(db, pAux, argc, argv, ppVtab, pzErr);
1779 +}
1780 +
1781 +/* No indices supported. */
1782 +static int recoverBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
1783 + FNENTRY();
1784 + return SQLITE_OK;
1785 +}
1786 +
1787 +/* Logically, this should never be called. */
1788 +static int recoverDisconnect(sqlite3_vtab *pVtab){
1789 + FNENTRY();
1790 + recoverRelease((Recover*)pVtab);
1791 + return SQLITE_OK;
1792 +}
1793 +
1794 +static int recoverDestroy(sqlite3_vtab *pVtab){
1795 + FNENTRY();
1796 + recoverRelease((Recover*)pVtab);
1797 + return SQLITE_OK;
1798 +}
1799 +
1800 +typedef struct RecoverCursor RecoverCursor;
1801 +struct RecoverCursor {
1802 + sqlite3_vtab_cursor base;
1803 + RecoverLeafCursor *pLeafCursor;
1804 + int iEncoding;
1805 + int bEOF;
1806 +};
1807 +
1808 +static int recoverOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
1809 + Recover *pRecover = (Recover*)pVTab;
1810 + u32 iRootPage; /* Root page of the backing table. */
1811 + int iEncoding; /* UTF encoding for backing database. */
1812 + unsigned nPageSize; /* Size of pages in backing database. */
1813 + RecoverPager *pPager; /* Backing database pager. */
1814 + RecoverLeafCursor *pLeafCursor; /* Cursor to read table's leaf pages. */
1815 + RecoverCursor *pCursor; /* Cursor to read rows from leaves. */
1816 + int rc;
1817 +
1818 + FNENTRY();
1819 +
1820 + iRootPage = 0;
1821 + rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable,
1822 + &iRootPage);
1823 + if( rc!=SQLITE_OK ){
1824 + return rc;
1825 + }
1826 +
1827 + rc = GetPager(pRecover->db, pRecover->zDb, &pPager, &nPageSize, &iEncoding);
1828 + if( rc!=SQLITE_OK ){
1829 + return rc;
1830 + }
1831 +
1832 + rc = leafCursorCreate(pPager, nPageSize, iRootPage, &pLeafCursor);
1833 + if( rc!=SQLITE_OK ){
1834 + pagerDestroy(pPager);
1835 + return rc;
1836 + }
1837 +
1838 + pCursor = sqlite3_malloc(sizeof(RecoverCursor));
1839 + if( !pCursor ){
1840 + leafCursorDestroy(pLeafCursor);
1841 + return SQLITE_NOMEM;
1842 + }
1843 + memset(pCursor, 0, sizeof(*pCursor));
1844 + pCursor->base.pVtab = pVTab;
1845 + pCursor->pLeafCursor = pLeafCursor;
1846 + pCursor->iEncoding = iEncoding;
1847 +
1848 + /* If no leaf pages were found, empty result set. */
1849 + /* TODO(shess): leafCursorNextValidCell() would return SQLITE_ROW or
1850 + * SQLITE_DONE to indicate whether there is further data to consider.
1851 + */
1852 + pCursor->bEOF = (pLeafCursor->pPage==NULL);
1853 +
1854 + *ppCursor = (sqlite3_vtab_cursor*)pCursor;
1855 + return SQLITE_OK;
1856 +}
1857 +
1858 +static int recoverClose(sqlite3_vtab_cursor *cur){
1859 + RecoverCursor *pCursor = (RecoverCursor*)cur;
1860 + FNENTRY();
1861 + if( pCursor->pLeafCursor ){
1862 + leafCursorDestroy(pCursor->pLeafCursor);
1863 + pCursor->pLeafCursor = NULL;
1864 + }
1865 + memset(pCursor, 0xA5, sizeof(*pCursor));
1866 + sqlite3_free(cur);
1867 + return SQLITE_OK;
1868 +}
1869 +
1870 +/* Helpful place to set a breakpoint. */
1871 +static int RecoverInvalidCell(){
1872 + return SQLITE_ERROR;
1873 +}
1874 +
1875 +/* Returns SQLITE_OK if the cell has an appropriate number of columns
1876 + * with the appropriate types of data.
1877 + */
1878 +static int recoverValidateLeafCell(Recover *pRecover, RecoverCursor *pCursor){
1879 + unsigned i;
1880 +
1881 + /* If the row's storage has too many columns, skip it. */
1882 + if( leafCursorCellColumns(pCursor->pLeafCursor)>pRecover->nCols ){
1883 + return RecoverInvalidCell();
1884 + }
1885 +
1886 + /* Skip rows with unexpected types. */
1887 + for( i=0; i<pRecover->nCols; ++i ){
1888 + u64 iType; /* Storage type of column i. */
1889 + int rc;
1890 +
1891 + /* ROWID alias. */
1892 + if( (pRecover->pTypes[i]&MASK_ROWID) ){
1893 + continue;
1894 + }
1895 +
1896 + rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iType, NULL, NULL);
1897 + assert( rc==SQLITE_OK );
1898 + if( rc!=SQLITE_OK || !SerialTypeIsCompatible(iType, pRecover->pTypes[i]) ){
1899 + return RecoverInvalidCell();
1900 + }
1901 + }
1902 +
1903 + return SQLITE_OK;
1904 +}
1905 +
1906 +static int recoverNext(sqlite3_vtab_cursor *pVtabCursor){
1907 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1908 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1909 + int rc;
1910 +
1911 + FNENTRY();
1912 +
1913 + /* Scan forward to the next cell with valid storage, then check that
1914 + * the stored data matches the schema.
1915 + */
1916 + while( (rc = leafCursorNextValidCell(pCursor->pLeafCursor))==SQLITE_ROW ){
1917 + if( recoverValidateLeafCell(pRecover, pCursor)==SQLITE_OK ){
1918 + return SQLITE_OK;
1919 + }
1920 + }
1921 +
1922 + if( rc==SQLITE_DONE ){
1923 + pCursor->bEOF = 1;
1924 + return SQLITE_OK;
1925 + }
1926 +
1927 + assert( rc!=SQLITE_OK );
1928 + return rc;
1929 +}
1930 +
1931 +static int recoverFilter(
1932 + sqlite3_vtab_cursor *pVtabCursor,
1933 + int idxNum, const char *idxStr,
1934 + int argc, sqlite3_value **argv
1935 +){
1936 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1937 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1938 + int rc;
1939 +
1940 + FNENTRY();
1941 +
1942 + /* There were no valid leaf pages in the table. */
1943 + if( pCursor->bEOF ){
1944 + return SQLITE_OK;
1945 + }
1946 +
1947 + /* Load the first cell, and iterate forward if it's not valid. If no cells a t
1948 + * all are valid, recoverNext() sets bEOF and returns appropriately.
1949 + */
1950 + rc = leafCursorCellDecode(pCursor->pLeafCursor);
1951 + if( rc!=SQLITE_OK || recoverValidateLeafCell(pRecover, pCursor)!=SQLITE_OK ){
1952 + return recoverNext(pVtabCursor);
1953 + }
1954 +
1955 + return SQLITE_OK;
1956 +}
1957 +
1958 +static int recoverEof(sqlite3_vtab_cursor *pVtabCursor){
1959 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
1960 + FNENTRY();
1961 + return pCursor->bEOF;
1962 +}
1963 +
1964 +static int recoverColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i) {
1965 + RecoverCursor *pCursor = (RecoverCursor*)cur;
1966 + Recover *pRecover = (Recover*)pCursor->base.pVtab;
1967 + u64 iColType; /* Storage type of column i. */
1968 + unsigned char *pColData; /* Column i's data. */
1969 + int shouldFree; /* Non-zero if pColData should be freed. */
1970 + int rc;
1971 +
1972 + FNENTRY();
1973 +
1974 + if( (unsigned)i>=pRecover->nCols ){
1975 + return SQLITE_ERROR;
1976 + }
1977 +
1978 + /* ROWID alias. */
1979 + if( (pRecover->pTypes[i]&MASK_ROWID) ){
1980 + sqlite3_result_int64(ctx, leafCursorCellRowid(pCursor->pLeafCursor));
1981 + return SQLITE_OK;
1982 + }
1983 +
1984 + pColData = NULL;
1985 + shouldFree = 0;
1986 + rc = leafCursorCellColInfo(pCursor->pLeafCursor, i, &iColType,
1987 + &pColData, &shouldFree);
1988 + if( rc!=SQLITE_OK ){
1989 + return rc;
1990 + }
1991 + /* recoverValidateLeafCell() should guarantee that this will never
1992 + * occur.
1993 + */
1994 + if( !SerialTypeIsCompatible(iColType, pRecover->pTypes[i]) ){
1995 + if( shouldFree ){
1996 + sqlite3_free(pColData);
1997 + }
1998 + return SQLITE_ERROR;
1999 + }
2000 +
2001 + switch( iColType ){
2002 + case 0 : sqlite3_result_null(ctx); break;
2003 + case 1 : sqlite3_result_int64(ctx, decodeSigned(pColData, 1)); break;
2004 + case 2 : sqlite3_result_int64(ctx, decodeSigned(pColData, 2)); break;
2005 + case 3 : sqlite3_result_int64(ctx, decodeSigned(pColData, 3)); break;
2006 + case 4 : sqlite3_result_int64(ctx, decodeSigned(pColData, 4)); break;
2007 + case 5 : sqlite3_result_int64(ctx, decodeSigned(pColData, 6)); break;
2008 + case 6 : sqlite3_result_int64(ctx, decodeSigned(pColData, 8)); break;
2009 + case 7 : sqlite3_result_double(ctx, decodeFloat64(pColData)); break;
2010 + case 8 : sqlite3_result_int(ctx, 0); break;
2011 + case 9 : sqlite3_result_int(ctx, 1); break;
2012 + case 10 : assert( iColType!=10 ); break;
2013 + case 11 : assert( iColType!=11 ); break;
2014 +
2015 + default : {
2016 + u32 l = SerialTypeLength(iColType);
2017 +
2018 + /* If pColData was already allocated, arrange to pass ownership. */
2019 + sqlite3_destructor_type pFn = SQLITE_TRANSIENT;
2020 + if( shouldFree ){
2021 + pFn = sqlite3_free;
2022 + shouldFree = 0;
2023 + }
2024 +
2025 + if( SerialTypeIsBlob(iColType) ){
2026 + sqlite3_result_blob(ctx, pColData, l, pFn);
2027 + }else{
2028 + if( pCursor->iEncoding==SQLITE_UTF16LE ){
2029 + sqlite3_result_text16le(ctx, (const void*)pColData, l, pFn);
2030 + }else if( pCursor->iEncoding==SQLITE_UTF16BE ){
2031 + sqlite3_result_text16be(ctx, (const void*)pColData, l, pFn);
2032 + }else{
2033 + sqlite3_result_text(ctx, (const char*)pColData, l, pFn);
2034 + }
2035 + }
2036 + } break;
2037 + }
2038 + if( shouldFree ){
2039 + sqlite3_free(pColData);
2040 + }
2041 + return SQLITE_OK;
2042 +}
2043 +
2044 +static int recoverRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid) {
2045 + RecoverCursor *pCursor = (RecoverCursor*)pVtabCursor;
2046 + FNENTRY();
2047 + *pRowid = leafCursorCellRowid(pCursor->pLeafCursor);
2048 + return SQLITE_OK;
2049 +}
2050 +
2051 +static sqlite3_module recoverModule = {
2052 + 0, /* iVersion */
2053 + recoverCreate, /* xCreate - create a table */
2054 + recoverConnect, /* xConnect - connect to an existing table */
2055 + recoverBestIndex, /* xBestIndex - Determine search strategy */
2056 + recoverDisconnect, /* xDisconnect - Disconnect from a table */
2057 + recoverDestroy, /* xDestroy - Drop a table */
2058 + recoverOpen, /* xOpen - open a cursor */
2059 + recoverClose, /* xClose - close a cursor */
2060 + recoverFilter, /* xFilter - configure scan constraints */
2061 + recoverNext, /* xNext - advance a cursor */
2062 + recoverEof, /* xEof */
2063 + recoverColumn, /* xColumn - read data */
2064 + recoverRowid, /* xRowid - read data */
2065 + 0, /* xUpdate - write data */
2066 + 0, /* xBegin - begin transaction */
2067 + 0, /* xSync - sync transaction */
2068 + 0, /* xCommit - commit transaction */
2069 + 0, /* xRollback - rollback transaction */
2070 + 0, /* xFindFunction - function overloading */
2071 + 0, /* xRename - rename the table */
2072 +};
2073 +
2074 +SQLITE_API
2075 +int recoverVtableInit(sqlite3 *db){
2076 + return sqlite3_create_module_v2(db, "recover", &recoverModule, NULL, 0);
2077 +}
2078 +
2079 +/* This section of code is for parsing the create input and
2080 + * initializing the module.
2081 + */
2082 +
2083 +/* Find the next word in zText and place the endpoints in pzWord*.
2084 + * Returns true if the word is non-empty. "Word" is defined as
2085 + * ASCII alphanumeric plus '_' at this time.
2086 + */
2087 +static int findWord(const char *zText,
2088 + const char **pzWordStart, const char **pzWordEnd){
2089 + int r;
2090 + while( ascii_isspace(*zText) ){
2091 + zText++;
2092 + }
2093 + *pzWordStart = zText;
2094 + while( ascii_isalnum(*zText) || *zText=='_' ){
2095 + zText++;
2096 + }
2097 + r = zText>*pzWordStart; /* In case pzWordStart==pzWordEnd */
2098 + *pzWordEnd = zText;
2099 + return r;
2100 +}
2101 +
2102 +/* Return true if the next word in zText is zWord, also setting
2103 + * *pzContinue to the character after the word.
2104 + */
2105 +static int expectWord(const char *zText, const char *zWord,
2106 + const char **pzContinue){
2107 + const char *zWordStart, *zWordEnd;
2108 + if( findWord(zText, &zWordStart, &zWordEnd) &&
2109 + ascii_strncasecmp(zWord, zWordStart, zWordEnd - zWordStart)==0 ){
2110 + *pzContinue = zWordEnd;
2111 + return 1;
2112 + }
2113 + return 0;
2114 +}
2115 +
2116 +/* Parse the name and type information out of parameter. In case of
2117 + * success, *pzNameStart/End contain the name of the column,
2118 + * *pzTypeStart/End contain the top-level type, and *pTypeMask has the
2119 + * type mask to use for the column.
2120 + */
2121 +static int findNameAndType(const char *parameter,
2122 + const char **pzNameStart, const char **pzNameEnd,
2123 + const char **pzTypeStart, const char **pzTypeEnd,
2124 + unsigned char *pTypeMask){
2125 + unsigned nNameLen; /* Length of found name. */
2126 + const char *zEnd; /* Current end of parsed column information. */
2127 + int bNotNull; /* Non-zero if NULL is not allowed for name. */
2128 + int bStrict; /* Non-zero if column requires exact type match. */
2129 + const char *zDummy; /* Dummy parameter, result unused. */
2130 + unsigned i;
2131 +
2132 + /* strictMask is used for STRICT, strictMask|otherMask if STRICT is
2133 + * not supplied. zReplace provides an alternate type to expose to
2134 + * the caller.
2135 + */
2136 + static struct {
2137 + const char *zName;
2138 + unsigned char strictMask;
2139 + unsigned char otherMask;
2140 + const char *zReplace;
2141 + } kTypeInfo[] = {
2142 + { "ANY",
2143 + MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL,
2144 + 0, "",
2145 + },
2146 + { "ROWID", MASK_INTEGER | MASK_ROWID, 0, "INTEGER", },
2147 + { "INTEGER", MASK_INTEGER | MASK_NULL, 0, NULL, },
2148 + { "FLOAT", MASK_FLOAT | MASK_NULL, MASK_INTEGER, NULL, },
2149 + { "NUMERIC", MASK_INTEGER | MASK_FLOAT | MASK_NULL, MASK_TEXT, NULL, },
2150 + { "TEXT", MASK_TEXT | MASK_NULL, MASK_BLOB, NULL, },
2151 + { "BLOB", MASK_BLOB | MASK_NULL, 0, NULL, },
2152 + };
2153 +
2154 + if( !findWord(parameter, pzNameStart, pzNameEnd) ){
2155 + return SQLITE_MISUSE;
2156 + }
2157 +
2158 + /* Manifest typing, accept any storage type. */
2159 + if( !findWord(*pzNameEnd, pzTypeStart, pzTypeEnd) ){
2160 + *pzTypeEnd = *pzTypeStart = "";
2161 + *pTypeMask = MASK_INTEGER | MASK_FLOAT | MASK_BLOB | MASK_TEXT | MASK_NULL;
2162 + return SQLITE_OK;
2163 + }
2164 +
2165 + nNameLen = *pzTypeEnd - *pzTypeStart;
2166 + for( i=0; i<ArraySize(kTypeInfo); ++i ){
2167 + if( ascii_strncasecmp(kTypeInfo[i].zName, *pzTypeStart, nNameLen)==0 ){
2168 + break;
2169 + }
2170 + }
2171 + if( i==ArraySize(kTypeInfo) ){
2172 + return SQLITE_MISUSE;
2173 + }
2174 +
2175 + zEnd = *pzTypeEnd;
2176 + bStrict = 0;
2177 + if( expectWord(zEnd, "STRICT", &zEnd) ){
2178 + /* TODO(shess): Ick. But I don't want another single-purpose
2179 + * flag, either.
2180 + */
2181 + if( kTypeInfo[i].zReplace && !kTypeInfo[i].zReplace[0] ){
2182 + return SQLITE_MISUSE;
2183 + }
2184 + bStrict = 1;
2185 + }
2186 +
2187 + bNotNull = 0;
2188 + if( expectWord(zEnd, "NOT", &zEnd) ){
2189 + if( expectWord(zEnd, "NULL", &zEnd) ){
2190 + bNotNull = 1;
2191 + }else{
2192 + /* Anything other than NULL after NOT is an error. */
2193 + return SQLITE_MISUSE;
2194 + }
2195 + }
2196 +
2197 + /* Anything else is an error. */
2198 + if( findWord(zEnd, &zDummy, &zDummy) ){
2199 + return SQLITE_MISUSE;
2200 + }
2201 +
2202 + *pTypeMask = kTypeInfo[i].strictMask;
2203 + if( !bStrict ){
2204 + *pTypeMask |= kTypeInfo[i].otherMask;
2205 + }
2206 + if( bNotNull ){
2207 + *pTypeMask &= ~MASK_NULL;
2208 + }
2209 + if( kTypeInfo[i].zReplace ){
2210 + *pzTypeStart = kTypeInfo[i].zReplace;
2211 + *pzTypeEnd = *pzTypeStart + strlen(*pzTypeStart);
2212 + }
2213 + return SQLITE_OK;
2214 +}
2215 +
2216 +/* Parse the arguments, placing type masks in *pTypes and the exposed
2217 + * schema in *pzCreateSql (for sqlite3_declare_vtab).
2218 + */
2219 +static int ParseColumnsAndGenerateCreate(unsigned nCols,
2220 + const char *const *pCols,
2221 + char **pzCreateSql,
2222 + unsigned char *pTypes,
2223 + char **pzErr){
2224 + unsigned i;
2225 + char *zCreateSql = sqlite3_mprintf("CREATE TABLE x(");
2226 + if( !zCreateSql ){
2227 + return SQLITE_NOMEM;
2228 + }
2229 +
2230 + for( i=0; i<nCols; i++ ){
2231 + const char *zSep = (i < nCols - 1 ? ", " : ")");
2232 + const char *zNotNull = "";
2233 + const char *zNameStart, *zNameEnd;
2234 + const char *zTypeStart, *zTypeEnd;
2235 + int rc = findNameAndType(pCols[i],
2236 + &zNameStart, &zNameEnd,
2237 + &zTypeStart, &zTypeEnd,
2238 + &pTypes[i]);
2239 + if( rc!=SQLITE_OK ){
2240 + *pzErr = sqlite3_mprintf("unable to parse column %d", i);
2241 + sqlite3_free(zCreateSql);
2242 + return rc;
2243 + }
2244 +
2245 + if( !(pTypes[i]&MASK_NULL) ){
2246 + zNotNull = " NOT NULL";
2247 + }
2248 +
2249 + /* Add name and type to the create statement. */
2250 + zCreateSql = sqlite3_mprintf("%z%.*s %.*s%s%s",
2251 + zCreateSql,
2252 + zNameEnd - zNameStart, zNameStart,
2253 + zTypeEnd - zTypeStart, zTypeStart,
2254 + zNotNull, zSep);
2255 + if( !zCreateSql ){
2256 + return SQLITE_NOMEM;
2257 + }
2258 + }
2259 +
2260 + *pzCreateSql = zCreateSql;
2261 + return SQLITE_OK;
2262 +}
2263 +
2264 +/* Helper function for initializing the module. */
2265 +/* argv[0] module name
2266 + * argv[1] db name for virtual table
2267 + * argv[2] virtual table name
2268 + * argv[3] backing table name
2269 + * argv[4] columns
2270 + */
2271 +/* TODO(shess): Since connect isn't supported, could inline into
2272 + * recoverCreate().
2273 + */
2274 +/* TODO(shess): Explore cases where it would make sense to set *pzErr. */
2275 +static int recoverInit(
2276 + sqlite3 *db, /* Database connection */
2277 + void *pAux, /* unused */
2278 + int argc, const char *const*argv, /* Parameters to CREATE TABLE statement * /
2279 + sqlite3_vtab **ppVtab, /* OUT: New virtual table */
2280 + char **pzErr /* OUT: Error message, if any */
2281 +){
2282 + const int kTypeCol = 4; /* First argument with column type info. */
2283 + Recover *pRecover; /* Virtual table structure being created. */
2284 + char *zDot; /* Any dot found in "db.table" backing. */
2285 + u32 iRootPage; /* Root page of backing table. */
2286 + char *zCreateSql; /* Schema of created virtual table. */
2287 + int rc;
2288 +
2289 + /* Require to be in the temp database. */
2290 + if( ascii_strcasecmp(argv[1], "temp")!=0 ){
2291 + *pzErr = sqlite3_mprintf("recover table must be in temp database");
2292 + return SQLITE_MISUSE;
2293 + }
2294 +
2295 + /* Need the backing table and at least one column. */
2296 + if( argc<=kTypeCol ){
2297 + *pzErr = sqlite3_mprintf("no columns specified");
2298 + return SQLITE_MISUSE;
2299 + }
2300 +
2301 + pRecover = sqlite3_malloc(sizeof(Recover));
2302 + if( !pRecover ){
2303 + return SQLITE_NOMEM;
2304 + }
2305 + memset(pRecover, 0, sizeof(*pRecover));
2306 + pRecover->base.pModule = &recoverModule;
2307 + pRecover->db = db;
2308 +
2309 + /* Parse out db.table, assuming main if no dot. */
2310 + zDot = strchr(argv[3], '.');
2311 + if( !zDot ){
2312 + pRecover->zDb = sqlite3_strdup("main");
2313 + pRecover->zTable = sqlite3_strdup(argv[3]);
2314 + }else if( zDot>argv[3] && zDot[1]!='\0' ){
2315 + pRecover->zDb = sqlite3_strndup(argv[3], zDot - argv[3]);
2316 + pRecover->zTable = sqlite3_strdup(zDot + 1);
2317 + }else{
2318 + /* ".table" or "db." not allowed. */
2319 + *pzErr = sqlite3_mprintf("ill-formed table specifier");
2320 + recoverRelease(pRecover);
2321 + return SQLITE_ERROR;
2322 + }
2323 +
2324 + pRecover->nCols = argc - kTypeCol;
2325 + pRecover->pTypes = sqlite3_malloc(pRecover->nCols);
2326 + if( !pRecover->zDb || !pRecover->zTable || !pRecover->pTypes ){
2327 + recoverRelease(pRecover);
2328 + return SQLITE_NOMEM;
2329 + }
2330 +
2331 + /* Require the backing table to exist. */
2332 + /* TODO(shess): Be more pedantic about the form of the descriptor
2333 + * string. This already fails for poorly-formed strings, simply
2334 + * because there won't be a root page, but it would make more sense
2335 + * to be explicit.
2336 + */
2337 + rc = getRootPage(pRecover->db, pRecover->zDb, pRecover->zTable, &iRootPage);
2338 + if( rc!=SQLITE_OK ){
2339 + *pzErr = sqlite3_mprintf("unable to find backing table");
2340 + recoverRelease(pRecover);
2341 + return rc;
2342 + }
2343 +
2344 + /* Parse the column definitions. */
2345 + rc = ParseColumnsAndGenerateCreate(pRecover->nCols, argv + kTypeCol,
2346 + &zCreateSql, pRecover->pTypes, pzErr);
2347 + if( rc!=SQLITE_OK ){
2348 + recoverRelease(pRecover);
2349 + return rc;
2350 + }
2351 +
2352 + rc = sqlite3_declare_vtab(db, zCreateSql);
2353 + sqlite3_free(zCreateSql);
2354 + if( rc!=SQLITE_OK ){
2355 + recoverRelease(pRecover);
2356 + return rc;
2357 + }
2358 +
2359 + *ppVtab = (sqlite3_vtab *)pRecover;
2360 + return SQLITE_OK;
2361 +}
2362 diff --git a/third_party/sqlite/src/src/recover.h b/third_party/sqlite/src/src/r ecover.h
2363 new file mode 100644
2364 index 0000000..691f2fd
2365 --- /dev/null
2366 +++ b/third_party/sqlite/src/src/recover.h
2367 @@ -0,0 +1,23 @@
2368 +/* TODO(shess): sqliteicu.h is able to make this include without
2369 +** trouble. It doesn't work when used with Chromium's SQLite. For
2370 +** now the including code must include sqlite3.h first.
2371 +*/
2372 +/* #include "sqlite3.h" */
2373 +
2374 +#ifdef __cplusplus
2375 +extern "C" {
2376 +#endif
2377 +
2378 +/*
2379 +** Call to initialize the recover virtual-table modules (see recover.c).
2380 +**
2381 +** This could be loaded by default in main.c, but that would make the
2382 +** virtual table available to Web SQL. Breaking it out allows only
2383 +** selected users to enable it (currently sql/recovery.cc).
2384 +*/
2385 +SQLITE_API
2386 +int recoverVtableInit(sqlite3 *db);
2387 +
2388 +#ifdef __cplusplus
2389 +} /* End of the 'extern "C"' block */
2390 +#endif
2391 diff --git a/third_party/sqlite/src/src/recover_varint.c b/third_party/sqlite/sr c/src/recover_varint.c
2392 new file mode 100644
2393 index 0000000..c111e2c
2394 --- /dev/null
2395 +++ b/third_party/sqlite/src/src/recover_varint.c
2396 @@ -0,0 +1,201 @@
2397 +/*
2398 +** 2016 Feb 29
2399 +**
2400 +** The author disclaims copyright to this source code. In place of
2401 +** a legal notice, here is a blessing:
2402 +**
2403 +** May you do good and not evil.
2404 +** May you find forgiveness for yourself and forgive others.
2405 +** May you share freely, never taking more than you give.
2406 +**
2407 +******************************************************************************
2408 +**
2409 +** Copy of sqlite3Fts5GetVarint() from fts3_varint.c, which in turn is copied
2410 +** from SQLite core.
2411 +*/
2412 +
2413 +#include <assert.h>
2414 +#include "sqlite3.h"
2415 +
2416 +/* Copied from fts3int.h. */
2417 +#ifndef SQLITE_AMALGAMATION
2418 +typedef unsigned char u8;
2419 +typedef unsigned int u32;
2420 +typedef sqlite3_uint64 u64;
2421 +#endif
2422 +
2423 +/*
2424 +** Bitmasks used by recoverGetVarint(). These precomputed constants
2425 +** are defined here rather than simply putting the constant expressions
2426 +** inline in order to work around bugs in the RVT compiler.
2427 +**
2428 +** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
2429 +**
2430 +** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
2431 +*/
2432 +#define SLOT_2_0 0x001fc07f
2433 +#define SLOT_4_2_0 0xf01fc07f
2434 +
2435 +/*
2436 +** Read a 64-bit variable-length integer from memory starting at p[0].
2437 +** Return the number of bytes read. The value is stored in *v.
2438 +*/
2439 +u8 recoverGetVarint(const unsigned char *p, u64 *v){
2440 + u32 a,b,s;
2441 +
2442 + a = *p;
2443 + /* a: p0 (unmasked) */
2444 + if (!(a&0x80))
2445 + {
2446 + *v = a;
2447 + return 1;
2448 + }
2449 +
2450 + p++;
2451 + b = *p;
2452 + /* b: p1 (unmasked) */
2453 + if (!(b&0x80))
2454 + {
2455 + a &= 0x7f;
2456 + a = a<<7;
2457 + a |= b;
2458 + *v = a;
2459 + return 2;
2460 + }
2461 +
2462 + /* Verify that constants are precomputed correctly */
2463 + assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
2464 + assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
2465 +
2466 + p++;
2467 + a = a<<14;
2468 + a |= *p;
2469 + /* a: p0<<14 | p2 (unmasked) */
2470 + if (!(a&0x80))
2471 + {
2472 + a &= SLOT_2_0;
2473 + b &= 0x7f;
2474 + b = b<<7;
2475 + a |= b;
2476 + *v = a;
2477 + return 3;
2478 + }
2479 +
2480 + /* CSE1 from below */
2481 + a &= SLOT_2_0;
2482 + p++;
2483 + b = b<<14;
2484 + b |= *p;
2485 + /* b: p1<<14 | p3 (unmasked) */
2486 + if (!(b&0x80))
2487 + {
2488 + b &= SLOT_2_0;
2489 + /* moved CSE1 up */
2490 + /* a &= (0x7f<<14)|(0x7f); */
2491 + a = a<<7;
2492 + a |= b;
2493 + *v = a;
2494 + return 4;
2495 + }
2496 +
2497 + /* a: p0<<14 | p2 (masked) */
2498 + /* b: p1<<14 | p3 (unmasked) */
2499 + /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
2500 + /* moved CSE1 up */
2501 + /* a &= (0x7f<<14)|(0x7f); */
2502 + b &= SLOT_2_0;
2503 + s = a;
2504 + /* s: p0<<14 | p2 (masked) */
2505 +
2506 + p++;
2507 + a = a<<14;
2508 + a |= *p;
2509 + /* a: p0<<28 | p2<<14 | p4 (unmasked) */
2510 + if (!(a&0x80))
2511 + {
2512 + /* we can skip these cause they were (effectively) done above in calc'ing s */
2513 + /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
2514 + /* b &= (0x7f<<14)|(0x7f); */
2515 + b = b<<7;
2516 + a |= b;
2517 + s = s>>18;
2518 + *v = ((u64)s)<<32 | a;
2519 + return 5;
2520 + }
2521 +
2522 + /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
2523 + s = s<<7;
2524 + s |= b;
2525 + /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
2526 +
2527 + p++;
2528 + b = b<<14;
2529 + b |= *p;
2530 + /* b: p1<<28 | p3<<14 | p5 (unmasked) */
2531 + if (!(b&0x80))
2532 + {
2533 + /* we can skip this cause it was (effectively) done above in calc'ing s */
2534 + /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
2535 + a &= SLOT_2_0;
2536 + a = a<<7;
2537 + a |= b;
2538 + s = s>>18;
2539 + *v = ((u64)s)<<32 | a;
2540 + return 6;
2541 + }
2542 +
2543 + p++;
2544 + a = a<<14;
2545 + a |= *p;
2546 + /* a: p2<<28 | p4<<14 | p6 (unmasked) */
2547 + if (!(a&0x80))
2548 + {
2549 + a &= SLOT_4_2_0;
2550 + b &= SLOT_2_0;
2551 + b = b<<7;
2552 + a |= b;
2553 + s = s>>11;
2554 + *v = ((u64)s)<<32 | a;
2555 + return 7;
2556 + }
2557 +
2558 + /* CSE2 from below */
2559 + a &= SLOT_2_0;
2560 + p++;
2561 + b = b<<14;
2562 + b |= *p;
2563 + /* b: p3<<28 | p5<<14 | p7 (unmasked) */
2564 + if (!(b&0x80))
2565 + {
2566 + b &= SLOT_4_2_0;
2567 + /* moved CSE2 up */
2568 + /* a &= (0x7f<<14)|(0x7f); */
2569 + a = a<<7;
2570 + a |= b;
2571 + s = s>>4;
2572 + *v = ((u64)s)<<32 | a;
2573 + return 8;
2574 + }
2575 +
2576 + p++;
2577 + a = a<<15;
2578 + a |= *p;
2579 + /* a: p4<<29 | p6<<15 | p8 (unmasked) */
2580 +
2581 + /* moved CSE2 up */
2582 + /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
2583 + b &= SLOT_2_0;
2584 + b = b<<8;
2585 + a |= b;
2586 +
2587 + s = s<<4;
2588 + b = p[-4];
2589 + b &= 0x7f;
2590 + b = b>>3;
2591 + s |= b;
2592 +
2593 + *v = ((u64)s)<<32 | a;
2594 +
2595 + return 9;
2596 +}
2597 +
2598 diff --git a/third_party/sqlite/src/test/recover.test b/third_party/sqlite/src/t est/recover.test
2599 new file mode 100644
2600 index 0000000..bfb7888
2601 --- /dev/null
2602 +++ b/third_party/sqlite/src/test/recover.test
2603 @@ -0,0 +1,164 @@
2604 +# 2012 January 11 {}
2605 +#
2606 +# The author disclaims copyright to this source code. In place of
2607 +# a legal notice, here is a blessing:
2608 +#
2609 +# May you do good and not evil.
2610 +# May you find forgiveness for yourself and forgive others.
2611 +# May you share freely, never taking more than you give.
2612 +#
2613 +#***********************************************************************
2614 +# This file implements regression tests for SQLite library.
2615 +#
2616 +# This file implements tests for the recover module, which can read
2617 +# through corrupt rows and pages.
2618 +#
2619 +# $Id$
2620 +
2621 +# TODO(shess): These all test that the module correctly reads good
2622 +# data. It would be good to implement tests of corrupt data.
2623 +
2624 +set testdir [file dirname $argv0]
2625 +source $testdir/tester.tcl
2626 +
2627 +db eval {
2628 + DROP TABLE IF EXISTS altered;
2629 + CREATE TABLE altered (
2630 + c TEXT
2631 + );
2632 + INSERT INTO altered VALUES ('a');
2633 + INSERT INTO altered VALUES ('b');
2634 + INSERT INTO altered VALUES ('c');
2635 + ALTER TABLE altered ADD COLUMN i INTEGER NOT NULL DEFAULT 10;
2636 + INSERT INTO altered VALUES ('d', 5);
2637 +}
2638 +
2639 +# SQLite will fill the earlier rows with the default.
2640 +do_test recover-alter-1.0 {
2641 + execsql {SELECT c, i FROM altered ORDER BY rowid}
2642 +} {a 10 b 10 c 10 d 5}
2643 +
2644 +# recover sees NULL for those rows.
2645 +do_test recover-alter-1.1 {
2646 + db eval {
2647 + DROP TABLE IF EXISTS temp.altered_recover;
2648 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2649 + altered,
2650 + c TEXT,
2651 + i INTEGER
2652 + );
2653 + }
2654 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2655 +} {a {} b {} c {} d 5}
2656 +
2657 +# Can skip those NULL columns like if they contained a real NULL.
2658 +do_test recover-alter-1.2 {
2659 + db eval {
2660 + DROP TABLE IF EXISTS temp.altered_recover;
2661 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2662 + altered,
2663 + c TEXT,
2664 + i INTEGER NOT NULL
2665 + );
2666 + }
2667 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2668 +} {d 5}
2669 +
2670 +if {0} {
2671 +# It would be neat if this could work. I tried putting "DEFAULT ..."
2672 +# in the schema exposed by the recover table, but it doesn't do the
2673 +# trick.
2674 +do_test recover-alter-1.2 {
2675 + db eval {
2676 + DROP TABLE IF EXISTS temp.altered_recover;
2677 + CREATE VIRTUAL TABLE temp.altered_recover USING recover(
2678 + altered,
2679 + c TEXT,
2680 + i INTEGER NOT NULL DEFAULT 10
2681 + );
2682 + }
2683 + execsql {SELECT c, i FROM altered_recover ORDER BY rowid}
2684 +} {a 10 b 10 c 10 d 5}
2685 +}
2686 +
2687 +# Helper function to generate an arbitrarily-sized table.
2688 +proc generate {table base count} {
2689 + db eval "DROP TABLE IF EXISTS $table"
2690 + db transaction immediate {
2691 + db eval "CREATE TABLE $table (t TEXT,n INT)"
2692 + for {set i 0} {$i<$count} {incr i} {
2693 + set t [concat $base $i]
2694 + db eval [concat {INSERT INTO} $table {VALUES ($t, $i)}]
2695 + }
2696 + }
2697 +}
2698 +
2699 +# Leaf-only database parses.
2700 +do_test recover-leaf-1.0 {
2701 + db close
2702 + sqlite3 db test.db
2703 + generate "leaf" "Leaf-node-generating line " 10
2704 +
2705 + db eval {
2706 + DROP TABLE IF EXISTS temp.leaf_recover;
2707 + CREATE VIRTUAL TABLE temp.leaf_recover USING recover(
2708 + leaf,
2709 + t TEXT,
2710 + n INTEGER
2711 + );
2712 + }
2713 + execsql {SELECT t, n FROM leaf_recover ORDER BY rowid}
2714 +} {{Leaf-node-generating line 0} 0 {Leaf-node-generating line 1} 1 {Leaf-node-g enerating line 2} 2 {Leaf-node-generating line 3} 3 {Leaf-node-generating line 4 } 4 {Leaf-node-generating line 5} 5 {Leaf-node-generating line 6} 6 {Leaf-node-g enerating line 7} 7 {Leaf-node-generating line 8} 8 {Leaf-node-generating line 9 } 9}
2715 +
2716 +# Empty table gives empty results.
2717 +do_test recover-leaf-2.0 {
2718 + db close
2719 + sqlite3 db test.db
2720 + generate "empty" "Leaf-node-generating line " 0
2721 +
2722 + db eval {
2723 + DROP TABLE IF EXISTS temp.leaf_recover;
2724 + CREATE VIRTUAL TABLE temp.leaf_recover USING recover(
2725 + empty,
2726 + t TEXT,
2727 + n INTEGER
2728 + );
2729 + }
2730 + execsql {SELECT t, n FROM leaf_recover ORDER BY rowid}
2731 +} {}
2732 +
2733 +# Single level of interior node.
2734 +do_test recover-interior-1.0 {
2735 + db close
2736 + sqlite3 db test.db
2737 + generate "interior" "Interior-node-generating line " 100
2738 +
2739 + db eval {
2740 + DROP TABLE IF EXISTS temp.interior_recover;
2741 + CREATE VIRTUAL TABLE temp.interior_recover USING recover(
2742 + interior,
2743 + t TEXT,
2744 + n INTEGER
2745 + );
2746 + }
2747 + execsql {SELECT t, n FROM interior_recover WHERE (rowid%10)=0 ORDER BY rowid}
2748 +} {{Interior-node-generating line 9} 9 {Interior-node-generating line 19} 19 {I nterior-node-generating line 29} 29 {Interior-node-generating line 39} 39 {Inter ior-node-generating line 49} 49 {Interior-node-generating line 59} 59 {Interior- node-generating line 69} 69 {Interior-node-generating line 79} 79 {Interior-node -generating line 89} 89 {Interior-node-generating line 99} 99}
2749 +
2750 +# Multiple levels of interior node.
2751 +do_test recover-interior-2.0 {
2752 + db close
2753 + sqlite3 db test.db
2754 + generate "interior2" "Interior-node-generating line " 5000
2755 +
2756 + db eval {
2757 + DROP TABLE IF EXISTS temp.interior2_recover;
2758 + CREATE VIRTUAL TABLE temp.interior2_recover USING recover(
2759 + interior2,
2760 + t TEXT,
2761 + n INTEGER
2762 + );
2763 + }
2764 + execsql {SELECT t, n FROM interior2_recover WHERE (rowid%500)=0 ORDER BY rowi d}
2765 +} {{Interior-node-generating line 499} 499 {Interior-node-generating line 999} 999 {Interior-node-generating line 1499} 1499 {Interior-node-generating line 199 9} 1999 {Interior-node-generating line 2499} 2499 {Interior-node-generating line 2999} 2999 {Interior-node-generating line 3499} 3499 {Interior-node-generating line 3999} 3999 {Interior-node-generating line 4499} 4499 {Interior-node-generat ing line 4999} 4999}
2766 +
2767 +finish_test
2768 diff --git a/third_party/sqlite/src/test/recover0.test b/third_party/sqlite/src/ test/recover0.test
2769 new file mode 100644
2770 index 0000000..aac2ed9
2771 --- /dev/null
2772 +++ b/third_party/sqlite/src/test/recover0.test
2773 @@ -0,0 +1,532 @@
2774 +# 2012 January 4 {}
2775 +#
2776 +# The author disclaims copyright to this source code. In place of
2777 +# a legal notice, here is a blessing:
2778 +#
2779 +# May you do good and not evil.
2780 +# May you find forgiveness for yourself and forgive others.
2781 +# May you share freely, never taking more than you give.
2782 +#
2783 +#***********************************************************************
2784 +# This file implements regression tests for SQLite library.
2785 +#
2786 +# Test recover module syntax.
2787 +#
2788 +# $Id$
2789 +
2790 +# TODO(shess): Test with attached databases.
2791 +
2792 +# TODO(shess): Handle column mismatches? As things stand, the code
2793 +# only needs to pull the root page, so that may not be completely
2794 +# feasible.
2795 +
2796 +set testdir [file dirname $argv0]
2797 +source $testdir/tester.tcl
2798 +
2799 +db eval {
2800 + DROP TABLE IF EXISTS backing;
2801 + CREATE TABLE backing (t TEXT);
2802 +
2803 + DROP TABLE IF EXISTS backing2;
2804 + CREATE TABLE backing2 (id INTEGER PRIMARY KEY, t TEXT);
2805 +}
2806 +
2807 +# Baseline create works.
2808 +do_test recover-syntax-0.0 {
2809 + db eval {DROP TABLE IF EXISTS temp.syntax}
2810 + catchsql {
2811 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2812 + backing,
2813 + t TEXT
2814 + );
2815 + }
2816 +} {0 {}}
2817 +
2818 +# Can specify database.
2819 +do_test recover-syntax-0.1 {
2820 + db eval {DROP TABLE IF EXISTS temp.syntax}
2821 + catchsql {
2822 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2823 + main.backing,
2824 + t TEXT
2825 + );
2826 + }
2827 +} {0 {}}
2828 +
2829 +# Can specify sqlite_master.
2830 +do_test recover-syntax-0.2 {
2831 + db eval {DROP TABLE IF EXISTS temp.syntax}
2832 + catchsql {
2833 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2834 + sqlite_master,
2835 + type TEXT,
2836 + name TEXT,
2837 + tbl_name TEXT,
2838 + rootpage INTEGER,
2839 + sql TEXT
2840 + );
2841 + }
2842 +} {0 {}}
2843 +
2844 +# Fails if virtual table is not in the temp database.
2845 +do_test recover-syntax-1.0 {
2846 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2847 + catchsql {
2848 + CREATE VIRTUAL TABLE syntax USING recover(
2849 + backing,
2850 + t TEXT
2851 + );
2852 + }
2853 +} {1 {recover table must be in temp database}}
2854 +
2855 +# Fails if mentions missing table.
2856 +do_test recover-syntax-2.0 {
2857 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2858 + catchsql {
2859 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2860 + snacking,
2861 + t TEXT
2862 + );
2863 + }
2864 +} {1 {unable to find backing table}}
2865 +
2866 +# Fails if mentions missing database.
2867 +do_test recover-syntax-2.1 {
2868 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2869 + catchsql {
2870 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2871 + db.backing,
2872 + t TEXT
2873 + );
2874 + }
2875 +} {1 {unable to find backing table}}
2876 +
2877 +# Fails if mentions garbage backing.
2878 +do_test recover-syntax-2.2 {
2879 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2880 + catchsql {
2881 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2882 + main.backing excess,
2883 + t TEXT
2884 + );
2885 + }
2886 +} {1 {unable to find backing table}}
2887 +
2888 +# Database only fails.
2889 +do_test recover-syntax-2.3 {
2890 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2891 + catchsql {
2892 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2893 + main.,
2894 + t TEXT
2895 + );
2896 + }
2897 +} {1 {ill-formed table specifier}}
2898 +
2899 +# Table only fails.
2900 +do_test recover-syntax-2.4 {
2901 + db eval {DROP TABLE IF EXISTS temp.syntax;}
2902 + catchsql {
2903 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2904 + .backing,
2905 + t TEXT
2906 + );
2907 + }
2908 +} {1 {ill-formed table specifier}}
2909 +
2910 +# Manifest typing.
2911 +do_test recover-syntax-3.0 {
2912 + db eval {DROP TABLE IF EXISTS temp.syntax}
2913 + execsql {
2914 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2915 + backing,
2916 + t
2917 + );
2918 + PRAGMA table_info(syntax);
2919 + }
2920 +} {0 t {} 0 {} 0}
2921 +
2922 +# ANY as an alternative for manifest typing.
2923 +do_test recover-syntax-3.1 {
2924 + db eval {DROP TABLE IF EXISTS temp.syntax}
2925 + execsql {
2926 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2927 + backing,
2928 + t ANY
2929 + );
2930 + PRAGMA table_info(syntax);
2931 + }
2932 +} {0 t {} 0 {} 0}
2933 +
2934 +# ANY NOT NULL
2935 +do_test recover-syntax-3.2 {
2936 + db eval {DROP TABLE IF EXISTS temp.syntax}
2937 + execsql {
2938 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2939 + backing,
2940 + t ANY NOT NULL
2941 + );
2942 + PRAGMA table_info(syntax);
2943 + }
2944 +} {0 t {} 1 {} 0}
2945 +
2946 +# ANY STRICT is not sensible.
2947 +do_test recover-syntax-3.3 {
2948 + db eval {DROP TABLE IF EXISTS temp.syntax}
2949 + catchsql {
2950 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2951 + backing,
2952 + v ANY STRICT
2953 + );
2954 + PRAGMA table_info(syntax);
2955 + }
2956 +} {1 {unable to parse column 0}}
2957 +
2958 +# TEXT column by type works.
2959 +do_test recover-syntax-4.0 {
2960 + db eval {DROP TABLE IF EXISTS temp.syntax}
2961 + execsql {
2962 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2963 + backing,
2964 + t TEXT
2965 + );
2966 + PRAGMA table_info(syntax);
2967 + }
2968 +} {0 t TEXT 0 {} 0}
2969 +
2970 +# TEXT NOT NULL
2971 +do_test recover-syntax-4.1 {
2972 + db eval {DROP TABLE IF EXISTS temp.syntax}
2973 + execsql {
2974 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2975 + backing,
2976 + t TEXT NOT NULL
2977 + );
2978 + PRAGMA table_info(syntax);
2979 + }
2980 +} {0 t TEXT 1 {} 0}
2981 +
2982 +# TEXT STRICT
2983 +do_test recover-syntax-4.2 {
2984 + db eval {DROP TABLE IF EXISTS temp.syntax}
2985 + execsql {
2986 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2987 + backing,
2988 + t TEXT STRICT
2989 + );
2990 + PRAGMA table_info(syntax);
2991 + }
2992 +} {0 t TEXT 0 {} 0}
2993 +
2994 +# TEXT STRICT NOT NULL
2995 +do_test recover-syntax-4.3 {
2996 + db eval {DROP TABLE IF EXISTS temp.syntax}
2997 + execsql {
2998 + CREATE VIRTUAL TABLE temp.syntax USING recover(
2999 + backing,
3000 + t TEXT STRICT NOT NULL
3001 + );
3002 + PRAGMA table_info(syntax);
3003 + }
3004 +} {0 t TEXT 1 {} 0}
3005 +
3006 +# INTEGER
3007 +do_test recover-syntax-5.0 {
3008 + db eval {DROP TABLE IF EXISTS temp.syntax}
3009 + execsql {
3010 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3011 + backing,
3012 + i INTEGER
3013 + );
3014 + PRAGMA table_info(syntax);
3015 + }
3016 +} {0 i INTEGER 0 {} 0}
3017 +
3018 +# INTEGER NOT NULL
3019 +do_test recover-syntax-5.1 {
3020 + db eval {DROP TABLE IF EXISTS temp.syntax}
3021 + execsql {
3022 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3023 + backing,
3024 + i INTEGER NOT NULL
3025 + );
3026 + PRAGMA table_info(syntax);
3027 + }
3028 +} {0 i INTEGER 1 {} 0}
3029 +
3030 +# INTEGER STRICT
3031 +do_test recover-syntax-5.2 {
3032 + db eval {DROP TABLE IF EXISTS temp.syntax}
3033 + execsql {
3034 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3035 + backing,
3036 + i INTEGER STRICT
3037 + );
3038 + PRAGMA table_info(syntax);
3039 + }
3040 +} {0 i INTEGER 0 {} 0}
3041 +
3042 +# INTEGER STRICT NOT NULL
3043 +do_test recover-syntax-5.3 {
3044 + db eval {DROP TABLE IF EXISTS temp.syntax}
3045 + execsql {
3046 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3047 + backing,
3048 + i INTEGER STRICT NOT NULL
3049 + );
3050 + PRAGMA table_info(syntax);
3051 + }
3052 +} {0 i INTEGER 1 {} 0}
3053 +
3054 +# BLOB
3055 +do_test recover-syntax-6.0 {
3056 + db eval {DROP TABLE IF EXISTS temp.syntax}
3057 + execsql {
3058 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3059 + backing,
3060 + b BLOB
3061 + );
3062 + PRAGMA table_info(syntax);
3063 + }
3064 +} {0 b BLOB 0 {} 0}
3065 +
3066 +# BLOB NOT NULL
3067 +do_test recover-syntax-6.1 {
3068 + db eval {DROP TABLE IF EXISTS temp.syntax}
3069 + execsql {
3070 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3071 + backing,
3072 + b BLOB NOT NULL
3073 + );
3074 + PRAGMA table_info(syntax);
3075 + }
3076 +} {0 b BLOB 1 {} 0}
3077 +
3078 +# BLOB STRICT
3079 +do_test recover-syntax-6.2 {
3080 + db eval {DROP TABLE IF EXISTS temp.syntax}
3081 + execsql {
3082 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3083 + backing,
3084 + b BLOB STRICT
3085 + );
3086 + PRAGMA table_info(syntax);
3087 + }
3088 +} {0 b BLOB 0 {} 0}
3089 +
3090 +# BLOB STRICT NOT NULL
3091 +do_test recover-syntax-6.3 {
3092 + db eval {DROP TABLE IF EXISTS temp.syntax}
3093 + execsql {
3094 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3095 + backing,
3096 + b BLOB STRICT NOT NULL
3097 + );
3098 + PRAGMA table_info(syntax);
3099 + }
3100 +} {0 b BLOB 1 {} 0}
3101 +
3102 +# FLOAT
3103 +do_test recover-syntax-7.0 {
3104 + db eval {DROP TABLE IF EXISTS temp.syntax}
3105 + execsql {
3106 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3107 + backing,
3108 + f FLOAT
3109 + );
3110 + PRAGMA table_info(syntax);
3111 + }
3112 +} {0 f FLOAT 0 {} 0}
3113 +
3114 +# FLOAT NOT NULL
3115 +do_test recover-syntax-7.1 {
3116 + db eval {DROP TABLE IF EXISTS temp.syntax}
3117 + execsql {
3118 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3119 + backing,
3120 + f FLOAT NOT NULL
3121 + );
3122 + PRAGMA table_info(syntax);
3123 + }
3124 +} {0 f FLOAT 1 {} 0}
3125 +
3126 +# FLOAT STRICT
3127 +do_test recover-syntax-7.2 {
3128 + db eval {DROP TABLE IF EXISTS temp.syntax}
3129 + execsql {
3130 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3131 + backing,
3132 + f FLOAT STRICT
3133 + );
3134 + PRAGMA table_info(syntax);
3135 + }
3136 +} {0 f FLOAT 0 {} 0}
3137 +
3138 +# FLOAT STRICT NOT NULL
3139 +do_test recover-syntax-7.3 {
3140 + db eval {DROP TABLE IF EXISTS temp.syntax}
3141 + execsql {
3142 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3143 + backing,
3144 + f FLOAT STRICT NOT NULL
3145 + );
3146 + PRAGMA table_info(syntax);
3147 + }
3148 +} {0 f FLOAT 1 {} 0}
3149 +
3150 +# NUMERIC
3151 +do_test recover-syntax-8.0 {
3152 + db eval {DROP TABLE IF EXISTS temp.syntax}
3153 + execsql {
3154 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3155 + backing,
3156 + f NUMERIC
3157 + );
3158 + PRAGMA table_info(syntax);
3159 + }
3160 +} {0 f NUMERIC 0 {} 0}
3161 +
3162 +# NUMERIC NOT NULL
3163 +do_test recover-syntax-8.1 {
3164 + db eval {DROP TABLE IF EXISTS temp.syntax}
3165 + execsql {
3166 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3167 + backing,
3168 + f NUMERIC NOT NULL
3169 + );
3170 + PRAGMA table_info(syntax);
3171 + }
3172 +} {0 f NUMERIC 1 {} 0}
3173 +
3174 +# NUMERIC STRICT
3175 +do_test recover-syntax-8.2 {
3176 + db eval {DROP TABLE IF EXISTS temp.syntax}
3177 + execsql {
3178 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3179 + backing,
3180 + f NUMERIC STRICT
3181 + );
3182 + PRAGMA table_info(syntax);
3183 + }
3184 +} {0 f NUMERIC 0 {} 0}
3185 +
3186 +# NUMERIC STRICT NOT NULL
3187 +do_test recover-syntax-8.3 {
3188 + db eval {DROP TABLE IF EXISTS temp.syntax}
3189 + execsql {
3190 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3191 + backing,
3192 + f NUMERIC STRICT NOT NULL
3193 + );
3194 + PRAGMA table_info(syntax);
3195 + }
3196 +} {0 f NUMERIC 1 {} 0}
3197 +
3198 +# ROWID
3199 +do_test recover-syntax-9.0 {
3200 + db eval {DROP TABLE IF EXISTS temp.syntax}
3201 + execsql {
3202 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3203 + backing2,
3204 + id ROWID,
3205 + v
3206 + );
3207 + PRAGMA table_info(syntax);
3208 + }
3209 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
3210 +
3211 +# ROWID NOT NULL (is default)
3212 +do_test recover-syntax-9.1 {
3213 + db eval {DROP TABLE IF EXISTS temp.syntax}
3214 + execsql {
3215 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3216 + backing2,
3217 + id ROWID NOT NULL,
3218 + v
3219 + );
3220 + PRAGMA table_info(syntax);
3221 + }
3222 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
3223 +
3224 +# ROWID STRICT
3225 +do_test recover-syntax-9.0 {
3226 + db eval {DROP TABLE IF EXISTS temp.syntax}
3227 + execsql {
3228 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3229 + backing2,
3230 + id ROWID STRICT,
3231 + v
3232 + );
3233 + PRAGMA table_info(syntax);
3234 + }
3235 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
3236 +
3237 +# ROWID STRICT NOT NULL (is default)
3238 +do_test recover-syntax-9.1 {
3239 + db eval {DROP TABLE IF EXISTS temp.syntax}
3240 + execsql {
3241 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3242 + backing2,
3243 + id ROWID STRICT NOT NULL,
3244 + v
3245 + );
3246 + PRAGMA table_info(syntax);
3247 + }
3248 +} {0 id INTEGER 1 {} 0 1 v {} 0 {} 0}
3249 +
3250 +# Invalid type info is not ignored.
3251 +do_test recover-syntax-10.0 {
3252 + db eval {DROP TABLE IF EXISTS temp.syntax}
3253 + catchsql {
3254 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3255 + backing,
3256 + v GARBAGE
3257 + );
3258 + }
3259 +} {1 {unable to parse column 0}}
3260 +
3261 +# Extraneous type info is not ignored.
3262 +do_test recover-syntax-10.1 {
3263 + db eval {DROP TABLE IF EXISTS temp.syntax}
3264 + catchsql {
3265 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3266 + backing,
3267 + v INTEGER GARBAGE
3268 + );
3269 + }
3270 +} {1 {unable to parse column 0}}
3271 +
3272 +# Extraneous type info is not ignored.
3273 +do_test recover-syntax-10.2 {
3274 + db eval {DROP TABLE IF EXISTS temp.syntax}
3275 + catchsql {
3276 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3277 + backing,
3278 + v INTEGER NOT NULL GARBAGE
3279 + );
3280 + }
3281 +} {1 {unable to parse column 0}}
3282 +
3283 +# Multiple types don't work.
3284 +do_test recover-syntax-10.3 {
3285 + db eval {DROP TABLE IF EXISTS temp.syntax}
3286 + catchsql {
3287 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3288 + backing,
3289 + v INTEGER FLOAT BLOB
3290 + );
3291 + }
3292 +} {1 {unable to parse column 0}}
3293 +
3294 +# Multiple types don't work.
3295 +do_test recover-syntax-10.4 {
3296 + db eval {DROP TABLE IF EXISTS temp.syntax}
3297 + catchsql {
3298 + CREATE VIRTUAL TABLE temp.syntax USING recover(
3299 + backing,
3300 + v INTEGER NOT NULL TEXT
3301 + );
3302 + }
3303 +} {1 {unable to parse column 0}}
3304 +
3305 +finish_test
3306 diff --git a/third_party/sqlite/src/test/recover1.test b/third_party/sqlite/src/ test/recover1.test
3307 new file mode 100644
3308 index 0000000..1d90f09
3309 --- /dev/null
3310 +++ b/third_party/sqlite/src/test/recover1.test
3311 @@ -0,0 +1,429 @@
3312 +# 2012 January 4 {}
3313 +#
3314 +# The author disclaims copyright to this source code. In place of
3315 +# a legal notice, here is a blessing:
3316 +#
3317 +# May you do good and not evil.
3318 +# May you find forgiveness for yourself and forgive others.
3319 +# May you share freely, never taking more than you give.
3320 +#
3321 +#***********************************************************************
3322 +# This file implements regression tests for SQLite library.
3323 +#
3324 +# Use tables to test leaf-node reading, and also type checking.
3325 +#
3326 +# $Id$
3327 +
3328 +set testdir [file dirname $argv0]
3329 +source $testdir/tester.tcl
3330 +
3331 +# A really basic table with manifest typing and a row of each type.
3332 +db close
3333 +sqlite3 db test.db
3334 +db eval {
3335 + DROP TABLE IF EXISTS types;
3336 + CREATE TABLE types (rowtype TEXT, value);
3337 + INSERT INTO types VALUES ("NULL", NULL);
3338 + INSERT INTO types VALUES ("INTEGER", 17);
3339 + INSERT INTO types VALUES ("FLOAT", 3.1415927);
3340 + INSERT INTO types VALUES ("TEXT", "This is text");
3341 + INSERT INTO types VALUES ("BLOB", CAST("This is a blob" AS BLOB));
3342 +
3343 + -- Same contents, with an alias for rowid. Testing separately
3344 + -- because it changes the structure of the data (the alias column is
3345 + -- serialized as NULL).
3346 + DROP TABLE IF EXISTS types2;
3347 + CREATE TABLE types2 (id INTEGER PRIMARY KEY, rowtype TEXT, value);
3348 + INSERT INTO types2 (id, rowtype, value)
3349 + SELECT rowid, rowtype, value FROM types;
3350 +}
3351 +
3352 +# Baseline results.
3353 +do_test recover-types-0.0 {
3354 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types}
3355 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is t ext} text 5 BLOB {This is a blob} blob}
3356 +
3357 +# With no restrictions, recover table shows identical results.
3358 +do_test recover-types-0.1 {
3359 + db eval {
3360 + DROP TABLE IF EXISTS temp.types_recover;
3361 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3362 + types,
3363 + rowtype TEXT,
3364 + value
3365 + );
3366 + }
3367 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3368 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is t ext} text 5 BLOB {This is a blob} blob}
3369 +
3370 +# Restrict by INTEGER
3371 +do_test recover-types-1.0 {
3372 + db eval {
3373 + DROP TABLE IF EXISTS temp.types_recover;
3374 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3375 + types,
3376 + rowtype TEXT,
3377 + value INTEGER
3378 + );
3379 + }
3380 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3381 +} {1 NULL {} null 2 INTEGER 17 integer}
3382 +
3383 +# Restrict by INTEGER NOT NULL
3384 +do_test recover-types-1.1 {
3385 + db eval {
3386 + DROP TABLE IF EXISTS temp.types_recover;
3387 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3388 + types,
3389 + rowtype TEXT,
3390 + value INTEGER NOT NULL
3391 + );
3392 + }
3393 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3394 +} {2 INTEGER 17 integer}
3395 +
3396 +# Restrict by FLOAT
3397 +do_test recover-types-2.0 {
3398 + db eval {
3399 + DROP TABLE IF EXISTS temp.types_recover;
3400 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3401 + types,
3402 + rowtype TEXT,
3403 + value FLOAT
3404 + );
3405 + }
3406 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3407 +} {1 NULL {} null 2 INTEGER 17.0 real 3 FLOAT 3.1415927 real}
3408 +
3409 +# Restrict by FLOAT NOT NULL
3410 +do_test recover-types-2.1 {
3411 + db eval {
3412 + DROP TABLE IF EXISTS temp.types_recover;
3413 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3414 + types,
3415 + rowtype TEXT,
3416 + value FLOAT NOT NULL
3417 + );
3418 + }
3419 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3420 +} {2 INTEGER 17.0 real 3 FLOAT 3.1415927 real}
3421 +
3422 +# Restrict by FLOAT STRICT
3423 +do_test recover-types-2.2 {
3424 + db eval {
3425 + DROP TABLE IF EXISTS temp.types_recover;
3426 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3427 + types,
3428 + rowtype TEXT,
3429 + value FLOAT STRICT
3430 + );
3431 + }
3432 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3433 +} {1 NULL {} null 3 FLOAT 3.1415927 real}
3434 +
3435 +# Restrict by FLOAT STRICT NOT NULL
3436 +do_test recover-types-2.3 {
3437 + db eval {
3438 + DROP TABLE IF EXISTS temp.types_recover;
3439 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3440 + types,
3441 + rowtype TEXT,
3442 + value FLOAT STRICT NOT NULL
3443 + );
3444 + }
3445 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3446 +} {3 FLOAT 3.1415927 real}
3447 +
3448 +# Restrict by TEXT
3449 +do_test recover-types-3.0 {
3450 + db eval {
3451 + DROP TABLE IF EXISTS temp.types_recover;
3452 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3453 + types,
3454 + rowtype TEXT,
3455 + value TEXT
3456 + );
3457 + }
3458 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3459 +} {1 NULL {} null 4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3460 +
3461 +# Restrict by TEXT NOT NULL
3462 +do_test recover-types-3.1 {
3463 + db eval {
3464 + DROP TABLE IF EXISTS temp.types_recover;
3465 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3466 + types,
3467 + rowtype TEXT,
3468 + value TEXT NOT NULL
3469 + );
3470 + }
3471 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3472 +} {4 TEXT {This is text} text 5 BLOB {This is a blob} blob}
3473 +
3474 +# Restrict by TEXT STRICT
3475 +do_test recover-types-3.2 {
3476 + db eval {
3477 + DROP TABLE IF EXISTS temp.types_recover;
3478 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3479 + types,
3480 + rowtype TEXT,
3481 + value TEXT STRICT
3482 + );
3483 + }
3484 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3485 +} {1 NULL {} null 4 TEXT {This is text} text}
3486 +
3487 +# Restrict by TEXT STRICT NOT NULL
3488 +do_test recover-types-3.3 {
3489 + db eval {
3490 + DROP TABLE IF EXISTS temp.types_recover;
3491 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3492 + types,
3493 + rowtype TEXT,
3494 + value TEXT STRICT NOT NULL
3495 + );
3496 + }
3497 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3498 +} {4 TEXT {This is text} text}
3499 +
3500 +# Restrict by BLOB
3501 +do_test recover-types-4.0 {
3502 + db eval {
3503 + DROP TABLE IF EXISTS temp.types_recover;
3504 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3505 + types,
3506 + rowtype TEXT,
3507 + value BLOB
3508 + );
3509 + }
3510 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3511 +} {1 NULL {} null 5 BLOB {This is a blob} blob}
3512 +
3513 +# Restrict by BLOB NOT NULL
3514 +do_test recover-types-4.1 {
3515 + db eval {
3516 + DROP TABLE IF EXISTS temp.types_recover;
3517 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3518 + types,
3519 + rowtype TEXT,
3520 + value BLOB NOT NULL
3521 + );
3522 + }
3523 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3524 +} {5 BLOB {This is a blob} blob}
3525 +
3526 +# Manifest typing.
3527 +do_test recover-types-5.0 {
3528 + db eval {
3529 + DROP TABLE IF EXISTS temp.types_recover;
3530 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3531 + types,
3532 + rowtype TEXT,
3533 + value
3534 + );
3535 + }
3536 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3537 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is t ext} text 5 BLOB {This is a blob} blob}
3538 +
3539 +# Should get same results specifying manifest typing explicitly.
3540 +do_test recover-types-5.1 {
3541 + db eval {
3542 + DROP TABLE IF EXISTS temp.types_recover;
3543 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3544 + types,
3545 + rowtype TEXT,
3546 + value ANY
3547 + );
3548 + }
3549 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3550 +} {1 NULL {} null 2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is t ext} text 5 BLOB {This is a blob} blob}
3551 +
3552 +# Same results, skipping the NULL row.
3553 +do_test recover-types-5.2 {
3554 + db eval {
3555 + DROP TABLE IF EXISTS temp.types_recover;
3556 + CREATE VIRTUAL TABLE temp.types_recover USING recover(
3557 + types,
3558 + rowtype TEXT,
3559 + value ANY NOT NULL
3560 + );
3561 + }
3562 + execsql {SELECT rowid, rowtype, value, TYPEOF(value) FROM types_recover}
3563 +} {2 INTEGER 17 integer 3 FLOAT 3.1415927 real 4 TEXT {This is text} text 5 BLO B {This is a blob} blob}
3564 +
3565 +# Test ROWID values.
3566 +do_test recover-types-6.0 {
3567 + db eval {
3568 + DROP TABLE IF EXISTS temp.types2_recover;
3569 + CREATE VIRTUAL TABLE temp.types2_recover USING recover(
3570 + types2,
3571 + id ROWID,
3572 + rowtype TEXT,
3573 + value
3574 + );
3575 + }
3576 + execsql {SELECT rowid, id, rowtype, value, TYPEOF(value) FROM types2_recover}
3577 +} {1 1 NULL {} null 2 2 INTEGER 17 integer 3 3 FLOAT 3.1415927 real 4 4 TEXT {T his is text} text 5 5 BLOB {This is a blob} blob}
3578 +
3579 +# ROWID NOT NULL is identical.
3580 +do_test recover-types-6.1 {
3581 + db eval {
3582 + DROP TABLE IF EXISTS temp.types2_recover;
3583 + CREATE VIRTUAL TABLE temp.types2_recover USING recover(
3584 + types2,
3585 + id ROWID NOT NULL,
3586 + rowtype TEXT,
3587 + value
3588 + );
3589 + }
3590 + execsql {SELECT rowid, id, rowtype, value, TYPEOF(value) FROM types2_recover}
3591 +} {1 1 NULL {} null 2 2 INTEGER 17 integer 3 3 FLOAT 3.1415927 real 4 4 TEXT {T his is text} text 5 5 BLOB {This is a blob} blob}
3592 +
3593 +# Check that each of the possible integer sizes is being decoded.
3594 +# TODO(shess): It would be neat to ACTUALLY test these things. As-is,
3595 +# this should exercise the code paths, but one needs logging or a
3596 +# debugger to verify that things are stored as expected.
3597 +do_test recover-types-7.0 {
3598 + db eval {
3599 + DROP TABLE IF EXISTS integers;
3600 + CREATE TABLE integers (value);
3601 +
3602 + -- encoded directly in type info.
3603 + INSERT INTO integers VALUES (0);
3604 + INSERT INTO integers VALUES (1);
3605 +
3606 + -- 8-bit signed.
3607 + INSERT INTO integers VALUES (2);
3608 + INSERT INTO integers VALUES (-2);
3609 + INSERT INTO integers VALUES (127);
3610 + INSERT INTO integers VALUES (-128);
3611 +
3612 + -- 16-bit signed.
3613 + INSERT INTO integers VALUES (12345);
3614 + INSERT INTO integers VALUES (-12345);
3615 + INSERT INTO integers VALUES (32767);
3616 + INSERT INTO integers VALUES (-32768);
3617 +
3618 + -- 24-bit signed.
3619 + INSERT INTO integers VALUES (1234567);
3620 + INSERT INTO integers VALUES (-1234567);
3621 + INSERT INTO integers VALUES (8388607);
3622 + INSERT INTO integers VALUES (-8388608);
3623 +
3624 + -- 32-bit signed.
3625 + INSERT INTO integers VALUES (1234567890);
3626 + INSERT INTO integers VALUES (-1234567890);
3627 + INSERT INTO integers VALUES (2147483647);
3628 + INSERT INTO integers VALUES (-2147483648);
3629 +
3630 + -- 48-bit signed.
3631 + INSERT INTO integers VALUES (123456789012345);
3632 + INSERT INTO integers VALUES (-123456789012345);
3633 + INSERT INTO integers VALUES (140737488355327);
3634 + INSERT INTO integers VALUES (-140737488355328);
3635 +
3636 + -- 64-bit signed.
3637 + INSERT INTO integers VALUES (9223372036854775807);
3638 + INSERT INTO integers VALUES (-9223372036854775808);
3639 +
3640 + DROP TABLE IF EXISTS integers_recover;
3641 + CREATE VIRTUAL TABLE temp.integers_recover USING recover(
3642 + integers,
3643 + value INTEGER
3644 + );
3645 + }
3646 + execsql {SELECT rowid, value FROM integers_recover}
3647 +} {1 0 2 1 3 2 4 -2 5 127 6 -128 7 12345 8 -12345 9 32767 10 -32768 11 1234567 12 -1234567 13 8388607 14 -8388608 15 1234567890 16 -1234567890 17 2147483647 18 -2147483648 19 123456789012345 20 -123456789012345 21 140737488355327 22 -14073 7488355328 23 9223372036854775807 24 -9223372036854775808}
3648 +
3649 +# If UTF16 support is disabled, ignore the rest of the tests.
3650 +#
3651 +ifcapable {!utf16} {
3652 + finish_test
3653 + return
3654 +}
3655 +
3656 +# Baseline UTF-8.
3657 +file delete -force test.db
3658 +sqlite3 db test.db;
3659 +db eval {
3660 + PRAGMA encoding = 'UTF-8';
3661 +}
3662 +
3663 +do_test recover-encoding-1.0 {
3664 + execsql {
3665 + DROP TABLE IF EXISTS e;
3666 + CREATE TABLE e (v TEXT);
3667 + INSERT INTO e VALUES('Mjollnir');
3668 + INSERT INTO e VALUES('Mjölnir');
3669 + INSERT INTO e VALUES('Mjǫlnir');
3670 + INSERT INTO e VALUES('Mjölner');
3671 + INSERT INTO e VALUES('Mjølner');
3672 + INSERT INTO e VALUES('ハンマー');
3673 + PRAGMA encoding;
3674 +
3675 + DROP TABLE IF EXISTS e_recover;
3676 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3677 + e,
3678 + v TEXT
3679 + );
3680 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3681 + }
3682 +} {UTF-8 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3683 +
3684 +# Reset the database to UTF-16LE.
3685 +file delete -force test.db
3686 +sqlite3 db test.db;
3687 +db eval {
3688 + PRAGMA encoding = 'UTF-16LE';
3689 +}
3690 +
3691 +do_test recover-encoding-2.0 {
3692 + execsql {
3693 + DROP TABLE IF EXISTS e;
3694 + CREATE TABLE e (v TEXT);
3695 + INSERT INTO e VALUES('Mjollnir');
3696 + INSERT INTO e VALUES('Mjölnir');
3697 + INSERT INTO e VALUES('Mjǫlnir');
3698 + INSERT INTO e VALUES('Mjölner');
3699 + INSERT INTO e VALUES('Mjølner');
3700 + INSERT INTO e VALUES('ハンマー');
3701 + PRAGMA encoding;
3702 +
3703 + DROP TABLE IF EXISTS e_recover;
3704 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3705 + e,
3706 + v TEXT
3707 + );
3708 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3709 + }
3710 +} {UTF-16le 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3711 +
3712 +# Reset the database to UTF-16BE.
3713 +file delete -force test.db
3714 +sqlite3 db test.db;
3715 +db eval {
3716 + PRAGMA encoding = 'UTF-16BE';
3717 +}
3718 +
3719 +do_test recover-encoding-3.0 {
3720 + execsql {
3721 + DROP TABLE IF EXISTS e;
3722 + CREATE TABLE e (v TEXT);
3723 + INSERT INTO e VALUES('Mjollnir');
3724 + INSERT INTO e VALUES('Mjölnir');
3725 + INSERT INTO e VALUES('Mjǫlnir');
3726 + INSERT INTO e VALUES('Mjölner');
3727 + INSERT INTO e VALUES('Mjølner');
3728 + INSERT INTO e VALUES('ハンマー');
3729 + PRAGMA encoding;
3730 +
3731 + DROP TABLE IF EXISTS e_recover;
3732 + CREATE VIRTUAL TABLE temp.e_recover USING recover(
3733 + e,
3734 + v TEXT
3735 + );
3736 + SELECT rowid, v FROM e_recover ORDER BY rowid;
3737 + }
3738 +} {UTF-16be 1 Mjollnir 2 Mjölnir 3 Mjǫlnir 4 Mjölner 5 Mjølner 6 ハンマー}
3739 +
3740 +finish_test
3741 diff --git a/third_party/sqlite/src/test/recover2.test b/third_party/sqlite/src/ test/recover2.test
3742 new file mode 100644
3743 index 0000000..8aa4e04
3744 --- /dev/null
3745 +++ b/third_party/sqlite/src/test/recover2.test
3746 @@ -0,0 +1,157 @@
3747 +# 2012 January 4 {}
3748 +#
3749 +# The author disclaims copyright to this source code. In place of
3750 +# a legal notice, here is a blessing:
3751 +#
3752 +# May you do good and not evil.
3753 +# May you find forgiveness for yourself and forgive others.
3754 +# May you share freely, never taking more than you give.
3755 +#
3756 +#***********************************************************************
3757 +# This file implements regression tests for SQLite library.
3758 +#
3759 +# This file implements tests for how the recover module handles cell
3760 +# overflow.
3761 +#
3762 +# $Id$
3763 +
3764 +set testdir [file dirname $argv0]
3765 +source $testdir/tester.tcl
3766 +
3767 +set lorem "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sagitti s gravida odio vitae ultrices. Nulla facilisi. Maecenas pulvinar, tellus ut bibe ndum semper, nibh tellus auctor nulla, in dignissim nisi ipsum id arcu. Nullam t incidunt arcu malesuada turpis faucibus in adipiscing enim mattis. Fusce augue m agna, scelerisque sollicitudin egestas in, cursus eu sapien. Pellentesque tempor risus at lectus convallis a convallis orci ornare. Integer tristique aliquam le o vel interdum.
3768 +
3769 +Phasellus quis dictum nisi. Curabitur at enim non felis pharetra imperdiet. Dui s tempus massa eu leo varius porta. Vestibulum sodales nulla at purus tincidunt ultrices. Nam euismod posuere nibh, nec sodales magna luctus ac. Ut commodo hend rerit mauris vitae gravida. In interdum justo ut sem eleifend convallis. Donec c ursus molestie elementum. Suspendisse at nisl tellus, vel consequat mauris. Null am non justo nibh.
3770 +
3771 +Maecenas varius sollicitudin libero, nec feugiat turpis facilisis eget. Quisque et sem risus. Aenean a magna quis purus hendrerit mattis eu vel lorem. Aenean f ringilla diam eget tortor lacinia sed mollis eros feugiat. Quisque ac purus sapi en. Nullam quis tellus vel magna convallis tincidunt. Donec eget ligula at liber o tincidunt congue ut ut lacus. Integer dignissim aliquet congue. Pellentesque s ed risus vitae lorem porta viverra ac eu risus. Vivamus congue suscipit odio pul vinar aliquet. Aliquam porttitor nunc non sapien auctor et vehicula augue molest ie.
3772 +
3773 +Aliquam et dui ac sem tempus dictum. Fusce arcu nulla, viverra sit amet suscipi t quis, malesuada at felis. Fusce ut diam felis. Fusce id ligula non eros fermen tum sodales in nec quam. Donec tempor bibendum arcu ac adipiscing. Praesent nisl lectus, tempor ut vehicula eget, mattis a justo. Mauris condimentum luctus eros a varius. Morbi mollis elit eget velit convallis eu sodales odio egestas. Cum s ociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus interdum, metus sit amet varius varius, lectus eros semper risus, sed s agittis ipsum libero in sapien. Nam lacinia nulla vitae magna facilisis sceleris que. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3774 +
3775 +Donec gravida dignissim eleifend. Aliquam vel tincidunt tortor. Curabitur massa ante, blandit a auctor at, ullamcorper sed nisl. Class aptent taciti sociosqu a d litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse ut fel is a eros egestas ultricies et quis mi. Vivamus ut risus massa. Donec nec ornare erat. Aliquam ornare, lorem a rhoncus aliquam, tellus diam tincidunt tellus, a mattis nunc ante ac arcu. Curabitur nec metus id risus commodo ullamcorper eu ut tortor."
3776 +
3777 +# Create a database which needs a multiple overflow pages to test the
3778 +# transition from main page to overflow, and then overflow to
3779 +# overflow.
3780 +do_test recover-overflow-1.0 {
3781 + db eval {
3782 + DROP TABLE IF EXISTS overflow;
3783 + CREATE TABLE overflow (value TEXT);
3784 + INSERT INTO overflow VALUES ($lorem);
3785 +
3786 + DROP TABLE IF EXISTS overflow_recover;
3787 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3788 + overflow,
3789 + value TEXT
3790 + );
3791 + }
3792 +
3793 + # Should have root page, leaf page, and 2 overflow pages, because
3794 + # length(value) is more than 2x page_size.
3795 + execsql {
3796 + PRAGMA page_count;
3797 + PRAGMA page_size;
3798 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3799 + }
3800 +} [list 4 1024 1 text [string length $lorem] $lorem]
3801 +
3802 +# No overflow. [1024-35 == 990, overhead of 1-byte rowid, 2-byte
3803 +# record length, 1-byte header length, 2-byte field type.]
3804 +set substr [string range $lorem 0 985]
3805 +do_test recover-overflow-1.1 {
3806 + db eval {
3807 + DROP TABLE IF EXISTS overflow;
3808 + CREATE TABLE overflow (value TEXT);
3809 + INSERT INTO overflow VALUES ($substr);
3810 +
3811 + DROP TABLE IF EXISTS overflow_recover;
3812 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3813 + overflow,
3814 + value TEXT
3815 + );
3816 + }
3817 +
3818 + # Trim to remove excess pages from prior tests.
3819 + db eval {VACUUM}
3820 +
3821 + execsql {
3822 + PRAGMA page_count;
3823 + PRAGMA page_size;
3824 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3825 + }
3826 +} [list 2 1024 1 text [string length $substr] $substr]
3827 +
3828 +# One byte of overflow.
3829 +set substr [string range $lorem 0 986]
3830 +do_test recover-overflow-1.2 {
3831 + db eval {
3832 + DROP TABLE IF EXISTS overflow;
3833 + CREATE TABLE overflow (value TEXT);
3834 + INSERT INTO overflow VALUES ($substr);
3835 +
3836 + DROP TABLE IF EXISTS overflow_recover;
3837 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3838 + overflow,
3839 + value TEXT
3840 + );
3841 + }
3842 +
3843 + # Trim to remove excess pages from prior tests.
3844 + db eval {VACUUM}
3845 +
3846 + execsql {
3847 + PRAGMA page_count;
3848 + PRAGMA page_size;
3849 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3850 + }
3851 +} [list 3 1024 1 text [string length $substr] $substr]
3852 +
3853 +# One full overflow page, plus maxLocal in-leaf. [985+1020]
3854 +set substr [string range $lorem 0 2005]
3855 +do_test recover-overflow-1.3 {
3856 + db eval {
3857 + DROP TABLE IF EXISTS overflow;
3858 + CREATE TABLE overflow (value TEXT);
3859 + INSERT INTO overflow VALUES ($substr);
3860 +
3861 + DROP TABLE IF EXISTS overflow_recover;
3862 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3863 + overflow,
3864 + value TEXT
3865 + );
3866 + }
3867 +
3868 + # Trim to remove excess pages from prior tests.
3869 + db eval {VACUUM}
3870 +
3871 + execsql {
3872 + PRAGMA page_count;
3873 + PRAGMA page_size;
3874 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3875 + }
3876 +} [list 3 1024 1 text [string length $substr] $substr]
3877 +
3878 +# Overflow to a second overflow page.
3879 +set substr [string range $lorem 0 2006]
3880 +do_test recover-overflow-1.4 {
3881 + db eval {
3882 + DROP TABLE IF EXISTS overflow;
3883 + CREATE TABLE overflow (value TEXT);
3884 + INSERT INTO overflow VALUES ($substr);
3885 +
3886 + DROP TABLE IF EXISTS overflow_recover;
3887 + CREATE VIRTUAL TABLE temp.overflow_recover USING recover(
3888 + overflow,
3889 + value TEXT
3890 + );
3891 + }
3892 +
3893 + # Trim to remove excess pages from prior tests.
3894 + db eval {VACUUM}
3895 +
3896 + execsql {
3897 + PRAGMA page_count;
3898 + PRAGMA page_size;
3899 + SELECT rowid, TYPEOF(value), length(value), value FROM overflow_recover;
3900 + }
3901 +} [list 4 1024 1 text [string length $substr] $substr]
3902 +
3903 +finish_test
3904 --
3905 2.5.0
3906
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698