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.16 2009/06/26 09:01:28 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( iAmt==28 ){ 424 /* Zeroing the first journal-file header. This is the end of a 425 ** transaction. */ 426 closeTransaction(pMain); 427 }else if( iAmt!=12 ){ 428 /* Writing the first journal header to a journal file. This happens 429 ** when a transaction is first started. */ 430 int rc; 431 u8 *z = (u8 *)zBuf; 432 pMain->nPage = decodeUint32(&z[16]); 433 pMain->nPagesize = decodeUint32(&z[24]); 434 if( SQLITE_OK!=(rc=openTransaction(pMain, p)) ){ 435 return rc; 436 } 437 } 438 } 439 if( p->iMaxOff<(iOfst + iAmt) ){ 440 p->iMaxOff = iOfst + iAmt; 441 } 442 } 443 444 if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ 445 if( iAmt<p->nPagesize 446 && p->nPagesize%iAmt==0 447 && iOfst>=(PENDING_BYTE+512) 448 && iOfst+iAmt<=PENDING_BYTE+p->nPagesize 449 ){ 450 /* No-op. This special case is hit when the backup code is copying a 451 ** to a database with a larger page-size than the source database and 452 ** it needs to fill in the non-locking-region part of the original 453 ** pending-byte page. 454 */ 455 }else{ 456 u32 pgno = iOfst/p->nPagesize + 1; 457 assert( (iAmt==1||iAmt==p->nPagesize) && ((iOfst+iAmt)%p->nPagesize)==0 ); 458 assert( pgno<=p->nPage || p->nSync>0 ); 459 assert( pgno>p->nPage || sqlite3BitvecTest(p->pWritable, pgno) ); 460 } 461 } 462 463 return sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst); 464 } 465 466 /* 467 ** Truncate an jt-file. 468 */ 469 static int jtTruncate(sqlite3_file *pFile, sqlite_int64 size){ 470 jt_file *p = (jt_file *)pFile; 471 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL && size==0 ){ 472 /* Truncating a journal file. This is the end of a transaction. */ 473 jt_file *pMain = locateDatabaseHandle(p->zName); 474 closeTransaction(pMain); 475 } 476 if( p->flags&SQLITE_OPEN_MAIN_DB && p->pWritable ){ 477 u32 pgno; 478 u32 locking_page = (u32)(PENDING_BYTE/p->nPagesize+1); 479 for(pgno=size/p->nPagesize+1; pgno<=p->nPage; pgno++){ 480 assert( pgno==locking_page || sqlite3BitvecTest(p->pWritable, pgno) ); 481 } 482 } 483 return sqlite3OsTruncate(p->pReal, size); 484 } 485 486 /* 487 ** The first argument to this function is a handle open on a journal file. 488 ** This function reads the journal file and adds the page number for each 489 ** page in the journal to the Bitvec object passed as the second argument. 490 */ 491 static int readJournalFile(jt_file *p, jt_file *pMain){ 492 int rc = SQLITE_OK; 493 unsigned char zBuf[28]; 494 sqlite3_file *pReal = p->pReal; 495 sqlite3_int64 iOff = 0; 496 sqlite3_int64 iSize = p->iMaxOff; 497 unsigned char *aPage; 498 int iSave; 499 500 aPage = sqlite3_malloc(pMain->nPagesize); 501 if( !aPage ){ 502 return SQLITE_IOERR_NOMEM; 503 } 504 505 stop_ioerr_simulation(&iSave); 506 507 while( rc==SQLITE_OK && iOff<iSize ){ 508 u32 nRec, nPage, nSector, nPagesize; 509 u32 ii; 510 511 /* Read and decode the next journal-header from the journal file. */ 512 rc = sqlite3OsRead(pReal, zBuf, 28, iOff); 513 if( rc!=SQLITE_OK 514 || decodeJournalHdr(zBuf, &nRec, &nPage, &nSector, &nPagesize) 515 ){ 516 goto finish_rjf; 517 } 518 iOff += nSector; 519 520 if( nRec==0 ){ 521 /* A trick. There might be another journal-header immediately 522 ** following this one. In this case, 0 records means 0 records, 523 ** not "read until the end of the file". See also ticket #2565. 524 */ 525 if( iSize>=(iOff+nSector) ){ 526 rc = sqlite3OsRead(pReal, zBuf, 28, iOff); 527 if( rc!=SQLITE_OK || 0==decodeJournalHdr(zBuf, 0, 0, 0, 0) ){ 528 continue; 529 } 530 } 531 nRec = (iSize-iOff) / (pMain->nPagesize+8); 532 } 533 534 /* Read all the records that follow the journal-header just read. */ 535 for(ii=0; rc==SQLITE_OK && ii<nRec && iOff<iSize; ii++){ 536 u32 pgno; 537 rc = sqlite3OsRead(pReal, zBuf, 4, iOff); 538 if( rc==SQLITE_OK ){ 539 pgno = decodeUint32(zBuf); 540 if( pgno>0 && pgno<=pMain->nPage ){ 541 if( 0==sqlite3BitvecTest(pMain->pWritable, pgno) ){ 542 rc = sqlite3OsRead(pReal, aPage, pMain->nPagesize, iOff+4); 543 if( rc==SQLITE_OK ){ 544 u32 cksum = genCksum(aPage, pMain->nPagesize); 545 assert( cksum==pMain->aCksum[pgno-1] ); 546 } 547 } 548 sqlite3BitvecSet(pMain->pWritable, pgno); 549 } 550 iOff += (8 + pMain->nPagesize); 551 } 552 } 553 554 iOff = ((iOff + (nSector-1)) / nSector) * nSector; 555 } 556 557 finish_rjf: 558 start_ioerr_simulation(iSave); 559 sqlite3_free(aPage); 560 if( rc==SQLITE_IOERR_SHORT_READ ){ 561 rc = SQLITE_OK; 562 } 563 return rc; 564 } 565 566 /* 567 ** Sync an jt-file. 568 */ 569 static int jtSync(sqlite3_file *pFile, int flags){ 570 jt_file *p = (jt_file *)pFile; 571 572 if( p->flags&SQLITE_OPEN_MAIN_JOURNAL ){ 573 int rc; 574 jt_file *pMain; /* The associated database file */ 575 576 /* The journal file is being synced. At this point, we inspect the 577 ** contents of the file up to this point and set each bit in the 578 ** jt_file.pWritable bitvec of the main database file associated with 579 ** this journal file. 580 */ 581 pMain = locateDatabaseHandle(p->zName); 582 assert(pMain); 583 584 /* Set the bitvec values */ 585 if( pMain->pWritable ){ 586 pMain->nSync++; 587 rc = readJournalFile(p, pMain); 588 if( rc!=SQLITE_OK ){ 589 return rc; 590 } 591 } 592 } 593 594 return sqlite3OsSync(p->pReal, flags); 595 } 596 597 /* 598 ** Return the current file-size of an jt-file. 599 */ 600 static int jtFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 601 jt_file *p = (jt_file *)pFile; 602 return sqlite3OsFileSize(p->pReal, pSize); 603 } 604 605 /* 606 ** Lock an jt-file. 607 */ 608 static int jtLock(sqlite3_file *pFile, int eLock){ 609 int rc; 610 jt_file *p = (jt_file *)pFile; 611 rc = sqlite3OsLock(p->pReal, eLock); 612 if( rc==SQLITE_OK && eLock>p->eLock ){ 613 p->eLock = eLock; 614 } 615 return rc; 616 } 617 618 /* 619 ** Unlock an jt-file. 620 */ 621 static int jtUnlock(sqlite3_file *pFile, int eLock){ 622 int rc; 623 jt_file *p = (jt_file *)pFile; 624 rc = sqlite3OsUnlock(p->pReal, eLock); 625 if( rc==SQLITE_OK && eLock<p->eLock ){ 626 p->eLock = eLock; 627 } 628 return rc; 629 } 630 631 /* 632 ** Check if another file-handle holds a RESERVED lock on an jt-file. 633 */ 634 static int jtCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 635 jt_file *p = (jt_file *)pFile; 636 return sqlite3OsCheckReservedLock(p->pReal, pResOut); 637 } 638 639 /* 640 ** File control method. For custom operations on an jt-file. 641 */ 642 static int jtFileControl(sqlite3_file *pFile, int op, void *pArg){ 643 jt_file *p = (jt_file *)pFile; 644 return sqlite3OsFileControl(p->pReal, op, pArg); 645 } 646 647 /* 648 ** Return the sector-size in bytes for an jt-file. 649 */ 650 static int jtSectorSize(sqlite3_file *pFile){ 651 jt_file *p = (jt_file *)pFile; 652 return sqlite3OsSectorSize(p->pReal); 653 } 654 655 /* 656 ** Return the device characteristic flags supported by an jt-file. 657 */ 658 static int jtDeviceCharacteristics(sqlite3_file *pFile){ 659 jt_file *p = (jt_file *)pFile; 660 return sqlite3OsDeviceCharacteristics(p->pReal); 661 } 662 663 /* 664 ** Open an jt file handle. 665 */ 666 static int jtOpen( 667 sqlite3_vfs *pVfs, 668 const char *zName, 669 sqlite3_file *pFile, 670 int flags, 671 int *pOutFlags 672 ){ 673 int rc; 674 jt_file *p = (jt_file *)pFile; 675 pFile->pMethods = 0; 676 p->pReal = (sqlite3_file *)&p[1]; 677 p->pReal->pMethods = 0; 678 rc = sqlite3OsOpen(g.pVfs, zName, p->pReal, flags, pOutFlags); 679 assert( rc==SQLITE_OK || p->pReal->pMethods==0 ); 680 if( rc==SQLITE_OK ){ 681 pFile->pMethods = &jt_io_methods; 682 p->eLock = 0; 683 p->zName = zName; 684 p->flags = flags; 685 p->pNext = 0; 686 p->pWritable = 0; 687 p->aCksum = 0; 688 enterJtMutex(); 689 if( zName ){ 690 p->pNext = g.pList; 691 g.pList = p; 692 } 693 leaveJtMutex(); 694 } 695 return rc; 696 } 697 698 /* 699 ** Delete the file located at zPath. If the dirSync argument is true, 700 ** ensure the file-system modifications are synced to disk before 701 ** returning. 702 */ 703 static int jtDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 704 int nPath = strlen(zPath); 705 if( nPath>8 && 0==strcmp("-journal", &zPath[nPath-8]) ){ 706 /* Deleting a journal file. The end of a transaction. */ 707 jt_file *pMain = locateDatabaseHandle(zPath); 708 if( pMain ){ 709 closeTransaction(pMain); 710 } 711 } 712 713 return sqlite3OsDelete(g.pVfs, zPath, dirSync); 714 } 715 716 /* 717 ** Test for access permissions. Return true if the requested permission 718 ** is available, or false otherwise. 719 */ 720 static int jtAccess( 721 sqlite3_vfs *pVfs, 722 const char *zPath, 723 int flags, 724 int *pResOut 725 ){ 726 return sqlite3OsAccess(g.pVfs, zPath, flags, pResOut); 727 } 728 729 /* 730 ** Populate buffer zOut with the full canonical pathname corresponding 731 ** to the pathname in zPath. zOut is guaranteed to point to a buffer 732 ** of at least (JT_MAX_PATHNAME+1) bytes. 733 */ 734 static int jtFullPathname( 735 sqlite3_vfs *pVfs, 736 const char *zPath, 737 int nOut, 738 char *zOut 739 ){ 740 return sqlite3OsFullPathname(g.pVfs, zPath, nOut, zOut); 741 } 742 743 /* 744 ** Open the dynamic library located at zPath and return a handle. 745 */ 746 static void *jtDlOpen(sqlite3_vfs *pVfs, const char *zPath){ 747 return g.pVfs->xDlOpen(g.pVfs, zPath); 748 } 749 750 /* 751 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable 752 ** utf-8 string describing the most recent error encountered associated 753 ** with dynamic libraries. 754 */ 755 static void jtDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ 756 g.pVfs->xDlError(g.pVfs, nByte, zErrMsg); 757 } 758 759 /* 760 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. 761 */ 762 static void (*jtDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ 763 return g.pVfs->xDlSym(g.pVfs, p, zSym); 764 } 765 766 /* 767 ** Close the dynamic library handle pHandle. 768 */ 769 static void jtDlClose(sqlite3_vfs *pVfs, void *pHandle){ 770 g.pVfs->xDlClose(g.pVfs, pHandle); 771 } 772 773 /* 774 ** Populate the buffer pointed to by zBufOut with nByte bytes of 775 ** random data. 776 */ 777 static int jtRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ 778 return sqlite3OsRandomness(g.pVfs, nByte, zBufOut); 779 } 780 781 /* 782 ** Sleep for nMicro microseconds. Return the number of microseconds 783 ** actually slept. 784 */ 785 static int jtSleep(sqlite3_vfs *pVfs, int nMicro){ 786 return sqlite3OsSleep(g.pVfs, nMicro); 787 } 788 789 /* 790 ** Return the current time as a Julian Day number in *pTimeOut. 791 */ 792 static int jtCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ 793 return sqlite3OsCurrentTime(g.pVfs, pTimeOut); 794 } 795 796 /************************************************************************** 797 ** Start of public API. 798 */ 799 800 /* 801 ** Configure the jt VFS as a wrapper around the VFS named by parameter 802 ** zWrap. If the isDefault parameter is true, then the jt VFS is installed 803 ** as the new default VFS for SQLite connections. If isDefault is not 804 ** true, then the jt VFS is installed as non-default. In this case it 805 ** is available via its name, "jt". 806 */ 807 int jt_register(char *zWrap, int isDefault){ 808 g.pVfs = sqlite3_vfs_find(zWrap); 809 if( g.pVfs==0 ){ 810 return SQLITE_ERROR; 811 } 812 jt_vfs.szOsFile = sizeof(jt_file) + g.pVfs->szOsFile; 813 sqlite3_vfs_register(&jt_vfs, isDefault); 814 return SQLITE_OK; 815 } 816 817 /* 818 ** Uninstall the jt VFS, if it is installed. 819 */ 820 void jt_unregister(void){ 821 sqlite3_vfs_unregister(&jt_vfs); 822 } 823 824 #endif 825