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.13 2009/03/26 11:49:11 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 extern int sqlite3_io_error_pending; 210 static void stop_ioerr_simulation(int *piSave){ 211 *piSave = sqlite3_io_error_pending; 212 sqlite3_io_error_pending = -1; 213 } 214 static void start_ioerr_simulation(int iSave){ 215 sqlite3_io_error_pending = iSave; 216 } 217 218 /* 219 ** The jt_file pointed to by the argument may or may not be a file-handle 220 ** open on a main database file. If it is, and a transaction is currently 221 ** opened on the file, then discard all transaction related data. 222 */ 223 static void closeTransaction(jt_file *p){ 224 sqlite3BitvecDestroy(p->pWritable); 225 sqlite3_free(p->aCksum); 226 p->pWritable = 0; 227 p->aCksum = 0; 228 p->nSync = 0; 229 } 230 231 /* 232 ** Close an jt-file. 233 */ 234 static int jtClose(sqlite3_file *pFile){ 235 jt_file **pp; 236 jt_file *p = (jt_file *)pFile; 237 238 closeTransaction(p); 239 if( p->zName ){ 240 for(pp=&g.pList; *pp!=p; pp=&(*pp)->pNext); 241 *pp = p->pNext; 242 } 243 244 return sqlite3OsClose(p->pReal); 245 } 246 247 /* 248 ** Read data from an jt-file. 249 */ 250 static int jtRead( 251 sqlite3_file *pFile, 252 void *zBuf, 253 int iAmt, 254 sqlite_int64 iOfst 255 ){ 256 jt_file *p = (jt_file *)pFile; 257 return sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst); 258 } 259 260 261 /* 262 ** Parameter zJournal is the name of a journal file that is currently 263 ** open. This function locates and returns the handle opened on the 264 ** corresponding database file by the pager that currently has the 265 ** journal file opened. This file-handle is identified by the 266 ** following properties: 267 ** 268 ** a) SQLITE_OPEN_MAIN_DB was specified when the file was opened. 269 ** 270 ** b) The file-name specified when the file was opened matches 271 ** all but the final 8 characters of the journal file name. 272 ** 273 ** c) There is currently a reserved lock on the file. 274 **/ 275 static jt_file *locateDatabaseHandle(const char *zJournal){ 276 jt_file *pMain = 0; 277 for(pMain=g.pList; pMain; pMain=pMain->pNext){ 278 int nName = strlen(zJournal) - strlen("-journal"); 279 if( (pMain->flags&SQLITE_OPEN_MAIN_DB) 280 && (strlen(pMain->zName)==nName) 281 && 0==memcmp(pMain->zName, zJournal, nName) 282 && (pMain->eLock>=SQLITE_LOCK_RESERVED) 283 ){ 284 break; 285 } 286 } 287 return pMain; 288 } 289 290 /* 291 ** Parameter z points to a buffer of 4 bytes in size containing a 292 ** unsigned 32-bit integer stored in big-endian format. Decode the 293 ** integer and return its value. 294 */ 295 static u32 decodeUint32(const unsigned char *z){ 296 return (z[0]<<24) + (z[1]<<16) + (z[2]<<8) + z[3]; 297 } 298 299 /* 300 ** Calculate a checksum from the buffer of length n bytes pointed to 301 ** by parameter z. 302 */ 303 static u32 genCksum(const unsigned char *z, int n){ 304 int i; 305 u32 cksum = 0; 306 for(i=0; i<n; i++){ 307 cksum = cksum + z[i] + (cksum<<3); 308 } 309 return cksum; 310 } 311 312 /* 313 ** The first argument, zBuf, points to a buffer containing a 28 byte 314 ** serialized journal header. This function deserializes four of the 315 ** integer fields contained in the journal header and writes their 316 ** values to the output variables. 317 ** 318 ** SQLITE_OK is returned if the journal-header is successfully 319 ** decoded. Otherwise, SQLITE_ERROR. 320 */ 321 static int decodeJournalHdr( 322 const unsigned char *zBuf, /* Input: 28 byte journal header */ 323 u32 *pnRec, /* Out: Number of journalled records */ 324 u32 *pnPage, /* Out: Original database page count */ 325 u32 *pnSector, /* Out: Sector size in bytes */ 326 u32 *pnPagesize /* Out: Page size in bytes */ 327 ){ 328 unsigned char aMagic[] = { 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7 }; 329 if( memcmp(aMagic, zBuf, 8) ) return SQLITE_ERROR; 330 if( pnRec ) *pnRec = decodeUint32(&zBuf[8]); 331 if( pnPage ) *pnPage = decodeUint32(&zBuf[16]); 332 if( pnSector ) *pnSector = decodeUint32(&zBuf[20]); 333 if( pnPagesize ) *pnPagesize = decodeUint32(&zBuf[24]); 334 return SQLITE_OK; 335 } 336 337 /* 338 ** This function is called when a new transaction is opened, just after 339 ** the first journal-header is written to the journal file. 340 */ 341 static int openTransaction(jt_file *pMain, jt_file *pJournal){ 342 unsigned char *aData; 343 sqlite3_file *p = pMain->pReal; 344 int rc = SQLITE_OK; 345 346 aData = sqlite3_malloc(pMain->nPagesize); 347 pMain->pWritable = sqlite3BitvecCreate(pMain->nPage); 348 pMain->aCksum = sqlite3_malloc(sizeof(u32) * (pMain->nPage + 1)); 349 pJournal->iMaxOff = 0; 350 351 if( !pMain->pWritable || !pMain->aCksum || !aData ){ 352 rc = SQLITE_IOERR_NOMEM; 353 }else if( pMain->nPage>0 ){ 354 u32 iTrunk; 355 int iSave; 356 357 stop_ioerr_simulation(&iSave); 358 359 /* Read the database free-list. Add the page-number for each free-list 360 ** leaf to the jt_file.pWritable bitvec. 361 */ 362 rc = sqlite3OsRead(p, aData, pMain->nPagesize, 0); 363 iTrunk = decodeUint32(&aData[32]); 364 while( rc==SQLITE_OK && iTrunk>0 ){ 365 u32 nLeaf; 366 u32 iLeaf; 367 sqlite3_int64 iOff = (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 377 /* Calculate and store a checksum for each page in the database file. */ 378 if( rc==SQLITE_OK ){ 379 int ii; 380 for(ii=0; rc==SQLITE_OK && ii<pMain->nPage; ii++){ 381 i64 iOff = (i64)(pMain->nPagesize) * (i64)ii; 382 if( iOff==PENDING_BYTE ) continue; 383 rc = sqlite3OsRead(pMain->pReal, aData, pMain->nPagesize, iOff); 384 pMain->aCksum[ii] = genCksum(aData, pMain->nPagesize); 385 } 386 } 387 388 start_ioerr_simulation(iSave); 389 } 390 391 sqlite3_free(aData); 392 return rc; 393 } 394 395 /* 396 ** Write data to an jt-file. 397 */ 398 static int jtWrite( 399 sqlite3_file *pFile, 400 const void *zBuf, 401 int iAmt, 402 sqlite_int64 iOfst 403 ){ 404 jt_file *p = (jt_file *)pFile; 405 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ 406 if( iOfst==0 ){ 407 jt_file *pMain = locateDatabaseHandle(p->zName); 408 assert( pMain ); 409 410 if( decodeJournalHdr(zBuf, 0, &pMain->nPage, 0, &pMain->nPagesize) ){ 411 /* Zeroing the first journal-file header. This is the end of a 412 ** transaction. */ 413 closeTransaction(pMain); 414 }else{ 415 /* Writing the first journal header to a journal file. This happens 416 ** when a transaction is first started. */ 417 int rc; 418 if( SQLITE_OK!=(rc=openTransaction(pMain, p)) ){ 419 return rc; 420 } 421 } 422 } 423 if( p->iMaxOff<(iOfst + iAmt) ){ 424 p->iMaxOff = iOfst + iAmt; 425 } 426 } 427 428 if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ 429 if( iAmt<p->nPagesize 430 && p->nPagesize%iAmt==0 431 && iOfst>=(PENDING_BYTE+512) 432 && iOfst+iAmt<=PENDING_BYTE+p->nPagesize 433 ){ 434 /* No-op. This special case is hit when the backup code is copying a 435 ** to a database with a larger page-size than the source database and 436 ** it needs to fill in the non-locking-region part of the original 437 ** pending-byte page. 438 */ 439 }else{ 440 u32 pgno = iOfst/p->nPagesize + 1; 441 assert( (iAmt==1||iAmt==p->nPagesize) && ((iOfst+iAmt)%p->nPagesize)==0 ); 442 assert( pgno<=p->nPage || p->nSync>0 ); 443 assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) ); 444 } 445 } 446 447 return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); 448 } 449 450 /* 451 ** Truncate an jt-file. 452 */ 453 static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){ 454 jt_file *p = (jt_file *)pFile; 455 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){ 456 /* Truncating a journal file. This is the end of a transaction. */ 457 jt_file *pMain = locateDatabaseHandle(p->zName); 458 closeTransaction(pMain); 459 } 460 if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ 461 u32 pgno; 462 u32 locking_page = (u32)(PENDING_BYTE/p->nPagesize+1); 463 for(pgno=size/p->nPagesize+1; pgno<=p->nPage; pgno++){ 464 assert( pgno==locking_page || sqlite3BitvecTest(p->pWritable, pgno) ); 465 } 466 } 467 return sqlite3OsTruncate(p->pReal, size); 468 } 469 470 /* 471 ** The first argument to this function is a handle open on a journal file. 472 ** This function reads the journal file and adds the page number for each 473 ** page in the journal to the Bitvec object passed as the second argument. 474 */ 475 static int readJournalFile(jt_file *p, jt_file *pMain){ 476 int rc = SQLITE_OK; 477 unsigned char zBuf[28]; 478 sqlite3_file *pReal = p->pReal; 479 sqlite3_int64 iOff = 0; 480 sqlite3_int64 iSize = p->iMaxOff; 481 unsigned char *aPage; 482 int iSave; 483 484 aPage = sqlite3_malloc(pMain->nPagesize); 485 if( !aPage ){ 486 return SQLITE_IOERR_NOMEM; 487 } 488 489 stop_ioerr_simulation(&iSave); 490 491 while( rc==SQLITE_OK && iOff<iSize ){ 492 u32 nRec, nPage, nSector, nPagesize; 493 u32 ii; 494 495 /* Read and decode the next journal-header from the journal file. */ 496 rc = sqlite3OsRead(pReal, zBuf, 28, iOff); 497 if( rc!=SQLITE_OK 498 || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize) 499 ){ 500 goto finish_rjf; 501 } 502 iOff += nSector; 503 504 if( nRec==0 ){ 505 /* A trick. There might be another journal-header immediately 506 ** following this one. In this case, 0 records means 0 records, 507 ** not "read until the end of the file". See also ticket #2565. 508 */ 509 if( iSize>=(iOff+nSector) ){ 510 rc = sqlite3OsRead(pReal, zBuf, 28, iOff); 511 if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){ 512 continue; 513 } 514 } 515 nRec = (iSize-iOff) / (pMain->nPagesize+8); 516 } 517 518 /* Read all the records that follow the journal-header just read. */ 519 for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){ 520 u32 pgno; 521 rc = sqlite3OsRead(pReal, zBuf, 4, iOff); 522 if( rc==SQLITE_OK ){ 523 pgno = decodeUint32(zBuf); 524 if( pgno>0 && pgno<=pMain->nPage ){ 525 if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){ 526 rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4); 527 if( rc==SQLITE_OK ){ 528 u32 cksum = genCksum(aPage, pMain->nPagesize); 529 assert( cksum==pMain->aCksum[pgno-1] ); 530 } 531 } 532 sqlite3BitvecSet(pMain->pWritable, pgno); 533 } 534 iOff += (8 + pMain->nPagesize); 535 } 536 } 537 538 iOff = ((iOff + (nSector-1)) / nSector) * nSector; 539 } 540 541 finish_rjf: 542 start_ioerr_simulation(iSave); 543 sqlite3_free(aPage); 544 if( rc==SQLITE_IOERR_SHORT_READ ){ 545 rc = SQLITE_OK; 546 } 547 return rc; 548 } 549 550 /* 551 ** Sync an jt-file. 552 */ 553 static int jtSync(sqlite3_file *pFile, int flags){ 554 jt_file *p = (jt_file *)pFile; 555 556 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ 557 int rc; 558 jt_file *pMain; /* The associated database file */ 559 560 /* The journal file is being synced. At this point, we inspect the 561 ** contents of the file up to this point and set each bit in the 562 ** jt_file.pWritable bitvec of the main database file associated with 563 ** this journal file. 564 */ 565 pMain = locateDatabaseHandle(p->zName); 566 assert(pMain); 567 568 /* Set the bitvec values */ 569 if( pMain->pWritable ){ 570 pMain->nSync++; 571 rc = readJournalFile(p, pMain); 572 if( rc!=SQLITE_OK ){ 573 return rc; 574 } 575 } 576 } 577 578 return sqlite3OsSync(p->pReal, flags); 579 } 580 581 /* 582 ** Return the current file-size of an jt-file. 583 */ 584 static int jtFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 585 jt_file *p = (jt_file *)pFile; 586 return sqlite3OsFileSize(p->pReal, pSize); 587 } 588 589 /* 590 ** Lock an jt-file. 591 */ 592 static int jtLock(sqlite3_file *pFile, int eLock){ 593 int rc; 594 jt_file *p = (jt_file *)pFile; 595 rc = sqlite3OsLock(p->pReal, eLock); 596 if( rc==SQLITE_OK && eLock>p->eLock ){ 597 p->eLock = eLock; 598 } 599 return rc; 600 } 601 602 /* 603 ** Unlock an jt-file. 604 */ 605 static int jtUnlock(sqlite3_file *pFile, int eLock){ 606 int rc; 607 jt_file *p = (jt_file *)pFile; 608 rc = sqlite3OsUnlock(p->pReal, eLock); 609 if( rc==SQLITE_OK && eLock<p->eLock ){ 610 p->eLock = eLock; 611 } 612 return rc; 613 } 614 615 /* 616 ** Check if another file-handle holds a RESERVED lock on an jt-file. 617 */ 618 static int jtCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 619 jt_file *p = (jt_file *)pFile; 620 return sqlite3OsCheckReservedLock(p->pReal, pResOut); 621 } 622 623 /* 624 ** File control method. For custom operations on an jt-file. 625 */ 626 static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){ 627 jt_file *p = (jt_file *)pFile; 628 return sqlite3OsFileControl(p->pReal, op, pArg); 629 } 630 631 /* 632 ** Return the sector-size in bytes for an jt-file. 633 */ 634 static int jtSectorSize(sqlite3_file *pFile){ 635 jt_file *p = (jt_file *)pFile; 636 return sqlite3OsSectorSize(p->pReal); 637 } 638 639 /* 640 ** Return the device characteristic flags supported by an jt-file. 641 */ 642 static int jtDeviceCharacteristics(sqlite3_file *pFile){ 643 jt_file *p = (jt_file *)pFile; 644 return sqlite3OsDeviceCharacteristics(p->pReal); 645 } 646 647 /* 648 ** Open an jt file handle. 649 */ 650 static int jtOpen( 651 sqlite3_vfs *pVfs, 652 const char *zName, 653 sqlite3_file *pFile, 654 int flags, 655 int *pOutFlags 656 ){ 657 int rc; 658 jt_file *p = (jt_file *)pFile; 659 pFile->pMethods = 0; 660 p->pReal = (sqlite3_file *)&p[1]; 661 p->pReal->pMethods = 0; 662 rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags); 663 assert( rc==SQLITE_OK || p->pReal->pMethods==0 ); 664 if( rc==SQLITE_OK ){ 665 pFile->pMethods = &jt_io_methods; 666 p->eLock = 0; 667 p->zName = zName; 668 p->flags = flags; 669 p->pNext = 0; 670 p->pWritable = 0; 671 p->aCksum = 0; 672 if( zName ){ 673 p->pNext = g.pList; 674 g.pList = p; 675 } 676 } 677 return rc; 678 } 679 680 /* 681 ** Delete the file located at zPath. If the dirSync argument is true, 682 ** ensure the file-system modifications are synced to disk before 683 ** returning. 684 */ 685 static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 686 int nPath = strlen(zPath); 687 if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){ 688 /* Deleting a journal file. The end of a transaction. */ 689 jt_file *pMain = locateDatabaseHandle(zPath); 690 if( pMain ){ 691 closeTransaction(pMain); 692 } 693 } 694 695 return sqlite3OsDelete(g.pVfs, zPath, dirSync); 696 } 697 698 /* 699 ** Test for access permissions. Return true if the requested permission 700 ** is available, or false otherwise. 701 */ 702 static int jtAccess( 703 sqlite3_vfs *pVfs, 704 const char *zPath, 705 int flags, 706 int *pResOut 707 ){ 708 return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut); 709 } 710 711 /* 712 ** Populate buffer zOut with the full canonical pathname corresponding 713 ** to the pathname in zPath. zOut is guaranteed to point to a buffer 714 ** of at least (JT_MAX_PATHNAME+1) bytes. 715 */ 716 static int jtFullPathname( 717 sqlite3_vfs *pVfs, 718 const char *zPath, 719 int nOut, 720 char *zOut 721 ){ 722 return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut); 723 } 724 725 /* 726 ** Open the dynamic library located at zPath and return a handle. 727 */ 728 static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){ 729 return g.pVfs->xDlOpen(g.pVfs, zPath); 730 } 731 732 /* 733 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable 734 ** utf-8 string describing the most recent error encountered associated 735 ** with dynamic libraries. 736 */ 737 static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ 738 g.pVfs->xDlError(g.pVfs, nByte, zErrMsg); 739 } 740 741 /* 742 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. 743 */ 744 static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ 745 return g.pVfs->xDlSym(g.pVfs, p, zSym); 746 } 747 748 /* 749 ** Close the dynamic library handle pHandle. 750 */ 751 static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){ 752 g.pVfs->xDlClose(g.pVfs, pHandle); 753 } 754 755 /* 756 ** Populate the buffer pointed to by zBufOut with nByte bytes of 757 ** random data. 758 */ 759 static int jtRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ 760 return sqlite3OsRandomness(g.pVfs, nByte, zBufOut); 761 } 762 763 /* 764 ** Sleep for nMicro microseconds. Return the number of microseconds 765 ** actually slept. 766 */ 767 static int jtSleep(sqlite3_vfs *pVfs, int nMicro){ 768 return sqlite3OsSleep(g.pVfs, nMicro); 769 } 770 771 /* 772 ** Return the current time as a Julian Day number in *pTimeOut. 773 */ 774 static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ 775 return sqlite3OsCurrentTime(g.pVfs, pTimeOut); 776 } 777 778 /************************************************************************** 779 ** Start of public API. 780 */ 781 782 /* 783 ** Configure the jt VFS as a wrapper around the VFS named by parameter 784 ** zWrap. If the isDefault parameter is true, then the jt VFS is installed 785 ** as the new default VFS for SQLite connections. If isDefault is not 786 ** true, then the jt VFS is installed as non-default. In this case it 787 ** is available via its name, "jt". 788 */ 789 int jt_register(char *zWrap, int isDefault){ 790 g.pVfs = sqlite3_vfs_find(zWrap); 791 if( g.pVfs==0 ){ 792 return SQLITE_ERROR; 793 } 794 jt_vfs.szOsFile = sizeof(jt_file) + g.pVfs->szOsFile; 795 sqlite3_vfs_register(&jt_vfs, isDefault); 796 return SQLITE_OK; 797 } 798 799 /* 800 ** Uninstall the jt VFS, if it is installed. 801 */ 802 void jt_unregister(){ 803 sqlite3_vfs_unregister(&jt_vfs); 804 } 805 806 #endif 807