1
0

test_journal.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. ** 2008 Jan 22
  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 code for a VFS layer that acts as a wrapper around
  14. ** an existing VFS. The code in this file attempts to verify that SQLite
  15. ** correctly populates and syncs a journal file before writing to a
  16. ** corresponding database file.
  17. **
  18. ** INTERFACE
  19. **
  20. ** The public interface to this wrapper VFS is two functions:
  21. **
  22. ** jt_register()
  23. ** jt_unregister()
  24. **
  25. ** See header comments associated with those two functions below for
  26. ** details.
  27. **
  28. ** LIMITATIONS
  29. **
  30. ** This wrapper will not work if "PRAGMA synchronous = off" is used.
  31. **
  32. ** OPERATION
  33. **
  34. ** Starting a Transaction:
  35. **
  36. ** When a write-transaction is started, the contents of the database is
  37. ** inspected and the following data stored as part of the database file
  38. ** handle (type struct jt_file):
  39. **
  40. ** a) The page-size of the database file.
  41. ** b) The number of pages that are in the database file.
  42. ** c) The set of page numbers corresponding to free-list leaf pages.
  43. ** d) A check-sum for every page in the database file.
  44. **
  45. ** The start of a write-transaction is deemed to have occurred when a
  46. ** 28-byte journal header is written to byte offset 0 of the journal
  47. ** file.
  48. **
  49. ** Syncing the Journal File:
  50. **
  51. ** Whenever the xSync method is invoked to sync a journal-file, the
  52. ** contents of the journal file are read. For each page written to
  53. ** the journal file, a check-sum is calculated and compared to the
  54. ** check-sum calculated for the corresponding database page when the
  55. ** write-transaction was initialized. The success of the comparison
  56. ** is assert()ed. So if SQLite has written something other than the
  57. ** original content to the database file, an assert() will fail.
  58. **
  59. ** Additionally, the set of page numbers for which records exist in
  60. ** the journal file is added to (unioned with) the set of page numbers
  61. ** corresponding to free-list leaf pages collected when the
  62. ** write-transaction was initialized. This set comprises the page-numbers
  63. ** corresponding to those pages that SQLite may now safely modify.
  64. **
  65. ** Writing to the Database File:
  66. **
  67. ** When a block of data is written to a database file, the following
  68. ** invariants are asserted:
  69. **
  70. ** a) That the block of data is an aligned block of page-size bytes.
  71. **
  72. ** b) That if the page being written did not exist when the
  73. ** transaction was started (i.e. the database file is growing), then
  74. ** the journal-file must have been synced at least once since
  75. ** the start of the transaction.
  76. **
  77. ** c) That if the page being written did exist when the transaction
  78. ** was started, then the page must have either been a free-list
  79. ** leaf page at the start of the transaction, or else must have
  80. ** been stored in the journal file prior to the most recent sync.
  81. **
  82. ** Closing a Transaction:
  83. **
  84. ** When a transaction is closed, all data collected at the start of
  85. ** the transaction, or following an xSync of a journal-file, is
  86. ** discarded. The end of a transaction is recognized when any one
  87. ** of the following occur:
  88. **
  89. ** a) A block of zeroes (or anything else that is not a valid
  90. ** journal-header) is written to the start of the journal file.
  91. **
  92. ** b) A journal file is truncated to zero bytes in size using xTruncate.
  93. **
  94. ** c) The journal file is deleted using xDelete.
  95. */
  96. #if SQLITE_TEST /* This file is used for testing only */
  97. #include "sqlite3.h"
  98. #include "sqliteInt.h"
  99. /*
  100. ** Maximum pathname length supported by the jt backend.
  101. */
  102. #define JT_MAX_PATHNAME 512
  103. /*
  104. ** Name used to identify this VFS.
  105. */
  106. #define JT_VFS_NAME "jt"
  107. typedef struct jt_file jt_file;
  108. struct jt_file {
  109. sqlite3_file base;
  110. const char *zName; /* Name of open file */
  111. int flags; /* Flags the file was opened with */
  112. /* The following are only used by database file file handles */
  113. int eLock; /* Current lock held on the file */
  114. u32 nPage; /* Size of file in pages when transaction started */
  115. u32 nPagesize; /* Page size when transaction started */
  116. Bitvec *pWritable; /* Bitvec of pages that may be written to the file */
  117. u32 *aCksum; /* Checksum for first nPage pages */
  118. int nSync; /* Number of times journal file has been synced */
  119. /* Only used by journal file-handles */
  120. sqlite3_int64 iMaxOff; /* Maximum offset written to this transaction */
  121. jt_file *pNext; /* All files are stored in a linked list */
  122. sqlite3_file *pReal; /* The file handle for the underlying vfs */
  123. };
  124. /*
  125. ** Method declarations for jt_file.
  126. */
  127. static int jtClose(sqlite3_file*);
  128. static int jtRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  129. static int jtWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
  130. static int jtTruncate(sqlite3_file*, sqlite3_int64 size);
  131. static int jtSync(sqlite3_file*, int flags);
  132. static int jtFileSize(sqlite3_file*, sqlite3_int64 *pSize);
  133. static int jtLock(sqlite3_file*, int);
  134. static int jtUnlock(sqlite3_file*, int);
  135. static int jtCheckReservedLock(sqlite3_file*, int *);
  136. static int jtFileControl(sqlite3_file*, int op, void *pArg);
  137. static int jtSectorSize(sqlite3_file*);
  138. static int jtDeviceCharacteristics(sqlite3_file*);
  139. /*
  140. ** Method declarations for jt_vfs.
  141. */
  142. static int jtOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
  143. static int jtDelete(sqlite3_vfs*, const char *zName, int syncDir);
  144. static int jtAccess(sqlite3_vfs*, const char *zName, int flags, int *);
  145. static int jtFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
  146. static void *jtDlOpen(sqlite3_vfs*, const char *zFilename);
  147. static void jtDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
  148. static void (*jtDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void);
  149. static void jtDlClose(sqlite3_vfs*, void*);
  150. static int jtRandomness(sqlite3_vfs*, int nByte, char *zOut);
  151. static int jtSleep(sqlite3_vfs*, int microseconds);
  152. static int jtCurrentTime(sqlite3_vfs*, double*);
  153. static int jtCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
  154. static sqlite3_vfs jt_vfs = {
  155. 2, /* iVersion */
  156. sizeof(jt_file), /* szOsFile */
  157. JT_MAX_PATHNAME, /* mxPathname */
  158. 0, /* pNext */
  159. JT_VFS_NAME, /* zName */
  160. 0, /* pAppData */
  161. jtOpen, /* xOpen */
  162. jtDelete, /* xDelete */
  163. jtAccess, /* xAccess */
  164. jtFullPathname, /* xFullPathname */
  165. jtDlOpen, /* xDlOpen */
  166. jtDlError, /* xDlError */
  167. jtDlSym, /* xDlSym */
  168. jtDlClose, /* xDlClose */
  169. jtRandomness, /* xRandomness */
  170. jtSleep, /* xSleep */
  171. jtCurrentTime, /* xCurrentTime */
  172. 0, /* xGetLastError */
  173. jtCurrentTimeInt64 /* xCurrentTimeInt64 */
  174. };
  175. static sqlite3_io_methods jt_io_methods = {
  176. 1, /* iVersion */
  177. jtClose, /* xClose */
  178. jtRead, /* xRead */
  179. jtWrite, /* xWrite */
  180. jtTruncate, /* xTruncate */
  181. jtSync, /* xSync */
  182. jtFileSize, /* xFileSize */
  183. jtLock, /* xLock */
  184. jtUnlock, /* xUnlock */
  185. jtCheckReservedLock, /* xCheckReservedLock */
  186. jtFileControl, /* xFileControl */
  187. jtSectorSize, /* xSectorSize */
  188. jtDeviceCharacteristics /* xDeviceCharacteristics */
  189. };
  190. struct JtGlobal {
  191. sqlite3_vfs *pVfs; /* Parent VFS */
  192. jt_file *pList; /* List of all open files */
  193. };
  194. static struct JtGlobal g = {0, 0};
  195. /*
  196. ** Functions to obtain and relinquish a mutex to protect g.pList. The
  197. ** STATIC_PRNG mutex is reused, purely for the sake of convenience.
  198. */
  199. static void enterJtMutex(void){
  200. sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG));
  201. }
  202. static void leaveJtMutex(void){
  203. sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG));
  204. }
  205. extern int sqlite3_io_error_pending;
  206. extern int sqlite3_io_error_hit;
  207. static void stop_ioerr_simulation(int *piSave, int *piSave2){
  208. *piSave = sqlite3_io_error_pending;
  209. *piSave2 = sqlite3_io_error_hit;
  210. sqlite3_io_error_pending = -1;
  211. sqlite3_io_error_hit = 0;
  212. }
  213. static void start_ioerr_simulation(int iSave, int iSave2){
  214. sqlite3_io_error_pending = iSave;
  215. sqlite3_io_error_hit = iSave2;
  216. }
  217. /*
  218. ** The jt_file pointed to by the argument may or may not be a file-handle
  219. ** open on a main database file. If it is, and a transaction is currently
  220. ** opened on the file, then discard all transaction related data.
  221. */
  222. static void closeTransaction(jt_file *p){
  223. sqlite3BitvecDestroy(p->pWritable);
  224. sqlite3_free(p->aCksum);
  225. p->pWritable = 0;
  226. p->aCksum = 0;
  227. p->nSync = 0;
  228. }
  229. /*
  230. ** Close an jt-file.
  231. */
  232. static int jtClose(sqlite3_file *pFile){
  233. jt_file **pp;
  234. jt_file *p = (jt_file *)pFile;
  235. closeTransaction(p);
  236. enterJtMutex();
  237. if( p->zName ){
  238. for(pp=&g.pList; *pp!=p; pp=&(*pp)->pNext);
  239. *pp = p->pNext;
  240. }
  241. leaveJtMutex();
  242. return sqlite3OsClose(p->pReal);
  243. }
  244. /*
  245. ** Read data from an jt-file.
  246. */
  247. static int jtRead(
  248. sqlite3_file *pFile,
  249. void *zBuf,
  250. int iAmt,
  251. sqlite_int64 iOfst
  252. ){
  253. jt_file *p = (jt_file *)pFile;
  254. return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
  255. }
  256. /*
  257. ** Parameter zJournal is the name of a journal file that is currently
  258. ** open. This function locates and returns the handle opened on the
  259. ** corresponding database file by the pager that currently has the
  260. ** journal file opened. This file-handle is identified by the
  261. ** following properties:
  262. **
  263. ** a) SQLITE_OPEN_MAIN_DB was specified when the file was opened.
  264. **
  265. ** b) The file-name specified when the file was opened matches
  266. ** all but the final 8 characters of the journal file name.
  267. **
  268. ** c) There is currently a reserved lock on the file.
  269. **/
  270. static jt_file *locateDatabaseHandle(const char *zJournal){
  271. jt_file *pMain = 0;
  272. enterJtMutex();
  273. for(pMain=g.pList; pMain; pMain=pMain->pNext){
  274. int nName = (int)(strlen(zJournal) - strlen("-journal"));
  275. if( (pMain->flags&SQLITE_OPEN_MAIN_DB)
  276. && ((int)strlen(pMain->zName)==nName)
  277. && 0==memcmp(pMain->zName, zJournal, nName)
  278. && (pMain->eLock>=SQLITE_LOCK_RESERVED)
  279. ){
  280. break;
  281. }
  282. }
  283. leaveJtMutex();
  284. return pMain;
  285. }
  286. /*
  287. ** Parameter z points to a buffer of 4 bytes in size containing a
  288. ** unsigned 32-bit integer stored in big-endian format. Decode the
  289. ** integer and return its value.
  290. */
  291. static u32 decodeUint32(const unsigned char *z){
  292. return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3];
  293. }
  294. /*
  295. ** Calculate a checksum from the buffer of length n bytes pointed to
  296. ** by parameter z.
  297. */
  298. static u32 genCksum(const unsigned char *z, int n){
  299. int i;
  300. u32 cksum = 0;
  301. for(i=0; i<n; i++){
  302. cksum = cksum + z[i] + (cksum<<3);
  303. }
  304. return cksum;
  305. }
  306. /*
  307. ** The first argument, zBuf, points to a buffer containing a 28 byte
  308. ** serialized journal header. This function deserializes four of the
  309. ** integer fields contained in the journal header and writes their
  310. ** values to the output variables.
  311. **
  312. ** SQLITE_OK is returned if the journal-header is successfully
  313. ** decoded. Otherwise, SQLITE_ERROR.
  314. */
  315. static int decodeJournalHdr(
  316. const unsigned char *zBuf, /* Input: 28 byte journal header */
  317. u32 *pnRec, /* Out: Number of journalled records */
  318. u32 *pnPage, /* Out: Original database page count */
  319. u32 *pnSector, /* Out: Sector size in bytes */
  320. u32 *pnPagesize /* Out: Page size in bytes */
  321. ){
  322. unsigned char aMagic[] = { 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7 };
  323. if( memcmp(aMagic, zBuf, 8) ) return SQLITE_ERROR;
  324. if( pnRec ) *pnRec = decodeUint32(&zBuf[8]);
  325. if( pnPage ) *pnPage = decodeUint32(&zBuf[16]);
  326. if( pnSector ) *pnSector = decodeUint32(&zBuf[20]);
  327. if( pnPagesize ) *pnPagesize = decodeUint32(&zBuf[24]);
  328. return SQLITE_OK;
  329. }
  330. /*
  331. ** This function is called when a new transaction is opened, just after
  332. ** the first journal-header is written to the journal file.
  333. */
  334. static int openTransaction(jt_file *pMain, jt_file *pJournal){
  335. unsigned char *aData;
  336. sqlite3_file *p = pMain->pReal;
  337. int rc = SQLITE_OK;
  338. closeTransaction(pMain);
  339. aData = sqlite3_malloc(pMain->nPagesize);
  340. pMain->pWritable = sqlite3BitvecCreate(pMain->nPage);
  341. pMain->aCksum = sqlite3_malloc(sizeof(u32) * (pMain->nPage + 1));
  342. pJournal->iMaxOff = 0;
  343. if( !pMain->pWritable || !pMain->aCksum || !aData ){
  344. rc = SQLITE_IOERR_NOMEM;
  345. }else if( pMain->nPage>0 ){
  346. u32 iTrunk;
  347. int iSave;
  348. int iSave2;
  349. stop_ioerr_simulation(&iSave, &iSave2);
  350. /* Read the database free-list. Add the page-number for each free-list
  351. ** leaf to the jt_file.pWritable bitvec.
  352. */
  353. rc = sqlite3OsRead(p, aData, pMain->nPagesize, 0);
  354. if( rc==SQLITE_OK ){
  355. u32 nDbsize = decodeUint32(&aData[28]);
  356. if( nDbsize>0 && memcmp(&aData[24], &aData[92], 4)==0 ){
  357. u32 iPg;
  358. for(iPg=nDbsize+1; iPg<=pMain->nPage; iPg++){
  359. sqlite3BitvecSet(pMain->pWritable, iPg);
  360. }
  361. }
  362. }
  363. iTrunk = decodeUint32(&aData[32]);
  364. while( rc==SQLITE_OK && iTrunk>0 ){
  365. u32 nLeaf;
  366. u32 iLeaf;
  367. sqlite3_int64 iOff = (i64)(iTrunk-1)*pMain->nPagesize;
  368. rc = sqlite3OsRead(p, aData, pMain->nPagesize, iOff);
  369. nLeaf = decodeUint32(&aData[4]);
  370. for(iLeaf=0; rc==SQLITE_OK && iLeaf<nLeaf; iLeaf++){
  371. u32 pgno = decodeUint32(&aData[8+4*iLeaf]);
  372. sqlite3BitvecSet(pMain->pWritable, pgno);
  373. }
  374. iTrunk = decodeUint32(aData);
  375. }
  376. /* Calculate and store a checksum for each page in the database file. */
  377. if( rc==SQLITE_OK ){
  378. int ii;
  379. for(ii=0; rc==SQLITE_OK && ii<(int)pMain->nPage; ii++){
  380. i64 iOff = (i64)(pMain->nPagesize) * (i64)ii;
  381. if( iOff==PENDING_BYTE ) continue;
  382. rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff);
  383. pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize);
  384. if( ii+1==pMain->nPage && rc==SQLITE_IOERR_SHORT_READ ) rc = SQLITE_OK;
  385. }
  386. }
  387. start_ioerr_simulation(iSave, iSave2);
  388. }
  389. sqlite3_free(aData);
  390. return rc;
  391. }
  392. /*
  393. ** The first argument to this function is a handle open on a journal file.
  394. ** This function reads the journal file and adds the page number for each
  395. ** page in the journal to the Bitvec object passed as the second argument.
  396. */
  397. static int readJournalFile(jt_file *p, jt_file *pMain){
  398. int rc = SQLITE_OK;
  399. unsigned char zBuf[28];
  400. sqlite3_file *pReal = p->pReal;
  401. sqlite3_int64 iOff = 0;
  402. sqlite3_int64 iSize = p->iMaxOff;
  403. unsigned char *aPage;
  404. int iSave;
  405. int iSave2;
  406. aPage = sqlite3_malloc(pMain->nPagesize);
  407. if( !aPage ){
  408. return SQLITE_IOERR_NOMEM;
  409. }
  410. stop_ioerr_simulation(&iSave, &iSave2);
  411. while( rc==SQLITE_OK && iOff<iSize ){
  412. u32 nRec, nPage, nSector, nPagesize;
  413. u32 ii;
  414. /* Read and decode the next journal-header from the journal file. */
  415. rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
  416. if( rc!=SQLITE_OK
  417. || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize)
  418. ){
  419. goto finish_rjf;
  420. }
  421. iOff += nSector;
  422. if( nRec==0 ){
  423. /* A trick. There might be another journal-header immediately
  424. ** following this one. In this case, 0 records means 0 records,
  425. ** not "read until the end of the file". See also ticket #2565.
  426. */
  427. if( iSize>=(iOff+nSector) ){
  428. rc = sqlite3OsRead(pReal, zBuf, 28, iOff);
  429. if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){
  430. continue;
  431. }
  432. }
  433. nRec = (u32)((iSize-iOff) / (pMain->nPagesize+8));
  434. }
  435. /* Read all the records that follow the journal-header just read. */
  436. for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){
  437. u32 pgno;
  438. rc = sqlite3OsRead(pReal, zBuf, 4, iOff);
  439. if( rc==SQLITE_OK ){
  440. pgno = decodeUint32(zBuf);
  441. if( pgno>0 && pgno<=pMain->nPage ){
  442. if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){
  443. rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4);
  444. if( rc==SQLITE_OK ){
  445. u32 cksum = genCksum(aPage, pMain->nPagesize);
  446. assert( cksum==pMain->aCksum[pgno-1] );
  447. }
  448. }
  449. sqlite3BitvecSet(pMain->pWritable, pgno);
  450. }
  451. iOff += (8 + pMain->nPagesize);
  452. }
  453. }
  454. iOff = ((iOff + (nSector-1)) / nSector) * nSector;
  455. }
  456. finish_rjf:
  457. start_ioerr_simulation(iSave, iSave2);
  458. sqlite3_free(aPage);
  459. if( rc==SQLITE_IOERR_SHORT_READ ){
  460. rc = SQLITE_OK;
  461. }
  462. return rc;
  463. }
  464. /*
  465. ** Write data to an jt-file.
  466. */
  467. static int jtWrite(
  468. sqlite3_file *pFile,
  469. const void *zBuf,
  470. int iAmt,
  471. sqlite_int64 iOfst
  472. ){
  473. int rc;
  474. jt_file *p = (jt_file *)pFile;
  475. if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){
  476. if( iOfst==0 ){
  477. jt_file *pMain = locateDatabaseHandle(p->zName);
  478. assert( pMain );
  479. if( iAmt==28 ){
  480. /* Zeroing the first journal-file header. This is the end of a
  481. ** transaction. */
  482. closeTransaction(pMain);
  483. }else if( iAmt!=12 ){
  484. /* Writing the first journal header to a journal file. This happens
  485. ** when a transaction is first started. */
  486. u8 *z = (u8 *)zBuf;
  487. pMain->nPage = decodeUint32(&z[16]);
  488. pMain->nPagesize = decodeUint32(&z[24]);
  489. if( SQLITE_OK!=(rc=openTransaction(pMain, p)) ){
  490. return rc;
  491. }
  492. }
  493. }
  494. if( p->iMaxOff<(iOfst + iAmt) ){
  495. p->iMaxOff = iOfst + iAmt;
  496. }
  497. }
  498. if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){
  499. if( iAmt<(int)p->nPagesize
  500. && p->nPagesize%iAmt==0
  501. && iOfst>=(PENDING_BYTE+512)
  502. && iOfst+iAmt<=PENDING_BYTE+p->nPagesize
  503. ){
  504. /* No-op. This special case is hit when the backup code is copying a
  505. ** to a database with a larger page-size than the source database and
  506. ** it needs to fill in the non-locking-region part of the original
  507. ** pending-byte page.
  508. */
  509. }else{
  510. u32 pgno = (u32)(iOfst/p->nPagesize + 1);
  511. assert( (iAmt==1||iAmt==p->nPagesize) && ((iOfst+iAmt)%p->nPagesize)==0 );
  512. assert( pgno<=p->nPage || p->nSync>0 );
  513. assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) );
  514. }
  515. }
  516. rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
  517. if( (p->flags&SQLITE_OPEN_MAIN_JOURNAL) && iAmt==12 ){
  518. jt_file *pMain = locateDatabaseHandle(p->zName);
  519. int rc2 = readJournalFile(p, pMain);
  520. if( rc==SQLITE_OK ) rc = rc2;
  521. }
  522. return rc;
  523. }
  524. /*
  525. ** Truncate an jt-file.
  526. */
  527. static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){
  528. jt_file *p = (jt_file *)pFile;
  529. if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){
  530. /* Truncating a journal file. This is the end of a transaction. */
  531. jt_file *pMain = locateDatabaseHandle(p->zName);
  532. closeTransaction(pMain);
  533. }
  534. if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){
  535. u32 pgno;
  536. u32 locking_page = (u32)(PENDING_BYTE/p->nPagesize+1);
  537. for(pgno=(u32)(size/p->nPagesize+1); pgno<=p->nPage; pgno++){
  538. assert( pgno==locking_page || sqlite3BitvecTest(p->pWritable, pgno) );
  539. }
  540. }
  541. return sqlite3OsTruncate(p->pReal, size);
  542. }
  543. /*
  544. ** Sync an jt-file.
  545. */
  546. static int jtSync(sqlite3_file *pFile, int flags){
  547. jt_file *p = (jt_file *)pFile;
  548. if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){
  549. int rc;
  550. jt_file *pMain; /* The associated database file */
  551. /* The journal file is being synced. At this point, we inspect the
  552. ** contents of the file up to this point and set each bit in the
  553. ** jt_file.pWritable bitvec of the main database file associated with
  554. ** this journal file.
  555. */
  556. pMain = locateDatabaseHandle(p->zName);
  557. assert(pMain);
  558. /* Set the bitvec values */
  559. if( pMain->pWritable ){
  560. pMain->nSync++;
  561. rc = readJournalFile(p, pMain);
  562. if( rc!=SQLITE_OK ){
  563. return rc;
  564. }
  565. }
  566. }
  567. return sqlite3OsSync(p->pReal, flags);
  568. }
  569. /*
  570. ** Return the current file-size of an jt-file.
  571. */
  572. static int jtFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
  573. jt_file *p = (jt_file *)pFile;
  574. return sqlite3OsFileSize(p->pReal, pSize);
  575. }
  576. /*
  577. ** Lock an jt-file.
  578. */
  579. static int jtLock(sqlite3_file *pFile, int eLock){
  580. int rc;
  581. jt_file *p = (jt_file *)pFile;
  582. rc = sqlite3OsLock(p->pReal, eLock);
  583. if( rc==SQLITE_OK && eLock>p->eLock ){
  584. p->eLock = eLock;
  585. }
  586. return rc;
  587. }
  588. /*
  589. ** Unlock an jt-file.
  590. */
  591. static int jtUnlock(sqlite3_file *pFile, int eLock){
  592. int rc;
  593. jt_file *p = (jt_file *)pFile;
  594. rc = sqlite3OsUnlock(p->pReal, eLock);
  595. if( rc==SQLITE_OK && eLock<p->eLock ){
  596. p->eLock = eLock;
  597. }
  598. return rc;
  599. }
  600. /*
  601. ** Check if another file-handle holds a RESERVED lock on an jt-file.
  602. */
  603. static int jtCheckReservedLock(sqlite3_file *pFile, int *pResOut){
  604. jt_file *p = (jt_file *)pFile;
  605. return sqlite3OsCheckReservedLock(p->pReal, pResOut);
  606. }
  607. /*
  608. ** File control method. For custom operations on an jt-file.
  609. */
  610. static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){
  611. jt_file *p = (jt_file *)pFile;
  612. return p->pReal->pMethods->xFileControl(p->pReal, op, pArg);
  613. }
  614. /*
  615. ** Return the sector-size in bytes for an jt-file.
  616. */
  617. static int jtSectorSize(sqlite3_file *pFile){
  618. jt_file *p = (jt_file *)pFile;
  619. return sqlite3OsSectorSize(p->pReal);
  620. }
  621. /*
  622. ** Return the device characteristic flags supported by an jt-file.
  623. */
  624. static int jtDeviceCharacteristics(sqlite3_file *pFile){
  625. jt_file *p = (jt_file *)pFile;
  626. return sqlite3OsDeviceCharacteristics(p->pReal);
  627. }
  628. /*
  629. ** Open an jt file handle.
  630. */
  631. static int jtOpen(
  632. sqlite3_vfs *pVfs,
  633. const char *zName,
  634. sqlite3_file *pFile,
  635. int flags,
  636. int *pOutFlags
  637. ){
  638. int rc;
  639. jt_file *p = (jt_file *)pFile;
  640. pFile->pMethods = 0;
  641. p->pReal = (sqlite3_file *)&p[1];
  642. p->pReal->pMethods = 0;
  643. rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags);
  644. assert( rc==SQLITE_OK || p->pReal->pMethods==0 );
  645. if( rc==SQLITE_OK ){
  646. pFile->pMethods = &jt_io_methods;
  647. p->eLock = 0;
  648. p->zName = zName;
  649. p->flags = flags;
  650. p->pNext = 0;
  651. p->pWritable = 0;
  652. p->aCksum = 0;
  653. enterJtMutex();
  654. if( zName ){
  655. p->pNext = g.pList;
  656. g.pList = p;
  657. }
  658. leaveJtMutex();
  659. }
  660. return rc;
  661. }
  662. /*
  663. ** Delete the file located at zPath. If the dirSync argument is true,
  664. ** ensure the file-system modifications are synced to disk before
  665. ** returning.
  666. */
  667. static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  668. int nPath = (int)strlen(zPath);
  669. if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){
  670. /* Deleting a journal file. The end of a transaction. */
  671. jt_file *pMain = locateDatabaseHandle(zPath);
  672. if( pMain ){
  673. closeTransaction(pMain);
  674. }
  675. }
  676. return sqlite3OsDelete(g.pVfs, zPath, dirSync);
  677. }
  678. /*
  679. ** Test for access permissions. Return true if the requested permission
  680. ** is available, or false otherwise.
  681. */
  682. static int jtAccess(
  683. sqlite3_vfs *pVfs,
  684. const char *zPath,
  685. int flags,
  686. int *pResOut
  687. ){
  688. return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut);
  689. }
  690. /*
  691. ** Populate buffer zOut with the full canonical pathname corresponding
  692. ** to the pathname in zPath. zOut is guaranteed to point to a buffer
  693. ** of at least (JT_MAX_PATHNAME+1) bytes.
  694. */
  695. static int jtFullPathname(
  696. sqlite3_vfs *pVfs,
  697. const char *zPath,
  698. int nOut,
  699. char *zOut
  700. ){
  701. return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut);
  702. }
  703. /*
  704. ** Open the dynamic library located at zPath and return a handle.
  705. */
  706. static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  707. return g.pVfs->xDlOpen(g.pVfs, zPath);
  708. }
  709. /*
  710. ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
  711. ** utf-8 string describing the most recent error encountered associated
  712. ** with dynamic libraries.
  713. */
  714. static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
  715. g.pVfs->xDlError(g.pVfs, nByte, zErrMsg);
  716. }
  717. /*
  718. ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
  719. */
  720. static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
  721. return g.pVfs->xDlSym(g.pVfs, p, zSym);
  722. }
  723. /*
  724. ** Close the dynamic library handle pHandle.
  725. */
  726. static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){
  727. g.pVfs->xDlClose(g.pVfs, pHandle);
  728. }
  729. /*
  730. ** Populate the buffer pointed to by zBufOut with nByte bytes of
  731. ** random data.
  732. */
  733. static int jtRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  734. return sqlite3OsRandomness(g.pVfs, nByte, zBufOut);
  735. }
  736. /*
  737. ** Sleep for nMicro microseconds. Return the number of microseconds
  738. ** actually slept.
  739. */
  740. static int jtSleep(sqlite3_vfs *pVfs, int nMicro){
  741. return sqlite3OsSleep(g.pVfs, nMicro);
  742. }
  743. /*
  744. ** Return the current time as a Julian Day number in *pTimeOut.
  745. */
  746. static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  747. return g.pVfs->xCurrentTime(g.pVfs, pTimeOut);
  748. }
  749. /*
  750. ** Return the current time as a Julian Day number in *pTimeOut.
  751. */
  752. static int jtCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
  753. return g.pVfs->xCurrentTimeInt64(g.pVfs, pTimeOut);
  754. }
  755. /**************************************************************************
  756. ** Start of public API.
  757. */
  758. /*
  759. ** Configure the jt VFS as a wrapper around the VFS named by parameter
  760. ** zWrap. If the isDefault parameter is true, then the jt VFS is installed
  761. ** as the new default VFS for SQLite connections. If isDefault is not
  762. ** true, then the jt VFS is installed as non-default. In this case it
  763. ** is available via its name, "jt".
  764. */
  765. int jt_register(char *zWrap, int isDefault){
  766. g.pVfs = sqlite3_vfs_find(zWrap);
  767. if( g.pVfs==0 ){
  768. return SQLITE_ERROR;
  769. }
  770. jt_vfs.szOsFile = sizeof(jt_file) + g.pVfs->szOsFile;
  771. if( g.pVfs->iVersion==1 ){
  772. jt_vfs.iVersion = 1;
  773. }else if( g.pVfs->xCurrentTimeInt64==0 ){
  774. jt_vfs.xCurrentTimeInt64 = 0;
  775. }
  776. sqlite3_vfs_register(&jt_vfs, isDefault);
  777. return SQLITE_OK;
  778. }
  779. /*
  780. ** Uninstall the jt VFS, if it is installed.
  781. */
  782. void jt_unregister(void){
  783. sqlite3_vfs_unregister(&jt_vfs);
  784. }
  785. #endif