| | @@ -636,11 +636,11 @@ |
| 636 | 636 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 637 | 637 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 638 | 638 | */ |
| 639 | 639 | #define SQLITE_VERSION "3.7.0" |
| 640 | 640 | #define SQLITE_VERSION_NUMBER 3007000 |
| 641 | | -#define SQLITE_SOURCE_ID "2010-06-21 12:47:41 ee0acef1faffd480fd2136f81fb2b6f6a17b5388" |
| 641 | +#define SQLITE_SOURCE_ID "2010-06-26 20:25:31 f149b498b6ada3fc9f71ee104c351554c80c7f8a" |
| 642 | 642 | |
| 643 | 643 | /* |
| 644 | 644 | ** CAPI3REF: Run-Time Library Version Numbers |
| 645 | 645 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 646 | 646 | ** |
| | @@ -7858,10 +7858,14 @@ |
| 7858 | 7858 | SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); |
| 7859 | 7859 | SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); |
| 7860 | 7860 | |
| 7861 | 7861 | /* Functions used to truncate the database file. */ |
| 7862 | 7862 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); |
| 7863 | + |
| 7864 | +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) |
| 7865 | +SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); |
| 7866 | +#endif |
| 7863 | 7867 | |
| 7864 | 7868 | /* Functions to support testing and debugging. */ |
| 7865 | 7869 | #if !defined(NDEBUG) || defined(SQLITE_TEST) |
| 7866 | 7870 | SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*); |
| 7867 | 7871 | SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*); |
| | @@ -16798,15 +16802,15 @@ |
| 16798 | 16802 | SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ |
| 16799 | 16803 | void *p; |
| 16800 | 16804 | assert( n>0 ); |
| 16801 | 16805 | |
| 16802 | 16806 | #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 16803 | | - /* Verify that no more than one scratch allocation per thread |
| 16807 | + /* Verify that no more than two scratch allocation per thread |
| 16804 | 16808 | ** is outstanding at one time. (This is only checked in the |
| 16805 | 16809 | ** single-threaded case since checking in the multi-threaded case |
| 16806 | 16810 | ** would be much more complicated.) */ |
| 16807 | | - assert( scratchAllocOut==0 ); |
| 16811 | + assert( scratchAllocOut<=1 ); |
| 16808 | 16812 | #endif |
| 16809 | 16813 | |
| 16810 | 16814 | if( sqlite3GlobalConfig.szScratch<n ){ |
| 16811 | 16815 | goto scratch_overflow; |
| 16812 | 16816 | }else{ |
| | @@ -16847,20 +16851,10 @@ |
| 16847 | 16851 | #endif |
| 16848 | 16852 | return p; |
| 16849 | 16853 | } |
| 16850 | 16854 | SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ |
| 16851 | 16855 | if( p ){ |
| 16852 | | - |
| 16853 | | -#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 16854 | | - /* Verify that no more than one scratch allocation per thread |
| 16855 | | - ** is outstanding at one time. (This is only checked in the |
| 16856 | | - ** single-threaded case since checking in the multi-threaded case |
| 16857 | | - ** would be much more complicated.) */ |
| 16858 | | - assert( scratchAllocOut==1 ); |
| 16859 | | - scratchAllocOut = 0; |
| 16860 | | -#endif |
| 16861 | | - |
| 16862 | 16856 | if( sqlite3GlobalConfig.pScratch==0 |
| 16863 | 16857 | || p<sqlite3GlobalConfig.pScratch |
| 16864 | 16858 | || p>=(void*)mem0.aScratchFree ){ |
| 16865 | 16859 | assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 16866 | 16860 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| | @@ -16882,10 +16876,20 @@ |
| 16882 | 16876 | sqlite3_mutex_enter(mem0.mutex); |
| 16883 | 16877 | assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch ); |
| 16884 | 16878 | mem0.aScratchFree[mem0.nScratchFree++] = i; |
| 16885 | 16879 | sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1); |
| 16886 | 16880 | sqlite3_mutex_leave(mem0.mutex); |
| 16881 | + |
| 16882 | +#if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 16883 | + /* Verify that no more than two scratch allocation per thread |
| 16884 | + ** is outstanding at one time. (This is only checked in the |
| 16885 | + ** single-threaded case since checking in the multi-threaded case |
| 16886 | + ** would be much more complicated.) */ |
| 16887 | + assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 16888 | + scratchAllocOut = 0; |
| 16889 | +#endif |
| 16890 | + |
| 16887 | 16891 | } |
| 16888 | 16892 | } |
| 16889 | 16893 | } |
| 16890 | 16894 | |
| 16891 | 16895 | /* |
| | @@ -25582,11 +25586,11 @@ |
| 25582 | 25586 | }; |
| 25583 | 25587 | |
| 25584 | 25588 | /* |
| 25585 | 25589 | ** Constants used for locking |
| 25586 | 25590 | */ |
| 25587 | | -#define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 25591 | +#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 25588 | 25592 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
| 25589 | 25593 | |
| 25590 | 25594 | /* |
| 25591 | 25595 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
| 25592 | 25596 | ** |
| | @@ -30013,11 +30017,11 @@ |
| 30013 | 30017 | }; |
| 30014 | 30018 | |
| 30015 | 30019 | /* |
| 30016 | 30020 | ** Constants used for locking |
| 30017 | 30021 | */ |
| 30018 | | -#define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 30022 | +#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 30019 | 30023 | #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
| 30020 | 30024 | |
| 30021 | 30025 | /* |
| 30022 | 30026 | ** Apply advisory locks for all n bytes beginning at ofst. |
| 30023 | 30027 | */ |
| | @@ -30138,11 +30142,11 @@ |
| 30138 | 30142 | sqlite3_free(p); |
| 30139 | 30143 | return SQLITE_NOMEM; |
| 30140 | 30144 | } |
| 30141 | 30145 | memset(pNew, 0, sizeof(*pNew)); |
| 30142 | 30146 | pNew->zFilename = (char*)&pNew[1]; |
| 30143 | | - sqlite3_snprintf(nName+15, pNew->zFilename, "%s-wal-index", pDbFd->zPath); |
| 30147 | + sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); |
| 30144 | 30148 | |
| 30145 | 30149 | /* Look to see if there is an existing winShmNode that can be used. |
| 30146 | 30150 | ** If no matching winShmNode currently exists, create a new one. |
| 30147 | 30151 | */ |
| 30148 | 30152 | winShmEnterMutex(); |
| | @@ -31136,11 +31140,11 @@ |
| 31136 | 31140 | GetSystemTimeAsFileTime( &ft ); |
| 31137 | 31141 | #endif |
| 31138 | 31142 | |
| 31139 | 31143 | *piNow = winFiletimeEpoch + |
| 31140 | 31144 | ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + |
| 31141 | | - (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)1000; |
| 31145 | + (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000; |
| 31142 | 31146 | |
| 31143 | 31147 | #ifdef SQLITE_TEST |
| 31144 | 31148 | if( sqlite3_current_time ){ |
| 31145 | 31149 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 31146 | 31150 | } |
| | @@ -33856,16 +33860,19 @@ |
| 33856 | 33860 | ** may attempt to commit the transaction again later (calling |
| 33857 | 33861 | ** CommitPhaseOne() again). This flag is used to ensure that the |
| 33858 | 33862 | ** master journal name is only written to the journal file the first |
| 33859 | 33863 | ** time CommitPhaseOne() is called. |
| 33860 | 33864 | ** |
| 33861 | | -** doNotSync |
| 33865 | +** doNotSpill, doNotSyncSpill |
| 33862 | 33866 | ** |
| 33863 | | -** When enabled, cache spills are prohibited and the journal file cannot |
| 33864 | | -** be synced. This variable is set and cleared by sqlite3PagerWrite() |
| 33865 | | -** in order to prevent a journal sync from happening in between the |
| 33866 | | -** journalling of two pages on the same sector. |
| 33867 | +** When enabled, cache spills are prohibited. The doNotSpill variable |
| 33868 | +** inhibits all cache spill and doNotSyncSpill inhibits those spills that |
| 33869 | +** would require a journal sync. The doNotSyncSpill is set and cleared |
| 33870 | +** by sqlite3PagerWrite() in order to prevent a journal sync from happening |
| 33871 | +** in between the journalling of two pages on the same sector. The |
| 33872 | +** doNotSpill value set to prevent pagerStress() from trying to use |
| 33873 | +** the journal during a rollback. |
| 33867 | 33874 | ** |
| 33868 | 33875 | ** needSync |
| 33869 | 33876 | ** |
| 33870 | 33877 | ** TODO: It might be easier to set this variable in writeJournalHdr() |
| 33871 | 33878 | ** and writeMasterJournal() only. Change its meaning to "unsynced data |
| | @@ -33905,11 +33912,12 @@ |
| 33905 | 33912 | u8 dbModified; /* True if there are any changes to the Db */ |
| 33906 | 33913 | u8 needSync; /* True if an fsync() is needed on the journal */ |
| 33907 | 33914 | u8 journalStarted; /* True if header of journal is synced */ |
| 33908 | 33915 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
| 33909 | 33916 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
| 33910 | | - u8 doNotSync; /* Boolean. While true, do not spill the cache */ |
| 33917 | + u8 doNotSpill; /* Do not spill the cache when non-zero */ |
| 33918 | + u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */ |
| 33911 | 33919 | u8 dbSizeValid; /* Set when dbSize is correct */ |
| 33912 | 33920 | u8 subjInMemory; /* True to use in-memory sub-journals */ |
| 33913 | 33921 | Pgno dbSize; /* Number of pages in the database */ |
| 33914 | 33922 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 33915 | 33923 | Pgno dbFileSize; /* Number of pages in the database file */ |
| | @@ -34772,13 +34780,14 @@ |
| 34772 | 34780 | static void pager_unlock(Pager *pPager){ |
| 34773 | 34781 | if( !pPager->exclusiveMode ){ |
| 34774 | 34782 | int rc = SQLITE_OK; /* Return code */ |
| 34775 | 34783 | int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0; |
| 34776 | 34784 | |
| 34777 | | - /* Always close the journal file when dropping the database lock. |
| 34778 | | - ** Otherwise, another connection with journal_mode=delete might |
| 34779 | | - ** delete the file out from under us. |
| 34785 | + /* If the operating system support deletion of open files, then |
| 34786 | + ** close the journal file when dropping the database lock. Otherwise |
| 34787 | + ** another connection with journal_mode=delete might delete the file |
| 34788 | + ** out from under us. |
| 34780 | 34789 | */ |
| 34781 | 34790 | assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 ); |
| 34782 | 34791 | assert( (PAGER_JOURNALMODE_OFF & 5)!=1 ); |
| 34783 | 34792 | assert( (PAGER_JOURNALMODE_WAL & 5)!=1 ); |
| 34784 | 34793 | assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 ); |
| | @@ -34835,11 +34844,11 @@ |
| 34835 | 34844 | ** structure, the second the error-code about to be returned by a pager |
| 34836 | 34845 | ** API function. The value returned is a copy of the second argument |
| 34837 | 34846 | ** to this function. |
| 34838 | 34847 | ** |
| 34839 | 34848 | ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL |
| 34840 | | -** the error becomes persistent. Until the persisten error is cleared, |
| 34849 | +** the error becomes persistent. Until the persistent error is cleared, |
| 34841 | 34850 | ** subsequent API calls on this Pager will immediately return the same |
| 34842 | 34851 | ** error code. |
| 34843 | 34852 | ** |
| 34844 | 34853 | ** A persistent error indicates that the contents of the pager-cache |
| 34845 | 34854 | ** cannot be trusted. This state can be cleared by completely discarding |
| | @@ -35240,13 +35249,16 @@ |
| 35240 | 35249 | ** the data just read from the sub-journal. Mark the page as dirty |
| 35241 | 35250 | ** and if the pager requires a journal-sync, then mark the page as |
| 35242 | 35251 | ** requiring a journal-sync before it is written. |
| 35243 | 35252 | */ |
| 35244 | 35253 | assert( isSavepnt ); |
| 35245 | | - if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1))!=SQLITE_OK ){ |
| 35246 | | - return rc; |
| 35247 | | - } |
| 35254 | + assert( pPager->doNotSpill==0 ); |
| 35255 | + pPager->doNotSpill++; |
| 35256 | + rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1); |
| 35257 | + assert( pPager->doNotSpill==1 ); |
| 35258 | + pPager->doNotSpill--; |
| 35259 | + if( rc!=SQLITE_OK ) return rc; |
| 35248 | 35260 | pPg->flags &= ~PGHDR_NEED_READ; |
| 35249 | 35261 | sqlite3PcacheMakeDirty(pPg); |
| 35250 | 35262 | } |
| 35251 | 35263 | if( pPg ){ |
| 35252 | 35264 | /* No page should ever be explicitly rolled back that is in use, except |
| | @@ -35346,10 +35358,13 @@ |
| 35346 | 35358 | int rc; /* Return code */ |
| 35347 | 35359 | sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */ |
| 35348 | 35360 | sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */ |
| 35349 | 35361 | char *zMasterJournal = 0; /* Contents of master journal file */ |
| 35350 | 35362 | i64 nMasterJournal; /* Size of master journal file */ |
| 35363 | + char *zJournal; /* Pointer to one journal within MJ file */ |
| 35364 | + char *zMasterPtr; /* Space to hold MJ filename from a journal file */ |
| 35365 | + int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */ |
| 35351 | 35366 | |
| 35352 | 35367 | /* Allocate space for both the pJournal and pMaster file descriptors. |
| 35353 | 35368 | ** If successful, open the master journal file for reading. |
| 35354 | 35369 | */ |
| 35355 | 35370 | pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2); |
| | @@ -35360,77 +35375,72 @@ |
| 35360 | 35375 | const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL); |
| 35361 | 35376 | rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0); |
| 35362 | 35377 | } |
| 35363 | 35378 | if( rc!=SQLITE_OK ) goto delmaster_out; |
| 35364 | 35379 | |
| 35365 | | - rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
| 35366 | | - if( rc!=SQLITE_OK ) goto delmaster_out; |
| 35367 | | - |
| 35368 | | - if( nMasterJournal>0 ){ |
| 35369 | | - char *zJournal; |
| 35370 | | - char *zMasterPtr = 0; |
| 35371 | | - int nMasterPtr = pVfs->mxPathname+1; |
| 35372 | | - |
| 35373 | | - /* Load the entire master journal file into space obtained from |
| 35374 | | - ** sqlite3_malloc() and pointed to by zMasterJournal. |
| 35375 | | - */ |
| 35376 | | - zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1); |
| 35377 | | - if( !zMasterJournal ){ |
| 35378 | | - rc = SQLITE_NOMEM; |
| 35379 | | - goto delmaster_out; |
| 35380 | | - } |
| 35381 | | - zMasterPtr = &zMasterJournal[nMasterJournal+1]; |
| 35382 | | - rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); |
| 35383 | | - if( rc!=SQLITE_OK ) goto delmaster_out; |
| 35384 | | - zMasterJournal[nMasterJournal] = 0; |
| 35385 | | - |
| 35386 | | - zJournal = zMasterJournal; |
| 35387 | | - while( (zJournal-zMasterJournal)<nMasterJournal ){ |
| 35388 | | - int exists; |
| 35389 | | - rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 35390 | | - if( rc!=SQLITE_OK ){ |
| 35391 | | - goto delmaster_out; |
| 35392 | | - } |
| 35393 | | - if( exists ){ |
| 35394 | | - /* One of the journals pointed to by the master journal exists. |
| 35395 | | - ** Open it and check if it points at the master journal. If |
| 35396 | | - ** so, return without deleting the master journal file. |
| 35397 | | - */ |
| 35398 | | - int c; |
| 35399 | | - int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 35400 | | - rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
| 35401 | | - if( rc!=SQLITE_OK ){ |
| 35402 | | - goto delmaster_out; |
| 35403 | | - } |
| 35404 | | - |
| 35405 | | - rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
| 35406 | | - sqlite3OsClose(pJournal); |
| 35407 | | - if( rc!=SQLITE_OK ){ |
| 35408 | | - goto delmaster_out; |
| 35409 | | - } |
| 35410 | | - |
| 35411 | | - c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
| 35412 | | - if( c ){ |
| 35413 | | - /* We have a match. Do not delete the master journal file. */ |
| 35414 | | - goto delmaster_out; |
| 35415 | | - } |
| 35416 | | - } |
| 35417 | | - zJournal += (sqlite3Strlen30(zJournal)+1); |
| 35418 | | - } |
| 35419 | | - } |
| 35420 | | - |
| 35380 | + /* Load the entire master journal file into space obtained from |
| 35381 | + ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain |
| 35382 | + ** sufficient space (in zMasterPtr) to hold the names of master |
| 35383 | + ** journal files extracted from regular rollback-journals. |
| 35384 | + */ |
| 35385 | + rc = sqlite3OsFileSize(pMaster, &nMasterJournal); |
| 35386 | + if( rc!=SQLITE_OK ) goto delmaster_out; |
| 35387 | + nMasterPtr = pVfs->mxPathname+1; |
| 35388 | + zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1); |
| 35389 | + if( !zMasterJournal ){ |
| 35390 | + rc = SQLITE_NOMEM; |
| 35391 | + goto delmaster_out; |
| 35392 | + } |
| 35393 | + zMasterPtr = &zMasterJournal[nMasterJournal+1]; |
| 35394 | + rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0); |
| 35395 | + if( rc!=SQLITE_OK ) goto delmaster_out; |
| 35396 | + zMasterJournal[nMasterJournal] = 0; |
| 35397 | + |
| 35398 | + zJournal = zMasterJournal; |
| 35399 | + while( (zJournal-zMasterJournal)<nMasterJournal ){ |
| 35400 | + int exists; |
| 35401 | + rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 35402 | + if( rc!=SQLITE_OK ){ |
| 35403 | + goto delmaster_out; |
| 35404 | + } |
| 35405 | + if( exists ){ |
| 35406 | + /* One of the journals pointed to by the master journal exists. |
| 35407 | + ** Open it and check if it points at the master journal. If |
| 35408 | + ** so, return without deleting the master journal file. |
| 35409 | + */ |
| 35410 | + int c; |
| 35411 | + int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL); |
| 35412 | + rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0); |
| 35413 | + if( rc!=SQLITE_OK ){ |
| 35414 | + goto delmaster_out; |
| 35415 | + } |
| 35416 | + |
| 35417 | + rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr); |
| 35418 | + sqlite3OsClose(pJournal); |
| 35419 | + if( rc!=SQLITE_OK ){ |
| 35420 | + goto delmaster_out; |
| 35421 | + } |
| 35422 | + |
| 35423 | + c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0; |
| 35424 | + if( c ){ |
| 35425 | + /* We have a match. Do not delete the master journal file. */ |
| 35426 | + goto delmaster_out; |
| 35427 | + } |
| 35428 | + } |
| 35429 | + zJournal += (sqlite3Strlen30(zJournal)+1); |
| 35430 | + } |
| 35431 | + |
| 35432 | + sqlite3OsClose(pMaster); |
| 35421 | 35433 | rc = sqlite3OsDelete(pVfs, zMaster, 0); |
| 35422 | 35434 | |
| 35423 | 35435 | delmaster_out: |
| 35424 | | - if( zMasterJournal ){ |
| 35425 | | - sqlite3_free(zMasterJournal); |
| 35426 | | - } |
| 35436 | + sqlite3_free(zMasterJournal); |
| 35427 | 35437 | if( pMaster ){ |
| 35428 | 35438 | sqlite3OsClose(pMaster); |
| 35429 | 35439 | assert( !isOpen(pJournal) ); |
| 35440 | + sqlite3_free(pMaster); |
| 35430 | 35441 | } |
| 35431 | | - sqlite3_free(pMaster); |
| 35432 | 35442 | return rc; |
| 35433 | 35443 | } |
| 35434 | 35444 | |
| 35435 | 35445 | |
| 35436 | 35446 | /* |
| | @@ -36125,11 +36135,11 @@ |
| 36125 | 36135 | for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){ |
| 36126 | 36136 | rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1); |
| 36127 | 36137 | } |
| 36128 | 36138 | assert( rc!=SQLITE_DONE ); |
| 36129 | 36139 | } |
| 36130 | | - assert( rc!=SQLITE_OK || pPager->journalOff==szJ ); |
| 36140 | + assert( rc!=SQLITE_OK || pPager->journalOff>=szJ ); |
| 36131 | 36141 | |
| 36132 | 36142 | /* Finally, rollback pages from the sub-journal. Page that were |
| 36133 | 36143 | ** previously rolled back out of the main journal (and are hence in pDone) |
| 36134 | 36144 | ** will be skipped. Out-of-range pages are also skipped. |
| 36135 | 36145 | */ |
| | @@ -37065,10 +37075,26 @@ |
| 37065 | 37075 | Pager *pPager = (Pager *)p; |
| 37066 | 37076 | int rc = SQLITE_OK; |
| 37067 | 37077 | |
| 37068 | 37078 | assert( pPg->pPager==pPager ); |
| 37069 | 37079 | assert( pPg->flags&PGHDR_DIRTY ); |
| 37080 | + |
| 37081 | + /* The doNotSyncSpill flag is set during times when doing a sync of |
| 37082 | + ** journal (and adding a new header) is not allowed. This occurs |
| 37083 | + ** during calls to sqlite3PagerWrite() while trying to journal multiple |
| 37084 | + ** pages belonging to the same sector. |
| 37085 | + ** |
| 37086 | + ** The doNotSpill flag inhibits all cache spilling regardless of whether |
| 37087 | + ** or not a sync is required. This is set during a rollback. |
| 37088 | + ** |
| 37089 | + ** Spilling is also inhibited when in an error state. |
| 37090 | + */ |
| 37091 | + if( pPager->errCode ) return SQLITE_OK; |
| 37092 | + if( pPager->doNotSpill ) return SQLITE_OK; |
| 37093 | + if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ |
| 37094 | + return SQLITE_OK; |
| 37095 | + } |
| 37070 | 37096 | |
| 37071 | 37097 | pPg->pDirty = 0; |
| 37072 | 37098 | if( pagerUseWal(pPager) ){ |
| 37073 | 37099 | /* Write a single frame for this page to the log. */ |
| 37074 | 37100 | if( subjRequiresPage(pPg) ){ |
| | @@ -37076,33 +37102,16 @@ |
| 37076 | 37102 | } |
| 37077 | 37103 | if( rc==SQLITE_OK ){ |
| 37078 | 37104 | rc = pagerWalFrames(pPager, pPg, 0, 0, 0); |
| 37079 | 37105 | } |
| 37080 | 37106 | }else{ |
| 37081 | | - /* The doNotSync flag is set by the sqlite3PagerWrite() function while it |
| 37082 | | - ** is journalling a set of two or more database pages that are stored |
| 37083 | | - ** on the same disk sector. Syncing the journal is not allowed while |
| 37084 | | - ** this is happening as it is important that all members of such a |
| 37085 | | - ** set of pages are synced to disk together. So, if the page this function |
| 37086 | | - ** is trying to make clean will require a journal sync and the doNotSync |
| 37087 | | - ** flag is set, return without doing anything. The pcache layer will |
| 37088 | | - ** just have to go ahead and allocate a new page buffer instead of |
| 37089 | | - ** reusing pPg. |
| 37090 | | - ** |
| 37091 | | - ** Similarly, if the pager has already entered the error state, do not |
| 37092 | | - ** try to write the contents of pPg to disk. |
| 37093 | | - */ |
| 37094 | | - if( NEVER(pPager->errCode) |
| 37095 | | - || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC) |
| 37096 | | - ){ |
| 37097 | | - return SQLITE_OK; |
| 37098 | | - } |
| 37099 | 37107 | |
| 37100 | 37108 | /* Sync the journal file if required. */ |
| 37101 | 37109 | if( pPg->flags&PGHDR_NEED_SYNC ){ |
| 37110 | + assert( !pPager->noSync ); |
| 37102 | 37111 | rc = syncJournal(pPager); |
| 37103 | | - if( rc==SQLITE_OK && pPager->fullSync && |
| 37112 | + if( rc==SQLITE_OK && |
| 37104 | 37113 | !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) && |
| 37105 | 37114 | !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 37106 | 37115 | ){ |
| 37107 | 37116 | pPager->nRec = 0; |
| 37108 | 37117 | rc = writeJournalHdr(pPager); |
| | @@ -38256,11 +38265,11 @@ |
| 38256 | 38265 | */ |
| 38257 | 38266 | rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory); |
| 38258 | 38267 | if( rc!=SQLITE_OK ){ |
| 38259 | 38268 | return rc; |
| 38260 | 38269 | } |
| 38261 | | - if( !isOpen(pPager->jfd) |
| 38270 | + if( pPager->pInJournal==0 |
| 38262 | 38271 | && pPager->journalMode!=PAGER_JOURNALMODE_OFF |
| 38263 | 38272 | && !pagerUseWal(pPager) |
| 38264 | 38273 | ){ |
| 38265 | 38274 | assert( pPager->useJournal ); |
| 38266 | 38275 | rc = pager_open_journal(pPager); |
| | @@ -38384,39 +38393,41 @@ |
| 38384 | 38393 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 38385 | 38394 | |
| 38386 | 38395 | if( nPagePerSector>1 ){ |
| 38387 | 38396 | Pgno nPageCount; /* Total number of pages in database file */ |
| 38388 | 38397 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 38389 | | - int nPage; /* Number of pages starting at pg1 to journal */ |
| 38398 | + int nPage = 0; /* Number of pages starting at pg1 to journal */ |
| 38390 | 38399 | int ii; /* Loop counter */ |
| 38391 | 38400 | int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */ |
| 38392 | 38401 | |
| 38393 | | - /* Set the doNotSync flag to 1. This is because we cannot allow a journal |
| 38394 | | - ** header to be written between the pages journaled by this function. |
| 38402 | + /* Set the doNotSyncSpill flag to 1. This is because we cannot allow |
| 38403 | + ** a journal header to be written between the pages journaled by |
| 38404 | + ** this function. |
| 38395 | 38405 | */ |
| 38396 | 38406 | assert( !MEMDB ); |
| 38397 | | - assert( pPager->doNotSync==0 ); |
| 38398 | | - pPager->doNotSync = 1; |
| 38407 | + assert( pPager->doNotSyncSpill==0 ); |
| 38408 | + pPager->doNotSyncSpill++; |
| 38399 | 38409 | |
| 38400 | 38410 | /* This trick assumes that both the page-size and sector-size are |
| 38401 | 38411 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 38402 | 38412 | ** of the first page of the sector pPg is located on. |
| 38403 | 38413 | */ |
| 38404 | 38414 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 38405 | 38415 | |
| 38406 | 38416 | rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount); |
| 38407 | | - if( rc ) return rc; |
| 38408 | | - if( pPg->pgno>nPageCount ){ |
| 38409 | | - nPage = (pPg->pgno - pg1)+1; |
| 38410 | | - }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 38411 | | - nPage = nPageCount+1-pg1; |
| 38412 | | - }else{ |
| 38413 | | - nPage = nPagePerSector; |
| 38414 | | - } |
| 38415 | | - assert(nPage>0); |
| 38416 | | - assert(pg1<=pPg->pgno); |
| 38417 | | - assert((pg1+nPage)>pPg->pgno); |
| 38417 | + if( rc==SQLITE_OK ){ |
| 38418 | + if( pPg->pgno>nPageCount ){ |
| 38419 | + nPage = (pPg->pgno - pg1)+1; |
| 38420 | + }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 38421 | + nPage = nPageCount+1-pg1; |
| 38422 | + }else{ |
| 38423 | + nPage = nPagePerSector; |
| 38424 | + } |
| 38425 | + assert(nPage>0); |
| 38426 | + assert(pg1<=pPg->pgno); |
| 38427 | + assert((pg1+nPage)>pPg->pgno); |
| 38428 | + } |
| 38418 | 38429 | |
| 38419 | 38430 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 38420 | 38431 | Pgno pg = pg1+ii; |
| 38421 | 38432 | PgHdr *pPage; |
| 38422 | 38433 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
| | @@ -38455,12 +38466,12 @@ |
| 38455 | 38466 | } |
| 38456 | 38467 | } |
| 38457 | 38468 | assert(pPager->needSync); |
| 38458 | 38469 | } |
| 38459 | 38470 | |
| 38460 | | - assert( pPager->doNotSync==1 ); |
| 38461 | | - pPager->doNotSync = 0; |
| 38471 | + assert( pPager->doNotSyncSpill==1 ); |
| 38472 | + pPager->doNotSyncSpill--; |
| 38462 | 38473 | }else{ |
| 38463 | 38474 | rc = pager_write(pDbPage); |
| 38464 | 38475 | } |
| 38465 | 38476 | return rc; |
| 38466 | 38477 | } |
| | @@ -38571,13 +38582,16 @@ |
| 38571 | 38582 | put32bits(((char*)pPgHdr->pData)+92, change_counter); |
| 38572 | 38583 | put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER); |
| 38573 | 38584 | |
| 38574 | 38585 | /* If running in direct mode, write the contents of page 1 to the file. */ |
| 38575 | 38586 | if( DIRECT_MODE ){ |
| 38576 | | - const void *zBuf = pPgHdr->pData; |
| 38587 | + const void *zBuf; |
| 38577 | 38588 | assert( pPager->dbFileSize>0 ); |
| 38578 | | - rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 38589 | + CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf); |
| 38590 | + if( rc==SQLITE_OK ){ |
| 38591 | + rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); |
| 38592 | + } |
| 38579 | 38593 | if( rc==SQLITE_OK ){ |
| 38580 | 38594 | pPager->changeCountDone = 1; |
| 38581 | 38595 | } |
| 38582 | 38596 | }else{ |
| 38583 | 38597 | pPager->changeCountDone = 1; |
| | @@ -38643,11 +38657,11 @@ |
| 38643 | 38657 | |
| 38644 | 38658 | /* The dbOrigSize is never set if journal_mode=OFF */ |
| 38645 | 38659 | assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 ); |
| 38646 | 38660 | |
| 38647 | 38661 | /* If a prior error occurred, report that error again. */ |
| 38648 | | - if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38662 | + if( pPager->errCode ) return pPager->errCode; |
| 38649 | 38663 | |
| 38650 | 38664 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 38651 | 38665 | pPager->zFilename, zMaster, pPager->dbSize)); |
| 38652 | 38666 | |
| 38653 | 38667 | if( MEMDB && pPager->dbModified ){ |
| | @@ -38821,14 +38835,15 @@ |
| 38821 | 38835 | ** But if (due to a coding error elsewhere in the system) it does get |
| 38822 | 38836 | ** called, just return the same error code without doing anything. */ |
| 38823 | 38837 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38824 | 38838 | |
| 38825 | 38839 | /* This function should not be called if the pager is not in at least |
| 38826 | | - ** PAGER_RESERVED state. And indeed SQLite never does this. But it is |
| 38827 | | - ** nice to have this defensive test here anyway. |
| 38840 | + ** PAGER_RESERVED state. **FIXME**: Make it so that this test always |
| 38841 | + ** fails - make it so that we never reach this point if we do not hold |
| 38842 | + ** all necessary locks. |
| 38828 | 38843 | */ |
| 38829 | | - if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR; |
| 38844 | + if( pPager->state<PAGER_RESERVED ) return SQLITE_ERROR; |
| 38830 | 38845 | |
| 38831 | 38846 | /* An optimization. If the database was not actually modified during |
| 38832 | 38847 | ** this transaction, the pager is running in exclusive-mode and is |
| 38833 | 38848 | ** using persistent journals, then this function is a no-op. |
| 38834 | 38849 | ** |
| | @@ -38873,11 +38888,11 @@ |
| 38873 | 38888 | ** performed. If successful, task (2). Regardless of the outcome |
| 38874 | 38889 | ** of either, the error state error code is returned to the caller |
| 38875 | 38890 | ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT). |
| 38876 | 38891 | ** |
| 38877 | 38892 | ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether |
| 38878 | | -** or not (1) is succussful, also attempt (2). If successful, return |
| 38893 | +** or not (1) is successful, also attempt (2). If successful, return |
| 38879 | 38894 | ** SQLITE_OK. Otherwise, enter the error state and return the first |
| 38880 | 38895 | ** error code encountered. |
| 38881 | 38896 | ** |
| 38882 | 38897 | ** In this case there is no chance that the database was written to. |
| 38883 | 38898 | ** So is safe to finalize the journal file even if the playback |
| | @@ -39647,11 +39662,27 @@ |
| 39647 | 39662 | sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED); |
| 39648 | 39663 | } |
| 39649 | 39664 | } |
| 39650 | 39665 | return rc; |
| 39651 | 39666 | } |
| 39652 | | -#endif |
| 39667 | + |
| 39668 | +#ifdef SQLITE_HAS_CODEC |
| 39669 | +/* |
| 39670 | +** This function is called by the wal module when writing page content |
| 39671 | +** into the log file. |
| 39672 | +** |
| 39673 | +** This function returns a pointer to a buffer containing the encrypted |
| 39674 | +** page content. If a malloc fails, this function may return NULL. |
| 39675 | +*/ |
| 39676 | +SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){ |
| 39677 | + void *aData = 0; |
| 39678 | + CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData); |
| 39679 | + return aData; |
| 39680 | +} |
| 39681 | +#endif /* SQLITE_HAS_CODEC */ |
| 39682 | + |
| 39683 | +#endif /* !SQLITE_OMIT_WAL */ |
| 39653 | 39684 | |
| 39654 | 39685 | #endif /* SQLITE_OMIT_DISKIO */ |
| 39655 | 39686 | |
| 39656 | 39687 | /************** End of pager.c ***********************************************/ |
| 39657 | 39688 | /************** Begin file wal.c *********************************************/ |
| | @@ -39686,23 +39717,25 @@ |
| 39686 | 39717 | ** frames can overwrite the old ones. A WAL always grows from beginning |
| 39687 | 39718 | ** toward the end. Checksums and counters attached to each frame are |
| 39688 | 39719 | ** used to determine which frames within the WAL are valid and which |
| 39689 | 39720 | ** are leftovers from prior checkpoints. |
| 39690 | 39721 | ** |
| 39691 | | -** The WAL header is 24 bytes in size and consists of the following six |
| 39722 | +** The WAL header is 32 bytes in size and consists of the following eight |
| 39692 | 39723 | ** big-endian 32-bit unsigned integer values: |
| 39693 | 39724 | ** |
| 39694 | 39725 | ** 0: Magic number. 0x377f0682 or 0x377f0683 |
| 39695 | 39726 | ** 4: File format version. Currently 3007000 |
| 39696 | 39727 | ** 8: Database page size. Example: 1024 |
| 39697 | 39728 | ** 12: Checkpoint sequence number |
| 39698 | 39729 | ** 16: Salt-1, random integer incremented with each checkpoint |
| 39699 | 39730 | ** 20: Salt-2, a different random integer changing with each ckpt |
| 39731 | +** 24: Checksum-1 (first part of checksum for first 24 bytes of header). |
| 39732 | +** 28: Checksum-2 (second part of checksum for first 24 bytes of header). |
| 39700 | 39733 | ** |
| 39701 | 39734 | ** Immediately following the wal-header are zero or more frames. Each |
| 39702 | 39735 | ** frame consists of a 24-byte frame-header followed by a <page-size> bytes |
| 39703 | | -** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned |
| 39736 | +** of page data. The frame-header is six big-endian 32-bit unsigned |
| 39704 | 39737 | ** integer values, as follows: |
| 39705 | 39738 | ** |
| 39706 | 39739 | ** 0: Page number. |
| 39707 | 39740 | ** 4: For commit records, the size of the database image in pages |
| 39708 | 39741 | ** after the commit. For all other records, zero. |
| | @@ -39733,10 +39766,15 @@ |
| 39733 | 39766 | ** |
| 39734 | 39767 | ** for i from 0 to n-1 step 2: |
| 39735 | 39768 | ** s0 += x[i] + s1; |
| 39736 | 39769 | ** s1 += x[i+1] + s0; |
| 39737 | 39770 | ** endfor |
| 39771 | +** |
| 39772 | +** Note that s0 and s1 are both weighted checksums using fibonacci weights |
| 39773 | +** in reverse order (the largest fibonacci weight occurs on the first element |
| 39774 | +** of the sequence being summed.) The s1 value spans all 32-bit |
| 39775 | +** terms of the sequence whereas s0 omits the final term. |
| 39738 | 39776 | ** |
| 39739 | 39777 | ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the |
| 39740 | 39778 | ** WAL is transferred into the database, then the database is VFS.xSync-ed. |
| 39741 | 39779 | ** The VFS.xSync operations serve as write barriers - all writes launched |
| 39742 | 39780 | ** before the xSync must complete before any write that launches after the |
| | @@ -39900,10 +39938,25 @@ |
| 39900 | 39938 | # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X |
| 39901 | 39939 | #else |
| 39902 | 39940 | # define WALTRACE(X) |
| 39903 | 39941 | #endif |
| 39904 | 39942 | |
| 39943 | +/* |
| 39944 | +** The maximum (and only) versions of the wal and wal-index formats |
| 39945 | +** that may be interpreted by this version of SQLite. |
| 39946 | +** |
| 39947 | +** If a client begins recovering a WAL file and finds that (a) the checksum |
| 39948 | +** values in the wal-header are correct and (b) the version field is not |
| 39949 | +** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN. |
| 39950 | +** |
| 39951 | +** Similarly, if a client successfully reads a wal-index header (i.e. the |
| 39952 | +** checksum test is successful) and finds that the version field is not |
| 39953 | +** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite |
| 39954 | +** returns SQLITE_CANTOPEN. |
| 39955 | +*/ |
| 39956 | +#define WAL_MAX_VERSION 3007000 |
| 39957 | +#define WALINDEX_MAX_VERSION 3007000 |
| 39905 | 39958 | |
| 39906 | 39959 | /* |
| 39907 | 39960 | ** Indices of various locking bytes. WAL_NREADER is the number |
| 39908 | 39961 | ** of available reader locks and should be at least 3. |
| 39909 | 39962 | */ |
| | @@ -39926,10 +39979,12 @@ |
| 39926 | 39979 | ** |
| 39927 | 39980 | ** The actual header in the wal-index consists of two copies of this |
| 39928 | 39981 | ** object. |
| 39929 | 39982 | */ |
| 39930 | 39983 | struct WalIndexHdr { |
| 39984 | + u32 iVersion; /* Wal-index version */ |
| 39985 | + u32 unused; /* Unused (padding) field */ |
| 39931 | 39986 | u32 iChange; /* Counter incremented each transaction */ |
| 39932 | 39987 | u8 isInit; /* 1 when initialized */ |
| 39933 | 39988 | u8 bigEndCksum; /* True if checksums in WAL are big-endian */ |
| 39934 | 39989 | u16 szPage; /* Database page size in bytes */ |
| 39935 | 39990 | u32 mxFrame; /* Index of last valid frame in the WAL */ |
| | @@ -40005,12 +40060,13 @@ |
| 40005 | 40060 | #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED) |
| 40006 | 40061 | |
| 40007 | 40062 | /* Size of header before each frame in wal */ |
| 40008 | 40063 | #define WAL_FRAME_HDRSIZE 24 |
| 40009 | 40064 | |
| 40010 | | -/* Size of write ahead log header */ |
| 40011 | | -#define WAL_HDRSIZE 24 |
| 40065 | +/* Size of write ahead log header, including checksum. */ |
| 40066 | +/* #define WAL_HDRSIZE 24 */ |
| 40067 | +#define WAL_HDRSIZE 32 |
| 40012 | 40068 | |
| 40013 | 40069 | /* WAL magic value. Either this value, or the same value with the least |
| 40014 | 40070 | ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit |
| 40015 | 40071 | ** big-endian format in the first 4 bytes of a WAL file. |
| 40016 | 40072 | ** |
| | @@ -40234,10 +40290,11 @@ |
| 40234 | 40290 | volatile WalIndexHdr *aHdr = walIndexHdr(pWal); |
| 40235 | 40291 | const int nCksum = offsetof(WalIndexHdr, aCksum); |
| 40236 | 40292 | |
| 40237 | 40293 | assert( pWal->writeLock ); |
| 40238 | 40294 | pWal->hdr.isInit = 1; |
| 40295 | + pWal->hdr.iVersion = WALINDEX_MAX_VERSION; |
| 40239 | 40296 | walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); |
| 40240 | 40297 | memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr)); |
| 40241 | 40298 | sqlite3OsShmBarrier(pWal->pDbFd); |
| 40242 | 40299 | memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr)); |
| 40243 | 40300 | } |
| | @@ -40683,10 +40740,11 @@ |
| 40683 | 40740 | u8 *aData; /* Pointer to data part of aFrame buffer */ |
| 40684 | 40741 | int iFrame; /* Index of last frame read */ |
| 40685 | 40742 | i64 iOffset; /* Next offset to read from log file */ |
| 40686 | 40743 | int szPage; /* Page size according to the log */ |
| 40687 | 40744 | u32 magic; /* Magic value read from WAL header */ |
| 40745 | + u32 version; /* Magic value read from WAL header */ |
| 40688 | 40746 | |
| 40689 | 40747 | /* Read in the WAL header. */ |
| 40690 | 40748 | rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); |
| 40691 | 40749 | if( rc!=SQLITE_OK ){ |
| 40692 | 40750 | goto recovery_error; |
| | @@ -40708,13 +40766,28 @@ |
| 40708 | 40766 | } |
| 40709 | 40767 | pWal->hdr.bigEndCksum = (magic&0x00000001); |
| 40710 | 40768 | pWal->szPage = szPage; |
| 40711 | 40769 | pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); |
| 40712 | 40770 | memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); |
| 40771 | + |
| 40772 | + /* Verify that the WAL header checksum is correct */ |
| 40713 | 40773 | walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, |
| 40714 | | - aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum |
| 40774 | + aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum |
| 40715 | 40775 | ); |
| 40776 | + if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24]) |
| 40777 | + || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28]) |
| 40778 | + ){ |
| 40779 | + goto finished; |
| 40780 | + } |
| 40781 | + |
| 40782 | + /* Verify that the version number on the WAL format is one that |
| 40783 | + ** are able to understand */ |
| 40784 | + version = sqlite3Get4byte(&aBuf[4]); |
| 40785 | + if( version!=WAL_MAX_VERSION ){ |
| 40786 | + rc = SQLITE_CANTOPEN_BKPT; |
| 40787 | + goto finished; |
| 40788 | + } |
| 40716 | 40789 | |
| 40717 | 40790 | /* Malloc a buffer to read frames into. */ |
| 40718 | 40791 | szFrame = szPage + WAL_FRAME_HDRSIZE; |
| 40719 | 40792 | aFrame = (u8 *)sqlite3_malloc(szFrame); |
| 40720 | 40793 | if( !aFrame ){ |
| | @@ -40899,53 +40972,101 @@ |
| 40899 | 40972 | |
| 40900 | 40973 | *piPage = p->iPrior = iRet; |
| 40901 | 40974 | return (iRet==0xFFFFFFFF); |
| 40902 | 40975 | } |
| 40903 | 40976 | |
| 40977 | +/* |
| 40978 | +** This function merges two sorted lists into a single sorted list. |
| 40979 | +*/ |
| 40980 | +static void walMerge( |
| 40981 | + u32 *aContent, /* Pages in wal */ |
| 40982 | + ht_slot *aLeft, /* IN: Left hand input list */ |
| 40983 | + int nLeft, /* IN: Elements in array *paLeft */ |
| 40984 | + ht_slot **paRight, /* IN/OUT: Right hand input list */ |
| 40985 | + int *pnRight, /* IN/OUT: Elements in *paRight */ |
| 40986 | + ht_slot *aTmp /* Temporary buffer */ |
| 40987 | +){ |
| 40988 | + int iLeft = 0; /* Current index in aLeft */ |
| 40989 | + int iRight = 0; /* Current index in aRight */ |
| 40990 | + int iOut = 0; /* Current index in output buffer */ |
| 40991 | + int nRight = *pnRight; |
| 40992 | + ht_slot *aRight = *paRight; |
| 40904 | 40993 | |
| 40994 | + assert( nLeft>0 && nRight>0 ); |
| 40995 | + while( iRight<nRight || iLeft<nLeft ){ |
| 40996 | + ht_slot logpage; |
| 40997 | + Pgno dbpage; |
| 40998 | + |
| 40999 | + if( (iLeft<nLeft) |
| 41000 | + && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 41001 | + ){ |
| 41002 | + logpage = aLeft[iLeft++]; |
| 41003 | + }else{ |
| 41004 | + logpage = aRight[iRight++]; |
| 41005 | + } |
| 41006 | + dbpage = aContent[logpage]; |
| 41007 | + |
| 41008 | + aTmp[iOut++] = logpage; |
| 41009 | + if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 41010 | + |
| 41011 | + assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 41012 | + assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 41013 | + } |
| 41014 | + |
| 41015 | + *paRight = aLeft; |
| 41016 | + *pnRight = iOut; |
| 41017 | + memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut); |
| 41018 | +} |
| 41019 | + |
| 41020 | +/* |
| 41021 | +** Sort the elements in list aList, removing any duplicates. |
| 41022 | +*/ |
| 40905 | 41023 | static void walMergesort( |
| 40906 | 41024 | u32 *aContent, /* Pages in wal */ |
| 40907 | 41025 | ht_slot *aBuffer, /* Buffer of at least *pnList items to use */ |
| 40908 | 41026 | ht_slot *aList, /* IN/OUT: List to sort */ |
| 40909 | 41027 | int *pnList /* IN/OUT: Number of elements in aList[] */ |
| 40910 | 41028 | ){ |
| 40911 | | - int nList = *pnList; |
| 40912 | | - if( nList>1 ){ |
| 40913 | | - int nLeft = nList / 2; /* Elements in left list */ |
| 40914 | | - int nRight = nList - nLeft; /* Elements in right list */ |
| 40915 | | - int iLeft = 0; /* Current index in aLeft */ |
| 40916 | | - int iRight = 0; /* Current index in aright */ |
| 40917 | | - int iOut = 0; /* Current index in output buffer */ |
| 40918 | | - ht_slot *aLeft = aList; /* Left list */ |
| 40919 | | - ht_slot *aRight = aList+nLeft;/* Right list */ |
| 40920 | | - |
| 40921 | | - /* TODO: Change to non-recursive version. */ |
| 40922 | | - walMergesort(aContent, aBuffer, aLeft, &nLeft); |
| 40923 | | - walMergesort(aContent, aBuffer, aRight, &nRight); |
| 40924 | | - |
| 40925 | | - while( iRight<nRight || iLeft<nLeft ){ |
| 40926 | | - ht_slot logpage; |
| 40927 | | - Pgno dbpage; |
| 40928 | | - |
| 40929 | | - if( (iLeft<nLeft) |
| 40930 | | - && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 40931 | | - ){ |
| 40932 | | - logpage = aLeft[iLeft++]; |
| 40933 | | - }else{ |
| 40934 | | - logpage = aRight[iRight++]; |
| 40935 | | - } |
| 40936 | | - dbpage = aContent[logpage]; |
| 40937 | | - |
| 40938 | | - aBuffer[iOut++] = logpage; |
| 40939 | | - if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 40940 | | - |
| 40941 | | - assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 40942 | | - assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 40943 | | - } |
| 40944 | | - memcpy(aList, aBuffer, sizeof(aList[0])*iOut); |
| 40945 | | - *pnList = iOut; |
| 40946 | | - } |
| 41029 | + struct Sublist { |
| 41030 | + int nList; /* Number of elements in aList */ |
| 41031 | + ht_slot *aList; /* Pointer to sub-list content */ |
| 41032 | + }; |
| 41033 | + |
| 41034 | + const int nList = *pnList; /* Size of input list */ |
| 41035 | + int nMerge; /* Number of elements in list aMerge */ |
| 41036 | + ht_slot *aMerge; /* List to be merged */ |
| 41037 | + int iList; /* Index into input list */ |
| 41038 | + int iSub = 0; /* Index into aSub array */ |
| 41039 | + struct Sublist aSub[13]; /* Array of sub-lists */ |
| 41040 | + |
| 41041 | + memset(aSub, 0, sizeof(aSub)); |
| 41042 | + assert( nList<=HASHTABLE_NPAGE && nList>0 ); |
| 41043 | + assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) ); |
| 41044 | + |
| 41045 | + for(iList=0; iList<nList; iList++){ |
| 41046 | + nMerge = 1; |
| 41047 | + aMerge = &aList[iList]; |
| 41048 | + for(iSub=0; iList & (1<<iSub); iSub++){ |
| 41049 | + struct Sublist *p = &aSub[iSub]; |
| 41050 | + assert( p->aList && p->nList<=(1<<iSub) ); |
| 41051 | + assert( p->aList==&aList[iList&~((2<<iSub)-1)] ); |
| 41052 | + walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer); |
| 41053 | + } |
| 41054 | + aSub[iSub].aList = aMerge; |
| 41055 | + aSub[iSub].nList = nMerge; |
| 41056 | + } |
| 41057 | + |
| 41058 | + for(iSub++; iSub<ArraySize(aSub); iSub++){ |
| 41059 | + if( nList & (1<<iSub) ){ |
| 41060 | + struct Sublist *p = &aSub[iSub]; |
| 41061 | + assert( p->nList<=(1<<iSub) ); |
| 41062 | + assert( p->aList==&aList[nList&~((2<<iSub)-1)] ); |
| 41063 | + walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer); |
| 41064 | + } |
| 41065 | + } |
| 41066 | + assert( aMerge==aList ); |
| 41067 | + *pnList = nMerge; |
| 40947 | 41068 | |
| 40948 | 41069 | #ifdef SQLITE_DEBUG |
| 40949 | 41070 | { |
| 40950 | 41071 | int i; |
| 40951 | 41072 | for(i=1; i<*pnList; i++){ |
| | @@ -40957,91 +41078,94 @@ |
| 40957 | 41078 | |
| 40958 | 41079 | /* |
| 40959 | 41080 | ** Free an iterator allocated by walIteratorInit(). |
| 40960 | 41081 | */ |
| 40961 | 41082 | static void walIteratorFree(WalIterator *p){ |
| 40962 | | - sqlite3_free(p); |
| 41083 | + sqlite3ScratchFree(p); |
| 40963 | 41084 | } |
| 40964 | 41085 | |
| 40965 | 41086 | /* |
| 40966 | | -** Map the wal-index into memory owned by this thread, if it is not |
| 40967 | | -** mapped already. Then construct a WalInterator object that can be |
| 40968 | | -** used to loop over all pages in the WAL in ascending order. |
| 41087 | +** Construct a WalInterator object that can be used to loop over all |
| 41088 | +** pages in the WAL in ascending order. The caller must hold the checkpoint |
| 40969 | 41089 | ** |
| 40970 | 41090 | ** On success, make *pp point to the newly allocated WalInterator object |
| 40971 | | -** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error |
| 40972 | | -** code. |
| 41091 | +** return SQLITE_OK. Otherwise, return an error code. If this routine |
| 41092 | +** returns an error, the value of *pp is undefined. |
| 40973 | 41093 | ** |
| 40974 | 41094 | ** The calling routine should invoke walIteratorFree() to destroy the |
| 40975 | | -** WalIterator object when it has finished with it. The caller must |
| 40976 | | -** also unmap the wal-index. But the wal-index must not be unmapped |
| 40977 | | -** prior to the WalIterator object being destroyed. |
| 41095 | +** WalIterator object when it has finished with it. |
| 40978 | 41096 | */ |
| 40979 | 41097 | static int walIteratorInit(Wal *pWal, WalIterator **pp){ |
| 40980 | 41098 | WalIterator *p; /* Return value */ |
| 40981 | 41099 | int nSegment; /* Number of segments to merge */ |
| 40982 | 41100 | u32 iLast; /* Last frame in log */ |
| 40983 | 41101 | int nByte; /* Number of bytes to allocate */ |
| 40984 | 41102 | int i; /* Iterator variable */ |
| 40985 | 41103 | ht_slot *aTmp; /* Temp space used by merge-sort */ |
| 40986 | | - ht_slot *aSpace; /* Space at the end of the allocation */ |
| 40987 | | - |
| 40988 | | - /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other |
| 40989 | | - ** thread is able to write to shared memory while this routine is |
| 40990 | | - ** running (or, indeed, while the WalIterator object exists). Hence, |
| 40991 | | - ** we can cast off the volatile qualification from shared memory |
| 40992 | | - */ |
| 40993 | | - assert( pWal->ckptLock ); |
| 41104 | + int rc = SQLITE_OK; /* Return Code */ |
| 41105 | + |
| 41106 | + /* This routine only runs while holding the checkpoint lock. And |
| 41107 | + ** it only runs if there is actually content in the log (mxFrame>0). |
| 41108 | + */ |
| 41109 | + assert( pWal->ckptLock && pWal->hdr.mxFrame>0 ); |
| 40994 | 41110 | iLast = pWal->hdr.mxFrame; |
| 40995 | 41111 | |
| 40996 | | - /* Allocate space for the WalIterator object */ |
| 41112 | + /* Allocate space for the WalIterator object. */ |
| 40997 | 41113 | nSegment = walFramePage(iLast) + 1; |
| 40998 | 41114 | nByte = sizeof(WalIterator) |
| 40999 | | - + nSegment*(sizeof(struct WalSegment)) |
| 41000 | | - + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot)); |
| 41001 | | - p = (WalIterator *)sqlite3_malloc(nByte); |
| 41115 | + + (nSegment-1)*sizeof(struct WalSegment) |
| 41116 | + + iLast*sizeof(ht_slot); |
| 41117 | + p = (WalIterator *)sqlite3ScratchMalloc(nByte); |
| 41002 | 41118 | if( !p ){ |
| 41003 | 41119 | return SQLITE_NOMEM; |
| 41004 | 41120 | } |
| 41005 | 41121 | memset(p, 0, nByte); |
| 41006 | | - |
| 41007 | | - /* Allocate space for the WalIterator object */ |
| 41008 | 41122 | p->nSegment = nSegment; |
| 41009 | | - aSpace = (ht_slot *)&p->aSegment[nSegment]; |
| 41010 | | - aTmp = &aSpace[HASHTABLE_NPAGE*nSegment]; |
| 41011 | | - for(i=0; i<nSegment; i++){ |
| 41123 | + |
| 41124 | + /* Allocate temporary space used by the merge-sort routine. This block |
| 41125 | + ** of memory will be freed before this function returns. |
| 41126 | + */ |
| 41127 | + aTmp = (ht_slot *)sqlite3ScratchMalloc( |
| 41128 | + sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 41129 | + ); |
| 41130 | + if( !aTmp ){ |
| 41131 | + rc = SQLITE_NOMEM; |
| 41132 | + } |
| 41133 | + |
| 41134 | + for(i=0; rc==SQLITE_OK && i<nSegment; i++){ |
| 41012 | 41135 | volatile ht_slot *aHash; |
| 41013 | | - int j; |
| 41014 | 41136 | u32 iZero; |
| 41015 | | - int nEntry; |
| 41016 | 41137 | volatile u32 *aPgno; |
| 41017 | | - int rc; |
| 41018 | 41138 | |
| 41019 | 41139 | rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero); |
| 41020 | | - if( rc!=SQLITE_OK ){ |
| 41021 | | - walIteratorFree(p); |
| 41022 | | - return rc; |
| 41023 | | - } |
| 41024 | | - aPgno++; |
| 41025 | | - nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno; |
| 41026 | | - iZero++; |
| 41027 | | - |
| 41028 | | - for(j=0; j<nEntry; j++){ |
| 41029 | | - aSpace[j] = j; |
| 41030 | | - } |
| 41031 | | - walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry); |
| 41032 | | - p->aSegment[i].iZero = iZero; |
| 41033 | | - p->aSegment[i].nEntry = nEntry; |
| 41034 | | - p->aSegment[i].aIndex = aSpace; |
| 41035 | | - p->aSegment[i].aPgno = (u32 *)aPgno; |
| 41036 | | - aSpace += HASHTABLE_NPAGE; |
| 41037 | | - } |
| 41038 | | - assert( aSpace==aTmp ); |
| 41039 | | - |
| 41040 | | - /* Return the fully initialized WalIterator object */ |
| 41140 | + if( rc==SQLITE_OK ){ |
| 41141 | + int j; /* Counter variable */ |
| 41142 | + int nEntry; /* Number of entries in this segment */ |
| 41143 | + ht_slot *aIndex; /* Sorted index for this segment */ |
| 41144 | + |
| 41145 | + aPgno++; |
| 41146 | + nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno; |
| 41147 | + aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero]; |
| 41148 | + iZero++; |
| 41149 | + |
| 41150 | + for(j=0; j<nEntry; j++){ |
| 41151 | + aIndex[j] = j; |
| 41152 | + } |
| 41153 | + walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry); |
| 41154 | + p->aSegment[i].iZero = iZero; |
| 41155 | + p->aSegment[i].nEntry = nEntry; |
| 41156 | + p->aSegment[i].aIndex = aIndex; |
| 41157 | + p->aSegment[i].aPgno = (u32 *)aPgno; |
| 41158 | + } |
| 41159 | + } |
| 41160 | + sqlite3ScratchFree(aTmp); |
| 41161 | + |
| 41162 | + if( rc!=SQLITE_OK ){ |
| 41163 | + walIteratorFree(p); |
| 41164 | + } |
| 41041 | 41165 | *pp = p; |
| 41042 | | - return SQLITE_OK ; |
| 41166 | + return rc; |
| 41043 | 41167 | } |
| 41044 | 41168 | |
| 41045 | 41169 | /* |
| 41046 | 41170 | ** Copy as much content as we can from the WAL back into the database file |
| 41047 | 41171 | ** in response to an sqlite3_wal_checkpoint() request or the equivalent. |
| | @@ -41086,15 +41210,18 @@ |
| 41086 | 41210 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
| 41087 | 41211 | u32 mxSafeFrame; /* Max frame that can be backfilled */ |
| 41088 | 41212 | int i; /* Loop counter */ |
| 41089 | 41213 | volatile WalCkptInfo *pInfo; /* The checkpoint status information */ |
| 41090 | 41214 | |
| 41215 | + if( pWal->hdr.mxFrame==0 ) return SQLITE_OK; |
| 41216 | + |
| 41091 | 41217 | /* Allocate the iterator */ |
| 41092 | 41218 | rc = walIteratorInit(pWal, &pIter); |
| 41093 | | - if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){ |
| 41094 | | - goto walcheckpoint_out; |
| 41219 | + if( rc!=SQLITE_OK ){ |
| 41220 | + return rc; |
| 41095 | 41221 | } |
| 41222 | + assert( pIter ); |
| 41096 | 41223 | |
| 41097 | 41224 | /*** TODO: Move this test out to the caller. Make it an assert() here ***/ |
| 41098 | 41225 | if( pWal->hdr.szPage!=nBuf ){ |
| 41099 | 41226 | rc = SQLITE_CORRUPT_BKPT; |
| 41100 | 41227 | goto walcheckpoint_out; |
| | @@ -41275,30 +41402,24 @@ |
| 41275 | 41402 | return 0; |
| 41276 | 41403 | } |
| 41277 | 41404 | |
| 41278 | 41405 | /* |
| 41279 | 41406 | ** Read the wal-index header from the wal-index and into pWal->hdr. |
| 41280 | | -** If the wal-header appears to be corrupt, try to recover the log |
| 41281 | | -** before returning. |
| 41407 | +** If the wal-header appears to be corrupt, try to reconstruct the |
| 41408 | +** wal-index from the WAL before returning. |
| 41282 | 41409 | ** |
| 41283 | 41410 | ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is |
| 41284 | 41411 | ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged |
| 41285 | 41412 | ** to 0. |
| 41286 | 41413 | ** |
| 41287 | | -** This routine also maps the wal-index content into memory and assigns |
| 41288 | | -** ownership of that mapping to the current thread. In some implementations, |
| 41289 | | -** only one thread at a time can hold a mapping of the wal-index. Hence, |
| 41290 | | -** the caller should strive to invoke walIndexUnmap() as soon as possible |
| 41291 | | -** after this routine returns. |
| 41292 | | -** |
| 41293 | 41414 | ** If the wal-index header is successfully read, return SQLITE_OK. |
| 41294 | 41415 | ** Otherwise an SQLite error code. |
| 41295 | 41416 | */ |
| 41296 | 41417 | static int walIndexReadHdr(Wal *pWal, int *pChanged){ |
| 41297 | 41418 | int rc; /* Return code */ |
| 41298 | 41419 | int badHdr; /* True if a header read failed */ |
| 41299 | | - volatile u32 *page0; |
| 41420 | + volatile u32 *page0; /* Chunk of wal-index containing header */ |
| 41300 | 41421 | |
| 41301 | 41422 | /* Ensure that page 0 of the wal-index (the page that contains the |
| 41302 | 41423 | ** wal-index header) is mapped. Return early if an error occurs here. |
| 41303 | 41424 | */ |
| 41304 | 41425 | assert( pChanged ); |
| | @@ -41309,11 +41430,11 @@ |
| 41309 | 41430 | assert( page0 || pWal->writeLock==0 ); |
| 41310 | 41431 | |
| 41311 | 41432 | /* If the first page of the wal-index has been mapped, try to read the |
| 41312 | 41433 | ** wal-index header immediately, without holding any lock. This usually |
| 41313 | 41434 | ** works, but may fail if the wal-index header is corrupt or currently |
| 41314 | | - ** being modified by another user. |
| 41435 | + ** being modified by another thread or process. |
| 41315 | 41436 | */ |
| 41316 | 41437 | badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); |
| 41317 | 41438 | |
| 41318 | 41439 | /* If the first attempt failed, it might have been due to a race |
| 41319 | 41440 | ** with a writer. So get a WRITE lock and try again. |
| | @@ -41333,10 +41454,18 @@ |
| 41333 | 41454 | } |
| 41334 | 41455 | } |
| 41335 | 41456 | pWal->writeLock = 0; |
| 41336 | 41457 | walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 41337 | 41458 | } |
| 41459 | + |
| 41460 | + /* If the header is read successfully, check the version number to make |
| 41461 | + ** sure the wal-index was not constructed with some future format that |
| 41462 | + ** this version of SQLite cannot understand. |
| 41463 | + */ |
| 41464 | + if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){ |
| 41465 | + rc = SQLITE_CANTOPEN_BKPT; |
| 41466 | + } |
| 41338 | 41467 | |
| 41339 | 41468 | return rc; |
| 41340 | 41469 | } |
| 41341 | 41470 | |
| 41342 | 41471 | /* |
| | @@ -41348,13 +41477,33 @@ |
| 41348 | 41477 | /* |
| 41349 | 41478 | ** Attempt to start a read transaction. This might fail due to a race or |
| 41350 | 41479 | ** other transient condition. When that happens, it returns WAL_RETRY to |
| 41351 | 41480 | ** indicate to the caller that it is safe to retry immediately. |
| 41352 | 41481 | ** |
| 41353 | | -** On success return SQLITE_OK. On a permantent failure (such an |
| 41482 | +** On success return SQLITE_OK. On a permanent failure (such an |
| 41354 | 41483 | ** I/O error or an SQLITE_BUSY because another process is running |
| 41355 | 41484 | ** recovery) return a positive error code. |
| 41485 | +** |
| 41486 | +** The useWal parameter is true to force the use of the WAL and disable |
| 41487 | +** the case where the WAL is bypassed because it has been completely |
| 41488 | +** checkpointed. If useWal==0 then this routine calls walIndexReadHdr() |
| 41489 | +** to make a copy of the wal-index header into pWal->hdr. If the |
| 41490 | +** wal-index header has changed, *pChanged is set to 1 (as an indication |
| 41491 | +** to the caller that the local paget cache is obsolete and needs to be |
| 41492 | +** flushed.) When useWal==1, the wal-index header is assumed to already |
| 41493 | +** be loaded and the pChanged parameter is unused. |
| 41494 | +** |
| 41495 | +** The caller must set the cnt parameter to the number of prior calls to |
| 41496 | +** this routine during the current read attempt that returned WAL_RETRY. |
| 41497 | +** This routine will start taking more aggressive measures to clear the |
| 41498 | +** race conditions after multiple WAL_RETRY returns, and after an excessive |
| 41499 | +** number of errors will ultimately return SQLITE_PROTOCOL. The |
| 41500 | +** SQLITE_PROTOCOL return indicates that some other process has gone rogue |
| 41501 | +** and is not honoring the locking protocol. There is a vanishingly small |
| 41502 | +** chance that SQLITE_PROTOCOL could be returned because of a run of really |
| 41503 | +** bad luck when there is lots of contention for the wal-index, but that |
| 41504 | +** possibility is so small that it can be safely neglected, we believe. |
| 41356 | 41505 | ** |
| 41357 | 41506 | ** On success, this routine obtains a read lock on |
| 41358 | 41507 | ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is |
| 41359 | 41508 | ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1) |
| 41360 | 41509 | ** that means the Wal does not hold any read lock. The reader must not |
| | @@ -41361,10 +41510,12 @@ |
| 41361 | 41510 | ** access any database page that is modified by a WAL frame up to and |
| 41362 | 41511 | ** including frame number aReadMark[pWal->readLock]. The reader will |
| 41363 | 41512 | ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0 |
| 41364 | 41513 | ** Or if pWal->readLock==0, then the reader will ignore the WAL |
| 41365 | 41514 | ** completely and get all content directly from the database file. |
| 41515 | +** If the useWal parameter is 1 then the WAL will never be ignored and |
| 41516 | +** this routine will always set pWal->readLock>0 on success. |
| 41366 | 41517 | ** When the read transaction is completed, the caller must release the |
| 41367 | 41518 | ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1. |
| 41368 | 41519 | ** |
| 41369 | 41520 | ** This routine uses the nBackfill and aReadMark[] fields of the header |
| 41370 | 41521 | ** to select a particular WAL_READ_LOCK() that strives to let the |
| | @@ -41405,13 +41556,13 @@ |
| 41405 | 41556 | rc = WAL_RETRY; |
| 41406 | 41557 | }else if( rc==SQLITE_BUSY ){ |
| 41407 | 41558 | rc = SQLITE_BUSY_RECOVERY; |
| 41408 | 41559 | } |
| 41409 | 41560 | } |
| 41410 | | - } |
| 41411 | | - if( rc!=SQLITE_OK ){ |
| 41412 | | - return rc; |
| 41561 | + if( rc!=SQLITE_OK ){ |
| 41562 | + return rc; |
| 41563 | + } |
| 41413 | 41564 | } |
| 41414 | 41565 | |
| 41415 | 41566 | pInfo = walCkptInfo(pWal); |
| 41416 | 41567 | if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){ |
| 41417 | 41568 | /* The WAL has been completely backfilled (or it is empty). |
| | @@ -41583,13 +41734,13 @@ |
| 41583 | 41734 | /* This routine is only be called from within a read transaction. */ |
| 41584 | 41735 | assert( pWal->readLock>=0 || pWal->lockError ); |
| 41585 | 41736 | |
| 41586 | 41737 | /* If the "last page" field of the wal-index header snapshot is 0, then |
| 41587 | 41738 | ** no data will be read from the wal under any circumstances. Return early |
| 41588 | | - ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if |
| 41589 | | - ** pWal->readLock==0, then the WAL is ignored by the reader so |
| 41590 | | - ** return early, as if the WAL were empty. |
| 41739 | + ** in this case as an optimization. Likewise, if pWal->readLock==0, |
| 41740 | + ** then the WAL is ignored by the reader so return early, as if the |
| 41741 | + ** WAL were empty. |
| 41591 | 41742 | */ |
| 41592 | 41743 | if( iLast==0 || pWal->readLock==0 ){ |
| 41593 | 41744 | *pInWal = 0; |
| 41594 | 41745 | return SQLITE_OK; |
| 41595 | 41746 | } |
| | @@ -41596,11 +41747,11 @@ |
| 41596 | 41747 | |
| 41597 | 41748 | /* Search the hash table or tables for an entry matching page number |
| 41598 | 41749 | ** pgno. Each iteration of the following for() loop searches one |
| 41599 | 41750 | ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). |
| 41600 | 41751 | ** |
| 41601 | | - ** This code may run concurrently to the code in walIndexAppend() |
| 41752 | + ** This code might run concurrently to the code in walIndexAppend() |
| 41602 | 41753 | ** that adds entries to the wal-index (and possibly to this hash |
| 41603 | 41754 | ** table). This means the value just read from the hash |
| 41604 | 41755 | ** slot (aHash[iKey]) may have been added before or after the |
| 41605 | 41756 | ** current read transaction was opened. Values added after the |
| 41606 | 41757 | ** read transaction was opened may have been written incorrectly - |
| | @@ -41919,44 +42070,59 @@ |
| 41919 | 42070 | ** header to the start of the WAL file. See comments at the top of |
| 41920 | 42071 | ** this source file for a description of the WAL header format. |
| 41921 | 42072 | */ |
| 41922 | 42073 | iFrame = pWal->hdr.mxFrame; |
| 41923 | 42074 | if( iFrame==0 ){ |
| 41924 | | - u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */ |
| 42075 | + u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */ |
| 42076 | + u32 aCksum[2]; /* Checksum for wal-header */ |
| 42077 | + |
| 41925 | 42078 | sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN)); |
| 41926 | | - sqlite3Put4byte(&aWalHdr[4], 3007000); |
| 42079 | + sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION); |
| 41927 | 42080 | sqlite3Put4byte(&aWalHdr[8], szPage); |
| 41928 | | - pWal->szPage = szPage; |
| 41929 | | - pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; |
| 41930 | 42081 | sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt); |
| 41931 | 42082 | memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); |
| 42083 | + walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); |
| 42084 | + sqlite3Put4byte(&aWalHdr[24], aCksum[0]); |
| 42085 | + sqlite3Put4byte(&aWalHdr[28], aCksum[1]); |
| 42086 | + |
| 42087 | + pWal->szPage = szPage; |
| 42088 | + pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; |
| 42089 | + pWal->hdr.aFrameCksum[0] = aCksum[0]; |
| 42090 | + pWal->hdr.aFrameCksum[1] = aCksum[1]; |
| 42091 | + |
| 41932 | 42092 | rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); |
| 41933 | 42093 | WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); |
| 41934 | 42094 | if( rc!=SQLITE_OK ){ |
| 41935 | 42095 | return rc; |
| 41936 | 42096 | } |
| 41937 | | - walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum); |
| 41938 | 42097 | } |
| 41939 | 42098 | assert( pWal->szPage==szPage ); |
| 41940 | 42099 | |
| 41941 | 42100 | /* Write the log file. */ |
| 41942 | 42101 | for(p=pList; p; p=p->pDirty){ |
| 41943 | 42102 | u32 nDbsize; /* Db-size field for frame header */ |
| 41944 | 42103 | i64 iOffset; /* Write offset in log file */ |
| 41945 | | - |
| 42104 | + void *pData; |
| 42105 | + |
| 42106 | + |
| 41946 | 42107 | iOffset = walFrameOffset(++iFrame, szPage); |
| 41947 | 42108 | |
| 41948 | 42109 | /* Populate and write the frame header */ |
| 41949 | 42110 | nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; |
| 41950 | | - walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame); |
| 42111 | +#if defined(SQLITE_HAS_CODEC) |
| 42112 | + if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM; |
| 42113 | +#else |
| 42114 | + pData = p->pData; |
| 42115 | +#endif |
| 42116 | + walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame); |
| 41951 | 42117 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
| 41952 | 42118 | if( rc!=SQLITE_OK ){ |
| 41953 | 42119 | return rc; |
| 41954 | 42120 | } |
| 41955 | 42121 | |
| 41956 | 42122 | /* Write the page data */ |
| 41957 | | - rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame)); |
| 42123 | + rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame)); |
| 41958 | 42124 | if( rc!=SQLITE_OK ){ |
| 41959 | 42125 | return rc; |
| 41960 | 42126 | } |
| 41961 | 42127 | pLast = p; |
| 41962 | 42128 | } |
| | @@ -41969,18 +42135,23 @@ |
| 41969 | 42135 | assert( isCommit ); |
| 41970 | 42136 | assert( iSegment>0 ); |
| 41971 | 42137 | |
| 41972 | 42138 | iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); |
| 41973 | 42139 | while( iOffset<iSegment ){ |
| 41974 | | - walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame); |
| 42140 | + void *pData; |
| 42141 | +#if defined(SQLITE_HAS_CODEC) |
| 42142 | + if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM; |
| 42143 | +#else |
| 42144 | + pData = pLast->pData; |
| 42145 | +#endif |
| 42146 | + walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame); |
| 41975 | 42147 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
| 41976 | 42148 | if( rc!=SQLITE_OK ){ |
| 41977 | 42149 | return rc; |
| 41978 | 42150 | } |
| 41979 | | - |
| 41980 | 42151 | iOffset += WAL_FRAME_HDRSIZE; |
| 41981 | | - rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset); |
| 42152 | + rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset); |
| 41982 | 42153 | if( rc!=SQLITE_OK ){ |
| 41983 | 42154 | return rc; |
| 41984 | 42155 | } |
| 41985 | 42156 | nLast++; |
| 41986 | 42157 | iOffset += szPage; |
| | @@ -52844,13 +53015,20 @@ |
| 52844 | 53015 | if( !pExpr ){ |
| 52845 | 53016 | *ppVal = 0; |
| 52846 | 53017 | return SQLITE_OK; |
| 52847 | 53018 | } |
| 52848 | 53019 | op = pExpr->op; |
| 52849 | | - if( op==TK_REGISTER ){ |
| 52850 | | - op = pExpr->op2; /* This only happens with SQLITE_ENABLE_STAT2 */ |
| 52851 | | - } |
| 53020 | + |
| 53021 | + /* op can only be TK_REGISTER is we have compiled with SQLITE_ENABLE_STAT2. |
| 53022 | + ** The ifdef here is to enable us to achieve 100% branch test coverage even |
| 53023 | + ** when SQLITE_ENABLE_STAT2 is omitted. |
| 53024 | + */ |
| 53025 | +#ifdef SQLITE_ENABLE_STAT2 |
| 53026 | + if( op==TK_REGISTER ) op = pExpr->op2; |
| 53027 | +#else |
| 53028 | + if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; |
| 53029 | +#endif |
| 52852 | 53030 | |
| 52853 | 53031 | if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ |
| 52854 | 53032 | pVal = sqlite3ValueNew(db); |
| 52855 | 53033 | if( pVal==0 ) goto no_mem; |
| 52856 | 53034 | if( ExprHasProperty(pExpr, EP_IntValue) ){ |
| | @@ -63351,10 +63529,14 @@ |
| 63351 | 63529 | */ |
| 63352 | 63530 | rc = sqlite3PagerCloseWal(u.cd.pPager); |
| 63353 | 63531 | if( rc==SQLITE_OK ){ |
| 63354 | 63532 | sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew); |
| 63355 | 63533 | } |
| 63534 | + }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){ |
| 63535 | + /* Cannot transition directly from MEMORY to WAL. Use mode OFF |
| 63536 | + ** as an intermediate */ |
| 63537 | + sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF); |
| 63356 | 63538 | } |
| 63357 | 63539 | |
| 63358 | 63540 | /* Open a transaction on the database file. Regardless of the journal |
| 63359 | 63541 | ** mode, this transaction always uses a rollback journal. |
| 63360 | 63542 | */ |
| | @@ -103905,21 +104087,26 @@ |
| 103905 | 104087 | ** a low-level error is first detected. |
| 103906 | 104088 | */ |
| 103907 | 104089 | SQLITE_PRIVATE int sqlite3CorruptError(int lineno){ |
| 103908 | 104090 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 103909 | 104091 | sqlite3_log(SQLITE_CORRUPT, |
| 103910 | | - "database corruption found by source line %d", lineno); |
| 104092 | + "database corruption at line %d of [%.10s]", |
| 104093 | + lineno, 20+sqlite3_sourceid()); |
| 103911 | 104094 | return SQLITE_CORRUPT; |
| 103912 | 104095 | } |
| 103913 | 104096 | SQLITE_PRIVATE int sqlite3MisuseError(int lineno){ |
| 103914 | 104097 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 103915 | | - sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno); |
| 104098 | + sqlite3_log(SQLITE_MISUSE, |
| 104099 | + "misuse at line %d of [%.10s]", |
| 104100 | + lineno, 20+sqlite3_sourceid()); |
| 103916 | 104101 | return SQLITE_MISUSE; |
| 103917 | 104102 | } |
| 103918 | 104103 | SQLITE_PRIVATE int sqlite3CantopenError(int lineno){ |
| 103919 | 104104 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 103920 | | - sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno); |
| 104105 | + sqlite3_log(SQLITE_CANTOPEN, |
| 104106 | + "cannot open file at line %d of [%.10s]", |
| 104107 | + lineno, 20+sqlite3_sourceid()); |
| 103921 | 104108 | return SQLITE_CANTOPEN; |
| 103922 | 104109 | } |
| 103923 | 104110 | |
| 103924 | 104111 | |
| 103925 | 104112 | #ifndef SQLITE_OMIT_DEPRECATED |
| 103926 | 104113 | |