1 /* 2 ** 2007 September 14 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 ** OVERVIEW: 14 ** 15 ** This file contains some example code demonstrating how the SQLite 16 ** vfs feature can be used to have SQLite operate directly on an 17 ** embedded media, without using an intermediate file system. 18 ** 19 ** Because this is only a demo designed to run on a workstation, the 20 ** underlying media is simulated using a regular file-system file. The 21 ** size of the file is fixed when it is first created (default size 10 MB). 22 ** From SQLite's point of view, this space is used to store a single 23 ** database file and the journal file. 24 ** 25 ** Any statement journal created is stored in volatile memory obtained 26 ** from sqlite3_malloc(). Any attempt to create a temporary database file 27 ** will fail (SQLITE_IOERR). To prevent SQLite from attempting this, 28 ** it should be configured to store all temporary database files in 29 ** main memory (see pragma "temp_store" or the SQLITE_TEMP_STORE compile 30 ** time option). 31 ** 32 ** ASSUMPTIONS: 33 ** 34 ** After it has been created, the blob file is accessed using the 35 ** following three functions only: 36 ** 37 ** mediaRead(); - Read a 512 byte block from the file. 38 ** mediaWrite(); - Write a 512 byte block to the file. 39 ** mediaSync(); - Tell the media hardware to sync. 40 ** 41 ** It is assumed that these can be easily implemented by any "real" 42 ** media vfs driver adapting this code. 43 ** 44 ** FILE FORMAT: 45 ** 46 ** The basic principle is that the "database file" is stored at the 47 ** beginning of the 10 MB blob and grows in a forward direction. The 48 ** "journal file" is stored at the end of the 10MB blob and grows 49 ** in the reverse direction. If, during a transaction, insufficient 50 ** space is available to expand either the journal or database file, 51 ** an SQLITE_FULL error is returned. The database file is never allowed 52 ** to consume more than 90% of the blob space. If SQLite tries to 53 ** create a file larger than this, SQLITE_FULL is returned. 54 ** 55 ** No allowance is made for "wear-leveling", as is required by. 56 ** embedded devices in the absence of equivalent hardware features. 57 ** 58 ** The first 512 block byte of the file is reserved for storing the 59 ** size of the "database file". It is updated as part of the sync() 60 ** operation. On startup, it can only be trusted if no journal file 61 ** exists. If a journal-file does exist, then it stores the real size 62 ** of the database region. The second and subsequent blocks store the 63 ** actual database content. 64 ** 65 ** The size of the "journal file" is not stored persistently in the 66 ** file. When the system is running, the size of the journal file is 67 ** stored in volatile memory. When recovering from a crash, this vfs 68 ** reports a very large size for the journal file. The normal journal 69 ** header and checksum mechanisms serve to prevent SQLite from 70 ** processing any data that lies past the logical end of the journal. 71 ** 72 ** When SQLite calls OsDelete() to delete the journal file, the final 73 ** 512 bytes of the blob (the area containing the first journal header) 74 ** are zeroed. 75 ** 76 ** LOCKING: 77 ** 78 ** File locking is a no-op. Only one connection may be open at any one 79 ** time using this demo vfs. 80 */ 81 82 #include "sqlite3.h" 83 #include <assert.h> 84 #include <string.h> 85 86 /* 87 ** Maximum pathname length supported by the fs backend. 88 */ 89 #define BLOCKSIZE 512 90 #define BLOBSIZE 10485760 91 92 /* 93 ** Name used to identify this VFS. 94 */ 95 #define FS_VFS_NAME "fs" 96 97 typedef struct fs_real_file fs_real_file; 98 struct fs_real_file { 99 sqlite3_file *pFile; 100 const char *zName; 101 int nDatabase; /* Current size of database region */ 102 int nJournal; /* Current size of journal region */ 103 int nBlob; /* Total size of allocated blob */ 104 int nRef; /* Number of pointers to this structure */ 105 fs_real_file *pNext; 106 fs_real_file **ppThis; 107 }; 108 109 typedef struct fs_file fs_file; 110 struct fs_file { 111 sqlite3_file base; 112 int eType; 113 fs_real_file *pReal; 114 }; 115 116 typedef struct tmp_file tmp_file; 117 struct tmp_file { 118 sqlite3_file base; 119 int nSize; 120 int nAlloc; 121 char *zAlloc; 122 }; 123 124 /* Values for fs_file.eType. */ 125 #define DATABASE_FILE 1 126 #define JOURNAL_FILE 2 127 128 /* 129 ** Method declarations for fs_file. 130 */ 131 static int fsClose(sqlite3_file*); 132 static int fsRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); 133 static int fsWrite(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); 134 static int fsTruncate(sqlite3_file*, sqlite3_int64 size); 135 static int fsSync(sqlite3_file*, int flags); 136 static int fsFileSize(sqlite3_file*, sqlite3_int64 *pSize); 137 static int fsLock(sqlite3_file*, int); 138 static int fsUnlock(sqlite3_file*, int); 139 static int fsCheckReservedLock(sqlite3_file*, int *pResOut); 140 static int fsFileControl(sqlite3_file*, int op, void *pArg); 141 static int fsSectorSize(sqlite3_file*); 142 static int fsDeviceCharacteristics(sqlite3_file*); 143 144 /* 145 ** Method declarations for tmp_file. 146 */ 147 static int tmpClose(sqlite3_file*); 148 static int tmpRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); 149 static int tmpWrite(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); 150 static int tmpTruncate(sqlite3_file*, sqlite3_int64 size); 151 static int tmpSync(sqlite3_file*, int flags); 152 static int tmpFileSize(sqlite3_file*, sqlite3_int64 *pSize); 153 static int tmpLock(sqlite3_file*, int); 154 static int tmpUnlock(sqlite3_file*, int); 155 static int tmpCheckReservedLock(sqlite3_file*, int *pResOut); 156 static int tmpFileControl(sqlite3_file*, int op, void *pArg); 157 static int tmpSectorSize(sqlite3_file*); 158 static int tmpDeviceCharacteristics(sqlite3_file*); 159 160 /* 161 ** Method declarations for fs_vfs. 162 */ 163 static int fsOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); 164 static int fsDelete(sqlite3_vfs*, const char *zName, int syncDir); 165 static int fsAccess(sqlite3_vfs*, const char *zName, int flags, int *); 166 static int fsFullPathname(sqlite3_vfs*, const char *zName, int nOut,char *zOut); 167 static void *fsDlOpen(sqlite3_vfs*, const char *zFilename); 168 static void fsDlError(sqlite3_vfs*, int nByte, char *zErrMsg); 169 static void (*fsDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); 170 static void fsDlClose(sqlite3_vfs*, void*); 171 static int fsRandomness(sqlite3_vfs*, int nByte, char *zOut); 172 static int fsSleep(sqlite3_vfs*, int microseconds); 173 static int fsCurrentTime(sqlite3_vfs*, double*); 174 175 176 typedef struct fs_vfs_t fs_vfs_t; 177 struct fs_vfs_t { 178 sqlite3_vfs base; 179 fs_real_file *pFileList; 180 sqlite3_vfs *pParent; 181 }; 182 183 static fs_vfs_t fs_vfs = { 184 { 185 1, /* iVersion */ 186 0, /* szOsFile */ 187 0, /* mxPathname */ 188 0, /* pNext */ 189 FS_VFS_NAME, /* zName */ 190 0, /* pAppData */ 191 fsOpen, /* xOpen */ 192 fsDelete, /* xDelete */ 193 fsAccess, /* xAccess */ 194 fsFullPathname, /* xFullPathname */ 195 fsDlOpen, /* xDlOpen */ 196 fsDlError, /* xDlError */ 197 fsDlSym, /* xDlSym */ 198 fsDlClose, /* xDlClose */ 199 fsRandomness, /* xRandomness */ 200 fsSleep, /* xSleep */ 201 fsCurrentTime, /* xCurrentTime */ 202 0 /* xCurrentTimeInt64 */ 203 }, 204 0, /* pFileList */ 205 0 /* pParent */ 206 }; 207 208 static sqlite3_io_methods fs_io_methods = { 209 1, /* iVersion */ 210 fsClose, /* xClose */ 211 fsRead, /* xRead */ 212 fsWrite, /* xWrite */ 213 fsTruncate, /* xTruncate */ 214 fsSync, /* xSync */ 215 fsFileSize, /* xFileSize */ 216 fsLock, /* xLock */ 217 fsUnlock, /* xUnlock */ 218 fsCheckReservedLock, /* xCheckReservedLock */ 219 fsFileControl, /* xFileControl */ 220 fsSectorSize, /* xSectorSize */ 221 fsDeviceCharacteristics, /* xDeviceCharacteristics */ 222 0, /* xShmOpen */ 223 0, /* xShmLock */ 224 0, /* xShmMap */ 225 0, /* xShmBarrier */ 226 0 /* xShmClose */ 227 }; 228 229 230 static sqlite3_io_methods tmp_io_methods = { 231 1, /* iVersion */ 232 tmpClose, /* xClose */ 233 tmpRead, /* xRead */ 234 tmpWrite, /* xWrite */ 235 tmpTruncate, /* xTruncate */ 236 tmpSync, /* xSync */ 237 tmpFileSize, /* xFileSize */ 238 tmpLock, /* xLock */ 239 tmpUnlock, /* xUnlock */ 240 tmpCheckReservedLock, /* xCheckReservedLock */ 241 tmpFileControl, /* xFileControl */ 242 tmpSectorSize, /* xSectorSize */ 243 tmpDeviceCharacteristics, /* xDeviceCharacteristics */ 244 0, /* xShmOpen */ 245 0, /* xShmLock */ 246 0, /* xShmMap */ 247 0, /* xShmBarrier */ 248 0 /* xShmClose */ 249 }; 250 251 /* Useful macros used in several places */ 252 #define MIN(x,y) ((x)<(y)?(x):(y)) 253 #define MAX(x,y) ((x)>(y)?(x):(y)) 254 255 256 /* 257 ** Close a tmp-file. 258 */ 259 static int tmpClose(sqlite3_file *pFile){ 260 tmp_file *pTmp = (tmp_file *)pFile; 261 sqlite3_free(pTmp->zAlloc); 262 return SQLITE_OK; 263 } 264 265 /* 266 ** Read data from a tmp-file. 267 */ 268 static int tmpRead( 269 sqlite3_file *pFile, 270 void *zBuf, 271 int iAmt, 272 sqlite_int64 iOfst 273 ){ 274 tmp_file *pTmp = (tmp_file *)pFile; 275 if( (iAmt+iOfst)>pTmp->nSize ){ 276 return SQLITE_IOERR_SHORT_READ; 277 } 278 memcpy(zBuf, &pTmp->zAlloc[iOfst], iAmt); 279 return SQLITE_OK; 280 } 281 282 /* 283 ** Write data to a tmp-file. 284 */ 285 static int tmpWrite( 286 sqlite3_file *pFile, 287 const void *zBuf, 288 int iAmt, 289 sqlite_int64 iOfst 290 ){ 291 tmp_file *pTmp = (tmp_file *)pFile; 292 if( (iAmt+iOfst)>pTmp->nAlloc ){ 293 int nNew = 2*(iAmt+iOfst+pTmp->nAlloc); 294 char *zNew = sqlite3_realloc(pTmp->zAlloc, nNew); 295 if( !zNew ){ 296 return SQLITE_NOMEM; 297 } 298 pTmp->zAlloc = zNew; 299 pTmp->nAlloc = nNew; 300 } 301 memcpy(&pTmp->zAlloc[iOfst], zBuf, iAmt); 302 pTmp->nSize = MAX(pTmp->nSize, iOfst+iAmt); 303 return SQLITE_OK; 304 } 305 306 /* 307 ** Truncate a tmp-file. 308 */ 309 static int tmpTruncate(sqlite3_file *pFile, sqlite_int64 size){ 310 tmp_file *pTmp = (tmp_file *)pFile; 311 pTmp->nSize = MIN(pTmp->nSize, size); 312 return SQLITE_OK; 313 } 314 315 /* 316 ** Sync a tmp-file. 317 */ 318 static int tmpSync(sqlite3_file *pFile, int flags){ 319 return SQLITE_OK; 320 } 321 322 /* 323 ** Return the current file-size of a tmp-file. 324 */ 325 static int tmpFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 326 tmp_file *pTmp = (tmp_file *)pFile; 327 *pSize = pTmp->nSize; 328 return SQLITE_OK; 329 } 330 331 /* 332 ** Lock a tmp-file. 333 */ 334 static int tmpLock(sqlite3_file *pFile, int eLock){ 335 return SQLITE_OK; 336 } 337 338 /* 339 ** Unlock a tmp-file. 340 */ 341 static int tmpUnlock(sqlite3_file *pFile, int eLock){ 342 return SQLITE_OK; 343 } 344 345 /* 346 ** Check if another file-handle holds a RESERVED lock on a tmp-file. 347 */ 348 static int tmpCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 349 *pResOut = 0; 350 return SQLITE_OK; 351 } 352 353 /* 354 ** File control method. For custom operations on a tmp-file. 355 */ 356 static int tmpFileControl(sqlite3_file *pFile, int op, void *pArg){ 357 return SQLITE_OK; 358 } 359 360 /* 361 ** Return the sector-size in bytes for a tmp-file. 362 */ 363 static int tmpSectorSize(sqlite3_file *pFile){ 364 return 0; 365 } 366 367 /* 368 ** Return the device characteristic flags supported by a tmp-file. 369 */ 370 static int tmpDeviceCharacteristics(sqlite3_file *pFile){ 371 return 0; 372 } 373 374 /* 375 ** Close an fs-file. 376 */ 377 static int fsClose(sqlite3_file *pFile){ 378 int rc = SQLITE_OK; 379 fs_file *p = (fs_file *)pFile; 380 fs_real_file *pReal = p->pReal; 381 382 /* Decrement the real_file ref-count. */ 383 pReal->nRef--; 384 assert(pReal->nRef>=0); 385 386 /* When the ref-count reaches 0, destroy the structure */ 387 if( pReal->nRef==0 ){ 388 *pReal->ppThis = pReal->pNext; 389 if( pReal->pNext ){ 390 pReal->pNext->ppThis = pReal->ppThis; 391 } 392 rc = pReal->pFile->pMethods->xClose(pReal->pFile); 393 sqlite3_free(pReal); 394 } 395 396 return rc; 397 } 398 399 /* 400 ** Read data from an fs-file. 401 */ 402 static int fsRead( 403 sqlite3_file *pFile, 404 void *zBuf, 405 int iAmt, 406 sqlite_int64 iOfst 407 ){ 408 int rc = SQLITE_OK; 409 fs_file *p = (fs_file *)pFile; 410 fs_real_file *pReal = p->pReal; 411 sqlite3_file *pF = pReal->pFile; 412 413 if( (p->eType==DATABASE_FILE && (iAmt+iOfst)>pReal->nDatabase) 414 || (p->eType==JOURNAL_FILE && (iAmt+iOfst)>pReal->nJournal) 415 ){ 416 rc = SQLITE_IOERR_SHORT_READ; 417 }else if( p->eType==DATABASE_FILE ){ 418 rc = pF->pMethods->xRead(pF, zBuf, iAmt, iOfst+BLOCKSIZE); 419 }else{ 420 /* Journal file. */ 421 int iRem = iAmt; 422 int iBuf = 0; 423 int ii = iOfst; 424 while( iRem>0 && rc==SQLITE_OK ){ 425 int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; 426 int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); 427 428 rc = pF->pMethods->xRead(pF, &((char *)zBuf)[iBuf], iRealAmt, iRealOff); 429 ii += iRealAmt; 430 iBuf += iRealAmt; 431 iRem -= iRealAmt; 432 } 433 } 434 435 return rc; 436 } 437 438 /* 439 ** Write data to an fs-file. 440 */ 441 static int fsWrite( 442 sqlite3_file *pFile, 443 const void *zBuf, 444 int iAmt, 445 sqlite_int64 iOfst 446 ){ 447 int rc = SQLITE_OK; 448 fs_file *p = (fs_file *)pFile; 449 fs_real_file *pReal = p->pReal; 450 sqlite3_file *pF = pReal->pFile; 451 452 if( p->eType==DATABASE_FILE ){ 453 if( (iAmt+iOfst+BLOCKSIZE)>(pReal->nBlob-pReal->nJournal) ){ 454 rc = SQLITE_FULL; 455 }else{ 456 rc = pF->pMethods->xWrite(pF, zBuf, iAmt, iOfst+BLOCKSIZE); 457 if( rc==SQLITE_OK ){ 458 pReal->nDatabase = MAX(pReal->nDatabase, iAmt+iOfst); 459 } 460 } 461 }else{ 462 /* Journal file. */ 463 int iRem = iAmt; 464 int iBuf = 0; 465 int ii = iOfst; 466 while( iRem>0 && rc==SQLITE_OK ){ 467 int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; 468 int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); 469 470 if( iRealOff<(pReal->nDatabase+BLOCKSIZE) ){ 471 rc = SQLITE_FULL; 472 }else{ 473 rc = pF->pMethods->xWrite(pF, &((char *)zBuf)[iBuf], iRealAmt,iRealOff); 474 ii += iRealAmt; 475 iBuf += iRealAmt; 476 iRem -= iRealAmt; 477 } 478 } 479 if( rc==SQLITE_OK ){ 480 pReal->nJournal = MAX(pReal->nJournal, iAmt+iOfst); 481 } 482 } 483 484 return rc; 485 } 486 487 /* 488 ** Truncate an fs-file. 489 */ 490 static int fsTruncate(sqlite3_file *pFile, sqlite_int64 size){ 491 fs_file *p = (fs_file *)pFile; 492 fs_real_file *pReal = p->pReal; 493 if( p->eType==DATABASE_FILE ){ 494 pReal->nDatabase = MIN(pReal->nDatabase, size); 495 }else{ 496 pReal->nJournal = MIN(pReal->nJournal, size); 497 } 498 return SQLITE_OK; 499 } 500 501 /* 502 ** Sync an fs-file. 503 */ 504 static int fsSync(sqlite3_file *pFile, int flags){ 505 fs_file *p = (fs_file *)pFile; 506 fs_real_file *pReal = p->pReal; 507 sqlite3_file *pRealFile = pReal->pFile; 508 int rc = SQLITE_OK; 509 510 if( p->eType==DATABASE_FILE ){ 511 unsigned char zSize[4]; 512 zSize[0] = (pReal->nDatabase&0xFF000000)>>24; 513 zSize[1] = (pReal->nDatabase&0x00FF0000)>>16; 514 zSize[2] = (pReal->nDatabase&0x0000FF00)>>8; 515 zSize[3] = (pReal->nDatabase&0x000000FF); 516 rc = pRealFile->pMethods->xWrite(pRealFile, zSize, 4, 0); 517 } 518 if( rc==SQLITE_OK ){ 519 rc = pRealFile->pMethods->xSync(pRealFile, flags&(~SQLITE_SYNC_DATAONLY)); 520 } 521 522 return rc; 523 } 524 525 /* 526 ** Return the current file-size of an fs-file. 527 */ 528 static int fsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 529 fs_file *p = (fs_file *)pFile; 530 fs_real_file *pReal = p->pReal; 531 if( p->eType==DATABASE_FILE ){ 532 *pSize = pReal->nDatabase; 533 }else{ 534 *pSize = pReal->nJournal; 535 } 536 return SQLITE_OK; 537 } 538 539 /* 540 ** Lock an fs-file. 541 */ 542 static int fsLock(sqlite3_file *pFile, int eLock){ 543 return SQLITE_OK; 544 } 545 546 /* 547 ** Unlock an fs-file. 548 */ 549 static int fsUnlock(sqlite3_file *pFile, int eLock){ 550 return SQLITE_OK; 551 } 552 553 /* 554 ** Check if another file-handle holds a RESERVED lock on an fs-file. 555 */ 556 static int fsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 557 *pResOut = 0; 558 return SQLITE_OK; 559 } 560 561 /* 562 ** File control method. For custom operations on an fs-file. 563 */ 564 static int fsFileControl(sqlite3_file *pFile, int op, void *pArg){ 565 return SQLITE_OK; 566 } 567 568 /* 569 ** Return the sector-size in bytes for an fs-file. 570 */ 571 static int fsSectorSize(sqlite3_file *pFile){ 572 return BLOCKSIZE; 573 } 574 575 /* 576 ** Return the device characteristic flags supported by an fs-file. 577 */ 578 static int fsDeviceCharacteristics(sqlite3_file *pFile){ 579 return 0; 580 } 581 582 /* 583 ** Open an fs file handle. 584 */ 585 static int fsOpen( 586 sqlite3_vfs *pVfs, 587 const char *zName, 588 sqlite3_file *pFile, 589 int flags, 590 int *pOutFlags 591 ){ 592 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 593 fs_file *p = (fs_file *)pFile; 594 fs_real_file *pReal = 0; 595 int eType; 596 int nName; 597 int rc = SQLITE_OK; 598 599 if( 0==(flags&(SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_MAIN_JOURNAL)) ){ 600 tmp_file *p = (tmp_file *)pFile; 601 memset(p, 0, sizeof(*p)); 602 p->base.pMethods = &tmp_io_methods; 603 return SQLITE_OK; 604 } 605 606 eType = ((flags&(SQLITE_OPEN_MAIN_DB))?DATABASE_FILE:JOURNAL_FILE); 607 p->base.pMethods = &fs_io_methods; 608 p->eType = eType; 609 610 assert(strlen("-journal")==8); 611 nName = strlen(zName)-((eType==JOURNAL_FILE)?8:0); 612 pReal=pFsVfs->pFileList; 613 for(; pReal && strncmp(pReal->zName, zName, nName); pReal=pReal->pNext); 614 615 if( !pReal ){ 616 int real_flags = (flags&~(SQLITE_OPEN_MAIN_DB))|SQLITE_OPEN_TEMP_DB; 617 sqlite3_int64 size; 618 sqlite3_file *pRealFile; 619 sqlite3_vfs *pParent = pFsVfs->pParent; 620 assert(eType==DATABASE_FILE); 621 622 pReal = (fs_real_file *)sqlite3_malloc(sizeof(*pReal)+pParent->szOsFile); 623 if( !pReal ){ 624 rc = SQLITE_NOMEM; 625 goto open_out; 626 } 627 memset(pReal, 0, sizeof(*pReal)+pParent->szOsFile); 628 pReal->zName = zName; 629 pReal->pFile = (sqlite3_file *)(&pReal[1]); 630 631 rc = pParent->xOpen(pParent, zName, pReal->pFile, real_flags, pOutFlags); 632 if( rc!=SQLITE_OK ){ 633 goto open_out; 634 } 635 pRealFile = pReal->pFile; 636 637 rc = pRealFile->pMethods->xFileSize(pRealFile, &size); 638 if( rc!=SQLITE_OK ){ 639 goto open_out; 640 } 641 if( size==0 ){ 642 rc = pRealFile->pMethods->xWrite(pRealFile, "\0", 1, BLOBSIZE-1); 643 pReal->nBlob = BLOBSIZE; 644 }else{ 645 unsigned char zS[4]; 646 pReal->nBlob = size; 647 rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, 0); 648 pReal->nDatabase = (zS[0]<<24)+(zS[1]<<16)+(zS[2]<<8)+zS[3]; 649 if( rc==SQLITE_OK ){ 650 rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, pReal->nBlob-4); 651 if( zS[0] || zS[1] || zS[2] || zS[3] ){ 652 pReal->nJournal = pReal->nBlob; 653 } 654 } 655 } 656 657 if( rc==SQLITE_OK ){ 658 pReal->pNext = pFsVfs->pFileList; 659 if( pReal->pNext ){ 660 pReal->pNext->ppThis = &pReal->pNext; 661 } 662 pReal->ppThis = &pFsVfs->pFileList; 663 pFsVfs->pFileList = pReal; 664 } 665 } 666 667 open_out: 668 if( pReal ){ 669 if( rc==SQLITE_OK ){ 670 p->pReal = pReal; 671 pReal->nRef++; 672 }else{ 673 if( pReal->pFile->pMethods ){ 674 pReal->pFile->pMethods->xClose(pReal->pFile); 675 } 676 sqlite3_free(pReal); 677 } 678 } 679 return rc; 680 } 681 682 /* 683 ** Delete the file located at zPath. If the dirSync argument is true, 684 ** ensure the file-system modifications are synced to disk before 685 ** returning. 686 */ 687 static int fsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 688 int rc = SQLITE_OK; 689 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 690 fs_real_file *pReal; 691 sqlite3_file *pF; 692 int nName = strlen(zPath) - 8; 693 694 assert(strlen("-journal")==8); 695 assert(strcmp("-journal", &zPath[nName])==0); 696 697 pReal = pFsVfs->pFileList; 698 for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); 699 if( pReal ){ 700 pF = pReal->pFile; 701 rc = pF->pMethods->xWrite(pF, "\0\0\0\0", 4, pReal->nBlob-BLOCKSIZE); 702 if( rc==SQLITE_OK ){ 703 pReal->nJournal = 0; 704 } 705 } 706 return rc; 707 } 708 709 /* 710 ** Test for access permissions. Return true if the requested permission 711 ** is available, or false otherwise. 712 */ 713 static int fsAccess( 714 sqlite3_vfs *pVfs, 715 const char *zPath, 716 int flags, 717 int *pResOut 718 ){ 719 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 720 fs_real_file *pReal; 721 int isJournal = 0; 722 int nName = strlen(zPath); 723 724 if( flags!=SQLITE_ACCESS_EXISTS ){ 725 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 726 return pParent->xAccess(pParent, zPath, flags, pResOut); 727 } 728 729 assert(strlen("-journal")==8); 730 if( nName>8 && strcmp("-journal", &zPath[nName-8])==0 ){ 731 nName -= 8; 732 isJournal = 1; 733 } 734 735 pReal = pFsVfs->pFileList; 736 for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); 737 738 *pResOut = (pReal && (!isJournal || pReal->nJournal>0)); 739 return SQLITE_OK; 740 } 741 742 /* 743 ** Populate buffer zOut with the full canonical pathname corresponding 744 ** to the pathname in zPath. zOut is guaranteed to point to a buffer 745 ** of at least (FS_MAX_PATHNAME+1) bytes. 746 */ 747 static int fsFullPathname( 748 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 749 const char *zPath, /* Possibly relative input path */ 750 int nOut, /* Size of output buffer in bytes */ 751 char *zOut /* Output buffer */ 752 ){ 753 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 754 return pParent->xFullPathname(pParent, zPath, nOut, zOut); 755 } 756 757 /* 758 ** Open the dynamic library located at zPath and return a handle. 759 */ 760 static void *fsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ 761 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 762 return pParent->xDlOpen(pParent, zPath); 763 } 764 765 /* 766 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable 767 ** utf-8 string describing the most recent error encountered associated 768 ** with dynamic libraries. 769 */ 770 static void fsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ 771 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 772 pParent->xDlError(pParent, nByte, zErrMsg); 773 } 774 775 /* 776 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. 777 */ 778 static void (*fsDlSym(sqlite3_vfs *pVfs, void *pH, const char *zSym))(void){ 779 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 780 return pParent->xDlSym(pParent, pH, zSym); 781 } 782 783 /* 784 ** Close the dynamic library handle pHandle. 785 */ 786 static void fsDlClose(sqlite3_vfs *pVfs, void *pHandle){ 787 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 788 pParent->xDlClose(pParent, pHandle); 789 } 790 791 /* 792 ** Populate the buffer pointed to by zBufOut with nByte bytes of 793 ** random data. 794 */ 795 static int fsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ 796 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 797 return pParent->xRandomness(pParent, nByte, zBufOut); 798 } 799 800 /* 801 ** Sleep for nMicro microseconds. Return the number of microseconds 802 ** actually slept. 803 */ 804 static int fsSleep(sqlite3_vfs *pVfs, int nMicro){ 805 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 806 return pParent->xSleep(pParent, nMicro); 807 } 808 809 /* 810 ** Return the current time as a Julian Day number in *pTimeOut. 811 */ 812 static int fsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ 813 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 814 return pParent->xCurrentTime(pParent, pTimeOut); 815 } 816 817 /* 818 ** This procedure registers the fs vfs with SQLite. If the argument is 819 ** true, the fs vfs becomes the new default vfs. It is the only publicly 820 ** available function in this file. 821 */ 822 int fs_register(void){ 823 if( fs_vfs.pParent ) return SQLITE_OK; 824 fs_vfs.pParent = sqlite3_vfs_find(0); 825 fs_vfs.base.mxPathname = fs_vfs.pParent->mxPathname; 826 fs_vfs.base.szOsFile = MAX(sizeof(tmp_file), sizeof(fs_file)); 827 return sqlite3_vfs_register(&fs_vfs.base, 0); 828 } 829 830 #ifdef SQLITE_TEST 831 int SqlitetestOnefile_Init() {return fs_register();} 832 #endif 833