xref: /sqlite-3.40.0/ext/rbu/sqlite3rbu.c (revision 50ba1a02)
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 /* Delta checksums disabled by default.  Compile with -DRBU_ENABLE_DELTA_CKSUM
100 ** to enable checksum verification.
101 */
102 #ifndef RBU_ENABLE_DELTA_CKSUM
103 # define RBU_ENABLE_DELTA_CKSUM 0
104 #endif
105 
106 /*
107 ** Swap two objects of type TYPE.
108 */
109 #if !defined(SQLITE_AMALGAMATION)
110 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
111 #endif
112 
113 /*
114 ** Name of the URI option that causes RBU to take an exclusive lock as
115 ** part of the incremental checkpoint operation.
116 */
117 #define RBU_EXCLUSIVE_CHECKPOINT "rbu_exclusive_checkpoint"
118 
119 
120 /*
121 ** The rbu_state table is used to save the state of a partially applied
122 ** update so that it can be resumed later. The table consists of integer
123 ** keys mapped to values as follows:
124 **
125 ** RBU_STATE_STAGE:
126 **   May be set to integer values 1, 2, 4 or 5. As follows:
127 **       1: the *-rbu file is currently under construction.
128 **       2: the *-rbu file has been constructed, but not yet moved
129 **          to the *-wal path.
130 **       4: the checkpoint is underway.
131 **       5: the rbu update has been checkpointed.
132 **
133 ** RBU_STATE_TBL:
134 **   Only valid if STAGE==1. The target database name of the table
135 **   currently being written.
136 **
137 ** RBU_STATE_IDX:
138 **   Only valid if STAGE==1. The target database name of the index
139 **   currently being written, or NULL if the main table is currently being
140 **   updated.
141 **
142 ** RBU_STATE_ROW:
143 **   Only valid if STAGE==1. Number of rows already processed for the current
144 **   table/index.
145 **
146 ** RBU_STATE_PROGRESS:
147 **   Trbul number of sqlite3rbu_step() calls made so far as part of this
148 **   rbu update.
149 **
150 ** RBU_STATE_CKPT:
151 **   Valid if STAGE==4. The 64-bit checksum associated with the wal-index
152 **   header created by recovering the *-wal file. This is used to detect
153 **   cases when another client appends frames to the *-wal file in the
154 **   middle of an incremental checkpoint (an incremental checkpoint cannot
155 **   be continued if this happens).
156 **
157 ** RBU_STATE_COOKIE:
158 **   Valid if STAGE==1. The current change-counter cookie value in the
159 **   target db file.
160 **
161 ** RBU_STATE_OALSZ:
162 **   Valid if STAGE==1. The size in bytes of the *-oal file.
163 **
164 ** RBU_STATE_DATATBL:
165 **   Only valid if STAGE==1. The RBU database name of the table
166 **   currently being read.
167 */
168 #define RBU_STATE_STAGE        1
169 #define RBU_STATE_TBL          2
170 #define RBU_STATE_IDX          3
171 #define RBU_STATE_ROW          4
172 #define RBU_STATE_PROGRESS     5
173 #define RBU_STATE_CKPT         6
174 #define RBU_STATE_COOKIE       7
175 #define RBU_STATE_OALSZ        8
176 #define RBU_STATE_PHASEONESTEP 9
177 #define RBU_STATE_DATATBL     10
178 
179 #define RBU_STAGE_OAL         1
180 #define RBU_STAGE_MOVE        2
181 #define RBU_STAGE_CAPTURE     3
182 #define RBU_STAGE_CKPT        4
183 #define RBU_STAGE_DONE        5
184 
185 
186 #define RBU_CREATE_STATE \
187   "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
188 
189 typedef struct RbuFrame RbuFrame;
190 typedef struct RbuObjIter RbuObjIter;
191 typedef struct RbuState RbuState;
192 typedef struct RbuSpan RbuSpan;
193 typedef struct rbu_vfs rbu_vfs;
194 typedef struct rbu_file rbu_file;
195 typedef struct RbuUpdateStmt RbuUpdateStmt;
196 
197 #if !defined(SQLITE_AMALGAMATION)
198 typedef unsigned int u32;
199 typedef unsigned short u16;
200 typedef unsigned char u8;
201 typedef sqlite3_int64 i64;
202 #endif
203 
204 /*
205 ** These values must match the values defined in wal.c for the equivalent
206 ** locks. These are not magic numbers as they are part of the SQLite file
207 ** format.
208 */
209 #define WAL_LOCK_WRITE  0
210 #define WAL_LOCK_CKPT   1
211 #define WAL_LOCK_READ0  3
212 
213 #define SQLITE_FCNTL_RBUCNT    5149216
214 
215 /*
216 ** A structure to store values read from the rbu_state table in memory.
217 */
218 struct RbuState {
219   int eStage;
220   char *zTbl;
221   char *zDataTbl;
222   char *zIdx;
223   i64 iWalCksum;
224   int nRow;
225   i64 nProgress;
226   u32 iCookie;
227   i64 iOalSz;
228   i64 nPhaseOneStep;
229 };
230 
231 struct RbuUpdateStmt {
232   char *zMask;                    /* Copy of update mask used with pUpdate */
233   sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
234   RbuUpdateStmt *pNext;
235 };
236 
237 struct RbuSpan {
238   const char *zSpan;
239   int nSpan;
240 };
241 
242 /*
243 ** An iterator of this type is used to iterate through all objects in
244 ** the target database that require updating. For each such table, the
245 ** iterator visits, in order:
246 **
247 **     * the table itself,
248 **     * each index of the table (zero or more points to visit), and
249 **     * a special "cleanup table" state.
250 **
251 ** abIndexed:
252 **   If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
253 **   it points to an array of flags nTblCol elements in size. The flag is
254 **   set for each column that is either a part of the PK or a part of an
255 **   index. Or clear otherwise.
256 **
257 **   If there are one or more partial indexes on the table, all fields of
258 **   this array set set to 1. This is because in that case, the module has
259 **   no way to tell which fields will be required to add and remove entries
260 **   from the partial indexes.
261 **
262 */
263 struct RbuObjIter {
264   sqlite3_stmt *pTblIter;         /* Iterate through tables */
265   sqlite3_stmt *pIdxIter;         /* Index iterator */
266   int nTblCol;                    /* Size of azTblCol[] array */
267   char **azTblCol;                /* Array of unquoted target column names */
268   char **azTblType;               /* Array of target column types */
269   int *aiSrcOrder;                /* src table col -> target table col */
270   u8 *abTblPk;                    /* Array of flags, set on target PK columns */
271   u8 *abNotNull;                  /* Array of flags, set on NOT NULL columns */
272   u8 *abIndexed;                  /* Array of flags, set on indexed & PK cols */
273   int eType;                      /* Table type - an RBU_PK_XXX value */
274 
275   /* Output variables. zTbl==0 implies EOF. */
276   int bCleanup;                   /* True in "cleanup" state */
277   const char *zTbl;               /* Name of target db table */
278   const char *zDataTbl;           /* Name of rbu db table (or null) */
279   const char *zIdx;               /* Name of target db index (or null) */
280   int iTnum;                      /* Root page of current object */
281   int iPkTnum;                    /* If eType==EXTERNAL, root of PK index */
282   int bUnique;                    /* Current index is unique */
283   int nIndex;                     /* Number of aux. indexes on table zTbl */
284 
285   /* Statements created by rbuObjIterPrepareAll() */
286   int nCol;                       /* Number of columns in current object */
287   sqlite3_stmt *pSelect;          /* Source data */
288   sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
289   sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
290   sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zDataTbl */
291   int nIdxCol;
292   RbuSpan *aIdxCol;
293   char *zIdxSql;
294 
295   /* Last UPDATE used (for PK b-tree updates only), or NULL. */
296   RbuUpdateStmt *pRbuUpdate;
297 };
298 
299 /*
300 ** Values for RbuObjIter.eType
301 **
302 **     0: Table does not exist (error)
303 **     1: Table has an implicit rowid.
304 **     2: Table has an explicit IPK column.
305 **     3: Table has an external PK index.
306 **     4: Table is WITHOUT ROWID.
307 **     5: Table is a virtual table.
308 */
309 #define RBU_PK_NOTABLE        0
310 #define RBU_PK_NONE           1
311 #define RBU_PK_IPK            2
312 #define RBU_PK_EXTERNAL       3
313 #define RBU_PK_WITHOUT_ROWID  4
314 #define RBU_PK_VTAB           5
315 
316 
317 /*
318 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
319 ** one of the following operations.
320 */
321 #define RBU_INSERT     1          /* Insert on a main table b-tree */
322 #define RBU_DELETE     2          /* Delete a row from a main table b-tree */
323 #define RBU_REPLACE    3          /* Delete and then insert a row */
324 #define RBU_IDX_DELETE 4          /* Delete a row from an aux. index b-tree */
325 #define RBU_IDX_INSERT 5          /* Insert on an aux. index b-tree */
326 
327 #define RBU_UPDATE     6          /* Update a row in a main table b-tree */
328 
329 /*
330 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
331 ** file should be copied to page iDbPage of the database file.
332 */
333 struct RbuFrame {
334   u32 iDbPage;
335   u32 iWalFrame;
336 };
337 
338 /*
339 ** RBU handle.
340 **
341 ** nPhaseOneStep:
342 **   If the RBU database contains an rbu_count table, this value is set to
343 **   a running estimate of the number of b-tree operations required to
344 **   finish populating the *-oal file. This allows the sqlite3_bp_progress()
345 **   API to calculate the permyriadage progress of populating the *-oal file
346 **   using the formula:
347 **
348 **     permyriadage = (10000 * nProgress) / nPhaseOneStep
349 **
350 **   nPhaseOneStep is initialized to the sum of:
351 **
352 **     nRow * (nIndex + 1)
353 **
354 **   for all source tables in the RBU database, where nRow is the number
355 **   of rows in the source table and nIndex the number of indexes on the
356 **   corresponding target database table.
357 **
358 **   This estimate is accurate if the RBU update consists entirely of
359 **   INSERT operations. However, it is inaccurate if:
360 **
361 **     * the RBU update contains any UPDATE operations. If the PK specified
362 **       for an UPDATE operation does not exist in the target table, then
363 **       no b-tree operations are required on index b-trees. Or if the
364 **       specified PK does exist, then (nIndex*2) such operations are
365 **       required (one delete and one insert on each index b-tree).
366 **
367 **     * the RBU update contains any DELETE operations for which the specified
368 **       PK does not exist. In this case no operations are required on index
369 **       b-trees.
370 **
371 **     * the RBU update contains REPLACE operations. These are similar to
372 **       UPDATE operations.
373 **
374 **   nPhaseOneStep is updated to account for the conditions above during the
375 **   first pass of each source table. The updated nPhaseOneStep value is
376 **   stored in the rbu_state table if the RBU update is suspended.
377 */
378 struct sqlite3rbu {
379   int eStage;                     /* Value of RBU_STATE_STAGE field */
380   sqlite3 *dbMain;                /* target database handle */
381   sqlite3 *dbRbu;                 /* rbu database handle */
382   char *zTarget;                  /* Path to target db */
383   char *zRbu;                     /* Path to rbu db */
384   char *zState;                   /* Path to state db (or NULL if zRbu) */
385   char zStateDb[5];               /* Db name for state ("stat" or "main") */
386   int rc;                         /* Value returned by last rbu_step() call */
387   char *zErrmsg;                  /* Error message if rc!=SQLITE_OK */
388   int nStep;                      /* Rows processed for current object */
389   int nProgress;                  /* Rows processed for all objects */
390   RbuObjIter objiter;             /* Iterator for skipping through tbl/idx */
391   const char *zVfsName;           /* Name of automatically created rbu vfs */
392   rbu_file *pTargetFd;            /* File handle open on target db */
393   int nPagePerSector;             /* Pages per sector for pTargetFd */
394   i64 iOalSz;
395   i64 nPhaseOneStep;
396   void *pRenameArg;
397   int (*xRename)(void*, const char*, const char*);
398 
399   /* The following state variables are used as part of the incremental
400   ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
401   ** function rbuSetupCheckpoint() for details.  */
402   u32 iMaxFrame;                  /* Largest iWalFrame value in aFrame[] */
403   u32 mLock;
404   int nFrame;                     /* Entries in aFrame[] array */
405   int nFrameAlloc;                /* Allocated size of aFrame[] array */
406   RbuFrame *aFrame;
407   int pgsz;
408   u8 *aBuf;
409   i64 iWalCksum;
410   i64 szTemp;                     /* Current size of all temp files in use */
411   i64 szTempLimit;                /* Total size limit for temp files */
412 
413   /* Used in RBU vacuum mode only */
414   int nRbu;                       /* Number of RBU VFS in the stack */
415   rbu_file *pRbuFd;               /* Fd for main db of dbRbu */
416 };
417 
418 /*
419 ** An rbu VFS is implemented using an instance of this structure.
420 **
421 ** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
422 ** It is NULL for RBU VFS objects created explicitly using
423 ** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
424 ** space used by the RBU handle.
425 */
426 struct rbu_vfs {
427   sqlite3_vfs base;               /* rbu VFS shim methods */
428   sqlite3_vfs *pRealVfs;          /* Underlying VFS */
429   sqlite3_mutex *mutex;           /* Mutex to protect pMain */
430   sqlite3rbu *pRbu;               /* Owner RBU object */
431   rbu_file *pMain;                /* List of main db files */
432   rbu_file *pMainRbu;             /* List of main db files with pRbu!=0 */
433 };
434 
435 /*
436 ** Each file opened by an rbu VFS is represented by an instance of
437 ** the following structure.
438 **
439 ** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
440 ** "sz" is set to the current size of the database file.
441 */
442 struct rbu_file {
443   sqlite3_file base;              /* sqlite3_file methods */
444   sqlite3_file *pReal;            /* Underlying file handle */
445   rbu_vfs *pRbuVfs;               /* Pointer to the rbu_vfs object */
446   sqlite3rbu *pRbu;               /* Pointer to rbu object (rbu target only) */
447   i64 sz;                         /* Size of file in bytes (temp only) */
448 
449   int openFlags;                  /* Flags this file was opened with */
450   u32 iCookie;                    /* Cookie value for main db files */
451   u8 iWriteVer;                   /* "write-version" value for main db files */
452   u8 bNolock;                     /* True to fail EXCLUSIVE locks */
453 
454   int nShm;                       /* Number of entries in apShm[] array */
455   char **apShm;                   /* Array of mmap'd *-shm regions */
456   char *zDel;                     /* Delete this when closing file */
457 
458   const char *zWal;               /* Wal filename for this main db file */
459   rbu_file *pWalFd;               /* Wal file descriptor for this main db */
460   rbu_file *pMainNext;            /* Next MAIN_DB file */
461   rbu_file *pMainRbuNext;         /* Next MAIN_DB file with pRbu!=0 */
462 };
463 
464 /*
465 ** True for an RBU vacuum handle, or false otherwise.
466 */
467 #define rbuIsVacuum(p) ((p)->zTarget==0)
468 
469 
470 /*************************************************************************
471 ** The following three functions, found below:
472 **
473 **   rbuDeltaGetInt()
474 **   rbuDeltaChecksum()
475 **   rbuDeltaApply()
476 **
477 ** are lifted from the fossil source code (http://fossil-scm.org). They
478 ** are used to implement the scalar SQL function rbu_fossil_delta().
479 */
480 
481 /*
482 ** Read bytes from *pz and convert them into a positive integer.  When
483 ** finished, leave *pz pointing to the first character past the end of
484 ** the integer.  The *pLen parameter holds the length of the string
485 ** in *pz and is decremented once for each character in the integer.
486 */
rbuDeltaGetInt(const char ** pz,int * pLen)487 static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
488   static const signed char zValue[] = {
489     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
490     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
491     -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1,
492      0,  1,  2,  3,  4,  5,  6,  7,    8,  9, -1, -1, -1, -1, -1, -1,
493     -1, 10, 11, 12, 13, 14, 15, 16,   17, 18, 19, 20, 21, 22, 23, 24,
494     25, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, -1, -1, -1, -1, 36,
495     -1, 37, 38, 39, 40, 41, 42, 43,   44, 45, 46, 47, 48, 49, 50, 51,
496     52, 53, 54, 55, 56, 57, 58, 59,   60, 61, 62, -1, -1, -1, 63, -1,
497   };
498   unsigned int v = 0;
499   int c;
500   unsigned char *z = (unsigned char*)*pz;
501   unsigned char *zStart = z;
502   while( (c = zValue[0x7f&*(z++)])>=0 ){
503      v = (v<<6) + c;
504   }
505   z--;
506   *pLen -= z - zStart;
507   *pz = (char*)z;
508   return v;
509 }
510 
511 #if RBU_ENABLE_DELTA_CKSUM
512 /*
513 ** Compute a 32-bit checksum on the N-byte buffer.  Return the result.
514 */
rbuDeltaChecksum(const char * zIn,size_t N)515 static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
516   const unsigned char *z = (const unsigned char *)zIn;
517   unsigned sum0 = 0;
518   unsigned sum1 = 0;
519   unsigned sum2 = 0;
520   unsigned sum3 = 0;
521   while(N >= 16){
522     sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
523     sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
524     sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
525     sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
526     z += 16;
527     N -= 16;
528   }
529   while(N >= 4){
530     sum0 += z[0];
531     sum1 += z[1];
532     sum2 += z[2];
533     sum3 += z[3];
534     z += 4;
535     N -= 4;
536   }
537   sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
538   switch(N){
539     case 3:   sum3 += (z[2] << 8);
540     case 2:   sum3 += (z[1] << 16);
541     case 1:   sum3 += (z[0] << 24);
542     default:  ;
543   }
544   return sum3;
545 }
546 #endif
547 
548 /*
549 ** Apply a delta.
550 **
551 ** The output buffer should be big enough to hold the whole output
552 ** file and a NUL terminator at the end.  The delta_output_size()
553 ** routine will determine this size for you.
554 **
555 ** The delta string should be null-terminated.  But the delta string
556 ** may contain embedded NUL characters (if the input and output are
557 ** binary files) so we also have to pass in the length of the delta in
558 ** the lenDelta parameter.
559 **
560 ** This function returns the size of the output file in bytes (excluding
561 ** the final NUL terminator character).  Except, if the delta string is
562 ** malformed or intended for use with a source file other than zSrc,
563 ** then this routine returns -1.
564 **
565 ** Refer to the delta_create() documentation above for a description
566 ** of the delta file format.
567 */
rbuDeltaApply(const char * zSrc,int lenSrc,const char * zDelta,int lenDelta,char * zOut)568 static int rbuDeltaApply(
569   const char *zSrc,      /* The source or pattern file */
570   int lenSrc,            /* Length of the source file */
571   const char *zDelta,    /* Delta to apply to the pattern */
572   int lenDelta,          /* Length of the delta */
573   char *zOut             /* Write the output into this preallocated buffer */
574 ){
575   unsigned int limit;
576   unsigned int total = 0;
577 #if RBU_ENABLE_DELTA_CKSUM
578   char *zOrigOut = zOut;
579 #endif
580 
581   limit = rbuDeltaGetInt(&zDelta, &lenDelta);
582   if( *zDelta!='\n' ){
583     /* ERROR: size integer not terminated by "\n" */
584     return -1;
585   }
586   zDelta++; lenDelta--;
587   while( *zDelta && lenDelta>0 ){
588     unsigned int cnt, ofst;
589     cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
590     switch( zDelta[0] ){
591       case '@': {
592         zDelta++; lenDelta--;
593         ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
594         if( lenDelta>0 && zDelta[0]!=',' ){
595           /* ERROR: copy command not terminated by ',' */
596           return -1;
597         }
598         zDelta++; lenDelta--;
599         total += cnt;
600         if( total>limit ){
601           /* ERROR: copy exceeds output file size */
602           return -1;
603         }
604         if( (int)(ofst+cnt) > lenSrc ){
605           /* ERROR: copy extends past end of input */
606           return -1;
607         }
608         memcpy(zOut, &zSrc[ofst], cnt);
609         zOut += cnt;
610         break;
611       }
612       case ':': {
613         zDelta++; lenDelta--;
614         total += cnt;
615         if( total>limit ){
616           /* ERROR:  insert command gives an output larger than predicted */
617           return -1;
618         }
619         if( (int)cnt>lenDelta ){
620           /* ERROR: insert count exceeds size of delta */
621           return -1;
622         }
623         memcpy(zOut, zDelta, cnt);
624         zOut += cnt;
625         zDelta += cnt;
626         lenDelta -= cnt;
627         break;
628       }
629       case ';': {
630         zDelta++; lenDelta--;
631         zOut[0] = 0;
632 #if RBU_ENABLE_DELTA_CKSUM
633         if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
634           /* ERROR:  bad checksum */
635           return -1;
636         }
637 #endif
638         if( total!=limit ){
639           /* ERROR: generated size does not match predicted size */
640           return -1;
641         }
642         return total;
643       }
644       default: {
645         /* ERROR: unknown delta operator */
646         return -1;
647       }
648     }
649   }
650   /* ERROR: unterminated delta */
651   return -1;
652 }
653 
rbuDeltaOutputSize(const char * zDelta,int lenDelta)654 static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
655   int size;
656   size = rbuDeltaGetInt(&zDelta, &lenDelta);
657   if( *zDelta!='\n' ){
658     /* ERROR: size integer not terminated by "\n" */
659     return -1;
660   }
661   return size;
662 }
663 
664 /*
665 ** End of code taken from fossil.
666 *************************************************************************/
667 
668 /*
669 ** Implementation of SQL scalar function rbu_fossil_delta().
670 **
671 ** This function applies a fossil delta patch to a blob. Exactly two
672 ** arguments must be passed to this function. The first is the blob to
673 ** patch and the second the patch to apply. If no error occurs, this
674 ** function returns the patched blob.
675 */
rbuFossilDeltaFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)676 static void rbuFossilDeltaFunc(
677   sqlite3_context *context,
678   int argc,
679   sqlite3_value **argv
680 ){
681   const char *aDelta;
682   int nDelta;
683   const char *aOrig;
684   int nOrig;
685 
686   int nOut;
687   int nOut2;
688   char *aOut;
689 
690   assert( argc==2 );
691 
692   nOrig = sqlite3_value_bytes(argv[0]);
693   aOrig = (const char*)sqlite3_value_blob(argv[0]);
694   nDelta = sqlite3_value_bytes(argv[1]);
695   aDelta = (const char*)sqlite3_value_blob(argv[1]);
696 
697   /* Figure out the size of the output */
698   nOut = rbuDeltaOutputSize(aDelta, nDelta);
699   if( nOut<0 ){
700     sqlite3_result_error(context, "corrupt fossil delta", -1);
701     return;
702   }
703 
704   aOut = sqlite3_malloc(nOut+1);
705   if( aOut==0 ){
706     sqlite3_result_error_nomem(context);
707   }else{
708     nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
709     if( nOut2!=nOut ){
710       sqlite3_free(aOut);
711       sqlite3_result_error(context, "corrupt fossil delta", -1);
712     }else{
713       sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
714     }
715   }
716 }
717 
718 
719 /*
720 ** Prepare the SQL statement in buffer zSql against database handle db.
721 ** If successful, set *ppStmt to point to the new statement and return
722 ** SQLITE_OK.
723 **
724 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
725 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
726 ** point to a buffer containing an error message. It is the responsibility
727 ** of the caller to (eventually) free this buffer using sqlite3_free().
728 */
prepareAndCollectError(sqlite3 * db,sqlite3_stmt ** ppStmt,char ** pzErrmsg,const char * zSql)729 static int prepareAndCollectError(
730   sqlite3 *db,
731   sqlite3_stmt **ppStmt,
732   char **pzErrmsg,
733   const char *zSql
734 ){
735   int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
736   if( rc!=SQLITE_OK ){
737     *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
738     *ppStmt = 0;
739   }
740   return rc;
741 }
742 
743 /*
744 ** Reset the SQL statement passed as the first argument. Return a copy
745 ** of the value returned by sqlite3_reset().
746 **
747 ** If an error has occurred, then set *pzErrmsg to point to a buffer
748 ** containing an error message. It is the responsibility of the caller
749 ** to eventually free this buffer using sqlite3_free().
750 */
resetAndCollectError(sqlite3_stmt * pStmt,char ** pzErrmsg)751 static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
752   int rc = sqlite3_reset(pStmt);
753   if( rc!=SQLITE_OK ){
754     *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
755   }
756   return rc;
757 }
758 
759 /*
760 ** Unless it is NULL, argument zSql points to a buffer allocated using
761 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
762 ** statement against database db and frees the buffer. If statement
763 ** compilation is successful, *ppStmt is set to point to the new statement
764 ** handle and SQLITE_OK is returned.
765 **
766 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
767 ** returned. In this case, *pzErrmsg may also be set to point to an error
768 ** message. It is the responsibility of the caller to free this error message
769 ** buffer using sqlite3_free().
770 **
771 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
772 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
773 */
prepareFreeAndCollectError(sqlite3 * db,sqlite3_stmt ** ppStmt,char ** pzErrmsg,char * zSql)774 static int prepareFreeAndCollectError(
775   sqlite3 *db,
776   sqlite3_stmt **ppStmt,
777   char **pzErrmsg,
778   char *zSql
779 ){
780   int rc;
781   assert( *pzErrmsg==0 );
782   if( zSql==0 ){
783     rc = SQLITE_NOMEM;
784     *ppStmt = 0;
785   }else{
786     rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
787     sqlite3_free(zSql);
788   }
789   return rc;
790 }
791 
792 /*
793 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
794 ** by an earlier call to rbuObjIterCacheTableInfo().
795 */
rbuObjIterFreeCols(RbuObjIter * pIter)796 static void rbuObjIterFreeCols(RbuObjIter *pIter){
797   int i;
798   for(i=0; i<pIter->nTblCol; i++){
799     sqlite3_free(pIter->azTblCol[i]);
800     sqlite3_free(pIter->azTblType[i]);
801   }
802   sqlite3_free(pIter->azTblCol);
803   pIter->azTblCol = 0;
804   pIter->azTblType = 0;
805   pIter->aiSrcOrder = 0;
806   pIter->abTblPk = 0;
807   pIter->abNotNull = 0;
808   pIter->nTblCol = 0;
809   pIter->eType = 0;               /* Invalid value */
810 }
811 
812 /*
813 ** Finalize all statements and free all allocations that are specific to
814 ** the current object (table/index pair).
815 */
rbuObjIterClearStatements(RbuObjIter * pIter)816 static void rbuObjIterClearStatements(RbuObjIter *pIter){
817   RbuUpdateStmt *pUp;
818 
819   sqlite3_finalize(pIter->pSelect);
820   sqlite3_finalize(pIter->pInsert);
821   sqlite3_finalize(pIter->pDelete);
822   sqlite3_finalize(pIter->pTmpInsert);
823   pUp = pIter->pRbuUpdate;
824   while( pUp ){
825     RbuUpdateStmt *pTmp = pUp->pNext;
826     sqlite3_finalize(pUp->pUpdate);
827     sqlite3_free(pUp);
828     pUp = pTmp;
829   }
830   sqlite3_free(pIter->aIdxCol);
831   sqlite3_free(pIter->zIdxSql);
832 
833   pIter->pSelect = 0;
834   pIter->pInsert = 0;
835   pIter->pDelete = 0;
836   pIter->pRbuUpdate = 0;
837   pIter->pTmpInsert = 0;
838   pIter->nCol = 0;
839   pIter->nIdxCol = 0;
840   pIter->aIdxCol = 0;
841   pIter->zIdxSql = 0;
842 }
843 
844 /*
845 ** Clean up any resources allocated as part of the iterator object passed
846 ** as the only argument.
847 */
rbuObjIterFinalize(RbuObjIter * pIter)848 static void rbuObjIterFinalize(RbuObjIter *pIter){
849   rbuObjIterClearStatements(pIter);
850   sqlite3_finalize(pIter->pTblIter);
851   sqlite3_finalize(pIter->pIdxIter);
852   rbuObjIterFreeCols(pIter);
853   memset(pIter, 0, sizeof(RbuObjIter));
854 }
855 
856 /*
857 ** Advance the iterator to the next position.
858 **
859 ** If no error occurs, SQLITE_OK is returned and the iterator is left
860 ** pointing to the next entry. Otherwise, an error code and message is
861 ** left in the RBU handle passed as the first argument. A copy of the
862 ** error code is returned.
863 */
rbuObjIterNext(sqlite3rbu * p,RbuObjIter * pIter)864 static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
865   int rc = p->rc;
866   if( rc==SQLITE_OK ){
867 
868     /* Free any SQLite statements used while processing the previous object */
869     rbuObjIterClearStatements(pIter);
870     if( pIter->zIdx==0 ){
871       rc = sqlite3_exec(p->dbMain,
872           "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
873           "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
874           "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
875           "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
876           , 0, 0, &p->zErrmsg
877       );
878     }
879 
880     if( rc==SQLITE_OK ){
881       if( pIter->bCleanup ){
882         rbuObjIterFreeCols(pIter);
883         pIter->bCleanup = 0;
884         rc = sqlite3_step(pIter->pTblIter);
885         if( rc!=SQLITE_ROW ){
886           rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
887           pIter->zTbl = 0;
888         }else{
889           pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
890           pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
891           rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
892         }
893       }else{
894         if( pIter->zIdx==0 ){
895           sqlite3_stmt *pIdx = pIter->pIdxIter;
896           rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
897         }
898         if( rc==SQLITE_OK ){
899           rc = sqlite3_step(pIter->pIdxIter);
900           if( rc!=SQLITE_ROW ){
901             rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
902             pIter->bCleanup = 1;
903             pIter->zIdx = 0;
904           }else{
905             pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
906             pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
907             pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
908             rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
909           }
910         }
911       }
912     }
913   }
914 
915   if( rc!=SQLITE_OK ){
916     rbuObjIterFinalize(pIter);
917     p->rc = rc;
918   }
919   return rc;
920 }
921 
922 
923 /*
924 ** The implementation of the rbu_target_name() SQL function. This function
925 ** accepts one or two arguments. The first argument is the name of a table -
926 ** the name of a table in the RBU database.  The second, if it is present, is 1
927 ** for a view or 0 for a table.
928 **
929 ** For a non-vacuum RBU handle, if the table name matches the pattern:
930 **
931 **     data[0-9]_<name>
932 **
933 ** where <name> is any sequence of 1 or more characters, <name> is returned.
934 ** Otherwise, if the only argument does not match the above pattern, an SQL
935 ** NULL is returned.
936 **
937 **     "data_t1"     -> "t1"
938 **     "data0123_t2" -> "t2"
939 **     "dataAB_t3"   -> NULL
940 **
941 ** For an rbu vacuum handle, a copy of the first argument is returned if
942 ** the second argument is either missing or 0 (not a view).
943 */
rbuTargetNameFunc(sqlite3_context * pCtx,int argc,sqlite3_value ** argv)944 static void rbuTargetNameFunc(
945   sqlite3_context *pCtx,
946   int argc,
947   sqlite3_value **argv
948 ){
949   sqlite3rbu *p = sqlite3_user_data(pCtx);
950   const char *zIn;
951   assert( argc==1 || argc==2 );
952 
953   zIn = (const char*)sqlite3_value_text(argv[0]);
954   if( zIn ){
955     if( rbuIsVacuum(p) ){
956       assert( argc==2 || argc==1 );
957       if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
958         sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
959       }
960     }else{
961       if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
962         int i;
963         for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
964         if( zIn[i]=='_' && zIn[i+1] ){
965           sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
966         }
967       }
968     }
969   }
970 }
971 
972 /*
973 ** Initialize the iterator structure passed as the second argument.
974 **
975 ** If no error occurs, SQLITE_OK is returned and the iterator is left
976 ** pointing to the first entry. Otherwise, an error code and message is
977 ** left in the RBU handle passed as the first argument. A copy of the
978 ** error code is returned.
979 */
rbuObjIterFirst(sqlite3rbu * p,RbuObjIter * pIter)980 static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
981   int rc;
982   memset(pIter, 0, sizeof(RbuObjIter));
983 
984   rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
985     sqlite3_mprintf(
986       "SELECT rbu_target_name(name, type='view') AS target, name "
987       "FROM sqlite_schema "
988       "WHERE type IN ('table', 'view') AND target IS NOT NULL "
989       " %s "
990       "ORDER BY name"
991   , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
992 
993   if( rc==SQLITE_OK ){
994     rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
995         "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
996         "  FROM main.sqlite_schema "
997         "  WHERE type='index' AND tbl_name = ?"
998     );
999   }
1000 
1001   pIter->bCleanup = 1;
1002   p->rc = rc;
1003   return rbuObjIterNext(p, pIter);
1004 }
1005 
1006 /*
1007 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
1008 ** an error code is stored in the RBU handle passed as the first argument.
1009 **
1010 ** If an error has already occurred (p->rc is already set to something other
1011 ** than SQLITE_OK), then this function returns NULL without modifying the
1012 ** stored error code. In this case it still calls sqlite3_free() on any
1013 ** printf() parameters associated with %z conversions.
1014 */
rbuMPrintf(sqlite3rbu * p,const char * zFmt,...)1015 static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
1016   char *zSql = 0;
1017   va_list ap;
1018   va_start(ap, zFmt);
1019   zSql = sqlite3_vmprintf(zFmt, ap);
1020   if( p->rc==SQLITE_OK ){
1021     if( zSql==0 ) p->rc = SQLITE_NOMEM;
1022   }else{
1023     sqlite3_free(zSql);
1024     zSql = 0;
1025   }
1026   va_end(ap);
1027   return zSql;
1028 }
1029 
1030 /*
1031 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
1032 ** arguments are the usual subsitution values. This function performs
1033 ** the printf() style substitutions and executes the result as an SQL
1034 ** statement on the RBU handles database.
1035 **
1036 ** If an error occurs, an error code and error message is stored in the
1037 ** RBU handle. If an error has already occurred when this function is
1038 ** called, it is a no-op.
1039 */
rbuMPrintfExec(sqlite3rbu * p,sqlite3 * db,const char * zFmt,...)1040 static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
1041   va_list ap;
1042   char *zSql;
1043   va_start(ap, zFmt);
1044   zSql = sqlite3_vmprintf(zFmt, ap);
1045   if( p->rc==SQLITE_OK ){
1046     if( zSql==0 ){
1047       p->rc = SQLITE_NOMEM;
1048     }else{
1049       p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
1050     }
1051   }
1052   sqlite3_free(zSql);
1053   va_end(ap);
1054   return p->rc;
1055 }
1056 
1057 /*
1058 ** Attempt to allocate and return a pointer to a zeroed block of nByte
1059 ** bytes.
1060 **
1061 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
1062 ** error code in the rbu handle passed as the first argument. Or, if an
1063 ** error has already occurred when this function is called, return NULL
1064 ** immediately without attempting the allocation or modifying the stored
1065 ** error code.
1066 */
rbuMalloc(sqlite3rbu * p,sqlite3_int64 nByte)1067 static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
1068   void *pRet = 0;
1069   if( p->rc==SQLITE_OK ){
1070     assert( nByte>0 );
1071     pRet = sqlite3_malloc64(nByte);
1072     if( pRet==0 ){
1073       p->rc = SQLITE_NOMEM;
1074     }else{
1075       memset(pRet, 0, nByte);
1076     }
1077   }
1078   return pRet;
1079 }
1080 
1081 
1082 /*
1083 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
1084 ** there is room for at least nCol elements. If an OOM occurs, store an
1085 ** error code in the RBU handle passed as the first argument.
1086 */
rbuAllocateIterArrays(sqlite3rbu * p,RbuObjIter * pIter,int nCol)1087 static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
1088   sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
1089   char **azNew;
1090 
1091   azNew = (char**)rbuMalloc(p, nByte);
1092   if( azNew ){
1093     pIter->azTblCol = azNew;
1094     pIter->azTblType = &azNew[nCol];
1095     pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
1096     pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
1097     pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
1098     pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
1099   }
1100 }
1101 
1102 /*
1103 ** The first argument must be a nul-terminated string. This function
1104 ** returns a copy of the string in memory obtained from sqlite3_malloc().
1105 ** It is the responsibility of the caller to eventually free this memory
1106 ** using sqlite3_free().
1107 **
1108 ** If an OOM condition is encountered when attempting to allocate memory,
1109 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
1110 ** if the allocation succeeds, (*pRc) is left unchanged.
1111 */
rbuStrndup(const char * zStr,int * pRc)1112 static char *rbuStrndup(const char *zStr, int *pRc){
1113   char *zRet = 0;
1114 
1115   if( *pRc==SQLITE_OK ){
1116     if( zStr ){
1117       size_t nCopy = strlen(zStr) + 1;
1118       zRet = (char*)sqlite3_malloc64(nCopy);
1119       if( zRet ){
1120         memcpy(zRet, zStr, nCopy);
1121       }else{
1122         *pRc = SQLITE_NOMEM;
1123       }
1124     }
1125   }
1126 
1127   return zRet;
1128 }
1129 
1130 /*
1131 ** Finalize the statement passed as the second argument.
1132 **
1133 ** If the sqlite3_finalize() call indicates that an error occurs, and the
1134 ** rbu handle error code is not already set, set the error code and error
1135 ** message accordingly.
1136 */
rbuFinalize(sqlite3rbu * p,sqlite3_stmt * pStmt)1137 static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
1138   sqlite3 *db = sqlite3_db_handle(pStmt);
1139   int rc = sqlite3_finalize(pStmt);
1140   if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
1141     p->rc = rc;
1142     p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1143   }
1144 }
1145 
1146 /* Determine the type of a table.
1147 **
1148 **   peType is of type (int*), a pointer to an output parameter of type
1149 **   (int). This call sets the output parameter as follows, depending
1150 **   on the type of the table specified by parameters dbName and zTbl.
1151 **
1152 **     RBU_PK_NOTABLE:       No such table.
1153 **     RBU_PK_NONE:          Table has an implicit rowid.
1154 **     RBU_PK_IPK:           Table has an explicit IPK column.
1155 **     RBU_PK_EXTERNAL:      Table has an external PK index.
1156 **     RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
1157 **     RBU_PK_VTAB:          Table is a virtual table.
1158 **
1159 **   Argument *piPk is also of type (int*), and also points to an output
1160 **   parameter. Unless the table has an external primary key index
1161 **   (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
1162 **   if the table does have an external primary key index, then *piPk
1163 **   is set to the root page number of the primary key index before
1164 **   returning.
1165 **
1166 ** ALGORITHM:
1167 **
1168 **   if( no entry exists in sqlite_schema ){
1169 **     return RBU_PK_NOTABLE
1170 **   }else if( sql for the entry starts with "CREATE VIRTUAL" ){
1171 **     return RBU_PK_VTAB
1172 **   }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
1173 **     if( the index that is the pk exists in sqlite_schema ){
1174 **       *piPK = rootpage of that index.
1175 **       return RBU_PK_EXTERNAL
1176 **     }else{
1177 **       return RBU_PK_WITHOUT_ROWID
1178 **     }
1179 **   }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
1180 **     return RBU_PK_IPK
1181 **   }else{
1182 **     return RBU_PK_NONE
1183 **   }
1184 */
rbuTableType(sqlite3rbu * p,const char * zTab,int * peType,int * piTnum,int * piPk)1185 static void rbuTableType(
1186   sqlite3rbu *p,
1187   const char *zTab,
1188   int *peType,
1189   int *piTnum,
1190   int *piPk
1191 ){
1192   /*
1193   ** 0) SELECT count(*) FROM sqlite_schema where name=%Q AND IsVirtual(%Q)
1194   ** 1) PRAGMA index_list = ?
1195   ** 2) SELECT count(*) FROM sqlite_schema where name=%Q
1196   ** 3) PRAGMA table_info = ?
1197   */
1198   sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
1199 
1200   *peType = RBU_PK_NOTABLE;
1201   *piPk = 0;
1202 
1203   assert( p->rc==SQLITE_OK );
1204   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
1205     sqlite3_mprintf(
1206           "SELECT "
1207           " (sql COLLATE nocase BETWEEN 'CREATE VIRTUAL' AND 'CREATE VIRTUAM'),"
1208           " rootpage"
1209           "  FROM sqlite_schema"
1210           " WHERE name=%Q", zTab
1211   ));
1212   if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
1213     /* Either an error, or no such table. */
1214     goto rbuTableType_end;
1215   }
1216   if( sqlite3_column_int(aStmt[0], 0) ){
1217     *peType = RBU_PK_VTAB;                     /* virtual table */
1218     goto rbuTableType_end;
1219   }
1220   *piTnum = sqlite3_column_int(aStmt[0], 1);
1221 
1222   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
1223     sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
1224   );
1225   if( p->rc ) goto rbuTableType_end;
1226   while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
1227     const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
1228     const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
1229     if( zOrig && zIdx && zOrig[0]=='p' ){
1230       p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
1231           sqlite3_mprintf(
1232             "SELECT rootpage FROM sqlite_schema WHERE name = %Q", zIdx
1233       ));
1234       if( p->rc==SQLITE_OK ){
1235         if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
1236           *piPk = sqlite3_column_int(aStmt[2], 0);
1237           *peType = RBU_PK_EXTERNAL;
1238         }else{
1239           *peType = RBU_PK_WITHOUT_ROWID;
1240         }
1241       }
1242       goto rbuTableType_end;
1243     }
1244   }
1245 
1246   p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
1247     sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
1248   );
1249   if( p->rc==SQLITE_OK ){
1250     while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
1251       if( sqlite3_column_int(aStmt[3],5)>0 ){
1252         *peType = RBU_PK_IPK;                /* explicit IPK column */
1253         goto rbuTableType_end;
1254       }
1255     }
1256     *peType = RBU_PK_NONE;
1257   }
1258 
1259 rbuTableType_end: {
1260     unsigned int i;
1261     for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
1262       rbuFinalize(p, aStmt[i]);
1263     }
1264   }
1265 }
1266 
1267 /*
1268 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
1269 ** the pIter->abIndexed[] array.
1270 */
rbuObjIterCacheIndexedCols(sqlite3rbu * p,RbuObjIter * pIter)1271 static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
1272   sqlite3_stmt *pList = 0;
1273   int bIndex = 0;
1274 
1275   if( p->rc==SQLITE_OK ){
1276     memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
1277     p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
1278         sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
1279     );
1280   }
1281 
1282   pIter->nIndex = 0;
1283   while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
1284     const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
1285     int bPartial = sqlite3_column_int(pList, 4);
1286     sqlite3_stmt *pXInfo = 0;
1287     if( zIdx==0 ) break;
1288     if( bPartial ){
1289       memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
1290     }
1291     p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1292         sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1293     );
1294     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1295       int iCid = sqlite3_column_int(pXInfo, 1);
1296       if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
1297       if( iCid==-2 ){
1298         memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
1299       }
1300     }
1301     rbuFinalize(p, pXInfo);
1302     bIndex = 1;
1303     pIter->nIndex++;
1304   }
1305 
1306   if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
1307     /* "PRAGMA index_list" includes the main PK b-tree */
1308     pIter->nIndex--;
1309   }
1310 
1311   rbuFinalize(p, pList);
1312   if( bIndex==0 ) pIter->abIndexed = 0;
1313 }
1314 
1315 
1316 /*
1317 ** If they are not already populated, populate the pIter->azTblCol[],
1318 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
1319 ** the table (not index) that the iterator currently points to.
1320 **
1321 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
1322 ** an error does occur, an error code and error message are also left in
1323 ** the RBU handle.
1324 */
rbuObjIterCacheTableInfo(sqlite3rbu * p,RbuObjIter * pIter)1325 static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
1326   if( pIter->azTblCol==0 ){
1327     sqlite3_stmt *pStmt = 0;
1328     int nCol = 0;
1329     int i;                        /* for() loop iterator variable */
1330     int bRbuRowid = 0;            /* If input table has column "rbu_rowid" */
1331     int iOrder = 0;
1332     int iTnum = 0;
1333 
1334     /* Figure out the type of table this step will deal with. */
1335     assert( pIter->eType==0 );
1336     rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
1337     if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
1338       p->rc = SQLITE_ERROR;
1339       p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
1340     }
1341     if( p->rc ) return p->rc;
1342     if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
1343 
1344     assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
1345          || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
1346          || pIter->eType==RBU_PK_VTAB
1347     );
1348 
1349     /* Populate the azTblCol[] and nTblCol variables based on the columns
1350     ** of the input table. Ignore any input table columns that begin with
1351     ** "rbu_".  */
1352     p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
1353         sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
1354     );
1355     if( p->rc==SQLITE_OK ){
1356       nCol = sqlite3_column_count(pStmt);
1357       rbuAllocateIterArrays(p, pIter, nCol);
1358     }
1359     for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
1360       const char *zName = (const char*)sqlite3_column_name(pStmt, i);
1361       if( sqlite3_strnicmp("rbu_", zName, 4) ){
1362         char *zCopy = rbuStrndup(zName, &p->rc);
1363         pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
1364         pIter->azTblCol[pIter->nTblCol++] = zCopy;
1365       }
1366       else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
1367         bRbuRowid = 1;
1368       }
1369     }
1370     sqlite3_finalize(pStmt);
1371     pStmt = 0;
1372 
1373     if( p->rc==SQLITE_OK
1374      && rbuIsVacuum(p)==0
1375      && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
1376     ){
1377       p->rc = SQLITE_ERROR;
1378       p->zErrmsg = sqlite3_mprintf(
1379           "table %q %s rbu_rowid column", pIter->zDataTbl,
1380           (bRbuRowid ? "may not have" : "requires")
1381       );
1382     }
1383 
1384     /* Check that all non-HIDDEN columns in the destination table are also
1385     ** present in the input table. Populate the abTblPk[], azTblType[] and
1386     ** aiTblOrder[] arrays at the same time.  */
1387     if( p->rc==SQLITE_OK ){
1388       p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
1389           sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
1390       );
1391     }
1392     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1393       const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
1394       if( zName==0 ) break;  /* An OOM - finalize() below returns S_NOMEM */
1395       for(i=iOrder; i<pIter->nTblCol; i++){
1396         if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
1397       }
1398       if( i==pIter->nTblCol ){
1399         p->rc = SQLITE_ERROR;
1400         p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
1401             pIter->zDataTbl, zName
1402         );
1403       }else{
1404         int iPk = sqlite3_column_int(pStmt, 5);
1405         int bNotNull = sqlite3_column_int(pStmt, 3);
1406         const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
1407 
1408         if( i!=iOrder ){
1409           SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
1410           SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
1411         }
1412 
1413         pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
1414         assert( iPk>=0 );
1415         pIter->abTblPk[iOrder] = (u8)iPk;
1416         pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
1417         iOrder++;
1418       }
1419     }
1420 
1421     rbuFinalize(p, pStmt);
1422     rbuObjIterCacheIndexedCols(p, pIter);
1423     assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
1424     assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
1425   }
1426 
1427   return p->rc;
1428 }
1429 
1430 /*
1431 ** This function constructs and returns a pointer to a nul-terminated
1432 ** string containing some SQL clause or list based on one or more of the
1433 ** column names currently stored in the pIter->azTblCol[] array.
1434 */
rbuObjIterGetCollist(sqlite3rbu * p,RbuObjIter * pIter)1435 static char *rbuObjIterGetCollist(
1436   sqlite3rbu *p,                  /* RBU object */
1437   RbuObjIter *pIter               /* Object iterator for column names */
1438 ){
1439   char *zList = 0;
1440   const char *zSep = "";
1441   int i;
1442   for(i=0; i<pIter->nTblCol; i++){
1443     const char *z = pIter->azTblCol[i];
1444     zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
1445     zSep = ", ";
1446   }
1447   return zList;
1448 }
1449 
1450 /*
1451 ** Return a comma separated list of the quoted PRIMARY KEY column names,
1452 ** in order, for the current table. Before each column name, add the text
1453 ** zPre. After each column name, add the zPost text. Use zSeparator as
1454 ** the separator text (usually ", ").
1455 */
rbuObjIterGetPkList(sqlite3rbu * p,RbuObjIter * pIter,const char * zPre,const char * zSeparator,const char * zPost)1456 static char *rbuObjIterGetPkList(
1457   sqlite3rbu *p,                  /* RBU object */
1458   RbuObjIter *pIter,              /* Object iterator for column names */
1459   const char *zPre,               /* Before each quoted column name */
1460   const char *zSeparator,         /* Separator to use between columns */
1461   const char *zPost               /* After each quoted column name */
1462 ){
1463   int iPk = 1;
1464   char *zRet = 0;
1465   const char *zSep = "";
1466   while( 1 ){
1467     int i;
1468     for(i=0; i<pIter->nTblCol; i++){
1469       if( (int)pIter->abTblPk[i]==iPk ){
1470         const char *zCol = pIter->azTblCol[i];
1471         zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
1472         zSep = zSeparator;
1473         break;
1474       }
1475     }
1476     if( i==pIter->nTblCol ) break;
1477     iPk++;
1478   }
1479   return zRet;
1480 }
1481 
1482 /*
1483 ** This function is called as part of restarting an RBU vacuum within
1484 ** stage 1 of the process (while the *-oal file is being built) while
1485 ** updating a table (not an index). The table may be a rowid table or
1486 ** a WITHOUT ROWID table. It queries the target database to find the
1487 ** largest key that has already been written to the target table and
1488 ** constructs a WHERE clause that can be used to extract the remaining
1489 ** rows from the source table. For a rowid table, the WHERE clause
1490 ** is of the form:
1491 **
1492 **     "WHERE _rowid_ > ?"
1493 **
1494 ** and for WITHOUT ROWID tables:
1495 **
1496 **     "WHERE (key1, key2) > (?, ?)"
1497 **
1498 ** Instead of "?" placeholders, the actual WHERE clauses created by
1499 ** this function contain literal SQL values.
1500 */
rbuVacuumTableStart(sqlite3rbu * p,RbuObjIter * pIter,int bRowid,const char * zWrite)1501 static char *rbuVacuumTableStart(
1502   sqlite3rbu *p,                  /* RBU handle */
1503   RbuObjIter *pIter,              /* RBU iterator object */
1504   int bRowid,                     /* True for a rowid table */
1505   const char *zWrite              /* Target table name prefix */
1506 ){
1507   sqlite3_stmt *pMax = 0;
1508   char *zRet = 0;
1509   if( bRowid ){
1510     p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
1511         sqlite3_mprintf(
1512           "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
1513         )
1514     );
1515     if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
1516       sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
1517       zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
1518     }
1519     rbuFinalize(p, pMax);
1520   }else{
1521     char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
1522     char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
1523     char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
1524 
1525     if( p->rc==SQLITE_OK ){
1526       p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
1527           sqlite3_mprintf(
1528             "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
1529                 zSelect, zWrite, pIter->zTbl, zOrder
1530           )
1531       );
1532       if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
1533         const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
1534         zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
1535       }
1536       rbuFinalize(p, pMax);
1537     }
1538 
1539     sqlite3_free(zOrder);
1540     sqlite3_free(zSelect);
1541     sqlite3_free(zList);
1542   }
1543   return zRet;
1544 }
1545 
1546 /*
1547 ** This function is called as part of restating an RBU vacuum when the
1548 ** current operation is writing content to an index. If possible, it
1549 ** queries the target index b-tree for the largest key already written to
1550 ** it, then composes and returns an expression that can be used in a WHERE
1551 ** clause to select the remaining required rows from the source table.
1552 ** It is only possible to return such an expression if:
1553 **
1554 **   * The index contains no DESC columns, and
1555 **   * The last key written to the index before the operation was
1556 **     suspended does not contain any NULL values.
1557 **
1558 ** The expression is of the form:
1559 **
1560 **   (index-field1, index-field2, ...) > (?, ?, ...)
1561 **
1562 ** except that the "?" placeholders are replaced with literal values.
1563 **
1564 ** If the expression cannot be created, NULL is returned. In this case,
1565 ** the caller has to use an OFFSET clause to extract only the required
1566 ** rows from the sourct table, just as it does for an RBU update operation.
1567 */
rbuVacuumIndexStart(sqlite3rbu * p,RbuObjIter * pIter)1568 static char *rbuVacuumIndexStart(
1569   sqlite3rbu *p,                  /* RBU handle */
1570   RbuObjIter *pIter               /* RBU iterator object */
1571 ){
1572   char *zOrder = 0;
1573   char *zLhs = 0;
1574   char *zSelect = 0;
1575   char *zVector = 0;
1576   char *zRet = 0;
1577   int bFailed = 0;
1578   const char *zSep = "";
1579   int iCol = 0;
1580   sqlite3_stmt *pXInfo = 0;
1581 
1582   p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1583       sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
1584   );
1585   while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1586     int iCid = sqlite3_column_int(pXInfo, 1);
1587     const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
1588     const char *zCol;
1589     if( sqlite3_column_int(pXInfo, 3) ){
1590       bFailed = 1;
1591       break;
1592     }
1593 
1594     if( iCid<0 ){
1595       if( pIter->eType==RBU_PK_IPK ){
1596         int i;
1597         for(i=0; pIter->abTblPk[i]==0; i++);
1598         assert( i<pIter->nTblCol );
1599         zCol = pIter->azTblCol[i];
1600       }else{
1601         zCol = "_rowid_";
1602       }
1603     }else{
1604       zCol = pIter->azTblCol[iCid];
1605     }
1606 
1607     zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
1608         zLhs, zSep, zCol, zCollate
1609         );
1610     zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
1611         zOrder, zSep, iCol, zCol, zCollate
1612         );
1613     zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
1614         zSelect, zSep, iCol, zCol
1615         );
1616     zSep = ", ";
1617     iCol++;
1618   }
1619   rbuFinalize(p, pXInfo);
1620   if( bFailed ) goto index_start_out;
1621 
1622   if( p->rc==SQLITE_OK ){
1623     sqlite3_stmt *pSel = 0;
1624 
1625     p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
1626         sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
1627           zSelect, pIter->zTbl, zOrder
1628         )
1629     );
1630     if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
1631       zSep = "";
1632       for(iCol=0; iCol<pIter->nCol; iCol++){
1633         const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
1634         if( zQuoted==0 ){
1635           p->rc = SQLITE_NOMEM;
1636         }else if( zQuoted[0]=='N' ){
1637           bFailed = 1;
1638           break;
1639         }
1640         zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
1641         zSep = ", ";
1642       }
1643 
1644       if( !bFailed ){
1645         zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
1646       }
1647     }
1648     rbuFinalize(p, pSel);
1649   }
1650 
1651  index_start_out:
1652   sqlite3_free(zOrder);
1653   sqlite3_free(zSelect);
1654   sqlite3_free(zVector);
1655   sqlite3_free(zLhs);
1656   return zRet;
1657 }
1658 
1659 /*
1660 ** This function is used to create a SELECT list (the list of SQL
1661 ** expressions that follows a SELECT keyword) for a SELECT statement
1662 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
1663 ** index object currently indicated by the iterator object passed as the
1664 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
1665 ** to obtain the required information.
1666 **
1667 ** If the index is of the following form:
1668 **
1669 **   CREATE INDEX i1 ON t1(c, b COLLATE nocase);
1670 **
1671 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
1672 ** "ipk", the returned string is:
1673 **
1674 **   "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
1675 **
1676 ** As well as the returned string, three other malloc'd strings are
1677 ** returned via output parameters. As follows:
1678 **
1679 **   pzImposterCols: ...
1680 **   pzImposterPk: ...
1681 **   pzWhere: ...
1682 */
rbuObjIterGetIndexCols(sqlite3rbu * p,RbuObjIter * pIter,char ** pzImposterCols,char ** pzImposterPk,char ** pzWhere,int * pnBind)1683 static char *rbuObjIterGetIndexCols(
1684   sqlite3rbu *p,                  /* RBU object */
1685   RbuObjIter *pIter,              /* Object iterator for column names */
1686   char **pzImposterCols,          /* OUT: Columns for imposter table */
1687   char **pzImposterPk,            /* OUT: Imposter PK clause */
1688   char **pzWhere,                 /* OUT: WHERE clause */
1689   int *pnBind                     /* OUT: Trbul number of columns */
1690 ){
1691   int rc = p->rc;                 /* Error code */
1692   int rc2;                        /* sqlite3_finalize() return code */
1693   char *zRet = 0;                 /* String to return */
1694   char *zImpCols = 0;             /* String to return via *pzImposterCols */
1695   char *zImpPK = 0;               /* String to return via *pzImposterPK */
1696   char *zWhere = 0;               /* String to return via *pzWhere */
1697   int nBind = 0;                  /* Value to return via *pnBind */
1698   const char *zCom = "";          /* Set to ", " later on */
1699   const char *zAnd = "";          /* Set to " AND " later on */
1700   sqlite3_stmt *pXInfo = 0;       /* PRAGMA index_xinfo = ? */
1701 
1702   if( rc==SQLITE_OK ){
1703     assert( p->zErrmsg==0 );
1704     rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1705         sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
1706     );
1707   }
1708 
1709   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1710     int iCid = sqlite3_column_int(pXInfo, 1);
1711     int bDesc = sqlite3_column_int(pXInfo, 3);
1712     const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
1713     const char *zCol = 0;
1714     const char *zType;
1715 
1716     if( iCid==-2 ){
1717       int iSeq = sqlite3_column_int(pXInfo, 0);
1718       zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
1719           pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
1720       );
1721       zType = "";
1722     }else {
1723       if( iCid<0 ){
1724         /* An integer primary key. If the table has an explicit IPK, use
1725         ** its name. Otherwise, use "rbu_rowid".  */
1726         if( pIter->eType==RBU_PK_IPK ){
1727           int i;
1728           for(i=0; pIter->abTblPk[i]==0; i++);
1729           assert( i<pIter->nTblCol );
1730           zCol = pIter->azTblCol[i];
1731         }else if( rbuIsVacuum(p) ){
1732           zCol = "_rowid_";
1733         }else{
1734           zCol = "rbu_rowid";
1735         }
1736         zType = "INTEGER";
1737       }else{
1738         zCol = pIter->azTblCol[iCid];
1739         zType = pIter->azTblType[iCid];
1740       }
1741       zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
1742     }
1743 
1744     if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
1745       const char *zOrder = (bDesc ? " DESC" : "");
1746       zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
1747           zImpPK, zCom, nBind, zCol, zOrder
1748       );
1749     }
1750     zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
1751         zImpCols, zCom, nBind, zCol, zType, zCollate
1752     );
1753     zWhere = sqlite3_mprintf(
1754         "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
1755     );
1756     if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
1757     zCom = ", ";
1758     zAnd = " AND ";
1759     nBind++;
1760   }
1761 
1762   rc2 = sqlite3_finalize(pXInfo);
1763   if( rc==SQLITE_OK ) rc = rc2;
1764 
1765   if( rc!=SQLITE_OK ){
1766     sqlite3_free(zRet);
1767     sqlite3_free(zImpCols);
1768     sqlite3_free(zImpPK);
1769     sqlite3_free(zWhere);
1770     zRet = 0;
1771     zImpCols = 0;
1772     zImpPK = 0;
1773     zWhere = 0;
1774     p->rc = rc;
1775   }
1776 
1777   *pzImposterCols = zImpCols;
1778   *pzImposterPk = zImpPK;
1779   *pzWhere = zWhere;
1780   *pnBind = nBind;
1781   return zRet;
1782 }
1783 
1784 /*
1785 ** Assuming the current table columns are "a", "b" and "c", and the zObj
1786 ** paramter is passed "old", return a string of the form:
1787 **
1788 **     "old.a, old.b, old.b"
1789 **
1790 ** With the column names escaped.
1791 **
1792 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
1793 ** the text ", old._rowid_" to the returned value.
1794 */
rbuObjIterGetOldlist(sqlite3rbu * p,RbuObjIter * pIter,const char * zObj)1795 static char *rbuObjIterGetOldlist(
1796   sqlite3rbu *p,
1797   RbuObjIter *pIter,
1798   const char *zObj
1799 ){
1800   char *zList = 0;
1801   if( p->rc==SQLITE_OK && pIter->abIndexed ){
1802     const char *zS = "";
1803     int i;
1804     for(i=0; i<pIter->nTblCol; i++){
1805       if( pIter->abIndexed[i] ){
1806         const char *zCol = pIter->azTblCol[i];
1807         zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
1808       }else{
1809         zList = sqlite3_mprintf("%z%sNULL", zList, zS);
1810       }
1811       zS = ", ";
1812       if( zList==0 ){
1813         p->rc = SQLITE_NOMEM;
1814         break;
1815       }
1816     }
1817 
1818     /* For a table with implicit rowids, append "old._rowid_" to the list. */
1819     if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
1820       zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
1821     }
1822   }
1823   return zList;
1824 }
1825 
1826 /*
1827 ** Return an expression that can be used in a WHERE clause to match the
1828 ** primary key of the current table. For example, if the table is:
1829 **
1830 **   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
1831 **
1832 ** Return the string:
1833 **
1834 **   "b = ?1 AND c = ?2"
1835 */
rbuObjIterGetWhere(sqlite3rbu * p,RbuObjIter * pIter)1836 static char *rbuObjIterGetWhere(
1837   sqlite3rbu *p,
1838   RbuObjIter *pIter
1839 ){
1840   char *zList = 0;
1841   if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
1842     zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
1843   }else if( pIter->eType==RBU_PK_EXTERNAL ){
1844     const char *zSep = "";
1845     int i;
1846     for(i=0; i<pIter->nTblCol; i++){
1847       if( pIter->abTblPk[i] ){
1848         zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
1849         zSep = " AND ";
1850       }
1851     }
1852     zList = rbuMPrintf(p,
1853         "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
1854     );
1855 
1856   }else{
1857     const char *zSep = "";
1858     int i;
1859     for(i=0; i<pIter->nTblCol; i++){
1860       if( pIter->abTblPk[i] ){
1861         const char *zCol = pIter->azTblCol[i];
1862         zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
1863         zSep = " AND ";
1864       }
1865     }
1866   }
1867   return zList;
1868 }
1869 
1870 /*
1871 ** The SELECT statement iterating through the keys for the current object
1872 ** (p->objiter.pSelect) currently points to a valid row. However, there
1873 ** is something wrong with the rbu_control value in the rbu_control value
1874 ** stored in the (p->nCol+1)'th column. Set the error code and error message
1875 ** of the RBU handle to something reflecting this.
1876 */
rbuBadControlError(sqlite3rbu * p)1877 static void rbuBadControlError(sqlite3rbu *p){
1878   p->rc = SQLITE_ERROR;
1879   p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
1880 }
1881 
1882 
1883 /*
1884 ** Return a nul-terminated string containing the comma separated list of
1885 ** assignments that should be included following the "SET" keyword of
1886 ** an UPDATE statement used to update the table object that the iterator
1887 ** passed as the second argument currently points to if the rbu_control
1888 ** column of the data_xxx table entry is set to zMask.
1889 **
1890 ** The memory for the returned string is obtained from sqlite3_malloc().
1891 ** It is the responsibility of the caller to eventually free it using
1892 ** sqlite3_free().
1893 **
1894 ** If an OOM error is encountered when allocating space for the new
1895 ** string, an error code is left in the rbu handle passed as the first
1896 ** argument and NULL is returned. Or, if an error has already occurred
1897 ** when this function is called, NULL is returned immediately, without
1898 ** attempting the allocation or modifying the stored error code.
1899 */
rbuObjIterGetSetlist(sqlite3rbu * p,RbuObjIter * pIter,const char * zMask)1900 static char *rbuObjIterGetSetlist(
1901   sqlite3rbu *p,
1902   RbuObjIter *pIter,
1903   const char *zMask
1904 ){
1905   char *zList = 0;
1906   if( p->rc==SQLITE_OK ){
1907     int i;
1908 
1909     if( (int)strlen(zMask)!=pIter->nTblCol ){
1910       rbuBadControlError(p);
1911     }else{
1912       const char *zSep = "";
1913       for(i=0; i<pIter->nTblCol; i++){
1914         char c = zMask[pIter->aiSrcOrder[i]];
1915         if( c=='x' ){
1916           zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
1917               zList, zSep, pIter->azTblCol[i], i+1
1918           );
1919           zSep = ", ";
1920         }
1921         else if( c=='d' ){
1922           zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
1923               zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
1924           );
1925           zSep = ", ";
1926         }
1927         else if( c=='f' ){
1928           zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
1929               zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
1930           );
1931           zSep = ", ";
1932         }
1933       }
1934     }
1935   }
1936   return zList;
1937 }
1938 
1939 /*
1940 ** Return a nul-terminated string consisting of nByte comma separated
1941 ** "?" expressions. For example, if nByte is 3, return a pointer to
1942 ** a buffer containing the string "?,?,?".
1943 **
1944 ** The memory for the returned string is obtained from sqlite3_malloc().
1945 ** It is the responsibility of the caller to eventually free it using
1946 ** sqlite3_free().
1947 **
1948 ** If an OOM error is encountered when allocating space for the new
1949 ** string, an error code is left in the rbu handle passed as the first
1950 ** argument and NULL is returned. Or, if an error has already occurred
1951 ** when this function is called, NULL is returned immediately, without
1952 ** attempting the allocation or modifying the stored error code.
1953 */
rbuObjIterGetBindlist(sqlite3rbu * p,int nBind)1954 static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
1955   char *zRet = 0;
1956   sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
1957 
1958   zRet = (char*)rbuMalloc(p, nByte);
1959   if( zRet ){
1960     int i;
1961     for(i=0; i<nBind; i++){
1962       zRet[i*2] = '?';
1963       zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
1964     }
1965   }
1966   return zRet;
1967 }
1968 
1969 /*
1970 ** The iterator currently points to a table (not index) of type
1971 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
1972 ** declaration for the corresponding imposter table. For example,
1973 ** if the iterator points to a table created as:
1974 **
1975 **   CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
1976 **
1977 ** this function returns:
1978 **
1979 **   PRIMARY KEY("b", "a" DESC)
1980 */
rbuWithoutRowidPK(sqlite3rbu * p,RbuObjIter * pIter)1981 static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
1982   char *z = 0;
1983   assert( pIter->zIdx==0 );
1984   if( p->rc==SQLITE_OK ){
1985     const char *zSep = "PRIMARY KEY(";
1986     sqlite3_stmt *pXList = 0;     /* PRAGMA index_list = (pIter->zTbl) */
1987     sqlite3_stmt *pXInfo = 0;     /* PRAGMA index_xinfo = <pk-index> */
1988 
1989     p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
1990         sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
1991     );
1992     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
1993       const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
1994       if( zOrig && strcmp(zOrig, "pk")==0 ){
1995         const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
1996         if( zIdx ){
1997           p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1998               sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1999           );
2000         }
2001         break;
2002       }
2003     }
2004     rbuFinalize(p, pXList);
2005 
2006     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
2007       if( sqlite3_column_int(pXInfo, 5) ){
2008         /* int iCid = sqlite3_column_int(pXInfo, 0); */
2009         const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
2010         const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
2011         z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
2012         zSep = ", ";
2013       }
2014     }
2015     z = rbuMPrintf(p, "%z)", z);
2016     rbuFinalize(p, pXInfo);
2017   }
2018   return z;
2019 }
2020 
2021 /*
2022 ** This function creates the second imposter table used when writing to
2023 ** a table b-tree where the table has an external primary key. If the
2024 ** iterator passed as the second argument does not currently point to
2025 ** a table (not index) with an external primary key, this function is a
2026 ** no-op.
2027 **
2028 ** Assuming the iterator does point to a table with an external PK, this
2029 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
2030 ** used to access that PK index. For example, if the target table is
2031 ** declared as follows:
2032 **
2033 **   CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
2034 **
2035 ** then the imposter table schema is:
2036 **
2037 **   CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
2038 **
2039 */
rbuCreateImposterTable2(sqlite3rbu * p,RbuObjIter * pIter)2040 static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
2041   if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
2042     int tnum = pIter->iPkTnum;    /* Root page of PK index */
2043     sqlite3_stmt *pQuery = 0;     /* SELECT name ... WHERE rootpage = $tnum */
2044     const char *zIdx = 0;         /* Name of PK index */
2045     sqlite3_stmt *pXInfo = 0;     /* PRAGMA main.index_xinfo = $zIdx */
2046     const char *zComma = "";
2047     char *zCols = 0;              /* Used to build up list of table cols */
2048     char *zPk = 0;                /* Used to build up table PK declaration */
2049 
2050     /* Figure out the name of the primary key index for the current table.
2051     ** This is needed for the argument to "PRAGMA index_xinfo". Set
2052     ** zIdx to point to a nul-terminated string containing this name. */
2053     p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
2054         "SELECT name FROM sqlite_schema WHERE rootpage = ?"
2055     );
2056     if( p->rc==SQLITE_OK ){
2057       sqlite3_bind_int(pQuery, 1, tnum);
2058       if( SQLITE_ROW==sqlite3_step(pQuery) ){
2059         zIdx = (const char*)sqlite3_column_text(pQuery, 0);
2060       }
2061     }
2062     if( zIdx ){
2063       p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
2064           sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
2065       );
2066     }
2067     rbuFinalize(p, pQuery);
2068 
2069     while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
2070       int bKey = sqlite3_column_int(pXInfo, 5);
2071       if( bKey ){
2072         int iCid = sqlite3_column_int(pXInfo, 1);
2073         int bDesc = sqlite3_column_int(pXInfo, 3);
2074         const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
2075         zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
2076             iCid, pIter->azTblType[iCid], zCollate
2077         );
2078         zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
2079         zComma = ", ";
2080       }
2081     }
2082     zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
2083     rbuFinalize(p, pXInfo);
2084 
2085     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
2086     rbuMPrintfExec(p, p->dbMain,
2087         "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
2088         zCols, zPk
2089     );
2090     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2091   }
2092 }
2093 
2094 /*
2095 ** If an error has already occurred when this function is called, it
2096 ** immediately returns zero (without doing any work). Or, if an error
2097 ** occurs during the execution of this function, it sets the error code
2098 ** in the sqlite3rbu object indicated by the first argument and returns
2099 ** zero.
2100 **
2101 ** The iterator passed as the second argument is guaranteed to point to
2102 ** a table (not an index) when this function is called. This function
2103 ** attempts to create any imposter table required to write to the main
2104 ** table b-tree of the table before returning. Non-zero is returned if
2105 ** an imposter table are created, or zero otherwise.
2106 **
2107 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
2108 ** virtual tables are written to directly. The imposter table has the
2109 ** same schema as the actual target table (less any UNIQUE constraints).
2110 ** More precisely, the "same schema" means the same columns, types,
2111 ** collation sequences. For tables that do not have an external PRIMARY
2112 ** KEY, it also means the same PRIMARY KEY declaration.
2113 */
rbuCreateImposterTable(sqlite3rbu * p,RbuObjIter * pIter)2114 static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
2115   if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
2116     int tnum = pIter->iTnum;
2117     const char *zComma = "";
2118     char *zSql = 0;
2119     int iCol;
2120     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
2121 
2122     for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
2123       const char *zPk = "";
2124       const char *zCol = pIter->azTblCol[iCol];
2125       const char *zColl = 0;
2126 
2127       p->rc = sqlite3_table_column_metadata(
2128           p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
2129       );
2130 
2131       if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
2132         /* If the target table column is an "INTEGER PRIMARY KEY", add
2133         ** "PRIMARY KEY" to the imposter table column declaration. */
2134         zPk = "PRIMARY KEY ";
2135       }
2136       zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
2137           zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
2138           (pIter->abNotNull[iCol] ? " NOT NULL" : "")
2139       );
2140       zComma = ", ";
2141     }
2142 
2143     if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
2144       char *zPk = rbuWithoutRowidPK(p, pIter);
2145       if( zPk ){
2146         zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
2147       }
2148     }
2149 
2150     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
2151     rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
2152         pIter->zTbl, zSql,
2153         (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
2154     );
2155     sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2156   }
2157 }
2158 
2159 /*
2160 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
2161 ** Specifically a statement of the form:
2162 **
2163 **     INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
2164 **
2165 ** The number of bound variables is equal to the number of columns in
2166 ** the target table, plus one (for the rbu_control column), plus one more
2167 ** (for the rbu_rowid column) if the target table is an implicit IPK or
2168 ** virtual table.
2169 */
rbuObjIterPrepareTmpInsert(sqlite3rbu * p,RbuObjIter * pIter,const char * zCollist,const char * zRbuRowid)2170 static void rbuObjIterPrepareTmpInsert(
2171   sqlite3rbu *p,
2172   RbuObjIter *pIter,
2173   const char *zCollist,
2174   const char *zRbuRowid
2175 ){
2176   int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
2177   char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
2178   if( zBind ){
2179     assert( pIter->pTmpInsert==0 );
2180     p->rc = prepareFreeAndCollectError(
2181         p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
2182           "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
2183           p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
2184     ));
2185   }
2186 }
2187 
rbuTmpInsertFunc(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)2188 static void rbuTmpInsertFunc(
2189   sqlite3_context *pCtx,
2190   int nVal,
2191   sqlite3_value **apVal
2192 ){
2193   sqlite3rbu *p = sqlite3_user_data(pCtx);
2194   int rc = SQLITE_OK;
2195   int i;
2196 
2197   assert( sqlite3_value_int(apVal[0])!=0
2198       || p->objiter.eType==RBU_PK_EXTERNAL
2199       || p->objiter.eType==RBU_PK_NONE
2200   );
2201   if( sqlite3_value_int(apVal[0])!=0 ){
2202     p->nPhaseOneStep += p->objiter.nIndex;
2203   }
2204 
2205   for(i=0; rc==SQLITE_OK && i<nVal; i++){
2206     rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
2207   }
2208   if( rc==SQLITE_OK ){
2209     sqlite3_step(p->objiter.pTmpInsert);
2210     rc = sqlite3_reset(p->objiter.pTmpInsert);
2211   }
2212 
2213   if( rc!=SQLITE_OK ){
2214     sqlite3_result_error_code(pCtx, rc);
2215   }
2216 }
2217 
rbuObjIterGetIndexWhere(sqlite3rbu * p,RbuObjIter * pIter)2218 static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
2219   sqlite3_stmt *pStmt = 0;
2220   int rc = p->rc;
2221   char *zRet = 0;
2222 
2223   assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
2224 
2225   if( rc==SQLITE_OK ){
2226     rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
2227         "SELECT trim(sql) FROM sqlite_schema WHERE type='index' AND name=?"
2228     );
2229   }
2230   if( rc==SQLITE_OK ){
2231     int rc2;
2232     rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
2233     if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
2234       char *zSql = (char*)sqlite3_column_text(pStmt, 0);
2235       if( zSql ){
2236         pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
2237       }
2238       if( zSql ){
2239         int nParen = 0;           /* Number of open parenthesis */
2240         int i;
2241         int iIdxCol = 0;
2242         int nIdxAlloc = 0;
2243         for(i=0; zSql[i]; i++){
2244           char c = zSql[i];
2245 
2246           /* If necessary, grow the pIter->aIdxCol[] array */
2247           if( iIdxCol==nIdxAlloc ){
2248             RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
2249                 pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
2250             );
2251             if( aIdxCol==0 ){
2252               rc = SQLITE_NOMEM;
2253               break;
2254             }
2255             pIter->aIdxCol = aIdxCol;
2256             nIdxAlloc += 16;
2257           }
2258 
2259           if( c=='(' ){
2260             if( nParen==0 ){
2261               assert( iIdxCol==0 );
2262               pIter->aIdxCol[0].zSpan = &zSql[i+1];
2263             }
2264             nParen++;
2265           }
2266           else if( c==')' ){
2267             nParen--;
2268             if( nParen==0 ){
2269               int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
2270               pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
2271               i++;
2272               break;
2273             }
2274           }else if( c==',' && nParen==1 ){
2275             int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
2276             pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
2277             pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
2278           }else if( c=='"' || c=='\'' || c=='`' ){
2279             for(i++; 1; i++){
2280               if( zSql[i]==c ){
2281                 if( zSql[i+1]!=c ) break;
2282                 i++;
2283               }
2284             }
2285           }else if( c=='[' ){
2286             for(i++; 1; i++){
2287               if( zSql[i]==']' ) break;
2288             }
2289           }else if( c=='-' && zSql[i+1]=='-' ){
2290             for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
2291             if( zSql[i]=='\0' ) break;
2292           }else if( c=='/' && zSql[i+1]=='*' ){
2293             for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
2294             if( zSql[i]=='\0' ) break;
2295             i++;
2296           }
2297         }
2298         if( zSql[i] ){
2299           zRet = rbuStrndup(&zSql[i], &rc);
2300         }
2301         pIter->nIdxCol = iIdxCol;
2302       }
2303     }
2304 
2305     rc2 = sqlite3_finalize(pStmt);
2306     if( rc==SQLITE_OK ) rc = rc2;
2307   }
2308 
2309   p->rc = rc;
2310   return zRet;
2311 }
2312 
2313 /*
2314 ** Ensure that the SQLite statement handles required to update the
2315 ** target database object currently indicated by the iterator passed
2316 ** as the second argument are available.
2317 */
rbuObjIterPrepareAll(sqlite3rbu * p,RbuObjIter * pIter,int nOffset)2318 static int rbuObjIterPrepareAll(
2319   sqlite3rbu *p,
2320   RbuObjIter *pIter,
2321   int nOffset                     /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
2322 ){
2323   assert( pIter->bCleanup==0 );
2324   if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
2325     const int tnum = pIter->iTnum;
2326     char *zCollist = 0;           /* List of indexed columns */
2327     char **pz = &p->zErrmsg;
2328     const char *zIdx = pIter->zIdx;
2329     char *zLimit = 0;
2330 
2331     if( nOffset ){
2332       zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
2333       if( !zLimit ) p->rc = SQLITE_NOMEM;
2334     }
2335 
2336     if( zIdx ){
2337       const char *zTbl = pIter->zTbl;
2338       char *zImposterCols = 0;    /* Columns for imposter table */
2339       char *zImposterPK = 0;      /* Primary key declaration for imposter */
2340       char *zWhere = 0;           /* WHERE clause on PK columns */
2341       char *zBind = 0;
2342       char *zPart = 0;
2343       int nBind = 0;
2344 
2345       assert( pIter->eType!=RBU_PK_VTAB );
2346       zPart = rbuObjIterGetIndexWhere(p, pIter);
2347       zCollist = rbuObjIterGetIndexCols(
2348           p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
2349       );
2350       zBind = rbuObjIterGetBindlist(p, nBind);
2351 
2352       /* Create the imposter table used to write to this index. */
2353       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
2354       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
2355       rbuMPrintfExec(p, p->dbMain,
2356           "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
2357           zTbl, zImposterCols, zImposterPK
2358       );
2359       sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2360 
2361       /* Create the statement to insert index entries */
2362       pIter->nCol = nBind;
2363       if( p->rc==SQLITE_OK ){
2364         p->rc = prepareFreeAndCollectError(
2365             p->dbMain, &pIter->pInsert, &p->zErrmsg,
2366           sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
2367         );
2368       }
2369 
2370       /* And to delete index entries */
2371       if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
2372         p->rc = prepareFreeAndCollectError(
2373             p->dbMain, &pIter->pDelete, &p->zErrmsg,
2374           sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
2375         );
2376       }
2377 
2378       /* Create the SELECT statement to read keys in sorted order */
2379       if( p->rc==SQLITE_OK ){
2380         char *zSql;
2381         if( rbuIsVacuum(p) ){
2382           char *zStart = 0;
2383           if( nOffset ){
2384             zStart = rbuVacuumIndexStart(p, pIter);
2385             if( zStart ){
2386               sqlite3_free(zLimit);
2387               zLimit = 0;
2388             }
2389           }
2390 
2391           zSql = sqlite3_mprintf(
2392               "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
2393               zCollist,
2394               pIter->zDataTbl,
2395               zPart,
2396               (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
2397               zCollist, zLimit
2398           );
2399           sqlite3_free(zStart);
2400         }else
2401 
2402         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2403           zSql = sqlite3_mprintf(
2404               "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
2405               zCollist, p->zStateDb, pIter->zDataTbl,
2406               zPart, zCollist, zLimit
2407           );
2408         }else{
2409           zSql = sqlite3_mprintf(
2410               "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
2411               "UNION ALL "
2412               "SELECT %s, rbu_control FROM '%q' "
2413               "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
2414               "ORDER BY %s%s",
2415               zCollist, p->zStateDb, pIter->zDataTbl, zPart,
2416               zCollist, pIter->zDataTbl,
2417               zPart,
2418               (zPart ? "AND" : "WHERE"),
2419               zCollist, zLimit
2420           );
2421         }
2422         if( p->rc==SQLITE_OK ){
2423           p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
2424         }else{
2425           sqlite3_free(zSql);
2426         }
2427       }
2428 
2429       sqlite3_free(zImposterCols);
2430       sqlite3_free(zImposterPK);
2431       sqlite3_free(zWhere);
2432       sqlite3_free(zBind);
2433       sqlite3_free(zPart);
2434     }else{
2435       int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
2436                     ||(pIter->eType==RBU_PK_NONE)
2437                     ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
2438       const char *zTbl = pIter->zTbl;       /* Table this step applies to */
2439       const char *zWrite;                   /* Imposter table name */
2440 
2441       char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
2442       char *zWhere = rbuObjIterGetWhere(p, pIter);
2443       char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
2444       char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
2445 
2446       zCollist = rbuObjIterGetCollist(p, pIter);
2447       pIter->nCol = pIter->nTblCol;
2448 
2449       /* Create the imposter table or tables (if required). */
2450       rbuCreateImposterTable(p, pIter);
2451       rbuCreateImposterTable2(p, pIter);
2452       zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
2453 
2454       /* Create the INSERT statement to write to the target PK b-tree */
2455       if( p->rc==SQLITE_OK ){
2456         p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
2457             sqlite3_mprintf(
2458               "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
2459               zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
2460             )
2461         );
2462       }
2463 
2464       /* Create the DELETE statement to write to the target PK b-tree.
2465       ** Because it only performs INSERT operations, this is not required for
2466       ** an rbu vacuum handle.  */
2467       if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
2468         p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
2469             sqlite3_mprintf(
2470               "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
2471             )
2472         );
2473       }
2474 
2475       if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
2476         const char *zRbuRowid = "";
2477         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2478           zRbuRowid = ", rbu_rowid";
2479         }
2480 
2481         /* Create the rbu_tmp_xxx table and the triggers to populate it. */
2482         rbuMPrintfExec(p, p->dbRbu,
2483             "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
2484             "SELECT *%s FROM '%q' WHERE 0;"
2485             , p->zStateDb, pIter->zDataTbl
2486             , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
2487             , pIter->zDataTbl
2488         );
2489 
2490         rbuMPrintfExec(p, p->dbMain,
2491             "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
2492             "BEGIN "
2493             "  SELECT rbu_tmp_insert(3, %s);"
2494             "END;"
2495 
2496             "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
2497             "BEGIN "
2498             "  SELECT rbu_tmp_insert(3, %s);"
2499             "END;"
2500 
2501             "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
2502             "BEGIN "
2503             "  SELECT rbu_tmp_insert(4, %s);"
2504             "END;",
2505             zWrite, zTbl, zOldlist,
2506             zWrite, zTbl, zOldlist,
2507             zWrite, zTbl, zNewlist
2508         );
2509 
2510         if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2511           rbuMPrintfExec(p, p->dbMain,
2512               "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
2513               "BEGIN "
2514               "  SELECT rbu_tmp_insert(0, %s);"
2515               "END;",
2516               zWrite, zTbl, zNewlist
2517           );
2518         }
2519 
2520         rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
2521       }
2522 
2523       /* Create the SELECT statement to read keys from data_xxx */
2524       if( p->rc==SQLITE_OK ){
2525         const char *zRbuRowid = "";
2526         char *zStart = 0;
2527         char *zOrder = 0;
2528         if( bRbuRowid ){
2529           zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
2530         }
2531 
2532         if( rbuIsVacuum(p) ){
2533           if( nOffset ){
2534             zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
2535             if( zStart ){
2536               sqlite3_free(zLimit);
2537               zLimit = 0;
2538             }
2539           }
2540           if( bRbuRowid ){
2541             zOrder = rbuMPrintf(p, "_rowid_");
2542           }else{
2543             zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
2544           }
2545         }
2546 
2547         if( p->rc==SQLITE_OK ){
2548           p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
2549               sqlite3_mprintf(
2550                 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
2551                 zCollist,
2552                 (rbuIsVacuum(p) ? "0 AS " : ""),
2553                 zRbuRowid,
2554                 pIter->zDataTbl, (zStart ? zStart : ""),
2555                 (zOrder ? "ORDER BY" : ""), zOrder,
2556                 zLimit
2557               )
2558           );
2559         }
2560         sqlite3_free(zStart);
2561         sqlite3_free(zOrder);
2562       }
2563 
2564       sqlite3_free(zWhere);
2565       sqlite3_free(zOldlist);
2566       sqlite3_free(zNewlist);
2567       sqlite3_free(zBindings);
2568     }
2569     sqlite3_free(zCollist);
2570     sqlite3_free(zLimit);
2571   }
2572 
2573   return p->rc;
2574 }
2575 
2576 /*
2577 ** Set output variable *ppStmt to point to an UPDATE statement that may
2578 ** be used to update the imposter table for the main table b-tree of the
2579 ** table object that pIter currently points to, assuming that the
2580 ** rbu_control column of the data_xyz table contains zMask.
2581 **
2582 ** If the zMask string does not specify any columns to update, then this
2583 ** is not an error. Output variable *ppStmt is set to NULL in this case.
2584 */
rbuGetUpdateStmt(sqlite3rbu * p,RbuObjIter * pIter,const char * zMask,sqlite3_stmt ** ppStmt)2585 static int rbuGetUpdateStmt(
2586   sqlite3rbu *p,                  /* RBU handle */
2587   RbuObjIter *pIter,              /* Object iterator */
2588   const char *zMask,              /* rbu_control value ('x.x.') */
2589   sqlite3_stmt **ppStmt           /* OUT: UPDATE statement handle */
2590 ){
2591   RbuUpdateStmt **pp;
2592   RbuUpdateStmt *pUp = 0;
2593   int nUp = 0;
2594 
2595   /* In case an error occurs */
2596   *ppStmt = 0;
2597 
2598   /* Search for an existing statement. If one is found, shift it to the front
2599   ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
2600   ** to the number of statements currently in the cache and pUp to the
2601   ** last object in the list.  */
2602   for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
2603     pUp = *pp;
2604     if( strcmp(pUp->zMask, zMask)==0 ){
2605       *pp = pUp->pNext;
2606       pUp->pNext = pIter->pRbuUpdate;
2607       pIter->pRbuUpdate = pUp;
2608       *ppStmt = pUp->pUpdate;
2609       return SQLITE_OK;
2610     }
2611     nUp++;
2612   }
2613   assert( pUp==0 || pUp->pNext==0 );
2614 
2615   if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
2616     for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
2617     *pp = 0;
2618     sqlite3_finalize(pUp->pUpdate);
2619     pUp->pUpdate = 0;
2620   }else{
2621     pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
2622   }
2623 
2624   if( pUp ){
2625     char *zWhere = rbuObjIterGetWhere(p, pIter);
2626     char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
2627     char *zUpdate = 0;
2628 
2629     pUp->zMask = (char*)&pUp[1];
2630     memcpy(pUp->zMask, zMask, pIter->nTblCol);
2631     pUp->pNext = pIter->pRbuUpdate;
2632     pIter->pRbuUpdate = pUp;
2633 
2634     if( zSet ){
2635       const char *zPrefix = "";
2636 
2637       if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
2638       zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
2639           zPrefix, pIter->zTbl, zSet, zWhere
2640       );
2641       p->rc = prepareFreeAndCollectError(
2642           p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
2643       );
2644       *ppStmt = pUp->pUpdate;
2645     }
2646     sqlite3_free(zWhere);
2647     sqlite3_free(zSet);
2648   }
2649 
2650   return p->rc;
2651 }
2652 
rbuOpenDbhandle(sqlite3rbu * p,const char * zName,int bUseVfs)2653 static sqlite3 *rbuOpenDbhandle(
2654   sqlite3rbu *p,
2655   const char *zName,
2656   int bUseVfs
2657 ){
2658   sqlite3 *db = 0;
2659   if( p->rc==SQLITE_OK ){
2660     const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
2661     p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
2662     if( p->rc ){
2663       p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2664       sqlite3_close(db);
2665       db = 0;
2666     }
2667   }
2668   return db;
2669 }
2670 
2671 /*
2672 ** Free an RbuState object allocated by rbuLoadState().
2673 */
rbuFreeState(RbuState * p)2674 static void rbuFreeState(RbuState *p){
2675   if( p ){
2676     sqlite3_free(p->zTbl);
2677     sqlite3_free(p->zDataTbl);
2678     sqlite3_free(p->zIdx);
2679     sqlite3_free(p);
2680   }
2681 }
2682 
2683 /*
2684 ** Allocate an RbuState object and load the contents of the rbu_state
2685 ** table into it. Return a pointer to the new object. It is the
2686 ** responsibility of the caller to eventually free the object using
2687 ** sqlite3_free().
2688 **
2689 ** If an error occurs, leave an error code and message in the rbu handle
2690 ** and return NULL.
2691 */
rbuLoadState(sqlite3rbu * p)2692 static RbuState *rbuLoadState(sqlite3rbu *p){
2693   RbuState *pRet = 0;
2694   sqlite3_stmt *pStmt = 0;
2695   int rc;
2696   int rc2;
2697 
2698   pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
2699   if( pRet==0 ) return 0;
2700 
2701   rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
2702       sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
2703   );
2704   while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
2705     switch( sqlite3_column_int(pStmt, 0) ){
2706       case RBU_STATE_STAGE:
2707         pRet->eStage = sqlite3_column_int(pStmt, 1);
2708         if( pRet->eStage!=RBU_STAGE_OAL
2709          && pRet->eStage!=RBU_STAGE_MOVE
2710          && pRet->eStage!=RBU_STAGE_CKPT
2711         ){
2712           p->rc = SQLITE_CORRUPT;
2713         }
2714         break;
2715 
2716       case RBU_STATE_TBL:
2717         pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2718         break;
2719 
2720       case RBU_STATE_IDX:
2721         pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2722         break;
2723 
2724       case RBU_STATE_ROW:
2725         pRet->nRow = sqlite3_column_int(pStmt, 1);
2726         break;
2727 
2728       case RBU_STATE_PROGRESS:
2729         pRet->nProgress = sqlite3_column_int64(pStmt, 1);
2730         break;
2731 
2732       case RBU_STATE_CKPT:
2733         pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
2734         break;
2735 
2736       case RBU_STATE_COOKIE:
2737         pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
2738         break;
2739 
2740       case RBU_STATE_OALSZ:
2741         pRet->iOalSz = sqlite3_column_int64(pStmt, 1);
2742         break;
2743 
2744       case RBU_STATE_PHASEONESTEP:
2745         pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
2746         break;
2747 
2748       case RBU_STATE_DATATBL:
2749         pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2750         break;
2751 
2752       default:
2753         rc = SQLITE_CORRUPT;
2754         break;
2755     }
2756   }
2757   rc2 = sqlite3_finalize(pStmt);
2758   if( rc==SQLITE_OK ) rc = rc2;
2759 
2760   p->rc = rc;
2761   return pRet;
2762 }
2763 
2764 
2765 /*
2766 ** Open the database handle and attach the RBU database as "rbu". If an
2767 ** error occurs, leave an error code and message in the RBU handle.
2768 **
2769 ** If argument dbMain is not NULL, then it is a database handle already
2770 ** open on the target database. Use this handle instead of opening a new
2771 ** one.
2772 */
rbuOpenDatabase(sqlite3rbu * p,sqlite3 * dbMain,int * pbRetry)2773 static void rbuOpenDatabase(sqlite3rbu *p, sqlite3 *dbMain, int *pbRetry){
2774   assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
2775   assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
2776   assert( dbMain==0 || rbuIsVacuum(p)==0 );
2777 
2778   /* Open the RBU database */
2779   p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
2780   p->dbMain = dbMain;
2781 
2782   if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2783     sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
2784     if( p->zState==0 ){
2785       const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
2786       p->zState = rbuMPrintf(p, "file:///%s-vacuum?modeof=%s", zFile, zFile);
2787     }
2788   }
2789 
2790   /* If using separate RBU and state databases, attach the state database to
2791   ** the RBU db handle now.  */
2792   if( p->zState ){
2793     rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
2794     memcpy(p->zStateDb, "stat", 4);
2795   }else{
2796     memcpy(p->zStateDb, "main", 4);
2797   }
2798 
2799 #if 0
2800   if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2801     p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
2802   }
2803 #endif
2804 
2805   /* If it has not already been created, create the rbu_state table */
2806   rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
2807 
2808 #if 0
2809   if( rbuIsVacuum(p) ){
2810     if( p->rc==SQLITE_OK ){
2811       int rc2;
2812       int bOk = 0;
2813       sqlite3_stmt *pCnt = 0;
2814       p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
2815           "SELECT count(*) FROM stat.sqlite_schema"
2816       );
2817       if( p->rc==SQLITE_OK
2818        && sqlite3_step(pCnt)==SQLITE_ROW
2819        && 1==sqlite3_column_int(pCnt, 0)
2820       ){
2821         bOk = 1;
2822       }
2823       rc2 = sqlite3_finalize(pCnt);
2824       if( p->rc==SQLITE_OK ) p->rc = rc2;
2825 
2826       if( p->rc==SQLITE_OK && bOk==0 ){
2827         p->rc = SQLITE_ERROR;
2828         p->zErrmsg = sqlite3_mprintf("invalid state database");
2829       }
2830 
2831       if( p->rc==SQLITE_OK ){
2832         p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
2833       }
2834     }
2835   }
2836 #endif
2837 
2838   if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2839     int bOpen = 0;
2840     int rc;
2841     p->nRbu = 0;
2842     p->pRbuFd = 0;
2843     rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
2844     if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
2845     if( p->eStage>=RBU_STAGE_MOVE ){
2846       bOpen = 1;
2847     }else{
2848       RbuState *pState = rbuLoadState(p);
2849       if( pState ){
2850         bOpen = (pState->eStage>=RBU_STAGE_MOVE);
2851         rbuFreeState(pState);
2852       }
2853     }
2854     if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
2855   }
2856 
2857   p->eStage = 0;
2858   if( p->rc==SQLITE_OK && p->dbMain==0 ){
2859     if( !rbuIsVacuum(p) ){
2860       p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
2861     }else if( p->pRbuFd->pWalFd ){
2862       if( pbRetry ){
2863         p->pRbuFd->bNolock = 0;
2864         sqlite3_close(p->dbRbu);
2865         sqlite3_close(p->dbMain);
2866         p->dbMain = 0;
2867         p->dbRbu = 0;
2868         *pbRetry = 1;
2869         return;
2870       }
2871       p->rc = SQLITE_ERROR;
2872       p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
2873     }else{
2874       char *zTarget;
2875       char *zExtra = 0;
2876       if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
2877         zExtra = &p->zRbu[5];
2878         while( *zExtra ){
2879           if( *zExtra++=='?' ) break;
2880         }
2881         if( *zExtra=='\0' ) zExtra = 0;
2882       }
2883 
2884       zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
2885           sqlite3_db_filename(p->dbRbu, "main"),
2886           (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
2887       );
2888 
2889       if( zTarget==0 ){
2890         p->rc = SQLITE_NOMEM;
2891         return;
2892       }
2893       p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
2894       sqlite3_free(zTarget);
2895     }
2896   }
2897 
2898   if( p->rc==SQLITE_OK ){
2899     p->rc = sqlite3_create_function(p->dbMain,
2900         "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
2901     );
2902   }
2903 
2904   if( p->rc==SQLITE_OK ){
2905     p->rc = sqlite3_create_function(p->dbMain,
2906         "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
2907     );
2908   }
2909 
2910   if( p->rc==SQLITE_OK ){
2911     p->rc = sqlite3_create_function(p->dbRbu,
2912         "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
2913     );
2914   }
2915 
2916   if( p->rc==SQLITE_OK ){
2917     p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
2918   }
2919   rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_schema");
2920 
2921   /* Mark the database file just opened as an RBU target database. If
2922   ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
2923   ** This is an error.  */
2924   if( p->rc==SQLITE_OK ){
2925     p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
2926   }
2927 
2928   if( p->rc==SQLITE_NOTFOUND ){
2929     p->rc = SQLITE_ERROR;
2930     p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
2931   }
2932 }
2933 
2934 /*
2935 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
2936 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
2937 **
2938 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
2939 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
2940 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
2941 ** three characters, then shorten the suffix on z[] to be the last three
2942 ** characters of the original suffix.
2943 **
2944 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
2945 ** do the suffix shortening regardless of URI parameter.
2946 **
2947 ** Examples:
2948 **
2949 **     test.db-journal    =>   test.nal
2950 **     test.db-wal        =>   test.wal
2951 **     test.db-shm        =>   test.shm
2952 **     test.db-mj7f3319fa =>   test.9fa
2953 */
rbuFileSuffix3(const char * zBase,char * z)2954 static void rbuFileSuffix3(const char *zBase, char *z){
2955 #ifdef SQLITE_ENABLE_8_3_NAMES
2956 #if SQLITE_ENABLE_8_3_NAMES<2
2957   if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
2958 #endif
2959   {
2960     int i, sz;
2961     sz = (int)strlen(z)&0xffffff;
2962     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
2963     if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
2964   }
2965 #endif
2966 }
2967 
2968 /*
2969 ** Return the current wal-index header checksum for the target database
2970 ** as a 64-bit integer.
2971 **
2972 ** The checksum is store in the first page of xShmMap memory as an 8-byte
2973 ** blob starting at byte offset 40.
2974 */
rbuShmChecksum(sqlite3rbu * p)2975 static i64 rbuShmChecksum(sqlite3rbu *p){
2976   i64 iRet = 0;
2977   if( p->rc==SQLITE_OK ){
2978     sqlite3_file *pDb = p->pTargetFd->pReal;
2979     u32 volatile *ptr;
2980     p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
2981     if( p->rc==SQLITE_OK ){
2982       iRet = ((i64)ptr[10] << 32) + ptr[11];
2983     }
2984   }
2985   return iRet;
2986 }
2987 
2988 /*
2989 ** This function is called as part of initializing or reinitializing an
2990 ** incremental checkpoint.
2991 **
2992 ** It populates the sqlite3rbu.aFrame[] array with the set of
2993 ** (wal frame -> db page) copy operations required to checkpoint the
2994 ** current wal file, and obtains the set of shm locks required to safely
2995 ** perform the copy operations directly on the file-system.
2996 **
2997 ** If argument pState is not NULL, then the incremental checkpoint is
2998 ** being resumed. In this case, if the checksum of the wal-index-header
2999 ** following recovery is not the same as the checksum saved in the RbuState
3000 ** object, then the rbu handle is set to DONE state. This occurs if some
3001 ** other client appends a transaction to the wal file in the middle of
3002 ** an incremental checkpoint.
3003 */
rbuSetupCheckpoint(sqlite3rbu * p,RbuState * pState)3004 static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
3005 
3006   /* If pState is NULL, then the wal file may not have been opened and
3007   ** recovered. Running a read-statement here to ensure that doing so
3008   ** does not interfere with the "capture" process below.  */
3009   if( pState==0 ){
3010     p->eStage = 0;
3011     if( p->rc==SQLITE_OK ){
3012       p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_schema", 0, 0, 0);
3013     }
3014   }
3015 
3016   /* Assuming no error has occurred, run a "restart" checkpoint with the
3017   ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
3018   ** special behaviour in the rbu VFS:
3019   **
3020   **   * If the exclusive shm WRITER or READ0 lock cannot be obtained,
3021   **     the checkpoint fails with SQLITE_BUSY (normally SQLite would
3022   **     proceed with running a passive checkpoint instead of failing).
3023   **
3024   **   * Attempts to read from the *-wal file or write to the database file
3025   **     do not perform any IO. Instead, the frame/page combinations that
3026   **     would be read/written are recorded in the sqlite3rbu.aFrame[]
3027   **     array.
3028   **
3029   **   * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
3030   **     READ0 and CHECKPOINT locks taken as part of the checkpoint are
3031   **     no-ops. These locks will not be released until the connection
3032   **     is closed.
3033   **
3034   **   * Attempting to xSync() the database file causes an SQLITE_INTERNAL
3035   **     error.
3036   **
3037   ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
3038   ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
3039   ** array populated with a set of (frame -> page) mappings. Because the
3040   ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
3041   ** data from the wal file into the database file according to the
3042   ** contents of aFrame[].
3043   */
3044   if( p->rc==SQLITE_OK ){
3045     int rc2;
3046     p->eStage = RBU_STAGE_CAPTURE;
3047     rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
3048     if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
3049   }
3050 
3051   if( p->rc==SQLITE_OK && p->nFrame>0 ){
3052     p->eStage = RBU_STAGE_CKPT;
3053     p->nStep = (pState ? pState->nRow : 0);
3054     p->aBuf = rbuMalloc(p, p->pgsz);
3055     p->iWalCksum = rbuShmChecksum(p);
3056   }
3057 
3058   if( p->rc==SQLITE_OK ){
3059     if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
3060       p->rc = SQLITE_DONE;
3061       p->eStage = RBU_STAGE_DONE;
3062     }else{
3063       int nSectorSize;
3064       sqlite3_file *pDb = p->pTargetFd->pReal;
3065       sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
3066       assert( p->nPagePerSector==0 );
3067       nSectorSize = pDb->pMethods->xSectorSize(pDb);
3068       if( nSectorSize>p->pgsz ){
3069         p->nPagePerSector = nSectorSize / p->pgsz;
3070       }else{
3071         p->nPagePerSector = 1;
3072       }
3073 
3074       /* Call xSync() on the wal file. This causes SQLite to sync the
3075       ** directory in which the target database and the wal file reside, in
3076       ** case it has not been synced since the rename() call in
3077       ** rbuMoveOalFile(). */
3078       p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL);
3079     }
3080   }
3081 }
3082 
3083 /*
3084 ** Called when iAmt bytes are read from offset iOff of the wal file while
3085 ** the rbu object is in capture mode. Record the frame number of the frame
3086 ** being read in the aFrame[] array.
3087 */
rbuCaptureWalRead(sqlite3rbu * pRbu,i64 iOff,int iAmt)3088 static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
3089   const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
3090   u32 iFrame;
3091 
3092   if( pRbu->mLock!=mReq ){
3093     pRbu->rc = SQLITE_BUSY;
3094     return SQLITE_INTERNAL;
3095   }
3096 
3097   pRbu->pgsz = iAmt;
3098   if( pRbu->nFrame==pRbu->nFrameAlloc ){
3099     int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
3100     RbuFrame *aNew;
3101     aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
3102     if( aNew==0 ) return SQLITE_NOMEM;
3103     pRbu->aFrame = aNew;
3104     pRbu->nFrameAlloc = nNew;
3105   }
3106 
3107   iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
3108   if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
3109   pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
3110   pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
3111   pRbu->nFrame++;
3112   return SQLITE_OK;
3113 }
3114 
3115 /*
3116 ** Called when a page of data is written to offset iOff of the database
3117 ** file while the rbu handle is in capture mode. Record the page number
3118 ** of the page being written in the aFrame[] array.
3119 */
rbuCaptureDbWrite(sqlite3rbu * pRbu,i64 iOff)3120 static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
3121   pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
3122   return SQLITE_OK;
3123 }
3124 
3125 /*
3126 ** This is called as part of an incremental checkpoint operation. Copy
3127 ** a single frame of data from the wal file into the database file, as
3128 ** indicated by the RbuFrame object.
3129 */
rbuCheckpointFrame(sqlite3rbu * p,RbuFrame * pFrame)3130 static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
3131   sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
3132   sqlite3_file *pDb = p->pTargetFd->pReal;
3133   i64 iOff;
3134 
3135   assert( p->rc==SQLITE_OK );
3136   iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
3137   p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
3138   if( p->rc ) return;
3139 
3140   iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
3141   p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
3142 }
3143 
3144 
3145 /*
3146 ** Take an EXCLUSIVE lock on the database file. Return SQLITE_OK if
3147 ** successful, or an SQLite error code otherwise.
3148 */
rbuLockDatabase(sqlite3 * db)3149 static int rbuLockDatabase(sqlite3 *db){
3150   int rc = SQLITE_OK;
3151   sqlite3_file *fd = 0;
3152   sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &fd);
3153 
3154   if( fd->pMethods ){
3155     rc = fd->pMethods->xLock(fd, SQLITE_LOCK_SHARED);
3156     if( rc==SQLITE_OK ){
3157       rc = fd->pMethods->xLock(fd, SQLITE_LOCK_EXCLUSIVE);
3158     }
3159   }
3160   return rc;
3161 }
3162 
3163 /*
3164 ** Return true if the database handle passed as the only argument
3165 ** was opened with the rbu_exclusive_checkpoint=1 URI parameter
3166 ** specified. Or false otherwise.
3167 */
rbuExclusiveCheckpoint(sqlite3 * db)3168 static int rbuExclusiveCheckpoint(sqlite3 *db){
3169   const char *zUri = sqlite3_db_filename(db, 0);
3170   return sqlite3_uri_boolean(zUri, RBU_EXCLUSIVE_CHECKPOINT, 0);
3171 }
3172 
3173 #if defined(_WIN32_WCE)
rbuWinUtf8ToUnicode(const char * zFilename)3174 static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
3175   int nChar;
3176   LPWSTR zWideFilename;
3177 
3178   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
3179   if( nChar==0 ){
3180     return 0;
3181   }
3182   zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
3183   if( zWideFilename==0 ){
3184     return 0;
3185   }
3186   memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
3187   nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
3188                                 nChar);
3189   if( nChar==0 ){
3190     sqlite3_free(zWideFilename);
3191     zWideFilename = 0;
3192   }
3193   return zWideFilename;
3194 }
3195 #endif
3196 
3197 /*
3198 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
3199 ** on the database file. This proc moves the *-oal file to the *-wal path,
3200 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
3201 ** If an error occurs, leave an error code and error message in the rbu
3202 ** handle.
3203 */
rbuMoveOalFile(sqlite3rbu * p)3204 static void rbuMoveOalFile(sqlite3rbu *p){
3205   const char *zBase = sqlite3_db_filename(p->dbMain, "main");
3206   const char *zMove = zBase;
3207   char *zOal;
3208   char *zWal;
3209 
3210   if( rbuIsVacuum(p) ){
3211     zMove = sqlite3_db_filename(p->dbRbu, "main");
3212   }
3213   zOal = sqlite3_mprintf("%s-oal", zMove);
3214   zWal = sqlite3_mprintf("%s-wal", zMove);
3215 
3216   assert( p->eStage==RBU_STAGE_MOVE );
3217   assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
3218   if( zWal==0 || zOal==0 ){
3219     p->rc = SQLITE_NOMEM;
3220   }else{
3221     /* Move the *-oal file to *-wal. At this point connection p->db is
3222     ** holding a SHARED lock on the target database file (because it is
3223     ** in WAL mode). So no other connection may be writing the db.
3224     **
3225     ** In order to ensure that there are no database readers, an EXCLUSIVE
3226     ** lock is obtained here before the *-oal is moved to *-wal.
3227     */
3228     sqlite3 *dbMain = 0;
3229     rbuFileSuffix3(zBase, zWal);
3230     rbuFileSuffix3(zBase, zOal);
3231 
3232     /* Re-open the databases. */
3233     rbuObjIterFinalize(&p->objiter);
3234     sqlite3_close(p->dbRbu);
3235     sqlite3_close(p->dbMain);
3236     p->dbMain = 0;
3237     p->dbRbu = 0;
3238 
3239     dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
3240     if( dbMain ){
3241       assert( p->rc==SQLITE_OK );
3242       p->rc = rbuLockDatabase(dbMain);
3243     }
3244 
3245     if( p->rc==SQLITE_OK ){
3246       p->rc = p->xRename(p->pRenameArg, zOal, zWal);
3247     }
3248 
3249     if( p->rc!=SQLITE_OK
3250      || rbuIsVacuum(p)
3251      || rbuExclusiveCheckpoint(dbMain)==0
3252     ){
3253       sqlite3_close(dbMain);
3254       dbMain = 0;
3255     }
3256 
3257     if( p->rc==SQLITE_OK ){
3258       rbuOpenDatabase(p, dbMain, 0);
3259       rbuSetupCheckpoint(p, 0);
3260     }
3261   }
3262 
3263   sqlite3_free(zWal);
3264   sqlite3_free(zOal);
3265 }
3266 
3267 /*
3268 ** The SELECT statement iterating through the keys for the current object
3269 ** (p->objiter.pSelect) currently points to a valid row. This function
3270 ** determines the type of operation requested by this row and returns
3271 ** one of the following values to indicate the result:
3272 **
3273 **     * RBU_INSERT
3274 **     * RBU_DELETE
3275 **     * RBU_IDX_DELETE
3276 **     * RBU_UPDATE
3277 **
3278 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
3279 ** point to the text value indicating the columns to update.
3280 **
3281 ** If the rbu_control field contains an invalid value, an error code and
3282 ** message are left in the RBU handle and zero returned.
3283 */
rbuStepType(sqlite3rbu * p,const char ** pzMask)3284 static int rbuStepType(sqlite3rbu *p, const char **pzMask){
3285   int iCol = p->objiter.nCol;     /* Index of rbu_control column */
3286   int res = 0;                    /* Return value */
3287 
3288   switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
3289     case SQLITE_INTEGER: {
3290       int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
3291       switch( iVal ){
3292         case 0: res = RBU_INSERT;     break;
3293         case 1: res = RBU_DELETE;     break;
3294         case 2: res = RBU_REPLACE;    break;
3295         case 3: res = RBU_IDX_DELETE; break;
3296         case 4: res = RBU_IDX_INSERT; break;
3297       }
3298       break;
3299     }
3300 
3301     case SQLITE_TEXT: {
3302       const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
3303       if( z==0 ){
3304         p->rc = SQLITE_NOMEM;
3305       }else{
3306         *pzMask = (const char*)z;
3307       }
3308       res = RBU_UPDATE;
3309 
3310       break;
3311     }
3312 
3313     default:
3314       break;
3315   }
3316 
3317   if( res==0 ){
3318     rbuBadControlError(p);
3319   }
3320   return res;
3321 }
3322 
3323 #ifdef SQLITE_DEBUG
3324 /*
3325 ** Assert that column iCol of statement pStmt is named zName.
3326 */
assertColumnName(sqlite3_stmt * pStmt,int iCol,const char * zName)3327 static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
3328   const char *zCol = sqlite3_column_name(pStmt, iCol);
3329   assert( 0==sqlite3_stricmp(zName, zCol) );
3330 }
3331 #else
3332 # define assertColumnName(x,y,z)
3333 #endif
3334 
3335 /*
3336 ** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
3337 ** RBU_IDX_DELETE. This function performs the work of a single
3338 ** sqlite3rbu_step() call for the type of operation specified by eType.
3339 */
rbuStepOneOp(sqlite3rbu * p,int eType)3340 static void rbuStepOneOp(sqlite3rbu *p, int eType){
3341   RbuObjIter *pIter = &p->objiter;
3342   sqlite3_value *pVal;
3343   sqlite3_stmt *pWriter;
3344   int i;
3345 
3346   assert( p->rc==SQLITE_OK );
3347   assert( eType!=RBU_DELETE || pIter->zIdx==0 );
3348   assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
3349        || eType==RBU_INSERT || eType==RBU_IDX_INSERT
3350   );
3351 
3352   /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
3353   ** statement below does actually delete a row, nPhaseOneStep will be
3354   ** incremented by the same amount when SQL function rbu_tmp_insert()
3355   ** is invoked by the trigger.  */
3356   if( eType==RBU_DELETE ){
3357     p->nPhaseOneStep -= p->objiter.nIndex;
3358   }
3359 
3360   if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
3361     pWriter = pIter->pDelete;
3362   }else{
3363     pWriter = pIter->pInsert;
3364   }
3365 
3366   for(i=0; i<pIter->nCol; i++){
3367     /* If this is an INSERT into a table b-tree and the table has an
3368     ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
3369     ** to write a NULL into the IPK column. That is not permitted.  */
3370     if( eType==RBU_INSERT
3371      && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
3372      && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
3373     ){
3374       p->rc = SQLITE_MISMATCH;
3375       p->zErrmsg = sqlite3_mprintf("datatype mismatch");
3376       return;
3377     }
3378 
3379     if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
3380       continue;
3381     }
3382 
3383     pVal = sqlite3_column_value(pIter->pSelect, i);
3384     p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
3385     if( p->rc ) return;
3386   }
3387   if( pIter->zIdx==0 ){
3388     if( pIter->eType==RBU_PK_VTAB
3389      || pIter->eType==RBU_PK_NONE
3390      || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
3391     ){
3392       /* For a virtual table, or a table with no primary key, the
3393       ** SELECT statement is:
3394       **
3395       **   SELECT <cols>, rbu_control, rbu_rowid FROM ....
3396       **
3397       ** Hence column_value(pIter->nCol+1).
3398       */
3399       assertColumnName(pIter->pSelect, pIter->nCol+1,
3400           rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
3401       );
3402       pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
3403       p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
3404     }
3405   }
3406   if( p->rc==SQLITE_OK ){
3407     sqlite3_step(pWriter);
3408     p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
3409   }
3410 }
3411 
3412 /*
3413 ** This function does the work for an sqlite3rbu_step() call.
3414 **
3415 ** The object-iterator (p->objiter) currently points to a valid object,
3416 ** and the input cursor (p->objiter.pSelect) currently points to a valid
3417 ** input row. Perform whatever processing is required and return.
3418 **
3419 ** If no  error occurs, SQLITE_OK is returned. Otherwise, an error code
3420 ** and message is left in the RBU handle and a copy of the error code
3421 ** returned.
3422 */
rbuStep(sqlite3rbu * p)3423 static int rbuStep(sqlite3rbu *p){
3424   RbuObjIter *pIter = &p->objiter;
3425   const char *zMask = 0;
3426   int eType = rbuStepType(p, &zMask);
3427 
3428   if( eType ){
3429     assert( eType==RBU_INSERT     || eType==RBU_DELETE
3430          || eType==RBU_REPLACE    || eType==RBU_IDX_DELETE
3431          || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
3432     );
3433     assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
3434 
3435     if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
3436       rbuBadControlError(p);
3437     }
3438     else if( eType==RBU_REPLACE ){
3439       if( pIter->zIdx==0 ){
3440         p->nPhaseOneStep += p->objiter.nIndex;
3441         rbuStepOneOp(p, RBU_DELETE);
3442       }
3443       if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
3444     }
3445     else if( eType!=RBU_UPDATE ){
3446       rbuStepOneOp(p, eType);
3447     }
3448     else{
3449       sqlite3_value *pVal;
3450       sqlite3_stmt *pUpdate = 0;
3451       assert( eType==RBU_UPDATE );
3452       p->nPhaseOneStep -= p->objiter.nIndex;
3453       rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
3454       if( pUpdate ){
3455         int i;
3456         for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
3457           char c = zMask[pIter->aiSrcOrder[i]];
3458           pVal = sqlite3_column_value(pIter->pSelect, i);
3459           if( pIter->abTblPk[i] || c!='.' ){
3460             p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
3461           }
3462         }
3463         if( p->rc==SQLITE_OK
3464          && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
3465         ){
3466           /* Bind the rbu_rowid value to column _rowid_ */
3467           assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
3468           pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
3469           p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
3470         }
3471         if( p->rc==SQLITE_OK ){
3472           sqlite3_step(pUpdate);
3473           p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
3474         }
3475       }
3476     }
3477   }
3478   return p->rc;
3479 }
3480 
3481 /*
3482 ** Increment the schema cookie of the main database opened by p->dbMain.
3483 **
3484 ** Or, if this is an RBU vacuum, set the schema cookie of the main db
3485 ** opened by p->dbMain to one more than the schema cookie of the main
3486 ** db opened by p->dbRbu.
3487 */
rbuIncrSchemaCookie(sqlite3rbu * p)3488 static void rbuIncrSchemaCookie(sqlite3rbu *p){
3489   if( p->rc==SQLITE_OK ){
3490     sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
3491     int iCookie = 1000000;
3492     sqlite3_stmt *pStmt;
3493 
3494     p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
3495         "PRAGMA schema_version"
3496     );
3497     if( p->rc==SQLITE_OK ){
3498       /* Coverage: it may be that this sqlite3_step() cannot fail. There
3499       ** is already a transaction open, so the prepared statement cannot
3500       ** throw an SQLITE_SCHEMA exception. The only database page the
3501       ** statement reads is page 1, which is guaranteed to be in the cache.
3502       ** And no memory allocations are required.  */
3503       if( SQLITE_ROW==sqlite3_step(pStmt) ){
3504         iCookie = sqlite3_column_int(pStmt, 0);
3505       }
3506       rbuFinalize(p, pStmt);
3507     }
3508     if( p->rc==SQLITE_OK ){
3509       rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
3510     }
3511   }
3512 }
3513 
3514 /*
3515 ** Update the contents of the rbu_state table within the rbu database. The
3516 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
3517 ** are determined by inspecting the rbu handle passed as the first argument.
3518 */
rbuSaveState(sqlite3rbu * p,int eStage)3519 static void rbuSaveState(sqlite3rbu *p, int eStage){
3520   if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
3521     sqlite3_stmt *pInsert = 0;
3522     rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
3523     int rc;
3524 
3525     assert( p->zErrmsg==0 );
3526     rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
3527         sqlite3_mprintf(
3528           "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
3529           "(%d, %d), "
3530           "(%d, %Q), "
3531           "(%d, %Q), "
3532           "(%d, %d), "
3533           "(%d, %d), "
3534           "(%d, %lld), "
3535           "(%d, %lld), "
3536           "(%d, %lld), "
3537           "(%d, %lld), "
3538           "(%d, %Q)  ",
3539           p->zStateDb,
3540           RBU_STATE_STAGE, eStage,
3541           RBU_STATE_TBL, p->objiter.zTbl,
3542           RBU_STATE_IDX, p->objiter.zIdx,
3543           RBU_STATE_ROW, p->nStep,
3544           RBU_STATE_PROGRESS, p->nProgress,
3545           RBU_STATE_CKPT, p->iWalCksum,
3546           RBU_STATE_COOKIE, (i64)pFd->iCookie,
3547           RBU_STATE_OALSZ, p->iOalSz,
3548           RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
3549           RBU_STATE_DATATBL, p->objiter.zDataTbl
3550       )
3551     );
3552     assert( pInsert==0 || rc==SQLITE_OK );
3553 
3554     if( rc==SQLITE_OK ){
3555       sqlite3_step(pInsert);
3556       rc = sqlite3_finalize(pInsert);
3557     }
3558     if( rc!=SQLITE_OK ) p->rc = rc;
3559   }
3560 }
3561 
3562 
3563 /*
3564 ** The second argument passed to this function is the name of a PRAGMA
3565 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
3566 ** This function executes the following on sqlite3rbu.dbRbu:
3567 **
3568 **   "PRAGMA main.$zPragma"
3569 **
3570 ** where $zPragma is the string passed as the second argument, then
3571 ** on sqlite3rbu.dbMain:
3572 **
3573 **   "PRAGMA main.$zPragma = $val"
3574 **
3575 ** where $val is the value returned by the first PRAGMA invocation.
3576 **
3577 ** In short, it copies the value  of the specified PRAGMA setting from
3578 ** dbRbu to dbMain.
3579 */
rbuCopyPragma(sqlite3rbu * p,const char * zPragma)3580 static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
3581   if( p->rc==SQLITE_OK ){
3582     sqlite3_stmt *pPragma = 0;
3583     p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
3584         sqlite3_mprintf("PRAGMA main.%s", zPragma)
3585     );
3586     if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
3587       p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
3588           zPragma, sqlite3_column_int(pPragma, 0)
3589       );
3590     }
3591     rbuFinalize(p, pPragma);
3592   }
3593 }
3594 
3595 /*
3596 ** The RBU handle passed as the only argument has just been opened and
3597 ** the state database is empty. If this RBU handle was opened for an
3598 ** RBU vacuum operation, create the schema in the target db.
3599 */
rbuCreateTargetSchema(sqlite3rbu * p)3600 static void rbuCreateTargetSchema(sqlite3rbu *p){
3601   sqlite3_stmt *pSql = 0;
3602   sqlite3_stmt *pInsert = 0;
3603 
3604   assert( rbuIsVacuum(p) );
3605   p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
3606   if( p->rc==SQLITE_OK ){
3607     p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
3608       "SELECT sql FROM sqlite_schema WHERE sql!='' AND rootpage!=0"
3609       " AND name!='sqlite_sequence' "
3610       " ORDER BY type DESC"
3611     );
3612   }
3613 
3614   while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
3615     const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
3616     p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
3617   }
3618   rbuFinalize(p, pSql);
3619   if( p->rc!=SQLITE_OK ) return;
3620 
3621   if( p->rc==SQLITE_OK ){
3622     p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
3623         "SELECT * FROM sqlite_schema WHERE rootpage=0 OR rootpage IS NULL"
3624     );
3625   }
3626 
3627   if( p->rc==SQLITE_OK ){
3628     p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
3629         "INSERT INTO sqlite_schema VALUES(?,?,?,?,?)"
3630     );
3631   }
3632 
3633   while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
3634     int i;
3635     for(i=0; i<5; i++){
3636       sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
3637     }
3638     sqlite3_step(pInsert);
3639     p->rc = sqlite3_reset(pInsert);
3640   }
3641   if( p->rc==SQLITE_OK ){
3642     p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
3643   }
3644 
3645   rbuFinalize(p, pSql);
3646   rbuFinalize(p, pInsert);
3647 }
3648 
3649 /*
3650 ** Step the RBU object.
3651 */
sqlite3rbu_step(sqlite3rbu * p)3652 int sqlite3rbu_step(sqlite3rbu *p){
3653   if( p ){
3654     switch( p->eStage ){
3655       case RBU_STAGE_OAL: {
3656         RbuObjIter *pIter = &p->objiter;
3657 
3658         /* If this is an RBU vacuum operation and the state table was empty
3659         ** when this handle was opened, create the target database schema. */
3660         if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
3661           rbuCreateTargetSchema(p);
3662           rbuCopyPragma(p, "user_version");
3663           rbuCopyPragma(p, "application_id");
3664         }
3665 
3666         while( p->rc==SQLITE_OK && pIter->zTbl ){
3667 
3668           if( pIter->bCleanup ){
3669             /* Clean up the rbu_tmp_xxx table for the previous table. It
3670             ** cannot be dropped as there are currently active SQL statements.
3671             ** But the contents can be deleted.  */
3672             if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
3673               rbuMPrintfExec(p, p->dbRbu,
3674                   "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
3675               );
3676             }
3677           }else{
3678             rbuObjIterPrepareAll(p, pIter, 0);
3679 
3680             /* Advance to the next row to process. */
3681             if( p->rc==SQLITE_OK ){
3682               int rc = sqlite3_step(pIter->pSelect);
3683               if( rc==SQLITE_ROW ){
3684                 p->nProgress++;
3685                 p->nStep++;
3686                 return rbuStep(p);
3687               }
3688               p->rc = sqlite3_reset(pIter->pSelect);
3689               p->nStep = 0;
3690             }
3691           }
3692 
3693           rbuObjIterNext(p, pIter);
3694         }
3695 
3696         if( p->rc==SQLITE_OK ){
3697           assert( pIter->zTbl==0 );
3698           rbuSaveState(p, RBU_STAGE_MOVE);
3699           rbuIncrSchemaCookie(p);
3700           if( p->rc==SQLITE_OK ){
3701             p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
3702           }
3703           if( p->rc==SQLITE_OK ){
3704             p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
3705           }
3706           p->eStage = RBU_STAGE_MOVE;
3707         }
3708         break;
3709       }
3710 
3711       case RBU_STAGE_MOVE: {
3712         if( p->rc==SQLITE_OK ){
3713           rbuMoveOalFile(p);
3714           p->nProgress++;
3715         }
3716         break;
3717       }
3718 
3719       case RBU_STAGE_CKPT: {
3720         if( p->rc==SQLITE_OK ){
3721           if( p->nStep>=p->nFrame ){
3722             sqlite3_file *pDb = p->pTargetFd->pReal;
3723 
3724             /* Sync the db file */
3725             p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
3726 
3727             /* Update nBackfill */
3728             if( p->rc==SQLITE_OK ){
3729               void volatile *ptr;
3730               p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
3731               if( p->rc==SQLITE_OK ){
3732                 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
3733               }
3734             }
3735 
3736             if( p->rc==SQLITE_OK ){
3737               p->eStage = RBU_STAGE_DONE;
3738               p->rc = SQLITE_DONE;
3739             }
3740           }else{
3741             /* At one point the following block copied a single frame from the
3742             ** wal file to the database file. So that one call to sqlite3rbu_step()
3743             ** checkpointed a single frame.
3744             **
3745             ** However, if the sector-size is larger than the page-size, and the
3746             ** application calls sqlite3rbu_savestate() or close() immediately
3747             ** after this step, then rbu_step() again, then a power failure occurs,
3748             ** then the database page written here may be damaged. Work around
3749             ** this by checkpointing frames until the next page in the aFrame[]
3750             ** lies on a different disk sector to the current one. */
3751             u32 iSector;
3752             do{
3753               RbuFrame *pFrame = &p->aFrame[p->nStep];
3754               iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
3755               rbuCheckpointFrame(p, pFrame);
3756               p->nStep++;
3757             }while( p->nStep<p->nFrame
3758                  && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
3759                  && p->rc==SQLITE_OK
3760             );
3761           }
3762           p->nProgress++;
3763         }
3764         break;
3765       }
3766 
3767       default:
3768         break;
3769     }
3770     return p->rc;
3771   }else{
3772     return SQLITE_NOMEM;
3773   }
3774 }
3775 
3776 /*
3777 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
3778 ** otherwise. Either or both argument may be NULL. Two NULL values are
3779 ** considered equal, and NULL is considered distinct from all other values.
3780 */
rbuStrCompare(const char * z1,const char * z2)3781 static int rbuStrCompare(const char *z1, const char *z2){
3782   if( z1==0 && z2==0 ) return 0;
3783   if( z1==0 || z2==0 ) return 1;
3784   return (sqlite3_stricmp(z1, z2)!=0);
3785 }
3786 
3787 /*
3788 ** This function is called as part of sqlite3rbu_open() when initializing
3789 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
3790 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
3791 ** things so that the next call to sqlite3rbu_step() continues on from
3792 ** where the previous rbu handle left off.
3793 **
3794 ** If an error occurs, an error code and error message are left in the
3795 ** rbu handle passed as the first argument.
3796 */
rbuSetupOal(sqlite3rbu * p,RbuState * pState)3797 static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
3798   assert( p->rc==SQLITE_OK );
3799   if( pState->zTbl ){
3800     RbuObjIter *pIter = &p->objiter;
3801     int rc = SQLITE_OK;
3802 
3803     while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
3804        || rbuStrCompare(pIter->zIdx, pState->zIdx)
3805        || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
3806        || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
3807     )){
3808       rc = rbuObjIterNext(p, pIter);
3809     }
3810 
3811     if( rc==SQLITE_OK && !pIter->zTbl ){
3812       rc = SQLITE_ERROR;
3813       p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
3814     }
3815 
3816     if( rc==SQLITE_OK ){
3817       p->nStep = pState->nRow;
3818       rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
3819     }
3820 
3821     p->rc = rc;
3822   }
3823 }
3824 
3825 /*
3826 ** If there is a "*-oal" file in the file-system corresponding to the
3827 ** target database in the file-system, delete it. If an error occurs,
3828 ** leave an error code and error message in the rbu handle.
3829 */
rbuDeleteOalFile(sqlite3rbu * p)3830 static void rbuDeleteOalFile(sqlite3rbu *p){
3831   char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
3832   if( zOal ){
3833     sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
3834     assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
3835     pVfs->xDelete(pVfs, zOal, 0);
3836     sqlite3_free(zOal);
3837   }
3838 }
3839 
3840 /*
3841 ** Allocate a private rbu VFS for the rbu handle passed as the only
3842 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
3843 ** specified a URI with a vfs=? option in place of a target database
3844 ** file name.
3845 */
rbuCreateVfs(sqlite3rbu * p)3846 static void rbuCreateVfs(sqlite3rbu *p){
3847   int rnd;
3848   char zRnd[64];
3849 
3850   assert( p->rc==SQLITE_OK );
3851   sqlite3_randomness(sizeof(int), (void*)&rnd);
3852   sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
3853   p->rc = sqlite3rbu_create_vfs(zRnd, 0);
3854   if( p->rc==SQLITE_OK ){
3855     sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
3856     assert( pVfs );
3857     p->zVfsName = pVfs->zName;
3858     ((rbu_vfs*)pVfs)->pRbu = p;
3859   }
3860 }
3861 
3862 /*
3863 ** Destroy the private VFS created for the rbu handle passed as the only
3864 ** argument by an earlier call to rbuCreateVfs().
3865 */
rbuDeleteVfs(sqlite3rbu * p)3866 static void rbuDeleteVfs(sqlite3rbu *p){
3867   if( p->zVfsName ){
3868     sqlite3rbu_destroy_vfs(p->zVfsName);
3869     p->zVfsName = 0;
3870   }
3871 }
3872 
3873 /*
3874 ** This user-defined SQL function is invoked with a single argument - the
3875 ** name of a table expected to appear in the target database. It returns
3876 ** the number of auxilliary indexes on the table.
3877 */
rbuIndexCntFunc(sqlite3_context * pCtx,int nVal,sqlite3_value ** apVal)3878 static void rbuIndexCntFunc(
3879   sqlite3_context *pCtx,
3880   int nVal,
3881   sqlite3_value **apVal
3882 ){
3883   sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
3884   sqlite3_stmt *pStmt = 0;
3885   char *zErrmsg = 0;
3886   int rc;
3887   sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
3888 
3889   assert( nVal==1 );
3890 
3891   rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg,
3892       sqlite3_mprintf("SELECT count(*) FROM sqlite_schema "
3893         "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
3894   );
3895   if( rc!=SQLITE_OK ){
3896     sqlite3_result_error(pCtx, zErrmsg, -1);
3897   }else{
3898     int nIndex = 0;
3899     if( SQLITE_ROW==sqlite3_step(pStmt) ){
3900       nIndex = sqlite3_column_int(pStmt, 0);
3901     }
3902     rc = sqlite3_finalize(pStmt);
3903     if( rc==SQLITE_OK ){
3904       sqlite3_result_int(pCtx, nIndex);
3905     }else{
3906       sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
3907     }
3908   }
3909 
3910   sqlite3_free(zErrmsg);
3911 }
3912 
3913 /*
3914 ** If the RBU database contains the rbu_count table, use it to initialize
3915 ** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
3916 ** is assumed to contain the same columns as:
3917 **
3918 **   CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
3919 **
3920 ** There should be one row in the table for each data_xxx table in the
3921 ** database. The 'tbl' column should contain the name of a data_xxx table,
3922 ** and the cnt column the number of rows it contains.
3923 **
3924 ** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
3925 ** for all rows in the rbu_count table, where nIndex is the number of
3926 ** indexes on the corresponding target database table.
3927 */
rbuInitPhaseOneSteps(sqlite3rbu * p)3928 static void rbuInitPhaseOneSteps(sqlite3rbu *p){
3929   if( p->rc==SQLITE_OK ){
3930     sqlite3_stmt *pStmt = 0;
3931     int bExists = 0;                /* True if rbu_count exists */
3932 
3933     p->nPhaseOneStep = -1;
3934 
3935     p->rc = sqlite3_create_function(p->dbRbu,
3936         "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
3937     );
3938 
3939     /* Check for the rbu_count table. If it does not exist, or if an error
3940     ** occurs, nPhaseOneStep will be left set to -1. */
3941     if( p->rc==SQLITE_OK ){
3942       p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
3943           "SELECT 1 FROM sqlite_schema WHERE tbl_name = 'rbu_count'"
3944       );
3945     }
3946     if( p->rc==SQLITE_OK ){
3947       if( SQLITE_ROW==sqlite3_step(pStmt) ){
3948         bExists = 1;
3949       }
3950       p->rc = sqlite3_finalize(pStmt);
3951     }
3952 
3953     if( p->rc==SQLITE_OK && bExists ){
3954       p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
3955           "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
3956           "FROM rbu_count"
3957       );
3958       if( p->rc==SQLITE_OK ){
3959         if( SQLITE_ROW==sqlite3_step(pStmt) ){
3960           p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
3961         }
3962         p->rc = sqlite3_finalize(pStmt);
3963       }
3964     }
3965   }
3966 }
3967 
3968 
openRbuHandle(const char * zTarget,const char * zRbu,const char * zState)3969 static sqlite3rbu *openRbuHandle(
3970   const char *zTarget,
3971   const char *zRbu,
3972   const char *zState
3973 ){
3974   sqlite3rbu *p;
3975   size_t nTarget = zTarget ? strlen(zTarget) : 0;
3976   size_t nRbu = strlen(zRbu);
3977   size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
3978 
3979   p = (sqlite3rbu*)sqlite3_malloc64(nByte);
3980   if( p ){
3981     RbuState *pState = 0;
3982 
3983     /* Create the custom VFS. */
3984     memset(p, 0, sizeof(sqlite3rbu));
3985     sqlite3rbu_rename_handler(p, 0, 0);
3986     rbuCreateVfs(p);
3987 
3988     /* Open the target, RBU and state databases */
3989     if( p->rc==SQLITE_OK ){
3990       char *pCsr = (char*)&p[1];
3991       int bRetry = 0;
3992       if( zTarget ){
3993         p->zTarget = pCsr;
3994         memcpy(p->zTarget, zTarget, nTarget+1);
3995         pCsr += nTarget+1;
3996       }
3997       p->zRbu = pCsr;
3998       memcpy(p->zRbu, zRbu, nRbu+1);
3999       pCsr += nRbu+1;
4000       if( zState ){
4001         p->zState = rbuMPrintf(p, "%s", zState);
4002       }
4003 
4004       /* If the first attempt to open the database file fails and the bRetry
4005       ** flag it set, this means that the db was not opened because it seemed
4006       ** to be a wal-mode db. But, this may have happened due to an earlier
4007       ** RBU vacuum operation leaving an old wal file in the directory.
4008       ** If this is the case, it will have been checkpointed and deleted
4009       ** when the handle was closed and a second attempt to open the
4010       ** database may succeed.  */
4011       rbuOpenDatabase(p, 0, &bRetry);
4012       if( bRetry ){
4013         rbuOpenDatabase(p, 0, 0);
4014       }
4015     }
4016 
4017     if( p->rc==SQLITE_OK ){
4018       pState = rbuLoadState(p);
4019       assert( pState || p->rc!=SQLITE_OK );
4020       if( p->rc==SQLITE_OK ){
4021 
4022         if( pState->eStage==0 ){
4023           rbuDeleteOalFile(p);
4024           rbuInitPhaseOneSteps(p);
4025           p->eStage = RBU_STAGE_OAL;
4026         }else{
4027           p->eStage = pState->eStage;
4028           p->nPhaseOneStep = pState->nPhaseOneStep;
4029         }
4030         p->nProgress = pState->nProgress;
4031         p->iOalSz = pState->iOalSz;
4032       }
4033     }
4034     assert( p->rc!=SQLITE_OK || p->eStage!=0 );
4035 
4036     if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
4037       if( p->eStage==RBU_STAGE_OAL ){
4038         p->rc = SQLITE_ERROR;
4039         p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
4040       }else if( p->eStage==RBU_STAGE_MOVE ){
4041         p->eStage = RBU_STAGE_CKPT;
4042         p->nStep = 0;
4043       }
4044     }
4045 
4046     if( p->rc==SQLITE_OK
4047      && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
4048      && pState->eStage!=0
4049     ){
4050       rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
4051       if( pFd->iCookie!=pState->iCookie ){
4052         /* At this point (pTargetFd->iCookie) contains the value of the
4053         ** change-counter cookie (the thing that gets incremented when a
4054         ** transaction is committed in rollback mode) currently stored on
4055         ** page 1 of the database file. */
4056         p->rc = SQLITE_BUSY;
4057         p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
4058             (rbuIsVacuum(p) ? "vacuum" : "update")
4059         );
4060       }
4061     }
4062 
4063     if( p->rc==SQLITE_OK ){
4064       if( p->eStage==RBU_STAGE_OAL ){
4065         sqlite3 *db = p->dbMain;
4066         p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
4067 
4068         /* Point the object iterator at the first object */
4069         if( p->rc==SQLITE_OK ){
4070           p->rc = rbuObjIterFirst(p, &p->objiter);
4071         }
4072 
4073         /* If the RBU database contains no data_xxx tables, declare the RBU
4074         ** update finished.  */
4075         if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
4076           p->rc = SQLITE_DONE;
4077           p->eStage = RBU_STAGE_DONE;
4078         }else{
4079           if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
4080             rbuCopyPragma(p, "page_size");
4081             rbuCopyPragma(p, "auto_vacuum");
4082           }
4083 
4084           /* Open transactions both databases. The *-oal file is opened or
4085           ** created at this point. */
4086           if( p->rc==SQLITE_OK ){
4087             p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
4088           }
4089 
4090           /* Check if the main database is a zipvfs db. If it is, set the upper
4091           ** level pager to use "journal_mode=off". This prevents it from
4092           ** generating a large journal using a temp file.  */
4093           if( p->rc==SQLITE_OK ){
4094             int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
4095             if( frc==SQLITE_OK ){
4096               p->rc = sqlite3_exec(
4097                 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
4098             }
4099           }
4100 
4101           if( p->rc==SQLITE_OK ){
4102             rbuSetupOal(p, pState);
4103           }
4104         }
4105       }else if( p->eStage==RBU_STAGE_MOVE ){
4106         /* no-op */
4107       }else if( p->eStage==RBU_STAGE_CKPT ){
4108         if( !rbuIsVacuum(p) && rbuExclusiveCheckpoint(p->dbMain) ){
4109           /* If the rbu_exclusive_checkpoint=1 URI parameter was specified
4110           ** and an incremental checkpoint is being resumed, attempt an
4111           ** exclusive lock on the db file. If this fails, so be it.  */
4112           p->eStage = RBU_STAGE_DONE;
4113           rbuLockDatabase(p->dbMain);
4114           p->eStage = RBU_STAGE_CKPT;
4115         }
4116         rbuSetupCheckpoint(p, pState);
4117       }else if( p->eStage==RBU_STAGE_DONE ){
4118         p->rc = SQLITE_DONE;
4119       }else{
4120         p->rc = SQLITE_CORRUPT;
4121       }
4122     }
4123 
4124     rbuFreeState(pState);
4125   }
4126 
4127   return p;
4128 }
4129 
4130 /*
4131 ** Allocate and return an RBU handle with all fields zeroed except for the
4132 ** error code, which is set to SQLITE_MISUSE.
4133 */
rbuMisuseError(void)4134 static sqlite3rbu *rbuMisuseError(void){
4135   sqlite3rbu *pRet;
4136   pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
4137   if( pRet ){
4138     memset(pRet, 0, sizeof(sqlite3rbu));
4139     pRet->rc = SQLITE_MISUSE;
4140   }
4141   return pRet;
4142 }
4143 
4144 /*
4145 ** Open and return a new RBU handle.
4146 */
sqlite3rbu_open(const char * zTarget,const char * zRbu,const char * zState)4147 sqlite3rbu *sqlite3rbu_open(
4148   const char *zTarget,
4149   const char *zRbu,
4150   const char *zState
4151 ){
4152   if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
4153   return openRbuHandle(zTarget, zRbu, zState);
4154 }
4155 
4156 /*
4157 ** Open a handle to begin or resume an RBU VACUUM operation.
4158 */
sqlite3rbu_vacuum(const char * zTarget,const char * zState)4159 sqlite3rbu *sqlite3rbu_vacuum(
4160   const char *zTarget,
4161   const char *zState
4162 ){
4163   if( zTarget==0 ){ return rbuMisuseError(); }
4164   if( zState ){
4165     int n = strlen(zState);
4166     if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
4167       return rbuMisuseError();
4168     }
4169   }
4170   /* TODO: Check that both arguments are non-NULL */
4171   return openRbuHandle(0, zTarget, zState);
4172 }
4173 
4174 /*
4175 ** Return the database handle used by pRbu.
4176 */
sqlite3rbu_db(sqlite3rbu * pRbu,int bRbu)4177 sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
4178   sqlite3 *db = 0;
4179   if( pRbu ){
4180     db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
4181   }
4182   return db;
4183 }
4184 
4185 
4186 /*
4187 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
4188 ** then edit any error message string so as to remove all occurrences of
4189 ** the pattern "rbu_imp_[0-9]*".
4190 */
rbuEditErrmsg(sqlite3rbu * p)4191 static void rbuEditErrmsg(sqlite3rbu *p){
4192   if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
4193     unsigned int i;
4194     size_t nErrmsg = strlen(p->zErrmsg);
4195     for(i=0; i<(nErrmsg-8); i++){
4196       if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
4197         int nDel = 8;
4198         while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
4199         memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
4200         nErrmsg -= nDel;
4201       }
4202     }
4203   }
4204 }
4205 
4206 /*
4207 ** Close the RBU handle.
4208 */
sqlite3rbu_close(sqlite3rbu * p,char ** pzErrmsg)4209 int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
4210   int rc;
4211   if( p ){
4212 
4213     /* Commit the transaction to the *-oal file. */
4214     if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
4215       p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
4216     }
4217 
4218     /* Sync the db file if currently doing an incremental checkpoint */
4219     if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
4220       sqlite3_file *pDb = p->pTargetFd->pReal;
4221       p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
4222     }
4223 
4224     rbuSaveState(p, p->eStage);
4225 
4226     if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
4227       p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
4228     }
4229 
4230     /* Close any open statement handles. */
4231     rbuObjIterFinalize(&p->objiter);
4232 
4233     /* If this is an RBU vacuum handle and the vacuum has either finished
4234     ** successfully or encountered an error, delete the contents of the
4235     ** state table. This causes the next call to sqlite3rbu_vacuum()
4236     ** specifying the current target and state databases to start a new
4237     ** vacuum from scratch.  */
4238     if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
4239       int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
4240       if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
4241     }
4242 
4243     /* Close the open database handle and VFS object. */
4244     sqlite3_close(p->dbRbu);
4245     sqlite3_close(p->dbMain);
4246     assert( p->szTemp==0 );
4247     rbuDeleteVfs(p);
4248     sqlite3_free(p->aBuf);
4249     sqlite3_free(p->aFrame);
4250 
4251     rbuEditErrmsg(p);
4252     rc = p->rc;
4253     if( pzErrmsg ){
4254       *pzErrmsg = p->zErrmsg;
4255     }else{
4256       sqlite3_free(p->zErrmsg);
4257     }
4258     sqlite3_free(p->zState);
4259     sqlite3_free(p);
4260   }else{
4261     rc = SQLITE_NOMEM;
4262     *pzErrmsg = 0;
4263   }
4264   return rc;
4265 }
4266 
4267 /*
4268 ** Return the total number of key-value operations (inserts, deletes or
4269 ** updates) that have been performed on the target database since the
4270 ** current RBU update was started.
4271 */
sqlite3rbu_progress(sqlite3rbu * pRbu)4272 sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
4273   return pRbu->nProgress;
4274 }
4275 
4276 /*
4277 ** Return permyriadage progress indications for the two main stages of
4278 ** an RBU update.
4279 */
sqlite3rbu_bp_progress(sqlite3rbu * p,int * pnOne,int * pnTwo)4280 void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
4281   const int MAX_PROGRESS = 10000;
4282   switch( p->eStage ){
4283     case RBU_STAGE_OAL:
4284       if( p->nPhaseOneStep>0 ){
4285         *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
4286       }else{
4287         *pnOne = -1;
4288       }
4289       *pnTwo = 0;
4290       break;
4291 
4292     case RBU_STAGE_MOVE:
4293       *pnOne = MAX_PROGRESS;
4294       *pnTwo = 0;
4295       break;
4296 
4297     case RBU_STAGE_CKPT:
4298       *pnOne = MAX_PROGRESS;
4299       *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
4300       break;
4301 
4302     case RBU_STAGE_DONE:
4303       *pnOne = MAX_PROGRESS;
4304       *pnTwo = MAX_PROGRESS;
4305       break;
4306 
4307     default:
4308       assert( 0 );
4309   }
4310 }
4311 
4312 /*
4313 ** Return the current state of the RBU vacuum or update operation.
4314 */
sqlite3rbu_state(sqlite3rbu * p)4315 int sqlite3rbu_state(sqlite3rbu *p){
4316   int aRes[] = {
4317     0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
4318     0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
4319   };
4320 
4321   assert( RBU_STAGE_OAL==1 );
4322   assert( RBU_STAGE_MOVE==2 );
4323   assert( RBU_STAGE_CKPT==4 );
4324   assert( RBU_STAGE_DONE==5 );
4325   assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
4326   assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
4327   assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
4328   assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
4329 
4330   if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
4331     return SQLITE_RBU_STATE_ERROR;
4332   }else{
4333     assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
4334     assert( p->eStage==RBU_STAGE_OAL
4335          || p->eStage==RBU_STAGE_MOVE
4336          || p->eStage==RBU_STAGE_CKPT
4337          || p->eStage==RBU_STAGE_DONE
4338     );
4339     return aRes[p->eStage];
4340   }
4341 }
4342 
sqlite3rbu_savestate(sqlite3rbu * p)4343 int sqlite3rbu_savestate(sqlite3rbu *p){
4344   int rc = p->rc;
4345   if( rc==SQLITE_DONE ) return SQLITE_OK;
4346 
4347   assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
4348   if( p->eStage==RBU_STAGE_OAL ){
4349     assert( rc!=SQLITE_DONE );
4350     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
4351   }
4352 
4353   /* Sync the db file */
4354   if( rc==SQLITE_OK && p->eStage==RBU_STAGE_CKPT ){
4355     sqlite3_file *pDb = p->pTargetFd->pReal;
4356     rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
4357   }
4358 
4359   p->rc = rc;
4360   rbuSaveState(p, p->eStage);
4361   rc = p->rc;
4362 
4363   if( p->eStage==RBU_STAGE_OAL ){
4364     assert( rc!=SQLITE_DONE );
4365     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
4366     if( rc==SQLITE_OK ){
4367       const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
4368       rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
4369     }
4370     if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
4371   }
4372 
4373   p->rc = rc;
4374   return rc;
4375 }
4376 
4377 /*
4378 ** Default xRename callback for RBU.
4379 */
xDefaultRename(void * pArg,const char * zOld,const char * zNew)4380 static int xDefaultRename(void *pArg, const char *zOld, const char *zNew){
4381   int rc = SQLITE_OK;
4382 #if defined(_WIN32_WCE)
4383   {
4384     LPWSTR zWideOld;
4385     LPWSTR zWideNew;
4386 
4387     zWideOld = rbuWinUtf8ToUnicode(zOld);
4388     if( zWideOld ){
4389       zWideNew = rbuWinUtf8ToUnicode(zNew);
4390       if( zWideNew ){
4391         if( MoveFileW(zWideOld, zWideNew) ){
4392           rc = SQLITE_OK;
4393         }else{
4394           rc = SQLITE_IOERR;
4395         }
4396         sqlite3_free(zWideNew);
4397       }else{
4398         rc = SQLITE_IOERR_NOMEM;
4399       }
4400       sqlite3_free(zWideOld);
4401     }else{
4402       rc = SQLITE_IOERR_NOMEM;
4403     }
4404   }
4405 #else
4406   rc = rename(zOld, zNew) ? SQLITE_IOERR : SQLITE_OK;
4407 #endif
4408   return rc;
4409 }
4410 
sqlite3rbu_rename_handler(sqlite3rbu * pRbu,void * pArg,int (* xRename)(void * pArg,const char * zOld,const char * zNew))4411 void sqlite3rbu_rename_handler(
4412   sqlite3rbu *pRbu,
4413   void *pArg,
4414   int (*xRename)(void *pArg, const char *zOld, const char *zNew)
4415 ){
4416   if( xRename ){
4417     pRbu->xRename = xRename;
4418     pRbu->pRenameArg = pArg;
4419   }else{
4420     pRbu->xRename = xDefaultRename;
4421     pRbu->pRenameArg = 0;
4422   }
4423 }
4424 
4425 /**************************************************************************
4426 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
4427 ** of a standard VFS in the following ways:
4428 **
4429 ** 1. Whenever the first page of a main database file is read or
4430 **    written, the value of the change-counter cookie is stored in
4431 **    rbu_file.iCookie. Similarly, the value of the "write-version"
4432 **    database header field is stored in rbu_file.iWriteVer. This ensures
4433 **    that the values are always trustworthy within an open transaction.
4434 **
4435 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
4436 **    member variable of the associated database file descriptor is set
4437 **    to point to the new file. A mutex protected linked list of all main
4438 **    db fds opened using a particular RBU VFS is maintained at
4439 **    rbu_vfs.pMain to facilitate this.
4440 **
4441 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
4442 **    object can be marked as the target database of an RBU update. This
4443 **    turns on the following extra special behaviour:
4444 **
4445 ** 3a. If xAccess() is called to check if there exists a *-wal file
4446 **     associated with an RBU target database currently in RBU_STAGE_OAL
4447 **     stage (preparing the *-oal file), the following special handling
4448 **     applies:
4449 **
4450 **      * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
4451 **        target database may not be in wal mode already.
4452 **
4453 **      * if the *-wal file does not exist, set the output parameter to
4454 **        non-zero (to tell SQLite that it does exist) anyway.
4455 **
4456 **     Then, when xOpen() is called to open the *-wal file associated with
4457 **     the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
4458 **     file, the rbu vfs opens the corresponding *-oal file instead.
4459 **
4460 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
4461 **     RBU_STAGE_OAL mode are actually stored in heap memory. This is to
4462 **     avoid creating a *-shm file on disk. Additionally, xShmLock() calls
4463 **     are no-ops on target database files in RBU_STAGE_OAL mode. This is
4464 **     because assert() statements in some VFS implementations fail if
4465 **     xShmLock() is called before xShmMap().
4466 **
4467 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
4468 **     mode except RBU_STAGE_DONE (all work completed and checkpointed), it
4469 **     fails with an SQLITE_BUSY error. This is to stop RBU connections
4470 **     from automatically checkpointing a *-wal (or *-oal) file from within
4471 **     sqlite3_close().
4472 **
4473 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
4474 **     all xWrite() calls on the target database file perform no IO.
4475 **     Instead the frame and page numbers that would be read and written
4476 **     are recorded. Additionally, successful attempts to obtain exclusive
4477 **     xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
4478 **     database file are recorded. xShmLock() calls to unlock the same
4479 **     locks are no-ops (so that once obtained, these locks are never
4480 **     relinquished). Finally, calls to xSync() on the target database
4481 **     file fail with SQLITE_INTERNAL errors.
4482 */
4483 
rbuUnlockShm(rbu_file * p)4484 static void rbuUnlockShm(rbu_file *p){
4485   assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
4486   if( p->pRbu ){
4487     int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
4488     int i;
4489     for(i=0; i<SQLITE_SHM_NLOCK;i++){
4490       if( (1<<i) & p->pRbu->mLock ){
4491         xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
4492       }
4493     }
4494     p->pRbu->mLock = 0;
4495   }
4496 }
4497 
4498 /*
4499 */
rbuUpdateTempSize(rbu_file * pFd,sqlite3_int64 nNew)4500 static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
4501   sqlite3rbu *pRbu = pFd->pRbu;
4502   i64 nDiff = nNew - pFd->sz;
4503   pRbu->szTemp += nDiff;
4504   pFd->sz = nNew;
4505   assert( pRbu->szTemp>=0 );
4506   if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL;
4507   return SQLITE_OK;
4508 }
4509 
4510 /*
4511 ** Add an item to the main-db lists, if it is not already present.
4512 **
4513 ** There are two main-db lists. One for all file descriptors, and one
4514 ** for all file descriptors with rbu_file.pDb!=0. If the argument has
4515 ** rbu_file.pDb!=0, then it is assumed to already be present on the
4516 ** main list and is only added to the pDb!=0 list.
4517 */
rbuMainlistAdd(rbu_file * p)4518 static void rbuMainlistAdd(rbu_file *p){
4519   rbu_vfs *pRbuVfs = p->pRbuVfs;
4520   rbu_file *pIter;
4521   assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) );
4522   sqlite3_mutex_enter(pRbuVfs->mutex);
4523   if( p->pRbu==0 ){
4524     for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
4525     p->pMainNext = pRbuVfs->pMain;
4526     pRbuVfs->pMain = p;
4527   }else{
4528     for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
4529     if( pIter==0 ){
4530       p->pMainRbuNext = pRbuVfs->pMainRbu;
4531       pRbuVfs->pMainRbu = p;
4532     }
4533   }
4534   sqlite3_mutex_leave(pRbuVfs->mutex);
4535 }
4536 
4537 /*
4538 ** Remove an item from the main-db lists.
4539 */
rbuMainlistRemove(rbu_file * p)4540 static void rbuMainlistRemove(rbu_file *p){
4541   rbu_file **pp;
4542   sqlite3_mutex_enter(p->pRbuVfs->mutex);
4543   for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
4544   if( *pp ) *pp = p->pMainNext;
4545   p->pMainNext = 0;
4546   for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
4547   if( *pp ) *pp = p->pMainRbuNext;
4548   p->pMainRbuNext = 0;
4549   sqlite3_mutex_leave(p->pRbuVfs->mutex);
4550 }
4551 
4552 /*
4553 ** Given that zWal points to a buffer containing a wal file name passed to
4554 ** either the xOpen() or xAccess() VFS method, search the main-db list for
4555 ** a file-handle opened by the same database connection on the corresponding
4556 ** database file.
4557 **
4558 ** If parameter bRbu is true, only search for file-descriptors with
4559 ** rbu_file.pDb!=0.
4560 */
rbuFindMaindb(rbu_vfs * pRbuVfs,const char * zWal,int bRbu)4561 static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
4562   rbu_file *pDb;
4563   sqlite3_mutex_enter(pRbuVfs->mutex);
4564   if( bRbu ){
4565     for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
4566   }else{
4567     for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
4568   }
4569   sqlite3_mutex_leave(pRbuVfs->mutex);
4570   return pDb;
4571 }
4572 
4573 /*
4574 ** Close an rbu file.
4575 */
rbuVfsClose(sqlite3_file * pFile)4576 static int rbuVfsClose(sqlite3_file *pFile){
4577   rbu_file *p = (rbu_file*)pFile;
4578   int rc;
4579   int i;
4580 
4581   /* Free the contents of the apShm[] array. And the array itself. */
4582   for(i=0; i<p->nShm; i++){
4583     sqlite3_free(p->apShm[i]);
4584   }
4585   sqlite3_free(p->apShm);
4586   p->apShm = 0;
4587   sqlite3_free(p->zDel);
4588 
4589   if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
4590     rbuMainlistRemove(p);
4591     rbuUnlockShm(p);
4592     p->pReal->pMethods->xShmUnmap(p->pReal, 0);
4593   }
4594   else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
4595     rbuUpdateTempSize(p, 0);
4596   }
4597   assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p );
4598 
4599   /* Close the underlying file handle */
4600   rc = p->pReal->pMethods->xClose(p->pReal);
4601   return rc;
4602 }
4603 
4604 
4605 /*
4606 ** Read and return an unsigned 32-bit big-endian integer from the buffer
4607 ** passed as the only argument.
4608 */
rbuGetU32(u8 * aBuf)4609 static u32 rbuGetU32(u8 *aBuf){
4610   return ((u32)aBuf[0] << 24)
4611        + ((u32)aBuf[1] << 16)
4612        + ((u32)aBuf[2] <<  8)
4613        + ((u32)aBuf[3]);
4614 }
4615 
4616 /*
4617 ** Write an unsigned 32-bit value in big-endian format to the supplied
4618 ** buffer.
4619 */
rbuPutU32(u8 * aBuf,u32 iVal)4620 static void rbuPutU32(u8 *aBuf, u32 iVal){
4621   aBuf[0] = (iVal >> 24) & 0xFF;
4622   aBuf[1] = (iVal >> 16) & 0xFF;
4623   aBuf[2] = (iVal >>  8) & 0xFF;
4624   aBuf[3] = (iVal >>  0) & 0xFF;
4625 }
4626 
rbuPutU16(u8 * aBuf,u16 iVal)4627 static void rbuPutU16(u8 *aBuf, u16 iVal){
4628   aBuf[0] = (iVal >>  8) & 0xFF;
4629   aBuf[1] = (iVal >>  0) & 0xFF;
4630 }
4631 
4632 /*
4633 ** Read data from an rbuVfs-file.
4634 */
rbuVfsRead(sqlite3_file * pFile,void * zBuf,int iAmt,sqlite_int64 iOfst)4635 static int rbuVfsRead(
4636   sqlite3_file *pFile,
4637   void *zBuf,
4638   int iAmt,
4639   sqlite_int64 iOfst
4640 ){
4641   rbu_file *p = (rbu_file*)pFile;
4642   sqlite3rbu *pRbu = p->pRbu;
4643   int rc;
4644 
4645   if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
4646     assert( p->openFlags & SQLITE_OPEN_WAL );
4647     rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
4648   }else{
4649     if( pRbu && pRbu->eStage==RBU_STAGE_OAL
4650      && (p->openFlags & SQLITE_OPEN_WAL)
4651      && iOfst>=pRbu->iOalSz
4652     ){
4653       rc = SQLITE_OK;
4654       memset(zBuf, 0, iAmt);
4655     }else{
4656       rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
4657 #if 1
4658       /* If this is being called to read the first page of the target
4659       ** database as part of an rbu vacuum operation, synthesize the
4660       ** contents of the first page if it does not yet exist. Otherwise,
4661       ** SQLite will not check for a *-wal file.  */
4662       if( pRbu && rbuIsVacuum(pRbu)
4663           && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
4664           && (p->openFlags & SQLITE_OPEN_MAIN_DB)
4665           && pRbu->rc==SQLITE_OK
4666       ){
4667         sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
4668         rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
4669         if( rc==SQLITE_OK ){
4670           u8 *aBuf = (u8*)zBuf;
4671           u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
4672           rbuPutU32(&aBuf[52], iRoot);      /* largest root page number */
4673           rbuPutU32(&aBuf[36], 0);          /* number of free pages */
4674           rbuPutU32(&aBuf[32], 0);          /* first page on free list trunk */
4675           rbuPutU32(&aBuf[28], 1);          /* size of db file in pages */
4676           rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1);  /* Change counter */
4677 
4678           if( iAmt>100 ){
4679             memset(&aBuf[100], 0, iAmt-100);
4680             rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
4681             aBuf[100] = 0x0D;
4682           }
4683         }
4684       }
4685 #endif
4686     }
4687     if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
4688       /* These look like magic numbers. But they are stable, as they are part
4689        ** of the definition of the SQLite file format, which may not change. */
4690       u8 *pBuf = (u8*)zBuf;
4691       p->iCookie = rbuGetU32(&pBuf[24]);
4692       p->iWriteVer = pBuf[19];
4693     }
4694   }
4695   return rc;
4696 }
4697 
4698 /*
4699 ** Write data to an rbuVfs-file.
4700 */
rbuVfsWrite(sqlite3_file * pFile,const void * zBuf,int iAmt,sqlite_int64 iOfst)4701 static int rbuVfsWrite(
4702   sqlite3_file *pFile,
4703   const void *zBuf,
4704   int iAmt,
4705   sqlite_int64 iOfst
4706 ){
4707   rbu_file *p = (rbu_file*)pFile;
4708   sqlite3rbu *pRbu = p->pRbu;
4709   int rc;
4710 
4711   if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
4712     assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
4713     rc = rbuCaptureDbWrite(p->pRbu, iOfst);
4714   }else{
4715     if( pRbu ){
4716       if( pRbu->eStage==RBU_STAGE_OAL
4717        && (p->openFlags & SQLITE_OPEN_WAL)
4718        && iOfst>=pRbu->iOalSz
4719       ){
4720         pRbu->iOalSz = iAmt + iOfst;
4721       }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE ){
4722         i64 szNew = iAmt+iOfst;
4723         if( szNew>p->sz ){
4724           rc = rbuUpdateTempSize(p, szNew);
4725           if( rc!=SQLITE_OK ) return rc;
4726         }
4727       }
4728     }
4729     rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
4730     if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
4731       /* These look like magic numbers. But they are stable, as they are part
4732       ** of the definition of the SQLite file format, which may not change. */
4733       u8 *pBuf = (u8*)zBuf;
4734       p->iCookie = rbuGetU32(&pBuf[24]);
4735       p->iWriteVer = pBuf[19];
4736     }
4737   }
4738   return rc;
4739 }
4740 
4741 /*
4742 ** Truncate an rbuVfs-file.
4743 */
rbuVfsTruncate(sqlite3_file * pFile,sqlite_int64 size)4744 static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
4745   rbu_file *p = (rbu_file*)pFile;
4746   if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE) && p->pRbu ){
4747     int rc = rbuUpdateTempSize(p, size);
4748     if( rc!=SQLITE_OK ) return rc;
4749   }
4750   return p->pReal->pMethods->xTruncate(p->pReal, size);
4751 }
4752 
4753 /*
4754 ** Sync an rbuVfs-file.
4755 */
rbuVfsSync(sqlite3_file * pFile,int flags)4756 static int rbuVfsSync(sqlite3_file *pFile, int flags){
4757   rbu_file *p = (rbu_file *)pFile;
4758   if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
4759     if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
4760       return SQLITE_INTERNAL;
4761     }
4762     return SQLITE_OK;
4763   }
4764   return p->pReal->pMethods->xSync(p->pReal, flags);
4765 }
4766 
4767 /*
4768 ** Return the current file-size of an rbuVfs-file.
4769 */
rbuVfsFileSize(sqlite3_file * pFile,sqlite_int64 * pSize)4770 static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
4771   rbu_file *p = (rbu_file *)pFile;
4772   int rc;
4773   rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
4774 
4775   /* If this is an RBU vacuum operation and this is the target database,
4776   ** pretend that it has at least one page. Otherwise, SQLite will not
4777   ** check for the existance of a *-wal file. rbuVfsRead() contains
4778   ** similar logic.  */
4779   if( rc==SQLITE_OK && *pSize==0
4780    && p->pRbu && rbuIsVacuum(p->pRbu)
4781    && (p->openFlags & SQLITE_OPEN_MAIN_DB)
4782   ){
4783     *pSize = 1024;
4784   }
4785   return rc;
4786 }
4787 
4788 /*
4789 ** Lock an rbuVfs-file.
4790 */
rbuVfsLock(sqlite3_file * pFile,int eLock)4791 static int rbuVfsLock(sqlite3_file *pFile, int eLock){
4792   rbu_file *p = (rbu_file*)pFile;
4793   sqlite3rbu *pRbu = p->pRbu;
4794   int rc = SQLITE_OK;
4795 
4796   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4797   if( eLock==SQLITE_LOCK_EXCLUSIVE
4798    && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
4799   ){
4800     /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
4801     ** prevents it from checkpointing the database from sqlite3_close(). */
4802     rc = SQLITE_BUSY;
4803   }else{
4804     rc = p->pReal->pMethods->xLock(p->pReal, eLock);
4805   }
4806 
4807   return rc;
4808 }
4809 
4810 /*
4811 ** Unlock an rbuVfs-file.
4812 */
rbuVfsUnlock(sqlite3_file * pFile,int eLock)4813 static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
4814   rbu_file *p = (rbu_file *)pFile;
4815   return p->pReal->pMethods->xUnlock(p->pReal, eLock);
4816 }
4817 
4818 /*
4819 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
4820 */
rbuVfsCheckReservedLock(sqlite3_file * pFile,int * pResOut)4821 static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
4822   rbu_file *p = (rbu_file *)pFile;
4823   return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
4824 }
4825 
4826 /*
4827 ** File control method. For custom operations on an rbuVfs-file.
4828 */
rbuVfsFileControl(sqlite3_file * pFile,int op,void * pArg)4829 static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
4830   rbu_file *p = (rbu_file *)pFile;
4831   int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
4832   int rc;
4833 
4834   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
4835        || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
4836   );
4837   if( op==SQLITE_FCNTL_RBU ){
4838     sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
4839 
4840     /* First try to find another RBU vfs lower down in the vfs stack. If
4841     ** one is found, this vfs will operate in pass-through mode. The lower
4842     ** level vfs will do the special RBU handling.  */
4843     rc = xControl(p->pReal, op, pArg);
4844 
4845     if( rc==SQLITE_NOTFOUND ){
4846       /* Now search for a zipvfs instance lower down in the VFS stack. If
4847       ** one is found, this is an error.  */
4848       void *dummy = 0;
4849       rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
4850       if( rc==SQLITE_OK ){
4851         rc = SQLITE_ERROR;
4852         pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
4853       }else if( rc==SQLITE_NOTFOUND ){
4854         pRbu->pTargetFd = p;
4855         p->pRbu = pRbu;
4856         rbuMainlistAdd(p);
4857         if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
4858         rc = SQLITE_OK;
4859       }
4860     }
4861     return rc;
4862   }
4863   else if( op==SQLITE_FCNTL_RBUCNT ){
4864     sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
4865     pRbu->nRbu++;
4866     pRbu->pRbuFd = p;
4867     p->bNolock = 1;
4868   }
4869 
4870   rc = xControl(p->pReal, op, pArg);
4871   if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4872     rbu_vfs *pRbuVfs = p->pRbuVfs;
4873     char *zIn = *(char**)pArg;
4874     char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
4875     *(char**)pArg = zOut;
4876     if( zOut==0 ) rc = SQLITE_NOMEM;
4877   }
4878 
4879   return rc;
4880 }
4881 
4882 /*
4883 ** Return the sector-size in bytes for an rbuVfs-file.
4884 */
rbuVfsSectorSize(sqlite3_file * pFile)4885 static int rbuVfsSectorSize(sqlite3_file *pFile){
4886   rbu_file *p = (rbu_file *)pFile;
4887   return p->pReal->pMethods->xSectorSize(p->pReal);
4888 }
4889 
4890 /*
4891 ** Return the device characteristic flags supported by an rbuVfs-file.
4892 */
rbuVfsDeviceCharacteristics(sqlite3_file * pFile)4893 static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
4894   rbu_file *p = (rbu_file *)pFile;
4895   return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
4896 }
4897 
4898 /*
4899 ** Take or release a shared-memory lock.
4900 */
rbuVfsShmLock(sqlite3_file * pFile,int ofst,int n,int flags)4901 static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
4902   rbu_file *p = (rbu_file*)pFile;
4903   sqlite3rbu *pRbu = p->pRbu;
4904   int rc = SQLITE_OK;
4905 
4906 #ifdef SQLITE_AMALGAMATION
4907     assert( WAL_CKPT_LOCK==1 );
4908 #endif
4909 
4910   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4911   if( pRbu && (
4912        pRbu->eStage==RBU_STAGE_OAL
4913     || pRbu->eStage==RBU_STAGE_MOVE
4914     || pRbu->eStage==RBU_STAGE_DONE
4915   )){
4916     /* Prevent SQLite from taking a shm-lock on the target file when it
4917     ** is supplying heap memory to the upper layer in place of *-shm
4918     ** segments. */
4919     if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
4920   }else{
4921     int bCapture = 0;
4922     if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
4923       bCapture = 1;
4924     }
4925     if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
4926       rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
4927       if( bCapture && rc==SQLITE_OK ){
4928         pRbu->mLock |= ((1<<n) - 1) << ofst;
4929       }
4930     }
4931   }
4932 
4933   return rc;
4934 }
4935 
4936 /*
4937 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
4938 */
rbuVfsShmMap(sqlite3_file * pFile,int iRegion,int szRegion,int isWrite,void volatile ** pp)4939 static int rbuVfsShmMap(
4940   sqlite3_file *pFile,
4941   int iRegion,
4942   int szRegion,
4943   int isWrite,
4944   void volatile **pp
4945 ){
4946   rbu_file *p = (rbu_file*)pFile;
4947   int rc = SQLITE_OK;
4948   int eStage = (p->pRbu ? p->pRbu->eStage : 0);
4949 
4950   /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
4951   ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
4952   ** instead of a file on disk.  */
4953   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4954   if( eStage==RBU_STAGE_OAL ){
4955     sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
4956     char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
4957 
4958     /* This is an RBU connection that uses its own heap memory for the
4959     ** pages of the *-shm file. Since no other process can have run
4960     ** recovery, the connection must request *-shm pages in order
4961     ** from start to finish.  */
4962     assert( iRegion==p->nShm );
4963     if( apNew==0 ){
4964       rc = SQLITE_NOMEM;
4965     }else{
4966       memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
4967       p->apShm = apNew;
4968       p->nShm = iRegion+1;
4969     }
4970 
4971     if( rc==SQLITE_OK ){
4972       char *pNew = (char*)sqlite3_malloc64(szRegion);
4973       if( pNew==0 ){
4974         rc = SQLITE_NOMEM;
4975       }else{
4976         memset(pNew, 0, szRegion);
4977         p->apShm[iRegion] = pNew;
4978       }
4979     }
4980 
4981     if( rc==SQLITE_OK ){
4982       *pp = p->apShm[iRegion];
4983     }else{
4984       *pp = 0;
4985     }
4986   }else{
4987     assert( p->apShm==0 );
4988     rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
4989   }
4990 
4991   return rc;
4992 }
4993 
4994 /*
4995 ** Memory barrier.
4996 */
rbuVfsShmBarrier(sqlite3_file * pFile)4997 static void rbuVfsShmBarrier(sqlite3_file *pFile){
4998   rbu_file *p = (rbu_file *)pFile;
4999   p->pReal->pMethods->xShmBarrier(p->pReal);
5000 }
5001 
5002 /*
5003 ** The xShmUnmap method.
5004 */
rbuVfsShmUnmap(sqlite3_file * pFile,int delFlag)5005 static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
5006   rbu_file *p = (rbu_file*)pFile;
5007   int rc = SQLITE_OK;
5008   int eStage = (p->pRbu ? p->pRbu->eStage : 0);
5009 
5010   assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
5011   if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
5012     /* no-op */
5013   }else{
5014     /* Release the checkpointer and writer locks */
5015     rbuUnlockShm(p);
5016     rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
5017   }
5018   return rc;
5019 }
5020 
5021 /*
5022 ** Open an rbu file handle.
5023 */
rbuVfsOpen(sqlite3_vfs * pVfs,const char * zName,sqlite3_file * pFile,int flags,int * pOutFlags)5024 static int rbuVfsOpen(
5025   sqlite3_vfs *pVfs,
5026   const char *zName,
5027   sqlite3_file *pFile,
5028   int flags,
5029   int *pOutFlags
5030 ){
5031   static sqlite3_io_methods rbuvfs_io_methods = {
5032     2,                            /* iVersion */
5033     rbuVfsClose,                  /* xClose */
5034     rbuVfsRead,                   /* xRead */
5035     rbuVfsWrite,                  /* xWrite */
5036     rbuVfsTruncate,               /* xTruncate */
5037     rbuVfsSync,                   /* xSync */
5038     rbuVfsFileSize,               /* xFileSize */
5039     rbuVfsLock,                   /* xLock */
5040     rbuVfsUnlock,                 /* xUnlock */
5041     rbuVfsCheckReservedLock,      /* xCheckReservedLock */
5042     rbuVfsFileControl,            /* xFileControl */
5043     rbuVfsSectorSize,             /* xSectorSize */
5044     rbuVfsDeviceCharacteristics,  /* xDeviceCharacteristics */
5045     rbuVfsShmMap,                 /* xShmMap */
5046     rbuVfsShmLock,                /* xShmLock */
5047     rbuVfsShmBarrier,             /* xShmBarrier */
5048     rbuVfsShmUnmap,               /* xShmUnmap */
5049     0, 0                          /* xFetch, xUnfetch */
5050   };
5051   rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
5052   sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
5053   rbu_file *pFd = (rbu_file *)pFile;
5054   int rc = SQLITE_OK;
5055   const char *zOpen = zName;
5056   int oflags = flags;
5057 
5058   memset(pFd, 0, sizeof(rbu_file));
5059   pFd->pReal = (sqlite3_file*)&pFd[1];
5060   pFd->pRbuVfs = pRbuVfs;
5061   pFd->openFlags = flags;
5062   if( zName ){
5063     if( flags & SQLITE_OPEN_MAIN_DB ){
5064       /* A main database has just been opened. The following block sets
5065       ** (pFd->zWal) to point to a buffer owned by SQLite that contains
5066       ** the name of the *-wal file this db connection will use. SQLite
5067       ** happens to pass a pointer to this buffer when using xAccess()
5068       ** or xOpen() to operate on the *-wal file.  */
5069       pFd->zWal = sqlite3_filename_wal(zName);
5070     }
5071     else if( flags & SQLITE_OPEN_WAL ){
5072       rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
5073       if( pDb ){
5074         if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
5075           /* This call is to open a *-wal file. Intead, open the *-oal. */
5076           size_t nOpen;
5077           if( rbuIsVacuum(pDb->pRbu) ){
5078             zOpen = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
5079             zOpen = sqlite3_filename_wal(zOpen);
5080           }
5081           nOpen = strlen(zOpen);
5082           ((char*)zOpen)[nOpen-3] = 'o';
5083           pFd->pRbu = pDb->pRbu;
5084         }
5085         pDb->pWalFd = pFd;
5086       }
5087     }
5088   }else{
5089     pFd->pRbu = pRbuVfs->pRbu;
5090   }
5091 
5092   if( oflags & SQLITE_OPEN_MAIN_DB
5093    && sqlite3_uri_boolean(zName, "rbu_memory", 0)
5094   ){
5095     assert( oflags & SQLITE_OPEN_MAIN_DB );
5096     oflags =  SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
5097               SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
5098     zOpen = 0;
5099   }
5100 
5101   if( rc==SQLITE_OK ){
5102     rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
5103   }
5104   if( pFd->pReal->pMethods ){
5105     /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
5106     ** pointer and, if the file is a main database file, link it into the
5107     ** mutex protected linked list of all such files.  */
5108     pFile->pMethods = &rbuvfs_io_methods;
5109     if( flags & SQLITE_OPEN_MAIN_DB ){
5110       rbuMainlistAdd(pFd);
5111     }
5112   }else{
5113     sqlite3_free(pFd->zDel);
5114   }
5115 
5116   return rc;
5117 }
5118 
5119 /*
5120 ** Delete the file located at zPath.
5121 */
rbuVfsDelete(sqlite3_vfs * pVfs,const char * zPath,int dirSync)5122 static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
5123   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5124   return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
5125 }
5126 
5127 /*
5128 ** Test for access permissions. Return true if the requested permission
5129 ** is available, or false otherwise.
5130 */
rbuVfsAccess(sqlite3_vfs * pVfs,const char * zPath,int flags,int * pResOut)5131 static int rbuVfsAccess(
5132   sqlite3_vfs *pVfs,
5133   const char *zPath,
5134   int flags,
5135   int *pResOut
5136 ){
5137   rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
5138   sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
5139   int rc;
5140 
5141   rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
5142 
5143   /* If this call is to check if a *-wal file associated with an RBU target
5144   ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
5145   ** the following special handling is activated:
5146   **
5147   **   a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
5148   **      ensures that the RBU extension never tries to update a database
5149   **      in wal mode, even if the first page of the database file has
5150   **      been damaged.
5151   **
5152   **   b) if the *-wal file does not exist, claim that it does anyway,
5153   **      causing SQLite to call xOpen() to open it. This call will also
5154   **      be intercepted (see the rbuVfsOpen() function) and the *-oal
5155   **      file opened instead.
5156   */
5157   if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
5158     rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
5159     if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
5160       assert( pDb->pRbu );
5161       if( *pResOut ){
5162         rc = SQLITE_CANTOPEN;
5163       }else{
5164         sqlite3_int64 sz = 0;
5165         rc = rbuVfsFileSize(&pDb->base, &sz);
5166         *pResOut = (sz>0);
5167       }
5168     }
5169   }
5170 
5171   return rc;
5172 }
5173 
5174 /*
5175 ** Populate buffer zOut with the full canonical pathname corresponding
5176 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
5177 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
5178 */
rbuVfsFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nOut,char * zOut)5179 static int rbuVfsFullPathname(
5180   sqlite3_vfs *pVfs,
5181   const char *zPath,
5182   int nOut,
5183   char *zOut
5184 ){
5185   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5186   return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
5187 }
5188 
5189 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5190 /*
5191 ** Open the dynamic library located at zPath and return a handle.
5192 */
rbuVfsDlOpen(sqlite3_vfs * pVfs,const char * zPath)5193 static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
5194   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5195   return pRealVfs->xDlOpen(pRealVfs, zPath);
5196 }
5197 
5198 /*
5199 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
5200 ** utf-8 string describing the most recent error encountered associated
5201 ** with dynamic libraries.
5202 */
rbuVfsDlError(sqlite3_vfs * pVfs,int nByte,char * zErrMsg)5203 static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
5204   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5205   pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
5206 }
5207 
5208 /*
5209 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
5210 */
rbuVfsDlSym(sqlite3_vfs * pVfs,void * pArg,const char * zSym)5211 static void (*rbuVfsDlSym(
5212   sqlite3_vfs *pVfs,
5213   void *pArg,
5214   const char *zSym
5215 ))(void){
5216   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5217   return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
5218 }
5219 
5220 /*
5221 ** Close the dynamic library handle pHandle.
5222 */
rbuVfsDlClose(sqlite3_vfs * pVfs,void * pHandle)5223 static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
5224   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5225   pRealVfs->xDlClose(pRealVfs, pHandle);
5226 }
5227 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
5228 
5229 /*
5230 ** Populate the buffer pointed to by zBufOut with nByte bytes of
5231 ** random data.
5232 */
rbuVfsRandomness(sqlite3_vfs * pVfs,int nByte,char * zBufOut)5233 static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
5234   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5235   return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
5236 }
5237 
5238 /*
5239 ** Sleep for nMicro microseconds. Return the number of microseconds
5240 ** actually slept.
5241 */
rbuVfsSleep(sqlite3_vfs * pVfs,int nMicro)5242 static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
5243   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5244   return pRealVfs->xSleep(pRealVfs, nMicro);
5245 }
5246 
5247 /*
5248 ** Return the current time as a Julian Day number in *pTimeOut.
5249 */
rbuVfsCurrentTime(sqlite3_vfs * pVfs,double * pTimeOut)5250 static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
5251   sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5252   return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
5253 }
5254 
5255 /*
5256 ** No-op.
5257 */
rbuVfsGetLastError(sqlite3_vfs * pVfs,int a,char * b)5258 static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
5259   return 0;
5260 }
5261 
5262 /*
5263 ** Deregister and destroy an RBU vfs created by an earlier call to
5264 ** sqlite3rbu_create_vfs().
5265 */
sqlite3rbu_destroy_vfs(const char * zName)5266 void sqlite3rbu_destroy_vfs(const char *zName){
5267   sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
5268   if( pVfs && pVfs->xOpen==rbuVfsOpen ){
5269     sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
5270     sqlite3_vfs_unregister(pVfs);
5271     sqlite3_free(pVfs);
5272   }
5273 }
5274 
5275 /*
5276 ** Create an RBU VFS named zName that accesses the underlying file-system
5277 ** via existing VFS zParent. The new object is registered as a non-default
5278 ** VFS with SQLite before returning.
5279 */
sqlite3rbu_create_vfs(const char * zName,const char * zParent)5280 int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
5281 
5282   /* Template for VFS */
5283   static sqlite3_vfs vfs_template = {
5284     1,                            /* iVersion */
5285     0,                            /* szOsFile */
5286     0,                            /* mxPathname */
5287     0,                            /* pNext */
5288     0,                            /* zName */
5289     0,                            /* pAppData */
5290     rbuVfsOpen,                   /* xOpen */
5291     rbuVfsDelete,                 /* xDelete */
5292     rbuVfsAccess,                 /* xAccess */
5293     rbuVfsFullPathname,           /* xFullPathname */
5294 
5295 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5296     rbuVfsDlOpen,                 /* xDlOpen */
5297     rbuVfsDlError,                /* xDlError */
5298     rbuVfsDlSym,                  /* xDlSym */
5299     rbuVfsDlClose,                /* xDlClose */
5300 #else
5301     0, 0, 0, 0,
5302 #endif
5303 
5304     rbuVfsRandomness,             /* xRandomness */
5305     rbuVfsSleep,                  /* xSleep */
5306     rbuVfsCurrentTime,            /* xCurrentTime */
5307     rbuVfsGetLastError,           /* xGetLastError */
5308     0,                            /* xCurrentTimeInt64 (version 2) */
5309     0, 0, 0                       /* Unimplemented version 3 methods */
5310   };
5311 
5312   rbu_vfs *pNew = 0;              /* Newly allocated VFS */
5313   int rc = SQLITE_OK;
5314   size_t nName;
5315   size_t nByte;
5316 
5317   nName = strlen(zName);
5318   nByte = sizeof(rbu_vfs) + nName + 1;
5319   pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
5320   if( pNew==0 ){
5321     rc = SQLITE_NOMEM;
5322   }else{
5323     sqlite3_vfs *pParent;           /* Parent VFS */
5324     memset(pNew, 0, nByte);
5325     pParent = sqlite3_vfs_find(zParent);
5326     if( pParent==0 ){
5327       rc = SQLITE_NOTFOUND;
5328     }else{
5329       char *zSpace;
5330       memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
5331       pNew->base.mxPathname = pParent->mxPathname;
5332       pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
5333       pNew->pRealVfs = pParent;
5334       pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
5335       memcpy(zSpace, zName, nName);
5336 
5337       /* Allocate the mutex and register the new VFS (not as the default) */
5338       pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
5339       if( pNew->mutex==0 ){
5340         rc = SQLITE_NOMEM;
5341       }else{
5342         rc = sqlite3_vfs_register(&pNew->base, 0);
5343       }
5344     }
5345 
5346     if( rc!=SQLITE_OK ){
5347       sqlite3_mutex_free(pNew->mutex);
5348       sqlite3_free(pNew);
5349     }
5350   }
5351 
5352   return rc;
5353 }
5354 
5355 /*
5356 ** Configure the aggregate temp file size limit for this RBU handle.
5357 */
sqlite3rbu_temp_size_limit(sqlite3rbu * pRbu,sqlite3_int64 n)5358 sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
5359   if( n>=0 ){
5360     pRbu->szTempLimit = n;
5361   }
5362   return pRbu->szTempLimit;
5363 }
5364 
sqlite3rbu_temp_size(sqlite3rbu * pRbu)5365 sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
5366   return pRbu->szTemp;
5367 }
5368 
5369 
5370 /**************************************************************************/
5371 
5372 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
5373