Fossil SCM

Update to the latest SQLite with the new WAL file format containing version numbers.

drh 2010-06-24 11:10 trunk
Commit e0bdd5afce4400944950e59f936d73b6d18efffa
2 files changed +117 -40 +1 -1
+117 -40
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636636
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637637
** [sqlite_version()] and [sqlite_source_id()].
638638
*/
639639
#define SQLITE_VERSION "3.7.0"
640640
#define SQLITE_VERSION_NUMBER 3007000
641
-#define SQLITE_SOURCE_ID "2010-06-23 15:18:12 51ef43b9f7db8fabf73d9c8a76dae6c275e50d58"
641
+#define SQLITE_SOURCE_ID "2010-06-24 10:50:18 7aac9ad6dd14b1c56eb8e4750ac769c6197c30bd"
642642
643643
/*
644644
** CAPI3REF: Run-Time Library Version Numbers
645645
** KEYWORDS: sqlite3_version, sqlite3_sourceid
646646
**
@@ -25586,11 +25586,11 @@
2558625586
};
2558725587
2558825588
/*
2558925589
** Constants used for locking
2559025590
*/
25591
-#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 */
2559225592
#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
2559325593
2559425594
/*
2559525595
** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
2559625596
**
@@ -30017,11 +30017,11 @@
3001730017
};
3001830018
3001930019
/*
3002030020
** Constants used for locking
3002130021
*/
30022
-#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 */
3002330023
#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
3002430024
3002530025
/*
3002630026
** Apply advisory locks for all n bytes beginning at ofst.
3002730027
*/
@@ -34840,11 +34840,11 @@
3484034840
** structure, the second the error-code about to be returned by a pager
3484134841
** API function. The value returned is a copy of the second argument
3484234842
** to this function.
3484334843
**
3484434844
** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34845
-** the error becomes persistent. Until the persisten error is cleared,
34845
+** the error becomes persistent. Until the persistent error is cleared,
3484634846
** subsequent API calls on this Pager will immediately return the same
3484734847
** error code.
3484834848
**
3484934849
** A persistent error indicates that the contents of the pager-cache
3485034850
** cannot be trusted. This state can be cleared by completely discarding
@@ -37092,13 +37092,11 @@
3709237092
** reusing pPg.
3709337093
**
3709437094
** Similarly, if the pager has already entered the error state, do not
3709537095
** try to write the contents of pPg to disk.
3709637096
*/
37097
- if( NEVER(pPager->errCode)
37098
- || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37099
- ){
37097
+ if( pPager->errCode || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC) ){
3710037098
return SQLITE_OK;
3710137099
}
3710237100
3710337101
/* Sync the journal file if required. */
3710437102
if( pPg->flags&PGHDR_NEED_SYNC ){
@@ -38649,11 +38647,11 @@
3864938647
3865038648
/* The dbOrigSize is never set if journal_mode=OFF */
3865138649
assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
3865238650
3865338651
/* If a prior error occurred, report that error again. */
38654
- if( NEVER(pPager->errCode) ) return pPager->errCode;
38652
+ if( pPager->errCode ) return pPager->errCode;
3865538653
3865638654
PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3865738655
pPager->zFilename, zMaster, pPager->dbSize));
3865838656
3865938657
if( MEMDB && pPager->dbModified ){
@@ -38879,11 +38877,11 @@
3887938877
** performed. If successful, task (2). Regardless of the outcome
3888038878
** of either, the error state error code is returned to the caller
3888138879
** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
3888238880
**
3888338881
** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38884
-** or not (1) is succussful, also attempt (2). If successful, return
38882
+** or not (1) is successful, also attempt (2). If successful, return
3888538883
** SQLITE_OK. Otherwise, enter the error state and return the first
3888638884
** error code encountered.
3888738885
**
3888838886
** In this case there is no chance that the database was written to.
3888938887
** So is safe to finalize the journal file even if the playback
@@ -39708,23 +39706,25 @@
3970839706
** frames can overwrite the old ones. A WAL always grows from beginning
3970939707
** toward the end. Checksums and counters attached to each frame are
3971039708
** used to determine which frames within the WAL are valid and which
3971139709
** are leftovers from prior checkpoints.
3971239710
**
39713
-** The WAL header is 24 bytes in size and consists of the following six
39711
+** The WAL header is 32 bytes in size and consists of the following eight
3971439712
** big-endian 32-bit unsigned integer values:
3971539713
**
3971639714
** 0: Magic number. 0x377f0682 or 0x377f0683
3971739715
** 4: File format version. Currently 3007000
3971839716
** 8: Database page size. Example: 1024
3971939717
** 12: Checkpoint sequence number
3972039718
** 16: Salt-1, random integer incremented with each checkpoint
3972139719
** 20: Salt-2, a different random integer changing with each ckpt
39720
+** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
39721
+** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
3972239722
**
3972339723
** Immediately following the wal-header are zero or more frames. Each
3972439724
** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39725
-** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39725
+** of page data. The frame-header is six big-endian 32-bit unsigned
3972639726
** integer values, as follows:
3972739727
**
3972839728
** 0: Page number.
3972939729
** 4: For commit records, the size of the database image in pages
3973039730
** after the commit. For all other records, zero.
@@ -39755,10 +39755,15 @@
3975539755
**
3975639756
** for i from 0 to n-1 step 2:
3975739757
** s0 += x[i] + s1;
3975839758
** s1 += x[i+1] + s0;
3975939759
** endfor
39760
+**
39761
+** Note that s0 and s1 are both weighted checksums using fibonacci weights
39762
+** in reverse order (the largest fibonacci weight occurs on the first element
39763
+** of the sequence being summed.) The s1 value spans all 32-bit
39764
+** terms of the sequence whereas s0 omits the final term.
3976039765
**
3976139766
** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
3976239767
** WAL is transferred into the database, then the database is VFS.xSync-ed.
3976339768
** The VFS.xSync operations serve as write barriers - all writes launched
3976439769
** before the xSync must complete before any write that launches after the
@@ -39922,10 +39927,25 @@
3992239927
# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
3992339928
#else
3992439929
# define WALTRACE(X)
3992539930
#endif
3992639931
39932
+/*
39933
+** The maximum (and only) versions of the wal and wal-index formats
39934
+** that may be interpreted by this version of SQLite.
39935
+**
39936
+** If a client begins recovering a WAL file and finds that (a) the checksum
39937
+** values in the wal-header are correct and (b) the version field is not
39938
+** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
39939
+**
39940
+** Similarly, if a client successfully reads a wal-index header (i.e. the
39941
+** checksum test is successful) and finds that the version field is not
39942
+** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
39943
+** returns SQLITE_CANTOPEN.
39944
+*/
39945
+#define WAL_MAX_VERSION 3007000
39946
+#define WALINDEX_MAX_VERSION 3007000
3992739947
3992839948
/*
3992939949
** Indices of various locking bytes. WAL_NREADER is the number
3993039950
** of available reader locks and should be at least 3.
3993139951
*/
@@ -39948,10 +39968,12 @@
3994839968
**
3994939969
** The actual header in the wal-index consists of two copies of this
3995039970
** object.
3995139971
*/
3995239972
struct WalIndexHdr {
39973
+ u32 iVersion; /* Wal-index version */
39974
+ u32 unused; /* Unused (padding) field */
3995339975
u32 iChange; /* Counter incremented each transaction */
3995439976
u8 isInit; /* 1 when initialized */
3995539977
u8 bigEndCksum; /* True if checksums in WAL are big-endian */
3995639978
u16 szPage; /* Database page size in bytes */
3995739979
u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40027,12 +40049,13 @@
4002740049
#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
4002840050
4002940051
/* Size of header before each frame in wal */
4003040052
#define WAL_FRAME_HDRSIZE 24
4003140053
40032
-/* Size of write ahead log header */
40033
-#define WAL_HDRSIZE 24
40054
+/* Size of write ahead log header, including checksum. */
40055
+/* #define WAL_HDRSIZE 24 */
40056
+#define WAL_HDRSIZE 32
4003440057
4003540058
/* WAL magic value. Either this value, or the same value with the least
4003640059
** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
4003740060
** big-endian format in the first 4 bytes of a WAL file.
4003840061
**
@@ -40256,10 +40279,11 @@
4025640279
volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
4025740280
const int nCksum = offsetof(WalIndexHdr, aCksum);
4025840281
4025940282
assert( pWal->writeLock );
4026040283
pWal->hdr.isInit = 1;
40284
+ pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
4026140285
walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
4026240286
memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
4026340287
sqlite3OsShmBarrier(pWal->pDbFd);
4026440288
memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
4026540289
}
@@ -40705,10 +40729,11 @@
4070540729
u8 *aData; /* Pointer to data part of aFrame buffer */
4070640730
int iFrame; /* Index of last frame read */
4070740731
i64 iOffset; /* Next offset to read from log file */
4070840732
int szPage; /* Page size according to the log */
4070940733
u32 magic; /* Magic value read from WAL header */
40734
+ u32 version; /* Magic value read from WAL header */
4071040735
4071140736
/* Read in the WAL header. */
4071240737
rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
4071340738
if( rc!=SQLITE_OK ){
4071440739
goto recovery_error;
@@ -40730,13 +40755,28 @@
4073040755
}
4073140756
pWal->hdr.bigEndCksum = (magic&0x00000001);
4073240757
pWal->szPage = szPage;
4073340758
pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
4073440759
memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40760
+
40761
+ /* Verify that the WAL header checksum is correct */
4073540762
walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40736
- aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40763
+ aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
4073740764
);
40765
+ if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
40766
+ || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
40767
+ ){
40768
+ goto finished;
40769
+ }
40770
+
40771
+ /* Verify that the version number on the WAL format is one that
40772
+ ** are able to understand */
40773
+ version = sqlite3Get4byte(&aBuf[4]);
40774
+ if( version!=WAL_MAX_VERSION ){
40775
+ rc = SQLITE_CANTOPEN_BKPT;
40776
+ goto finished;
40777
+ }
4073840778
4073940779
/* Malloc a buffer to read frames into. */
4074040780
szFrame = szPage + WAL_FRAME_HDRSIZE;
4074140781
aFrame = (u8 *)sqlite3_malloc(szFrame);
4074240782
if( !aFrame ){
@@ -41297,30 +41337,24 @@
4129741337
return 0;
4129841338
}
4129941339
4130041340
/*
4130141341
** Read the wal-index header from the wal-index and into pWal->hdr.
41302
-** If the wal-header appears to be corrupt, try to recover the log
41303
-** before returning.
41342
+** If the wal-header appears to be corrupt, try to reconstruct the
41343
+** wal-index from the WAL before returning.
4130441344
**
4130541345
** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
4130641346
** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
4130741347
** to 0.
4130841348
**
41309
-** This routine also maps the wal-index content into memory and assigns
41310
-** ownership of that mapping to the current thread. In some implementations,
41311
-** only one thread at a time can hold a mapping of the wal-index. Hence,
41312
-** the caller should strive to invoke walIndexUnmap() as soon as possible
41313
-** after this routine returns.
41314
-**
4131541349
** If the wal-index header is successfully read, return SQLITE_OK.
4131641350
** Otherwise an SQLite error code.
4131741351
*/
4131841352
static int walIndexReadHdr(Wal *pWal, int *pChanged){
4131941353
int rc; /* Return code */
4132041354
int badHdr; /* True if a header read failed */
41321
- volatile u32 *page0;
41355
+ volatile u32 *page0; /* Chunk of wal-index containing header */
4132241356
4132341357
/* Ensure that page 0 of the wal-index (the page that contains the
4132441358
** wal-index header) is mapped. Return early if an error occurs here.
4132541359
*/
4132641360
assert( pChanged );
@@ -41331,11 +41365,11 @@
4133141365
assert( page0 || pWal->writeLock==0 );
4133241366
4133341367
/* If the first page of the wal-index has been mapped, try to read the
4133441368
** wal-index header immediately, without holding any lock. This usually
4133541369
** works, but may fail if the wal-index header is corrupt or currently
41336
- ** being modified by another user.
41370
+ ** being modified by another thread or process.
4133741371
*/
4133841372
badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
4133941373
4134041374
/* If the first attempt failed, it might have been due to a race
4134141375
** with a writer. So get a WRITE lock and try again.
@@ -41355,10 +41389,18 @@
4135541389
}
4135641390
}
4135741391
pWal->writeLock = 0;
4135841392
walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
4135941393
}
41394
+
41395
+ /* If the header is read successfully, check the version number to make
41396
+ ** sure the wal-index was not constructed with some future format that
41397
+ ** this version of SQLite cannot understand.
41398
+ */
41399
+ if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
41400
+ rc = SQLITE_CANTOPEN_BKPT;
41401
+ }
4136041402
4136141403
return rc;
4136241404
}
4136341405
4136441406
/*
@@ -41370,13 +41412,33 @@
4137041412
/*
4137141413
** Attempt to start a read transaction. This might fail due to a race or
4137241414
** other transient condition. When that happens, it returns WAL_RETRY to
4137341415
** indicate to the caller that it is safe to retry immediately.
4137441416
**
41375
-** On success return SQLITE_OK. On a permantent failure (such an
41417
+** On success return SQLITE_OK. On a permanent failure (such an
4137641418
** I/O error or an SQLITE_BUSY because another process is running
4137741419
** recovery) return a positive error code.
41420
+**
41421
+** The useWal parameter is true to force the use of the WAL and disable
41422
+** the case where the WAL is bypassed because it has been completely
41423
+** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
41424
+** to make a copy of the wal-index header into pWal->hdr. If the
41425
+** wal-index header has changed, *pChanged is set to 1 (as an indication
41426
+** to the caller that the local paget cache is obsolete and needs to be
41427
+** flushed.) When useWal==1, the wal-index header is assumed to already
41428
+** be loaded and the pChanged parameter is unused.
41429
+**
41430
+** The caller must set the cnt parameter to the number of prior calls to
41431
+** this routine during the current read attempt that returned WAL_RETRY.
41432
+** This routine will start taking more aggressive measures to clear the
41433
+** race conditions after multiple WAL_RETRY returns, and after an excessive
41434
+** number of errors will ultimately return SQLITE_PROTOCOL. The
41435
+** SQLITE_PROTOCOL return indicates that some other process has gone rogue
41436
+** and is not honoring the locking protocol. There is a vanishingly small
41437
+** chance that SQLITE_PROTOCOL could be returned because of a run of really
41438
+** bad luck when there is lots of contention for the wal-index, but that
41439
+** possibility is so small that it can be safely neglected, we believe.
4137841440
**
4137941441
** On success, this routine obtains a read lock on
4138041442
** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
4138141443
** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
4138241444
** that means the Wal does not hold any read lock. The reader must not
@@ -41383,10 +41445,12 @@
4138341445
** access any database page that is modified by a WAL frame up to and
4138441446
** including frame number aReadMark[pWal->readLock]. The reader will
4138541447
** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
4138641448
** Or if pWal->readLock==0, then the reader will ignore the WAL
4138741449
** completely and get all content directly from the database file.
41450
+** If the useWal parameter is 1 then the WAL will never be ignored and
41451
+** this routine will always set pWal->readLock>0 on success.
4138841452
** When the read transaction is completed, the caller must release the
4138941453
** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
4139041454
**
4139141455
** This routine uses the nBackfill and aReadMark[] fields of the header
4139241456
** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41427,13 +41491,13 @@
4142741491
rc = WAL_RETRY;
4142841492
}else if( rc==SQLITE_BUSY ){
4142941493
rc = SQLITE_BUSY_RECOVERY;
4143041494
}
4143141495
}
41432
- }
41433
- if( rc!=SQLITE_OK ){
41434
- return rc;
41496
+ if( rc!=SQLITE_OK ){
41497
+ return rc;
41498
+ }
4143541499
}
4143641500
4143741501
pInfo = walCkptInfo(pWal);
4143841502
if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
4143941503
/* The WAL has been completely backfilled (or it is empty).
@@ -41605,13 +41669,13 @@
4160541669
/* This routine is only be called from within a read transaction. */
4160641670
assert( pWal->readLock>=0 || pWal->lockError );
4160741671
4160841672
/* If the "last page" field of the wal-index header snapshot is 0, then
4160941673
** no data will be read from the wal under any circumstances. Return early
41610
- ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41611
- ** pWal->readLock==0, then the WAL is ignored by the reader so
41612
- ** return early, as if the WAL were empty.
41674
+ ** in this case as an optimization. Likewise, if pWal->readLock==0,
41675
+ ** then the WAL is ignored by the reader so return early, as if the
41676
+ ** WAL were empty.
4161341677
*/
4161441678
if( iLast==0 || pWal->readLock==0 ){
4161541679
*pInWal = 0;
4161641680
return SQLITE_OK;
4161741681
}
@@ -41618,11 +41682,11 @@
4161841682
4161941683
/* Search the hash table or tables for an entry matching page number
4162041684
** pgno. Each iteration of the following for() loop searches one
4162141685
** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
4162241686
**
41623
- ** This code may run concurrently to the code in walIndexAppend()
41687
+ ** This code might run concurrently to the code in walIndexAppend()
4162441688
** that adds entries to the wal-index (and possibly to this hash
4162541689
** table). This means the value just read from the hash
4162641690
** slot (aHash[iKey]) may have been added before or after the
4162741691
** current read transaction was opened. Values added after the
4162841692
** read transaction was opened may have been written incorrectly -
@@ -41941,24 +42005,32 @@
4194142005
** header to the start of the WAL file. See comments at the top of
4194242006
** this source file for a description of the WAL header format.
4194342007
*/
4194442008
iFrame = pWal->hdr.mxFrame;
4194542009
if( iFrame==0 ){
41946
- u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
42010
+ u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
42011
+ u32 aCksum[2]; /* Checksum for wal-header */
42012
+
4194742013
sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41948
- sqlite3Put4byte(&aWalHdr[4], 3007000);
42014
+ sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
4194942015
sqlite3Put4byte(&aWalHdr[8], szPage);
41950
- pWal->szPage = szPage;
41951
- pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
4195242016
sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
4195342017
memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
42018
+ walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
42019
+ sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
42020
+ sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
42021
+
42022
+ pWal->szPage = szPage;
42023
+ pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
42024
+ pWal->hdr.aFrameCksum[0] = aCksum[0];
42025
+ pWal->hdr.aFrameCksum[1] = aCksum[1];
42026
+
4195442027
rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
4195542028
WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
4195642029
if( rc!=SQLITE_OK ){
4195742030
return rc;
4195842031
}
41959
- walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
4196042032
}
4196142033
assert( pWal->szPage==szPage );
4196242034
4196342035
/* Write the log file. */
4196442036
for(p=pList; p; p=p->pDirty){
@@ -103943,21 +104015,26 @@
103943104015
** a low-level error is first detected.
103944104016
*/
103945104017
SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
103946104018
testcase( sqlite3GlobalConfig.xLog!=0 );
103947104019
sqlite3_log(SQLITE_CORRUPT,
103948
- "database corruption found by source line %d", lineno);
104020
+ "database corruption at line %d of [%.10s]",
104021
+ lineno, 20+sqlite3_sourceid());
103949104022
return SQLITE_CORRUPT;
103950104023
}
103951104024
SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
103952104025
testcase( sqlite3GlobalConfig.xLog!=0 );
103953
- sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno);
104026
+ sqlite3_log(SQLITE_MISUSE,
104027
+ "misuse at line %d of [%.10s]",
104028
+ lineno, 20+sqlite3_sourceid());
103954104029
return SQLITE_MISUSE;
103955104030
}
103956104031
SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
103957104032
testcase( sqlite3GlobalConfig.xLog!=0 );
103958
- sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno);
104033
+ sqlite3_log(SQLITE_CANTOPEN,
104034
+ "cannot open file at line %d of [%.10s]",
104035
+ lineno, 20+sqlite3_sourceid());
103959104036
return SQLITE_CANTOPEN;
103960104037
}
103961104038
103962104039
103963104040
#ifndef SQLITE_OMIT_DEPRECATED
103964104041
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637 ** [sqlite_version()] and [sqlite_source_id()].
638 */
639 #define SQLITE_VERSION "3.7.0"
640 #define SQLITE_VERSION_NUMBER 3007000
641 #define SQLITE_SOURCE_ID "2010-06-23 15:18:12 51ef43b9f7db8fabf73d9c8a76dae6c275e50d58"
642
643 /*
644 ** CAPI3REF: Run-Time Library Version Numbers
645 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
646 **
@@ -25586,11 +25586,11 @@
25586 };
25587
25588 /*
25589 ** Constants used for locking
25590 */
25591 #define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25592 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25593
25594 /*
25595 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25596 **
@@ -30017,11 +30017,11 @@
30017 };
30018
30019 /*
30020 ** Constants used for locking
30021 */
30022 #define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30023 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30024
30025 /*
30026 ** Apply advisory locks for all n bytes beginning at ofst.
30027 */
@@ -34840,11 +34840,11 @@
34840 ** structure, the second the error-code about to be returned by a pager
34841 ** API function. The value returned is a copy of the second argument
34842 ** to this function.
34843 **
34844 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34845 ** the error becomes persistent. Until the persisten error is cleared,
34846 ** subsequent API calls on this Pager will immediately return the same
34847 ** error code.
34848 **
34849 ** A persistent error indicates that the contents of the pager-cache
34850 ** cannot be trusted. This state can be cleared by completely discarding
@@ -37092,13 +37092,11 @@
37092 ** reusing pPg.
37093 **
37094 ** Similarly, if the pager has already entered the error state, do not
37095 ** try to write the contents of pPg to disk.
37096 */
37097 if( NEVER(pPager->errCode)
37098 || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37099 ){
37100 return SQLITE_OK;
37101 }
37102
37103 /* Sync the journal file if required. */
37104 if( pPg->flags&PGHDR_NEED_SYNC ){
@@ -38649,11 +38647,11 @@
38649
38650 /* The dbOrigSize is never set if journal_mode=OFF */
38651 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
38652
38653 /* If a prior error occurred, report that error again. */
38654 if( NEVER(pPager->errCode) ) return pPager->errCode;
38655
38656 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
38657 pPager->zFilename, zMaster, pPager->dbSize));
38658
38659 if( MEMDB && pPager->dbModified ){
@@ -38879,11 +38877,11 @@
38879 ** performed. If successful, task (2). Regardless of the outcome
38880 ** of either, the error state error code is returned to the caller
38881 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
38882 **
38883 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38884 ** or not (1) is succussful, also attempt (2). If successful, return
38885 ** SQLITE_OK. Otherwise, enter the error state and return the first
38886 ** error code encountered.
38887 **
38888 ** In this case there is no chance that the database was written to.
38889 ** So is safe to finalize the journal file even if the playback
@@ -39708,23 +39706,25 @@
39708 ** frames can overwrite the old ones. A WAL always grows from beginning
39709 ** toward the end. Checksums and counters attached to each frame are
39710 ** used to determine which frames within the WAL are valid and which
39711 ** are leftovers from prior checkpoints.
39712 **
39713 ** The WAL header is 24 bytes in size and consists of the following six
39714 ** big-endian 32-bit unsigned integer values:
39715 **
39716 ** 0: Magic number. 0x377f0682 or 0x377f0683
39717 ** 4: File format version. Currently 3007000
39718 ** 8: Database page size. Example: 1024
39719 ** 12: Checkpoint sequence number
39720 ** 16: Salt-1, random integer incremented with each checkpoint
39721 ** 20: Salt-2, a different random integer changing with each ckpt
 
 
39722 **
39723 ** Immediately following the wal-header are zero or more frames. Each
39724 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39725 ** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39726 ** integer values, as follows:
39727 **
39728 ** 0: Page number.
39729 ** 4: For commit records, the size of the database image in pages
39730 ** after the commit. For all other records, zero.
@@ -39755,10 +39755,15 @@
39755 **
39756 ** for i from 0 to n-1 step 2:
39757 ** s0 += x[i] + s1;
39758 ** s1 += x[i+1] + s0;
39759 ** endfor
 
 
 
 
 
39760 **
39761 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39762 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
39763 ** The VFS.xSync operations serve as write barriers - all writes launched
39764 ** before the xSync must complete before any write that launches after the
@@ -39922,10 +39927,25 @@
39922 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39923 #else
39924 # define WALTRACE(X)
39925 #endif
39926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39927
39928 /*
39929 ** Indices of various locking bytes. WAL_NREADER is the number
39930 ** of available reader locks and should be at least 3.
39931 */
@@ -39948,10 +39968,12 @@
39948 **
39949 ** The actual header in the wal-index consists of two copies of this
39950 ** object.
39951 */
39952 struct WalIndexHdr {
 
 
39953 u32 iChange; /* Counter incremented each transaction */
39954 u8 isInit; /* 1 when initialized */
39955 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39956 u16 szPage; /* Database page size in bytes */
39957 u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40027,12 +40049,13 @@
40027 #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
40028
40029 /* Size of header before each frame in wal */
40030 #define WAL_FRAME_HDRSIZE 24
40031
40032 /* Size of write ahead log header */
40033 #define WAL_HDRSIZE 24
 
40034
40035 /* WAL magic value. Either this value, or the same value with the least
40036 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
40037 ** big-endian format in the first 4 bytes of a WAL file.
40038 **
@@ -40256,10 +40279,11 @@
40256 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40257 const int nCksum = offsetof(WalIndexHdr, aCksum);
40258
40259 assert( pWal->writeLock );
40260 pWal->hdr.isInit = 1;
 
40261 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40262 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40263 sqlite3OsShmBarrier(pWal->pDbFd);
40264 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40265 }
@@ -40705,10 +40729,11 @@
40705 u8 *aData; /* Pointer to data part of aFrame buffer */
40706 int iFrame; /* Index of last frame read */
40707 i64 iOffset; /* Next offset to read from log file */
40708 int szPage; /* Page size according to the log */
40709 u32 magic; /* Magic value read from WAL header */
 
40710
40711 /* Read in the WAL header. */
40712 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40713 if( rc!=SQLITE_OK ){
40714 goto recovery_error;
@@ -40730,13 +40755,28 @@
40730 }
40731 pWal->hdr.bigEndCksum = (magic&0x00000001);
40732 pWal->szPage = szPage;
40733 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40734 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
 
 
40735 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40736 aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40737 );
 
 
 
 
 
 
 
 
 
 
 
 
 
40738
40739 /* Malloc a buffer to read frames into. */
40740 szFrame = szPage + WAL_FRAME_HDRSIZE;
40741 aFrame = (u8 *)sqlite3_malloc(szFrame);
40742 if( !aFrame ){
@@ -41297,30 +41337,24 @@
41297 return 0;
41298 }
41299
41300 /*
41301 ** Read the wal-index header from the wal-index and into pWal->hdr.
41302 ** If the wal-header appears to be corrupt, try to recover the log
41303 ** before returning.
41304 **
41305 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41306 ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41307 ** to 0.
41308 **
41309 ** This routine also maps the wal-index content into memory and assigns
41310 ** ownership of that mapping to the current thread. In some implementations,
41311 ** only one thread at a time can hold a mapping of the wal-index. Hence,
41312 ** the caller should strive to invoke walIndexUnmap() as soon as possible
41313 ** after this routine returns.
41314 **
41315 ** If the wal-index header is successfully read, return SQLITE_OK.
41316 ** Otherwise an SQLite error code.
41317 */
41318 static int walIndexReadHdr(Wal *pWal, int *pChanged){
41319 int rc; /* Return code */
41320 int badHdr; /* True if a header read failed */
41321 volatile u32 *page0;
41322
41323 /* Ensure that page 0 of the wal-index (the page that contains the
41324 ** wal-index header) is mapped. Return early if an error occurs here.
41325 */
41326 assert( pChanged );
@@ -41331,11 +41365,11 @@
41331 assert( page0 || pWal->writeLock==0 );
41332
41333 /* If the first page of the wal-index has been mapped, try to read the
41334 ** wal-index header immediately, without holding any lock. This usually
41335 ** works, but may fail if the wal-index header is corrupt or currently
41336 ** being modified by another user.
41337 */
41338 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41339
41340 /* If the first attempt failed, it might have been due to a race
41341 ** with a writer. So get a WRITE lock and try again.
@@ -41355,10 +41389,18 @@
41355 }
41356 }
41357 pWal->writeLock = 0;
41358 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41359 }
 
 
 
 
 
 
 
 
41360
41361 return rc;
41362 }
41363
41364 /*
@@ -41370,13 +41412,33 @@
41370 /*
41371 ** Attempt to start a read transaction. This might fail due to a race or
41372 ** other transient condition. When that happens, it returns WAL_RETRY to
41373 ** indicate to the caller that it is safe to retry immediately.
41374 **
41375 ** On success return SQLITE_OK. On a permantent failure (such an
41376 ** I/O error or an SQLITE_BUSY because another process is running
41377 ** recovery) return a positive error code.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41378 **
41379 ** On success, this routine obtains a read lock on
41380 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41381 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41382 ** that means the Wal does not hold any read lock. The reader must not
@@ -41383,10 +41445,12 @@
41383 ** access any database page that is modified by a WAL frame up to and
41384 ** including frame number aReadMark[pWal->readLock]. The reader will
41385 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41386 ** Or if pWal->readLock==0, then the reader will ignore the WAL
41387 ** completely and get all content directly from the database file.
 
 
41388 ** When the read transaction is completed, the caller must release the
41389 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41390 **
41391 ** This routine uses the nBackfill and aReadMark[] fields of the header
41392 ** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41427,13 +41491,13 @@
41427 rc = WAL_RETRY;
41428 }else if( rc==SQLITE_BUSY ){
41429 rc = SQLITE_BUSY_RECOVERY;
41430 }
41431 }
41432 }
41433 if( rc!=SQLITE_OK ){
41434 return rc;
41435 }
41436
41437 pInfo = walCkptInfo(pWal);
41438 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41439 /* The WAL has been completely backfilled (or it is empty).
@@ -41605,13 +41669,13 @@
41605 /* This routine is only be called from within a read transaction. */
41606 assert( pWal->readLock>=0 || pWal->lockError );
41607
41608 /* If the "last page" field of the wal-index header snapshot is 0, then
41609 ** no data will be read from the wal under any circumstances. Return early
41610 ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41611 ** pWal->readLock==0, then the WAL is ignored by the reader so
41612 ** return early, as if the WAL were empty.
41613 */
41614 if( iLast==0 || pWal->readLock==0 ){
41615 *pInWal = 0;
41616 return SQLITE_OK;
41617 }
@@ -41618,11 +41682,11 @@
41618
41619 /* Search the hash table or tables for an entry matching page number
41620 ** pgno. Each iteration of the following for() loop searches one
41621 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41622 **
41623 ** This code may run concurrently to the code in walIndexAppend()
41624 ** that adds entries to the wal-index (and possibly to this hash
41625 ** table). This means the value just read from the hash
41626 ** slot (aHash[iKey]) may have been added before or after the
41627 ** current read transaction was opened. Values added after the
41628 ** read transaction was opened may have been written incorrectly -
@@ -41941,24 +42005,32 @@
41941 ** header to the start of the WAL file. See comments at the top of
41942 ** this source file for a description of the WAL header format.
41943 */
41944 iFrame = pWal->hdr.mxFrame;
41945 if( iFrame==0 ){
41946 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
 
 
41947 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41948 sqlite3Put4byte(&aWalHdr[4], 3007000);
41949 sqlite3Put4byte(&aWalHdr[8], szPage);
41950 pWal->szPage = szPage;
41951 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
41952 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
41953 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
 
 
 
 
 
 
 
 
 
41954 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
41955 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
41956 if( rc!=SQLITE_OK ){
41957 return rc;
41958 }
41959 walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
41960 }
41961 assert( pWal->szPage==szPage );
41962
41963 /* Write the log file. */
41964 for(p=pList; p; p=p->pDirty){
@@ -103943,21 +104015,26 @@
103943 ** a low-level error is first detected.
103944 */
103945 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
103946 testcase( sqlite3GlobalConfig.xLog!=0 );
103947 sqlite3_log(SQLITE_CORRUPT,
103948 "database corruption found by source line %d", lineno);
 
103949 return SQLITE_CORRUPT;
103950 }
103951 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
103952 testcase( sqlite3GlobalConfig.xLog!=0 );
103953 sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno);
 
 
103954 return SQLITE_MISUSE;
103955 }
103956 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
103957 testcase( sqlite3GlobalConfig.xLog!=0 );
103958 sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno);
 
 
103959 return SQLITE_CANTOPEN;
103960 }
103961
103962
103963 #ifndef SQLITE_OMIT_DEPRECATED
103964
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637 ** [sqlite_version()] and [sqlite_source_id()].
638 */
639 #define SQLITE_VERSION "3.7.0"
640 #define SQLITE_VERSION_NUMBER 3007000
641 #define SQLITE_SOURCE_ID "2010-06-24 10:50:18 7aac9ad6dd14b1c56eb8e4750ac769c6197c30bd"
642
643 /*
644 ** CAPI3REF: Run-Time Library Version Numbers
645 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
646 **
@@ -25586,11 +25586,11 @@
25586 };
25587
25588 /*
25589 ** Constants used for locking
25590 */
25591 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25592 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25593
25594 /*
25595 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25596 **
@@ -30017,11 +30017,11 @@
30017 };
30018
30019 /*
30020 ** Constants used for locking
30021 */
30022 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30023 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30024
30025 /*
30026 ** Apply advisory locks for all n bytes beginning at ofst.
30027 */
@@ -34840,11 +34840,11 @@
34840 ** structure, the second the error-code about to be returned by a pager
34841 ** API function. The value returned is a copy of the second argument
34842 ** to this function.
34843 **
34844 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34845 ** the error becomes persistent. Until the persistent error is cleared,
34846 ** subsequent API calls on this Pager will immediately return the same
34847 ** error code.
34848 **
34849 ** A persistent error indicates that the contents of the pager-cache
34850 ** cannot be trusted. This state can be cleared by completely discarding
@@ -37092,13 +37092,11 @@
37092 ** reusing pPg.
37093 **
37094 ** Similarly, if the pager has already entered the error state, do not
37095 ** try to write the contents of pPg to disk.
37096 */
37097 if( pPager->errCode || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC) ){
 
 
37098 return SQLITE_OK;
37099 }
37100
37101 /* Sync the journal file if required. */
37102 if( pPg->flags&PGHDR_NEED_SYNC ){
@@ -38649,11 +38647,11 @@
38647
38648 /* The dbOrigSize is never set if journal_mode=OFF */
38649 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
38650
38651 /* If a prior error occurred, report that error again. */
38652 if( pPager->errCode ) return pPager->errCode;
38653
38654 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
38655 pPager->zFilename, zMaster, pPager->dbSize));
38656
38657 if( MEMDB && pPager->dbModified ){
@@ -38879,11 +38877,11 @@
38877 ** performed. If successful, task (2). Regardless of the outcome
38878 ** of either, the error state error code is returned to the caller
38879 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
38880 **
38881 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38882 ** or not (1) is successful, also attempt (2). If successful, return
38883 ** SQLITE_OK. Otherwise, enter the error state and return the first
38884 ** error code encountered.
38885 **
38886 ** In this case there is no chance that the database was written to.
38887 ** So is safe to finalize the journal file even if the playback
@@ -39708,23 +39706,25 @@
39706 ** frames can overwrite the old ones. A WAL always grows from beginning
39707 ** toward the end. Checksums and counters attached to each frame are
39708 ** used to determine which frames within the WAL are valid and which
39709 ** are leftovers from prior checkpoints.
39710 **
39711 ** The WAL header is 32 bytes in size and consists of the following eight
39712 ** big-endian 32-bit unsigned integer values:
39713 **
39714 ** 0: Magic number. 0x377f0682 or 0x377f0683
39715 ** 4: File format version. Currently 3007000
39716 ** 8: Database page size. Example: 1024
39717 ** 12: Checkpoint sequence number
39718 ** 16: Salt-1, random integer incremented with each checkpoint
39719 ** 20: Salt-2, a different random integer changing with each ckpt
39720 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
39721 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
39722 **
39723 ** Immediately following the wal-header are zero or more frames. Each
39724 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39725 ** of page data. The frame-header is six big-endian 32-bit unsigned
39726 ** integer values, as follows:
39727 **
39728 ** 0: Page number.
39729 ** 4: For commit records, the size of the database image in pages
39730 ** after the commit. For all other records, zero.
@@ -39755,10 +39755,15 @@
39755 **
39756 ** for i from 0 to n-1 step 2:
39757 ** s0 += x[i] + s1;
39758 ** s1 += x[i+1] + s0;
39759 ** endfor
39760 **
39761 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
39762 ** in reverse order (the largest fibonacci weight occurs on the first element
39763 ** of the sequence being summed.) The s1 value spans all 32-bit
39764 ** terms of the sequence whereas s0 omits the final term.
39765 **
39766 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39767 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
39768 ** The VFS.xSync operations serve as write barriers - all writes launched
39769 ** before the xSync must complete before any write that launches after the
@@ -39922,10 +39927,25 @@
39927 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39928 #else
39929 # define WALTRACE(X)
39930 #endif
39931
39932 /*
39933 ** The maximum (and only) versions of the wal and wal-index formats
39934 ** that may be interpreted by this version of SQLite.
39935 **
39936 ** If a client begins recovering a WAL file and finds that (a) the checksum
39937 ** values in the wal-header are correct and (b) the version field is not
39938 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
39939 **
39940 ** Similarly, if a client successfully reads a wal-index header (i.e. the
39941 ** checksum test is successful) and finds that the version field is not
39942 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
39943 ** returns SQLITE_CANTOPEN.
39944 */
39945 #define WAL_MAX_VERSION 3007000
39946 #define WALINDEX_MAX_VERSION 3007000
39947
39948 /*
39949 ** Indices of various locking bytes. WAL_NREADER is the number
39950 ** of available reader locks and should be at least 3.
39951 */
@@ -39948,10 +39968,12 @@
39968 **
39969 ** The actual header in the wal-index consists of two copies of this
39970 ** object.
39971 */
39972 struct WalIndexHdr {
39973 u32 iVersion; /* Wal-index version */
39974 u32 unused; /* Unused (padding) field */
39975 u32 iChange; /* Counter incremented each transaction */
39976 u8 isInit; /* 1 when initialized */
39977 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39978 u16 szPage; /* Database page size in bytes */
39979 u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40027,12 +40049,13 @@
40049 #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
40050
40051 /* Size of header before each frame in wal */
40052 #define WAL_FRAME_HDRSIZE 24
40053
40054 /* Size of write ahead log header, including checksum. */
40055 /* #define WAL_HDRSIZE 24 */
40056 #define WAL_HDRSIZE 32
40057
40058 /* WAL magic value. Either this value, or the same value with the least
40059 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
40060 ** big-endian format in the first 4 bytes of a WAL file.
40061 **
@@ -40256,10 +40279,11 @@
40279 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40280 const int nCksum = offsetof(WalIndexHdr, aCksum);
40281
40282 assert( pWal->writeLock );
40283 pWal->hdr.isInit = 1;
40284 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
40285 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40286 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40287 sqlite3OsShmBarrier(pWal->pDbFd);
40288 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40289 }
@@ -40705,10 +40729,11 @@
40729 u8 *aData; /* Pointer to data part of aFrame buffer */
40730 int iFrame; /* Index of last frame read */
40731 i64 iOffset; /* Next offset to read from log file */
40732 int szPage; /* Page size according to the log */
40733 u32 magic; /* Magic value read from WAL header */
40734 u32 version; /* Magic value read from WAL header */
40735
40736 /* Read in the WAL header. */
40737 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40738 if( rc!=SQLITE_OK ){
40739 goto recovery_error;
@@ -40730,13 +40755,28 @@
40755 }
40756 pWal->hdr.bigEndCksum = (magic&0x00000001);
40757 pWal->szPage = szPage;
40758 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40759 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40760
40761 /* Verify that the WAL header checksum is correct */
40762 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40763 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
40764 );
40765 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
40766 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
40767 ){
40768 goto finished;
40769 }
40770
40771 /* Verify that the version number on the WAL format is one that
40772 ** are able to understand */
40773 version = sqlite3Get4byte(&aBuf[4]);
40774 if( version!=WAL_MAX_VERSION ){
40775 rc = SQLITE_CANTOPEN_BKPT;
40776 goto finished;
40777 }
40778
40779 /* Malloc a buffer to read frames into. */
40780 szFrame = szPage + WAL_FRAME_HDRSIZE;
40781 aFrame = (u8 *)sqlite3_malloc(szFrame);
40782 if( !aFrame ){
@@ -41297,30 +41337,24 @@
41337 return 0;
41338 }
41339
41340 /*
41341 ** Read the wal-index header from the wal-index and into pWal->hdr.
41342 ** If the wal-header appears to be corrupt, try to reconstruct the
41343 ** wal-index from the WAL before returning.
41344 **
41345 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41346 ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41347 ** to 0.
41348 **
 
 
 
 
 
 
41349 ** If the wal-index header is successfully read, return SQLITE_OK.
41350 ** Otherwise an SQLite error code.
41351 */
41352 static int walIndexReadHdr(Wal *pWal, int *pChanged){
41353 int rc; /* Return code */
41354 int badHdr; /* True if a header read failed */
41355 volatile u32 *page0; /* Chunk of wal-index containing header */
41356
41357 /* Ensure that page 0 of the wal-index (the page that contains the
41358 ** wal-index header) is mapped. Return early if an error occurs here.
41359 */
41360 assert( pChanged );
@@ -41331,11 +41365,11 @@
41365 assert( page0 || pWal->writeLock==0 );
41366
41367 /* If the first page of the wal-index has been mapped, try to read the
41368 ** wal-index header immediately, without holding any lock. This usually
41369 ** works, but may fail if the wal-index header is corrupt or currently
41370 ** being modified by another thread or process.
41371 */
41372 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41373
41374 /* If the first attempt failed, it might have been due to a race
41375 ** with a writer. So get a WRITE lock and try again.
@@ -41355,10 +41389,18 @@
41389 }
41390 }
41391 pWal->writeLock = 0;
41392 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41393 }
41394
41395 /* If the header is read successfully, check the version number to make
41396 ** sure the wal-index was not constructed with some future format that
41397 ** this version of SQLite cannot understand.
41398 */
41399 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
41400 rc = SQLITE_CANTOPEN_BKPT;
41401 }
41402
41403 return rc;
41404 }
41405
41406 /*
@@ -41370,13 +41412,33 @@
41412 /*
41413 ** Attempt to start a read transaction. This might fail due to a race or
41414 ** other transient condition. When that happens, it returns WAL_RETRY to
41415 ** indicate to the caller that it is safe to retry immediately.
41416 **
41417 ** On success return SQLITE_OK. On a permanent failure (such an
41418 ** I/O error or an SQLITE_BUSY because another process is running
41419 ** recovery) return a positive error code.
41420 **
41421 ** The useWal parameter is true to force the use of the WAL and disable
41422 ** the case where the WAL is bypassed because it has been completely
41423 ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
41424 ** to make a copy of the wal-index header into pWal->hdr. If the
41425 ** wal-index header has changed, *pChanged is set to 1 (as an indication
41426 ** to the caller that the local paget cache is obsolete and needs to be
41427 ** flushed.) When useWal==1, the wal-index header is assumed to already
41428 ** be loaded and the pChanged parameter is unused.
41429 **
41430 ** The caller must set the cnt parameter to the number of prior calls to
41431 ** this routine during the current read attempt that returned WAL_RETRY.
41432 ** This routine will start taking more aggressive measures to clear the
41433 ** race conditions after multiple WAL_RETRY returns, and after an excessive
41434 ** number of errors will ultimately return SQLITE_PROTOCOL. The
41435 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
41436 ** and is not honoring the locking protocol. There is a vanishingly small
41437 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
41438 ** bad luck when there is lots of contention for the wal-index, but that
41439 ** possibility is so small that it can be safely neglected, we believe.
41440 **
41441 ** On success, this routine obtains a read lock on
41442 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41443 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41444 ** that means the Wal does not hold any read lock. The reader must not
@@ -41383,10 +41445,12 @@
41445 ** access any database page that is modified by a WAL frame up to and
41446 ** including frame number aReadMark[pWal->readLock]. The reader will
41447 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41448 ** Or if pWal->readLock==0, then the reader will ignore the WAL
41449 ** completely and get all content directly from the database file.
41450 ** If the useWal parameter is 1 then the WAL will never be ignored and
41451 ** this routine will always set pWal->readLock>0 on success.
41452 ** When the read transaction is completed, the caller must release the
41453 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41454 **
41455 ** This routine uses the nBackfill and aReadMark[] fields of the header
41456 ** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41427,13 +41491,13 @@
41491 rc = WAL_RETRY;
41492 }else if( rc==SQLITE_BUSY ){
41493 rc = SQLITE_BUSY_RECOVERY;
41494 }
41495 }
41496 if( rc!=SQLITE_OK ){
41497 return rc;
41498 }
41499 }
41500
41501 pInfo = walCkptInfo(pWal);
41502 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41503 /* The WAL has been completely backfilled (or it is empty).
@@ -41605,13 +41669,13 @@
41669 /* This routine is only be called from within a read transaction. */
41670 assert( pWal->readLock>=0 || pWal->lockError );
41671
41672 /* If the "last page" field of the wal-index header snapshot is 0, then
41673 ** no data will be read from the wal under any circumstances. Return early
41674 ** in this case as an optimization. Likewise, if pWal->readLock==0,
41675 ** then the WAL is ignored by the reader so return early, as if the
41676 ** WAL were empty.
41677 */
41678 if( iLast==0 || pWal->readLock==0 ){
41679 *pInWal = 0;
41680 return SQLITE_OK;
41681 }
@@ -41618,11 +41682,11 @@
41682
41683 /* Search the hash table or tables for an entry matching page number
41684 ** pgno. Each iteration of the following for() loop searches one
41685 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41686 **
41687 ** This code might run concurrently to the code in walIndexAppend()
41688 ** that adds entries to the wal-index (and possibly to this hash
41689 ** table). This means the value just read from the hash
41690 ** slot (aHash[iKey]) may have been added before or after the
41691 ** current read transaction was opened. Values added after the
41692 ** read transaction was opened may have been written incorrectly -
@@ -41941,24 +42005,32 @@
42005 ** header to the start of the WAL file. See comments at the top of
42006 ** this source file for a description of the WAL header format.
42007 */
42008 iFrame = pWal->hdr.mxFrame;
42009 if( iFrame==0 ){
42010 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
42011 u32 aCksum[2]; /* Checksum for wal-header */
42012
42013 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
42014 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
42015 sqlite3Put4byte(&aWalHdr[8], szPage);
 
 
42016 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
42017 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
42018 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
42019 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
42020 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
42021
42022 pWal->szPage = szPage;
42023 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
42024 pWal->hdr.aFrameCksum[0] = aCksum[0];
42025 pWal->hdr.aFrameCksum[1] = aCksum[1];
42026
42027 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
42028 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
42029 if( rc!=SQLITE_OK ){
42030 return rc;
42031 }
 
42032 }
42033 assert( pWal->szPage==szPage );
42034
42035 /* Write the log file. */
42036 for(p=pList; p; p=p->pDirty){
@@ -103943,21 +104015,26 @@
104015 ** a low-level error is first detected.
104016 */
104017 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
104018 testcase( sqlite3GlobalConfig.xLog!=0 );
104019 sqlite3_log(SQLITE_CORRUPT,
104020 "database corruption at line %d of [%.10s]",
104021 lineno, 20+sqlite3_sourceid());
104022 return SQLITE_CORRUPT;
104023 }
104024 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
104025 testcase( sqlite3GlobalConfig.xLog!=0 );
104026 sqlite3_log(SQLITE_MISUSE,
104027 "misuse at line %d of [%.10s]",
104028 lineno, 20+sqlite3_sourceid());
104029 return SQLITE_MISUSE;
104030 }
104031 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
104032 testcase( sqlite3GlobalConfig.xLog!=0 );
104033 sqlite3_log(SQLITE_CANTOPEN,
104034 "cannot open file at line %d of [%.10s]",
104035 lineno, 20+sqlite3_sourceid());
104036 return SQLITE_CANTOPEN;
104037 }
104038
104039
104040 #ifndef SQLITE_OMIT_DEPRECATED
104041
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.0"
111111
#define SQLITE_VERSION_NUMBER 3007000
112
-#define SQLITE_SOURCE_ID "2010-06-23 15:18:12 51ef43b9f7db8fabf73d9c8a76dae6c275e50d58"
112
+#define SQLITE_SOURCE_ID "2010-06-24 10:50:18 7aac9ad6dd14b1c56eb8e4750ac769c6197c30bd"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.0"
111 #define SQLITE_VERSION_NUMBER 3007000
112 #define SQLITE_SOURCE_ID "2010-06-23 15:18:12 51ef43b9f7db8fabf73d9c8a76dae6c275e50d58"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.0"
111 #define SQLITE_VERSION_NUMBER 3007000
112 #define SQLITE_SOURCE_ID "2010-06-24 10:50:18 7aac9ad6dd14b1c56eb8e4750ac769c6197c30bd"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button