test_multiplex.c 46 KB

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