xref: /sqlite-3.40.0/src/test_multiplex.c (revision dfe4e6bb)
1 /*
2 ** 2010 October 28
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 ** This file contains a VFS "shim" - a layer that sits in between the
14 ** pager and the real VFS - that breaks up a very large database file
15 ** into two or more smaller files on disk.  This is useful, for example,
16 ** in order to support large, multi-gigabyte databases on older filesystems
17 ** that limit the maximum file size to 2 GiB.
18 **
19 ** USAGE:
20 **
21 ** Compile this source file and link it with your application.  Then
22 ** at start-time, invoke the following procedure:
23 **
24 **   int sqlite3_multiplex_initialize(
25 **      const char *zOrigVfsName,    // The underlying real VFS
26 **      int makeDefault              // True to make multiplex the default VFS
27 **   );
28 **
29 ** The procedure call above will create and register a new VFS shim named
30 ** "multiplex".  The multiplex VFS will use the VFS named by zOrigVfsName to
31 ** do the actual disk I/O.  (The zOrigVfsName parameter may be NULL, in
32 ** which case the default VFS at the moment sqlite3_multiplex_initialize()
33 ** is called will be used as the underlying real VFS.)
34 **
35 ** If the makeDefault parameter is TRUE then multiplex becomes the new
36 ** default VFS.  Otherwise, you can use the multiplex VFS by specifying
37 ** "multiplex" as the 4th parameter to sqlite3_open_v2() or by employing
38 ** URI filenames and adding "vfs=multiplex" as a parameter to the filename
39 ** URI.
40 **
41 ** The multiplex VFS allows databases up to 32 GiB in size.  But it splits
42 ** the files up into smaller pieces, so that they will work even on
43 ** filesystems that do not support large files.  The default chunk size
44 ** is 2147418112 bytes (which is 64KiB less than 2GiB) but this can be
45 ** changed at compile-time by defining the SQLITE_MULTIPLEX_CHUNK_SIZE
46 ** macro.  Use the "chunksize=NNNN" query parameter with a URI filename
47 ** in order to select an alternative chunk size for individual connections
48 ** at run-time.
49 */
50 #include "sqlite3.h"
51 #include <string.h>
52 #include <assert.h>
53 #include <stdlib.h>
54 #include "test_multiplex.h"
55 
56 #ifndef SQLITE_CORE
57   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
58 #endif
59 #include "sqlite3ext.h"
60 
61 /*
62 ** These should be defined to be the same as the values in
63 ** sqliteInt.h.  They are defined separately here so that
64 ** the multiplex VFS shim can be built as a loadable
65 ** module.
66 */
67 #define UNUSED_PARAMETER(x) (void)(x)
68 #define MAX_PAGE_SIZE       0x10000
69 #define DEFAULT_SECTOR_SIZE 0x1000
70 
71 /*
72 ** For a build without mutexes, no-op the mutex calls.
73 */
74 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
75 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
76 #define sqlite3_mutex_free(X)
77 #define sqlite3_mutex_enter(X)
78 #define sqlite3_mutex_try(X)      SQLITE_OK
79 #define sqlite3_mutex_leave(X)
80 #define sqlite3_mutex_held(X)     ((void)(X),1)
81 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
82 #endif /* SQLITE_THREADSAFE==0 */
83 
84 /* Maximum chunk number */
85 #define MX_CHUNK_NUMBER 299
86 
87 /* First chunk for rollback journal files */
88 #define SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET 400
89 #define SQLITE_MULTIPLEX_WAL_8_3_OFFSET 700
90 
91 
92 /************************ Shim Definitions ******************************/
93 
94 #ifndef SQLITE_MULTIPLEX_VFS_NAME
95 # define SQLITE_MULTIPLEX_VFS_NAME "multiplex"
96 #endif
97 
98 /* This is the limit on the chunk size.  It may be changed by calling
99 ** the xFileControl() interface.  It will be rounded up to a
100 ** multiple of MAX_PAGE_SIZE.  We default it here to 2GiB less 64KiB.
101 */
102 #ifndef SQLITE_MULTIPLEX_CHUNK_SIZE
103 # define SQLITE_MULTIPLEX_CHUNK_SIZE 2147418112
104 #endif
105 
106 /* This used to be the default limit on number of chunks, but
107 ** it is no longer enforced. There is currently no limit to the
108 ** number of chunks.
109 **
110 ** May be changed by calling the xFileControl() interface.
111 */
112 #ifndef SQLITE_MULTIPLEX_MAX_CHUNKS
113 # define SQLITE_MULTIPLEX_MAX_CHUNKS 12
114 #endif
115 
116 /************************ Object Definitions ******************************/
117 
118 /* Forward declaration of all object types */
119 typedef struct multiplexGroup multiplexGroup;
120 typedef struct multiplexConn multiplexConn;
121 
122 /*
123 ** A "multiplex group" is a collection of files that collectively
124 ** makeup a single SQLite DB file.  This allows the size of the DB
125 ** to exceed the limits imposed by the file system.
126 **
127 ** There is an instance of the following object for each defined multiplex
128 ** group.
129 */
130 struct multiplexGroup {
131   struct multiplexReal {           /* For each chunk */
132     sqlite3_file *p;                  /* Handle for the chunk */
133     char *z;                          /* Name of this chunk */
134   } *aReal;                        /* list of all chunks */
135   int nReal;                       /* Number of chunks */
136   char *zName;                     /* Base filename of this group */
137   int nName;                       /* Length of base filename */
138   int flags;                       /* Flags used for original opening */
139   unsigned int szChunk;            /* Chunk size used for this group */
140   unsigned char bEnabled;          /* TRUE to use Multiplex VFS for this file */
141   unsigned char bTruncate;         /* TRUE to enable truncation of databases */
142   multiplexGroup *pNext, *pPrev;   /* Doubly linked list of all group objects */
143 };
144 
145 /*
146 ** An instance of the following object represents each open connection
147 ** to a file that is multiplex'ed.  This object is a
148 ** subclass of sqlite3_file.  The sqlite3_file object for the underlying
149 ** VFS is appended to this structure.
150 */
151 struct multiplexConn {
152   sqlite3_file base;              /* Base class - must be first */
153   multiplexGroup *pGroup;         /* The underlying group of files */
154 };
155 
156 /************************* Global Variables **********************************/
157 /*
158 ** All global variables used by this file are containing within the following
159 ** gMultiplex structure.
160 */
161 static struct {
162   /* The pOrigVfs is the real, original underlying VFS implementation.
163   ** Most operations pass-through to the real VFS.  This value is read-only
164   ** during operation.  It is only modified at start-time and thus does not
165   ** require a mutex.
166   */
167   sqlite3_vfs *pOrigVfs;
168 
169   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
170   ** at start-time and thus does not require a mutex
171   */
172   sqlite3_vfs sThisVfs;
173 
174   /* The sIoMethods defines the methods used by sqlite3_file objects
175   ** associated with this shim.  It is initialized at start-time and does
176   ** not require a mutex.
177   **
178   ** When the underlying VFS is called to open a file, it might return
179   ** either a version 1 or a version 2 sqlite3_file object.  This shim
180   ** has to create a wrapper sqlite3_file of the same version.  Hence
181   ** there are two I/O method structures, one for version 1 and the other
182   ** for version 2.
183   */
184   sqlite3_io_methods sIoMethodsV1;
185   sqlite3_io_methods sIoMethodsV2;
186 
187   /* True when this shim has been initialized.
188   */
189   int isInitialized;
190 
191   /* For run-time access any of the other global data structures in this
192   ** shim, the following mutex must be held. In practice, all this mutex
193   ** protects is add/remove operations to/from the linked list of group objects
194   ** starting at pGroups below. More specifically, it protects the value of
195   ** pGroups itself, and the pNext/pPrev fields of each multiplexGroup
196   ** structure.  */
197   sqlite3_mutex *pMutex;
198 
199   /* List of multiplexGroup objects.
200   */
201   multiplexGroup *pGroups;
202 } gMultiplex;
203 
204 /************************* Utility Routines *********************************/
205 /*
206 ** Acquire and release the mutex used to serialize access to the
207 ** list of multiplexGroups.
208 */
209 static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); }
210 static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); }
211 
212 /*
213 ** Compute a string length that is limited to what can be stored in
214 ** lower 30 bits of a 32-bit signed integer.
215 **
216 ** The value returned will never be negative.  Nor will it ever be greater
217 ** than the actual length of the string.  For very long strings (greater
218 ** than 1GiB) the value returned might be less than the true string length.
219 */
220 static int multiplexStrlen30(const char *z){
221   const char *z2 = z;
222   if( z==0 ) return 0;
223   while( *z2 ){ z2++; }
224   return 0x3fffffff & (int)(z2 - z);
225 }
226 
227 /*
228 ** Generate the file-name for chunk iChunk of the group with base name
229 ** zBase. The file-name is written to buffer zOut before returning. Buffer
230 ** zOut must be allocated by the caller so that it is at least (nBase+5)
231 ** bytes in size, where nBase is the length of zBase, not including the
232 ** nul-terminator.
233 **
234 ** If iChunk is 0 (or 400 - the number for the first journal file chunk),
235 ** the output is a copy of the input string. Otherwise, if
236 ** SQLITE_ENABLE_8_3_NAMES is not defined or the input buffer does not contain
237 ** a "." character, then the output is a copy of the input string with the
238 ** three-digit zero-padded decimal representation if iChunk appended to it.
239 ** For example:
240 **
241 **   zBase="test.db", iChunk=4  ->  zOut="test.db004"
242 **
243 ** Or, if SQLITE_ENABLE_8_3_NAMES is defined and the input buffer contains
244 ** a "." character, then everything after the "." is replaced by the
245 ** three-digit representation of iChunk.
246 **
247 **   zBase="test.db", iChunk=4  ->  zOut="test.004"
248 **
249 ** The output buffer string is terminated by 2 0x00 bytes. This makes it safe
250 ** to pass to sqlite3_uri_parameter() and similar.
251 */
252 static void multiplexFilename(
253   const char *zBase,              /* Filename for chunk 0 */
254   int nBase,                      /* Size of zBase in bytes (without \0) */
255   int flags,                      /* Flags used to open file */
256   int iChunk,                     /* Chunk to generate filename for */
257   char *zOut                      /* Buffer to write generated name to */
258 ){
259   int n = nBase;
260   memcpy(zOut, zBase, n+1);
261   if( iChunk!=0 && iChunk<=MX_CHUNK_NUMBER ){
262 #ifdef SQLITE_ENABLE_8_3_NAMES
263     int i;
264     for(i=n-1; i>0 && i>=n-4 && zOut[i]!='.'; i--){}
265     if( i>=n-4 ) n = i+1;
266     if( flags & SQLITE_OPEN_MAIN_JOURNAL ){
267       /* The extensions on overflow files for main databases are 001, 002,
268       ** 003 and so forth.  To avoid name collisions, add 400 to the
269       ** extensions of journal files so that they are 401, 402, 403, ....
270       */
271       iChunk += SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET;
272     }else if( flags & SQLITE_OPEN_WAL ){
273       /* To avoid name collisions, add 700 to the
274       ** extensions of WAL files so that they are 701, 702, 703, ....
275       */
276       iChunk += SQLITE_MULTIPLEX_WAL_8_3_OFFSET;
277     }
278 #endif
279     sqlite3_snprintf(4,&zOut[n],"%03d",iChunk);
280     n += 3;
281   }
282 
283   assert( zOut[n]=='\0' );
284   zOut[n+1] = '\0';
285 }
286 
287 /* Compute the filename for the iChunk-th chunk
288 */
289 static int multiplexSubFilename(multiplexGroup *pGroup, int iChunk){
290   if( iChunk>=pGroup->nReal ){
291     struct multiplexReal *p;
292     p = sqlite3_realloc64(pGroup->aReal, (iChunk+1)*sizeof(*p));
293     if( p==0 ){
294       return SQLITE_NOMEM;
295     }
296     memset(&p[pGroup->nReal], 0, sizeof(p[0])*(iChunk+1-pGroup->nReal));
297     pGroup->aReal = p;
298     pGroup->nReal = iChunk+1;
299   }
300   if( pGroup->zName && pGroup->aReal[iChunk].z==0 ){
301     char *z;
302     int n = pGroup->nName;
303     pGroup->aReal[iChunk].z = z = sqlite3_malloc64( n+5 );
304     if( z==0 ){
305       return SQLITE_NOMEM;
306     }
307     multiplexFilename(pGroup->zName, pGroup->nName, pGroup->flags, iChunk, z);
308   }
309   return SQLITE_OK;
310 }
311 
312 /* Translate an sqlite3_file* that is really a multiplexGroup* into
313 ** the sqlite3_file* for the underlying original VFS.
314 **
315 ** For chunk 0, the pGroup->flags determines whether or not a new file
316 ** is created if it does not already exist.  For chunks 1 and higher, the
317 ** file is created only if createFlag is 1.
318 */
319 static sqlite3_file *multiplexSubOpen(
320   multiplexGroup *pGroup,    /* The multiplexor group */
321   int iChunk,                /* Which chunk to open.  0==original file */
322   int *rc,                   /* Result code in and out */
323   int *pOutFlags,            /* Output flags */
324   int createFlag             /* True to create if iChunk>0 */
325 ){
326   sqlite3_file *pSubOpen = 0;
327   sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;        /* Real VFS */
328 
329 #ifdef SQLITE_ENABLE_8_3_NAMES
330   /* If JOURNAL_8_3_OFFSET is set to (say) 400, then any overflow files are
331   ** part of a database journal are named db.401, db.402, and so on. A
332   ** database may therefore not grow to larger than 400 chunks. Attempting
333   ** to open chunk 401 indicates the database is full. */
334   if( iChunk>=SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET ){
335     sqlite3_log(SQLITE_FULL, "multiplexed chunk overflow: %s", pGroup->zName);
336     *rc = SQLITE_FULL;
337     return 0;
338   }
339 #endif
340 
341   *rc = multiplexSubFilename(pGroup, iChunk);
342   if( (*rc)==SQLITE_OK && (pSubOpen = pGroup->aReal[iChunk].p)==0 ){
343     int flags, bExists;
344     flags = pGroup->flags;
345     if( createFlag ){
346       flags |= SQLITE_OPEN_CREATE;
347     }else if( iChunk==0 ){
348       /* Fall through */
349     }else if( pGroup->aReal[iChunk].z==0 ){
350       return 0;
351     }else{
352       *rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[iChunk].z,
353                               SQLITE_ACCESS_EXISTS, &bExists);
354      if( *rc || !bExists ){
355         if( *rc ){
356           sqlite3_log(*rc, "multiplexor.xAccess failure on %s",
357                       pGroup->aReal[iChunk].z);
358         }
359         return 0;
360       }
361       flags &= ~SQLITE_OPEN_CREATE;
362     }
363     pSubOpen = sqlite3_malloc64( pOrigVfs->szOsFile );
364     if( pSubOpen==0 ){
365       *rc = SQLITE_IOERR_NOMEM;
366       return 0;
367     }
368     pGroup->aReal[iChunk].p = pSubOpen;
369     *rc = pOrigVfs->xOpen(pOrigVfs, pGroup->aReal[iChunk].z, pSubOpen,
370                           flags, pOutFlags);
371     if( (*rc)!=SQLITE_OK ){
372       sqlite3_log(*rc, "multiplexor.xOpen failure on %s",
373                   pGroup->aReal[iChunk].z);
374       sqlite3_free(pSubOpen);
375       pGroup->aReal[iChunk].p = 0;
376       return 0;
377     }
378   }
379   return pSubOpen;
380 }
381 
382 /*
383 ** Return the size, in bytes, of chunk number iChunk.  If that chunk
384 ** does not exist, then return 0.  This function does not distingish between
385 ** non-existant files and zero-length files.
386 */
387 static sqlite3_int64 multiplexSubSize(
388   multiplexGroup *pGroup,    /* The multiplexor group */
389   int iChunk,                /* Which chunk to open.  0==original file */
390   int *rc                    /* Result code in and out */
391 ){
392   sqlite3_file *pSub;
393   sqlite3_int64 sz = 0;
394 
395   if( *rc ) return 0;
396   pSub = multiplexSubOpen(pGroup, iChunk, rc, NULL, 0);
397   if( pSub==0 ) return 0;
398   *rc = pSub->pMethods->xFileSize(pSub, &sz);
399   return sz;
400 }
401 
402 /*
403 ** This is the implementation of the multiplex_control() SQL function.
404 */
405 static void multiplexControlFunc(
406   sqlite3_context *context,
407   int argc,
408   sqlite3_value **argv
409 ){
410   int rc = SQLITE_OK;
411   sqlite3 *db = sqlite3_context_db_handle(context);
412   int op = 0;
413   int iVal;
414 
415   if( !db || argc!=2 ){
416     rc = SQLITE_ERROR;
417   }else{
418     /* extract params */
419     op = sqlite3_value_int(argv[0]);
420     iVal = sqlite3_value_int(argv[1]);
421     /* map function op to file_control op */
422     switch( op ){
423       case 1:
424         op = MULTIPLEX_CTRL_ENABLE;
425         break;
426       case 2:
427         op = MULTIPLEX_CTRL_SET_CHUNK_SIZE;
428         break;
429       case 3:
430         op = MULTIPLEX_CTRL_SET_MAX_CHUNKS;
431         break;
432       default:
433         rc = SQLITE_NOTFOUND;
434         break;
435     }
436   }
437   if( rc==SQLITE_OK ){
438     rc = sqlite3_file_control(db, 0, op, &iVal);
439   }
440   sqlite3_result_error_code(context, rc);
441 }
442 
443 /*
444 ** This is the entry point to register the auto-extension for the
445 ** multiplex_control() function.
446 */
447 static int multiplexFuncInit(
448   sqlite3 *db,
449   char **pzErrMsg,
450   const sqlite3_api_routines *pApi
451 ){
452   int rc;
453   rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY,
454       0, multiplexControlFunc, 0, 0);
455   return rc;
456 }
457 
458 /*
459 ** Close a single sub-file in the connection group.
460 */
461 static void multiplexSubClose(
462   multiplexGroup *pGroup,
463   int iChunk,
464   sqlite3_vfs *pOrigVfs
465 ){
466   sqlite3_file *pSubOpen = pGroup->aReal[iChunk].p;
467   if( pSubOpen ){
468     pSubOpen->pMethods->xClose(pSubOpen);
469     if( pOrigVfs && pGroup->aReal[iChunk].z ){
470       pOrigVfs->xDelete(pOrigVfs, pGroup->aReal[iChunk].z, 0);
471     }
472     sqlite3_free(pGroup->aReal[iChunk].p);
473   }
474   sqlite3_free(pGroup->aReal[iChunk].z);
475   memset(&pGroup->aReal[iChunk], 0, sizeof(pGroup->aReal[iChunk]));
476 }
477 
478 /*
479 ** Deallocate memory held by a multiplexGroup
480 */
481 static void multiplexFreeComponents(multiplexGroup *pGroup){
482   int i;
483   for(i=0; i<pGroup->nReal; i++){ multiplexSubClose(pGroup, i, 0); }
484   sqlite3_free(pGroup->aReal);
485   pGroup->aReal = 0;
486   pGroup->nReal = 0;
487 }
488 
489 
490 /************************* VFS Method Wrappers *****************************/
491 
492 /*
493 ** This is the xOpen method used for the "multiplex" VFS.
494 **
495 ** Most of the work is done by the underlying original VFS.  This method
496 ** simply links the new file into the appropriate multiplex group if it is a
497 ** file that needs to be tracked.
498 */
499 static int multiplexOpen(
500   sqlite3_vfs *pVfs,         /* The multiplex VFS */
501   const char *zName,         /* Name of file to be opened */
502   sqlite3_file *pConn,       /* Fill in this file descriptor */
503   int flags,                 /* Flags to control the opening */
504   int *pOutFlags             /* Flags showing results of opening */
505 ){
506   int rc = SQLITE_OK;                  /* Result code */
507   multiplexConn *pMultiplexOpen;       /* The new multiplex file descriptor */
508   multiplexGroup *pGroup = 0;          /* Corresponding multiplexGroup object */
509   sqlite3_file *pSubOpen = 0;                    /* Real file descriptor */
510   sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
511   int nName = 0;
512   int sz = 0;
513   char *zToFree = 0;
514 
515   UNUSED_PARAMETER(pVfs);
516   memset(pConn, 0, pVfs->szOsFile);
517   assert( zName || (flags & SQLITE_OPEN_DELETEONCLOSE) );
518 
519   /* We need to create a group structure and manage
520   ** access to this group of files.
521   */
522   multiplexEnter();
523   pMultiplexOpen = (multiplexConn*)pConn;
524 
525   if( rc==SQLITE_OK ){
526     /* allocate space for group */
527     nName = zName ? multiplexStrlen30(zName) : 0;
528     sz = sizeof(multiplexGroup)                             /* multiplexGroup */
529        + nName + 1;                                         /* zName */
530     pGroup = sqlite3_malloc64( sz );
531     if( pGroup==0 ){
532       rc = SQLITE_NOMEM;
533     }
534   }
535 
536   if( rc==SQLITE_OK ){
537     const char *zUri = (flags & SQLITE_OPEN_URI) ? zName : 0;
538     /* assign pointers to extra space allocated */
539     memset(pGroup, 0, sz);
540     pMultiplexOpen->pGroup = pGroup;
541     pGroup->bEnabled = (unsigned char)-1;
542     pGroup->bTruncate = (unsigned char)sqlite3_uri_boolean(zUri, "truncate",
543                                    (flags & SQLITE_OPEN_MAIN_DB)==0);
544     pGroup->szChunk = (int)sqlite3_uri_int64(zUri, "chunksize",
545                                         SQLITE_MULTIPLEX_CHUNK_SIZE);
546     pGroup->szChunk = (pGroup->szChunk+0xffff)&~0xffff;
547     if( zName ){
548       char *p = (char *)&pGroup[1];
549       pGroup->zName = p;
550       memcpy(pGroup->zName, zName, nName+1);
551       pGroup->nName = nName;
552     }
553     if( pGroup->bEnabled ){
554       /* Make sure that the chunksize is such that the pending byte does not
555       ** falls at the end of a chunk.  A region of up to 64K following
556       ** the pending byte is never written, so if the pending byte occurs
557       ** near the end of a chunk, that chunk will be too small. */
558 #ifndef SQLITE_OMIT_WSD
559       extern int sqlite3PendingByte;
560 #else
561       int sqlite3PendingByte = 0x40000000;
562 #endif
563       while( (sqlite3PendingByte % pGroup->szChunk)>=(pGroup->szChunk-65536) ){
564         pGroup->szChunk += 65536;
565       }
566     }
567     pGroup->flags = flags;
568     rc = multiplexSubFilename(pGroup, 1);
569     if( rc==SQLITE_OK ){
570       pSubOpen = multiplexSubOpen(pGroup, 0, &rc, pOutFlags, 0);
571       if( pSubOpen==0 && rc==SQLITE_OK ) rc = SQLITE_CANTOPEN;
572     }
573     if( rc==SQLITE_OK ){
574       sqlite3_int64 sz64;
575 
576       rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz64);
577       if( rc==SQLITE_OK && zName ){
578         int bExists;
579         if( flags & SQLITE_OPEN_MASTER_JOURNAL ){
580           pGroup->bEnabled = 0;
581         }else
582         if( sz64==0 ){
583           if( flags & SQLITE_OPEN_MAIN_JOURNAL ){
584             /* If opening a main journal file and the first chunk is zero
585             ** bytes in size, delete any subsequent chunks from the
586             ** file-system. */
587             int iChunk = 1;
588             do {
589               rc = pOrigVfs->xAccess(pOrigVfs,
590                   pGroup->aReal[iChunk].z, SQLITE_ACCESS_EXISTS, &bExists
591               );
592               if( rc==SQLITE_OK && bExists ){
593                 rc = pOrigVfs->xDelete(pOrigVfs, pGroup->aReal[iChunk].z, 0);
594                 if( rc==SQLITE_OK ){
595                   rc = multiplexSubFilename(pGroup, ++iChunk);
596                 }
597               }
598             }while( rc==SQLITE_OK && bExists );
599           }
600         }else{
601           /* If the first overflow file exists and if the size of the main file
602           ** is different from the chunk size, that means the chunk size is set
603           ** set incorrectly.  So fix it.
604           **
605           ** Or, if the first overflow file does not exist and the main file is
606           ** larger than the chunk size, that means the chunk size is too small.
607           ** But we have no way of determining the intended chunk size, so
608           ** just disable the multiplexor all togethre.
609           */
610           rc = pOrigVfs->xAccess(pOrigVfs, pGroup->aReal[1].z,
611               SQLITE_ACCESS_EXISTS, &bExists);
612           bExists = multiplexSubSize(pGroup, 1, &rc)>0;
613           if( rc==SQLITE_OK && bExists && sz64==(sz64&0xffff0000) && sz64>0
614               && sz64!=pGroup->szChunk ){
615             pGroup->szChunk = (int)sz64;
616           }else if( rc==SQLITE_OK && !bExists && sz64>pGroup->szChunk ){
617             pGroup->bEnabled = 0;
618           }
619         }
620       }
621     }
622 
623     if( rc==SQLITE_OK ){
624       if( pSubOpen->pMethods->iVersion==1 ){
625         pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1;
626       }else{
627         pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2;
628       }
629       /* place this group at the head of our list */
630       pGroup->pNext = gMultiplex.pGroups;
631       if( gMultiplex.pGroups ) gMultiplex.pGroups->pPrev = pGroup;
632       gMultiplex.pGroups = pGroup;
633     }else{
634       multiplexFreeComponents(pGroup);
635       sqlite3_free(pGroup);
636     }
637   }
638   multiplexLeave();
639   sqlite3_free(zToFree);
640   return rc;
641 }
642 
643 /*
644 ** This is the xDelete method used for the "multiplex" VFS.
645 ** It attempts to delete the filename specified.
646 */
647 static int multiplexDelete(
648   sqlite3_vfs *pVfs,         /* The multiplex VFS */
649   const char *zName,         /* Name of file to delete */
650   int syncDir
651 ){
652   int rc;
653   sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
654   rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
655   if( rc==SQLITE_OK ){
656     /* If the main chunk was deleted successfully, also delete any subsequent
657     ** chunks - starting with the last (highest numbered).
658     */
659     int nName = (int)strlen(zName);
660     char *z;
661     z = sqlite3_malloc64(nName + 5);
662     if( z==0 ){
663       rc = SQLITE_IOERR_NOMEM;
664     }else{
665       int iChunk = 0;
666       int bExists;
667       do{
668         multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, ++iChunk, z);
669         rc = pOrigVfs->xAccess(pOrigVfs, z, SQLITE_ACCESS_EXISTS, &bExists);
670       }while( rc==SQLITE_OK && bExists );
671       while( rc==SQLITE_OK && iChunk>1 ){
672         multiplexFilename(zName, nName, SQLITE_OPEN_MAIN_JOURNAL, --iChunk, z);
673         rc = pOrigVfs->xDelete(pOrigVfs, z, syncDir);
674       }
675       if( rc==SQLITE_OK ){
676         iChunk = 0;
677         do{
678           multiplexFilename(zName, nName, SQLITE_OPEN_WAL, ++iChunk, z);
679           rc = pOrigVfs->xAccess(pOrigVfs, z, SQLITE_ACCESS_EXISTS, &bExists);
680         }while( rc==SQLITE_OK && bExists );
681         while( rc==SQLITE_OK && iChunk>1 ){
682           multiplexFilename(zName, nName, SQLITE_OPEN_WAL, --iChunk, z);
683           rc = pOrigVfs->xDelete(pOrigVfs, z, syncDir);
684         }
685       }
686     }
687     sqlite3_free(z);
688   }
689   return rc;
690 }
691 
692 static int multiplexAccess(sqlite3_vfs *a, const char *b, int c, int *d){
693   return gMultiplex.pOrigVfs->xAccess(gMultiplex.pOrigVfs, b, c, d);
694 }
695 static int multiplexFullPathname(sqlite3_vfs *a, const char *b, int c, char *d){
696   return gMultiplex.pOrigVfs->xFullPathname(gMultiplex.pOrigVfs, b, c, d);
697 }
698 static void *multiplexDlOpen(sqlite3_vfs *a, const char *b){
699   return gMultiplex.pOrigVfs->xDlOpen(gMultiplex.pOrigVfs, b);
700 }
701 static void multiplexDlError(sqlite3_vfs *a, int b, char *c){
702   gMultiplex.pOrigVfs->xDlError(gMultiplex.pOrigVfs, b, c);
703 }
704 static void (*multiplexDlSym(sqlite3_vfs *a, void *b, const char *c))(void){
705   return gMultiplex.pOrigVfs->xDlSym(gMultiplex.pOrigVfs, b, c);
706 }
707 static void multiplexDlClose(sqlite3_vfs *a, void *b){
708   gMultiplex.pOrigVfs->xDlClose(gMultiplex.pOrigVfs, b);
709 }
710 static int multiplexRandomness(sqlite3_vfs *a, int b, char *c){
711   return gMultiplex.pOrigVfs->xRandomness(gMultiplex.pOrigVfs, b, c);
712 }
713 static int multiplexSleep(sqlite3_vfs *a, int b){
714   return gMultiplex.pOrigVfs->xSleep(gMultiplex.pOrigVfs, b);
715 }
716 static int multiplexCurrentTime(sqlite3_vfs *a, double *b){
717   return gMultiplex.pOrigVfs->xCurrentTime(gMultiplex.pOrigVfs, b);
718 }
719 static int multiplexGetLastError(sqlite3_vfs *a, int b, char *c){
720   if( gMultiplex.pOrigVfs->xGetLastError ){
721     return gMultiplex.pOrigVfs->xGetLastError(gMultiplex.pOrigVfs, b, c);
722   }else{
723     return 0;
724   }
725 }
726 static int multiplexCurrentTimeInt64(sqlite3_vfs *a, sqlite3_int64 *b){
727   return gMultiplex.pOrigVfs->xCurrentTimeInt64(gMultiplex.pOrigVfs, b);
728 }
729 
730 /************************ I/O Method Wrappers *******************************/
731 
732 /* xClose requests get passed through to the original VFS.
733 ** We loop over all open chunk handles and close them.
734 ** The group structure for this file is unlinked from
735 ** our list of groups and freed.
736 */
737 static int multiplexClose(sqlite3_file *pConn){
738   multiplexConn *p = (multiplexConn*)pConn;
739   multiplexGroup *pGroup = p->pGroup;
740   int rc = SQLITE_OK;
741   multiplexEnter();
742   multiplexFreeComponents(pGroup);
743   /* remove from linked list */
744   if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev;
745   if( pGroup->pPrev ){
746     pGroup->pPrev->pNext = pGroup->pNext;
747   }else{
748     gMultiplex.pGroups = pGroup->pNext;
749   }
750   sqlite3_free(pGroup);
751   multiplexLeave();
752   return rc;
753 }
754 
755 /* Pass xRead requests thru to the original VFS after
756 ** determining the correct chunk to operate on.
757 ** Break up reads across chunk boundaries.
758 */
759 static int multiplexRead(
760   sqlite3_file *pConn,
761   void *pBuf,
762   int iAmt,
763   sqlite3_int64 iOfst
764 ){
765   multiplexConn *p = (multiplexConn*)pConn;
766   multiplexGroup *pGroup = p->pGroup;
767   int rc = SQLITE_OK;
768   if( !pGroup->bEnabled ){
769     sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
770     if( pSubOpen==0 ){
771       rc = SQLITE_IOERR_READ;
772     }else{
773       rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
774     }
775   }else{
776     while( iAmt > 0 ){
777       int i = (int)(iOfst / pGroup->szChunk);
778       sqlite3_file *pSubOpen;
779       pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1);
780       if( pSubOpen ){
781         int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) - pGroup->szChunk;
782         if( extra<0 ) extra = 0;
783         iAmt -= extra;
784         rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt,
785                                        iOfst % pGroup->szChunk);
786         if( rc!=SQLITE_OK ) break;
787         pBuf = (char *)pBuf + iAmt;
788         iOfst += iAmt;
789         iAmt = extra;
790       }else{
791         rc = SQLITE_IOERR_READ;
792         break;
793       }
794     }
795   }
796 
797   return rc;
798 }
799 
800 /* Pass xWrite requests thru to the original VFS after
801 ** determining the correct chunk to operate on.
802 ** Break up writes across chunk boundaries.
803 */
804 static int multiplexWrite(
805   sqlite3_file *pConn,
806   const void *pBuf,
807   int iAmt,
808   sqlite3_int64 iOfst
809 ){
810   multiplexConn *p = (multiplexConn*)pConn;
811   multiplexGroup *pGroup = p->pGroup;
812   int rc = SQLITE_OK;
813   if( !pGroup->bEnabled ){
814     sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
815     if( pSubOpen==0 ){
816       rc = SQLITE_IOERR_WRITE;
817     }else{
818       rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
819     }
820   }else{
821     while( rc==SQLITE_OK && iAmt>0 ){
822       int i = (int)(iOfst / pGroup->szChunk);
823       sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, i, &rc, NULL, 1);
824       if( pSubOpen ){
825         int extra = ((int)(iOfst % pGroup->szChunk) + iAmt) -
826                     pGroup->szChunk;
827         if( extra<0 ) extra = 0;
828         iAmt -= extra;
829         rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt,
830                                         iOfst % pGroup->szChunk);
831         pBuf = (char *)pBuf + iAmt;
832         iOfst += iAmt;
833         iAmt = extra;
834       }
835     }
836   }
837   return rc;
838 }
839 
840 /* Pass xTruncate requests thru to the original VFS after
841 ** determining the correct chunk to operate on.  Delete any
842 ** chunks above the truncate mark.
843 */
844 static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){
845   multiplexConn *p = (multiplexConn*)pConn;
846   multiplexGroup *pGroup = p->pGroup;
847   int rc = SQLITE_OK;
848   multiplexEnter();
849   if( !pGroup->bEnabled ){
850     sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
851     if( pSubOpen==0 ){
852       rc = SQLITE_IOERR_TRUNCATE;
853     }else{
854       rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
855     }
856   }else{
857     int i;
858     int iBaseGroup = (int)(size / pGroup->szChunk);
859     sqlite3_file *pSubOpen;
860     sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
861     /* delete the chunks above the truncate limit */
862     for(i = pGroup->nReal-1; i>iBaseGroup && rc==SQLITE_OK; i--){
863       if( pGroup->bTruncate ){
864         multiplexSubClose(pGroup, i, pOrigVfs);
865       }else{
866         pSubOpen = multiplexSubOpen(pGroup, i, &rc, 0, 0);
867         if( pSubOpen ){
868           rc = pSubOpen->pMethods->xTruncate(pSubOpen, 0);
869         }
870       }
871     }
872     if( rc==SQLITE_OK ){
873       pSubOpen = multiplexSubOpen(pGroup, iBaseGroup, &rc, 0, 0);
874       if( pSubOpen ){
875         rc = pSubOpen->pMethods->xTruncate(pSubOpen, size % pGroup->szChunk);
876       }
877     }
878     if( rc ) rc = SQLITE_IOERR_TRUNCATE;
879   }
880   multiplexLeave();
881   return rc;
882 }
883 
884 /* Pass xSync requests through to the original VFS without change
885 */
886 static int multiplexSync(sqlite3_file *pConn, int flags){
887   multiplexConn *p = (multiplexConn*)pConn;
888   multiplexGroup *pGroup = p->pGroup;
889   int rc = SQLITE_OK;
890   int i;
891   multiplexEnter();
892   for(i=0; i<pGroup->nReal; i++){
893     sqlite3_file *pSubOpen = pGroup->aReal[i].p;
894     if( pSubOpen ){
895       int rc2 = pSubOpen->pMethods->xSync(pSubOpen, flags);
896       if( rc2!=SQLITE_OK ) rc = rc2;
897     }
898   }
899   multiplexLeave();
900   return rc;
901 }
902 
903 /* Pass xFileSize requests through to the original VFS.
904 ** Aggregate the size of all the chunks before returning.
905 */
906 static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
907   multiplexConn *p = (multiplexConn*)pConn;
908   multiplexGroup *pGroup = p->pGroup;
909   int rc = SQLITE_OK;
910   int i;
911   multiplexEnter();
912   if( !pGroup->bEnabled ){
913     sqlite3_file *pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
914     if( pSubOpen==0 ){
915       rc = SQLITE_IOERR_FSTAT;
916     }else{
917       rc = pSubOpen->pMethods->xFileSize(pSubOpen, pSize);
918     }
919   }else{
920     *pSize = 0;
921     for(i=0; rc==SQLITE_OK; i++){
922       sqlite3_int64 sz = multiplexSubSize(pGroup, i, &rc);
923       if( sz==0 ) break;
924       *pSize = i*(sqlite3_int64)pGroup->szChunk + sz;
925     }
926   }
927   multiplexLeave();
928   return rc;
929 }
930 
931 /* Pass xLock requests through to the original VFS unchanged.
932 */
933 static int multiplexLock(sqlite3_file *pConn, int lock){
934   multiplexConn *p = (multiplexConn*)pConn;
935   int rc;
936   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
937   if( pSubOpen ){
938     return pSubOpen->pMethods->xLock(pSubOpen, lock);
939   }
940   return SQLITE_BUSY;
941 }
942 
943 /* Pass xUnlock requests through to the original VFS unchanged.
944 */
945 static int multiplexUnlock(sqlite3_file *pConn, int lock){
946   multiplexConn *p = (multiplexConn*)pConn;
947   int rc;
948   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
949   if( pSubOpen ){
950     return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
951   }
952   return SQLITE_IOERR_UNLOCK;
953 }
954 
955 /* Pass xCheckReservedLock requests through to the original VFS unchanged.
956 */
957 static int multiplexCheckReservedLock(sqlite3_file *pConn, int *pResOut){
958   multiplexConn *p = (multiplexConn*)pConn;
959   int rc;
960   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
961   if( pSubOpen ){
962     return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
963   }
964   return SQLITE_IOERR_CHECKRESERVEDLOCK;
965 }
966 
967 /* Pass xFileControl requests through to the original VFS unchanged,
968 ** except for any MULTIPLEX_CTRL_* requests here.
969 */
970 static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){
971   multiplexConn *p = (multiplexConn*)pConn;
972   multiplexGroup *pGroup = p->pGroup;
973   int rc = SQLITE_ERROR;
974   sqlite3_file *pSubOpen;
975 
976   if( !gMultiplex.isInitialized ) return SQLITE_MISUSE;
977   switch( op ){
978     case MULTIPLEX_CTRL_ENABLE:
979       if( pArg ) {
980         int bEnabled = *(int *)pArg;
981         pGroup->bEnabled = (unsigned char)bEnabled;
982         rc = SQLITE_OK;
983       }
984       break;
985     case MULTIPLEX_CTRL_SET_CHUNK_SIZE:
986       if( pArg ) {
987         unsigned int szChunk = *(unsigned*)pArg;
988         if( szChunk<1 ){
989           rc = SQLITE_MISUSE;
990         }else{
991           /* Round up to nearest multiple of MAX_PAGE_SIZE. */
992           szChunk = (szChunk + (MAX_PAGE_SIZE-1));
993           szChunk &= ~(MAX_PAGE_SIZE-1);
994           pGroup->szChunk = szChunk;
995           rc = SQLITE_OK;
996         }
997       }
998       break;
999     case MULTIPLEX_CTRL_SET_MAX_CHUNKS:
1000       rc = SQLITE_OK;
1001       break;
1002     case SQLITE_FCNTL_SIZE_HINT:
1003     case SQLITE_FCNTL_CHUNK_SIZE:
1004       /* no-op these */
1005       rc = SQLITE_OK;
1006       break;
1007     case SQLITE_FCNTL_PRAGMA: {
1008       char **aFcntl = (char**)pArg;
1009       /*
1010       ** EVIDENCE-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
1011       ** file control is an array of pointers to strings (char**) in which the
1012       ** second element of the array is the name of the pragma and the third
1013       ** element is the argument to the pragma or NULL if the pragma has no
1014       ** argument.
1015       */
1016       if( aFcntl[1] && sqlite3_stricmp(aFcntl[1],"multiplex_truncate")==0 ){
1017         if( aFcntl[2] && aFcntl[2][0] ){
1018           if( sqlite3_stricmp(aFcntl[2], "on")==0
1019            || sqlite3_stricmp(aFcntl[2], "1")==0 ){
1020             pGroup->bTruncate = 1;
1021           }else
1022           if( sqlite3_stricmp(aFcntl[2], "off")==0
1023            || sqlite3_stricmp(aFcntl[2], "0")==0 ){
1024             pGroup->bTruncate = 0;
1025           }
1026         }
1027         /* EVIDENCE-OF: R-27806-26076 The handler for an SQLITE_FCNTL_PRAGMA
1028         ** file control can optionally make the first element of the char**
1029         ** argument point to a string obtained from sqlite3_mprintf() or the
1030         ** equivalent and that string will become the result of the pragma
1031         ** or the error message if the pragma fails.
1032         */
1033         aFcntl[0] = sqlite3_mprintf(pGroup->bTruncate ? "on" : "off");
1034         rc = SQLITE_OK;
1035         break;
1036       }
1037       /* If the multiplexor does not handle the pragma, pass it through
1038       ** into the default case. */
1039     }
1040     default:
1041       pSubOpen = multiplexSubOpen(pGroup, 0, &rc, NULL, 0);
1042       if( pSubOpen ){
1043         rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
1044         if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
1045          *(char**)pArg = sqlite3_mprintf("multiplex/%z", *(char**)pArg);
1046         }
1047       }
1048       break;
1049   }
1050   return rc;
1051 }
1052 
1053 /* Pass xSectorSize requests through to the original VFS unchanged.
1054 */
1055 static int multiplexSectorSize(sqlite3_file *pConn){
1056   multiplexConn *p = (multiplexConn*)pConn;
1057   int rc;
1058   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1059   if( pSubOpen && pSubOpen->pMethods->xSectorSize ){
1060     return pSubOpen->pMethods->xSectorSize(pSubOpen);
1061   }
1062   return DEFAULT_SECTOR_SIZE;
1063 }
1064 
1065 /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
1066 */
1067 static int multiplexDeviceCharacteristics(sqlite3_file *pConn){
1068   multiplexConn *p = (multiplexConn*)pConn;
1069   int rc;
1070   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1071   if( pSubOpen ){
1072     return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
1073   }
1074   return 0;
1075 }
1076 
1077 /* Pass xShmMap requests through to the original VFS unchanged.
1078 */
1079 static int multiplexShmMap(
1080   sqlite3_file *pConn,            /* Handle open on database file */
1081   int iRegion,                    /* Region to retrieve */
1082   int szRegion,                   /* Size of regions */
1083   int bExtend,                    /* True to extend file if necessary */
1084   void volatile **pp              /* OUT: Mapped memory */
1085 ){
1086   multiplexConn *p = (multiplexConn*)pConn;
1087   int rc;
1088   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1089   if( pSubOpen ){
1090     return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend,pp);
1091   }
1092   return SQLITE_IOERR;
1093 }
1094 
1095 /* Pass xShmLock requests through to the original VFS unchanged.
1096 */
1097 static int multiplexShmLock(
1098   sqlite3_file *pConn,       /* Database file holding the shared memory */
1099   int ofst,                  /* First lock to acquire or release */
1100   int n,                     /* Number of locks to acquire or release */
1101   int flags                  /* What to do with the lock */
1102 ){
1103   multiplexConn *p = (multiplexConn*)pConn;
1104   int rc;
1105   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1106   if( pSubOpen ){
1107     return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
1108   }
1109   return SQLITE_BUSY;
1110 }
1111 
1112 /* Pass xShmBarrier requests through to the original VFS unchanged.
1113 */
1114 static void multiplexShmBarrier(sqlite3_file *pConn){
1115   multiplexConn *p = (multiplexConn*)pConn;
1116   int rc;
1117   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1118   if( pSubOpen ){
1119     pSubOpen->pMethods->xShmBarrier(pSubOpen);
1120   }
1121 }
1122 
1123 /* Pass xShmUnmap requests through to the original VFS unchanged.
1124 */
1125 static int multiplexShmUnmap(sqlite3_file *pConn, int deleteFlag){
1126   multiplexConn *p = (multiplexConn*)pConn;
1127   int rc;
1128   sqlite3_file *pSubOpen = multiplexSubOpen(p->pGroup, 0, &rc, NULL, 0);
1129   if( pSubOpen ){
1130     return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
1131   }
1132   return SQLITE_OK;
1133 }
1134 
1135 /************************** Public Interfaces *****************************/
1136 /*
1137 ** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize()
1138 **
1139 ** Use the VFS named zOrigVfsName as the VFS that does the actual work.
1140 ** Use the default if zOrigVfsName==NULL.
1141 **
1142 ** The multiplex VFS shim is named "multiplex".  It will become the default
1143 ** VFS if makeDefault is non-zero.
1144 **
1145 ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
1146 ** during start-up.
1147 */
1148 int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault){
1149   sqlite3_vfs *pOrigVfs;
1150   if( gMultiplex.isInitialized ) return SQLITE_MISUSE;
1151   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
1152   if( pOrigVfs==0 ) return SQLITE_ERROR;
1153   assert( pOrigVfs!=&gMultiplex.sThisVfs );
1154   gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
1155   if( !gMultiplex.pMutex ){
1156     return SQLITE_NOMEM;
1157   }
1158   gMultiplex.pGroups = NULL;
1159   gMultiplex.isInitialized = 1;
1160   gMultiplex.pOrigVfs = pOrigVfs;
1161   gMultiplex.sThisVfs = *pOrigVfs;
1162   gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn);
1163   gMultiplex.sThisVfs.zName = SQLITE_MULTIPLEX_VFS_NAME;
1164   gMultiplex.sThisVfs.xOpen = multiplexOpen;
1165   gMultiplex.sThisVfs.xDelete = multiplexDelete;
1166   gMultiplex.sThisVfs.xAccess = multiplexAccess;
1167   gMultiplex.sThisVfs.xFullPathname = multiplexFullPathname;
1168   gMultiplex.sThisVfs.xDlOpen = multiplexDlOpen;
1169   gMultiplex.sThisVfs.xDlError = multiplexDlError;
1170   gMultiplex.sThisVfs.xDlSym = multiplexDlSym;
1171   gMultiplex.sThisVfs.xDlClose = multiplexDlClose;
1172   gMultiplex.sThisVfs.xRandomness = multiplexRandomness;
1173   gMultiplex.sThisVfs.xSleep = multiplexSleep;
1174   gMultiplex.sThisVfs.xCurrentTime = multiplexCurrentTime;
1175   gMultiplex.sThisVfs.xGetLastError = multiplexGetLastError;
1176   gMultiplex.sThisVfs.xCurrentTimeInt64 = multiplexCurrentTimeInt64;
1177 
1178   gMultiplex.sIoMethodsV1.iVersion = 1;
1179   gMultiplex.sIoMethodsV1.xClose = multiplexClose;
1180   gMultiplex.sIoMethodsV1.xRead = multiplexRead;
1181   gMultiplex.sIoMethodsV1.xWrite = multiplexWrite;
1182   gMultiplex.sIoMethodsV1.xTruncate = multiplexTruncate;
1183   gMultiplex.sIoMethodsV1.xSync = multiplexSync;
1184   gMultiplex.sIoMethodsV1.xFileSize = multiplexFileSize;
1185   gMultiplex.sIoMethodsV1.xLock = multiplexLock;
1186   gMultiplex.sIoMethodsV1.xUnlock = multiplexUnlock;
1187   gMultiplex.sIoMethodsV1.xCheckReservedLock = multiplexCheckReservedLock;
1188   gMultiplex.sIoMethodsV1.xFileControl = multiplexFileControl;
1189   gMultiplex.sIoMethodsV1.xSectorSize = multiplexSectorSize;
1190   gMultiplex.sIoMethodsV1.xDeviceCharacteristics =
1191                                             multiplexDeviceCharacteristics;
1192   gMultiplex.sIoMethodsV2 = gMultiplex.sIoMethodsV1;
1193   gMultiplex.sIoMethodsV2.iVersion = 2;
1194   gMultiplex.sIoMethodsV2.xShmMap = multiplexShmMap;
1195   gMultiplex.sIoMethodsV2.xShmLock = multiplexShmLock;
1196   gMultiplex.sIoMethodsV2.xShmBarrier = multiplexShmBarrier;
1197   gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap;
1198   sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault);
1199 
1200   sqlite3_auto_extension((void(*)(void))multiplexFuncInit);
1201 
1202   return SQLITE_OK;
1203 }
1204 
1205 /*
1206 ** CAPI: Shutdown the multiplex system - sqlite3_multiplex_shutdown()
1207 **
1208 ** All SQLite database connections must be closed before calling this
1209 ** routine.
1210 **
1211 ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
1212 ** shutting down in order to free all remaining multiplex groups.
1213 */
1214 int sqlite3_multiplex_shutdown(int eForce){
1215   int rc = SQLITE_OK;
1216   if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
1217   if( gMultiplex.pGroups ){
1218     sqlite3_log(SQLITE_MISUSE, "sqlite3_multiplex_shutdown() called "
1219                 "while database connections are still open");
1220     if( !eForce ) return SQLITE_MISUSE;
1221     rc = SQLITE_MISUSE;
1222   }
1223   gMultiplex.isInitialized = 0;
1224   sqlite3_mutex_free(gMultiplex.pMutex);
1225   sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
1226   memset(&gMultiplex, 0, sizeof(gMultiplex));
1227   return rc;
1228 }
1229 
1230 /***************************** Test Code ***********************************/
1231 #ifdef SQLITE_TEST
1232 #if defined(INCLUDE_SQLITE_TCL_H)
1233 #  include "sqlite_tcl.h"
1234 #else
1235 #  include "tcl.h"
1236 #  ifndef SQLITE_TCLAPI
1237 #    define SQLITE_TCLAPI
1238 #  endif
1239 #endif
1240 extern const char *sqlite3ErrName(int);
1241 
1242 
1243 /*
1244 ** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT
1245 */
1246 static int SQLITE_TCLAPI test_multiplex_initialize(
1247   void * clientData,
1248   Tcl_Interp *interp,
1249   int objc,
1250   Tcl_Obj *CONST objv[]
1251 ){
1252   const char *zName;              /* Name of new multiplex VFS */
1253   int makeDefault;                /* True to make the new VFS the default */
1254   int rc;                         /* Value returned by multiplex_initialize() */
1255 
1256   UNUSED_PARAMETER(clientData);
1257 
1258   /* Process arguments */
1259   if( objc!=3 ){
1260     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
1261     return TCL_ERROR;
1262   }
1263   zName = Tcl_GetString(objv[1]);
1264   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
1265   if( zName[0]=='\0' ) zName = 0;
1266 
1267   /* Call sqlite3_multiplex_initialize() */
1268   rc = sqlite3_multiplex_initialize(zName, makeDefault);
1269   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1270 
1271   return TCL_OK;
1272 }
1273 
1274 /*
1275 ** tclcmd: sqlite3_multiplex_shutdown
1276 */
1277 static int SQLITE_TCLAPI test_multiplex_shutdown(
1278   void * clientData,
1279   Tcl_Interp *interp,
1280   int objc,
1281   Tcl_Obj *CONST objv[]
1282 ){
1283   int rc;                         /* Value returned by multiplex_shutdown() */
1284 
1285   UNUSED_PARAMETER(clientData);
1286 
1287   if( objc==2 && strcmp(Tcl_GetString(objv[1]),"-force")!=0 ){
1288     objc = 3;
1289   }
1290   if( (objc!=1 && objc!=2) ){
1291     Tcl_WrongNumArgs(interp, 1, objv, "?-force?");
1292     return TCL_ERROR;
1293   }
1294 
1295   /* Call sqlite3_multiplex_shutdown() */
1296   rc = sqlite3_multiplex_shutdown(objc==2);
1297   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1298 
1299   return TCL_OK;
1300 }
1301 
1302 /*
1303 ** tclcmd:  sqlite3_multiplex_dump
1304 */
1305 static int SQLITE_TCLAPI test_multiplex_dump(
1306   void * clientData,
1307   Tcl_Interp *interp,
1308   int objc,
1309   Tcl_Obj *CONST objv[]
1310 ){
1311   Tcl_Obj *pResult;
1312   Tcl_Obj *pGroupTerm;
1313   multiplexGroup *pGroup;
1314   int i;
1315   int nChunks = 0;
1316 
1317   UNUSED_PARAMETER(clientData);
1318   UNUSED_PARAMETER(objc);
1319   UNUSED_PARAMETER(objv);
1320 
1321   pResult = Tcl_NewObj();
1322   multiplexEnter();
1323   for(pGroup=gMultiplex.pGroups; pGroup; pGroup=pGroup->pNext){
1324     pGroupTerm = Tcl_NewObj();
1325 
1326     if( pGroup->zName ){
1327       pGroup->zName[pGroup->nName] = '\0';
1328       Tcl_ListObjAppendElement(interp, pGroupTerm,
1329           Tcl_NewStringObj(pGroup->zName, -1));
1330     }else{
1331       Tcl_ListObjAppendElement(interp, pGroupTerm, Tcl_NewObj());
1332     }
1333     Tcl_ListObjAppendElement(interp, pGroupTerm,
1334           Tcl_NewIntObj(pGroup->nName));
1335     Tcl_ListObjAppendElement(interp, pGroupTerm,
1336           Tcl_NewIntObj(pGroup->flags));
1337 
1338     /* count number of chunks with open handles */
1339     for(i=0; i<pGroup->nReal; i++){
1340       if( pGroup->aReal[i].p!=0 ) nChunks++;
1341     }
1342     Tcl_ListObjAppendElement(interp, pGroupTerm,
1343           Tcl_NewIntObj(nChunks));
1344 
1345     Tcl_ListObjAppendElement(interp, pGroupTerm,
1346           Tcl_NewIntObj(pGroup->szChunk));
1347     Tcl_ListObjAppendElement(interp, pGroupTerm,
1348           Tcl_NewIntObj(pGroup->nReal));
1349 
1350     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
1351   }
1352   multiplexLeave();
1353   Tcl_SetObjResult(interp, pResult);
1354   return TCL_OK;
1355 }
1356 
1357 /*
1358 ** Tclcmd: test_multiplex_control HANDLE DBNAME SUB-COMMAND ?INT-VALUE?
1359 */
1360 static int SQLITE_TCLAPI test_multiplex_control(
1361   ClientData cd,
1362   Tcl_Interp *interp,
1363   int objc,
1364   Tcl_Obj *CONST objv[]
1365 ){
1366   int rc;                         /* Return code from file_control() */
1367   int idx;                        /* Index in aSub[] */
1368   Tcl_CmdInfo cmdInfo;            /* Command info structure for HANDLE */
1369   sqlite3 *db;                    /* Underlying db handle for HANDLE */
1370   int iValue = 0;
1371   void *pArg = 0;
1372 
1373   struct SubCommand {
1374     const char *zName;
1375     int op;
1376     int argtype;
1377   } aSub[] = {
1378     { "enable",       MULTIPLEX_CTRL_ENABLE,           1 },
1379     { "chunk_size",   MULTIPLEX_CTRL_SET_CHUNK_SIZE,   1 },
1380     { "max_chunks",   MULTIPLEX_CTRL_SET_MAX_CHUNKS,   1 },
1381     { 0, 0, 0 }
1382   };
1383 
1384   if( objc!=5 ){
1385     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE DBNAME SUB-COMMAND INT-VALUE");
1386     return TCL_ERROR;
1387   }
1388 
1389   if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
1390     Tcl_AppendResult(interp, "expected database handle, got \"", 0);
1391     Tcl_AppendResult(interp, Tcl_GetString(objv[1]), "\"", 0);
1392     return TCL_ERROR;
1393   }else{
1394     db = *(sqlite3 **)cmdInfo.objClientData;
1395   }
1396 
1397   rc = Tcl_GetIndexFromObjStruct(
1398       interp, objv[3], aSub, sizeof(aSub[0]), "sub-command", 0, &idx
1399   );
1400   if( rc!=TCL_OK ) return rc;
1401 
1402   switch( aSub[idx].argtype ){
1403     case 1:
1404       if( Tcl_GetIntFromObj(interp, objv[4], &iValue) ){
1405         return TCL_ERROR;
1406       }
1407       pArg = (void *)&iValue;
1408       break;
1409     default:
1410       Tcl_WrongNumArgs(interp, 4, objv, "SUB-COMMAND");
1411       return TCL_ERROR;
1412   }
1413 
1414   rc = sqlite3_file_control(db, Tcl_GetString(objv[2]), aSub[idx].op, pArg);
1415   Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1416   return (rc==SQLITE_OK) ? TCL_OK : TCL_ERROR;
1417 }
1418 
1419 /*
1420 ** This routine registers the custom TCL commands defined in this
1421 ** module.  This should be the only procedure visible from outside
1422 ** of this module.
1423 */
1424 int Sqlitemultiplex_Init(Tcl_Interp *interp){
1425   static struct {
1426      char *zName;
1427      Tcl_ObjCmdProc *xProc;
1428   } aCmd[] = {
1429     { "sqlite3_multiplex_initialize", test_multiplex_initialize },
1430     { "sqlite3_multiplex_shutdown", test_multiplex_shutdown },
1431     { "sqlite3_multiplex_dump", test_multiplex_dump },
1432     { "sqlite3_multiplex_control", test_multiplex_control },
1433   };
1434   int i;
1435 
1436   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
1437     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
1438   }
1439 
1440   return TCL_OK;
1441 }
1442 #endif
1443