| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 #endif | 421 #endif |
| 422 | 422 |
| 423 /* | 423 /* |
| 424 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method | 424 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method |
| 425 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. | 425 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. |
| 426 ** This could conceivably cause corruption following a power failure on | 426 ** This could conceivably cause corruption following a power failure on |
| 427 ** such a system. This is currently an undocumented limit. | 427 ** such a system. This is currently an undocumented limit. |
| 428 */ | 428 */ |
| 429 #define MAX_SECTOR_SIZE 0x10000 | 429 #define MAX_SECTOR_SIZE 0x10000 |
| 430 | 430 |
| 431 |
| 431 /* | 432 /* |
| 432 ** An instance of the following structure is allocated for each active | 433 ** An instance of the following structure is allocated for each active |
| 433 ** savepoint and statement transaction in the system. All such structures | 434 ** savepoint and statement transaction in the system. All such structures |
| 434 ** are stored in the Pager.aSavepoint[] array, which is allocated and | 435 ** are stored in the Pager.aSavepoint[] array, which is allocated and |
| 435 ** resized using sqlite3Realloc(). | 436 ** resized using sqlite3Realloc(). |
| 436 ** | 437 ** |
| 437 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is | 438 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is |
| 438 ** set to 0. If a journal-header is written into the main journal while | 439 ** set to 0. If a journal-header is written into the main journal while |
| 439 ** the savepoint is active, then iHdrOffset is set to the byte offset | 440 ** the savepoint is active, then iHdrOffset is set to the byte offset |
| 440 ** immediately following the last journal record written into the main | 441 ** immediately following the last journal record written into the main |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX | 617 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX |
| 617 ** sub-codes. | 618 ** sub-codes. |
| 618 */ | 619 */ |
| 619 struct Pager { | 620 struct Pager { |
| 620 sqlite3_vfs *pVfs; /* OS functions to use for IO */ | 621 sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
| 621 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ | 622 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
| 622 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ | 623 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ |
| 623 u8 useJournal; /* Use a rollback journal on this file */ | 624 u8 useJournal; /* Use a rollback journal on this file */ |
| 624 u8 noSync; /* Do not sync the journal if true */ | 625 u8 noSync; /* Do not sync the journal if true */ |
| 625 u8 fullSync; /* Do extra syncs of the journal for robustness */ | 626 u8 fullSync; /* Do extra syncs of the journal for robustness */ |
| 627 u8 extraSync; /* sync directory after journal delete */ |
| 626 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ | 628 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */ |
| 627 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */ | 629 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */ |
| 628 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ | 630 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */ |
| 629 u8 tempFile; /* zFilename is a temporary or immutable file */ | 631 u8 tempFile; /* zFilename is a temporary or immutable file */ |
| 630 u8 noLock; /* Do not lock (except in WAL mode) */ | 632 u8 noLock; /* Do not lock (except in WAL mode) */ |
| 631 u8 readOnly; /* True for a read-only database */ | 633 u8 readOnly; /* True for a read-only database */ |
| 632 u8 memDb; /* True to inhibit all file I/O */ | 634 u8 memDb; /* True to inhibit all file I/O */ |
| 633 | 635 |
| 634 /************************************************************************** | 636 /************************************************************************** |
| 635 ** The following block contains those class members that change during | 637 ** The following block contains those class members that change during |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 i64 journalSizeLimit; /* Size limit for persistent journal files */ | 686 i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 685 char *zFilename; /* Name of the database file */ | 687 char *zFilename; /* Name of the database file */ |
| 686 char *zJournal; /* Name of the journal file */ | 688 char *zJournal; /* Name of the journal file */ |
| 687 int (*xBusyHandler)(void*); /* Function to call when busy */ | 689 int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 688 void *pBusyHandlerArg; /* Context argument for xBusyHandler */ | 690 void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 689 int aStat[3]; /* Total cache hits, misses and writes */ | 691 int aStat[3]; /* Total cache hits, misses and writes */ |
| 690 #ifdef SQLITE_TEST | 692 #ifdef SQLITE_TEST |
| 691 int nRead; /* Database pages read */ | 693 int nRead; /* Database pages read */ |
| 692 #endif | 694 #endif |
| 693 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ | 695 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ |
| 696 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ |
| 694 #ifdef SQLITE_HAS_CODEC | 697 #ifdef SQLITE_HAS_CODEC |
| 695 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ | 698 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ |
| 696 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ | 699 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ |
| 697 void (*xCodecFree)(void*); /* Destructor for the codec */ | 700 void (*xCodecFree)(void*); /* Destructor for the codec */ |
| 698 void *pCodec; /* First argument to xCodec... methods */ | 701 void *pCodec; /* First argument to xCodec... methods */ |
| 699 #endif | 702 #endif |
| 700 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ | 703 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
| 701 PCache *pPCache; /* Pointer to page cache object */ | 704 PCache *pPCache; /* Pointer to page cache object */ |
| 702 #ifndef SQLITE_OMIT_WAL | 705 #ifndef SQLITE_OMIT_WAL |
| 703 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ | 706 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 ** | 807 ** |
| 805 ** if( isOpen(pPager->jfd) ){ ... | 808 ** if( isOpen(pPager->jfd) ){ ... |
| 806 ** | 809 ** |
| 807 ** instead of | 810 ** instead of |
| 808 ** | 811 ** |
| 809 ** if( pPager->jfd->pMethods ){ ... | 812 ** if( pPager->jfd->pMethods ){ ... |
| 810 */ | 813 */ |
| 811 #define isOpen(pFd) ((pFd)->pMethods!=0) | 814 #define isOpen(pFd) ((pFd)->pMethods!=0) |
| 812 | 815 |
| 813 /* | 816 /* |
| 814 ** Return true if this pager uses a write-ahead log instead of the usual | 817 ** Return true if this pager uses a write-ahead log to read page pgno. |
| 815 ** rollback journal. Otherwise false. | 818 ** Return false if the pager reads pgno directly from the database. |
| 816 */ | 819 */ |
| 820 #if !defined(SQLITE_OMIT_WAL) && defined(SQLITE_DIRECT_OVERFLOW_READ) |
| 821 int sqlite3PagerUseWal(Pager *pPager, Pgno pgno){ |
| 822 u32 iRead = 0; |
| 823 int rc; |
| 824 if( pPager->pWal==0 ) return 0; |
| 825 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead); |
| 826 return rc || iRead; |
| 827 } |
| 828 #endif |
| 817 #ifndef SQLITE_OMIT_WAL | 829 #ifndef SQLITE_OMIT_WAL |
| 818 static int pagerUseWal(Pager *pPager){ | 830 # define pagerUseWal(x) ((x)->pWal!=0) |
| 819 return (pPager->pWal!=0); | |
| 820 } | |
| 821 #else | 831 #else |
| 822 # define pagerUseWal(x) 0 | 832 # define pagerUseWal(x) 0 |
| 823 # define pagerRollbackWal(x) 0 | 833 # define pagerRollbackWal(x) 0 |
| 824 # define pagerWalFrames(v,w,x,y) 0 | 834 # define pagerWalFrames(v,w,x,y) 0 |
| 825 # define pagerOpenWalIfPresent(z) SQLITE_OK | 835 # define pagerOpenWalIfPresent(z) SQLITE_OK |
| 826 # define pagerBeginReadTransaction(z) SQLITE_OK | 836 # define pagerBeginReadTransaction(z) SQLITE_OK |
| 827 #endif | 837 #endif |
| 828 | 838 |
| 829 #ifndef NDEBUG | 839 #ifndef NDEBUG |
| 830 /* | 840 /* |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 | 873 |
| 864 /* Check that MEMDB implies noSync. And an in-memory journal. Since | 874 /* Check that MEMDB implies noSync. And an in-memory journal. Since |
| 865 ** this means an in-memory pager performs no IO at all, it cannot encounter | 875 ** this means an in-memory pager performs no IO at all, it cannot encounter |
| 866 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing | 876 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing |
| 867 ** a journal file. (although the in-memory journal implementation may | 877 ** a journal file. (although the in-memory journal implementation may |
| 868 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It | 878 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It |
| 869 ** is therefore not possible for an in-memory pager to enter the ERROR | 879 ** is therefore not possible for an in-memory pager to enter the ERROR |
| 870 ** state. | 880 ** state. |
| 871 */ | 881 */ |
| 872 if( MEMDB ){ | 882 if( MEMDB ){ |
| 883 assert( !isOpen(p->fd) ); |
| 873 assert( p->noSync ); | 884 assert( p->noSync ); |
| 874 assert( p->journalMode==PAGER_JOURNALMODE_OFF | 885 assert( p->journalMode==PAGER_JOURNALMODE_OFF |
| 875 || p->journalMode==PAGER_JOURNALMODE_MEMORY | 886 || p->journalMode==PAGER_JOURNALMODE_MEMORY |
| 876 ); | 887 ); |
| 877 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); | 888 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); |
| 878 assert( pagerUseWal(p)==0 ); | 889 assert( pagerUseWal(p)==0 ); |
| 879 } | 890 } |
| 880 | 891 |
| 881 /* If changeCountDone is set, a RESERVED lock or greater must be held | 892 /* If changeCountDone is set, a RESERVED lock or greater must be held |
| 882 ** on the file. | 893 ** on the file. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 949 || p->journalMode==PAGER_JOURNALMODE_WAL | 960 || p->journalMode==PAGER_JOURNALMODE_WAL |
| 950 ); | 961 ); |
| 951 break; | 962 break; |
| 952 | 963 |
| 953 case PAGER_ERROR: | 964 case PAGER_ERROR: |
| 954 /* There must be at least one outstanding reference to the pager if | 965 /* There must be at least one outstanding reference to the pager if |
| 955 ** in ERROR state. Otherwise the pager should have already dropped | 966 ** in ERROR state. Otherwise the pager should have already dropped |
| 956 ** back to OPEN state. | 967 ** back to OPEN state. |
| 957 */ | 968 */ |
| 958 assert( pPager->errCode!=SQLITE_OK ); | 969 assert( pPager->errCode!=SQLITE_OK ); |
| 959 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); | 970 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile ); |
| 960 break; | 971 break; |
| 961 } | 972 } |
| 962 | 973 |
| 963 return 1; | 974 return 1; |
| 964 } | 975 } |
| 965 #endif /* ifndef NDEBUG */ | 976 #endif /* ifndef NDEBUG */ |
| 966 | 977 |
| 967 #ifdef SQLITE_DEBUG | 978 #ifdef SQLITE_DEBUG |
| 968 /* | 979 /* |
| 969 ** Return a pointer to a human readable string in a static buffer | 980 ** Return a pointer to a human readable string in a static buffer |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1008 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?" | 1019 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?" |
| 1009 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal | 1020 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal |
| 1010 , p->journalOff, p->journalHdr | 1021 , p->journalOff, p->journalHdr |
| 1011 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize | 1022 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize |
| 1012 ); | 1023 ); |
| 1013 | 1024 |
| 1014 return zRet; | 1025 return zRet; |
| 1015 } | 1026 } |
| 1016 #endif | 1027 #endif |
| 1017 | 1028 |
| 1029 /* Forward references to the various page getters */ |
| 1030 static int getPageNormal(Pager*,Pgno,DbPage**,int); |
| 1031 static int getPageError(Pager*,Pgno,DbPage**,int); |
| 1032 #if SQLITE_MAX_MMAP_SIZE>0 |
| 1033 static int getPageMMap(Pager*,Pgno,DbPage**,int); |
| 1034 #endif |
| 1035 |
| 1036 /* |
| 1037 ** Set the Pager.xGet method for the appropriate routine used to fetch |
| 1038 ** content from the pager. |
| 1039 */ |
| 1040 static void setGetterMethod(Pager *pPager){ |
| 1041 if( pPager->errCode ){ |
| 1042 pPager->xGet = getPageError; |
| 1043 #if SQLITE_MAX_MMAP_SIZE>0 |
| 1044 }else if( USEFETCH(pPager) |
| 1045 #ifdef SQLITE_HAS_CODEC |
| 1046 && pPager->xCodec==0 |
| 1047 #endif |
| 1048 ){ |
| 1049 pPager->xGet = getPageMMap; |
| 1050 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
| 1051 }else{ |
| 1052 pPager->xGet = getPageNormal; |
| 1053 } |
| 1054 } |
| 1055 |
| 1018 /* | 1056 /* |
| 1019 ** Return true if it is necessary to write page *pPg into the sub-journal. | 1057 ** Return true if it is necessary to write page *pPg into the sub-journal. |
| 1020 ** A page needs to be written into the sub-journal if there exists one | 1058 ** A page needs to be written into the sub-journal if there exists one |
| 1021 ** or more open savepoints for which: | 1059 ** or more open savepoints for which: |
| 1022 ** | 1060 ** |
| 1023 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and | 1061 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and |
| 1024 ** * The bit corresponding to the page-number is not set in | 1062 ** * The bit corresponding to the page-number is not set in |
| 1025 ** PagerSavepoint.pInSavepoint. | 1063 ** PagerSavepoint.pInSavepoint. |
| 1026 */ | 1064 */ |
| 1027 static int subjRequiresPage(PgHdr *pPg){ | 1065 static int subjRequiresPage(PgHdr *pPg){ |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1161 | 1199 |
| 1162 assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); | 1200 assert(SQLITE_IOCAP_ATOMIC512==(512>>8)); |
| 1163 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); | 1201 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8)); |
| 1164 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){ | 1202 if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){ |
| 1165 return 0; | 1203 return 0; |
| 1166 } | 1204 } |
| 1167 } | 1205 } |
| 1168 | 1206 |
| 1169 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); | 1207 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager); |
| 1170 } | 1208 } |
| 1209 #else |
| 1210 # define jrnlBufferSize(x) 0 |
| 1171 #endif | 1211 #endif |
| 1172 | 1212 |
| 1173 /* | 1213 /* |
| 1174 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking | 1214 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking |
| 1175 ** on the cache using a hash function. This is used for testing | 1215 ** on the cache using a hash function. This is used for testing |
| 1176 ** and debugging only. | 1216 ** and debugging only. |
| 1177 */ | 1217 */ |
| 1178 #ifdef SQLITE_CHECK_PAGES | 1218 #ifdef SQLITE_CHECK_PAGES |
| 1179 /* | 1219 /* |
| 1180 ** Return a 32-bit hash of the page data for pPage. | 1220 ** Return a 32-bit hash of the page data for pPage. |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1321 ** journal file in bytes is larger than this value, then truncate the | 1361 ** journal file in bytes is larger than this value, then truncate the |
| 1322 ** journal file to Pager.journalSizeLimit bytes. The journal file does | 1362 ** journal file to Pager.journalSizeLimit bytes. The journal file does |
| 1323 ** not need to be synced following this operation. | 1363 ** not need to be synced following this operation. |
| 1324 ** | 1364 ** |
| 1325 ** If an IO error occurs, abandon processing and return the IO error code. | 1365 ** If an IO error occurs, abandon processing and return the IO error code. |
| 1326 ** Otherwise, return SQLITE_OK. | 1366 ** Otherwise, return SQLITE_OK. |
| 1327 */ | 1367 */ |
| 1328 static int zeroJournalHdr(Pager *pPager, int doTruncate){ | 1368 static int zeroJournalHdr(Pager *pPager, int doTruncate){ |
| 1329 int rc = SQLITE_OK; /* Return code */ | 1369 int rc = SQLITE_OK; /* Return code */ |
| 1330 assert( isOpen(pPager->jfd) ); | 1370 assert( isOpen(pPager->jfd) ); |
| 1371 assert( !sqlite3JournalIsInMemory(pPager->jfd) ); |
| 1331 if( pPager->journalOff ){ | 1372 if( pPager->journalOff ){ |
| 1332 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ | 1373 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */ |
| 1333 | 1374 |
| 1334 IOTRACE(("JZEROHDR %p\n", pPager)) | 1375 IOTRACE(("JZEROHDR %p\n", pPager)) |
| 1335 if( doTruncate || iLimit==0 ){ | 1376 if( doTruncate || iLimit==0 ){ |
| 1336 rc = sqlite3OsTruncate(pPager->jfd, 0); | 1377 rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 1337 }else{ | 1378 }else{ |
| 1338 static const char zeroHdr[28] = {0}; | 1379 static const char zeroHdr[28] = {0}; |
| 1339 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); | 1380 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0); |
| 1340 } | 1381 } |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1702 /* | 1743 /* |
| 1703 ** Free all structures in the Pager.aSavepoint[] array and set both | 1744 ** Free all structures in the Pager.aSavepoint[] array and set both |
| 1704 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal | 1745 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal |
| 1705 ** if it is open and the pager is not in exclusive mode. | 1746 ** if it is open and the pager is not in exclusive mode. |
| 1706 */ | 1747 */ |
| 1707 static void releaseAllSavepoints(Pager *pPager){ | 1748 static void releaseAllSavepoints(Pager *pPager){ |
| 1708 int ii; /* Iterator for looping through Pager.aSavepoint */ | 1749 int ii; /* Iterator for looping through Pager.aSavepoint */ |
| 1709 for(ii=0; ii<pPager->nSavepoint; ii++){ | 1750 for(ii=0; ii<pPager->nSavepoint; ii++){ |
| 1710 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); | 1751 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); |
| 1711 } | 1752 } |
| 1712 if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){ | 1753 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){ |
| 1713 sqlite3OsClose(pPager->sjfd); | 1754 sqlite3OsClose(pPager->sjfd); |
| 1714 } | 1755 } |
| 1715 sqlite3_free(pPager->aSavepoint); | 1756 sqlite3_free(pPager->aSavepoint); |
| 1716 pPager->aSavepoint = 0; | 1757 pPager->aSavepoint = 0; |
| 1717 pPager->nSavepoint = 0; | 1758 pPager->nSavepoint = 0; |
| 1718 pPager->nSubRec = 0; | 1759 pPager->nSubRec = 0; |
| 1719 } | 1760 } |
| 1720 | 1761 |
| 1721 /* | 1762 /* |
| 1722 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint | 1763 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1808 assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); | 1849 assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); |
| 1809 pPager->changeCountDone = 0; | 1850 pPager->changeCountDone = 0; |
| 1810 pPager->eState = PAGER_OPEN; | 1851 pPager->eState = PAGER_OPEN; |
| 1811 } | 1852 } |
| 1812 | 1853 |
| 1813 /* If Pager.errCode is set, the contents of the pager cache cannot be | 1854 /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 1814 ** trusted. Now that there are no outstanding references to the pager, | 1855 ** trusted. Now that there are no outstanding references to the pager, |
| 1815 ** it can safely move back to PAGER_OPEN state. This happens in both | 1856 ** it can safely move back to PAGER_OPEN state. This happens in both |
| 1816 ** normal and exclusive-locking mode. | 1857 ** normal and exclusive-locking mode. |
| 1817 */ | 1858 */ |
| 1859 assert( pPager->errCode==SQLITE_OK || !MEMDB ); |
| 1818 if( pPager->errCode ){ | 1860 if( pPager->errCode ){ |
| 1819 assert( !MEMDB ); | 1861 if( pPager->tempFile==0 ){ |
| 1820 pager_reset(pPager); | 1862 pager_reset(pPager); |
| 1821 pPager->changeCountDone = pPager->tempFile; | 1863 pPager->changeCountDone = 0; |
| 1822 pPager->eState = PAGER_OPEN; | 1864 pPager->eState = PAGER_OPEN; |
| 1865 }else{ |
| 1866 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER); |
| 1867 } |
| 1868 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); |
| 1823 pPager->errCode = SQLITE_OK; | 1869 pPager->errCode = SQLITE_OK; |
| 1824 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); | 1870 setGetterMethod(pPager); |
| 1825 } | 1871 } |
| 1826 | 1872 |
| 1827 pPager->journalOff = 0; | 1873 pPager->journalOff = 0; |
| 1828 pPager->journalHdr = 0; | 1874 pPager->journalHdr = 0; |
| 1829 pPager->setMaster = 0; | 1875 pPager->setMaster = 0; |
| 1830 } | 1876 } |
| 1831 | 1877 |
| 1832 /* | 1878 /* |
| 1833 ** This function is called whenever an IOERR or FULL error that requires | 1879 ** This function is called whenever an IOERR or FULL error that requires |
| 1834 ** the pager to transition into the ERROR state may ahve occurred. | 1880 ** the pager to transition into the ERROR state may ahve occurred. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1852 int rc2 = rc & 0xff; | 1898 int rc2 = rc & 0xff; |
| 1853 assert( rc==SQLITE_OK || !MEMDB ); | 1899 assert( rc==SQLITE_OK || !MEMDB ); |
| 1854 assert( | 1900 assert( |
| 1855 pPager->errCode==SQLITE_FULL || | 1901 pPager->errCode==SQLITE_FULL || |
| 1856 pPager->errCode==SQLITE_OK || | 1902 pPager->errCode==SQLITE_OK || |
| 1857 (pPager->errCode & 0xff)==SQLITE_IOERR | 1903 (pPager->errCode & 0xff)==SQLITE_IOERR |
| 1858 ); | 1904 ); |
| 1859 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ | 1905 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ |
| 1860 pPager->errCode = rc; | 1906 pPager->errCode = rc; |
| 1861 pPager->eState = PAGER_ERROR; | 1907 pPager->eState = PAGER_ERROR; |
| 1908 setGetterMethod(pPager); |
| 1862 } | 1909 } |
| 1863 return rc; | 1910 return rc; |
| 1864 } | 1911 } |
| 1865 | 1912 |
| 1866 static int pager_truncate(Pager *pPager, Pgno nPage); | 1913 static int pager_truncate(Pager *pPager, Pgno nPage); |
| 1867 | 1914 |
| 1868 /* | 1915 /* |
| 1916 ** The write transaction open on pPager is being committed (bCommit==1) |
| 1917 ** or rolled back (bCommit==0). |
| 1918 ** |
| 1919 ** Return TRUE if and only if all dirty pages should be flushed to disk. |
| 1920 ** |
| 1921 ** Rules: |
| 1922 ** |
| 1923 ** * For non-TEMP databases, always sync to disk. This is necessary |
| 1924 ** for transactions to be durable. |
| 1925 ** |
| 1926 ** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing |
| 1927 ** file has been created already (via a spill on pagerStress()) and |
| 1928 ** when the number of dirty pages in memory exceeds 25% of the total |
| 1929 ** cache size. |
| 1930 */ |
| 1931 static int pagerFlushOnCommit(Pager *pPager, int bCommit){ |
| 1932 if( pPager->tempFile==0 ) return 1; |
| 1933 if( !bCommit ) return 0; |
| 1934 if( !isOpen(pPager->fd) ) return 0; |
| 1935 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25); |
| 1936 } |
| 1937 |
| 1938 /* |
| 1869 ** This routine ends a transaction. A transaction is usually ended by | 1939 ** This routine ends a transaction. A transaction is usually ended by |
| 1870 ** either a COMMIT or a ROLLBACK operation. This routine may be called | 1940 ** either a COMMIT or a ROLLBACK operation. This routine may be called |
| 1871 ** after rollback of a hot-journal, or if an error occurs while opening | 1941 ** after rollback of a hot-journal, or if an error occurs while opening |
| 1872 ** the journal file or writing the very first journal-header of a | 1942 ** the journal file or writing the very first journal-header of a |
| 1873 ** database transaction. | 1943 ** database transaction. |
| 1874 ** | 1944 ** |
| 1875 ** This routine is never called in PAGER_ERROR state. If it is called | 1945 ** This routine is never called in PAGER_ERROR state. If it is called |
| 1876 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less | 1946 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less |
| 1877 ** exclusive than a RESERVED lock, it is a no-op. | 1947 ** exclusive than a RESERVED lock, it is a no-op. |
| 1878 ** | 1948 ** |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1940 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){ | 2010 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){ |
| 1941 return SQLITE_OK; | 2011 return SQLITE_OK; |
| 1942 } | 2012 } |
| 1943 | 2013 |
| 1944 releaseAllSavepoints(pPager); | 2014 releaseAllSavepoints(pPager); |
| 1945 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); | 2015 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); |
| 1946 if( isOpen(pPager->jfd) ){ | 2016 if( isOpen(pPager->jfd) ){ |
| 1947 assert( !pagerUseWal(pPager) ); | 2017 assert( !pagerUseWal(pPager) ); |
| 1948 | 2018 |
| 1949 /* Finalize the journal file. */ | 2019 /* Finalize the journal file. */ |
| 1950 if( sqlite3IsMemJournal(pPager->jfd) ){ | 2020 if( sqlite3JournalIsInMemory(pPager->jfd) ){ |
| 1951 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); | 2021 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */ |
| 1952 sqlite3OsClose(pPager->jfd); | 2022 sqlite3OsClose(pPager->jfd); |
| 1953 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ | 2023 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){ |
| 1954 if( pPager->journalOff==0 ){ | 2024 if( pPager->journalOff==0 ){ |
| 1955 rc = SQLITE_OK; | 2025 rc = SQLITE_OK; |
| 1956 }else{ | 2026 }else{ |
| 1957 rc = sqlite3OsTruncate(pPager->jfd, 0); | 2027 rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 1958 if( rc==SQLITE_OK && pPager->fullSync ){ | 2028 if( rc==SQLITE_OK && pPager->fullSync ){ |
| 1959 /* Make sure the new file size is written into the inode right away. | 2029 /* Make sure the new file size is written into the inode right away. |
| 1960 ** Otherwise the journal might resurrect following a power loss and | 2030 ** Otherwise the journal might resurrect following a power loss and |
| 1961 ** cause the last transaction to roll back. See | 2031 ** cause the last transaction to roll back. See |
| 1962 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773 | 2032 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773 |
| 1963 */ | 2033 */ |
| 1964 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); | 2034 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags); |
| 1965 } | 2035 } |
| 1966 } | 2036 } |
| 1967 pPager->journalOff = 0; | 2037 pPager->journalOff = 0; |
| 1968 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST | 2038 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 1969 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) | 2039 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) |
| 1970 ){ | 2040 ){ |
| 1971 rc = zeroJournalHdr(pPager, hasMaster); | 2041 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile); |
| 1972 pPager->journalOff = 0; | 2042 pPager->journalOff = 0; |
| 1973 }else{ | 2043 }else{ |
| 1974 /* This branch may be executed with Pager.journalMode==MEMORY if | 2044 /* This branch may be executed with Pager.journalMode==MEMORY if |
| 1975 ** a hot-journal was just rolled back. In this case the journal | 2045 ** a hot-journal was just rolled back. In this case the journal |
| 1976 ** file should be closed and deleted. If this connection writes to | 2046 ** file should be closed and deleted. If this connection writes to |
| 1977 ** the database file, it will do so using an in-memory journal. | 2047 ** the database file, it will do so using an in-memory journal. |
| 1978 */ | 2048 */ |
| 1979 int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd)); | 2049 int bDelete = !pPager->tempFile; |
| 2050 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); |
| 1980 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE | 2051 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE |
| 1981 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY | 2052 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY |
| 1982 || pPager->journalMode==PAGER_JOURNALMODE_WAL | 2053 || pPager->journalMode==PAGER_JOURNALMODE_WAL |
| 1983 ); | 2054 ); |
| 1984 sqlite3OsClose(pPager->jfd); | 2055 sqlite3OsClose(pPager->jfd); |
| 1985 if( bDelete ){ | 2056 if( bDelete ){ |
| 1986 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); | 2057 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync); |
| 1987 } | 2058 } |
| 1988 } | 2059 } |
| 1989 } | 2060 } |
| 1990 | 2061 |
| 1991 #ifdef SQLITE_CHECK_PAGES | 2062 #ifdef SQLITE_CHECK_PAGES |
| 1992 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); | 2063 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
| 1993 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ | 2064 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ |
| 1994 PgHdr *p = sqlite3PagerLookup(pPager, 1); | 2065 PgHdr *p = sqlite3PagerLookup(pPager, 1); |
| 1995 if( p ){ | 2066 if( p ){ |
| 1996 p->pageHash = 0; | 2067 p->pageHash = 0; |
| 1997 sqlite3PagerUnrefNotNull(p); | 2068 sqlite3PagerUnrefNotNull(p); |
| 1998 } | 2069 } |
| 1999 } | 2070 } |
| 2000 #endif | 2071 #endif |
| 2001 | 2072 |
| 2002 sqlite3BitvecDestroy(pPager->pInJournal); | 2073 sqlite3BitvecDestroy(pPager->pInJournal); |
| 2003 pPager->pInJournal = 0; | 2074 pPager->pInJournal = 0; |
| 2004 pPager->nRec = 0; | 2075 pPager->nRec = 0; |
| 2005 sqlite3PcacheCleanAll(pPager->pPCache); | 2076 if( rc==SQLITE_OK ){ |
| 2006 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); | 2077 if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){ |
| 2078 sqlite3PcacheCleanAll(pPager->pPCache); |
| 2079 }else{ |
| 2080 sqlite3PcacheClearWritable(pPager->pPCache); |
| 2081 } |
| 2082 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); |
| 2083 } |
| 2007 | 2084 |
| 2008 if( pagerUseWal(pPager) ){ | 2085 if( pagerUseWal(pPager) ){ |
| 2009 /* Drop the WAL write-lock, if any. Also, if the connection was in | 2086 /* Drop the WAL write-lock, if any. Also, if the connection was in |
| 2010 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE | 2087 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE |
| 2011 ** lock held on the database file. | 2088 ** lock held on the database file. |
| 2012 */ | 2089 */ |
| 2013 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); | 2090 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); |
| 2014 assert( rc2==SQLITE_OK ); | 2091 assert( rc2==SQLITE_OK ); |
| 2015 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ | 2092 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){ |
| 2016 /* This branch is taken when committing a transaction in rollback-journal | 2093 /* This branch is taken when committing a transaction in rollback-journal |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2280 ** 2008-04-14: When attempting to vacuum a corrupt database file, it | 2357 ** 2008-04-14: When attempting to vacuum a corrupt database file, it |
| 2281 ** is possible to fail a statement on a database that does not yet exist. | 2358 ** is possible to fail a statement on a database that does not yet exist. |
| 2282 ** Do not attempt to write if database file has never been opened. | 2359 ** Do not attempt to write if database file has never been opened. |
| 2283 */ | 2360 */ |
| 2284 if( pagerUseWal(pPager) ){ | 2361 if( pagerUseWal(pPager) ){ |
| 2285 pPg = 0; | 2362 pPg = 0; |
| 2286 }else{ | 2363 }else{ |
| 2287 pPg = sqlite3PagerLookup(pPager, pgno); | 2364 pPg = sqlite3PagerLookup(pPager, pgno); |
| 2288 } | 2365 } |
| 2289 assert( pPg || !MEMDB ); | 2366 assert( pPg || !MEMDB ); |
| 2290 assert( pPager->eState!=PAGER_OPEN || pPg==0 ); | 2367 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile ); |
| 2291 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", | 2368 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", |
| 2292 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), | 2369 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), |
| 2293 (isMainJrnl?"main-journal":"sub-journal") | 2370 (isMainJrnl?"main-journal":"sub-journal") |
| 2294 )); | 2371 )); |
| 2295 if( isMainJrnl ){ | 2372 if( isMainJrnl ){ |
| 2296 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); | 2373 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); |
| 2297 }else{ | 2374 }else{ |
| 2298 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); | 2375 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); |
| 2299 } | 2376 } |
| 2300 if( isOpen(pPager->fd) | 2377 if( isOpen(pPager->fd) |
| 2301 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) | 2378 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 2302 && isSynced | 2379 && isSynced |
| 2303 ){ | 2380 ){ |
| 2304 i64 ofst = (pgno-1)*(i64)pPager->pageSize; | 2381 i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 2305 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); | 2382 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); |
| 2306 assert( !pagerUseWal(pPager) ); | 2383 assert( !pagerUseWal(pPager) ); |
| 2307 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); | 2384 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); |
| 2308 if( pgno>pPager->dbFileSize ){ | 2385 if( pgno>pPager->dbFileSize ){ |
| 2309 pPager->dbFileSize = pgno; | 2386 pPager->dbFileSize = pgno; |
| 2310 } | 2387 } |
| 2311 if( pPager->pBackup ){ | 2388 if( pPager->pBackup ){ |
| 2312 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM); | 2389 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); |
| 2313 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); | 2390 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); |
| 2314 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData); | 2391 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData); |
| 2315 } | 2392 } |
| 2316 }else if( !isMainJrnl && pPg==0 ){ | 2393 }else if( !isMainJrnl && pPg==0 ){ |
| 2317 /* If this is a rollback of a savepoint and data was not written to | 2394 /* If this is a rollback of a savepoint and data was not written to |
| 2318 ** the database and the page is not in-memory, there is a potential | 2395 ** the database and the page is not in-memory, there is a potential |
| 2319 ** problem. When the page is next fetched by the b-tree layer, it | 2396 ** problem. When the page is next fetched by the b-tree layer, it |
| 2320 ** will be read from the database file, which may or may not be | 2397 ** will be read from the database file, which may or may not be |
| 2321 ** current. | 2398 ** current. |
| 2322 ** | 2399 ** |
| 2323 ** There are a couple of different ways this can happen. All are quite | 2400 ** There are a couple of different ways this can happen. All are quite |
| 2324 ** obscure. When running in synchronous mode, this can only happen | 2401 ** obscure. When running in synchronous mode, this can only happen |
| 2325 ** if the page is on the free-list at the start of the transaction, then | 2402 ** if the page is on the free-list at the start of the transaction, then |
| 2326 ** populated, then moved using sqlite3PagerMovepage(). | 2403 ** populated, then moved using sqlite3PagerMovepage(). |
| 2327 ** | 2404 ** |
| 2328 ** The solution is to add an in-memory page to the cache containing | 2405 ** The solution is to add an in-memory page to the cache containing |
| 2329 ** the data just read from the sub-journal. Mark the page as dirty | 2406 ** the data just read from the sub-journal. Mark the page as dirty |
| 2330 ** and if the pager requires a journal-sync, then mark the page as | 2407 ** and if the pager requires a journal-sync, then mark the page as |
| 2331 ** requiring a journal-sync before it is written. | 2408 ** requiring a journal-sync before it is written. |
| 2332 */ | 2409 */ |
| 2333 assert( isSavepnt ); | 2410 assert( isSavepnt ); |
| 2334 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 ); | 2411 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 ); |
| 2335 pPager->doNotSpill |= SPILLFLAG_ROLLBACK; | 2412 pPager->doNotSpill |= SPILLFLAG_ROLLBACK; |
| 2336 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1); | 2413 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1); |
| 2337 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 ); | 2414 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 ); |
| 2338 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK; | 2415 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK; |
| 2339 if( rc!=SQLITE_OK ) return rc; | 2416 if( rc!=SQLITE_OK ) return rc; |
| 2340 pPg->flags &= ~PGHDR_NEED_READ; | |
| 2341 sqlite3PcacheMakeDirty(pPg); | 2417 sqlite3PcacheMakeDirty(pPg); |
| 2342 } | 2418 } |
| 2343 if( pPg ){ | 2419 if( pPg ){ |
| 2344 /* No page should ever be explicitly rolled back that is in use, except | 2420 /* No page should ever be explicitly rolled back that is in use, except |
| 2345 ** for page 1 which is held in use in order to keep the lock on the | 2421 ** for page 1 which is held in use in order to keep the lock on the |
| 2346 ** database active. However such a page may be rolled back as a result | 2422 ** database active. However such a page may be rolled back as a result |
| 2347 ** of an internal error resulting in an automatic call to | 2423 ** of an internal error resulting in an automatic call to |
| 2348 ** sqlite3PagerRollback(). | 2424 ** sqlite3PagerRollback(). |
| 2349 */ | 2425 */ |
| 2350 void *pData; | 2426 void *pData; |
| 2351 pData = pPg->pData; | 2427 pData = pPg->pData; |
| 2352 memcpy(pData, (u8*)aData, pPager->pageSize); | 2428 memcpy(pData, (u8*)aData, pPager->pageSize); |
| 2353 pPager->xReiniter(pPg); | 2429 pPager->xReiniter(pPg); |
| 2354 if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){ | 2430 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But |
| 2355 /* If the contents of this page were just restored from the main | 2431 ** that call was dangerous and had no detectable benefit since the cache |
| 2356 ** journal file, then its content must be as they were when the | 2432 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so |
| 2357 ** transaction was first opened. In this case we can mark the page | 2433 ** has been removed. */ |
| 2358 ** as clean, since there will be no need to write it out to the | |
| 2359 ** database. | |
| 2360 ** | |
| 2361 ** There is one exception to this rule. If the page is being rolled | |
| 2362 ** back as part of a savepoint (or statement) rollback from an | |
| 2363 ** unsynced portion of the main journal file, then it is not safe | |
| 2364 ** to mark the page as clean. This is because marking the page as | |
| 2365 ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is | |
| 2366 ** already in the journal file (recorded in Pager.pInJournal) and | |
| 2367 ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to | |
| 2368 ** again within this transaction, it will be marked as dirty but | |
| 2369 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially | |
| 2370 ** be written out into the database file before its journal file | |
| 2371 ** segment is synced. If a crash occurs during or following this, | |
| 2372 ** database corruption may ensue. | |
| 2373 */ | |
| 2374 assert( !pagerUseWal(pPager) ); | |
| 2375 sqlite3PcacheMakeClean(pPg); | |
| 2376 } | |
| 2377 pager_set_pagehash(pPg); | 2434 pager_set_pagehash(pPg); |
| 2378 | 2435 |
| 2379 /* If this was page 1, then restore the value of Pager.dbFileVers. | 2436 /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 2380 ** Do this before any decoding. */ | 2437 ** Do this before any decoding. */ |
| 2381 if( pgno==1 ){ | 2438 if( pgno==1 ){ |
| 2382 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); | 2439 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
| 2383 } | 2440 } |
| 2384 | 2441 |
| 2385 /* Decode the page just read from disk */ | 2442 /* Decode the page just read from disk */ |
| 2386 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM); | 2443 CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); |
| 2387 sqlite3PcacheRelease(pPg); | 2444 sqlite3PcacheRelease(pPg); |
| 2388 } | 2445 } |
| 2389 return rc; | 2446 return rc; |
| 2390 } | 2447 } |
| 2391 | 2448 |
| 2392 /* | 2449 /* |
| 2393 ** Parameter zMaster is the name of a master journal file. A single journal | 2450 ** Parameter zMaster is the name of a master journal file. A single journal |
| 2394 ** file that referred to the master journal file has just been rolled back. | 2451 ** file that referred to the master journal file has just been rolled back. |
| 2395 ** This routine checks if it is possible to delete the master journal file, | 2452 ** This routine checks if it is possible to delete the master journal file, |
| 2396 ** and does so if it is. | 2453 ** and does so if it is. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2442 char *zJournal; /* Pointer to one journal within MJ file */ | 2499 char *zJournal; /* Pointer to one journal within MJ file */ |
| 2443 char *zMasterPtr; /* Space to hold MJ filename from a journal file */ | 2500 char *zMasterPtr; /* Space to hold MJ filename from a journal file */ |
| 2444 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */ | 2501 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */ |
| 2445 | 2502 |
| 2446 /* Allocate space for both the pJournal and pMaster file descriptors. | 2503 /* Allocate space for both the pJournal and pMaster file descriptors. |
| 2447 ** If successful, open the master journal file for reading. | 2504 ** If successful, open the master journal file for reading. |
| 2448 */ | 2505 */ |
| 2449 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); | 2506 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); |
| 2450 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); | 2507 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile); |
| 2451 if( !pMaster ){ | 2508 if( !pMaster ){ |
| 2452 rc = SQLITE_NOMEM; | 2509 rc = SQLITE_NOMEM_BKPT; |
| 2453 }else{ | 2510 }else{ |
| 2454 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); | 2511 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 2455 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); | 2512 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
| 2456 } | 2513 } |
| 2457 if( rc!=SQLITE_OK ) goto delmaster_out; | 2514 if( rc!=SQLITE_OK ) goto delmaster_out; |
| 2458 | 2515 |
| 2459 /* Load the entire master journal file into space obtained from | 2516 /* Load the entire master journal file into space obtained from |
| 2460 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain | 2517 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain |
| 2461 ** sufficient space (in zMasterPtr) to hold the names of master | 2518 ** sufficient space (in zMasterPtr) to hold the names of master |
| 2462 ** journal files extracted from regular rollback-journals. | 2519 ** journal files extracted from regular rollback-journals. |
| 2463 */ | 2520 */ |
| 2464 rc = sqlite3OsFileSize(pMaster, &nMasterJournal); | 2521 rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
| 2465 if( rc!=SQLITE_OK ) goto delmaster_out; | 2522 if( rc!=SQLITE_OK ) goto delmaster_out; |
| 2466 nMasterPtr = pVfs->mxPathname+1; | 2523 nMasterPtr = pVfs->mxPathname+1; |
| 2467 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1); | 2524 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1); |
| 2468 if( !zMasterJournal ){ | 2525 if( !zMasterJournal ){ |
| 2469 rc = SQLITE_NOMEM; | 2526 rc = SQLITE_NOMEM_BKPT; |
| 2470 goto delmaster_out; | 2527 goto delmaster_out; |
| 2471 } | 2528 } |
| 2472 zMasterPtr = &zMasterJournal[nMasterJournal+1]; | 2529 zMasterPtr = &zMasterJournal[nMasterJournal+1]; |
| 2473 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); | 2530 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); |
| 2474 if( rc!=SQLITE_OK ) goto delmaster_out; | 2531 if( rc!=SQLITE_OK ) goto delmaster_out; |
| 2475 zMasterJournal[nMasterJournal] = 0; | 2532 zMasterJournal[nMasterJournal] = 0; |
| 2476 | 2533 |
| 2477 zJournal = zMasterJournal; | 2534 zJournal = zMasterJournal; |
| 2478 while( (zJournal-zMasterJournal)<nMasterJournal ){ | 2535 while( (zJournal-zMasterJournal)<nMasterJournal ){ |
| 2479 int exists; | 2536 int exists; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2707 } | 2764 } |
| 2708 | 2765 |
| 2709 /* Read the master journal name from the journal, if it is present. | 2766 /* Read the master journal name from the journal, if it is present. |
| 2710 ** If a master journal file name is specified, but the file is not | 2767 ** If a master journal file name is specified, but the file is not |
| 2711 ** present on disk, then the journal is not hot and does not need to be | 2768 ** present on disk, then the journal is not hot and does not need to be |
| 2712 ** played back. | 2769 ** played back. |
| 2713 ** | 2770 ** |
| 2714 ** TODO: Technically the following is an error because it assumes that | 2771 ** TODO: Technically the following is an error because it assumes that |
| 2715 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that | 2772 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that |
| 2716 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, | 2773 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c, |
| 2717 ** mxPathname is 512, which is the same as the minimum allowable value | 2774 ** mxPathname is 512, which is the same as the minimum allowable value |
| 2718 ** for pageSize. | 2775 ** for pageSize. |
| 2719 */ | 2776 */ |
| 2720 zMaster = pPager->pTmpSpace; | 2777 zMaster = pPager->pTmpSpace; |
| 2721 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); | 2778 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 2722 if( rc==SQLITE_OK && zMaster[0] ){ | 2779 if( rc==SQLITE_OK && zMaster[0] ){ |
| 2723 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); | 2780 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
| 2724 } | 2781 } |
| 2725 zMaster = 0; | 2782 zMaster = 0; |
| 2726 if( rc!=SQLITE_OK || !res ){ | 2783 if( rc!=SQLITE_OK || !res ){ |
| 2727 goto end_playback; | 2784 goto end_playback; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2929 ** 24..39 of the database are white noise. But the probability of | 2986 ** 24..39 of the database are white noise. But the probability of |
| 2930 ** white noise equaling 16 bytes of 0xff is vanishingly small so | 2987 ** white noise equaling 16 bytes of 0xff is vanishingly small so |
| 2931 ** we should still be ok. | 2988 ** we should still be ok. |
| 2932 */ | 2989 */ |
| 2933 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); | 2990 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers)); |
| 2934 }else{ | 2991 }else{ |
| 2935 u8 *dbFileVers = &((u8*)pPg->pData)[24]; | 2992 u8 *dbFileVers = &((u8*)pPg->pData)[24]; |
| 2936 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); | 2993 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); |
| 2937 } | 2994 } |
| 2938 } | 2995 } |
| 2939 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM); | 2996 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM_BKPT); |
| 2940 | 2997 |
| 2941 PAGER_INCR(sqlite3_pager_readdb_count); | 2998 PAGER_INCR(sqlite3_pager_readdb_count); |
| 2942 PAGER_INCR(pPager->nRead); | 2999 PAGER_INCR(pPager->nRead); |
| 2943 IOTRACE(("PGIN %p %d\n", pPager, pgno)); | 3000 IOTRACE(("PGIN %p %d\n", pPager, pgno)); |
| 2944 PAGERTRACE(("FETCH %d page %d hash(%08x)\n", | 3001 PAGERTRACE(("FETCH %d page %d hash(%08x)\n", |
| 2945 PAGERID(pPager), pgno, pager_pagehash(pPg))); | 3002 PAGERID(pPager), pgno, pager_pagehash(pPg))); |
| 2946 | 3003 |
| 2947 return rc; | 3004 return rc; |
| 2948 } | 3005 } |
| 2949 | 3006 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3157 Pgno nPage; /* Value to return via *pnPage */ | 3214 Pgno nPage; /* Value to return via *pnPage */ |
| 3158 | 3215 |
| 3159 /* Query the WAL sub-system for the database size. The WalDbsize() | 3216 /* Query the WAL sub-system for the database size. The WalDbsize() |
| 3160 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or | 3217 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or |
| 3161 ** if the database size is not available. The database size is not | 3218 ** if the database size is not available. The database size is not |
| 3162 ** available from the WAL sub-system if the log file is empty or | 3219 ** available from the WAL sub-system if the log file is empty or |
| 3163 ** contains no valid committed transactions. | 3220 ** contains no valid committed transactions. |
| 3164 */ | 3221 */ |
| 3165 assert( pPager->eState==PAGER_OPEN ); | 3222 assert( pPager->eState==PAGER_OPEN ); |
| 3166 assert( pPager->eLock>=SHARED_LOCK ); | 3223 assert( pPager->eLock>=SHARED_LOCK ); |
| 3224 assert( isOpen(pPager->fd) ); |
| 3225 assert( pPager->tempFile==0 ); |
| 3167 nPage = sqlite3WalDbsize(pPager->pWal); | 3226 nPage = sqlite3WalDbsize(pPager->pWal); |
| 3168 | 3227 |
| 3169 /* If the number of pages in the database is not available from the | 3228 /* If the number of pages in the database is not available from the |
| 3170 ** WAL sub-system, determine the page counte based on the size of | 3229 ** WAL sub-system, determine the page counte based on the size of |
| 3171 ** the database file. If the size of the database file is not an | 3230 ** the database file. If the size of the database file is not an |
| 3172 ** integer multiple of the page-size, round up the result. | 3231 ** integer multiple of the page-size, round up the result. |
| 3173 */ | 3232 */ |
| 3174 if( nPage==0 ){ | 3233 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){ |
| 3175 i64 n = 0; /* Size of db file in bytes */ | 3234 i64 n = 0; /* Size of db file in bytes */ |
| 3176 assert( isOpen(pPager->fd) || pPager->tempFile ); | 3235 int rc = sqlite3OsFileSize(pPager->fd, &n); |
| 3177 if( isOpen(pPager->fd) ){ | 3236 if( rc!=SQLITE_OK ){ |
| 3178 int rc = sqlite3OsFileSize(pPager->fd, &n); | 3237 return rc; |
| 3179 if( rc!=SQLITE_OK ){ | |
| 3180 return rc; | |
| 3181 } | |
| 3182 } | 3238 } |
| 3183 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize); | 3239 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize); |
| 3184 } | 3240 } |
| 3185 | 3241 |
| 3186 /* If the current number of pages in the file is greater than the | 3242 /* If the current number of pages in the file is greater than the |
| 3187 ** configured maximum pager number, increase the allowed limit so | 3243 ** configured maximum pager number, increase the allowed limit so |
| 3188 ** that the file can be read. | 3244 ** that the file can be read. |
| 3189 */ | 3245 */ |
| 3190 if( nPage>pPager->mxPgno ){ | 3246 if( nPage>pPager->mxPgno ){ |
| 3191 pPager->mxPgno = (Pgno)nPage; | 3247 pPager->mxPgno = (Pgno)nPage; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3289 int rc = SQLITE_OK; /* Return code */ | 3345 int rc = SQLITE_OK; /* Return code */ |
| 3290 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ | 3346 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ |
| 3291 | 3347 |
| 3292 assert( pPager->eState!=PAGER_ERROR ); | 3348 assert( pPager->eState!=PAGER_ERROR ); |
| 3293 assert( pPager->eState>=PAGER_WRITER_LOCKED ); | 3349 assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 3294 | 3350 |
| 3295 /* Allocate a bitvec to use to store the set of pages rolled back */ | 3351 /* Allocate a bitvec to use to store the set of pages rolled back */ |
| 3296 if( pSavepoint ){ | 3352 if( pSavepoint ){ |
| 3297 pDone = sqlite3BitvecCreate(pSavepoint->nOrig); | 3353 pDone = sqlite3BitvecCreate(pSavepoint->nOrig); |
| 3298 if( !pDone ){ | 3354 if( !pDone ){ |
| 3299 return SQLITE_NOMEM; | 3355 return SQLITE_NOMEM_BKPT; |
| 3300 } | 3356 } |
| 3301 } | 3357 } |
| 3302 | 3358 |
| 3303 /* Set the database size back to the value it was before the savepoint | 3359 /* Set the database size back to the value it was before the savepoint |
| 3304 ** being reverted was opened. | 3360 ** being reverted was opened. |
| 3305 */ | 3361 */ |
| 3306 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; | 3362 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize; |
| 3307 pPager->changeCountDone = pPager->tempFile; | 3363 pPager->changeCountDone = pPager->tempFile; |
| 3308 | 3364 |
| 3309 if( !pSavepoint && pagerUseWal(pPager) ){ | 3365 if( !pSavepoint && pagerUseWal(pPager) ){ |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3410 /* | 3466 /* |
| 3411 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap. | 3467 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap. |
| 3412 */ | 3468 */ |
| 3413 static void pagerFixMaplimit(Pager *pPager){ | 3469 static void pagerFixMaplimit(Pager *pPager){ |
| 3414 #if SQLITE_MAX_MMAP_SIZE>0 | 3470 #if SQLITE_MAX_MMAP_SIZE>0 |
| 3415 sqlite3_file *fd = pPager->fd; | 3471 sqlite3_file *fd = pPager->fd; |
| 3416 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){ | 3472 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){ |
| 3417 sqlite3_int64 sz; | 3473 sqlite3_int64 sz; |
| 3418 sz = pPager->szMmap; | 3474 sz = pPager->szMmap; |
| 3419 pPager->bUseFetch = (sz>0); | 3475 pPager->bUseFetch = (sz>0); |
| 3476 setGetterMethod(pPager); |
| 3420 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz); | 3477 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz); |
| 3421 } | 3478 } |
| 3422 #endif | 3479 #endif |
| 3423 } | 3480 } |
| 3424 | 3481 |
| 3425 /* | 3482 /* |
| 3426 ** Change the maximum size of any memory mapping made of the database file. | 3483 ** Change the maximum size of any memory mapping made of the database file. |
| 3427 */ | 3484 */ |
| 3428 void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){ | 3485 void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){ |
| 3429 pPager->szMmap = szMmap; | 3486 pPager->szMmap = szMmap; |
| 3430 pagerFixMaplimit(pPager); | 3487 pagerFixMaplimit(pPager); |
| 3431 } | 3488 } |
| 3432 | 3489 |
| 3433 /* | 3490 /* |
| 3434 ** Free as much memory as possible from the pager. | 3491 ** Free as much memory as possible from the pager. |
| 3435 */ | 3492 */ |
| 3436 void sqlite3PagerShrink(Pager *pPager){ | 3493 void sqlite3PagerShrink(Pager *pPager){ |
| 3437 sqlite3PcacheShrink(pPager->pPCache); | 3494 sqlite3PcacheShrink(pPager->pPCache); |
| 3438 } | 3495 } |
| 3439 | 3496 |
| 3440 /* | 3497 /* |
| 3441 ** Adjust settings of the pager to those specified in the pgFlags parameter. | 3498 ** Adjust settings of the pager to those specified in the pgFlags parameter. |
| 3442 ** | 3499 ** |
| 3443 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness | 3500 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness |
| 3444 ** of the database to damage due to OS crashes or power failures by | 3501 ** of the database to damage due to OS crashes or power failures by |
| 3445 ** changing the number of syncs()s when writing the journals. | 3502 ** changing the number of syncs()s when writing the journals. |
| 3446 ** There are three levels: | 3503 ** There are four levels: |
| 3447 ** | 3504 ** |
| 3448 ** OFF sqlite3OsSync() is never called. This is the default | 3505 ** OFF sqlite3OsSync() is never called. This is the default |
| 3449 ** for temporary and transient files. | 3506 ** for temporary and transient files. |
| 3450 ** | 3507 ** |
| 3451 ** NORMAL The journal is synced once before writes begin on the | 3508 ** NORMAL The journal is synced once before writes begin on the |
| 3452 ** database. This is normally adequate protection, but | 3509 ** database. This is normally adequate protection, but |
| 3453 ** it is theoretically possible, though very unlikely, | 3510 ** it is theoretically possible, though very unlikely, |
| 3454 ** that an inopertune power failure could leave the journal | 3511 ** that an inopertune power failure could leave the journal |
| 3455 ** in a state which would cause damage to the database | 3512 ** in a state which would cause damage to the database |
| 3456 ** when it is rolled back. | 3513 ** when it is rolled back. |
| 3457 ** | 3514 ** |
| 3458 ** FULL The journal is synced twice before writes begin on the | 3515 ** FULL The journal is synced twice before writes begin on the |
| 3459 ** database (with some additional information - the nRec field | 3516 ** database (with some additional information - the nRec field |
| 3460 ** of the journal header - being written in between the two | 3517 ** of the journal header - being written in between the two |
| 3461 ** syncs). If we assume that writing a | 3518 ** syncs). If we assume that writing a |
| 3462 ** single disk sector is atomic, then this mode provides | 3519 ** single disk sector is atomic, then this mode provides |
| 3463 ** assurance that the journal will not be corrupted to the | 3520 ** assurance that the journal will not be corrupted to the |
| 3464 ** point of causing damage to the database during rollback. | 3521 ** point of causing damage to the database during rollback. |
| 3465 ** | 3522 ** |
| 3523 ** EXTRA This is like FULL except that is also syncs the directory |
| 3524 ** that contains the rollback journal after the rollback |
| 3525 ** journal is unlinked. |
| 3526 ** |
| 3466 ** The above is for a rollback-journal mode. For WAL mode, OFF continues | 3527 ** The above is for a rollback-journal mode. For WAL mode, OFF continues |
| 3467 ** to mean that no syncs ever occur. NORMAL means that the WAL is synced | 3528 ** to mean that no syncs ever occur. NORMAL means that the WAL is synced |
| 3468 ** prior to the start of checkpoint and that the database file is synced | 3529 ** prior to the start of checkpoint and that the database file is synced |
| 3469 ** at the conclusion of the checkpoint if the entire content of the WAL | 3530 ** at the conclusion of the checkpoint if the entire content of the WAL |
| 3470 ** was written back into the database. But no sync operations occur for | 3531 ** was written back into the database. But no sync operations occur for |
| 3471 ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL | 3532 ** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL |
| 3472 ** file is synced following each commit operation, in addition to the | 3533 ** file is synced following each commit operation, in addition to the |
| 3473 ** syncs associated with NORMAL. | 3534 ** syncs associated with NORMAL. There is no difference between FULL |
| 3535 ** and EXTRA for WAL mode. |
| 3474 ** | 3536 ** |
| 3475 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The | 3537 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The |
| 3476 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync | 3538 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync |
| 3477 ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an | 3539 ** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an |
| 3478 ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL | 3540 ** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL |
| 3479 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the | 3541 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the |
| 3480 ** synchronous=FULL versus synchronous=NORMAL setting determines when | 3542 ** synchronous=FULL versus synchronous=NORMAL setting determines when |
| 3481 ** the xSync primitive is called and is relevant to all platforms. | 3543 ** the xSync primitive is called and is relevant to all platforms. |
| 3482 ** | 3544 ** |
| 3483 ** Numeric values associated with these states are OFF==1, NORMAL=2, | 3545 ** Numeric values associated with these states are OFF==1, NORMAL=2, |
| 3484 ** and FULL=3. | 3546 ** and FULL=3. |
| 3485 */ | 3547 */ |
| 3486 #ifndef SQLITE_OMIT_PAGER_PRAGMAS | 3548 #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 3487 void sqlite3PagerSetFlags( | 3549 void sqlite3PagerSetFlags( |
| 3488 Pager *pPager, /* The pager to set safety level for */ | 3550 Pager *pPager, /* The pager to set safety level for */ |
| 3489 unsigned pgFlags /* Various flags */ | 3551 unsigned pgFlags /* Various flags */ |
| 3490 ){ | 3552 ){ |
| 3491 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK; | 3553 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK; |
| 3492 assert( level>=1 && level<=3 ); | 3554 if( pPager->tempFile ){ |
| 3493 pPager->noSync = (level==1 || pPager->tempFile) ?1:0; | 3555 pPager->noSync = 1; |
| 3494 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; | 3556 pPager->fullSync = 0; |
| 3557 pPager->extraSync = 0; |
| 3558 }else{ |
| 3559 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0; |
| 3560 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0; |
| 3561 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0; |
| 3562 } |
| 3495 if( pPager->noSync ){ | 3563 if( pPager->noSync ){ |
| 3496 pPager->syncFlags = 0; | 3564 pPager->syncFlags = 0; |
| 3497 pPager->ckptSyncFlags = 0; | 3565 pPager->ckptSyncFlags = 0; |
| 3498 }else if( pgFlags & PAGER_FULLFSYNC ){ | 3566 }else if( pgFlags & PAGER_FULLFSYNC ){ |
| 3499 pPager->syncFlags = SQLITE_SYNC_FULL; | 3567 pPager->syncFlags = SQLITE_SYNC_FULL; |
| 3500 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; | 3568 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; |
| 3501 }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){ | 3569 }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){ |
| 3502 pPager->syncFlags = SQLITE_SYNC_NORMAL; | 3570 pPager->syncFlags = SQLITE_SYNC_NORMAL; |
| 3503 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; | 3571 pPager->ckptSyncFlags = SQLITE_SYNC_FULL; |
| 3504 }else{ | 3572 }else{ |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3646 && pageSize && pageSize!=(u32)pPager->pageSize | 3714 && pageSize && pageSize!=(u32)pPager->pageSize |
| 3647 ){ | 3715 ){ |
| 3648 char *pNew = NULL; /* New temp space */ | 3716 char *pNew = NULL; /* New temp space */ |
| 3649 i64 nByte = 0; | 3717 i64 nByte = 0; |
| 3650 | 3718 |
| 3651 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){ | 3719 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){ |
| 3652 rc = sqlite3OsFileSize(pPager->fd, &nByte); | 3720 rc = sqlite3OsFileSize(pPager->fd, &nByte); |
| 3653 } | 3721 } |
| 3654 if( rc==SQLITE_OK ){ | 3722 if( rc==SQLITE_OK ){ |
| 3655 pNew = (char *)sqlite3PageMalloc(pageSize); | 3723 pNew = (char *)sqlite3PageMalloc(pageSize); |
| 3656 if( !pNew ) rc = SQLITE_NOMEM; | 3724 if( !pNew ) rc = SQLITE_NOMEM_BKPT; |
| 3657 } | 3725 } |
| 3658 | 3726 |
| 3659 if( rc==SQLITE_OK ){ | 3727 if( rc==SQLITE_OK ){ |
| 3660 pager_reset(pPager); | 3728 pager_reset(pPager); |
| 3661 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); | 3729 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 3662 } | 3730 } |
| 3663 if( rc==SQLITE_OK ){ | 3731 if( rc==SQLITE_OK ){ |
| 3664 sqlite3PageFree(pPager->pTmpSpace); | 3732 sqlite3PageFree(pPager->pTmpSpace); |
| 3665 pPager->pTmpSpace = pNew; | 3733 pPager->pTmpSpace = pNew; |
| 3666 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); | 3734 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3895 int rc = SQLITE_OK; | 3963 int rc = SQLITE_OK; |
| 3896 if( !pPager->noSync ){ | 3964 if( !pPager->noSync ){ |
| 3897 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL); | 3965 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL); |
| 3898 } | 3966 } |
| 3899 if( rc==SQLITE_OK ){ | 3967 if( rc==SQLITE_OK ){ |
| 3900 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr); | 3968 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr); |
| 3901 } | 3969 } |
| 3902 return rc; | 3970 return rc; |
| 3903 } | 3971 } |
| 3904 | 3972 |
| 3973 #if SQLITE_MAX_MMAP_SIZE>0 |
| 3905 /* | 3974 /* |
| 3906 ** Obtain a reference to a memory mapped page object for page number pgno. | 3975 ** Obtain a reference to a memory mapped page object for page number pgno. |
| 3907 ** The new object will use the pointer pData, obtained from xFetch(). | 3976 ** The new object will use the pointer pData, obtained from xFetch(). |
| 3908 ** If successful, set *ppPage to point to the new page reference | 3977 ** If successful, set *ppPage to point to the new page reference |
| 3909 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set | 3978 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set |
| 3910 ** *ppPage to zero. | 3979 ** *ppPage to zero. |
| 3911 ** | 3980 ** |
| 3912 ** Page references obtained by calling this function should be released | 3981 ** Page references obtained by calling this function should be released |
| 3913 ** by calling pagerReleaseMapPage(). | 3982 ** by calling pagerReleaseMapPage(). |
| 3914 */ | 3983 */ |
| 3915 static int pagerAcquireMapPage( | 3984 static int pagerAcquireMapPage( |
| 3916 Pager *pPager, /* Pager object */ | 3985 Pager *pPager, /* Pager object */ |
| 3917 Pgno pgno, /* Page number */ | 3986 Pgno pgno, /* Page number */ |
| 3918 void *pData, /* xFetch()'d data for this page */ | 3987 void *pData, /* xFetch()'d data for this page */ |
| 3919 PgHdr **ppPage /* OUT: Acquired page object */ | 3988 PgHdr **ppPage /* OUT: Acquired page object */ |
| 3920 ){ | 3989 ){ |
| 3921 PgHdr *p; /* Memory mapped page to return */ | 3990 PgHdr *p; /* Memory mapped page to return */ |
| 3922 | 3991 |
| 3923 if( pPager->pMmapFreelist ){ | 3992 if( pPager->pMmapFreelist ){ |
| 3924 *ppPage = p = pPager->pMmapFreelist; | 3993 *ppPage = p = pPager->pMmapFreelist; |
| 3925 pPager->pMmapFreelist = p->pDirty; | 3994 pPager->pMmapFreelist = p->pDirty; |
| 3926 p->pDirty = 0; | 3995 p->pDirty = 0; |
| 3927 memset(p->pExtra, 0, pPager->nExtra); | 3996 assert( pPager->nExtra>=8 ); |
| 3997 memset(p->pExtra, 0, 8); |
| 3928 }else{ | 3998 }else{ |
| 3929 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra); | 3999 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra); |
| 3930 if( p==0 ){ | 4000 if( p==0 ){ |
| 3931 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData); | 4001 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData); |
| 3932 return SQLITE_NOMEM; | 4002 return SQLITE_NOMEM_BKPT; |
| 3933 } | 4003 } |
| 3934 p->pExtra = (void *)&p[1]; | 4004 p->pExtra = (void *)&p[1]; |
| 3935 p->flags = PGHDR_MMAP; | 4005 p->flags = PGHDR_MMAP; |
| 3936 p->nRef = 1; | 4006 p->nRef = 1; |
| 3937 p->pPager = pPager; | 4007 p->pPager = pPager; |
| 3938 } | 4008 } |
| 3939 | 4009 |
| 3940 assert( p->pExtra==(void *)&p[1] ); | 4010 assert( p->pExtra==(void *)&p[1] ); |
| 3941 assert( p->pPage==0 ); | 4011 assert( p->pPage==0 ); |
| 3942 assert( p->flags==PGHDR_MMAP ); | 4012 assert( p->flags==PGHDR_MMAP ); |
| 3943 assert( p->pPager==pPager ); | 4013 assert( p->pPager==pPager ); |
| 3944 assert( p->nRef==1 ); | 4014 assert( p->nRef==1 ); |
| 3945 | 4015 |
| 3946 p->pgno = pgno; | 4016 p->pgno = pgno; |
| 3947 p->pData = pData; | 4017 p->pData = pData; |
| 3948 pPager->nMmapOut++; | 4018 pPager->nMmapOut++; |
| 3949 | 4019 |
| 3950 return SQLITE_OK; | 4020 return SQLITE_OK; |
| 3951 } | 4021 } |
| 4022 #endif |
| 3952 | 4023 |
| 3953 /* | 4024 /* |
| 3954 ** Release a reference to page pPg. pPg must have been returned by an | 4025 ** Release a reference to page pPg. pPg must have been returned by an |
| 3955 ** earlier call to pagerAcquireMapPage(). | 4026 ** earlier call to pagerAcquireMapPage(). |
| 3956 */ | 4027 */ |
| 3957 static void pagerReleaseMapPage(PgHdr *pPg){ | 4028 static void pagerReleaseMapPage(PgHdr *pPg){ |
| 3958 Pager *pPager = pPg->pPager; | 4029 Pager *pPager = pPg->pPager; |
| 3959 pPager->nMmapOut--; | 4030 pPager->nMmapOut--; |
| 3960 pPg->pDirty = pPager->pMmapFreelist; | 4031 pPg->pDirty = pPager->pMmapFreelist; |
| 3961 pPager->pMmapFreelist = pPg; | 4032 pPager->pMmapFreelist = pPg; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 3984 ** transaction is rolled back. All outstanding pages are invalidated | 4055 ** transaction is rolled back. All outstanding pages are invalidated |
| 3985 ** and their memory is freed. Any attempt to use a page associated | 4056 ** and their memory is freed. Any attempt to use a page associated |
| 3986 ** with this page cache after this function returns will likely | 4057 ** with this page cache after this function returns will likely |
| 3987 ** result in a coredump. | 4058 ** result in a coredump. |
| 3988 ** | 4059 ** |
| 3989 ** This function always succeeds. If a transaction is active an attempt | 4060 ** This function always succeeds. If a transaction is active an attempt |
| 3990 ** is made to roll it back. If an error occurs during the rollback | 4061 ** is made to roll it back. If an error occurs during the rollback |
| 3991 ** a hot journal may be left in the filesystem but no error is returned | 4062 ** a hot journal may be left in the filesystem but no error is returned |
| 3992 ** to the caller. | 4063 ** to the caller. |
| 3993 */ | 4064 */ |
| 3994 int sqlite3PagerClose(Pager *pPager){ | 4065 int sqlite3PagerClose(Pager *pPager, sqlite3 *db){ |
| 3995 u8 *pTmp = (u8 *)pPager->pTmpSpace; | 4066 u8 *pTmp = (u8 *)pPager->pTmpSpace; |
| 3996 | 4067 |
| 4068 assert( db || pagerUseWal(pPager)==0 ); |
| 3997 assert( assert_pager_state(pPager) ); | 4069 assert( assert_pager_state(pPager) ); |
| 3998 disable_simulated_io_errors(); | 4070 disable_simulated_io_errors(); |
| 3999 sqlite3BeginBenignMalloc(); | 4071 sqlite3BeginBenignMalloc(); |
| 4000 pagerFreeMapHdrs(pPager); | 4072 pagerFreeMapHdrs(pPager); |
| 4001 /* pPager->errCode = 0; */ | 4073 /* pPager->errCode = 0; */ |
| 4002 pPager->exclusiveMode = 0; | 4074 pPager->exclusiveMode = 0; |
| 4003 #ifndef SQLITE_OMIT_WAL | 4075 #ifndef SQLITE_OMIT_WAL |
| 4004 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp); | 4076 assert( db || pPager->pWal==0 ); |
| 4077 sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags, pPager->pageSize, |
| 4078 (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp) |
| 4079 ); |
| 4005 pPager->pWal = 0; | 4080 pPager->pWal = 0; |
| 4006 #endif | 4081 #endif |
| 4007 pager_reset(pPager); | 4082 pager_reset(pPager); |
| 4008 if( MEMDB ){ | 4083 if( MEMDB ){ |
| 4009 pager_unlock(pPager); | 4084 pager_unlock(pPager); |
| 4010 }else{ | 4085 }else{ |
| 4011 /* If it is open, sync the journal file before calling UnlockAndRollback. | 4086 /* If it is open, sync the journal file before calling UnlockAndRollback. |
| 4012 ** If this is not done, then an unsynced portion of the open journal | 4087 ** If this is not done, then an unsynced portion of the open journal |
| 4013 ** file may be played back into the database. If a power failure occurs | 4088 ** file may be played back into the database. If a power failure occurs |
| 4014 ** while this is happening, the database could become corrupt. | 4089 ** while this is happening, the database could become corrupt. |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4236 ** | 4311 ** |
| 4237 ** If everything is successful, SQLITE_OK is returned. If an IO error | 4312 ** If everything is successful, SQLITE_OK is returned. If an IO error |
| 4238 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot | 4313 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot |
| 4239 ** be obtained, SQLITE_BUSY is returned. | 4314 ** be obtained, SQLITE_BUSY is returned. |
| 4240 */ | 4315 */ |
| 4241 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ | 4316 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ |
| 4242 int rc = SQLITE_OK; /* Return code */ | 4317 int rc = SQLITE_OK; /* Return code */ |
| 4243 | 4318 |
| 4244 /* This function is only called for rollback pagers in WRITER_DBMOD state. */ | 4319 /* This function is only called for rollback pagers in WRITER_DBMOD state. */ |
| 4245 assert( !pagerUseWal(pPager) ); | 4320 assert( !pagerUseWal(pPager) ); |
| 4246 assert( pPager->eState==PAGER_WRITER_DBMOD ); | 4321 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD ); |
| 4247 assert( pPager->eLock==EXCLUSIVE_LOCK ); | 4322 assert( pPager->eLock==EXCLUSIVE_LOCK ); |
| 4323 assert( isOpen(pPager->fd) || pList->pDirty==0 ); |
| 4248 | 4324 |
| 4249 /* If the file is a temp-file has not yet been opened, open it now. It | 4325 /* If the file is a temp-file has not yet been opened, open it now. It |
| 4250 ** is not possible for rc to be other than SQLITE_OK if this branch | 4326 ** is not possible for rc to be other than SQLITE_OK if this branch |
| 4251 ** is taken, as pager_wait_on_lock() is a no-op for temp-files. | 4327 ** is taken, as pager_wait_on_lock() is a no-op for temp-files. |
| 4252 */ | 4328 */ |
| 4253 if( !isOpen(pPager->fd) ){ | 4329 if( !isOpen(pPager->fd) ){ |
| 4254 assert( pPager->tempFile && rc==SQLITE_OK ); | 4330 assert( pPager->tempFile && rc==SQLITE_OK ); |
| 4255 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); | 4331 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags); |
| 4256 } | 4332 } |
| 4257 | 4333 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 4280 ** set (set by sqlite3PagerDontWrite()). | 4356 ** set (set by sqlite3PagerDontWrite()). |
| 4281 */ | 4357 */ |
| 4282 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ | 4358 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ |
| 4283 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ | 4359 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ |
| 4284 char *pData; /* Data to write */ | 4360 char *pData; /* Data to write */ |
| 4285 | 4361 |
| 4286 assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); | 4362 assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); |
| 4287 if( pList->pgno==1 ) pager_write_changecounter(pList); | 4363 if( pList->pgno==1 ) pager_write_changecounter(pList); |
| 4288 | 4364 |
| 4289 /* Encode the database */ | 4365 /* Encode the database */ |
| 4290 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); | 4366 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData); |
| 4291 | 4367 |
| 4292 /* Write out the page data. */ | 4368 /* Write out the page data. */ |
| 4293 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); | 4369 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); |
| 4294 | 4370 |
| 4295 /* If page 1 was just written, update Pager.dbFileVers to match | 4371 /* If page 1 was just written, update Pager.dbFileVers to match |
| 4296 ** the value now stored in the database file. If writing this | 4372 ** the value now stored in the database file. If writing this |
| 4297 ** page caused the database file to grow, update dbFileSize. | 4373 ** page caused the database file to grow, update dbFileSize. |
| 4298 */ | 4374 */ |
| 4299 if( pgno==1 ){ | 4375 if( pgno==1 ){ |
| 4300 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); | 4376 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 4325 ** Ensure that the sub-journal file is open. If it is already open, this | 4401 ** Ensure that the sub-journal file is open. If it is already open, this |
| 4326 ** function is a no-op. | 4402 ** function is a no-op. |
| 4327 ** | 4403 ** |
| 4328 ** SQLITE_OK is returned if everything goes according to plan. An | 4404 ** SQLITE_OK is returned if everything goes according to plan. An |
| 4329 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() | 4405 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() |
| 4330 ** fails. | 4406 ** fails. |
| 4331 */ | 4407 */ |
| 4332 static int openSubJournal(Pager *pPager){ | 4408 static int openSubJournal(Pager *pPager){ |
| 4333 int rc = SQLITE_OK; | 4409 int rc = SQLITE_OK; |
| 4334 if( !isOpen(pPager->sjfd) ){ | 4410 if( !isOpen(pPager->sjfd) ){ |
| 4411 const int flags = SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_READWRITE |
| 4412 | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE |
| 4413 | SQLITE_OPEN_DELETEONCLOSE; |
| 4414 int nStmtSpill = sqlite3Config.nStmtSpill; |
| 4335 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){ | 4415 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){ |
| 4336 sqlite3MemJournalOpen(pPager->sjfd); | 4416 nStmtSpill = -1; |
| 4337 }else{ | |
| 4338 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL); | |
| 4339 } | 4417 } |
| 4418 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill); |
| 4340 } | 4419 } |
| 4341 return rc; | 4420 return rc; |
| 4342 } | 4421 } |
| 4343 | 4422 |
| 4344 /* | 4423 /* |
| 4345 ** Append a record of the current state of page pPg to the sub-journal. | 4424 ** Append a record of the current state of page pPg to the sub-journal. |
| 4346 ** | 4425 ** |
| 4347 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs | 4426 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs |
| 4348 ** for all open savepoints before returning. | 4427 ** for all open savepoints before returning. |
| 4349 ** | 4428 ** |
| (...skipping 17 matching lines...) Expand all Loading... |
| 4367 ); | 4446 ); |
| 4368 rc = openSubJournal(pPager); | 4447 rc = openSubJournal(pPager); |
| 4369 | 4448 |
| 4370 /* If the sub-journal was opened successfully (or was already open), | 4449 /* If the sub-journal was opened successfully (or was already open), |
| 4371 ** write the journal record into the file. */ | 4450 ** write the journal record into the file. */ |
| 4372 if( rc==SQLITE_OK ){ | 4451 if( rc==SQLITE_OK ){ |
| 4373 void *pData = pPg->pData; | 4452 void *pData = pPg->pData; |
| 4374 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); | 4453 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); |
| 4375 char *pData2; | 4454 char *pData2; |
| 4376 | 4455 |
| 4377 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); | 4456 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); |
| 4378 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); | 4457 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); |
| 4379 rc = write32bits(pPager->sjfd, offset, pPg->pgno); | 4458 rc = write32bits(pPager->sjfd, offset, pPg->pgno); |
| 4380 if( rc==SQLITE_OK ){ | 4459 if( rc==SQLITE_OK ){ |
| 4381 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); | 4460 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4); |
| 4382 } | 4461 } |
| 4383 } | 4462 } |
| 4384 } | 4463 } |
| 4385 if( rc==SQLITE_OK ){ | 4464 if( rc==SQLITE_OK ){ |
| 4386 pPager->nSubRec++; | 4465 pPager->nSubRec++; |
| 4387 assert( pPager->nSavepoint>0 ); | 4466 assert( pPager->nSavepoint>0 ); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4509 ** | 4588 ** |
| 4510 ** The zFilename argument is the path to the database file to open. | 4589 ** The zFilename argument is the path to the database file to open. |
| 4511 ** If zFilename is NULL then a randomly-named temporary file is created | 4590 ** If zFilename is NULL then a randomly-named temporary file is created |
| 4512 ** and used as the file to be cached. Temporary files are be deleted | 4591 ** and used as the file to be cached. Temporary files are be deleted |
| 4513 ** automatically when they are closed. If zFilename is ":memory:" then | 4592 ** automatically when they are closed. If zFilename is ":memory:" then |
| 4514 ** all information is held in cache. It is never written to disk. | 4593 ** all information is held in cache. It is never written to disk. |
| 4515 ** This can be used to implement an in-memory database. | 4594 ** This can be used to implement an in-memory database. |
| 4516 ** | 4595 ** |
| 4517 ** The nExtra parameter specifies the number of bytes of space allocated | 4596 ** The nExtra parameter specifies the number of bytes of space allocated |
| 4518 ** along with each page reference. This space is available to the user | 4597 ** along with each page reference. This space is available to the user |
| 4519 ** via the sqlite3PagerGetExtra() API. | 4598 ** via the sqlite3PagerGetExtra() API. When a new page is allocated, the |
| 4599 ** first 8 bytes of this space are zeroed but the remainder is uninitialized. |
| 4600 ** (The extra space is used by btree as the MemPage object.) |
| 4520 ** | 4601 ** |
| 4521 ** The flags argument is used to specify properties that affect the | 4602 ** The flags argument is used to specify properties that affect the |
| 4522 ** operation of the pager. It should be passed some bitwise combination | 4603 ** operation of the pager. It should be passed some bitwise combination |
| 4523 ** of the PAGER_* flags. | 4604 ** of the PAGER_* flags. |
| 4524 ** | 4605 ** |
| 4525 ** The vfsFlags parameter is a bitmask to pass to the flags parameter | 4606 ** The vfsFlags parameter is a bitmask to pass to the flags parameter |
| 4526 ** of the xOpen() method of the supplied VFS when opening files. | 4607 ** of the xOpen() method of the supplied VFS when opening files. |
| 4527 ** | 4608 ** |
| 4528 ** If the pager object is allocated and the specified file opened | 4609 ** If the pager object is allocated and the specified file opened |
| 4529 ** successfully, SQLITE_OK is returned and *ppPager set to point to | 4610 ** successfully, SQLITE_OK is returned and *ppPager set to point to |
| (...skipping 20 matching lines...) Expand all Loading... |
| 4550 int journalFileSize; /* Bytes to allocate for each journal fd */ | 4631 int journalFileSize; /* Bytes to allocate for each journal fd */ |
| 4551 char *zPathname = 0; /* Full path to database file */ | 4632 char *zPathname = 0; /* Full path to database file */ |
| 4552 int nPathname = 0; /* Number of bytes in zPathname */ | 4633 int nPathname = 0; /* Number of bytes in zPathname */ |
| 4553 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ | 4634 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ |
| 4554 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ | 4635 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ |
| 4555 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ | 4636 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ |
| 4556 const char *zUri = 0; /* URI args to copy */ | 4637 const char *zUri = 0; /* URI args to copy */ |
| 4557 int nUri = 0; /* Number of bytes of URI args at *zUri */ | 4638 int nUri = 0; /* Number of bytes of URI args at *zUri */ |
| 4558 | 4639 |
| 4559 /* Figure out how much space is required for each journal file-handle | 4640 /* Figure out how much space is required for each journal file-handle |
| 4560 ** (there are two of them, the main journal and the sub-journal). This | 4641 ** (there are two of them, the main journal and the sub-journal). */ |
| 4561 ** is the maximum space required for an in-memory journal file handle | 4642 journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); |
| 4562 ** and a regular journal file-handle. Note that a "regular journal-handle" | |
| 4563 ** may be a wrapper capable of caching the first portion of the journal | |
| 4564 ** file in memory to implement the atomic-write optimization (see | |
| 4565 ** source file journal.c). | |
| 4566 */ | |
| 4567 if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){ | |
| 4568 journalFileSize = ROUND8(sqlite3JournalSize(pVfs)); | |
| 4569 }else{ | |
| 4570 journalFileSize = ROUND8(sqlite3MemJournalSize()); | |
| 4571 } | |
| 4572 | 4643 |
| 4573 /* Set the output variable to NULL in case an error occurs. */ | 4644 /* Set the output variable to NULL in case an error occurs. */ |
| 4574 *ppPager = 0; | 4645 *ppPager = 0; |
| 4575 | 4646 |
| 4576 #ifndef SQLITE_OMIT_MEMORYDB | 4647 #ifndef SQLITE_OMIT_MEMORYDB |
| 4577 if( flags & PAGER_MEMORY ){ | 4648 if( flags & PAGER_MEMORY ){ |
| 4578 memDb = 1; | 4649 memDb = 1; |
| 4579 if( zFilename && zFilename[0] ){ | 4650 if( zFilename && zFilename[0] ){ |
| 4580 zPathname = sqlite3DbStrDup(0, zFilename); | 4651 zPathname = sqlite3DbStrDup(0, zFilename); |
| 4581 if( zPathname==0 ) return SQLITE_NOMEM; | 4652 if( zPathname==0 ) return SQLITE_NOMEM_BKPT; |
| 4582 nPathname = sqlite3Strlen30(zPathname); | 4653 nPathname = sqlite3Strlen30(zPathname); |
| 4583 zFilename = 0; | 4654 zFilename = 0; |
| 4584 } | 4655 } |
| 4585 } | 4656 } |
| 4586 #endif | 4657 #endif |
| 4587 | 4658 |
| 4588 /* Compute and store the full pathname in an allocated buffer pointed | 4659 /* Compute and store the full pathname in an allocated buffer pointed |
| 4589 ** to by zPathname, length nPathname. Or, if this is a temporary file, | 4660 ** to by zPathname, length nPathname. Or, if this is a temporary file, |
| 4590 ** leave both nPathname and zPathname set to 0. | 4661 ** leave both nPathname and zPathname set to 0. |
| 4591 */ | 4662 */ |
| 4592 if( zFilename && zFilename[0] ){ | 4663 if( zFilename && zFilename[0] ){ |
| 4593 const char *z; | 4664 const char *z; |
| 4594 nPathname = pVfs->mxPathname+1; | 4665 nPathname = pVfs->mxPathname+1; |
| 4595 zPathname = sqlite3DbMallocRaw(0, nPathname*2); | 4666 zPathname = sqlite3DbMallocRaw(0, nPathname*2); |
| 4596 if( zPathname==0 ){ | 4667 if( zPathname==0 ){ |
| 4597 return SQLITE_NOMEM; | 4668 return SQLITE_NOMEM_BKPT; |
| 4598 } | 4669 } |
| 4599 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ | 4670 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */ |
| 4600 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); | 4671 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname); |
| 4601 nPathname = sqlite3Strlen30(zPathname); | 4672 nPathname = sqlite3Strlen30(zPathname); |
| 4602 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1]; | 4673 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1]; |
| 4603 while( *z ){ | 4674 while( *z ){ |
| 4604 z += sqlite3Strlen30(z)+1; | 4675 z += sqlite3Strlen30(z)+1; |
| 4605 z += sqlite3Strlen30(z)+1; | 4676 z += sqlite3Strlen30(z)+1; |
| 4606 } | 4677 } |
| 4607 nUri = (int)(&z[1] - zUri); | 4678 nUri = (int)(&z[1] - zUri); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4640 journalFileSize * 2 + /* The two journal files */ | 4711 journalFileSize * 2 + /* The two journal files */ |
| 4641 nPathname + 1 + nUri + /* zFilename */ | 4712 nPathname + 1 + nUri + /* zFilename */ |
| 4642 nPathname + 8 + 2 /* zJournal */ | 4713 nPathname + 8 + 2 /* zJournal */ |
| 4643 #ifndef SQLITE_OMIT_WAL | 4714 #ifndef SQLITE_OMIT_WAL |
| 4644 + nPathname + 4 + 2 /* zWal */ | 4715 + nPathname + 4 + 2 /* zWal */ |
| 4645 #endif | 4716 #endif |
| 4646 ); | 4717 ); |
| 4647 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); | 4718 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) ); |
| 4648 if( !pPtr ){ | 4719 if( !pPtr ){ |
| 4649 sqlite3DbFree(0, zPathname); | 4720 sqlite3DbFree(0, zPathname); |
| 4650 return SQLITE_NOMEM; | 4721 return SQLITE_NOMEM_BKPT; |
| 4651 } | 4722 } |
| 4652 pPager = (Pager*)(pPtr); | 4723 pPager = (Pager*)(pPtr); |
| 4653 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); | 4724 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))); |
| 4654 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); | 4725 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)); |
| 4655 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); | 4726 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)); |
| 4656 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); | 4727 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize); |
| 4657 pPager->zFilename = (char*)(pPtr += journalFileSize); | 4728 pPager->zFilename = (char*)(pPtr += journalFileSize); |
| 4658 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); | 4729 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) ); |
| 4659 | 4730 |
| 4660 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ | 4731 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */ |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4749 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. | 4820 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. |
| 4750 */ | 4821 */ |
| 4751 if( rc==SQLITE_OK ){ | 4822 if( rc==SQLITE_OK ){ |
| 4752 assert( pPager->memDb==0 ); | 4823 assert( pPager->memDb==0 ); |
| 4753 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1); | 4824 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1); |
| 4754 testcase( rc!=SQLITE_OK ); | 4825 testcase( rc!=SQLITE_OK ); |
| 4755 } | 4826 } |
| 4756 | 4827 |
| 4757 /* Initialize the PCache object. */ | 4828 /* Initialize the PCache object. */ |
| 4758 if( rc==SQLITE_OK ){ | 4829 if( rc==SQLITE_OK ){ |
| 4759 assert( nExtra<1000 ); | |
| 4760 nExtra = ROUND8(nExtra); | 4830 nExtra = ROUND8(nExtra); |
| 4831 assert( nExtra>=8 && nExtra<1000 ); |
| 4761 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, | 4832 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb, |
| 4762 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); | 4833 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache); |
| 4763 } | 4834 } |
| 4764 | 4835 |
| 4765 /* If an error occurred above, free the Pager structure and close the file. | 4836 /* If an error occurred above, free the Pager structure and close the file. |
| 4766 */ | 4837 */ |
| 4767 if( rc!=SQLITE_OK ){ | 4838 if( rc!=SQLITE_OK ){ |
| 4768 sqlite3OsClose(pPager->fd); | 4839 sqlite3OsClose(pPager->fd); |
| 4769 sqlite3PageFree(pPager->pTmpSpace); | 4840 sqlite3PageFree(pPager->pTmpSpace); |
| 4770 sqlite3_free(pPager); | 4841 sqlite3_free(pPager); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 4789 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); | 4860 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 4790 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); | 4861 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 4791 pPager->exclusiveMode = (u8)tempFile; | 4862 pPager->exclusiveMode = (u8)tempFile; |
| 4792 pPager->changeCountDone = pPager->tempFile; | 4863 pPager->changeCountDone = pPager->tempFile; |
| 4793 pPager->memDb = (u8)memDb; | 4864 pPager->memDb = (u8)memDb; |
| 4794 pPager->readOnly = (u8)readOnly; | 4865 pPager->readOnly = (u8)readOnly; |
| 4795 assert( useJournal || pPager->tempFile ); | 4866 assert( useJournal || pPager->tempFile ); |
| 4796 pPager->noSync = pPager->tempFile; | 4867 pPager->noSync = pPager->tempFile; |
| 4797 if( pPager->noSync ){ | 4868 if( pPager->noSync ){ |
| 4798 assert( pPager->fullSync==0 ); | 4869 assert( pPager->fullSync==0 ); |
| 4870 assert( pPager->extraSync==0 ); |
| 4799 assert( pPager->syncFlags==0 ); | 4871 assert( pPager->syncFlags==0 ); |
| 4800 assert( pPager->walSyncFlags==0 ); | 4872 assert( pPager->walSyncFlags==0 ); |
| 4801 assert( pPager->ckptSyncFlags==0 ); | 4873 assert( pPager->ckptSyncFlags==0 ); |
| 4802 }else{ | 4874 }else{ |
| 4803 pPager->fullSync = 1; | 4875 pPager->fullSync = 1; |
| 4876 pPager->extraSync = 0; |
| 4804 pPager->syncFlags = SQLITE_SYNC_NORMAL; | 4877 pPager->syncFlags = SQLITE_SYNC_NORMAL; |
| 4805 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS; | 4878 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS; |
| 4806 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; | 4879 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL; |
| 4807 } | 4880 } |
| 4808 /* pPager->pFirst = 0; */ | 4881 /* pPager->pFirst = 0; */ |
| 4809 /* pPager->pFirstSynced = 0; */ | 4882 /* pPager->pFirstSynced = 0; */ |
| 4810 /* pPager->pLast = 0; */ | 4883 /* pPager->pLast = 0; */ |
| 4811 pPager->nExtra = (u16)nExtra; | 4884 pPager->nExtra = (u16)nExtra; |
| 4812 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; | 4885 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT; |
| 4813 assert( isOpen(pPager->fd) || tempFile ); | 4886 assert( isOpen(pPager->fd) || tempFile ); |
| 4814 setSectorSize(pPager); | 4887 setSectorSize(pPager); |
| 4815 if( !useJournal ){ | 4888 if( !useJournal ){ |
| 4816 pPager->journalMode = PAGER_JOURNALMODE_OFF; | 4889 pPager->journalMode = PAGER_JOURNALMODE_OFF; |
| 4817 }else if( memDb ){ | 4890 }else if( memDb ){ |
| 4818 pPager->journalMode = PAGER_JOURNALMODE_MEMORY; | 4891 pPager->journalMode = PAGER_JOURNALMODE_MEMORY; |
| 4819 } | 4892 } |
| 4820 /* pPager->xBusyHandler = 0; */ | 4893 /* pPager->xBusyHandler = 0; */ |
| 4821 /* pPager->pBusyHandlerArg = 0; */ | 4894 /* pPager->pBusyHandlerArg = 0; */ |
| 4822 pPager->xReiniter = xReinit; | 4895 pPager->xReiniter = xReinit; |
| 4896 setGetterMethod(pPager); |
| 4823 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ | 4897 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */ |
| 4824 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ | 4898 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */ |
| 4825 | 4899 |
| 4826 *ppPager = pPager; | 4900 *ppPager = pPager; |
| 4827 return SQLITE_OK; | 4901 return SQLITE_OK; |
| 4828 } | 4902 } |
| 4829 | 4903 |
| 4830 | 4904 |
| 4831 /* Verify that the database file has not be deleted or renamed out from | 4905 /* Verify that the database file has not be deleted or renamed out from |
| 4832 ** under the pager. Return SQLITE_OK if the database is still were it ought | 4906 ** under the pager. Return SQLITE_OK if the database is still were it ought |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4910 ** call above, but then delete the journal and drop the lock before | 4984 ** call above, but then delete the journal and drop the lock before |
| 4911 ** we get to the following sqlite3OsCheckReservedLock() call. If that | 4985 ** we get to the following sqlite3OsCheckReservedLock() call. If that |
| 4912 ** is the case, this routine might think there is a hot journal when | 4986 ** is the case, this routine might think there is a hot journal when |
| 4913 ** in fact there is none. This results in a false-positive which will | 4987 ** in fact there is none. This results in a false-positive which will |
| 4914 ** be dealt with by the playback routine. Ticket #3883. | 4988 ** be dealt with by the playback routine. Ticket #3883. |
| 4915 */ | 4989 */ |
| 4916 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); | 4990 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 4917 if( rc==SQLITE_OK && !locked ){ | 4991 if( rc==SQLITE_OK && !locked ){ |
| 4918 Pgno nPage; /* Number of pages in database file */ | 4992 Pgno nPage; /* Number of pages in database file */ |
| 4919 | 4993 |
| 4994 assert( pPager->tempFile==0 ); |
| 4920 rc = pagerPagecount(pPager, &nPage); | 4995 rc = pagerPagecount(pPager, &nPage); |
| 4921 if( rc==SQLITE_OK ){ | 4996 if( rc==SQLITE_OK ){ |
| 4922 /* If the database is zero pages in size, that means that either (1) the | 4997 /* If the database is zero pages in size, that means that either (1) the |
| 4923 ** journal is a remnant from a prior database with the same name where | 4998 ** journal is a remnant from a prior database with the same name where |
| 4924 ** the database file but not the journal was deleted, or (2) the initial | 4999 ** the database file but not the journal was deleted, or (2) the initial |
| 4925 ** transaction that populates a new database is being rolled back. | 5000 ** transaction that populates a new database is being rolled back. |
| 4926 ** In either case, the journal file can be deleted. However, take care | 5001 ** In either case, the journal file can be deleted. However, take care |
| 4927 ** not to delete the journal file if it is already open due to | 5002 ** not to delete the journal file if it is already open due to |
| 4928 ** journal_mode=PERSIST. | 5003 ** journal_mode=PERSIST. |
| 4929 */ | 5004 */ |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5002 ** If everything is successful, SQLITE_OK is returned. If an IO error | 5077 ** If everything is successful, SQLITE_OK is returned. If an IO error |
| 5003 ** occurs while locking the database, checking for a hot-journal file or | 5078 ** occurs while locking the database, checking for a hot-journal file or |
| 5004 ** rolling back a journal file, the IO error code is returned. | 5079 ** rolling back a journal file, the IO error code is returned. |
| 5005 */ | 5080 */ |
| 5006 int sqlite3PagerSharedLock(Pager *pPager){ | 5081 int sqlite3PagerSharedLock(Pager *pPager){ |
| 5007 int rc = SQLITE_OK; /* Return code */ | 5082 int rc = SQLITE_OK; /* Return code */ |
| 5008 | 5083 |
| 5009 /* This routine is only called from b-tree and only when there are no | 5084 /* This routine is only called from b-tree and only when there are no |
| 5010 ** outstanding pages. This implies that the pager state should either | 5085 ** outstanding pages. This implies that the pager state should either |
| 5011 ** be OPEN or READER. READER is only possible if the pager is or was in | 5086 ** be OPEN or READER. READER is only possible if the pager is or was in |
| 5012 ** exclusive access mode. | 5087 ** exclusive access mode. */ |
| 5013 */ | |
| 5014 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); | 5088 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 5015 assert( assert_pager_state(pPager) ); | 5089 assert( assert_pager_state(pPager) ); |
| 5016 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); | 5090 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); |
| 5017 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } | 5091 assert( pPager->errCode==SQLITE_OK ); |
| 5018 | 5092 |
| 5019 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ | 5093 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ |
| 5020 int bHotJournal = 1; /* True if there exists a hot journal-file */ | 5094 int bHotJournal = 1; /* True if there exists a hot journal-file */ |
| 5021 | 5095 |
| 5022 assert( !MEMDB ); | 5096 assert( !MEMDB ); |
| 5097 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK ); |
| 5023 | 5098 |
| 5024 rc = pager_wait_on_lock(pPager, SHARED_LOCK); | 5099 rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 5025 if( rc!=SQLITE_OK ){ | 5100 if( rc!=SQLITE_OK ){ |
| 5026 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK ); | 5101 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK ); |
| 5027 goto failed; | 5102 goto failed; |
| 5028 } | 5103 } |
| 5029 | 5104 |
| 5030 /* If a journal file exists, and there is no RESERVED lock on the | 5105 /* If a journal file exists, and there is no RESERVED lock on the |
| 5031 ** database file, then it either needs to be played back or deleted. | 5106 ** database file, then it either needs to be played back or deleted. |
| 5032 */ | 5107 */ |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5098 ** playing back the hot-journal so that we don't end up with | 5173 ** playing back the hot-journal so that we don't end up with |
| 5099 ** an inconsistent cache. Sync the hot journal before playing | 5174 ** an inconsistent cache. Sync the hot journal before playing |
| 5100 ** it back since the process that crashed and left the hot journal | 5175 ** it back since the process that crashed and left the hot journal |
| 5101 ** probably did not sync it and we are required to always sync | 5176 ** probably did not sync it and we are required to always sync |
| 5102 ** the journal before playing it back. | 5177 ** the journal before playing it back. |
| 5103 */ | 5178 */ |
| 5104 if( isOpen(pPager->jfd) ){ | 5179 if( isOpen(pPager->jfd) ){ |
| 5105 assert( rc==SQLITE_OK ); | 5180 assert( rc==SQLITE_OK ); |
| 5106 rc = pagerSyncHotJournal(pPager); | 5181 rc = pagerSyncHotJournal(pPager); |
| 5107 if( rc==SQLITE_OK ){ | 5182 if( rc==SQLITE_OK ){ |
| 5108 rc = pager_playback(pPager, 1); | 5183 rc = pager_playback(pPager, !pPager->tempFile); |
| 5109 pPager->eState = PAGER_OPEN; | 5184 pPager->eState = PAGER_OPEN; |
| 5110 } | 5185 } |
| 5111 }else if( !pPager->exclusiveMode ){ | 5186 }else if( !pPager->exclusiveMode ){ |
| 5112 pagerUnlockDb(pPager, SHARED_LOCK); | 5187 pagerUnlockDb(pPager, SHARED_LOCK); |
| 5113 } | 5188 } |
| 5114 | 5189 |
| 5115 if( rc!=SQLITE_OK ){ | 5190 if( rc!=SQLITE_OK ){ |
| 5116 /* This branch is taken if an error occurs while trying to open | 5191 /* This branch is taken if an error occurs while trying to open |
| 5117 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The | 5192 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The |
| 5118 ** pager_unlock() routine will be called before returning to unlock | 5193 ** pager_unlock() routine will be called before returning to unlock |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5194 #ifndef SQLITE_OMIT_WAL | 5269 #ifndef SQLITE_OMIT_WAL |
| 5195 assert( pPager->pWal==0 || rc==SQLITE_OK ); | 5270 assert( pPager->pWal==0 || rc==SQLITE_OK ); |
| 5196 #endif | 5271 #endif |
| 5197 } | 5272 } |
| 5198 | 5273 |
| 5199 if( pagerUseWal(pPager) ){ | 5274 if( pagerUseWal(pPager) ){ |
| 5200 assert( rc==SQLITE_OK ); | 5275 assert( rc==SQLITE_OK ); |
| 5201 rc = pagerBeginReadTransaction(pPager); | 5276 rc = pagerBeginReadTransaction(pPager); |
| 5202 } | 5277 } |
| 5203 | 5278 |
| 5204 if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ | 5279 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ |
| 5205 rc = pagerPagecount(pPager, &pPager->dbSize); | 5280 rc = pagerPagecount(pPager, &pPager->dbSize); |
| 5206 } | 5281 } |
| 5207 | 5282 |
| 5208 failed: | 5283 failed: |
| 5209 if( rc!=SQLITE_OK ){ | 5284 if( rc!=SQLITE_OK ){ |
| 5210 assert( !MEMDB ); | 5285 assert( !MEMDB ); |
| 5211 pager_unlock(pPager); | 5286 pager_unlock(pPager); |
| 5212 assert( pPager->eState==PAGER_OPEN ); | 5287 assert( pPager->eState==PAGER_OPEN ); |
| 5213 }else{ | 5288 }else{ |
| 5214 pPager->eState = PAGER_READER; | 5289 pPager->eState = PAGER_READER; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 5225 ** the rollback journal, the unlock is not performed and there is | 5300 ** the rollback journal, the unlock is not performed and there is |
| 5226 ** nothing to rollback, so this routine is a no-op. | 5301 ** nothing to rollback, so this routine is a no-op. |
| 5227 */ | 5302 */ |
| 5228 static void pagerUnlockIfUnused(Pager *pPager){ | 5303 static void pagerUnlockIfUnused(Pager *pPager){ |
| 5229 if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ | 5304 if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ |
| 5230 pagerUnlockAndRollback(pPager); | 5305 pagerUnlockAndRollback(pPager); |
| 5231 } | 5306 } |
| 5232 } | 5307 } |
| 5233 | 5308 |
| 5234 /* | 5309 /* |
| 5235 ** Acquire a reference to page number pgno in pager pPager (a page | 5310 ** The page getter methods each try to acquire a reference to a |
| 5236 ** reference has type DbPage*). If the requested reference is | 5311 ** page with page number pgno. If the requested reference is |
| 5237 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. | 5312 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned. |
| 5238 ** | 5313 ** |
| 5314 ** There are different implementations of the getter method depending |
| 5315 ** on the current state of the pager. |
| 5316 ** |
| 5317 ** getPageNormal() -- The normal getter |
| 5318 ** getPageError() -- Used if the pager is in an error state |
| 5319 ** getPageMmap() -- Used if memory-mapped I/O is enabled |
| 5320 ** |
| 5239 ** If the requested page is already in the cache, it is returned. | 5321 ** If the requested page is already in the cache, it is returned. |
| 5240 ** Otherwise, a new page object is allocated and populated with data | 5322 ** Otherwise, a new page object is allocated and populated with data |
| 5241 ** read from the database file. In some cases, the pcache module may | 5323 ** read from the database file. In some cases, the pcache module may |
| 5242 ** choose not to allocate a new page object and may reuse an existing | 5324 ** choose not to allocate a new page object and may reuse an existing |
| 5243 ** object with no outstanding references. | 5325 ** object with no outstanding references. |
| 5244 ** | 5326 ** |
| 5245 ** The extra data appended to a page is always initialized to zeros the | 5327 ** The extra data appended to a page is always initialized to zeros the |
| 5246 ** first time a page is loaded into memory. If the page requested is | 5328 ** first time a page is loaded into memory. If the page requested is |
| 5247 ** already in the cache when this function is called, then the extra | 5329 ** already in the cache when this function is called, then the extra |
| 5248 ** data is left as it was when the page object was last used. | 5330 ** data is left as it was when the page object was last used. |
| 5249 ** | 5331 ** |
| 5250 ** If the database image is smaller than the requested page or if a | 5332 ** If the database image is smaller than the requested page or if |
| 5251 ** non-zero value is passed as the noContent parameter and the | 5333 ** the flags parameter contains the PAGER_GET_NOCONTENT bit and the |
| 5252 ** requested page is not already stored in the cache, then no | 5334 ** requested page is not already stored in the cache, then no |
| 5253 ** actual disk read occurs. In this case the memory image of the | 5335 ** actual disk read occurs. In this case the memory image of the |
| 5254 ** page is initialized to all zeros. | 5336 ** page is initialized to all zeros. |
| 5255 ** | 5337 ** |
| 5256 ** If noContent is true, it means that we do not care about the contents | 5338 ** If PAGER_GET_NOCONTENT is true, it means that we do not care about |
| 5257 ** of the page. This occurs in two scenarios: | 5339 ** the contents of the page. This occurs in two scenarios: |
| 5258 ** | 5340 ** |
| 5259 ** a) When reading a free-list leaf page from the database, and | 5341 ** a) When reading a free-list leaf page from the database, and |
| 5260 ** | 5342 ** |
| 5261 ** b) When a savepoint is being rolled back and we need to load | 5343 ** b) When a savepoint is being rolled back and we need to load |
| 5262 ** a new page into the cache to be filled with the data read | 5344 ** a new page into the cache to be filled with the data read |
| 5263 ** from the savepoint journal. | 5345 ** from the savepoint journal. |
| 5264 ** | 5346 ** |
| 5265 ** If noContent is true, then the data returned is zeroed instead of | 5347 ** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead |
| 5266 ** being read from the database. Additionally, the bits corresponding | 5348 ** of being read from the database. Additionally, the bits corresponding |
| 5267 ** to pgno in Pager.pInJournal (bitvec of pages already written to the | 5349 ** to pgno in Pager.pInJournal (bitvec of pages already written to the |
| 5268 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open | 5350 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open |
| 5269 ** savepoints are set. This means if the page is made writable at any | 5351 ** savepoints are set. This means if the page is made writable at any |
| 5270 ** point in the future, using a call to sqlite3PagerWrite(), its contents | 5352 ** point in the future, using a call to sqlite3PagerWrite(), its contents |
| 5271 ** will not be journaled. This saves IO. | 5353 ** will not be journaled. This saves IO. |
| 5272 ** | 5354 ** |
| 5273 ** The acquisition might fail for several reasons. In all cases, | 5355 ** The acquisition might fail for several reasons. In all cases, |
| 5274 ** an appropriate error code is returned and *ppPage is set to NULL. | 5356 ** an appropriate error code is returned and *ppPage is set to NULL. |
| 5275 ** | 5357 ** |
| 5276 ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt | 5358 ** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt |
| 5277 ** to find a page in the in-memory cache first. If the page is not already | 5359 ** to find a page in the in-memory cache first. If the page is not already |
| 5278 ** in memory, this routine goes to disk to read it in whereas Lookup() | 5360 ** in memory, this routine goes to disk to read it in whereas Lookup() |
| 5279 ** just returns 0. This routine acquires a read-lock the first time it | 5361 ** just returns 0. This routine acquires a read-lock the first time it |
| 5280 ** has to go to disk, and could also playback an old journal if necessary. | 5362 ** has to go to disk, and could also playback an old journal if necessary. |
| 5281 ** Since Lookup() never goes to disk, it never has to deal with locks | 5363 ** Since Lookup() never goes to disk, it never has to deal with locks |
| 5282 ** or journal files. | 5364 ** or journal files. |
| 5283 */ | 5365 */ |
| 5284 int sqlite3PagerGet( | 5366 static int getPageNormal( |
| 5285 Pager *pPager, /* The pager open on the database file */ | 5367 Pager *pPager, /* The pager open on the database file */ |
| 5286 Pgno pgno, /* Page number to fetch */ | 5368 Pgno pgno, /* Page number to fetch */ |
| 5287 DbPage **ppPage, /* Write a pointer to the page here */ | 5369 DbPage **ppPage, /* Write a pointer to the page here */ |
| 5288 int flags /* PAGER_GET_XXX flags */ | 5370 int flags /* PAGER_GET_XXX flags */ |
| 5289 ){ | 5371 ){ |
| 5290 int rc = SQLITE_OK; | 5372 int rc = SQLITE_OK; |
| 5291 PgHdr *pPg = 0; | 5373 PgHdr *pPg; |
| 5292 u32 iFrame = 0; /* Frame to read from WAL file */ | 5374 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */ |
| 5293 const int noContent = (flags & PAGER_GET_NOCONTENT); | 5375 sqlite3_pcache_page *pBase; |
| 5294 | 5376 |
| 5295 /* It is acceptable to use a read-only (mmap) page for any page except | 5377 assert( pPager->errCode==SQLITE_OK ); |
| 5296 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY | |
| 5297 ** flag was specified by the caller. And so long as the db is not a | |
| 5298 ** temporary or in-memory database. */ | |
| 5299 const int bMmapOk = (pgno>1 && USEFETCH(pPager) | |
| 5300 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) | |
| 5301 #ifdef SQLITE_HAS_CODEC | |
| 5302 && pPager->xCodec==0 | |
| 5303 #endif | |
| 5304 ); | |
| 5305 | |
| 5306 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here | |
| 5307 ** allows the compiler optimizer to reuse the results of the "pgno>1" | |
| 5308 ** test in the previous statement, and avoid testing pgno==0 in the | |
| 5309 ** common case where pgno is large. */ | |
| 5310 if( pgno<=1 && pgno==0 ){ | |
| 5311 return SQLITE_CORRUPT_BKPT; | |
| 5312 } | |
| 5313 assert( pPager->eState>=PAGER_READER ); | 5378 assert( pPager->eState>=PAGER_READER ); |
| 5314 assert( assert_pager_state(pPager) ); | 5379 assert( assert_pager_state(pPager) ); |
| 5315 assert( noContent==0 || bMmapOk==0 ); | |
| 5316 | |
| 5317 assert( pPager->hasHeldSharedLock==1 ); | 5380 assert( pPager->hasHeldSharedLock==1 ); |
| 5318 | 5381 |
| 5319 /* If the pager is in the error state, return an error immediately. | 5382 if( pgno==0 ) return SQLITE_CORRUPT_BKPT; |
| 5320 ** Otherwise, request the page from the PCache layer. */ | 5383 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); |
| 5321 if( pPager->errCode!=SQLITE_OK ){ | 5384 if( pBase==0 ){ |
| 5322 rc = pPager->errCode; | 5385 pPg = 0; |
| 5323 }else{ | 5386 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); |
| 5324 if( bMmapOk && pagerUseWal(pPager) ){ | 5387 if( rc!=SQLITE_OK ) goto pager_acquire_err; |
| 5325 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); | 5388 if( pBase==0 ){ |
| 5326 if( rc!=SQLITE_OK ) goto pager_acquire_err; | 5389 rc = SQLITE_NOMEM_BKPT; |
| 5327 } | 5390 goto pager_acquire_err; |
| 5328 | |
| 5329 if( bMmapOk && iFrame==0 ){ | |
| 5330 void *pData = 0; | |
| 5331 | |
| 5332 rc = sqlite3OsFetch(pPager->fd, | |
| 5333 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData | |
| 5334 ); | |
| 5335 | |
| 5336 if( rc==SQLITE_OK && pData ){ | |
| 5337 if( pPager->eState>PAGER_READER ){ | |
| 5338 pPg = sqlite3PagerLookup(pPager, pgno); | |
| 5339 } | |
| 5340 if( pPg==0 ){ | |
| 5341 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); | |
| 5342 }else{ | |
| 5343 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); | |
| 5344 } | |
| 5345 if( pPg ){ | |
| 5346 assert( rc==SQLITE_OK ); | |
| 5347 *ppPage = pPg; | |
| 5348 return SQLITE_OK; | |
| 5349 } | |
| 5350 } | |
| 5351 if( rc!=SQLITE_OK ){ | |
| 5352 goto pager_acquire_err; | |
| 5353 } | |
| 5354 } | |
| 5355 | |
| 5356 { | |
| 5357 sqlite3_pcache_page *pBase; | |
| 5358 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3); | |
| 5359 if( pBase==0 ){ | |
| 5360 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase); | |
| 5361 if( rc!=SQLITE_OK ) goto pager_acquire_err; | |
| 5362 if( pBase==0 ){ | |
| 5363 pPg = *ppPage = 0; | |
| 5364 rc = SQLITE_NOMEM; | |
| 5365 goto pager_acquire_err; | |
| 5366 } | |
| 5367 } | |
| 5368 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); | |
| 5369 assert( pPg!=0 ); | |
| 5370 } | 5391 } |
| 5371 } | 5392 } |
| 5372 | 5393 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase); |
| 5373 if( rc!=SQLITE_OK ){ | |
| 5374 /* Either the call to sqlite3PcacheFetch() returned an error or the | |
| 5375 ** pager was already in the error-state when this function was called. | |
| 5376 ** Set pPg to 0 and jump to the exception handler. */ | |
| 5377 pPg = 0; | |
| 5378 goto pager_acquire_err; | |
| 5379 } | |
| 5380 assert( pPg==(*ppPage) ); | 5394 assert( pPg==(*ppPage) ); |
| 5381 assert( pPg->pgno==pgno ); | 5395 assert( pPg->pgno==pgno ); |
| 5382 assert( pPg->pPager==pPager || pPg->pPager==0 ); | 5396 assert( pPg->pPager==pPager || pPg->pPager==0 ); |
| 5383 | 5397 |
| 5398 noContent = (flags & PAGER_GET_NOCONTENT)!=0; |
| 5384 if( pPg->pPager && !noContent ){ | 5399 if( pPg->pPager && !noContent ){ |
| 5385 /* In this case the pcache already contains an initialized copy of | 5400 /* In this case the pcache already contains an initialized copy of |
| 5386 ** the page. Return without further ado. */ | 5401 ** the page. Return without further ado. */ |
| 5387 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); | 5402 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) ); |
| 5388 pPager->aStat[PAGER_STAT_HIT]++; | 5403 pPager->aStat[PAGER_STAT_HIT]++; |
| 5389 return SQLITE_OK; | 5404 return SQLITE_OK; |
| 5390 | 5405 |
| 5391 }else{ | 5406 }else{ |
| 5392 /* The pager cache has created a new page. Its content needs to | 5407 /* The pager cache has created a new page. Its content needs to |
| 5393 ** be initialized. */ | 5408 ** be initialized. But first some error checks: |
| 5394 | 5409 ** |
| 5395 pPg->pPager = pPager; | 5410 ** (1) The maximum page number is 2^31 |
| 5396 | 5411 ** (2) Never try to fetch the locking page |
| 5397 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page | 5412 */ |
| 5398 ** number greater than this, or the unused locking-page, is requested. */ | |
| 5399 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ | 5413 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ |
| 5400 rc = SQLITE_CORRUPT_BKPT; | 5414 rc = SQLITE_CORRUPT_BKPT; |
| 5401 goto pager_acquire_err; | 5415 goto pager_acquire_err; |
| 5402 } | 5416 } |
| 5403 | 5417 |
| 5404 if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){ | 5418 pPg->pPager = pPager; |
| 5419 |
| 5420 assert( !isOpen(pPager->fd) || !MEMDB ); |
| 5421 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){ |
| 5405 if( pgno>pPager->mxPgno ){ | 5422 if( pgno>pPager->mxPgno ){ |
| 5406 rc = SQLITE_FULL; | 5423 rc = SQLITE_FULL; |
| 5407 goto pager_acquire_err; | 5424 goto pager_acquire_err; |
| 5408 } | 5425 } |
| 5409 if( noContent ){ | 5426 if( noContent ){ |
| 5410 /* Failure to set the bits in the InJournal bit-vectors is benign. | 5427 /* Failure to set the bits in the InJournal bit-vectors is benign. |
| 5411 ** It merely means that we might do some extra work to journal a | 5428 ** It merely means that we might do some extra work to journal a |
| 5412 ** page that does not need to be journaled. Nevertheless, be sure | 5429 ** page that does not need to be journaled. Nevertheless, be sure |
| 5413 ** to test the case where a malloc error occurs while trying to set | 5430 ** to test the case where a malloc error occurs while trying to set |
| 5414 ** a bit in a bit vector. | 5431 ** a bit in a bit vector. |
| 5415 */ | 5432 */ |
| 5416 sqlite3BeginBenignMalloc(); | 5433 sqlite3BeginBenignMalloc(); |
| 5417 if( pgno<=pPager->dbOrigSize ){ | 5434 if( pgno<=pPager->dbOrigSize ){ |
| 5418 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno); | 5435 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno); |
| 5419 testcase( rc==SQLITE_NOMEM ); | 5436 testcase( rc==SQLITE_NOMEM ); |
| 5420 } | 5437 } |
| 5421 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno); | 5438 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno); |
| 5422 testcase( rc==SQLITE_NOMEM ); | 5439 testcase( rc==SQLITE_NOMEM ); |
| 5423 sqlite3EndBenignMalloc(); | 5440 sqlite3EndBenignMalloc(); |
| 5424 } | 5441 } |
| 5425 memset(pPg->pData, 0, pPager->pageSize); | 5442 memset(pPg->pData, 0, pPager->pageSize); |
| 5426 IOTRACE(("ZERO %p %d\n", pPager, pgno)); | 5443 IOTRACE(("ZERO %p %d\n", pPager, pgno)); |
| 5427 }else{ | 5444 }else{ |
| 5428 if( pagerUseWal(pPager) && bMmapOk==0 ){ | 5445 u32 iFrame = 0; /* Frame to read from WAL file */ |
| 5446 if( pagerUseWal(pPager) ){ |
| 5429 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); | 5447 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); |
| 5430 if( rc!=SQLITE_OK ) goto pager_acquire_err; | 5448 if( rc!=SQLITE_OK ) goto pager_acquire_err; |
| 5431 } | 5449 } |
| 5432 assert( pPg->pPager==pPager ); | 5450 assert( pPg->pPager==pPager ); |
| 5433 pPager->aStat[PAGER_STAT_MISS]++; | 5451 pPager->aStat[PAGER_STAT_MISS]++; |
| 5434 rc = readDbPage(pPg, iFrame); | 5452 rc = readDbPage(pPg, iFrame); |
| 5435 if( rc!=SQLITE_OK ){ | 5453 if( rc!=SQLITE_OK ){ |
| 5436 goto pager_acquire_err; | 5454 goto pager_acquire_err; |
| 5437 } | 5455 } |
| 5438 } | 5456 } |
| 5439 pager_set_pagehash(pPg); | 5457 pager_set_pagehash(pPg); |
| 5440 } | 5458 } |
| 5441 | |
| 5442 return SQLITE_OK; | 5459 return SQLITE_OK; |
| 5443 | 5460 |
| 5444 pager_acquire_err: | 5461 pager_acquire_err: |
| 5445 assert( rc!=SQLITE_OK ); | 5462 assert( rc!=SQLITE_OK ); |
| 5446 if( pPg ){ | 5463 if( pPg ){ |
| 5447 sqlite3PcacheDrop(pPg); | 5464 sqlite3PcacheDrop(pPg); |
| 5448 } | 5465 } |
| 5449 pagerUnlockIfUnused(pPager); | 5466 pagerUnlockIfUnused(pPager); |
| 5450 | |
| 5451 *ppPage = 0; | 5467 *ppPage = 0; |
| 5452 return rc; | 5468 return rc; |
| 5453 } | 5469 } |
| 5454 | 5470 |
| 5471 #if SQLITE_MAX_MMAP_SIZE>0 |
| 5472 /* The page getter for when memory-mapped I/O is enabled */ |
| 5473 static int getPageMMap( |
| 5474 Pager *pPager, /* The pager open on the database file */ |
| 5475 Pgno pgno, /* Page number to fetch */ |
| 5476 DbPage **ppPage, /* Write a pointer to the page here */ |
| 5477 int flags /* PAGER_GET_XXX flags */ |
| 5478 ){ |
| 5479 int rc = SQLITE_OK; |
| 5480 PgHdr *pPg = 0; |
| 5481 u32 iFrame = 0; /* Frame to read from WAL file */ |
| 5482 |
| 5483 /* It is acceptable to use a read-only (mmap) page for any page except |
| 5484 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY |
| 5485 ** flag was specified by the caller. And so long as the db is not a |
| 5486 ** temporary or in-memory database. */ |
| 5487 const int bMmapOk = (pgno>1 |
| 5488 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY)) |
| 5489 ); |
| 5490 |
| 5491 assert( USEFETCH(pPager) ); |
| 5492 #ifdef SQLITE_HAS_CODEC |
| 5493 assert( pPager->xCodec==0 ); |
| 5494 #endif |
| 5495 |
| 5496 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here |
| 5497 ** allows the compiler optimizer to reuse the results of the "pgno>1" |
| 5498 ** test in the previous statement, and avoid testing pgno==0 in the |
| 5499 ** common case where pgno is large. */ |
| 5500 if( pgno<=1 && pgno==0 ){ |
| 5501 return SQLITE_CORRUPT_BKPT; |
| 5502 } |
| 5503 assert( pPager->eState>=PAGER_READER ); |
| 5504 assert( assert_pager_state(pPager) ); |
| 5505 assert( pPager->hasHeldSharedLock==1 ); |
| 5506 assert( pPager->errCode==SQLITE_OK ); |
| 5507 |
| 5508 if( bMmapOk && pagerUseWal(pPager) ){ |
| 5509 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame); |
| 5510 if( rc!=SQLITE_OK ){ |
| 5511 *ppPage = 0; |
| 5512 return rc; |
| 5513 } |
| 5514 } |
| 5515 if( bMmapOk && iFrame==0 ){ |
| 5516 void *pData = 0; |
| 5517 rc = sqlite3OsFetch(pPager->fd, |
| 5518 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData |
| 5519 ); |
| 5520 if( rc==SQLITE_OK && pData ){ |
| 5521 if( pPager->eState>PAGER_READER || pPager->tempFile ){ |
| 5522 pPg = sqlite3PagerLookup(pPager, pgno); |
| 5523 } |
| 5524 if( pPg==0 ){ |
| 5525 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg); |
| 5526 }else{ |
| 5527 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData); |
| 5528 } |
| 5529 if( pPg ){ |
| 5530 assert( rc==SQLITE_OK ); |
| 5531 *ppPage = pPg; |
| 5532 return SQLITE_OK; |
| 5533 } |
| 5534 } |
| 5535 if( rc!=SQLITE_OK ){ |
| 5536 *ppPage = 0; |
| 5537 return rc; |
| 5538 } |
| 5539 } |
| 5540 return getPageNormal(pPager, pgno, ppPage, flags); |
| 5541 } |
| 5542 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
| 5543 |
| 5544 /* The page getter method for when the pager is an error state */ |
| 5545 static int getPageError( |
| 5546 Pager *pPager, /* The pager open on the database file */ |
| 5547 Pgno pgno, /* Page number to fetch */ |
| 5548 DbPage **ppPage, /* Write a pointer to the page here */ |
| 5549 int flags /* PAGER_GET_XXX flags */ |
| 5550 ){ |
| 5551 UNUSED_PARAMETER(pgno); |
| 5552 UNUSED_PARAMETER(flags); |
| 5553 assert( pPager->errCode!=SQLITE_OK ); |
| 5554 *ppPage = 0; |
| 5555 return pPager->errCode; |
| 5556 } |
| 5557 |
| 5558 |
| 5559 /* Dispatch all page fetch requests to the appropriate getter method. |
| 5560 */ |
| 5561 int sqlite3PagerGet( |
| 5562 Pager *pPager, /* The pager open on the database file */ |
| 5563 Pgno pgno, /* Page number to fetch */ |
| 5564 DbPage **ppPage, /* Write a pointer to the page here */ |
| 5565 int flags /* PAGER_GET_XXX flags */ |
| 5566 ){ |
| 5567 return pPager->xGet(pPager, pgno, ppPage, flags); |
| 5568 } |
| 5569 |
| 5455 /* | 5570 /* |
| 5456 ** Acquire a page if it is already in the in-memory cache. Do | 5571 ** Acquire a page if it is already in the in-memory cache. Do |
| 5457 ** not read the page from disk. Return a pointer to the page, | 5572 ** not read the page from disk. Return a pointer to the page, |
| 5458 ** or 0 if the page is not in cache. | 5573 ** or 0 if the page is not in cache. |
| 5459 ** | 5574 ** |
| 5460 ** See also sqlite3PagerGet(). The difference between this routine | 5575 ** See also sqlite3PagerGet(). The difference between this routine |
| 5461 ** and sqlite3PagerGet() is that _get() will go to the disk and read | 5576 ** and sqlite3PagerGet() is that _get() will go to the disk and read |
| 5462 ** in the page if the page is not already in cache. This routine | 5577 ** in the page if the page is not already in cache. This routine |
| 5463 ** returns NULL if the page is not in cache or if a disk I/O error | 5578 ** returns NULL if the page is not in cache or if a disk I/O error |
| 5464 ** has ever happened. | 5579 ** has ever happened. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5528 assert( pPager->pInJournal==0 ); | 5643 assert( pPager->pInJournal==0 ); |
| 5529 | 5644 |
| 5530 /* If already in the error state, this function is a no-op. But on | 5645 /* If already in the error state, this function is a no-op. But on |
| 5531 ** the other hand, this routine is never called if we are already in | 5646 ** the other hand, this routine is never called if we are already in |
| 5532 ** an error state. */ | 5647 ** an error state. */ |
| 5533 if( NEVER(pPager->errCode) ) return pPager->errCode; | 5648 if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 5534 | 5649 |
| 5535 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ | 5650 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 5536 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); | 5651 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
| 5537 if( pPager->pInJournal==0 ){ | 5652 if( pPager->pInJournal==0 ){ |
| 5538 return SQLITE_NOMEM; | 5653 return SQLITE_NOMEM_BKPT; |
| 5539 } | 5654 } |
| 5540 | 5655 |
| 5541 /* Open the journal file if it is not already open. */ | 5656 /* Open the journal file if it is not already open. */ |
| 5542 if( !isOpen(pPager->jfd) ){ | 5657 if( !isOpen(pPager->jfd) ){ |
| 5543 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ | 5658 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 5544 sqlite3MemJournalOpen(pPager->jfd); | 5659 sqlite3MemJournalOpen(pPager->jfd); |
| 5545 }else{ | 5660 }else{ |
| 5546 const int flags = /* VFS flags to open journal file */ | 5661 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 5547 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| | 5662 int nSpill; |
| 5548 (pPager->tempFile ? | |
| 5549 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): | |
| 5550 (SQLITE_OPEN_MAIN_JOURNAL) | |
| 5551 ); | |
| 5552 | 5663 |
| 5664 if( pPager->tempFile ){ |
| 5665 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL); |
| 5666 nSpill = sqlite3Config.nStmtSpill; |
| 5667 }else{ |
| 5668 flags |= SQLITE_OPEN_MAIN_JOURNAL; |
| 5669 nSpill = jrnlBufferSize(pPager); |
| 5670 } |
| 5671 |
| 5553 /* Verify that the database still has the same name as it did when | 5672 /* Verify that the database still has the same name as it did when |
| 5554 ** it was originally opened. */ | 5673 ** it was originally opened. */ |
| 5555 rc = databaseIsUnmoved(pPager); | 5674 rc = databaseIsUnmoved(pPager); |
| 5556 if( rc==SQLITE_OK ){ | 5675 if( rc==SQLITE_OK ){ |
| 5557 #ifdef SQLITE_ENABLE_ATOMIC_WRITE | 5676 rc = sqlite3JournalOpen ( |
| 5558 rc = sqlite3JournalOpen( | 5677 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill |
| 5559 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) | |
| 5560 ); | 5678 ); |
| 5561 #else | |
| 5562 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); | |
| 5563 #endif | |
| 5564 } | 5679 } |
| 5565 } | 5680 } |
| 5566 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); | 5681 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); |
| 5567 } | 5682 } |
| 5568 | 5683 |
| 5569 | 5684 |
| 5570 /* Write the first journal header to the journal file and open | 5685 /* Write the first journal header to the journal file and open |
| 5571 ** the sub-journal if necessary. | 5686 ** the sub-journal if necessary. |
| 5572 */ | 5687 */ |
| 5573 if( rc==SQLITE_OK ){ | 5688 if( rc==SQLITE_OK ){ |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5683 u32 cksum; | 5798 u32 cksum; |
| 5684 char *pData2; | 5799 char *pData2; |
| 5685 i64 iOff = pPager->journalOff; | 5800 i64 iOff = pPager->journalOff; |
| 5686 | 5801 |
| 5687 /* We should never write to the journal file the page that | 5802 /* We should never write to the journal file the page that |
| 5688 ** contains the database locks. The following assert verifies | 5803 ** contains the database locks. The following assert verifies |
| 5689 ** that we do not. */ | 5804 ** that we do not. */ |
| 5690 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); | 5805 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 5691 | 5806 |
| 5692 assert( pPager->journalHdr<=pPager->journalOff ); | 5807 assert( pPager->journalHdr<=pPager->journalOff ); |
| 5693 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); | 5808 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); |
| 5694 cksum = pager_cksum(pPager, (u8*)pData2); | 5809 cksum = pager_cksum(pPager, (u8*)pData2); |
| 5695 | 5810 |
| 5696 /* Even if an IO or diskfull error occurs while journalling the | 5811 /* Even if an IO or diskfull error occurs while journalling the |
| 5697 ** page in the block above, set the need-sync flag for the page. | 5812 ** page in the block above, set the need-sync flag for the page. |
| 5698 ** Otherwise, when the transaction is rolled back, the logic in | 5813 ** Otherwise, when the transaction is rolled back, the logic in |
| 5699 ** playback_one_page() will think that the page needs to be restored | 5814 ** playback_one_page() will think that the page needs to be restored |
| 5700 ** in the database file. And if an IO error occurs while doing so, | 5815 ** in the database file. And if an IO error occurs while doing so, |
| 5701 ** then corruption may follow. | 5816 ** then corruption may follow. |
| 5702 */ | 5817 */ |
| 5703 pPg->flags |= PGHDR_NEED_SYNC; | 5818 pPg->flags |= PGHDR_NEED_SYNC; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5918 ** must have been written to the journal file before returning. | 6033 ** must have been written to the journal file before returning. |
| 5919 ** | 6034 ** |
| 5920 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned | 6035 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned |
| 5921 ** as appropriate. Otherwise, SQLITE_OK. | 6036 ** as appropriate. Otherwise, SQLITE_OK. |
| 5922 */ | 6037 */ |
| 5923 int sqlite3PagerWrite(PgHdr *pPg){ | 6038 int sqlite3PagerWrite(PgHdr *pPg){ |
| 5924 Pager *pPager = pPg->pPager; | 6039 Pager *pPager = pPg->pPager; |
| 5925 assert( (pPg->flags & PGHDR_MMAP)==0 ); | 6040 assert( (pPg->flags & PGHDR_MMAP)==0 ); |
| 5926 assert( pPager->eState>=PAGER_WRITER_LOCKED ); | 6041 assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 5927 assert( assert_pager_state(pPager) ); | 6042 assert( assert_pager_state(pPager) ); |
| 5928 if( pPager->errCode ){ | 6043 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ |
| 5929 return pPager->errCode; | |
| 5930 }else if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){ | |
| 5931 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); | 6044 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg); |
| 5932 return SQLITE_OK; | 6045 return SQLITE_OK; |
| 6046 }else if( pPager->errCode ){ |
| 6047 return pPager->errCode; |
| 5933 }else if( pPager->sectorSize > (u32)pPager->pageSize ){ | 6048 }else if( pPager->sectorSize > (u32)pPager->pageSize ){ |
| 6049 assert( pPager->tempFile==0 ); |
| 5934 return pagerWriteLargeSector(pPg); | 6050 return pagerWriteLargeSector(pPg); |
| 5935 }else{ | 6051 }else{ |
| 5936 return pager_write(pPg); | 6052 return pager_write(pPg); |
| 5937 } | 6053 } |
| 5938 } | 6054 } |
| 5939 | 6055 |
| 5940 /* | 6056 /* |
| 5941 ** Return TRUE if the page given in the argument was previously passed | 6057 ** Return TRUE if the page given in the argument was previously passed |
| 5942 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok | 6058 ** to sqlite3PagerWrite(). In other words, return TRUE if it is ok |
| 5943 ** to change the content of the page. | 6059 ** to change the content of the page. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 5954 ** that page might be marked as dirty. This happens, for example, when | 6070 ** that page might be marked as dirty. This happens, for example, when |
| 5955 ** the page has been added as a leaf of the freelist and so its | 6071 ** the page has been added as a leaf of the freelist and so its |
| 5956 ** content no longer matters. | 6072 ** content no longer matters. |
| 5957 ** | 6073 ** |
| 5958 ** The overlying software layer calls this routine when all of the data | 6074 ** The overlying software layer calls this routine when all of the data |
| 5959 ** on the given page is unused. The pager marks the page as clean so | 6075 ** on the given page is unused. The pager marks the page as clean so |
| 5960 ** that it does not get written to disk. | 6076 ** that it does not get written to disk. |
| 5961 ** | 6077 ** |
| 5962 ** Tests show that this optimization can quadruple the speed of large | 6078 ** Tests show that this optimization can quadruple the speed of large |
| 5963 ** DELETE operations. | 6079 ** DELETE operations. |
| 6080 ** |
| 6081 ** This optimization cannot be used with a temp-file, as the page may |
| 6082 ** have been dirty at the start of the transaction. In that case, if |
| 6083 ** memory pressure forces page pPg out of the cache, the data does need |
| 6084 ** to be written out to disk so that it may be read back in if the |
| 6085 ** current transaction is rolled back. |
| 5964 */ | 6086 */ |
| 5965 void sqlite3PagerDontWrite(PgHdr *pPg){ | 6087 void sqlite3PagerDontWrite(PgHdr *pPg){ |
| 5966 Pager *pPager = pPg->pPager; | 6088 Pager *pPager = pPg->pPager; |
| 5967 if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ | 6089 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ |
| 5968 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); | 6090 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); |
| 5969 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) | 6091 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
| 5970 pPg->flags |= PGHDR_DONT_WRITE; | 6092 pPg->flags |= PGHDR_DONT_WRITE; |
| 5971 pPg->flags &= ~PGHDR_WRITEABLE; | 6093 pPg->flags &= ~PGHDR_WRITEABLE; |
| 6094 testcase( pPg->flags & PGHDR_NEED_SYNC ); |
| 5972 pager_set_pagehash(pPg); | 6095 pager_set_pagehash(pPg); |
| 5973 } | 6096 } |
| 5974 } | 6097 } |
| 5975 | 6098 |
| 5976 /* | 6099 /* |
| 5977 ** This routine is called to increment the value of the database file | 6100 ** This routine is called to increment the value of the database file |
| 5978 ** change-counter, stored as a 4-byte big-endian integer starting at | 6101 ** change-counter, stored as a 4-byte big-endian integer starting at |
| 5979 ** byte offset 24 of the pager file. The secondary change counter at | 6102 ** byte offset 24 of the pager file. The secondary change counter at |
| 5980 ** 92 is also updated, as is the SQLite version number at offset 96. | 6103 ** 92 is also updated, as is the SQLite version number at offset 96. |
| 5981 ** | 6104 ** |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6040 } | 6163 } |
| 6041 | 6164 |
| 6042 if( rc==SQLITE_OK ){ | 6165 if( rc==SQLITE_OK ){ |
| 6043 /* Actually do the update of the change counter */ | 6166 /* Actually do the update of the change counter */ |
| 6044 pager_write_changecounter(pPgHdr); | 6167 pager_write_changecounter(pPgHdr); |
| 6045 | 6168 |
| 6046 /* If running in direct mode, write the contents of page 1 to the file. */ | 6169 /* If running in direct mode, write the contents of page 1 to the file. */ |
| 6047 if( DIRECT_MODE ){ | 6170 if( DIRECT_MODE ){ |
| 6048 const void *zBuf; | 6171 const void *zBuf; |
| 6049 assert( pPager->dbFileSize>0 ); | 6172 assert( pPager->dbFileSize>0 ); |
| 6050 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf); | 6173 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf); |
| 6051 if( rc==SQLITE_OK ){ | 6174 if( rc==SQLITE_OK ){ |
| 6052 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); | 6175 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 6053 pPager->aStat[PAGER_STAT_WRITE]++; | 6176 pPager->aStat[PAGER_STAT_WRITE]++; |
| 6054 } | 6177 } |
| 6055 if( rc==SQLITE_OK ){ | 6178 if( rc==SQLITE_OK ){ |
| 6056 /* Update the pager's copy of the change-counter. Otherwise, the | 6179 /* Update the pager's copy of the change-counter. Otherwise, the |
| 6057 ** next time a read transaction is opened the cache will be | 6180 ** next time a read transaction is opened the cache will be |
| 6058 ** flushed (as the change-counter values will not match). */ | 6181 ** flushed (as the change-counter values will not match). */ |
| 6059 const void *pCopy = (const void *)&((const char *)zBuf)[24]; | 6182 const void *pCopy = (const void *)&((const char *)zBuf)[24]; |
| 6060 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers)); | 6183 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers)); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6156 assert( pPager->eState==PAGER_WRITER_LOCKED | 6279 assert( pPager->eState==PAGER_WRITER_LOCKED |
| 6157 || pPager->eState==PAGER_WRITER_CACHEMOD | 6280 || pPager->eState==PAGER_WRITER_CACHEMOD |
| 6158 || pPager->eState==PAGER_WRITER_DBMOD | 6281 || pPager->eState==PAGER_WRITER_DBMOD |
| 6159 || pPager->eState==PAGER_ERROR | 6282 || pPager->eState==PAGER_ERROR |
| 6160 ); | 6283 ); |
| 6161 assert( assert_pager_state(pPager) ); | 6284 assert( assert_pager_state(pPager) ); |
| 6162 | 6285 |
| 6163 /* If a prior error occurred, report that error again. */ | 6286 /* If a prior error occurred, report that error again. */ |
| 6164 if( NEVER(pPager->errCode) ) return pPager->errCode; | 6287 if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 6165 | 6288 |
| 6289 /* Provide the ability to easily simulate an I/O error during testing */ |
| 6290 if( sqlite3FaultSim(400) ) return SQLITE_IOERR; |
| 6291 |
| 6166 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", | 6292 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 6167 pPager->zFilename, zMaster, pPager->dbSize)); | 6293 pPager->zFilename, zMaster, pPager->dbSize)); |
| 6168 | 6294 |
| 6169 /* If no database changes have been made, return early. */ | 6295 /* If no database changes have been made, return early. */ |
| 6170 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK; | 6296 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK; |
| 6171 | 6297 |
| 6172 if( MEMDB ){ | 6298 assert( MEMDB==0 || pPager->tempFile ); |
| 6299 assert( isOpen(pPager->fd) || pPager->tempFile ); |
| 6300 if( 0==pagerFlushOnCommit(pPager, 1) ){ |
| 6173 /* If this is an in-memory db, or no pages have been written to, or this | 6301 /* If this is an in-memory db, or no pages have been written to, or this |
| 6174 ** function has already been called, it is mostly a no-op. However, any | 6302 ** function has already been called, it is mostly a no-op. However, any |
| 6175 ** backup in progress needs to be restarted. | 6303 ** backup in progress needs to be restarted. */ |
| 6176 */ | |
| 6177 sqlite3BackupRestart(pPager->pBackup); | 6304 sqlite3BackupRestart(pPager->pBackup); |
| 6178 }else{ | 6305 }else{ |
| 6179 if( pagerUseWal(pPager) ){ | 6306 if( pagerUseWal(pPager) ){ |
| 6180 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); | 6307 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); |
| 6181 PgHdr *pPageOne = 0; | 6308 PgHdr *pPageOne = 0; |
| 6182 if( pList==0 ){ | 6309 if( pList==0 ){ |
| 6183 /* Must have at least one page for the WAL commit flag. | 6310 /* Must have at least one page for the WAL commit flag. |
| 6184 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ | 6311 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */ |
| 6185 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0); | 6312 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0); |
| 6186 pList = pPageOne; | 6313 pList = pPageOne; |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6405 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ | 6532 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ |
| 6406 int eState = pPager->eState; | 6533 int eState = pPager->eState; |
| 6407 rc = pager_end_transaction(pPager, 0, 0); | 6534 rc = pager_end_transaction(pPager, 0, 0); |
| 6408 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ | 6535 if( !MEMDB && eState>PAGER_WRITER_LOCKED ){ |
| 6409 /* This can happen using journal_mode=off. Move the pager to the error | 6536 /* This can happen using journal_mode=off. Move the pager to the error |
| 6410 ** state to indicate that the contents of the cache may not be trusted. | 6537 ** state to indicate that the contents of the cache may not be trusted. |
| 6411 ** Any active readers will get SQLITE_ABORT. | 6538 ** Any active readers will get SQLITE_ABORT. |
| 6412 */ | 6539 */ |
| 6413 pPager->errCode = SQLITE_ABORT; | 6540 pPager->errCode = SQLITE_ABORT; |
| 6414 pPager->eState = PAGER_ERROR; | 6541 pPager->eState = PAGER_ERROR; |
| 6542 setGetterMethod(pPager); |
| 6415 return rc; | 6543 return rc; |
| 6416 } | 6544 } |
| 6417 }else{ | 6545 }else{ |
| 6418 rc = pager_playback(pPager, 0); | 6546 rc = pager_playback(pPager, 0); |
| 6419 } | 6547 } |
| 6420 | 6548 |
| 6421 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); | 6549 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); |
| 6422 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT | 6550 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT |
| 6423 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR | 6551 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR |
| 6424 || rc==SQLITE_CANTOPEN | 6552 || rc==SQLITE_CANTOPEN |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6505 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE ); | 6633 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE ); |
| 6506 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 ); | 6634 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 ); |
| 6507 | 6635 |
| 6508 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT]; | 6636 *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT]; |
| 6509 if( reset ){ | 6637 if( reset ){ |
| 6510 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0; | 6638 pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0; |
| 6511 } | 6639 } |
| 6512 } | 6640 } |
| 6513 | 6641 |
| 6514 /* | 6642 /* |
| 6515 ** Return true if this is an in-memory pager. | 6643 ** Return true if this is an in-memory or temp-file backed pager. |
| 6516 */ | 6644 */ |
| 6517 int sqlite3PagerIsMemdb(Pager *pPager){ | 6645 int sqlite3PagerIsMemdb(Pager *pPager){ |
| 6518 return MEMDB; | 6646 return pPager->tempFile; |
| 6519 } | 6647 } |
| 6520 | 6648 |
| 6521 /* | 6649 /* |
| 6522 ** Check that there are at least nSavepoint savepoints open. If there are | 6650 ** Check that there are at least nSavepoint savepoints open. If there are |
| 6523 ** currently less than nSavepoints open, then open one or more savepoints | 6651 ** currently less than nSavepoints open, then open one or more savepoints |
| 6524 ** to make up the difference. If the number of savepoints is already | 6652 ** to make up the difference. If the number of savepoints is already |
| 6525 ** equal to nSavepoint, then this function is a no-op. | 6653 ** equal to nSavepoint, then this function is a no-op. |
| 6526 ** | 6654 ** |
| 6527 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error | 6655 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error |
| 6528 ** occurs while opening the sub-journal file, then an IO error code is | 6656 ** occurs while opening the sub-journal file, then an IO error code is |
| (...skipping 10 matching lines...) Expand all Loading... |
| 6539 assert( nSavepoint>nCurrent && pPager->useJournal ); | 6667 assert( nSavepoint>nCurrent && pPager->useJournal ); |
| 6540 | 6668 |
| 6541 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM | 6669 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 6542 ** if the allocation fails. Otherwise, zero the new portion in case a | 6670 ** if the allocation fails. Otherwise, zero the new portion in case a |
| 6543 ** malloc failure occurs while populating it in the for(...) loop below. | 6671 ** malloc failure occurs while populating it in the for(...) loop below. |
| 6544 */ | 6672 */ |
| 6545 aNew = (PagerSavepoint *)sqlite3Realloc( | 6673 aNew = (PagerSavepoint *)sqlite3Realloc( |
| 6546 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint | 6674 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint |
| 6547 ); | 6675 ); |
| 6548 if( !aNew ){ | 6676 if( !aNew ){ |
| 6549 return SQLITE_NOMEM; | 6677 return SQLITE_NOMEM_BKPT; |
| 6550 } | 6678 } |
| 6551 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); | 6679 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); |
| 6552 pPager->aSavepoint = aNew; | 6680 pPager->aSavepoint = aNew; |
| 6553 | 6681 |
| 6554 /* Populate the PagerSavepoint structures just allocated. */ | 6682 /* Populate the PagerSavepoint structures just allocated. */ |
| 6555 for(ii=nCurrent; ii<nSavepoint; ii++){ | 6683 for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 6556 aNew[ii].nOrig = pPager->dbSize; | 6684 aNew[ii].nOrig = pPager->dbSize; |
| 6557 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ | 6685 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 6558 aNew[ii].iOffset = pPager->journalOff; | 6686 aNew[ii].iOffset = pPager->journalOff; |
| 6559 }else{ | 6687 }else{ |
| 6560 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); | 6688 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 6561 } | 6689 } |
| 6562 aNew[ii].iSubRec = pPager->nSubRec; | 6690 aNew[ii].iSubRec = pPager->nSubRec; |
| 6563 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); | 6691 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 6564 if( !aNew[ii].pInSavepoint ){ | 6692 if( !aNew[ii].pInSavepoint ){ |
| 6565 return SQLITE_NOMEM; | 6693 return SQLITE_NOMEM_BKPT; |
| 6566 } | 6694 } |
| 6567 if( pagerUseWal(pPager) ){ | 6695 if( pagerUseWal(pPager) ){ |
| 6568 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); | 6696 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); |
| 6569 } | 6697 } |
| 6570 pPager->nSavepoint = ii+1; | 6698 pPager->nSavepoint = ii+1; |
| 6571 } | 6699 } |
| 6572 assert( pPager->nSavepoint==nSavepoint ); | 6700 assert( pPager->nSavepoint==nSavepoint ); |
| 6573 assertTruncateConstraint(pPager); | 6701 assertTruncateConstraint(pPager); |
| 6574 return rc; | 6702 return rc; |
| 6575 } | 6703 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6609 ** | 6737 ** |
| 6610 ** In any case, all savepoints with an index greater than iSavepoint | 6738 ** In any case, all savepoints with an index greater than iSavepoint |
| 6611 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), | 6739 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE), |
| 6612 ** then savepoint iSavepoint is also destroyed. | 6740 ** then savepoint iSavepoint is also destroyed. |
| 6613 ** | 6741 ** |
| 6614 ** This function may return SQLITE_NOMEM if a memory allocation fails, | 6742 ** This function may return SQLITE_NOMEM if a memory allocation fails, |
| 6615 ** or an IO error code if an IO error occurs while rolling back a | 6743 ** or an IO error code if an IO error occurs while rolling back a |
| 6616 ** savepoint. If no errors occur, SQLITE_OK is returned. | 6744 ** savepoint. If no errors occur, SQLITE_OK is returned. |
| 6617 */ | 6745 */ |
| 6618 int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ | 6746 int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ |
| 6619 int rc = pPager->errCode; /* Return code */ | 6747 int rc = pPager->errCode; |
| 6748 |
| 6749 #ifdef SQLITE_ENABLE_ZIPVFS |
| 6750 if( op==SAVEPOINT_RELEASE ) rc = SQLITE_OK; |
| 6751 #endif |
| 6620 | 6752 |
| 6621 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); | 6753 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 6622 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); | 6754 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); |
| 6623 | 6755 |
| 6624 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){ | 6756 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){ |
| 6625 int ii; /* Iterator variable */ | 6757 int ii; /* Iterator variable */ |
| 6626 int nNew; /* Number of remaining savepoints after this op. */ | 6758 int nNew; /* Number of remaining savepoints after this op. */ |
| 6627 | 6759 |
| 6628 /* Figure out how many savepoints will still be active after this | 6760 /* Figure out how many savepoints will still be active after this |
| 6629 ** operation. Store this value in nNew. Then free resources associated | 6761 ** operation. Store this value in nNew. Then free resources associated |
| 6630 ** with any savepoints that are destroyed by this operation. | 6762 ** with any savepoints that are destroyed by this operation. |
| 6631 */ | 6763 */ |
| 6632 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1); | 6764 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1); |
| 6633 for(ii=nNew; ii<pPager->nSavepoint; ii++){ | 6765 for(ii=nNew; ii<pPager->nSavepoint; ii++){ |
| 6634 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); | 6766 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint); |
| 6635 } | 6767 } |
| 6636 pPager->nSavepoint = nNew; | 6768 pPager->nSavepoint = nNew; |
| 6637 | 6769 |
| 6638 /* If this is a release of the outermost savepoint, truncate | 6770 /* If this is a release of the outermost savepoint, truncate |
| 6639 ** the sub-journal to zero bytes in size. */ | 6771 ** the sub-journal to zero bytes in size. */ |
| 6640 if( op==SAVEPOINT_RELEASE ){ | 6772 if( op==SAVEPOINT_RELEASE ){ |
| 6641 if( nNew==0 && isOpen(pPager->sjfd) ){ | 6773 if( nNew==0 && isOpen(pPager->sjfd) ){ |
| 6642 /* Only truncate if it is an in-memory sub-journal. */ | 6774 /* Only truncate if it is an in-memory sub-journal. */ |
| 6643 if( sqlite3IsMemJournal(pPager->sjfd) ){ | 6775 if( sqlite3JournalIsInMemory(pPager->sjfd) ){ |
| 6644 rc = sqlite3OsTruncate(pPager->sjfd, 0); | 6776 rc = sqlite3OsTruncate(pPager->sjfd, 0); |
| 6645 assert( rc==SQLITE_OK ); | 6777 assert( rc==SQLITE_OK ); |
| 6646 } | 6778 } |
| 6647 pPager->nSubRec = 0; | 6779 pPager->nSubRec = 0; |
| 6648 } | 6780 } |
| 6649 } | 6781 } |
| 6650 /* Else this is a rollback operation, playback the specified savepoint. | 6782 /* Else this is a rollback operation, playback the specified savepoint. |
| 6651 ** If this is a temp-file, it is possible that the journal file has | 6783 ** If this is a temp-file, it is possible that the journal file has |
| 6652 ** not yet been opened. In this case there have been no changes to | 6784 ** not yet been opened. In this case there have been no changes to |
| 6653 ** the database file, so the playback operation can be skipped. | 6785 ** the database file, so the playback operation can be skipped. |
| 6654 */ | 6786 */ |
| 6655 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){ | 6787 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){ |
| 6656 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; | 6788 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; |
| 6657 rc = pagerPlaybackSavepoint(pPager, pSavepoint); | 6789 rc = pagerPlaybackSavepoint(pPager, pSavepoint); |
| 6658 assert(rc!=SQLITE_DONE); | 6790 assert(rc!=SQLITE_DONE); |
| 6659 } | 6791 } |
| 6792 |
| 6793 #ifdef SQLITE_ENABLE_ZIPVFS |
| 6794 /* If the cache has been modified but the savepoint cannot be rolled |
| 6795 ** back journal_mode=off, put the pager in the error state. This way, |
| 6796 ** if the VFS used by this pager includes ZipVFS, the entire transaction |
| 6797 ** can be rolled back at the ZipVFS level. */ |
| 6798 else if( |
| 6799 pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 6800 && pPager->eState>=PAGER_WRITER_CACHEMOD |
| 6801 ){ |
| 6802 pPager->errCode = SQLITE_ABORT; |
| 6803 pPager->eState = PAGER_ERROR; |
| 6804 setGetterMethod(pPager); |
| 6805 } |
| 6806 #endif |
| 6660 } | 6807 } |
| 6661 | 6808 |
| 6662 return rc; | 6809 return rc; |
| 6663 } | 6810 } |
| 6664 | 6811 |
| 6665 /* | 6812 /* |
| 6666 ** Return the full pathname of the database file. | 6813 ** Return the full pathname of the database file. |
| 6667 ** | 6814 ** |
| 6668 ** Except, if the pager is in-memory only, then return an empty string if | 6815 ** Except, if the pager is in-memory only, then return an empty string if |
| 6669 ** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when | 6816 ** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6704 #endif | 6851 #endif |
| 6705 } | 6852 } |
| 6706 | 6853 |
| 6707 /* | 6854 /* |
| 6708 ** Return the full pathname of the journal file. | 6855 ** Return the full pathname of the journal file. |
| 6709 */ | 6856 */ |
| 6710 const char *sqlite3PagerJournalname(Pager *pPager){ | 6857 const char *sqlite3PagerJournalname(Pager *pPager){ |
| 6711 return pPager->zJournal; | 6858 return pPager->zJournal; |
| 6712 } | 6859 } |
| 6713 | 6860 |
| 6714 /* | |
| 6715 ** Return true if fsync() calls are disabled for this pager. Return FALSE | |
| 6716 ** if fsync()s are executed normally. | |
| 6717 */ | |
| 6718 int sqlite3PagerNosync(Pager *pPager){ | |
| 6719 return pPager->noSync; | |
| 6720 } | |
| 6721 | |
| 6722 #ifdef SQLITE_HAS_CODEC | 6861 #ifdef SQLITE_HAS_CODEC |
| 6723 /* | 6862 /* |
| 6724 ** Set or retrieve the codec for this pager | 6863 ** Set or retrieve the codec for this pager |
| 6725 */ | 6864 */ |
| 6726 void sqlite3PagerSetCodec( | 6865 void sqlite3PagerSetCodec( |
| 6727 Pager *pPager, | 6866 Pager *pPager, |
| 6728 void *(*xCodec)(void*,void*,Pgno,int), | 6867 void *(*xCodec)(void*,void*,Pgno,int), |
| 6729 void (*xCodecSizeChng)(void*,int,int), | 6868 void (*xCodecSizeChng)(void*,int,int), |
| 6730 void (*xCodecFree)(void*), | 6869 void (*xCodecFree)(void*), |
| 6731 void *pCodec | 6870 void *pCodec |
| 6732 ){ | 6871 ){ |
| 6733 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); | 6872 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); |
| 6734 pPager->xCodec = pPager->memDb ? 0 : xCodec; | 6873 pPager->xCodec = pPager->memDb ? 0 : xCodec; |
| 6735 pPager->xCodecSizeChng = xCodecSizeChng; | 6874 pPager->xCodecSizeChng = xCodecSizeChng; |
| 6736 pPager->xCodecFree = xCodecFree; | 6875 pPager->xCodecFree = xCodecFree; |
| 6737 pPager->pCodec = pCodec; | 6876 pPager->pCodec = pCodec; |
| 6877 setGetterMethod(pPager); |
| 6738 pagerReportSize(pPager); | 6878 pagerReportSize(pPager); |
| 6739 } | 6879 } |
| 6740 void *sqlite3PagerGetCodec(Pager *pPager){ | 6880 void *sqlite3PagerGetCodec(Pager *pPager){ |
| 6741 return pPager->pCodec; | 6881 return pPager->pCodec; |
| 6742 } | 6882 } |
| 6743 | 6883 |
| 6744 /* | 6884 /* |
| 6745 ** This function is called by the wal module when writing page content | 6885 ** This function is called by the wal module when writing page content |
| 6746 ** into the log file. | 6886 ** into the log file. |
| 6747 ** | 6887 ** |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6796 | 6936 |
| 6797 assert( pPg->nRef>0 ); | 6937 assert( pPg->nRef>0 ); |
| 6798 assert( pPager->eState==PAGER_WRITER_CACHEMOD | 6938 assert( pPager->eState==PAGER_WRITER_CACHEMOD |
| 6799 || pPager->eState==PAGER_WRITER_DBMOD | 6939 || pPager->eState==PAGER_WRITER_DBMOD |
| 6800 ); | 6940 ); |
| 6801 assert( assert_pager_state(pPager) ); | 6941 assert( assert_pager_state(pPager) ); |
| 6802 | 6942 |
| 6803 /* In order to be able to rollback, an in-memory database must journal | 6943 /* In order to be able to rollback, an in-memory database must journal |
| 6804 ** the page we are moving from. | 6944 ** the page we are moving from. |
| 6805 */ | 6945 */ |
| 6806 if( MEMDB ){ | 6946 assert( pPager->tempFile || !MEMDB ); |
| 6947 if( pPager->tempFile ){ |
| 6807 rc = sqlite3PagerWrite(pPg); | 6948 rc = sqlite3PagerWrite(pPg); |
| 6808 if( rc ) return rc; | 6949 if( rc ) return rc; |
| 6809 } | 6950 } |
| 6810 | 6951 |
| 6811 /* If the page being moved is dirty and has not been saved by the latest | 6952 /* If the page being moved is dirty and has not been saved by the latest |
| 6812 ** savepoint, then save the current contents of the page into the | 6953 ** savepoint, then save the current contents of the page into the |
| 6813 ** sub-journal now. This is required to handle the following scenario: | 6954 ** sub-journal now. This is required to handle the following scenario: |
| 6814 ** | 6955 ** |
| 6815 ** BEGIN; | 6956 ** BEGIN; |
| 6816 ** <journal page X, then modify it in memory> | 6957 ** <journal page X, then modify it in memory> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6853 /* If the cache contains a page with page-number pgno, remove it | 6994 /* If the cache contains a page with page-number pgno, remove it |
| 6854 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for | 6995 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for |
| 6855 ** page pgno before the 'move' operation, it needs to be retained | 6996 ** page pgno before the 'move' operation, it needs to be retained |
| 6856 ** for the page moved there. | 6997 ** for the page moved there. |
| 6857 */ | 6998 */ |
| 6858 pPg->flags &= ~PGHDR_NEED_SYNC; | 6999 pPg->flags &= ~PGHDR_NEED_SYNC; |
| 6859 pPgOld = sqlite3PagerLookup(pPager, pgno); | 7000 pPgOld = sqlite3PagerLookup(pPager, pgno); |
| 6860 assert( !pPgOld || pPgOld->nRef==1 ); | 7001 assert( !pPgOld || pPgOld->nRef==1 ); |
| 6861 if( pPgOld ){ | 7002 if( pPgOld ){ |
| 6862 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); | 7003 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
| 6863 if( MEMDB ){ | 7004 if( pPager->tempFile ){ |
| 6864 /* Do not discard pages from an in-memory database since we might | 7005 /* Do not discard pages from an in-memory database since we might |
| 6865 ** need to rollback later. Just move the page out of the way. */ | 7006 ** need to rollback later. Just move the page out of the way. */ |
| 6866 sqlite3PcacheMove(pPgOld, pPager->dbSize+1); | 7007 sqlite3PcacheMove(pPgOld, pPager->dbSize+1); |
| 6867 }else{ | 7008 }else{ |
| 6868 sqlite3PcacheDrop(pPgOld); | 7009 sqlite3PcacheDrop(pPgOld); |
| 6869 } | 7010 } |
| 6870 } | 7011 } |
| 6871 | 7012 |
| 6872 origPgno = pPg->pgno; | 7013 origPgno = pPg->pgno; |
| 6873 sqlite3PcacheMove(pPg, pgno); | 7014 sqlite3PcacheMove(pPg, pgno); |
| 6874 sqlite3PcacheMakeDirty(pPg); | 7015 sqlite3PcacheMakeDirty(pPg); |
| 6875 | 7016 |
| 6876 /* For an in-memory database, make sure the original page continues | 7017 /* For an in-memory database, make sure the original page continues |
| 6877 ** to exist, in case the transaction needs to roll back. Use pPgOld | 7018 ** to exist, in case the transaction needs to roll back. Use pPgOld |
| 6878 ** as the original page since it has already been allocated. | 7019 ** as the original page since it has already been allocated. |
| 6879 */ | 7020 */ |
| 6880 if( MEMDB ){ | 7021 if( pPager->tempFile && pPgOld ){ |
| 6881 assert( pPgOld ); | |
| 6882 sqlite3PcacheMove(pPgOld, origPgno); | 7022 sqlite3PcacheMove(pPgOld, origPgno); |
| 6883 sqlite3PagerUnrefNotNull(pPgOld); | 7023 sqlite3PagerUnrefNotNull(pPgOld); |
| 6884 } | 7024 } |
| 6885 | 7025 |
| 6886 if( needSyncPgno ){ | 7026 if( needSyncPgno ){ |
| 6887 /* If needSyncPgno is non-zero, then the journal file needs to be | 7027 /* If needSyncPgno is non-zero, then the journal file needs to be |
| 6888 ** sync()ed before any data is written to database file page needSyncPgno. | 7028 ** sync()ed before any data is written to database file page needSyncPgno. |
| 6889 ** Currently, no such page exists in the page-cache and the | 7029 ** Currently, no such page exists in the page-cache and the |
| 6890 ** "is journaled" bitvec flag has been set. This needs to be remedied by | 7030 ** "is journaled" bitvec flag has been set. This needs to be remedied by |
| 6891 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC | 7031 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7123 */ | 7263 */ |
| 7124 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ | 7264 sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){ |
| 7125 return &pPager->pBackup; | 7265 return &pPager->pBackup; |
| 7126 } | 7266 } |
| 7127 | 7267 |
| 7128 #ifndef SQLITE_OMIT_VACUUM | 7268 #ifndef SQLITE_OMIT_VACUUM |
| 7129 /* | 7269 /* |
| 7130 ** Unless this is an in-memory or temporary database, clear the pager cache. | 7270 ** Unless this is an in-memory or temporary database, clear the pager cache. |
| 7131 */ | 7271 */ |
| 7132 void sqlite3PagerClearCache(Pager *pPager){ | 7272 void sqlite3PagerClearCache(Pager *pPager){ |
| 7133 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager); | 7273 assert( MEMDB==0 || pPager->tempFile ); |
| 7274 if( pPager->tempFile==0 ) pager_reset(pPager); |
| 7134 } | 7275 } |
| 7135 #endif | 7276 #endif |
| 7136 | 7277 |
| 7278 |
| 7137 #ifndef SQLITE_OMIT_WAL | 7279 #ifndef SQLITE_OMIT_WAL |
| 7138 /* | 7280 /* |
| 7139 ** This function is called when the user invokes "PRAGMA wal_checkpoint", | 7281 ** This function is called when the user invokes "PRAGMA wal_checkpoint", |
| 7140 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint() | 7282 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint() |
| 7141 ** or wal_blocking_checkpoint() API functions. | 7283 ** or wal_blocking_checkpoint() API functions. |
| 7142 ** | 7284 ** |
| 7143 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. | 7285 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. |
| 7144 */ | 7286 */ |
| 7145 int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){ | 7287 int sqlite3PagerCheckpoint( |
| 7288 Pager *pPager, /* Checkpoint on this pager */ |
| 7289 sqlite3 *db, /* Db handle used to check for interrupts */ |
| 7290 int eMode, /* Type of checkpoint */ |
| 7291 int *pnLog, /* OUT: Final number of frames in log */ |
| 7292 int *pnCkpt /* OUT: Final number of checkpointed frames */ |
| 7293 ){ |
| 7146 int rc = SQLITE_OK; | 7294 int rc = SQLITE_OK; |
| 7147 if( pPager->pWal ){ | 7295 if( pPager->pWal ){ |
| 7148 rc = sqlite3WalCheckpoint(pPager->pWal, eMode, | 7296 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode, |
| 7149 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), | 7297 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler), |
| 7150 pPager->pBusyHandlerArg, | 7298 pPager->pBusyHandlerArg, |
| 7151 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, | 7299 pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace, |
| 7152 pnLog, pnCkpt | 7300 pnLog, pnCkpt |
| 7153 ); | 7301 ); |
| 7154 } | 7302 } |
| 7155 return rc; | 7303 return rc; |
| 7156 } | 7304 } |
| 7157 | 7305 |
| 7158 int sqlite3PagerWalCallback(Pager *pPager){ | 7306 int sqlite3PagerWalCallback(Pager *pPager){ |
| 7159 return sqlite3WalCallback(pPager->pWal); | 7307 return sqlite3WalCallback(pPager->pWal); |
| 7160 } | 7308 } |
| 7161 | 7309 |
| 7162 /* | 7310 /* |
| 7163 ** Return true if the underlying VFS for the given pager supports the | 7311 ** Return true if the underlying VFS for the given pager supports the |
| 7164 ** primitives necessary for write-ahead logging. | 7312 ** primitives necessary for write-ahead logging. |
| 7165 */ | 7313 */ |
| 7166 int sqlite3PagerWalSupported(Pager *pPager){ | 7314 int sqlite3PagerWalSupported(Pager *pPager){ |
| 7167 const sqlite3_io_methods *pMethods = pPager->fd->pMethods; | 7315 const sqlite3_io_methods *pMethods = pPager->fd->pMethods; |
| 7316 if( pPager->noLock ) return 0; |
| 7168 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap); | 7317 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap); |
| 7169 } | 7318 } |
| 7170 | 7319 |
| 7171 /* | 7320 /* |
| 7172 ** Attempt to take an exclusive lock on the database file. If a PENDING lock | 7321 ** Attempt to take an exclusive lock on the database file. If a PENDING lock |
| 7173 ** is obtained instead, immediately release it. | 7322 ** is obtained instead, immediately release it. |
| 7174 */ | 7323 */ |
| 7175 static int pagerExclusiveLock(Pager *pPager){ | 7324 static int pagerExclusiveLock(Pager *pPager){ |
| 7176 int rc; /* Return code */ | 7325 int rc; /* Return code */ |
| 7177 | 7326 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7269 | 7418 |
| 7270 /* | 7419 /* |
| 7271 ** This function is called to close the connection to the log file prior | 7420 ** This function is called to close the connection to the log file prior |
| 7272 ** to switching from WAL to rollback mode. | 7421 ** to switching from WAL to rollback mode. |
| 7273 ** | 7422 ** |
| 7274 ** Before closing the log file, this function attempts to take an | 7423 ** Before closing the log file, this function attempts to take an |
| 7275 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an | 7424 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an |
| 7276 ** error (SQLITE_BUSY) is returned and the log connection is not closed. | 7425 ** error (SQLITE_BUSY) is returned and the log connection is not closed. |
| 7277 ** If successful, the EXCLUSIVE lock is not released before returning. | 7426 ** If successful, the EXCLUSIVE lock is not released before returning. |
| 7278 */ | 7427 */ |
| 7279 int sqlite3PagerCloseWal(Pager *pPager){ | 7428 int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ |
| 7280 int rc = SQLITE_OK; | 7429 int rc = SQLITE_OK; |
| 7281 | 7430 |
| 7282 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); | 7431 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); |
| 7283 | 7432 |
| 7284 /* If the log file is not already open, but does exist in the file-system, | 7433 /* If the log file is not already open, but does exist in the file-system, |
| 7285 ** it may need to be checkpointed before the connection can switch to | 7434 ** it may need to be checkpointed before the connection can switch to |
| 7286 ** rollback mode. Open it now so this can happen. | 7435 ** rollback mode. Open it now so this can happen. |
| 7287 */ | 7436 */ |
| 7288 if( !pPager->pWal ){ | 7437 if( !pPager->pWal ){ |
| 7289 int logexists = 0; | 7438 int logexists = 0; |
| 7290 rc = pagerLockDb(pPager, SHARED_LOCK); | 7439 rc = pagerLockDb(pPager, SHARED_LOCK); |
| 7291 if( rc==SQLITE_OK ){ | 7440 if( rc==SQLITE_OK ){ |
| 7292 rc = sqlite3OsAccess( | 7441 rc = sqlite3OsAccess( |
| 7293 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists | 7442 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists |
| 7294 ); | 7443 ); |
| 7295 } | 7444 } |
| 7296 if( rc==SQLITE_OK && logexists ){ | 7445 if( rc==SQLITE_OK && logexists ){ |
| 7297 rc = pagerOpenWal(pPager); | 7446 rc = pagerOpenWal(pPager); |
| 7298 } | 7447 } |
| 7299 } | 7448 } |
| 7300 | 7449 |
| 7301 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on | 7450 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on |
| 7302 ** the database file, the log and log-summary files will be deleted. | 7451 ** the database file, the log and log-summary files will be deleted. |
| 7303 */ | 7452 */ |
| 7304 if( rc==SQLITE_OK && pPager->pWal ){ | 7453 if( rc==SQLITE_OK && pPager->pWal ){ |
| 7305 rc = pagerExclusiveLock(pPager); | 7454 rc = pagerExclusiveLock(pPager); |
| 7306 if( rc==SQLITE_OK ){ | 7455 if( rc==SQLITE_OK ){ |
| 7307 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, | 7456 rc = sqlite3WalClose(pPager->pWal, db, pPager->ckptSyncFlags, |
| 7308 pPager->pageSize, (u8*)pPager->pTmpSpace); | 7457 pPager->pageSize, (u8*)pPager->pTmpSpace); |
| 7309 pPager->pWal = 0; | 7458 pPager->pWal = 0; |
| 7310 pagerFixMaplimit(pPager); | 7459 pagerFixMaplimit(pPager); |
| 7460 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK); |
| 7311 } | 7461 } |
| 7312 } | 7462 } |
| 7313 return rc; | 7463 return rc; |
| 7314 } | 7464 } |
| 7315 | 7465 |
| 7316 #ifdef SQLITE_ENABLE_SNAPSHOT | 7466 #ifdef SQLITE_ENABLE_SNAPSHOT |
| 7317 /* | 7467 /* |
| 7318 ** If this is a WAL database, obtain a snapshot handle for the snapshot | 7468 ** If this is a WAL database, obtain a snapshot handle for the snapshot |
| 7319 ** currently open. Otherwise, return an error. | 7469 ** currently open. Otherwise, return an error. |
| 7320 */ | 7470 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 7333 */ | 7483 */ |
| 7334 int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ | 7484 int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){ |
| 7335 int rc = SQLITE_OK; | 7485 int rc = SQLITE_OK; |
| 7336 if( pPager->pWal ){ | 7486 if( pPager->pWal ){ |
| 7337 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); | 7487 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot); |
| 7338 }else{ | 7488 }else{ |
| 7339 rc = SQLITE_ERROR; | 7489 rc = SQLITE_ERROR; |
| 7340 } | 7490 } |
| 7341 return rc; | 7491 return rc; |
| 7342 } | 7492 } |
| 7493 |
| 7494 /* |
| 7495 ** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this |
| 7496 ** is not a WAL database, return an error. |
| 7497 */ |
| 7498 int sqlite3PagerSnapshotRecover(Pager *pPager){ |
| 7499 int rc; |
| 7500 if( pPager->pWal ){ |
| 7501 rc = sqlite3WalSnapshotRecover(pPager->pWal); |
| 7502 }else{ |
| 7503 rc = SQLITE_ERROR; |
| 7504 } |
| 7505 return rc; |
| 7506 } |
| 7343 #endif /* SQLITE_ENABLE_SNAPSHOT */ | 7507 #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 7344 #endif /* !SQLITE_OMIT_WAL */ | 7508 #endif /* !SQLITE_OMIT_WAL */ |
| 7345 | 7509 |
| 7346 #ifdef SQLITE_ENABLE_ZIPVFS | 7510 #ifdef SQLITE_ENABLE_ZIPVFS |
| 7347 /* | 7511 /* |
| 7348 ** A read-lock must be held on the pager when this function is called. If | 7512 ** A read-lock must be held on the pager when this function is called. If |
| 7349 ** the pager is in WAL mode and the WAL file currently contains one or more | 7513 ** the pager is in WAL mode and the WAL file currently contains one or more |
| 7350 ** frames, return the size in bytes of the page images stored within the | 7514 ** frames, return the size in bytes of the page images stored within the |
| 7351 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file | 7515 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file |
| 7352 ** is empty, return 0. | 7516 ** is empty, return 0. |
| 7353 */ | 7517 */ |
| 7354 int sqlite3PagerWalFramesize(Pager *pPager){ | 7518 int sqlite3PagerWalFramesize(Pager *pPager){ |
| 7355 assert( pPager->eState>=PAGER_READER ); | 7519 assert( pPager->eState>=PAGER_READER ); |
| 7356 return sqlite3WalFramesize(pPager->pWal); | 7520 return sqlite3WalFramesize(pPager->pWal); |
| 7357 } | 7521 } |
| 7358 #endif | 7522 #endif |
| 7359 | 7523 |
| 7360 | |
| 7361 #endif /* SQLITE_OMIT_DISKIO */ | 7524 #endif /* SQLITE_OMIT_DISKIO */ |
| OLD | NEW |