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 }, 203 0, /* pFileList */ 204 0 /* pParent */ 205 }; 206 207 static sqlite3_io_methods fs_io_methods = { 208 1, /* iVersion */ 209 fsClose, /* xClose */ 210 fsRead, /* xRead */ 211 fsWrite, /* xWrite */ 212 fsTruncate, /* xTruncate */ 213 fsSync, /* xSync */ 214 fsFileSize, /* xFileSize */ 215 fsLock, /* xLock */ 216 fsUnlock, /* xUnlock */ 217 fsCheckReservedLock, /* xCheckReservedLock */ 218 fsFileControl, /* xFileControl */ 219 fsSectorSize, /* xSectorSize */ 220 fsDeviceCharacteristics /* xDeviceCharacteristics */ 221 }; 222 223 224 static sqlite3_io_methods tmp_io_methods = { 225 1, /* iVersion */ 226 tmpClose, /* xClose */ 227 tmpRead, /* xRead */ 228 tmpWrite, /* xWrite */ 229 tmpTruncate, /* xTruncate */ 230 tmpSync, /* xSync */ 231 tmpFileSize, /* xFileSize */ 232 tmpLock, /* xLock */ 233 tmpUnlock, /* xUnlock */ 234 tmpCheckReservedLock, /* xCheckReservedLock */ 235 tmpFileControl, /* xFileControl */ 236 tmpSectorSize, /* xSectorSize */ 237 tmpDeviceCharacteristics /* xDeviceCharacteristics */ 238 }; 239 240 /* Useful macros used in several places */ 241 #define MIN(x,y) ((x)<(y)?(x):(y)) 242 #define MAX(x,y) ((x)>(y)?(x):(y)) 243 244 245 /* 246 ** Close a tmp-file. 247 */ 248 static int tmpClose(sqlite3_file *pFile){ 249 tmp_file *pTmp = (tmp_file *)pFile; 250 sqlite3_free(pTmp->zAlloc); 251 return SQLITE_OK; 252 } 253 254 /* 255 ** Read data from a tmp-file. 256 */ 257 static int tmpRead( 258 sqlite3_file *pFile, 259 void *zBuf, 260 int iAmt, 261 sqlite_int64 iOfst 262 ){ 263 tmp_file *pTmp = (tmp_file *)pFile; 264 if( (iAmt+iOfst)>pTmp->nSize ){ 265 return SQLITE_IOERR_SHORT_READ; 266 } 267 memcpy(zBuf, &pTmp->zAlloc[iOfst], iAmt); 268 return SQLITE_OK; 269 } 270 271 /* 272 ** Write data to a tmp-file. 273 */ 274 static int tmpWrite( 275 sqlite3_file *pFile, 276 const void *zBuf, 277 int iAmt, 278 sqlite_int64 iOfst 279 ){ 280 tmp_file *pTmp = (tmp_file *)pFile; 281 if( (iAmt+iOfst)>pTmp->nAlloc ){ 282 int nNew = 2*(iAmt+iOfst+pTmp->nAlloc); 283 char *zNew = sqlite3_realloc(pTmp->zAlloc, nNew); 284 if( !zNew ){ 285 return SQLITE_NOMEM; 286 } 287 pTmp->zAlloc = zNew; 288 pTmp->nAlloc = nNew; 289 } 290 memcpy(&pTmp->zAlloc[iOfst], zBuf, iAmt); 291 pTmp->nSize = MAX(pTmp->nSize, iOfst+iAmt); 292 return SQLITE_OK; 293 } 294 295 /* 296 ** Truncate a tmp-file. 297 */ 298 static int tmpTruncate(sqlite3_file *pFile, sqlite_int64 size){ 299 tmp_file *pTmp = (tmp_file *)pFile; 300 pTmp->nSize = MIN(pTmp->nSize, size); 301 return SQLITE_OK; 302 } 303 304 /* 305 ** Sync a tmp-file. 306 */ 307 static int tmpSync(sqlite3_file *pFile, int flags){ 308 return SQLITE_OK; 309 } 310 311 /* 312 ** Return the current file-size of a tmp-file. 313 */ 314 static int tmpFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 315 tmp_file *pTmp = (tmp_file *)pFile; 316 *pSize = pTmp->nSize; 317 return SQLITE_OK; 318 } 319 320 /* 321 ** Lock a tmp-file. 322 */ 323 static int tmpLock(sqlite3_file *pFile, int eLock){ 324 return SQLITE_OK; 325 } 326 327 /* 328 ** Unlock a tmp-file. 329 */ 330 static int tmpUnlock(sqlite3_file *pFile, int eLock){ 331 return SQLITE_OK; 332 } 333 334 /* 335 ** Check if another file-handle holds a RESERVED lock on a tmp-file. 336 */ 337 static int tmpCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 338 *pResOut = 0; 339 return SQLITE_OK; 340 } 341 342 /* 343 ** File control method. For custom operations on a tmp-file. 344 */ 345 static int tmpFileControl(sqlite3_file *pFile, int op, void *pArg){ 346 return SQLITE_OK; 347 } 348 349 /* 350 ** Return the sector-size in bytes for a tmp-file. 351 */ 352 static int tmpSectorSize(sqlite3_file *pFile){ 353 return 0; 354 } 355 356 /* 357 ** Return the device characteristic flags supported by a tmp-file. 358 */ 359 static int tmpDeviceCharacteristics(sqlite3_file *pFile){ 360 return 0; 361 } 362 363 /* 364 ** Close an fs-file. 365 */ 366 static int fsClose(sqlite3_file *pFile){ 367 int rc = SQLITE_OK; 368 fs_file *p = (fs_file *)pFile; 369 fs_real_file *pReal = p->pReal; 370 371 /* Decrement the real_file ref-count. */ 372 pReal->nRef--; 373 assert(pReal->nRef>=0); 374 375 /* When the ref-count reaches 0, destroy the structure */ 376 if( pReal->nRef==0 ){ 377 *pReal->ppThis = pReal->pNext; 378 if( pReal->pNext ){ 379 pReal->pNext->ppThis = pReal->ppThis; 380 } 381 rc = pReal->pFile->pMethods->xClose(pReal->pFile); 382 sqlite3_free(pReal); 383 } 384 385 return rc; 386 } 387 388 /* 389 ** Read data from an fs-file. 390 */ 391 static int fsRead( 392 sqlite3_file *pFile, 393 void *zBuf, 394 int iAmt, 395 sqlite_int64 iOfst 396 ){ 397 int rc = SQLITE_OK; 398 fs_file *p = (fs_file *)pFile; 399 fs_real_file *pReal = p->pReal; 400 sqlite3_file *pF = pReal->pFile; 401 402 if( (p->eType==DATABASE_FILE && (iAmt+iOfst)>pReal->nDatabase) 403 || (p->eType==JOURNAL_FILE && (iAmt+iOfst)>pReal->nJournal) 404 ){ 405 rc = SQLITE_IOERR_SHORT_READ; 406 }else if( p->eType==DATABASE_FILE ){ 407 rc = pF->pMethods->xRead(pF, zBuf, iAmt, iOfst+BLOCKSIZE); 408 }else{ 409 /* Journal file. */ 410 int iRem = iAmt; 411 int iBuf = 0; 412 int ii = iOfst; 413 while( iRem>0 && rc==SQLITE_OK ){ 414 int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; 415 int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); 416 417 rc = pF->pMethods->xRead(pF, &((char *)zBuf)[iBuf], iRealAmt, iRealOff); 418 ii += iRealAmt; 419 iBuf += iRealAmt; 420 iRem -= iRealAmt; 421 } 422 } 423 424 return rc; 425 } 426 427 /* 428 ** Write data to an fs-file. 429 */ 430 static int fsWrite( 431 sqlite3_file *pFile, 432 const void *zBuf, 433 int iAmt, 434 sqlite_int64 iOfst 435 ){ 436 int rc = SQLITE_OK; 437 fs_file *p = (fs_file *)pFile; 438 fs_real_file *pReal = p->pReal; 439 sqlite3_file *pF = pReal->pFile; 440 441 if( p->eType==DATABASE_FILE ){ 442 if( (iAmt+iOfst+BLOCKSIZE)>(pReal->nBlob-pReal->nJournal) ){ 443 rc = SQLITE_FULL; 444 }else{ 445 rc = pF->pMethods->xWrite(pF, zBuf, iAmt, iOfst+BLOCKSIZE); 446 if( rc==SQLITE_OK ){ 447 pReal->nDatabase = MAX(pReal->nDatabase, iAmt+iOfst); 448 } 449 } 450 }else{ 451 /* Journal file. */ 452 int iRem = iAmt; 453 int iBuf = 0; 454 int ii = iOfst; 455 while( iRem>0 && rc==SQLITE_OK ){ 456 int iRealOff = pReal->nBlob - BLOCKSIZE*((ii/BLOCKSIZE)+1) + ii%BLOCKSIZE; 457 int iRealAmt = MIN(iRem, BLOCKSIZE - (iRealOff%BLOCKSIZE)); 458 459 if( iRealOff<(pReal->nDatabase+BLOCKSIZE) ){ 460 rc = SQLITE_FULL; 461 }else{ 462 rc = pF->pMethods->xWrite(pF, &((char *)zBuf)[iBuf], iRealAmt,iRealOff); 463 ii += iRealAmt; 464 iBuf += iRealAmt; 465 iRem -= iRealAmt; 466 } 467 } 468 if( rc==SQLITE_OK ){ 469 pReal->nJournal = MAX(pReal->nJournal, iAmt+iOfst); 470 } 471 } 472 473 return rc; 474 } 475 476 /* 477 ** Truncate an fs-file. 478 */ 479 static int fsTruncate(sqlite3_file *pFile, sqlite_int64 size){ 480 fs_file *p = (fs_file *)pFile; 481 fs_real_file *pReal = p->pReal; 482 if( p->eType==DATABASE_FILE ){ 483 pReal->nDatabase = MIN(pReal->nDatabase, size); 484 }else{ 485 pReal->nJournal = MIN(pReal->nJournal, size); 486 } 487 return SQLITE_OK; 488 } 489 490 /* 491 ** Sync an fs-file. 492 */ 493 static int fsSync(sqlite3_file *pFile, int flags){ 494 fs_file *p = (fs_file *)pFile; 495 fs_real_file *pReal = p->pReal; 496 sqlite3_file *pRealFile = pReal->pFile; 497 int rc = SQLITE_OK; 498 499 if( p->eType==DATABASE_FILE ){ 500 unsigned char zSize[4]; 501 zSize[0] = (pReal->nDatabase&0xFF000000)>>24; 502 zSize[1] = (pReal->nDatabase&0x00FF0000)>>16; 503 zSize[2] = (pReal->nDatabase&0x0000FF00)>>8; 504 zSize[3] = (pReal->nDatabase&0x000000FF); 505 rc = pRealFile->pMethods->xWrite(pRealFile, zSize, 4, 0); 506 } 507 if( rc==SQLITE_OK ){ 508 rc = pRealFile->pMethods->xSync(pRealFile, flags&(~SQLITE_SYNC_DATAONLY)); 509 } 510 511 return rc; 512 } 513 514 /* 515 ** Return the current file-size of an fs-file. 516 */ 517 static int fsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 518 fs_file *p = (fs_file *)pFile; 519 fs_real_file *pReal = p->pReal; 520 if( p->eType==DATABASE_FILE ){ 521 *pSize = pReal->nDatabase; 522 }else{ 523 *pSize = pReal->nJournal; 524 } 525 return SQLITE_OK; 526 } 527 528 /* 529 ** Lock an fs-file. 530 */ 531 static int fsLock(sqlite3_file *pFile, int eLock){ 532 return SQLITE_OK; 533 } 534 535 /* 536 ** Unlock an fs-file. 537 */ 538 static int fsUnlock(sqlite3_file *pFile, int eLock){ 539 return SQLITE_OK; 540 } 541 542 /* 543 ** Check if another file-handle holds a RESERVED lock on an fs-file. 544 */ 545 static int fsCheckReservedLock(sqlite3_file *pFile, int *pResOut){ 546 *pResOut = 0; 547 return SQLITE_OK; 548 } 549 550 /* 551 ** File control method. For custom operations on an fs-file. 552 */ 553 static int fsFileControl(sqlite3_file *pFile, int op, void *pArg){ 554 return SQLITE_OK; 555 } 556 557 /* 558 ** Return the sector-size in bytes for an fs-file. 559 */ 560 static int fsSectorSize(sqlite3_file *pFile){ 561 return BLOCKSIZE; 562 } 563 564 /* 565 ** Return the device characteristic flags supported by an fs-file. 566 */ 567 static int fsDeviceCharacteristics(sqlite3_file *pFile){ 568 return 0; 569 } 570 571 /* 572 ** Open an fs file handle. 573 */ 574 static int fsOpen( 575 sqlite3_vfs *pVfs, 576 const char *zName, 577 sqlite3_file *pFile, 578 int flags, 579 int *pOutFlags 580 ){ 581 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 582 fs_file *p = (fs_file *)pFile; 583 fs_real_file *pReal = 0; 584 int eType; 585 int nName; 586 int rc = SQLITE_OK; 587 588 if( 0==(flags&(SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_MAIN_JOURNAL)) ){ 589 tmp_file *p = (tmp_file *)pFile; 590 memset(p, 0, sizeof(*p)); 591 p->base.pMethods = &tmp_io_methods; 592 return SQLITE_OK; 593 } 594 595 eType = ((flags&(SQLITE_OPEN_MAIN_DB))?DATABASE_FILE:JOURNAL_FILE); 596 p->base.pMethods = &fs_io_methods; 597 p->eType = eType; 598 599 assert(strlen("-journal")==8); 600 nName = strlen(zName)-((eType==JOURNAL_FILE)?8:0); 601 pReal=pFsVfs->pFileList; 602 for(; pReal && strncmp(pReal->zName, zName, nName); pReal=pReal->pNext); 603 604 if( !pReal ){ 605 int real_flags = (flags&~(SQLITE_OPEN_MAIN_DB))|SQLITE_OPEN_TEMP_DB; 606 sqlite3_int64 size; 607 sqlite3_file *pRealFile; 608 sqlite3_vfs *pParent = pFsVfs->pParent; 609 assert(eType==DATABASE_FILE); 610 611 pReal = (fs_real_file *)sqlite3_malloc(sizeof(*pReal)+pParent->szOsFile); 612 if( !pReal ){ 613 rc = SQLITE_NOMEM; 614 goto open_out; 615 } 616 memset(pReal, 0, sizeof(*pReal)+pParent->szOsFile); 617 pReal->zName = zName; 618 pReal->pFile = (sqlite3_file *)(&pReal[1]); 619 620 rc = pParent->xOpen(pParent, zName, pReal->pFile, real_flags, pOutFlags); 621 if( rc!=SQLITE_OK ){ 622 goto open_out; 623 } 624 pRealFile = pReal->pFile; 625 626 rc = pRealFile->pMethods->xFileSize(pRealFile, &size); 627 if( rc!=SQLITE_OK ){ 628 goto open_out; 629 } 630 if( size==0 ){ 631 rc = pRealFile->pMethods->xWrite(pRealFile, "\0", 1, BLOBSIZE-1); 632 pReal->nBlob = BLOBSIZE; 633 }else{ 634 unsigned char zS[4]; 635 pReal->nBlob = size; 636 rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, 0); 637 pReal->nDatabase = (zS[0]<<24)+(zS[1]<<16)+(zS[2]<<8)+zS[3]; 638 if( rc==SQLITE_OK ){ 639 rc = pRealFile->pMethods->xRead(pRealFile, zS, 4, pReal->nBlob-4); 640 if( zS[0] || zS[1] || zS[2] || zS[3] ){ 641 pReal->nJournal = pReal->nBlob; 642 } 643 } 644 } 645 646 if( rc==SQLITE_OK ){ 647 pReal->pNext = pFsVfs->pFileList; 648 if( pReal->pNext ){ 649 pReal->pNext->ppThis = &pReal->pNext; 650 } 651 pReal->ppThis = &pFsVfs->pFileList; 652 pFsVfs->pFileList = pReal; 653 } 654 } 655 656 open_out: 657 if( pReal ){ 658 if( rc==SQLITE_OK ){ 659 p->pReal = pReal; 660 pReal->nRef++; 661 }else{ 662 if( pReal->pFile->pMethods ){ 663 pReal->pFile->pMethods->xClose(pReal->pFile); 664 } 665 sqlite3_free(pReal); 666 } 667 } 668 return rc; 669 } 670 671 /* 672 ** Delete the file located at zPath. If the dirSync argument is true, 673 ** ensure the file-system modifications are synced to disk before 674 ** returning. 675 */ 676 static int fsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ 677 int rc = SQLITE_OK; 678 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 679 fs_real_file *pReal; 680 sqlite3_file *pF; 681 int nName = strlen(zPath) - 8; 682 683 assert(strlen("-journal")==8); 684 assert(strcmp("-journal", &zPath[nName])==0); 685 686 pReal = pFsVfs->pFileList; 687 for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); 688 if( pReal ){ 689 pF = pReal->pFile; 690 rc = pF->pMethods->xWrite(pF, "\0\0\0\0", 4, pReal->nBlob-BLOCKSIZE); 691 if( rc==SQLITE_OK ){ 692 pReal->nJournal = 0; 693 } 694 } 695 return rc; 696 } 697 698 /* 699 ** Test for access permissions. Return true if the requested permission 700 ** is available, or false otherwise. 701 */ 702 static int fsAccess( 703 sqlite3_vfs *pVfs, 704 const char *zPath, 705 int flags, 706 int *pResOut 707 ){ 708 fs_vfs_t *pFsVfs = (fs_vfs_t *)pVfs; 709 fs_real_file *pReal; 710 int isJournal = 0; 711 int nName = strlen(zPath); 712 713 if( flags!=SQLITE_ACCESS_EXISTS ){ 714 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 715 return pParent->xAccess(pParent, zPath, flags, pResOut); 716 } 717 718 assert(strlen("-journal")==8); 719 if( nName>8 && strcmp("-journal", &zPath[nName-8])==0 ){ 720 nName -= 8; 721 isJournal = 1; 722 } 723 724 pReal = pFsVfs->pFileList; 725 for(; pReal && strncmp(pReal->zName, zPath, nName); pReal=pReal->pNext); 726 727 *pResOut = (pReal && (!isJournal || pReal->nJournal>0)); 728 return SQLITE_OK; 729 } 730 731 /* 732 ** Populate buffer zOut with the full canonical pathname corresponding 733 ** to the pathname in zPath. zOut is guaranteed to point to a buffer 734 ** of at least (FS_MAX_PATHNAME+1) bytes. 735 */ 736 static int fsFullPathname( 737 sqlite3_vfs *pVfs, /* Pointer to vfs object */ 738 const char *zPath, /* Possibly relative input path */ 739 int nOut, /* Size of output buffer in bytes */ 740 char *zOut /* Output buffer */ 741 ){ 742 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 743 return pParent->xFullPathname(pParent, zPath, nOut, zOut); 744 } 745 746 /* 747 ** Open the dynamic library located at zPath and return a handle. 748 */ 749 static void *fsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ 750 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 751 return pParent->xDlOpen(pParent, zPath); 752 } 753 754 /* 755 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable 756 ** utf-8 string describing the most recent error encountered associated 757 ** with dynamic libraries. 758 */ 759 static void fsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ 760 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 761 pParent->xDlError(pParent, nByte, zErrMsg); 762 } 763 764 /* 765 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle. 766 */ 767 static void (*fsDlSym(sqlite3_vfs *pVfs, void *pH, const char *zSym))(void){ 768 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 769 return pParent->xDlSym(pParent, pH, zSym); 770 } 771 772 /* 773 ** Close the dynamic library handle pHandle. 774 */ 775 static void fsDlClose(sqlite3_vfs *pVfs, void *pHandle){ 776 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 777 pParent->xDlClose(pParent, pHandle); 778 } 779 780 /* 781 ** Populate the buffer pointed to by zBufOut with nByte bytes of 782 ** random data. 783 */ 784 static int fsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ 785 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 786 return pParent->xRandomness(pParent, nByte, zBufOut); 787 } 788 789 /* 790 ** Sleep for nMicro microseconds. Return the number of microseconds 791 ** actually slept. 792 */ 793 static int fsSleep(sqlite3_vfs *pVfs, int nMicro){ 794 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 795 return pParent->xSleep(pParent, nMicro); 796 } 797 798 /* 799 ** Return the current time as a Julian Day number in *pTimeOut. 800 */ 801 static int fsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ 802 sqlite3_vfs *pParent = ((fs_vfs_t *)pVfs)->pParent; 803 return pParent->xCurrentTime(pParent, pTimeOut); 804 } 805 806 /* 807 ** This procedure registers the fs vfs with SQLite. If the argument is 808 ** true, the fs vfs becomes the new default vfs. It is the only publicly 809 ** available function in this file. 810 */ 811 int fs_register(void){ 812 if( fs_vfs.pParent ) return SQLITE_OK; 813 fs_vfs.pParent = sqlite3_vfs_find(0); 814 fs_vfs.base.mxPathname = fs_vfs.pParent->mxPathname; 815 fs_vfs.base.szOsFile = MAX(sizeof(tmp_file), sizeof(fs_file)); 816 return sqlite3_vfs_register(&fs_vfs.base, 0); 817 } 818 819 #ifdef SQLITE_TEST 820 int SqlitetestOnefile_Init() {return fs_register();} 821 #endif 822