xref: /sqlite-3.40.0/ext/rbu/sqlite3rbu.c (revision 38d69855)
1 /*
2 ** 2014 August 30
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 **
14 ** OVERVIEW
15 **
16 **  The RBU extension requires that the RBU update be packaged as an
17 **  SQLite database. The tables it expects to find are described in
18 **  sqlite3rbu.h.  Essentially, for each table xyz in the target database
19 **  that the user wishes to write to, a corresponding data_xyz table is
20 **  created in the RBU database and populated with one row for each row to
21 **  update, insert or delete from the target table.
22 **
23 **  The update proceeds in three stages:
24 **
25 **  1) The database is updated. The modified database pages are written
26 **     to a *-oal file. A *-oal file is just like a *-wal file, except
27 **     that it is named "<database>-oal" instead of "<database>-wal".
28 **     Because regular SQLite clients do not look for file named
29 **     "<database>-oal", they go on using the original database in
30 **     rollback mode while the *-oal file is being generated.
31 **
32 **     During this stage RBU does not update the database by writing
33 **     directly to the target tables. Instead it creates "imposter"
34 **     tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
35 **     to update each b-tree individually. All updates required by each
36 **     b-tree are completed before moving on to the next, and all
37 **     updates are done in sorted key order.
38 **
39 **  2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
40 **     location using a call to rename(2). Before doing this the RBU
41 **     module takes an EXCLUSIVE lock on the database file, ensuring
42 **     that there are no other active readers.
43 **
44 **     Once the EXCLUSIVE lock is released, any other database readers
45 **     detect the new *-wal file and read the database in wal mode. At
46 **     this point they see the new version of the database - including
47 **     the updates made as part of the RBU update.
48 **
49 **  3) The new *-wal file is checkpointed. This proceeds in the same way
50 **     as a regular database checkpoint, except that a single frame is
51 **     checkpointed each time sqlite3rbu_step() is called. If the RBU
52 **     handle is closed before the entire *-wal file is checkpointed,
53 **     the checkpoint progress is saved in the RBU database and the
54 **     checkpoint can be resumed by another RBU client at some point in
55 **     the future.
56 **
57 ** POTENTIAL PROBLEMS
58 **
59 **  The rename() call might not be portable. And RBU is not currently
60 **  syncing the directory after renaming the file.
61 **
62 **  When state is saved, any commit to the *-oal file and the commit to
63 **  the RBU update database are not atomic. So if the power fails at the
64 **  wrong moment they might get out of sync. As the main database will be
65 **  committed before the RBU update database this will likely either just
66 **  pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
67 **  constraint violations).
68 **
69 **  If some client does modify the target database mid RBU update, or some
70 **  other error occurs, the RBU extension will keep throwing errors. It's
71 **  not really clear how to get out of this state. The system could just
72 **  by delete the RBU update database and *-oal file and have the device
73 **  download the update again and start over.
74 **
75 **  At present, for an UPDATE, both the new.* and old.* records are
76 **  collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
77 **  fields are collected.  This means we're probably writing a lot more
78 **  data to disk when saving the state of an ongoing update to the RBU
79 **  update database than is strictly necessary.
80 **
81 */
82 
83 #include <assert.h>
84 #include <string.h>
85 #include <stdio.h>
86 
87 #include "sqlite3.h"
88 
89 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
90 #include "sqlite3rbu.h"
91 
92 #if defined(_WIN32_WCE)
93 #include "windows.h"
94 #endif
95 
96 /* Maximum number of prepared UPDATE statements held by this module */
97 #define SQLITE_RBU_UPDATE_CACHESIZE 16
98 
99 /*
100 ** Swap two objects of type TYPE.
101 */
102 #if !defined(SQLITE_AMALGAMATION)
103 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
104 #endif
105 
106 /*
107 ** The rbu_state table is used to save the state of a partially applied
108 ** update so that it can be resumed later. The table consists of integer
109 ** keys mapped to values as follows:
110 **
111 ** RBU_STATE_STAGE:
112 **   May be set to integer values 1, 2, 4 or 5. As follows:
113 **       1: the *-rbu file is currently under construction.
114 **       2: the *-rbu file has been constructed, but not yet moved
115 **          to the *-wal path.
116 **       4: the checkpoint is underway.
117 **       5: the rbu update has been checkpointed.
118 **
119 ** RBU_STATE_TBL:
120 **   Only valid if STAGE==1. The target database name of the table
121 **   currently being written.
122 **
123 ** RBU_STATE_IDX:
124 **   Only valid if STAGE==1. The target database name of the index
125 **   currently being written, or NULL if the main table is currently being
126 **   updated.
127 **
128 ** RBU_STATE_ROW:
129 **   Only valid if STAGE==1. Number of rows already processed for the current
130 **   table/index.
131 **
132 ** RBU_STATE_PROGRESS:
133 **   Trbul number of sqlite3rbu_step() calls made so far as part of this
134 **   rbu update.
135 **
136 ** RBU_STATE_CKPT:
137 **   Valid if STAGE==4. The 64-bit checksum associated with the wal-index
138 **   header created by recovering the *-wal file. This is used to detect
139 **   cases when another client appends frames to the *-wal file in the
140 **   middle of an incremental checkpoint (an incremental checkpoint cannot
141 **   be continued if this happens).
142 **
143 ** RBU_STATE_COOKIE:
144 **   Valid if STAGE==1. The current change-counter cookie value in the
145 **   target db file.
146 **
147 ** RBU_STATE_OALSZ:
148 **   Valid if STAGE==1. The size in bytes of the *-oal file.
149 */
150 #define RBU_STATE_STAGE       1
151 #define RBU_STATE_TBL         2
152 #define RBU_STATE_IDX         3
153 #define RBU_STATE_ROW         4
154 #define RBU_STATE_PROGRESS    5
155 #define RBU_STATE_CKPT        6
156 #define RBU_STATE_COOKIE      7
157 #define RBU_STATE_OALSZ       8
158 
159 #define RBU_STAGE_OAL         1
160 #define RBU_STAGE_MOVE        2
161 #define RBU_STAGE_CAPTURE     3
162 #define RBU_STAGE_CKPT        4
163 #define RBU_STAGE_DONE        5
164 
165 
166 #define RBU_CREATE_STATE \
167   "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
168 
169 typedef struct RbuFrame RbuFrame;
170 typedef struct RbuObjIter RbuObjIter;
171 typedef struct RbuState RbuState;
172 typedef struct rbu_vfs rbu_vfs;
173 typedef struct rbu_file rbu_file;
174 typedef struct RbuUpdateStmt RbuUpdateStmt;
175 
176 #if !defined(SQLITE_AMALGAMATION)
177 typedef unsigned int u32;
178 typedef unsigned char u8;
179 typedef sqlite3_int64 i64;
180 #endif
181 
182 /*
183 ** These values must match the values defined in wal.c for the equivalent
184 ** locks. These are not magic numbers as they are part of the SQLite file
185 ** format.
186 */
187 #define WAL_LOCK_WRITE  0
188 #define WAL_LOCK_CKPT   1
189 #define WAL_LOCK_READ0  3
190 
191 /*
192 ** A structure to store values read from the rbu_state table in memory.
193 */
194 struct RbuState {
195   int eStage;
196   char *zTbl;
197   char *zIdx;
198   i64 iWalCksum;
199   int nRow;
200   i64 nProgress;
201   u32 iCookie;
202   i64 iOalSz;
203 };
204 
205 struct RbuUpdateStmt {
206   char *zMask;                    /* Copy of update mask used with pUpdate */
207   sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
208   RbuUpdateStmt *pNext;
209 };
210 
211 /*
212 ** An iterator of this type is used to iterate through all objects in
213 ** the target database that require updating. For each such table, the
214 ** iterator visits, in order:
215 **
216 **     * the table itself,
217 **     * each index of the table (zero or more points to visit), and
218 **     * a special "cleanup table" state.
219 **
220 ** abIndexed:
221 **   If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
222 **   it points to an array of flags nTblCol elements in size. The flag is
223 **   set for each column that is either a part of the PK or a part of an
224 **   index. Or clear otherwise.
225 **
226 */
227 struct RbuObjIter {
228   sqlite3_stmt *pTblIter;         /* Iterate through tables */
229   sqlite3_stmt *pIdxIter;         /* Index iterator */
230   int nTblCol;                    /* Size of azTblCol[] array */
231   char **azTblCol;                /* Array of unquoted target column names */
232   char **azTblType;               /* Array of target column types */
233   int *aiSrcOrder;                /* src table col -> target table col */
234   u8 *abTblPk;                    /* Array of flags, set on target PK columns */
235   u8 *abNotNull;                  /* Array of flags, set on NOT NULL columns */
236   u8 *abIndexed;                  /* Array of flags, set on indexed & PK cols */
237   int eType;                      /* Table type - an RBU_PK_XXX value */
238 
239   /* Output variables. zTbl==0 implies EOF. */
240   int bCleanup;                   /* True in "cleanup" state */
241   const char *zTbl;               /* Name of target db table */
242   const char *zDataTbl;           /* Name of rbu db table (or null) */
243   const char *zIdx;               /* Name of target db index (or null) */
244   int iTnum;                      /* Root page of current object */
245   int iPkTnum;                    /* If eType==EXTERNAL, root of PK index */
246   int bUnique;                    /* Current index is unique */
247 
248   /* Statements created by rbuObjIterPrepareAll() */
249   int nCol;                       /* Number of columns in current object */
250   sqlite3_stmt *pSelect;          /* Source data */
251   sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
252   sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
253   sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zDataTbl */
254 
255   /* Last UPDATE used (for PK b-tree updates only), or NULL. */
256   RbuUpdateStmt *pRbuUpdate;
257 };
258 
259 /*
260 ** Values for RbuObjIter.eType
261 **
262 **     0: Table does not exist (error)
263 **     1: Table has an implicit rowid.
264 **     2: Table has an explicit IPK column.
265 **     3: Table has an external PK index.
266 **     4: Table is WITHOUT ROWID.
267 **     5: Table is a virtual table.
268 */
269 #define RBU_PK_NOTABLE        0
270 #define RBU_PK_NONE           1
271 #define RBU_PK_IPK            2
272 #define RBU_PK_EXTERNAL       3
273 #define RBU_PK_WITHOUT_ROWID  4
274 #define RBU_PK_VTAB           5
275 
276 
277 /*
278 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
279 ** one of the following operations.
280 */
281 #define RBU_INSERT     1          /* Insert on a main table b-tree */
282 #define RBU_DELETE     2          /* Delete a row from a main table b-tree */
283 #define RBU_IDX_DELETE 3          /* Delete a row from an aux. index b-tree */
284 #define RBU_IDX_INSERT 4          /* Insert on an aux. index b-tree */
285 #define RBU_UPDATE     5          /* Update a row in a main table b-tree */
286 
287 
288 /*
289 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
290 ** file should be copied to page iDbPage of the database file.
291 */
292 struct RbuFrame {
293   u32 iDbPage;
294   u32 iWalFrame;
295 };
296 
297 /*
298 ** RBU handle.
299 */
300 struct sqlite3rbu {
301   int eStage;                     /* Value of RBU_STATE_STAGE field */
302   sqlite3 *dbMain;                /* target database handle */
303   sqlite3 *dbRbu;                 /* rbu database handle */
304   char *zTarget;                  /* Path to target db */
305   char *zRbu;                     /* Path to rbu db */
306   char *zState;                   /* Path to state db (or NULL if zRbu) */
307   char zStateDb[5];               /* Db name for state ("stat" or "main") */
308   int rc;                         /* Value returned by last rbu_step() call */
309   char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
310   int nStep;                      /* Rows processed for current object */
311   int nProgress;                  /* Rows processed for all objects */
312   RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
313   const char *zVfsName;           /* Name of automatically created rbu vfs */
314   rbu_file *pTargetFd;            /* File handle open on target db */
315   i64 iOalSz;
316 
317   /* The following state variables are used as part of the incremental
318   ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
319   ** function rbuSetupCheckpoint() for details.  */
320   u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
321   u32 mLock;
322   int nFrame;                     /* Entries in aFrame[] array */
323   int nFrameAlloc;                /* Allocated size of aFrame[] array */
324   RbuFrame *aFrame;
325   int pgsz;
326   u8 *aBuf;
327   i64 iWalCksum;
328 };
329 
330 /*
331 ** An rbu VFS is implemented using an instance of this structure.
332 */
333 struct rbu_vfs {
334   sqlite3_vfs base;               /* rbu VFS shim methods */
335   sqlite3_vfs *pRealVfs;          /* Underlying VFS */
336   sqlite3_mutex *mutex;           /* Mutex to protect pMain */
337   rbu_file *pMain;                /* Linked list of main db files */
338 };
339 
340 /*
341 ** Each file opened by an rbu VFS is represented by an instance of
342 ** the following structure.
343 */
344 struct rbu_file {
345   sqlite3_file base;              /* sqlite3_file methods */
346   sqlite3_file *pReal;            /* Underlying file handle */
347   rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
348   sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
349 
350   int openFlags;                  /* Flags this file was opened with */
351   u32 iCookie;                    /* Cookie value for main db files */
352   u8 iWriteVer;                   /* "write-version" value for main db files */
353 
354   int nShm;                       /* Number of entries in apShm[] array */
355   char **apShm;                   /* Array of mmap'd *-shm regions */
356   char *zDel;                     /* Delete this when closing file */
357 
358   const char *zWal;               /* Wal filename for this main db file */
359   rbu_file *pWalFd;               /* Wal file descriptor for this main db */
360   rbu_file *pMainNext;            /* Next MAIN_DB file */
361 };
362 
363 
364 /*************************************************************************
365 ** The following three functions, found below:
366 **
367 **   rbuDeltaGetInt()
368 **   rbuDeltaChecksum()
369 **   rbuDeltaApply()
370 **
371 ** are lifted from the fossil source code (http://fossil-scm.org). They
372 ** are used to implement the scalar SQL function rbu_fossil_delta().
373 */
374 
375 /*
376 ** Read bytes from *pz and convert them into a positive integer.  When
377 ** finished, leave *pz pointing to the first character past the end of
378 ** the integer.  The *pLen parameter holds the length of the string
379 ** in *pz and is decremented once for each character in the integer.
380 */
381 static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
382   static const signed char zValue[] = {
383     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
384     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
385     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
386      0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
387     -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
388     25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
389     -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
390     52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1,
391   };
392   unsigned int v = 0;
393   int c;
394   unsigned char *z = (unsigned char*)*pz;
395   unsigned char *zStart = z;
396   while( (c = zValue[0x7f&*(z++)])>=0 ){
397      v = (v<<6) + c;
398   }
399   z--;
400   *pLen -= z - zStart;
401   *pz = (char*)z;
402   return v;
403 }
404 
405 /*
406 ** Compute a 32-bit checksum on the N-byte buffer.  Return the result.
407 */
408 static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
409   const unsigned char *z = (const unsigned char *)zIn;
410   unsigned sum0 = 0;
411   unsigned sum1 = 0;
412   unsigned sum2 = 0;
413   unsigned sum3 = 0;
414   while(N >= 16){
415     sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
416     sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
417     sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
418     sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
419     z += 16;
420     N -= 16;
421   }
422   while(N >= 4){
423     sum0 += z[0];
424     sum1 += z[1];
425     sum2 += z[2];
426     sum3 += z[3];
427     z += 4;
428     N -= 4;
429   }
430   sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
431   switch(N){
432     case 3:   sum3 += (z[2] << 8);
433     case 2:   sum3 += (z[1] << 16);
434     case 1:   sum3 += (z[0] << 24);
435     default:  ;
436   }
437   return sum3;
438 }
439 
440 /*
441 ** Apply a delta.
442 **
443 ** The output buffer should be big enough to hold the whole output
444 ** file and a NUL terminator at the end.  The delta_output_size()
445 ** routine will determine this size for you.
446 **
447 ** The delta string should be null-terminated.  But the delta string
448 ** may contain embedded NUL characters (if the input and output are
449 ** binary files) so we also have to pass in the length of the delta in
450 ** the lenDelta parameter.
451 **
452 ** This function returns the size of the output file in bytes (excluding
453 ** the final NUL terminator character).  Except, if the delta string is
454 ** malformed or intended for use with a source file other than zSrc,
455 ** then this routine returns -1.
456 **
457 ** Refer to the delta_create() documentation above for a description
458 ** of the delta file format.
459 */
460 static int rbuDeltaApply(
461   const char *zSrc,      /* The source or pattern file */
462   int lenSrc,            /* Length of the source file */
463   const char *zDelta,    /* Delta to apply to the pattern */
464   int lenDelta,          /* Length of the delta */
465   char *zOut             /* Write the output into this preallocated buffer */
466 ){
467   unsigned int limit;
468   unsigned int total = 0;
469 #ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
470   char *zOrigOut = zOut;
471 #endif
472 
473   limit = rbuDeltaGetInt(&zDelta, &lenDelta);
474   if( *zDelta!='\n' ){
475     /* ERROR: size integer not terminated by "\n" */
476     return -1;
477   }
478   zDelta++; lenDelta--;
479   while( *zDelta && lenDelta>0 ){
480     unsigned int cnt, ofst;
481     cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
482     switch( zDelta[0] ){
483       case '@': {
484         zDelta++; lenDelta--;
485         ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
486         if( lenDelta>0 && zDelta[0]!=',' ){
487           /* ERROR: copy command not terminated by ',' */
488           return -1;
489         }
490         zDelta++; lenDelta--;
491         total += cnt;
492         if( total>limit ){
493           /* ERROR: copy exceeds output file size */
494           return -1;
495         }
496         if( (int)(ofst+cnt) > lenSrc ){
497           /* ERROR: copy extends past end of input */
498           return -1;
499         }
500         memcpy(zOut, &zSrc[ofst], cnt);
501         zOut += cnt;
502         break;
503       }
504       case ':': {
505         zDelta++; lenDelta--;
506         total += cnt;
507         if( total>limit ){
508           /* ERROR:  insert command gives an output larger than predicted */
509           return -1;
510         }
511         if( (int)cnt>lenDelta ){
512           /* ERROR: insert count exceeds size of delta */
513           return -1;
514         }
515         memcpy(zOut, zDelta, cnt);
516         zOut += cnt;
517         zDelta += cnt;
518         lenDelta -= cnt;
519         break;
520       }
521       case ';': {
522         zDelta++; lenDelta--;
523         zOut[0] = 0;
524 #ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
525         if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
526           /* ERROR:  bad checksum */
527           return -1;
528         }
529 #endif
530         if( total!=limit ){
531           /* ERROR: generated size does not match predicted size */
532           return -1;
533         }
534         return total;
535       }
536       default: {
537         /* ERROR: unknown delta operator */
538         return -1;
539       }
540     }
541   }
542   /* ERROR: unterminated delta */
543   return -1;
544 }
545 
546 static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
547   int size;
548   size = rbuDeltaGetInt(&zDelta, &lenDelta);
549   if( *zDelta!='\n' ){
550     /* ERROR: size integer not terminated by "\n" */
551     return -1;
552   }
553   return size;
554 }
555 
556 /*
557 ** End of code taken from fossil.
558 *************************************************************************/
559 
560 /*
561 ** Implementation of SQL scalar function rbu_fossil_delta().
562 **
563 ** This function applies a fossil delta patch to a blob. Exactly two
564 ** arguments must be passed to this function. The first is the blob to
565 ** patch and the second the patch to apply. If no error occurs, this
566 ** function returns the patched blob.
567 */
568 static void rbuFossilDeltaFunc(
569   sqlite3_context *context,
570   int argc,
571   sqlite3_value **argv
572 ){
573   const char *aDelta;
574   int nDelta;
575   const char *aOrig;
576   int nOrig;
577 
578   int nOut;
579   int nOut2;
580   char *aOut;
581 
582   assert( argc==2 );
583 
584   nOrig = sqlite3_value_bytes(argv[0]);
585   aOrig = (const char*)sqlite3_value_blob(argv[0]);
586   nDelta = sqlite3_value_bytes(argv[1]);
587   aDelta = (const char*)sqlite3_value_blob(argv[1]);
588 
589   /* Figure out the size of the output */
590   nOut = rbuDeltaOutputSize(aDelta, nDelta);
591   if( nOut<0 ){
592     sqlite3_result_error(context, "corrupt fossil delta", -1);
593     return;
594   }
595 
596   aOut = sqlite3_malloc(nOut+1);
597   if( aOut==0 ){
598     sqlite3_result_error_nomem(context);
599   }else{
600     nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
601     if( nOut2!=nOut ){
602       sqlite3_result_error(context, "corrupt fossil delta", -1);
603     }else{
604       sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
605     }
606   }
607 }
608 
609 
610 /*
611 ** Prepare the SQL statement in buffer zSql against database handle db.
612 ** If successful, set *ppStmt to point to the new statement and return
613 ** SQLITE_OK.
614 **
615 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
616 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
617 ** point to a buffer containing an error message. It is the responsibility
618 ** of the caller to (eventually) free this buffer using sqlite3_free().
619 */
620 static int prepareAndCollectError(
621   sqlite3 *db,
622   sqlite3_stmt **ppStmt,
623   char **pzErrmsg,
624   const char *zSql
625 ){
626   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
627   if( rc!=SQLITE_OK ){
628     *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
629     *ppStmt = 0;
630   }
631   return rc;
632 }
633 
634 /*
635 ** Reset the SQL statement passed as the first argument. Return a copy
636 ** of the value returned by sqlite3_reset().
637 **
638 ** If an error has occurred, then set *pzErrmsg to point to a buffer
639 ** containing an error message. It is the responsibility of the caller
640 ** to eventually free this buffer using sqlite3_free().
641 */
642 static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
643   int rc = sqlite3_reset(pStmt);
644   if( rc!=SQLITE_OK ){
645     *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
646   }
647   return rc;
648 }
649 
650 /*
651 ** Unless it is NULL, argument zSql points to a buffer allocated using
652 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
653 ** statement against database db and frees the buffer. If statement
654 ** compilation is successful, *ppStmt is set to point to the new statement
655 ** handle and SQLITE_OK is returned.
656 **
657 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
658 ** returned. In this case, *pzErrmsg may also be set to point to an error
659 ** message. It is the responsibility of the caller to free this error message
660 ** buffer using sqlite3_free().
661 **
662 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
663 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
664 */
665 static int prepareFreeAndCollectError(
666   sqlite3 *db,
667   sqlite3_stmt **ppStmt,
668   char **pzErrmsg,
669   char *zSql
670 ){
671   int rc;
672   assert( *pzErrmsg==0 );
673   if( zSql==0 ){
674     rc = SQLITE_NOMEM;
675     *ppStmt = 0;
676   }else{
677     rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
678     sqlite3_free(zSql);
679   }
680   return rc;
681 }
682 
683 /*
684 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
685 ** by an earlier call to rbuObjIterCacheTableInfo().
686 */
687 static void rbuObjIterFreeCols(RbuObjIter *pIter){
688   int i;
689   for(i=0; i<pIter->nTblCol; i++){
690     sqlite3_free(pIter->azTblCol[i]);
691     sqlite3_free(pIter->azTblType[i]);
692   }
693   sqlite3_free(pIter->azTblCol);
694   pIter->azTblCol = 0;
695   pIter->azTblType = 0;
696   pIter->aiSrcOrder = 0;
697   pIter->abTblPk = 0;
698   pIter->abNotNull = 0;
699   pIter->nTblCol = 0;
700   pIter->eType = 0;               /* Invalid value */
701 }
702 
703 /*
704 ** Finalize all statements and free all allocations that are specific to
705 ** the current object (table/index pair).
706 */
707 static void rbuObjIterClearStatements(RbuObjIter *pIter){
708   RbuUpdateStmt *pUp;
709 
710   sqlite3_finalize(pIter->pSelect);
711   sqlite3_finalize(pIter->pInsert);
712   sqlite3_finalize(pIter->pDelete);
713   sqlite3_finalize(pIter->pTmpInsert);
714   pUp = pIter->pRbuUpdate;
715   while( pUp ){
716     RbuUpdateStmt *pTmp = pUp->pNext;
717     sqlite3_finalize(pUp->pUpdate);
718     sqlite3_free(pUp);
719     pUp = pTmp;
720   }
721 
722   pIter->pSelect = 0;
723   pIter->pInsert = 0;
724   pIter->pDelete = 0;
725   pIter->pRbuUpdate = 0;
726   pIter->pTmpInsert = 0;
727   pIter->nCol = 0;
728 }
729 
730 /*
731 ** Clean up any resources allocated as part of the iterator object passed
732 ** as the only argument.
733 */
734 static void rbuObjIterFinalize(RbuObjIter *pIter){
735   rbuObjIterClearStatements(pIter);
736   sqlite3_finalize(pIter->pTblIter);
737   sqlite3_finalize(pIter->pIdxIter);
738   rbuObjIterFreeCols(pIter);
739   memset(pIter, 0, sizeof(RbuObjIter));
740 }
741 
742 /*
743 ** Advance the iterator to the next position.
744 **
745 ** If no error occurs, SQLITE_OK is returned and the iterator is left
746 ** pointing to the next entry. Otherwise, an error code and message is
747 ** left in the RBU handle passed as the first argument. A copy of the
748 ** error code is returned.
749 */
750 static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
751   int rc = p->rc;
752   if( rc==SQLITE_OK ){
753 
754     /* Free any SQLite statements used while processing the previous object */
755     rbuObjIterClearStatements(pIter);
756     if( pIter->zIdx==0 ){
757       rc = sqlite3_exec(p->dbMain,
758           "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
759           "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
760           "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
761           "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
762           , 0, 0, &p->zErrmsg
763       );
764     }
765 
766     if( rc==SQLITE_OK ){
767       if( pIter->bCleanup ){
768         rbuObjIterFreeCols(pIter);
769         pIter->bCleanup = 0;
770         rc = sqlite3_step(pIter->pTblIter);
771         if( rc!=SQLITE_ROW ){
772           rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
773           pIter->zTbl = 0;
774         }else{
775           pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
776           pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
777           rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
778         }
779       }else{
780         if( pIter->zIdx==0 ){
781           sqlite3_stmt *pIdx = pIter->pIdxIter;
782           rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
783         }
784         if( rc==SQLITE_OK ){
785           rc = sqlite3_step(pIter->pIdxIter);
786           if( rc!=SQLITE_ROW ){
787             rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
788             pIter->bCleanup = 1;
789             pIter->zIdx = 0;
790           }else{
791             pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
792             pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
793             pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
794             rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
795           }
796         }
797       }
798     }
799   }
800 
801   if( rc!=SQLITE_OK ){
802     rbuObjIterFinalize(pIter);
803     p->rc = rc;
804   }
805   return rc;
806 }
807 
808 
809 /*
810 ** The implementation of the rbu_target_name() SQL function. This function
811 ** accepts one argument - the name of a table in the RBU database. If the
812 ** table name matches the pattern:
813 **
814 **     data[0-9]_<name>
815 **
816 ** where <name> is any sequence of 1 or more characters, <name> is returned.
817 ** Otherwise, if the only argument does not match the above pattern, an SQL
818 ** NULL is returned.
819 **
820 **     "data_t1"     -> "t1"
821 **     "data0123_t2" -> "t2"
822 **     "dataAB_t3"   -> NULL
823 */
824 static void rbuTargetNameFunc(
825   sqlite3_context *context,
826   int argc,
827   sqlite3_value **argv
828 ){
829   const char *zIn;
830   assert( argc==1 );
831 
832   zIn = (const char*)sqlite3_value_text(argv[0]);
833   if( zIn && strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
834     int i;
835     for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
836     if( zIn[i]=='_' && zIn[i+1] ){
837       sqlite3_result_text(context, &zIn[i+1], -1, SQLITE_STATIC);
838     }
839   }
840 }
841 
842 /*
843 ** Initialize the iterator structure passed as the second argument.
844 **
845 ** If no error occurs, SQLITE_OK is returned and the iterator is left
846 ** pointing to the first entry. Otherwise, an error code and message is
847 ** left in the RBU handle passed as the first argument. A copy of the
848 ** error code is returned.
849 */
850 static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
851   int rc;
852   memset(pIter, 0, sizeof(RbuObjIter));
853 
854   rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
855       "SELECT rbu_target_name(name) AS target, name FROM sqlite_master "
856       "WHERE type IN ('table', 'view') AND target IS NOT NULL "
857       "ORDER BY name"
858   );
859 
860   if( rc==SQLITE_OK ){
861     rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
862         "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
863         "  FROM main.sqlite_master "
864         "  WHERE type='index' AND tbl_name = ?"
865     );
866   }
867 
868   pIter->bCleanup = 1;
869   p->rc = rc;
870   return rbuObjIterNext(p, pIter);
871 }
872 
873 /*
874 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
875 ** an error code is stored in the RBU handle passed as the first argument.
876 **
877 ** If an error has already occurred (p->rc is already set to something other
878 ** than SQLITE_OK), then this function returns NULL without modifying the
879 ** stored error code. In this case it still calls sqlite3_free() on any
880 ** printf() parameters associated with %z conversions.
881 */
882 static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
883   char *zSql = 0;
884   va_list ap;
885   va_start(ap, zFmt);
886   zSql = sqlite3_vmprintf(zFmt, ap);
887   if( p->rc==SQLITE_OK ){
888     if( zSql==0 ) p->rc = SQLITE_NOMEM;
889   }else{
890     sqlite3_free(zSql);
891     zSql = 0;
892   }
893   va_end(ap);
894   return zSql;
895 }
896 
897 /*
898 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
899 ** arguments are the usual subsitution values. This function performs
900 ** the printf() style substitutions and executes the result as an SQL
901 ** statement on the RBU handles database.
902 **
903 ** If an error occurs, an error code and error message is stored in the
904 ** RBU handle. If an error has already occurred when this function is
905 ** called, it is a no-op.
906 */
907 static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
908   va_list ap;
909   char *zSql;
910   va_start(ap, zFmt);
911   zSql = sqlite3_vmprintf(zFmt, ap);
912   if( p->rc==SQLITE_OK ){
913     if( zSql==0 ){
914       p->rc = SQLITE_NOMEM;
915     }else{
916       p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
917     }
918   }
919   sqlite3_free(zSql);
920   va_end(ap);
921   return p->rc;
922 }
923 
924 /*
925 ** Attempt to allocate and return a pointer to a zeroed block of nByte
926 ** bytes.
927 **
928 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
929 ** error code in the rbu handle passed as the first argument. Or, if an
930 ** error has already occurred when this function is called, return NULL
931 ** immediately without attempting the allocation or modifying the stored
932 ** error code.
933 */
934 static void *rbuMalloc(sqlite3rbu *p, int nByte){
935   void *pRet = 0;
936   if( p->rc==SQLITE_OK ){
937     assert( nByte>0 );
938     pRet = sqlite3_malloc64(nByte);
939     if( pRet==0 ){
940       p->rc = SQLITE_NOMEM;
941     }else{
942       memset(pRet, 0, nByte);
943     }
944   }
945   return pRet;
946 }
947 
948 
949 /*
950 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
951 ** there is room for at least nCol elements. If an OOM occurs, store an
952 ** error code in the RBU handle passed as the first argument.
953 */
954 static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
955   int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
956   char **azNew;
957 
958   azNew = (char**)rbuMalloc(p, nByte);
959   if( azNew ){
960     pIter->azTblCol = azNew;
961     pIter->azTblType = &azNew[nCol];
962     pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
963     pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
964     pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
965     pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
966   }
967 }
968 
969 /*
970 ** The first argument must be a nul-terminated string. This function
971 ** returns a copy of the string in memory obtained from sqlite3_malloc().
972 ** It is the responsibility of the caller to eventually free this memory
973 ** using sqlite3_free().
974 **
975 ** If an OOM condition is encountered when attempting to allocate memory,
976 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
977 ** if the allocation succeeds, (*pRc) is left unchanged.
978 */
979 static char *rbuStrndup(const char *zStr, int *pRc){
980   char *zRet = 0;
981 
982   assert( *pRc==SQLITE_OK );
983   if( zStr ){
984     size_t nCopy = strlen(zStr) + 1;
985     zRet = (char*)sqlite3_malloc64(nCopy);
986     if( zRet ){
987       memcpy(zRet, zStr, nCopy);
988     }else{
989       *pRc = SQLITE_NOMEM;
990     }
991   }
992 
993   return zRet;
994 }
995 
996 /*
997 ** Finalize the statement passed as the second argument.
998 **
999 ** If the sqlite3_finalize() call indicates that an error occurs, and the
1000 ** rbu handle error code is not already set, set the error code and error
1001 ** message accordingly.
1002 */
1003 static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
1004   sqlite3 *db = sqlite3_db_handle(pStmt);
1005   int rc = sqlite3_finalize(pStmt);
1006   if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
1007     p->rc = rc;
1008     p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1009   }
1010 }
1011 
1012 /* Determine the type of a table.
1013 **
1014 **   peType is of type (int*), a pointer to an output parameter of type
1015 **   (int). This call sets the output parameter as follows, depending
1016 **   on the type of the table specified by parameters dbName and zTbl.
1017 **
1018 **     RBU_PK_NOTABLE:       No such table.
1019 **     RBU_PK_NONE:          Table has an implicit rowid.
1020 **     RBU_PK_IPK:           Table has an explicit IPK column.
1021 **     RBU_PK_EXTERNAL:      Table has an external PK index.
1022 **     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
1023 **     RBU_PK_VTAB:          Table is a virtual table.
1024 **
1025 **   Argument *piPk is also of type (int*), and also points to an output
1026 **   parameter. Unless the table has an external primary key index
1027 **   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
1028 **   if the table does have an external primary key index, then *piPk
1029 **   is set to the root page number of the primary key index before
1030 **   returning.
1031 **
1032 ** ALGORITHM:
1033 **
1034 **   if( no entry exists in sqlite_master ){
1035 **     return RBU_PK_NOTABLE
1036 **   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
1037 **     return RBU_PK_VTAB
1038 **   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
1039 **     if( the index that is the pk exists in sqlite_master ){
1040 **       *piPK = rootpage of that index.
1041 **       return RBU_PK_EXTERNAL
1042 **     }else{
1043 **       return RBU_PK_WITHOUT_ROWID
1044 **     }
1045 **   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
1046 **     return RBU_PK_IPK
1047 **   }else{
1048 **     return RBU_PK_NONE
1049 **   }
1050 */
1051 static void rbuTableType(
1052   sqlite3rbu *p,
1053   const char *zTab,
1054   int *peType,
1055   int *piTnum,
1056   int *piPk
1057 ){
1058   /*
1059   ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
1060   ** 1) PRAGMA index_list = ?
1061   ** 2) SELECT count(*) FROM sqlite_master where name=%Q
1062   ** 3) PRAGMA table_info = ?
1063   */
1064   sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
1065 
1066   *peType = RBU_PK_NOTABLE;
1067   *piPk = 0;
1068 
1069   assert( p->rc==SQLITE_OK );
1070   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
1071     sqlite3_mprintf(
1072           "SELECT (sql LIKE 'create virtual%%'), rootpage"
1073           "  FROM sqlite_master"
1074           " WHERE name=%Q", zTab
1075   ));
1076   if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
1077     /* Either an error, or no such table. */
1078     goto rbuTableType_end;
1079   }
1080   if( sqlite3_column_int(aStmt[0], 0) ){
1081     *peType = RBU_PK_VTAB;                     /* virtual table */
1082     goto rbuTableType_end;
1083   }
1084   *piTnum = sqlite3_column_int(aStmt[0], 1);
1085 
1086   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
1087     sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
1088   );
1089   if( p->rc ) goto rbuTableType_end;
1090   while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
1091     const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
1092     const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
1093     if( zOrig && zIdx && zOrig[0]=='p' ){
1094       p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
1095           sqlite3_mprintf(
1096             "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
1097       ));
1098       if( p->rc==SQLITE_OK ){
1099         if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
1100           *piPk = sqlite3_column_int(aStmt[2], 0);
1101           *peType = RBU_PK_EXTERNAL;
1102         }else{
1103           *peType = RBU_PK_WITHOUT_ROWID;
1104         }
1105       }
1106       goto rbuTableType_end;
1107     }
1108   }
1109 
1110   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
1111     sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
1112   );
1113   if( p->rc==SQLITE_OK ){
1114     while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
1115       if( sqlite3_column_int(aStmt[3],5)>0 ){
1116         *peType = RBU_PK_IPK;                /* explicit IPK column */
1117         goto rbuTableType_end;
1118       }
1119     }
1120     *peType = RBU_PK_NONE;
1121   }
1122 
1123 rbuTableType_end: {
1124     unsigned int i;
1125     for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
1126       rbuFinalize(p, aStmt[i]);
1127     }
1128   }
1129 }
1130 
1131 /*
1132 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
1133 ** the pIter->abIndexed[] array.
1134 */
1135 static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
1136   sqlite3_stmt *pList = 0;
1137   int bIndex = 0;
1138 
1139   if( p->rc==SQLITE_OK ){
1140     memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
1141     p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
1142         sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
1143     );
1144   }
1145 
1146   while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
1147     const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
1148     sqlite3_stmt *pXInfo = 0;
1149     if( zIdx==0 ) break;
1150     p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1151         sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1152     );
1153     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1154       int iCid = sqlite3_column_int(pXInfo, 1);
1155       if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
1156     }
1157     rbuFinalize(p, pXInfo);
1158     bIndex = 1;
1159   }
1160 
1161   rbuFinalize(p, pList);
1162   if( bIndex==0 ) pIter->abIndexed = 0;
1163 }
1164 
1165 
1166 /*
1167 ** If they are not already populated, populate the pIter->azTblCol[],
1168 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
1169 ** the table (not index) that the iterator currently points to.
1170 **
1171 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
1172 ** an error does occur, an error code and error message are also left in
1173 ** the RBU handle.
1174 */
1175 static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
1176   if( pIter->azTblCol==0 ){
1177     sqlite3_stmt *pStmt = 0;
1178     int nCol = 0;
1179     int i;                        /* for() loop iterator variable */
1180     int bRbuRowid = 0;            /* If input table has column "rbu_rowid" */
1181     int iOrder = 0;
1182     int iTnum = 0;
1183 
1184     /* Figure out the type of table this step will deal with. */
1185     assert( pIter->eType==0 );
1186     rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
1187     if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
1188       p->rc = SQLITE_ERROR;
1189       p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
1190     }
1191     if( p->rc ) return p->rc;
1192     if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
1193 
1194     assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
1195          || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
1196          || pIter->eType==RBU_PK_VTAB
1197     );
1198 
1199     /* Populate the azTblCol[] and nTblCol variables based on the columns
1200     ** of the input table. Ignore any input table columns that begin with
1201     ** "rbu_".  */
1202     p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
1203         sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
1204     );
1205     if( p->rc==SQLITE_OK ){
1206       nCol = sqlite3_column_count(pStmt);
1207       rbuAllocateIterArrays(p, pIter, nCol);
1208     }
1209     for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
1210       const char *zName = (const char*)sqlite3_column_name(pStmt, i);
1211       if( sqlite3_strnicmp("rbu_", zName, 4) ){
1212         char *zCopy = rbuStrndup(zName, &p->rc);
1213         pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
1214         pIter->azTblCol[pIter->nTblCol++] = zCopy;
1215       }
1216       else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
1217         bRbuRowid = 1;
1218       }
1219     }
1220     sqlite3_finalize(pStmt);
1221     pStmt = 0;
1222 
1223     if( p->rc==SQLITE_OK
1224      && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
1225     ){
1226       p->rc = SQLITE_ERROR;
1227       p->zErrmsg = sqlite3_mprintf(
1228           "table %q %s rbu_rowid column", pIter->zDataTbl,
1229           (bRbuRowid ? "may not have" : "requires")
1230       );
1231     }
1232 
1233     /* Check that all non-HIDDEN columns in the destination table are also
1234     ** present in the input table. Populate the abTblPk[], azTblType[] and
1235     ** aiTblOrder[] arrays at the same time.  */
1236     if( p->rc==SQLITE_OK ){
1237       p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
1238           sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
1239       );
1240     }
1241     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1242       const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
1243       if( zName==0 ) break;  /* An OOM - finalize() below returns S_NOMEM */
1244       for(i=iOrder; i<pIter->nTblCol; i++){
1245         if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
1246       }
1247       if( i==pIter->nTblCol ){
1248         p->rc = SQLITE_ERROR;
1249         p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
1250             pIter->zDataTbl, zName
1251         );
1252       }else{
1253         int iPk = sqlite3_column_int(pStmt, 5);
1254         int bNotNull = sqlite3_column_int(pStmt, 3);
1255         const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
1256 
1257         if( i!=iOrder ){
1258           SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
1259           SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
1260         }
1261 
1262         pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
1263         pIter->abTblPk[iOrder] = (iPk!=0);
1264         pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
1265         iOrder++;
1266       }
1267     }
1268 
1269     rbuFinalize(p, pStmt);
1270     rbuObjIterCacheIndexedCols(p, pIter);
1271     assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
1272   }
1273 
1274   return p->rc;
1275 }
1276 
1277 /*
1278 ** This function constructs and returns a pointer to a nul-terminated
1279 ** string containing some SQL clause or list based on one or more of the
1280 ** column names currently stored in the pIter->azTblCol[] array.
1281 */
1282 static char *rbuObjIterGetCollist(
1283   sqlite3rbu *p,                  /* RBU object */
1284   RbuObjIter *pIter               /* Object iterator for column names */
1285 ){
1286   char *zList = 0;
1287   const char *zSep = "";
1288   int i;
1289   for(i=0; i<pIter->nTblCol; i++){
1290     const char *z = pIter->azTblCol[i];
1291     zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
1292     zSep = ", ";
1293   }
1294   return zList;
1295 }
1296 
1297 /*
1298 ** This function is used to create a SELECT list (the list of SQL
1299 ** expressions that follows a SELECT keyword) for a SELECT statement
1300 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
1301 ** index object currently indicated by the iterator object passed as the
1302 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
1303 ** to obtain the required information.
1304 **
1305 ** If the index is of the following form:
1306 **
1307 **   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
1308 **
1309 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
1310 ** "ipk", the returned string is:
1311 **
1312 **   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
1313 **
1314 ** As well as the returned string, three other malloc'd strings are
1315 ** returned via output parameters. As follows:
1316 **
1317 **   pzImposterCols: ...
1318 **   pzImposterPk: ...
1319 **   pzWhere: ...
1320 */
1321 static char *rbuObjIterGetIndexCols(
1322   sqlite3rbu *p,                  /* RBU object */
1323   RbuObjIter *pIter,              /* Object iterator for column names */
1324   char **pzImposterCols,          /* OUT: Columns for imposter table */
1325   char **pzImposterPk,            /* OUT: Imposter PK clause */
1326   char **pzWhere,                 /* OUT: WHERE clause */
1327   int *pnBind                     /* OUT: Trbul number of columns */
1328 ){
1329   int rc = p->rc;                 /* Error code */
1330   int rc2;                        /* sqlite3_finalize() return code */
1331   char *zRet = 0;                 /* String to return */
1332   char *zImpCols = 0;             /* String to return via *pzImposterCols */
1333   char *zImpPK = 0;               /* String to return via *pzImposterPK */
1334   char *zWhere = 0;               /* String to return via *pzWhere */
1335   int nBind = 0;                  /* Value to return via *pnBind */
1336   const char *zCom = "";          /* Set to ", " later on */
1337   const char *zAnd = "";          /* Set to " AND " later on */
1338   sqlite3_stmt *pXInfo = 0;       /* PRAGMA index_xinfo = ? */
1339 
1340   if( rc==SQLITE_OK ){
1341     assert( p->zErrmsg==0 );
1342     rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1343         sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
1344     );
1345   }
1346 
1347   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1348     int iCid = sqlite3_column_int(pXInfo, 1);
1349     int bDesc = sqlite3_column_int(pXInfo, 3);
1350     const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
1351     const char *zCol;
1352     const char *zType;
1353 
1354     if( iCid<0 ){
1355       /* An integer primary key. If the table has an explicit IPK, use
1356       ** its name. Otherwise, use "rbu_rowid".  */
1357       if( pIter->eType==RBU_PK_IPK ){
1358         int i;
1359         for(i=0; pIter->abTblPk[i]==0; i++);
1360         assert( i<pIter->nTblCol );
1361         zCol = pIter->azTblCol[i];
1362       }else{
1363         zCol = "rbu_rowid";
1364       }
1365       zType = "INTEGER";
1366     }else{
1367       zCol = pIter->azTblCol[iCid];
1368       zType = pIter->azTblType[iCid];
1369     }
1370 
1371     zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
1372     if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
1373       const char *zOrder = (bDesc ? " DESC" : "");
1374       zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
1375           zImpPK, zCom, nBind, zCol, zOrder
1376       );
1377     }
1378     zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
1379         zImpCols, zCom, nBind, zCol, zType, zCollate
1380     );
1381     zWhere = sqlite3_mprintf(
1382         "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
1383     );
1384     if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
1385     zCom = ", ";
1386     zAnd = " AND ";
1387     nBind++;
1388   }
1389 
1390   rc2 = sqlite3_finalize(pXInfo);
1391   if( rc==SQLITE_OK ) rc = rc2;
1392 
1393   if( rc!=SQLITE_OK ){
1394     sqlite3_free(zRet);
1395     sqlite3_free(zImpCols);
1396     sqlite3_free(zImpPK);
1397     sqlite3_free(zWhere);
1398     zRet = 0;
1399     zImpCols = 0;
1400     zImpPK = 0;
1401     zWhere = 0;
1402     p->rc = rc;
1403   }
1404 
1405   *pzImposterCols = zImpCols;
1406   *pzImposterPk = zImpPK;
1407   *pzWhere = zWhere;
1408   *pnBind = nBind;
1409   return zRet;
1410 }
1411 
1412 /*
1413 ** Assuming the current table columns are "a", "b" and "c", and the zObj
1414 ** paramter is passed "old", return a string of the form:
1415 **
1416 **     "old.a, old.b, old.b"
1417 **
1418 ** With the column names escaped.
1419 **
1420 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
1421 ** the text ", old._rowid_" to the returned value.
1422 */
1423 static char *rbuObjIterGetOldlist(
1424   sqlite3rbu *p,
1425   RbuObjIter *pIter,
1426   const char *zObj
1427 ){
1428   char *zList = 0;
1429   if( p->rc==SQLITE_OK && pIter->abIndexed ){
1430     const char *zS = "";
1431     int i;
1432     for(i=0; i<pIter->nTblCol; i++){
1433       if( pIter->abIndexed[i] ){
1434         const char *zCol = pIter->azTblCol[i];
1435         zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
1436       }else{
1437         zList = sqlite3_mprintf("%z%sNULL", zList, zS);
1438       }
1439       zS = ", ";
1440       if( zList==0 ){
1441         p->rc = SQLITE_NOMEM;
1442         break;
1443       }
1444     }
1445 
1446     /* For a table with implicit rowids, append "old._rowid_" to the list. */
1447     if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
1448       zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
1449     }
1450   }
1451   return zList;
1452 }
1453 
1454 /*
1455 ** Return an expression that can be used in a WHERE clause to match the
1456 ** primary key of the current table. For example, if the table is:
1457 **
1458 **   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
1459 **
1460 ** Return the string:
1461 **
1462 **   "b = ?1 AND c = ?2"
1463 */
1464 static char *rbuObjIterGetWhere(
1465   sqlite3rbu *p,
1466   RbuObjIter *pIter
1467 ){
1468   char *zList = 0;
1469   if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
1470     zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
1471   }else if( pIter->eType==RBU_PK_EXTERNAL ){
1472     const char *zSep = "";
1473     int i;
1474     for(i=0; i<pIter->nTblCol; i++){
1475       if( pIter->abTblPk[i] ){
1476         zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
1477         zSep = " AND ";
1478       }
1479     }
1480     zList = rbuMPrintf(p,
1481         "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
1482     );
1483 
1484   }else{
1485     const char *zSep = "";
1486     int i;
1487     for(i=0; i<pIter->nTblCol; i++){
1488       if( pIter->abTblPk[i] ){
1489         const char *zCol = pIter->azTblCol[i];
1490         zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
1491         zSep = " AND ";
1492       }
1493     }
1494   }
1495   return zList;
1496 }
1497 
1498 /*
1499 ** The SELECT statement iterating through the keys for the current object
1500 ** (p->objiter.pSelect) currently points to a valid row. However, there
1501 ** is something wrong with the rbu_control value in the rbu_control value
1502 ** stored in the (p->nCol+1)'th column. Set the error code and error message
1503 ** of the RBU handle to something reflecting this.
1504 */
1505 static void rbuBadControlError(sqlite3rbu *p){
1506   p->rc = SQLITE_ERROR;
1507   p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
1508 }
1509 
1510 
1511 /*
1512 ** Return a nul-terminated string containing the comma separated list of
1513 ** assignments that should be included following the "SET" keyword of
1514 ** an UPDATE statement used to update the table object that the iterator
1515 ** passed as the second argument currently points to if the rbu_control
1516 ** column of the data_xxx table entry is set to zMask.
1517 **
1518 ** The memory for the returned string is obtained from sqlite3_malloc().
1519 ** It is the responsibility of the caller to eventually free it using
1520 ** sqlite3_free().
1521 **
1522 ** If an OOM error is encountered when allocating space for the new
1523 ** string, an error code is left in the rbu handle passed as the first
1524 ** argument and NULL is returned. Or, if an error has already occurred
1525 ** when this function is called, NULL is returned immediately, without
1526 ** attempting the allocation or modifying the stored error code.
1527 */
1528 static char *rbuObjIterGetSetlist(
1529   sqlite3rbu *p,
1530   RbuObjIter *pIter,
1531   const char *zMask
1532 ){
1533   char *zList = 0;
1534   if( p->rc==SQLITE_OK ){
1535     int i;
1536 
1537     if( (int)strlen(zMask)!=pIter->nTblCol ){
1538       rbuBadControlError(p);
1539     }else{
1540       const char *zSep = "";
1541       for(i=0; i<pIter->nTblCol; i++){
1542         char c = zMask[pIter->aiSrcOrder[i]];
1543         if( c=='x' ){
1544           zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
1545               zList, zSep, pIter->azTblCol[i], i+1
1546           );
1547           zSep = ", ";
1548         }
1549         else if( c=='d' ){
1550           zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
1551               zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
1552           );
1553           zSep = ", ";
1554         }
1555         else if( c=='f' ){
1556           zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
1557               zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
1558           );
1559           zSep = ", ";
1560         }
1561       }
1562     }
1563   }
1564   return zList;
1565 }
1566 
1567 /*
1568 ** Return a nul-terminated string consisting of nByte comma separated
1569 ** "?" expressions. For example, if nByte is 3, return a pointer to
1570 ** a buffer containing the string "?,?,?".
1571 **
1572 ** The memory for the returned string is obtained from sqlite3_malloc().
1573 ** It is the responsibility of the caller to eventually free it using
1574 ** sqlite3_free().
1575 **
1576 ** If an OOM error is encountered when allocating space for the new
1577 ** string, an error code is left in the rbu handle passed as the first
1578 ** argument and NULL is returned. Or, if an error has already occurred
1579 ** when this function is called, NULL is returned immediately, without
1580 ** attempting the allocation or modifying the stored error code.
1581 */
1582 static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
1583   char *zRet = 0;
1584   int nByte = nBind*2 + 1;
1585 
1586   zRet = (char*)rbuMalloc(p, nByte);
1587   if( zRet ){
1588     int i;
1589     for(i=0; i<nBind; i++){
1590       zRet[i*2] = '?';
1591       zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
1592     }
1593   }
1594   return zRet;
1595 }
1596 
1597 /*
1598 ** The iterator currently points to a table (not index) of type
1599 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
1600 ** declaration for the corresponding imposter table. For example,
1601 ** if the iterator points to a table created as:
1602 **
1603 **   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
1604 **
1605 ** this function returns:
1606 **
1607 **   PRIMARY KEY("b", "a" DESC)
1608 */
1609 static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
1610   char *z = 0;
1611   assert( pIter->zIdx==0 );
1612   if( p->rc==SQLITE_OK ){
1613     const char *zSep = "PRIMARY KEY(";
1614     sqlite3_stmt *pXList = 0;     /* PRAGMA index_list = (pIter->zTbl) */
1615     sqlite3_stmt *pXInfo = 0;     /* PRAGMA index_xinfo = <pk-index> */
1616 
1617     p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
1618         sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
1619     );
1620     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
1621       const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
1622       if( zOrig && strcmp(zOrig, "pk")==0 ){
1623         const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
1624         if( zIdx ){
1625           p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1626               sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1627           );
1628         }
1629         break;
1630       }
1631     }
1632     rbuFinalize(p, pXList);
1633 
1634     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1635       if( sqlite3_column_int(pXInfo, 5) ){
1636         /* int iCid = sqlite3_column_int(pXInfo, 0); */
1637         const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
1638         const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
1639         z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
1640         zSep = ", ";
1641       }
1642     }
1643     z = rbuMPrintf(p, "%z)", z);
1644     rbuFinalize(p, pXInfo);
1645   }
1646   return z;
1647 }
1648 
1649 /*
1650 ** This function creates the second imposter table used when writing to
1651 ** a table b-tree where the table has an external primary key. If the
1652 ** iterator passed as the second argument does not currently point to
1653 ** a table (not index) with an external primary key, this function is a
1654 ** no-op.
1655 **
1656 ** Assuming the iterator does point to a table with an external PK, this
1657 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
1658 ** used to access that PK index. For example, if the target table is
1659 ** declared as follows:
1660 **
1661 **   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
1662 **
1663 ** then the imposter table schema is:
1664 **
1665 **   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
1666 **
1667 */
1668 static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
1669   if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
1670     int tnum = pIter->iPkTnum;    /* Root page of PK index */
1671     sqlite3_stmt *pQuery = 0;     /* SELECT name ... WHERE rootpage = $tnum */
1672     const char *zIdx = 0;         /* Name of PK index */
1673     sqlite3_stmt *pXInfo = 0;     /* PRAGMA main.index_xinfo = $zIdx */
1674     const char *zComma = "";
1675     char *zCols = 0;              /* Used to build up list of table cols */
1676     char *zPk = 0;                /* Used to build up table PK declaration */
1677 
1678     /* Figure out the name of the primary key index for the current table.
1679     ** This is needed for the argument to "PRAGMA index_xinfo". Set
1680     ** zIdx to point to a nul-terminated string containing this name. */
1681     p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
1682         "SELECT name FROM sqlite_master WHERE rootpage = ?"
1683     );
1684     if( p->rc==SQLITE_OK ){
1685       sqlite3_bind_int(pQuery, 1, tnum);
1686       if( SQLITE_ROW==sqlite3_step(pQuery) ){
1687         zIdx = (const char*)sqlite3_column_text(pQuery, 0);
1688       }
1689     }
1690     if( zIdx ){
1691       p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1692           sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1693       );
1694     }
1695     rbuFinalize(p, pQuery);
1696 
1697     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1698       int bKey = sqlite3_column_int(pXInfo, 5);
1699       if( bKey ){
1700         int iCid = sqlite3_column_int(pXInfo, 1);
1701         int bDesc = sqlite3_column_int(pXInfo, 3);
1702         const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
1703         zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
1704             iCid, pIter->azTblType[iCid], zCollate
1705         );
1706         zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
1707         zComma = ", ";
1708       }
1709     }
1710     zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
1711     rbuFinalize(p, pXInfo);
1712 
1713     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
1714     rbuMPrintfExec(p, p->dbMain,
1715         "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
1716         zCols, zPk
1717     );
1718     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
1719   }
1720 }
1721 
1722 /*
1723 ** If an error has already occurred when this function is called, it
1724 ** immediately returns zero (without doing any work). Or, if an error
1725 ** occurs during the execution of this function, it sets the error code
1726 ** in the sqlite3rbu object indicated by the first argument and returns
1727 ** zero.
1728 **
1729 ** The iterator passed as the second argument is guaranteed to point to
1730 ** a table (not an index) when this function is called. This function
1731 ** attempts to create any imposter table required to write to the main
1732 ** table b-tree of the table before returning. Non-zero is returned if
1733 ** an imposter table are created, or zero otherwise.
1734 **
1735 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
1736 ** virtual tables are written to directly. The imposter table has the
1737 ** same schema as the actual target table (less any UNIQUE constraints).
1738 ** More precisely, the "same schema" means the same columns, types,
1739 ** collation sequences. For tables that do not have an external PRIMARY
1740 ** KEY, it also means the same PRIMARY KEY declaration.
1741 */
1742 static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
1743   if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
1744     int tnum = pIter->iTnum;
1745     const char *zComma = "";
1746     char *zSql = 0;
1747     int iCol;
1748     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
1749 
1750     for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
1751       const char *zPk = "";
1752       const char *zCol = pIter->azTblCol[iCol];
1753       const char *zColl = 0;
1754 
1755       p->rc = sqlite3_table_column_metadata(
1756           p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
1757       );
1758 
1759       if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
1760         /* If the target table column is an "INTEGER PRIMARY KEY", add
1761         ** "PRIMARY KEY" to the imposter table column declaration. */
1762         zPk = "PRIMARY KEY ";
1763       }
1764       zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
1765           zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
1766           (pIter->abNotNull[iCol] ? " NOT NULL" : "")
1767       );
1768       zComma = ", ";
1769     }
1770 
1771     if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
1772       char *zPk = rbuWithoutRowidPK(p, pIter);
1773       if( zPk ){
1774         zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
1775       }
1776     }
1777 
1778     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
1779     rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
1780         pIter->zTbl, zSql,
1781         (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
1782     );
1783     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
1784   }
1785 }
1786 
1787 /*
1788 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
1789 ** Specifically a statement of the form:
1790 **
1791 **     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
1792 **
1793 ** The number of bound variables is equal to the number of columns in
1794 ** the target table, plus one (for the rbu_control column), plus one more
1795 ** (for the rbu_rowid column) if the target table is an implicit IPK or
1796 ** virtual table.
1797 */
1798 static void rbuObjIterPrepareTmpInsert(
1799   sqlite3rbu *p,
1800   RbuObjIter *pIter,
1801   const char *zCollist,
1802   const char *zRbuRowid
1803 ){
1804   int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
1805   char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
1806   if( zBind ){
1807     assert( pIter->pTmpInsert==0 );
1808     p->rc = prepareFreeAndCollectError(
1809         p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
1810           "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
1811           p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
1812     ));
1813   }
1814 }
1815 
1816 static void rbuTmpInsertFunc(
1817   sqlite3_context *pCtx,
1818   int nVal,
1819   sqlite3_value **apVal
1820 ){
1821   sqlite3rbu *p = sqlite3_user_data(pCtx);
1822   int rc = SQLITE_OK;
1823   int i;
1824 
1825   for(i=0; rc==SQLITE_OK && i<nVal; i++){
1826     rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
1827   }
1828   if( rc==SQLITE_OK ){
1829     sqlite3_step(p->objiter.pTmpInsert);
1830     rc = sqlite3_reset(p->objiter.pTmpInsert);
1831   }
1832 
1833   if( rc!=SQLITE_OK ){
1834     sqlite3_result_error_code(pCtx, rc);
1835   }
1836 }
1837 
1838 /*
1839 ** Ensure that the SQLite statement handles required to update the
1840 ** target database object currently indicated by the iterator passed
1841 ** as the second argument are available.
1842 */
1843 static int rbuObjIterPrepareAll(
1844   sqlite3rbu *p,
1845   RbuObjIter *pIter,
1846   int nOffset                     /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
1847 ){
1848   assert( pIter->bCleanup==0 );
1849   if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
1850     const int tnum = pIter->iTnum;
1851     char *zCollist = 0;           /* List of indexed columns */
1852     char **pz = &p->zErrmsg;
1853     const char *zIdx = pIter->zIdx;
1854     char *zLimit = 0;
1855 
1856     if( nOffset ){
1857       zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
1858       if( !zLimit ) p->rc = SQLITE_NOMEM;
1859     }
1860 
1861     if( zIdx ){
1862       const char *zTbl = pIter->zTbl;
1863       char *zImposterCols = 0;    /* Columns for imposter table */
1864       char *zImposterPK = 0;      /* Primary key declaration for imposter */
1865       char *zWhere = 0;           /* WHERE clause on PK columns */
1866       char *zBind = 0;
1867       int nBind = 0;
1868 
1869       assert( pIter->eType!=RBU_PK_VTAB );
1870       zCollist = rbuObjIterGetIndexCols(
1871           p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
1872       );
1873       zBind = rbuObjIterGetBindlist(p, nBind);
1874 
1875       /* Create the imposter table used to write to this index. */
1876       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
1877       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
1878       rbuMPrintfExec(p, p->dbMain,
1879           "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
1880           zTbl, zImposterCols, zImposterPK
1881       );
1882       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
1883 
1884       /* Create the statement to insert index entries */
1885       pIter->nCol = nBind;
1886       if( p->rc==SQLITE_OK ){
1887         p->rc = prepareFreeAndCollectError(
1888             p->dbMain, &pIter->pInsert, &p->zErrmsg,
1889           sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
1890         );
1891       }
1892 
1893       /* And to delete index entries */
1894       if( p->rc==SQLITE_OK ){
1895         p->rc = prepareFreeAndCollectError(
1896             p->dbMain, &pIter->pDelete, &p->zErrmsg,
1897           sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
1898         );
1899       }
1900 
1901       /* Create the SELECT statement to read keys in sorted order */
1902       if( p->rc==SQLITE_OK ){
1903         char *zSql;
1904         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
1905           zSql = sqlite3_mprintf(
1906               "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
1907               zCollist, p->zStateDb, pIter->zDataTbl,
1908               zCollist, zLimit
1909           );
1910         }else{
1911           zSql = sqlite3_mprintf(
1912               "SELECT %s, rbu_control FROM '%q' "
1913               "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
1914               "UNION ALL "
1915               "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
1916               "ORDER BY %s%s",
1917               zCollist, pIter->zDataTbl,
1918               zCollist, p->zStateDb, pIter->zDataTbl,
1919               zCollist, zLimit
1920           );
1921         }
1922         p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
1923       }
1924 
1925       sqlite3_free(zImposterCols);
1926       sqlite3_free(zImposterPK);
1927       sqlite3_free(zWhere);
1928       sqlite3_free(zBind);
1929     }else{
1930       int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
1931       const char *zTbl = pIter->zTbl;       /* Table this step applies to */
1932       const char *zWrite;                   /* Imposter table name */
1933 
1934       char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
1935       char *zWhere = rbuObjIterGetWhere(p, pIter);
1936       char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
1937       char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
1938 
1939       zCollist = rbuObjIterGetCollist(p, pIter);
1940       pIter->nCol = pIter->nTblCol;
1941 
1942       /* Create the imposter table or tables (if required). */
1943       rbuCreateImposterTable(p, pIter);
1944       rbuCreateImposterTable2(p, pIter);
1945       zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
1946 
1947       /* Create the INSERT statement to write to the target PK b-tree */
1948       if( p->rc==SQLITE_OK ){
1949         p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
1950             sqlite3_mprintf(
1951               "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
1952               zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
1953             )
1954         );
1955       }
1956 
1957       /* Create the DELETE statement to write to the target PK b-tree */
1958       if( p->rc==SQLITE_OK ){
1959         p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
1960             sqlite3_mprintf(
1961               "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
1962             )
1963         );
1964       }
1965 
1966       if( pIter->abIndexed ){
1967         const char *zRbuRowid = "";
1968         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
1969           zRbuRowid = ", rbu_rowid";
1970         }
1971 
1972         /* Create the rbu_tmp_xxx table and the triggers to populate it. */
1973         rbuMPrintfExec(p, p->dbRbu,
1974             "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
1975             "SELECT *%s FROM '%q' WHERE 0;"
1976             , p->zStateDb, pIter->zDataTbl
1977             , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
1978             , pIter->zDataTbl
1979         );
1980 
1981         rbuMPrintfExec(p, p->dbMain,
1982             "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
1983             "BEGIN "
1984             "  SELECT rbu_tmp_insert(2, %s);"
1985             "END;"
1986 
1987             "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
1988             "BEGIN "
1989             "  SELECT rbu_tmp_insert(2, %s);"
1990             "END;"
1991 
1992             "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
1993             "BEGIN "
1994             "  SELECT rbu_tmp_insert(3, %s);"
1995             "END;",
1996             zWrite, zTbl, zOldlist,
1997             zWrite, zTbl, zOldlist,
1998             zWrite, zTbl, zNewlist
1999         );
2000 
2001         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2002           rbuMPrintfExec(p, p->dbMain,
2003               "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
2004               "BEGIN "
2005               "  SELECT rbu_tmp_insert(0, %s);"
2006               "END;",
2007               zWrite, zTbl, zNewlist
2008           );
2009         }
2010 
2011         rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
2012       }
2013 
2014       /* Create the SELECT statement to read keys from data_xxx */
2015       if( p->rc==SQLITE_OK ){
2016         p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
2017             sqlite3_mprintf(
2018               "SELECT %s, rbu_control%s FROM '%q'%s",
2019               zCollist, (bRbuRowid ? ", rbu_rowid" : ""),
2020               pIter->zDataTbl, zLimit
2021             )
2022         );
2023       }
2024 
2025       sqlite3_free(zWhere);
2026       sqlite3_free(zOldlist);
2027       sqlite3_free(zNewlist);
2028       sqlite3_free(zBindings);
2029     }
2030     sqlite3_free(zCollist);
2031     sqlite3_free(zLimit);
2032   }
2033 
2034   return p->rc;
2035 }
2036 
2037 /*
2038 ** Set output variable *ppStmt to point to an UPDATE statement that may
2039 ** be used to update the imposter table for the main table b-tree of the
2040 ** table object that pIter currently points to, assuming that the
2041 ** rbu_control column of the data_xyz table contains zMask.
2042 **
2043 ** If the zMask string does not specify any columns to update, then this
2044 ** is not an error. Output variable *ppStmt is set to NULL in this case.
2045 */
2046 static int rbuGetUpdateStmt(
2047   sqlite3rbu *p,                  /* RBU handle */
2048   RbuObjIter *pIter,              /* Object iterator */
2049   const char *zMask,              /* rbu_control value ('x.x.') */
2050   sqlite3_stmt **ppStmt           /* OUT: UPDATE statement handle */
2051 ){
2052   RbuUpdateStmt **pp;
2053   RbuUpdateStmt *pUp = 0;
2054   int nUp = 0;
2055 
2056   /* In case an error occurs */
2057   *ppStmt = 0;
2058 
2059   /* Search for an existing statement. If one is found, shift it to the front
2060   ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
2061   ** to the number of statements currently in the cache and pUp to the
2062   ** last object in the list.  */
2063   for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
2064     pUp = *pp;
2065     if( strcmp(pUp->zMask, zMask)==0 ){
2066       *pp = pUp->pNext;
2067       pUp->pNext = pIter->pRbuUpdate;
2068       pIter->pRbuUpdate = pUp;
2069       *ppStmt = pUp->pUpdate;
2070       return SQLITE_OK;
2071     }
2072     nUp++;
2073   }
2074   assert( pUp==0 || pUp->pNext==0 );
2075 
2076   if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
2077     for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
2078     *pp = 0;
2079     sqlite3_finalize(pUp->pUpdate);
2080     pUp->pUpdate = 0;
2081   }else{
2082     pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
2083   }
2084 
2085   if( pUp ){
2086     char *zWhere = rbuObjIterGetWhere(p, pIter);
2087     char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
2088     char *zUpdate = 0;
2089 
2090     pUp->zMask = (char*)&pUp[1];
2091     memcpy(pUp->zMask, zMask, pIter->nTblCol);
2092     pUp->pNext = pIter->pRbuUpdate;
2093     pIter->pRbuUpdate = pUp;
2094 
2095     if( zSet ){
2096       const char *zPrefix = "";
2097 
2098       if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
2099       zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
2100           zPrefix, pIter->zTbl, zSet, zWhere
2101       );
2102       p->rc = prepareFreeAndCollectError(
2103           p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
2104       );
2105       *ppStmt = pUp->pUpdate;
2106     }
2107     sqlite3_free(zWhere);
2108     sqlite3_free(zSet);
2109   }
2110 
2111   return p->rc;
2112 }
2113 
2114 static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
2115   sqlite3 *db = 0;
2116   if( p->rc==SQLITE_OK ){
2117     const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
2118     p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
2119     if( p->rc ){
2120       p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2121       sqlite3_close(db);
2122       db = 0;
2123     }
2124   }
2125   return db;
2126 }
2127 
2128 /*
2129 ** Open the database handle and attach the RBU database as "rbu". If an
2130 ** error occurs, leave an error code and message in the RBU handle.
2131 */
2132 static void rbuOpenDatabase(sqlite3rbu *p){
2133   assert( p->rc==SQLITE_OK );
2134   assert( p->dbMain==0 && p->dbRbu==0 );
2135 
2136   p->eStage = 0;
2137   p->dbMain = rbuOpenDbhandle(p, p->zTarget);
2138   p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
2139 
2140   /* If using separate RBU and state databases, attach the state database to
2141   ** the RBU db handle now.  */
2142   if( p->zState ){
2143     rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
2144     memcpy(p->zStateDb, "stat", 4);
2145   }else{
2146     memcpy(p->zStateDb, "main", 4);
2147   }
2148 
2149   if( p->rc==SQLITE_OK ){
2150     p->rc = sqlite3_create_function(p->dbMain,
2151         "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
2152     );
2153   }
2154 
2155   if( p->rc==SQLITE_OK ){
2156     p->rc = sqlite3_create_function(p->dbMain,
2157         "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
2158     );
2159   }
2160 
2161   if( p->rc==SQLITE_OK ){
2162     p->rc = sqlite3_create_function(p->dbRbu,
2163         "rbu_target_name", 1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
2164     );
2165   }
2166 
2167   if( p->rc==SQLITE_OK ){
2168     p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
2169   }
2170   rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
2171 
2172   /* Mark the database file just opened as an RBU target database. If
2173   ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
2174   ** This is an error.  */
2175   if( p->rc==SQLITE_OK ){
2176     p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
2177   }
2178 
2179   if( p->rc==SQLITE_NOTFOUND ){
2180     p->rc = SQLITE_ERROR;
2181     p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
2182   }
2183 }
2184 
2185 /*
2186 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
2187 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
2188 **
2189 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
2190 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
2191 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
2192 ** three characters, then shorten the suffix on z[] to be the last three
2193 ** characters of the original suffix.
2194 **
2195 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
2196 ** do the suffix shortening regardless of URI parameter.
2197 **
2198 ** Examples:
2199 **
2200 **     test.db-journal    =>   test.nal
2201 **     test.db-wal        =>   test.wal
2202 **     test.db-shm        =>   test.shm
2203 **     test.db-mj7f3319fa =>   test.9fa
2204 */
2205 static void rbuFileSuffix3(const char *zBase, char *z){
2206 #ifdef SQLITE_ENABLE_8_3_NAMES
2207 #if SQLITE_ENABLE_8_3_NAMES<2
2208   if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
2209 #endif
2210   {
2211     int i, sz;
2212     sz = sqlite3Strlen30(z);
2213     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
2214     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
2215   }
2216 #endif
2217 }
2218 
2219 /*
2220 ** Return the current wal-index header checksum for the target database
2221 ** as a 64-bit integer.
2222 **
2223 ** The checksum is store in the first page of xShmMap memory as an 8-byte
2224 ** blob starting at byte offset 40.
2225 */
2226 static i64 rbuShmChecksum(sqlite3rbu *p){
2227   i64 iRet = 0;
2228   if( p->rc==SQLITE_OK ){
2229     sqlite3_file *pDb = p->pTargetFd->pReal;
2230     u32 volatile *ptr;
2231     p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
2232     if( p->rc==SQLITE_OK ){
2233       iRet = ((i64)ptr[10] << 32) + ptr[11];
2234     }
2235   }
2236   return iRet;
2237 }
2238 
2239 /*
2240 ** This function is called as part of initializing or reinitializing an
2241 ** incremental checkpoint.
2242 **
2243 ** It populates the sqlite3rbu.aFrame[] array with the set of
2244 ** (wal frame -> db page) copy operations required to checkpoint the
2245 ** current wal file, and obtains the set of shm locks required to safely
2246 ** perform the copy operations directly on the file-system.
2247 **
2248 ** If argument pState is not NULL, then the incremental checkpoint is
2249 ** being resumed. In this case, if the checksum of the wal-index-header
2250 ** following recovery is not the same as the checksum saved in the RbuState
2251 ** object, then the rbu handle is set to DONE state. This occurs if some
2252 ** other client appends a transaction to the wal file in the middle of
2253 ** an incremental checkpoint.
2254 */
2255 static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
2256 
2257   /* If pState is NULL, then the wal file may not have been opened and
2258   ** recovered. Running a read-statement here to ensure that doing so
2259   ** does not interfere with the "capture" process below.  */
2260   if( pState==0 ){
2261     p->eStage = 0;
2262     if( p->rc==SQLITE_OK ){
2263       p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
2264     }
2265   }
2266 
2267   /* Assuming no error has occurred, run a "restart" checkpoint with the
2268   ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
2269   ** special behaviour in the rbu VFS:
2270   **
2271   **   * If the exclusive shm WRITER or READ0 lock cannot be obtained,
2272   **     the checkpoint fails with SQLITE_BUSY (normally SQLite would
2273   **     proceed with running a passive checkpoint instead of failing).
2274   **
2275   **   * Attempts to read from the *-wal file or write to the database file
2276   **     do not perform any IO. Instead, the frame/page combinations that
2277   **     would be read/written are recorded in the sqlite3rbu.aFrame[]
2278   **     array.
2279   **
2280   **   * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
2281   **     READ0 and CHECKPOINT locks taken as part of the checkpoint are
2282   **     no-ops. These locks will not be released until the connection
2283   **     is closed.
2284   **
2285   **   * Attempting to xSync() the database file causes an SQLITE_INTERNAL
2286   **     error.
2287   **
2288   ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
2289   ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
2290   ** array populated with a set of (frame -> page) mappings. Because the
2291   ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
2292   ** data from the wal file into the database file according to the
2293   ** contents of aFrame[].
2294   */
2295   if( p->rc==SQLITE_OK ){
2296     int rc2;
2297     p->eStage = RBU_STAGE_CAPTURE;
2298     rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
2299     if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
2300   }
2301 
2302   if( p->rc==SQLITE_OK ){
2303     p->eStage = RBU_STAGE_CKPT;
2304     p->nStep = (pState ? pState->nRow : 0);
2305     p->aBuf = rbuMalloc(p, p->pgsz);
2306     p->iWalCksum = rbuShmChecksum(p);
2307   }
2308 
2309   if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
2310     p->rc = SQLITE_DONE;
2311     p->eStage = RBU_STAGE_DONE;
2312   }
2313 }
2314 
2315 /*
2316 ** Called when iAmt bytes are read from offset iOff of the wal file while
2317 ** the rbu object is in capture mode. Record the frame number of the frame
2318 ** being read in the aFrame[] array.
2319 */
2320 static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
2321   const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
2322   u32 iFrame;
2323 
2324   if( pRbu->mLock!=mReq ){
2325     pRbu->rc = SQLITE_BUSY;
2326     return SQLITE_INTERNAL;
2327   }
2328 
2329   pRbu->pgsz = iAmt;
2330   if( pRbu->nFrame==pRbu->nFrameAlloc ){
2331     int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
2332     RbuFrame *aNew;
2333     aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
2334     if( aNew==0 ) return SQLITE_NOMEM;
2335     pRbu->aFrame = aNew;
2336     pRbu->nFrameAlloc = nNew;
2337   }
2338 
2339   iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
2340   if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
2341   pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
2342   pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
2343   pRbu->nFrame++;
2344   return SQLITE_OK;
2345 }
2346 
2347 /*
2348 ** Called when a page of data is written to offset iOff of the database
2349 ** file while the rbu handle is in capture mode. Record the page number
2350 ** of the page being written in the aFrame[] array.
2351 */
2352 static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
2353   pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
2354   return SQLITE_OK;
2355 }
2356 
2357 /*
2358 ** This is called as part of an incremental checkpoint operation. Copy
2359 ** a single frame of data from the wal file into the database file, as
2360 ** indicated by the RbuFrame object.
2361 */
2362 static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
2363   sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
2364   sqlite3_file *pDb = p->pTargetFd->pReal;
2365   i64 iOff;
2366 
2367   assert( p->rc==SQLITE_OK );
2368   iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
2369   p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
2370   if( p->rc ) return;
2371 
2372   iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
2373   p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
2374 }
2375 
2376 
2377 /*
2378 ** Take an EXCLUSIVE lock on the database file.
2379 */
2380 static void rbuLockDatabase(sqlite3rbu *p){
2381   sqlite3_file *pReal = p->pTargetFd->pReal;
2382   assert( p->rc==SQLITE_OK );
2383   p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
2384   if( p->rc==SQLITE_OK ){
2385     p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
2386   }
2387 }
2388 
2389 #if defined(_WIN32_WCE)
2390 static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
2391   int nChar;
2392   LPWSTR zWideFilename;
2393 
2394   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
2395   if( nChar==0 ){
2396     return 0;
2397   }
2398   zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
2399   if( zWideFilename==0 ){
2400     return 0;
2401   }
2402   memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
2403   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
2404                                 nChar);
2405   if( nChar==0 ){
2406     sqlite3_free(zWideFilename);
2407     zWideFilename = 0;
2408   }
2409   return zWideFilename;
2410 }
2411 #endif
2412 
2413 /*
2414 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
2415 ** on the database file. This proc moves the *-oal file to the *-wal path,
2416 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
2417 ** If an error occurs, leave an error code and error message in the rbu
2418 ** handle.
2419 */
2420 static void rbuMoveOalFile(sqlite3rbu *p){
2421   const char *zBase = sqlite3_db_filename(p->dbMain, "main");
2422 
2423   char *zWal = sqlite3_mprintf("%s-wal", zBase);
2424   char *zOal = sqlite3_mprintf("%s-oal", zBase);
2425 
2426   assert( p->eStage==RBU_STAGE_MOVE );
2427   assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
2428   if( zWal==0 || zOal==0 ){
2429     p->rc = SQLITE_NOMEM;
2430   }else{
2431     /* Move the *-oal file to *-wal. At this point connection p->db is
2432     ** holding a SHARED lock on the target database file (because it is
2433     ** in WAL mode). So no other connection may be writing the db.
2434     **
2435     ** In order to ensure that there are no database readers, an EXCLUSIVE
2436     ** lock is obtained here before the *-oal is moved to *-wal.
2437     */
2438     rbuLockDatabase(p);
2439     if( p->rc==SQLITE_OK ){
2440       rbuFileSuffix3(zBase, zWal);
2441       rbuFileSuffix3(zBase, zOal);
2442 
2443       /* Re-open the databases. */
2444       rbuObjIterFinalize(&p->objiter);
2445       sqlite3_close(p->dbMain);
2446       sqlite3_close(p->dbRbu);
2447       p->dbMain = 0;
2448       p->dbRbu = 0;
2449 
2450 #if defined(_WIN32_WCE)
2451       {
2452         LPWSTR zWideOal;
2453         LPWSTR zWideWal;
2454 
2455         zWideOal = rbuWinUtf8ToUnicode(zOal);
2456         if( zWideOal ){
2457           zWideWal = rbuWinUtf8ToUnicode(zWal);
2458           if( zWideWal ){
2459             if( MoveFileW(zWideOal, zWideWal) ){
2460               p->rc = SQLITE_OK;
2461             }else{
2462               p->rc = SQLITE_IOERR;
2463             }
2464             sqlite3_free(zWideWal);
2465           }else{
2466             p->rc = SQLITE_IOERR_NOMEM;
2467           }
2468           sqlite3_free(zWideOal);
2469         }else{
2470           p->rc = SQLITE_IOERR_NOMEM;
2471         }
2472       }
2473 #else
2474       p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
2475 #endif
2476 
2477       if( p->rc==SQLITE_OK ){
2478         rbuOpenDatabase(p);
2479         rbuSetupCheckpoint(p, 0);
2480       }
2481     }
2482   }
2483 
2484   sqlite3_free(zWal);
2485   sqlite3_free(zOal);
2486 }
2487 
2488 /*
2489 ** The SELECT statement iterating through the keys for the current object
2490 ** (p->objiter.pSelect) currently points to a valid row. This function
2491 ** determines the type of operation requested by this row and returns
2492 ** one of the following values to indicate the result:
2493 **
2494 **     * RBU_INSERT
2495 **     * RBU_DELETE
2496 **     * RBU_IDX_DELETE
2497 **     * RBU_UPDATE
2498 **
2499 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
2500 ** point to the text value indicating the columns to update.
2501 **
2502 ** If the rbu_control field contains an invalid value, an error code and
2503 ** message are left in the RBU handle and zero returned.
2504 */
2505 static int rbuStepType(sqlite3rbu *p, const char **pzMask){
2506   int iCol = p->objiter.nCol;     /* Index of rbu_control column */
2507   int res = 0;                    /* Return value */
2508 
2509   switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
2510     case SQLITE_INTEGER: {
2511       int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
2512       if( iVal==0 ){
2513         res = RBU_INSERT;
2514       }else if( iVal==1 ){
2515         res = RBU_DELETE;
2516       }else if( iVal==2 ){
2517         res = RBU_IDX_DELETE;
2518       }else if( iVal==3 ){
2519         res = RBU_IDX_INSERT;
2520       }
2521       break;
2522     }
2523 
2524     case SQLITE_TEXT: {
2525       const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
2526       if( z==0 ){
2527         p->rc = SQLITE_NOMEM;
2528       }else{
2529         *pzMask = (const char*)z;
2530       }
2531       res = RBU_UPDATE;
2532 
2533       break;
2534     }
2535 
2536     default:
2537       break;
2538   }
2539 
2540   if( res==0 ){
2541     rbuBadControlError(p);
2542   }
2543   return res;
2544 }
2545 
2546 #ifdef SQLITE_DEBUG
2547 /*
2548 ** Assert that column iCol of statement pStmt is named zName.
2549 */
2550 static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
2551   const char *zCol = sqlite3_column_name(pStmt, iCol);
2552   assert( 0==sqlite3_stricmp(zName, zCol) );
2553 }
2554 #else
2555 # define assertColumnName(x,y,z)
2556 #endif
2557 
2558 /*
2559 ** This function does the work for an sqlite3rbu_step() call.
2560 **
2561 ** The object-iterator (p->objiter) currently points to a valid object,
2562 ** and the input cursor (p->objiter.pSelect) currently points to a valid
2563 ** input row. Perform whatever processing is required and return.
2564 **
2565 ** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
2566 ** and message is left in the RBU handle and a copy of the error code
2567 ** returned.
2568 */
2569 static int rbuStep(sqlite3rbu *p){
2570   RbuObjIter *pIter = &p->objiter;
2571   const char *zMask = 0;
2572   int i;
2573   int eType = rbuStepType(p, &zMask);
2574 
2575   if( eType ){
2576     assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
2577 
2578     if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
2579       rbuBadControlError(p);
2580     }
2581     else if(
2582         eType==RBU_INSERT
2583      || eType==RBU_DELETE
2584      || eType==RBU_IDX_DELETE
2585      || eType==RBU_IDX_INSERT
2586     ){
2587       sqlite3_value *pVal;
2588       sqlite3_stmt *pWriter;
2589 
2590       assert( eType!=RBU_UPDATE );
2591       assert( eType!=RBU_DELETE || pIter->zIdx==0 );
2592 
2593       if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
2594         pWriter = pIter->pDelete;
2595       }else{
2596         pWriter = pIter->pInsert;
2597       }
2598 
2599       for(i=0; i<pIter->nCol; i++){
2600         /* If this is an INSERT into a table b-tree and the table has an
2601         ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
2602         ** to write a NULL into the IPK column. That is not permitted.  */
2603         if( eType==RBU_INSERT
2604          && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
2605          && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
2606         ){
2607           p->rc = SQLITE_MISMATCH;
2608           p->zErrmsg = sqlite3_mprintf("datatype mismatch");
2609           goto step_out;
2610         }
2611 
2612         if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
2613           continue;
2614         }
2615 
2616         pVal = sqlite3_column_value(pIter->pSelect, i);
2617         p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
2618         if( p->rc ) goto step_out;
2619       }
2620       if( pIter->zIdx==0
2621        && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
2622       ){
2623         /* For a virtual table, or a table with no primary key, the
2624         ** SELECT statement is:
2625         **
2626         **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
2627         **
2628         ** Hence column_value(pIter->nCol+1).
2629         */
2630         assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
2631         pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
2632         p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
2633       }
2634       if( p->rc==SQLITE_OK ){
2635         sqlite3_step(pWriter);
2636         p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
2637       }
2638     }else{
2639       sqlite3_value *pVal;
2640       sqlite3_stmt *pUpdate = 0;
2641       assert( eType==RBU_UPDATE );
2642       rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
2643       if( pUpdate ){
2644         for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
2645           char c = zMask[pIter->aiSrcOrder[i]];
2646           pVal = sqlite3_column_value(pIter->pSelect, i);
2647           if( pIter->abTblPk[i] || c!='.' ){
2648             p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
2649           }
2650         }
2651         if( p->rc==SQLITE_OK
2652          && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
2653         ){
2654           /* Bind the rbu_rowid value to column _rowid_ */
2655           assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
2656           pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
2657           p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
2658         }
2659         if( p->rc==SQLITE_OK ){
2660           sqlite3_step(pUpdate);
2661           p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
2662         }
2663       }
2664     }
2665   }
2666 
2667  step_out:
2668   return p->rc;
2669 }
2670 
2671 /*
2672 ** Increment the schema cookie of the main database opened by p->dbMain.
2673 */
2674 static void rbuIncrSchemaCookie(sqlite3rbu *p){
2675   if( p->rc==SQLITE_OK ){
2676     int iCookie = 1000000;
2677     sqlite3_stmt *pStmt;
2678 
2679     p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
2680         "PRAGMA schema_version"
2681     );
2682     if( p->rc==SQLITE_OK ){
2683       /* Coverage: it may be that this sqlite3_step() cannot fail. There
2684       ** is already a transaction open, so the prepared statement cannot
2685       ** throw an SQLITE_SCHEMA exception. The only database page the
2686       ** statement reads is page 1, which is guaranteed to be in the cache.
2687       ** And no memory allocations are required.  */
2688       if( SQLITE_ROW==sqlite3_step(pStmt) ){
2689         iCookie = sqlite3_column_int(pStmt, 0);
2690       }
2691       rbuFinalize(p, pStmt);
2692     }
2693     if( p->rc==SQLITE_OK ){
2694       rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
2695     }
2696   }
2697 }
2698 
2699 /*
2700 ** Update the contents of the rbu_state table within the rbu database. The
2701 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
2702 ** are determined by inspecting the rbu handle passed as the first argument.
2703 */
2704 static void rbuSaveState(sqlite3rbu *p, int eStage){
2705   if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
2706     sqlite3_stmt *pInsert = 0;
2707     int rc;
2708 
2709     assert( p->zErrmsg==0 );
2710     rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
2711         sqlite3_mprintf(
2712           "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
2713           "(%d, %d), "
2714           "(%d, %Q), "
2715           "(%d, %Q), "
2716           "(%d, %d), "
2717           "(%d, %d), "
2718           "(%d, %lld), "
2719           "(%d, %lld), "
2720           "(%d, %lld) ",
2721           p->zStateDb,
2722           RBU_STATE_STAGE, eStage,
2723           RBU_STATE_TBL, p->objiter.zTbl,
2724           RBU_STATE_IDX, p->objiter.zIdx,
2725           RBU_STATE_ROW, p->nStep,
2726           RBU_STATE_PROGRESS, p->nProgress,
2727           RBU_STATE_CKPT, p->iWalCksum,
2728           RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
2729           RBU_STATE_OALSZ, p->iOalSz
2730       )
2731     );
2732     assert( pInsert==0 || rc==SQLITE_OK );
2733 
2734     if( rc==SQLITE_OK ){
2735       sqlite3_step(pInsert);
2736       rc = sqlite3_finalize(pInsert);
2737     }
2738     if( rc!=SQLITE_OK ) p->rc = rc;
2739   }
2740 }
2741 
2742 
2743 /*
2744 ** Step the RBU object.
2745 */
2746 int sqlite3rbu_step(sqlite3rbu *p){
2747   if( p ){
2748     switch( p->eStage ){
2749       case RBU_STAGE_OAL: {
2750         RbuObjIter *pIter = &p->objiter;
2751         while( p->rc==SQLITE_OK && pIter->zTbl ){
2752 
2753           if( pIter->bCleanup ){
2754             /* Clean up the rbu_tmp_xxx table for the previous table. It
2755             ** cannot be dropped as there are currently active SQL statements.
2756             ** But the contents can be deleted.  */
2757             if( pIter->abIndexed ){
2758               rbuMPrintfExec(p, p->dbRbu,
2759                   "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
2760               );
2761             }
2762           }else{
2763             rbuObjIterPrepareAll(p, pIter, 0);
2764 
2765             /* Advance to the next row to process. */
2766             if( p->rc==SQLITE_OK ){
2767               int rc = sqlite3_step(pIter->pSelect);
2768               if( rc==SQLITE_ROW ){
2769                 p->nProgress++;
2770                 p->nStep++;
2771                 return rbuStep(p);
2772               }
2773               p->rc = sqlite3_reset(pIter->pSelect);
2774               p->nStep = 0;
2775             }
2776           }
2777 
2778           rbuObjIterNext(p, pIter);
2779         }
2780 
2781         if( p->rc==SQLITE_OK ){
2782           assert( pIter->zTbl==0 );
2783           rbuSaveState(p, RBU_STAGE_MOVE);
2784           rbuIncrSchemaCookie(p);
2785           if( p->rc==SQLITE_OK ){
2786             p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
2787           }
2788           if( p->rc==SQLITE_OK ){
2789             p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
2790           }
2791           p->eStage = RBU_STAGE_MOVE;
2792         }
2793         break;
2794       }
2795 
2796       case RBU_STAGE_MOVE: {
2797         if( p->rc==SQLITE_OK ){
2798           rbuMoveOalFile(p);
2799           p->nProgress++;
2800         }
2801         break;
2802       }
2803 
2804       case RBU_STAGE_CKPT: {
2805         if( p->rc==SQLITE_OK ){
2806           if( p->nStep>=p->nFrame ){
2807             sqlite3_file *pDb = p->pTargetFd->pReal;
2808 
2809             /* Sync the db file */
2810             p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
2811 
2812             /* Update nBackfill */
2813             if( p->rc==SQLITE_OK ){
2814               void volatile *ptr;
2815               p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
2816               if( p->rc==SQLITE_OK ){
2817                 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
2818               }
2819             }
2820 
2821             if( p->rc==SQLITE_OK ){
2822               p->eStage = RBU_STAGE_DONE;
2823               p->rc = SQLITE_DONE;
2824             }
2825           }else{
2826             RbuFrame *pFrame = &p->aFrame[p->nStep];
2827             rbuCheckpointFrame(p, pFrame);
2828             p->nStep++;
2829           }
2830           p->nProgress++;
2831         }
2832         break;
2833       }
2834 
2835       default:
2836         break;
2837     }
2838     return p->rc;
2839   }else{
2840     return SQLITE_NOMEM;
2841   }
2842 }
2843 
2844 /*
2845 ** Free an RbuState object allocated by rbuLoadState().
2846 */
2847 static void rbuFreeState(RbuState *p){
2848   if( p ){
2849     sqlite3_free(p->zTbl);
2850     sqlite3_free(p->zIdx);
2851     sqlite3_free(p);
2852   }
2853 }
2854 
2855 /*
2856 ** Allocate an RbuState object and load the contents of the rbu_state
2857 ** table into it. Return a pointer to the new object. It is the
2858 ** responsibility of the caller to eventually free the object using
2859 ** sqlite3_free().
2860 **
2861 ** If an error occurs, leave an error code and message in the rbu handle
2862 ** and return NULL.
2863 */
2864 static RbuState *rbuLoadState(sqlite3rbu *p){
2865   RbuState *pRet = 0;
2866   sqlite3_stmt *pStmt = 0;
2867   int rc;
2868   int rc2;
2869 
2870   pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
2871   if( pRet==0 ) return 0;
2872 
2873   rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
2874       sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
2875   );
2876   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
2877     switch( sqlite3_column_int(pStmt, 0) ){
2878       case RBU_STATE_STAGE:
2879         pRet->eStage = sqlite3_column_int(pStmt, 1);
2880         if( pRet->eStage!=RBU_STAGE_OAL
2881          && pRet->eStage!=RBU_STAGE_MOVE
2882          && pRet->eStage!=RBU_STAGE_CKPT
2883         ){
2884           p->rc = SQLITE_CORRUPT;
2885         }
2886         break;
2887 
2888       case RBU_STATE_TBL:
2889         pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2890         break;
2891 
2892       case RBU_STATE_IDX:
2893         pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2894         break;
2895 
2896       case RBU_STATE_ROW:
2897         pRet->nRow = sqlite3_column_int(pStmt, 1);
2898         break;
2899 
2900       case RBU_STATE_PROGRESS:
2901         pRet->nProgress = sqlite3_column_int64(pStmt, 1);
2902         break;
2903 
2904       case RBU_STATE_CKPT:
2905         pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
2906         break;
2907 
2908       case RBU_STATE_COOKIE:
2909         pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
2910         break;
2911 
2912       case RBU_STATE_OALSZ:
2913         pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
2914         break;
2915 
2916       default:
2917         rc = SQLITE_CORRUPT;
2918         break;
2919     }
2920   }
2921   rc2 = sqlite3_finalize(pStmt);
2922   if( rc==SQLITE_OK ) rc = rc2;
2923 
2924   p->rc = rc;
2925   return pRet;
2926 }
2927 
2928 /*
2929 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
2930 ** otherwise. Either or both argument may be NULL. Two NULL values are
2931 ** considered equal, and NULL is considered distinct from all other values.
2932 */
2933 static int rbuStrCompare(const char *z1, const char *z2){
2934   if( z1==0 && z2==0 ) return 0;
2935   if( z1==0 || z2==0 ) return 1;
2936   return (sqlite3_stricmp(z1, z2)!=0);
2937 }
2938 
2939 /*
2940 ** This function is called as part of sqlite3rbu_open() when initializing
2941 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
2942 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
2943 ** things so that the next call to sqlite3rbu_step() continues on from
2944 ** where the previous rbu handle left off.
2945 **
2946 ** If an error occurs, an error code and error message are left in the
2947 ** rbu handle passed as the first argument.
2948 */
2949 static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
2950   assert( p->rc==SQLITE_OK );
2951   if( pState->zTbl ){
2952     RbuObjIter *pIter = &p->objiter;
2953     int rc = SQLITE_OK;
2954 
2955     while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
2956        || rbuStrCompare(pIter->zIdx, pState->zIdx)
2957        || rbuStrCompare(pIter->zTbl, pState->zTbl)
2958     )){
2959       rc = rbuObjIterNext(p, pIter);
2960     }
2961 
2962     if( rc==SQLITE_OK && !pIter->zTbl ){
2963       rc = SQLITE_ERROR;
2964       p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
2965     }
2966 
2967     if( rc==SQLITE_OK ){
2968       p->nStep = pState->nRow;
2969       rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
2970     }
2971 
2972     p->rc = rc;
2973   }
2974 }
2975 
2976 /*
2977 ** If there is a "*-oal" file in the file-system corresponding to the
2978 ** target database in the file-system, delete it. If an error occurs,
2979 ** leave an error code and error message in the rbu handle.
2980 */
2981 static void rbuDeleteOalFile(sqlite3rbu *p){
2982   char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
2983   if( zOal ){
2984     sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
2985     assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
2986     pVfs->xDelete(pVfs, zOal, 0);
2987     sqlite3_free(zOal);
2988   }
2989 }
2990 
2991 /*
2992 ** Allocate a private rbu VFS for the rbu handle passed as the only
2993 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
2994 ** specified a URI with a vfs=? option in place of a target database
2995 ** file name.
2996 */
2997 static void rbuCreateVfs(sqlite3rbu *p){
2998   int rnd;
2999   char zRnd[64];
3000 
3001   assert( p->rc==SQLITE_OK );
3002   sqlite3_randomness(sizeof(int), (void*)&rnd);
3003   sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
3004   p->rc = sqlite3rbu_create_vfs(zRnd, 0);
3005   if( p->rc==SQLITE_OK ){
3006     sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
3007     assert( pVfs );
3008     p->zVfsName = pVfs->zName;
3009   }
3010 }
3011 
3012 /*
3013 ** Destroy the private VFS created for the rbu handle passed as the only
3014 ** argument by an earlier call to rbuCreateVfs().
3015 */
3016 static void rbuDeleteVfs(sqlite3rbu *p){
3017   if( p->zVfsName ){
3018     sqlite3rbu_destroy_vfs(p->zVfsName);
3019     p->zVfsName = 0;
3020   }
3021 }
3022 
3023 /*
3024 ** Open and return a new RBU handle.
3025 */
3026 sqlite3rbu *sqlite3rbu_open(
3027   const char *zTarget,
3028   const char *zRbu,
3029   const char *zState
3030 ){
3031   sqlite3rbu *p;
3032   size_t nTarget = strlen(zTarget);
3033   size_t nRbu = strlen(zRbu);
3034   size_t nState = zState ? strlen(zState) : 0;
3035   size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1+ nState+1;
3036 
3037   p = (sqlite3rbu*)sqlite3_malloc64(nByte);
3038   if( p ){
3039     RbuState *pState = 0;
3040 
3041     /* Create the custom VFS. */
3042     memset(p, 0, sizeof(sqlite3rbu));
3043     rbuCreateVfs(p);
3044 
3045     /* Open the target database */
3046     if( p->rc==SQLITE_OK ){
3047       p->zTarget = (char*)&p[1];
3048       memcpy(p->zTarget, zTarget, nTarget+1);
3049       p->zRbu = &p->zTarget[nTarget+1];
3050       memcpy(p->zRbu, zRbu, nRbu+1);
3051       if( zState ){
3052         p->zState = &p->zRbu[nRbu+1];
3053         memcpy(p->zState, zState, nState+1);
3054       }
3055       rbuOpenDatabase(p);
3056     }
3057 
3058     /* If it has not already been created, create the rbu_state table */
3059     rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
3060 
3061     if( p->rc==SQLITE_OK ){
3062       pState = rbuLoadState(p);
3063       assert( pState || p->rc!=SQLITE_OK );
3064       if( p->rc==SQLITE_OK ){
3065 
3066         if( pState->eStage==0 ){
3067           rbuDeleteOalFile(p);
3068           p->eStage = RBU_STAGE_OAL;
3069         }else{
3070           p->eStage = pState->eStage;
3071         }
3072         p->nProgress = pState->nProgress;
3073         p->iOalSz = pState->iOalSz;
3074       }
3075     }
3076     assert( p->rc!=SQLITE_OK || p->eStage!=0 );
3077 
3078     if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
3079       if( p->eStage==RBU_STAGE_OAL ){
3080         p->rc = SQLITE_ERROR;
3081         p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
3082       }else if( p->eStage==RBU_STAGE_MOVE ){
3083         p->eStage = RBU_STAGE_CKPT;
3084         p->nStep = 0;
3085       }
3086     }
3087 
3088     if( p->rc==SQLITE_OK
3089      && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
3090      && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
3091     ){
3092       /* At this point (pTargetFd->iCookie) contains the value of the
3093       ** change-counter cookie (the thing that gets incremented when a
3094       ** transaction is committed in rollback mode) currently stored on
3095       ** page 1 of the database file. */
3096       p->rc = SQLITE_BUSY;
3097       p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
3098     }
3099 
3100     if( p->rc==SQLITE_OK ){
3101       if( p->eStage==RBU_STAGE_OAL ){
3102         sqlite3 *db = p->dbMain;
3103 
3104         /* Open transactions both databases. The *-oal file is opened or
3105         ** created at this point. */
3106         p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
3107         if( p->rc==SQLITE_OK ){
3108           p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
3109         }
3110 
3111         /* Check if the main database is a zipvfs db. If it is, set the upper
3112         ** level pager to use "journal_mode=off". This prevents it from
3113         ** generating a large journal using a temp file.  */
3114         if( p->rc==SQLITE_OK ){
3115           int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
3116           if( frc==SQLITE_OK ){
3117             p->rc = sqlite3_exec(db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
3118           }
3119         }
3120 
3121         /* Point the object iterator at the first object */
3122         if( p->rc==SQLITE_OK ){
3123           p->rc = rbuObjIterFirst(p, &p->objiter);
3124         }
3125 
3126         /* If the RBU database contains no data_xxx tables, declare the RBU
3127         ** update finished.  */
3128         if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
3129           p->rc = SQLITE_DONE;
3130         }
3131 
3132         if( p->rc==SQLITE_OK ){
3133           rbuSetupOal(p, pState);
3134         }
3135 
3136       }else if( p->eStage==RBU_STAGE_MOVE ){
3137         /* no-op */
3138       }else if( p->eStage==RBU_STAGE_CKPT ){
3139         rbuSetupCheckpoint(p, pState);
3140       }else if( p->eStage==RBU_STAGE_DONE ){
3141         p->rc = SQLITE_DONE;
3142       }else{
3143         p->rc = SQLITE_CORRUPT;
3144       }
3145     }
3146 
3147     rbuFreeState(pState);
3148   }
3149 
3150   return p;
3151 }
3152 
3153 
3154 /*
3155 ** Return the database handle used by pRbu.
3156 */
3157 sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
3158   sqlite3 *db = 0;
3159   if( pRbu ){
3160     db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
3161   }
3162   return db;
3163 }
3164 
3165 
3166 /*
3167 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
3168 ** then edit any error message string so as to remove all occurrences of
3169 ** the pattern "rbu_imp_[0-9]*".
3170 */
3171 static void rbuEditErrmsg(sqlite3rbu *p){
3172   if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
3173     int i;
3174     size_t nErrmsg = strlen(p->zErrmsg);
3175     for(i=0; i<(nErrmsg-8); i++){
3176       if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
3177         int nDel = 8;
3178         while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
3179         memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
3180         nErrmsg -= nDel;
3181       }
3182     }
3183   }
3184 }
3185 
3186 /*
3187 ** Close the RBU handle.
3188 */
3189 int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
3190   int rc;
3191   if( p ){
3192 
3193     /* Commit the transaction to the *-oal file. */
3194     if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
3195       p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
3196     }
3197 
3198     rbuSaveState(p, p->eStage);
3199 
3200     if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
3201       p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
3202     }
3203 
3204     /* Close any open statement handles. */
3205     rbuObjIterFinalize(&p->objiter);
3206 
3207     /* Close the open database handle and VFS object. */
3208     sqlite3_close(p->dbMain);
3209     sqlite3_close(p->dbRbu);
3210     rbuDeleteVfs(p);
3211     sqlite3_free(p->aBuf);
3212     sqlite3_free(p->aFrame);
3213 
3214     rbuEditErrmsg(p);
3215     rc = p->rc;
3216     *pzErrmsg = p->zErrmsg;
3217     sqlite3_free(p);
3218   }else{
3219     rc = SQLITE_NOMEM;
3220     *pzErrmsg = 0;
3221   }
3222   return rc;
3223 }
3224 
3225 /*
3226 ** Return the total number of key-value operations (inserts, deletes or
3227 ** updates) that have been performed on the target database since the
3228 ** current RBU update was started.
3229 */
3230 sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
3231   return pRbu->nProgress;
3232 }
3233 
3234 int sqlite3rbu_savestate(sqlite3rbu *p){
3235   int rc = p->rc;
3236 
3237   if( rc==SQLITE_DONE ) return SQLITE_OK;
3238 
3239   assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
3240   if( p->eStage==RBU_STAGE_OAL ){
3241     assert( rc!=SQLITE_DONE );
3242     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
3243   }
3244 
3245   p->rc = rc;
3246   rbuSaveState(p, p->eStage);
3247   rc = p->rc;
3248 
3249   if( p->eStage==RBU_STAGE_OAL ){
3250     assert( rc!=SQLITE_DONE );
3251     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
3252     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
3253     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
3254   }
3255 
3256   p->rc = rc;
3257   return rc;
3258 }
3259 
3260 /**************************************************************************
3261 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
3262 ** of a standard VFS in the following ways:
3263 **
3264 ** 1. Whenever the first page of a main database file is read or
3265 **    written, the value of the change-counter cookie is stored in
3266 **    rbu_file.iCookie. Similarly, the value of the "write-version"
3267 **    database header field is stored in rbu_file.iWriteVer. This ensures
3268 **    that the values are always trustworthy within an open transaction.
3269 **
3270 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
3271 **    member variable of the associated database file descriptor is set
3272 **    to point to the new file. A mutex protected linked list of all main
3273 **    db fds opened using a particular RBU VFS is maintained at
3274 **    rbu_vfs.pMain to facilitate this.
3275 **
3276 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
3277 **    object can be marked as the target database of an RBU update. This
3278 **    turns on the following extra special behaviour:
3279 **
3280 ** 3a. If xAccess() is called to check if there exists a *-wal file
3281 **     associated with an RBU target database currently in RBU_STAGE_OAL
3282 **     stage (preparing the *-oal file), the following special handling
3283 **     applies:
3284 **
3285 **      * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
3286 **        target database may not be in wal mode already.
3287 **
3288 **      * if the *-wal file does not exist, set the output parameter to
3289 **        non-zero (to tell SQLite that it does exist) anyway.
3290 **
3291 **     Then, when xOpen() is called to open the *-wal file associated with
3292 **     the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
3293 **     file, the rbu vfs opens the corresponding *-oal file instead.
3294 **
3295 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
3296 **     RBU_STAGE_OAL mode are actually stored in heap memory. This is to
3297 **     avoid creating a *-shm file on disk. Additionally, xShmLock() calls
3298 **     are no-ops on target database files in RBU_STAGE_OAL mode. This is
3299 **     because assert() statements in some VFS implementations fail if
3300 **     xShmLock() is called before xShmMap().
3301 **
3302 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
3303 **     mode except RBU_STAGE_DONE (all work completed and checkpointed), it
3304 **     fails with an SQLITE_BUSY error. This is to stop RBU connections
3305 **     from automatically checkpointing a *-wal (or *-oal) file from within
3306 **     sqlite3_close().
3307 **
3308 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
3309 **     all xWrite() calls on the target database file perform no IO.
3310 **     Instead the frame and page numbers that would be read and written
3311 **     are recorded. Additionally, successful attempts to obtain exclusive
3312 **     xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
3313 **     database file are recorded. xShmLock() calls to unlock the same
3314 **     locks are no-ops (so that once obtained, these locks are never
3315 **     relinquished). Finally, calls to xSync() on the target database
3316 **     file fail with SQLITE_INTERNAL errors.
3317 */
3318 
3319 static void rbuUnlockShm(rbu_file *p){
3320   if( p->pRbu ){
3321     int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
3322     int i;
3323     for(i=0; i<SQLITE_SHM_NLOCK;i++){
3324       if( (1<<i) & p->pRbu->mLock ){
3325         xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
3326       }
3327     }
3328     p->pRbu->mLock = 0;
3329   }
3330 }
3331 
3332 /*
3333 ** Close an rbu file.
3334 */
3335 static int rbuVfsClose(sqlite3_file *pFile){
3336   rbu_file *p = (rbu_file*)pFile;
3337   int rc;
3338   int i;
3339 
3340   /* Free the contents of the apShm[] array. And the array itself. */
3341   for(i=0; i<p->nShm; i++){
3342     sqlite3_free(p->apShm[i]);
3343   }
3344   sqlite3_free(p->apShm);
3345   p->apShm = 0;
3346   sqlite3_free(p->zDel);
3347 
3348   if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
3349     rbu_file **pp;
3350     sqlite3_mutex_enter(p->pRbuVfs->mutex);
3351     for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
3352     *pp = p->pMainNext;
3353     sqlite3_mutex_leave(p->pRbuVfs->mutex);
3354     rbuUnlockShm(p);
3355     p->pReal->pMethods->xShmUnmap(p->pReal, 0);
3356   }
3357 
3358   /* Close the underlying file handle */
3359   rc = p->pReal->pMethods->xClose(p->pReal);
3360   return rc;
3361 }
3362 
3363 
3364 /*
3365 ** Read and return an unsigned 32-bit big-endian integer from the buffer
3366 ** passed as the only argument.
3367 */
3368 static u32 rbuGetU32(u8 *aBuf){
3369   return ((u32)aBuf[0] << 24)
3370        + ((u32)aBuf[1] << 16)
3371        + ((u32)aBuf[2] <<  8)
3372        + ((u32)aBuf[3]);
3373 }
3374 
3375 /*
3376 ** Read data from an rbuVfs-file.
3377 */
3378 static int rbuVfsRead(
3379   sqlite3_file *pFile,
3380   void *zBuf,
3381   int iAmt,
3382   sqlite_int64 iOfst
3383 ){
3384   rbu_file *p = (rbu_file*)pFile;
3385   sqlite3rbu *pRbu = p->pRbu;
3386   int rc;
3387 
3388   if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
3389     assert( p->openFlags & SQLITE_OPEN_WAL );
3390     rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
3391   }else{
3392     if( pRbu && pRbu->eStage==RBU_STAGE_OAL
3393      && (p->openFlags & SQLITE_OPEN_WAL)
3394      && iOfst>=pRbu->iOalSz
3395     ){
3396       rc = SQLITE_OK;
3397       memset(zBuf, 0, iAmt);
3398     }else{
3399       rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
3400     }
3401     if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
3402       /* These look like magic numbers. But they are stable, as they are part
3403        ** of the definition of the SQLite file format, which may not change. */
3404       u8 *pBuf = (u8*)zBuf;
3405       p->iCookie = rbuGetU32(&pBuf[24]);
3406       p->iWriteVer = pBuf[19];
3407     }
3408   }
3409   return rc;
3410 }
3411 
3412 /*
3413 ** Write data to an rbuVfs-file.
3414 */
3415 static int rbuVfsWrite(
3416   sqlite3_file *pFile,
3417   const void *zBuf,
3418   int iAmt,
3419   sqlite_int64 iOfst
3420 ){
3421   rbu_file *p = (rbu_file*)pFile;
3422   sqlite3rbu *pRbu = p->pRbu;
3423   int rc;
3424 
3425   if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
3426     assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
3427     rc = rbuCaptureDbWrite(p->pRbu, iOfst);
3428   }else{
3429     if( pRbu && pRbu->eStage==RBU_STAGE_OAL
3430      && (p->openFlags & SQLITE_OPEN_WAL)
3431      && iOfst>=pRbu->iOalSz
3432     ){
3433       pRbu->iOalSz = iAmt + iOfst;
3434     }
3435     rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
3436     if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
3437       /* These look like magic numbers. But they are stable, as they are part
3438       ** of the definition of the SQLite file format, which may not change. */
3439       u8 *pBuf = (u8*)zBuf;
3440       p->iCookie = rbuGetU32(&pBuf[24]);
3441       p->iWriteVer = pBuf[19];
3442     }
3443   }
3444   return rc;
3445 }
3446 
3447 /*
3448 ** Truncate an rbuVfs-file.
3449 */
3450 static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
3451   rbu_file *p = (rbu_file*)pFile;
3452   return p->pReal->pMethods->xTruncate(p->pReal, size);
3453 }
3454 
3455 /*
3456 ** Sync an rbuVfs-file.
3457 */
3458 static int rbuVfsSync(sqlite3_file *pFile, int flags){
3459   rbu_file *p = (rbu_file *)pFile;
3460   if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
3461     if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
3462       return SQLITE_INTERNAL;
3463     }
3464     return SQLITE_OK;
3465   }
3466   return p->pReal->pMethods->xSync(p->pReal, flags);
3467 }
3468 
3469 /*
3470 ** Return the current file-size of an rbuVfs-file.
3471 */
3472 static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3473   rbu_file *p = (rbu_file *)pFile;
3474   return p->pReal->pMethods->xFileSize(p->pReal, pSize);
3475 }
3476 
3477 /*
3478 ** Lock an rbuVfs-file.
3479 */
3480 static int rbuVfsLock(sqlite3_file *pFile, int eLock){
3481   rbu_file *p = (rbu_file*)pFile;
3482   sqlite3rbu *pRbu = p->pRbu;
3483   int rc = SQLITE_OK;
3484 
3485   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
3486   if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
3487     /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
3488     ** prevents it from checkpointing the database from sqlite3_close(). */
3489     rc = SQLITE_BUSY;
3490   }else{
3491     rc = p->pReal->pMethods->xLock(p->pReal, eLock);
3492   }
3493 
3494   return rc;
3495 }
3496 
3497 /*
3498 ** Unlock an rbuVfs-file.
3499 */
3500 static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
3501   rbu_file *p = (rbu_file *)pFile;
3502   return p->pReal->pMethods->xUnlock(p->pReal, eLock);
3503 }
3504 
3505 /*
3506 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
3507 */
3508 static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3509   rbu_file *p = (rbu_file *)pFile;
3510   return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
3511 }
3512 
3513 /*
3514 ** File control method. For custom operations on an rbuVfs-file.
3515 */
3516 static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
3517   rbu_file *p = (rbu_file *)pFile;
3518   int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
3519   int rc;
3520 
3521   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
3522        || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
3523   );
3524   if( op==SQLITE_FCNTL_RBU ){
3525     sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
3526 
3527     /* First try to find another RBU vfs lower down in the vfs stack. If
3528     ** one is found, this vfs will operate in pass-through mode. The lower
3529     ** level vfs will do the special RBU handling.  */
3530     rc = xControl(p->pReal, op, pArg);
3531 
3532     if( rc==SQLITE_NOTFOUND ){
3533       /* Now search for a zipvfs instance lower down in the VFS stack. If
3534       ** one is found, this is an error.  */
3535       void *dummy = 0;
3536       rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
3537       if( rc==SQLITE_OK ){
3538         rc = SQLITE_ERROR;
3539         pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
3540       }else if( rc==SQLITE_NOTFOUND ){
3541         pRbu->pTargetFd = p;
3542         p->pRbu = pRbu;
3543         if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
3544         rc = SQLITE_OK;
3545       }
3546     }
3547     return rc;
3548   }
3549 
3550   rc = xControl(p->pReal, op, pArg);
3551   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3552     rbu_vfs *pRbuVfs = p->pRbuVfs;
3553     char *zIn = *(char**)pArg;
3554     char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
3555     *(char**)pArg = zOut;
3556     if( zOut==0 ) rc = SQLITE_NOMEM;
3557   }
3558 
3559   return rc;
3560 }
3561 
3562 /*
3563 ** Return the sector-size in bytes for an rbuVfs-file.
3564 */
3565 static int rbuVfsSectorSize(sqlite3_file *pFile){
3566   rbu_file *p = (rbu_file *)pFile;
3567   return p->pReal->pMethods->xSectorSize(p->pReal);
3568 }
3569 
3570 /*
3571 ** Return the device characteristic flags supported by an rbuVfs-file.
3572 */
3573 static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
3574   rbu_file *p = (rbu_file *)pFile;
3575   return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
3576 }
3577 
3578 /*
3579 ** Take or release a shared-memory lock.
3580 */
3581 static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
3582   rbu_file *p = (rbu_file*)pFile;
3583   sqlite3rbu *pRbu = p->pRbu;
3584   int rc = SQLITE_OK;
3585 
3586 #ifdef SQLITE_AMALGAMATION
3587     assert( WAL_CKPT_LOCK==1 );
3588 #endif
3589 
3590   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
3591   if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
3592     /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
3593     ** taking this lock also prevents any checkpoints from occurring.
3594     ** todo: really, it's not clear why this might occur, as
3595     ** wal_autocheckpoint ought to be turned off.  */
3596     if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
3597   }else{
3598     int bCapture = 0;
3599     if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
3600      && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
3601      && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
3602     ){
3603       bCapture = 1;
3604     }
3605 
3606     if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
3607       rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
3608       if( bCapture && rc==SQLITE_OK ){
3609         pRbu->mLock |= (1 << ofst);
3610       }
3611     }
3612   }
3613 
3614   return rc;
3615 }
3616 
3617 /*
3618 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
3619 */
3620 static int rbuVfsShmMap(
3621   sqlite3_file *pFile,
3622   int iRegion,
3623   int szRegion,
3624   int isWrite,
3625   void volatile **pp
3626 ){
3627   rbu_file *p = (rbu_file*)pFile;
3628   int rc = SQLITE_OK;
3629   int eStage = (p->pRbu ? p->pRbu->eStage : 0);
3630 
3631   /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
3632   ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
3633   ** instead of a file on disk.  */
3634   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
3635   if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
3636     if( iRegion<=p->nShm ){
3637       int nByte = (iRegion+1) * sizeof(char*);
3638       char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
3639       if( apNew==0 ){
3640         rc = SQLITE_NOMEM;
3641       }else{
3642         memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
3643         p->apShm = apNew;
3644         p->nShm = iRegion+1;
3645       }
3646     }
3647 
3648     if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
3649       char *pNew = (char*)sqlite3_malloc64(szRegion);
3650       if( pNew==0 ){
3651         rc = SQLITE_NOMEM;
3652       }else{
3653         memset(pNew, 0, szRegion);
3654         p->apShm[iRegion] = pNew;
3655       }
3656     }
3657 
3658     if( rc==SQLITE_OK ){
3659       *pp = p->apShm[iRegion];
3660     }else{
3661       *pp = 0;
3662     }
3663   }else{
3664     assert( p->apShm==0 );
3665     rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
3666   }
3667 
3668   return rc;
3669 }
3670 
3671 /*
3672 ** Memory barrier.
3673 */
3674 static void rbuVfsShmBarrier(sqlite3_file *pFile){
3675   rbu_file *p = (rbu_file *)pFile;
3676   p->pReal->pMethods->xShmBarrier(p->pReal);
3677 }
3678 
3679 /*
3680 ** The xShmUnmap method.
3681 */
3682 static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
3683   rbu_file *p = (rbu_file*)pFile;
3684   int rc = SQLITE_OK;
3685   int eStage = (p->pRbu ? p->pRbu->eStage : 0);
3686 
3687   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
3688   if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
3689     /* no-op */
3690   }else{
3691     /* Release the checkpointer and writer locks */
3692     rbuUnlockShm(p);
3693     rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
3694   }
3695   return rc;
3696 }
3697 
3698 /*
3699 ** Given that zWal points to a buffer containing a wal file name passed to
3700 ** either the xOpen() or xAccess() VFS method, return a pointer to the
3701 ** file-handle opened by the same database connection on the corresponding
3702 ** database file.
3703 */
3704 static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
3705   rbu_file *pDb;
3706   sqlite3_mutex_enter(pRbuVfs->mutex);
3707   for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
3708   sqlite3_mutex_leave(pRbuVfs->mutex);
3709   return pDb;
3710 }
3711 
3712 /*
3713 ** Open an rbu file handle.
3714 */
3715 static int rbuVfsOpen(
3716   sqlite3_vfs *pVfs,
3717   const char *zName,
3718   sqlite3_file *pFile,
3719   int flags,
3720   int *pOutFlags
3721 ){
3722   static sqlite3_io_methods rbuvfs_io_methods = {
3723     2,                            /* iVersion */
3724     rbuVfsClose,                  /* xClose */
3725     rbuVfsRead,                   /* xRead */
3726     rbuVfsWrite,                  /* xWrite */
3727     rbuVfsTruncate,               /* xTruncate */
3728     rbuVfsSync,                   /* xSync */
3729     rbuVfsFileSize,               /* xFileSize */
3730     rbuVfsLock,                   /* xLock */
3731     rbuVfsUnlock,                 /* xUnlock */
3732     rbuVfsCheckReservedLock,      /* xCheckReservedLock */
3733     rbuVfsFileControl,            /* xFileControl */
3734     rbuVfsSectorSize,             /* xSectorSize */
3735     rbuVfsDeviceCharacteristics,  /* xDeviceCharacteristics */
3736     rbuVfsShmMap,                 /* xShmMap */
3737     rbuVfsShmLock,                /* xShmLock */
3738     rbuVfsShmBarrier,             /* xShmBarrier */
3739     rbuVfsShmUnmap,               /* xShmUnmap */
3740     0, 0                          /* xFetch, xUnfetch */
3741   };
3742   rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
3743   sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
3744   rbu_file *pFd = (rbu_file *)pFile;
3745   int rc = SQLITE_OK;
3746   const char *zOpen = zName;
3747 
3748   memset(pFd, 0, sizeof(rbu_file));
3749   pFd->pReal = (sqlite3_file*)&pFd[1];
3750   pFd->pRbuVfs = pRbuVfs;
3751   pFd->openFlags = flags;
3752   if( zName ){
3753     if( flags & SQLITE_OPEN_MAIN_DB ){
3754       /* A main database has just been opened. The following block sets
3755       ** (pFd->zWal) to point to a buffer owned by SQLite that contains
3756       ** the name of the *-wal file this db connection will use. SQLite
3757       ** happens to pass a pointer to this buffer when using xAccess()
3758       ** or xOpen() to operate on the *-wal file.  */
3759       int n = (int)strlen(zName);
3760       const char *z = &zName[n];
3761       if( flags & SQLITE_OPEN_URI ){
3762         int odd = 0;
3763         while( 1 ){
3764           if( z[0]==0 ){
3765             odd = 1 - odd;
3766             if( odd && z[1]==0 ) break;
3767           }
3768           z++;
3769         }
3770         z += 2;
3771       }else{
3772         while( *z==0 ) z++;
3773       }
3774       z += (n + 8 + 1);
3775       pFd->zWal = z;
3776     }
3777     else if( flags & SQLITE_OPEN_WAL ){
3778       rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
3779       if( pDb ){
3780         if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
3781           /* This call is to open a *-wal file. Intead, open the *-oal. This
3782           ** code ensures that the string passed to xOpen() is terminated by a
3783           ** pair of '\0' bytes in case the VFS attempts to extract a URI
3784           ** parameter from it.  */
3785           size_t nCopy = strlen(zName);
3786           char *zCopy = sqlite3_malloc64(nCopy+2);
3787           if( zCopy ){
3788             memcpy(zCopy, zName, nCopy);
3789             zCopy[nCopy-3] = 'o';
3790             zCopy[nCopy] = '\0';
3791             zCopy[nCopy+1] = '\0';
3792             zOpen = (const char*)(pFd->zDel = zCopy);
3793           }else{
3794             rc = SQLITE_NOMEM;
3795           }
3796           pFd->pRbu = pDb->pRbu;
3797         }
3798         pDb->pWalFd = pFd;
3799       }
3800     }
3801   }
3802 
3803   if( rc==SQLITE_OK ){
3804     rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, flags, pOutFlags);
3805   }
3806   if( pFd->pReal->pMethods ){
3807     /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
3808     ** pointer and, if the file is a main database file, link it into the
3809     ** mutex protected linked list of all such files.  */
3810     pFile->pMethods = &rbuvfs_io_methods;
3811     if( flags & SQLITE_OPEN_MAIN_DB ){
3812       sqlite3_mutex_enter(pRbuVfs->mutex);
3813       pFd->pMainNext = pRbuVfs->pMain;
3814       pRbuVfs->pMain = pFd;
3815       sqlite3_mutex_leave(pRbuVfs->mutex);
3816     }
3817   }else{
3818     sqlite3_free(pFd->zDel);
3819   }
3820 
3821   return rc;
3822 }
3823 
3824 /*
3825 ** Delete the file located at zPath.
3826 */
3827 static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3828   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3829   return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
3830 }
3831 
3832 /*
3833 ** Test for access permissions. Return true if the requested permission
3834 ** is available, or false otherwise.
3835 */
3836 static int rbuVfsAccess(
3837   sqlite3_vfs *pVfs,
3838   const char *zPath,
3839   int flags,
3840   int *pResOut
3841 ){
3842   rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
3843   sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
3844   int rc;
3845 
3846   rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
3847 
3848   /* If this call is to check if a *-wal file associated with an RBU target
3849   ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
3850   ** the following special handling is activated:
3851   **
3852   **   a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
3853   **      ensures that the RBU extension never tries to update a database
3854   **      in wal mode, even if the first page of the database file has
3855   **      been damaged.
3856   **
3857   **   b) if the *-wal file does not exist, claim that it does anyway,
3858   **      causing SQLite to call xOpen() to open it. This call will also
3859   **      be intercepted (see the rbuVfsOpen() function) and the *-oal
3860   **      file opened instead.
3861   */
3862   if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
3863     rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
3864     if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
3865       if( *pResOut ){
3866         rc = SQLITE_CANTOPEN;
3867       }else{
3868         *pResOut = 1;
3869       }
3870     }
3871   }
3872 
3873   return rc;
3874 }
3875 
3876 /*
3877 ** Populate buffer zOut with the full canonical pathname corresponding
3878 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
3879 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
3880 */
3881 static int rbuVfsFullPathname(
3882   sqlite3_vfs *pVfs,
3883   const char *zPath,
3884   int nOut,
3885   char *zOut
3886 ){
3887   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3888   return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
3889 }
3890 
3891 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3892 /*
3893 ** Open the dynamic library located at zPath and return a handle.
3894 */
3895 static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3896   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3897   return pRealVfs->xDlOpen(pRealVfs, zPath);
3898 }
3899 
3900 /*
3901 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
3902 ** utf-8 string describing the most recent error encountered associated
3903 ** with dynamic libraries.
3904 */
3905 static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3906   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3907   pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
3908 }
3909 
3910 /*
3911 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
3912 */
3913 static void (*rbuVfsDlSym(
3914   sqlite3_vfs *pVfs,
3915   void *pArg,
3916   const char *zSym
3917 ))(void){
3918   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3919   return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
3920 }
3921 
3922 /*
3923 ** Close the dynamic library handle pHandle.
3924 */
3925 static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
3926   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3927   pRealVfs->xDlClose(pRealVfs, pHandle);
3928 }
3929 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
3930 
3931 /*
3932 ** Populate the buffer pointed to by zBufOut with nByte bytes of
3933 ** random data.
3934 */
3935 static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
3936   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3937   return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
3938 }
3939 
3940 /*
3941 ** Sleep for nMicro microseconds. Return the number of microseconds
3942 ** actually slept.
3943 */
3944 static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
3945   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3946   return pRealVfs->xSleep(pRealVfs, nMicro);
3947 }
3948 
3949 /*
3950 ** Return the current time as a Julian Day number in *pTimeOut.
3951 */
3952 static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
3953   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
3954   return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
3955 }
3956 
3957 /*
3958 ** No-op.
3959 */
3960 static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
3961   return 0;
3962 }
3963 
3964 /*
3965 ** Deregister and destroy an RBU vfs created by an earlier call to
3966 ** sqlite3rbu_create_vfs().
3967 */
3968 void sqlite3rbu_destroy_vfs(const char *zName){
3969   sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
3970   if( pVfs && pVfs->xOpen==rbuVfsOpen ){
3971     sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
3972     sqlite3_vfs_unregister(pVfs);
3973     sqlite3_free(pVfs);
3974   }
3975 }
3976 
3977 /*
3978 ** Create an RBU VFS named zName that accesses the underlying file-system
3979 ** via existing VFS zParent. The new object is registered as a non-default
3980 ** VFS with SQLite before returning.
3981 */
3982 int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
3983 
3984   /* Template for VFS */
3985   static sqlite3_vfs vfs_template = {
3986     1,                            /* iVersion */
3987     0,                            /* szOsFile */
3988     0,                            /* mxPathname */
3989     0,                            /* pNext */
3990     0,                            /* zName */
3991     0,                            /* pAppData */
3992     rbuVfsOpen,                   /* xOpen */
3993     rbuVfsDelete,                 /* xDelete */
3994     rbuVfsAccess,                 /* xAccess */
3995     rbuVfsFullPathname,           /* xFullPathname */
3996 
3997 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3998     rbuVfsDlOpen,                 /* xDlOpen */
3999     rbuVfsDlError,                /* xDlError */
4000     rbuVfsDlSym,                  /* xDlSym */
4001     rbuVfsDlClose,                /* xDlClose */
4002 #else
4003     0, 0, 0, 0,
4004 #endif
4005 
4006     rbuVfsRandomness,             /* xRandomness */
4007     rbuVfsSleep,                  /* xSleep */
4008     rbuVfsCurrentTime,            /* xCurrentTime */
4009     rbuVfsGetLastError,           /* xGetLastError */
4010     0,                            /* xCurrentTimeInt64 (version 2) */
4011     0, 0, 0                       /* Unimplemented version 3 methods */
4012   };
4013 
4014   rbu_vfs *pNew = 0;              /* Newly allocated VFS */
4015   int rc = SQLITE_OK;
4016   size_t nName;
4017   size_t nByte;
4018 
4019   nName = strlen(zName);
4020   nByte = sizeof(rbu_vfs) + nName + 1;
4021   pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
4022   if( pNew==0 ){
4023     rc = SQLITE_NOMEM;
4024   }else{
4025     sqlite3_vfs *pParent;           /* Parent VFS */
4026     memset(pNew, 0, nByte);
4027     pParent = sqlite3_vfs_find(zParent);
4028     if( pParent==0 ){
4029       rc = SQLITE_NOTFOUND;
4030     }else{
4031       char *zSpace;
4032       memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
4033       pNew->base.mxPathname = pParent->mxPathname;
4034       pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
4035       pNew->pRealVfs = pParent;
4036       pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
4037       memcpy(zSpace, zName, nName);
4038 
4039       /* Allocate the mutex and register the new VFS (not as the default) */
4040       pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
4041       if( pNew->mutex==0 ){
4042         rc = SQLITE_NOMEM;
4043       }else{
4044         rc = sqlite3_vfs_register(&pNew->base, 0);
4045       }
4046     }
4047 
4048     if( rc!=SQLITE_OK ){
4049       sqlite3_mutex_free(pNew->mutex);
4050       sqlite3_free(pNew);
4051     }
4052   }
4053 
4054   return rc;
4055 }
4056 
4057 
4058 /**************************************************************************/
4059 
4060 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
4061