test_multiplex.c 45 KB

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