Fossil SCM

Update the internal SQLite to a 3.7.1 release candidate.

drh 2010-08-18 02:22 trunk
Commit 07afdea6e2c3c976e68fea5ae146fb938942ee15
2 files changed +1852 -1206 +23 -14
+1852 -1206
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -213,23 +213,22 @@
213213
*/
214214
#ifndef SQLITE_MAX_VARIABLE_NUMBER
215215
# define SQLITE_MAX_VARIABLE_NUMBER 999
216216
#endif
217217
218
-/* Maximum page size. The upper bound on this value is 32768. This a limit
219
-** imposed by the necessity of storing the value in a 2-byte unsigned integer
220
-** and the fact that the page size must be a power of 2.
218
+/* Maximum page size. The upper bound on this value is 65536. This a limit
219
+** imposed by the use of 16-bit offsets within each page.
221220
**
222221
** If this limit is changed, then the compiled library is technically
223222
** incompatible with an SQLite library compiled with a different limit. If
224223
** a process operating on a database with a page-size of 65536 bytes
225224
** crashes, then an instance of SQLite compiled with the default page-size
226225
** limit will not be able to rollback the aborted transaction. This could
227226
** lead to database corruption.
228227
*/
229228
#ifndef SQLITE_MAX_PAGE_SIZE
230
-# define SQLITE_MAX_PAGE_SIZE 32768
229
+# define SQLITE_MAX_PAGE_SIZE 65536
231230
#endif
232231
233232
234233
/*
235234
** The default size of a database page.
@@ -631,11 +630,11 @@
631630
** be held constant and Z will be incremented or else Y will be incremented
632631
** and Z will be reset to zero.
633632
**
634633
** Since version 3.6.18, SQLite source code has been stored in the
635634
** <a href="http://www.fossil-scm.org/">Fossil configuration management
636
-** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to
635
+** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
637636
** a string which identifies a particular check-in of SQLite
638637
** within its configuration management system. ^The SQLITE_SOURCE_ID
639638
** string contains the date and time of the check-in (UTC) and an SHA1
640639
** hash of the entire source tree.
641640
**
@@ -643,11 +642,11 @@
643642
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
644643
** [sqlite_version()] and [sqlite_source_id()].
645644
*/
646645
#define SQLITE_VERSION "3.7.1"
647646
#define SQLITE_VERSION_NUMBER 3007001
648
-#define SQLITE_SOURCE_ID "2010-08-05 03:21:40 fbe70e1106bcc5086ceb9d8f39cc39baf3643092"
647
+#define SQLITE_SOURCE_ID "2010-08-17 19:49:14 acb171d4cfef2fec8833f761019f5c81f0d138a0"
649648
650649
/*
651650
** CAPI3REF: Run-Time Library Version Numbers
652651
** KEYWORDS: sqlite3_version, sqlite3_sourceid
653652
**
@@ -688,19 +687,19 @@
688687
** ^The sqlite3_compileoption_used() function returns 0 or 1
689688
** indicating whether the specified option was defined at
690689
** compile time. ^The SQLITE_ prefix may be omitted from the
691690
** option name passed to sqlite3_compileoption_used().
692691
**
693
-** ^The sqlite3_compileoption_get() function allows interating
692
+** ^The sqlite3_compileoption_get() function allows iterating
694693
** over the list of options that were defined at compile time by
695694
** returning the N-th compile time option string. ^If N is out of range,
696695
** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
697696
** prefix is omitted from any strings returned by
698697
** sqlite3_compileoption_get().
699698
**
700699
** ^Support for the diagnostic functions sqlite3_compileoption_used()
701
-** and sqlite3_compileoption_get() may be omitted by specifing the
700
+** and sqlite3_compileoption_get() may be omitted by specifying the
702701
** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
703702
**
704703
** See also: SQL functions [sqlite_compileoption_used()] and
705704
** [sqlite_compileoption_get()] and the [compile_options pragma].
706705
*/
@@ -802,11 +801,11 @@
802801
/*
803802
** CAPI3REF: Closing A Database Connection
804803
**
805804
** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
806805
** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
807
-** successfullly destroyed and all associated resources are deallocated.
806
+** successfully destroyed and all associated resources are deallocated.
808807
**
809808
** Applications must [sqlite3_finalize | finalize] all [prepared statements]
810809
** and [sqlite3_blob_close | close] all [BLOB handles] associated with
811810
** the [sqlite3] object prior to attempting to close the object. ^If
812811
** sqlite3_close() is called on a [database connection] that still has
@@ -1229,16 +1228,25 @@
12291228
** layer a hint of how large the database file will grow to be during the
12301229
** current transaction. This hint is not guaranteed to be accurate but it
12311230
** is often close. The underlying VFS might choose to preallocate database
12321231
** file space based on this hint in order to help writes to the database
12331232
** file run faster.
1233
+**
1234
+** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1235
+** extends and truncates the database file in chunks of a size specified
1236
+** by the user. The fourth argument to [sqlite3_file_control()] should
1237
+** point to an integer (type int) containing the new chunk-size to use
1238
+** for the nominated database. Allocating database file space in large
1239
+** chunks (say 1MB at a time), may reduce file-system fragmentation and
1240
+** improve performance on some systems.
12341241
*/
12351242
#define SQLITE_FCNTL_LOCKSTATE 1
12361243
#define SQLITE_GET_LOCKPROXYFILE 2
12371244
#define SQLITE_SET_LOCKPROXYFILE 3
12381245
#define SQLITE_LAST_ERRNO 4
12391246
#define SQLITE_FCNTL_SIZE_HINT 5
1247
+#define SQLITE_FCNTL_CHUNK_SIZE 6
12401248
12411249
/*
12421250
** CAPI3REF: Mutex Handle
12431251
**
12441252
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3197,11 +3205,11 @@
31973205
** <li> @VVV
31983206
** <li> $VVV
31993207
** </ul>
32003208
**
32013209
** In the templates above, NNN represents an integer literal,
3202
-** and VVV represents an alphanumeric identifer.)^ ^The values of these
3210
+** and VVV represents an alphanumeric identifier.)^ ^The values of these
32033211
** parameters (also called "host parameter names" or "SQL parameters")
32043212
** can be set using the sqlite3_bind_*() routines defined here.
32053213
**
32063214
** ^The first argument to the sqlite3_bind_*() routines is always
32073215
** a pointer to the [sqlite3_stmt] object returned from
@@ -3976,11 +3984,11 @@
39763984
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
39773985
39783986
/*
39793987
** CAPI3REF: Obtain Aggregate Function Context
39803988
**
3981
-** Implementions of aggregate SQL functions use this
3989
+** Implementations of aggregate SQL functions use this
39823990
** routine to allocate memory for storing their state.
39833991
**
39843992
** ^The first time the sqlite3_aggregate_context(C,N) routine is called
39853993
** for a particular aggregate function, SQLite
39863994
** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -4248,11 +4256,11 @@
42484256
** the routine expects pointers to 16-bit word aligned strings
42494257
** of UTF-16 in the native byte order.
42504258
**
42514259
** A pointer to the user supplied routine must be passed as the fifth
42524260
** argument. ^If it is NULL, this is the same as deleting the collation
4253
-** sequence (so that SQLite cannot call it anymore).
4261
+** sequence (so that SQLite cannot call it any more).
42544262
** ^Each time the application supplied function is invoked, it is passed
42554263
** as its first parameter a copy of the void* passed as the fourth argument
42564264
** to sqlite3_create_collation() or sqlite3_create_collation16().
42574265
**
42584266
** ^The remaining arguments to the application-supplied routine are two strings,
@@ -5466,11 +5474,11 @@
54665474
** of passing a NULL pointer instead of a valid mutex handle are undefined
54675475
** (i.e. it is acceptable to provide an implementation that segfaults if
54685476
** it is passed a NULL pointer).
54695477
**
54705478
** The xMutexInit() method must be threadsafe. ^It must be harmless to
5471
-** invoke xMutexInit() mutiple times within the same process and without
5479
+** invoke xMutexInit() multiple times within the same process and without
54725480
** intervening calls to xMutexEnd(). Second and subsequent calls to
54735481
** xMutexInit() must be no-ops.
54745482
**
54755483
** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
54765484
** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5636,11 +5644,11 @@
56365644
56375645
/*
56385646
** CAPI3REF: SQLite Runtime Status
56395647
**
56405648
** ^This interface is used to retrieve runtime status information
5641
-** about the preformance of SQLite, and optionally to reset various
5649
+** about the performance of SQLite, and optionally to reset various
56425650
** highwater marks. ^The first argument is an integer code for
56435651
** the specific parameter to measure. ^(Recognized integer codes
56445652
** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
56455653
** ^The current value of the parameter is returned into *pCurrent.
56465654
** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5762,11 +5770,11 @@
57625770
** ^This interface is used to retrieve runtime status information
57635771
** about a single [database connection]. ^The first argument is the
57645772
** database connection object to be interrogated. ^The second argument
57655773
** is an integer constant, taken from the set of
57665774
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5767
-** determiness the parameter to interrogate. The set of
5775
+** determines the parameter to interrogate. The set of
57685776
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
57695777
** to grow in future releases of SQLite.
57705778
**
57715779
** ^The current value of the requested parameter is written into *pCur
57725780
** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -6184,11 +6192,11 @@
61846192
**
61856193
** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
61866194
**
61876195
** ^Each call to sqlite3_backup_step() sets two values inside
61886196
** the [sqlite3_backup] object: the number of pages still to be backed
6189
-** up and the total number of pages in the source databae file.
6197
+** up and the total number of pages in the source database file.
61906198
** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
61916199
** retrieve these two values, respectively.
61926200
**
61936201
** ^The values returned by these functions are only updated by
61946202
** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -6280,11 +6288,11 @@
62806288
** ^(There may be at most one unlock-notify callback registered by a
62816289
** blocked connection. If sqlite3_unlock_notify() is called when the
62826290
** blocked connection already has a registered unlock-notify callback,
62836291
** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
62846292
** called with a NULL pointer as its second argument, then any existing
6285
-** unlock-notify callback is cancelled. ^The blocked connections
6293
+** unlock-notify callback is canceled. ^The blocked connections
62866294
** unlock-notify callback may also be canceled by closing the blocked
62876295
** connection using [sqlite3_close()].
62886296
**
62896297
** The unlock-notify callback is not reentrant. If an application invokes
62906298
** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -6362,11 +6370,11 @@
63626370
/*
63636371
** CAPI3REF: String Comparison
63646372
**
63656373
** ^The [sqlite3_strnicmp()] API allows applications and extensions to
63666374
** compare the contents of two buffers containing UTF-8 strings in a
6367
-** case-indendent fashion, using the same definition of case independence
6375
+** case-independent fashion, using the same definition of case independence
63686376
** that SQLite uses internally when comparing identifiers.
63696377
*/
63706378
SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
63716379
63726380
/*
@@ -7868,11 +7876,11 @@
78687876
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
78697877
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
78707878
78717879
/* Functions used to configure a Pager object. */
78727880
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
7873
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
7881
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
78747882
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
78757883
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
78767884
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
78777885
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
78787886
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
@@ -7895,11 +7903,11 @@
78957903
SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
78967904
SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
78977905
SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
78987906
78997907
/* Functions used to manage pager transactions and savepoints. */
7900
-SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*);
7908
+SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
79017909
SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
79027910
SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
79037911
SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
79047912
SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
79057913
SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
@@ -9183,13 +9191,13 @@
91839191
** argument to sqlite3VdbeKeyCompare and is used to control the
91849192
** comparison of the two index keys.
91859193
*/
91869194
struct KeyInfo {
91879195
sqlite3 *db; /* The database connection */
9188
- u8 enc; /* Text encoding - one of the TEXT_Utf* values */
9196
+ u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
91899197
u16 nField; /* Number of entries in aColl[] */
9190
- u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */
9198
+ u8 *aSortOrder; /* Sort order for each column. May be NULL */
91919199
CollSeq *aColl[1]; /* Collating sequence for each term of the key */
91929200
};
91939201
91949202
/*
91959203
** An instance of the following structure holds information about a
@@ -16624,10 +16632,11 @@
1662416632
#else
1662516633
/* Use the built-in recursive mutexes if they are available.
1662616634
*/
1662716635
pthread_mutex_lock(&p->mutex);
1662816636
#if SQLITE_MUTEX_NREF
16637
+ assert( p->nRef>0 || p->owner==0 );
1662916638
p->owner = pthread_self();
1663016639
p->nRef++;
1663116640
#endif
1663216641
#endif
1663316642
@@ -16696,10 +16705,11 @@
1669616705
*/
1669716706
static void pthreadMutexLeave(sqlite3_mutex *p){
1669816707
assert( pthreadMutexHeld(p) );
1669916708
#if SQLITE_MUTEX_NREF
1670016709
p->nRef--;
16710
+ if( p->nRef==0 ) p->owner = 0;
1670116711
#endif
1670216712
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1670316713
1670416714
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
1670516715
if( p->nRef==0 ){
@@ -16960,11 +16970,11 @@
1696016970
** allocated mutex. SQLite is careful to deallocate every
1696116971
** mutex that it allocates.
1696216972
*/
1696316973
static void winMutexFree(sqlite3_mutex *p){
1696416974
assert( p );
16965
- assert( p->nRef==0 );
16975
+ assert( p->nRef==0 && p->owner==0 );
1696616976
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
1696716977
DeleteCriticalSection(&p->mutex);
1696816978
sqlite3_free(p);
1696916979
}
1697016980
@@ -16984,10 +16994,11 @@
1698416994
DWORD tid = GetCurrentThreadId();
1698516995
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
1698616996
#endif
1698716997
EnterCriticalSection(&p->mutex);
1698816998
#ifdef SQLITE_DEBUG
16999
+ assert( p->nRef>0 || p->owner==0 );
1698917000
p->owner = tid;
1699017001
p->nRef++;
1699117002
if( p->trace ){
1699217003
printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
1699317004
}
@@ -17037,10 +17048,11 @@
1703717048
#ifndef NDEBUG
1703817049
DWORD tid = GetCurrentThreadId();
1703917050
assert( p->nRef>0 );
1704017051
assert( p->owner==tid );
1704117052
p->nRef--;
17053
+ if( p->nRef==0 ) p->owner = 0;
1704217054
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1704317055
#endif
1704417056
LeaveCriticalSection(&p->mutex);
1704517057
#ifdef SQLITE_DEBUG
1704617058
if( p->trace ){
@@ -22609,10 +22621,11 @@
2260922621
void *lockingContext; /* Locking style specific state */
2261022622
UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
2261122623
int fileFlags; /* Miscellanous flags */
2261222624
const char *zPath; /* Name of the file */
2261322625
unixShm *pShm; /* Shared memory segment information */
22626
+ int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
2261422627
#if SQLITE_ENABLE_LOCKING_STYLE
2261522628
int openFlags; /* The flags specified at open() */
2261622629
#endif
2261722630
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
2261822631
unsigned fsFlags; /* cached details from statfs() */
@@ -25367,19 +25380,21 @@
2536725380
offset += wrote;
2536825381
pBuf = &((char*)pBuf)[wrote];
2536925382
}
2537025383
SimulateIOError(( wrote=(-1), amt=1 ));
2537125384
SimulateDiskfullError(( wrote=0, amt=1 ));
25385
+
2537225386
if( amt>0 ){
2537325387
if( wrote<0 ){
2537425388
/* lastErrno set by seekAndWrite */
2537525389
return SQLITE_IOERR_WRITE;
2537625390
}else{
2537725391
pFile->lastErrno = 0; /* not a system error */
2537825392
return SQLITE_FULL;
2537925393
}
2538025394
}
25395
+
2538125396
return SQLITE_OK;
2538225397
}
2538325398
2538425399
#ifdef SQLITE_TEST
2538525400
/*
@@ -25577,16 +25592,27 @@
2557725592
2557825593
/*
2557925594
** Truncate an open file to a specified size
2558025595
*/
2558125596
static int unixTruncate(sqlite3_file *id, i64 nByte){
25597
+ unixFile *pFile = (unixFile *)id;
2558225598
int rc;
25583
- assert( id );
25599
+ assert( pFile );
2558425600
SimulateIOError( return SQLITE_IOERR_TRUNCATE );
25585
- rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
25601
+
25602
+ /* If the user has configured a chunk-size for this file, truncate the
25603
+ ** file so that it consists of an integer number of chunks (i.e. the
25604
+ ** actual file size after the operation may be larger than the requested
25605
+ ** size).
25606
+ */
25607
+ if( pFile->szChunk ){
25608
+ nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
25609
+ }
25610
+
25611
+ rc = ftruncate(pFile->h, (off_t)nByte);
2558625612
if( rc ){
25587
- ((unixFile*)id)->lastErrno = errno;
25613
+ pFile->lastErrno = errno;
2558825614
return SQLITE_IOERR_TRUNCATE;
2558925615
}else{
2559025616
#ifndef NDEBUG
2559125617
/* If we are doing a normal write to a database file (as opposed to
2559225618
** doing a hot-journal rollback or a write to some file other than a
@@ -25593,12 +25619,12 @@
2559325619
** normal database file) and we truncate the file to zero length,
2559425620
** that effectively updates the change counter. This might happen
2559525621
** when restoring a database using the backup API from a zero-length
2559625622
** source.
2559725623
*/
25598
- if( ((unixFile*)id)->inNormalWrite && nByte==0 ){
25599
- ((unixFile*)id)->transCntrChng = 1;
25624
+ if( pFile->inNormalWrite && nByte==0 ){
25625
+ pFile->transCntrChng = 1;
2560025626
}
2560125627
#endif
2560225628
2560325629
return SQLITE_OK;
2560425630
}
@@ -25637,10 +25663,58 @@
2563725663
** proxying locking division.
2563825664
*/
2563925665
static int proxyFileControl(sqlite3_file*,int,void*);
2564025666
#endif
2564125667
25668
+/*
25669
+** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
25670
+** file-control operation.
25671
+**
25672
+** If the user has configured a chunk-size for this file, it could be
25673
+** that the file needs to be extended at this point. Otherwise, the
25674
+** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
25675
+*/
25676
+static int fcntlSizeHint(unixFile *pFile, i64 nByte){
25677
+ if( pFile->szChunk ){
25678
+ i64 nSize; /* Required file size */
25679
+ struct stat buf; /* Used to hold return values of fstat() */
25680
+
25681
+ if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
25682
+
25683
+ nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
25684
+ if( nSize>(i64)buf.st_size ){
25685
+#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
25686
+ if( posix_fallocate(pFile->h, buf.st_size, nSize-buf.st_size) ){
25687
+ return SQLITE_IOERR_WRITE;
25688
+ }
25689
+#else
25690
+ /* If the OS does not have posix_fallocate(), fake it. First use
25691
+ ** ftruncate() to set the file size, then write a single byte to
25692
+ ** the last byte in each block within the extended region. This
25693
+ ** is the same technique used by glibc to implement posix_fallocate()
25694
+ ** on systems that do not have a real fallocate() system call.
25695
+ */
25696
+ int nBlk = buf.st_blksize; /* File-system block size */
25697
+ i64 iWrite; /* Next offset to write to */
25698
+ int nWrite; /* Return value from seekAndWrite() */
25699
+
25700
+ if( ftruncate(pFile->h, nSize) ){
25701
+ pFile->lastErrno = errno;
25702
+ return SQLITE_IOERR_TRUNCATE;
25703
+ }
25704
+ iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
25705
+ do {
25706
+ nWrite = seekAndWrite(pFile, iWrite, "", 1);
25707
+ iWrite += nBlk;
25708
+ } while( nWrite==1 && iWrite<nSize );
25709
+ if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
25710
+#endif
25711
+ }
25712
+ }
25713
+
25714
+ return SQLITE_OK;
25715
+}
2564225716
2564325717
/*
2564425718
** Information and control of an open file handle.
2564525719
*/
2564625720
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -25650,18 +25724,17 @@
2565025724
return SQLITE_OK;
2565125725
}
2565225726
case SQLITE_LAST_ERRNO: {
2565325727
*(int*)pArg = ((unixFile*)id)->lastErrno;
2565425728
return SQLITE_OK;
25729
+ }
25730
+ case SQLITE_FCNTL_CHUNK_SIZE: {
25731
+ ((unixFile*)id)->szChunk = *(int *)pArg;
25732
+ return SQLITE_OK;
2565525733
}
2565625734
case SQLITE_FCNTL_SIZE_HINT: {
25657
-#if 0 /* No performance advantage seen on Linux */
25658
- sqlite3_int64 szFile = *(sqlite3_int64*)pArg;
25659
- unixFile *pFile = (unixFile*)id;
25660
- ftruncate(pFile->h, szFile);
25661
-#endif
25662
- return SQLITE_OK;
25735
+ return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
2566325736
}
2566425737
#ifndef NDEBUG
2566525738
/* The pager calls this method to signal that it has done
2566625739
** a rollback and that the database is therefore unchanged and
2566725740
** it hence it is OK for the transaction change counter to be
@@ -28522,11 +28595,11 @@
2852228595
}else{
2852328596
if( pCtx->conchFile ){
2852428597
pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
2852528598
sqlite3_free(pCtx->conchFile);
2852628599
}
28527
- sqlite3_free(pCtx->lockProxyPath);
28600
+ sqlite3DbFree(0, pCtx->lockProxyPath);
2852828601
sqlite3_free(pCtx->conchFilePath);
2852928602
sqlite3_free(pCtx);
2853028603
}
2853128604
OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
2853228605
(rc==SQLITE_OK ? "ok" : "failed")));
@@ -28713,13 +28786,13 @@
2871328786
}
2871428787
rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
2871528788
if( rc ) return rc;
2871628789
sqlite3_free(conchFile);
2871728790
}
28718
- sqlite3_free(pCtx->lockProxyPath);
28791
+ sqlite3DbFree(0, pCtx->lockProxyPath);
2871928792
sqlite3_free(pCtx->conchFilePath);
28720
- sqlite3_free(pCtx->dbPath);
28793
+ sqlite3DbFree(0, pCtx->dbPath);
2872128794
/* restore the original locking context and pMethod then close it */
2872228795
pFile->lockingContext = pCtx->oldLockingContext;
2872328796
pFile->pMethod = pCtx->pOldMethod;
2872428797
sqlite3_free(pCtx);
2872528798
return pFile->pMethod->xClose(id);
@@ -29161,10 +29234,11 @@
2916129234
short sharedLockByte; /* Randomly chosen byte used as a shared lock */
2916229235
DWORD lastErrno; /* The Windows errno from the last I/O error */
2916329236
DWORD sectorSize; /* Sector size of the device file is on */
2916429237
winShm *pShm; /* Instance of shared memory on this file */
2916529238
const char *zPath; /* Full pathname of this file */
29239
+ int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
2916629240
#if SQLITE_OS_WINCE
2916729241
WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
2916829242
HANDLE hMutex; /* Mutex used to control access to shared lock */
2916929243
HANDLE hShared; /* Shared memory segment used for locking */
2917029244
winceLock local; /* Locks obtained by this instance of winFile */
@@ -29671,10 +29745,46 @@
2967129745
2967229746
/*****************************************************************************
2967329747
** The next group of routines implement the I/O methods specified
2967429748
** by the sqlite3_io_methods object.
2967529749
******************************************************************************/
29750
+
29751
+/*
29752
+** Some microsoft compilers lack this definition.
29753
+*/
29754
+#ifndef INVALID_SET_FILE_POINTER
29755
+# define INVALID_SET_FILE_POINTER ((DWORD)-1)
29756
+#endif
29757
+
29758
+/*
29759
+** Move the current position of the file handle passed as the first
29760
+** argument to offset iOffset within the file. If successful, return 0.
29761
+** Otherwise, set pFile->lastErrno and return non-zero.
29762
+*/
29763
+static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
29764
+ LONG upperBits; /* Most sig. 32 bits of new offset */
29765
+ LONG lowerBits; /* Least sig. 32 bits of new offset */
29766
+ DWORD dwRet; /* Value returned by SetFilePointer() */
29767
+
29768
+ upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
29769
+ lowerBits = (LONG)(iOffset & 0xffffffff);
29770
+
29771
+ /* API oddity: If successful, SetFilePointer() returns a dword
29772
+ ** containing the lower 32-bits of the new file-offset. Or, if it fails,
29773
+ ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
29774
+ ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
29775
+ ** whether an error has actually occured, it is also necessary to call
29776
+ ** GetLastError().
29777
+ */
29778
+ dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29779
+ if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
29780
+ pFile->lastErrno = GetLastError();
29781
+ return 1;
29782
+ }
29783
+
29784
+ return 0;
29785
+}
2967629786
2967729787
/*
2967829788
** Close a file.
2967929789
**
2968029790
** It is reported that an attempt to close a handle might sometimes
@@ -29714,17 +29824,10 @@
2971429824
OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
2971529825
OpenCounter(-1);
2971629826
return rc ? SQLITE_OK : SQLITE_IOERR;
2971729827
}
2971829828
29719
-/*
29720
-** Some microsoft compilers lack this definition.
29721
-*/
29722
-#ifndef INVALID_SET_FILE_POINTER
29723
-# define INVALID_SET_FILE_POINTER ((DWORD)-1)
29724
-#endif
29725
-
2972629829
/*
2972729830
** Read data from a file into a buffer. Return SQLITE_OK if all
2972829831
** bytes were read successfully and SQLITE_IOERR if anything goes
2972929832
** wrong.
2973029833
*/
@@ -29732,112 +29835,108 @@
2973229835
sqlite3_file *id, /* File to read from */
2973329836
void *pBuf, /* Write content into this buffer */
2973429837
int amt, /* Number of bytes to read */
2973529838
sqlite3_int64 offset /* Begin reading at this offset */
2973629839
){
29737
- LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
29738
- LONG lowerBits = (LONG)(offset & 0xffffffff);
29739
- DWORD rc;
29740
- winFile *pFile = (winFile*)id;
29741
- DWORD error;
29742
- DWORD got;
29840
+ winFile *pFile = (winFile*)id; /* file handle */
29841
+ DWORD nRead; /* Number of bytes actually read from file */
2974329842
2974429843
assert( id!=0 );
2974529844
SimulateIOError(return SQLITE_IOERR_READ);
2974629845
OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
29747
- rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29748
- if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29749
- pFile->lastErrno = error;
29846
+
29847
+ if( seekWinFile(pFile, offset) ){
2975029848
return SQLITE_FULL;
2975129849
}
29752
- if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){
29850
+ if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
2975329851
pFile->lastErrno = GetLastError();
2975429852
return SQLITE_IOERR_READ;
2975529853
}
29756
- if( got==(DWORD)amt ){
29757
- return SQLITE_OK;
29758
- }else{
29854
+ if( nRead<(DWORD)amt ){
2975929855
/* Unread parts of the buffer must be zero-filled */
29760
- memset(&((char*)pBuf)[got], 0, amt-got);
29856
+ memset(&((char*)pBuf)[nRead], 0, amt-nRead);
2976129857
return SQLITE_IOERR_SHORT_READ;
2976229858
}
29859
+
29860
+ return SQLITE_OK;
2976329861
}
2976429862
2976529863
/*
2976629864
** Write data from a buffer into a file. Return SQLITE_OK on success
2976729865
** or some other error code on failure.
2976829866
*/
2976929867
static int winWrite(
29770
- sqlite3_file *id, /* File to write into */
29771
- const void *pBuf, /* The bytes to be written */
29772
- int amt, /* Number of bytes to write */
29773
- sqlite3_int64 offset /* Offset into the file to begin writing at */
29868
+ sqlite3_file *id, /* File to write into */
29869
+ const void *pBuf, /* The bytes to be written */
29870
+ int amt, /* Number of bytes to write */
29871
+ sqlite3_int64 offset /* Offset into the file to begin writing at */
2977429872
){
29775
- LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
29776
- LONG lowerBits = (LONG)(offset & 0xffffffff);
29777
- DWORD rc;
29778
- winFile *pFile = (winFile*)id;
29779
- DWORD error;
29780
- DWORD wrote = 0;
29781
-
29782
- assert( id!=0 );
29873
+ int rc; /* True if error has occured, else false */
29874
+ winFile *pFile = (winFile*)id; /* File handle */
29875
+
29876
+ assert( amt>0 );
29877
+ assert( pFile );
2978329878
SimulateIOError(return SQLITE_IOERR_WRITE);
2978429879
SimulateDiskfullError(return SQLITE_FULL);
29785
- OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
29786
- rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29787
- if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29788
- pFile->lastErrno = error;
29789
- if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29790
- return SQLITE_FULL;
29791
- }else{
29792
- return SQLITE_IOERR_WRITE;
29793
- }
29794
- }
29795
- assert( amt>0 );
29796
- while(
29797
- amt>0
29798
- && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0
29799
- && wrote>0
29800
- ){
29801
- amt -= wrote;
29802
- pBuf = &((char*)pBuf)[wrote];
29803
- }
29804
- if( !rc || amt>(int)wrote ){
29805
- pFile->lastErrno = GetLastError();
29806
- if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29807
- return SQLITE_FULL;
29808
- }else{
29809
- return SQLITE_IOERR_WRITE;
29810
- }
29880
+
29881
+ OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
29882
+
29883
+ rc = seekWinFile(pFile, offset);
29884
+ if( rc==0 ){
29885
+ u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
29886
+ int nRem = amt; /* Number of bytes yet to be written */
29887
+ DWORD nWrite; /* Bytes written by each WriteFile() call */
29888
+
29889
+ while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){
29890
+ aRem += nWrite;
29891
+ nRem -= nWrite;
29892
+ }
29893
+ if( nRem>0 ){
29894
+ pFile->lastErrno = GetLastError();
29895
+ rc = 1;
29896
+ }
29897
+ }
29898
+
29899
+ if( rc ){
29900
+ if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29901
+ return SQLITE_FULL;
29902
+ }
29903
+ return SQLITE_IOERR_WRITE;
2981129904
}
2981229905
return SQLITE_OK;
2981329906
}
2981429907
2981529908
/*
2981629909
** Truncate an open file to a specified size
2981729910
*/
2981829911
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
29819
- LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
29820
- LONG lowerBits = (LONG)(nByte & 0xffffffff);
29821
- DWORD dwRet;
29822
- winFile *pFile = (winFile*)id;
29823
- DWORD error;
29824
- int rc = SQLITE_OK;
29825
-
29826
- assert( id!=0 );
29912
+ winFile *pFile = (winFile*)id; /* File handle object */
29913
+ int rc = SQLITE_OK; /* Return code for this function */
29914
+
29915
+ assert( pFile );
29916
+
2982729917
OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
2982829918
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
29829
- dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29830
- if( dwRet==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29831
- pFile->lastErrno = error;
29919
+
29920
+ /* If the user has configured a chunk-size for this file, truncate the
29921
+ ** file so that it consists of an integer number of chunks (i.e. the
29922
+ ** actual file size after the operation may be larger than the requested
29923
+ ** size).
29924
+ */
29925
+ if( pFile->szChunk ){
29926
+ nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
29927
+ }
29928
+
29929
+ /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
29930
+ if( seekWinFile(pFile, nByte) ){
2983229931
rc = SQLITE_IOERR_TRUNCATE;
29833
- /* SetEndOfFile will fail if nByte is negative */
29834
- }else if( !SetEndOfFile(pFile->h) ){
29932
+ }else if( 0==SetEndOfFile(pFile->h) ){
2983529933
pFile->lastErrno = GetLastError();
2983629934
rc = SQLITE_IOERR_TRUNCATE;
2983729935
}
29838
- OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc==SQLITE_OK ? "ok" : "failed"));
29936
+
29937
+ OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
2983929938
return rc;
2984029939
}
2984129940
2984229941
#ifdef SQLITE_TEST
2984329942
/*
@@ -30197,10 +30296,14 @@
3019730296
return SQLITE_OK;
3019830297
}
3019930298
case SQLITE_LAST_ERRNO: {
3020030299
*(int*)pArg = (int)((winFile*)id)->lastErrno;
3020130300
return SQLITE_OK;
30301
+ }
30302
+ case SQLITE_FCNTL_CHUNK_SIZE: {
30303
+ ((winFile*)id)->szChunk = *(int *)pArg;
30304
+ return SQLITE_OK;
3020230305
}
3020330306
case SQLITE_FCNTL_SIZE_HINT: {
3020430307
sqlite3_int64 sz = *(sqlite3_int64*)pArg;
3020530308
SimulateIOErrorBenign(1);
3020630309
winTruncate(id, sz);
@@ -31749,11 +31852,11 @@
3174931852
** start of a transaction, and is thus usually less than a few thousand,
3175031853
** but can be as large as 2 billion for a really big database.
3175131854
*/
3175231855
3175331856
/* Size of the Bitvec structure in bytes. */
31754
-#define BITVEC_SZ (sizeof(void*)*128) /* 512 on 32bit. 1024 on 64bit */
31857
+#define BITVEC_SZ 512
3175531858
3175631859
/* Round the union size down to the nearest pointer boundary, since that's how
3175731860
** it will be aligned within the Bitvec struct. */
3175831861
#define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
3175931862
@@ -33964,11 +34067,11 @@
3396434067
# define sqlite3WalOpen(x,y,z) 0
3396534068
# define sqlite3WalClose(w,x,y,z) 0
3396634069
# define sqlite3WalBeginReadTransaction(y,z) 0
3396734070
# define sqlite3WalEndReadTransaction(z)
3396834071
# define sqlite3WalRead(v,w,x,y,z) 0
33969
-# define sqlite3WalDbsize(y,z)
34072
+# define sqlite3WalDbsize(y) 0
3397034073
# define sqlite3WalBeginWriteTransaction(y) 0
3397134074
# define sqlite3WalEndWriteTransaction(x) 0
3397234075
# define sqlite3WalUndo(x,y,z) 0
3397334076
# define sqlite3WalSavepoint(y,z)
3397434077
# define sqlite3WalSavepointUndo(y,z) 0
@@ -34000,13 +34103,12 @@
3400034103
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
3400134104
3400234105
/* Read a page from the write-ahead log, if it is present. */
3400334106
SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
3400434107
34005
-/* Return the size of the database as it existed at the beginning
34006
-** of the snapshot */
34007
-SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno);
34108
+/* If the WAL is not empty, return the size of the database. */
34109
+SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
3400834110
3400934111
/* Obtain or release the WRITER lock. */
3401034112
SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
3401134113
SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
3401234114
@@ -34048,12 +34150,16 @@
3404834150
#endif /* _WAL_H_ */
3404934151
3405034152
/************** End of wal.h *************************************************/
3405134153
/************** Continuing where we left off in pager.c **********************/
3405234154
34053
-/*
34054
-******************** NOTES ON THE DESIGN OF THE PAGER ************************
34155
+
34156
+/******************* NOTES ON THE DESIGN OF THE PAGER ************************
34157
+**
34158
+** This comment block describes invariants that hold when using a rollback
34159
+** journal. These invariants do not apply for journal_mode=WAL,
34160
+** journal_mode=MEMORY, or journal_mode=OFF.
3405534161
**
3405634162
** Within this comment block, a page is deemed to have been synced
3405734163
** automatically as soon as it is written when PRAGMA synchronous=OFF.
3405834164
** Otherwise, the page is not synced until the xSync method of the VFS
3405934165
** is called successfully on the file containing the page.
@@ -34083,11 +34189,11 @@
3408334189
** both the content in the database when the rollback journal was written
3408434190
** and the content in the database at the beginning of the current
3408534191
** transaction.
3408634192
**
3408734193
** (3) Writes to the database file are an integer multiple of the page size
34088
-** in length and are aligned to a page boundary.
34194
+** in length and are aligned on a page boundary.
3408934195
**
3409034196
** (4) Reads from the database file are either aligned on a page boundary and
3409134197
** an integer multiple of the page size in length or are taken from the
3409234198
** first 100 bytes of the database file.
3409334199
**
@@ -34114,11 +34220,12 @@
3411434220
** method is a no-op, but that does not change the fact the SQLite will
3411534221
** invoke it.)
3411634222
**
3411734223
** (9) Whenever the database file is modified, at least one bit in the range
3411834224
** of bytes from 24 through 39 inclusive will be changed prior to releasing
34119
-** the EXCLUSIVE lock.
34225
+** the EXCLUSIVE lock, thus signaling other connections on the same
34226
+** database to flush their caches.
3412034227
**
3412134228
** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
3412234229
** than one billion transactions.
3412334230
**
3412434231
** (11) A database file is well-formed at the beginning and at the conclusion
@@ -34127,11 +34234,12 @@
3412734234
** (12) An EXCLUSIVE lock is held on the database file when writing to
3412834235
** the database file.
3412934236
**
3413034237
** (13) A SHARED lock is held on the database file while reading any
3413134238
** content out of the database file.
34132
-*/
34239
+**
34240
+******************************************************************************/
3413334241
3413434242
/*
3413534243
** Macros for troubleshooting. Normally turned off
3413634244
*/
3413734245
#if 0
@@ -34152,62 +34260,283 @@
3415234260
*/
3415334261
#define PAGERID(p) ((int)(p->fd))
3415434262
#define FILEHANDLEID(fd) ((int)fd)
3415534263
3415634264
/*
34157
-** The page cache as a whole is always in one of the following
34158
-** states:
34159
-**
34160
-** PAGER_UNLOCK The page cache is not currently reading or
34161
-** writing the database file. There is no
34162
-** data held in memory. This is the initial
34163
-** state.
34164
-**
34165
-** PAGER_SHARED The page cache is reading the database.
34166
-** Writing is not permitted. There can be
34167
-** multiple readers accessing the same database
34168
-** file at the same time.
34169
-**
34170
-** PAGER_RESERVED This process has reserved the database for writing
34171
-** but has not yet made any changes. Only one process
34172
-** at a time can reserve the database. The original
34173
-** database file has not been modified so other
34174
-** processes may still be reading the on-disk
34175
-** database file.
34176
-**
34177
-** PAGER_EXCLUSIVE The page cache is writing the database.
34178
-** Access is exclusive. No other processes or
34179
-** threads can be reading or writing while one
34180
-** process is writing.
34181
-**
34182
-** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
34183
-** after all dirty pages have been written to the
34184
-** database file and the file has been synced to
34185
-** disk. All that remains to do is to remove or
34186
-** truncate the journal file and the transaction
34187
-** will be committed.
34188
-**
34189
-** The page cache comes up in PAGER_UNLOCK. The first time a
34190
-** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
34191
-** After all pages have been released using sqlite_page_unref(),
34192
-** the state transitions back to PAGER_UNLOCK. The first time
34193
-** that sqlite3PagerWrite() is called, the state transitions to
34194
-** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
34195
-** called on an outstanding page which means that the pager must
34196
-** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
34197
-** PAGER_RESERVED means that there is an open rollback journal.
34198
-** The transition to PAGER_EXCLUSIVE occurs before any changes
34199
-** are made to the database file, though writes to the rollback
34200
-** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
34201
-** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
34202
-** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
34203
-*/
34204
-#define PAGER_UNLOCK 0
34205
-#define PAGER_SHARED 1 /* same as SHARED_LOCK */
34206
-#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
34207
-#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
34208
-#define PAGER_SYNCED 5
34265
+** The Pager.eState variable stores the current 'state' of a pager. A
34266
+** pager may be in any one of the seven states shown in the following
34267
+** state diagram.
34268
+**
34269
+** OPEN <------+------+
34270
+** | | |
34271
+** V | |
34272
+** +---------> READER-------+ |
34273
+** | | |
34274
+** | V |
34275
+** |<-------WRITER_LOCKED------> ERROR
34276
+** | | ^
34277
+** | V |
34278
+** |<------WRITER_CACHEMOD-------->|
34279
+** | | |
34280
+** | V |
34281
+** |<-------WRITER_DBMOD---------->|
34282
+** | | |
34283
+** | V |
34284
+** +<------WRITER_FINISHED-------->+
34285
+**
34286
+**
34287
+** List of state transitions and the C [function] that performs each:
34288
+**
34289
+** OPEN -> READER [sqlite3PagerSharedLock]
34290
+** READER -> OPEN [pager_unlock]
34291
+**
34292
+** READER -> WRITER_LOCKED [sqlite3PagerBegin]
34293
+** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
34294
+** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
34295
+** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
34296
+** WRITER_*** -> READER [pager_end_transaction]
34297
+**
34298
+** WRITER_*** -> ERROR [pager_error]
34299
+** ERROR -> OPEN [pager_unlock]
34300
+**
34301
+**
34302
+** OPEN:
34303
+**
34304
+** The pager starts up in this state. Nothing is guaranteed in this
34305
+** state - the file may or may not be locked and the database size is
34306
+** unknown. The database may not be read or written.
34307
+**
34308
+** * No read or write transaction is active.
34309
+** * Any lock, or no lock at all, may be held on the database file.
34310
+** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
34311
+**
34312
+** READER:
34313
+**
34314
+** In this state all the requirements for reading the database in
34315
+** rollback (non-WAL) mode are met. Unless the pager is (or recently
34316
+** was) in exclusive-locking mode, a user-level read transaction is
34317
+** open. The database size is known in this state.
34318
+**
34319
+** A connection running with locking_mode=normal enters this state when
34320
+** it opens a read-transaction on the database and returns to state
34321
+** OPEN after the read-transaction is completed. However a connection
34322
+** running in locking_mode=exclusive (including temp databases) remains in
34323
+** this state even after the read-transaction is closed. The only way
34324
+** a locking_mode=exclusive connection can transition from READER to OPEN
34325
+** is via the ERROR state (see below).
34326
+**
34327
+** * A read transaction may be active (but a write-transaction cannot).
34328
+** * A SHARED or greater lock is held on the database file.
34329
+** * The dbSize variable may be trusted (even if a user-level read
34330
+** transaction is not active). The dbOrigSize and dbFileSize variables
34331
+** may not be trusted at this point.
34332
+** * If the database is a WAL database, then the WAL connection is open.
34333
+** * Even if a read-transaction is not open, it is guaranteed that
34334
+** there is no hot-journal in the file-system.
34335
+**
34336
+** WRITER_LOCKED:
34337
+**
34338
+** The pager moves to this state from READER when a write-transaction
34339
+** is first opened on the database. In WRITER_LOCKED state, all locks
34340
+** required to start a write-transaction are held, but no actual
34341
+** modifications to the cache or database have taken place.
34342
+**
34343
+** In rollback mode, a RESERVED or (if the transaction was opened with
34344
+** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
34345
+** moving to this state, but the journal file is not written to or opened
34346
+** to in this state. If the transaction is committed or rolled back while
34347
+** in WRITER_LOCKED state, all that is required is to unlock the database
34348
+** file.
34349
+**
34350
+** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
34351
+** If the connection is running with locking_mode=exclusive, an attempt
34352
+** is made to obtain an EXCLUSIVE lock on the database file.
34353
+**
34354
+** * A write transaction is active.
34355
+** * If the connection is open in rollback-mode, a RESERVED or greater
34356
+** lock is held on the database file.
34357
+** * If the connection is open in WAL-mode, a WAL write transaction
34358
+** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
34359
+** called).
34360
+** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
34361
+** * The contents of the pager cache have not been modified.
34362
+** * The journal file may or may not be open.
34363
+** * Nothing (not even the first header) has been written to the journal.
34364
+**
34365
+** WRITER_CACHEMOD:
34366
+**
34367
+** A pager moves from WRITER_LOCKED state to this state when a page is
34368
+** first modified by the upper layer. In rollback mode the journal file
34369
+** is opened (if it is not already open) and a header written to the
34370
+** start of it. The database file on disk has not been modified.
34371
+**
34372
+** * A write transaction is active.
34373
+** * A RESERVED or greater lock is held on the database file.
34374
+** * The journal file is open and the first header has been written
34375
+** to it, but the header has not been synced to disk.
34376
+** * The contents of the page cache have been modified.
34377
+**
34378
+** WRITER_DBMOD:
34379
+**
34380
+** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
34381
+** when it modifies the contents of the database file. WAL connections
34382
+** never enter this state (since they do not modify the database file,
34383
+** just the log file).
34384
+**
34385
+** * A write transaction is active.
34386
+** * An EXCLUSIVE or greater lock is held on the database file.
34387
+** * The journal file is open and the first header has been written
34388
+** and synced to disk.
34389
+** * The contents of the page cache have been modified (and possibly
34390
+** written to disk).
34391
+**
34392
+** WRITER_FINISHED:
34393
+**
34394
+** It is not possible for a WAL connection to enter this state.
34395
+**
34396
+** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
34397
+** state after the entire transaction has been successfully written into the
34398
+** database file. In this state the transaction may be committed simply
34399
+** by finalizing the journal file. Once in WRITER_FINISHED state, it is
34400
+** not possible to modify the database further. At this point, the upper
34401
+** layer must either commit or rollback the transaction.
34402
+**
34403
+** * A write transaction is active.
34404
+** * An EXCLUSIVE or greater lock is held on the database file.
34405
+** * All writing and syncing of journal and database data has finished.
34406
+** If no error occured, all that remains is to finalize the journal to
34407
+** commit the transaction. If an error did occur, the caller will need
34408
+** to rollback the transaction.
34409
+**
34410
+** ERROR:
34411
+**
34412
+** The ERROR state is entered when an IO or disk-full error (including
34413
+** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
34414
+** difficult to be sure that the in-memory pager state (cache contents,
34415
+** db size etc.) are consistent with the contents of the file-system.
34416
+**
34417
+** Temporary pager files may enter the ERROR state, but in-memory pagers
34418
+** cannot.
34419
+**
34420
+** For example, if an IO error occurs while performing a rollback,
34421
+** the contents of the page-cache may be left in an inconsistent state.
34422
+** At this point it would be dangerous to change back to READER state
34423
+** (as usually happens after a rollback). Any subsequent readers might
34424
+** report database corruption (due to the inconsistent cache), and if
34425
+** they upgrade to writers, they may inadvertently corrupt the database
34426
+** file. To avoid this hazard, the pager switches into the ERROR state
34427
+** instead of READER following such an error.
34428
+**
34429
+** Once it has entered the ERROR state, any attempt to use the pager
34430
+** to read or write data returns an error. Eventually, once all
34431
+** outstanding transactions have been abandoned, the pager is able to
34432
+** transition back to OPEN state, discarding the contents of the
34433
+** page-cache and any other in-memory state at the same time. Everything
34434
+** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
34435
+** when a read-transaction is next opened on the pager (transitioning
34436
+** the pager into READER state). At that point the system has recovered
34437
+** from the error.
34438
+**
34439
+** Specifically, the pager jumps into the ERROR state if:
34440
+**
34441
+** 1. An error occurs while attempting a rollback. This happens in
34442
+** function sqlite3PagerRollback().
34443
+**
34444
+** 2. An error occurs while attempting to finalize a journal file
34445
+** following a commit in function sqlite3PagerCommitPhaseTwo().
34446
+**
34447
+** 3. An error occurs while attempting to write to the journal or
34448
+** database file in function pagerStress() in order to free up
34449
+** memory.
34450
+**
34451
+** In other cases, the error is returned to the b-tree layer. The b-tree
34452
+** layer then attempts a rollback operation. If the error condition
34453
+** persists, the pager enters the ERROR state via condition (1) above.
34454
+**
34455
+** Condition (3) is necessary because it can be triggered by a read-only
34456
+** statement executed within a transaction. In this case, if the error
34457
+** code were simply returned to the user, the b-tree layer would not
34458
+** automatically attempt a rollback, as it assumes that an error in a
34459
+** read-only statement cannot leave the pager in an internally inconsistent
34460
+** state.
34461
+**
34462
+** * The Pager.errCode variable is set to something other than SQLITE_OK.
34463
+** * There are one or more outstanding references to pages (after the
34464
+** last reference is dropped the pager should move back to OPEN state).
34465
+** * The pager is not an in-memory pager.
34466
+**
34467
+**
34468
+** Notes:
34469
+**
34470
+** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
34471
+** connection is open in WAL mode. A WAL connection is always in one
34472
+** of the first four states.
34473
+**
34474
+** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
34475
+** state. There are two exceptions: immediately after exclusive-mode has
34476
+** been turned on (and before any read or write transactions are
34477
+** executed), and when the pager is leaving the "error state".
34478
+**
34479
+** * See also: assert_pager_state().
34480
+*/
34481
+#define PAGER_OPEN 0
34482
+#define PAGER_READER 1
34483
+#define PAGER_WRITER_LOCKED 2
34484
+#define PAGER_WRITER_CACHEMOD 3
34485
+#define PAGER_WRITER_DBMOD 4
34486
+#define PAGER_WRITER_FINISHED 5
34487
+#define PAGER_ERROR 6
34488
+
34489
+/*
34490
+** The Pager.eLock variable is almost always set to one of the
34491
+** following locking-states, according to the lock currently held on
34492
+** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
34493
+** This variable is kept up to date as locks are taken and released by
34494
+** the pagerLockDb() and pagerUnlockDb() wrappers.
34495
+**
34496
+** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
34497
+** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
34498
+** the operation was successful. In these circumstances pagerLockDb() and
34499
+** pagerUnlockDb() take a conservative approach - eLock is always updated
34500
+** when unlocking the file, and only updated when locking the file if the
34501
+** VFS call is successful. This way, the Pager.eLock variable may be set
34502
+** to a less exclusive (lower) value than the lock that is actually held
34503
+** at the system level, but it is never set to a more exclusive value.
34504
+**
34505
+** This is usually safe. If an xUnlock fails or appears to fail, there may
34506
+** be a few redundant xLock() calls or a lock may be held for longer than
34507
+** required, but nothing really goes wrong.
34508
+**
34509
+** The exception is when the database file is unlocked as the pager moves
34510
+** from ERROR to OPEN state. At this point there may be a hot-journal file
34511
+** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
34512
+** transition, by the same pager or any other). If the call to xUnlock()
34513
+** fails at this point and the pager is left holding an EXCLUSIVE lock, this
34514
+** can confuse the call to xCheckReservedLock() call made later as part
34515
+** of hot-journal detection.
34516
+**
34517
+** xCheckReservedLock() is defined as returning true "if there is a RESERVED
34518
+** lock held by this process or any others". So xCheckReservedLock may
34519
+** return true because the caller itself is holding an EXCLUSIVE lock (but
34520
+** doesn't know it because of a previous error in xUnlock). If this happens
34521
+** a hot-journal may be mistaken for a journal being created by an active
34522
+** transaction in another process, causing SQLite to read from the database
34523
+** without rolling it back.
34524
+**
34525
+** To work around this, if a call to xUnlock() fails when unlocking the
34526
+** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
34527
+** is only changed back to a real locking state after a successful call
34528
+** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
34529
+** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
34530
+** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
34531
+** lock on the database file before attempting to roll it back. See function
34532
+** PagerSharedLock() for more detail.
34533
+**
34534
+** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
34535
+** PAGER_OPEN state.
34536
+*/
34537
+#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
3420934538
3421034539
/*
3421134540
** A macro used for invoking the codec if there is one
3421234541
*/
3421334542
#ifdef SQLITE_HAS_CODEC
@@ -34253,37 +34582,32 @@
3425334582
u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
3425434583
#endif
3425534584
};
3425634585
3425734586
/*
34258
-** A open page cache is an instance of the following structure.
34259
-**
34260
-** errCode
34261
-**
34262
-** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
34263
-** or SQLITE_FULL. Once one of the first three errors occurs, it persists
34264
-** and is returned as the result of every major pager API call. The
34265
-** SQLITE_FULL return code is slightly different. It persists only until the
34266
-** next successful rollback is performed on the pager cache. Also,
34267
-** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
34268
-** APIs, they may still be used successfully.
34269
-**
34270
-** dbSizeValid, dbSize, dbOrigSize, dbFileSize
34271
-**
34272
-** Managing the size of the database file in pages is a little complicated.
34273
-** The variable Pager.dbSize contains the number of pages that the database
34274
-** image currently contains. As the database image grows or shrinks this
34275
-** variable is updated. The variable Pager.dbFileSize contains the number
34276
-** of pages in the database file. This may be different from Pager.dbSize
34277
-** if some pages have been appended to the database image but not yet written
34278
-** out from the cache to the actual file on disk. Or if the image has been
34279
-** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
34280
-** contains the number of pages in the database image when the current
34281
-** transaction was opened. The contents of all three of these variables is
34282
-** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
34283
-**
34284
-** TODO: Under what conditions is dbSizeValid set? Cleared?
34587
+** A open page cache is an instance of struct Pager. A description of
34588
+** some of the more important member variables follows:
34589
+**
34590
+** eState
34591
+**
34592
+** The current 'state' of the pager object. See the comment and state
34593
+** diagram above for a description of the pager state.
34594
+**
34595
+** eLock
34596
+**
34597
+** For a real on-disk database, the current lock held on the database file -
34598
+** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
34599
+**
34600
+** For a temporary or in-memory database (neither of which require any
34601
+** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
34602
+** databases always have Pager.exclusiveMode==1, this tricks the pager
34603
+** logic into thinking that it already has all the locks it will ever
34604
+** need (and no reason to release them).
34605
+**
34606
+** In some (obscure) circumstances, this variable may also be set to
34607
+** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
34608
+** details.
3428534609
**
3428634610
** changeCountDone
3428734611
**
3428834612
** This boolean variable is used to make sure that the change-counter
3428934613
** (the 4-byte header field at byte offset 24 of the database file) is
@@ -34298,28 +34622,10 @@
3429834622
**
3429934623
** This mechanism means that when running in exclusive mode, a connection
3430034624
** need only update the change-counter once, for the first transaction
3430134625
** committed.
3430234626
**
34303
-** dbModified
34304
-**
34305
-** The dbModified flag is set whenever a database page is dirtied.
34306
-** It is cleared at the end of each transaction.
34307
-**
34308
-** It is used when committing or otherwise ending a transaction. If
34309
-** the dbModified flag is clear then less work has to be done.
34310
-**
34311
-** journalStarted
34312
-**
34313
-** This flag is set during a write-transaction after the first
34314
-** journal-header is written and synced to disk.
34315
-**
34316
-** After this has happened, new pages appended to the database
34317
-** do not need the PGHDR_NEED_SYNC flag set, as they do not need
34318
-** to wait for a journal sync before they can be written out to
34319
-** the database file (see function pager_write()).
34320
-**
3432134627
** setMaster
3432234628
**
3432334629
** When PagerCommitPhaseOne() is called to commit a transaction, it may
3432434630
** (or may not) specify a master-journal name to be written into the
3432534631
** journal file before it is synced to disk.
@@ -34326,84 +34632,146 @@
3432634632
**
3432734633
** Whether or not a journal file contains a master-journal pointer affects
3432834634
** the way in which the journal file is finalized after the transaction is
3432934635
** committed or rolled back when running in "journal_mode=PERSIST" mode.
3433034636
** If a journal file does not contain a master-journal pointer, it is
34331
-** finalized by overwriting the first journal header with zeroes. If,
34332
-** on the other hand, it does contain a master-journal pointer, the
34333
-** journal file is finalized by truncating it to zero bytes, just as if
34334
-** the connection were running in "journal_mode=truncate" mode.
34637
+** finalized by overwriting the first journal header with zeroes. If
34638
+** it does contain a master-journal pointer the journal file is finalized
34639
+** by truncating it to zero bytes, just as if the connection were
34640
+** running in "journal_mode=truncate" mode.
3433534641
**
3433634642
** Journal files that contain master journal pointers cannot be finalized
3433734643
** simply by overwriting the first journal-header with zeroes, as the
3433834644
** master journal pointer could interfere with hot-journal rollback of any
3433934645
** subsequently interrupted transaction that reuses the journal file.
3434034646
**
3434134647
** The flag is cleared as soon as the journal file is finalized (either
3434234648
** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
3434334649
** journal file from being successfully finalized, the setMaster flag
34344
-** is cleared anyway.
34650
+** is cleared anyway (and the pager will move to ERROR state).
3434534651
**
3434634652
** doNotSpill, doNotSyncSpill
3434734653
**
34348
-** When enabled, cache spills are prohibited. The doNotSpill variable
34349
-** inhibits all cache spill and doNotSyncSpill inhibits those spills that
34350
-** would require a journal sync. The doNotSyncSpill is set and cleared
34351
-** by sqlite3PagerWrite() in order to prevent a journal sync from happening
34352
-** in between the journalling of two pages on the same sector. The
34353
-** doNotSpill value set to prevent pagerStress() from trying to use
34354
-** the journal during a rollback.
34355
-**
34356
-** needSync
34357
-**
34358
-** TODO: It might be easier to set this variable in writeJournalHdr()
34359
-** and writeMasterJournal() only. Change its meaning to "unsynced data
34360
-** has been written to the journal".
34654
+** These two boolean variables control the behaviour of cache-spills
34655
+** (calls made by the pcache module to the pagerStress() routine to
34656
+** write cached data to the file-system in order to free up memory).
34657
+**
34658
+** When doNotSpill is non-zero, writing to the database from pagerStress()
34659
+** is disabled altogether. This is done in a very obscure case that
34660
+** comes up during savepoint rollback that requires the pcache module
34661
+** to allocate a new page to prevent the journal file from being written
34662
+** while it is being traversed by code in pager_playback().
34663
+**
34664
+** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
34665
+** is permitted, but syncing the journal file is not. This flag is set
34666
+** by sqlite3PagerWrite() when the file-system sector-size is larger than
34667
+** the database page-size in order to prevent a journal sync from happening
34668
+** in between the journalling of two pages on the same sector.
3436134669
**
3436234670
** subjInMemory
3436334671
**
3436434672
** This is a boolean variable. If true, then any required sub-journal
3436534673
** is opened as an in-memory journal file. If false, then in-memory
3436634674
** sub-journals are only used for in-memory pager files.
34675
+**
34676
+** This variable is updated by the upper layer each time a new
34677
+** write-transaction is opened.
34678
+**
34679
+** dbSize, dbOrigSize, dbFileSize
34680
+**
34681
+** Variable dbSize is set to the number of pages in the database file.
34682
+** It is valid in PAGER_READER and higher states (all states except for
34683
+** OPEN and ERROR).
34684
+**
34685
+** dbSize is set based on the size of the database file, which may be
34686
+** larger than the size of the database (the value stored at offset
34687
+** 28 of the database header by the btree). If the size of the file
34688
+** is not an integer multiple of the page-size, the value stored in
34689
+** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
34690
+** Except, any file that is greater than 0 bytes in size is considered
34691
+** to have at least one page. (i.e. a 1KB file with 2K page-size leads
34692
+** to dbSize==1).
34693
+**
34694
+** During a write-transaction, if pages with page-numbers greater than
34695
+** dbSize are modified in the cache, dbSize is updated accordingly.
34696
+** Similarly, if the database is truncated using PagerTruncateImage(),
34697
+** dbSize is updated.
34698
+**
34699
+** Variables dbOrigSize and dbFileSize are valid in states
34700
+** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
34701
+** variable at the start of the transaction. It is used during rollback,
34702
+** and to determine whether or not pages need to be journalled before
34703
+** being modified.
34704
+**
34705
+** Throughout a write-transaction, dbFileSize contains the size of
34706
+** the file on disk in pages. It is set to a copy of dbSize when the
34707
+** write-transaction is first opened, and updated when VFS calls are made
34708
+** to write or truncate the database file on disk.
34709
+**
34710
+** The only reason the dbFileSize variable is required is to suppress
34711
+** unnecessary calls to xTruncate() after committing a transaction. If,
34712
+** when a transaction is committed, the dbFileSize variable indicates
34713
+** that the database file is larger than the database image (Pager.dbSize),
34714
+** pager_truncate() is called. The pager_truncate() call uses xFilesize()
34715
+** to measure the database file on disk, and then truncates it if required.
34716
+** dbFileSize is not used when rolling back a transaction. In this case
34717
+** pager_truncate() is called unconditionally (which means there may be
34718
+** a call to xFilesize() that is not strictly required). In either case,
34719
+** pager_truncate() may cause the file to become smaller or larger.
34720
+**
34721
+** dbHintSize
34722
+**
34723
+** The dbHintSize variable is used to limit the number of calls made to
34724
+** the VFS xFileControl(FCNTL_SIZE_HINT) method.
34725
+**
34726
+** dbHintSize is set to a copy of the dbSize variable when a
34727
+** write-transaction is opened (at the same time as dbFileSize and
34728
+** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
34729
+** dbHintSize is increased to the number of pages that correspond to the
34730
+** size-hint passed to the method call. See pager_write_pagelist() for
34731
+** details.
34732
+**
34733
+** errCode
34734
+**
34735
+** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
34736
+** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
34737
+** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
34738
+** sub-codes.
3436734739
*/
3436834740
struct Pager {
3436934741
sqlite3_vfs *pVfs; /* OS functions to use for IO */
3437034742
u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
34371
- u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
34743
+ u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
3437234744
u8 useJournal; /* Use a rollback journal on this file */
3437334745
u8 noReadlock; /* Do not bother to obtain readlocks */
3437434746
u8 noSync; /* Do not sync the journal if true */
3437534747
u8 fullSync; /* Do extra syncs of the journal for robustness */
3437634748
u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
3437734749
u8 tempFile; /* zFilename is a temporary file */
3437834750
u8 readOnly; /* True for a read-only database */
3437934751
u8 memDb; /* True to inhibit all file I/O */
3438034752
34381
- /* The following block contains those class members that are dynamically
34382
- ** modified during normal operations. The other variables in this structure
34383
- ** are either constant throughout the lifetime of the pager, or else
34384
- ** used to store configuration parameters that affect the way the pager
34385
- ** operates.
34386
- **
34387
- ** The 'state' variable is described in more detail along with the
34388
- ** descriptions of the values it may take - PAGER_UNLOCK etc. Many of the
34389
- ** other variables in this block are described in the comment directly
34390
- ** above this class definition.
34753
+ /**************************************************************************
34754
+ ** The following block contains those class members that change during
34755
+ ** routine opertion. Class members not in this block are either fixed
34756
+ ** when the pager is first created or else only change when there is a
34757
+ ** significant mode change (such as changing the page_size, locking_mode,
34758
+ ** or the journal_mode). From another view, these class members describe
34759
+ ** the "state" of the pager, while other class members describe the
34760
+ ** "configuration" of the pager.
3439134761
*/
34392
- u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
34393
- u8 dbModified; /* True if there are any changes to the Db */
34394
- u8 needSync; /* True if an fsync() is needed on the journal */
34395
- u8 journalStarted; /* True if header of journal is synced */
34762
+ u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
34763
+ u8 eLock; /* Current lock held on database file */
3439634764
u8 changeCountDone; /* Set after incrementing the change-counter */
3439734765
u8 setMaster; /* True if a m-j name has been written to jrnl */
3439834766
u8 doNotSpill; /* Do not spill the cache when non-zero */
3439934767
u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
34400
- u8 dbSizeValid; /* Set when dbSize is correct */
3440134768
u8 subjInMemory; /* True to use in-memory sub-journals */
3440234769
Pgno dbSize; /* Number of pages in the database */
3440334770
Pgno dbOrigSize; /* dbSize before the current transaction */
3440434771
Pgno dbFileSize; /* Number of pages in the database file */
34772
+ Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
3440534773
int errCode; /* One of several kinds of errors */
3440634774
int nRec; /* Pages journalled since last j-header written */
3440734775
u32 cksumInit; /* Quasi-random value added to every checksum */
3440834776
u32 nSubRec; /* Number of records written to sub-journal */
3440934777
Bitvec *pInJournal; /* One bit for each page in the database file */
@@ -34410,21 +34778,25 @@
3441034778
sqlite3_file *fd; /* File descriptor for database */
3441134779
sqlite3_file *jfd; /* File descriptor for main journal */
3441234780
sqlite3_file *sjfd; /* File descriptor for sub-journal */
3441334781
i64 journalOff; /* Current write offset in the journal file */
3441434782
i64 journalHdr; /* Byte offset to previous journal header */
34415
- i64 journalSizeLimit; /* Size limit for persistent journal files */
34783
+ sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
3441634784
PagerSavepoint *aSavepoint; /* Array of active savepoints */
3441734785
int nSavepoint; /* Number of elements in aSavepoint[] */
3441834786
char dbFileVers[16]; /* Changes whenever database file changes */
34419
- u32 sectorSize; /* Assumed sector size during rollback */
34787
+ /*
34788
+ ** End of the routinely-changing class members
34789
+ ***************************************************************************/
3442034790
3442134791
u16 nExtra; /* Add this many bytes to each in-memory page */
3442234792
i16 nReserve; /* Number of unused bytes at end of each page */
3442334793
u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
34794
+ u32 sectorSize; /* Assumed sector size during rollback */
3442434795
int pageSize; /* Number of bytes in a page */
3442534796
Pgno mxPgno; /* Maximum allowed size of the database */
34797
+ i64 journalSizeLimit; /* Size limit for persistent journal files */
3442634798
char *zFilename; /* Name of the database file */
3442734799
char *zJournal; /* Name of the journal file */
3442834800
int (*xBusyHandler)(void*); /* Function to call when busy */
3442934801
void *pBusyHandlerArg; /* Context argument for xBusyHandler */
3443034802
#ifdef SQLITE_TEST
@@ -34438,11 +34810,10 @@
3443834810
void (*xCodecFree)(void*); /* Destructor for the codec */
3443934811
void *pCodec; /* First argument to xCodec... methods */
3444034812
#endif
3444134813
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
3444234814
PCache *pPCache; /* Pointer to page cache object */
34443
- sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
3444434815
#ifndef SQLITE_OMIT_WAL
3444534816
Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
3444634817
char *zWal; /* File name for write-ahead log */
3444734818
#endif
3444834819
};
@@ -34517,26 +34888,225 @@
3451734888
/*
3451834889
** The maximum legal page number is (2^31 - 1).
3451934890
*/
3452034891
#define PAGER_MAX_PGNO 2147483647
3452134892
34893
+/*
34894
+** The argument to this macro is a file descriptor (type sqlite3_file*).
34895
+** Return 0 if it is not open, or non-zero (but not 1) if it is.
34896
+**
34897
+** This is so that expressions can be written as:
34898
+**
34899
+** if( isOpen(pPager->jfd) ){ ...
34900
+**
34901
+** instead of
34902
+**
34903
+** if( pPager->jfd->pMethods ){ ...
34904
+*/
34905
+#define isOpen(pFd) ((pFd)->pMethods)
34906
+
34907
+/*
34908
+** Return true if this pager uses a write-ahead log instead of the usual
34909
+** rollback journal. Otherwise false.
34910
+*/
34911
+#ifndef SQLITE_OMIT_WAL
34912
+static int pagerUseWal(Pager *pPager){
34913
+ return (pPager->pWal!=0);
34914
+}
34915
+#else
34916
+# define pagerUseWal(x) 0
34917
+# define pagerRollbackWal(x) 0
34918
+# define pagerWalFrames(v,w,x,y,z) 0
34919
+# define pagerOpenWalIfPresent(z) SQLITE_OK
34920
+# define pagerBeginReadTransaction(z) SQLITE_OK
34921
+#endif
34922
+
3452234923
#ifndef NDEBUG
3452334924
/*
3452434925
** Usage:
3452534926
**
3452634927
** assert( assert_pager_state(pPager) );
34928
+**
34929
+** This function runs many asserts to try to find inconsistencies in
34930
+** the internal state of the Pager object.
3452734931
*/
34528
-static int assert_pager_state(Pager *pPager){
34932
+static int assert_pager_state(Pager *p){
34933
+ Pager *pPager = p;
3452934934
34530
- /* A temp-file is always in PAGER_EXCLUSIVE or PAGER_SYNCED state. */
34531
- assert( pPager->tempFile==0 || pPager->state>=PAGER_EXCLUSIVE );
34935
+ /* State must be valid. */
34936
+ assert( p->eState==PAGER_OPEN
34937
+ || p->eState==PAGER_READER
34938
+ || p->eState==PAGER_WRITER_LOCKED
34939
+ || p->eState==PAGER_WRITER_CACHEMOD
34940
+ || p->eState==PAGER_WRITER_DBMOD
34941
+ || p->eState==PAGER_WRITER_FINISHED
34942
+ || p->eState==PAGER_ERROR
34943
+ );
3453234944
34533
- /* The changeCountDone flag is always set for temp-files */
34534
- assert( pPager->tempFile==0 || pPager->changeCountDone );
34945
+ /* Regardless of the current state, a temp-file connection always behaves
34946
+ ** as if it has an exclusive lock on the database file. It never updates
34947
+ ** the change-counter field, so the changeCountDone flag is always set.
34948
+ */
34949
+ assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
34950
+ assert( p->tempFile==0 || pPager->changeCountDone );
34951
+
34952
+ /* If the useJournal flag is clear, the journal-mode must be "OFF".
34953
+ ** And if the journal-mode is "OFF", the journal file must not be open.
34954
+ */
34955
+ assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
34956
+ assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
34957
+
34958
+ /* Check that MEMDB implies noSync. And an in-memory journal. Since
34959
+ ** this means an in-memory pager performs no IO at all, it cannot encounter
34960
+ ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
34961
+ ** a journal file. (although the in-memory journal implementation may
34962
+ ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
34963
+ ** is therefore not possible for an in-memory pager to enter the ERROR
34964
+ ** state.
34965
+ */
34966
+ if( MEMDB ){
34967
+ assert( p->noSync );
34968
+ assert( p->journalMode==PAGER_JOURNALMODE_OFF
34969
+ || p->journalMode==PAGER_JOURNALMODE_MEMORY
34970
+ );
34971
+ assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
34972
+ assert( pagerUseWal(p)==0 );
34973
+ }
34974
+
34975
+ /* If changeCountDone is set, a RESERVED lock or greater must be held
34976
+ ** on the file.
34977
+ */
34978
+ assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
34979
+ assert( p->eLock!=PENDING_LOCK );
34980
+
34981
+ switch( p->eState ){
34982
+ case PAGER_OPEN:
34983
+ assert( !MEMDB );
34984
+ assert( pPager->errCode==SQLITE_OK );
34985
+ assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
34986
+ break;
34987
+
34988
+ case PAGER_READER:
34989
+ assert( pPager->errCode==SQLITE_OK );
34990
+ assert( p->eLock!=UNKNOWN_LOCK );
34991
+ assert( p->eLock>=SHARED_LOCK || p->noReadlock );
34992
+ break;
34993
+
34994
+ case PAGER_WRITER_LOCKED:
34995
+ assert( p->eLock!=UNKNOWN_LOCK );
34996
+ assert( pPager->errCode==SQLITE_OK );
34997
+ if( !pagerUseWal(pPager) ){
34998
+ assert( p->eLock>=RESERVED_LOCK );
34999
+ }
35000
+ assert( pPager->dbSize==pPager->dbOrigSize );
35001
+ assert( pPager->dbOrigSize==pPager->dbFileSize );
35002
+ assert( pPager->dbOrigSize==pPager->dbHintSize );
35003
+ assert( pPager->setMaster==0 );
35004
+ break;
35005
+
35006
+ case PAGER_WRITER_CACHEMOD:
35007
+ assert( p->eLock!=UNKNOWN_LOCK );
35008
+ assert( pPager->errCode==SQLITE_OK );
35009
+ if( !pagerUseWal(pPager) ){
35010
+ /* It is possible that if journal_mode=wal here that neither the
35011
+ ** journal file nor the WAL file are open. This happens during
35012
+ ** a rollback transaction that switches from journal_mode=off
35013
+ ** to journal_mode=wal.
35014
+ */
35015
+ assert( p->eLock>=RESERVED_LOCK );
35016
+ assert( isOpen(p->jfd)
35017
+ || p->journalMode==PAGER_JOURNALMODE_OFF
35018
+ || p->journalMode==PAGER_JOURNALMODE_WAL
35019
+ );
35020
+ }
35021
+ assert( pPager->dbOrigSize==pPager->dbFileSize );
35022
+ assert( pPager->dbOrigSize==pPager->dbHintSize );
35023
+ break;
35024
+
35025
+ case PAGER_WRITER_DBMOD:
35026
+ assert( p->eLock==EXCLUSIVE_LOCK );
35027
+ assert( pPager->errCode==SQLITE_OK );
35028
+ assert( !pagerUseWal(pPager) );
35029
+ assert( p->eLock>=EXCLUSIVE_LOCK );
35030
+ assert( isOpen(p->jfd)
35031
+ || p->journalMode==PAGER_JOURNALMODE_OFF
35032
+ || p->journalMode==PAGER_JOURNALMODE_WAL
35033
+ );
35034
+ assert( pPager->dbOrigSize<=pPager->dbHintSize );
35035
+ break;
35036
+
35037
+ case PAGER_WRITER_FINISHED:
35038
+ assert( p->eLock==EXCLUSIVE_LOCK );
35039
+ assert( pPager->errCode==SQLITE_OK );
35040
+ assert( !pagerUseWal(pPager) );
35041
+ assert( isOpen(p->jfd)
35042
+ || p->journalMode==PAGER_JOURNALMODE_OFF
35043
+ || p->journalMode==PAGER_JOURNALMODE_WAL
35044
+ );
35045
+ break;
35046
+
35047
+ case PAGER_ERROR:
35048
+ /* There must be at least one outstanding reference to the pager if
35049
+ ** in ERROR state. Otherwise the pager should have already dropped
35050
+ ** back to OPEN state.
35051
+ */
35052
+ assert( pPager->errCode!=SQLITE_OK );
35053
+ assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
35054
+ break;
35055
+ }
3453535056
3453635057
return 1;
3453735058
}
35059
+
35060
+/*
35061
+** Return a pointer to a human readable string in a static buffer
35062
+** containing the state of the Pager object passed as an argument. This
35063
+** is intended to be used within debuggers. For example, as an alternative
35064
+** to "print *pPager" in gdb:
35065
+**
35066
+** (gdb) printf "%s", print_pager_state(pPager)
35067
+*/
35068
+static char *print_pager_state(Pager *p){
35069
+ static char zRet[1024];
35070
+
35071
+ sqlite3_snprintf(1024, zRet,
35072
+ "Filename: %s\n"
35073
+ "State: %s errCode=%d\n"
35074
+ "Lock: %s\n"
35075
+ "Locking mode: locking_mode=%s\n"
35076
+ "Journal mode: journal_mode=%s\n"
35077
+ "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
35078
+ "Journal: journalOff=%lld journalHdr=%lld\n"
35079
+ "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
35080
+ , p->zFilename
35081
+ , p->eState==PAGER_OPEN ? "OPEN" :
35082
+ p->eState==PAGER_READER ? "READER" :
35083
+ p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
35084
+ p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
35085
+ p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
35086
+ p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
35087
+ p->eState==PAGER_ERROR ? "ERROR" : "?error?"
35088
+ , (int)p->errCode
35089
+ , p->eLock==NO_LOCK ? "NO_LOCK" :
35090
+ p->eLock==RESERVED_LOCK ? "RESERVED" :
35091
+ p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
35092
+ p->eLock==SHARED_LOCK ? "SHARED" :
35093
+ p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
35094
+ , p->exclusiveMode ? "exclusive" : "normal"
35095
+ , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
35096
+ p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
35097
+ p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
35098
+ p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
35099
+ p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
35100
+ p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
35101
+ , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
35102
+ , p->journalOff, p->journalHdr
35103
+ , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
35104
+ );
35105
+
35106
+ return zRet;
35107
+}
3453835108
#endif
3453935109
3454035110
/*
3454135111
** Return true if it is necessary to write page *pPg into the sub-journal.
3454235112
** A page needs to be written into the sub-journal if there exists one
@@ -34584,10 +35154,11 @@
3458435154
3458535155
/*
3458635156
** Write a 32-bit integer into a string buffer in big-endian byte order.
3458735157
*/
3458835158
#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
35159
+
3458935160
3459035161
/*
3459135162
** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
3459235163
** on success or an error code is something goes wrong.
3459335164
*/
@@ -34596,31 +35167,57 @@
3459635167
put32bits(ac, val);
3459735168
return sqlite3OsWrite(fd, ac, 4, offset);
3459835169
}
3459935170
3460035171
/*
34601
-** The argument to this macro is a file descriptor (type sqlite3_file*).
34602
-** Return 0 if it is not open, or non-zero (but not 1) if it is.
34603
-**
34604
-** This is so that expressions can be written as:
34605
-**
34606
-** if( isOpen(pPager->jfd) ){ ...
34607
-**
34608
-** instead of
34609
-**
34610
-** if( pPager->jfd->pMethods ){ ...
34611
-*/
34612
-#define isOpen(pFd) ((pFd)->pMethods)
35172
+** Unlock the database file to level eLock, which must be either NO_LOCK
35173
+** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
35174
+** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
35175
+**
35176
+** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
35177
+** called, do not modify it. See the comment above the #define of
35178
+** UNKNOWN_LOCK for an explanation of this.
35179
+*/
35180
+static int pagerUnlockDb(Pager *pPager, int eLock){
35181
+ int rc = SQLITE_OK;
35182
+
35183
+ assert( !pPager->exclusiveMode );
35184
+ assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
35185
+ assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
35186
+ if( isOpen(pPager->fd) ){
35187
+ assert( pPager->eLock>=eLock );
35188
+ rc = sqlite3OsUnlock(pPager->fd, eLock);
35189
+ if( pPager->eLock!=UNKNOWN_LOCK ){
35190
+ pPager->eLock = eLock;
35191
+ }
35192
+ IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
35193
+ }
35194
+ return rc;
35195
+}
3461335196
3461435197
/*
34615
-** If file pFd is open, call sqlite3OsUnlock() on it.
35198
+** Lock the database file to level eLock, which must be either SHARED_LOCK,
35199
+** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
35200
+** Pager.eLock variable to the new locking state.
35201
+**
35202
+** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
35203
+** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
35204
+** See the comment above the #define of UNKNOWN_LOCK for an explanation
35205
+** of this.
3461635206
*/
34617
-static int osUnlock(sqlite3_file *pFd, int eLock){
34618
- if( !isOpen(pFd) ){
34619
- return SQLITE_OK;
35207
+static int pagerLockDb(Pager *pPager, int eLock){
35208
+ int rc = SQLITE_OK;
35209
+
35210
+ assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
35211
+ if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
35212
+ rc = sqlite3OsLock(pPager->fd, eLock);
35213
+ if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
35214
+ pPager->eLock = eLock;
35215
+ IOTRACE(("LOCK %p %d\n", pPager, eLock))
35216
+ }
3462035217
}
34621
- return sqlite3OsUnlock(pFd, eLock);
35218
+ return rc;
3462235219
}
3462335220
3462435221
/*
3462535222
** This function determines whether or not the atomic-write optimization
3462635223
** can be used with this pager. The optimization can be used if:
@@ -34865,11 +35462,11 @@
3486535462
** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
3486635463
*/
3486735464
static int writeJournalHdr(Pager *pPager){
3486835465
int rc = SQLITE_OK; /* Return code */
3486935466
char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
34870
- u32 nHeader = pPager->pageSize; /* Size of buffer pointed to by zHeader */
35467
+ u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
3487135468
u32 nWrite; /* Bytes of header sector written */
3487235469
int ii; /* Loop counter */
3487335470
3487435471
assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
3487535472
@@ -34908,11 +35505,11 @@
3490835505
**
3490935506
** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
3491035507
** that garbage data is never appended to the journal file.
3491135508
*/
3491235509
assert( isOpen(pPager->fd) || pPager->noSync );
34913
- if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
35510
+ if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
3491435511
|| (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
3491535512
){
3491635513
memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
3491735514
put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
3491835515
}else{
@@ -35032,11 +35629,10 @@
3503235629
}
3503335630
3503435631
if( pPager->journalOff==0 ){
3503535632
u32 iPageSize; /* Page-size field of journal header */
3503635633
u32 iSectorSize; /* Sector-size field of journal header */
35037
- u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */
3503835634
3503935635
/* Read the page-size and sector-size journal header fields. */
3504035636
if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
3504135637
|| SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
3504235638
){
@@ -35062,14 +35658,12 @@
3506235658
3506335659
/* Update the page-size to match the value read from the journal.
3506435660
** Use a testcase() macro to make sure that malloc failure within
3506535661
** PagerSetPagesize() is tested.
3506635662
*/
35067
- iPageSize16 = (u16)iPageSize;
35068
- rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
35663
+ rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
3506935664
testcase( rc!=SQLITE_OK );
35070
- assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
3507135665
3507235666
/* Update the assumed sector-size to match the value used by
3507335667
** the process that created this journal. If this journal was
3507435668
** created by a process other than this one, then this routine
3507535669
** is being called from within pager_playback(). The local value
@@ -35108,10 +35702,12 @@
3510835702
i64 iHdrOff; /* Offset of header in journal file */
3510935703
i64 jrnlSize; /* Size of journal file on disk */
3511035704
u32 cksum = 0; /* Checksum of string zMaster */
3511135705
3511235706
assert( pPager->setMaster==0 );
35707
+ assert( !pagerUseWal(pPager) );
35708
+
3511335709
if( !zMaster
3511435710
|| pPager->journalMode==PAGER_JOURNALMODE_MEMORY
3511535711
|| pPager->journalMode==PAGER_JOURNALMODE_OFF
3511635712
){
3511735713
return SQLITE_OK;
@@ -35144,11 +35740,10 @@
3514435740
|| (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
3514535741
){
3514635742
return rc;
3514735743
}
3514835744
pPager->journalOff += (nMaster+20);
35149
- pPager->needSync = !pPager->noSync;
3515035745
3515135746
/* If the pager is in peristent-journal mode, then the physical
3515235747
** journal-file may extend past the end of the master-journal name
3515335748
** and 8 bytes of magic data just written to the file. This is
3515435749
** dangerous because the code to rollback a hot-journal file
@@ -35180,21 +35775,15 @@
3518035775
(void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
3518135776
return p;
3518235777
}
3518335778
3518435779
/*
35185
-** Unless the pager is in error-state, discard all in-memory pages. If
35186
-** the pager is in error-state, then this call is a no-op.
35187
-**
35188
-** TODO: Why can we not reset the pager while in error state?
35780
+** Discard the entire contents of the in-memory page-cache.
3518935781
*/
3519035782
static void pager_reset(Pager *pPager){
35191
- if( SQLITE_OK==pPager->errCode ){
35192
- sqlite3BackupRestart(pPager->pBackup);
35193
- sqlite3PcacheClear(pPager->pPCache);
35194
- pPager->dbSizeValid = 0;
35195
- }
35783
+ sqlite3BackupRestart(pPager->pBackup);
35784
+ sqlite3PcacheClear(pPager->pPCache);
3519635785
}
3519735786
3519835787
/*
3519935788
** Free all structures in the Pager.aSavepoint[] array and set both
3520035789
** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
@@ -35233,38 +35822,43 @@
3523335822
}
3523435823
return rc;
3523535824
}
3523635825
3523735826
/*
35238
-** Return true if this pager uses a write-ahead log instead of the usual
35239
-** rollback journal. Otherwise false.
35240
-*/
35241
-#ifndef SQLITE_OMIT_WAL
35242
-static int pagerUseWal(Pager *pPager){
35243
- return (pPager->pWal!=0);
35244
-}
35245
-#else
35246
-# define pagerUseWal(x) 0
35247
-# define pagerRollbackWal(x) 0
35248
-# define pagerWalFrames(v,w,x,y,z) 0
35249
-# define pagerOpenWalIfPresent(z) SQLITE_OK
35250
-# define pagerBeginReadTransaction(z) SQLITE_OK
35251
-#endif
35252
-
35253
-/*
35254
-** Unlock the database file. This function is a no-op if the pager
35255
-** is in exclusive mode.
35827
+** This function is a no-op if the pager is in exclusive mode and not
35828
+** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
35829
+** state.
3525635830
**
35257
-** If the pager is currently in error state, discard the contents of
35258
-** the cache and reset the Pager structure internal state. If there is
35259
-** an open journal-file, then the next time a shared-lock is obtained
35260
-** on the pager file (by this or any other process), it will be
35261
-** treated as a hot-journal and rolled back.
35831
+** If the pager is not in exclusive-access mode, the database file is
35832
+** completely unlocked. If the file is unlocked and the file-system does
35833
+** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
35834
+** closed (if it is open).
35835
+**
35836
+** If the pager is in ERROR state when this function is called, the
35837
+** contents of the pager cache are discarded before switching back to
35838
+** the OPEN state. Regardless of whether the pager is in exclusive-mode
35839
+** or not, any journal file left in the file-system will be treated
35840
+** as a hot-journal and rolled back the next time a read-transaction
35841
+** is opened (by this or by any other connection).
3526235842
*/
3526335843
static void pager_unlock(Pager *pPager){
35264
- if( !pPager->exclusiveMode ){
35265
- int rc = SQLITE_OK; /* Return code */
35844
+
35845
+ assert( pPager->eState==PAGER_READER
35846
+ || pPager->eState==PAGER_OPEN
35847
+ || pPager->eState==PAGER_ERROR
35848
+ );
35849
+
35850
+ sqlite3BitvecDestroy(pPager->pInJournal);
35851
+ pPager->pInJournal = 0;
35852
+ releaseAllSavepoints(pPager);
35853
+
35854
+ if( pagerUseWal(pPager) ){
35855
+ assert( !isOpen(pPager->jfd) );
35856
+ sqlite3WalEndReadTransaction(pPager->pWal);
35857
+ pPager->eState = PAGER_OPEN;
35858
+ }else if( !pPager->exclusiveMode ){
35859
+ int rc; /* Error code returned by pagerUnlockDb() */
3526635860
int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
3526735861
3526835862
/* If the operating system support deletion of open files, then
3526935863
** close the journal file when dropping the database lock. Otherwise
3527035864
** another connection with journal_mode=delete might delete the file
@@ -35280,62 +35874,60 @@
3528035874
|| 1!=(pPager->journalMode & 5)
3528135875
){
3528235876
sqlite3OsClose(pPager->jfd);
3528335877
}
3528435878
35285
- sqlite3BitvecDestroy(pPager->pInJournal);
35286
- pPager->pInJournal = 0;
35287
- releaseAllSavepoints(pPager);
35288
-
35289
- /* If the file is unlocked, somebody else might change it. The
35290
- ** values stored in Pager.dbSize etc. might become invalid if
35291
- ** this happens. One can argue that this doesn't need to be cleared
35292
- ** until the change-counter check fails in PagerSharedLock().
35293
- ** Clearing the page size cache here is being conservative.
35294
- */
35295
- pPager->dbSizeValid = 0;
35296
-
35297
- if( pagerUseWal(pPager) ){
35298
- sqlite3WalEndReadTransaction(pPager->pWal);
35299
- }else{
35300
- rc = osUnlock(pPager->fd, NO_LOCK);
35301
- }
35302
- if( rc ){
35303
- pPager->errCode = rc;
35304
- }
35305
- IOTRACE(("UNLOCK %p\n", pPager))
35306
-
35307
- /* If Pager.errCode is set, the contents of the pager cache cannot be
35308
- ** trusted. Now that the pager file is unlocked, the contents of the
35309
- ** cache can be discarded and the error code safely cleared.
35310
- */
35311
- if( pPager->errCode ){
35312
- if( rc==SQLITE_OK ){
35313
- pPager->errCode = SQLITE_OK;
35314
- }
35315
- pager_reset(pPager);
35316
- }
35317
-
35879
+ /* If the pager is in the ERROR state and the call to unlock the database
35880
+ ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
35881
+ ** above the #define for UNKNOWN_LOCK for an explanation of why this
35882
+ ** is necessary.
35883
+ */
35884
+ rc = pagerUnlockDb(pPager, NO_LOCK);
35885
+ if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
35886
+ pPager->eLock = UNKNOWN_LOCK;
35887
+ }
35888
+
35889
+ /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
35890
+ ** without clearing the error code. This is intentional - the error
35891
+ ** code is cleared and the cache reset in the block below.
35892
+ */
35893
+ assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
3531835894
pPager->changeCountDone = 0;
35319
- pPager->state = PAGER_UNLOCK;
35320
- pPager->dbModified = 0;
35895
+ pPager->eState = PAGER_OPEN;
3532135896
}
35897
+
35898
+ /* If Pager.errCode is set, the contents of the pager cache cannot be
35899
+ ** trusted. Now that there are no outstanding references to the pager,
35900
+ ** it can safely move back to PAGER_OPEN state. This happens in both
35901
+ ** normal and exclusive-locking mode.
35902
+ */
35903
+ if( pPager->errCode ){
35904
+ assert( !MEMDB );
35905
+ pager_reset(pPager);
35906
+ pPager->changeCountDone = pPager->tempFile;
35907
+ pPager->eState = PAGER_OPEN;
35908
+ pPager->errCode = SQLITE_OK;
35909
+ }
35910
+
35911
+ pPager->journalOff = 0;
35912
+ pPager->journalHdr = 0;
35913
+ pPager->setMaster = 0;
3532235914
}
3532335915
3532435916
/*
35325
-** This function should be called when an IOERR, CORRUPT or FULL error
35326
-** may have occurred. The first argument is a pointer to the pager
35327
-** structure, the second the error-code about to be returned by a pager
35328
-** API function. The value returned is a copy of the second argument
35329
-** to this function.
35330
-**
35331
-** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
35332
-** the error becomes persistent. Until the persistent error is cleared,
35333
-** subsequent API calls on this Pager will immediately return the same
35334
-** error code.
35335
-**
35336
-** A persistent error indicates that the contents of the pager-cache
35917
+** This function is called whenever an IOERR or FULL error that requires
35918
+** the pager to transition into the ERROR state may ahve occurred.
35919
+** The first argument is a pointer to the pager structure, the second
35920
+** the error-code about to be returned by a pager API function. The
35921
+** value returned is a copy of the second argument to this function.
35922
+**
35923
+** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
35924
+** IOERR sub-codes, the pager enters the ERROR state and the error code
35925
+** is stored in Pager.errCode. While the pager remains in the ERROR state,
35926
+** all major API calls on the Pager will immediately return Pager.errCode.
35927
+**
35928
+** The ERROR state indicates that the contents of the pager-cache
3533735929
** cannot be trusted. This state can be cleared by completely discarding
3533835930
** the contents of the pager-cache. If a transaction was active when
3533935931
** the persistent error occurred, then the rollback journal may need
3534035932
** to be replayed to restore the contents of the database file (as if
3534135933
** it were a hot-journal).
@@ -35348,49 +35940,25 @@
3534835940
pPager->errCode==SQLITE_OK ||
3534935941
(pPager->errCode & 0xff)==SQLITE_IOERR
3535035942
);
3535135943
if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
3535235944
pPager->errCode = rc;
35945
+ pPager->eState = PAGER_ERROR;
3535335946
}
3535435947
return rc;
3535535948
}
3535635949
35357
-/*
35358
-** Execute a rollback if a transaction is active and unlock the
35359
-** database file.
35360
-**
35361
-** If the pager has already entered the error state, do not attempt
35362
-** the rollback at this time. Instead, pager_unlock() is called. The
35363
-** call to pager_unlock() will discard all in-memory pages, unlock
35364
-** the database file and clear the error state. If this means that
35365
-** there is a hot-journal left in the file-system, the next connection
35366
-** to obtain a shared lock on the pager (which may be this one) will
35367
-** roll it back.
35368
-**
35369
-** If the pager has not already entered the error state, but an IO or
35370
-** malloc error occurs during a rollback, then this will itself cause
35371
-** the pager to enter the error state. Which will be cleared by the
35372
-** call to pager_unlock(), as described above.
35373
-*/
35374
-static void pagerUnlockAndRollback(Pager *pPager){
35375
- if( pPager->errCode==SQLITE_OK && pPager->state>=PAGER_RESERVED ){
35376
- sqlite3BeginBenignMalloc();
35377
- sqlite3PagerRollback(pPager);
35378
- sqlite3EndBenignMalloc();
35379
- }
35380
- pager_unlock(pPager);
35381
-}
35382
-
3538335950
/*
3538435951
** This routine ends a transaction. A transaction is usually ended by
3538535952
** either a COMMIT or a ROLLBACK operation. This routine may be called
3538635953
** after rollback of a hot-journal, or if an error occurs while opening
3538735954
** the journal file or writing the very first journal-header of a
3538835955
** database transaction.
3538935956
**
35390
-** If the pager is in PAGER_SHARED or PAGER_UNLOCK state when this
35391
-** routine is called, it is a no-op (returns SQLITE_OK).
35957
+** This routine is never called in PAGER_ERROR state. If it is called
35958
+** in PAGER_NONE or PAGER_SHARED state and the lock held is less
35959
+** exclusive than a RESERVED lock, it is a no-op.
3539235960
**
3539335961
** Otherwise, any active savepoints are released.
3539435962
**
3539535963
** If the journal file is open, then it is "finalized". Once a journal
3539635964
** file has been finalized it is not possible to use it to roll back a
@@ -35417,17 +35985,13 @@
3541735985
** If the pager is running in exclusive mode, this method of finalizing
3541835986
** the journal file is never used. Instead, if the journalMode is
3541935987
** DELETE and the pager is in exclusive mode, the method described under
3542035988
** journalMode==PERSIST is used instead.
3542135989
**
35422
-** After the journal is finalized, if running in non-exclusive mode, the
35423
-** pager moves to PAGER_SHARED state (and downgrades the lock on the
35424
-** database file accordingly).
35425
-**
35426
-** If the pager is running in exclusive mode and is in PAGER_SYNCED state,
35427
-** it moves to PAGER_EXCLUSIVE. No locks are downgraded when running in
35428
-** exclusive mode.
35990
+** After the journal is finalized, the pager moves to PAGER_READER state.
35991
+** If running in non-exclusive rollback mode, the lock on the file is
35992
+** downgraded to a SHARED_LOCK.
3542935993
**
3543035994
** SQLITE_OK is returned if no error occurs. If an error occurs during
3543135995
** any of the IO operations to finalize the journal file or unlock the
3543235996
** database then the IO error code is returned to the user. If the
3543335997
** operation to finalize the journal file fails, then the code still
@@ -35438,15 +36002,30 @@
3543836002
*/
3543936003
static int pager_end_transaction(Pager *pPager, int hasMaster){
3544036004
int rc = SQLITE_OK; /* Error code from journal finalization operation */
3544136005
int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
3544236006
35443
- if( pPager->state<PAGER_RESERVED ){
36007
+ /* Do nothing if the pager does not have an open write transaction
36008
+ ** or at least a RESERVED lock. This function may be called when there
36009
+ ** is no write-transaction active but a RESERVED or greater lock is
36010
+ ** held under two circumstances:
36011
+ **
36012
+ ** 1. After a successful hot-journal rollback, it is called with
36013
+ ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
36014
+ **
36015
+ ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
36016
+ ** lock switches back to locking_mode=normal and then executes a
36017
+ ** read-transaction, this function is called with eState==PAGER_READER
36018
+ ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
36019
+ */
36020
+ assert( assert_pager_state(pPager) );
36021
+ assert( pPager->eState!=PAGER_ERROR );
36022
+ if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
3544436023
return SQLITE_OK;
3544536024
}
36025
+
3544636026
releaseAllSavepoints(pPager);
35447
-
3544836027
assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
3544936028
if( isOpen(pPager->jfd) ){
3545036029
assert( !pagerUseWal(pPager) );
3545136030
3545236031
/* Finalize the journal file. */
@@ -35458,18 +36037,15 @@
3545836037
rc = SQLITE_OK;
3545936038
}else{
3546036039
rc = sqlite3OsTruncate(pPager->jfd, 0);
3546136040
}
3546236041
pPager->journalOff = 0;
35463
- pPager->journalStarted = 0;
3546436042
}else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
3546536043
|| (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
3546636044
){
3546736045
rc = zeroJournalHdr(pPager, hasMaster);
35468
- pager_error(pPager, rc);
3546936046
pPager->journalOff = 0;
35470
- pPager->journalStarted = 0;
3547136047
}else{
3547236048
/* This branch may be executed with Pager.journalMode==MEMORY if
3547336049
** a hot-journal was just rolled back. In this case the journal
3547436050
** file should be closed and deleted. If this connection writes to
3547536051
** the database file, it will do so using an in-memory journal.
@@ -35490,43 +36066,63 @@
3549036066
}
3549136067
sqlite3BitvecDestroy(pPager->pInJournal);
3549236068
pPager->pInJournal = 0;
3549336069
pPager->nRec = 0;
3549436070
sqlite3PcacheCleanAll(pPager->pPCache);
36071
+ sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
3549536072
3549636073
if( pagerUseWal(pPager) ){
36074
+ /* Drop the WAL write-lock, if any. Also, if the connection was in
36075
+ ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
36076
+ ** lock held on the database file.
36077
+ */
3549736078
rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
3549836079
assert( rc2==SQLITE_OK );
35499
- pPager->state = PAGER_SHARED;
35500
-
35501
- /* If the connection was in locking_mode=exclusive mode but is no longer,
35502
- ** drop the EXCLUSIVE lock held on the database file.
35503
- */
35504
- if( !pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, 0) ){
35505
- rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35506
- }
35507
- }else if( !pPager->exclusiveMode ){
35508
- rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35509
- pPager->state = PAGER_SHARED;
36080
+ }
36081
+ if( !pPager->exclusiveMode
36082
+ && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
36083
+ ){
36084
+ rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
3551036085
pPager->changeCountDone = 0;
35511
- }else if( pPager->state==PAGER_SYNCED ){
35512
- pPager->state = PAGER_EXCLUSIVE;
35513
- }
35514
- pPager->setMaster = 0;
35515
- pPager->needSync = 0;
35516
- pPager->dbModified = 0;
35517
-
35518
- /* TODO: Is this optimal? Why is the db size invalidated here
35519
- ** when the database file is not unlocked? */
35520
- pPager->dbOrigSize = 0;
35521
- sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
35522
- if( !MEMDB ){
35523
- pPager->dbSizeValid = 0;
35524
- }
36086
+ }
36087
+ pPager->eState = PAGER_READER;
36088
+ pPager->setMaster = 0;
3552536089
3552636090
return (rc==SQLITE_OK?rc2:rc);
3552736091
}
36092
+
36093
+/*
36094
+** Execute a rollback if a transaction is active and unlock the
36095
+** database file.
36096
+**
36097
+** If the pager has already entered the ERROR state, do not attempt
36098
+** the rollback at this time. Instead, pager_unlock() is called. The
36099
+** call to pager_unlock() will discard all in-memory pages, unlock
36100
+** the database file and move the pager back to OPEN state. If this
36101
+** means that there is a hot-journal left in the file-system, the next
36102
+** connection to obtain a shared lock on the pager (which may be this one)
36103
+** will roll it back.
36104
+**
36105
+** If the pager has not already entered the ERROR state, but an IO or
36106
+** malloc error occurs during a rollback, then this will itself cause
36107
+** the pager to enter the ERROR state. Which will be cleared by the
36108
+** call to pager_unlock(), as described above.
36109
+*/
36110
+static void pagerUnlockAndRollback(Pager *pPager){
36111
+ if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
36112
+ assert( assert_pager_state(pPager) );
36113
+ if( pPager->eState>=PAGER_WRITER_LOCKED ){
36114
+ sqlite3BeginBenignMalloc();
36115
+ sqlite3PagerRollback(pPager);
36116
+ sqlite3EndBenignMalloc();
36117
+ }else if( !pPager->exclusiveMode ){
36118
+ assert( pPager->eState==PAGER_READER );
36119
+ pager_end_transaction(pPager, 0);
36120
+ }
36121
+ }
36122
+ pager_unlock(pPager);
36123
+}
3552836124
3552936125
/*
3553036126
** Parameter aData must point to a buffer of pPager->pageSize bytes
3553136127
** of data. Compute and return a checksum based ont the contents of the
3553236128
** page of data and the current value of pPager->cksumInit.
@@ -35574,13 +36170,12 @@
3557436170
** Read a single page from either the journal file (if isMainJrnl==1) or
3557536171
** from the sub-journal (if isMainJrnl==0) and playback that page.
3557636172
** The page begins at offset *pOffset into the file. The *pOffset
3557736173
** value is increased to the start of the next page in the journal.
3557836174
**
35579
-** The isMainJrnl flag is true if this is the main rollback journal and
35580
-** false for the statement journal. The main rollback journal uses
35581
-** checksums - the statement journal does not.
36175
+** The main rollback journal uses checksums - the statement journal does
36176
+** not.
3558236177
**
3558336178
** If the page number of the page record read from the (sub-)journal file
3558436179
** is greater than the current value of Pager.dbSize, then playback is
3558536180
** skipped and SQLITE_OK is returned.
3558636181
**
@@ -35629,10 +36224,21 @@
3562936224
assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
3563036225
3563136226
aData = pPager->pTmpSpace;
3563236227
assert( aData ); /* Temp storage must have already been allocated */
3563336228
assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
36229
+
36230
+ /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
36231
+ ** or savepoint rollback done at the request of the caller) or this is
36232
+ ** a hot-journal rollback. If it is a hot-journal rollback, the pager
36233
+ ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
36234
+ ** only reads from the main journal, not the sub-journal.
36235
+ */
36236
+ assert( pPager->eState>=PAGER_WRITER_CACHEMOD
36237
+ || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
36238
+ );
36239
+ assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
3563436240
3563536241
/* Read the page number and page data from the journal or sub-journal
3563636242
** file. Return an error code to the caller if an IO error occurs.
3563736243
*/
3563836244
jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -35666,20 +36272,19 @@
3566636272
** rollback, then don't bother to play it back again.
3566736273
*/
3566836274
if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
3566936275
return rc;
3567036276
}
35671
- assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
3567236277
3567336278
/* When playing back page 1, restore the nReserve setting
3567436279
*/
3567536280
if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
3567636281
pPager->nReserve = ((u8*)aData)[20];
3567736282
pagerReportSize(pPager);
3567836283
}
3567936284
35680
- /* If the pager is in RESERVED state, then there must be a copy of this
36285
+ /* If the pager is in CACHEMOD state, then there must be a copy of this
3568136286
** page in the pager cache. In this case just update the pager cache,
3568236287
** not the database file. The page is left marked dirty in this case.
3568336288
**
3568436289
** An exception to the above rule: If the database is in no-sync mode
3568536290
** and a page is moved during an incremental vacuum then the page may
@@ -35686,12 +36291,15 @@
3568636291
** not be in the pager cache. Later: if a malloc() or IO error occurs
3568736292
** during a Movepage() call, then the page may not be in the cache
3568836293
** either. So the condition described in the above paragraph is not
3568936294
** assert()able.
3569036295
**
35691
- ** If in EXCLUSIVE state, then we update the pager cache if it exists
35692
- ** and the main file. The page is then marked not dirty.
36296
+ ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
36297
+ ** pager cache if it exists and the main file. The page is then marked
36298
+ ** not dirty. Since this code is only executed in PAGER_OPEN state for
36299
+ ** a hot-journal rollback, it is guaranteed that the page-cache is empty
36300
+ ** if the pager is in OPEN state.
3569336301
**
3569436302
** Ticket #1171: The statement journal might contain page content that is
3569536303
** different from the page content at the start of the transaction.
3569636304
** This occurs when a page is changed prior to the start of a statement
3569736305
** then changed again within the statement. When rolling back such a
@@ -35713,21 +36321,22 @@
3571336321
pPg = 0;
3571436322
}else{
3571536323
pPg = pager_lookup(pPager, pgno);
3571636324
}
3571736325
assert( pPg || !MEMDB );
36326
+ assert( pPager->eState!=PAGER_OPEN || pPg==0 );
3571836327
PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
3571936328
PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
3572036329
(isMainJrnl?"main-journal":"sub-journal")
3572136330
));
3572236331
if( isMainJrnl ){
3572336332
isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
3572436333
}else{
3572536334
isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
3572636335
}
35727
- if( (pPager->state>=PAGER_EXCLUSIVE)
35728
- && isOpen(pPager->fd)
36336
+ if( isOpen(pPager->fd)
36337
+ && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
3572936338
&& isSynced
3573036339
){
3573136340
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
3573236341
testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
3573336342
assert( !pagerUseWal(pPager) );
@@ -35953,14 +36562,14 @@
3595336562
/*
3595436563
** This function is used to change the actual size of the database
3595536564
** file in the file-system. This only happens when committing a transaction,
3595636565
** or rolling back a transaction (including rolling back a hot-journal).
3595736566
**
35958
-** If the main database file is not open, or an exclusive lock is not
35959
-** held, this function is a no-op. Otherwise, the size of the file is
35960
-** changed to nPage pages (nPage*pPager->pageSize bytes). If the file
35961
-** on disk is currently larger than nPage pages, then use the VFS
36567
+** If the main database file is not open, or the pager is not in either
36568
+** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
36569
+** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
36570
+** If the file on disk is currently larger than nPage pages, then use the VFS
3596236571
** xTruncate() method to truncate it.
3596336572
**
3596436573
** Or, it might might be the case that the file on disk is smaller than
3596536574
** nPage pages. Some operating system implementations can get confused if
3596636575
** you try to truncate a file to some size that is larger than it
@@ -35970,12 +36579,18 @@
3597036579
** If successful, return SQLITE_OK. If an IO error occurs while modifying
3597136580
** the database file, return the error code to the caller.
3597236581
*/
3597336582
static int pager_truncate(Pager *pPager, Pgno nPage){
3597436583
int rc = SQLITE_OK;
35975
- if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){
36584
+ assert( pPager->eState!=PAGER_ERROR );
36585
+ assert( pPager->eState!=PAGER_READER );
36586
+
36587
+ if( isOpen(pPager->fd)
36588
+ && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
36589
+ ){
3597636590
i64 currentSize, newSize;
36591
+ assert( pPager->eLock==EXCLUSIVE_LOCK );
3597736592
/* TODO: Is it safe to use Pager.dbFileSize here? */
3597836593
rc = sqlite3OsFileSize(pPager->fd, &currentSize);
3597936594
newSize = pPager->pageSize*(i64)nPage;
3598036595
if( rc==SQLITE_OK && currentSize!=newSize ){
3598136596
if( currentSize>newSize ){
@@ -36095,11 +36710,11 @@
3609536710
/* Figure out how many records are in the journal. Abort early if
3609636711
** the journal is empty.
3609736712
*/
3609836713
assert( isOpen(pPager->jfd) );
3609936714
rc = sqlite3OsFileSize(pPager->jfd, &szJ);
36100
- if( rc!=SQLITE_OK || szJ==0 ){
36715
+ if( rc!=SQLITE_OK ){
3610136716
goto end_playback;
3610236717
}
3610336718
3610436719
/* Read the master journal name from the journal, if it is present.
3610536720
** If a master journal file name is specified, but the file is not
@@ -36129,11 +36744,11 @@
3612936744
** occurs.
3613036745
*/
3613136746
while( 1 ){
3613236747
/* Read the next journal header from the journal file. If there are
3613336748
** not enough bytes left in the journal file for a complete header, or
36134
- ** it is corrupted, then a process must of failed while writing it.
36749
+ ** it is corrupted, then a process must have failed while writing it.
3613536750
** This indicates nothing more needs to be rolled back.
3613636751
*/
3613736752
rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
3613836753
if( rc!=SQLITE_OK ){
3613936754
if( rc==SQLITE_DONE ){
@@ -36243,14 +36858,13 @@
3624336858
if( rc==SQLITE_OK ){
3624436859
zMaster = pPager->pTmpSpace;
3624536860
rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
3624636861
testcase( rc!=SQLITE_OK );
3624736862
}
36248
- if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
36249
- rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
36250
- }
36251
- if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
36863
+ if( rc==SQLITE_OK && !pPager->noSync
36864
+ && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
36865
+ ){
3625236866
rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3625336867
}
3625436868
if( rc==SQLITE_OK ){
3625536869
rc = pager_end_transaction(pPager, zMaster[0]!='\0');
3625636870
testcase( rc!=SQLITE_OK );
@@ -36288,11 +36902,11 @@
3628836902
Pgno pgno = pPg->pgno; /* Page number to read */
3628936903
int rc = SQLITE_OK; /* Return code */
3629036904
int isInWal = 0; /* True if page is in log file */
3629136905
int pgsz = pPager->pageSize; /* Number of bytes to read */
3629236906
36293
- assert( pPager->state>=PAGER_SHARED && !MEMDB );
36907
+ assert( pPager->eState>=PAGER_READER && !MEMDB );
3629436908
assert( isOpen(pPager->fd) );
3629536909
3629636910
if( NEVER(!isOpen(pPager->fd)) ){
3629736911
assert( pPager->tempFile );
3629836912
memset(pPg->pData, 0, pPager->pageSize);
@@ -36451,31 +37065,82 @@
3645137065
static int pagerBeginReadTransaction(Pager *pPager){
3645237066
int rc; /* Return code */
3645337067
int changed = 0; /* True if cache must be reset */
3645437068
3645537069
assert( pagerUseWal(pPager) );
37070
+ assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
3645637071
3645737072
/* sqlite3WalEndReadTransaction() was not called for the previous
3645837073
** transaction in locking_mode=EXCLUSIVE. So call it now. If we
3645937074
** are in locking_mode=NORMAL and EndRead() was previously called,
3646037075
** the duplicate call is harmless.
3646137076
*/
3646237077
sqlite3WalEndReadTransaction(pPager->pWal);
3646337078
3646437079
rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
36465
- if( rc==SQLITE_OK ){
36466
- int dummy;
36467
- if( changed ){
36468
- pager_reset(pPager);
36469
- assert( pPager->errCode || pPager->dbSizeValid==0 );
36470
- }
36471
- rc = sqlite3PagerPagecount(pPager, &dummy);
36472
- }
36473
- pPager->state = PAGER_SHARED;
37080
+ if( rc==SQLITE_OK && changed ){
37081
+ pager_reset(pPager);
37082
+ }
3647437083
3647537084
return rc;
3647637085
}
37086
+
37087
+/*
37088
+** This function is called as part of the transition from PAGER_OPEN
37089
+** to PAGER_READER state to determine the size of the database file
37090
+** in pages (assuming the page size currently stored in Pager.pageSize).
37091
+**
37092
+** If no error occurs, SQLITE_OK is returned and the size of the database
37093
+** in pages is stored in *pnPage. Otherwise, an error code (perhaps
37094
+** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
37095
+*/
37096
+static int pagerPagecount(Pager *pPager, Pgno *pnPage){
37097
+ Pgno nPage; /* Value to return via *pnPage */
37098
+
37099
+ /* Query the WAL sub-system for the database size. The WalDbsize()
37100
+ ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
37101
+ ** if the database size is not available. The database size is not
37102
+ ** available from the WAL sub-system if the log file is empty or
37103
+ ** contains no valid committed transactions.
37104
+ */
37105
+ assert( pPager->eState==PAGER_OPEN );
37106
+ assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
37107
+ nPage = sqlite3WalDbsize(pPager->pWal);
37108
+
37109
+ /* If the database size was not available from the WAL sub-system,
37110
+ ** determine it based on the size of the database file. If the size
37111
+ ** of the database file is not an integer multiple of the page-size,
37112
+ ** round down to the nearest page. Except, any file larger than 0
37113
+ ** bytes in size is considered to contain at least one page.
37114
+ */
37115
+ if( nPage==0 ){
37116
+ i64 n = 0; /* Size of db file in bytes */
37117
+ assert( isOpen(pPager->fd) || pPager->tempFile );
37118
+ if( isOpen(pPager->fd) ){
37119
+ int rc = sqlite3OsFileSize(pPager->fd, &n);
37120
+ if( rc!=SQLITE_OK ){
37121
+ return rc;
37122
+ }
37123
+ }
37124
+ nPage = (Pgno)(n / pPager->pageSize);
37125
+ if( nPage==0 && n>0 ){
37126
+ nPage = 1;
37127
+ }
37128
+ }
37129
+
37130
+ /* If the current number of pages in the file is greater than the
37131
+ ** configured maximum pager number, increase the allowed limit so
37132
+ ** that the file can be read.
37133
+ */
37134
+ if( nPage>pPager->mxPgno ){
37135
+ pPager->mxPgno = (Pgno)nPage;
37136
+ }
37137
+
37138
+ *pnPage = nPage;
37139
+ return SQLITE_OK;
37140
+}
37141
+
3647737142
3647837143
/*
3647937144
** Check if the *-wal file that corresponds to the database opened by pPager
3648037145
** exists if the database is not empy, or verify that the *-wal file does
3648137146
** not exist (by deleting it) if the database file is empty.
@@ -36485,25 +37150,26 @@
3648537150
** if no error occurs, make sure Pager.journalMode is not set to
3648637151
** PAGER_JOURNALMODE_WAL.
3648737152
**
3648837153
** Return SQLITE_OK or an error code.
3648937154
**
36490
-** If the WAL file is opened, also open a snapshot (read transaction).
36491
-**
3649237155
** The caller must hold a SHARED lock on the database file to call this
3649337156
** function. Because an EXCLUSIVE lock on the db file is required to delete
3649437157
** a WAL on a none-empty database, this ensures there is no race condition
3649537158
** between the xAccess() below and an xDelete() being executed by some
3649637159
** other connection.
3649737160
*/
3649837161
static int pagerOpenWalIfPresent(Pager *pPager){
3649937162
int rc = SQLITE_OK;
37163
+ assert( pPager->eState==PAGER_OPEN );
37164
+ assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
37165
+
3650037166
if( !pPager->tempFile ){
3650137167
int isWal; /* True if WAL file exists */
36502
- int nPage; /* Size of the database file */
36503
- assert( pPager->state>=SHARED_LOCK );
36504
- rc = sqlite3PagerPagecount(pPager, &nPage);
37168
+ Pgno nPage; /* Size of the database file */
37169
+
37170
+ rc = pagerPagecount(pPager, &nPage);
3650537171
if( rc ) return rc;
3650637172
if( nPage==0 ){
3650737173
rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
3650837174
isWal = 0;
3650937175
}else{
@@ -36511,15 +37177,12 @@
3651137177
pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
3651237178
);
3651337179
}
3651437180
if( rc==SQLITE_OK ){
3651537181
if( isWal ){
36516
- pager_reset(pPager);
37182
+ testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
3651737183
rc = sqlite3PagerOpenWal(pPager, 0);
36518
- if( rc==SQLITE_OK ){
36519
- rc = pagerBeginReadTransaction(pPager);
36520
- }
3652137184
}else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
3652237185
pPager->journalMode = PAGER_JOURNALMODE_DELETE;
3652337186
}
3652437187
}
3652537188
}
@@ -36567,11 +37230,12 @@
3656737230
i64 szJ; /* Effective size of the main journal */
3656837231
i64 iHdrOff; /* End of first segment of main-journal records */
3656937232
int rc = SQLITE_OK; /* Return code */
3657037233
Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
3657137234
36572
- assert( pPager->state>=PAGER_SHARED );
37235
+ assert( pPager->eState!=PAGER_ERROR );
37236
+ assert( pPager->eState>=PAGER_WRITER_LOCKED );
3657337237
3657437238
/* Allocate a bitvec to use to store the set of pages rolled back */
3657537239
if( pSavepoint ){
3657637240
pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
3657737241
if( !pDone ){
@@ -36706,11 +37370,10 @@
3670637370
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
3670737371
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
3670837372
pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
3670937373
pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
3671037374
pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
36711
- if( pPager->noSync ) pPager->needSync = 0;
3671237375
}
3671337376
#endif
3671437377
3671537378
/*
3671637379
** The following global variable is incremented whenever the library
@@ -36788,11 +37451,11 @@
3678837451
** Change the page size used by the Pager object. The new page size
3678937452
** is passed in *pPageSize.
3679037453
**
3679137454
** If the pager is in the error state when this function is called, it
3679237455
** is a no-op. The value returned is the error state error code (i.e.
36793
-** one of SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_FULL).
37456
+** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
3679437457
**
3679537458
** Otherwise, if all of the following are true:
3679637459
**
3679737460
** * the new page size (value of *pPageSize) is valid (a power
3679837461
** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
@@ -36812,32 +37475,52 @@
3681237475
** If the page size is not changed, either because one of the enumerated
3681337476
** conditions above is not true, the pager was in error state when this
3681437477
** function was called, or because the memory allocation attempt failed,
3681537478
** then *pPageSize is set to the old, retained page size before returning.
3681637479
*/
36817
-SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
36818
- int rc = pPager->errCode;
36819
-
36820
- if( rc==SQLITE_OK ){
36821
- u16 pageSize = *pPageSize;
36822
- assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
36823
- if( (pPager->memDb==0 || pPager->dbSize==0)
36824
- && sqlite3PcacheRefCount(pPager->pPCache)==0
36825
- && pageSize && pageSize!=pPager->pageSize
36826
- ){
36827
- char *pNew = (char *)sqlite3PageMalloc(pageSize);
36828
- if( !pNew ){
36829
- rc = SQLITE_NOMEM;
36830
- }else{
36831
- pager_reset(pPager);
36832
- pPager->pageSize = pageSize;
36833
- sqlite3PageFree(pPager->pTmpSpace);
36834
- pPager->pTmpSpace = pNew;
36835
- sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
36836
- }
36837
- }
36838
- *pPageSize = (u16)pPager->pageSize;
37480
+SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
37481
+ int rc = SQLITE_OK;
37482
+
37483
+ /* It is not possible to do a full assert_pager_state() here, as this
37484
+ ** function may be called from within PagerOpen(), before the state
37485
+ ** of the Pager object is internally consistent.
37486
+ **
37487
+ ** At one point this function returned an error if the pager was in
37488
+ ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
37489
+ ** there is at least one outstanding page reference, this function
37490
+ ** is a no-op for that case anyhow.
37491
+ */
37492
+
37493
+ u32 pageSize = *pPageSize;
37494
+ assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
37495
+ if( (pPager->memDb==0 || pPager->dbSize==0)
37496
+ && sqlite3PcacheRefCount(pPager->pPCache)==0
37497
+ && pageSize && pageSize!=(u32)pPager->pageSize
37498
+ ){
37499
+ char *pNew; /* New temp space */
37500
+ i64 nByte = 0;
37501
+
37502
+ if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
37503
+ rc = sqlite3OsFileSize(pPager->fd, &nByte);
37504
+ }
37505
+ if( rc==SQLITE_OK ){
37506
+ pNew = (char *)sqlite3PageMalloc(pageSize);
37507
+ if( !pNew ) rc = SQLITE_NOMEM;
37508
+ }
37509
+
37510
+ if( rc==SQLITE_OK ){
37511
+ pager_reset(pPager);
37512
+ pPager->dbSize = nByte/pageSize;
37513
+ pPager->pageSize = pageSize;
37514
+ sqlite3PageFree(pPager->pTmpSpace);
37515
+ pPager->pTmpSpace = pNew;
37516
+ sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
37517
+ }
37518
+ }
37519
+
37520
+ *pPageSize = pPager->pageSize;
37521
+ if( rc==SQLITE_OK ){
3683937522
if( nReserve<0 ) nReserve = pPager->nReserve;
3684037523
assert( nReserve>=0 && nReserve<1000 );
3684137524
pPager->nReserve = (i16)nReserve;
3684237525
pagerReportSize(pPager);
3684337526
}
@@ -36862,17 +37545,15 @@
3686237545
** maximum page count below the current size of the database.
3686337546
**
3686437547
** Regardless of mxPage, return the current maximum page count.
3686537548
*/
3686637549
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
36867
- int nPage;
3686837550
if( mxPage>0 ){
3686937551
pPager->mxPgno = mxPage;
3687037552
}
36871
- if( pPager->state!=PAGER_UNLOCK ){
36872
- sqlite3PagerPagecount(pPager, &nPage);
36873
- assert( (int)pPager->mxPgno>=nPage );
37553
+ if( pPager->eState!=PAGER_OPEN && pPager->mxPgno<pPager->dbSize ){
37554
+ pPager->mxPgno = pPager->dbSize;
3687437555
}
3687537556
return pPager->mxPgno;
3687637557
}
3687737558
3687837559
/*
@@ -36933,70 +37614,20 @@
3693337614
}
3693437615
return rc;
3693537616
}
3693637617
3693737618
/*
36938
-** Return the total number of pages in the database file associated
36939
-** with pPager. Normally, this is calculated as (<db file size>/<page-size>).
37619
+** This function may only be called when a read-transaction is open on
37620
+** the pager. It returns the total number of pages in the database.
37621
+**
3694037622
** However, if the file is between 1 and <page-size> bytes in size, then
3694137623
** this is considered a 1 page file.
36942
-**
36943
-** If the pager is in error state when this function is called, then the
36944
-** error state error code is returned and *pnPage left unchanged. Or,
36945
-** if the file system has to be queried for the size of the file and
36946
-** the query attempt returns an IO error, the IO error code is returned
36947
-** and *pnPage is left unchanged.
36948
-**
36949
-** Otherwise, if everything is successful, then SQLITE_OK is returned
36950
-** and *pnPage is set to the number of pages in the database.
36951
-*/
36952
-SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
36953
- Pgno nPage = 0; /* Value to return via *pnPage */
36954
-
36955
- /* Determine the number of pages in the file. Store this in nPage. */
36956
- if( pPager->dbSizeValid ){
36957
- nPage = pPager->dbSize;
36958
- }else{
36959
- int rc; /* Error returned by OsFileSize() */
36960
- i64 n = 0; /* File size in bytes returned by OsFileSize() */
36961
-
36962
- if( pagerUseWal(pPager) && pPager->state!=PAGER_UNLOCK ){
36963
- sqlite3WalDbsize(pPager->pWal, &nPage);
36964
- }
36965
-
36966
- if( nPage==0 ){
36967
- assert( isOpen(pPager->fd) || pPager->tempFile );
36968
- if( isOpen(pPager->fd) ){
36969
- if( SQLITE_OK!=(rc = sqlite3OsFileSize(pPager->fd, &n)) ){
36970
- pager_error(pPager, rc);
36971
- return rc;
36972
- }
36973
- }
36974
- if( n>0 && n<pPager->pageSize ){
36975
- nPage = 1;
36976
- }else{
36977
- nPage = (Pgno)(n / pPager->pageSize);
36978
- }
36979
- }
36980
- if( pPager->state!=PAGER_UNLOCK ){
36981
- pPager->dbSize = nPage;
36982
- pPager->dbFileSize = nPage;
36983
- pPager->dbSizeValid = 1;
36984
- }
36985
- }
36986
-
36987
- /* If the current number of pages in the file is greater than the
36988
- ** configured maximum pager number, increase the allowed limit so
36989
- ** that the file can be read.
36990
- */
36991
- if( nPage>pPager->mxPgno ){
36992
- pPager->mxPgno = (Pgno)nPage;
36993
- }
36994
-
36995
- /* Set the output variable and return SQLITE_OK */
36996
- *pnPage = nPage;
36997
- return SQLITE_OK;
37624
+*/
37625
+SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
37626
+ assert( pPager->eState>=PAGER_READER );
37627
+ assert( pPager->eState!=PAGER_WRITER_FINISHED );
37628
+ *pnPage = (int)pPager->dbSize;
3699837629
}
3699937630
3700037631
3700137632
/*
3700237633
** Try to obtain a lock of type locktype on the database file. If
@@ -37013,42 +37644,23 @@
3701337644
** variable to locktype before returning.
3701437645
*/
3701537646
static int pager_wait_on_lock(Pager *pPager, int locktype){
3701637647
int rc; /* Return code */
3701737648
37018
- /* The OS lock values must be the same as the Pager lock values */
37019
- assert( PAGER_SHARED==SHARED_LOCK );
37020
- assert( PAGER_RESERVED==RESERVED_LOCK );
37021
- assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
37022
-
37023
- /* If the file is currently unlocked then the size must be unknown. It
37024
- ** must not have been modified at this point.
37025
- */
37026
- assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
37027
- assert( pPager->state>=PAGER_SHARED || pPager->dbModified==0 );
37028
-
3702937649
/* Check that this is either a no-op (because the requested lock is
3703037650
** already held, or one of the transistions that the busy-handler
3703137651
** may be invoked during, according to the comment above
3703237652
** sqlite3PagerSetBusyhandler().
3703337653
*/
37034
- assert( (pPager->state>=locktype)
37035
- || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED)
37036
- || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE)
37654
+ assert( (pPager->eLock>=locktype)
37655
+ || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
37656
+ || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
3703737657
);
3703837658
37039
- if( pPager->state>=locktype ){
37040
- rc = SQLITE_OK;
37041
- }else{
37042
- do {
37043
- rc = sqlite3OsLock(pPager->fd, locktype);
37044
- }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
37045
- if( rc==SQLITE_OK ){
37046
- pPager->state = (u8)locktype;
37047
- IOTRACE(("LOCK %p %d\n", pPager, locktype))
37048
- }
37049
- }
37659
+ do {
37660
+ rc = pagerLockDb(pPager, locktype);
37661
+ }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
3705037662
return rc;
3705137663
}
3705237664
3705337665
/*
3705437666
** Function assertTruncateConstraint(pPager) checks that one of the
@@ -37089,13 +37701,12 @@
3708937701
** function does not actually modify the database file on disk. It
3709037702
** just sets the internal state of the pager object so that the
3709137703
** truncation will be done when the current transaction is committed.
3709237704
*/
3709337705
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
37094
- assert( pPager->dbSizeValid );
3709537706
assert( pPager->dbSize>=nPage );
37096
- assert( pPager->state>=PAGER_RESERVED );
37707
+ assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
3709737708
pPager->dbSize = nPage;
3709837709
assertTruncateConstraint(pPager);
3709937710
}
3710037711
3710137712
@@ -37141,11 +37752,11 @@
3714137752
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
3714237753
u8 *pTmp = (u8 *)pPager->pTmpSpace;
3714337754
3714437755
disable_simulated_io_errors();
3714537756
sqlite3BeginBenignMalloc();
37146
- pPager->errCode = 0;
37757
+ /* pPager->errCode = 0; */
3714737758
pPager->exclusiveMode = 0;
3714837759
#ifndef SQLITE_OMIT_WAL
3714937760
sqlite3WalClose(pPager->pWal,
3715037761
(pPager->noSync ? 0 : pPager->sync_flags),
3715137762
pPager->pageSize, pTmp
@@ -37154,18 +37765,23 @@
3715437765
#endif
3715537766
pager_reset(pPager);
3715637767
if( MEMDB ){
3715737768
pager_unlock(pPager);
3715837769
}else{
37159
- /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
37160
- ** call which may be made from within pagerUnlockAndRollback(). If it
37161
- ** is not -1, then the unsynced portion of an open journal file may
37162
- ** be played back into the database. If a power failure occurs while
37163
- ** this is happening, the database may become corrupt.
37770
+ /* If it is open, sync the journal file before calling UnlockAndRollback.
37771
+ ** If this is not done, then an unsynced portion of the open journal
37772
+ ** file may be played back into the database. If a power failure occurs
37773
+ ** while this is happening, the database could become corrupt.
37774
+ **
37775
+ ** If an error occurs while trying to sync the journal, shift the pager
37776
+ ** into the ERROR state. This causes UnlockAndRollback to unlock the
37777
+ ** database and close the journal file without attempting to roll it
37778
+ ** back or finalize it. The next database user will have to do hot-journal
37779
+ ** rollback before accessing the database file.
3716437780
*/
3716537781
if( isOpen(pPager->jfd) ){
37166
- pPager->errCode = pagerSyncHotJournal(pPager);
37782
+ pager_error(pPager, pagerSyncHotJournal(pPager));
3716737783
}
3716837784
pagerUnlockAndRollback(pPager);
3716937785
}
3717037786
sqlite3EndBenignMalloc();
3717137787
enable_simulated_io_errors();
@@ -37206,13 +37822,13 @@
3720637822
/*
3720737823
** Sync the journal. In other words, make sure all the pages that have
3720837824
** been written to the journal have actually reached the surface of the
3720937825
** disk and can be restored in the event of a hot-journal rollback.
3721037826
**
37211
-** If the Pager.needSync flag is not set, then this function is a
37212
-** no-op. Otherwise, the actions required depend on the journal-mode
37213
-** and the device characteristics of the the file-system, as follows:
37827
+** If the Pager.noSync flag is set, then this function is a no-op.
37828
+** Otherwise, the actions required depend on the journal-mode and the
37829
+** device characteristics of the the file-system, as follows:
3721437830
**
3721537831
** * If the journal file is an in-memory journal file, no action need
3721637832
** be taken.
3721737833
**
3721837834
** * Otherwise, if the device does not support the SAFE_APPEND property,
@@ -37232,22 +37848,29 @@
3723237848
** <update nRec field>
3723337849
** }
3723437850
** if( NOT SEQUENTIAL ) xSync(<journal file>);
3723537851
** }
3723637852
**
37237
-** The Pager.needSync flag is never be set for temporary files, or any
37238
-** file operating in no-sync mode (Pager.noSync set to non-zero).
37239
-**
3724037853
** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
3724137854
** page currently held in memory before returning SQLITE_OK. If an IO
3724237855
** error is encountered, then the IO error code is returned to the caller.
3724337856
*/
37244
-static int syncJournal(Pager *pPager){
37245
- if( pPager->needSync ){
37857
+static int syncJournal(Pager *pPager, int newHdr){
37858
+ int rc; /* Return code */
37859
+
37860
+ assert( pPager->eState==PAGER_WRITER_CACHEMOD
37861
+ || pPager->eState==PAGER_WRITER_DBMOD
37862
+ );
37863
+ assert( assert_pager_state(pPager) );
37864
+ assert( !pagerUseWal(pPager) );
37865
+
37866
+ rc = sqlite3PagerExclusiveLock(pPager);
37867
+ if( rc!=SQLITE_OK ) return rc;
37868
+
37869
+ if( !pPager->noSync ){
3724637870
assert( !pPager->tempFile );
37247
- if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
37248
- int rc; /* Return code */
37871
+ if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
3724937872
const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
3725037873
assert( isOpen(pPager->jfd) );
3725137874
3725237875
if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
3725337876
/* This block deals with an obscure problem. If the last connection
@@ -37318,21 +37941,29 @@
3731837941
rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
3731937942
(pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
3732037943
);
3732137944
if( rc!=SQLITE_OK ) return rc;
3732237945
}
37323
- }
37324
-
37325
- /* The journal file was just successfully synced. Set Pager.needSync
37326
- ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
37327
- */
37328
- pPager->needSync = 0;
37329
- pPager->journalStarted = 1;
37330
- pPager->journalHdr = pPager->journalOff;
37331
- sqlite3PcacheClearSyncFlags(pPager->pPCache);
37946
+
37947
+ pPager->journalHdr = pPager->journalOff;
37948
+ if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
37949
+ pPager->nRec = 0;
37950
+ rc = writeJournalHdr(pPager);
37951
+ if( rc!=SQLITE_OK ) return rc;
37952
+ }
37953
+ }else{
37954
+ pPager->journalHdr = pPager->journalOff;
37955
+ }
3733237956
}
3733337957
37958
+ /* Unless the pager is in noSync mode, the journal file was just
37959
+ ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
37960
+ ** all pages.
37961
+ */
37962
+ sqlite3PcacheClearSyncFlags(pPager->pPCache);
37963
+ pPager->eState = PAGER_WRITER_DBMOD;
37964
+ assert( assert_pager_state(pPager) );
3733437965
return SQLITE_OK;
3733537966
}
3733637967
3733737968
/*
3733837969
** The argument is the first in a linked list of dirty pages connected
@@ -37365,31 +37996,16 @@
3736537996
** If everything is successful, SQLITE_OK is returned. If an IO error
3736637997
** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
3736737998
** be obtained, SQLITE_BUSY is returned.
3736837999
*/
3736938000
static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
37370
- int rc; /* Return code */
37371
-
37372
- /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
37373
- ** database file. If there is already an EXCLUSIVE lock, the following
37374
- ** call is a no-op.
37375
- **
37376
- ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
37377
- ** through an intermediate state PENDING. A PENDING lock prevents new
37378
- ** readers from attaching to the database but is unsufficient for us to
37379
- ** write. The idea of a PENDING lock is to prevent new readers from
37380
- ** coming in while we wait for existing readers to clear.
37381
- **
37382
- ** While the pager is in the RESERVED state, the original database file
37383
- ** is unchanged and we can rollback without having to playback the
37384
- ** journal into the original database file. Once we transition to
37385
- ** EXCLUSIVE, it means the database file has been changed and any rollback
37386
- ** will require a journal playback.
37387
- */
38001
+ int rc = SQLITE_OK; /* Return code */
38002
+
38003
+ /* This function is only called for rollback pagers in WRITER_DBMOD state. */
3738838004
assert( !pagerUseWal(pPager) );
37389
- assert( pPager->state>=PAGER_RESERVED );
37390
- rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38005
+ assert( pPager->eState==PAGER_WRITER_DBMOD );
38006
+ assert( pPager->eLock==EXCLUSIVE_LOCK );
3739138007
3739238008
/* If the file is a temp-file has not yet been opened, open it now. It
3739338009
** is not possible for rc to be other than SQLITE_OK if this branch
3739438010
** is taken, as pager_wait_on_lock() is a no-op for temp-files.
3739538011
*/
@@ -37400,13 +38016,14 @@
3740038016
3740138017
/* Before the first write, give the VFS a hint of what the final
3740238018
** file size will be.
3740338019
*/
3740438020
assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
37405
- if( rc==SQLITE_OK && pPager->dbSize>(pPager->dbOrigSize+1) ){
38021
+ if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
3740638022
sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
3740738023
sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
38024
+ pPager->dbHintSize = pPager->dbSize;
3740838025
}
3740938026
3741038027
while( rc==SQLITE_OK && pList ){
3741138028
Pgno pgno = pList->pgno;
3741238029
@@ -37419,10 +38036,12 @@
3741938036
** set (set by sqlite3PagerDontWrite()).
3742038037
*/
3742138038
if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
3742238039
i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
3742338040
char *pData; /* Data to write */
38041
+
38042
+ assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
3742438043
3742538044
/* Encode the database */
3742638045
CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
3742738046
3742838047
/* Write out the page data. */
@@ -37562,13 +38181,18 @@
3756238181
** pages belonging to the same sector.
3756338182
**
3756438183
** The doNotSpill flag inhibits all cache spilling regardless of whether
3756538184
** or not a sync is required. This is set during a rollback.
3756638185
**
37567
- ** Spilling is also inhibited when in an error state.
38186
+ ** Spilling is also prohibited when in an error state since that could
38187
+ ** lead to database corruption. In the current implementaton it
38188
+ ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
38189
+ ** while in the error state, hence it is impossible for this routine to
38190
+ ** be called in the error state. Nevertheless, we include a NEVER()
38191
+ ** test for the error state as a safeguard against future changes.
3756838192
*/
37569
- if( pPager->errCode ) return SQLITE_OK;
38193
+ if( NEVER(pPager->errCode) ) return SQLITE_OK;
3757038194
if( pPager->doNotSpill ) return SQLITE_OK;
3757138195
if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
3757238196
return SQLITE_OK;
3757338197
}
3757438198
@@ -37582,20 +38206,14 @@
3758238206
rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
3758338207
}
3758438208
}else{
3758538209
3758638210
/* Sync the journal file if required. */
37587
- if( pPg->flags&PGHDR_NEED_SYNC ){
37588
- assert( !pPager->noSync );
37589
- rc = syncJournal(pPager);
37590
- if( rc==SQLITE_OK &&
37591
- !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37592
- !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37593
- ){
37594
- pPager->nRec = 0;
37595
- rc = writeJournalHdr(pPager);
37596
- }
38211
+ if( pPg->flags&PGHDR_NEED_SYNC
38212
+ || pPager->eState==PAGER_WRITER_CACHEMOD
38213
+ ){
38214
+ rc = syncJournal(pPager, 1);
3759738215
}
3759838216
3759938217
/* If the page number of this page is larger than the current size of
3760038218
** the database image, it may need to be written to the sub-journal.
3760138219
** This is because the call to pager_write_pagelist() below will not
@@ -37629,10 +38247,11 @@
3762938247
rc = subjournalPage(pPg);
3763038248
}
3763138249
3763238250
/* Write the contents of the page out to the database file. */
3763338251
if( rc==SQLITE_OK ){
38252
+ assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
3763438253
rc = pager_write_pagelist(pPager, pPg);
3763538254
}
3763638255
}
3763738256
3763838257
/* Mark the page as clean. */
@@ -37639,11 +38258,11 @@
3763938258
if( rc==SQLITE_OK ){
3764038259
PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
3764138260
sqlite3PcacheMakeClean(pPg);
3764238261
}
3764338262
37644
- return pager_error(pPager, rc);
38263
+ return pager_error(pPager, rc);
3764538264
}
3764638265
3764738266
3764838267
/*
3764938268
** Allocate and initialize a new Pager object and put a pointer to it
@@ -37694,11 +38313,11 @@
3769438313
char *zPathname = 0; /* Full path to database file */
3769538314
int nPathname = 0; /* Number of bytes in zPathname */
3769638315
int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
3769738316
int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
3769838317
int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
37699
- u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
38318
+ u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
3770038319
3770138320
/* Figure out how much space is required for each journal file-handle
3770238321
** (there are two of them, the main journal and the sub-journal). This
3770338322
** is the maximum space required for an in-memory journal file handle
3770438323
** and a regular journal file-handle. Note that a "regular journal-handle"
@@ -37829,11 +38448,11 @@
3782938448
assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
3783038449
if( szPageDflt<pPager->sectorSize ){
3783138450
if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
3783238451
szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
3783338452
}else{
37834
- szPageDflt = (u16)pPager->sectorSize;
38453
+ szPageDflt = (u32)pPager->sectorSize;
3783538454
}
3783638455
}
3783738456
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3783838457
{
3783938458
int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
@@ -37857,11 +38476,12 @@
3785738476
** This branch is also run for an in-memory database. An in-memory
3785838477
** database is the same as a temp-file that is never written out to
3785938478
** disk and uses an in-memory rollback journal.
3786038479
*/
3786138480
tempFile = 1;
37862
- pPager->state = PAGER_EXCLUSIVE;
38481
+ pPager->eState = PAGER_READER;
38482
+ pPager->eLock = EXCLUSIVE_LOCK;
3786338483
readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
3786438484
}
3786538485
3786638486
/* The following call to PagerSetPagesize() serves to set the value of
3786738487
** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -37894,27 +38514,27 @@
3789438514
pPager->useJournal = (u8)useJournal;
3789538515
pPager->noReadlock = (noReadlock && readOnly) ?1:0;
3789638516
/* pPager->stmtOpen = 0; */
3789738517
/* pPager->stmtInUse = 0; */
3789838518
/* pPager->nRef = 0; */
37899
- pPager->dbSizeValid = (u8)memDb;
3790038519
/* pPager->stmtSize = 0; */
3790138520
/* pPager->stmtJSize = 0; */
3790238521
/* pPager->nPage = 0; */
3790338522
pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
3790438523
/* pPager->state = PAGER_UNLOCK; */
38524
+#if 0
3790538525
assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
38526
+#endif
3790638527
/* pPager->errMask = 0; */
3790738528
pPager->tempFile = (u8)tempFile;
3790838529
assert( tempFile==PAGER_LOCKINGMODE_NORMAL
3790938530
|| tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
3791038531
assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
3791138532
pPager->exclusiveMode = (u8)tempFile;
3791238533
pPager->changeCountDone = pPager->tempFile;
3791338534
pPager->memDb = (u8)memDb;
3791438535
pPager->readOnly = (u8)readOnly;
37915
- /* pPager->needSync = 0; */
3791638536
assert( useJournal || pPager->tempFile );
3791738537
pPager->noSync = pPager->tempFile;
3791838538
pPager->fullSync = pPager->noSync ?0:1;
3791938539
pPager->sync_flags = SQLITE_SYNC_NORMAL;
3792038540
/* pPager->pFirst = 0; */
@@ -37975,24 +38595,24 @@
3797538595
sqlite3_vfs * const pVfs = pPager->pVfs;
3797638596
int rc = SQLITE_OK; /* Return code */
3797738597
int exists = 1; /* True if a journal file is present */
3797838598
int jrnlOpen = !!isOpen(pPager->jfd);
3797938599
37980
- assert( pPager!=0 );
3798138600
assert( pPager->useJournal );
3798238601
assert( isOpen(pPager->fd) );
37983
- assert( pPager->state <= PAGER_SHARED );
38602
+ assert( pPager->eState==PAGER_OPEN );
38603
+
3798438604
assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
3798538605
SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
3798638606
));
3798738607
3798838608
*pExists = 0;
3798938609
if( !jrnlOpen ){
3799038610
rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
3799138611
}
3799238612
if( rc==SQLITE_OK && exists ){
37993
- int locked; /* True if some process holds a RESERVED lock */
38613
+ int locked = 0; /* True if some process holds a RESERVED lock */
3799438614
3799538615
/* Race condition here: Another process might have been holding the
3799638616
** the RESERVED lock and have a journal open at the sqlite3OsAccess()
3799738617
** call above, but then delete the journal and drop the lock before
3799838618
** we get to the following sqlite3OsCheckReservedLock() call. If that
@@ -38000,25 +38620,25 @@
3800038620
** in fact there is none. This results in a false-positive which will
3800138621
** be dealt with by the playback routine. Ticket #3883.
3800238622
*/
3800338623
rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
3800438624
if( rc==SQLITE_OK && !locked ){
38005
- int nPage;
38625
+ Pgno nPage; /* Number of pages in database file */
3800638626
3800738627
/* Check the size of the database file. If it consists of 0 pages,
3800838628
** then delete the journal file. See the header comment above for
3800938629
** the reasoning here. Delete the obsolete journal file under
3801038630
** a RESERVED lock to avoid race conditions and to avoid violating
3801138631
** [H33020].
3801238632
*/
38013
- rc = sqlite3PagerPagecount(pPager, &nPage);
38633
+ rc = pagerPagecount(pPager, &nPage);
3801438634
if( rc==SQLITE_OK ){
3801538635
if( nPage==0 ){
3801638636
sqlite3BeginBenignMalloc();
38017
- if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){
38637
+ if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
3801838638
sqlite3OsDelete(pVfs, pPager->zJournal, 0);
38019
- sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
38639
+ pagerUnlockDb(pPager, SHARED_LOCK);
3802038640
}
3802138641
sqlite3EndBenignMalloc();
3802238642
}else{
3802338643
/* The journal file exists and no other connection has a reserved
3802438644
** or greater lock on the database file. Now check that there is
@@ -38067,11 +38687,11 @@
3806738687
** has been successfully called. If a shared-lock is already held when
3806838688
** this function is called, it is a no-op.
3806938689
**
3807038690
** The following operations are also performed by this function.
3807138691
**
38072
-** 1) If the pager is currently in PAGER_UNLOCK state (no lock held
38692
+** 1) If the pager is currently in PAGER_OPEN state (no lock held
3807338693
** on the database file), then an attempt is made to obtain a
3807438694
** SHARED lock on the database file. Immediately after obtaining
3807538695
** the SHARED lock, the file-system is checked for a hot-journal,
3807638696
** which is played back if present. Following any hot-journal
3807738697
** rollback, the contents of the cache are validated by checking
@@ -38082,70 +38702,51 @@
3808238702
** no outstanding references to any pages, and is in the error state,
3808338703
** then an attempt is made to clear the error state by discarding
3808438704
** the contents of the page cache and rolling back any open journal
3808538705
** file.
3808638706
**
38087
-** If the operation described by (2) above is not attempted, and if the
38088
-** pager is in an error state other than SQLITE_FULL when this is called,
38089
-** the error state error code is returned. It is permitted to read the
38090
-** database when in SQLITE_FULL error state.
38091
-**
38092
-** Otherwise, if everything is successful, SQLITE_OK is returned. If an
38093
-** IO error occurs while locking the database, checking for a hot-journal
38094
-** file or rolling back a journal file, the IO error code is returned.
38707
+** If everything is successful, SQLITE_OK is returned. If an IO error
38708
+** occurs while locking the database, checking for a hot-journal file or
38709
+** rolling back a journal file, the IO error code is returned.
3809538710
*/
3809638711
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
3809738712
int rc = SQLITE_OK; /* Return code */
38098
- int isErrorReset = 0; /* True if recovering from error state */
3809938713
3810038714
/* This routine is only called from b-tree and only when there are no
38101
- ** outstanding pages */
38715
+ ** outstanding pages. This implies that the pager state should either
38716
+ ** be OPEN or READER. READER is only possible if the pager is or was in
38717
+ ** exclusive access mode.
38718
+ */
3810238719
assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
38720
+ assert( assert_pager_state(pPager) );
38721
+ assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
3810338722
if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
3810438723
38105
- /* If this database is in an error-state, now is a chance to clear
38106
- ** the error. Discard the contents of the pager-cache and rollback
38107
- ** any hot journal in the file-system.
38108
- */
38109
- if( pPager->errCode ){
38110
- if( isOpen(pPager->jfd) || pPager->zJournal ){
38111
- isErrorReset = 1;
38112
- }
38113
- pPager->errCode = SQLITE_OK;
38114
- pager_reset(pPager);
38115
- }
38116
-
38117
- if( pagerUseWal(pPager) ){
38118
- rc = pagerBeginReadTransaction(pPager);
38119
- }else if( pPager->state==PAGER_UNLOCK || isErrorReset ){
38120
- sqlite3_vfs * const pVfs = pPager->pVfs;
38121
- int isHotJournal = 0;
38724
+ if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
38725
+ int bHotJournal = 1; /* True if there exists a hot journal-file */
38726
+
3812238727
assert( !MEMDB );
38123
- assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
38124
- if( pPager->noReadlock ){
38125
- assert( pPager->readOnly );
38126
- pPager->state = PAGER_SHARED;
38127
- }else{
38728
+ assert( pPager->noReadlock==0 || pPager->readOnly );
38729
+
38730
+ if( pPager->noReadlock==0 ){
3812838731
rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3812938732
if( rc!=SQLITE_OK ){
38130
- assert( pPager->state==PAGER_UNLOCK );
38131
- return pager_error(pPager, rc);
38733
+ assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
38734
+ goto failed;
3813238735
}
3813338736
}
38134
- assert( pPager->state>=SHARED_LOCK );
3813538737
3813638738
/* If a journal file exists, and there is no RESERVED lock on the
3813738739
** database file, then it either needs to be played back or deleted.
3813838740
*/
38139
- if( !isErrorReset ){
38140
- assert( pPager->state <= PAGER_SHARED );
38141
- rc = hasHotJournal(pPager, &isHotJournal);
38142
- if( rc!=SQLITE_OK ){
38143
- goto failed;
38144
- }
38145
- }
38146
- if( isErrorReset || isHotJournal ){
38741
+ if( pPager->eLock<=SHARED_LOCK ){
38742
+ rc = hasHotJournal(pPager, &bHotJournal);
38743
+ }
38744
+ if( rc!=SQLITE_OK ){
38745
+ goto failed;
38746
+ }
38747
+ if( bHotJournal ){
3814738748
/* Get an EXCLUSIVE lock on the database file. At this point it is
3814838749
** important that a RESERVED lock is not obtained on the way to the
3814938750
** EXCLUSIVE lock. If it were, another process might open the
3815038751
** database file, detect the RESERVED lock, and conclude that the
3815138752
** database is safe to read while this process is still rolling the
@@ -38153,62 +38754,49 @@
3815338754
**
3815438755
** Because the intermediate RESERVED lock is not requested, any
3815538756
** other process attempting to access the database file will get to
3815638757
** this point in the code and fail to obtain its own EXCLUSIVE lock
3815738758
** on the database file.
38158
- */
38159
- if( pPager->state<EXCLUSIVE_LOCK ){
38160
- rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38161
- if( rc!=SQLITE_OK ){
38162
- rc = pager_error(pPager, rc);
38163
- goto failed;
38164
- }
38165
- pPager->state = PAGER_EXCLUSIVE;
38166
- }
38167
-
38168
- /* Open the journal for read/write access. This is because in
38169
- ** exclusive-access mode the file descriptor will be kept open and
38170
- ** possibly used for a transaction later on. On some systems, the
38171
- ** OsTruncate() call used in exclusive-access mode also requires
38172
- ** a read/write file handle.
38173
- */
38174
- if( !isOpen(pPager->jfd) ){
38175
- int res;
38176
- rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
38177
- if( rc==SQLITE_OK ){
38178
- if( res ){
38179
- int fout = 0;
38180
- int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
38181
- assert( !pPager->tempFile );
38182
- rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
38183
- assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38184
- if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
38185
- rc = SQLITE_CANTOPEN_BKPT;
38186
- sqlite3OsClose(pPager->jfd);
38187
- }
38188
- }else{
38189
- /* If the journal does not exist, it usually means that some
38190
- ** other connection managed to get in and roll it back before
38191
- ** this connection obtained the exclusive lock above. Or, it
38192
- ** may mean that the pager was in the error-state when this
38193
- ** function was called and the journal file does not exist. */
38194
- rc = pager_end_transaction(pPager, 0);
38195
- }
38196
- }
38197
- }
38198
- if( rc!=SQLITE_OK ){
38199
- goto failed;
38200
- }
38201
-
38202
- /* Reset the journal status fields to indicates that we have no
38203
- ** rollback journal at this time. */
38204
- pPager->journalStarted = 0;
38205
- pPager->journalOff = 0;
38206
- pPager->setMaster = 0;
38207
- pPager->journalHdr = 0;
38208
-
38209
- /* Make sure the journal file has been synced to disk. */
38759
+ **
38760
+ ** Unless the pager is in locking_mode=exclusive mode, the lock is
38761
+ ** downgraded to SHARED_LOCK before this function returns.
38762
+ */
38763
+ rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
38764
+ if( rc!=SQLITE_OK ){
38765
+ goto failed;
38766
+ }
38767
+
38768
+ /* If it is not already open and the file exists on disk, open the
38769
+ ** journal for read/write access. Write access is required because
38770
+ ** in exclusive-access mode the file descriptor will be kept open
38771
+ ** and possibly used for a transaction later on. Also, write-access
38772
+ ** is usually required to finalize the journal in journal_mode=persist
38773
+ ** mode (and also for journal_mode=truncate on some systems).
38774
+ **
38775
+ ** If the journal does not exist, it usually means that some
38776
+ ** other connection managed to get in and roll it back before
38777
+ ** this connection obtained the exclusive lock above. Or, it
38778
+ ** may mean that the pager was in the error-state when this
38779
+ ** function was called and the journal file does not exist.
38780
+ */
38781
+ if( !isOpen(pPager->jfd) ){
38782
+ sqlite3_vfs * const pVfs = pPager->pVfs;
38783
+ int bExists; /* True if journal file exists */
38784
+ rc = sqlite3OsAccess(
38785
+ pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
38786
+ if( rc==SQLITE_OK && bExists ){
38787
+ int fout = 0;
38788
+ int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
38789
+ assert( !pPager->tempFile );
38790
+ rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
38791
+ assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38792
+ if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
38793
+ rc = SQLITE_CANTOPEN_BKPT;
38794
+ sqlite3OsClose(pPager->jfd);
38795
+ }
38796
+ }
38797
+ }
3821038798
3821138799
/* Playback and delete the journal. Drop the database write
3821238800
** lock and reacquire the read lock. Purge the cache before
3821338801
** playing back the hot-journal so that we don't end up with
3821438802
** an inconsistent cache. Sync the hot journal before playing
@@ -38215,25 +38803,50 @@
3821538803
** it back since the process that crashed and left the hot journal
3821638804
** probably did not sync it and we are required to always sync
3821738805
** the journal before playing it back.
3821838806
*/
3821938807
if( isOpen(pPager->jfd) ){
38808
+ assert( rc==SQLITE_OK );
3822038809
rc = pagerSyncHotJournal(pPager);
3822138810
if( rc==SQLITE_OK ){
3822238811
rc = pager_playback(pPager, 1);
38223
- }
38224
- if( rc!=SQLITE_OK ){
38225
- rc = pager_error(pPager, rc);
38226
- goto failed;
38227
- }
38228
- }
38229
- assert( (pPager->state==PAGER_SHARED)
38230
- || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
38812
+ pPager->eState = PAGER_OPEN;
38813
+ }
38814
+ }else if( !pPager->exclusiveMode ){
38815
+ pagerUnlockDb(pPager, SHARED_LOCK);
38816
+ }
38817
+
38818
+ if( rc!=SQLITE_OK ){
38819
+ /* This branch is taken if an error occurs while trying to open
38820
+ ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
38821
+ ** pager_unlock() routine will be called before returning to unlock
38822
+ ** the file. If the unlock attempt fails, then Pager.eLock must be
38823
+ ** set to UNKNOWN_LOCK (see the comment above the #define for
38824
+ ** UNKNOWN_LOCK above for an explanation).
38825
+ **
38826
+ ** In order to get pager_unlock() to do this, set Pager.eState to
38827
+ ** PAGER_ERROR now. This is not actually counted as a transition
38828
+ ** to ERROR state in the state diagram at the top of this file,
38829
+ ** since we know that the same call to pager_unlock() will very
38830
+ ** shortly transition the pager object to the OPEN state. Calling
38831
+ ** assert_pager_state() would fail now, as it should not be possible
38832
+ ** to be in ERROR state when there are zero outstanding page
38833
+ ** references.
38834
+ */
38835
+ pager_error(pPager, rc);
38836
+ goto failed;
38837
+ }
38838
+
38839
+ assert( pPager->eState==PAGER_OPEN );
38840
+ assert( (pPager->eLock==SHARED_LOCK)
38841
+ || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
3823138842
);
3823238843
}
3823338844
38234
- if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){
38845
+ if( !pPager->tempFile
38846
+ && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
38847
+ ){
3823538848
/* The shared-lock has just been acquired on the database file
3823638849
** and there are already pages in the cache (from a previous
3823738850
** read or write transaction). Check to see if the database
3823838851
** has been modified. If the database has changed, flush the
3823938852
** cache.
@@ -38246,18 +38859,15 @@
3824638859
**
3824738860
** There is a vanishingly small chance that a change will not be
3824838861
** detected. The chance of an undetected change is so small that
3824938862
** it can be neglected.
3825038863
*/
38251
- int nPage = 0;
38864
+ Pgno nPage = 0;
3825238865
char dbFileVers[sizeof(pPager->dbFileVers)];
38253
- sqlite3PagerPagecount(pPager, &nPage);
3825438866
38255
- if( pPager->errCode ){
38256
- rc = pPager->errCode;
38257
- goto failed;
38258
- }
38867
+ rc = pagerPagecount(pPager, &nPage);
38868
+ if( rc ) goto failed;
3825938869
3826038870
if( nPage>0 ){
3826138871
IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
3826238872
rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
3826338873
if( rc!=SQLITE_OK ){
@@ -38269,22 +38879,34 @@
3826938879
3827038880
if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3827138881
pager_reset(pPager);
3827238882
}
3827338883
}
38274
- assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
3827538884
3827638885
/* If there is a WAL file in the file-system, open this database in WAL
3827738886
** mode. Otherwise, the following function call is a no-op.
3827838887
*/
3827938888
rc = pagerOpenWalIfPresent(pPager);
38889
+ assert( pPager->pWal==0 || rc==SQLITE_OK );
38890
+ }
38891
+
38892
+ if( pagerUseWal(pPager) ){
38893
+ assert( rc==SQLITE_OK );
38894
+ rc = pagerBeginReadTransaction(pPager);
38895
+ }
38896
+
38897
+ if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
38898
+ rc = pagerPagecount(pPager, &pPager->dbSize);
3828038899
}
3828138900
3828238901
failed:
3828338902
if( rc!=SQLITE_OK ){
38284
- /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
38903
+ assert( !MEMDB );
3828538904
pager_unlock(pPager);
38905
+ assert( pPager->eState==PAGER_OPEN );
38906
+ }else{
38907
+ pPager->eState = PAGER_READER;
3828638908
}
3828738909
return rc;
3828838910
}
3828938911
3829038912
/*
@@ -38294,13 +38916,11 @@
3829438916
** Except, in locking_mode=EXCLUSIVE when there is nothing to in
3829538917
** the rollback journal, the unlock is not performed and there is
3829638918
** nothing to rollback, so this routine is a no-op.
3829738919
*/
3829838920
static void pagerUnlockIfUnused(Pager *pPager){
38299
- if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
38300
- && (!pPager->exclusiveMode || pPager->journalOff>0)
38301
- ){
38921
+ if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
3830238922
pagerUnlockAndRollback(pPager);
3830338923
}
3830438924
}
3830538925
3830638926
/*
@@ -38360,20 +38980,20 @@
3836038980
int noContent /* Do not bother reading content from disk if true */
3836138981
){
3836238982
int rc;
3836338983
PgHdr *pPg;
3836438984
38985
+ assert( pPager->eState>=PAGER_READER );
3836538986
assert( assert_pager_state(pPager) );
38366
- assert( pPager->state>PAGER_UNLOCK );
3836738987
3836838988
if( pgno==0 ){
3836938989
return SQLITE_CORRUPT_BKPT;
3837038990
}
3837138991
3837238992
/* If the pager is in the error state, return an error immediately.
3837338993
** Otherwise, request the page from the PCache layer. */
38374
- if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){
38994
+ if( pPager->errCode!=SQLITE_OK ){
3837538995
rc = pPager->errCode;
3837638996
}else{
3837738997
rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
3837838998
}
3837938999
@@ -38395,11 +39015,10 @@
3839539015
return SQLITE_OK;
3839639016
3839739017
}else{
3839839018
/* The pager cache has created a new page. Its content needs to
3839939019
** be initialized. */
38400
- int nMax;
3840139020
3840239021
PAGER_INCR(pPager->nMiss);
3840339022
pPg = *ppPage;
3840439023
pPg->pPager = pPager;
3840539024
@@ -38408,16 +39027,11 @@
3840839027
if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
3840939028
rc = SQLITE_CORRUPT_BKPT;
3841039029
goto pager_acquire_err;
3841139030
}
3841239031
38413
- rc = sqlite3PagerPagecount(pPager, &nMax);
38414
- if( rc!=SQLITE_OK ){
38415
- goto pager_acquire_err;
38416
- }
38417
-
38418
- if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
39032
+ if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
3841939033
if( pgno>pPager->mxPgno ){
3842039034
rc = SQLITE_FULL;
3842139035
goto pager_acquire_err;
3842239036
}
3842339037
if( noContent ){
@@ -38464,13 +39078,11 @@
3846439078
}
3846539079
3846639080
/*
3846739081
** Acquire a page if it is already in the in-memory cache. Do
3846839082
** not read the page from disk. Return a pointer to the page,
38469
-** or 0 if the page is not in cache. Also, return 0 if the
38470
-** pager is in PAGER_UNLOCK state when this function is called,
38471
-** or if the pager is in an error state other than SQLITE_FULL.
39083
+** or 0 if the page is not in cache.
3847239084
**
3847339085
** See also sqlite3PagerGet(). The difference between this routine
3847439086
** and sqlite3PagerGet() is that _get() will go to the disk and read
3847539087
** in the page if the page is not already in cache. This routine
3847639088
** returns NULL if the page is not in cache or if a disk I/O error
@@ -38479,11 +39091,11 @@
3847939091
SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
3848039092
PgHdr *pPg = 0;
3848139093
assert( pPager!=0 );
3848239094
assert( pgno!=0 );
3848339095
assert( pPager->pPCache!=0 );
38484
- assert( pPager->state > PAGER_UNLOCK );
39096
+ assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
3848539097
sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
3848639098
return pPg;
3848739099
}
3848839100
3848939101
/*
@@ -38524,73 +39136,71 @@
3852439136
** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
3852539137
** an IO error code if opening or writing the journal file fails.
3852639138
*/
3852739139
static int pager_open_journal(Pager *pPager){
3852839140
int rc = SQLITE_OK; /* Return code */
38529
- int nPage; /* Size of database file */
3853039141
sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
3853139142
38532
- assert( pPager->state>=PAGER_RESERVED );
38533
- assert( pPager->useJournal );
38534
- assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF );
39143
+ assert( pPager->eState==PAGER_WRITER_LOCKED );
39144
+ assert( assert_pager_state(pPager) );
3853539145
assert( pPager->pInJournal==0 );
3853639146
3853739147
/* If already in the error state, this function is a no-op. But on
3853839148
** the other hand, this routine is never called if we are already in
3853939149
** an error state. */
3854039150
if( NEVER(pPager->errCode) ) return pPager->errCode;
3854139151
38542
- testcase( pPager->dbSizeValid==0 );
38543
- rc = sqlite3PagerPagecount(pPager, &nPage);
38544
- if( rc ) return rc;
38545
- pPager->pInJournal = sqlite3BitvecCreate(nPage);
38546
- if( pPager->pInJournal==0 ){
38547
- return SQLITE_NOMEM;
38548
- }
38549
-
38550
- /* Open the journal file if it is not already open. */
38551
- if( !isOpen(pPager->jfd) ){
38552
- if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
38553
- sqlite3MemJournalOpen(pPager->jfd);
38554
- }else{
38555
- const int flags = /* VFS flags to open journal file */
38556
- SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
38557
- (pPager->tempFile ?
38558
- (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
38559
- (SQLITE_OPEN_MAIN_JOURNAL)
38560
- );
38561
-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
38562
- rc = sqlite3JournalOpen(
38563
- pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
38564
- );
38565
-#else
38566
- rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
38567
-#endif
38568
- }
38569
- assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38570
- }
38571
-
38572
-
38573
- /* Write the first journal header to the journal file and open
38574
- ** the sub-journal if necessary.
38575
- */
38576
- if( rc==SQLITE_OK ){
38577
- /* TODO: Check if all of these are really required. */
38578
- pPager->dbOrigSize = pPager->dbSize;
38579
- pPager->journalStarted = 0;
38580
- pPager->needSync = 0;
38581
- pPager->nRec = 0;
38582
- pPager->journalOff = 0;
38583
- pPager->setMaster = 0;
38584
- pPager->journalHdr = 0;
38585
- rc = writeJournalHdr(pPager);
39152
+ if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
39153
+ pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
39154
+ if( pPager->pInJournal==0 ){
39155
+ return SQLITE_NOMEM;
39156
+ }
39157
+
39158
+ /* Open the journal file if it is not already open. */
39159
+ if( !isOpen(pPager->jfd) ){
39160
+ if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
39161
+ sqlite3MemJournalOpen(pPager->jfd);
39162
+ }else{
39163
+ const int flags = /* VFS flags to open journal file */
39164
+ SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
39165
+ (pPager->tempFile ?
39166
+ (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
39167
+ (SQLITE_OPEN_MAIN_JOURNAL)
39168
+ );
39169
+ #ifdef SQLITE_ENABLE_ATOMIC_WRITE
39170
+ rc = sqlite3JournalOpen(
39171
+ pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
39172
+ );
39173
+ #else
39174
+ rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
39175
+ #endif
39176
+ }
39177
+ assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
39178
+ }
39179
+
39180
+
39181
+ /* Write the first journal header to the journal file and open
39182
+ ** the sub-journal if necessary.
39183
+ */
39184
+ if( rc==SQLITE_OK ){
39185
+ /* TODO: Check if all of these are really required. */
39186
+ pPager->nRec = 0;
39187
+ pPager->journalOff = 0;
39188
+ pPager->setMaster = 0;
39189
+ pPager->journalHdr = 0;
39190
+ rc = writeJournalHdr(pPager);
39191
+ }
3858639192
}
3858739193
3858839194
if( rc!=SQLITE_OK ){
3858939195
sqlite3BitvecDestroy(pPager->pInJournal);
3859039196
pPager->pInJournal = 0;
39197
+ }else{
39198
+ assert( pPager->eState==PAGER_WRITER_LOCKED );
39199
+ pPager->eState = PAGER_WRITER_CACHEMOD;
3859139200
}
39201
+
3859239202
return rc;
3859339203
}
3859439204
3859539205
/*
3859639206
** Begin a write-transaction on the specified pager object. If a
@@ -38599,18 +39209,10 @@
3859939209
** If the exFlag argument is false, then acquire at least a RESERVED
3860039210
** lock on the database file. If exFlag is true, then acquire at least
3860139211
** an EXCLUSIVE lock. If such a lock is already held, no locking
3860239212
** functions need be called.
3860339213
**
38604
-** If this is not a temporary or in-memory file and, the journal file is
38605
-** opened if it has not been already. For a temporary file, the opening
38606
-** of the journal file is deferred until there is an actual need to
38607
-** write to the journal. TODO: Why handle temporary files differently?
38608
-**
38609
-** If the journal file is opened (or if it is already open), then a
38610
-** journal-header is written to the start of it.
38611
-**
3861239214
** If the subjInMemory argument is non-zero, then any sub-journal opened
3861339215
** within this transaction will be opened as an in-memory file. This
3861439216
** has no effect if the sub-journal is already opened (as it may be when
3861539217
** running in exclusive mode) or if the transaction does not require a
3861639218
** sub-journal. If the subjInMemory argument is zero, then any required
@@ -38617,24 +39219,24 @@
3861739219
** sub-journal is implemented in-memory if pPager is an in-memory database,
3861839220
** or using a temporary file otherwise.
3861939221
*/
3862039222
SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
3862139223
int rc = SQLITE_OK;
38622
- assert( pPager->state!=PAGER_UNLOCK );
39224
+
39225
+ if( pPager->errCode ) return pPager->errCode;
39226
+ assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
3862339227
pPager->subjInMemory = (u8)subjInMemory;
3862439228
38625
- if( pPager->state==PAGER_SHARED ){
39229
+ if( ALWAYS(pPager->eState==PAGER_READER) ){
3862639230
assert( pPager->pInJournal==0 );
38627
- assert( !MEMDB && !pPager->tempFile );
3862839231
3862939232
if( pagerUseWal(pPager) ){
3863039233
/* If the pager is configured to use locking_mode=exclusive, and an
3863139234
** exclusive lock on the database is not already held, obtain it now.
3863239235
*/
3863339236
if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
38634
- rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38635
- pPager->state = PAGER_SHARED;
39237
+ rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
3863639238
if( rc!=SQLITE_OK ){
3863739239
return rc;
3863839240
}
3863939241
sqlite3WalExclusiveMode(pPager->pWal, 1);
3864039242
}
@@ -38641,56 +39243,44 @@
3864139243
3864239244
/* Grab the write lock on the log file. If successful, upgrade to
3864339245
** PAGER_RESERVED state. Otherwise, return an error code to the caller.
3864439246
** The busy-handler is not invoked if another connection already
3864539247
** holds the write-lock. If possible, the upper layer will call it.
38646
- **
38647
- ** WAL mode sets Pager.state to PAGER_RESERVED when it has an open
38648
- ** transaction, but never to PAGER_EXCLUSIVE. This is because in
38649
- ** PAGER_EXCLUSIVE state the code to roll back savepoint transactions
38650
- ** may copy data from the sub-journal into the database file as well
38651
- ** as into the page cache. Which would be incorrect in WAL mode.
3865239248
*/
3865339249
rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
38654
- if( rc==SQLITE_OK ){
38655
- pPager->dbOrigSize = pPager->dbSize;
38656
- pPager->state = PAGER_RESERVED;
38657
- pPager->journalOff = 0;
38658
- }
38659
-
38660
- assert( rc!=SQLITE_OK || pPager->state==PAGER_RESERVED );
38661
- assert( rc==SQLITE_OK || pPager->state==PAGER_SHARED );
3866239250
}else{
3866339251
/* Obtain a RESERVED lock on the database file. If the exFlag parameter
3866439252
** is true, then immediately upgrade this to an EXCLUSIVE lock. The
3866539253
** busy-handler callback can be used when upgrading to the EXCLUSIVE
3866639254
** lock, but not when obtaining the RESERVED lock.
3866739255
*/
38668
- rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
38669
- if( rc==SQLITE_OK ){
38670
- pPager->state = PAGER_RESERVED;
38671
- if( exFlag ){
38672
- rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38673
- }
38674
- }
38675
- }
38676
-
38677
- /* No need to open the journal file at this time. It will be
38678
- ** opened before it is written to. If we defer opening the journal,
38679
- ** we might save the work of creating a file if the transaction
38680
- ** ends up being a no-op.
38681
- */
38682
-
38683
- if( rc!=SQLITE_OK ){
38684
- assert( !pPager->dbModified );
38685
- /* Ignore any IO error that occurs within pager_end_transaction(). The
38686
- ** purpose of this call is to reset the internal state of the pager
38687
- ** sub-system. It doesn't matter if the journal-file is not properly
38688
- ** finalized at this point (since it is not a valid journal file anyway).
38689
- */
38690
- pager_end_transaction(pPager, 0);
38691
- }
39256
+ rc = pagerLockDb(pPager, RESERVED_LOCK);
39257
+ if( rc==SQLITE_OK && exFlag ){
39258
+ rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
39259
+ }
39260
+ }
39261
+
39262
+ if( rc==SQLITE_OK ){
39263
+ /* Change to WRITER_LOCKED state.
39264
+ **
39265
+ ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
39266
+ ** when it has an open transaction, but never to DBMOD or FINISHED.
39267
+ ** This is because in those states the code to roll back savepoint
39268
+ ** transactions may copy data from the sub-journal into the database
39269
+ ** file as well as into the page cache. Which would be incorrect in
39270
+ ** WAL mode.
39271
+ */
39272
+ pPager->eState = PAGER_WRITER_LOCKED;
39273
+ pPager->dbHintSize = pPager->dbSize;
39274
+ pPager->dbFileSize = pPager->dbSize;
39275
+ pPager->dbOrigSize = pPager->dbSize;
39276
+ pPager->journalOff = 0;
39277
+ }
39278
+
39279
+ assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
39280
+ assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
39281
+ assert( assert_pager_state(pPager) );
3869239282
}
3869339283
3869439284
PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
3869539285
return rc;
3869639286
}
@@ -38705,110 +39295,98 @@
3870539295
static int pager_write(PgHdr *pPg){
3870639296
void *pData = pPg->pData;
3870739297
Pager *pPager = pPg->pPager;
3870839298
int rc = SQLITE_OK;
3870939299
38710
- /* This routine is not called unless a transaction has already been
38711
- ** started.
39300
+ /* This routine is not called unless a write-transaction has already
39301
+ ** been started. The journal file may or may not be open at this point.
39302
+ ** It is never called in the ERROR state.
3871239303
*/
38713
- assert( pPager->state>=PAGER_RESERVED );
39304
+ assert( pPager->eState==PAGER_WRITER_LOCKED
39305
+ || pPager->eState==PAGER_WRITER_CACHEMOD
39306
+ || pPager->eState==PAGER_WRITER_DBMOD
39307
+ );
39308
+ assert( assert_pager_state(pPager) );
3871439309
3871539310
/* If an error has been previously detected, report the same error
38716
- ** again.
38717
- */
39311
+ ** again. This should not happen, but the check provides robustness. */
3871839312
if( NEVER(pPager->errCode) ) return pPager->errCode;
3871939313
3872039314
/* Higher-level routines never call this function if database is not
3872139315
** writable. But check anyway, just for robustness. */
3872239316
if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
3872339317
38724
- assert( !pPager->setMaster );
38725
-
3872639318
CHECK_PAGE(pPg);
3872739319
3872839320
/* Mark the page as dirty. If the page has already been written
3872939321
** to the journal then we can return right away.
3873039322
*/
3873139323
sqlite3PcacheMakeDirty(pPg);
3873239324
if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
3873339325
assert( !pagerUseWal(pPager) );
38734
- pPager->dbModified = 1;
39326
+ assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
3873539327
}else{
3873639328
3873739329
/* If we get this far, it means that the page needs to be
38738
- ** written to the transaction journal or the ckeckpoint journal
39330
+ ** written to the transaction journal or the checkpoint journal
3873939331
** or both.
3874039332
**
38741
- ** Higher level routines should have already started a transaction,
38742
- ** which means they have acquired the necessary locks but the rollback
38743
- ** journal might not yet be open.
39333
+ ** Higher level routines have already obtained the necessary locks
39334
+ ** to begin the write-transaction, but the rollback journal might not
39335
+ ** yet be open. Open it now if this is the case.
3874439336
*/
38745
- assert( pPager->state>=RESERVED_LOCK );
38746
- if( pPager->pInJournal==0
38747
- && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38748
- && !pagerUseWal(pPager)
38749
- ){
38750
- assert( pPager->useJournal );
39337
+ if( pPager->eState==PAGER_WRITER_LOCKED ){
3875139338
rc = pager_open_journal(pPager);
3875239339
if( rc!=SQLITE_OK ) return rc;
3875339340
}
38754
- pPager->dbModified = 1;
39341
+ assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
39342
+ assert( assert_pager_state(pPager) );
3875539343
3875639344
/* The transaction journal now exists and we have a RESERVED or an
3875739345
** EXCLUSIVE lock on the main database file. Write the current page to
3875839346
** the transaction journal if it is not there already.
3875939347
*/
38760
- if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
38761
- assert( !pagerUseWal(pPager) );
38762
- if( pPg->pgno<=pPager->dbOrigSize ){
39348
+ if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
39349
+ assert( pagerUseWal(pPager)==0 );
39350
+ if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
3876339351
u32 cksum;
3876439352
char *pData2;
39353
+ i64 iOff = pPager->journalOff;
3876539354
3876639355
/* We should never write to the journal file the page that
3876739356
** contains the database locks. The following assert verifies
3876839357
** that we do not. */
3876939358
assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
3877039359
38771
- assert( pPager->journalHdr <= pPager->journalOff );
39360
+ assert( pPager->journalHdr<=pPager->journalOff );
3877239361
CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
3877339362
cksum = pager_cksum(pPager, (u8*)pData2);
38774
- rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
38775
- if( rc==SQLITE_OK ){
38776
- rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
38777
- pPager->journalOff + 4);
38778
- pPager->journalOff += pPager->pageSize+4;
38779
- }
38780
- if( rc==SQLITE_OK ){
38781
- rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
38782
- pPager->journalOff += 4;
38783
- }
39363
+
39364
+ /* Even if an IO or diskfull error occurs while journalling the
39365
+ ** page in the block above, set the need-sync flag for the page.
39366
+ ** Otherwise, when the transaction is rolled back, the logic in
39367
+ ** playback_one_page() will think that the page needs to be restored
39368
+ ** in the database file. And if an IO error occurs while doing so,
39369
+ ** then corruption may follow.
39370
+ */
39371
+ pPg->flags |= PGHDR_NEED_SYNC;
39372
+
39373
+ rc = write32bits(pPager->jfd, iOff, pPg->pgno);
39374
+ if( rc!=SQLITE_OK ) return rc;
39375
+ rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
39376
+ if( rc!=SQLITE_OK ) return rc;
39377
+ rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
39378
+ if( rc!=SQLITE_OK ) return rc;
39379
+
3878439380
IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
3878539381
pPager->journalOff, pPager->pageSize));
3878639382
PAGER_INCR(sqlite3_pager_writej_count);
3878739383
PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
3878839384
PAGERID(pPager), pPg->pgno,
3878939385
((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
3879039386
38791
- /* Even if an IO or diskfull error occurred while journalling the
38792
- ** page in the block above, set the need-sync flag for the page.
38793
- ** Otherwise, when the transaction is rolled back, the logic in
38794
- ** playback_one_page() will think that the page needs to be restored
38795
- ** in the database file. And if an IO error occurs while doing so,
38796
- ** then corruption may follow.
38797
- */
38798
- if( !pPager->noSync ){
38799
- pPg->flags |= PGHDR_NEED_SYNC;
38800
- pPager->needSync = 1;
38801
- }
38802
-
38803
- /* An error has occurred writing to the journal file. The
38804
- ** transaction will be rolled back by the layer above.
38805
- */
38806
- if( rc!=SQLITE_OK ){
38807
- return rc;
38808
- }
38809
-
39387
+ pPager->journalOff += 8 + pPager->pageSize;
3881039388
pPager->nRec++;
3881139389
assert( pPager->pInJournal!=0 );
3881239390
rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
3881339391
testcase( rc==SQLITE_NOMEM );
3881439392
assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
@@ -38816,13 +39394,12 @@
3881639394
if( rc!=SQLITE_OK ){
3881739395
assert( rc==SQLITE_NOMEM );
3881839396
return rc;
3881939397
}
3882039398
}else{
38821
- if( !pPager->journalStarted && !pPager->noSync ){
39399
+ if( pPager->eState!=PAGER_WRITER_DBMOD ){
3882239400
pPg->flags |= PGHDR_NEED_SYNC;
38823
- pPager->needSync = 1;
3882439401
}
3882539402
PAGERTRACE(("APPEND %d page %d needSync=%d\n",
3882639403
PAGERID(pPager), pPg->pgno,
3882739404
((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
3882839405
}
@@ -38838,11 +39415,10 @@
3883839415
}
3883939416
}
3884039417
3884139418
/* Update the database size and return.
3884239419
*/
38843
- assert( pPager->state>=PAGER_SHARED );
3884439420
if( pPager->dbSize<pPg->pgno ){
3884539421
pPager->dbSize = pPg->pgno;
3884639422
}
3884739423
return rc;
3884839424
}
@@ -38866,10 +39442,14 @@
3886639442
3886739443
PgHdr *pPg = pDbPage;
3886839444
Pager *pPager = pPg->pPager;
3886939445
Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3887039446
39447
+ assert( pPager->eState>=PAGER_WRITER_LOCKED );
39448
+ assert( pPager->eState!=PAGER_ERROR );
39449
+ assert( assert_pager_state(pPager) );
39450
+
3887139451
if( nPagePerSector>1 ){
3887239452
Pgno nPageCount; /* Total number of pages in database file */
3887339453
Pgno pg1; /* First page of the sector pPg is located on. */
3887439454
int nPage = 0; /* Number of pages starting at pg1 to journal */
3887539455
int ii; /* Loop counter */
@@ -38887,23 +39467,21 @@
3888739467
** an integer power of 2. It sets variable pg1 to the identifier
3888839468
** of the first page of the sector pPg is located on.
3888939469
*/
3889039470
pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3889139471
38892
- rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount);
38893
- if( rc==SQLITE_OK ){
38894
- if( pPg->pgno>nPageCount ){
38895
- nPage = (pPg->pgno - pg1)+1;
38896
- }else if( (pg1+nPagePerSector-1)>nPageCount ){
38897
- nPage = nPageCount+1-pg1;
38898
- }else{
38899
- nPage = nPagePerSector;
38900
- }
38901
- assert(nPage>0);
38902
- assert(pg1<=pPg->pgno);
38903
- assert((pg1+nPage)>pPg->pgno);
38904
- }
39472
+ nPageCount = pPager->dbSize;
39473
+ if( pPg->pgno>nPageCount ){
39474
+ nPage = (pPg->pgno - pg1)+1;
39475
+ }else if( (pg1+nPagePerSector-1)>nPageCount ){
39476
+ nPage = nPageCount+1-pg1;
39477
+ }else{
39478
+ nPage = nPagePerSector;
39479
+ }
39480
+ assert(nPage>0);
39481
+ assert(pg1<=pPg->pgno);
39482
+ assert((pg1+nPage)>pPg->pgno);
3890539483
3890639484
for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3890739485
Pgno pg = pg1+ii;
3890839486
PgHdr *pPage;
3890939487
if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38911,11 +39489,10 @@
3891139489
rc = sqlite3PagerGet(pPager, pg, &pPage);
3891239490
if( rc==SQLITE_OK ){
3891339491
rc = pager_write(pPage);
3891439492
if( pPage->flags&PGHDR_NEED_SYNC ){
3891539493
needSync = 1;
38916
- assert(pPager->needSync);
3891739494
}
3891839495
sqlite3PagerUnref(pPage);
3891939496
}
3892039497
}
3892139498
}else if( (pPage = pager_lookup(pPager, pg))!=0 ){
@@ -38931,19 +39508,18 @@
3893139508
** writing to any of these nPage pages may damage the others, the
3893239509
** journal file must contain sync()ed copies of all of them
3893339510
** before any of them can be written out to the database file.
3893439511
*/
3893539512
if( rc==SQLITE_OK && needSync ){
38936
- assert( !MEMDB && pPager->noSync==0 );
39513
+ assert( !MEMDB );
3893739514
for(ii=0; ii<nPage; ii++){
3893839515
PgHdr *pPage = pager_lookup(pPager, pg1+ii);
3893939516
if( pPage ){
3894039517
pPage->flags |= PGHDR_NEED_SYNC;
3894139518
sqlite3PagerUnref(pPage);
3894239519
}
3894339520
}
38944
- assert(pPager->needSync);
3894539521
}
3894639522
3894739523
assert( pPager->doNotSyncSpill==1 );
3894839524
pPager->doNotSyncSpill--;
3894939525
}else{
@@ -39005,10 +39581,15 @@
3900539581
** by writing an updated version of page 1 using a call to the
3900639582
** sqlite3OsWrite() function.
3900739583
*/
3900839584
static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
3900939585
int rc = SQLITE_OK;
39586
+
39587
+ assert( pPager->eState==PAGER_WRITER_CACHEMOD
39588
+ || pPager->eState==PAGER_WRITER_DBMOD
39589
+ );
39590
+ assert( assert_pager_state(pPager) );
3901039591
3901139592
/* Declare and initialize constant integer 'isDirect'. If the
3901239593
** atomic-write optimization is enabled in this build, then isDirect
3901339594
** is initialized to the value passed as the isDirectMode parameter
3901439595
** to this function. Otherwise, it is always set to zero.
@@ -39024,11 +39605,10 @@
3902439605
UNUSED_PARAMETER(isDirectMode);
3902539606
#else
3902639607
# define DIRECT_MODE isDirectMode
3902739608
#endif
3902839609
39029
- assert( pPager->state>=PAGER_RESERVED );
3903039610
if( !pPager->changeCountDone && pPager->dbSize>0 ){
3903139611
PgHdr *pPgHdr; /* Reference to page 1 */
3903239612
u32 change_counter; /* Initial value of change-counter field */
3903339613
3903439614
assert( !pPager->tempFile && isOpen(pPager->fd) );
@@ -39109,13 +39689,17 @@
3910939689
** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
3911039690
** returned.
3911139691
*/
3911239692
SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
3911339693
int rc = SQLITE_OK;
39114
- assert( pPager->state>=PAGER_RESERVED );
39694
+ assert( pPager->eState==PAGER_WRITER_CACHEMOD
39695
+ || pPager->eState==PAGER_WRITER_DBMOD
39696
+ || pPager->eState==PAGER_WRITER_LOCKED
39697
+ );
39698
+ assert( assert_pager_state(pPager) );
3911539699
if( 0==pagerUseWal(pPager) ){
39116
- rc = pager_wait_on_lock(pPager, PAGER_EXCLUSIVE);
39700
+ rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3911739701
}
3911839702
return rc;
3911939703
}
3912039704
3912139705
/*
@@ -39149,26 +39733,33 @@
3914939733
const char *zMaster, /* If not NULL, the master journal name */
3915039734
int noSync /* True to omit the xSync on the db file */
3915139735
){
3915239736
int rc = SQLITE_OK; /* Return code */
3915339737
39154
- /* The dbOrigSize is never set if journal_mode=OFF */
39155
- assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
39738
+ assert( pPager->eState==PAGER_WRITER_LOCKED
39739
+ || pPager->eState==PAGER_WRITER_CACHEMOD
39740
+ || pPager->eState==PAGER_WRITER_DBMOD
39741
+ || pPager->eState==PAGER_ERROR
39742
+ );
39743
+ assert( assert_pager_state(pPager) );
3915639744
3915739745
/* If a prior error occurred, report that error again. */
39158
- if( pPager->errCode ) return pPager->errCode;
39746
+ if( NEVER(pPager->errCode) ) return pPager->errCode;
3915939747
3916039748
PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3916139749
pPager->zFilename, zMaster, pPager->dbSize));
3916239750
39163
- if( MEMDB && pPager->dbModified ){
39751
+ /* If no database changes have been made, return early. */
39752
+ if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
39753
+
39754
+ if( MEMDB ){
3916439755
/* If this is an in-memory db, or no pages have been written to, or this
3916539756
** function has already been called, it is mostly a no-op. However, any
3916639757
** backup in progress needs to be restarted.
3916739758
*/
3916839759
sqlite3BackupRestart(pPager->pBackup);
39169
- }else if( pPager->dbModified ){
39760
+ }else{
3917039761
if( pagerUseWal(pPager) ){
3917139762
PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
3917239763
if( pList ){
3917339764
rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
3917439765
(pPager->fullSync ? pPager->sync_flags : 0)
@@ -39207,11 +39798,11 @@
3920739798
|| pPager->journalMode==PAGER_JOURNALMODE_OFF
3920839799
|| pPager->journalMode==PAGER_JOURNALMODE_WAL
3920939800
);
3921039801
if( !zMaster && isOpen(pPager->jfd)
3921139802
&& pPager->journalOff==jrnlBufferSize(pPager)
39212
- && pPager->dbSize>=pPager->dbFileSize
39803
+ && pPager->dbSize>=pPager->dbOrigSize
3921339804
&& (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
3921439805
){
3921539806
/* Update the db file change counter via the direct-write method. The
3921639807
** following call will modify the in-memory representation of page 1
3921739808
** to include the updated change counter and then write page 1
@@ -39237,17 +39828,14 @@
3923739828
** Before reading the pages with page numbers larger than the
3923839829
** current value of Pager.dbSize, set dbSize back to the value
3923939830
** that it took at the start of the transaction. Otherwise, the
3924039831
** calls to sqlite3PagerGet() return zeroed pages instead of
3924139832
** reading data from the database file.
39242
- **
39243
- ** When journal_mode==OFF the dbOrigSize is always zero, so this
39244
- ** block never runs if journal_mode=OFF.
3924539833
*/
3924639834
#ifndef SQLITE_OMIT_AUTOVACUUM
3924739835
if( pPager->dbSize<pPager->dbOrigSize
39248
- && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
39836
+ && pPager->journalMode!=PAGER_JOURNALMODE_OFF
3924939837
){
3925039838
Pgno i; /* Iterator variable */
3925139839
const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
3925239840
const Pgno dbSize = pPager->dbSize; /* Database image size */
3925339841
pPager->dbSize = pPager->dbOrigSize;
@@ -39270,18 +39858,24 @@
3927039858
** or if zMaster is NULL (no master journal), then this call is a no-op.
3927139859
*/
3927239860
rc = writeMasterJournal(pPager, zMaster);
3927339861
if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
3927439862
39275
- /* Sync the journal file. If the atomic-update optimization is being
39276
- ** used, this call will not create the journal file or perform any
39277
- ** real IO.
39863
+ /* Sync the journal file and write all dirty pages to the database.
39864
+ ** If the atomic-update optimization is being used, this sync will not
39865
+ ** create the journal file or perform any real IO.
39866
+ **
39867
+ ** Because the change-counter page was just modified, unless the
39868
+ ** atomic-update optimization is used it is almost certain that the
39869
+ ** journal requires a sync here. However, in locking_mode=exclusive
39870
+ ** on a system under memory pressure it is just possible that this is
39871
+ ** not the case. In this case it is likely enough that the redundant
39872
+ ** xSync() call will be changed to a no-op by the OS anyhow.
3927839873
*/
39279
- rc = syncJournal(pPager);
39874
+ rc = syncJournal(pPager, 0);
3928039875
if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
3928139876
39282
- /* Write all dirty pages to the database file. */
3928339877
rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
3928439878
if( rc!=SQLITE_OK ){
3928539879
assert( rc!=SQLITE_IOERR_BLOCKED );
3928639880
goto commit_phase_one_exit;
3928739881
}
@@ -39290,11 +39884,11 @@
3929039884
/* If the file on disk is not the same size as the database image,
3929139885
** then use pager_truncate to grow or shrink the file here.
3929239886
*/
3929339887
if( pPager->dbSize!=pPager->dbFileSize ){
3929439888
Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
39295
- assert( pPager->state>=PAGER_EXCLUSIVE );
39889
+ assert( pPager->eState==PAGER_WRITER_DBMOD );
3929639890
rc = pager_truncate(pPager, nNew);
3929739891
if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
3929839892
}
3929939893
3930039894
/* Finally, sync the database file. */
@@ -39301,16 +39895,16 @@
3930139895
if( !pPager->noSync && !noSync ){
3930239896
rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3930339897
}
3930439898
IOTRACE(("DBSYNC %p\n", pPager))
3930539899
}
39306
-
39307
- assert( pPager->state!=PAGER_SYNCED );
39308
- pPager->state = PAGER_SYNCED;
3930939900
}
3931039901
3931139902
commit_phase_one_exit:
39903
+ if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
39904
+ pPager->eState = PAGER_WRITER_FINISHED;
39905
+ }
3931239906
return rc;
3931339907
}
3931439908
3931539909
3931639910
/*
@@ -39334,16 +39928,15 @@
3933439928
/* This routine should not be called if a prior error has occurred.
3933539929
** But if (due to a coding error elsewhere in the system) it does get
3933639930
** called, just return the same error code without doing anything. */
3933739931
if( NEVER(pPager->errCode) ) return pPager->errCode;
3933839932
39339
- /* This function should not be called if the pager is not in at least
39340
- ** PAGER_RESERVED state. **FIXME**: Make it so that this test always
39341
- ** fails - make it so that we never reach this point if we do not hold
39342
- ** all necessary locks.
39343
- */
39344
- if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
39933
+ assert( pPager->eState==PAGER_WRITER_LOCKED
39934
+ || pPager->eState==PAGER_WRITER_FINISHED
39935
+ || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
39936
+ );
39937
+ assert( assert_pager_state(pPager) );
3934539938
3934639939
/* An optimization. If the database was not actually modified during
3934739940
** this transaction, the pager is running in exclusive-mode and is
3934839941
** using persistent journals, then this function is a no-op.
3934939942
**
@@ -39352,106 +39945,80 @@
3935239945
** a hot-journal during hot-journal rollback, 0 changes will be made
3935339946
** to the database file. So there is no need to zero the journal
3935439947
** header. Since the pager is in exclusive mode, there is no need
3935539948
** to drop any locks either.
3935639949
*/
39357
- if( pPager->dbModified==0 && pPager->exclusiveMode
39950
+ if( pPager->eState==PAGER_WRITER_LOCKED
39951
+ && pPager->exclusiveMode
3935839952
&& pPager->journalMode==PAGER_JOURNALMODE_PERSIST
3935939953
){
3936039954
assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
39955
+ pPager->eState = PAGER_READER;
3936139956
return SQLITE_OK;
3936239957
}
3936339958
3936439959
PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
39365
- assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified );
3936639960
rc = pager_end_transaction(pPager, pPager->setMaster);
3936739961
return pager_error(pPager, rc);
3936839962
}
3936939963
3937039964
/*
39371
-** Rollback all changes. The database falls back to PAGER_SHARED mode.
39965
+** If a write transaction is open, then all changes made within the
39966
+** transaction are reverted and the current write-transaction is closed.
39967
+** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
39968
+** state if an error occurs.
3937239969
**
39373
-** This function performs two tasks:
39970
+** If the pager is already in PAGER_ERROR state when this function is called,
39971
+** it returns Pager.errCode immediately. No work is performed in this case.
39972
+**
39973
+** Otherwise, in rollback mode, this function performs two functions:
3937439974
**
3937539975
** 1) It rolls back the journal file, restoring all database file and
3937639976
** in-memory cache pages to the state they were in when the transaction
3937739977
** was opened, and
39978
+**
3937839979
** 2) It finalizes the journal file, so that it is not used for hot
3937939980
** rollback at any point in the future.
3938039981
**
39381
-** subject to the following qualifications:
39382
-**
39383
-** * If the journal file is not yet open when this function is called,
39384
-** then only (2) is performed. In this case there is no journal file
39385
-** to roll back.
39386
-**
39387
-** * If in an error state other than SQLITE_FULL, then task (1) is
39388
-** performed. If successful, task (2). Regardless of the outcome
39389
-** of either, the error state error code is returned to the caller
39390
-** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
39391
-**
39392
-** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
39393
-** or not (1) is successful, also attempt (2). If successful, return
39394
-** SQLITE_OK. Otherwise, enter the error state and return the first
39395
-** error code encountered.
39396
-**
39397
-** In this case there is no chance that the database was written to.
39398
-** So is safe to finalize the journal file even if the playback
39399
-** (operation 1) failed. However the pager must enter the error state
39400
-** as the contents of the in-memory cache are now suspect.
39401
-**
39402
-** * Finally, if in PAGER_EXCLUSIVE state, then attempt (1). Only
39403
-** attempt (2) if (1) is successful. Return SQLITE_OK if successful,
39404
-** otherwise enter the error state and return the error code from the
39405
-** failing operation.
39406
-**
39407
-** In this case the database file may have been written to. So if the
39408
-** playback operation did not succeed it would not be safe to finalize
39409
-** the journal file. It needs to be left in the file-system so that
39410
-** some other process can use it to restore the database state (by
39411
-** hot-journal rollback).
39982
+** Finalization of the journal file (task 2) is only performed if the
39983
+** rollback is successful.
39984
+**
39985
+** In WAL mode, all cache-entries containing data modified within the
39986
+** current transaction are either expelled from the cache or reverted to
39987
+** their pre-transaction state by re-reading data from the database or
39988
+** WAL files. The WAL transaction is then closed.
3941239989
*/
3941339990
SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
3941439991
int rc = SQLITE_OK; /* Return code */
3941539992
PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
39993
+
39994
+ /* PagerRollback() is a no-op if called in READER or OPEN state. If
39995
+ ** the pager is already in the ERROR state, the rollback is not
39996
+ ** attempted here. Instead, the error code is returned to the caller.
39997
+ */
39998
+ assert( assert_pager_state(pPager) );
39999
+ if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
40000
+ if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
40001
+
3941640002
if( pagerUseWal(pPager) ){
3941740003
int rc2;
39418
-
3941940004
rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
3942040005
rc2 = pager_end_transaction(pPager, pPager->setMaster);
3942140006
if( rc==SQLITE_OK ) rc = rc2;
39422
- rc = pager_error(pPager, rc);
39423
- }else if( !pPager->dbModified || !isOpen(pPager->jfd) ){
39424
- rc = pager_end_transaction(pPager, pPager->setMaster);
39425
- }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
39426
- if( pPager->state>=PAGER_EXCLUSIVE ){
39427
- pager_playback(pPager, 0);
39428
- }
39429
- rc = pPager->errCode;
39430
- }else{
39431
- if( pPager->state==PAGER_RESERVED ){
39432
- int rc2;
39433
- rc = pager_playback(pPager, 0);
39434
- rc2 = pager_end_transaction(pPager, pPager->setMaster);
39435
- if( rc==SQLITE_OK ){
39436
- rc = rc2;
39437
- }
39438
- }else{
39439
- rc = pager_playback(pPager, 0);
39440
- }
39441
-
39442
- if( !MEMDB ){
39443
- pPager->dbSizeValid = 0;
39444
- }
39445
-
39446
- /* If an error occurs during a ROLLBACK, we can no longer trust the pager
39447
- ** cache. So call pager_error() on the way out to make any error
39448
- ** persistent.
39449
- */
39450
- rc = pager_error(pPager, rc);
39451
- }
39452
- return rc;
40007
+ }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
40008
+ rc = pager_end_transaction(pPager, 0);
40009
+ }else{
40010
+ rc = pager_playback(pPager, 0);
40011
+ }
40012
+
40013
+ assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
40014
+ assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
40015
+
40016
+ /* If an error occurs during a ROLLBACK, we can no longer trust the pager
40017
+ ** cache. So call pager_error() on the way out to make any error persistent.
40018
+ */
40019
+ return pager_error(pPager, rc);
3945340020
}
3945440021
3945540022
/*
3945640023
** Return TRUE if the database file is opened read-only. Return FALSE
3945740024
** if the database is (in theory) writable.
@@ -39493,12 +40060,12 @@
3949340060
SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
3949440061
static int a[11];
3949540062
a[0] = sqlite3PcacheRefCount(pPager->pPCache);
3949640063
a[1] = sqlite3PcachePagecount(pPager->pPCache);
3949740064
a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
39498
- a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
39499
- a[4] = pPager->state;
40065
+ a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
40066
+ a[4] = pPager->eState;
3950040067
a[5] = pPager->errCode;
3950140068
a[6] = pPager->nHit;
3950240069
a[7] = pPager->nMiss;
3950340070
a[8] = 0; /* Used to be pPager->nOvfl */
3950440071
a[9] = pPager->nRead;
@@ -39525,18 +40092,17 @@
3952540092
** returned. Otherwise, SQLITE_OK.
3952640093
*/
3952740094
SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
3952840095
int rc = SQLITE_OK; /* Return code */
3952940096
int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
40097
+
40098
+ assert( pPager->eState>=PAGER_WRITER_LOCKED );
40099
+ assert( assert_pager_state(pPager) );
3953040100
3953140101
if( nSavepoint>nCurrent && pPager->useJournal ){
3953240102
int ii; /* Iterator variable */
3953340103
PagerSavepoint *aNew; /* New Pager.aSavepoint array */
39534
- int nPage; /* Size of database file */
39535
-
39536
- rc = sqlite3PagerPagecount(pPager, &nPage);
39537
- if( rc ) return rc;
3953840104
3953940105
/* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
3954040106
** if the allocation fails. Otherwise, zero the new portion in case a
3954140107
** malloc failure occurs while populating it in the for(...) loop below.
3954240108
*/
@@ -39549,18 +40115,18 @@
3954940115
memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
3955040116
pPager->aSavepoint = aNew;
3955140117
3955240118
/* Populate the PagerSavepoint structures just allocated. */
3955340119
for(ii=nCurrent; ii<nSavepoint; ii++){
39554
- aNew[ii].nOrig = nPage;
40120
+ aNew[ii].nOrig = pPager->dbSize;
3955540121
if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
3955640122
aNew[ii].iOffset = pPager->journalOff;
3955740123
}else{
3955840124
aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
3955940125
}
3956040126
aNew[ii].iSubRec = pPager->nSubRec;
39561
- aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage);
40127
+ aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
3956240128
if( !aNew[ii].pInSavepoint ){
3956340129
return SQLITE_NOMEM;
3956440130
}
3956540131
if( pagerUseWal(pPager) ){
3956640132
sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
@@ -39603,16 +40169,16 @@
3960340169
** This function may return SQLITE_NOMEM if a memory allocation fails,
3960440170
** or an IO error code if an IO error occurs while rolling back a
3960540171
** savepoint. If no errors occur, SQLITE_OK is returned.
3960640172
*/
3960740173
SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
39608
- int rc = SQLITE_OK;
40174
+ int rc = pPager->errCode; /* Return code */
3960940175
3961040176
assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
3961140177
assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
3961240178
39613
- if( iSavepoint<pPager->nSavepoint ){
40179
+ if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
3961440180
int ii; /* Iterator variable */
3961540181
int nNew; /* Number of remaining savepoints after this op. */
3961640182
3961740183
/* Figure out how many savepoints will still be active after this
3961840184
** operation. Store this value in nNew. Then free resources associated
@@ -39644,12 +40210,12 @@
3964440210
else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
3964540211
PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
3964640212
rc = pagerPlaybackSavepoint(pPager, pSavepoint);
3964740213
assert(rc!=SQLITE_DONE);
3964840214
}
39649
-
3965040215
}
40216
+
3965140217
return rc;
3965240218
}
3965340219
3965440220
/*
3965540221
** Return the full pathname of the database file.
@@ -39743,10 +40309,14 @@
3974340309
Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
3974440310
int rc; /* Return code */
3974540311
Pgno origPgno; /* The original page number */
3974640312
3974740313
assert( pPg->nRef>0 );
40314
+ assert( pPager->eState==PAGER_WRITER_CACHEMOD
40315
+ || pPager->eState==PAGER_WRITER_DBMOD
40316
+ );
40317
+ assert( assert_pager_state(pPager) );
3974840318
3974940319
/* In order to be able to rollback, an in-memory database must journal
3975040320
** the page we are moving from.
3975140321
*/
3975240322
if( MEMDB ){
@@ -39792,15 +40362,14 @@
3979240362
*/
3979340363
if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
3979440364
needSyncPgno = pPg->pgno;
3979540365
assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
3979640366
assert( pPg->flags&PGHDR_DIRTY );
39797
- assert( pPager->needSync );
3979840367
}
3979940368
3980040369
/* If the cache contains a page with page-number pgno, remove it
39801
- ** from its hash chain. Also, if the PgHdr.needSync was set for
40370
+ ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
3980240371
** page pgno before the 'move' operation, it needs to be retained
3980340372
** for the page moved there.
3980440373
*/
3980540374
pPg->flags &= ~PGHDR_NEED_SYNC;
3980640375
pPgOld = pager_lookup(pPager, pgno);
@@ -39808,67 +40377,59 @@
3980840377
if( pPgOld ){
3980940378
pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
3981040379
if( MEMDB ){
3981140380
/* Do not discard pages from an in-memory database since we might
3981240381
** need to rollback later. Just move the page out of the way. */
39813
- assert( pPager->dbSizeValid );
3981440382
sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
3981540383
}else{
3981640384
sqlite3PcacheDrop(pPgOld);
3981740385
}
3981840386
}
3981940387
3982040388
origPgno = pPg->pgno;
3982140389
sqlite3PcacheMove(pPg, pgno);
3982240390
sqlite3PcacheMakeDirty(pPg);
39823
- pPager->dbModified = 1;
40391
+
40392
+ /* For an in-memory database, make sure the original page continues
40393
+ ** to exist, in case the transaction needs to roll back. Use pPgOld
40394
+ ** as the original page since it has already been allocated.
40395
+ */
40396
+ if( MEMDB ){
40397
+ assert( pPgOld );
40398
+ sqlite3PcacheMove(pPgOld, origPgno);
40399
+ sqlite3PagerUnref(pPgOld);
40400
+ }
3982440401
3982540402
if( needSyncPgno ){
3982640403
/* If needSyncPgno is non-zero, then the journal file needs to be
3982740404
** sync()ed before any data is written to database file page needSyncPgno.
3982840405
** Currently, no such page exists in the page-cache and the
3982940406
** "is journaled" bitvec flag has been set. This needs to be remedied by
39830
- ** loading the page into the pager-cache and setting the PgHdr.needSync
40407
+ ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
3983140408
** flag.
3983240409
**
3983340410
** If the attempt to load the page into the page-cache fails, (due
3983440411
** to a malloc() or IO failure), clear the bit in the pInJournal[]
3983540412
** array. Otherwise, if the page is loaded and written again in
3983640413
** this transaction, it may be written to the database file before
3983740414
** it is synced into the journal file. This way, it may end up in
3983840415
** the journal file twice, but that is not a problem.
39839
- **
39840
- ** The sqlite3PagerGet() call may cause the journal to sync. So make
39841
- ** sure the Pager.needSync flag is set too.
3984240416
*/
3984340417
PgHdr *pPgHdr;
39844
- assert( pPager->needSync );
3984540418
rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
3984640419
if( rc!=SQLITE_OK ){
3984740420
if( needSyncPgno<=pPager->dbOrigSize ){
3984840421
assert( pPager->pTmpSpace!=0 );
3984940422
sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
3985040423
}
3985140424
return rc;
3985240425
}
39853
- pPager->needSync = 1;
39854
- assert( pPager->noSync==0 && !MEMDB );
3985540426
pPgHdr->flags |= PGHDR_NEED_SYNC;
3985640427
sqlite3PcacheMakeDirty(pPgHdr);
3985740428
sqlite3PagerUnref(pPgHdr);
3985840429
}
3985940430
39860
- /*
39861
- ** For an in-memory database, make sure the original page continues
39862
- ** to exist, in case the transaction needs to roll back. Use pPgOld
39863
- ** as the original page since it has already been allocated.
39864
- */
39865
- if( MEMDB ){
39866
- sqlite3PcacheMove(pPgOld, origPgno);
39867
- sqlite3PagerUnref(pPgOld);
39868
- }
39869
-
3987040431
return SQLITE_OK;
3987140432
}
3987240433
#endif
3987340434
3987440435
/*
@@ -39929,10 +40490,17 @@
3992940490
**
3993040491
** The returned indicate the current (possibly updated) journal-mode.
3993140492
*/
3993240493
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
3993340494
u8 eOld = pPager->journalMode; /* Prior journalmode */
40495
+
40496
+#ifdef SQLITE_DEBUG
40497
+ /* The print_pager_state() routine is intended to be used by the debugger
40498
+ ** only. We invoke it once here to suppress a compiler warning. */
40499
+ print_pager_state(pPager);
40500
+#endif
40501
+
3993440502
3993540503
/* The eMode parameter is always valid */
3993640504
assert( eMode==PAGER_JOURNALMODE_DELETE
3993740505
|| eMode==PAGER_JOURNALMODE_TRUNCATE
3993840506
|| eMode==PAGER_JOURNALMODE_PERSIST
@@ -39955,24 +40523,17 @@
3995540523
eMode = eOld;
3995640524
}
3995740525
}
3995840526
3995940527
if( eMode!=eOld ){
39960
- /* When changing between rollback modes, close the journal file prior
39961
- ** to the change. But when changing from a rollback mode to WAL, keep
39962
- ** the journal open since there is a rollback-style transaction in play
39963
- ** used to convert the version numbers in the btree header.
39964
- */
39965
- if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
39966
- sqlite3OsClose(pPager->jfd);
39967
- }
3996840528
3996940529
/* Change the journal mode. */
40530
+ assert( pPager->eState!=PAGER_ERROR );
3997040531
pPager->journalMode = (u8)eMode;
3997140532
3997240533
/* When transistioning from TRUNCATE or PERSIST to any other journal
39973
- ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then
40534
+ ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
3997440535
** delete the journal file.
3997540536
*/
3997640537
assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
3997740538
assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
3997840539
assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
@@ -39989,28 +40550,34 @@
3998940550
**
3999040551
** Before deleting the journal file, obtain a RESERVED lock on the
3999140552
** database file. This ensures that the journal file is not deleted
3999240553
** while it is in use by some other client.
3999340554
*/
39994
- int rc = SQLITE_OK;
39995
- int state = pPager->state;
39996
- if( state<PAGER_SHARED ){
39997
- rc = sqlite3PagerSharedLock(pPager);
39998
- }
39999
- if( pPager->state==PAGER_SHARED ){
40000
- assert( rc==SQLITE_OK );
40001
- rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
40002
- }
40003
- if( rc==SQLITE_OK ){
40555
+ sqlite3OsClose(pPager->jfd);
40556
+ if( pPager->eLock>=RESERVED_LOCK ){
4000440557
sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40558
+ }else{
40559
+ int rc = SQLITE_OK;
40560
+ int state = pPager->eState;
40561
+ assert( state==PAGER_OPEN || state==PAGER_READER );
40562
+ if( state==PAGER_OPEN ){
40563
+ rc = sqlite3PagerSharedLock(pPager);
40564
+ }
40565
+ if( pPager->eState==PAGER_READER ){
40566
+ assert( rc==SQLITE_OK );
40567
+ rc = pagerLockDb(pPager, RESERVED_LOCK);
40568
+ }
40569
+ if( rc==SQLITE_OK ){
40570
+ sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40571
+ }
40572
+ if( rc==SQLITE_OK && state==PAGER_READER ){
40573
+ pagerUnlockDb(pPager, SHARED_LOCK);
40574
+ }else if( state==PAGER_OPEN ){
40575
+ pager_unlock(pPager);
40576
+ }
40577
+ assert( state==pPager->eState );
4000540578
}
40006
- if( rc==SQLITE_OK && state==PAGER_SHARED ){
40007
- sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
40008
- }else if( state==PAGER_UNLOCK ){
40009
- pager_unlock(pPager);
40010
- }
40011
- assert( state==pPager->state );
4001240579
}
4001340580
}
4001440581
4001540582
/* Return the new journal mode */
4001640583
return (int)pPager->journalMode;
@@ -40027,11 +40594,12 @@
4002740594
** Return TRUE if the pager is in a state where it is OK to change the
4002840595
** journalmode. Journalmode changes can only happen when the database
4002940596
** is unmodified.
4003040597
*/
4003140598
SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
40032
- if( pPager->dbModified ) return 0;
40599
+ assert( assert_pager_state(pPager) );
40600
+ if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
4003340601
if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
4003440602
return 1;
4003540603
}
4003640604
4003740605
/*
@@ -40092,39 +40660,46 @@
4009240660
**
4009340661
** If the pager passed as the first argument is open on a real database
4009440662
** file (not a temp file or an in-memory database), and the WAL file
4009540663
** is not already open, make an attempt to open it now. If successful,
4009640664
** return SQLITE_OK. If an error occurs or the VFS used by the pager does
40097
-** not support the xShmXXX() methods, return an error code. *pisOpen is
40665
+** not support the xShmXXX() methods, return an error code. *pbOpen is
4009840666
** not modified in either case.
4009940667
**
4010040668
** If the pager is open on a temp-file (or in-memory database), or if
40101
-** the WAL file is already open, set *pisOpen to 1 and return SQLITE_OK
40669
+** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
4010240670
** without doing anything.
4010340671
*/
4010440672
SQLITE_PRIVATE int sqlite3PagerOpenWal(
4010540673
Pager *pPager, /* Pager object */
40106
- int *pisOpen /* OUT: Set to true if call is a no-op */
40674
+ int *pbOpen /* OUT: Set to true if call is a no-op */
4010740675
){
4010840676
int rc = SQLITE_OK; /* Return code */
4010940677
40110
- assert( pPager->state>=PAGER_SHARED );
40111
- assert( (pisOpen==0 && !pPager->tempFile && !pPager->pWal) || *pisOpen==0 );
40678
+ assert( assert_pager_state(pPager) );
40679
+ assert( pPager->eState==PAGER_OPEN || pbOpen );
40680
+ assert( pPager->eState==PAGER_READER || !pbOpen );
40681
+ assert( pbOpen==0 || *pbOpen==0 );
40682
+ assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
4011240683
4011340684
if( !pPager->tempFile && !pPager->pWal ){
4011440685
if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
40686
+
40687
+ /* Close any rollback journal previously open */
40688
+ sqlite3OsClose(pPager->jfd);
4011540689
4011640690
/* Open the connection to the log file. If this operation fails,
4011740691
** (e.g. due to malloc() failure), unlock the database file and
4011840692
** return an error code.
4011940693
*/
4012040694
rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal);
4012140695
if( rc==SQLITE_OK ){
4012240696
pPager->journalMode = PAGER_JOURNALMODE_WAL;
40697
+ pPager->eState = PAGER_OPEN;
4012340698
}
4012440699
}else{
40125
- *pisOpen = 1;
40700
+ *pbOpen = 1;
4012640701
}
4012740702
4012840703
return rc;
4012940704
}
4013040705
@@ -40146,11 +40721,11 @@
4014640721
** it may need to be checkpointed before the connection can switch to
4014740722
** rollback mode. Open it now so this can happen.
4014840723
*/
4014940724
if( !pPager->pWal ){
4015040725
int logexists = 0;
40151
- rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED);
40726
+ rc = pagerLockDb(pPager, SHARED_LOCK);
4015240727
if( rc==SQLITE_OK ){
4015340728
rc = sqlite3OsAccess(
4015440729
pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
4015540730
);
4015640731
}
@@ -40162,21 +40737,21 @@
4016240737
4016340738
/* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
4016440739
** the database file, the log and log-summary files will be deleted.
4016540740
*/
4016640741
if( rc==SQLITE_OK && pPager->pWal ){
40167
- rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE);
40742
+ rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
4016840743
if( rc==SQLITE_OK ){
4016940744
rc = sqlite3WalClose(pPager->pWal,
4017040745
(pPager->noSync ? 0 : pPager->sync_flags),
4017140746
pPager->pageSize, (u8*)pPager->pTmpSpace
4017240747
);
4017340748
pPager->pWal = 0;
4017440749
}else{
4017540750
/* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
4017640751
** that we did get back to SHARED. */
40177
- sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
40752
+ pagerUnlockDb(pPager, SQLITE_LOCK_SHARED);
4017840753
}
4017940754
}
4018040755
return rc;
4018140756
}
4018240757
@@ -40492,18 +41067,22 @@
4049241067
/*
4049341068
** The following object holds a copy of the wal-index header content.
4049441069
**
4049541070
** The actual header in the wal-index consists of two copies of this
4049641071
** object.
41072
+**
41073
+** The szPage value can be any power of 2 between 512 and 32768, inclusive.
41074
+** Or it can be 1 to represent a 65536-byte page. The latter case was
41075
+** added in 3.7.1 when support for 64K pages was added.
4049741076
*/
4049841077
struct WalIndexHdr {
4049941078
u32 iVersion; /* Wal-index version */
4050041079
u32 unused; /* Unused (padding) field */
4050141080
u32 iChange; /* Counter incremented each transaction */
4050241081
u8 isInit; /* 1 when initialized */
4050341082
u8 bigEndCksum; /* True if checksums in WAL are big-endian */
40504
- u16 szPage; /* Database page size in bytes */
41083
+ u16 szPage; /* Database page size in bytes. 1==64K */
4050541084
u32 mxFrame; /* Index of last valid frame in the WAL */
4050641085
u32 nPage; /* Size of database in pages */
4050741086
u32 aFrameCksum[2]; /* Checksum of last frame in log */
4050841087
u32 aSalt[2]; /* Two salt values copied from WAL header */
4050941088
u32 aCksum[2]; /* Checksum over all prior fields */
@@ -40610,11 +41189,11 @@
4061041189
sqlite3_file *pDbFd; /* File handle for the database file */
4061141190
sqlite3_file *pWalFd; /* File handle for WAL file */
4061241191
u32 iCallback; /* Value to pass to log callback (or 0) */
4061341192
int nWiData; /* Size of array apWiData */
4061441193
volatile u32 **apWiData; /* Pointer to wal-index content in memory */
40615
- u16 szPage; /* Database page size */
41194
+ u32 szPage; /* Database page size */
4061641195
i16 readLock; /* Which read lock is being held. -1 for none */
4061741196
u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
4061841197
u8 writeLock; /* True if in a write transaction */
4061941198
u8 ckptLock; /* True if holding a checkpoint lock */
4062041199
u8 readOnly; /* True if the WAL file is open read-only */
@@ -41281,11 +41860,11 @@
4128141860
|| szPage<512
4128241861
){
4128341862
goto finished;
4128441863
}
4128541864
pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
41286
- pWal->szPage = (u16)szPage;
41865
+ pWal->szPage = szPage;
4128741866
pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
4128841867
memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
4128941868
4129041869
/* Verify that the WAL header checksum is correct */
4129141870
walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
@@ -41331,11 +41910,13 @@
4133141910
4133241911
/* If nTruncate is non-zero, this is a commit record. */
4133341912
if( nTruncate ){
4133441913
pWal->hdr.mxFrame = iFrame;
4133541914
pWal->hdr.nPage = nTruncate;
41336
- pWal->hdr.szPage = (u16)szPage;
41915
+ pWal->hdr.szPage = (szPage&0xff00) | (szPage>>16);
41916
+ testcase( szPage<=32768 );
41917
+ testcase( szPage>=65536 );
4133741918
aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
4133841919
aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
4133941920
}
4134041921
}
4134141922
@@ -41356,10 +41937,21 @@
4135641937
*/
4135741938
pInfo = walCkptInfo(pWal);
4135841939
pInfo->nBackfill = 0;
4135941940
pInfo->aReadMark[0] = 0;
4136041941
for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
41942
+
41943
+ /* If more than one frame was recovered from the log file, report an
41944
+ ** event via sqlite3_log(). This is to help with identifying performance
41945
+ ** problems caused by applications routinely shutting down without
41946
+ ** checkpointing the log file.
41947
+ */
41948
+ if( pWal->hdr.nPage ){
41949
+ sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
41950
+ pWal->hdr.nPage, pWal->zWalName
41951
+ );
41952
+ }
4136141953
}
4136241954
4136341955
recovery_error:
4136441956
WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
4136541957
walUnlockExclusive(pWal, iLock, nLock);
@@ -41716,18 +42308,22 @@
4171642308
int sync_flags, /* Flags for OsSync() (or 0) */
4171742309
int nBuf, /* Size of zBuf in bytes */
4171842310
u8 *zBuf /* Temporary buffer to use */
4171942311
){
4172042312
int rc; /* Return code */
41721
- int szPage = pWal->hdr.szPage; /* Database page-size */
42313
+ int szPage; /* Database page-size */
4172242314
WalIterator *pIter = 0; /* Wal iterator context */
4172342315
u32 iDbpage = 0; /* Next database page to write */
4172442316
u32 iFrame = 0; /* Wal frame containing data for iDbpage */
4172542317
u32 mxSafeFrame; /* Max frame that can be backfilled */
42318
+ u32 mxPage; /* Max database page to write */
4172642319
int i; /* Loop counter */
4172742320
volatile WalCkptInfo *pInfo; /* The checkpoint status information */
4172842321
42322
+ szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42323
+ testcase( szPage<=32768 );
42324
+ testcase( szPage>=65536 );
4172942325
if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
4173042326
4173142327
/* Allocate the iterator */
4173242328
rc = walIteratorInit(pWal, &pIter);
4173342329
if( rc!=SQLITE_OK ){
@@ -41734,11 +42330,11 @@
4173442330
return rc;
4173542331
}
4173642332
assert( pIter );
4173742333
4173842334
/*** TODO: Move this test out to the caller. Make it an assert() here ***/
41739
- if( pWal->hdr.szPage!=nBuf ){
42335
+ if( szPage!=nBuf ){
4174042336
rc = SQLITE_CORRUPT_BKPT;
4174142337
goto walcheckpoint_out;
4174242338
}
4174342339
4174442340
/* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -41745,10 +42341,11 @@
4174542341
** safe to write into the database. Frames beyond mxSafeFrame might
4174642342
** overwrite database pages that are in use by active readers and thus
4174742343
** cannot be backfilled from the WAL.
4174842344
*/
4174942345
mxSafeFrame = pWal->hdr.mxFrame;
42346
+ mxPage = pWal->hdr.nPage;
4175042347
pInfo = walCkptInfo(pWal);
4175142348
for(i=1; i<WAL_NREADER; i++){
4175242349
u32 y = pInfo->aReadMark[i];
4175342350
if( mxSafeFrame>=y ){
4175442351
assert( y<=pWal->hdr.mxFrame );
@@ -41765,22 +42362,34 @@
4176542362
}
4176642363
4176742364
if( pInfo->nBackfill<mxSafeFrame
4176842365
&& (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
4176942366
){
42367
+ i64 nSize; /* Current size of database file */
4177042368
u32 nBackfill = pInfo->nBackfill;
4177142369
4177242370
/* Sync the WAL to disk */
4177342371
if( sync_flags ){
4177442372
rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
4177542373
}
42374
+
42375
+ /* If the database file may grow as a result of this checkpoint, hint
42376
+ ** about the eventual size of the db file to the VFS layer.
42377
+ */
42378
+ if( rc==SQLITE_OK ){
42379
+ i64 nReq = ((i64)mxPage * szPage);
42380
+ rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
42381
+ if( rc==SQLITE_OK && nSize<nReq ){
42382
+ sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
42383
+ }
42384
+ }
4177642385
4177742386
/* Iterate through the contents of the WAL, copying data to the db file. */
4177842387
while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
4177942388
i64 iOffset;
4178042389
assert( walFramePgno(pWal, iFrame)==iDbpage );
41781
- if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue;
42390
+ if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
4178242391
iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
4178342392
/* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
4178442393
rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
4178542394
if( rc!=SQLITE_OK ) break;
4178642395
iOffset = (iDbpage-1)*(i64)szPage;
@@ -41883,11 +42492,11 @@
4188342492
WalIndexHdr volatile *aHdr; /* Header in shared memory */
4188442493
4188542494
/* The first page of the wal-index must be mapped at this point. */
4188642495
assert( pWal->nWiData>0 && pWal->apWiData[0] );
4188742496
41888
- /* Read the header. This might happen currently with a write to the
42497
+ /* Read the header. This might happen concurrently with a write to the
4188942498
** same area of shared memory on a different CPU in a SMP,
4189042499
** meaning it is possible that an inconsistent snapshot is read
4189142500
** from the file. If this happens, return non-zero.
4189242501
**
4189342502
** There are two copies of the header at the beginning of the wal-index.
@@ -41912,11 +42521,13 @@
4191242521
}
4191342522
4191442523
if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
4191542524
*pChanged = 1;
4191642525
memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
41917
- pWal->szPage = pWal->hdr.szPage;
42526
+ pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42527
+ testcase( pWal->szPage<=32768 );
42528
+ testcase( pWal->szPage>=65536 );
4191842529
}
4191942530
4192042531
/* The header was successfully read. Return zero. */
4192142532
return 0;
4192242533
}
@@ -42231,10 +42842,11 @@
4223142842
/*
4223242843
** Finish with a read transaction. All this does is release the
4223342844
** read-lock.
4223442845
*/
4223542846
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
42847
+ sqlite3WalEndWriteTransaction(pWal);
4223642848
if( pWal->readLock>=0 ){
4223742849
walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
4223842850
pWal->readLock = -1;
4223942851
}
4224042852
}
@@ -42341,11 +42953,17 @@
4234142953
4234242954
/* If iRead is non-zero, then it is the log frame number that contains the
4234342955
** required page. Read and return data from the log file.
4234442956
*/
4234542957
if( iRead ){
42346
- i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE;
42958
+ int sz;
42959
+ i64 iOffset;
42960
+ sz = pWal->hdr.szPage;
42961
+ sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42962
+ testcase( sz<=32768 );
42963
+ testcase( sz>=65536 );
42964
+ iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
4234742965
*pInWal = 1;
4234842966
/* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
4234942967
return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
4235042968
}
4235142969
@@ -42353,15 +42971,17 @@
4235342971
return SQLITE_OK;
4235442972
}
4235542973
4235642974
4235742975
/*
42358
-** Set *pPgno to the size of the database file (or zero, if unknown).
42976
+** Return the size of the database in pages (or zero, if unknown).
4235942977
*/
42360
-SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
42361
- assert( pWal->readLock>=0 || pWal->lockError );
42362
- *pPgno = pWal->hdr.nPage;
42978
+SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
42979
+ if( pWal && ALWAYS(pWal->readLock>=0) ){
42980
+ return pWal->hdr.nPage;
42981
+ }
42982
+ return 0;
4236342983
}
4236442984
4236542985
4236642986
/*
4236742987
** This function starts a write transaction on the WAL.
@@ -42433,11 +43053,11 @@
4243343053
** Otherwise, if the callback function does not return an error, this
4243443054
** function returns SQLITE_OK.
4243543055
*/
4243643056
SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
4243743057
int rc = SQLITE_OK;
42438
- if( pWal->writeLock ){
43058
+ if( ALWAYS(pWal->writeLock) ){
4243943059
Pgno iMax = pWal->hdr.mxFrame;
4244043060
Pgno iFrame;
4244143061
4244243062
/* Restore the clients cache of the wal-index header to the state it
4244343063
** was in before the client began writing to the database.
@@ -42622,11 +43242,11 @@
4262243242
memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
4262343243
walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
4262443244
sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
4262543245
sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
4262643246
42627
- pWal->szPage = (u16)szPage;
43247
+ pWal->szPage = szPage;
4262843248
pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
4262943249
pWal->hdr.aFrameCksum[0] = aCksum[0];
4263043250
pWal->hdr.aFrameCksum[1] = aCksum[1];
4263143251
4263243252
rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
@@ -42717,11 +43337,13 @@
4271743337
rc = walIndexAppend(pWal, iFrame, pLast->pgno);
4271843338
}
4271943339
4272043340
if( rc==SQLITE_OK ){
4272143341
/* Update the private copy of the header. */
42722
- pWal->hdr.szPage = (u16)szPage;
43342
+ pWal->hdr.szPage = (szPage&0xff00) | (szPage>>16);
43343
+ testcase( szPage<=32768 );
43344
+ testcase( szPage>=65536 );
4272343345
pWal->hdr.mxFrame = iFrame;
4272443346
if( isCommit ){
4272543347
pWal->hdr.iChange++;
4272643348
pWal->hdr.nPage = nTruncate;
4272743349
}
@@ -42929,11 +43551,11 @@
4292943551
**
4293043552
** FORMAT DETAILS
4293143553
**
4293243554
** The file is divided into pages. The first page is called page 1,
4293343555
** the second is page 2, and so forth. A page number of zero indicates
42934
-** "no such page". The page size can be any power of 2 between 512 and 32768.
43556
+** "no such page". The page size can be any power of 2 between 512 and 65536.
4293543557
** Each page can be either a btree page, a freelist page, an overflow
4293643558
** page, or a pointer-map page.
4293743559
**
4293843560
** The first page is always a btree page. The first 100 bytes of the first
4293943561
** page contain a special header (the "file header") that describes the file.
@@ -43295,18 +43917,18 @@
4329543917
u8 initiallyEmpty; /* Database is empty at start of transaction */
4329643918
#ifndef SQLITE_OMIT_AUTOVACUUM
4329743919
u8 autoVacuum; /* True if auto-vacuum is enabled */
4329843920
u8 incrVacuum; /* True if incr-vacuum is enabled */
4329943921
#endif
43300
- u16 pageSize; /* Total number of bytes on a page */
43301
- u16 usableSize; /* Number of usable bytes on each page */
4330243922
u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
4330343923
u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
4330443924
u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
4330543925
u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
4330643926
u8 inTransaction; /* Transaction state */
4330743927
u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
43928
+ u32 pageSize; /* Total number of bytes on a page */
43929
+ u32 usableSize; /* Number of usable bytes on each page */
4330843930
int nTransaction; /* Number of open transactions (read + write) */
4330943931
u32 nPage; /* Number of pages in the database */
4331043932
void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
4331143933
void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
4331243934
sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -43901,11 +44523,20 @@
4390144523
# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
4390244524
#else
4390344525
# define TRACE(X)
4390444526
#endif
4390544527
43906
-
44528
+/*
44529
+** Extract a 2-byte big-endian integer from an array of unsigned bytes.
44530
+** But if the value is zero, make it 65536.
44531
+**
44532
+** This routine is used to extract the "offset to cell content area" value
44533
+** from the header of a btree page. If the page size is 65536 and the page
44534
+** is empty, the offset should be 65536, but the 2-byte value stores zero.
44535
+** This routine makes the necessary adjustment to 65536.
44536
+*/
44537
+#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
4390744538
4390844539
#ifndef SQLITE_OMIT_SHARED_CACHE
4390944540
/*
4391044541
** A list of BtShared objects that are eligible for participation
4391144542
** in shared cache. This variable has file scope during normal builds,
@@ -45023,21 +45654,21 @@
4502345654
assert( nByte < usableSize-8 );
4502445655
4502545656
nFrag = data[hdr+7];
4502645657
assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
4502745658
gap = pPage->cellOffset + 2*pPage->nCell;
45028
- top = get2byte(&data[hdr+5]);
45659
+ top = get2byteNotZero(&data[hdr+5]);
4502945660
if( gap>top ) return SQLITE_CORRUPT_BKPT;
4503045661
testcase( gap+2==top );
4503145662
testcase( gap+1==top );
4503245663
testcase( gap==top );
4503345664
4503445665
if( nFrag>=60 ){
4503545666
/* Always defragment highly fragmented pages */
4503645667
rc = defragmentPage(pPage);
4503745668
if( rc ) return rc;
45038
- top = get2byte(&data[hdr+5]);
45669
+ top = get2byteNotZero(&data[hdr+5]);
4503945670
}else if( gap+2<=top ){
4504045671
/* Search the freelist looking for a free slot big enough to satisfy
4504145672
** the request. The allocation is made from the first free slot in
4504245673
** the list that is large enough to accomadate it.
4504345674
*/
@@ -45075,11 +45706,11 @@
4507545706
*/
4507645707
testcase( gap+2+nByte==top );
4507745708
if( gap+2+nByte>top ){
4507845709
rc = defragmentPage(pPage);
4507945710
if( rc ) return rc;
45080
- top = get2byte(&data[hdr+5]);
45711
+ top = get2byteNotZero(&data[hdr+5]);
4508145712
assert( gap+nByte<=top );
4508245713
}
4508345714
4508445715
4508545716
/* Allocate memory from the gap in between the cell pointer array
@@ -45241,28 +45872,28 @@
4524145872
if( !pPage->isInit ){
4524245873
u16 pc; /* Address of a freeblock within pPage->aData[] */
4524345874
u8 hdr; /* Offset to beginning of page header */
4524445875
u8 *data; /* Equal to pPage->aData */
4524545876
BtShared *pBt; /* The main btree structure */
45246
- u16 usableSize; /* Amount of usable space on each page */
45247
- u16 cellOffset; /* Offset from start of page to first cell pointer */
45248
- u16 nFree; /* Number of unused bytes on the page */
45249
- u16 top; /* First byte of the cell content area */
45877
+ int usableSize; /* Amount of usable space on each page */
45878
+ int cellOffset; /* Offset from start of page to first cell pointer */
45879
+ int nFree; /* Number of unused bytes on the page */
45880
+ int top; /* First byte of the cell content area */
4525045881
int iCellFirst; /* First allowable cell or freeblock offset */
4525145882
int iCellLast; /* Last possible cell or freeblock offset */
4525245883
4525345884
pBt = pPage->pBt;
4525445885
4525545886
hdr = pPage->hdrOffset;
4525645887
data = pPage->aData;
4525745888
if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
45258
- assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
45259
- pPage->maskPage = pBt->pageSize - 1;
45889
+ assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
45890
+ pPage->maskPage = (u16)(pBt->pageSize - 1);
4526045891
pPage->nOverflow = 0;
4526145892
usableSize = pBt->usableSize;
4526245893
pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
45263
- top = get2byte(&data[hdr+5]);
45894
+ top = get2byteNotZero(&data[hdr+5]);
4526445895
pPage->nCell = get2byte(&data[hdr+3]);
4526545896
if( pPage->nCell>MX_CELL(pBt) ){
4526645897
/* To many cells for a single page. The page must be corrupt */
4526745898
return SQLITE_CORRUPT_BKPT;
4526845899
}
@@ -45362,12 +45993,12 @@
4536245993
pPage->nFree = pBt->usableSize - first;
4536345994
decodeFlags(pPage, flags);
4536445995
pPage->hdrOffset = hdr;
4536545996
pPage->cellOffset = first;
4536645997
pPage->nOverflow = 0;
45367
- assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
45368
- pPage->maskPage = pBt->pageSize - 1;
45998
+ assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
45999
+ pPage->maskPage = (u16)(pBt->pageSize - 1);
4536946000
pPage->nCell = 0;
4537046001
pPage->isInit = 1;
4537146002
}
4537246003
4537346004
@@ -45671,11 +46302,11 @@
4567146302
pBt->pPage1 = 0;
4567246303
pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
4567346304
#ifdef SQLITE_SECURE_DELETE
4567446305
pBt->secureDelete = 1;
4567546306
#endif
45676
- pBt->pageSize = get2byte(&zDbHeader[16]);
46307
+ pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
4567746308
if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
4567846309
|| ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
4567946310
pBt->pageSize = 0;
4568046311
#ifndef SQLITE_OMIT_AUTOVACUUM
4568146312
/* If the magic name ":memory:" will create an in-memory database, then
@@ -45985,11 +46616,11 @@
4598546616
assert( nReserve>=0 && nReserve<=255 );
4598646617
if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
4598746618
((pageSize-1)&pageSize)==0 ){
4598846619
assert( (pageSize & 7)==0 );
4598946620
assert( !pBt->pPage1 && !pBt->pCursor );
45990
- pBt->pageSize = (u16)pageSize;
46621
+ pBt->pageSize = (u32)pageSize;
4599146622
freeTempSpace(pBt);
4599246623
}
4599346624
rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
4599446625
pBt->usableSize = pBt->pageSize - (u16)nReserve;
4599546626
if( iFix ) pBt->pageSizeFixed = 1;
@@ -46120,19 +46751,17 @@
4612046751
4612146752
/* Do some checking to help insure the file we opened really is
4612246753
** a valid database file.
4612346754
*/
4612446755
nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
46125
- if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){;
46126
- goto page1_init_failed;
46127
- }
46756
+ sqlite3PagerPagecount(pBt->pPager, &nPageFile);
4612846757
if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
4612946758
nPage = nPageFile;
4613046759
}
4613146760
if( nPage>0 ){
46132
- int pageSize;
46133
- int usableSize;
46761
+ u32 pageSize;
46762
+ u32 usableSize;
4613446763
u8 *page1 = pPage1->aData;
4613546764
rc = SQLITE_NOTADB;
4613646765
if( memcmp(page1, zMagicHeader, 16)!=0 ){
4613746766
goto page1_init_failed;
4613846767
}
@@ -46179,13 +46808,14 @@
4617946808
** version 3.6.0, we require them to be fixed.
4618046809
*/
4618146810
if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
4618246811
goto page1_init_failed;
4618346812
}
46184
- pageSize = get2byte(&page1[16]);
46185
- if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
46186
- (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
46813
+ pageSize = (page1[16]<<8) | (page1[17]<<16);
46814
+ if( ((pageSize-1)&pageSize)!=0
46815
+ || pageSize>SQLITE_MAX_PAGE_SIZE
46816
+ || pageSize<=256
4618746817
){
4618846818
goto page1_init_failed;
4618946819
}
4619046820
assert( (pageSize & 7)==0 );
4619146821
usableSize = pageSize - page1[20];
@@ -46195,12 +46825,12 @@
4619546825
** actually pageSize. Unlock the database, leave pBt->pPage1 at
4619646826
** zero and return SQLITE_OK. The caller will call this function
4619746827
** again with the correct page-size.
4619846828
*/
4619946829
releasePage(pPage1);
46200
- pBt->usableSize = (u16)usableSize;
46201
- pBt->pageSize = (u16)pageSize;
46830
+ pBt->usableSize = usableSize;
46831
+ pBt->pageSize = pageSize;
4620246832
freeTempSpace(pBt);
4620346833
rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
4620446834
pageSize-usableSize);
4620546835
return rc;
4620646836
}
@@ -46209,12 +46839,12 @@
4620946839
goto page1_init_failed;
4621046840
}
4621146841
if( usableSize<480 ){
4621246842
goto page1_init_failed;
4621346843
}
46214
- pBt->pageSize = (u16)pageSize;
46215
- pBt->usableSize = (u16)usableSize;
46844
+ pBt->pageSize = pageSize;
46845
+ pBt->usableSize = usableSize;
4621646846
#ifndef SQLITE_OMIT_AUTOVACUUM
4621746847
pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
4621846848
pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
4621946849
#endif
4622046850
}
@@ -46286,11 +46916,12 @@
4628646916
data = pP1->aData;
4628746917
rc = sqlite3PagerWrite(pP1->pDbPage);
4628846918
if( rc ) return rc;
4628946919
memcpy(data, zMagicHeader, sizeof(zMagicHeader));
4629046920
assert( sizeof(zMagicHeader)==16 );
46291
- put2byte(&data[16], pBt->pageSize);
46921
+ data[16] = (pBt->pageSize>>8)&0xff;
46922
+ data[17] = (pBt->pageSize>>16)&0xff;
4629246923
data[18] = 1;
4629346924
data[19] = 1;
4629446925
assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
4629546926
data[20] = (u8)(pBt->pageSize - pBt->usableSize);
4629646927
data[21] = 64;
@@ -48297,13 +48928,13 @@
4829748928
c = +1;
4829848929
}
4829948930
pCur->validNKey = 1;
4830048931
pCur->info.nKey = nCellKey;
4830148932
}else{
48302
- /* The maximum supported page-size is 32768 bytes. This means that
48933
+ /* The maximum supported page-size is 65536 bytes. This means that
4830348934
** the maximum number of record bytes stored on an index B-Tree
48304
- ** page is at most 8198 bytes, which may be stored as a 2-byte
48935
+ ** page is less than 16384 bytes and may be stored as a 2-byte
4830548936
** varint. This information is used to attempt to avoid parsing
4830648937
** the entire cell by checking for the cases where the record is
4830748938
** stored entirely within the b-tree page by inspecting the first
4830848939
** 2 bytes of the cell.
4830948940
*/
@@ -49193,11 +49824,11 @@
4919349824
**
4919449825
** "sz" must be the number of bytes in the cell.
4919549826
*/
4919649827
static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
4919749828
int i; /* Loop counter */
49198
- int pc; /* Offset to cell content of cell being deleted */
49829
+ u32 pc; /* Offset to cell content of cell being deleted */
4919949830
u8 *data; /* pPage->aData */
4920049831
u8 *ptr; /* Used to move bytes around within data[] */
4920149832
int rc; /* The return code */
4920249833
int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
4920349834
@@ -49211,11 +49842,11 @@
4921149842
ptr = &data[pPage->cellOffset + 2*idx];
4921249843
pc = get2byte(ptr);
4921349844
hdr = pPage->hdrOffset;
4921449845
testcase( pc==get2byte(&data[hdr+5]) );
4921549846
testcase( pc+sz==pPage->pBt->usableSize );
49216
- if( pc < get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
49847
+ if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
4921749848
*pRC = SQLITE_CORRUPT_BKPT;
4921849849
return;
4921949850
}
4922049851
rc = freeSpace(pPage, pc, sz);
4922149852
if( rc ){
@@ -49268,11 +49899,11 @@
4926849899
int nSkip = (iChild ? 4 : 0);
4926949900
4927049901
if( *pRC ) return;
4927149902
4927249903
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
49273
- assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
49904
+ assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
4927449905
assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
4927549906
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4927649907
/* The cell should normally be sized correctly. However, when moving a
4927749908
** malformed cell from a leaf page to an interior page, if the cell size
4927849909
** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
@@ -49348,16 +49979,16 @@
4934849979
const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
4934949980
const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
4935049981
4935149982
assert( pPage->nOverflow==0 );
4935249983
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49353
- assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
49984
+ assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921);
4935449985
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4935549986
4935649987
/* Check that the page has just been zeroed by zeroPage() */
4935749988
assert( pPage->nCell==0 );
49358
- assert( get2byte(&data[hdr+5])==nUsable );
49989
+ assert( get2byteNotZero(&data[hdr+5])==nUsable );
4935949990
4936049991
pCellptr = &data[pPage->cellOffset + nCell*2];
4936149992
cellbody = nUsable;
4936249993
for(i=nCell-1; i>=0; i--){
4936349994
pCellptr -= 2;
@@ -49419,11 +50050,12 @@
4941950050
4942050051
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4942150052
assert( sqlite3PagerIswriteable(pParent->pDbPage) );
4942250053
assert( pPage->nOverflow==1 );
4942350054
49424
- if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
50055
+ /* This error condition is now caught prior to reaching this function */
50056
+ if( NEVER(pPage->nCell<=0) ) return SQLITE_CORRUPT_BKPT;
4942550057
4942650058
/* Allocate a new page. This page will become the right-sibling of
4942750059
** pPage. Make the parent page writable, so that the new divider cell
4942850060
** may be inserted. If both these operations are successful, proceed.
4942950061
*/
@@ -49748,11 +50380,11 @@
4974850380
** In this case, temporarily copy the cell into the aOvflSpace[]
4974950381
** buffer. It will be copied out again as soon as the aSpace[] buffer
4975050382
** is allocated. */
4975150383
if( pBt->secureDelete ){
4975250384
int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
49753
- if( (iOff+szNew[i])>pBt->usableSize ){
50385
+ if( (iOff+szNew[i])>(int)pBt->usableSize ){
4975450386
rc = SQLITE_CORRUPT_BKPT;
4975550387
memset(apOld, 0, (i+1)*sizeof(MemPage*));
4975650388
goto balance_cleanup;
4975750389
}else{
4975850390
memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
@@ -51317,11 +51949,11 @@
5131751949
#ifndef SQLITE_OMIT_AUTOVACUUM
5131851950
if( pCheck->pBt->autoVacuum ){
5131951951
checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
5132051952
}
5132151953
#endif
51322
- if( n>pCheck->pBt->usableSize/4-2 ){
51954
+ if( n>(int)pCheck->pBt->usableSize/4-2 ){
5132351955
checkAppendMsg(pCheck, zContext,
5132451956
"freelist leaf count too big on page %d", iPage);
5132551957
N--;
5132651958
}else{
5132751959
for(i=0; i<n; i++){
@@ -51528,24 +52160,24 @@
5152852160
hdr = pPage->hdrOffset;
5152952161
hit = sqlite3PageMalloc( pBt->pageSize );
5153052162
if( hit==0 ){
5153152163
pCheck->mallocFailed = 1;
5153252164
}else{
51533
- u16 contentOffset = get2byte(&data[hdr+5]);
52165
+ int contentOffset = get2byteNotZero(&data[hdr+5]);
5153452166
assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
5153552167
memset(hit+contentOffset, 0, usableSize-contentOffset);
5153652168
memset(hit, 1, contentOffset);
5153752169
nCell = get2byte(&data[hdr+3]);
5153852170
cellStart = hdr + 12 - 4*pPage->leaf;
5153952171
for(i=0; i<nCell; i++){
5154052172
int pc = get2byte(&data[cellStart+i*2]);
51541
- u16 size = 1024;
52173
+ u32 size = 65536;
5154252174
int j;
5154352175
if( pc<=usableSize-4 ){
5154452176
size = cellSizePtr(pPage, &data[pc]);
5154552177
}
51546
- if( (pc+size-1)>=usableSize ){
52178
+ if( (int)(pc+size-1)>=usableSize ){
5154752179
checkAppendMsg(pCheck, 0,
5154852180
"Corruption detected in cell %d on page %d",i,iPage);
5154952181
}else{
5155052182
for(j=pc+size-1; j>=pc; j--) hit[j]++;
5155152183
}
@@ -55769,12 +56401,21 @@
5576956401
mrc = p->rc & 0xff;
5577056402
assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
5577156403
isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
5577256404
|| mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
5577356405
if( isSpecialError ){
55774
- /* If the query was read-only, we need do no rollback at all. Otherwise,
55775
- ** proceed with the special handling.
56406
+ /* If the query was read-only and the error code is SQLITE_INTERRUPT,
56407
+ ** no rollback is necessary. Otherwise, at least a savepoint
56408
+ ** transaction must be rolled back to restore the database to a
56409
+ ** consistent state.
56410
+ **
56411
+ ** Even if the statement is read-only, it is important to perform
56412
+ ** a statement or transaction rollback operation. If the error
56413
+ ** occured while writing to the journal, sub-journal or database
56414
+ ** file as part of an effort to free up cache space (see function
56415
+ ** pagerStress() in pager.c), the rollback is required to restore
56416
+ ** the pager to a consistent state.
5577656417
*/
5577756418
if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
5577856419
if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
5577956420
eStatementOp = SAVEPOINT_ROLLBACK;
5578056421
}else{
@@ -63200,11 +63841,11 @@
6320063841
** If P5 is non-zero then the key value is increased by an epsilon
6320163842
** prior to the comparison. This make the opcode work like IdxGT except
6320263843
** that if the key from register P3 is a prefix of the key in the cursor,
6320363844
** the result is false whereas it would be true with IdxGT.
6320463845
*/
63205
-/* Opcode: IdxLT P1 P2 P3 * P5
63846
+/* Opcode: IdxLT P1 P2 P3 P4 P5
6320663847
**
6320763848
** The P4 register values beginning with P3 form an unpacked index
6320863849
** key that omits the ROWID. Compare this key value against the index
6320963850
** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
6321063851
**
@@ -67547,17 +68188,18 @@
6754768188
assert( z[0]=='?' );
6754868189
pExpr->iColumn = (ynVar)(++pParse->nVar);
6754968190
}else if( z[0]=='?' ){
6755068191
/* Wildcard of the form "?nnn". Convert "nnn" to an integer and
6755168192
** use it as the variable number */
67552
- int i = atoi((char*)&z[1]);
68193
+ i64 i;
68194
+ int bOk = sqlite3Atoi64(&z[1], &i);
6755368195
pExpr->iColumn = (ynVar)i;
6755468196
testcase( i==0 );
6755568197
testcase( i==1 );
6755668198
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
6755768199
testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
67558
- if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
68200
+ if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
6755968201
sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
6756068202
db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
6756168203
}
6756268204
if( i>pParse->nVar ){
6756368205
pParse->nVar = i;
@@ -72092,10 +72734,11 @@
7209272734
}
7209372735
}
7209472736
sqlite3DbFree(db, pIdx->aSample);
7209572737
}
7209672738
#else
72739
+ UNUSED_PARAMETER(db);
7209772740
UNUSED_PARAMETER(pIdx);
7209872741
#endif
7209972742
}
7210072743
7210172744
/*
@@ -87738,11 +88381,11 @@
8773888381
** be sent.
8773988382
**
8774088383
** regReturn is the number of the register holding the subroutine
8774188384
** return address.
8774288385
**
87743
-** If regPrev>0 then it is a the first register in a vector that
88386
+** If regPrev>0 then it is the first register in a vector that
8774488387
** records the previous output. mem[regPrev] is a flag that is false
8774588388
** if there has been no previous output. If regPrev>0 then code is
8774688389
** generated to suppress duplicates. pKeyInfo is used for comparing
8774788390
** keys.
8774888391
**
@@ -88435,16 +89078,17 @@
8843589078
** (1) The subquery and the outer query do not both use aggregates.
8843689079
**
8843789080
** (2) The subquery is not an aggregate or the outer query is not a join.
8843889081
**
8843989082
** (3) The subquery is not the right operand of a left outer join
88440
-** (Originally ticket #306. Strenghtened by ticket #3300)
89083
+** (Originally ticket #306. Strengthened by ticket #3300)
8844189084
**
88442
-** (4) The subquery is not DISTINCT or the outer query is not a join.
89085
+** (4) The subquery is not DISTINCT.
8844389086
**
88444
-** (5) The subquery is not DISTINCT or the outer query does not use
88445
-** aggregates.
89087
+** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
89088
+** sub-queries that were excluded from this optimization. Restriction
89089
+** (4) has since been expanded to exclude all DISTINCT subqueries.
8844689090
**
8844789091
** (6) The subquery does not use aggregates or the outer query is not
8844889092
** DISTINCT.
8844989093
**
8845089094
** (7) The subquery has a FROM clause.
@@ -88460,13 +89104,13 @@
8846089104
** (11) The subquery and the outer query do not both have ORDER BY clauses.
8846189105
**
8846289106
** (**) Not implemented. Subsumed into restriction (3). Was previously
8846389107
** a separate restriction deriving from ticket #350.
8846489108
**
88465
-** (13) The subquery and outer query do not both use LIMIT
89109
+** (13) The subquery and outer query do not both use LIMIT.
8846689110
**
88467
-** (14) The subquery does not use OFFSET
89111
+** (14) The subquery does not use OFFSET.
8846889112
**
8846989113
** (15) The outer query is not part of a compound select or the
8847089114
** subquery does not have a LIMIT clause.
8847189115
** (See ticket #2339 and ticket [02a8e81d44]).
8847289116
**
@@ -88553,13 +89197,13 @@
8855389197
if( pSub->pOffset ) return 0; /* Restriction (14) */
8855489198
if( p->pRightmost && pSub->pLimit ){
8855589199
return 0; /* Restriction (15) */
8855689200
}
8855789201
if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
88558
- if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit)
88559
- && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
88560
- return 0;
89202
+ if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
89203
+ if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
89204
+ return 0; /* Restrictions (8)(9) */
8856189205
}
8856289206
if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
8856389207
return 0; /* Restriction (6) */
8856489208
}
8856589209
if( p->pOrderBy && pSub->pOrderBy ){
@@ -93225,11 +93869,11 @@
9322593869
pParse->pNewTable->aCol = 0;
9322693870
}
9322793871
db->pVTab = 0;
9322893872
}else{
9322993873
sqlite3Error(db, SQLITE_ERROR, zErr);
93230
- sqlite3_free(zErr);
93874
+ sqlite3DbFree(db, zErr);
9323193875
rc = SQLITE_ERROR;
9323293876
}
9323393877
pParse->declareVtab = 0;
9323493878
9323593879
if( pParse->pVdbe ){
@@ -96859,39 +97503,39 @@
9685997503
**
9686097504
** This case is also used when there are no WHERE clause
9686197505
** constraints but an index is selected anyway, in order
9686297506
** to force the output order to conform to an ORDER BY.
9686397507
*/
96864
- int aStartOp[] = {
97508
+ static const u8 aStartOp[] = {
9686597509
0,
9686697510
0,
9686797511
OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
9686897512
OP_Last, /* 3: (!start_constraints && startEq && bRev) */
9686997513
OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */
9687097514
OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */
9687197515
OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */
9687297516
OP_SeekLe /* 7: (start_constraints && startEq && bRev) */
9687397517
};
96874
- int aEndOp[] = {
97518
+ static const u8 aEndOp[] = {
9687597519
OP_Noop, /* 0: (!end_constraints) */
9687697520
OP_IdxGE, /* 1: (end_constraints && !bRev) */
9687797521
OP_IdxLT /* 2: (end_constraints && bRev) */
9687897522
};
96879
- int nEq = pLevel->plan.nEq;
97523
+ int nEq = pLevel->plan.nEq; /* Number of == or IN terms */
9688097524
int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
9688197525
int regBase; /* Base register holding constraint values */
9688297526
int r1; /* Temp register */
9688397527
WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
9688497528
WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
9688597529
int startEq; /* True if range start uses ==, >= or <= */
9688697530
int endEq; /* True if range end uses ==, >= or <= */
9688797531
int start_constraints; /* Start of range is constrained */
9688897532
int nConstraint; /* Number of constraint terms */
96889
- Index *pIdx; /* The index we will be using */
96890
- int iIdxCur; /* The VDBE cursor for the index */
96891
- int nExtraReg = 0; /* Number of extra registers needed */
96892
- int op; /* Instruction opcode */
97533
+ Index *pIdx; /* The index we will be using */
97534
+ int iIdxCur; /* The VDBE cursor for the index */
97535
+ int nExtraReg = 0; /* Number of extra registers needed */
97536
+ int op; /* Instruction opcode */
9689397537
char *zStartAff; /* Affinity for start of range constraint */
9689497538
char *zEndAff; /* Affinity for end of range constraint */
9689597539
9689697540
pIdx = pLevel->plan.u.pIdx;
9689797541
iIdxCur = pLevel->iIdxCur;
@@ -108720,11 +109364,11 @@
108720109364
** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following
108721109365
** two forward declarations are for functions declared in these files
108722109366
** used to retrieve the respective implementations.
108723109367
**
108724109368
** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
108725
-** to by the argument to point a the "simple" tokenizer implementation.
109369
+** to by the argument to point to the "simple" tokenizer implementation.
108726109370
** Function ...PorterTokenizerModule() sets *pModule to point to the
108727109371
** porter tokenizer/stemmer implementation.
108728109372
*/
108729109373
SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
108730109374
SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
@@ -108922,11 +109566,11 @@
108922109566
** is defined to accept an argument of type char, and always returns 0 for
108923109567
** any values that fall outside of the range of the unsigned char type (i.e.
108924109568
** negative values).
108925109569
*/
108926109570
static int fts3isspace(char c){
108927
- return (c&0x80)==0 ? isspace(c) : 0;
109571
+ return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
108928109572
}
108929109573
108930109574
/*
108931109575
** Extract the next token from buffer z (length n) using the tokenizer
108932109576
** and other information (column names etc.) in pParse. Create an Fts3Expr
@@ -111309,10 +111953,13 @@
111309111953
111310111954
111311111955
static int simpleDelim(simple_tokenizer *t, unsigned char c){
111312111956
return c<0x80 && t->delim[c];
111313111957
}
111958
+static int fts3_isalnum(int x){
111959
+ return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
111960
+}
111314111961
111315111962
/*
111316111963
** Create a new tokenizer instance.
111317111964
*/
111318111965
static int simpleCreate(
@@ -111343,11 +111990,11 @@
111343111990
}
111344111991
} else {
111345111992
/* Mark non-alphanumeric ASCII characters as delimiters */
111346111993
int i;
111347111994
for(i=1; i<0x80; i++){
111348
- t->delim[i] = !isalnum(i) ? -1 : 0;
111995
+ t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
111349111996
}
111350111997
}
111351111998
111352111999
*ppTokenizer = &t->base;
111353112000
return SQLITE_OK;
@@ -111449,11 +112096,11 @@
111449112096
for(i=0; i<n; i++){
111450112097
/* TODO(shess) This needs expansion to handle UTF-8
111451112098
** case-insensitivity.
111452112099
*/
111453112100
unsigned char ch = p[iStartOffset+i];
111454
- c->pToken[i] = (char)(ch<0x80 ? tolower(ch) : ch);
112101
+ c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
111455112102
}
111456112103
*ppToken = c->pToken;
111457112104
*pnBytes = n;
111458112105
*piStartOffset = iStartOffset;
111459112106
*piEndOffset = c->iOffset;
@@ -116355,15 +117002,14 @@
116355117002
** least desirable):
116356117003
**
116357117004
** idxNum idxStr Strategy
116358117005
** ------------------------------------------------
116359117006
** 1 Unused Direct lookup by rowid.
116360
-** 2 See below R-tree query.
116361
-** 3 Unused Full table scan.
117007
+** 2 See below R-tree query or full-table scan.
116362117008
** ------------------------------------------------
116363117009
**
116364
-** If strategy 1 or 3 is used, then idxStr is not meaningful. If strategy
117010
+** If strategy 1 is used, then idxStr is not meaningful. If strategy
116365117011
** 2 is used, idxStr is formatted to contain 2 bytes for each
116366117012
** constraint used. The first two bytes of idxStr correspond to
116367117013
** the constraint in sqlite3_index_info.aConstraintUsage[] with
116368117014
** (argvIndex==1) etc.
116369117015
**
116370117016
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -213,23 +213,22 @@
213 */
214 #ifndef SQLITE_MAX_VARIABLE_NUMBER
215 # define SQLITE_MAX_VARIABLE_NUMBER 999
216 #endif
217
218 /* Maximum page size. The upper bound on this value is 32768. This a limit
219 ** imposed by the necessity of storing the value in a 2-byte unsigned integer
220 ** and the fact that the page size must be a power of 2.
221 **
222 ** If this limit is changed, then the compiled library is technically
223 ** incompatible with an SQLite library compiled with a different limit. If
224 ** a process operating on a database with a page-size of 65536 bytes
225 ** crashes, then an instance of SQLite compiled with the default page-size
226 ** limit will not be able to rollback the aborted transaction. This could
227 ** lead to database corruption.
228 */
229 #ifndef SQLITE_MAX_PAGE_SIZE
230 # define SQLITE_MAX_PAGE_SIZE 32768
231 #endif
232
233
234 /*
235 ** The default size of a database page.
@@ -631,11 +630,11 @@
631 ** be held constant and Z will be incremented or else Y will be incremented
632 ** and Z will be reset to zero.
633 **
634 ** Since version 3.6.18, SQLite source code has been stored in the
635 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
636 ** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to
637 ** a string which identifies a particular check-in of SQLite
638 ** within its configuration management system. ^The SQLITE_SOURCE_ID
639 ** string contains the date and time of the check-in (UTC) and an SHA1
640 ** hash of the entire source tree.
641 **
@@ -643,11 +642,11 @@
643 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
644 ** [sqlite_version()] and [sqlite_source_id()].
645 */
646 #define SQLITE_VERSION "3.7.1"
647 #define SQLITE_VERSION_NUMBER 3007001
648 #define SQLITE_SOURCE_ID "2010-08-05 03:21:40 fbe70e1106bcc5086ceb9d8f39cc39baf3643092"
649
650 /*
651 ** CAPI3REF: Run-Time Library Version Numbers
652 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
653 **
@@ -688,19 +687,19 @@
688 ** ^The sqlite3_compileoption_used() function returns 0 or 1
689 ** indicating whether the specified option was defined at
690 ** compile time. ^The SQLITE_ prefix may be omitted from the
691 ** option name passed to sqlite3_compileoption_used().
692 **
693 ** ^The sqlite3_compileoption_get() function allows interating
694 ** over the list of options that were defined at compile time by
695 ** returning the N-th compile time option string. ^If N is out of range,
696 ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
697 ** prefix is omitted from any strings returned by
698 ** sqlite3_compileoption_get().
699 **
700 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
701 ** and sqlite3_compileoption_get() may be omitted by specifing the
702 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
703 **
704 ** See also: SQL functions [sqlite_compileoption_used()] and
705 ** [sqlite_compileoption_get()] and the [compile_options pragma].
706 */
@@ -802,11 +801,11 @@
802 /*
803 ** CAPI3REF: Closing A Database Connection
804 **
805 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
806 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
807 ** successfullly destroyed and all associated resources are deallocated.
808 **
809 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
810 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
811 ** the [sqlite3] object prior to attempting to close the object. ^If
812 ** sqlite3_close() is called on a [database connection] that still has
@@ -1229,16 +1228,25 @@
1229 ** layer a hint of how large the database file will grow to be during the
1230 ** current transaction. This hint is not guaranteed to be accurate but it
1231 ** is often close. The underlying VFS might choose to preallocate database
1232 ** file space based on this hint in order to help writes to the database
1233 ** file run faster.
 
 
 
 
 
 
 
 
1234 */
1235 #define SQLITE_FCNTL_LOCKSTATE 1
1236 #define SQLITE_GET_LOCKPROXYFILE 2
1237 #define SQLITE_SET_LOCKPROXYFILE 3
1238 #define SQLITE_LAST_ERRNO 4
1239 #define SQLITE_FCNTL_SIZE_HINT 5
 
1240
1241 /*
1242 ** CAPI3REF: Mutex Handle
1243 **
1244 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3197,11 +3205,11 @@
3197 ** <li> @VVV
3198 ** <li> $VVV
3199 ** </ul>
3200 **
3201 ** In the templates above, NNN represents an integer literal,
3202 ** and VVV represents an alphanumeric identifer.)^ ^The values of these
3203 ** parameters (also called "host parameter names" or "SQL parameters")
3204 ** can be set using the sqlite3_bind_*() routines defined here.
3205 **
3206 ** ^The first argument to the sqlite3_bind_*() routines is always
3207 ** a pointer to the [sqlite3_stmt] object returned from
@@ -3976,11 +3984,11 @@
3976 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
3977
3978 /*
3979 ** CAPI3REF: Obtain Aggregate Function Context
3980 **
3981 ** Implementions of aggregate SQL functions use this
3982 ** routine to allocate memory for storing their state.
3983 **
3984 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
3985 ** for a particular aggregate function, SQLite
3986 ** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -4248,11 +4256,11 @@
4248 ** the routine expects pointers to 16-bit word aligned strings
4249 ** of UTF-16 in the native byte order.
4250 **
4251 ** A pointer to the user supplied routine must be passed as the fifth
4252 ** argument. ^If it is NULL, this is the same as deleting the collation
4253 ** sequence (so that SQLite cannot call it anymore).
4254 ** ^Each time the application supplied function is invoked, it is passed
4255 ** as its first parameter a copy of the void* passed as the fourth argument
4256 ** to sqlite3_create_collation() or sqlite3_create_collation16().
4257 **
4258 ** ^The remaining arguments to the application-supplied routine are two strings,
@@ -5466,11 +5474,11 @@
5466 ** of passing a NULL pointer instead of a valid mutex handle are undefined
5467 ** (i.e. it is acceptable to provide an implementation that segfaults if
5468 ** it is passed a NULL pointer).
5469 **
5470 ** The xMutexInit() method must be threadsafe. ^It must be harmless to
5471 ** invoke xMutexInit() mutiple times within the same process and without
5472 ** intervening calls to xMutexEnd(). Second and subsequent calls to
5473 ** xMutexInit() must be no-ops.
5474 **
5475 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5476 ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5636,11 +5644,11 @@
5636
5637 /*
5638 ** CAPI3REF: SQLite Runtime Status
5639 **
5640 ** ^This interface is used to retrieve runtime status information
5641 ** about the preformance of SQLite, and optionally to reset various
5642 ** highwater marks. ^The first argument is an integer code for
5643 ** the specific parameter to measure. ^(Recognized integer codes
5644 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
5645 ** ^The current value of the parameter is returned into *pCurrent.
5646 ** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5762,11 +5770,11 @@
5762 ** ^This interface is used to retrieve runtime status information
5763 ** about a single [database connection]. ^The first argument is the
5764 ** database connection object to be interrogated. ^The second argument
5765 ** is an integer constant, taken from the set of
5766 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5767 ** determiness the parameter to interrogate. The set of
5768 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
5769 ** to grow in future releases of SQLite.
5770 **
5771 ** ^The current value of the requested parameter is written into *pCur
5772 ** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -6184,11 +6192,11 @@
6184 **
6185 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
6186 **
6187 ** ^Each call to sqlite3_backup_step() sets two values inside
6188 ** the [sqlite3_backup] object: the number of pages still to be backed
6189 ** up and the total number of pages in the source databae file.
6190 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6191 ** retrieve these two values, respectively.
6192 **
6193 ** ^The values returned by these functions are only updated by
6194 ** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -6280,11 +6288,11 @@
6280 ** ^(There may be at most one unlock-notify callback registered by a
6281 ** blocked connection. If sqlite3_unlock_notify() is called when the
6282 ** blocked connection already has a registered unlock-notify callback,
6283 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6284 ** called with a NULL pointer as its second argument, then any existing
6285 ** unlock-notify callback is cancelled. ^The blocked connections
6286 ** unlock-notify callback may also be canceled by closing the blocked
6287 ** connection using [sqlite3_close()].
6288 **
6289 ** The unlock-notify callback is not reentrant. If an application invokes
6290 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -6362,11 +6370,11 @@
6362 /*
6363 ** CAPI3REF: String Comparison
6364 **
6365 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
6366 ** compare the contents of two buffers containing UTF-8 strings in a
6367 ** case-indendent fashion, using the same definition of case independence
6368 ** that SQLite uses internally when comparing identifiers.
6369 */
6370 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6371
6372 /*
@@ -7868,11 +7876,11 @@
7868 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
7869 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
7870
7871 /* Functions used to configure a Pager object. */
7872 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
7873 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
7874 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7875 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7876 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7877 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7878 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
@@ -7895,11 +7903,11 @@
7895 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
7896 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
7897 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
7898
7899 /* Functions used to manage pager transactions and savepoints. */
7900 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*);
7901 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
7902 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
7903 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
7904 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
7905 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
@@ -9183,13 +9191,13 @@
9183 ** argument to sqlite3VdbeKeyCompare and is used to control the
9184 ** comparison of the two index keys.
9185 */
9186 struct KeyInfo {
9187 sqlite3 *db; /* The database connection */
9188 u8 enc; /* Text encoding - one of the TEXT_Utf* values */
9189 u16 nField; /* Number of entries in aColl[] */
9190 u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */
9191 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
9192 };
9193
9194 /*
9195 ** An instance of the following structure holds information about a
@@ -16624,10 +16632,11 @@
16624 #else
16625 /* Use the built-in recursive mutexes if they are available.
16626 */
16627 pthread_mutex_lock(&p->mutex);
16628 #if SQLITE_MUTEX_NREF
 
16629 p->owner = pthread_self();
16630 p->nRef++;
16631 #endif
16632 #endif
16633
@@ -16696,10 +16705,11 @@
16696 */
16697 static void pthreadMutexLeave(sqlite3_mutex *p){
16698 assert( pthreadMutexHeld(p) );
16699 #if SQLITE_MUTEX_NREF
16700 p->nRef--;
 
16701 #endif
16702 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
16703
16704 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
16705 if( p->nRef==0 ){
@@ -16960,11 +16970,11 @@
16960 ** allocated mutex. SQLite is careful to deallocate every
16961 ** mutex that it allocates.
16962 */
16963 static void winMutexFree(sqlite3_mutex *p){
16964 assert( p );
16965 assert( p->nRef==0 );
16966 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
16967 DeleteCriticalSection(&p->mutex);
16968 sqlite3_free(p);
16969 }
16970
@@ -16984,10 +16994,11 @@
16984 DWORD tid = GetCurrentThreadId();
16985 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
16986 #endif
16987 EnterCriticalSection(&p->mutex);
16988 #ifdef SQLITE_DEBUG
 
16989 p->owner = tid;
16990 p->nRef++;
16991 if( p->trace ){
16992 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16993 }
@@ -17037,10 +17048,11 @@
17037 #ifndef NDEBUG
17038 DWORD tid = GetCurrentThreadId();
17039 assert( p->nRef>0 );
17040 assert( p->owner==tid );
17041 p->nRef--;
 
17042 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17043 #endif
17044 LeaveCriticalSection(&p->mutex);
17045 #ifdef SQLITE_DEBUG
17046 if( p->trace ){
@@ -22609,10 +22621,11 @@
22609 void *lockingContext; /* Locking style specific state */
22610 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22611 int fileFlags; /* Miscellanous flags */
22612 const char *zPath; /* Name of the file */
22613 unixShm *pShm; /* Shared memory segment information */
 
22614 #if SQLITE_ENABLE_LOCKING_STYLE
22615 int openFlags; /* The flags specified at open() */
22616 #endif
22617 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
22618 unsigned fsFlags; /* cached details from statfs() */
@@ -25367,19 +25380,21 @@
25367 offset += wrote;
25368 pBuf = &((char*)pBuf)[wrote];
25369 }
25370 SimulateIOError(( wrote=(-1), amt=1 ));
25371 SimulateDiskfullError(( wrote=0, amt=1 ));
 
25372 if( amt>0 ){
25373 if( wrote<0 ){
25374 /* lastErrno set by seekAndWrite */
25375 return SQLITE_IOERR_WRITE;
25376 }else{
25377 pFile->lastErrno = 0; /* not a system error */
25378 return SQLITE_FULL;
25379 }
25380 }
 
25381 return SQLITE_OK;
25382 }
25383
25384 #ifdef SQLITE_TEST
25385 /*
@@ -25577,16 +25592,27 @@
25577
25578 /*
25579 ** Truncate an open file to a specified size
25580 */
25581 static int unixTruncate(sqlite3_file *id, i64 nByte){
 
25582 int rc;
25583 assert( id );
25584 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
25585 rc = ftruncate(((unixFile*)id)->h, (off_t)nByte);
 
 
 
 
 
 
 
 
 
 
25586 if( rc ){
25587 ((unixFile*)id)->lastErrno = errno;
25588 return SQLITE_IOERR_TRUNCATE;
25589 }else{
25590 #ifndef NDEBUG
25591 /* If we are doing a normal write to a database file (as opposed to
25592 ** doing a hot-journal rollback or a write to some file other than a
@@ -25593,12 +25619,12 @@
25593 ** normal database file) and we truncate the file to zero length,
25594 ** that effectively updates the change counter. This might happen
25595 ** when restoring a database using the backup API from a zero-length
25596 ** source.
25597 */
25598 if( ((unixFile*)id)->inNormalWrite && nByte==0 ){
25599 ((unixFile*)id)->transCntrChng = 1;
25600 }
25601 #endif
25602
25603 return SQLITE_OK;
25604 }
@@ -25637,10 +25663,58 @@
25637 ** proxying locking division.
25638 */
25639 static int proxyFileControl(sqlite3_file*,int,void*);
25640 #endif
25641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25642
25643 /*
25644 ** Information and control of an open file handle.
25645 */
25646 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -25650,18 +25724,17 @@
25650 return SQLITE_OK;
25651 }
25652 case SQLITE_LAST_ERRNO: {
25653 *(int*)pArg = ((unixFile*)id)->lastErrno;
25654 return SQLITE_OK;
 
 
 
 
25655 }
25656 case SQLITE_FCNTL_SIZE_HINT: {
25657 #if 0 /* No performance advantage seen on Linux */
25658 sqlite3_int64 szFile = *(sqlite3_int64*)pArg;
25659 unixFile *pFile = (unixFile*)id;
25660 ftruncate(pFile->h, szFile);
25661 #endif
25662 return SQLITE_OK;
25663 }
25664 #ifndef NDEBUG
25665 /* The pager calls this method to signal that it has done
25666 ** a rollback and that the database is therefore unchanged and
25667 ** it hence it is OK for the transaction change counter to be
@@ -28522,11 +28595,11 @@
28522 }else{
28523 if( pCtx->conchFile ){
28524 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
28525 sqlite3_free(pCtx->conchFile);
28526 }
28527 sqlite3_free(pCtx->lockProxyPath);
28528 sqlite3_free(pCtx->conchFilePath);
28529 sqlite3_free(pCtx);
28530 }
28531 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
28532 (rc==SQLITE_OK ? "ok" : "failed")));
@@ -28713,13 +28786,13 @@
28713 }
28714 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
28715 if( rc ) return rc;
28716 sqlite3_free(conchFile);
28717 }
28718 sqlite3_free(pCtx->lockProxyPath);
28719 sqlite3_free(pCtx->conchFilePath);
28720 sqlite3_free(pCtx->dbPath);
28721 /* restore the original locking context and pMethod then close it */
28722 pFile->lockingContext = pCtx->oldLockingContext;
28723 pFile->pMethod = pCtx->pOldMethod;
28724 sqlite3_free(pCtx);
28725 return pFile->pMethod->xClose(id);
@@ -29161,10 +29234,11 @@
29161 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
29162 DWORD lastErrno; /* The Windows errno from the last I/O error */
29163 DWORD sectorSize; /* Sector size of the device file is on */
29164 winShm *pShm; /* Instance of shared memory on this file */
29165 const char *zPath; /* Full pathname of this file */
 
29166 #if SQLITE_OS_WINCE
29167 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
29168 HANDLE hMutex; /* Mutex used to control access to shared lock */
29169 HANDLE hShared; /* Shared memory segment used for locking */
29170 winceLock local; /* Locks obtained by this instance of winFile */
@@ -29671,10 +29745,46 @@
29671
29672 /*****************************************************************************
29673 ** The next group of routines implement the I/O methods specified
29674 ** by the sqlite3_io_methods object.
29675 ******************************************************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29676
29677 /*
29678 ** Close a file.
29679 **
29680 ** It is reported that an attempt to close a handle might sometimes
@@ -29714,17 +29824,10 @@
29714 OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
29715 OpenCounter(-1);
29716 return rc ? SQLITE_OK : SQLITE_IOERR;
29717 }
29718
29719 /*
29720 ** Some microsoft compilers lack this definition.
29721 */
29722 #ifndef INVALID_SET_FILE_POINTER
29723 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
29724 #endif
29725
29726 /*
29727 ** Read data from a file into a buffer. Return SQLITE_OK if all
29728 ** bytes were read successfully and SQLITE_IOERR if anything goes
29729 ** wrong.
29730 */
@@ -29732,112 +29835,108 @@
29732 sqlite3_file *id, /* File to read from */
29733 void *pBuf, /* Write content into this buffer */
29734 int amt, /* Number of bytes to read */
29735 sqlite3_int64 offset /* Begin reading at this offset */
29736 ){
29737 LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
29738 LONG lowerBits = (LONG)(offset & 0xffffffff);
29739 DWORD rc;
29740 winFile *pFile = (winFile*)id;
29741 DWORD error;
29742 DWORD got;
29743
29744 assert( id!=0 );
29745 SimulateIOError(return SQLITE_IOERR_READ);
29746 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
29747 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29748 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29749 pFile->lastErrno = error;
29750 return SQLITE_FULL;
29751 }
29752 if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){
29753 pFile->lastErrno = GetLastError();
29754 return SQLITE_IOERR_READ;
29755 }
29756 if( got==(DWORD)amt ){
29757 return SQLITE_OK;
29758 }else{
29759 /* Unread parts of the buffer must be zero-filled */
29760 memset(&((char*)pBuf)[got], 0, amt-got);
29761 return SQLITE_IOERR_SHORT_READ;
29762 }
 
 
29763 }
29764
29765 /*
29766 ** Write data from a buffer into a file. Return SQLITE_OK on success
29767 ** or some other error code on failure.
29768 */
29769 static int winWrite(
29770 sqlite3_file *id, /* File to write into */
29771 const void *pBuf, /* The bytes to be written */
29772 int amt, /* Number of bytes to write */
29773 sqlite3_int64 offset /* Offset into the file to begin writing at */
29774 ){
29775 LONG upperBits = (LONG)((offset>>32) & 0x7fffffff);
29776 LONG lowerBits = (LONG)(offset & 0xffffffff);
29777 DWORD rc;
29778 winFile *pFile = (winFile*)id;
29779 DWORD error;
29780 DWORD wrote = 0;
29781
29782 assert( id!=0 );
29783 SimulateIOError(return SQLITE_IOERR_WRITE);
29784 SimulateDiskfullError(return SQLITE_FULL);
29785 OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
29786 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29787 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29788 pFile->lastErrno = error;
29789 if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29790 return SQLITE_FULL;
29791 }else{
29792 return SQLITE_IOERR_WRITE;
29793 }
29794 }
29795 assert( amt>0 );
29796 while(
29797 amt>0
29798 && (rc = WriteFile(pFile->h, pBuf, amt, &wrote, 0))!=0
29799 && wrote>0
29800 ){
29801 amt -= wrote;
29802 pBuf = &((char*)pBuf)[wrote];
29803 }
29804 if( !rc || amt>(int)wrote ){
29805 pFile->lastErrno = GetLastError();
29806 if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29807 return SQLITE_FULL;
29808 }else{
29809 return SQLITE_IOERR_WRITE;
29810 }
29811 }
29812 return SQLITE_OK;
29813 }
29814
29815 /*
29816 ** Truncate an open file to a specified size
29817 */
29818 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
29819 LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
29820 LONG lowerBits = (LONG)(nByte & 0xffffffff);
29821 DWORD dwRet;
29822 winFile *pFile = (winFile*)id;
29823 DWORD error;
29824 int rc = SQLITE_OK;
29825
29826 assert( id!=0 );
29827 OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
29828 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
29829 dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29830 if( dwRet==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29831 pFile->lastErrno = error;
 
 
 
 
 
 
 
 
 
29832 rc = SQLITE_IOERR_TRUNCATE;
29833 /* SetEndOfFile will fail if nByte is negative */
29834 }else if( !SetEndOfFile(pFile->h) ){
29835 pFile->lastErrno = GetLastError();
29836 rc = SQLITE_IOERR_TRUNCATE;
29837 }
29838 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc==SQLITE_OK ? "ok" : "failed"));
 
29839 return rc;
29840 }
29841
29842 #ifdef SQLITE_TEST
29843 /*
@@ -30197,10 +30296,14 @@
30197 return SQLITE_OK;
30198 }
30199 case SQLITE_LAST_ERRNO: {
30200 *(int*)pArg = (int)((winFile*)id)->lastErrno;
30201 return SQLITE_OK;
 
 
 
 
30202 }
30203 case SQLITE_FCNTL_SIZE_HINT: {
30204 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
30205 SimulateIOErrorBenign(1);
30206 winTruncate(id, sz);
@@ -31749,11 +31852,11 @@
31749 ** start of a transaction, and is thus usually less than a few thousand,
31750 ** but can be as large as 2 billion for a really big database.
31751 */
31752
31753 /* Size of the Bitvec structure in bytes. */
31754 #define BITVEC_SZ (sizeof(void*)*128) /* 512 on 32bit. 1024 on 64bit */
31755
31756 /* Round the union size down to the nearest pointer boundary, since that's how
31757 ** it will be aligned within the Bitvec struct. */
31758 #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
31759
@@ -33964,11 +34067,11 @@
33964 # define sqlite3WalOpen(x,y,z) 0
33965 # define sqlite3WalClose(w,x,y,z) 0
33966 # define sqlite3WalBeginReadTransaction(y,z) 0
33967 # define sqlite3WalEndReadTransaction(z)
33968 # define sqlite3WalRead(v,w,x,y,z) 0
33969 # define sqlite3WalDbsize(y,z)
33970 # define sqlite3WalBeginWriteTransaction(y) 0
33971 # define sqlite3WalEndWriteTransaction(x) 0
33972 # define sqlite3WalUndo(x,y,z) 0
33973 # define sqlite3WalSavepoint(y,z)
33974 # define sqlite3WalSavepointUndo(y,z) 0
@@ -34000,13 +34103,12 @@
34000 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
34001
34002 /* Read a page from the write-ahead log, if it is present. */
34003 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
34004
34005 /* Return the size of the database as it existed at the beginning
34006 ** of the snapshot */
34007 SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno);
34008
34009 /* Obtain or release the WRITER lock. */
34010 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
34011 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
34012
@@ -34048,12 +34150,16 @@
34048 #endif /* _WAL_H_ */
34049
34050 /************** End of wal.h *************************************************/
34051 /************** Continuing where we left off in pager.c **********************/
34052
34053 /*
34054 ******************** NOTES ON THE DESIGN OF THE PAGER ************************
 
 
 
 
34055 **
34056 ** Within this comment block, a page is deemed to have been synced
34057 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
34058 ** Otherwise, the page is not synced until the xSync method of the VFS
34059 ** is called successfully on the file containing the page.
@@ -34083,11 +34189,11 @@
34083 ** both the content in the database when the rollback journal was written
34084 ** and the content in the database at the beginning of the current
34085 ** transaction.
34086 **
34087 ** (3) Writes to the database file are an integer multiple of the page size
34088 ** in length and are aligned to a page boundary.
34089 **
34090 ** (4) Reads from the database file are either aligned on a page boundary and
34091 ** an integer multiple of the page size in length or are taken from the
34092 ** first 100 bytes of the database file.
34093 **
@@ -34114,11 +34220,12 @@
34114 ** method is a no-op, but that does not change the fact the SQLite will
34115 ** invoke it.)
34116 **
34117 ** (9) Whenever the database file is modified, at least one bit in the range
34118 ** of bytes from 24 through 39 inclusive will be changed prior to releasing
34119 ** the EXCLUSIVE lock.
 
34120 **
34121 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
34122 ** than one billion transactions.
34123 **
34124 ** (11) A database file is well-formed at the beginning and at the conclusion
@@ -34127,11 +34234,12 @@
34127 ** (12) An EXCLUSIVE lock is held on the database file when writing to
34128 ** the database file.
34129 **
34130 ** (13) A SHARED lock is held on the database file while reading any
34131 ** content out of the database file.
34132 */
 
34133
34134 /*
34135 ** Macros for troubleshooting. Normally turned off
34136 */
34137 #if 0
@@ -34152,62 +34260,283 @@
34152 */
34153 #define PAGERID(p) ((int)(p->fd))
34154 #define FILEHANDLEID(fd) ((int)fd)
34155
34156 /*
34157 ** The page cache as a whole is always in one of the following
34158 ** states:
34159 **
34160 ** PAGER_UNLOCK The page cache is not currently reading or
34161 ** writing the database file. There is no
34162 ** data held in memory. This is the initial
34163 ** state.
34164 **
34165 ** PAGER_SHARED The page cache is reading the database.
34166 ** Writing is not permitted. There can be
34167 ** multiple readers accessing the same database
34168 ** file at the same time.
34169 **
34170 ** PAGER_RESERVED This process has reserved the database for writing
34171 ** but has not yet made any changes. Only one process
34172 ** at a time can reserve the database. The original
34173 ** database file has not been modified so other
34174 ** processes may still be reading the on-disk
34175 ** database file.
34176 **
34177 ** PAGER_EXCLUSIVE The page cache is writing the database.
34178 ** Access is exclusive. No other processes or
34179 ** threads can be reading or writing while one
34180 ** process is writing.
34181 **
34182 ** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
34183 ** after all dirty pages have been written to the
34184 ** database file and the file has been synced to
34185 ** disk. All that remains to do is to remove or
34186 ** truncate the journal file and the transaction
34187 ** will be committed.
34188 **
34189 ** The page cache comes up in PAGER_UNLOCK. The first time a
34190 ** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
34191 ** After all pages have been released using sqlite_page_unref(),
34192 ** the state transitions back to PAGER_UNLOCK. The first time
34193 ** that sqlite3PagerWrite() is called, the state transitions to
34194 ** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
34195 ** called on an outstanding page which means that the pager must
34196 ** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
34197 ** PAGER_RESERVED means that there is an open rollback journal.
34198 ** The transition to PAGER_EXCLUSIVE occurs before any changes
34199 ** are made to the database file, though writes to the rollback
34200 ** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
34201 ** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
34202 ** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
34203 */
34204 #define PAGER_UNLOCK 0
34205 #define PAGER_SHARED 1 /* same as SHARED_LOCK */
34206 #define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
34207 #define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
34208 #define PAGER_SYNCED 5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34209
34210 /*
34211 ** A macro used for invoking the codec if there is one
34212 */
34213 #ifdef SQLITE_HAS_CODEC
@@ -34253,37 +34582,32 @@
34253 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
34254 #endif
34255 };
34256
34257 /*
34258 ** A open page cache is an instance of the following structure.
34259 **
34260 ** errCode
34261 **
34262 ** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
34263 ** or SQLITE_FULL. Once one of the first three errors occurs, it persists
34264 ** and is returned as the result of every major pager API call. The
34265 ** SQLITE_FULL return code is slightly different. It persists only until the
34266 ** next successful rollback is performed on the pager cache. Also,
34267 ** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
34268 ** APIs, they may still be used successfully.
34269 **
34270 ** dbSizeValid, dbSize, dbOrigSize, dbFileSize
34271 **
34272 ** Managing the size of the database file in pages is a little complicated.
34273 ** The variable Pager.dbSize contains the number of pages that the database
34274 ** image currently contains. As the database image grows or shrinks this
34275 ** variable is updated. The variable Pager.dbFileSize contains the number
34276 ** of pages in the database file. This may be different from Pager.dbSize
34277 ** if some pages have been appended to the database image but not yet written
34278 ** out from the cache to the actual file on disk. Or if the image has been
34279 ** truncated by an incremental-vacuum operation. The Pager.dbOrigSize variable
34280 ** contains the number of pages in the database image when the current
34281 ** transaction was opened. The contents of all three of these variables is
34282 ** only guaranteed to be correct if the boolean Pager.dbSizeValid is true.
34283 **
34284 ** TODO: Under what conditions is dbSizeValid set? Cleared?
34285 **
34286 ** changeCountDone
34287 **
34288 ** This boolean variable is used to make sure that the change-counter
34289 ** (the 4-byte header field at byte offset 24 of the database file) is
@@ -34298,28 +34622,10 @@
34298 **
34299 ** This mechanism means that when running in exclusive mode, a connection
34300 ** need only update the change-counter once, for the first transaction
34301 ** committed.
34302 **
34303 ** dbModified
34304 **
34305 ** The dbModified flag is set whenever a database page is dirtied.
34306 ** It is cleared at the end of each transaction.
34307 **
34308 ** It is used when committing or otherwise ending a transaction. If
34309 ** the dbModified flag is clear then less work has to be done.
34310 **
34311 ** journalStarted
34312 **
34313 ** This flag is set during a write-transaction after the first
34314 ** journal-header is written and synced to disk.
34315 **
34316 ** After this has happened, new pages appended to the database
34317 ** do not need the PGHDR_NEED_SYNC flag set, as they do not need
34318 ** to wait for a journal sync before they can be written out to
34319 ** the database file (see function pager_write()).
34320 **
34321 ** setMaster
34322 **
34323 ** When PagerCommitPhaseOne() is called to commit a transaction, it may
34324 ** (or may not) specify a master-journal name to be written into the
34325 ** journal file before it is synced to disk.
@@ -34326,84 +34632,146 @@
34326 **
34327 ** Whether or not a journal file contains a master-journal pointer affects
34328 ** the way in which the journal file is finalized after the transaction is
34329 ** committed or rolled back when running in "journal_mode=PERSIST" mode.
34330 ** If a journal file does not contain a master-journal pointer, it is
34331 ** finalized by overwriting the first journal header with zeroes. If,
34332 ** on the other hand, it does contain a master-journal pointer, the
34333 ** journal file is finalized by truncating it to zero bytes, just as if
34334 ** the connection were running in "journal_mode=truncate" mode.
34335 **
34336 ** Journal files that contain master journal pointers cannot be finalized
34337 ** simply by overwriting the first journal-header with zeroes, as the
34338 ** master journal pointer could interfere with hot-journal rollback of any
34339 ** subsequently interrupted transaction that reuses the journal file.
34340 **
34341 ** The flag is cleared as soon as the journal file is finalized (either
34342 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
34343 ** journal file from being successfully finalized, the setMaster flag
34344 ** is cleared anyway.
34345 **
34346 ** doNotSpill, doNotSyncSpill
34347 **
34348 ** When enabled, cache spills are prohibited. The doNotSpill variable
34349 ** inhibits all cache spill and doNotSyncSpill inhibits those spills that
34350 ** would require a journal sync. The doNotSyncSpill is set and cleared
34351 ** by sqlite3PagerWrite() in order to prevent a journal sync from happening
34352 ** in between the journalling of two pages on the same sector. The
34353 ** doNotSpill value set to prevent pagerStress() from trying to use
34354 ** the journal during a rollback.
34355 **
34356 ** needSync
34357 **
34358 ** TODO: It might be easier to set this variable in writeJournalHdr()
34359 ** and writeMasterJournal() only. Change its meaning to "unsynced data
34360 ** has been written to the journal".
 
 
34361 **
34362 ** subjInMemory
34363 **
34364 ** This is a boolean variable. If true, then any required sub-journal
34365 ** is opened as an in-memory journal file. If false, then in-memory
34366 ** sub-journals are only used for in-memory pager files.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34367 */
34368 struct Pager {
34369 sqlite3_vfs *pVfs; /* OS functions to use for IO */
34370 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
34371 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
34372 u8 useJournal; /* Use a rollback journal on this file */
34373 u8 noReadlock; /* Do not bother to obtain readlocks */
34374 u8 noSync; /* Do not sync the journal if true */
34375 u8 fullSync; /* Do extra syncs of the journal for robustness */
34376 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
34377 u8 tempFile; /* zFilename is a temporary file */
34378 u8 readOnly; /* True for a read-only database */
34379 u8 memDb; /* True to inhibit all file I/O */
34380
34381 /* The following block contains those class members that are dynamically
34382 ** modified during normal operations. The other variables in this structure
34383 ** are either constant throughout the lifetime of the pager, or else
34384 ** used to store configuration parameters that affect the way the pager
34385 ** operates.
34386 **
34387 ** The 'state' variable is described in more detail along with the
34388 ** descriptions of the values it may take - PAGER_UNLOCK etc. Many of the
34389 ** other variables in this block are described in the comment directly
34390 ** above this class definition.
34391 */
34392 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
34393 u8 dbModified; /* True if there are any changes to the Db */
34394 u8 needSync; /* True if an fsync() is needed on the journal */
34395 u8 journalStarted; /* True if header of journal is synced */
34396 u8 changeCountDone; /* Set after incrementing the change-counter */
34397 u8 setMaster; /* True if a m-j name has been written to jrnl */
34398 u8 doNotSpill; /* Do not spill the cache when non-zero */
34399 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
34400 u8 dbSizeValid; /* Set when dbSize is correct */
34401 u8 subjInMemory; /* True to use in-memory sub-journals */
34402 Pgno dbSize; /* Number of pages in the database */
34403 Pgno dbOrigSize; /* dbSize before the current transaction */
34404 Pgno dbFileSize; /* Number of pages in the database file */
 
34405 int errCode; /* One of several kinds of errors */
34406 int nRec; /* Pages journalled since last j-header written */
34407 u32 cksumInit; /* Quasi-random value added to every checksum */
34408 u32 nSubRec; /* Number of records written to sub-journal */
34409 Bitvec *pInJournal; /* One bit for each page in the database file */
@@ -34410,21 +34778,25 @@
34410 sqlite3_file *fd; /* File descriptor for database */
34411 sqlite3_file *jfd; /* File descriptor for main journal */
34412 sqlite3_file *sjfd; /* File descriptor for sub-journal */
34413 i64 journalOff; /* Current write offset in the journal file */
34414 i64 journalHdr; /* Byte offset to previous journal header */
34415 i64 journalSizeLimit; /* Size limit for persistent journal files */
34416 PagerSavepoint *aSavepoint; /* Array of active savepoints */
34417 int nSavepoint; /* Number of elements in aSavepoint[] */
34418 char dbFileVers[16]; /* Changes whenever database file changes */
34419 u32 sectorSize; /* Assumed sector size during rollback */
 
 
34420
34421 u16 nExtra; /* Add this many bytes to each in-memory page */
34422 i16 nReserve; /* Number of unused bytes at end of each page */
34423 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
 
34424 int pageSize; /* Number of bytes in a page */
34425 Pgno mxPgno; /* Maximum allowed size of the database */
 
34426 char *zFilename; /* Name of the database file */
34427 char *zJournal; /* Name of the journal file */
34428 int (*xBusyHandler)(void*); /* Function to call when busy */
34429 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
34430 #ifdef SQLITE_TEST
@@ -34438,11 +34810,10 @@
34438 void (*xCodecFree)(void*); /* Destructor for the codec */
34439 void *pCodec; /* First argument to xCodec... methods */
34440 #endif
34441 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
34442 PCache *pPCache; /* Pointer to page cache object */
34443 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
34444 #ifndef SQLITE_OMIT_WAL
34445 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
34446 char *zWal; /* File name for write-ahead log */
34447 #endif
34448 };
@@ -34517,26 +34888,225 @@
34517 /*
34518 ** The maximum legal page number is (2^31 - 1).
34519 */
34520 #define PAGER_MAX_PGNO 2147483647
34521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34522 #ifndef NDEBUG
34523 /*
34524 ** Usage:
34525 **
34526 ** assert( assert_pager_state(pPager) );
 
 
 
34527 */
34528 static int assert_pager_state(Pager *pPager){
 
34529
34530 /* A temp-file is always in PAGER_EXCLUSIVE or PAGER_SYNCED state. */
34531 assert( pPager->tempFile==0 || pPager->state>=PAGER_EXCLUSIVE );
 
 
 
 
 
 
 
34532
34533 /* The changeCountDone flag is always set for temp-files */
34534 assert( pPager->tempFile==0 || pPager->changeCountDone );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34535
34536 return 1;
34537 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34538 #endif
34539
34540 /*
34541 ** Return true if it is necessary to write page *pPg into the sub-journal.
34542 ** A page needs to be written into the sub-journal if there exists one
@@ -34584,10 +35154,11 @@
34584
34585 /*
34586 ** Write a 32-bit integer into a string buffer in big-endian byte order.
34587 */
34588 #define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
 
34589
34590 /*
34591 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
34592 ** on success or an error code is something goes wrong.
34593 */
@@ -34596,31 +35167,57 @@
34596 put32bits(ac, val);
34597 return sqlite3OsWrite(fd, ac, 4, offset);
34598 }
34599
34600 /*
34601 ** The argument to this macro is a file descriptor (type sqlite3_file*).
34602 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
34603 **
34604 ** This is so that expressions can be written as:
34605 **
34606 ** if( isOpen(pPager->jfd) ){ ...
34607 **
34608 ** instead of
34609 **
34610 ** if( pPager->jfd->pMethods ){ ...
34611 */
34612 #define isOpen(pFd) ((pFd)->pMethods)
 
 
 
 
 
 
 
 
 
 
 
 
34613
34614 /*
34615 ** If file pFd is open, call sqlite3OsUnlock() on it.
 
 
 
 
 
 
 
34616 */
34617 static int osUnlock(sqlite3_file *pFd, int eLock){
34618 if( !isOpen(pFd) ){
34619 return SQLITE_OK;
 
 
 
 
 
 
 
34620 }
34621 return sqlite3OsUnlock(pFd, eLock);
34622 }
34623
34624 /*
34625 ** This function determines whether or not the atomic-write optimization
34626 ** can be used with this pager. The optimization can be used if:
@@ -34865,11 +35462,11 @@
34865 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
34866 */
34867 static int writeJournalHdr(Pager *pPager){
34868 int rc = SQLITE_OK; /* Return code */
34869 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
34870 u32 nHeader = pPager->pageSize; /* Size of buffer pointed to by zHeader */
34871 u32 nWrite; /* Bytes of header sector written */
34872 int ii; /* Loop counter */
34873
34874 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
34875
@@ -34908,11 +35505,11 @@
34908 **
34909 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
34910 ** that garbage data is never appended to the journal file.
34911 */
34912 assert( isOpen(pPager->fd) || pPager->noSync );
34913 if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
34914 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
34915 ){
34916 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
34917 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
34918 }else{
@@ -35032,11 +35629,10 @@
35032 }
35033
35034 if( pPager->journalOff==0 ){
35035 u32 iPageSize; /* Page-size field of journal header */
35036 u32 iSectorSize; /* Sector-size field of journal header */
35037 u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */
35038
35039 /* Read the page-size and sector-size journal header fields. */
35040 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
35041 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
35042 ){
@@ -35062,14 +35658,12 @@
35062
35063 /* Update the page-size to match the value read from the journal.
35064 ** Use a testcase() macro to make sure that malloc failure within
35065 ** PagerSetPagesize() is tested.
35066 */
35067 iPageSize16 = (u16)iPageSize;
35068 rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1);
35069 testcase( rc!=SQLITE_OK );
35070 assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize );
35071
35072 /* Update the assumed sector-size to match the value used by
35073 ** the process that created this journal. If this journal was
35074 ** created by a process other than this one, then this routine
35075 ** is being called from within pager_playback(). The local value
@@ -35108,10 +35702,12 @@
35108 i64 iHdrOff; /* Offset of header in journal file */
35109 i64 jrnlSize; /* Size of journal file on disk */
35110 u32 cksum = 0; /* Checksum of string zMaster */
35111
35112 assert( pPager->setMaster==0 );
 
 
35113 if( !zMaster
35114 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
35115 || pPager->journalMode==PAGER_JOURNALMODE_OFF
35116 ){
35117 return SQLITE_OK;
@@ -35144,11 +35740,10 @@
35144 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
35145 ){
35146 return rc;
35147 }
35148 pPager->journalOff += (nMaster+20);
35149 pPager->needSync = !pPager->noSync;
35150
35151 /* If the pager is in peristent-journal mode, then the physical
35152 ** journal-file may extend past the end of the master-journal name
35153 ** and 8 bytes of magic data just written to the file. This is
35154 ** dangerous because the code to rollback a hot-journal file
@@ -35180,21 +35775,15 @@
35180 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
35181 return p;
35182 }
35183
35184 /*
35185 ** Unless the pager is in error-state, discard all in-memory pages. If
35186 ** the pager is in error-state, then this call is a no-op.
35187 **
35188 ** TODO: Why can we not reset the pager while in error state?
35189 */
35190 static void pager_reset(Pager *pPager){
35191 if( SQLITE_OK==pPager->errCode ){
35192 sqlite3BackupRestart(pPager->pBackup);
35193 sqlite3PcacheClear(pPager->pPCache);
35194 pPager->dbSizeValid = 0;
35195 }
35196 }
35197
35198 /*
35199 ** Free all structures in the Pager.aSavepoint[] array and set both
35200 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
@@ -35233,38 +35822,43 @@
35233 }
35234 return rc;
35235 }
35236
35237 /*
35238 ** Return true if this pager uses a write-ahead log instead of the usual
35239 ** rollback journal. Otherwise false.
35240 */
35241 #ifndef SQLITE_OMIT_WAL
35242 static int pagerUseWal(Pager *pPager){
35243 return (pPager->pWal!=0);
35244 }
35245 #else
35246 # define pagerUseWal(x) 0
35247 # define pagerRollbackWal(x) 0
35248 # define pagerWalFrames(v,w,x,y,z) 0
35249 # define pagerOpenWalIfPresent(z) SQLITE_OK
35250 # define pagerBeginReadTransaction(z) SQLITE_OK
35251 #endif
35252
35253 /*
35254 ** Unlock the database file. This function is a no-op if the pager
35255 ** is in exclusive mode.
35256 **
35257 ** If the pager is currently in error state, discard the contents of
35258 ** the cache and reset the Pager structure internal state. If there is
35259 ** an open journal-file, then the next time a shared-lock is obtained
35260 ** on the pager file (by this or any other process), it will be
35261 ** treated as a hot-journal and rolled back.
 
 
 
 
 
 
35262 */
35263 static void pager_unlock(Pager *pPager){
35264 if( !pPager->exclusiveMode ){
35265 int rc = SQLITE_OK; /* Return code */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35266 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
35267
35268 /* If the operating system support deletion of open files, then
35269 ** close the journal file when dropping the database lock. Otherwise
35270 ** another connection with journal_mode=delete might delete the file
@@ -35280,62 +35874,60 @@
35280 || 1!=(pPager->journalMode & 5)
35281 ){
35282 sqlite3OsClose(pPager->jfd);
35283 }
35284
35285 sqlite3BitvecDestroy(pPager->pInJournal);
35286 pPager->pInJournal = 0;
35287 releaseAllSavepoints(pPager);
35288
35289 /* If the file is unlocked, somebody else might change it. The
35290 ** values stored in Pager.dbSize etc. might become invalid if
35291 ** this happens. One can argue that this doesn't need to be cleared
35292 ** until the change-counter check fails in PagerSharedLock().
35293 ** Clearing the page size cache here is being conservative.
35294 */
35295 pPager->dbSizeValid = 0;
35296
35297 if( pagerUseWal(pPager) ){
35298 sqlite3WalEndReadTransaction(pPager->pWal);
35299 }else{
35300 rc = osUnlock(pPager->fd, NO_LOCK);
35301 }
35302 if( rc ){
35303 pPager->errCode = rc;
35304 }
35305 IOTRACE(("UNLOCK %p\n", pPager))
35306
35307 /* If Pager.errCode is set, the contents of the pager cache cannot be
35308 ** trusted. Now that the pager file is unlocked, the contents of the
35309 ** cache can be discarded and the error code safely cleared.
35310 */
35311 if( pPager->errCode ){
35312 if( rc==SQLITE_OK ){
35313 pPager->errCode = SQLITE_OK;
35314 }
35315 pager_reset(pPager);
35316 }
35317
35318 pPager->changeCountDone = 0;
35319 pPager->state = PAGER_UNLOCK;
35320 pPager->dbModified = 0;
35321 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35322 }
35323
35324 /*
35325 ** This function should be called when an IOERR, CORRUPT or FULL error
35326 ** may have occurred. The first argument is a pointer to the pager
35327 ** structure, the second the error-code about to be returned by a pager
35328 ** API function. The value returned is a copy of the second argument
35329 ** to this function.
35330 **
35331 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
35332 ** the error becomes persistent. Until the persistent error is cleared,
35333 ** subsequent API calls on this Pager will immediately return the same
35334 ** error code.
35335 **
35336 ** A persistent error indicates that the contents of the pager-cache
35337 ** cannot be trusted. This state can be cleared by completely discarding
35338 ** the contents of the pager-cache. If a transaction was active when
35339 ** the persistent error occurred, then the rollback journal may need
35340 ** to be replayed to restore the contents of the database file (as if
35341 ** it were a hot-journal).
@@ -35348,49 +35940,25 @@
35348 pPager->errCode==SQLITE_OK ||
35349 (pPager->errCode & 0xff)==SQLITE_IOERR
35350 );
35351 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
35352 pPager->errCode = rc;
 
35353 }
35354 return rc;
35355 }
35356
35357 /*
35358 ** Execute a rollback if a transaction is active and unlock the
35359 ** database file.
35360 **
35361 ** If the pager has already entered the error state, do not attempt
35362 ** the rollback at this time. Instead, pager_unlock() is called. The
35363 ** call to pager_unlock() will discard all in-memory pages, unlock
35364 ** the database file and clear the error state. If this means that
35365 ** there is a hot-journal left in the file-system, the next connection
35366 ** to obtain a shared lock on the pager (which may be this one) will
35367 ** roll it back.
35368 **
35369 ** If the pager has not already entered the error state, but an IO or
35370 ** malloc error occurs during a rollback, then this will itself cause
35371 ** the pager to enter the error state. Which will be cleared by the
35372 ** call to pager_unlock(), as described above.
35373 */
35374 static void pagerUnlockAndRollback(Pager *pPager){
35375 if( pPager->errCode==SQLITE_OK && pPager->state>=PAGER_RESERVED ){
35376 sqlite3BeginBenignMalloc();
35377 sqlite3PagerRollback(pPager);
35378 sqlite3EndBenignMalloc();
35379 }
35380 pager_unlock(pPager);
35381 }
35382
35383 /*
35384 ** This routine ends a transaction. A transaction is usually ended by
35385 ** either a COMMIT or a ROLLBACK operation. This routine may be called
35386 ** after rollback of a hot-journal, or if an error occurs while opening
35387 ** the journal file or writing the very first journal-header of a
35388 ** database transaction.
35389 **
35390 ** If the pager is in PAGER_SHARED or PAGER_UNLOCK state when this
35391 ** routine is called, it is a no-op (returns SQLITE_OK).
 
35392 **
35393 ** Otherwise, any active savepoints are released.
35394 **
35395 ** If the journal file is open, then it is "finalized". Once a journal
35396 ** file has been finalized it is not possible to use it to roll back a
@@ -35417,17 +35985,13 @@
35417 ** If the pager is running in exclusive mode, this method of finalizing
35418 ** the journal file is never used. Instead, if the journalMode is
35419 ** DELETE and the pager is in exclusive mode, the method described under
35420 ** journalMode==PERSIST is used instead.
35421 **
35422 ** After the journal is finalized, if running in non-exclusive mode, the
35423 ** pager moves to PAGER_SHARED state (and downgrades the lock on the
35424 ** database file accordingly).
35425 **
35426 ** If the pager is running in exclusive mode and is in PAGER_SYNCED state,
35427 ** it moves to PAGER_EXCLUSIVE. No locks are downgraded when running in
35428 ** exclusive mode.
35429 **
35430 ** SQLITE_OK is returned if no error occurs. If an error occurs during
35431 ** any of the IO operations to finalize the journal file or unlock the
35432 ** database then the IO error code is returned to the user. If the
35433 ** operation to finalize the journal file fails, then the code still
@@ -35438,15 +36002,30 @@
35438 */
35439 static int pager_end_transaction(Pager *pPager, int hasMaster){
35440 int rc = SQLITE_OK; /* Error code from journal finalization operation */
35441 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
35442
35443 if( pPager->state<PAGER_RESERVED ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35444 return SQLITE_OK;
35445 }
 
35446 releaseAllSavepoints(pPager);
35447
35448 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
35449 if( isOpen(pPager->jfd) ){
35450 assert( !pagerUseWal(pPager) );
35451
35452 /* Finalize the journal file. */
@@ -35458,18 +36037,15 @@
35458 rc = SQLITE_OK;
35459 }else{
35460 rc = sqlite3OsTruncate(pPager->jfd, 0);
35461 }
35462 pPager->journalOff = 0;
35463 pPager->journalStarted = 0;
35464 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
35465 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
35466 ){
35467 rc = zeroJournalHdr(pPager, hasMaster);
35468 pager_error(pPager, rc);
35469 pPager->journalOff = 0;
35470 pPager->journalStarted = 0;
35471 }else{
35472 /* This branch may be executed with Pager.journalMode==MEMORY if
35473 ** a hot-journal was just rolled back. In this case the journal
35474 ** file should be closed and deleted. If this connection writes to
35475 ** the database file, it will do so using an in-memory journal.
@@ -35490,43 +36066,63 @@
35490 }
35491 sqlite3BitvecDestroy(pPager->pInJournal);
35492 pPager->pInJournal = 0;
35493 pPager->nRec = 0;
35494 sqlite3PcacheCleanAll(pPager->pPCache);
 
35495
35496 if( pagerUseWal(pPager) ){
 
 
 
 
35497 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
35498 assert( rc2==SQLITE_OK );
35499 pPager->state = PAGER_SHARED;
35500
35501 /* If the connection was in locking_mode=exclusive mode but is no longer,
35502 ** drop the EXCLUSIVE lock held on the database file.
35503 */
35504 if( !pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, 0) ){
35505 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35506 }
35507 }else if( !pPager->exclusiveMode ){
35508 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35509 pPager->state = PAGER_SHARED;
35510 pPager->changeCountDone = 0;
35511 }else if( pPager->state==PAGER_SYNCED ){
35512 pPager->state = PAGER_EXCLUSIVE;
35513 }
35514 pPager->setMaster = 0;
35515 pPager->needSync = 0;
35516 pPager->dbModified = 0;
35517
35518 /* TODO: Is this optimal? Why is the db size invalidated here
35519 ** when the database file is not unlocked? */
35520 pPager->dbOrigSize = 0;
35521 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
35522 if( !MEMDB ){
35523 pPager->dbSizeValid = 0;
35524 }
35525
35526 return (rc==SQLITE_OK?rc2:rc);
35527 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35528
35529 /*
35530 ** Parameter aData must point to a buffer of pPager->pageSize bytes
35531 ** of data. Compute and return a checksum based ont the contents of the
35532 ** page of data and the current value of pPager->cksumInit.
@@ -35574,13 +36170,12 @@
35574 ** Read a single page from either the journal file (if isMainJrnl==1) or
35575 ** from the sub-journal (if isMainJrnl==0) and playback that page.
35576 ** The page begins at offset *pOffset into the file. The *pOffset
35577 ** value is increased to the start of the next page in the journal.
35578 **
35579 ** The isMainJrnl flag is true if this is the main rollback journal and
35580 ** false for the statement journal. The main rollback journal uses
35581 ** checksums - the statement journal does not.
35582 **
35583 ** If the page number of the page record read from the (sub-)journal file
35584 ** is greater than the current value of Pager.dbSize, then playback is
35585 ** skipped and SQLITE_OK is returned.
35586 **
@@ -35629,10 +36224,21 @@
35629 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
35630
35631 aData = pPager->pTmpSpace;
35632 assert( aData ); /* Temp storage must have already been allocated */
35633 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
 
 
 
 
 
 
 
 
 
 
 
35634
35635 /* Read the page number and page data from the journal or sub-journal
35636 ** file. Return an error code to the caller if an IO error occurs.
35637 */
35638 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -35666,20 +36272,19 @@
35666 ** rollback, then don't bother to play it back again.
35667 */
35668 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
35669 return rc;
35670 }
35671 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
35672
35673 /* When playing back page 1, restore the nReserve setting
35674 */
35675 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
35676 pPager->nReserve = ((u8*)aData)[20];
35677 pagerReportSize(pPager);
35678 }
35679
35680 /* If the pager is in RESERVED state, then there must be a copy of this
35681 ** page in the pager cache. In this case just update the pager cache,
35682 ** not the database file. The page is left marked dirty in this case.
35683 **
35684 ** An exception to the above rule: If the database is in no-sync mode
35685 ** and a page is moved during an incremental vacuum then the page may
@@ -35686,12 +36291,15 @@
35686 ** not be in the pager cache. Later: if a malloc() or IO error occurs
35687 ** during a Movepage() call, then the page may not be in the cache
35688 ** either. So the condition described in the above paragraph is not
35689 ** assert()able.
35690 **
35691 ** If in EXCLUSIVE state, then we update the pager cache if it exists
35692 ** and the main file. The page is then marked not dirty.
 
 
 
35693 **
35694 ** Ticket #1171: The statement journal might contain page content that is
35695 ** different from the page content at the start of the transaction.
35696 ** This occurs when a page is changed prior to the start of a statement
35697 ** then changed again within the statement. When rolling back such a
@@ -35713,21 +36321,22 @@
35713 pPg = 0;
35714 }else{
35715 pPg = pager_lookup(pPager, pgno);
35716 }
35717 assert( pPg || !MEMDB );
 
35718 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
35719 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
35720 (isMainJrnl?"main-journal":"sub-journal")
35721 ));
35722 if( isMainJrnl ){
35723 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
35724 }else{
35725 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
35726 }
35727 if( (pPager->state>=PAGER_EXCLUSIVE)
35728 && isOpen(pPager->fd)
35729 && isSynced
35730 ){
35731 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
35732 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
35733 assert( !pagerUseWal(pPager) );
@@ -35953,14 +36562,14 @@
35953 /*
35954 ** This function is used to change the actual size of the database
35955 ** file in the file-system. This only happens when committing a transaction,
35956 ** or rolling back a transaction (including rolling back a hot-journal).
35957 **
35958 ** If the main database file is not open, or an exclusive lock is not
35959 ** held, this function is a no-op. Otherwise, the size of the file is
35960 ** changed to nPage pages (nPage*pPager->pageSize bytes). If the file
35961 ** on disk is currently larger than nPage pages, then use the VFS
35962 ** xTruncate() method to truncate it.
35963 **
35964 ** Or, it might might be the case that the file on disk is smaller than
35965 ** nPage pages. Some operating system implementations can get confused if
35966 ** you try to truncate a file to some size that is larger than it
@@ -35970,12 +36579,18 @@
35970 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
35971 ** the database file, return the error code to the caller.
35972 */
35973 static int pager_truncate(Pager *pPager, Pgno nPage){
35974 int rc = SQLITE_OK;
35975 if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){
 
 
 
 
 
35976 i64 currentSize, newSize;
 
35977 /* TODO: Is it safe to use Pager.dbFileSize here? */
35978 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
35979 newSize = pPager->pageSize*(i64)nPage;
35980 if( rc==SQLITE_OK && currentSize!=newSize ){
35981 if( currentSize>newSize ){
@@ -36095,11 +36710,11 @@
36095 /* Figure out how many records are in the journal. Abort early if
36096 ** the journal is empty.
36097 */
36098 assert( isOpen(pPager->jfd) );
36099 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
36100 if( rc!=SQLITE_OK || szJ==0 ){
36101 goto end_playback;
36102 }
36103
36104 /* Read the master journal name from the journal, if it is present.
36105 ** If a master journal file name is specified, but the file is not
@@ -36129,11 +36744,11 @@
36129 ** occurs.
36130 */
36131 while( 1 ){
36132 /* Read the next journal header from the journal file. If there are
36133 ** not enough bytes left in the journal file for a complete header, or
36134 ** it is corrupted, then a process must of failed while writing it.
36135 ** This indicates nothing more needs to be rolled back.
36136 */
36137 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
36138 if( rc!=SQLITE_OK ){
36139 if( rc==SQLITE_DONE ){
@@ -36243,14 +36858,13 @@
36243 if( rc==SQLITE_OK ){
36244 zMaster = pPager->pTmpSpace;
36245 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
36246 testcase( rc!=SQLITE_OK );
36247 }
36248 if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
36249 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
36250 }
36251 if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
36252 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
36253 }
36254 if( rc==SQLITE_OK ){
36255 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
36256 testcase( rc!=SQLITE_OK );
@@ -36288,11 +36902,11 @@
36288 Pgno pgno = pPg->pgno; /* Page number to read */
36289 int rc = SQLITE_OK; /* Return code */
36290 int isInWal = 0; /* True if page is in log file */
36291 int pgsz = pPager->pageSize; /* Number of bytes to read */
36292
36293 assert( pPager->state>=PAGER_SHARED && !MEMDB );
36294 assert( isOpen(pPager->fd) );
36295
36296 if( NEVER(!isOpen(pPager->fd)) ){
36297 assert( pPager->tempFile );
36298 memset(pPg->pData, 0, pPager->pageSize);
@@ -36451,31 +37065,82 @@
36451 static int pagerBeginReadTransaction(Pager *pPager){
36452 int rc; /* Return code */
36453 int changed = 0; /* True if cache must be reset */
36454
36455 assert( pagerUseWal(pPager) );
 
36456
36457 /* sqlite3WalEndReadTransaction() was not called for the previous
36458 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
36459 ** are in locking_mode=NORMAL and EndRead() was previously called,
36460 ** the duplicate call is harmless.
36461 */
36462 sqlite3WalEndReadTransaction(pPager->pWal);
36463
36464 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
36465 if( rc==SQLITE_OK ){
36466 int dummy;
36467 if( changed ){
36468 pager_reset(pPager);
36469 assert( pPager->errCode || pPager->dbSizeValid==0 );
36470 }
36471 rc = sqlite3PagerPagecount(pPager, &dummy);
36472 }
36473 pPager->state = PAGER_SHARED;
36474
36475 return rc;
36476 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36477
36478 /*
36479 ** Check if the *-wal file that corresponds to the database opened by pPager
36480 ** exists if the database is not empy, or verify that the *-wal file does
36481 ** not exist (by deleting it) if the database file is empty.
@@ -36485,25 +37150,26 @@
36485 ** if no error occurs, make sure Pager.journalMode is not set to
36486 ** PAGER_JOURNALMODE_WAL.
36487 **
36488 ** Return SQLITE_OK or an error code.
36489 **
36490 ** If the WAL file is opened, also open a snapshot (read transaction).
36491 **
36492 ** The caller must hold a SHARED lock on the database file to call this
36493 ** function. Because an EXCLUSIVE lock on the db file is required to delete
36494 ** a WAL on a none-empty database, this ensures there is no race condition
36495 ** between the xAccess() below and an xDelete() being executed by some
36496 ** other connection.
36497 */
36498 static int pagerOpenWalIfPresent(Pager *pPager){
36499 int rc = SQLITE_OK;
 
 
 
36500 if( !pPager->tempFile ){
36501 int isWal; /* True if WAL file exists */
36502 int nPage; /* Size of the database file */
36503 assert( pPager->state>=SHARED_LOCK );
36504 rc = sqlite3PagerPagecount(pPager, &nPage);
36505 if( rc ) return rc;
36506 if( nPage==0 ){
36507 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
36508 isWal = 0;
36509 }else{
@@ -36511,15 +37177,12 @@
36511 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
36512 );
36513 }
36514 if( rc==SQLITE_OK ){
36515 if( isWal ){
36516 pager_reset(pPager);
36517 rc = sqlite3PagerOpenWal(pPager, 0);
36518 if( rc==SQLITE_OK ){
36519 rc = pagerBeginReadTransaction(pPager);
36520 }
36521 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
36522 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
36523 }
36524 }
36525 }
@@ -36567,11 +37230,12 @@
36567 i64 szJ; /* Effective size of the main journal */
36568 i64 iHdrOff; /* End of first segment of main-journal records */
36569 int rc = SQLITE_OK; /* Return code */
36570 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
36571
36572 assert( pPager->state>=PAGER_SHARED );
 
36573
36574 /* Allocate a bitvec to use to store the set of pages rolled back */
36575 if( pSavepoint ){
36576 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
36577 if( !pDone ){
@@ -36706,11 +37370,10 @@
36706 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
36707 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
36708 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
36709 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
36710 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
36711 if( pPager->noSync ) pPager->needSync = 0;
36712 }
36713 #endif
36714
36715 /*
36716 ** The following global variable is incremented whenever the library
@@ -36788,11 +37451,11 @@
36788 ** Change the page size used by the Pager object. The new page size
36789 ** is passed in *pPageSize.
36790 **
36791 ** If the pager is in the error state when this function is called, it
36792 ** is a no-op. The value returned is the error state error code (i.e.
36793 ** one of SQLITE_IOERR, SQLITE_CORRUPT or SQLITE_FULL).
36794 **
36795 ** Otherwise, if all of the following are true:
36796 **
36797 ** * the new page size (value of *pPageSize) is valid (a power
36798 ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
@@ -36812,32 +37475,52 @@
36812 ** If the page size is not changed, either because one of the enumerated
36813 ** conditions above is not true, the pager was in error state when this
36814 ** function was called, or because the memory allocation attempt failed,
36815 ** then *pPageSize is set to the old, retained page size before returning.
36816 */
36817 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize, int nReserve){
36818 int rc = pPager->errCode;
36819
36820 if( rc==SQLITE_OK ){
36821 u16 pageSize = *pPageSize;
36822 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
36823 if( (pPager->memDb==0 || pPager->dbSize==0)
36824 && sqlite3PcacheRefCount(pPager->pPCache)==0
36825 && pageSize && pageSize!=pPager->pageSize
36826 ){
36827 char *pNew = (char *)sqlite3PageMalloc(pageSize);
36828 if( !pNew ){
36829 rc = SQLITE_NOMEM;
36830 }else{
36831 pager_reset(pPager);
36832 pPager->pageSize = pageSize;
36833 sqlite3PageFree(pPager->pTmpSpace);
36834 pPager->pTmpSpace = pNew;
36835 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
36836 }
36837 }
36838 *pPageSize = (u16)pPager->pageSize;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36839 if( nReserve<0 ) nReserve = pPager->nReserve;
36840 assert( nReserve>=0 && nReserve<1000 );
36841 pPager->nReserve = (i16)nReserve;
36842 pagerReportSize(pPager);
36843 }
@@ -36862,17 +37545,15 @@
36862 ** maximum page count below the current size of the database.
36863 **
36864 ** Regardless of mxPage, return the current maximum page count.
36865 */
36866 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
36867 int nPage;
36868 if( mxPage>0 ){
36869 pPager->mxPgno = mxPage;
36870 }
36871 if( pPager->state!=PAGER_UNLOCK ){
36872 sqlite3PagerPagecount(pPager, &nPage);
36873 assert( (int)pPager->mxPgno>=nPage );
36874 }
36875 return pPager->mxPgno;
36876 }
36877
36878 /*
@@ -36933,70 +37614,20 @@
36933 }
36934 return rc;
36935 }
36936
36937 /*
36938 ** Return the total number of pages in the database file associated
36939 ** with pPager. Normally, this is calculated as (<db file size>/<page-size>).
 
36940 ** However, if the file is between 1 and <page-size> bytes in size, then
36941 ** this is considered a 1 page file.
36942 **
36943 ** If the pager is in error state when this function is called, then the
36944 ** error state error code is returned and *pnPage left unchanged. Or,
36945 ** if the file system has to be queried for the size of the file and
36946 ** the query attempt returns an IO error, the IO error code is returned
36947 ** and *pnPage is left unchanged.
36948 **
36949 ** Otherwise, if everything is successful, then SQLITE_OK is returned
36950 ** and *pnPage is set to the number of pages in the database.
36951 */
36952 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
36953 Pgno nPage = 0; /* Value to return via *pnPage */
36954
36955 /* Determine the number of pages in the file. Store this in nPage. */
36956 if( pPager->dbSizeValid ){
36957 nPage = pPager->dbSize;
36958 }else{
36959 int rc; /* Error returned by OsFileSize() */
36960 i64 n = 0; /* File size in bytes returned by OsFileSize() */
36961
36962 if( pagerUseWal(pPager) && pPager->state!=PAGER_UNLOCK ){
36963 sqlite3WalDbsize(pPager->pWal, &nPage);
36964 }
36965
36966 if( nPage==0 ){
36967 assert( isOpen(pPager->fd) || pPager->tempFile );
36968 if( isOpen(pPager->fd) ){
36969 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pPager->fd, &n)) ){
36970 pager_error(pPager, rc);
36971 return rc;
36972 }
36973 }
36974 if( n>0 && n<pPager->pageSize ){
36975 nPage = 1;
36976 }else{
36977 nPage = (Pgno)(n / pPager->pageSize);
36978 }
36979 }
36980 if( pPager->state!=PAGER_UNLOCK ){
36981 pPager->dbSize = nPage;
36982 pPager->dbFileSize = nPage;
36983 pPager->dbSizeValid = 1;
36984 }
36985 }
36986
36987 /* If the current number of pages in the file is greater than the
36988 ** configured maximum pager number, increase the allowed limit so
36989 ** that the file can be read.
36990 */
36991 if( nPage>pPager->mxPgno ){
36992 pPager->mxPgno = (Pgno)nPage;
36993 }
36994
36995 /* Set the output variable and return SQLITE_OK */
36996 *pnPage = nPage;
36997 return SQLITE_OK;
36998 }
36999
37000
37001 /*
37002 ** Try to obtain a lock of type locktype on the database file. If
@@ -37013,42 +37644,23 @@
37013 ** variable to locktype before returning.
37014 */
37015 static int pager_wait_on_lock(Pager *pPager, int locktype){
37016 int rc; /* Return code */
37017
37018 /* The OS lock values must be the same as the Pager lock values */
37019 assert( PAGER_SHARED==SHARED_LOCK );
37020 assert( PAGER_RESERVED==RESERVED_LOCK );
37021 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
37022
37023 /* If the file is currently unlocked then the size must be unknown. It
37024 ** must not have been modified at this point.
37025 */
37026 assert( pPager->state>=PAGER_SHARED || pPager->dbSizeValid==0 );
37027 assert( pPager->state>=PAGER_SHARED || pPager->dbModified==0 );
37028
37029 /* Check that this is either a no-op (because the requested lock is
37030 ** already held, or one of the transistions that the busy-handler
37031 ** may be invoked during, according to the comment above
37032 ** sqlite3PagerSetBusyhandler().
37033 */
37034 assert( (pPager->state>=locktype)
37035 || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED)
37036 || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE)
37037 );
37038
37039 if( pPager->state>=locktype ){
37040 rc = SQLITE_OK;
37041 }else{
37042 do {
37043 rc = sqlite3OsLock(pPager->fd, locktype);
37044 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
37045 if( rc==SQLITE_OK ){
37046 pPager->state = (u8)locktype;
37047 IOTRACE(("LOCK %p %d\n", pPager, locktype))
37048 }
37049 }
37050 return rc;
37051 }
37052
37053 /*
37054 ** Function assertTruncateConstraint(pPager) checks that one of the
@@ -37089,13 +37701,12 @@
37089 ** function does not actually modify the database file on disk. It
37090 ** just sets the internal state of the pager object so that the
37091 ** truncation will be done when the current transaction is committed.
37092 */
37093 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
37094 assert( pPager->dbSizeValid );
37095 assert( pPager->dbSize>=nPage );
37096 assert( pPager->state>=PAGER_RESERVED );
37097 pPager->dbSize = nPage;
37098 assertTruncateConstraint(pPager);
37099 }
37100
37101
@@ -37141,11 +37752,11 @@
37141 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
37142 u8 *pTmp = (u8 *)pPager->pTmpSpace;
37143
37144 disable_simulated_io_errors();
37145 sqlite3BeginBenignMalloc();
37146 pPager->errCode = 0;
37147 pPager->exclusiveMode = 0;
37148 #ifndef SQLITE_OMIT_WAL
37149 sqlite3WalClose(pPager->pWal,
37150 (pPager->noSync ? 0 : pPager->sync_flags),
37151 pPager->pageSize, pTmp
@@ -37154,18 +37765,23 @@
37154 #endif
37155 pager_reset(pPager);
37156 if( MEMDB ){
37157 pager_unlock(pPager);
37158 }else{
37159 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
37160 ** call which may be made from within pagerUnlockAndRollback(). If it
37161 ** is not -1, then the unsynced portion of an open journal file may
37162 ** be played back into the database. If a power failure occurs while
37163 ** this is happening, the database may become corrupt.
 
 
 
 
 
37164 */
37165 if( isOpen(pPager->jfd) ){
37166 pPager->errCode = pagerSyncHotJournal(pPager);
37167 }
37168 pagerUnlockAndRollback(pPager);
37169 }
37170 sqlite3EndBenignMalloc();
37171 enable_simulated_io_errors();
@@ -37206,13 +37822,13 @@
37206 /*
37207 ** Sync the journal. In other words, make sure all the pages that have
37208 ** been written to the journal have actually reached the surface of the
37209 ** disk and can be restored in the event of a hot-journal rollback.
37210 **
37211 ** If the Pager.needSync flag is not set, then this function is a
37212 ** no-op. Otherwise, the actions required depend on the journal-mode
37213 ** and the device characteristics of the the file-system, as follows:
37214 **
37215 ** * If the journal file is an in-memory journal file, no action need
37216 ** be taken.
37217 **
37218 ** * Otherwise, if the device does not support the SAFE_APPEND property,
@@ -37232,22 +37848,29 @@
37232 ** <update nRec field>
37233 ** }
37234 ** if( NOT SEQUENTIAL ) xSync(<journal file>);
37235 ** }
37236 **
37237 ** The Pager.needSync flag is never be set for temporary files, or any
37238 ** file operating in no-sync mode (Pager.noSync set to non-zero).
37239 **
37240 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
37241 ** page currently held in memory before returning SQLITE_OK. If an IO
37242 ** error is encountered, then the IO error code is returned to the caller.
37243 */
37244 static int syncJournal(Pager *pPager){
37245 if( pPager->needSync ){
 
 
 
 
 
 
 
 
 
 
 
37246 assert( !pPager->tempFile );
37247 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
37248 int rc; /* Return code */
37249 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
37250 assert( isOpen(pPager->jfd) );
37251
37252 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
37253 /* This block deals with an obscure problem. If the last connection
@@ -37318,21 +37941,29 @@
37318 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
37319 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
37320 );
37321 if( rc!=SQLITE_OK ) return rc;
37322 }
37323 }
37324
37325 /* The journal file was just successfully synced. Set Pager.needSync
37326 ** to zero and clear the PGHDR_NEED_SYNC flag on all pagess.
37327 */
37328 pPager->needSync = 0;
37329 pPager->journalStarted = 1;
37330 pPager->journalHdr = pPager->journalOff;
37331 sqlite3PcacheClearSyncFlags(pPager->pPCache);
 
37332 }
37333
 
 
 
 
 
 
 
37334 return SQLITE_OK;
37335 }
37336
37337 /*
37338 ** The argument is the first in a linked list of dirty pages connected
@@ -37365,31 +37996,16 @@
37365 ** If everything is successful, SQLITE_OK is returned. If an IO error
37366 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
37367 ** be obtained, SQLITE_BUSY is returned.
37368 */
37369 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
37370 int rc; /* Return code */
37371
37372 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
37373 ** database file. If there is already an EXCLUSIVE lock, the following
37374 ** call is a no-op.
37375 **
37376 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
37377 ** through an intermediate state PENDING. A PENDING lock prevents new
37378 ** readers from attaching to the database but is unsufficient for us to
37379 ** write. The idea of a PENDING lock is to prevent new readers from
37380 ** coming in while we wait for existing readers to clear.
37381 **
37382 ** While the pager is in the RESERVED state, the original database file
37383 ** is unchanged and we can rollback without having to playback the
37384 ** journal into the original database file. Once we transition to
37385 ** EXCLUSIVE, it means the database file has been changed and any rollback
37386 ** will require a journal playback.
37387 */
37388 assert( !pagerUseWal(pPager) );
37389 assert( pPager->state>=PAGER_RESERVED );
37390 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
37391
37392 /* If the file is a temp-file has not yet been opened, open it now. It
37393 ** is not possible for rc to be other than SQLITE_OK if this branch
37394 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
37395 */
@@ -37400,13 +38016,14 @@
37400
37401 /* Before the first write, give the VFS a hint of what the final
37402 ** file size will be.
37403 */
37404 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
37405 if( rc==SQLITE_OK && pPager->dbSize>(pPager->dbOrigSize+1) ){
37406 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
37407 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
 
37408 }
37409
37410 while( rc==SQLITE_OK && pList ){
37411 Pgno pgno = pList->pgno;
37412
@@ -37419,10 +38036,12 @@
37419 ** set (set by sqlite3PagerDontWrite()).
37420 */
37421 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
37422 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
37423 char *pData; /* Data to write */
 
 
37424
37425 /* Encode the database */
37426 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
37427
37428 /* Write out the page data. */
@@ -37562,13 +38181,18 @@
37562 ** pages belonging to the same sector.
37563 **
37564 ** The doNotSpill flag inhibits all cache spilling regardless of whether
37565 ** or not a sync is required. This is set during a rollback.
37566 **
37567 ** Spilling is also inhibited when in an error state.
 
 
 
 
 
37568 */
37569 if( pPager->errCode ) return SQLITE_OK;
37570 if( pPager->doNotSpill ) return SQLITE_OK;
37571 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
37572 return SQLITE_OK;
37573 }
37574
@@ -37582,20 +38206,14 @@
37582 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
37583 }
37584 }else{
37585
37586 /* Sync the journal file if required. */
37587 if( pPg->flags&PGHDR_NEED_SYNC ){
37588 assert( !pPager->noSync );
37589 rc = syncJournal(pPager);
37590 if( rc==SQLITE_OK &&
37591 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37592 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37593 ){
37594 pPager->nRec = 0;
37595 rc = writeJournalHdr(pPager);
37596 }
37597 }
37598
37599 /* If the page number of this page is larger than the current size of
37600 ** the database image, it may need to be written to the sub-journal.
37601 ** This is because the call to pager_write_pagelist() below will not
@@ -37629,10 +38247,11 @@
37629 rc = subjournalPage(pPg);
37630 }
37631
37632 /* Write the contents of the page out to the database file. */
37633 if( rc==SQLITE_OK ){
 
37634 rc = pager_write_pagelist(pPager, pPg);
37635 }
37636 }
37637
37638 /* Mark the page as clean. */
@@ -37639,11 +38258,11 @@
37639 if( rc==SQLITE_OK ){
37640 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
37641 sqlite3PcacheMakeClean(pPg);
37642 }
37643
37644 return pager_error(pPager, rc);
37645 }
37646
37647
37648 /*
37649 ** Allocate and initialize a new Pager object and put a pointer to it
@@ -37694,11 +38313,11 @@
37694 char *zPathname = 0; /* Full path to database file */
37695 int nPathname = 0; /* Number of bytes in zPathname */
37696 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
37697 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
37698 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
37699 u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
37700
37701 /* Figure out how much space is required for each journal file-handle
37702 ** (there are two of them, the main journal and the sub-journal). This
37703 ** is the maximum space required for an in-memory journal file handle
37704 ** and a regular journal file-handle. Note that a "regular journal-handle"
@@ -37829,11 +38448,11 @@
37829 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
37830 if( szPageDflt<pPager->sectorSize ){
37831 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
37832 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
37833 }else{
37834 szPageDflt = (u16)pPager->sectorSize;
37835 }
37836 }
37837 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
37838 {
37839 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
@@ -37857,11 +38476,12 @@
37857 ** This branch is also run for an in-memory database. An in-memory
37858 ** database is the same as a temp-file that is never written out to
37859 ** disk and uses an in-memory rollback journal.
37860 */
37861 tempFile = 1;
37862 pPager->state = PAGER_EXCLUSIVE;
 
37863 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
37864 }
37865
37866 /* The following call to PagerSetPagesize() serves to set the value of
37867 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -37894,27 +38514,27 @@
37894 pPager->useJournal = (u8)useJournal;
37895 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
37896 /* pPager->stmtOpen = 0; */
37897 /* pPager->stmtInUse = 0; */
37898 /* pPager->nRef = 0; */
37899 pPager->dbSizeValid = (u8)memDb;
37900 /* pPager->stmtSize = 0; */
37901 /* pPager->stmtJSize = 0; */
37902 /* pPager->nPage = 0; */
37903 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
37904 /* pPager->state = PAGER_UNLOCK; */
 
37905 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
 
37906 /* pPager->errMask = 0; */
37907 pPager->tempFile = (u8)tempFile;
37908 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
37909 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
37910 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
37911 pPager->exclusiveMode = (u8)tempFile;
37912 pPager->changeCountDone = pPager->tempFile;
37913 pPager->memDb = (u8)memDb;
37914 pPager->readOnly = (u8)readOnly;
37915 /* pPager->needSync = 0; */
37916 assert( useJournal || pPager->tempFile );
37917 pPager->noSync = pPager->tempFile;
37918 pPager->fullSync = pPager->noSync ?0:1;
37919 pPager->sync_flags = SQLITE_SYNC_NORMAL;
37920 /* pPager->pFirst = 0; */
@@ -37975,24 +38595,24 @@
37975 sqlite3_vfs * const pVfs = pPager->pVfs;
37976 int rc = SQLITE_OK; /* Return code */
37977 int exists = 1; /* True if a journal file is present */
37978 int jrnlOpen = !!isOpen(pPager->jfd);
37979
37980 assert( pPager!=0 );
37981 assert( pPager->useJournal );
37982 assert( isOpen(pPager->fd) );
37983 assert( pPager->state <= PAGER_SHARED );
 
37984 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
37985 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
37986 ));
37987
37988 *pExists = 0;
37989 if( !jrnlOpen ){
37990 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
37991 }
37992 if( rc==SQLITE_OK && exists ){
37993 int locked; /* True if some process holds a RESERVED lock */
37994
37995 /* Race condition here: Another process might have been holding the
37996 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
37997 ** call above, but then delete the journal and drop the lock before
37998 ** we get to the following sqlite3OsCheckReservedLock() call. If that
@@ -38000,25 +38620,25 @@
38000 ** in fact there is none. This results in a false-positive which will
38001 ** be dealt with by the playback routine. Ticket #3883.
38002 */
38003 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
38004 if( rc==SQLITE_OK && !locked ){
38005 int nPage;
38006
38007 /* Check the size of the database file. If it consists of 0 pages,
38008 ** then delete the journal file. See the header comment above for
38009 ** the reasoning here. Delete the obsolete journal file under
38010 ** a RESERVED lock to avoid race conditions and to avoid violating
38011 ** [H33020].
38012 */
38013 rc = sqlite3PagerPagecount(pPager, &nPage);
38014 if( rc==SQLITE_OK ){
38015 if( nPage==0 ){
38016 sqlite3BeginBenignMalloc();
38017 if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){
38018 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
38019 sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
38020 }
38021 sqlite3EndBenignMalloc();
38022 }else{
38023 /* The journal file exists and no other connection has a reserved
38024 ** or greater lock on the database file. Now check that there is
@@ -38067,11 +38687,11 @@
38067 ** has been successfully called. If a shared-lock is already held when
38068 ** this function is called, it is a no-op.
38069 **
38070 ** The following operations are also performed by this function.
38071 **
38072 ** 1) If the pager is currently in PAGER_UNLOCK state (no lock held
38073 ** on the database file), then an attempt is made to obtain a
38074 ** SHARED lock on the database file. Immediately after obtaining
38075 ** the SHARED lock, the file-system is checked for a hot-journal,
38076 ** which is played back if present. Following any hot-journal
38077 ** rollback, the contents of the cache are validated by checking
@@ -38082,70 +38702,51 @@
38082 ** no outstanding references to any pages, and is in the error state,
38083 ** then an attempt is made to clear the error state by discarding
38084 ** the contents of the page cache and rolling back any open journal
38085 ** file.
38086 **
38087 ** If the operation described by (2) above is not attempted, and if the
38088 ** pager is in an error state other than SQLITE_FULL when this is called,
38089 ** the error state error code is returned. It is permitted to read the
38090 ** database when in SQLITE_FULL error state.
38091 **
38092 ** Otherwise, if everything is successful, SQLITE_OK is returned. If an
38093 ** IO error occurs while locking the database, checking for a hot-journal
38094 ** file or rolling back a journal file, the IO error code is returned.
38095 */
38096 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
38097 int rc = SQLITE_OK; /* Return code */
38098 int isErrorReset = 0; /* True if recovering from error state */
38099
38100 /* This routine is only called from b-tree and only when there are no
38101 ** outstanding pages */
 
 
 
38102 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
 
 
38103 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
38104
38105 /* If this database is in an error-state, now is a chance to clear
38106 ** the error. Discard the contents of the pager-cache and rollback
38107 ** any hot journal in the file-system.
38108 */
38109 if( pPager->errCode ){
38110 if( isOpen(pPager->jfd) || pPager->zJournal ){
38111 isErrorReset = 1;
38112 }
38113 pPager->errCode = SQLITE_OK;
38114 pager_reset(pPager);
38115 }
38116
38117 if( pagerUseWal(pPager) ){
38118 rc = pagerBeginReadTransaction(pPager);
38119 }else if( pPager->state==PAGER_UNLOCK || isErrorReset ){
38120 sqlite3_vfs * const pVfs = pPager->pVfs;
38121 int isHotJournal = 0;
38122 assert( !MEMDB );
38123 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
38124 if( pPager->noReadlock ){
38125 assert( pPager->readOnly );
38126 pPager->state = PAGER_SHARED;
38127 }else{
38128 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
38129 if( rc!=SQLITE_OK ){
38130 assert( pPager->state==PAGER_UNLOCK );
38131 return pager_error(pPager, rc);
38132 }
38133 }
38134 assert( pPager->state>=SHARED_LOCK );
38135
38136 /* If a journal file exists, and there is no RESERVED lock on the
38137 ** database file, then it either needs to be played back or deleted.
38138 */
38139 if( !isErrorReset ){
38140 assert( pPager->state <= PAGER_SHARED );
38141 rc = hasHotJournal(pPager, &isHotJournal);
38142 if( rc!=SQLITE_OK ){
38143 goto failed;
38144 }
38145 }
38146 if( isErrorReset || isHotJournal ){
38147 /* Get an EXCLUSIVE lock on the database file. At this point it is
38148 ** important that a RESERVED lock is not obtained on the way to the
38149 ** EXCLUSIVE lock. If it were, another process might open the
38150 ** database file, detect the RESERVED lock, and conclude that the
38151 ** database is safe to read while this process is still rolling the
@@ -38153,62 +38754,49 @@
38153 **
38154 ** Because the intermediate RESERVED lock is not requested, any
38155 ** other process attempting to access the database file will get to
38156 ** this point in the code and fail to obtain its own EXCLUSIVE lock
38157 ** on the database file.
38158 */
38159 if( pPager->state<EXCLUSIVE_LOCK ){
38160 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38161 if( rc!=SQLITE_OK ){
38162 rc = pager_error(pPager, rc);
38163 goto failed;
38164 }
38165 pPager->state = PAGER_EXCLUSIVE;
38166 }
38167
38168 /* Open the journal for read/write access. This is because in
38169 ** exclusive-access mode the file descriptor will be kept open and
38170 ** possibly used for a transaction later on. On some systems, the
38171 ** OsTruncate() call used in exclusive-access mode also requires
38172 ** a read/write file handle.
38173 */
38174 if( !isOpen(pPager->jfd) ){
38175 int res;
38176 rc = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS,&res);
38177 if( rc==SQLITE_OK ){
38178 if( res ){
38179 int fout = 0;
38180 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
38181 assert( !pPager->tempFile );
38182 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
38183 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38184 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
38185 rc = SQLITE_CANTOPEN_BKPT;
38186 sqlite3OsClose(pPager->jfd);
38187 }
38188 }else{
38189 /* If the journal does not exist, it usually means that some
38190 ** other connection managed to get in and roll it back before
38191 ** this connection obtained the exclusive lock above. Or, it
38192 ** may mean that the pager was in the error-state when this
38193 ** function was called and the journal file does not exist. */
38194 rc = pager_end_transaction(pPager, 0);
38195 }
38196 }
38197 }
38198 if( rc!=SQLITE_OK ){
38199 goto failed;
38200 }
38201
38202 /* Reset the journal status fields to indicates that we have no
38203 ** rollback journal at this time. */
38204 pPager->journalStarted = 0;
38205 pPager->journalOff = 0;
38206 pPager->setMaster = 0;
38207 pPager->journalHdr = 0;
38208
38209 /* Make sure the journal file has been synced to disk. */
38210
38211 /* Playback and delete the journal. Drop the database write
38212 ** lock and reacquire the read lock. Purge the cache before
38213 ** playing back the hot-journal so that we don't end up with
38214 ** an inconsistent cache. Sync the hot journal before playing
@@ -38215,25 +38803,50 @@
38215 ** it back since the process that crashed and left the hot journal
38216 ** probably did not sync it and we are required to always sync
38217 ** the journal before playing it back.
38218 */
38219 if( isOpen(pPager->jfd) ){
 
38220 rc = pagerSyncHotJournal(pPager);
38221 if( rc==SQLITE_OK ){
38222 rc = pager_playback(pPager, 1);
38223 }
38224 if( rc!=SQLITE_OK ){
38225 rc = pager_error(pPager, rc);
38226 goto failed;
38227 }
38228 }
38229 assert( (pPager->state==PAGER_SHARED)
38230 || (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38231 );
38232 }
38233
38234 if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){
 
 
38235 /* The shared-lock has just been acquired on the database file
38236 ** and there are already pages in the cache (from a previous
38237 ** read or write transaction). Check to see if the database
38238 ** has been modified. If the database has changed, flush the
38239 ** cache.
@@ -38246,18 +38859,15 @@
38246 **
38247 ** There is a vanishingly small chance that a change will not be
38248 ** detected. The chance of an undetected change is so small that
38249 ** it can be neglected.
38250 */
38251 int nPage = 0;
38252 char dbFileVers[sizeof(pPager->dbFileVers)];
38253 sqlite3PagerPagecount(pPager, &nPage);
38254
38255 if( pPager->errCode ){
38256 rc = pPager->errCode;
38257 goto failed;
38258 }
38259
38260 if( nPage>0 ){
38261 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
38262 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
38263 if( rc!=SQLITE_OK ){
@@ -38269,22 +38879,34 @@
38269
38270 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
38271 pager_reset(pPager);
38272 }
38273 }
38274 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
38275
38276 /* If there is a WAL file in the file-system, open this database in WAL
38277 ** mode. Otherwise, the following function call is a no-op.
38278 */
38279 rc = pagerOpenWalIfPresent(pPager);
 
 
 
 
 
 
 
 
 
 
38280 }
38281
38282 failed:
38283 if( rc!=SQLITE_OK ){
38284 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
38285 pager_unlock(pPager);
 
 
 
38286 }
38287 return rc;
38288 }
38289
38290 /*
@@ -38294,13 +38916,11 @@
38294 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
38295 ** the rollback journal, the unlock is not performed and there is
38296 ** nothing to rollback, so this routine is a no-op.
38297 */
38298 static void pagerUnlockIfUnused(Pager *pPager){
38299 if( (sqlite3PcacheRefCount(pPager->pPCache)==0)
38300 && (!pPager->exclusiveMode || pPager->journalOff>0)
38301 ){
38302 pagerUnlockAndRollback(pPager);
38303 }
38304 }
38305
38306 /*
@@ -38360,20 +38980,20 @@
38360 int noContent /* Do not bother reading content from disk if true */
38361 ){
38362 int rc;
38363 PgHdr *pPg;
38364
 
38365 assert( assert_pager_state(pPager) );
38366 assert( pPager->state>PAGER_UNLOCK );
38367
38368 if( pgno==0 ){
38369 return SQLITE_CORRUPT_BKPT;
38370 }
38371
38372 /* If the pager is in the error state, return an error immediately.
38373 ** Otherwise, request the page from the PCache layer. */
38374 if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){
38375 rc = pPager->errCode;
38376 }else{
38377 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
38378 }
38379
@@ -38395,11 +39015,10 @@
38395 return SQLITE_OK;
38396
38397 }else{
38398 /* The pager cache has created a new page. Its content needs to
38399 ** be initialized. */
38400 int nMax;
38401
38402 PAGER_INCR(pPager->nMiss);
38403 pPg = *ppPage;
38404 pPg->pPager = pPager;
38405
@@ -38408,16 +39027,11 @@
38408 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
38409 rc = SQLITE_CORRUPT_BKPT;
38410 goto pager_acquire_err;
38411 }
38412
38413 rc = sqlite3PagerPagecount(pPager, &nMax);
38414 if( rc!=SQLITE_OK ){
38415 goto pager_acquire_err;
38416 }
38417
38418 if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
38419 if( pgno>pPager->mxPgno ){
38420 rc = SQLITE_FULL;
38421 goto pager_acquire_err;
38422 }
38423 if( noContent ){
@@ -38464,13 +39078,11 @@
38464 }
38465
38466 /*
38467 ** Acquire a page if it is already in the in-memory cache. Do
38468 ** not read the page from disk. Return a pointer to the page,
38469 ** or 0 if the page is not in cache. Also, return 0 if the
38470 ** pager is in PAGER_UNLOCK state when this function is called,
38471 ** or if the pager is in an error state other than SQLITE_FULL.
38472 **
38473 ** See also sqlite3PagerGet(). The difference between this routine
38474 ** and sqlite3PagerGet() is that _get() will go to the disk and read
38475 ** in the page if the page is not already in cache. This routine
38476 ** returns NULL if the page is not in cache or if a disk I/O error
@@ -38479,11 +39091,11 @@
38479 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
38480 PgHdr *pPg = 0;
38481 assert( pPager!=0 );
38482 assert( pgno!=0 );
38483 assert( pPager->pPCache!=0 );
38484 assert( pPager->state > PAGER_UNLOCK );
38485 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
38486 return pPg;
38487 }
38488
38489 /*
@@ -38524,73 +39136,71 @@
38524 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
38525 ** an IO error code if opening or writing the journal file fails.
38526 */
38527 static int pager_open_journal(Pager *pPager){
38528 int rc = SQLITE_OK; /* Return code */
38529 int nPage; /* Size of database file */
38530 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
38531
38532 assert( pPager->state>=PAGER_RESERVED );
38533 assert( pPager->useJournal );
38534 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF );
38535 assert( pPager->pInJournal==0 );
38536
38537 /* If already in the error state, this function is a no-op. But on
38538 ** the other hand, this routine is never called if we are already in
38539 ** an error state. */
38540 if( NEVER(pPager->errCode) ) return pPager->errCode;
38541
38542 testcase( pPager->dbSizeValid==0 );
38543 rc = sqlite3PagerPagecount(pPager, &nPage);
38544 if( rc ) return rc;
38545 pPager->pInJournal = sqlite3BitvecCreate(nPage);
38546 if( pPager->pInJournal==0 ){
38547 return SQLITE_NOMEM;
38548 }
38549
38550 /* Open the journal file if it is not already open. */
38551 if( !isOpen(pPager->jfd) ){
38552 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
38553 sqlite3MemJournalOpen(pPager->jfd);
38554 }else{
38555 const int flags = /* VFS flags to open journal file */
38556 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
38557 (pPager->tempFile ?
38558 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
38559 (SQLITE_OPEN_MAIN_JOURNAL)
38560 );
38561 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
38562 rc = sqlite3JournalOpen(
38563 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
38564 );
38565 #else
38566 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
38567 #endif
38568 }
38569 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38570 }
38571
38572
38573 /* Write the first journal header to the journal file and open
38574 ** the sub-journal if necessary.
38575 */
38576 if( rc==SQLITE_OK ){
38577 /* TODO: Check if all of these are really required. */
38578 pPager->dbOrigSize = pPager->dbSize;
38579 pPager->journalStarted = 0;
38580 pPager->needSync = 0;
38581 pPager->nRec = 0;
38582 pPager->journalOff = 0;
38583 pPager->setMaster = 0;
38584 pPager->journalHdr = 0;
38585 rc = writeJournalHdr(pPager);
38586 }
38587
38588 if( rc!=SQLITE_OK ){
38589 sqlite3BitvecDestroy(pPager->pInJournal);
38590 pPager->pInJournal = 0;
 
 
 
38591 }
 
38592 return rc;
38593 }
38594
38595 /*
38596 ** Begin a write-transaction on the specified pager object. If a
@@ -38599,18 +39209,10 @@
38599 ** If the exFlag argument is false, then acquire at least a RESERVED
38600 ** lock on the database file. If exFlag is true, then acquire at least
38601 ** an EXCLUSIVE lock. If such a lock is already held, no locking
38602 ** functions need be called.
38603 **
38604 ** If this is not a temporary or in-memory file and, the journal file is
38605 ** opened if it has not been already. For a temporary file, the opening
38606 ** of the journal file is deferred until there is an actual need to
38607 ** write to the journal. TODO: Why handle temporary files differently?
38608 **
38609 ** If the journal file is opened (or if it is already open), then a
38610 ** journal-header is written to the start of it.
38611 **
38612 ** If the subjInMemory argument is non-zero, then any sub-journal opened
38613 ** within this transaction will be opened as an in-memory file. This
38614 ** has no effect if the sub-journal is already opened (as it may be when
38615 ** running in exclusive mode) or if the transaction does not require a
38616 ** sub-journal. If the subjInMemory argument is zero, then any required
@@ -38617,24 +39219,24 @@
38617 ** sub-journal is implemented in-memory if pPager is an in-memory database,
38618 ** or using a temporary file otherwise.
38619 */
38620 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
38621 int rc = SQLITE_OK;
38622 assert( pPager->state!=PAGER_UNLOCK );
 
 
38623 pPager->subjInMemory = (u8)subjInMemory;
38624
38625 if( pPager->state==PAGER_SHARED ){
38626 assert( pPager->pInJournal==0 );
38627 assert( !MEMDB && !pPager->tempFile );
38628
38629 if( pagerUseWal(pPager) ){
38630 /* If the pager is configured to use locking_mode=exclusive, and an
38631 ** exclusive lock on the database is not already held, obtain it now.
38632 */
38633 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
38634 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38635 pPager->state = PAGER_SHARED;
38636 if( rc!=SQLITE_OK ){
38637 return rc;
38638 }
38639 sqlite3WalExclusiveMode(pPager->pWal, 1);
38640 }
@@ -38641,56 +39243,44 @@
38641
38642 /* Grab the write lock on the log file. If successful, upgrade to
38643 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
38644 ** The busy-handler is not invoked if another connection already
38645 ** holds the write-lock. If possible, the upper layer will call it.
38646 **
38647 ** WAL mode sets Pager.state to PAGER_RESERVED when it has an open
38648 ** transaction, but never to PAGER_EXCLUSIVE. This is because in
38649 ** PAGER_EXCLUSIVE state the code to roll back savepoint transactions
38650 ** may copy data from the sub-journal into the database file as well
38651 ** as into the page cache. Which would be incorrect in WAL mode.
38652 */
38653 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
38654 if( rc==SQLITE_OK ){
38655 pPager->dbOrigSize = pPager->dbSize;
38656 pPager->state = PAGER_RESERVED;
38657 pPager->journalOff = 0;
38658 }
38659
38660 assert( rc!=SQLITE_OK || pPager->state==PAGER_RESERVED );
38661 assert( rc==SQLITE_OK || pPager->state==PAGER_SHARED );
38662 }else{
38663 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
38664 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
38665 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
38666 ** lock, but not when obtaining the RESERVED lock.
38667 */
38668 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
38669 if( rc==SQLITE_OK ){
38670 pPager->state = PAGER_RESERVED;
38671 if( exFlag ){
38672 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38673 }
38674 }
38675 }
38676
38677 /* No need to open the journal file at this time. It will be
38678 ** opened before it is written to. If we defer opening the journal,
38679 ** we might save the work of creating a file if the transaction
38680 ** ends up being a no-op.
38681 */
38682
38683 if( rc!=SQLITE_OK ){
38684 assert( !pPager->dbModified );
38685 /* Ignore any IO error that occurs within pager_end_transaction(). The
38686 ** purpose of this call is to reset the internal state of the pager
38687 ** sub-system. It doesn't matter if the journal-file is not properly
38688 ** finalized at this point (since it is not a valid journal file anyway).
38689 */
38690 pager_end_transaction(pPager, 0);
38691 }
 
 
38692 }
38693
38694 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
38695 return rc;
38696 }
@@ -38705,110 +39295,98 @@
38705 static int pager_write(PgHdr *pPg){
38706 void *pData = pPg->pData;
38707 Pager *pPager = pPg->pPager;
38708 int rc = SQLITE_OK;
38709
38710 /* This routine is not called unless a transaction has already been
38711 ** started.
 
38712 */
38713 assert( pPager->state>=PAGER_RESERVED );
 
 
 
 
38714
38715 /* If an error has been previously detected, report the same error
38716 ** again.
38717 */
38718 if( NEVER(pPager->errCode) ) return pPager->errCode;
38719
38720 /* Higher-level routines never call this function if database is not
38721 ** writable. But check anyway, just for robustness. */
38722 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
38723
38724 assert( !pPager->setMaster );
38725
38726 CHECK_PAGE(pPg);
38727
38728 /* Mark the page as dirty. If the page has already been written
38729 ** to the journal then we can return right away.
38730 */
38731 sqlite3PcacheMakeDirty(pPg);
38732 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
38733 assert( !pagerUseWal(pPager) );
38734 pPager->dbModified = 1;
38735 }else{
38736
38737 /* If we get this far, it means that the page needs to be
38738 ** written to the transaction journal or the ckeckpoint journal
38739 ** or both.
38740 **
38741 ** Higher level routines should have already started a transaction,
38742 ** which means they have acquired the necessary locks but the rollback
38743 ** journal might not yet be open.
38744 */
38745 assert( pPager->state>=RESERVED_LOCK );
38746 if( pPager->pInJournal==0
38747 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38748 && !pagerUseWal(pPager)
38749 ){
38750 assert( pPager->useJournal );
38751 rc = pager_open_journal(pPager);
38752 if( rc!=SQLITE_OK ) return rc;
38753 }
38754 pPager->dbModified = 1;
 
38755
38756 /* The transaction journal now exists and we have a RESERVED or an
38757 ** EXCLUSIVE lock on the main database file. Write the current page to
38758 ** the transaction journal if it is not there already.
38759 */
38760 if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
38761 assert( !pagerUseWal(pPager) );
38762 if( pPg->pgno<=pPager->dbOrigSize ){
38763 u32 cksum;
38764 char *pData2;
 
38765
38766 /* We should never write to the journal file the page that
38767 ** contains the database locks. The following assert verifies
38768 ** that we do not. */
38769 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
38770
38771 assert( pPager->journalHdr <= pPager->journalOff );
38772 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
38773 cksum = pager_cksum(pPager, (u8*)pData2);
38774 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
38775 if( rc==SQLITE_OK ){
38776 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
38777 pPager->journalOff + 4);
38778 pPager->journalOff += pPager->pageSize+4;
38779 }
38780 if( rc==SQLITE_OK ){
38781 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
38782 pPager->journalOff += 4;
38783 }
 
 
 
 
 
 
 
38784 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
38785 pPager->journalOff, pPager->pageSize));
38786 PAGER_INCR(sqlite3_pager_writej_count);
38787 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
38788 PAGERID(pPager), pPg->pgno,
38789 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
38790
38791 /* Even if an IO or diskfull error occurred while journalling the
38792 ** page in the block above, set the need-sync flag for the page.
38793 ** Otherwise, when the transaction is rolled back, the logic in
38794 ** playback_one_page() will think that the page needs to be restored
38795 ** in the database file. And if an IO error occurs while doing so,
38796 ** then corruption may follow.
38797 */
38798 if( !pPager->noSync ){
38799 pPg->flags |= PGHDR_NEED_SYNC;
38800 pPager->needSync = 1;
38801 }
38802
38803 /* An error has occurred writing to the journal file. The
38804 ** transaction will be rolled back by the layer above.
38805 */
38806 if( rc!=SQLITE_OK ){
38807 return rc;
38808 }
38809
38810 pPager->nRec++;
38811 assert( pPager->pInJournal!=0 );
38812 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
38813 testcase( rc==SQLITE_NOMEM );
38814 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
@@ -38816,13 +39394,12 @@
38816 if( rc!=SQLITE_OK ){
38817 assert( rc==SQLITE_NOMEM );
38818 return rc;
38819 }
38820 }else{
38821 if( !pPager->journalStarted && !pPager->noSync ){
38822 pPg->flags |= PGHDR_NEED_SYNC;
38823 pPager->needSync = 1;
38824 }
38825 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
38826 PAGERID(pPager), pPg->pgno,
38827 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
38828 }
@@ -38838,11 +39415,10 @@
38838 }
38839 }
38840
38841 /* Update the database size and return.
38842 */
38843 assert( pPager->state>=PAGER_SHARED );
38844 if( pPager->dbSize<pPg->pgno ){
38845 pPager->dbSize = pPg->pgno;
38846 }
38847 return rc;
38848 }
@@ -38866,10 +39442,14 @@
38866
38867 PgHdr *pPg = pDbPage;
38868 Pager *pPager = pPg->pPager;
38869 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
38870
 
 
 
 
38871 if( nPagePerSector>1 ){
38872 Pgno nPageCount; /* Total number of pages in database file */
38873 Pgno pg1; /* First page of the sector pPg is located on. */
38874 int nPage = 0; /* Number of pages starting at pg1 to journal */
38875 int ii; /* Loop counter */
@@ -38887,23 +39467,21 @@
38887 ** an integer power of 2. It sets variable pg1 to the identifier
38888 ** of the first page of the sector pPg is located on.
38889 */
38890 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
38891
38892 rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount);
38893 if( rc==SQLITE_OK ){
38894 if( pPg->pgno>nPageCount ){
38895 nPage = (pPg->pgno - pg1)+1;
38896 }else if( (pg1+nPagePerSector-1)>nPageCount ){
38897 nPage = nPageCount+1-pg1;
38898 }else{
38899 nPage = nPagePerSector;
38900 }
38901 assert(nPage>0);
38902 assert(pg1<=pPg->pgno);
38903 assert((pg1+nPage)>pPg->pgno);
38904 }
38905
38906 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
38907 Pgno pg = pg1+ii;
38908 PgHdr *pPage;
38909 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38911,11 +39489,10 @@
38911 rc = sqlite3PagerGet(pPager, pg, &pPage);
38912 if( rc==SQLITE_OK ){
38913 rc = pager_write(pPage);
38914 if( pPage->flags&PGHDR_NEED_SYNC ){
38915 needSync = 1;
38916 assert(pPager->needSync);
38917 }
38918 sqlite3PagerUnref(pPage);
38919 }
38920 }
38921 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
@@ -38931,19 +39508,18 @@
38931 ** writing to any of these nPage pages may damage the others, the
38932 ** journal file must contain sync()ed copies of all of them
38933 ** before any of them can be written out to the database file.
38934 */
38935 if( rc==SQLITE_OK && needSync ){
38936 assert( !MEMDB && pPager->noSync==0 );
38937 for(ii=0; ii<nPage; ii++){
38938 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
38939 if( pPage ){
38940 pPage->flags |= PGHDR_NEED_SYNC;
38941 sqlite3PagerUnref(pPage);
38942 }
38943 }
38944 assert(pPager->needSync);
38945 }
38946
38947 assert( pPager->doNotSyncSpill==1 );
38948 pPager->doNotSyncSpill--;
38949 }else{
@@ -39005,10 +39581,15 @@
39005 ** by writing an updated version of page 1 using a call to the
39006 ** sqlite3OsWrite() function.
39007 */
39008 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
39009 int rc = SQLITE_OK;
 
 
 
 
 
39010
39011 /* Declare and initialize constant integer 'isDirect'. If the
39012 ** atomic-write optimization is enabled in this build, then isDirect
39013 ** is initialized to the value passed as the isDirectMode parameter
39014 ** to this function. Otherwise, it is always set to zero.
@@ -39024,11 +39605,10 @@
39024 UNUSED_PARAMETER(isDirectMode);
39025 #else
39026 # define DIRECT_MODE isDirectMode
39027 #endif
39028
39029 assert( pPager->state>=PAGER_RESERVED );
39030 if( !pPager->changeCountDone && pPager->dbSize>0 ){
39031 PgHdr *pPgHdr; /* Reference to page 1 */
39032 u32 change_counter; /* Initial value of change-counter field */
39033
39034 assert( !pPager->tempFile && isOpen(pPager->fd) );
@@ -39109,13 +39689,17 @@
39109 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
39110 ** returned.
39111 */
39112 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
39113 int rc = SQLITE_OK;
39114 assert( pPager->state>=PAGER_RESERVED );
 
 
 
 
39115 if( 0==pagerUseWal(pPager) ){
39116 rc = pager_wait_on_lock(pPager, PAGER_EXCLUSIVE);
39117 }
39118 return rc;
39119 }
39120
39121 /*
@@ -39149,26 +39733,33 @@
39149 const char *zMaster, /* If not NULL, the master journal name */
39150 int noSync /* True to omit the xSync on the db file */
39151 ){
39152 int rc = SQLITE_OK; /* Return code */
39153
39154 /* The dbOrigSize is never set if journal_mode=OFF */
39155 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
 
 
 
 
39156
39157 /* If a prior error occurred, report that error again. */
39158 if( pPager->errCode ) return pPager->errCode;
39159
39160 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
39161 pPager->zFilename, zMaster, pPager->dbSize));
39162
39163 if( MEMDB && pPager->dbModified ){
 
 
 
39164 /* If this is an in-memory db, or no pages have been written to, or this
39165 ** function has already been called, it is mostly a no-op. However, any
39166 ** backup in progress needs to be restarted.
39167 */
39168 sqlite3BackupRestart(pPager->pBackup);
39169 }else if( pPager->dbModified ){
39170 if( pagerUseWal(pPager) ){
39171 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
39172 if( pList ){
39173 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
39174 (pPager->fullSync ? pPager->sync_flags : 0)
@@ -39207,11 +39798,11 @@
39207 || pPager->journalMode==PAGER_JOURNALMODE_OFF
39208 || pPager->journalMode==PAGER_JOURNALMODE_WAL
39209 );
39210 if( !zMaster && isOpen(pPager->jfd)
39211 && pPager->journalOff==jrnlBufferSize(pPager)
39212 && pPager->dbSize>=pPager->dbFileSize
39213 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
39214 ){
39215 /* Update the db file change counter via the direct-write method. The
39216 ** following call will modify the in-memory representation of page 1
39217 ** to include the updated change counter and then write page 1
@@ -39237,17 +39828,14 @@
39237 ** Before reading the pages with page numbers larger than the
39238 ** current value of Pager.dbSize, set dbSize back to the value
39239 ** that it took at the start of the transaction. Otherwise, the
39240 ** calls to sqlite3PagerGet() return zeroed pages instead of
39241 ** reading data from the database file.
39242 **
39243 ** When journal_mode==OFF the dbOrigSize is always zero, so this
39244 ** block never runs if journal_mode=OFF.
39245 */
39246 #ifndef SQLITE_OMIT_AUTOVACUUM
39247 if( pPager->dbSize<pPager->dbOrigSize
39248 && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
39249 ){
39250 Pgno i; /* Iterator variable */
39251 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
39252 const Pgno dbSize = pPager->dbSize; /* Database image size */
39253 pPager->dbSize = pPager->dbOrigSize;
@@ -39270,18 +39858,24 @@
39270 ** or if zMaster is NULL (no master journal), then this call is a no-op.
39271 */
39272 rc = writeMasterJournal(pPager, zMaster);
39273 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39274
39275 /* Sync the journal file. If the atomic-update optimization is being
39276 ** used, this call will not create the journal file or perform any
39277 ** real IO.
 
 
 
 
 
 
 
39278 */
39279 rc = syncJournal(pPager);
39280 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39281
39282 /* Write all dirty pages to the database file. */
39283 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
39284 if( rc!=SQLITE_OK ){
39285 assert( rc!=SQLITE_IOERR_BLOCKED );
39286 goto commit_phase_one_exit;
39287 }
@@ -39290,11 +39884,11 @@
39290 /* If the file on disk is not the same size as the database image,
39291 ** then use pager_truncate to grow or shrink the file here.
39292 */
39293 if( pPager->dbSize!=pPager->dbFileSize ){
39294 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
39295 assert( pPager->state>=PAGER_EXCLUSIVE );
39296 rc = pager_truncate(pPager, nNew);
39297 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39298 }
39299
39300 /* Finally, sync the database file. */
@@ -39301,16 +39895,16 @@
39301 if( !pPager->noSync && !noSync ){
39302 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
39303 }
39304 IOTRACE(("DBSYNC %p\n", pPager))
39305 }
39306
39307 assert( pPager->state!=PAGER_SYNCED );
39308 pPager->state = PAGER_SYNCED;
39309 }
39310
39311 commit_phase_one_exit:
 
 
 
39312 return rc;
39313 }
39314
39315
39316 /*
@@ -39334,16 +39928,15 @@
39334 /* This routine should not be called if a prior error has occurred.
39335 ** But if (due to a coding error elsewhere in the system) it does get
39336 ** called, just return the same error code without doing anything. */
39337 if( NEVER(pPager->errCode) ) return pPager->errCode;
39338
39339 /* This function should not be called if the pager is not in at least
39340 ** PAGER_RESERVED state. **FIXME**: Make it so that this test always
39341 ** fails - make it so that we never reach this point if we do not hold
39342 ** all necessary locks.
39343 */
39344 if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
39345
39346 /* An optimization. If the database was not actually modified during
39347 ** this transaction, the pager is running in exclusive-mode and is
39348 ** using persistent journals, then this function is a no-op.
39349 **
@@ -39352,106 +39945,80 @@
39352 ** a hot-journal during hot-journal rollback, 0 changes will be made
39353 ** to the database file. So there is no need to zero the journal
39354 ** header. Since the pager is in exclusive mode, there is no need
39355 ** to drop any locks either.
39356 */
39357 if( pPager->dbModified==0 && pPager->exclusiveMode
 
39358 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
39359 ){
39360 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
 
39361 return SQLITE_OK;
39362 }
39363
39364 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
39365 assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified );
39366 rc = pager_end_transaction(pPager, pPager->setMaster);
39367 return pager_error(pPager, rc);
39368 }
39369
39370 /*
39371 ** Rollback all changes. The database falls back to PAGER_SHARED mode.
 
 
 
39372 **
39373 ** This function performs two tasks:
 
 
 
39374 **
39375 ** 1) It rolls back the journal file, restoring all database file and
39376 ** in-memory cache pages to the state they were in when the transaction
39377 ** was opened, and
 
39378 ** 2) It finalizes the journal file, so that it is not used for hot
39379 ** rollback at any point in the future.
39380 **
39381 ** subject to the following qualifications:
39382 **
39383 ** * If the journal file is not yet open when this function is called,
39384 ** then only (2) is performed. In this case there is no journal file
39385 ** to roll back.
39386 **
39387 ** * If in an error state other than SQLITE_FULL, then task (1) is
39388 ** performed. If successful, task (2). Regardless of the outcome
39389 ** of either, the error state error code is returned to the caller
39390 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
39391 **
39392 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
39393 ** or not (1) is successful, also attempt (2). If successful, return
39394 ** SQLITE_OK. Otherwise, enter the error state and return the first
39395 ** error code encountered.
39396 **
39397 ** In this case there is no chance that the database was written to.
39398 ** So is safe to finalize the journal file even if the playback
39399 ** (operation 1) failed. However the pager must enter the error state
39400 ** as the contents of the in-memory cache are now suspect.
39401 **
39402 ** * Finally, if in PAGER_EXCLUSIVE state, then attempt (1). Only
39403 ** attempt (2) if (1) is successful. Return SQLITE_OK if successful,
39404 ** otherwise enter the error state and return the error code from the
39405 ** failing operation.
39406 **
39407 ** In this case the database file may have been written to. So if the
39408 ** playback operation did not succeed it would not be safe to finalize
39409 ** the journal file. It needs to be left in the file-system so that
39410 ** some other process can use it to restore the database state (by
39411 ** hot-journal rollback).
39412 */
39413 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
39414 int rc = SQLITE_OK; /* Return code */
39415 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
 
 
 
 
 
 
 
 
 
39416 if( pagerUseWal(pPager) ){
39417 int rc2;
39418
39419 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
39420 rc2 = pager_end_transaction(pPager, pPager->setMaster);
39421 if( rc==SQLITE_OK ) rc = rc2;
39422 rc = pager_error(pPager, rc);
39423 }else if( !pPager->dbModified || !isOpen(pPager->jfd) ){
39424 rc = pager_end_transaction(pPager, pPager->setMaster);
39425 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
39426 if( pPager->state>=PAGER_EXCLUSIVE ){
39427 pager_playback(pPager, 0);
39428 }
39429 rc = pPager->errCode;
39430 }else{
39431 if( pPager->state==PAGER_RESERVED ){
39432 int rc2;
39433 rc = pager_playback(pPager, 0);
39434 rc2 = pager_end_transaction(pPager, pPager->setMaster);
39435 if( rc==SQLITE_OK ){
39436 rc = rc2;
39437 }
39438 }else{
39439 rc = pager_playback(pPager, 0);
39440 }
39441
39442 if( !MEMDB ){
39443 pPager->dbSizeValid = 0;
39444 }
39445
39446 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
39447 ** cache. So call pager_error() on the way out to make any error
39448 ** persistent.
39449 */
39450 rc = pager_error(pPager, rc);
39451 }
39452 return rc;
39453 }
39454
39455 /*
39456 ** Return TRUE if the database file is opened read-only. Return FALSE
39457 ** if the database is (in theory) writable.
@@ -39493,12 +40060,12 @@
39493 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
39494 static int a[11];
39495 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
39496 a[1] = sqlite3PcachePagecount(pPager->pPCache);
39497 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
39498 a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1;
39499 a[4] = pPager->state;
39500 a[5] = pPager->errCode;
39501 a[6] = pPager->nHit;
39502 a[7] = pPager->nMiss;
39503 a[8] = 0; /* Used to be pPager->nOvfl */
39504 a[9] = pPager->nRead;
@@ -39525,18 +40092,17 @@
39525 ** returned. Otherwise, SQLITE_OK.
39526 */
39527 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
39528 int rc = SQLITE_OK; /* Return code */
39529 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
 
 
 
39530
39531 if( nSavepoint>nCurrent && pPager->useJournal ){
39532 int ii; /* Iterator variable */
39533 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
39534 int nPage; /* Size of database file */
39535
39536 rc = sqlite3PagerPagecount(pPager, &nPage);
39537 if( rc ) return rc;
39538
39539 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
39540 ** if the allocation fails. Otherwise, zero the new portion in case a
39541 ** malloc failure occurs while populating it in the for(...) loop below.
39542 */
@@ -39549,18 +40115,18 @@
39549 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
39550 pPager->aSavepoint = aNew;
39551
39552 /* Populate the PagerSavepoint structures just allocated. */
39553 for(ii=nCurrent; ii<nSavepoint; ii++){
39554 aNew[ii].nOrig = nPage;
39555 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
39556 aNew[ii].iOffset = pPager->journalOff;
39557 }else{
39558 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
39559 }
39560 aNew[ii].iSubRec = pPager->nSubRec;
39561 aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage);
39562 if( !aNew[ii].pInSavepoint ){
39563 return SQLITE_NOMEM;
39564 }
39565 if( pagerUseWal(pPager) ){
39566 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
@@ -39603,16 +40169,16 @@
39603 ** This function may return SQLITE_NOMEM if a memory allocation fails,
39604 ** or an IO error code if an IO error occurs while rolling back a
39605 ** savepoint. If no errors occur, SQLITE_OK is returned.
39606 */
39607 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
39608 int rc = SQLITE_OK;
39609
39610 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
39611 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
39612
39613 if( iSavepoint<pPager->nSavepoint ){
39614 int ii; /* Iterator variable */
39615 int nNew; /* Number of remaining savepoints after this op. */
39616
39617 /* Figure out how many savepoints will still be active after this
39618 ** operation. Store this value in nNew. Then free resources associated
@@ -39644,12 +40210,12 @@
39644 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
39645 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
39646 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
39647 assert(rc!=SQLITE_DONE);
39648 }
39649
39650 }
 
39651 return rc;
39652 }
39653
39654 /*
39655 ** Return the full pathname of the database file.
@@ -39743,10 +40309,14 @@
39743 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
39744 int rc; /* Return code */
39745 Pgno origPgno; /* The original page number */
39746
39747 assert( pPg->nRef>0 );
 
 
 
 
39748
39749 /* In order to be able to rollback, an in-memory database must journal
39750 ** the page we are moving from.
39751 */
39752 if( MEMDB ){
@@ -39792,15 +40362,14 @@
39792 */
39793 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
39794 needSyncPgno = pPg->pgno;
39795 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
39796 assert( pPg->flags&PGHDR_DIRTY );
39797 assert( pPager->needSync );
39798 }
39799
39800 /* If the cache contains a page with page-number pgno, remove it
39801 ** from its hash chain. Also, if the PgHdr.needSync was set for
39802 ** page pgno before the 'move' operation, it needs to be retained
39803 ** for the page moved there.
39804 */
39805 pPg->flags &= ~PGHDR_NEED_SYNC;
39806 pPgOld = pager_lookup(pPager, pgno);
@@ -39808,67 +40377,59 @@
39808 if( pPgOld ){
39809 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
39810 if( MEMDB ){
39811 /* Do not discard pages from an in-memory database since we might
39812 ** need to rollback later. Just move the page out of the way. */
39813 assert( pPager->dbSizeValid );
39814 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
39815 }else{
39816 sqlite3PcacheDrop(pPgOld);
39817 }
39818 }
39819
39820 origPgno = pPg->pgno;
39821 sqlite3PcacheMove(pPg, pgno);
39822 sqlite3PcacheMakeDirty(pPg);
39823 pPager->dbModified = 1;
 
 
 
 
 
 
 
 
 
39824
39825 if( needSyncPgno ){
39826 /* If needSyncPgno is non-zero, then the journal file needs to be
39827 ** sync()ed before any data is written to database file page needSyncPgno.
39828 ** Currently, no such page exists in the page-cache and the
39829 ** "is journaled" bitvec flag has been set. This needs to be remedied by
39830 ** loading the page into the pager-cache and setting the PgHdr.needSync
39831 ** flag.
39832 **
39833 ** If the attempt to load the page into the page-cache fails, (due
39834 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
39835 ** array. Otherwise, if the page is loaded and written again in
39836 ** this transaction, it may be written to the database file before
39837 ** it is synced into the journal file. This way, it may end up in
39838 ** the journal file twice, but that is not a problem.
39839 **
39840 ** The sqlite3PagerGet() call may cause the journal to sync. So make
39841 ** sure the Pager.needSync flag is set too.
39842 */
39843 PgHdr *pPgHdr;
39844 assert( pPager->needSync );
39845 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
39846 if( rc!=SQLITE_OK ){
39847 if( needSyncPgno<=pPager->dbOrigSize ){
39848 assert( pPager->pTmpSpace!=0 );
39849 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
39850 }
39851 return rc;
39852 }
39853 pPager->needSync = 1;
39854 assert( pPager->noSync==0 && !MEMDB );
39855 pPgHdr->flags |= PGHDR_NEED_SYNC;
39856 sqlite3PcacheMakeDirty(pPgHdr);
39857 sqlite3PagerUnref(pPgHdr);
39858 }
39859
39860 /*
39861 ** For an in-memory database, make sure the original page continues
39862 ** to exist, in case the transaction needs to roll back. Use pPgOld
39863 ** as the original page since it has already been allocated.
39864 */
39865 if( MEMDB ){
39866 sqlite3PcacheMove(pPgOld, origPgno);
39867 sqlite3PagerUnref(pPgOld);
39868 }
39869
39870 return SQLITE_OK;
39871 }
39872 #endif
39873
39874 /*
@@ -39929,10 +40490,17 @@
39929 **
39930 ** The returned indicate the current (possibly updated) journal-mode.
39931 */
39932 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
39933 u8 eOld = pPager->journalMode; /* Prior journalmode */
 
 
 
 
 
 
 
39934
39935 /* The eMode parameter is always valid */
39936 assert( eMode==PAGER_JOURNALMODE_DELETE
39937 || eMode==PAGER_JOURNALMODE_TRUNCATE
39938 || eMode==PAGER_JOURNALMODE_PERSIST
@@ -39955,24 +40523,17 @@
39955 eMode = eOld;
39956 }
39957 }
39958
39959 if( eMode!=eOld ){
39960 /* When changing between rollback modes, close the journal file prior
39961 ** to the change. But when changing from a rollback mode to WAL, keep
39962 ** the journal open since there is a rollback-style transaction in play
39963 ** used to convert the version numbers in the btree header.
39964 */
39965 if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
39966 sqlite3OsClose(pPager->jfd);
39967 }
39968
39969 /* Change the journal mode. */
 
39970 pPager->journalMode = (u8)eMode;
39971
39972 /* When transistioning from TRUNCATE or PERSIST to any other journal
39973 ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then
39974 ** delete the journal file.
39975 */
39976 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
39977 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
39978 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
@@ -39989,28 +40550,34 @@
39989 **
39990 ** Before deleting the journal file, obtain a RESERVED lock on the
39991 ** database file. This ensures that the journal file is not deleted
39992 ** while it is in use by some other client.
39993 */
39994 int rc = SQLITE_OK;
39995 int state = pPager->state;
39996 if( state<PAGER_SHARED ){
39997 rc = sqlite3PagerSharedLock(pPager);
39998 }
39999 if( pPager->state==PAGER_SHARED ){
40000 assert( rc==SQLITE_OK );
40001 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
40002 }
40003 if( rc==SQLITE_OK ){
40004 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40005 }
40006 if( rc==SQLITE_OK && state==PAGER_SHARED ){
40007 sqlite3OsUnlock(pPager->fd, SHARED_LOCK);
40008 }else if( state==PAGER_UNLOCK ){
40009 pager_unlock(pPager);
40010 }
40011 assert( state==pPager->state );
40012 }
40013 }
40014
40015 /* Return the new journal mode */
40016 return (int)pPager->journalMode;
@@ -40027,11 +40594,12 @@
40027 ** Return TRUE if the pager is in a state where it is OK to change the
40028 ** journalmode. Journalmode changes can only happen when the database
40029 ** is unmodified.
40030 */
40031 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
40032 if( pPager->dbModified ) return 0;
 
40033 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
40034 return 1;
40035 }
40036
40037 /*
@@ -40092,39 +40660,46 @@
40092 **
40093 ** If the pager passed as the first argument is open on a real database
40094 ** file (not a temp file or an in-memory database), and the WAL file
40095 ** is not already open, make an attempt to open it now. If successful,
40096 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
40097 ** not support the xShmXXX() methods, return an error code. *pisOpen is
40098 ** not modified in either case.
40099 **
40100 ** If the pager is open on a temp-file (or in-memory database), or if
40101 ** the WAL file is already open, set *pisOpen to 1 and return SQLITE_OK
40102 ** without doing anything.
40103 */
40104 SQLITE_PRIVATE int sqlite3PagerOpenWal(
40105 Pager *pPager, /* Pager object */
40106 int *pisOpen /* OUT: Set to true if call is a no-op */
40107 ){
40108 int rc = SQLITE_OK; /* Return code */
40109
40110 assert( pPager->state>=PAGER_SHARED );
40111 assert( (pisOpen==0 && !pPager->tempFile && !pPager->pWal) || *pisOpen==0 );
 
 
 
40112
40113 if( !pPager->tempFile && !pPager->pWal ){
40114 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
 
 
 
40115
40116 /* Open the connection to the log file. If this operation fails,
40117 ** (e.g. due to malloc() failure), unlock the database file and
40118 ** return an error code.
40119 */
40120 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal);
40121 if( rc==SQLITE_OK ){
40122 pPager->journalMode = PAGER_JOURNALMODE_WAL;
 
40123 }
40124 }else{
40125 *pisOpen = 1;
40126 }
40127
40128 return rc;
40129 }
40130
@@ -40146,11 +40721,11 @@
40146 ** it may need to be checkpointed before the connection can switch to
40147 ** rollback mode. Open it now so this can happen.
40148 */
40149 if( !pPager->pWal ){
40150 int logexists = 0;
40151 rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED);
40152 if( rc==SQLITE_OK ){
40153 rc = sqlite3OsAccess(
40154 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
40155 );
40156 }
@@ -40162,21 +40737,21 @@
40162
40163 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
40164 ** the database file, the log and log-summary files will be deleted.
40165 */
40166 if( rc==SQLITE_OK && pPager->pWal ){
40167 rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE);
40168 if( rc==SQLITE_OK ){
40169 rc = sqlite3WalClose(pPager->pWal,
40170 (pPager->noSync ? 0 : pPager->sync_flags),
40171 pPager->pageSize, (u8*)pPager->pTmpSpace
40172 );
40173 pPager->pWal = 0;
40174 }else{
40175 /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
40176 ** that we did get back to SHARED. */
40177 sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
40178 }
40179 }
40180 return rc;
40181 }
40182
@@ -40492,18 +41067,22 @@
40492 /*
40493 ** The following object holds a copy of the wal-index header content.
40494 **
40495 ** The actual header in the wal-index consists of two copies of this
40496 ** object.
 
 
 
 
40497 */
40498 struct WalIndexHdr {
40499 u32 iVersion; /* Wal-index version */
40500 u32 unused; /* Unused (padding) field */
40501 u32 iChange; /* Counter incremented each transaction */
40502 u8 isInit; /* 1 when initialized */
40503 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
40504 u16 szPage; /* Database page size in bytes */
40505 u32 mxFrame; /* Index of last valid frame in the WAL */
40506 u32 nPage; /* Size of database in pages */
40507 u32 aFrameCksum[2]; /* Checksum of last frame in log */
40508 u32 aSalt[2]; /* Two salt values copied from WAL header */
40509 u32 aCksum[2]; /* Checksum over all prior fields */
@@ -40610,11 +41189,11 @@
40610 sqlite3_file *pDbFd; /* File handle for the database file */
40611 sqlite3_file *pWalFd; /* File handle for WAL file */
40612 u32 iCallback; /* Value to pass to log callback (or 0) */
40613 int nWiData; /* Size of array apWiData */
40614 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
40615 u16 szPage; /* Database page size */
40616 i16 readLock; /* Which read lock is being held. -1 for none */
40617 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
40618 u8 writeLock; /* True if in a write transaction */
40619 u8 ckptLock; /* True if holding a checkpoint lock */
40620 u8 readOnly; /* True if the WAL file is open read-only */
@@ -41281,11 +41860,11 @@
41281 || szPage<512
41282 ){
41283 goto finished;
41284 }
41285 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
41286 pWal->szPage = (u16)szPage;
41287 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
41288 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
41289
41290 /* Verify that the WAL header checksum is correct */
41291 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
@@ -41331,11 +41910,13 @@
41331
41332 /* If nTruncate is non-zero, this is a commit record. */
41333 if( nTruncate ){
41334 pWal->hdr.mxFrame = iFrame;
41335 pWal->hdr.nPage = nTruncate;
41336 pWal->hdr.szPage = (u16)szPage;
 
 
41337 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
41338 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
41339 }
41340 }
41341
@@ -41356,10 +41937,21 @@
41356 */
41357 pInfo = walCkptInfo(pWal);
41358 pInfo->nBackfill = 0;
41359 pInfo->aReadMark[0] = 0;
41360 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
 
 
 
 
 
 
 
 
 
 
 
41361 }
41362
41363 recovery_error:
41364 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
41365 walUnlockExclusive(pWal, iLock, nLock);
@@ -41716,18 +42308,22 @@
41716 int sync_flags, /* Flags for OsSync() (or 0) */
41717 int nBuf, /* Size of zBuf in bytes */
41718 u8 *zBuf /* Temporary buffer to use */
41719 ){
41720 int rc; /* Return code */
41721 int szPage = pWal->hdr.szPage; /* Database page-size */
41722 WalIterator *pIter = 0; /* Wal iterator context */
41723 u32 iDbpage = 0; /* Next database page to write */
41724 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
41725 u32 mxSafeFrame; /* Max frame that can be backfilled */
 
41726 int i; /* Loop counter */
41727 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
41728
 
 
 
41729 if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
41730
41731 /* Allocate the iterator */
41732 rc = walIteratorInit(pWal, &pIter);
41733 if( rc!=SQLITE_OK ){
@@ -41734,11 +42330,11 @@
41734 return rc;
41735 }
41736 assert( pIter );
41737
41738 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
41739 if( pWal->hdr.szPage!=nBuf ){
41740 rc = SQLITE_CORRUPT_BKPT;
41741 goto walcheckpoint_out;
41742 }
41743
41744 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -41745,10 +42341,11 @@
41745 ** safe to write into the database. Frames beyond mxSafeFrame might
41746 ** overwrite database pages that are in use by active readers and thus
41747 ** cannot be backfilled from the WAL.
41748 */
41749 mxSafeFrame = pWal->hdr.mxFrame;
 
41750 pInfo = walCkptInfo(pWal);
41751 for(i=1; i<WAL_NREADER; i++){
41752 u32 y = pInfo->aReadMark[i];
41753 if( mxSafeFrame>=y ){
41754 assert( y<=pWal->hdr.mxFrame );
@@ -41765,22 +42362,34 @@
41765 }
41766
41767 if( pInfo->nBackfill<mxSafeFrame
41768 && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
41769 ){
 
41770 u32 nBackfill = pInfo->nBackfill;
41771
41772 /* Sync the WAL to disk */
41773 if( sync_flags ){
41774 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
41775 }
 
 
 
 
 
 
 
 
 
 
 
41776
41777 /* Iterate through the contents of the WAL, copying data to the db file. */
41778 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
41779 i64 iOffset;
41780 assert( walFramePgno(pWal, iFrame)==iDbpage );
41781 if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue;
41782 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
41783 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
41784 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
41785 if( rc!=SQLITE_OK ) break;
41786 iOffset = (iDbpage-1)*(i64)szPage;
@@ -41883,11 +42492,11 @@
41883 WalIndexHdr volatile *aHdr; /* Header in shared memory */
41884
41885 /* The first page of the wal-index must be mapped at this point. */
41886 assert( pWal->nWiData>0 && pWal->apWiData[0] );
41887
41888 /* Read the header. This might happen currently with a write to the
41889 ** same area of shared memory on a different CPU in a SMP,
41890 ** meaning it is possible that an inconsistent snapshot is read
41891 ** from the file. If this happens, return non-zero.
41892 **
41893 ** There are two copies of the header at the beginning of the wal-index.
@@ -41912,11 +42521,13 @@
41912 }
41913
41914 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
41915 *pChanged = 1;
41916 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
41917 pWal->szPage = pWal->hdr.szPage;
 
 
41918 }
41919
41920 /* The header was successfully read. Return zero. */
41921 return 0;
41922 }
@@ -42231,10 +42842,11 @@
42231 /*
42232 ** Finish with a read transaction. All this does is release the
42233 ** read-lock.
42234 */
42235 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
 
42236 if( pWal->readLock>=0 ){
42237 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
42238 pWal->readLock = -1;
42239 }
42240 }
@@ -42341,11 +42953,17 @@
42341
42342 /* If iRead is non-zero, then it is the log frame number that contains the
42343 ** required page. Read and return data from the log file.
42344 */
42345 if( iRead ){
42346 i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE;
 
 
 
 
 
 
42347 *pInWal = 1;
42348 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
42349 return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
42350 }
42351
@@ -42353,15 +42971,17 @@
42353 return SQLITE_OK;
42354 }
42355
42356
42357 /*
42358 ** Set *pPgno to the size of the database file (or zero, if unknown).
42359 */
42360 SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
42361 assert( pWal->readLock>=0 || pWal->lockError );
42362 *pPgno = pWal->hdr.nPage;
 
 
42363 }
42364
42365
42366 /*
42367 ** This function starts a write transaction on the WAL.
@@ -42433,11 +43053,11 @@
42433 ** Otherwise, if the callback function does not return an error, this
42434 ** function returns SQLITE_OK.
42435 */
42436 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
42437 int rc = SQLITE_OK;
42438 if( pWal->writeLock ){
42439 Pgno iMax = pWal->hdr.mxFrame;
42440 Pgno iFrame;
42441
42442 /* Restore the clients cache of the wal-index header to the state it
42443 ** was in before the client began writing to the database.
@@ -42622,11 +43242,11 @@
42622 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
42623 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
42624 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
42625 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
42626
42627 pWal->szPage = (u16)szPage;
42628 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
42629 pWal->hdr.aFrameCksum[0] = aCksum[0];
42630 pWal->hdr.aFrameCksum[1] = aCksum[1];
42631
42632 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
@@ -42717,11 +43337,13 @@
42717 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
42718 }
42719
42720 if( rc==SQLITE_OK ){
42721 /* Update the private copy of the header. */
42722 pWal->hdr.szPage = (u16)szPage;
 
 
42723 pWal->hdr.mxFrame = iFrame;
42724 if( isCommit ){
42725 pWal->hdr.iChange++;
42726 pWal->hdr.nPage = nTruncate;
42727 }
@@ -42929,11 +43551,11 @@
42929 **
42930 ** FORMAT DETAILS
42931 **
42932 ** The file is divided into pages. The first page is called page 1,
42933 ** the second is page 2, and so forth. A page number of zero indicates
42934 ** "no such page". The page size can be any power of 2 between 512 and 32768.
42935 ** Each page can be either a btree page, a freelist page, an overflow
42936 ** page, or a pointer-map page.
42937 **
42938 ** The first page is always a btree page. The first 100 bytes of the first
42939 ** page contain a special header (the "file header") that describes the file.
@@ -43295,18 +43917,18 @@
43295 u8 initiallyEmpty; /* Database is empty at start of transaction */
43296 #ifndef SQLITE_OMIT_AUTOVACUUM
43297 u8 autoVacuum; /* True if auto-vacuum is enabled */
43298 u8 incrVacuum; /* True if incr-vacuum is enabled */
43299 #endif
43300 u16 pageSize; /* Total number of bytes on a page */
43301 u16 usableSize; /* Number of usable bytes on each page */
43302 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
43303 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
43304 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
43305 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
43306 u8 inTransaction; /* Transaction state */
43307 u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
 
 
43308 int nTransaction; /* Number of open transactions (read + write) */
43309 u32 nPage; /* Number of pages in the database */
43310 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
43311 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
43312 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -43901,11 +44523,20 @@
43901 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
43902 #else
43903 # define TRACE(X)
43904 #endif
43905
43906
 
 
 
 
 
 
 
 
 
43907
43908 #ifndef SQLITE_OMIT_SHARED_CACHE
43909 /*
43910 ** A list of BtShared objects that are eligible for participation
43911 ** in shared cache. This variable has file scope during normal builds,
@@ -45023,21 +45654,21 @@
45023 assert( nByte < usableSize-8 );
45024
45025 nFrag = data[hdr+7];
45026 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
45027 gap = pPage->cellOffset + 2*pPage->nCell;
45028 top = get2byte(&data[hdr+5]);
45029 if( gap>top ) return SQLITE_CORRUPT_BKPT;
45030 testcase( gap+2==top );
45031 testcase( gap+1==top );
45032 testcase( gap==top );
45033
45034 if( nFrag>=60 ){
45035 /* Always defragment highly fragmented pages */
45036 rc = defragmentPage(pPage);
45037 if( rc ) return rc;
45038 top = get2byte(&data[hdr+5]);
45039 }else if( gap+2<=top ){
45040 /* Search the freelist looking for a free slot big enough to satisfy
45041 ** the request. The allocation is made from the first free slot in
45042 ** the list that is large enough to accomadate it.
45043 */
@@ -45075,11 +45706,11 @@
45075 */
45076 testcase( gap+2+nByte==top );
45077 if( gap+2+nByte>top ){
45078 rc = defragmentPage(pPage);
45079 if( rc ) return rc;
45080 top = get2byte(&data[hdr+5]);
45081 assert( gap+nByte<=top );
45082 }
45083
45084
45085 /* Allocate memory from the gap in between the cell pointer array
@@ -45241,28 +45872,28 @@
45241 if( !pPage->isInit ){
45242 u16 pc; /* Address of a freeblock within pPage->aData[] */
45243 u8 hdr; /* Offset to beginning of page header */
45244 u8 *data; /* Equal to pPage->aData */
45245 BtShared *pBt; /* The main btree structure */
45246 u16 usableSize; /* Amount of usable space on each page */
45247 u16 cellOffset; /* Offset from start of page to first cell pointer */
45248 u16 nFree; /* Number of unused bytes on the page */
45249 u16 top; /* First byte of the cell content area */
45250 int iCellFirst; /* First allowable cell or freeblock offset */
45251 int iCellLast; /* Last possible cell or freeblock offset */
45252
45253 pBt = pPage->pBt;
45254
45255 hdr = pPage->hdrOffset;
45256 data = pPage->aData;
45257 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
45258 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
45259 pPage->maskPage = pBt->pageSize - 1;
45260 pPage->nOverflow = 0;
45261 usableSize = pBt->usableSize;
45262 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
45263 top = get2byte(&data[hdr+5]);
45264 pPage->nCell = get2byte(&data[hdr+3]);
45265 if( pPage->nCell>MX_CELL(pBt) ){
45266 /* To many cells for a single page. The page must be corrupt */
45267 return SQLITE_CORRUPT_BKPT;
45268 }
@@ -45362,12 +45993,12 @@
45362 pPage->nFree = pBt->usableSize - first;
45363 decodeFlags(pPage, flags);
45364 pPage->hdrOffset = hdr;
45365 pPage->cellOffset = first;
45366 pPage->nOverflow = 0;
45367 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
45368 pPage->maskPage = pBt->pageSize - 1;
45369 pPage->nCell = 0;
45370 pPage->isInit = 1;
45371 }
45372
45373
@@ -45671,11 +46302,11 @@
45671 pBt->pPage1 = 0;
45672 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
45673 #ifdef SQLITE_SECURE_DELETE
45674 pBt->secureDelete = 1;
45675 #endif
45676 pBt->pageSize = get2byte(&zDbHeader[16]);
45677 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
45678 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
45679 pBt->pageSize = 0;
45680 #ifndef SQLITE_OMIT_AUTOVACUUM
45681 /* If the magic name ":memory:" will create an in-memory database, then
@@ -45985,11 +46616,11 @@
45985 assert( nReserve>=0 && nReserve<=255 );
45986 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
45987 ((pageSize-1)&pageSize)==0 ){
45988 assert( (pageSize & 7)==0 );
45989 assert( !pBt->pPage1 && !pBt->pCursor );
45990 pBt->pageSize = (u16)pageSize;
45991 freeTempSpace(pBt);
45992 }
45993 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
45994 pBt->usableSize = pBt->pageSize - (u16)nReserve;
45995 if( iFix ) pBt->pageSizeFixed = 1;
@@ -46120,19 +46751,17 @@
46120
46121 /* Do some checking to help insure the file we opened really is
46122 ** a valid database file.
46123 */
46124 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
46125 if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){;
46126 goto page1_init_failed;
46127 }
46128 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
46129 nPage = nPageFile;
46130 }
46131 if( nPage>0 ){
46132 int pageSize;
46133 int usableSize;
46134 u8 *page1 = pPage1->aData;
46135 rc = SQLITE_NOTADB;
46136 if( memcmp(page1, zMagicHeader, 16)!=0 ){
46137 goto page1_init_failed;
46138 }
@@ -46179,13 +46808,14 @@
46179 ** version 3.6.0, we require them to be fixed.
46180 */
46181 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
46182 goto page1_init_failed;
46183 }
46184 pageSize = get2byte(&page1[16]);
46185 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
46186 (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
 
46187 ){
46188 goto page1_init_failed;
46189 }
46190 assert( (pageSize & 7)==0 );
46191 usableSize = pageSize - page1[20];
@@ -46195,12 +46825,12 @@
46195 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
46196 ** zero and return SQLITE_OK. The caller will call this function
46197 ** again with the correct page-size.
46198 */
46199 releasePage(pPage1);
46200 pBt->usableSize = (u16)usableSize;
46201 pBt->pageSize = (u16)pageSize;
46202 freeTempSpace(pBt);
46203 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
46204 pageSize-usableSize);
46205 return rc;
46206 }
@@ -46209,12 +46839,12 @@
46209 goto page1_init_failed;
46210 }
46211 if( usableSize<480 ){
46212 goto page1_init_failed;
46213 }
46214 pBt->pageSize = (u16)pageSize;
46215 pBt->usableSize = (u16)usableSize;
46216 #ifndef SQLITE_OMIT_AUTOVACUUM
46217 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
46218 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
46219 #endif
46220 }
@@ -46286,11 +46916,12 @@
46286 data = pP1->aData;
46287 rc = sqlite3PagerWrite(pP1->pDbPage);
46288 if( rc ) return rc;
46289 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
46290 assert( sizeof(zMagicHeader)==16 );
46291 put2byte(&data[16], pBt->pageSize);
 
46292 data[18] = 1;
46293 data[19] = 1;
46294 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
46295 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
46296 data[21] = 64;
@@ -48297,13 +48928,13 @@
48297 c = +1;
48298 }
48299 pCur->validNKey = 1;
48300 pCur->info.nKey = nCellKey;
48301 }else{
48302 /* The maximum supported page-size is 32768 bytes. This means that
48303 ** the maximum number of record bytes stored on an index B-Tree
48304 ** page is at most 8198 bytes, which may be stored as a 2-byte
48305 ** varint. This information is used to attempt to avoid parsing
48306 ** the entire cell by checking for the cases where the record is
48307 ** stored entirely within the b-tree page by inspecting the first
48308 ** 2 bytes of the cell.
48309 */
@@ -49193,11 +49824,11 @@
49193 **
49194 ** "sz" must be the number of bytes in the cell.
49195 */
49196 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
49197 int i; /* Loop counter */
49198 int pc; /* Offset to cell content of cell being deleted */
49199 u8 *data; /* pPage->aData */
49200 u8 *ptr; /* Used to move bytes around within data[] */
49201 int rc; /* The return code */
49202 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
49203
@@ -49211,11 +49842,11 @@
49211 ptr = &data[pPage->cellOffset + 2*idx];
49212 pc = get2byte(ptr);
49213 hdr = pPage->hdrOffset;
49214 testcase( pc==get2byte(&data[hdr+5]) );
49215 testcase( pc+sz==pPage->pBt->usableSize );
49216 if( pc < get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
49217 *pRC = SQLITE_CORRUPT_BKPT;
49218 return;
49219 }
49220 rc = freeSpace(pPage, pc, sz);
49221 if( rc ){
@@ -49268,11 +49899,11 @@
49268 int nSkip = (iChild ? 4 : 0);
49269
49270 if( *pRC ) return;
49271
49272 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
49273 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
49274 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
49275 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49276 /* The cell should normally be sized correctly. However, when moving a
49277 ** malformed cell from a leaf page to an interior page, if the cell size
49278 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
@@ -49348,16 +49979,16 @@
49348 const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
49349 const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
49350
49351 assert( pPage->nOverflow==0 );
49352 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49353 assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
49354 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49355
49356 /* Check that the page has just been zeroed by zeroPage() */
49357 assert( pPage->nCell==0 );
49358 assert( get2byte(&data[hdr+5])==nUsable );
49359
49360 pCellptr = &data[pPage->cellOffset + nCell*2];
49361 cellbody = nUsable;
49362 for(i=nCell-1; i>=0; i--){
49363 pCellptr -= 2;
@@ -49419,11 +50050,12 @@
49419
49420 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49421 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
49422 assert( pPage->nOverflow==1 );
49423
49424 if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
 
49425
49426 /* Allocate a new page. This page will become the right-sibling of
49427 ** pPage. Make the parent page writable, so that the new divider cell
49428 ** may be inserted. If both these operations are successful, proceed.
49429 */
@@ -49748,11 +50380,11 @@
49748 ** In this case, temporarily copy the cell into the aOvflSpace[]
49749 ** buffer. It will be copied out again as soon as the aSpace[] buffer
49750 ** is allocated. */
49751 if( pBt->secureDelete ){
49752 int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
49753 if( (iOff+szNew[i])>pBt->usableSize ){
49754 rc = SQLITE_CORRUPT_BKPT;
49755 memset(apOld, 0, (i+1)*sizeof(MemPage*));
49756 goto balance_cleanup;
49757 }else{
49758 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
@@ -51317,11 +51949,11 @@
51317 #ifndef SQLITE_OMIT_AUTOVACUUM
51318 if( pCheck->pBt->autoVacuum ){
51319 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
51320 }
51321 #endif
51322 if( n>pCheck->pBt->usableSize/4-2 ){
51323 checkAppendMsg(pCheck, zContext,
51324 "freelist leaf count too big on page %d", iPage);
51325 N--;
51326 }else{
51327 for(i=0; i<n; i++){
@@ -51528,24 +52160,24 @@
51528 hdr = pPage->hdrOffset;
51529 hit = sqlite3PageMalloc( pBt->pageSize );
51530 if( hit==0 ){
51531 pCheck->mallocFailed = 1;
51532 }else{
51533 u16 contentOffset = get2byte(&data[hdr+5]);
51534 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
51535 memset(hit+contentOffset, 0, usableSize-contentOffset);
51536 memset(hit, 1, contentOffset);
51537 nCell = get2byte(&data[hdr+3]);
51538 cellStart = hdr + 12 - 4*pPage->leaf;
51539 for(i=0; i<nCell; i++){
51540 int pc = get2byte(&data[cellStart+i*2]);
51541 u16 size = 1024;
51542 int j;
51543 if( pc<=usableSize-4 ){
51544 size = cellSizePtr(pPage, &data[pc]);
51545 }
51546 if( (pc+size-1)>=usableSize ){
51547 checkAppendMsg(pCheck, 0,
51548 "Corruption detected in cell %d on page %d",i,iPage);
51549 }else{
51550 for(j=pc+size-1; j>=pc; j--) hit[j]++;
51551 }
@@ -55769,12 +56401,21 @@
55769 mrc = p->rc & 0xff;
55770 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
55771 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
55772 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
55773 if( isSpecialError ){
55774 /* If the query was read-only, we need do no rollback at all. Otherwise,
55775 ** proceed with the special handling.
 
 
 
 
 
 
 
 
 
55776 */
55777 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
55778 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
55779 eStatementOp = SAVEPOINT_ROLLBACK;
55780 }else{
@@ -63200,11 +63841,11 @@
63200 ** If P5 is non-zero then the key value is increased by an epsilon
63201 ** prior to the comparison. This make the opcode work like IdxGT except
63202 ** that if the key from register P3 is a prefix of the key in the cursor,
63203 ** the result is false whereas it would be true with IdxGT.
63204 */
63205 /* Opcode: IdxLT P1 P2 P3 * P5
63206 **
63207 ** The P4 register values beginning with P3 form an unpacked index
63208 ** key that omits the ROWID. Compare this key value against the index
63209 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
63210 **
@@ -67547,17 +68188,18 @@
67547 assert( z[0]=='?' );
67548 pExpr->iColumn = (ynVar)(++pParse->nVar);
67549 }else if( z[0]=='?' ){
67550 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
67551 ** use it as the variable number */
67552 int i = atoi((char*)&z[1]);
 
67553 pExpr->iColumn = (ynVar)i;
67554 testcase( i==0 );
67555 testcase( i==1 );
67556 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
67557 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
67558 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
67559 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
67560 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
67561 }
67562 if( i>pParse->nVar ){
67563 pParse->nVar = i;
@@ -72092,10 +72734,11 @@
72092 }
72093 }
72094 sqlite3DbFree(db, pIdx->aSample);
72095 }
72096 #else
 
72097 UNUSED_PARAMETER(pIdx);
72098 #endif
72099 }
72100
72101 /*
@@ -87738,11 +88381,11 @@
87738 ** be sent.
87739 **
87740 ** regReturn is the number of the register holding the subroutine
87741 ** return address.
87742 **
87743 ** If regPrev>0 then it is a the first register in a vector that
87744 ** records the previous output. mem[regPrev] is a flag that is false
87745 ** if there has been no previous output. If regPrev>0 then code is
87746 ** generated to suppress duplicates. pKeyInfo is used for comparing
87747 ** keys.
87748 **
@@ -88435,16 +89078,17 @@
88435 ** (1) The subquery and the outer query do not both use aggregates.
88436 **
88437 ** (2) The subquery is not an aggregate or the outer query is not a join.
88438 **
88439 ** (3) The subquery is not the right operand of a left outer join
88440 ** (Originally ticket #306. Strenghtened by ticket #3300)
88441 **
88442 ** (4) The subquery is not DISTINCT or the outer query is not a join.
88443 **
88444 ** (5) The subquery is not DISTINCT or the outer query does not use
88445 ** aggregates.
 
88446 **
88447 ** (6) The subquery does not use aggregates or the outer query is not
88448 ** DISTINCT.
88449 **
88450 ** (7) The subquery has a FROM clause.
@@ -88460,13 +89104,13 @@
88460 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
88461 **
88462 ** (**) Not implemented. Subsumed into restriction (3). Was previously
88463 ** a separate restriction deriving from ticket #350.
88464 **
88465 ** (13) The subquery and outer query do not both use LIMIT
88466 **
88467 ** (14) The subquery does not use OFFSET
88468 **
88469 ** (15) The outer query is not part of a compound select or the
88470 ** subquery does not have a LIMIT clause.
88471 ** (See ticket #2339 and ticket [02a8e81d44]).
88472 **
@@ -88553,13 +89197,13 @@
88553 if( pSub->pOffset ) return 0; /* Restriction (14) */
88554 if( p->pRightmost && pSub->pLimit ){
88555 return 0; /* Restriction (15) */
88556 }
88557 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
88558 if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit)
88559 && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
88560 return 0;
88561 }
88562 if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
88563 return 0; /* Restriction (6) */
88564 }
88565 if( p->pOrderBy && pSub->pOrderBy ){
@@ -93225,11 +93869,11 @@
93225 pParse->pNewTable->aCol = 0;
93226 }
93227 db->pVTab = 0;
93228 }else{
93229 sqlite3Error(db, SQLITE_ERROR, zErr);
93230 sqlite3_free(zErr);
93231 rc = SQLITE_ERROR;
93232 }
93233 pParse->declareVtab = 0;
93234
93235 if( pParse->pVdbe ){
@@ -96859,39 +97503,39 @@
96859 **
96860 ** This case is also used when there are no WHERE clause
96861 ** constraints but an index is selected anyway, in order
96862 ** to force the output order to conform to an ORDER BY.
96863 */
96864 int aStartOp[] = {
96865 0,
96866 0,
96867 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
96868 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
96869 OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */
96870 OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */
96871 OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */
96872 OP_SeekLe /* 7: (start_constraints && startEq && bRev) */
96873 };
96874 int aEndOp[] = {
96875 OP_Noop, /* 0: (!end_constraints) */
96876 OP_IdxGE, /* 1: (end_constraints && !bRev) */
96877 OP_IdxLT /* 2: (end_constraints && bRev) */
96878 };
96879 int nEq = pLevel->plan.nEq;
96880 int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
96881 int regBase; /* Base register holding constraint values */
96882 int r1; /* Temp register */
96883 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
96884 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
96885 int startEq; /* True if range start uses ==, >= or <= */
96886 int endEq; /* True if range end uses ==, >= or <= */
96887 int start_constraints; /* Start of range is constrained */
96888 int nConstraint; /* Number of constraint terms */
96889 Index *pIdx; /* The index we will be using */
96890 int iIdxCur; /* The VDBE cursor for the index */
96891 int nExtraReg = 0; /* Number of extra registers needed */
96892 int op; /* Instruction opcode */
96893 char *zStartAff; /* Affinity for start of range constraint */
96894 char *zEndAff; /* Affinity for end of range constraint */
96895
96896 pIdx = pLevel->plan.u.pIdx;
96897 iIdxCur = pLevel->iIdxCur;
@@ -108720,11 +109364,11 @@
108720 ** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following
108721 ** two forward declarations are for functions declared in these files
108722 ** used to retrieve the respective implementations.
108723 **
108724 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
108725 ** to by the argument to point a the "simple" tokenizer implementation.
108726 ** Function ...PorterTokenizerModule() sets *pModule to point to the
108727 ** porter tokenizer/stemmer implementation.
108728 */
108729 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
108730 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
@@ -108922,11 +109566,11 @@
108922 ** is defined to accept an argument of type char, and always returns 0 for
108923 ** any values that fall outside of the range of the unsigned char type (i.e.
108924 ** negative values).
108925 */
108926 static int fts3isspace(char c){
108927 return (c&0x80)==0 ? isspace(c) : 0;
108928 }
108929
108930 /*
108931 ** Extract the next token from buffer z (length n) using the tokenizer
108932 ** and other information (column names etc.) in pParse. Create an Fts3Expr
@@ -111309,10 +111953,13 @@
111309
111310
111311 static int simpleDelim(simple_tokenizer *t, unsigned char c){
111312 return c<0x80 && t->delim[c];
111313 }
 
 
 
111314
111315 /*
111316 ** Create a new tokenizer instance.
111317 */
111318 static int simpleCreate(
@@ -111343,11 +111990,11 @@
111343 }
111344 } else {
111345 /* Mark non-alphanumeric ASCII characters as delimiters */
111346 int i;
111347 for(i=1; i<0x80; i++){
111348 t->delim[i] = !isalnum(i) ? -1 : 0;
111349 }
111350 }
111351
111352 *ppTokenizer = &t->base;
111353 return SQLITE_OK;
@@ -111449,11 +112096,11 @@
111449 for(i=0; i<n; i++){
111450 /* TODO(shess) This needs expansion to handle UTF-8
111451 ** case-insensitivity.
111452 */
111453 unsigned char ch = p[iStartOffset+i];
111454 c->pToken[i] = (char)(ch<0x80 ? tolower(ch) : ch);
111455 }
111456 *ppToken = c->pToken;
111457 *pnBytes = n;
111458 *piStartOffset = iStartOffset;
111459 *piEndOffset = c->iOffset;
@@ -116355,15 +117002,14 @@
116355 ** least desirable):
116356 **
116357 ** idxNum idxStr Strategy
116358 ** ------------------------------------------------
116359 ** 1 Unused Direct lookup by rowid.
116360 ** 2 See below R-tree query.
116361 ** 3 Unused Full table scan.
116362 ** ------------------------------------------------
116363 **
116364 ** If strategy 1 or 3 is used, then idxStr is not meaningful. If strategy
116365 ** 2 is used, idxStr is formatted to contain 2 bytes for each
116366 ** constraint used. The first two bytes of idxStr correspond to
116367 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
116368 ** (argvIndex==1) etc.
116369 **
116370
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -213,23 +213,22 @@
213 */
214 #ifndef SQLITE_MAX_VARIABLE_NUMBER
215 # define SQLITE_MAX_VARIABLE_NUMBER 999
216 #endif
217
218 /* Maximum page size. The upper bound on this value is 65536. This a limit
219 ** imposed by the use of 16-bit offsets within each page.
 
220 **
221 ** If this limit is changed, then the compiled library is technically
222 ** incompatible with an SQLite library compiled with a different limit. If
223 ** a process operating on a database with a page-size of 65536 bytes
224 ** crashes, then an instance of SQLite compiled with the default page-size
225 ** limit will not be able to rollback the aborted transaction. This could
226 ** lead to database corruption.
227 */
228 #ifndef SQLITE_MAX_PAGE_SIZE
229 # define SQLITE_MAX_PAGE_SIZE 65536
230 #endif
231
232
233 /*
234 ** The default size of a database page.
@@ -631,11 +630,11 @@
630 ** be held constant and Z will be incremented or else Y will be incremented
631 ** and Z will be reset to zero.
632 **
633 ** Since version 3.6.18, SQLite source code has been stored in the
634 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
635 ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
636 ** a string which identifies a particular check-in of SQLite
637 ** within its configuration management system. ^The SQLITE_SOURCE_ID
638 ** string contains the date and time of the check-in (UTC) and an SHA1
639 ** hash of the entire source tree.
640 **
@@ -643,11 +642,11 @@
642 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
643 ** [sqlite_version()] and [sqlite_source_id()].
644 */
645 #define SQLITE_VERSION "3.7.1"
646 #define SQLITE_VERSION_NUMBER 3007001
647 #define SQLITE_SOURCE_ID "2010-08-17 19:49:14 acb171d4cfef2fec8833f761019f5c81f0d138a0"
648
649 /*
650 ** CAPI3REF: Run-Time Library Version Numbers
651 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
652 **
@@ -688,19 +687,19 @@
687 ** ^The sqlite3_compileoption_used() function returns 0 or 1
688 ** indicating whether the specified option was defined at
689 ** compile time. ^The SQLITE_ prefix may be omitted from the
690 ** option name passed to sqlite3_compileoption_used().
691 **
692 ** ^The sqlite3_compileoption_get() function allows iterating
693 ** over the list of options that were defined at compile time by
694 ** returning the N-th compile time option string. ^If N is out of range,
695 ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
696 ** prefix is omitted from any strings returned by
697 ** sqlite3_compileoption_get().
698 **
699 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
700 ** and sqlite3_compileoption_get() may be omitted by specifying the
701 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
702 **
703 ** See also: SQL functions [sqlite_compileoption_used()] and
704 ** [sqlite_compileoption_get()] and the [compile_options pragma].
705 */
@@ -802,11 +801,11 @@
801 /*
802 ** CAPI3REF: Closing A Database Connection
803 **
804 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
805 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
806 ** successfully destroyed and all associated resources are deallocated.
807 **
808 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
809 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
810 ** the [sqlite3] object prior to attempting to close the object. ^If
811 ** sqlite3_close() is called on a [database connection] that still has
@@ -1229,16 +1228,25 @@
1228 ** layer a hint of how large the database file will grow to be during the
1229 ** current transaction. This hint is not guaranteed to be accurate but it
1230 ** is often close. The underlying VFS might choose to preallocate database
1231 ** file space based on this hint in order to help writes to the database
1232 ** file run faster.
1233 **
1234 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1235 ** extends and truncates the database file in chunks of a size specified
1236 ** by the user. The fourth argument to [sqlite3_file_control()] should
1237 ** point to an integer (type int) containing the new chunk-size to use
1238 ** for the nominated database. Allocating database file space in large
1239 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1240 ** improve performance on some systems.
1241 */
1242 #define SQLITE_FCNTL_LOCKSTATE 1
1243 #define SQLITE_GET_LOCKPROXYFILE 2
1244 #define SQLITE_SET_LOCKPROXYFILE 3
1245 #define SQLITE_LAST_ERRNO 4
1246 #define SQLITE_FCNTL_SIZE_HINT 5
1247 #define SQLITE_FCNTL_CHUNK_SIZE 6
1248
1249 /*
1250 ** CAPI3REF: Mutex Handle
1251 **
1252 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3197,11 +3205,11 @@
3205 ** <li> @VVV
3206 ** <li> $VVV
3207 ** </ul>
3208 **
3209 ** In the templates above, NNN represents an integer literal,
3210 ** and VVV represents an alphanumeric identifier.)^ ^The values of these
3211 ** parameters (also called "host parameter names" or "SQL parameters")
3212 ** can be set using the sqlite3_bind_*() routines defined here.
3213 **
3214 ** ^The first argument to the sqlite3_bind_*() routines is always
3215 ** a pointer to the [sqlite3_stmt] object returned from
@@ -3976,11 +3984,11 @@
3984 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
3985
3986 /*
3987 ** CAPI3REF: Obtain Aggregate Function Context
3988 **
3989 ** Implementations of aggregate SQL functions use this
3990 ** routine to allocate memory for storing their state.
3991 **
3992 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
3993 ** for a particular aggregate function, SQLite
3994 ** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -4248,11 +4256,11 @@
4256 ** the routine expects pointers to 16-bit word aligned strings
4257 ** of UTF-16 in the native byte order.
4258 **
4259 ** A pointer to the user supplied routine must be passed as the fifth
4260 ** argument. ^If it is NULL, this is the same as deleting the collation
4261 ** sequence (so that SQLite cannot call it any more).
4262 ** ^Each time the application supplied function is invoked, it is passed
4263 ** as its first parameter a copy of the void* passed as the fourth argument
4264 ** to sqlite3_create_collation() or sqlite3_create_collation16().
4265 **
4266 ** ^The remaining arguments to the application-supplied routine are two strings,
@@ -5466,11 +5474,11 @@
5474 ** of passing a NULL pointer instead of a valid mutex handle are undefined
5475 ** (i.e. it is acceptable to provide an implementation that segfaults if
5476 ** it is passed a NULL pointer).
5477 **
5478 ** The xMutexInit() method must be threadsafe. ^It must be harmless to
5479 ** invoke xMutexInit() multiple times within the same process and without
5480 ** intervening calls to xMutexEnd(). Second and subsequent calls to
5481 ** xMutexInit() must be no-ops.
5482 **
5483 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5484 ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5636,11 +5644,11 @@
5644
5645 /*
5646 ** CAPI3REF: SQLite Runtime Status
5647 **
5648 ** ^This interface is used to retrieve runtime status information
5649 ** about the performance of SQLite, and optionally to reset various
5650 ** highwater marks. ^The first argument is an integer code for
5651 ** the specific parameter to measure. ^(Recognized integer codes
5652 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
5653 ** ^The current value of the parameter is returned into *pCurrent.
5654 ** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5762,11 +5770,11 @@
5770 ** ^This interface is used to retrieve runtime status information
5771 ** about a single [database connection]. ^The first argument is the
5772 ** database connection object to be interrogated. ^The second argument
5773 ** is an integer constant, taken from the set of
5774 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5775 ** determines the parameter to interrogate. The set of
5776 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
5777 ** to grow in future releases of SQLite.
5778 **
5779 ** ^The current value of the requested parameter is written into *pCur
5780 ** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -6184,11 +6192,11 @@
6192 **
6193 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
6194 **
6195 ** ^Each call to sqlite3_backup_step() sets two values inside
6196 ** the [sqlite3_backup] object: the number of pages still to be backed
6197 ** up and the total number of pages in the source database file.
6198 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6199 ** retrieve these two values, respectively.
6200 **
6201 ** ^The values returned by these functions are only updated by
6202 ** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -6280,11 +6288,11 @@
6288 ** ^(There may be at most one unlock-notify callback registered by a
6289 ** blocked connection. If sqlite3_unlock_notify() is called when the
6290 ** blocked connection already has a registered unlock-notify callback,
6291 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6292 ** called with a NULL pointer as its second argument, then any existing
6293 ** unlock-notify callback is canceled. ^The blocked connections
6294 ** unlock-notify callback may also be canceled by closing the blocked
6295 ** connection using [sqlite3_close()].
6296 **
6297 ** The unlock-notify callback is not reentrant. If an application invokes
6298 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -6362,11 +6370,11 @@
6370 /*
6371 ** CAPI3REF: String Comparison
6372 **
6373 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
6374 ** compare the contents of two buffers containing UTF-8 strings in a
6375 ** case-independent fashion, using the same definition of case independence
6376 ** that SQLite uses internally when comparing identifiers.
6377 */
6378 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6379
6380 /*
@@ -7868,11 +7876,11 @@
7876 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
7877 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
7878
7879 /* Functions used to configure a Pager object. */
7880 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
7881 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
7882 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7883 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7884 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7885 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7886 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
@@ -7895,11 +7903,11 @@
7903 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
7904 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
7905 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
7906
7907 /* Functions used to manage pager transactions and savepoints. */
7908 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
7909 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
7910 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
7911 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
7912 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
7913 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
@@ -9183,13 +9191,13 @@
9191 ** argument to sqlite3VdbeKeyCompare and is used to control the
9192 ** comparison of the two index keys.
9193 */
9194 struct KeyInfo {
9195 sqlite3 *db; /* The database connection */
9196 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
9197 u16 nField; /* Number of entries in aColl[] */
9198 u8 *aSortOrder; /* Sort order for each column. May be NULL */
9199 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
9200 };
9201
9202 /*
9203 ** An instance of the following structure holds information about a
@@ -16624,10 +16632,11 @@
16632 #else
16633 /* Use the built-in recursive mutexes if they are available.
16634 */
16635 pthread_mutex_lock(&p->mutex);
16636 #if SQLITE_MUTEX_NREF
16637 assert( p->nRef>0 || p->owner==0 );
16638 p->owner = pthread_self();
16639 p->nRef++;
16640 #endif
16641 #endif
16642
@@ -16696,10 +16705,11 @@
16705 */
16706 static void pthreadMutexLeave(sqlite3_mutex *p){
16707 assert( pthreadMutexHeld(p) );
16708 #if SQLITE_MUTEX_NREF
16709 p->nRef--;
16710 if( p->nRef==0 ) p->owner = 0;
16711 #endif
16712 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
16713
16714 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
16715 if( p->nRef==0 ){
@@ -16960,11 +16970,11 @@
16970 ** allocated mutex. SQLite is careful to deallocate every
16971 ** mutex that it allocates.
16972 */
16973 static void winMutexFree(sqlite3_mutex *p){
16974 assert( p );
16975 assert( p->nRef==0 && p->owner==0 );
16976 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
16977 DeleteCriticalSection(&p->mutex);
16978 sqlite3_free(p);
16979 }
16980
@@ -16984,10 +16994,11 @@
16994 DWORD tid = GetCurrentThreadId();
16995 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
16996 #endif
16997 EnterCriticalSection(&p->mutex);
16998 #ifdef SQLITE_DEBUG
16999 assert( p->nRef>0 || p->owner==0 );
17000 p->owner = tid;
17001 p->nRef++;
17002 if( p->trace ){
17003 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
17004 }
@@ -17037,10 +17048,11 @@
17048 #ifndef NDEBUG
17049 DWORD tid = GetCurrentThreadId();
17050 assert( p->nRef>0 );
17051 assert( p->owner==tid );
17052 p->nRef--;
17053 if( p->nRef==0 ) p->owner = 0;
17054 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
17055 #endif
17056 LeaveCriticalSection(&p->mutex);
17057 #ifdef SQLITE_DEBUG
17058 if( p->trace ){
@@ -22609,10 +22621,11 @@
22621 void *lockingContext; /* Locking style specific state */
22622 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22623 int fileFlags; /* Miscellanous flags */
22624 const char *zPath; /* Name of the file */
22625 unixShm *pShm; /* Shared memory segment information */
22626 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
22627 #if SQLITE_ENABLE_LOCKING_STYLE
22628 int openFlags; /* The flags specified at open() */
22629 #endif
22630 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
22631 unsigned fsFlags; /* cached details from statfs() */
@@ -25367,19 +25380,21 @@
25380 offset += wrote;
25381 pBuf = &((char*)pBuf)[wrote];
25382 }
25383 SimulateIOError(( wrote=(-1), amt=1 ));
25384 SimulateDiskfullError(( wrote=0, amt=1 ));
25385
25386 if( amt>0 ){
25387 if( wrote<0 ){
25388 /* lastErrno set by seekAndWrite */
25389 return SQLITE_IOERR_WRITE;
25390 }else{
25391 pFile->lastErrno = 0; /* not a system error */
25392 return SQLITE_FULL;
25393 }
25394 }
25395
25396 return SQLITE_OK;
25397 }
25398
25399 #ifdef SQLITE_TEST
25400 /*
@@ -25577,16 +25592,27 @@
25592
25593 /*
25594 ** Truncate an open file to a specified size
25595 */
25596 static int unixTruncate(sqlite3_file *id, i64 nByte){
25597 unixFile *pFile = (unixFile *)id;
25598 int rc;
25599 assert( pFile );
25600 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
25601
25602 /* If the user has configured a chunk-size for this file, truncate the
25603 ** file so that it consists of an integer number of chunks (i.e. the
25604 ** actual file size after the operation may be larger than the requested
25605 ** size).
25606 */
25607 if( pFile->szChunk ){
25608 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
25609 }
25610
25611 rc = ftruncate(pFile->h, (off_t)nByte);
25612 if( rc ){
25613 pFile->lastErrno = errno;
25614 return SQLITE_IOERR_TRUNCATE;
25615 }else{
25616 #ifndef NDEBUG
25617 /* If we are doing a normal write to a database file (as opposed to
25618 ** doing a hot-journal rollback or a write to some file other than a
@@ -25593,12 +25619,12 @@
25619 ** normal database file) and we truncate the file to zero length,
25620 ** that effectively updates the change counter. This might happen
25621 ** when restoring a database using the backup API from a zero-length
25622 ** source.
25623 */
25624 if( pFile->inNormalWrite && nByte==0 ){
25625 pFile->transCntrChng = 1;
25626 }
25627 #endif
25628
25629 return SQLITE_OK;
25630 }
@@ -25637,10 +25663,58 @@
25663 ** proxying locking division.
25664 */
25665 static int proxyFileControl(sqlite3_file*,int,void*);
25666 #endif
25667
25668 /*
25669 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
25670 ** file-control operation.
25671 **
25672 ** If the user has configured a chunk-size for this file, it could be
25673 ** that the file needs to be extended at this point. Otherwise, the
25674 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
25675 */
25676 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
25677 if( pFile->szChunk ){
25678 i64 nSize; /* Required file size */
25679 struct stat buf; /* Used to hold return values of fstat() */
25680
25681 if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
25682
25683 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
25684 if( nSize>(i64)buf.st_size ){
25685 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
25686 if( posix_fallocate(pFile->h, buf.st_size, nSize-buf.st_size) ){
25687 return SQLITE_IOERR_WRITE;
25688 }
25689 #else
25690 /* If the OS does not have posix_fallocate(), fake it. First use
25691 ** ftruncate() to set the file size, then write a single byte to
25692 ** the last byte in each block within the extended region. This
25693 ** is the same technique used by glibc to implement posix_fallocate()
25694 ** on systems that do not have a real fallocate() system call.
25695 */
25696 int nBlk = buf.st_blksize; /* File-system block size */
25697 i64 iWrite; /* Next offset to write to */
25698 int nWrite; /* Return value from seekAndWrite() */
25699
25700 if( ftruncate(pFile->h, nSize) ){
25701 pFile->lastErrno = errno;
25702 return SQLITE_IOERR_TRUNCATE;
25703 }
25704 iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
25705 do {
25706 nWrite = seekAndWrite(pFile, iWrite, "", 1);
25707 iWrite += nBlk;
25708 } while( nWrite==1 && iWrite<nSize );
25709 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
25710 #endif
25711 }
25712 }
25713
25714 return SQLITE_OK;
25715 }
25716
25717 /*
25718 ** Information and control of an open file handle.
25719 */
25720 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -25650,18 +25724,17 @@
25724 return SQLITE_OK;
25725 }
25726 case SQLITE_LAST_ERRNO: {
25727 *(int*)pArg = ((unixFile*)id)->lastErrno;
25728 return SQLITE_OK;
25729 }
25730 case SQLITE_FCNTL_CHUNK_SIZE: {
25731 ((unixFile*)id)->szChunk = *(int *)pArg;
25732 return SQLITE_OK;
25733 }
25734 case SQLITE_FCNTL_SIZE_HINT: {
25735 return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
 
 
 
 
 
25736 }
25737 #ifndef NDEBUG
25738 /* The pager calls this method to signal that it has done
25739 ** a rollback and that the database is therefore unchanged and
25740 ** it hence it is OK for the transaction change counter to be
@@ -28522,11 +28595,11 @@
28595 }else{
28596 if( pCtx->conchFile ){
28597 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
28598 sqlite3_free(pCtx->conchFile);
28599 }
28600 sqlite3DbFree(0, pCtx->lockProxyPath);
28601 sqlite3_free(pCtx->conchFilePath);
28602 sqlite3_free(pCtx);
28603 }
28604 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
28605 (rc==SQLITE_OK ? "ok" : "failed")));
@@ -28713,13 +28786,13 @@
28786 }
28787 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
28788 if( rc ) return rc;
28789 sqlite3_free(conchFile);
28790 }
28791 sqlite3DbFree(0, pCtx->lockProxyPath);
28792 sqlite3_free(pCtx->conchFilePath);
28793 sqlite3DbFree(0, pCtx->dbPath);
28794 /* restore the original locking context and pMethod then close it */
28795 pFile->lockingContext = pCtx->oldLockingContext;
28796 pFile->pMethod = pCtx->pOldMethod;
28797 sqlite3_free(pCtx);
28798 return pFile->pMethod->xClose(id);
@@ -29161,10 +29234,11 @@
29234 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
29235 DWORD lastErrno; /* The Windows errno from the last I/O error */
29236 DWORD sectorSize; /* Sector size of the device file is on */
29237 winShm *pShm; /* Instance of shared memory on this file */
29238 const char *zPath; /* Full pathname of this file */
29239 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
29240 #if SQLITE_OS_WINCE
29241 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
29242 HANDLE hMutex; /* Mutex used to control access to shared lock */
29243 HANDLE hShared; /* Shared memory segment used for locking */
29244 winceLock local; /* Locks obtained by this instance of winFile */
@@ -29671,10 +29745,46 @@
29745
29746 /*****************************************************************************
29747 ** The next group of routines implement the I/O methods specified
29748 ** by the sqlite3_io_methods object.
29749 ******************************************************************************/
29750
29751 /*
29752 ** Some microsoft compilers lack this definition.
29753 */
29754 #ifndef INVALID_SET_FILE_POINTER
29755 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
29756 #endif
29757
29758 /*
29759 ** Move the current position of the file handle passed as the first
29760 ** argument to offset iOffset within the file. If successful, return 0.
29761 ** Otherwise, set pFile->lastErrno and return non-zero.
29762 */
29763 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
29764 LONG upperBits; /* Most sig. 32 bits of new offset */
29765 LONG lowerBits; /* Least sig. 32 bits of new offset */
29766 DWORD dwRet; /* Value returned by SetFilePointer() */
29767
29768 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
29769 lowerBits = (LONG)(iOffset & 0xffffffff);
29770
29771 /* API oddity: If successful, SetFilePointer() returns a dword
29772 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
29773 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
29774 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
29775 ** whether an error has actually occured, it is also necessary to call
29776 ** GetLastError().
29777 */
29778 dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29779 if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
29780 pFile->lastErrno = GetLastError();
29781 return 1;
29782 }
29783
29784 return 0;
29785 }
29786
29787 /*
29788 ** Close a file.
29789 **
29790 ** It is reported that an attempt to close a handle might sometimes
@@ -29714,17 +29824,10 @@
29824 OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
29825 OpenCounter(-1);
29826 return rc ? SQLITE_OK : SQLITE_IOERR;
29827 }
29828
 
 
 
 
 
 
 
29829 /*
29830 ** Read data from a file into a buffer. Return SQLITE_OK if all
29831 ** bytes were read successfully and SQLITE_IOERR if anything goes
29832 ** wrong.
29833 */
@@ -29732,112 +29835,108 @@
29835 sqlite3_file *id, /* File to read from */
29836 void *pBuf, /* Write content into this buffer */
29837 int amt, /* Number of bytes to read */
29838 sqlite3_int64 offset /* Begin reading at this offset */
29839 ){
29840 winFile *pFile = (winFile*)id; /* file handle */
29841 DWORD nRead; /* Number of bytes actually read from file */
 
 
 
 
29842
29843 assert( id!=0 );
29844 SimulateIOError(return SQLITE_IOERR_READ);
29845 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
29846
29847 if( seekWinFile(pFile, offset) ){
 
29848 return SQLITE_FULL;
29849 }
29850 if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
29851 pFile->lastErrno = GetLastError();
29852 return SQLITE_IOERR_READ;
29853 }
29854 if( nRead<(DWORD)amt ){
 
 
29855 /* Unread parts of the buffer must be zero-filled */
29856 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
29857 return SQLITE_IOERR_SHORT_READ;
29858 }
29859
29860 return SQLITE_OK;
29861 }
29862
29863 /*
29864 ** Write data from a buffer into a file. Return SQLITE_OK on success
29865 ** or some other error code on failure.
29866 */
29867 static int winWrite(
29868 sqlite3_file *id, /* File to write into */
29869 const void *pBuf, /* The bytes to be written */
29870 int amt, /* Number of bytes to write */
29871 sqlite3_int64 offset /* Offset into the file to begin writing at */
29872 ){
29873 int rc; /* True if error has occured, else false */
29874 winFile *pFile = (winFile*)id; /* File handle */
29875
29876 assert( amt>0 );
29877 assert( pFile );
 
 
 
29878 SimulateIOError(return SQLITE_IOERR_WRITE);
29879 SimulateDiskfullError(return SQLITE_FULL);
29880
29881 OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
29882
29883 rc = seekWinFile(pFile, offset);
29884 if( rc==0 ){
29885 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
29886 int nRem = amt; /* Number of bytes yet to be written */
29887 DWORD nWrite; /* Bytes written by each WriteFile() call */
29888
29889 while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){
29890 aRem += nWrite;
29891 nRem -= nWrite;
29892 }
29893 if( nRem>0 ){
29894 pFile->lastErrno = GetLastError();
29895 rc = 1;
29896 }
29897 }
29898
29899 if( rc ){
29900 if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){
29901 return SQLITE_FULL;
29902 }
29903 return SQLITE_IOERR_WRITE;
 
 
29904 }
29905 return SQLITE_OK;
29906 }
29907
29908 /*
29909 ** Truncate an open file to a specified size
29910 */
29911 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
29912 winFile *pFile = (winFile*)id; /* File handle object */
29913 int rc = SQLITE_OK; /* Return code for this function */
29914
29915 assert( pFile );
29916
 
 
 
29917 OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
29918 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
29919
29920 /* If the user has configured a chunk-size for this file, truncate the
29921 ** file so that it consists of an integer number of chunks (i.e. the
29922 ** actual file size after the operation may be larger than the requested
29923 ** size).
29924 */
29925 if( pFile->szChunk ){
29926 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
29927 }
29928
29929 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
29930 if( seekWinFile(pFile, nByte) ){
29931 rc = SQLITE_IOERR_TRUNCATE;
29932 }else if( 0==SetEndOfFile(pFile->h) ){
 
29933 pFile->lastErrno = GetLastError();
29934 rc = SQLITE_IOERR_TRUNCATE;
29935 }
29936
29937 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
29938 return rc;
29939 }
29940
29941 #ifdef SQLITE_TEST
29942 /*
@@ -30197,10 +30296,14 @@
30296 return SQLITE_OK;
30297 }
30298 case SQLITE_LAST_ERRNO: {
30299 *(int*)pArg = (int)((winFile*)id)->lastErrno;
30300 return SQLITE_OK;
30301 }
30302 case SQLITE_FCNTL_CHUNK_SIZE: {
30303 ((winFile*)id)->szChunk = *(int *)pArg;
30304 return SQLITE_OK;
30305 }
30306 case SQLITE_FCNTL_SIZE_HINT: {
30307 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
30308 SimulateIOErrorBenign(1);
30309 winTruncate(id, sz);
@@ -31749,11 +31852,11 @@
31852 ** start of a transaction, and is thus usually less than a few thousand,
31853 ** but can be as large as 2 billion for a really big database.
31854 */
31855
31856 /* Size of the Bitvec structure in bytes. */
31857 #define BITVEC_SZ 512
31858
31859 /* Round the union size down to the nearest pointer boundary, since that's how
31860 ** it will be aligned within the Bitvec struct. */
31861 #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
31862
@@ -33964,11 +34067,11 @@
34067 # define sqlite3WalOpen(x,y,z) 0
34068 # define sqlite3WalClose(w,x,y,z) 0
34069 # define sqlite3WalBeginReadTransaction(y,z) 0
34070 # define sqlite3WalEndReadTransaction(z)
34071 # define sqlite3WalRead(v,w,x,y,z) 0
34072 # define sqlite3WalDbsize(y) 0
34073 # define sqlite3WalBeginWriteTransaction(y) 0
34074 # define sqlite3WalEndWriteTransaction(x) 0
34075 # define sqlite3WalUndo(x,y,z) 0
34076 # define sqlite3WalSavepoint(y,z)
34077 # define sqlite3WalSavepointUndo(y,z) 0
@@ -34000,13 +34103,12 @@
34103 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
34104
34105 /* Read a page from the write-ahead log, if it is present. */
34106 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
34107
34108 /* If the WAL is not empty, return the size of the database. */
34109 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
 
34110
34111 /* Obtain or release the WRITER lock. */
34112 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
34113 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
34114
@@ -34048,12 +34150,16 @@
34150 #endif /* _WAL_H_ */
34151
34152 /************** End of wal.h *************************************************/
34153 /************** Continuing where we left off in pager.c **********************/
34154
34155
34156 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
34157 **
34158 ** This comment block describes invariants that hold when using a rollback
34159 ** journal. These invariants do not apply for journal_mode=WAL,
34160 ** journal_mode=MEMORY, or journal_mode=OFF.
34161 **
34162 ** Within this comment block, a page is deemed to have been synced
34163 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
34164 ** Otherwise, the page is not synced until the xSync method of the VFS
34165 ** is called successfully on the file containing the page.
@@ -34083,11 +34189,11 @@
34189 ** both the content in the database when the rollback journal was written
34190 ** and the content in the database at the beginning of the current
34191 ** transaction.
34192 **
34193 ** (3) Writes to the database file are an integer multiple of the page size
34194 ** in length and are aligned on a page boundary.
34195 **
34196 ** (4) Reads from the database file are either aligned on a page boundary and
34197 ** an integer multiple of the page size in length or are taken from the
34198 ** first 100 bytes of the database file.
34199 **
@@ -34114,11 +34220,12 @@
34220 ** method is a no-op, but that does not change the fact the SQLite will
34221 ** invoke it.)
34222 **
34223 ** (9) Whenever the database file is modified, at least one bit in the range
34224 ** of bytes from 24 through 39 inclusive will be changed prior to releasing
34225 ** the EXCLUSIVE lock, thus signaling other connections on the same
34226 ** database to flush their caches.
34227 **
34228 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
34229 ** than one billion transactions.
34230 **
34231 ** (11) A database file is well-formed at the beginning and at the conclusion
@@ -34127,11 +34234,12 @@
34234 ** (12) An EXCLUSIVE lock is held on the database file when writing to
34235 ** the database file.
34236 **
34237 ** (13) A SHARED lock is held on the database file while reading any
34238 ** content out of the database file.
34239 **
34240 ******************************************************************************/
34241
34242 /*
34243 ** Macros for troubleshooting. Normally turned off
34244 */
34245 #if 0
@@ -34152,62 +34260,283 @@
34260 */
34261 #define PAGERID(p) ((int)(p->fd))
34262 #define FILEHANDLEID(fd) ((int)fd)
34263
34264 /*
34265 ** The Pager.eState variable stores the current 'state' of a pager. A
34266 ** pager may be in any one of the seven states shown in the following
34267 ** state diagram.
34268 **
34269 ** OPEN <------+------+
34270 ** | | |
34271 ** V | |
34272 ** +---------> READER-------+ |
34273 ** | | |
34274 ** | V |
34275 ** |<-------WRITER_LOCKED------> ERROR
34276 ** | | ^
34277 ** | V |
34278 ** |<------WRITER_CACHEMOD-------->|
34279 ** | | |
34280 ** | V |
34281 ** |<-------WRITER_DBMOD---------->|
34282 ** | | |
34283 ** | V |
34284 ** +<------WRITER_FINISHED-------->+
34285 **
34286 **
34287 ** List of state transitions and the C [function] that performs each:
34288 **
34289 ** OPEN -> READER [sqlite3PagerSharedLock]
34290 ** READER -> OPEN [pager_unlock]
34291 **
34292 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
34293 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
34294 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
34295 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
34296 ** WRITER_*** -> READER [pager_end_transaction]
34297 **
34298 ** WRITER_*** -> ERROR [pager_error]
34299 ** ERROR -> OPEN [pager_unlock]
34300 **
34301 **
34302 ** OPEN:
34303 **
34304 ** The pager starts up in this state. Nothing is guaranteed in this
34305 ** state - the file may or may not be locked and the database size is
34306 ** unknown. The database may not be read or written.
34307 **
34308 ** * No read or write transaction is active.
34309 ** * Any lock, or no lock at all, may be held on the database file.
34310 ** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
34311 **
34312 ** READER:
34313 **
34314 ** In this state all the requirements for reading the database in
34315 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
34316 ** was) in exclusive-locking mode, a user-level read transaction is
34317 ** open. The database size is known in this state.
34318 **
34319 ** A connection running with locking_mode=normal enters this state when
34320 ** it opens a read-transaction on the database and returns to state
34321 ** OPEN after the read-transaction is completed. However a connection
34322 ** running in locking_mode=exclusive (including temp databases) remains in
34323 ** this state even after the read-transaction is closed. The only way
34324 ** a locking_mode=exclusive connection can transition from READER to OPEN
34325 ** is via the ERROR state (see below).
34326 **
34327 ** * A read transaction may be active (but a write-transaction cannot).
34328 ** * A SHARED or greater lock is held on the database file.
34329 ** * The dbSize variable may be trusted (even if a user-level read
34330 ** transaction is not active). The dbOrigSize and dbFileSize variables
34331 ** may not be trusted at this point.
34332 ** * If the database is a WAL database, then the WAL connection is open.
34333 ** * Even if a read-transaction is not open, it is guaranteed that
34334 ** there is no hot-journal in the file-system.
34335 **
34336 ** WRITER_LOCKED:
34337 **
34338 ** The pager moves to this state from READER when a write-transaction
34339 ** is first opened on the database. In WRITER_LOCKED state, all locks
34340 ** required to start a write-transaction are held, but no actual
34341 ** modifications to the cache or database have taken place.
34342 **
34343 ** In rollback mode, a RESERVED or (if the transaction was opened with
34344 ** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
34345 ** moving to this state, but the journal file is not written to or opened
34346 ** to in this state. If the transaction is committed or rolled back while
34347 ** in WRITER_LOCKED state, all that is required is to unlock the database
34348 ** file.
34349 **
34350 ** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
34351 ** If the connection is running with locking_mode=exclusive, an attempt
34352 ** is made to obtain an EXCLUSIVE lock on the database file.
34353 **
34354 ** * A write transaction is active.
34355 ** * If the connection is open in rollback-mode, a RESERVED or greater
34356 ** lock is held on the database file.
34357 ** * If the connection is open in WAL-mode, a WAL write transaction
34358 ** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
34359 ** called).
34360 ** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
34361 ** * The contents of the pager cache have not been modified.
34362 ** * The journal file may or may not be open.
34363 ** * Nothing (not even the first header) has been written to the journal.
34364 **
34365 ** WRITER_CACHEMOD:
34366 **
34367 ** A pager moves from WRITER_LOCKED state to this state when a page is
34368 ** first modified by the upper layer. In rollback mode the journal file
34369 ** is opened (if it is not already open) and a header written to the
34370 ** start of it. The database file on disk has not been modified.
34371 **
34372 ** * A write transaction is active.
34373 ** * A RESERVED or greater lock is held on the database file.
34374 ** * The journal file is open and the first header has been written
34375 ** to it, but the header has not been synced to disk.
34376 ** * The contents of the page cache have been modified.
34377 **
34378 ** WRITER_DBMOD:
34379 **
34380 ** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
34381 ** when it modifies the contents of the database file. WAL connections
34382 ** never enter this state (since they do not modify the database file,
34383 ** just the log file).
34384 **
34385 ** * A write transaction is active.
34386 ** * An EXCLUSIVE or greater lock is held on the database file.
34387 ** * The journal file is open and the first header has been written
34388 ** and synced to disk.
34389 ** * The contents of the page cache have been modified (and possibly
34390 ** written to disk).
34391 **
34392 ** WRITER_FINISHED:
34393 **
34394 ** It is not possible for a WAL connection to enter this state.
34395 **
34396 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
34397 ** state after the entire transaction has been successfully written into the
34398 ** database file. In this state the transaction may be committed simply
34399 ** by finalizing the journal file. Once in WRITER_FINISHED state, it is
34400 ** not possible to modify the database further. At this point, the upper
34401 ** layer must either commit or rollback the transaction.
34402 **
34403 ** * A write transaction is active.
34404 ** * An EXCLUSIVE or greater lock is held on the database file.
34405 ** * All writing and syncing of journal and database data has finished.
34406 ** If no error occured, all that remains is to finalize the journal to
34407 ** commit the transaction. If an error did occur, the caller will need
34408 ** to rollback the transaction.
34409 **
34410 ** ERROR:
34411 **
34412 ** The ERROR state is entered when an IO or disk-full error (including
34413 ** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
34414 ** difficult to be sure that the in-memory pager state (cache contents,
34415 ** db size etc.) are consistent with the contents of the file-system.
34416 **
34417 ** Temporary pager files may enter the ERROR state, but in-memory pagers
34418 ** cannot.
34419 **
34420 ** For example, if an IO error occurs while performing a rollback,
34421 ** the contents of the page-cache may be left in an inconsistent state.
34422 ** At this point it would be dangerous to change back to READER state
34423 ** (as usually happens after a rollback). Any subsequent readers might
34424 ** report database corruption (due to the inconsistent cache), and if
34425 ** they upgrade to writers, they may inadvertently corrupt the database
34426 ** file. To avoid this hazard, the pager switches into the ERROR state
34427 ** instead of READER following such an error.
34428 **
34429 ** Once it has entered the ERROR state, any attempt to use the pager
34430 ** to read or write data returns an error. Eventually, once all
34431 ** outstanding transactions have been abandoned, the pager is able to
34432 ** transition back to OPEN state, discarding the contents of the
34433 ** page-cache and any other in-memory state at the same time. Everything
34434 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
34435 ** when a read-transaction is next opened on the pager (transitioning
34436 ** the pager into READER state). At that point the system has recovered
34437 ** from the error.
34438 **
34439 ** Specifically, the pager jumps into the ERROR state if:
34440 **
34441 ** 1. An error occurs while attempting a rollback. This happens in
34442 ** function sqlite3PagerRollback().
34443 **
34444 ** 2. An error occurs while attempting to finalize a journal file
34445 ** following a commit in function sqlite3PagerCommitPhaseTwo().
34446 **
34447 ** 3. An error occurs while attempting to write to the journal or
34448 ** database file in function pagerStress() in order to free up
34449 ** memory.
34450 **
34451 ** In other cases, the error is returned to the b-tree layer. The b-tree
34452 ** layer then attempts a rollback operation. If the error condition
34453 ** persists, the pager enters the ERROR state via condition (1) above.
34454 **
34455 ** Condition (3) is necessary because it can be triggered by a read-only
34456 ** statement executed within a transaction. In this case, if the error
34457 ** code were simply returned to the user, the b-tree layer would not
34458 ** automatically attempt a rollback, as it assumes that an error in a
34459 ** read-only statement cannot leave the pager in an internally inconsistent
34460 ** state.
34461 **
34462 ** * The Pager.errCode variable is set to something other than SQLITE_OK.
34463 ** * There are one or more outstanding references to pages (after the
34464 ** last reference is dropped the pager should move back to OPEN state).
34465 ** * The pager is not an in-memory pager.
34466 **
34467 **
34468 ** Notes:
34469 **
34470 ** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
34471 ** connection is open in WAL mode. A WAL connection is always in one
34472 ** of the first four states.
34473 **
34474 ** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
34475 ** state. There are two exceptions: immediately after exclusive-mode has
34476 ** been turned on (and before any read or write transactions are
34477 ** executed), and when the pager is leaving the "error state".
34478 **
34479 ** * See also: assert_pager_state().
34480 */
34481 #define PAGER_OPEN 0
34482 #define PAGER_READER 1
34483 #define PAGER_WRITER_LOCKED 2
34484 #define PAGER_WRITER_CACHEMOD 3
34485 #define PAGER_WRITER_DBMOD 4
34486 #define PAGER_WRITER_FINISHED 5
34487 #define PAGER_ERROR 6
34488
34489 /*
34490 ** The Pager.eLock variable is almost always set to one of the
34491 ** following locking-states, according to the lock currently held on
34492 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
34493 ** This variable is kept up to date as locks are taken and released by
34494 ** the pagerLockDb() and pagerUnlockDb() wrappers.
34495 **
34496 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
34497 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
34498 ** the operation was successful. In these circumstances pagerLockDb() and
34499 ** pagerUnlockDb() take a conservative approach - eLock is always updated
34500 ** when unlocking the file, and only updated when locking the file if the
34501 ** VFS call is successful. This way, the Pager.eLock variable may be set
34502 ** to a less exclusive (lower) value than the lock that is actually held
34503 ** at the system level, but it is never set to a more exclusive value.
34504 **
34505 ** This is usually safe. If an xUnlock fails or appears to fail, there may
34506 ** be a few redundant xLock() calls or a lock may be held for longer than
34507 ** required, but nothing really goes wrong.
34508 **
34509 ** The exception is when the database file is unlocked as the pager moves
34510 ** from ERROR to OPEN state. At this point there may be a hot-journal file
34511 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
34512 ** transition, by the same pager or any other). If the call to xUnlock()
34513 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
34514 ** can confuse the call to xCheckReservedLock() call made later as part
34515 ** of hot-journal detection.
34516 **
34517 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
34518 ** lock held by this process or any others". So xCheckReservedLock may
34519 ** return true because the caller itself is holding an EXCLUSIVE lock (but
34520 ** doesn't know it because of a previous error in xUnlock). If this happens
34521 ** a hot-journal may be mistaken for a journal being created by an active
34522 ** transaction in another process, causing SQLite to read from the database
34523 ** without rolling it back.
34524 **
34525 ** To work around this, if a call to xUnlock() fails when unlocking the
34526 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
34527 ** is only changed back to a real locking state after a successful call
34528 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
34529 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
34530 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
34531 ** lock on the database file before attempting to roll it back. See function
34532 ** PagerSharedLock() for more detail.
34533 **
34534 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
34535 ** PAGER_OPEN state.
34536 */
34537 #define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1)
34538
34539 /*
34540 ** A macro used for invoking the codec if there is one
34541 */
34542 #ifdef SQLITE_HAS_CODEC
@@ -34253,37 +34582,32 @@
34582 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
34583 #endif
34584 };
34585
34586 /*
34587 ** A open page cache is an instance of struct Pager. A description of
34588 ** some of the more important member variables follows:
34589 **
34590 ** eState
34591 **
34592 ** The current 'state' of the pager object. See the comment and state
34593 ** diagram above for a description of the pager state.
34594 **
34595 ** eLock
34596 **
34597 ** For a real on-disk database, the current lock held on the database file -
34598 ** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
34599 **
34600 ** For a temporary or in-memory database (neither of which require any
34601 ** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
34602 ** databases always have Pager.exclusiveMode==1, this tricks the pager
34603 ** logic into thinking that it already has all the locks it will ever
34604 ** need (and no reason to release them).
34605 **
34606 ** In some (obscure) circumstances, this variable may also be set to
34607 ** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
34608 ** details.
 
 
 
 
 
34609 **
34610 ** changeCountDone
34611 **
34612 ** This boolean variable is used to make sure that the change-counter
34613 ** (the 4-byte header field at byte offset 24 of the database file) is
@@ -34298,28 +34622,10 @@
34622 **
34623 ** This mechanism means that when running in exclusive mode, a connection
34624 ** need only update the change-counter once, for the first transaction
34625 ** committed.
34626 **
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34627 ** setMaster
34628 **
34629 ** When PagerCommitPhaseOne() is called to commit a transaction, it may
34630 ** (or may not) specify a master-journal name to be written into the
34631 ** journal file before it is synced to disk.
@@ -34326,84 +34632,146 @@
34632 **
34633 ** Whether or not a journal file contains a master-journal pointer affects
34634 ** the way in which the journal file is finalized after the transaction is
34635 ** committed or rolled back when running in "journal_mode=PERSIST" mode.
34636 ** If a journal file does not contain a master-journal pointer, it is
34637 ** finalized by overwriting the first journal header with zeroes. If
34638 ** it does contain a master-journal pointer the journal file is finalized
34639 ** by truncating it to zero bytes, just as if the connection were
34640 ** running in "journal_mode=truncate" mode.
34641 **
34642 ** Journal files that contain master journal pointers cannot be finalized
34643 ** simply by overwriting the first journal-header with zeroes, as the
34644 ** master journal pointer could interfere with hot-journal rollback of any
34645 ** subsequently interrupted transaction that reuses the journal file.
34646 **
34647 ** The flag is cleared as soon as the journal file is finalized (either
34648 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
34649 ** journal file from being successfully finalized, the setMaster flag
34650 ** is cleared anyway (and the pager will move to ERROR state).
34651 **
34652 ** doNotSpill, doNotSyncSpill
34653 **
34654 ** These two boolean variables control the behaviour of cache-spills
34655 ** (calls made by the pcache module to the pagerStress() routine to
34656 ** write cached data to the file-system in order to free up memory).
34657 **
34658 ** When doNotSpill is non-zero, writing to the database from pagerStress()
34659 ** is disabled altogether. This is done in a very obscure case that
34660 ** comes up during savepoint rollback that requires the pcache module
34661 ** to allocate a new page to prevent the journal file from being written
34662 ** while it is being traversed by code in pager_playback().
34663 **
34664 ** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
34665 ** is permitted, but syncing the journal file is not. This flag is set
34666 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
34667 ** the database page-size in order to prevent a journal sync from happening
34668 ** in between the journalling of two pages on the same sector.
34669 **
34670 ** subjInMemory
34671 **
34672 ** This is a boolean variable. If true, then any required sub-journal
34673 ** is opened as an in-memory journal file. If false, then in-memory
34674 ** sub-journals are only used for in-memory pager files.
34675 **
34676 ** This variable is updated by the upper layer each time a new
34677 ** write-transaction is opened.
34678 **
34679 ** dbSize, dbOrigSize, dbFileSize
34680 **
34681 ** Variable dbSize is set to the number of pages in the database file.
34682 ** It is valid in PAGER_READER and higher states (all states except for
34683 ** OPEN and ERROR).
34684 **
34685 ** dbSize is set based on the size of the database file, which may be
34686 ** larger than the size of the database (the value stored at offset
34687 ** 28 of the database header by the btree). If the size of the file
34688 ** is not an integer multiple of the page-size, the value stored in
34689 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
34690 ** Except, any file that is greater than 0 bytes in size is considered
34691 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
34692 ** to dbSize==1).
34693 **
34694 ** During a write-transaction, if pages with page-numbers greater than
34695 ** dbSize are modified in the cache, dbSize is updated accordingly.
34696 ** Similarly, if the database is truncated using PagerTruncateImage(),
34697 ** dbSize is updated.
34698 **
34699 ** Variables dbOrigSize and dbFileSize are valid in states
34700 ** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
34701 ** variable at the start of the transaction. It is used during rollback,
34702 ** and to determine whether or not pages need to be journalled before
34703 ** being modified.
34704 **
34705 ** Throughout a write-transaction, dbFileSize contains the size of
34706 ** the file on disk in pages. It is set to a copy of dbSize when the
34707 ** write-transaction is first opened, and updated when VFS calls are made
34708 ** to write or truncate the database file on disk.
34709 **
34710 ** The only reason the dbFileSize variable is required is to suppress
34711 ** unnecessary calls to xTruncate() after committing a transaction. If,
34712 ** when a transaction is committed, the dbFileSize variable indicates
34713 ** that the database file is larger than the database image (Pager.dbSize),
34714 ** pager_truncate() is called. The pager_truncate() call uses xFilesize()
34715 ** to measure the database file on disk, and then truncates it if required.
34716 ** dbFileSize is not used when rolling back a transaction. In this case
34717 ** pager_truncate() is called unconditionally (which means there may be
34718 ** a call to xFilesize() that is not strictly required). In either case,
34719 ** pager_truncate() may cause the file to become smaller or larger.
34720 **
34721 ** dbHintSize
34722 **
34723 ** The dbHintSize variable is used to limit the number of calls made to
34724 ** the VFS xFileControl(FCNTL_SIZE_HINT) method.
34725 **
34726 ** dbHintSize is set to a copy of the dbSize variable when a
34727 ** write-transaction is opened (at the same time as dbFileSize and
34728 ** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
34729 ** dbHintSize is increased to the number of pages that correspond to the
34730 ** size-hint passed to the method call. See pager_write_pagelist() for
34731 ** details.
34732 **
34733 ** errCode
34734 **
34735 ** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
34736 ** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
34737 ** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
34738 ** sub-codes.
34739 */
34740 struct Pager {
34741 sqlite3_vfs *pVfs; /* OS functions to use for IO */
34742 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
34743 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
34744 u8 useJournal; /* Use a rollback journal on this file */
34745 u8 noReadlock; /* Do not bother to obtain readlocks */
34746 u8 noSync; /* Do not sync the journal if true */
34747 u8 fullSync; /* Do extra syncs of the journal for robustness */
34748 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
34749 u8 tempFile; /* zFilename is a temporary file */
34750 u8 readOnly; /* True for a read-only database */
34751 u8 memDb; /* True to inhibit all file I/O */
34752
34753 /**************************************************************************
34754 ** The following block contains those class members that change during
34755 ** routine opertion. Class members not in this block are either fixed
34756 ** when the pager is first created or else only change when there is a
34757 ** significant mode change (such as changing the page_size, locking_mode,
34758 ** or the journal_mode). From another view, these class members describe
34759 ** the "state" of the pager, while other class members describe the
34760 ** "configuration" of the pager.
 
 
34761 */
34762 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
34763 u8 eLock; /* Current lock held on database file */
 
 
34764 u8 changeCountDone; /* Set after incrementing the change-counter */
34765 u8 setMaster; /* True if a m-j name has been written to jrnl */
34766 u8 doNotSpill; /* Do not spill the cache when non-zero */
34767 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
 
34768 u8 subjInMemory; /* True to use in-memory sub-journals */
34769 Pgno dbSize; /* Number of pages in the database */
34770 Pgno dbOrigSize; /* dbSize before the current transaction */
34771 Pgno dbFileSize; /* Number of pages in the database file */
34772 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
34773 int errCode; /* One of several kinds of errors */
34774 int nRec; /* Pages journalled since last j-header written */
34775 u32 cksumInit; /* Quasi-random value added to every checksum */
34776 u32 nSubRec; /* Number of records written to sub-journal */
34777 Bitvec *pInJournal; /* One bit for each page in the database file */
@@ -34410,21 +34778,25 @@
34778 sqlite3_file *fd; /* File descriptor for database */
34779 sqlite3_file *jfd; /* File descriptor for main journal */
34780 sqlite3_file *sjfd; /* File descriptor for sub-journal */
34781 i64 journalOff; /* Current write offset in the journal file */
34782 i64 journalHdr; /* Byte offset to previous journal header */
34783 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
34784 PagerSavepoint *aSavepoint; /* Array of active savepoints */
34785 int nSavepoint; /* Number of elements in aSavepoint[] */
34786 char dbFileVers[16]; /* Changes whenever database file changes */
34787 /*
34788 ** End of the routinely-changing class members
34789 ***************************************************************************/
34790
34791 u16 nExtra; /* Add this many bytes to each in-memory page */
34792 i16 nReserve; /* Number of unused bytes at end of each page */
34793 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
34794 u32 sectorSize; /* Assumed sector size during rollback */
34795 int pageSize; /* Number of bytes in a page */
34796 Pgno mxPgno; /* Maximum allowed size of the database */
34797 i64 journalSizeLimit; /* Size limit for persistent journal files */
34798 char *zFilename; /* Name of the database file */
34799 char *zJournal; /* Name of the journal file */
34800 int (*xBusyHandler)(void*); /* Function to call when busy */
34801 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
34802 #ifdef SQLITE_TEST
@@ -34438,11 +34810,10 @@
34810 void (*xCodecFree)(void*); /* Destructor for the codec */
34811 void *pCodec; /* First argument to xCodec... methods */
34812 #endif
34813 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
34814 PCache *pPCache; /* Pointer to page cache object */
 
34815 #ifndef SQLITE_OMIT_WAL
34816 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
34817 char *zWal; /* File name for write-ahead log */
34818 #endif
34819 };
@@ -34517,26 +34888,225 @@
34888 /*
34889 ** The maximum legal page number is (2^31 - 1).
34890 */
34891 #define PAGER_MAX_PGNO 2147483647
34892
34893 /*
34894 ** The argument to this macro is a file descriptor (type sqlite3_file*).
34895 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
34896 **
34897 ** This is so that expressions can be written as:
34898 **
34899 ** if( isOpen(pPager->jfd) ){ ...
34900 **
34901 ** instead of
34902 **
34903 ** if( pPager->jfd->pMethods ){ ...
34904 */
34905 #define isOpen(pFd) ((pFd)->pMethods)
34906
34907 /*
34908 ** Return true if this pager uses a write-ahead log instead of the usual
34909 ** rollback journal. Otherwise false.
34910 */
34911 #ifndef SQLITE_OMIT_WAL
34912 static int pagerUseWal(Pager *pPager){
34913 return (pPager->pWal!=0);
34914 }
34915 #else
34916 # define pagerUseWal(x) 0
34917 # define pagerRollbackWal(x) 0
34918 # define pagerWalFrames(v,w,x,y,z) 0
34919 # define pagerOpenWalIfPresent(z) SQLITE_OK
34920 # define pagerBeginReadTransaction(z) SQLITE_OK
34921 #endif
34922
34923 #ifndef NDEBUG
34924 /*
34925 ** Usage:
34926 **
34927 ** assert( assert_pager_state(pPager) );
34928 **
34929 ** This function runs many asserts to try to find inconsistencies in
34930 ** the internal state of the Pager object.
34931 */
34932 static int assert_pager_state(Pager *p){
34933 Pager *pPager = p;
34934
34935 /* State must be valid. */
34936 assert( p->eState==PAGER_OPEN
34937 || p->eState==PAGER_READER
34938 || p->eState==PAGER_WRITER_LOCKED
34939 || p->eState==PAGER_WRITER_CACHEMOD
34940 || p->eState==PAGER_WRITER_DBMOD
34941 || p->eState==PAGER_WRITER_FINISHED
34942 || p->eState==PAGER_ERROR
34943 );
34944
34945 /* Regardless of the current state, a temp-file connection always behaves
34946 ** as if it has an exclusive lock on the database file. It never updates
34947 ** the change-counter field, so the changeCountDone flag is always set.
34948 */
34949 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
34950 assert( p->tempFile==0 || pPager->changeCountDone );
34951
34952 /* If the useJournal flag is clear, the journal-mode must be "OFF".
34953 ** And if the journal-mode is "OFF", the journal file must not be open.
34954 */
34955 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
34956 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
34957
34958 /* Check that MEMDB implies noSync. And an in-memory journal. Since
34959 ** this means an in-memory pager performs no IO at all, it cannot encounter
34960 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
34961 ** a journal file. (although the in-memory journal implementation may
34962 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
34963 ** is therefore not possible for an in-memory pager to enter the ERROR
34964 ** state.
34965 */
34966 if( MEMDB ){
34967 assert( p->noSync );
34968 assert( p->journalMode==PAGER_JOURNALMODE_OFF
34969 || p->journalMode==PAGER_JOURNALMODE_MEMORY
34970 );
34971 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
34972 assert( pagerUseWal(p)==0 );
34973 }
34974
34975 /* If changeCountDone is set, a RESERVED lock or greater must be held
34976 ** on the file.
34977 */
34978 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
34979 assert( p->eLock!=PENDING_LOCK );
34980
34981 switch( p->eState ){
34982 case PAGER_OPEN:
34983 assert( !MEMDB );
34984 assert( pPager->errCode==SQLITE_OK );
34985 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
34986 break;
34987
34988 case PAGER_READER:
34989 assert( pPager->errCode==SQLITE_OK );
34990 assert( p->eLock!=UNKNOWN_LOCK );
34991 assert( p->eLock>=SHARED_LOCK || p->noReadlock );
34992 break;
34993
34994 case PAGER_WRITER_LOCKED:
34995 assert( p->eLock!=UNKNOWN_LOCK );
34996 assert( pPager->errCode==SQLITE_OK );
34997 if( !pagerUseWal(pPager) ){
34998 assert( p->eLock>=RESERVED_LOCK );
34999 }
35000 assert( pPager->dbSize==pPager->dbOrigSize );
35001 assert( pPager->dbOrigSize==pPager->dbFileSize );
35002 assert( pPager->dbOrigSize==pPager->dbHintSize );
35003 assert( pPager->setMaster==0 );
35004 break;
35005
35006 case PAGER_WRITER_CACHEMOD:
35007 assert( p->eLock!=UNKNOWN_LOCK );
35008 assert( pPager->errCode==SQLITE_OK );
35009 if( !pagerUseWal(pPager) ){
35010 /* It is possible that if journal_mode=wal here that neither the
35011 ** journal file nor the WAL file are open. This happens during
35012 ** a rollback transaction that switches from journal_mode=off
35013 ** to journal_mode=wal.
35014 */
35015 assert( p->eLock>=RESERVED_LOCK );
35016 assert( isOpen(p->jfd)
35017 || p->journalMode==PAGER_JOURNALMODE_OFF
35018 || p->journalMode==PAGER_JOURNALMODE_WAL
35019 );
35020 }
35021 assert( pPager->dbOrigSize==pPager->dbFileSize );
35022 assert( pPager->dbOrigSize==pPager->dbHintSize );
35023 break;
35024
35025 case PAGER_WRITER_DBMOD:
35026 assert( p->eLock==EXCLUSIVE_LOCK );
35027 assert( pPager->errCode==SQLITE_OK );
35028 assert( !pagerUseWal(pPager) );
35029 assert( p->eLock>=EXCLUSIVE_LOCK );
35030 assert( isOpen(p->jfd)
35031 || p->journalMode==PAGER_JOURNALMODE_OFF
35032 || p->journalMode==PAGER_JOURNALMODE_WAL
35033 );
35034 assert( pPager->dbOrigSize<=pPager->dbHintSize );
35035 break;
35036
35037 case PAGER_WRITER_FINISHED:
35038 assert( p->eLock==EXCLUSIVE_LOCK );
35039 assert( pPager->errCode==SQLITE_OK );
35040 assert( !pagerUseWal(pPager) );
35041 assert( isOpen(p->jfd)
35042 || p->journalMode==PAGER_JOURNALMODE_OFF
35043 || p->journalMode==PAGER_JOURNALMODE_WAL
35044 );
35045 break;
35046
35047 case PAGER_ERROR:
35048 /* There must be at least one outstanding reference to the pager if
35049 ** in ERROR state. Otherwise the pager should have already dropped
35050 ** back to OPEN state.
35051 */
35052 assert( pPager->errCode!=SQLITE_OK );
35053 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
35054 break;
35055 }
35056
35057 return 1;
35058 }
35059
35060 /*
35061 ** Return a pointer to a human readable string in a static buffer
35062 ** containing the state of the Pager object passed as an argument. This
35063 ** is intended to be used within debuggers. For example, as an alternative
35064 ** to "print *pPager" in gdb:
35065 **
35066 ** (gdb) printf "%s", print_pager_state(pPager)
35067 */
35068 static char *print_pager_state(Pager *p){
35069 static char zRet[1024];
35070
35071 sqlite3_snprintf(1024, zRet,
35072 "Filename: %s\n"
35073 "State: %s errCode=%d\n"
35074 "Lock: %s\n"
35075 "Locking mode: locking_mode=%s\n"
35076 "Journal mode: journal_mode=%s\n"
35077 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
35078 "Journal: journalOff=%lld journalHdr=%lld\n"
35079 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
35080 , p->zFilename
35081 , p->eState==PAGER_OPEN ? "OPEN" :
35082 p->eState==PAGER_READER ? "READER" :
35083 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
35084 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
35085 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
35086 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
35087 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
35088 , (int)p->errCode
35089 , p->eLock==NO_LOCK ? "NO_LOCK" :
35090 p->eLock==RESERVED_LOCK ? "RESERVED" :
35091 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
35092 p->eLock==SHARED_LOCK ? "SHARED" :
35093 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
35094 , p->exclusiveMode ? "exclusive" : "normal"
35095 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
35096 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
35097 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
35098 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
35099 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
35100 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
35101 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
35102 , p->journalOff, p->journalHdr
35103 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
35104 );
35105
35106 return zRet;
35107 }
35108 #endif
35109
35110 /*
35111 ** Return true if it is necessary to write page *pPg into the sub-journal.
35112 ** A page needs to be written into the sub-journal if there exists one
@@ -34584,10 +35154,11 @@
35154
35155 /*
35156 ** Write a 32-bit integer into a string buffer in big-endian byte order.
35157 */
35158 #define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
35159
35160
35161 /*
35162 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
35163 ** on success or an error code is something goes wrong.
35164 */
@@ -34596,31 +35167,57 @@
35167 put32bits(ac, val);
35168 return sqlite3OsWrite(fd, ac, 4, offset);
35169 }
35170
35171 /*
35172 ** Unlock the database file to level eLock, which must be either NO_LOCK
35173 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
35174 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
35175 **
35176 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
35177 ** called, do not modify it. See the comment above the #define of
35178 ** UNKNOWN_LOCK for an explanation of this.
35179 */
35180 static int pagerUnlockDb(Pager *pPager, int eLock){
35181 int rc = SQLITE_OK;
35182
35183 assert( !pPager->exclusiveMode );
35184 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
35185 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
35186 if( isOpen(pPager->fd) ){
35187 assert( pPager->eLock>=eLock );
35188 rc = sqlite3OsUnlock(pPager->fd, eLock);
35189 if( pPager->eLock!=UNKNOWN_LOCK ){
35190 pPager->eLock = eLock;
35191 }
35192 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
35193 }
35194 return rc;
35195 }
35196
35197 /*
35198 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
35199 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
35200 ** Pager.eLock variable to the new locking state.
35201 **
35202 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
35203 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
35204 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
35205 ** of this.
35206 */
35207 static int pagerLockDb(Pager *pPager, int eLock){
35208 int rc = SQLITE_OK;
35209
35210 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
35211 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
35212 rc = sqlite3OsLock(pPager->fd, eLock);
35213 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
35214 pPager->eLock = eLock;
35215 IOTRACE(("LOCK %p %d\n", pPager, eLock))
35216 }
35217 }
35218 return rc;
35219 }
35220
35221 /*
35222 ** This function determines whether or not the atomic-write optimization
35223 ** can be used with this pager. The optimization can be used if:
@@ -34865,11 +35462,11 @@
35462 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
35463 */
35464 static int writeJournalHdr(Pager *pPager){
35465 int rc = SQLITE_OK; /* Return code */
35466 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
35467 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
35468 u32 nWrite; /* Bytes of header sector written */
35469 int ii; /* Loop counter */
35470
35471 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
35472
@@ -34908,11 +35505,11 @@
35505 **
35506 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
35507 ** that garbage data is never appended to the journal file.
35508 */
35509 assert( isOpen(pPager->fd) || pPager->noSync );
35510 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
35511 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
35512 ){
35513 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
35514 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
35515 }else{
@@ -35032,11 +35629,10 @@
35629 }
35630
35631 if( pPager->journalOff==0 ){
35632 u32 iPageSize; /* Page-size field of journal header */
35633 u32 iSectorSize; /* Sector-size field of journal header */
 
35634
35635 /* Read the page-size and sector-size journal header fields. */
35636 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
35637 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
35638 ){
@@ -35062,14 +35658,12 @@
35658
35659 /* Update the page-size to match the value read from the journal.
35660 ** Use a testcase() macro to make sure that malloc failure within
35661 ** PagerSetPagesize() is tested.
35662 */
35663 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
 
35664 testcase( rc!=SQLITE_OK );
 
35665
35666 /* Update the assumed sector-size to match the value used by
35667 ** the process that created this journal. If this journal was
35668 ** created by a process other than this one, then this routine
35669 ** is being called from within pager_playback(). The local value
@@ -35108,10 +35702,12 @@
35702 i64 iHdrOff; /* Offset of header in journal file */
35703 i64 jrnlSize; /* Size of journal file on disk */
35704 u32 cksum = 0; /* Checksum of string zMaster */
35705
35706 assert( pPager->setMaster==0 );
35707 assert( !pagerUseWal(pPager) );
35708
35709 if( !zMaster
35710 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
35711 || pPager->journalMode==PAGER_JOURNALMODE_OFF
35712 ){
35713 return SQLITE_OK;
@@ -35144,11 +35740,10 @@
35740 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
35741 ){
35742 return rc;
35743 }
35744 pPager->journalOff += (nMaster+20);
 
35745
35746 /* If the pager is in peristent-journal mode, then the physical
35747 ** journal-file may extend past the end of the master-journal name
35748 ** and 8 bytes of magic data just written to the file. This is
35749 ** dangerous because the code to rollback a hot-journal file
@@ -35180,21 +35775,15 @@
35775 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
35776 return p;
35777 }
35778
35779 /*
35780 ** Discard the entire contents of the in-memory page-cache.
 
 
 
35781 */
35782 static void pager_reset(Pager *pPager){
35783 sqlite3BackupRestart(pPager->pBackup);
35784 sqlite3PcacheClear(pPager->pPCache);
 
 
 
35785 }
35786
35787 /*
35788 ** Free all structures in the Pager.aSavepoint[] array and set both
35789 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
@@ -35233,38 +35822,43 @@
35822 }
35823 return rc;
35824 }
35825
35826 /*
35827 ** This function is a no-op if the pager is in exclusive mode and not
35828 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
35829 ** state.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35830 **
35831 ** If the pager is not in exclusive-access mode, the database file is
35832 ** completely unlocked. If the file is unlocked and the file-system does
35833 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
35834 ** closed (if it is open).
35835 **
35836 ** If the pager is in ERROR state when this function is called, the
35837 ** contents of the pager cache are discarded before switching back to
35838 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
35839 ** or not, any journal file left in the file-system will be treated
35840 ** as a hot-journal and rolled back the next time a read-transaction
35841 ** is opened (by this or by any other connection).
35842 */
35843 static void pager_unlock(Pager *pPager){
35844
35845 assert( pPager->eState==PAGER_READER
35846 || pPager->eState==PAGER_OPEN
35847 || pPager->eState==PAGER_ERROR
35848 );
35849
35850 sqlite3BitvecDestroy(pPager->pInJournal);
35851 pPager->pInJournal = 0;
35852 releaseAllSavepoints(pPager);
35853
35854 if( pagerUseWal(pPager) ){
35855 assert( !isOpen(pPager->jfd) );
35856 sqlite3WalEndReadTransaction(pPager->pWal);
35857 pPager->eState = PAGER_OPEN;
35858 }else if( !pPager->exclusiveMode ){
35859 int rc; /* Error code returned by pagerUnlockDb() */
35860 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
35861
35862 /* If the operating system support deletion of open files, then
35863 ** close the journal file when dropping the database lock. Otherwise
35864 ** another connection with journal_mode=delete might delete the file
@@ -35280,62 +35874,60 @@
35874 || 1!=(pPager->journalMode & 5)
35875 ){
35876 sqlite3OsClose(pPager->jfd);
35877 }
35878
35879 /* If the pager is in the ERROR state and the call to unlock the database
35880 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
35881 ** above the #define for UNKNOWN_LOCK for an explanation of why this
35882 ** is necessary.
35883 */
35884 rc = pagerUnlockDb(pPager, NO_LOCK);
35885 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
35886 pPager->eLock = UNKNOWN_LOCK;
35887 }
35888
35889 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
35890 ** without clearing the error code. This is intentional - the error
35891 ** code is cleared and the cache reset in the block below.
35892 */
35893 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35894 pPager->changeCountDone = 0;
35895 pPager->eState = PAGER_OPEN;
 
35896 }
35897
35898 /* If Pager.errCode is set, the contents of the pager cache cannot be
35899 ** trusted. Now that there are no outstanding references to the pager,
35900 ** it can safely move back to PAGER_OPEN state. This happens in both
35901 ** normal and exclusive-locking mode.
35902 */
35903 if( pPager->errCode ){
35904 assert( !MEMDB );
35905 pager_reset(pPager);
35906 pPager->changeCountDone = pPager->tempFile;
35907 pPager->eState = PAGER_OPEN;
35908 pPager->errCode = SQLITE_OK;
35909 }
35910
35911 pPager->journalOff = 0;
35912 pPager->journalHdr = 0;
35913 pPager->setMaster = 0;
35914 }
35915
35916 /*
35917 ** This function is called whenever an IOERR or FULL error that requires
35918 ** the pager to transition into the ERROR state may ahve occurred.
35919 ** The first argument is a pointer to the pager structure, the second
35920 ** the error-code about to be returned by a pager API function. The
35921 ** value returned is a copy of the second argument to this function.
35922 **
35923 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
35924 ** IOERR sub-codes, the pager enters the ERROR state and the error code
35925 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
35926 ** all major API calls on the Pager will immediately return Pager.errCode.
35927 **
35928 ** The ERROR state indicates that the contents of the pager-cache
35929 ** cannot be trusted. This state can be cleared by completely discarding
35930 ** the contents of the pager-cache. If a transaction was active when
35931 ** the persistent error occurred, then the rollback journal may need
35932 ** to be replayed to restore the contents of the database file (as if
35933 ** it were a hot-journal).
@@ -35348,49 +35940,25 @@
35940 pPager->errCode==SQLITE_OK ||
35941 (pPager->errCode & 0xff)==SQLITE_IOERR
35942 );
35943 if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
35944 pPager->errCode = rc;
35945 pPager->eState = PAGER_ERROR;
35946 }
35947 return rc;
35948 }
35949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35950 /*
35951 ** This routine ends a transaction. A transaction is usually ended by
35952 ** either a COMMIT or a ROLLBACK operation. This routine may be called
35953 ** after rollback of a hot-journal, or if an error occurs while opening
35954 ** the journal file or writing the very first journal-header of a
35955 ** database transaction.
35956 **
35957 ** This routine is never called in PAGER_ERROR state. If it is called
35958 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
35959 ** exclusive than a RESERVED lock, it is a no-op.
35960 **
35961 ** Otherwise, any active savepoints are released.
35962 **
35963 ** If the journal file is open, then it is "finalized". Once a journal
35964 ** file has been finalized it is not possible to use it to roll back a
@@ -35417,17 +35985,13 @@
35985 ** If the pager is running in exclusive mode, this method of finalizing
35986 ** the journal file is never used. Instead, if the journalMode is
35987 ** DELETE and the pager is in exclusive mode, the method described under
35988 ** journalMode==PERSIST is used instead.
35989 **
35990 ** After the journal is finalized, the pager moves to PAGER_READER state.
35991 ** If running in non-exclusive rollback mode, the lock on the file is
35992 ** downgraded to a SHARED_LOCK.
 
 
 
 
35993 **
35994 ** SQLITE_OK is returned if no error occurs. If an error occurs during
35995 ** any of the IO operations to finalize the journal file or unlock the
35996 ** database then the IO error code is returned to the user. If the
35997 ** operation to finalize the journal file fails, then the code still
@@ -35438,15 +36002,30 @@
36002 */
36003 static int pager_end_transaction(Pager *pPager, int hasMaster){
36004 int rc = SQLITE_OK; /* Error code from journal finalization operation */
36005 int rc2 = SQLITE_OK; /* Error code from db file unlock operation */
36006
36007 /* Do nothing if the pager does not have an open write transaction
36008 ** or at least a RESERVED lock. This function may be called when there
36009 ** is no write-transaction active but a RESERVED or greater lock is
36010 ** held under two circumstances:
36011 **
36012 ** 1. After a successful hot-journal rollback, it is called with
36013 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
36014 **
36015 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
36016 ** lock switches back to locking_mode=normal and then executes a
36017 ** read-transaction, this function is called with eState==PAGER_READER
36018 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
36019 */
36020 assert( assert_pager_state(pPager) );
36021 assert( pPager->eState!=PAGER_ERROR );
36022 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
36023 return SQLITE_OK;
36024 }
36025
36026 releaseAllSavepoints(pPager);
 
36027 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
36028 if( isOpen(pPager->jfd) ){
36029 assert( !pagerUseWal(pPager) );
36030
36031 /* Finalize the journal file. */
@@ -35458,18 +36037,15 @@
36037 rc = SQLITE_OK;
36038 }else{
36039 rc = sqlite3OsTruncate(pPager->jfd, 0);
36040 }
36041 pPager->journalOff = 0;
 
36042 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
36043 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
36044 ){
36045 rc = zeroJournalHdr(pPager, hasMaster);
 
36046 pPager->journalOff = 0;
 
36047 }else{
36048 /* This branch may be executed with Pager.journalMode==MEMORY if
36049 ** a hot-journal was just rolled back. In this case the journal
36050 ** file should be closed and deleted. If this connection writes to
36051 ** the database file, it will do so using an in-memory journal.
@@ -35490,43 +36066,63 @@
36066 }
36067 sqlite3BitvecDestroy(pPager->pInJournal);
36068 pPager->pInJournal = 0;
36069 pPager->nRec = 0;
36070 sqlite3PcacheCleanAll(pPager->pPCache);
36071 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
36072
36073 if( pagerUseWal(pPager) ){
36074 /* Drop the WAL write-lock, if any. Also, if the connection was in
36075 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
36076 ** lock held on the database file.
36077 */
36078 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
36079 assert( rc2==SQLITE_OK );
36080 }
36081 if( !pPager->exclusiveMode
36082 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
36083 ){
36084 rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
 
 
 
 
 
 
36085 pPager->changeCountDone = 0;
36086 }
36087 pPager->eState = PAGER_READER;
36088 pPager->setMaster = 0;
 
 
 
 
 
 
 
 
 
 
 
36089
36090 return (rc==SQLITE_OK?rc2:rc);
36091 }
36092
36093 /*
36094 ** Execute a rollback if a transaction is active and unlock the
36095 ** database file.
36096 **
36097 ** If the pager has already entered the ERROR state, do not attempt
36098 ** the rollback at this time. Instead, pager_unlock() is called. The
36099 ** call to pager_unlock() will discard all in-memory pages, unlock
36100 ** the database file and move the pager back to OPEN state. If this
36101 ** means that there is a hot-journal left in the file-system, the next
36102 ** connection to obtain a shared lock on the pager (which may be this one)
36103 ** will roll it back.
36104 **
36105 ** If the pager has not already entered the ERROR state, but an IO or
36106 ** malloc error occurs during a rollback, then this will itself cause
36107 ** the pager to enter the ERROR state. Which will be cleared by the
36108 ** call to pager_unlock(), as described above.
36109 */
36110 static void pagerUnlockAndRollback(Pager *pPager){
36111 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
36112 assert( assert_pager_state(pPager) );
36113 if( pPager->eState>=PAGER_WRITER_LOCKED ){
36114 sqlite3BeginBenignMalloc();
36115 sqlite3PagerRollback(pPager);
36116 sqlite3EndBenignMalloc();
36117 }else if( !pPager->exclusiveMode ){
36118 assert( pPager->eState==PAGER_READER );
36119 pager_end_transaction(pPager, 0);
36120 }
36121 }
36122 pager_unlock(pPager);
36123 }
36124
36125 /*
36126 ** Parameter aData must point to a buffer of pPager->pageSize bytes
36127 ** of data. Compute and return a checksum based ont the contents of the
36128 ** page of data and the current value of pPager->cksumInit.
@@ -35574,13 +36170,12 @@
36170 ** Read a single page from either the journal file (if isMainJrnl==1) or
36171 ** from the sub-journal (if isMainJrnl==0) and playback that page.
36172 ** The page begins at offset *pOffset into the file. The *pOffset
36173 ** value is increased to the start of the next page in the journal.
36174 **
36175 ** The main rollback journal uses checksums - the statement journal does
36176 ** not.
 
36177 **
36178 ** If the page number of the page record read from the (sub-)journal file
36179 ** is greater than the current value of Pager.dbSize, then playback is
36180 ** skipped and SQLITE_OK is returned.
36181 **
@@ -35629,10 +36224,21 @@
36224 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
36225
36226 aData = pPager->pTmpSpace;
36227 assert( aData ); /* Temp storage must have already been allocated */
36228 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
36229
36230 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
36231 ** or savepoint rollback done at the request of the caller) or this is
36232 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
36233 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
36234 ** only reads from the main journal, not the sub-journal.
36235 */
36236 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
36237 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
36238 );
36239 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
36240
36241 /* Read the page number and page data from the journal or sub-journal
36242 ** file. Return an error code to the caller if an IO error occurs.
36243 */
36244 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -35666,20 +36272,19 @@
36272 ** rollback, then don't bother to play it back again.
36273 */
36274 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
36275 return rc;
36276 }
 
36277
36278 /* When playing back page 1, restore the nReserve setting
36279 */
36280 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
36281 pPager->nReserve = ((u8*)aData)[20];
36282 pagerReportSize(pPager);
36283 }
36284
36285 /* If the pager is in CACHEMOD state, then there must be a copy of this
36286 ** page in the pager cache. In this case just update the pager cache,
36287 ** not the database file. The page is left marked dirty in this case.
36288 **
36289 ** An exception to the above rule: If the database is in no-sync mode
36290 ** and a page is moved during an incremental vacuum then the page may
@@ -35686,12 +36291,15 @@
36291 ** not be in the pager cache. Later: if a malloc() or IO error occurs
36292 ** during a Movepage() call, then the page may not be in the cache
36293 ** either. So the condition described in the above paragraph is not
36294 ** assert()able.
36295 **
36296 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
36297 ** pager cache if it exists and the main file. The page is then marked
36298 ** not dirty. Since this code is only executed in PAGER_OPEN state for
36299 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
36300 ** if the pager is in OPEN state.
36301 **
36302 ** Ticket #1171: The statement journal might contain page content that is
36303 ** different from the page content at the start of the transaction.
36304 ** This occurs when a page is changed prior to the start of a statement
36305 ** then changed again within the statement. When rolling back such a
@@ -35713,21 +36321,22 @@
36321 pPg = 0;
36322 }else{
36323 pPg = pager_lookup(pPager, pgno);
36324 }
36325 assert( pPg || !MEMDB );
36326 assert( pPager->eState!=PAGER_OPEN || pPg==0 );
36327 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
36328 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
36329 (isMainJrnl?"main-journal":"sub-journal")
36330 ));
36331 if( isMainJrnl ){
36332 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
36333 }else{
36334 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
36335 }
36336 if( isOpen(pPager->fd)
36337 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
36338 && isSynced
36339 ){
36340 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
36341 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
36342 assert( !pagerUseWal(pPager) );
@@ -35953,14 +36562,14 @@
36562 /*
36563 ** This function is used to change the actual size of the database
36564 ** file in the file-system. This only happens when committing a transaction,
36565 ** or rolling back a transaction (including rolling back a hot-journal).
36566 **
36567 ** If the main database file is not open, or the pager is not in either
36568 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
36569 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
36570 ** If the file on disk is currently larger than nPage pages, then use the VFS
36571 ** xTruncate() method to truncate it.
36572 **
36573 ** Or, it might might be the case that the file on disk is smaller than
36574 ** nPage pages. Some operating system implementations can get confused if
36575 ** you try to truncate a file to some size that is larger than it
@@ -35970,12 +36579,18 @@
36579 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
36580 ** the database file, return the error code to the caller.
36581 */
36582 static int pager_truncate(Pager *pPager, Pgno nPage){
36583 int rc = SQLITE_OK;
36584 assert( pPager->eState!=PAGER_ERROR );
36585 assert( pPager->eState!=PAGER_READER );
36586
36587 if( isOpen(pPager->fd)
36588 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
36589 ){
36590 i64 currentSize, newSize;
36591 assert( pPager->eLock==EXCLUSIVE_LOCK );
36592 /* TODO: Is it safe to use Pager.dbFileSize here? */
36593 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
36594 newSize = pPager->pageSize*(i64)nPage;
36595 if( rc==SQLITE_OK && currentSize!=newSize ){
36596 if( currentSize>newSize ){
@@ -36095,11 +36710,11 @@
36710 /* Figure out how many records are in the journal. Abort early if
36711 ** the journal is empty.
36712 */
36713 assert( isOpen(pPager->jfd) );
36714 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
36715 if( rc!=SQLITE_OK ){
36716 goto end_playback;
36717 }
36718
36719 /* Read the master journal name from the journal, if it is present.
36720 ** If a master journal file name is specified, but the file is not
@@ -36129,11 +36744,11 @@
36744 ** occurs.
36745 */
36746 while( 1 ){
36747 /* Read the next journal header from the journal file. If there are
36748 ** not enough bytes left in the journal file for a complete header, or
36749 ** it is corrupted, then a process must have failed while writing it.
36750 ** This indicates nothing more needs to be rolled back.
36751 */
36752 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
36753 if( rc!=SQLITE_OK ){
36754 if( rc==SQLITE_DONE ){
@@ -36243,14 +36858,13 @@
36858 if( rc==SQLITE_OK ){
36859 zMaster = pPager->pTmpSpace;
36860 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
36861 testcase( rc!=SQLITE_OK );
36862 }
36863 if( rc==SQLITE_OK && !pPager->noSync
36864 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
36865 ){
 
36866 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
36867 }
36868 if( rc==SQLITE_OK ){
36869 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
36870 testcase( rc!=SQLITE_OK );
@@ -36288,11 +36902,11 @@
36902 Pgno pgno = pPg->pgno; /* Page number to read */
36903 int rc = SQLITE_OK; /* Return code */
36904 int isInWal = 0; /* True if page is in log file */
36905 int pgsz = pPager->pageSize; /* Number of bytes to read */
36906
36907 assert( pPager->eState>=PAGER_READER && !MEMDB );
36908 assert( isOpen(pPager->fd) );
36909
36910 if( NEVER(!isOpen(pPager->fd)) ){
36911 assert( pPager->tempFile );
36912 memset(pPg->pData, 0, pPager->pageSize);
@@ -36451,31 +37065,82 @@
37065 static int pagerBeginReadTransaction(Pager *pPager){
37066 int rc; /* Return code */
37067 int changed = 0; /* True if cache must be reset */
37068
37069 assert( pagerUseWal(pPager) );
37070 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
37071
37072 /* sqlite3WalEndReadTransaction() was not called for the previous
37073 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
37074 ** are in locking_mode=NORMAL and EndRead() was previously called,
37075 ** the duplicate call is harmless.
37076 */
37077 sqlite3WalEndReadTransaction(pPager->pWal);
37078
37079 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
37080 if( rc==SQLITE_OK && changed ){
37081 pager_reset(pPager);
37082 }
 
 
 
 
 
 
37083
37084 return rc;
37085 }
37086
37087 /*
37088 ** This function is called as part of the transition from PAGER_OPEN
37089 ** to PAGER_READER state to determine the size of the database file
37090 ** in pages (assuming the page size currently stored in Pager.pageSize).
37091 **
37092 ** If no error occurs, SQLITE_OK is returned and the size of the database
37093 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
37094 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
37095 */
37096 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
37097 Pgno nPage; /* Value to return via *pnPage */
37098
37099 /* Query the WAL sub-system for the database size. The WalDbsize()
37100 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
37101 ** if the database size is not available. The database size is not
37102 ** available from the WAL sub-system if the log file is empty or
37103 ** contains no valid committed transactions.
37104 */
37105 assert( pPager->eState==PAGER_OPEN );
37106 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
37107 nPage = sqlite3WalDbsize(pPager->pWal);
37108
37109 /* If the database size was not available from the WAL sub-system,
37110 ** determine it based on the size of the database file. If the size
37111 ** of the database file is not an integer multiple of the page-size,
37112 ** round down to the nearest page. Except, any file larger than 0
37113 ** bytes in size is considered to contain at least one page.
37114 */
37115 if( nPage==0 ){
37116 i64 n = 0; /* Size of db file in bytes */
37117 assert( isOpen(pPager->fd) || pPager->tempFile );
37118 if( isOpen(pPager->fd) ){
37119 int rc = sqlite3OsFileSize(pPager->fd, &n);
37120 if( rc!=SQLITE_OK ){
37121 return rc;
37122 }
37123 }
37124 nPage = (Pgno)(n / pPager->pageSize);
37125 if( nPage==0 && n>0 ){
37126 nPage = 1;
37127 }
37128 }
37129
37130 /* If the current number of pages in the file is greater than the
37131 ** configured maximum pager number, increase the allowed limit so
37132 ** that the file can be read.
37133 */
37134 if( nPage>pPager->mxPgno ){
37135 pPager->mxPgno = (Pgno)nPage;
37136 }
37137
37138 *pnPage = nPage;
37139 return SQLITE_OK;
37140 }
37141
37142
37143 /*
37144 ** Check if the *-wal file that corresponds to the database opened by pPager
37145 ** exists if the database is not empy, or verify that the *-wal file does
37146 ** not exist (by deleting it) if the database file is empty.
@@ -36485,25 +37150,26 @@
37150 ** if no error occurs, make sure Pager.journalMode is not set to
37151 ** PAGER_JOURNALMODE_WAL.
37152 **
37153 ** Return SQLITE_OK or an error code.
37154 **
 
 
37155 ** The caller must hold a SHARED lock on the database file to call this
37156 ** function. Because an EXCLUSIVE lock on the db file is required to delete
37157 ** a WAL on a none-empty database, this ensures there is no race condition
37158 ** between the xAccess() below and an xDelete() being executed by some
37159 ** other connection.
37160 */
37161 static int pagerOpenWalIfPresent(Pager *pPager){
37162 int rc = SQLITE_OK;
37163 assert( pPager->eState==PAGER_OPEN );
37164 assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
37165
37166 if( !pPager->tempFile ){
37167 int isWal; /* True if WAL file exists */
37168 Pgno nPage; /* Size of the database file */
37169
37170 rc = pagerPagecount(pPager, &nPage);
37171 if( rc ) return rc;
37172 if( nPage==0 ){
37173 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
37174 isWal = 0;
37175 }else{
@@ -36511,15 +37177,12 @@
37177 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
37178 );
37179 }
37180 if( rc==SQLITE_OK ){
37181 if( isWal ){
37182 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
37183 rc = sqlite3PagerOpenWal(pPager, 0);
 
 
 
37184 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
37185 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
37186 }
37187 }
37188 }
@@ -36567,11 +37230,12 @@
37230 i64 szJ; /* Effective size of the main journal */
37231 i64 iHdrOff; /* End of first segment of main-journal records */
37232 int rc = SQLITE_OK; /* Return code */
37233 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
37234
37235 assert( pPager->eState!=PAGER_ERROR );
37236 assert( pPager->eState>=PAGER_WRITER_LOCKED );
37237
37238 /* Allocate a bitvec to use to store the set of pages rolled back */
37239 if( pSavepoint ){
37240 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
37241 if( !pDone ){
@@ -36706,11 +37370,10 @@
37370 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
37371 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){
37372 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
37373 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
37374 pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
 
37375 }
37376 #endif
37377
37378 /*
37379 ** The following global variable is incremented whenever the library
@@ -36788,11 +37451,11 @@
37451 ** Change the page size used by the Pager object. The new page size
37452 ** is passed in *pPageSize.
37453 **
37454 ** If the pager is in the error state when this function is called, it
37455 ** is a no-op. The value returned is the error state error code (i.e.
37456 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
37457 **
37458 ** Otherwise, if all of the following are true:
37459 **
37460 ** * the new page size (value of *pPageSize) is valid (a power
37461 ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
@@ -36812,32 +37475,52 @@
37475 ** If the page size is not changed, either because one of the enumerated
37476 ** conditions above is not true, the pager was in error state when this
37477 ** function was called, or because the memory allocation attempt failed,
37478 ** then *pPageSize is set to the old, retained page size before returning.
37479 */
37480 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
37481 int rc = SQLITE_OK;
37482
37483 /* It is not possible to do a full assert_pager_state() here, as this
37484 ** function may be called from within PagerOpen(), before the state
37485 ** of the Pager object is internally consistent.
37486 **
37487 ** At one point this function returned an error if the pager was in
37488 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
37489 ** there is at least one outstanding page reference, this function
37490 ** is a no-op for that case anyhow.
37491 */
37492
37493 u32 pageSize = *pPageSize;
37494 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
37495 if( (pPager->memDb==0 || pPager->dbSize==0)
37496 && sqlite3PcacheRefCount(pPager->pPCache)==0
37497 && pageSize && pageSize!=(u32)pPager->pageSize
37498 ){
37499 char *pNew; /* New temp space */
37500 i64 nByte = 0;
37501
37502 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
37503 rc = sqlite3OsFileSize(pPager->fd, &nByte);
37504 }
37505 if( rc==SQLITE_OK ){
37506 pNew = (char *)sqlite3PageMalloc(pageSize);
37507 if( !pNew ) rc = SQLITE_NOMEM;
37508 }
37509
37510 if( rc==SQLITE_OK ){
37511 pager_reset(pPager);
37512 pPager->dbSize = nByte/pageSize;
37513 pPager->pageSize = pageSize;
37514 sqlite3PageFree(pPager->pTmpSpace);
37515 pPager->pTmpSpace = pNew;
37516 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
37517 }
37518 }
37519
37520 *pPageSize = pPager->pageSize;
37521 if( rc==SQLITE_OK ){
37522 if( nReserve<0 ) nReserve = pPager->nReserve;
37523 assert( nReserve>=0 && nReserve<1000 );
37524 pPager->nReserve = (i16)nReserve;
37525 pagerReportSize(pPager);
37526 }
@@ -36862,17 +37545,15 @@
37545 ** maximum page count below the current size of the database.
37546 **
37547 ** Regardless of mxPage, return the current maximum page count.
37548 */
37549 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
 
37550 if( mxPage>0 ){
37551 pPager->mxPgno = mxPage;
37552 }
37553 if( pPager->eState!=PAGER_OPEN && pPager->mxPgno<pPager->dbSize ){
37554 pPager->mxPgno = pPager->dbSize;
 
37555 }
37556 return pPager->mxPgno;
37557 }
37558
37559 /*
@@ -36933,70 +37614,20 @@
37614 }
37615 return rc;
37616 }
37617
37618 /*
37619 ** This function may only be called when a read-transaction is open on
37620 ** the pager. It returns the total number of pages in the database.
37621 **
37622 ** However, if the file is between 1 and <page-size> bytes in size, then
37623 ** this is considered a 1 page file.
37624 */
37625 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
37626 assert( pPager->eState>=PAGER_READER );
37627 assert( pPager->eState!=PAGER_WRITER_FINISHED );
37628 *pnPage = (int)pPager->dbSize;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37629 }
37630
37631
37632 /*
37633 ** Try to obtain a lock of type locktype on the database file. If
@@ -37013,42 +37644,23 @@
37644 ** variable to locktype before returning.
37645 */
37646 static int pager_wait_on_lock(Pager *pPager, int locktype){
37647 int rc; /* Return code */
37648
 
 
 
 
 
 
 
 
 
 
 
37649 /* Check that this is either a no-op (because the requested lock is
37650 ** already held, or one of the transistions that the busy-handler
37651 ** may be invoked during, according to the comment above
37652 ** sqlite3PagerSetBusyhandler().
37653 */
37654 assert( (pPager->eLock>=locktype)
37655 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
37656 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
37657 );
37658
37659 do {
37660 rc = pagerLockDb(pPager, locktype);
37661 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
 
 
 
 
 
 
 
 
37662 return rc;
37663 }
37664
37665 /*
37666 ** Function assertTruncateConstraint(pPager) checks that one of the
@@ -37089,13 +37701,12 @@
37701 ** function does not actually modify the database file on disk. It
37702 ** just sets the internal state of the pager object so that the
37703 ** truncation will be done when the current transaction is committed.
37704 */
37705 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
 
37706 assert( pPager->dbSize>=nPage );
37707 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
37708 pPager->dbSize = nPage;
37709 assertTruncateConstraint(pPager);
37710 }
37711
37712
@@ -37141,11 +37752,11 @@
37752 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
37753 u8 *pTmp = (u8 *)pPager->pTmpSpace;
37754
37755 disable_simulated_io_errors();
37756 sqlite3BeginBenignMalloc();
37757 /* pPager->errCode = 0; */
37758 pPager->exclusiveMode = 0;
37759 #ifndef SQLITE_OMIT_WAL
37760 sqlite3WalClose(pPager->pWal,
37761 (pPager->noSync ? 0 : pPager->sync_flags),
37762 pPager->pageSize, pTmp
@@ -37154,18 +37765,23 @@
37765 #endif
37766 pager_reset(pPager);
37767 if( MEMDB ){
37768 pager_unlock(pPager);
37769 }else{
37770 /* If it is open, sync the journal file before calling UnlockAndRollback.
37771 ** If this is not done, then an unsynced portion of the open journal
37772 ** file may be played back into the database. If a power failure occurs
37773 ** while this is happening, the database could become corrupt.
37774 **
37775 ** If an error occurs while trying to sync the journal, shift the pager
37776 ** into the ERROR state. This causes UnlockAndRollback to unlock the
37777 ** database and close the journal file without attempting to roll it
37778 ** back or finalize it. The next database user will have to do hot-journal
37779 ** rollback before accessing the database file.
37780 */
37781 if( isOpen(pPager->jfd) ){
37782 pager_error(pPager, pagerSyncHotJournal(pPager));
37783 }
37784 pagerUnlockAndRollback(pPager);
37785 }
37786 sqlite3EndBenignMalloc();
37787 enable_simulated_io_errors();
@@ -37206,13 +37822,13 @@
37822 /*
37823 ** Sync the journal. In other words, make sure all the pages that have
37824 ** been written to the journal have actually reached the surface of the
37825 ** disk and can be restored in the event of a hot-journal rollback.
37826 **
37827 ** If the Pager.noSync flag is set, then this function is a no-op.
37828 ** Otherwise, the actions required depend on the journal-mode and the
37829 ** device characteristics of the the file-system, as follows:
37830 **
37831 ** * If the journal file is an in-memory journal file, no action need
37832 ** be taken.
37833 **
37834 ** * Otherwise, if the device does not support the SAFE_APPEND property,
@@ -37232,22 +37848,29 @@
37848 ** <update nRec field>
37849 ** }
37850 ** if( NOT SEQUENTIAL ) xSync(<journal file>);
37851 ** }
37852 **
 
 
 
37853 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
37854 ** page currently held in memory before returning SQLITE_OK. If an IO
37855 ** error is encountered, then the IO error code is returned to the caller.
37856 */
37857 static int syncJournal(Pager *pPager, int newHdr){
37858 int rc; /* Return code */
37859
37860 assert( pPager->eState==PAGER_WRITER_CACHEMOD
37861 || pPager->eState==PAGER_WRITER_DBMOD
37862 );
37863 assert( assert_pager_state(pPager) );
37864 assert( !pagerUseWal(pPager) );
37865
37866 rc = sqlite3PagerExclusiveLock(pPager);
37867 if( rc!=SQLITE_OK ) return rc;
37868
37869 if( !pPager->noSync ){
37870 assert( !pPager->tempFile );
37871 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
 
37872 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
37873 assert( isOpen(pPager->jfd) );
37874
37875 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
37876 /* This block deals with an obscure problem. If the last connection
@@ -37318,21 +37941,29 @@
37941 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
37942 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
37943 );
37944 if( rc!=SQLITE_OK ) return rc;
37945 }
37946
37947 pPager->journalHdr = pPager->journalOff;
37948 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
37949 pPager->nRec = 0;
37950 rc = writeJournalHdr(pPager);
37951 if( rc!=SQLITE_OK ) return rc;
37952 }
37953 }else{
37954 pPager->journalHdr = pPager->journalOff;
37955 }
37956 }
37957
37958 /* Unless the pager is in noSync mode, the journal file was just
37959 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
37960 ** all pages.
37961 */
37962 sqlite3PcacheClearSyncFlags(pPager->pPCache);
37963 pPager->eState = PAGER_WRITER_DBMOD;
37964 assert( assert_pager_state(pPager) );
37965 return SQLITE_OK;
37966 }
37967
37968 /*
37969 ** The argument is the first in a linked list of dirty pages connected
@@ -37365,31 +37996,16 @@
37996 ** If everything is successful, SQLITE_OK is returned. If an IO error
37997 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
37998 ** be obtained, SQLITE_BUSY is returned.
37999 */
38000 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
38001 int rc = SQLITE_OK; /* Return code */
38002
38003 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38004 assert( !pagerUseWal(pPager) );
38005 assert( pPager->eState==PAGER_WRITER_DBMOD );
38006 assert( pPager->eLock==EXCLUSIVE_LOCK );
38007
38008 /* If the file is a temp-file has not yet been opened, open it now. It
38009 ** is not possible for rc to be other than SQLITE_OK if this branch
38010 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
38011 */
@@ -37400,13 +38016,14 @@
38016
38017 /* Before the first write, give the VFS a hint of what the final
38018 ** file size will be.
38019 */
38020 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
38021 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
38022 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
38023 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
38024 pPager->dbHintSize = pPager->dbSize;
38025 }
38026
38027 while( rc==SQLITE_OK && pList ){
38028 Pgno pgno = pList->pgno;
38029
@@ -37419,10 +38036,12 @@
38036 ** set (set by sqlite3PagerDontWrite()).
38037 */
38038 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
38039 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
38040 char *pData; /* Data to write */
38041
38042 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
38043
38044 /* Encode the database */
38045 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
38046
38047 /* Write out the page data. */
@@ -37562,13 +38181,18 @@
38181 ** pages belonging to the same sector.
38182 **
38183 ** The doNotSpill flag inhibits all cache spilling regardless of whether
38184 ** or not a sync is required. This is set during a rollback.
38185 **
38186 ** Spilling is also prohibited when in an error state since that could
38187 ** lead to database corruption. In the current implementaton it
38188 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
38189 ** while in the error state, hence it is impossible for this routine to
38190 ** be called in the error state. Nevertheless, we include a NEVER()
38191 ** test for the error state as a safeguard against future changes.
38192 */
38193 if( NEVER(pPager->errCode) ) return SQLITE_OK;
38194 if( pPager->doNotSpill ) return SQLITE_OK;
38195 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
38196 return SQLITE_OK;
38197 }
38198
@@ -37582,20 +38206,14 @@
38206 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
38207 }
38208 }else{
38209
38210 /* Sync the journal file if required. */
38211 if( pPg->flags&PGHDR_NEED_SYNC
38212 || pPager->eState==PAGER_WRITER_CACHEMOD
38213 ){
38214 rc = syncJournal(pPager, 1);
 
 
 
 
 
 
38215 }
38216
38217 /* If the page number of this page is larger than the current size of
38218 ** the database image, it may need to be written to the sub-journal.
38219 ** This is because the call to pager_write_pagelist() below will not
@@ -37629,10 +38247,11 @@
38247 rc = subjournalPage(pPg);
38248 }
38249
38250 /* Write the contents of the page out to the database file. */
38251 if( rc==SQLITE_OK ){
38252 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
38253 rc = pager_write_pagelist(pPager, pPg);
38254 }
38255 }
38256
38257 /* Mark the page as clean. */
@@ -37639,11 +38258,11 @@
38258 if( rc==SQLITE_OK ){
38259 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
38260 sqlite3PcacheMakeClean(pPg);
38261 }
38262
38263 return pager_error(pPager, rc);
38264 }
38265
38266
38267 /*
38268 ** Allocate and initialize a new Pager object and put a pointer to it
@@ -37694,11 +38313,11 @@
38313 char *zPathname = 0; /* Full path to database file */
38314 int nPathname = 0; /* Number of bytes in zPathname */
38315 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
38316 int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */
38317 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
38318 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */
38319
38320 /* Figure out how much space is required for each journal file-handle
38321 ** (there are two of them, the main journal and the sub-journal). This
38322 ** is the maximum space required for an in-memory journal file handle
38323 ** and a regular journal file-handle. Note that a "regular journal-handle"
@@ -37829,11 +38448,11 @@
38448 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
38449 if( szPageDflt<pPager->sectorSize ){
38450 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
38451 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
38452 }else{
38453 szPageDflt = (u32)pPager->sectorSize;
38454 }
38455 }
38456 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
38457 {
38458 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
@@ -37857,11 +38476,12 @@
38476 ** This branch is also run for an in-memory database. An in-memory
38477 ** database is the same as a temp-file that is never written out to
38478 ** disk and uses an in-memory rollback journal.
38479 */
38480 tempFile = 1;
38481 pPager->eState = PAGER_READER;
38482 pPager->eLock = EXCLUSIVE_LOCK;
38483 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
38484 }
38485
38486 /* The following call to PagerSetPagesize() serves to set the value of
38487 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -37894,27 +38514,27 @@
38514 pPager->useJournal = (u8)useJournal;
38515 pPager->noReadlock = (noReadlock && readOnly) ?1:0;
38516 /* pPager->stmtOpen = 0; */
38517 /* pPager->stmtInUse = 0; */
38518 /* pPager->nRef = 0; */
 
38519 /* pPager->stmtSize = 0; */
38520 /* pPager->stmtJSize = 0; */
38521 /* pPager->nPage = 0; */
38522 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
38523 /* pPager->state = PAGER_UNLOCK; */
38524 #if 0
38525 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
38526 #endif
38527 /* pPager->errMask = 0; */
38528 pPager->tempFile = (u8)tempFile;
38529 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
38530 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
38531 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
38532 pPager->exclusiveMode = (u8)tempFile;
38533 pPager->changeCountDone = pPager->tempFile;
38534 pPager->memDb = (u8)memDb;
38535 pPager->readOnly = (u8)readOnly;
 
38536 assert( useJournal || pPager->tempFile );
38537 pPager->noSync = pPager->tempFile;
38538 pPager->fullSync = pPager->noSync ?0:1;
38539 pPager->sync_flags = SQLITE_SYNC_NORMAL;
38540 /* pPager->pFirst = 0; */
@@ -37975,24 +38595,24 @@
38595 sqlite3_vfs * const pVfs = pPager->pVfs;
38596 int rc = SQLITE_OK; /* Return code */
38597 int exists = 1; /* True if a journal file is present */
38598 int jrnlOpen = !!isOpen(pPager->jfd);
38599
 
38600 assert( pPager->useJournal );
38601 assert( isOpen(pPager->fd) );
38602 assert( pPager->eState==PAGER_OPEN );
38603
38604 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
38605 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
38606 ));
38607
38608 *pExists = 0;
38609 if( !jrnlOpen ){
38610 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
38611 }
38612 if( rc==SQLITE_OK && exists ){
38613 int locked = 0; /* True if some process holds a RESERVED lock */
38614
38615 /* Race condition here: Another process might have been holding the
38616 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
38617 ** call above, but then delete the journal and drop the lock before
38618 ** we get to the following sqlite3OsCheckReservedLock() call. If that
@@ -38000,25 +38620,25 @@
38620 ** in fact there is none. This results in a false-positive which will
38621 ** be dealt with by the playback routine. Ticket #3883.
38622 */
38623 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
38624 if( rc==SQLITE_OK && !locked ){
38625 Pgno nPage; /* Number of pages in database file */
38626
38627 /* Check the size of the database file. If it consists of 0 pages,
38628 ** then delete the journal file. See the header comment above for
38629 ** the reasoning here. Delete the obsolete journal file under
38630 ** a RESERVED lock to avoid race conditions and to avoid violating
38631 ** [H33020].
38632 */
38633 rc = pagerPagecount(pPager, &nPage);
38634 if( rc==SQLITE_OK ){
38635 if( nPage==0 ){
38636 sqlite3BeginBenignMalloc();
38637 if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
38638 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
38639 pagerUnlockDb(pPager, SHARED_LOCK);
38640 }
38641 sqlite3EndBenignMalloc();
38642 }else{
38643 /* The journal file exists and no other connection has a reserved
38644 ** or greater lock on the database file. Now check that there is
@@ -38067,11 +38687,11 @@
38687 ** has been successfully called. If a shared-lock is already held when
38688 ** this function is called, it is a no-op.
38689 **
38690 ** The following operations are also performed by this function.
38691 **
38692 ** 1) If the pager is currently in PAGER_OPEN state (no lock held
38693 ** on the database file), then an attempt is made to obtain a
38694 ** SHARED lock on the database file. Immediately after obtaining
38695 ** the SHARED lock, the file-system is checked for a hot-journal,
38696 ** which is played back if present. Following any hot-journal
38697 ** rollback, the contents of the cache are validated by checking
@@ -38082,70 +38702,51 @@
38702 ** no outstanding references to any pages, and is in the error state,
38703 ** then an attempt is made to clear the error state by discarding
38704 ** the contents of the page cache and rolling back any open journal
38705 ** file.
38706 **
38707 ** If everything is successful, SQLITE_OK is returned. If an IO error
38708 ** occurs while locking the database, checking for a hot-journal file or
38709 ** rolling back a journal file, the IO error code is returned.
 
 
 
 
 
38710 */
38711 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
38712 int rc = SQLITE_OK; /* Return code */
 
38713
38714 /* This routine is only called from b-tree and only when there are no
38715 ** outstanding pages. This implies that the pager state should either
38716 ** be OPEN or READER. READER is only possible if the pager is or was in
38717 ** exclusive access mode.
38718 */
38719 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
38720 assert( assert_pager_state(pPager) );
38721 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
38722 if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
38723
38724 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
38725 int bHotJournal = 1; /* True if there exists a hot journal-file */
38726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38727 assert( !MEMDB );
38728 assert( pPager->noReadlock==0 || pPager->readOnly );
38729
38730 if( pPager->noReadlock==0 ){
 
 
38731 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
38732 if( rc!=SQLITE_OK ){
38733 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
38734 goto failed;
38735 }
38736 }
 
38737
38738 /* If a journal file exists, and there is no RESERVED lock on the
38739 ** database file, then it either needs to be played back or deleted.
38740 */
38741 if( pPager->eLock<=SHARED_LOCK ){
38742 rc = hasHotJournal(pPager, &bHotJournal);
38743 }
38744 if( rc!=SQLITE_OK ){
38745 goto failed;
38746 }
38747 if( bHotJournal ){
 
38748 /* Get an EXCLUSIVE lock on the database file. At this point it is
38749 ** important that a RESERVED lock is not obtained on the way to the
38750 ** EXCLUSIVE lock. If it were, another process might open the
38751 ** database file, detect the RESERVED lock, and conclude that the
38752 ** database is safe to read while this process is still rolling the
@@ -38153,62 +38754,49 @@
38754 **
38755 ** Because the intermediate RESERVED lock is not requested, any
38756 ** other process attempting to access the database file will get to
38757 ** this point in the code and fail to obtain its own EXCLUSIVE lock
38758 ** on the database file.
38759 **
38760 ** Unless the pager is in locking_mode=exclusive mode, the lock is
38761 ** downgraded to SHARED_LOCK before this function returns.
38762 */
38763 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
38764 if( rc!=SQLITE_OK ){
38765 goto failed;
38766 }
38767
38768 /* If it is not already open and the file exists on disk, open the
38769 ** journal for read/write access. Write access is required because
38770 ** in exclusive-access mode the file descriptor will be kept open
38771 ** and possibly used for a transaction later on. Also, write-access
38772 ** is usually required to finalize the journal in journal_mode=persist
38773 ** mode (and also for journal_mode=truncate on some systems).
38774 **
38775 ** If the journal does not exist, it usually means that some
38776 ** other connection managed to get in and roll it back before
38777 ** this connection obtained the exclusive lock above. Or, it
38778 ** may mean that the pager was in the error-state when this
38779 ** function was called and the journal file does not exist.
38780 */
38781 if( !isOpen(pPager->jfd) ){
38782 sqlite3_vfs * const pVfs = pPager->pVfs;
38783 int bExists; /* True if journal file exists */
38784 rc = sqlite3OsAccess(
38785 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
38786 if( rc==SQLITE_OK && bExists ){
38787 int fout = 0;
38788 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
38789 assert( !pPager->tempFile );
38790 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
38791 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
38792 if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
38793 rc = SQLITE_CANTOPEN_BKPT;
38794 sqlite3OsClose(pPager->jfd);
38795 }
38796 }
38797 }
 
 
 
 
 
 
 
 
 
 
 
 
 
38798
38799 /* Playback and delete the journal. Drop the database write
38800 ** lock and reacquire the read lock. Purge the cache before
38801 ** playing back the hot-journal so that we don't end up with
38802 ** an inconsistent cache. Sync the hot journal before playing
@@ -38215,25 +38803,50 @@
38803 ** it back since the process that crashed and left the hot journal
38804 ** probably did not sync it and we are required to always sync
38805 ** the journal before playing it back.
38806 */
38807 if( isOpen(pPager->jfd) ){
38808 assert( rc==SQLITE_OK );
38809 rc = pagerSyncHotJournal(pPager);
38810 if( rc==SQLITE_OK ){
38811 rc = pager_playback(pPager, 1);
38812 pPager->eState = PAGER_OPEN;
38813 }
38814 }else if( !pPager->exclusiveMode ){
38815 pagerUnlockDb(pPager, SHARED_LOCK);
38816 }
38817
38818 if( rc!=SQLITE_OK ){
38819 /* This branch is taken if an error occurs while trying to open
38820 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
38821 ** pager_unlock() routine will be called before returning to unlock
38822 ** the file. If the unlock attempt fails, then Pager.eLock must be
38823 ** set to UNKNOWN_LOCK (see the comment above the #define for
38824 ** UNKNOWN_LOCK above for an explanation).
38825 **
38826 ** In order to get pager_unlock() to do this, set Pager.eState to
38827 ** PAGER_ERROR now. This is not actually counted as a transition
38828 ** to ERROR state in the state diagram at the top of this file,
38829 ** since we know that the same call to pager_unlock() will very
38830 ** shortly transition the pager object to the OPEN state. Calling
38831 ** assert_pager_state() would fail now, as it should not be possible
38832 ** to be in ERROR state when there are zero outstanding page
38833 ** references.
38834 */
38835 pager_error(pPager, rc);
38836 goto failed;
38837 }
38838
38839 assert( pPager->eState==PAGER_OPEN );
38840 assert( (pPager->eLock==SHARED_LOCK)
38841 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
38842 );
38843 }
38844
38845 if( !pPager->tempFile
38846 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
38847 ){
38848 /* The shared-lock has just been acquired on the database file
38849 ** and there are already pages in the cache (from a previous
38850 ** read or write transaction). Check to see if the database
38851 ** has been modified. If the database has changed, flush the
38852 ** cache.
@@ -38246,18 +38859,15 @@
38859 **
38860 ** There is a vanishingly small chance that a change will not be
38861 ** detected. The chance of an undetected change is so small that
38862 ** it can be neglected.
38863 */
38864 Pgno nPage = 0;
38865 char dbFileVers[sizeof(pPager->dbFileVers)];
 
38866
38867 rc = pagerPagecount(pPager, &nPage);
38868 if( rc ) goto failed;
 
 
38869
38870 if( nPage>0 ){
38871 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
38872 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
38873 if( rc!=SQLITE_OK ){
@@ -38269,22 +38879,34 @@
38879
38880 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
38881 pager_reset(pPager);
38882 }
38883 }
 
38884
38885 /* If there is a WAL file in the file-system, open this database in WAL
38886 ** mode. Otherwise, the following function call is a no-op.
38887 */
38888 rc = pagerOpenWalIfPresent(pPager);
38889 assert( pPager->pWal==0 || rc==SQLITE_OK );
38890 }
38891
38892 if( pagerUseWal(pPager) ){
38893 assert( rc==SQLITE_OK );
38894 rc = pagerBeginReadTransaction(pPager);
38895 }
38896
38897 if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
38898 rc = pagerPagecount(pPager, &pPager->dbSize);
38899 }
38900
38901 failed:
38902 if( rc!=SQLITE_OK ){
38903 assert( !MEMDB );
38904 pager_unlock(pPager);
38905 assert( pPager->eState==PAGER_OPEN );
38906 }else{
38907 pPager->eState = PAGER_READER;
38908 }
38909 return rc;
38910 }
38911
38912 /*
@@ -38294,13 +38916,11 @@
38916 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
38917 ** the rollback journal, the unlock is not performed and there is
38918 ** nothing to rollback, so this routine is a no-op.
38919 */
38920 static void pagerUnlockIfUnused(Pager *pPager){
38921 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
 
 
38922 pagerUnlockAndRollback(pPager);
38923 }
38924 }
38925
38926 /*
@@ -38360,20 +38980,20 @@
38980 int noContent /* Do not bother reading content from disk if true */
38981 ){
38982 int rc;
38983 PgHdr *pPg;
38984
38985 assert( pPager->eState>=PAGER_READER );
38986 assert( assert_pager_state(pPager) );
 
38987
38988 if( pgno==0 ){
38989 return SQLITE_CORRUPT_BKPT;
38990 }
38991
38992 /* If the pager is in the error state, return an error immediately.
38993 ** Otherwise, request the page from the PCache layer. */
38994 if( pPager->errCode!=SQLITE_OK ){
38995 rc = pPager->errCode;
38996 }else{
38997 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
38998 }
38999
@@ -38395,11 +39015,10 @@
39015 return SQLITE_OK;
39016
39017 }else{
39018 /* The pager cache has created a new page. Its content needs to
39019 ** be initialized. */
 
39020
39021 PAGER_INCR(pPager->nMiss);
39022 pPg = *ppPage;
39023 pPg->pPager = pPager;
39024
@@ -38408,16 +39027,11 @@
39027 if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
39028 rc = SQLITE_CORRUPT_BKPT;
39029 goto pager_acquire_err;
39030 }
39031
39032 if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
 
 
 
 
 
39033 if( pgno>pPager->mxPgno ){
39034 rc = SQLITE_FULL;
39035 goto pager_acquire_err;
39036 }
39037 if( noContent ){
@@ -38464,13 +39078,11 @@
39078 }
39079
39080 /*
39081 ** Acquire a page if it is already in the in-memory cache. Do
39082 ** not read the page from disk. Return a pointer to the page,
39083 ** or 0 if the page is not in cache.
 
 
39084 **
39085 ** See also sqlite3PagerGet(). The difference between this routine
39086 ** and sqlite3PagerGet() is that _get() will go to the disk and read
39087 ** in the page if the page is not already in cache. This routine
39088 ** returns NULL if the page is not in cache or if a disk I/O error
@@ -38479,11 +39091,11 @@
39091 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
39092 PgHdr *pPg = 0;
39093 assert( pPager!=0 );
39094 assert( pgno!=0 );
39095 assert( pPager->pPCache!=0 );
39096 assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
39097 sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
39098 return pPg;
39099 }
39100
39101 /*
@@ -38524,73 +39136,71 @@
39136 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
39137 ** an IO error code if opening or writing the journal file fails.
39138 */
39139 static int pager_open_journal(Pager *pPager){
39140 int rc = SQLITE_OK; /* Return code */
 
39141 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
39142
39143 assert( pPager->eState==PAGER_WRITER_LOCKED );
39144 assert( assert_pager_state(pPager) );
 
39145 assert( pPager->pInJournal==0 );
39146
39147 /* If already in the error state, this function is a no-op. But on
39148 ** the other hand, this routine is never called if we are already in
39149 ** an error state. */
39150 if( NEVER(pPager->errCode) ) return pPager->errCode;
39151
39152 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
39153 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
39154 if( pPager->pInJournal==0 ){
39155 return SQLITE_NOMEM;
39156 }
39157
39158 /* Open the journal file if it is not already open. */
39159 if( !isOpen(pPager->jfd) ){
39160 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
39161 sqlite3MemJournalOpen(pPager->jfd);
39162 }else{
39163 const int flags = /* VFS flags to open journal file */
39164 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
39165 (pPager->tempFile ?
39166 (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
39167 (SQLITE_OPEN_MAIN_JOURNAL)
39168 );
39169 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
39170 rc = sqlite3JournalOpen(
39171 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
39172 );
39173 #else
39174 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
39175 #endif
39176 }
39177 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
39178 }
39179
39180
39181 /* Write the first journal header to the journal file and open
39182 ** the sub-journal if necessary.
39183 */
39184 if( rc==SQLITE_OK ){
39185 /* TODO: Check if all of these are really required. */
39186 pPager->nRec = 0;
39187 pPager->journalOff = 0;
39188 pPager->setMaster = 0;
39189 pPager->journalHdr = 0;
39190 rc = writeJournalHdr(pPager);
39191 }
 
 
 
 
39192 }
39193
39194 if( rc!=SQLITE_OK ){
39195 sqlite3BitvecDestroy(pPager->pInJournal);
39196 pPager->pInJournal = 0;
39197 }else{
39198 assert( pPager->eState==PAGER_WRITER_LOCKED );
39199 pPager->eState = PAGER_WRITER_CACHEMOD;
39200 }
39201
39202 return rc;
39203 }
39204
39205 /*
39206 ** Begin a write-transaction on the specified pager object. If a
@@ -38599,18 +39209,10 @@
39209 ** If the exFlag argument is false, then acquire at least a RESERVED
39210 ** lock on the database file. If exFlag is true, then acquire at least
39211 ** an EXCLUSIVE lock. If such a lock is already held, no locking
39212 ** functions need be called.
39213 **
 
 
 
 
 
 
 
 
39214 ** If the subjInMemory argument is non-zero, then any sub-journal opened
39215 ** within this transaction will be opened as an in-memory file. This
39216 ** has no effect if the sub-journal is already opened (as it may be when
39217 ** running in exclusive mode) or if the transaction does not require a
39218 ** sub-journal. If the subjInMemory argument is zero, then any required
@@ -38617,24 +39219,24 @@
39219 ** sub-journal is implemented in-memory if pPager is an in-memory database,
39220 ** or using a temporary file otherwise.
39221 */
39222 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
39223 int rc = SQLITE_OK;
39224
39225 if( pPager->errCode ) return pPager->errCode;
39226 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
39227 pPager->subjInMemory = (u8)subjInMemory;
39228
39229 if( ALWAYS(pPager->eState==PAGER_READER) ){
39230 assert( pPager->pInJournal==0 );
 
39231
39232 if( pagerUseWal(pPager) ){
39233 /* If the pager is configured to use locking_mode=exclusive, and an
39234 ** exclusive lock on the database is not already held, obtain it now.
39235 */
39236 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
39237 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
 
39238 if( rc!=SQLITE_OK ){
39239 return rc;
39240 }
39241 sqlite3WalExclusiveMode(pPager->pWal, 1);
39242 }
@@ -38641,56 +39243,44 @@
39243
39244 /* Grab the write lock on the log file. If successful, upgrade to
39245 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
39246 ** The busy-handler is not invoked if another connection already
39247 ** holds the write-lock. If possible, the upper layer will call it.
 
 
 
 
 
 
39248 */
39249 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
 
 
 
 
 
 
 
 
39250 }else{
39251 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
39252 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
39253 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
39254 ** lock, but not when obtaining the RESERVED lock.
39255 */
39256 rc = pagerLockDb(pPager, RESERVED_LOCK);
39257 if( rc==SQLITE_OK && exFlag ){
39258 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
39259 }
39260 }
39261
39262 if( rc==SQLITE_OK ){
39263 /* Change to WRITER_LOCKED state.
39264 **
39265 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
39266 ** when it has an open transaction, but never to DBMOD or FINISHED.
39267 ** This is because in those states the code to roll back savepoint
39268 ** transactions may copy data from the sub-journal into the database
39269 ** file as well as into the page cache. Which would be incorrect in
39270 ** WAL mode.
39271 */
39272 pPager->eState = PAGER_WRITER_LOCKED;
39273 pPager->dbHintSize = pPager->dbSize;
39274 pPager->dbFileSize = pPager->dbSize;
39275 pPager->dbOrigSize = pPager->dbSize;
39276 pPager->journalOff = 0;
39277 }
39278
39279 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
39280 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
39281 assert( assert_pager_state(pPager) );
39282 }
39283
39284 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
39285 return rc;
39286 }
@@ -38705,110 +39295,98 @@
39295 static int pager_write(PgHdr *pPg){
39296 void *pData = pPg->pData;
39297 Pager *pPager = pPg->pPager;
39298 int rc = SQLITE_OK;
39299
39300 /* This routine is not called unless a write-transaction has already
39301 ** been started. The journal file may or may not be open at this point.
39302 ** It is never called in the ERROR state.
39303 */
39304 assert( pPager->eState==PAGER_WRITER_LOCKED
39305 || pPager->eState==PAGER_WRITER_CACHEMOD
39306 || pPager->eState==PAGER_WRITER_DBMOD
39307 );
39308 assert( assert_pager_state(pPager) );
39309
39310 /* If an error has been previously detected, report the same error
39311 ** again. This should not happen, but the check provides robustness. */
 
39312 if( NEVER(pPager->errCode) ) return pPager->errCode;
39313
39314 /* Higher-level routines never call this function if database is not
39315 ** writable. But check anyway, just for robustness. */
39316 if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
39317
 
 
39318 CHECK_PAGE(pPg);
39319
39320 /* Mark the page as dirty. If the page has already been written
39321 ** to the journal then we can return right away.
39322 */
39323 sqlite3PcacheMakeDirty(pPg);
39324 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
39325 assert( !pagerUseWal(pPager) );
39326 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
39327 }else{
39328
39329 /* If we get this far, it means that the page needs to be
39330 ** written to the transaction journal or the checkpoint journal
39331 ** or both.
39332 **
39333 ** Higher level routines have already obtained the necessary locks
39334 ** to begin the write-transaction, but the rollback journal might not
39335 ** yet be open. Open it now if this is the case.
39336 */
39337 if( pPager->eState==PAGER_WRITER_LOCKED ){
 
 
 
 
 
39338 rc = pager_open_journal(pPager);
39339 if( rc!=SQLITE_OK ) return rc;
39340 }
39341 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
39342 assert( assert_pager_state(pPager) );
39343
39344 /* The transaction journal now exists and we have a RESERVED or an
39345 ** EXCLUSIVE lock on the main database file. Write the current page to
39346 ** the transaction journal if it is not there already.
39347 */
39348 if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
39349 assert( pagerUseWal(pPager)==0 );
39350 if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
39351 u32 cksum;
39352 char *pData2;
39353 i64 iOff = pPager->journalOff;
39354
39355 /* We should never write to the journal file the page that
39356 ** contains the database locks. The following assert verifies
39357 ** that we do not. */
39358 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
39359
39360 assert( pPager->journalHdr<=pPager->journalOff );
39361 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
39362 cksum = pager_cksum(pPager, (u8*)pData2);
39363
39364 /* Even if an IO or diskfull error occurs while journalling the
39365 ** page in the block above, set the need-sync flag for the page.
39366 ** Otherwise, when the transaction is rolled back, the logic in
39367 ** playback_one_page() will think that the page needs to be restored
39368 ** in the database file. And if an IO error occurs while doing so,
39369 ** then corruption may follow.
39370 */
39371 pPg->flags |= PGHDR_NEED_SYNC;
39372
39373 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
39374 if( rc!=SQLITE_OK ) return rc;
39375 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
39376 if( rc!=SQLITE_OK ) return rc;
39377 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
39378 if( rc!=SQLITE_OK ) return rc;
39379
39380 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
39381 pPager->journalOff, pPager->pageSize));
39382 PAGER_INCR(sqlite3_pager_writej_count);
39383 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
39384 PAGERID(pPager), pPg->pgno,
39385 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
39386
39387 pPager->journalOff += 8 + pPager->pageSize;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39388 pPager->nRec++;
39389 assert( pPager->pInJournal!=0 );
39390 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
39391 testcase( rc==SQLITE_NOMEM );
39392 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
@@ -38816,13 +39394,12 @@
39394 if( rc!=SQLITE_OK ){
39395 assert( rc==SQLITE_NOMEM );
39396 return rc;
39397 }
39398 }else{
39399 if( pPager->eState!=PAGER_WRITER_DBMOD ){
39400 pPg->flags |= PGHDR_NEED_SYNC;
 
39401 }
39402 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
39403 PAGERID(pPager), pPg->pgno,
39404 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
39405 }
@@ -38838,11 +39415,10 @@
39415 }
39416 }
39417
39418 /* Update the database size and return.
39419 */
 
39420 if( pPager->dbSize<pPg->pgno ){
39421 pPager->dbSize = pPg->pgno;
39422 }
39423 return rc;
39424 }
@@ -38866,10 +39442,14 @@
39442
39443 PgHdr *pPg = pDbPage;
39444 Pager *pPager = pPg->pPager;
39445 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
39446
39447 assert( pPager->eState>=PAGER_WRITER_LOCKED );
39448 assert( pPager->eState!=PAGER_ERROR );
39449 assert( assert_pager_state(pPager) );
39450
39451 if( nPagePerSector>1 ){
39452 Pgno nPageCount; /* Total number of pages in database file */
39453 Pgno pg1; /* First page of the sector pPg is located on. */
39454 int nPage = 0; /* Number of pages starting at pg1 to journal */
39455 int ii; /* Loop counter */
@@ -38887,23 +39467,21 @@
39467 ** an integer power of 2. It sets variable pg1 to the identifier
39468 ** of the first page of the sector pPg is located on.
39469 */
39470 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
39471
39472 nPageCount = pPager->dbSize;
39473 if( pPg->pgno>nPageCount ){
39474 nPage = (pPg->pgno - pg1)+1;
39475 }else if( (pg1+nPagePerSector-1)>nPageCount ){
39476 nPage = nPageCount+1-pg1;
39477 }else{
39478 nPage = nPagePerSector;
39479 }
39480 assert(nPage>0);
39481 assert(pg1<=pPg->pgno);
39482 assert((pg1+nPage)>pPg->pgno);
 
 
39483
39484 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
39485 Pgno pg = pg1+ii;
39486 PgHdr *pPage;
39487 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38911,11 +39489,10 @@
39489 rc = sqlite3PagerGet(pPager, pg, &pPage);
39490 if( rc==SQLITE_OK ){
39491 rc = pager_write(pPage);
39492 if( pPage->flags&PGHDR_NEED_SYNC ){
39493 needSync = 1;
 
39494 }
39495 sqlite3PagerUnref(pPage);
39496 }
39497 }
39498 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
@@ -38931,19 +39508,18 @@
39508 ** writing to any of these nPage pages may damage the others, the
39509 ** journal file must contain sync()ed copies of all of them
39510 ** before any of them can be written out to the database file.
39511 */
39512 if( rc==SQLITE_OK && needSync ){
39513 assert( !MEMDB );
39514 for(ii=0; ii<nPage; ii++){
39515 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
39516 if( pPage ){
39517 pPage->flags |= PGHDR_NEED_SYNC;
39518 sqlite3PagerUnref(pPage);
39519 }
39520 }
 
39521 }
39522
39523 assert( pPager->doNotSyncSpill==1 );
39524 pPager->doNotSyncSpill--;
39525 }else{
@@ -39005,10 +39581,15 @@
39581 ** by writing an updated version of page 1 using a call to the
39582 ** sqlite3OsWrite() function.
39583 */
39584 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
39585 int rc = SQLITE_OK;
39586
39587 assert( pPager->eState==PAGER_WRITER_CACHEMOD
39588 || pPager->eState==PAGER_WRITER_DBMOD
39589 );
39590 assert( assert_pager_state(pPager) );
39591
39592 /* Declare and initialize constant integer 'isDirect'. If the
39593 ** atomic-write optimization is enabled in this build, then isDirect
39594 ** is initialized to the value passed as the isDirectMode parameter
39595 ** to this function. Otherwise, it is always set to zero.
@@ -39024,11 +39605,10 @@
39605 UNUSED_PARAMETER(isDirectMode);
39606 #else
39607 # define DIRECT_MODE isDirectMode
39608 #endif
39609
 
39610 if( !pPager->changeCountDone && pPager->dbSize>0 ){
39611 PgHdr *pPgHdr; /* Reference to page 1 */
39612 u32 change_counter; /* Initial value of change-counter field */
39613
39614 assert( !pPager->tempFile && isOpen(pPager->fd) );
@@ -39109,13 +39689,17 @@
39689 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
39690 ** returned.
39691 */
39692 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
39693 int rc = SQLITE_OK;
39694 assert( pPager->eState==PAGER_WRITER_CACHEMOD
39695 || pPager->eState==PAGER_WRITER_DBMOD
39696 || pPager->eState==PAGER_WRITER_LOCKED
39697 );
39698 assert( assert_pager_state(pPager) );
39699 if( 0==pagerUseWal(pPager) ){
39700 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
39701 }
39702 return rc;
39703 }
39704
39705 /*
@@ -39149,26 +39733,33 @@
39733 const char *zMaster, /* If not NULL, the master journal name */
39734 int noSync /* True to omit the xSync on the db file */
39735 ){
39736 int rc = SQLITE_OK; /* Return code */
39737
39738 assert( pPager->eState==PAGER_WRITER_LOCKED
39739 || pPager->eState==PAGER_WRITER_CACHEMOD
39740 || pPager->eState==PAGER_WRITER_DBMOD
39741 || pPager->eState==PAGER_ERROR
39742 );
39743 assert( assert_pager_state(pPager) );
39744
39745 /* If a prior error occurred, report that error again. */
39746 if( NEVER(pPager->errCode) ) return pPager->errCode;
39747
39748 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
39749 pPager->zFilename, zMaster, pPager->dbSize));
39750
39751 /* If no database changes have been made, return early. */
39752 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
39753
39754 if( MEMDB ){
39755 /* If this is an in-memory db, or no pages have been written to, or this
39756 ** function has already been called, it is mostly a no-op. However, any
39757 ** backup in progress needs to be restarted.
39758 */
39759 sqlite3BackupRestart(pPager->pBackup);
39760 }else{
39761 if( pagerUseWal(pPager) ){
39762 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
39763 if( pList ){
39764 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
39765 (pPager->fullSync ? pPager->sync_flags : 0)
@@ -39207,11 +39798,11 @@
39798 || pPager->journalMode==PAGER_JOURNALMODE_OFF
39799 || pPager->journalMode==PAGER_JOURNALMODE_WAL
39800 );
39801 if( !zMaster && isOpen(pPager->jfd)
39802 && pPager->journalOff==jrnlBufferSize(pPager)
39803 && pPager->dbSize>=pPager->dbOrigSize
39804 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
39805 ){
39806 /* Update the db file change counter via the direct-write method. The
39807 ** following call will modify the in-memory representation of page 1
39808 ** to include the updated change counter and then write page 1
@@ -39237,17 +39828,14 @@
39828 ** Before reading the pages with page numbers larger than the
39829 ** current value of Pager.dbSize, set dbSize back to the value
39830 ** that it took at the start of the transaction. Otherwise, the
39831 ** calls to sqlite3PagerGet() return zeroed pages instead of
39832 ** reading data from the database file.
 
 
 
39833 */
39834 #ifndef SQLITE_OMIT_AUTOVACUUM
39835 if( pPager->dbSize<pPager->dbOrigSize
39836 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
39837 ){
39838 Pgno i; /* Iterator variable */
39839 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
39840 const Pgno dbSize = pPager->dbSize; /* Database image size */
39841 pPager->dbSize = pPager->dbOrigSize;
@@ -39270,18 +39858,24 @@
39858 ** or if zMaster is NULL (no master journal), then this call is a no-op.
39859 */
39860 rc = writeMasterJournal(pPager, zMaster);
39861 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39862
39863 /* Sync the journal file and write all dirty pages to the database.
39864 ** If the atomic-update optimization is being used, this sync will not
39865 ** create the journal file or perform any real IO.
39866 **
39867 ** Because the change-counter page was just modified, unless the
39868 ** atomic-update optimization is used it is almost certain that the
39869 ** journal requires a sync here. However, in locking_mode=exclusive
39870 ** on a system under memory pressure it is just possible that this is
39871 ** not the case. In this case it is likely enough that the redundant
39872 ** xSync() call will be changed to a no-op by the OS anyhow.
39873 */
39874 rc = syncJournal(pPager, 0);
39875 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39876
 
39877 rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
39878 if( rc!=SQLITE_OK ){
39879 assert( rc!=SQLITE_IOERR_BLOCKED );
39880 goto commit_phase_one_exit;
39881 }
@@ -39290,11 +39884,11 @@
39884 /* If the file on disk is not the same size as the database image,
39885 ** then use pager_truncate to grow or shrink the file here.
39886 */
39887 if( pPager->dbSize!=pPager->dbFileSize ){
39888 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
39889 assert( pPager->eState==PAGER_WRITER_DBMOD );
39890 rc = pager_truncate(pPager, nNew);
39891 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
39892 }
39893
39894 /* Finally, sync the database file. */
@@ -39301,16 +39895,16 @@
39895 if( !pPager->noSync && !noSync ){
39896 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
39897 }
39898 IOTRACE(("DBSYNC %p\n", pPager))
39899 }
 
 
 
39900 }
39901
39902 commit_phase_one_exit:
39903 if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
39904 pPager->eState = PAGER_WRITER_FINISHED;
39905 }
39906 return rc;
39907 }
39908
39909
39910 /*
@@ -39334,16 +39928,15 @@
39928 /* This routine should not be called if a prior error has occurred.
39929 ** But if (due to a coding error elsewhere in the system) it does get
39930 ** called, just return the same error code without doing anything. */
39931 if( NEVER(pPager->errCode) ) return pPager->errCode;
39932
39933 assert( pPager->eState==PAGER_WRITER_LOCKED
39934 || pPager->eState==PAGER_WRITER_FINISHED
39935 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
39936 );
39937 assert( assert_pager_state(pPager) );
 
39938
39939 /* An optimization. If the database was not actually modified during
39940 ** this transaction, the pager is running in exclusive-mode and is
39941 ** using persistent journals, then this function is a no-op.
39942 **
@@ -39352,106 +39945,80 @@
39945 ** a hot-journal during hot-journal rollback, 0 changes will be made
39946 ** to the database file. So there is no need to zero the journal
39947 ** header. Since the pager is in exclusive mode, there is no need
39948 ** to drop any locks either.
39949 */
39950 if( pPager->eState==PAGER_WRITER_LOCKED
39951 && pPager->exclusiveMode
39952 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
39953 ){
39954 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
39955 pPager->eState = PAGER_READER;
39956 return SQLITE_OK;
39957 }
39958
39959 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
 
39960 rc = pager_end_transaction(pPager, pPager->setMaster);
39961 return pager_error(pPager, rc);
39962 }
39963
39964 /*
39965 ** If a write transaction is open, then all changes made within the
39966 ** transaction are reverted and the current write-transaction is closed.
39967 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
39968 ** state if an error occurs.
39969 **
39970 ** If the pager is already in PAGER_ERROR state when this function is called,
39971 ** it returns Pager.errCode immediately. No work is performed in this case.
39972 **
39973 ** Otherwise, in rollback mode, this function performs two functions:
39974 **
39975 ** 1) It rolls back the journal file, restoring all database file and
39976 ** in-memory cache pages to the state they were in when the transaction
39977 ** was opened, and
39978 **
39979 ** 2) It finalizes the journal file, so that it is not used for hot
39980 ** rollback at any point in the future.
39981 **
39982 ** Finalization of the journal file (task 2) is only performed if the
39983 ** rollback is successful.
39984 **
39985 ** In WAL mode, all cache-entries containing data modified within the
39986 ** current transaction are either expelled from the cache or reverted to
39987 ** their pre-transaction state by re-reading data from the database or
39988 ** WAL files. The WAL transaction is then closed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39989 */
39990 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
39991 int rc = SQLITE_OK; /* Return code */
39992 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
39993
39994 /* PagerRollback() is a no-op if called in READER or OPEN state. If
39995 ** the pager is already in the ERROR state, the rollback is not
39996 ** attempted here. Instead, the error code is returned to the caller.
39997 */
39998 assert( assert_pager_state(pPager) );
39999 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
40000 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
40001
40002 if( pagerUseWal(pPager) ){
40003 int rc2;
 
40004 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
40005 rc2 = pager_end_transaction(pPager, pPager->setMaster);
40006 if( rc==SQLITE_OK ) rc = rc2;
40007 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
40008 rc = pager_end_transaction(pPager, 0);
40009 }else{
40010 rc = pager_playback(pPager, 0);
40011 }
40012
40013 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
40014 assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
40015
40016 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
40017 ** cache. So call pager_error() on the way out to make any error persistent.
40018 */
40019 return pager_error(pPager, rc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40020 }
40021
40022 /*
40023 ** Return TRUE if the database file is opened read-only. Return FALSE
40024 ** if the database is (in theory) writable.
@@ -39493,12 +40060,12 @@
40060 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
40061 static int a[11];
40062 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
40063 a[1] = sqlite3PcachePagecount(pPager->pPCache);
40064 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
40065 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
40066 a[4] = pPager->eState;
40067 a[5] = pPager->errCode;
40068 a[6] = pPager->nHit;
40069 a[7] = pPager->nMiss;
40070 a[8] = 0; /* Used to be pPager->nOvfl */
40071 a[9] = pPager->nRead;
@@ -39525,18 +40092,17 @@
40092 ** returned. Otherwise, SQLITE_OK.
40093 */
40094 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
40095 int rc = SQLITE_OK; /* Return code */
40096 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
40097
40098 assert( pPager->eState>=PAGER_WRITER_LOCKED );
40099 assert( assert_pager_state(pPager) );
40100
40101 if( nSavepoint>nCurrent && pPager->useJournal ){
40102 int ii; /* Iterator variable */
40103 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
 
 
 
 
40104
40105 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
40106 ** if the allocation fails. Otherwise, zero the new portion in case a
40107 ** malloc failure occurs while populating it in the for(...) loop below.
40108 */
@@ -39549,18 +40115,18 @@
40115 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
40116 pPager->aSavepoint = aNew;
40117
40118 /* Populate the PagerSavepoint structures just allocated. */
40119 for(ii=nCurrent; ii<nSavepoint; ii++){
40120 aNew[ii].nOrig = pPager->dbSize;
40121 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
40122 aNew[ii].iOffset = pPager->journalOff;
40123 }else{
40124 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
40125 }
40126 aNew[ii].iSubRec = pPager->nSubRec;
40127 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
40128 if( !aNew[ii].pInSavepoint ){
40129 return SQLITE_NOMEM;
40130 }
40131 if( pagerUseWal(pPager) ){
40132 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
@@ -39603,16 +40169,16 @@
40169 ** This function may return SQLITE_NOMEM if a memory allocation fails,
40170 ** or an IO error code if an IO error occurs while rolling back a
40171 ** savepoint. If no errors occur, SQLITE_OK is returned.
40172 */
40173 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
40174 int rc = pPager->errCode; /* Return code */
40175
40176 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
40177 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
40178
40179 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
40180 int ii; /* Iterator variable */
40181 int nNew; /* Number of remaining savepoints after this op. */
40182
40183 /* Figure out how many savepoints will still be active after this
40184 ** operation. Store this value in nNew. Then free resources associated
@@ -39644,12 +40210,12 @@
40210 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
40211 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
40212 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
40213 assert(rc!=SQLITE_DONE);
40214 }
 
40215 }
40216
40217 return rc;
40218 }
40219
40220 /*
40221 ** Return the full pathname of the database file.
@@ -39743,10 +40309,14 @@
40309 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
40310 int rc; /* Return code */
40311 Pgno origPgno; /* The original page number */
40312
40313 assert( pPg->nRef>0 );
40314 assert( pPager->eState==PAGER_WRITER_CACHEMOD
40315 || pPager->eState==PAGER_WRITER_DBMOD
40316 );
40317 assert( assert_pager_state(pPager) );
40318
40319 /* In order to be able to rollback, an in-memory database must journal
40320 ** the page we are moving from.
40321 */
40322 if( MEMDB ){
@@ -39792,15 +40362,14 @@
40362 */
40363 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
40364 needSyncPgno = pPg->pgno;
40365 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
40366 assert( pPg->flags&PGHDR_DIRTY );
 
40367 }
40368
40369 /* If the cache contains a page with page-number pgno, remove it
40370 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
40371 ** page pgno before the 'move' operation, it needs to be retained
40372 ** for the page moved there.
40373 */
40374 pPg->flags &= ~PGHDR_NEED_SYNC;
40375 pPgOld = pager_lookup(pPager, pgno);
@@ -39808,67 +40377,59 @@
40377 if( pPgOld ){
40378 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
40379 if( MEMDB ){
40380 /* Do not discard pages from an in-memory database since we might
40381 ** need to rollback later. Just move the page out of the way. */
 
40382 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
40383 }else{
40384 sqlite3PcacheDrop(pPgOld);
40385 }
40386 }
40387
40388 origPgno = pPg->pgno;
40389 sqlite3PcacheMove(pPg, pgno);
40390 sqlite3PcacheMakeDirty(pPg);
40391
40392 /* For an in-memory database, make sure the original page continues
40393 ** to exist, in case the transaction needs to roll back. Use pPgOld
40394 ** as the original page since it has already been allocated.
40395 */
40396 if( MEMDB ){
40397 assert( pPgOld );
40398 sqlite3PcacheMove(pPgOld, origPgno);
40399 sqlite3PagerUnref(pPgOld);
40400 }
40401
40402 if( needSyncPgno ){
40403 /* If needSyncPgno is non-zero, then the journal file needs to be
40404 ** sync()ed before any data is written to database file page needSyncPgno.
40405 ** Currently, no such page exists in the page-cache and the
40406 ** "is journaled" bitvec flag has been set. This needs to be remedied by
40407 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
40408 ** flag.
40409 **
40410 ** If the attempt to load the page into the page-cache fails, (due
40411 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
40412 ** array. Otherwise, if the page is loaded and written again in
40413 ** this transaction, it may be written to the database file before
40414 ** it is synced into the journal file. This way, it may end up in
40415 ** the journal file twice, but that is not a problem.
 
 
 
40416 */
40417 PgHdr *pPgHdr;
 
40418 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
40419 if( rc!=SQLITE_OK ){
40420 if( needSyncPgno<=pPager->dbOrigSize ){
40421 assert( pPager->pTmpSpace!=0 );
40422 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
40423 }
40424 return rc;
40425 }
 
 
40426 pPgHdr->flags |= PGHDR_NEED_SYNC;
40427 sqlite3PcacheMakeDirty(pPgHdr);
40428 sqlite3PagerUnref(pPgHdr);
40429 }
40430
 
 
 
 
 
 
 
 
 
 
40431 return SQLITE_OK;
40432 }
40433 #endif
40434
40435 /*
@@ -39929,10 +40490,17 @@
40490 **
40491 ** The returned indicate the current (possibly updated) journal-mode.
40492 */
40493 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
40494 u8 eOld = pPager->journalMode; /* Prior journalmode */
40495
40496 #ifdef SQLITE_DEBUG
40497 /* The print_pager_state() routine is intended to be used by the debugger
40498 ** only. We invoke it once here to suppress a compiler warning. */
40499 print_pager_state(pPager);
40500 #endif
40501
40502
40503 /* The eMode parameter is always valid */
40504 assert( eMode==PAGER_JOURNALMODE_DELETE
40505 || eMode==PAGER_JOURNALMODE_TRUNCATE
40506 || eMode==PAGER_JOURNALMODE_PERSIST
@@ -39955,24 +40523,17 @@
40523 eMode = eOld;
40524 }
40525 }
40526
40527 if( eMode!=eOld ){
 
 
 
 
 
 
 
 
40528
40529 /* Change the journal mode. */
40530 assert( pPager->eState!=PAGER_ERROR );
40531 pPager->journalMode = (u8)eMode;
40532
40533 /* When transistioning from TRUNCATE or PERSIST to any other journal
40534 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
40535 ** delete the journal file.
40536 */
40537 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
40538 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
40539 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
@@ -39989,28 +40550,34 @@
40550 **
40551 ** Before deleting the journal file, obtain a RESERVED lock on the
40552 ** database file. This ensures that the journal file is not deleted
40553 ** while it is in use by some other client.
40554 */
40555 sqlite3OsClose(pPager->jfd);
40556 if( pPager->eLock>=RESERVED_LOCK ){
 
 
 
 
 
 
 
 
40557 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40558 }else{
40559 int rc = SQLITE_OK;
40560 int state = pPager->eState;
40561 assert( state==PAGER_OPEN || state==PAGER_READER );
40562 if( state==PAGER_OPEN ){
40563 rc = sqlite3PagerSharedLock(pPager);
40564 }
40565 if( pPager->eState==PAGER_READER ){
40566 assert( rc==SQLITE_OK );
40567 rc = pagerLockDb(pPager, RESERVED_LOCK);
40568 }
40569 if( rc==SQLITE_OK ){
40570 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
40571 }
40572 if( rc==SQLITE_OK && state==PAGER_READER ){
40573 pagerUnlockDb(pPager, SHARED_LOCK);
40574 }else if( state==PAGER_OPEN ){
40575 pager_unlock(pPager);
40576 }
40577 assert( state==pPager->eState );
40578 }
 
 
 
 
 
 
40579 }
40580 }
40581
40582 /* Return the new journal mode */
40583 return (int)pPager->journalMode;
@@ -40027,11 +40594,12 @@
40594 ** Return TRUE if the pager is in a state where it is OK to change the
40595 ** journalmode. Journalmode changes can only happen when the database
40596 ** is unmodified.
40597 */
40598 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
40599 assert( assert_pager_state(pPager) );
40600 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
40601 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
40602 return 1;
40603 }
40604
40605 /*
@@ -40092,39 +40660,46 @@
40660 **
40661 ** If the pager passed as the first argument is open on a real database
40662 ** file (not a temp file or an in-memory database), and the WAL file
40663 ** is not already open, make an attempt to open it now. If successful,
40664 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
40665 ** not support the xShmXXX() methods, return an error code. *pbOpen is
40666 ** not modified in either case.
40667 **
40668 ** If the pager is open on a temp-file (or in-memory database), or if
40669 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
40670 ** without doing anything.
40671 */
40672 SQLITE_PRIVATE int sqlite3PagerOpenWal(
40673 Pager *pPager, /* Pager object */
40674 int *pbOpen /* OUT: Set to true if call is a no-op */
40675 ){
40676 int rc = SQLITE_OK; /* Return code */
40677
40678 assert( assert_pager_state(pPager) );
40679 assert( pPager->eState==PAGER_OPEN || pbOpen );
40680 assert( pPager->eState==PAGER_READER || !pbOpen );
40681 assert( pbOpen==0 || *pbOpen==0 );
40682 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
40683
40684 if( !pPager->tempFile && !pPager->pWal ){
40685 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
40686
40687 /* Close any rollback journal previously open */
40688 sqlite3OsClose(pPager->jfd);
40689
40690 /* Open the connection to the log file. If this operation fails,
40691 ** (e.g. due to malloc() failure), unlock the database file and
40692 ** return an error code.
40693 */
40694 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal);
40695 if( rc==SQLITE_OK ){
40696 pPager->journalMode = PAGER_JOURNALMODE_WAL;
40697 pPager->eState = PAGER_OPEN;
40698 }
40699 }else{
40700 *pbOpen = 1;
40701 }
40702
40703 return rc;
40704 }
40705
@@ -40146,11 +40721,11 @@
40721 ** it may need to be checkpointed before the connection can switch to
40722 ** rollback mode. Open it now so this can happen.
40723 */
40724 if( !pPager->pWal ){
40725 int logexists = 0;
40726 rc = pagerLockDb(pPager, SHARED_LOCK);
40727 if( rc==SQLITE_OK ){
40728 rc = sqlite3OsAccess(
40729 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
40730 );
40731 }
@@ -40162,21 +40737,21 @@
40737
40738 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
40739 ** the database file, the log and log-summary files will be deleted.
40740 */
40741 if( rc==SQLITE_OK && pPager->pWal ){
40742 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
40743 if( rc==SQLITE_OK ){
40744 rc = sqlite3WalClose(pPager->pWal,
40745 (pPager->noSync ? 0 : pPager->sync_flags),
40746 pPager->pageSize, (u8*)pPager->pTmpSpace
40747 );
40748 pPager->pWal = 0;
40749 }else{
40750 /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
40751 ** that we did get back to SHARED. */
40752 pagerUnlockDb(pPager, SQLITE_LOCK_SHARED);
40753 }
40754 }
40755 return rc;
40756 }
40757
@@ -40492,18 +41067,22 @@
41067 /*
41068 ** The following object holds a copy of the wal-index header content.
41069 **
41070 ** The actual header in the wal-index consists of two copies of this
41071 ** object.
41072 **
41073 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
41074 ** Or it can be 1 to represent a 65536-byte page. The latter case was
41075 ** added in 3.7.1 when support for 64K pages was added.
41076 */
41077 struct WalIndexHdr {
41078 u32 iVersion; /* Wal-index version */
41079 u32 unused; /* Unused (padding) field */
41080 u32 iChange; /* Counter incremented each transaction */
41081 u8 isInit; /* 1 when initialized */
41082 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
41083 u16 szPage; /* Database page size in bytes. 1==64K */
41084 u32 mxFrame; /* Index of last valid frame in the WAL */
41085 u32 nPage; /* Size of database in pages */
41086 u32 aFrameCksum[2]; /* Checksum of last frame in log */
41087 u32 aSalt[2]; /* Two salt values copied from WAL header */
41088 u32 aCksum[2]; /* Checksum over all prior fields */
@@ -40610,11 +41189,11 @@
41189 sqlite3_file *pDbFd; /* File handle for the database file */
41190 sqlite3_file *pWalFd; /* File handle for WAL file */
41191 u32 iCallback; /* Value to pass to log callback (or 0) */
41192 int nWiData; /* Size of array apWiData */
41193 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
41194 u32 szPage; /* Database page size */
41195 i16 readLock; /* Which read lock is being held. -1 for none */
41196 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
41197 u8 writeLock; /* True if in a write transaction */
41198 u8 ckptLock; /* True if holding a checkpoint lock */
41199 u8 readOnly; /* True if the WAL file is open read-only */
@@ -41281,11 +41860,11 @@
41860 || szPage<512
41861 ){
41862 goto finished;
41863 }
41864 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
41865 pWal->szPage = szPage;
41866 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
41867 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
41868
41869 /* Verify that the WAL header checksum is correct */
41870 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
@@ -41331,11 +41910,13 @@
41910
41911 /* If nTruncate is non-zero, this is a commit record. */
41912 if( nTruncate ){
41913 pWal->hdr.mxFrame = iFrame;
41914 pWal->hdr.nPage = nTruncate;
41915 pWal->hdr.szPage = (szPage&0xff00) | (szPage>>16);
41916 testcase( szPage<=32768 );
41917 testcase( szPage>=65536 );
41918 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
41919 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
41920 }
41921 }
41922
@@ -41356,10 +41937,21 @@
41937 */
41938 pInfo = walCkptInfo(pWal);
41939 pInfo->nBackfill = 0;
41940 pInfo->aReadMark[0] = 0;
41941 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
41942
41943 /* If more than one frame was recovered from the log file, report an
41944 ** event via sqlite3_log(). This is to help with identifying performance
41945 ** problems caused by applications routinely shutting down without
41946 ** checkpointing the log file.
41947 */
41948 if( pWal->hdr.nPage ){
41949 sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
41950 pWal->hdr.nPage, pWal->zWalName
41951 );
41952 }
41953 }
41954
41955 recovery_error:
41956 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
41957 walUnlockExclusive(pWal, iLock, nLock);
@@ -41716,18 +42308,22 @@
42308 int sync_flags, /* Flags for OsSync() (or 0) */
42309 int nBuf, /* Size of zBuf in bytes */
42310 u8 *zBuf /* Temporary buffer to use */
42311 ){
42312 int rc; /* Return code */
42313 int szPage; /* Database page-size */
42314 WalIterator *pIter = 0; /* Wal iterator context */
42315 u32 iDbpage = 0; /* Next database page to write */
42316 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
42317 u32 mxSafeFrame; /* Max frame that can be backfilled */
42318 u32 mxPage; /* Max database page to write */
42319 int i; /* Loop counter */
42320 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
42321
42322 szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42323 testcase( szPage<=32768 );
42324 testcase( szPage>=65536 );
42325 if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
42326
42327 /* Allocate the iterator */
42328 rc = walIteratorInit(pWal, &pIter);
42329 if( rc!=SQLITE_OK ){
@@ -41734,11 +42330,11 @@
42330 return rc;
42331 }
42332 assert( pIter );
42333
42334 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
42335 if( szPage!=nBuf ){
42336 rc = SQLITE_CORRUPT_BKPT;
42337 goto walcheckpoint_out;
42338 }
42339
42340 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
@@ -41745,10 +42341,11 @@
42341 ** safe to write into the database. Frames beyond mxSafeFrame might
42342 ** overwrite database pages that are in use by active readers and thus
42343 ** cannot be backfilled from the WAL.
42344 */
42345 mxSafeFrame = pWal->hdr.mxFrame;
42346 mxPage = pWal->hdr.nPage;
42347 pInfo = walCkptInfo(pWal);
42348 for(i=1; i<WAL_NREADER; i++){
42349 u32 y = pInfo->aReadMark[i];
42350 if( mxSafeFrame>=y ){
42351 assert( y<=pWal->hdr.mxFrame );
@@ -41765,22 +42362,34 @@
42362 }
42363
42364 if( pInfo->nBackfill<mxSafeFrame
42365 && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
42366 ){
42367 i64 nSize; /* Current size of database file */
42368 u32 nBackfill = pInfo->nBackfill;
42369
42370 /* Sync the WAL to disk */
42371 if( sync_flags ){
42372 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
42373 }
42374
42375 /* If the database file may grow as a result of this checkpoint, hint
42376 ** about the eventual size of the db file to the VFS layer.
42377 */
42378 if( rc==SQLITE_OK ){
42379 i64 nReq = ((i64)mxPage * szPage);
42380 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
42381 if( rc==SQLITE_OK && nSize<nReq ){
42382 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
42383 }
42384 }
42385
42386 /* Iterate through the contents of the WAL, copying data to the db file. */
42387 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
42388 i64 iOffset;
42389 assert( walFramePgno(pWal, iFrame)==iDbpage );
42390 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
42391 iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
42392 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
42393 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
42394 if( rc!=SQLITE_OK ) break;
42395 iOffset = (iDbpage-1)*(i64)szPage;
@@ -41883,11 +42492,11 @@
42492 WalIndexHdr volatile *aHdr; /* Header in shared memory */
42493
42494 /* The first page of the wal-index must be mapped at this point. */
42495 assert( pWal->nWiData>0 && pWal->apWiData[0] );
42496
42497 /* Read the header. This might happen concurrently with a write to the
42498 ** same area of shared memory on a different CPU in a SMP,
42499 ** meaning it is possible that an inconsistent snapshot is read
42500 ** from the file. If this happens, return non-zero.
42501 **
42502 ** There are two copies of the header at the beginning of the wal-index.
@@ -41912,11 +42521,13 @@
42521 }
42522
42523 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
42524 *pChanged = 1;
42525 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
42526 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42527 testcase( pWal->szPage<=32768 );
42528 testcase( pWal->szPage>=65536 );
42529 }
42530
42531 /* The header was successfully read. Return zero. */
42532 return 0;
42533 }
@@ -42231,10 +42842,11 @@
42842 /*
42843 ** Finish with a read transaction. All this does is release the
42844 ** read-lock.
42845 */
42846 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
42847 sqlite3WalEndWriteTransaction(pWal);
42848 if( pWal->readLock>=0 ){
42849 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
42850 pWal->readLock = -1;
42851 }
42852 }
@@ -42341,11 +42953,17 @@
42953
42954 /* If iRead is non-zero, then it is the log frame number that contains the
42955 ** required page. Read and return data from the log file.
42956 */
42957 if( iRead ){
42958 int sz;
42959 i64 iOffset;
42960 sz = pWal->hdr.szPage;
42961 sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
42962 testcase( sz<=32768 );
42963 testcase( sz>=65536 );
42964 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
42965 *pInWal = 1;
42966 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
42967 return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
42968 }
42969
@@ -42353,15 +42971,17 @@
42971 return SQLITE_OK;
42972 }
42973
42974
42975 /*
42976 ** Return the size of the database in pages (or zero, if unknown).
42977 */
42978 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
42979 if( pWal && ALWAYS(pWal->readLock>=0) ){
42980 return pWal->hdr.nPage;
42981 }
42982 return 0;
42983 }
42984
42985
42986 /*
42987 ** This function starts a write transaction on the WAL.
@@ -42433,11 +43053,11 @@
43053 ** Otherwise, if the callback function does not return an error, this
43054 ** function returns SQLITE_OK.
43055 */
43056 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
43057 int rc = SQLITE_OK;
43058 if( ALWAYS(pWal->writeLock) ){
43059 Pgno iMax = pWal->hdr.mxFrame;
43060 Pgno iFrame;
43061
43062 /* Restore the clients cache of the wal-index header to the state it
43063 ** was in before the client began writing to the database.
@@ -42622,11 +43242,11 @@
43242 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
43243 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
43244 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
43245 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
43246
43247 pWal->szPage = szPage;
43248 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
43249 pWal->hdr.aFrameCksum[0] = aCksum[0];
43250 pWal->hdr.aFrameCksum[1] = aCksum[1];
43251
43252 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
@@ -42717,11 +43337,13 @@
43337 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
43338 }
43339
43340 if( rc==SQLITE_OK ){
43341 /* Update the private copy of the header. */
43342 pWal->hdr.szPage = (szPage&0xff00) | (szPage>>16);
43343 testcase( szPage<=32768 );
43344 testcase( szPage>=65536 );
43345 pWal->hdr.mxFrame = iFrame;
43346 if( isCommit ){
43347 pWal->hdr.iChange++;
43348 pWal->hdr.nPage = nTruncate;
43349 }
@@ -42929,11 +43551,11 @@
43551 **
43552 ** FORMAT DETAILS
43553 **
43554 ** The file is divided into pages. The first page is called page 1,
43555 ** the second is page 2, and so forth. A page number of zero indicates
43556 ** "no such page". The page size can be any power of 2 between 512 and 65536.
43557 ** Each page can be either a btree page, a freelist page, an overflow
43558 ** page, or a pointer-map page.
43559 **
43560 ** The first page is always a btree page. The first 100 bytes of the first
43561 ** page contain a special header (the "file header") that describes the file.
@@ -43295,18 +43917,18 @@
43917 u8 initiallyEmpty; /* Database is empty at start of transaction */
43918 #ifndef SQLITE_OMIT_AUTOVACUUM
43919 u8 autoVacuum; /* True if auto-vacuum is enabled */
43920 u8 incrVacuum; /* True if incr-vacuum is enabled */
43921 #endif
 
 
43922 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
43923 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
43924 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
43925 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
43926 u8 inTransaction; /* Transaction state */
43927 u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
43928 u32 pageSize; /* Total number of bytes on a page */
43929 u32 usableSize; /* Number of usable bytes on each page */
43930 int nTransaction; /* Number of open transactions (read + write) */
43931 u32 nPage; /* Number of pages in the database */
43932 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
43933 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
43934 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -43901,11 +44523,20 @@
44523 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
44524 #else
44525 # define TRACE(X)
44526 #endif
44527
44528 /*
44529 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
44530 ** But if the value is zero, make it 65536.
44531 **
44532 ** This routine is used to extract the "offset to cell content area" value
44533 ** from the header of a btree page. If the page size is 65536 and the page
44534 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
44535 ** This routine makes the necessary adjustment to 65536.
44536 */
44537 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
44538
44539 #ifndef SQLITE_OMIT_SHARED_CACHE
44540 /*
44541 ** A list of BtShared objects that are eligible for participation
44542 ** in shared cache. This variable has file scope during normal builds,
@@ -45023,21 +45654,21 @@
45654 assert( nByte < usableSize-8 );
45655
45656 nFrag = data[hdr+7];
45657 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
45658 gap = pPage->cellOffset + 2*pPage->nCell;
45659 top = get2byteNotZero(&data[hdr+5]);
45660 if( gap>top ) return SQLITE_CORRUPT_BKPT;
45661 testcase( gap+2==top );
45662 testcase( gap+1==top );
45663 testcase( gap==top );
45664
45665 if( nFrag>=60 ){
45666 /* Always defragment highly fragmented pages */
45667 rc = defragmentPage(pPage);
45668 if( rc ) return rc;
45669 top = get2byteNotZero(&data[hdr+5]);
45670 }else if( gap+2<=top ){
45671 /* Search the freelist looking for a free slot big enough to satisfy
45672 ** the request. The allocation is made from the first free slot in
45673 ** the list that is large enough to accomadate it.
45674 */
@@ -45075,11 +45706,11 @@
45706 */
45707 testcase( gap+2+nByte==top );
45708 if( gap+2+nByte>top ){
45709 rc = defragmentPage(pPage);
45710 if( rc ) return rc;
45711 top = get2byteNotZero(&data[hdr+5]);
45712 assert( gap+nByte<=top );
45713 }
45714
45715
45716 /* Allocate memory from the gap in between the cell pointer array
@@ -45241,28 +45872,28 @@
45872 if( !pPage->isInit ){
45873 u16 pc; /* Address of a freeblock within pPage->aData[] */
45874 u8 hdr; /* Offset to beginning of page header */
45875 u8 *data; /* Equal to pPage->aData */
45876 BtShared *pBt; /* The main btree structure */
45877 int usableSize; /* Amount of usable space on each page */
45878 int cellOffset; /* Offset from start of page to first cell pointer */
45879 int nFree; /* Number of unused bytes on the page */
45880 int top; /* First byte of the cell content area */
45881 int iCellFirst; /* First allowable cell or freeblock offset */
45882 int iCellLast; /* Last possible cell or freeblock offset */
45883
45884 pBt = pPage->pBt;
45885
45886 hdr = pPage->hdrOffset;
45887 data = pPage->aData;
45888 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
45889 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
45890 pPage->maskPage = (u16)(pBt->pageSize - 1);
45891 pPage->nOverflow = 0;
45892 usableSize = pBt->usableSize;
45893 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
45894 top = get2byteNotZero(&data[hdr+5]);
45895 pPage->nCell = get2byte(&data[hdr+3]);
45896 if( pPage->nCell>MX_CELL(pBt) ){
45897 /* To many cells for a single page. The page must be corrupt */
45898 return SQLITE_CORRUPT_BKPT;
45899 }
@@ -45362,12 +45993,12 @@
45993 pPage->nFree = pBt->usableSize - first;
45994 decodeFlags(pPage, flags);
45995 pPage->hdrOffset = hdr;
45996 pPage->cellOffset = first;
45997 pPage->nOverflow = 0;
45998 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
45999 pPage->maskPage = (u16)(pBt->pageSize - 1);
46000 pPage->nCell = 0;
46001 pPage->isInit = 1;
46002 }
46003
46004
@@ -45671,11 +46302,11 @@
46302 pBt->pPage1 = 0;
46303 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
46304 #ifdef SQLITE_SECURE_DELETE
46305 pBt->secureDelete = 1;
46306 #endif
46307 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
46308 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
46309 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
46310 pBt->pageSize = 0;
46311 #ifndef SQLITE_OMIT_AUTOVACUUM
46312 /* If the magic name ":memory:" will create an in-memory database, then
@@ -45985,11 +46616,11 @@
46616 assert( nReserve>=0 && nReserve<=255 );
46617 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
46618 ((pageSize-1)&pageSize)==0 ){
46619 assert( (pageSize & 7)==0 );
46620 assert( !pBt->pPage1 && !pBt->pCursor );
46621 pBt->pageSize = (u32)pageSize;
46622 freeTempSpace(pBt);
46623 }
46624 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
46625 pBt->usableSize = pBt->pageSize - (u16)nReserve;
46626 if( iFix ) pBt->pageSizeFixed = 1;
@@ -46120,19 +46751,17 @@
46751
46752 /* Do some checking to help insure the file we opened really is
46753 ** a valid database file.
46754 */
46755 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
46756 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
 
 
46757 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
46758 nPage = nPageFile;
46759 }
46760 if( nPage>0 ){
46761 u32 pageSize;
46762 u32 usableSize;
46763 u8 *page1 = pPage1->aData;
46764 rc = SQLITE_NOTADB;
46765 if( memcmp(page1, zMagicHeader, 16)!=0 ){
46766 goto page1_init_failed;
46767 }
@@ -46179,13 +46808,14 @@
46808 ** version 3.6.0, we require them to be fixed.
46809 */
46810 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
46811 goto page1_init_failed;
46812 }
46813 pageSize = (page1[16]<<8) | (page1[17]<<16);
46814 if( ((pageSize-1)&pageSize)!=0
46815 || pageSize>SQLITE_MAX_PAGE_SIZE
46816 || pageSize<=256
46817 ){
46818 goto page1_init_failed;
46819 }
46820 assert( (pageSize & 7)==0 );
46821 usableSize = pageSize - page1[20];
@@ -46195,12 +46825,12 @@
46825 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
46826 ** zero and return SQLITE_OK. The caller will call this function
46827 ** again with the correct page-size.
46828 */
46829 releasePage(pPage1);
46830 pBt->usableSize = usableSize;
46831 pBt->pageSize = pageSize;
46832 freeTempSpace(pBt);
46833 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
46834 pageSize-usableSize);
46835 return rc;
46836 }
@@ -46209,12 +46839,12 @@
46839 goto page1_init_failed;
46840 }
46841 if( usableSize<480 ){
46842 goto page1_init_failed;
46843 }
46844 pBt->pageSize = pageSize;
46845 pBt->usableSize = usableSize;
46846 #ifndef SQLITE_OMIT_AUTOVACUUM
46847 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
46848 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
46849 #endif
46850 }
@@ -46286,11 +46916,12 @@
46916 data = pP1->aData;
46917 rc = sqlite3PagerWrite(pP1->pDbPage);
46918 if( rc ) return rc;
46919 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
46920 assert( sizeof(zMagicHeader)==16 );
46921 data[16] = (pBt->pageSize>>8)&0xff;
46922 data[17] = (pBt->pageSize>>16)&0xff;
46923 data[18] = 1;
46924 data[19] = 1;
46925 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
46926 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
46927 data[21] = 64;
@@ -48297,13 +48928,13 @@
48928 c = +1;
48929 }
48930 pCur->validNKey = 1;
48931 pCur->info.nKey = nCellKey;
48932 }else{
48933 /* The maximum supported page-size is 65536 bytes. This means that
48934 ** the maximum number of record bytes stored on an index B-Tree
48935 ** page is less than 16384 bytes and may be stored as a 2-byte
48936 ** varint. This information is used to attempt to avoid parsing
48937 ** the entire cell by checking for the cases where the record is
48938 ** stored entirely within the b-tree page by inspecting the first
48939 ** 2 bytes of the cell.
48940 */
@@ -49193,11 +49824,11 @@
49824 **
49825 ** "sz" must be the number of bytes in the cell.
49826 */
49827 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
49828 int i; /* Loop counter */
49829 u32 pc; /* Offset to cell content of cell being deleted */
49830 u8 *data; /* pPage->aData */
49831 u8 *ptr; /* Used to move bytes around within data[] */
49832 int rc; /* The return code */
49833 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
49834
@@ -49211,11 +49842,11 @@
49842 ptr = &data[pPage->cellOffset + 2*idx];
49843 pc = get2byte(ptr);
49844 hdr = pPage->hdrOffset;
49845 testcase( pc==get2byte(&data[hdr+5]) );
49846 testcase( pc+sz==pPage->pBt->usableSize );
49847 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
49848 *pRC = SQLITE_CORRUPT_BKPT;
49849 return;
49850 }
49851 rc = freeSpace(pPage, pc, sz);
49852 if( rc ){
@@ -49268,11 +49899,11 @@
49899 int nSkip = (iChild ? 4 : 0);
49900
49901 if( *pRC ) return;
49902
49903 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
49904 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
49905 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
49906 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49907 /* The cell should normally be sized correctly. However, when moving a
49908 ** malformed cell from a leaf page to an interior page, if the cell size
49909 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
@@ -49348,16 +49979,16 @@
49979 const int hdr = pPage->hdrOffset; /* Offset of header on pPage */
49980 const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
49981
49982 assert( pPage->nOverflow==0 );
49983 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49984 assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921);
49985 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49986
49987 /* Check that the page has just been zeroed by zeroPage() */
49988 assert( pPage->nCell==0 );
49989 assert( get2byteNotZero(&data[hdr+5])==nUsable );
49990
49991 pCellptr = &data[pPage->cellOffset + nCell*2];
49992 cellbody = nUsable;
49993 for(i=nCell-1; i>=0; i--){
49994 pCellptr -= 2;
@@ -49419,11 +50050,12 @@
50050
50051 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50052 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
50053 assert( pPage->nOverflow==1 );
50054
50055 /* This error condition is now caught prior to reaching this function */
50056 if( NEVER(pPage->nCell<=0) ) return SQLITE_CORRUPT_BKPT;
50057
50058 /* Allocate a new page. This page will become the right-sibling of
50059 ** pPage. Make the parent page writable, so that the new divider cell
50060 ** may be inserted. If both these operations are successful, proceed.
50061 */
@@ -49748,11 +50380,11 @@
50380 ** In this case, temporarily copy the cell into the aOvflSpace[]
50381 ** buffer. It will be copied out again as soon as the aSpace[] buffer
50382 ** is allocated. */
50383 if( pBt->secureDelete ){
50384 int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
50385 if( (iOff+szNew[i])>(int)pBt->usableSize ){
50386 rc = SQLITE_CORRUPT_BKPT;
50387 memset(apOld, 0, (i+1)*sizeof(MemPage*));
50388 goto balance_cleanup;
50389 }else{
50390 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
@@ -51317,11 +51949,11 @@
51949 #ifndef SQLITE_OMIT_AUTOVACUUM
51950 if( pCheck->pBt->autoVacuum ){
51951 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
51952 }
51953 #endif
51954 if( n>(int)pCheck->pBt->usableSize/4-2 ){
51955 checkAppendMsg(pCheck, zContext,
51956 "freelist leaf count too big on page %d", iPage);
51957 N--;
51958 }else{
51959 for(i=0; i<n; i++){
@@ -51528,24 +52160,24 @@
52160 hdr = pPage->hdrOffset;
52161 hit = sqlite3PageMalloc( pBt->pageSize );
52162 if( hit==0 ){
52163 pCheck->mallocFailed = 1;
52164 }else{
52165 int contentOffset = get2byteNotZero(&data[hdr+5]);
52166 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
52167 memset(hit+contentOffset, 0, usableSize-contentOffset);
52168 memset(hit, 1, contentOffset);
52169 nCell = get2byte(&data[hdr+3]);
52170 cellStart = hdr + 12 - 4*pPage->leaf;
52171 for(i=0; i<nCell; i++){
52172 int pc = get2byte(&data[cellStart+i*2]);
52173 u32 size = 65536;
52174 int j;
52175 if( pc<=usableSize-4 ){
52176 size = cellSizePtr(pPage, &data[pc]);
52177 }
52178 if( (int)(pc+size-1)>=usableSize ){
52179 checkAppendMsg(pCheck, 0,
52180 "Corruption detected in cell %d on page %d",i,iPage);
52181 }else{
52182 for(j=pc+size-1; j>=pc; j--) hit[j]++;
52183 }
@@ -55769,12 +56401,21 @@
56401 mrc = p->rc & 0xff;
56402 assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */
56403 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
56404 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
56405 if( isSpecialError ){
56406 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
56407 ** no rollback is necessary. Otherwise, at least a savepoint
56408 ** transaction must be rolled back to restore the database to a
56409 ** consistent state.
56410 **
56411 ** Even if the statement is read-only, it is important to perform
56412 ** a statement or transaction rollback operation. If the error
56413 ** occured while writing to the journal, sub-journal or database
56414 ** file as part of an effort to free up cache space (see function
56415 ** pagerStress() in pager.c), the rollback is required to restore
56416 ** the pager to a consistent state.
56417 */
56418 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
56419 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
56420 eStatementOp = SAVEPOINT_ROLLBACK;
56421 }else{
@@ -63200,11 +63841,11 @@
63841 ** If P5 is non-zero then the key value is increased by an epsilon
63842 ** prior to the comparison. This make the opcode work like IdxGT except
63843 ** that if the key from register P3 is a prefix of the key in the cursor,
63844 ** the result is false whereas it would be true with IdxGT.
63845 */
63846 /* Opcode: IdxLT P1 P2 P3 P4 P5
63847 **
63848 ** The P4 register values beginning with P3 form an unpacked index
63849 ** key that omits the ROWID. Compare this key value against the index
63850 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
63851 **
@@ -67547,17 +68188,18 @@
68188 assert( z[0]=='?' );
68189 pExpr->iColumn = (ynVar)(++pParse->nVar);
68190 }else if( z[0]=='?' ){
68191 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
68192 ** use it as the variable number */
68193 i64 i;
68194 int bOk = sqlite3Atoi64(&z[1], &i);
68195 pExpr->iColumn = (ynVar)i;
68196 testcase( i==0 );
68197 testcase( i==1 );
68198 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
68199 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
68200 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
68201 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
68202 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
68203 }
68204 if( i>pParse->nVar ){
68205 pParse->nVar = i;
@@ -72092,10 +72734,11 @@
72734 }
72735 }
72736 sqlite3DbFree(db, pIdx->aSample);
72737 }
72738 #else
72739 UNUSED_PARAMETER(db);
72740 UNUSED_PARAMETER(pIdx);
72741 #endif
72742 }
72743
72744 /*
@@ -87738,11 +88381,11 @@
88381 ** be sent.
88382 **
88383 ** regReturn is the number of the register holding the subroutine
88384 ** return address.
88385 **
88386 ** If regPrev>0 then it is the first register in a vector that
88387 ** records the previous output. mem[regPrev] is a flag that is false
88388 ** if there has been no previous output. If regPrev>0 then code is
88389 ** generated to suppress duplicates. pKeyInfo is used for comparing
88390 ** keys.
88391 **
@@ -88435,16 +89078,17 @@
89078 ** (1) The subquery and the outer query do not both use aggregates.
89079 **
89080 ** (2) The subquery is not an aggregate or the outer query is not a join.
89081 **
89082 ** (3) The subquery is not the right operand of a left outer join
89083 ** (Originally ticket #306. Strengthened by ticket #3300)
89084 **
89085 ** (4) The subquery is not DISTINCT.
89086 **
89087 ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
89088 ** sub-queries that were excluded from this optimization. Restriction
89089 ** (4) has since been expanded to exclude all DISTINCT subqueries.
89090 **
89091 ** (6) The subquery does not use aggregates or the outer query is not
89092 ** DISTINCT.
89093 **
89094 ** (7) The subquery has a FROM clause.
@@ -88460,13 +89104,13 @@
89104 ** (11) The subquery and the outer query do not both have ORDER BY clauses.
89105 **
89106 ** (**) Not implemented. Subsumed into restriction (3). Was previously
89107 ** a separate restriction deriving from ticket #350.
89108 **
89109 ** (13) The subquery and outer query do not both use LIMIT.
89110 **
89111 ** (14) The subquery does not use OFFSET.
89112 **
89113 ** (15) The outer query is not part of a compound select or the
89114 ** subquery does not have a LIMIT clause.
89115 ** (See ticket #2339 and ticket [02a8e81d44]).
89116 **
@@ -88553,13 +89197,13 @@
89197 if( pSub->pOffset ) return 0; /* Restriction (14) */
89198 if( p->pRightmost && pSub->pLimit ){
89199 return 0; /* Restriction (15) */
89200 }
89201 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
89202 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */
89203 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
89204 return 0; /* Restrictions (8)(9) */
89205 }
89206 if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
89207 return 0; /* Restriction (6) */
89208 }
89209 if( p->pOrderBy && pSub->pOrderBy ){
@@ -93225,11 +93869,11 @@
93869 pParse->pNewTable->aCol = 0;
93870 }
93871 db->pVTab = 0;
93872 }else{
93873 sqlite3Error(db, SQLITE_ERROR, zErr);
93874 sqlite3DbFree(db, zErr);
93875 rc = SQLITE_ERROR;
93876 }
93877 pParse->declareVtab = 0;
93878
93879 if( pParse->pVdbe ){
@@ -96859,39 +97503,39 @@
97503 **
97504 ** This case is also used when there are no WHERE clause
97505 ** constraints but an index is selected anyway, in order
97506 ** to force the output order to conform to an ORDER BY.
97507 */
97508 static const u8 aStartOp[] = {
97509 0,
97510 0,
97511 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
97512 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
97513 OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */
97514 OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */
97515 OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */
97516 OP_SeekLe /* 7: (start_constraints && startEq && bRev) */
97517 };
97518 static const u8 aEndOp[] = {
97519 OP_Noop, /* 0: (!end_constraints) */
97520 OP_IdxGE, /* 1: (end_constraints && !bRev) */
97521 OP_IdxLT /* 2: (end_constraints && bRev) */
97522 };
97523 int nEq = pLevel->plan.nEq; /* Number of == or IN terms */
97524 int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
97525 int regBase; /* Base register holding constraint values */
97526 int r1; /* Temp register */
97527 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
97528 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
97529 int startEq; /* True if range start uses ==, >= or <= */
97530 int endEq; /* True if range end uses ==, >= or <= */
97531 int start_constraints; /* Start of range is constrained */
97532 int nConstraint; /* Number of constraint terms */
97533 Index *pIdx; /* The index we will be using */
97534 int iIdxCur; /* The VDBE cursor for the index */
97535 int nExtraReg = 0; /* Number of extra registers needed */
97536 int op; /* Instruction opcode */
97537 char *zStartAff; /* Affinity for start of range constraint */
97538 char *zEndAff; /* Affinity for end of range constraint */
97539
97540 pIdx = pLevel->plan.u.pIdx;
97541 iIdxCur = pLevel->iIdxCur;
@@ -108720,11 +109364,11 @@
109364 ** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following
109365 ** two forward declarations are for functions declared in these files
109366 ** used to retrieve the respective implementations.
109367 **
109368 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
109369 ** to by the argument to point to the "simple" tokenizer implementation.
109370 ** Function ...PorterTokenizerModule() sets *pModule to point to the
109371 ** porter tokenizer/stemmer implementation.
109372 */
109373 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
109374 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
@@ -108922,11 +109566,11 @@
109566 ** is defined to accept an argument of type char, and always returns 0 for
109567 ** any values that fall outside of the range of the unsigned char type (i.e.
109568 ** negative values).
109569 */
109570 static int fts3isspace(char c){
109571 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
109572 }
109573
109574 /*
109575 ** Extract the next token from buffer z (length n) using the tokenizer
109576 ** and other information (column names etc.) in pParse. Create an Fts3Expr
@@ -111309,10 +111953,13 @@
111953
111954
111955 static int simpleDelim(simple_tokenizer *t, unsigned char c){
111956 return c<0x80 && t->delim[c];
111957 }
111958 static int fts3_isalnum(int x){
111959 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
111960 }
111961
111962 /*
111963 ** Create a new tokenizer instance.
111964 */
111965 static int simpleCreate(
@@ -111343,11 +111990,11 @@
111990 }
111991 } else {
111992 /* Mark non-alphanumeric ASCII characters as delimiters */
111993 int i;
111994 for(i=1; i<0x80; i++){
111995 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
111996 }
111997 }
111998
111999 *ppTokenizer = &t->base;
112000 return SQLITE_OK;
@@ -111449,11 +112096,11 @@
112096 for(i=0; i<n; i++){
112097 /* TODO(shess) This needs expansion to handle UTF-8
112098 ** case-insensitivity.
112099 */
112100 unsigned char ch = p[iStartOffset+i];
112101 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
112102 }
112103 *ppToken = c->pToken;
112104 *pnBytes = n;
112105 *piStartOffset = iStartOffset;
112106 *piEndOffset = c->iOffset;
@@ -116355,15 +117002,14 @@
117002 ** least desirable):
117003 **
117004 ** idxNum idxStr Strategy
117005 ** ------------------------------------------------
117006 ** 1 Unused Direct lookup by rowid.
117007 ** 2 See below R-tree query or full-table scan.
 
117008 ** ------------------------------------------------
117009 **
117010 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
117011 ** 2 is used, idxStr is formatted to contain 2 bytes for each
117012 ** constraint used. The first two bytes of idxStr correspond to
117013 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
117014 ** (argvIndex==1) etc.
117015 **
117016
+23 -14
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -95,11 +95,11 @@
9595
** be held constant and Z will be incremented or else Y will be incremented
9696
** and Z will be reset to zero.
9797
**
9898
** Since version 3.6.18, SQLite source code has been stored in the
9999
** <a href="http://www.fossil-scm.org/">Fossil configuration management
100
-** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to
100
+** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
101101
** a string which identifies a particular check-in of SQLite
102102
** within its configuration management system. ^The SQLITE_SOURCE_ID
103103
** string contains the date and time of the check-in (UTC) and an SHA1
104104
** hash of the entire source tree.
105105
**
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.1"
111111
#define SQLITE_VERSION_NUMBER 3007001
112
-#define SQLITE_SOURCE_ID "2010-08-05 03:21:40 fbe70e1106bcc5086ceb9d8f39cc39baf3643092"
112
+#define SQLITE_SOURCE_ID "2010-08-17 19:49:14 acb171d4cfef2fec8833f761019f5c81f0d138a0"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -152,19 +152,19 @@
152152
** ^The sqlite3_compileoption_used() function returns 0 or 1
153153
** indicating whether the specified option was defined at
154154
** compile time. ^The SQLITE_ prefix may be omitted from the
155155
** option name passed to sqlite3_compileoption_used().
156156
**
157
-** ^The sqlite3_compileoption_get() function allows interating
157
+** ^The sqlite3_compileoption_get() function allows iterating
158158
** over the list of options that were defined at compile time by
159159
** returning the N-th compile time option string. ^If N is out of range,
160160
** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
161161
** prefix is omitted from any strings returned by
162162
** sqlite3_compileoption_get().
163163
**
164164
** ^Support for the diagnostic functions sqlite3_compileoption_used()
165
-** and sqlite3_compileoption_get() may be omitted by specifing the
165
+** and sqlite3_compileoption_get() may be omitted by specifying the
166166
** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
167167
**
168168
** See also: SQL functions [sqlite_compileoption_used()] and
169169
** [sqlite_compileoption_get()] and the [compile_options pragma].
170170
*/
@@ -266,11 +266,11 @@
266266
/*
267267
** CAPI3REF: Closing A Database Connection
268268
**
269269
** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
270270
** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
271
-** successfullly destroyed and all associated resources are deallocated.
271
+** successfully destroyed and all associated resources are deallocated.
272272
**
273273
** Applications must [sqlite3_finalize | finalize] all [prepared statements]
274274
** and [sqlite3_blob_close | close] all [BLOB handles] associated with
275275
** the [sqlite3] object prior to attempting to close the object. ^If
276276
** sqlite3_close() is called on a [database connection] that still has
@@ -693,16 +693,25 @@
693693
** layer a hint of how large the database file will grow to be during the
694694
** current transaction. This hint is not guaranteed to be accurate but it
695695
** is often close. The underlying VFS might choose to preallocate database
696696
** file space based on this hint in order to help writes to the database
697697
** file run faster.
698
+**
699
+** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
700
+** extends and truncates the database file in chunks of a size specified
701
+** by the user. The fourth argument to [sqlite3_file_control()] should
702
+** point to an integer (type int) containing the new chunk-size to use
703
+** for the nominated database. Allocating database file space in large
704
+** chunks (say 1MB at a time), may reduce file-system fragmentation and
705
+** improve performance on some systems.
698706
*/
699707
#define SQLITE_FCNTL_LOCKSTATE 1
700708
#define SQLITE_GET_LOCKPROXYFILE 2
701709
#define SQLITE_SET_LOCKPROXYFILE 3
702710
#define SQLITE_LAST_ERRNO 4
703711
#define SQLITE_FCNTL_SIZE_HINT 5
712
+#define SQLITE_FCNTL_CHUNK_SIZE 6
704713
705714
/*
706715
** CAPI3REF: Mutex Handle
707716
**
708717
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2661,11 +2670,11 @@
26612670
** <li> @VVV
26622671
** <li> $VVV
26632672
** </ul>
26642673
**
26652674
** In the templates above, NNN represents an integer literal,
2666
-** and VVV represents an alphanumeric identifer.)^ ^The values of these
2675
+** and VVV represents an alphanumeric identifier.)^ ^The values of these
26672676
** parameters (also called "host parameter names" or "SQL parameters")
26682677
** can be set using the sqlite3_bind_*() routines defined here.
26692678
**
26702679
** ^The first argument to the sqlite3_bind_*() routines is always
26712680
** a pointer to the [sqlite3_stmt] object returned from
@@ -3440,11 +3449,11 @@
34403449
SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
34413450
34423451
/*
34433452
** CAPI3REF: Obtain Aggregate Function Context
34443453
**
3445
-** Implementions of aggregate SQL functions use this
3454
+** Implementations of aggregate SQL functions use this
34463455
** routine to allocate memory for storing their state.
34473456
**
34483457
** ^The first time the sqlite3_aggregate_context(C,N) routine is called
34493458
** for a particular aggregate function, SQLite
34503459
** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -3712,11 +3721,11 @@
37123721
** the routine expects pointers to 16-bit word aligned strings
37133722
** of UTF-16 in the native byte order.
37143723
**
37153724
** A pointer to the user supplied routine must be passed as the fifth
37163725
** argument. ^If it is NULL, this is the same as deleting the collation
3717
-** sequence (so that SQLite cannot call it anymore).
3726
+** sequence (so that SQLite cannot call it any more).
37183727
** ^Each time the application supplied function is invoked, it is passed
37193728
** as its first parameter a copy of the void* passed as the fourth argument
37203729
** to sqlite3_create_collation() or sqlite3_create_collation16().
37213730
**
37223731
** ^The remaining arguments to the application-supplied routine are two strings,
@@ -4930,11 +4939,11 @@
49304939
** of passing a NULL pointer instead of a valid mutex handle are undefined
49314940
** (i.e. it is acceptable to provide an implementation that segfaults if
49324941
** it is passed a NULL pointer).
49334942
**
49344943
** The xMutexInit() method must be threadsafe. ^It must be harmless to
4935
-** invoke xMutexInit() mutiple times within the same process and without
4944
+** invoke xMutexInit() multiple times within the same process and without
49364945
** intervening calls to xMutexEnd(). Second and subsequent calls to
49374946
** xMutexInit() must be no-ops.
49384947
**
49394948
** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
49404949
** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5100,11 +5109,11 @@
51005109
51015110
/*
51025111
** CAPI3REF: SQLite Runtime Status
51035112
**
51045113
** ^This interface is used to retrieve runtime status information
5105
-** about the preformance of SQLite, and optionally to reset various
5114
+** about the performance of SQLite, and optionally to reset various
51065115
** highwater marks. ^The first argument is an integer code for
51075116
** the specific parameter to measure. ^(Recognized integer codes
51085117
** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
51095118
** ^The current value of the parameter is returned into *pCurrent.
51105119
** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5226,11 +5235,11 @@
52265235
** ^This interface is used to retrieve runtime status information
52275236
** about a single [database connection]. ^The first argument is the
52285237
** database connection object to be interrogated. ^The second argument
52295238
** is an integer constant, taken from the set of
52305239
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5231
-** determiness the parameter to interrogate. The set of
5240
+** determines the parameter to interrogate. The set of
52325241
** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
52335242
** to grow in future releases of SQLite.
52345243
**
52355244
** ^The current value of the requested parameter is written into *pCur
52365245
** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -5648,11 +5657,11 @@
56485657
**
56495658
** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
56505659
**
56515660
** ^Each call to sqlite3_backup_step() sets two values inside
56525661
** the [sqlite3_backup] object: the number of pages still to be backed
5653
-** up and the total number of pages in the source databae file.
5662
+** up and the total number of pages in the source database file.
56545663
** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
56555664
** retrieve these two values, respectively.
56565665
**
56575666
** ^The values returned by these functions are only updated by
56585667
** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -5744,11 +5753,11 @@
57445753
** ^(There may be at most one unlock-notify callback registered by a
57455754
** blocked connection. If sqlite3_unlock_notify() is called when the
57465755
** blocked connection already has a registered unlock-notify callback,
57475756
** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
57485757
** called with a NULL pointer as its second argument, then any existing
5749
-** unlock-notify callback is cancelled. ^The blocked connections
5758
+** unlock-notify callback is canceled. ^The blocked connections
57505759
** unlock-notify callback may also be canceled by closing the blocked
57515760
** connection using [sqlite3_close()].
57525761
**
57535762
** The unlock-notify callback is not reentrant. If an application invokes
57545763
** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -5826,11 +5835,11 @@
58265835
/*
58275836
** CAPI3REF: String Comparison
58285837
**
58295838
** ^The [sqlite3_strnicmp()] API allows applications and extensions to
58305839
** compare the contents of two buffers containing UTF-8 strings in a
5831
-** case-indendent fashion, using the same definition of case independence
5840
+** case-independent fashion, using the same definition of case independence
58325841
** that SQLite uses internally when comparing identifiers.
58335842
*/
58345843
SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
58355844
58365845
/*
58375846
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -95,11 +95,11 @@
95 ** be held constant and Z will be incremented or else Y will be incremented
96 ** and Z will be reset to zero.
97 **
98 ** Since version 3.6.18, SQLite source code has been stored in the
99 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
100 ** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to
101 ** a string which identifies a particular check-in of SQLite
102 ** within its configuration management system. ^The SQLITE_SOURCE_ID
103 ** string contains the date and time of the check-in (UTC) and an SHA1
104 ** hash of the entire source tree.
105 **
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.1"
111 #define SQLITE_VERSION_NUMBER 3007001
112 #define SQLITE_SOURCE_ID "2010-08-05 03:21:40 fbe70e1106bcc5086ceb9d8f39cc39baf3643092"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -152,19 +152,19 @@
152 ** ^The sqlite3_compileoption_used() function returns 0 or 1
153 ** indicating whether the specified option was defined at
154 ** compile time. ^The SQLITE_ prefix may be omitted from the
155 ** option name passed to sqlite3_compileoption_used().
156 **
157 ** ^The sqlite3_compileoption_get() function allows interating
158 ** over the list of options that were defined at compile time by
159 ** returning the N-th compile time option string. ^If N is out of range,
160 ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
161 ** prefix is omitted from any strings returned by
162 ** sqlite3_compileoption_get().
163 **
164 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
165 ** and sqlite3_compileoption_get() may be omitted by specifing the
166 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
167 **
168 ** See also: SQL functions [sqlite_compileoption_used()] and
169 ** [sqlite_compileoption_get()] and the [compile_options pragma].
170 */
@@ -266,11 +266,11 @@
266 /*
267 ** CAPI3REF: Closing A Database Connection
268 **
269 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
270 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
271 ** successfullly destroyed and all associated resources are deallocated.
272 **
273 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
274 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
275 ** the [sqlite3] object prior to attempting to close the object. ^If
276 ** sqlite3_close() is called on a [database connection] that still has
@@ -693,16 +693,25 @@
693 ** layer a hint of how large the database file will grow to be during the
694 ** current transaction. This hint is not guaranteed to be accurate but it
695 ** is often close. The underlying VFS might choose to preallocate database
696 ** file space based on this hint in order to help writes to the database
697 ** file run faster.
 
 
 
 
 
 
 
 
698 */
699 #define SQLITE_FCNTL_LOCKSTATE 1
700 #define SQLITE_GET_LOCKPROXYFILE 2
701 #define SQLITE_SET_LOCKPROXYFILE 3
702 #define SQLITE_LAST_ERRNO 4
703 #define SQLITE_FCNTL_SIZE_HINT 5
 
704
705 /*
706 ** CAPI3REF: Mutex Handle
707 **
708 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2661,11 +2670,11 @@
2661 ** <li> @VVV
2662 ** <li> $VVV
2663 ** </ul>
2664 **
2665 ** In the templates above, NNN represents an integer literal,
2666 ** and VVV represents an alphanumeric identifer.)^ ^The values of these
2667 ** parameters (also called "host parameter names" or "SQL parameters")
2668 ** can be set using the sqlite3_bind_*() routines defined here.
2669 **
2670 ** ^The first argument to the sqlite3_bind_*() routines is always
2671 ** a pointer to the [sqlite3_stmt] object returned from
@@ -3440,11 +3449,11 @@
3440 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
3441
3442 /*
3443 ** CAPI3REF: Obtain Aggregate Function Context
3444 **
3445 ** Implementions of aggregate SQL functions use this
3446 ** routine to allocate memory for storing their state.
3447 **
3448 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
3449 ** for a particular aggregate function, SQLite
3450 ** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -3712,11 +3721,11 @@
3712 ** the routine expects pointers to 16-bit word aligned strings
3713 ** of UTF-16 in the native byte order.
3714 **
3715 ** A pointer to the user supplied routine must be passed as the fifth
3716 ** argument. ^If it is NULL, this is the same as deleting the collation
3717 ** sequence (so that SQLite cannot call it anymore).
3718 ** ^Each time the application supplied function is invoked, it is passed
3719 ** as its first parameter a copy of the void* passed as the fourth argument
3720 ** to sqlite3_create_collation() or sqlite3_create_collation16().
3721 **
3722 ** ^The remaining arguments to the application-supplied routine are two strings,
@@ -4930,11 +4939,11 @@
4930 ** of passing a NULL pointer instead of a valid mutex handle are undefined
4931 ** (i.e. it is acceptable to provide an implementation that segfaults if
4932 ** it is passed a NULL pointer).
4933 **
4934 ** The xMutexInit() method must be threadsafe. ^It must be harmless to
4935 ** invoke xMutexInit() mutiple times within the same process and without
4936 ** intervening calls to xMutexEnd(). Second and subsequent calls to
4937 ** xMutexInit() must be no-ops.
4938 **
4939 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
4940 ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5100,11 +5109,11 @@
5100
5101 /*
5102 ** CAPI3REF: SQLite Runtime Status
5103 **
5104 ** ^This interface is used to retrieve runtime status information
5105 ** about the preformance of SQLite, and optionally to reset various
5106 ** highwater marks. ^The first argument is an integer code for
5107 ** the specific parameter to measure. ^(Recognized integer codes
5108 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
5109 ** ^The current value of the parameter is returned into *pCurrent.
5110 ** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5226,11 +5235,11 @@
5226 ** ^This interface is used to retrieve runtime status information
5227 ** about a single [database connection]. ^The first argument is the
5228 ** database connection object to be interrogated. ^The second argument
5229 ** is an integer constant, taken from the set of
5230 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5231 ** determiness the parameter to interrogate. The set of
5232 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
5233 ** to grow in future releases of SQLite.
5234 **
5235 ** ^The current value of the requested parameter is written into *pCur
5236 ** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -5648,11 +5657,11 @@
5648 **
5649 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
5650 **
5651 ** ^Each call to sqlite3_backup_step() sets two values inside
5652 ** the [sqlite3_backup] object: the number of pages still to be backed
5653 ** up and the total number of pages in the source databae file.
5654 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
5655 ** retrieve these two values, respectively.
5656 **
5657 ** ^The values returned by these functions are only updated by
5658 ** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -5744,11 +5753,11 @@
5744 ** ^(There may be at most one unlock-notify callback registered by a
5745 ** blocked connection. If sqlite3_unlock_notify() is called when the
5746 ** blocked connection already has a registered unlock-notify callback,
5747 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
5748 ** called with a NULL pointer as its second argument, then any existing
5749 ** unlock-notify callback is cancelled. ^The blocked connections
5750 ** unlock-notify callback may also be canceled by closing the blocked
5751 ** connection using [sqlite3_close()].
5752 **
5753 ** The unlock-notify callback is not reentrant. If an application invokes
5754 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -5826,11 +5835,11 @@
5826 /*
5827 ** CAPI3REF: String Comparison
5828 **
5829 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
5830 ** compare the contents of two buffers containing UTF-8 strings in a
5831 ** case-indendent fashion, using the same definition of case independence
5832 ** that SQLite uses internally when comparing identifiers.
5833 */
5834 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
5835
5836 /*
5837
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -95,11 +95,11 @@
95 ** be held constant and Z will be incremented or else Y will be incremented
96 ** and Z will be reset to zero.
97 **
98 ** Since version 3.6.18, SQLite source code has been stored in the
99 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
100 ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
101 ** a string which identifies a particular check-in of SQLite
102 ** within its configuration management system. ^The SQLITE_SOURCE_ID
103 ** string contains the date and time of the check-in (UTC) and an SHA1
104 ** hash of the entire source tree.
105 **
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.1"
111 #define SQLITE_VERSION_NUMBER 3007001
112 #define SQLITE_SOURCE_ID "2010-08-17 19:49:14 acb171d4cfef2fec8833f761019f5c81f0d138a0"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -152,19 +152,19 @@
152 ** ^The sqlite3_compileoption_used() function returns 0 or 1
153 ** indicating whether the specified option was defined at
154 ** compile time. ^The SQLITE_ prefix may be omitted from the
155 ** option name passed to sqlite3_compileoption_used().
156 **
157 ** ^The sqlite3_compileoption_get() function allows iterating
158 ** over the list of options that were defined at compile time by
159 ** returning the N-th compile time option string. ^If N is out of range,
160 ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
161 ** prefix is omitted from any strings returned by
162 ** sqlite3_compileoption_get().
163 **
164 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
165 ** and sqlite3_compileoption_get() may be omitted by specifying the
166 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
167 **
168 ** See also: SQL functions [sqlite_compileoption_used()] and
169 ** [sqlite_compileoption_get()] and the [compile_options pragma].
170 */
@@ -266,11 +266,11 @@
266 /*
267 ** CAPI3REF: Closing A Database Connection
268 **
269 ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
270 ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
271 ** successfully destroyed and all associated resources are deallocated.
272 **
273 ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
274 ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
275 ** the [sqlite3] object prior to attempting to close the object. ^If
276 ** sqlite3_close() is called on a [database connection] that still has
@@ -693,16 +693,25 @@
693 ** layer a hint of how large the database file will grow to be during the
694 ** current transaction. This hint is not guaranteed to be accurate but it
695 ** is often close. The underlying VFS might choose to preallocate database
696 ** file space based on this hint in order to help writes to the database
697 ** file run faster.
698 **
699 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
700 ** extends and truncates the database file in chunks of a size specified
701 ** by the user. The fourth argument to [sqlite3_file_control()] should
702 ** point to an integer (type int) containing the new chunk-size to use
703 ** for the nominated database. Allocating database file space in large
704 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
705 ** improve performance on some systems.
706 */
707 #define SQLITE_FCNTL_LOCKSTATE 1
708 #define SQLITE_GET_LOCKPROXYFILE 2
709 #define SQLITE_SET_LOCKPROXYFILE 3
710 #define SQLITE_LAST_ERRNO 4
711 #define SQLITE_FCNTL_SIZE_HINT 5
712 #define SQLITE_FCNTL_CHUNK_SIZE 6
713
714 /*
715 ** CAPI3REF: Mutex Handle
716 **
717 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2661,11 +2670,11 @@
2670 ** <li> @VVV
2671 ** <li> $VVV
2672 ** </ul>
2673 **
2674 ** In the templates above, NNN represents an integer literal,
2675 ** and VVV represents an alphanumeric identifier.)^ ^The values of these
2676 ** parameters (also called "host parameter names" or "SQL parameters")
2677 ** can be set using the sqlite3_bind_*() routines defined here.
2678 **
2679 ** ^The first argument to the sqlite3_bind_*() routines is always
2680 ** a pointer to the [sqlite3_stmt] object returned from
@@ -3440,11 +3449,11 @@
3449 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
3450
3451 /*
3452 ** CAPI3REF: Obtain Aggregate Function Context
3453 **
3454 ** Implementations of aggregate SQL functions use this
3455 ** routine to allocate memory for storing their state.
3456 **
3457 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
3458 ** for a particular aggregate function, SQLite
3459 ** allocates N of memory, zeroes out that memory, and returns a pointer
@@ -3712,11 +3721,11 @@
3721 ** the routine expects pointers to 16-bit word aligned strings
3722 ** of UTF-16 in the native byte order.
3723 **
3724 ** A pointer to the user supplied routine must be passed as the fifth
3725 ** argument. ^If it is NULL, this is the same as deleting the collation
3726 ** sequence (so that SQLite cannot call it any more).
3727 ** ^Each time the application supplied function is invoked, it is passed
3728 ** as its first parameter a copy of the void* passed as the fourth argument
3729 ** to sqlite3_create_collation() or sqlite3_create_collation16().
3730 **
3731 ** ^The remaining arguments to the application-supplied routine are two strings,
@@ -4930,11 +4939,11 @@
4939 ** of passing a NULL pointer instead of a valid mutex handle are undefined
4940 ** (i.e. it is acceptable to provide an implementation that segfaults if
4941 ** it is passed a NULL pointer).
4942 **
4943 ** The xMutexInit() method must be threadsafe. ^It must be harmless to
4944 ** invoke xMutexInit() multiple times within the same process and without
4945 ** intervening calls to xMutexEnd(). Second and subsequent calls to
4946 ** xMutexInit() must be no-ops.
4947 **
4948 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
4949 ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
@@ -5100,11 +5109,11 @@
5109
5110 /*
5111 ** CAPI3REF: SQLite Runtime Status
5112 **
5113 ** ^This interface is used to retrieve runtime status information
5114 ** about the performance of SQLite, and optionally to reset various
5115 ** highwater marks. ^The first argument is an integer code for
5116 ** the specific parameter to measure. ^(Recognized integer codes
5117 ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
5118 ** ^The current value of the parameter is returned into *pCurrent.
5119 ** ^The highest recorded value is returned in *pHighwater. ^If the
@@ -5226,11 +5235,11 @@
5235 ** ^This interface is used to retrieve runtime status information
5236 ** about a single [database connection]. ^The first argument is the
5237 ** database connection object to be interrogated. ^The second argument
5238 ** is an integer constant, taken from the set of
5239 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that
5240 ** determines the parameter to interrogate. The set of
5241 ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely
5242 ** to grow in future releases of SQLite.
5243 **
5244 ** ^The current value of the requested parameter is written into *pCur
5245 ** and the highest instantaneous value is written into *pHiwtr. ^If
@@ -5648,11 +5657,11 @@
5657 **
5658 ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
5659 **
5660 ** ^Each call to sqlite3_backup_step() sets two values inside
5661 ** the [sqlite3_backup] object: the number of pages still to be backed
5662 ** up and the total number of pages in the source database file.
5663 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
5664 ** retrieve these two values, respectively.
5665 **
5666 ** ^The values returned by these functions are only updated by
5667 ** sqlite3_backup_step(). ^If the source database is modified during a backup
@@ -5744,11 +5753,11 @@
5753 ** ^(There may be at most one unlock-notify callback registered by a
5754 ** blocked connection. If sqlite3_unlock_notify() is called when the
5755 ** blocked connection already has a registered unlock-notify callback,
5756 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
5757 ** called with a NULL pointer as its second argument, then any existing
5758 ** unlock-notify callback is canceled. ^The blocked connections
5759 ** unlock-notify callback may also be canceled by closing the blocked
5760 ** connection using [sqlite3_close()].
5761 **
5762 ** The unlock-notify callback is not reentrant. If an application invokes
5763 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
@@ -5826,11 +5835,11 @@
5835 /*
5836 ** CAPI3REF: String Comparison
5837 **
5838 ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
5839 ** compare the contents of two buffers containing UTF-8 strings in a
5840 ** case-independent fashion, using the same definition of case independence
5841 ** that SQLite uses internally when comparing identifiers.
5842 */
5843 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
5844
5845 /*
5846

Keyboard Shortcuts

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