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