Fossil SCM
Update the internal SQLite to a 3.7.1 release candidate.
Commit
07afdea6e2c3c976e68fea5ae146fb938942ee15
Parent
391ef2853b287ed…
2 files changed
+1852
-1206
+23
-14
+1852
-1206
| --- src/sqlite3.c | ||
| +++ src/sqlite3.c | ||
| @@ -213,23 +213,22 @@ | ||
| 213 | 213 | */ |
| 214 | 214 | #ifndef SQLITE_MAX_VARIABLE_NUMBER |
| 215 | 215 | # define SQLITE_MAX_VARIABLE_NUMBER 999 |
| 216 | 216 | #endif |
| 217 | 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. | |
| 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. | |
| 221 | 220 | ** |
| 222 | 221 | ** If this limit is changed, then the compiled library is technically |
| 223 | 222 | ** incompatible with an SQLite library compiled with a different limit. If |
| 224 | 223 | ** a process operating on a database with a page-size of 65536 bytes |
| 225 | 224 | ** crashes, then an instance of SQLite compiled with the default page-size |
| 226 | 225 | ** limit will not be able to rollback the aborted transaction. This could |
| 227 | 226 | ** lead to database corruption. |
| 228 | 227 | */ |
| 229 | 228 | #ifndef SQLITE_MAX_PAGE_SIZE |
| 230 | -# define SQLITE_MAX_PAGE_SIZE 32768 | |
| 229 | +# define SQLITE_MAX_PAGE_SIZE 65536 | |
| 231 | 230 | #endif |
| 232 | 231 | |
| 233 | 232 | |
| 234 | 233 | /* |
| 235 | 234 | ** The default size of a database page. |
| @@ -631,11 +630,11 @@ | ||
| 631 | 630 | ** be held constant and Z will be incremented or else Y will be incremented |
| 632 | 631 | ** and Z will be reset to zero. |
| 633 | 632 | ** |
| 634 | 633 | ** Since version 3.6.18, SQLite source code has been stored in the |
| 635 | 634 | ** <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 | |
| 637 | 636 | ** a string which identifies a particular check-in of SQLite |
| 638 | 637 | ** within its configuration management system. ^The SQLITE_SOURCE_ID |
| 639 | 638 | ** string contains the date and time of the check-in (UTC) and an SHA1 |
| 640 | 639 | ** hash of the entire source tree. |
| 641 | 640 | ** |
| @@ -643,11 +642,11 @@ | ||
| 643 | 642 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 644 | 643 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 645 | 644 | */ |
| 646 | 645 | #define SQLITE_VERSION "3.7.1" |
| 647 | 646 | #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" | |
| 649 | 648 | |
| 650 | 649 | /* |
| 651 | 650 | ** CAPI3REF: Run-Time Library Version Numbers |
| 652 | 651 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 653 | 652 | ** |
| @@ -688,19 +687,19 @@ | ||
| 688 | 687 | ** ^The sqlite3_compileoption_used() function returns 0 or 1 |
| 689 | 688 | ** indicating whether the specified option was defined at |
| 690 | 689 | ** compile time. ^The SQLITE_ prefix may be omitted from the |
| 691 | 690 | ** option name passed to sqlite3_compileoption_used(). |
| 692 | 691 | ** |
| 693 | -** ^The sqlite3_compileoption_get() function allows interating | |
| 692 | +** ^The sqlite3_compileoption_get() function allows iterating | |
| 694 | 693 | ** over the list of options that were defined at compile time by |
| 695 | 694 | ** returning the N-th compile time option string. ^If N is out of range, |
| 696 | 695 | ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ |
| 697 | 696 | ** prefix is omitted from any strings returned by |
| 698 | 697 | ** sqlite3_compileoption_get(). |
| 699 | 698 | ** |
| 700 | 699 | ** ^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 | |
| 702 | 701 | ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. |
| 703 | 702 | ** |
| 704 | 703 | ** See also: SQL functions [sqlite_compileoption_used()] and |
| 705 | 704 | ** [sqlite_compileoption_get()] and the [compile_options pragma]. |
| 706 | 705 | */ |
| @@ -802,11 +801,11 @@ | ||
| 802 | 801 | /* |
| 803 | 802 | ** CAPI3REF: Closing A Database Connection |
| 804 | 803 | ** |
| 805 | 804 | ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object. |
| 806 | 805 | ** ^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. | |
| 808 | 807 | ** |
| 809 | 808 | ** Applications must [sqlite3_finalize | finalize] all [prepared statements] |
| 810 | 809 | ** and [sqlite3_blob_close | close] all [BLOB handles] associated with |
| 811 | 810 | ** the [sqlite3] object prior to attempting to close the object. ^If |
| 812 | 811 | ** sqlite3_close() is called on a [database connection] that still has |
| @@ -1229,16 +1228,25 @@ | ||
| 1229 | 1228 | ** layer a hint of how large the database file will grow to be during the |
| 1230 | 1229 | ** current transaction. This hint is not guaranteed to be accurate but it |
| 1231 | 1230 | ** is often close. The underlying VFS might choose to preallocate database |
| 1232 | 1231 | ** file space based on this hint in order to help writes to the database |
| 1233 | 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. | |
| 1234 | 1241 | */ |
| 1235 | 1242 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1236 | 1243 | #define SQLITE_GET_LOCKPROXYFILE 2 |
| 1237 | 1244 | #define SQLITE_SET_LOCKPROXYFILE 3 |
| 1238 | 1245 | #define SQLITE_LAST_ERRNO 4 |
| 1239 | 1246 | #define SQLITE_FCNTL_SIZE_HINT 5 |
| 1247 | +#define SQLITE_FCNTL_CHUNK_SIZE 6 | |
| 1240 | 1248 | |
| 1241 | 1249 | /* |
| 1242 | 1250 | ** CAPI3REF: Mutex Handle |
| 1243 | 1251 | ** |
| 1244 | 1252 | ** The mutex module within SQLite defines [sqlite3_mutex] to be an |
| @@ -3197,11 +3205,11 @@ | ||
| 3197 | 3205 | ** <li> @VVV |
| 3198 | 3206 | ** <li> $VVV |
| 3199 | 3207 | ** </ul> |
| 3200 | 3208 | ** |
| 3201 | 3209 | ** 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 | |
| 3203 | 3211 | ** parameters (also called "host parameter names" or "SQL parameters") |
| 3204 | 3212 | ** can be set using the sqlite3_bind_*() routines defined here. |
| 3205 | 3213 | ** |
| 3206 | 3214 | ** ^The first argument to the sqlite3_bind_*() routines is always |
| 3207 | 3215 | ** a pointer to the [sqlite3_stmt] object returned from |
| @@ -3976,11 +3984,11 @@ | ||
| 3976 | 3984 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); |
| 3977 | 3985 | |
| 3978 | 3986 | /* |
| 3979 | 3987 | ** CAPI3REF: Obtain Aggregate Function Context |
| 3980 | 3988 | ** |
| 3981 | -** Implementions of aggregate SQL functions use this | |
| 3989 | +** Implementations of aggregate SQL functions use this | |
| 3982 | 3990 | ** routine to allocate memory for storing their state. |
| 3983 | 3991 | ** |
| 3984 | 3992 | ** ^The first time the sqlite3_aggregate_context(C,N) routine is called |
| 3985 | 3993 | ** for a particular aggregate function, SQLite |
| 3986 | 3994 | ** allocates N of memory, zeroes out that memory, and returns a pointer |
| @@ -4248,11 +4256,11 @@ | ||
| 4248 | 4256 | ** the routine expects pointers to 16-bit word aligned strings |
| 4249 | 4257 | ** of UTF-16 in the native byte order. |
| 4250 | 4258 | ** |
| 4251 | 4259 | ** A pointer to the user supplied routine must be passed as the fifth |
| 4252 | 4260 | ** 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). | |
| 4254 | 4262 | ** ^Each time the application supplied function is invoked, it is passed |
| 4255 | 4263 | ** as its first parameter a copy of the void* passed as the fourth argument |
| 4256 | 4264 | ** to sqlite3_create_collation() or sqlite3_create_collation16(). |
| 4257 | 4265 | ** |
| 4258 | 4266 | ** ^The remaining arguments to the application-supplied routine are two strings, |
| @@ -5466,11 +5474,11 @@ | ||
| 5466 | 5474 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 5467 | 5475 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 5468 | 5476 | ** it is passed a NULL pointer). |
| 5469 | 5477 | ** |
| 5470 | 5478 | ** 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 | |
| 5472 | 5480 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 5473 | 5481 | ** xMutexInit() must be no-ops. |
| 5474 | 5482 | ** |
| 5475 | 5483 | ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 5476 | 5484 | ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| @@ -5636,11 +5644,11 @@ | ||
| 5636 | 5644 | |
| 5637 | 5645 | /* |
| 5638 | 5646 | ** CAPI3REF: SQLite Runtime Status |
| 5639 | 5647 | ** |
| 5640 | 5648 | ** ^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 | |
| 5642 | 5650 | ** highwater marks. ^The first argument is an integer code for |
| 5643 | 5651 | ** the specific parameter to measure. ^(Recognized integer codes |
| 5644 | 5652 | ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^ |
| 5645 | 5653 | ** ^The current value of the parameter is returned into *pCurrent. |
| 5646 | 5654 | ** ^The highest recorded value is returned in *pHighwater. ^If the |
| @@ -5762,11 +5770,11 @@ | ||
| 5762 | 5770 | ** ^This interface is used to retrieve runtime status information |
| 5763 | 5771 | ** about a single [database connection]. ^The first argument is the |
| 5764 | 5772 | ** database connection object to be interrogated. ^The second argument |
| 5765 | 5773 | ** is an integer constant, taken from the set of |
| 5766 | 5774 | ** [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 | |
| 5768 | 5776 | ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely |
| 5769 | 5777 | ** to grow in future releases of SQLite. |
| 5770 | 5778 | ** |
| 5771 | 5779 | ** ^The current value of the requested parameter is written into *pCur |
| 5772 | 5780 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| @@ -6184,11 +6192,11 @@ | ||
| 6184 | 6192 | ** |
| 6185 | 6193 | ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b> |
| 6186 | 6194 | ** |
| 6187 | 6195 | ** ^Each call to sqlite3_backup_step() sets two values inside |
| 6188 | 6196 | ** 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. | |
| 6190 | 6198 | ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces |
| 6191 | 6199 | ** retrieve these two values, respectively. |
| 6192 | 6200 | ** |
| 6193 | 6201 | ** ^The values returned by these functions are only updated by |
| 6194 | 6202 | ** sqlite3_backup_step(). ^If the source database is modified during a backup |
| @@ -6280,11 +6288,11 @@ | ||
| 6280 | 6288 | ** ^(There may be at most one unlock-notify callback registered by a |
| 6281 | 6289 | ** blocked connection. If sqlite3_unlock_notify() is called when the |
| 6282 | 6290 | ** blocked connection already has a registered unlock-notify callback, |
| 6283 | 6291 | ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is |
| 6284 | 6292 | ** 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 | |
| 6286 | 6294 | ** unlock-notify callback may also be canceled by closing the blocked |
| 6287 | 6295 | ** connection using [sqlite3_close()]. |
| 6288 | 6296 | ** |
| 6289 | 6297 | ** The unlock-notify callback is not reentrant. If an application invokes |
| 6290 | 6298 | ** any sqlite3_xxx API functions from within an unlock-notify callback, a |
| @@ -6362,11 +6370,11 @@ | ||
| 6362 | 6370 | /* |
| 6363 | 6371 | ** CAPI3REF: String Comparison |
| 6364 | 6372 | ** |
| 6365 | 6373 | ** ^The [sqlite3_strnicmp()] API allows applications and extensions to |
| 6366 | 6374 | ** 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 | |
| 6368 | 6376 | ** that SQLite uses internally when comparing identifiers. |
| 6369 | 6377 | */ |
| 6370 | 6378 | SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); |
| 6371 | 6379 | |
| 6372 | 6380 | /* |
| @@ -7868,11 +7876,11 @@ | ||
| 7868 | 7876 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager); |
| 7869 | 7877 | SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); |
| 7870 | 7878 | |
| 7871 | 7879 | /* Functions used to configure a Pager object. */ |
| 7872 | 7880 | SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); |
| 7873 | -SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int); | |
| 7881 | +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int); | |
| 7874 | 7882 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); |
| 7875 | 7883 | SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); |
| 7876 | 7884 | SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int); |
| 7877 | 7885 | SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int); |
| 7878 | 7886 | SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int); |
| @@ -7895,11 +7903,11 @@ | ||
| 7895 | 7903 | SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*); |
| 7896 | 7904 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); |
| 7897 | 7905 | SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); |
| 7898 | 7906 | |
| 7899 | 7907 | /* Functions used to manage pager transactions and savepoints. */ |
| 7900 | -SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*); | |
| 7908 | +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*); | |
| 7901 | 7909 | SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int); |
| 7902 | 7910 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); |
| 7903 | 7911 | SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*); |
| 7904 | 7912 | SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager); |
| 7905 | 7913 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*); |
| @@ -9183,13 +9191,13 @@ | ||
| 9183 | 9191 | ** argument to sqlite3VdbeKeyCompare and is used to control the |
| 9184 | 9192 | ** comparison of the two index keys. |
| 9185 | 9193 | */ |
| 9186 | 9194 | struct KeyInfo { |
| 9187 | 9195 | 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 */ | |
| 9189 | 9197 | 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 */ | |
| 9191 | 9199 | CollSeq *aColl[1]; /* Collating sequence for each term of the key */ |
| 9192 | 9200 | }; |
| 9193 | 9201 | |
| 9194 | 9202 | /* |
| 9195 | 9203 | ** An instance of the following structure holds information about a |
| @@ -16624,10 +16632,11 @@ | ||
| 16624 | 16632 | #else |
| 16625 | 16633 | /* Use the built-in recursive mutexes if they are available. |
| 16626 | 16634 | */ |
| 16627 | 16635 | pthread_mutex_lock(&p->mutex); |
| 16628 | 16636 | #if SQLITE_MUTEX_NREF |
| 16637 | + assert( p->nRef>0 || p->owner==0 ); | |
| 16629 | 16638 | p->owner = pthread_self(); |
| 16630 | 16639 | p->nRef++; |
| 16631 | 16640 | #endif |
| 16632 | 16641 | #endif |
| 16633 | 16642 | |
| @@ -16696,10 +16705,11 @@ | ||
| 16696 | 16705 | */ |
| 16697 | 16706 | static void pthreadMutexLeave(sqlite3_mutex *p){ |
| 16698 | 16707 | assert( pthreadMutexHeld(p) ); |
| 16699 | 16708 | #if SQLITE_MUTEX_NREF |
| 16700 | 16709 | p->nRef--; |
| 16710 | + if( p->nRef==0 ) p->owner = 0; | |
| 16701 | 16711 | #endif |
| 16702 | 16712 | assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 16703 | 16713 | |
| 16704 | 16714 | #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 16705 | 16715 | if( p->nRef==0 ){ |
| @@ -16960,11 +16970,11 @@ | ||
| 16960 | 16970 | ** allocated mutex. SQLite is careful to deallocate every |
| 16961 | 16971 | ** mutex that it allocates. |
| 16962 | 16972 | */ |
| 16963 | 16973 | static void winMutexFree(sqlite3_mutex *p){ |
| 16964 | 16974 | assert( p ); |
| 16965 | - assert( p->nRef==0 ); | |
| 16975 | + assert( p->nRef==0 && p->owner==0 ); | |
| 16966 | 16976 | assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 16967 | 16977 | DeleteCriticalSection(&p->mutex); |
| 16968 | 16978 | sqlite3_free(p); |
| 16969 | 16979 | } |
| 16970 | 16980 | |
| @@ -16984,10 +16994,11 @@ | ||
| 16984 | 16994 | DWORD tid = GetCurrentThreadId(); |
| 16985 | 16995 | assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); |
| 16986 | 16996 | #endif |
| 16987 | 16997 | EnterCriticalSection(&p->mutex); |
| 16988 | 16998 | #ifdef SQLITE_DEBUG |
| 16999 | + assert( p->nRef>0 || p->owner==0 ); | |
| 16989 | 17000 | p->owner = tid; |
| 16990 | 17001 | p->nRef++; |
| 16991 | 17002 | if( p->trace ){ |
| 16992 | 17003 | printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); |
| 16993 | 17004 | } |
| @@ -17037,10 +17048,11 @@ | ||
| 17037 | 17048 | #ifndef NDEBUG |
| 17038 | 17049 | DWORD tid = GetCurrentThreadId(); |
| 17039 | 17050 | assert( p->nRef>0 ); |
| 17040 | 17051 | assert( p->owner==tid ); |
| 17041 | 17052 | p->nRef--; |
| 17053 | + if( p->nRef==0 ) p->owner = 0; | |
| 17042 | 17054 | assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 17043 | 17055 | #endif |
| 17044 | 17056 | LeaveCriticalSection(&p->mutex); |
| 17045 | 17057 | #ifdef SQLITE_DEBUG |
| 17046 | 17058 | if( p->trace ){ |
| @@ -22609,10 +22621,11 @@ | ||
| 22609 | 22621 | void *lockingContext; /* Locking style specific state */ |
| 22610 | 22622 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 22611 | 22623 | int fileFlags; /* Miscellanous flags */ |
| 22612 | 22624 | const char *zPath; /* Name of the file */ |
| 22613 | 22625 | unixShm *pShm; /* Shared memory segment information */ |
| 22626 | + int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ | |
| 22614 | 22627 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 22615 | 22628 | int openFlags; /* The flags specified at open() */ |
| 22616 | 22629 | #endif |
| 22617 | 22630 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
| 22618 | 22631 | unsigned fsFlags; /* cached details from statfs() */ |
| @@ -25367,19 +25380,21 @@ | ||
| 25367 | 25380 | offset += wrote; |
| 25368 | 25381 | pBuf = &((char*)pBuf)[wrote]; |
| 25369 | 25382 | } |
| 25370 | 25383 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 25371 | 25384 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 25385 | + | |
| 25372 | 25386 | if( amt>0 ){ |
| 25373 | 25387 | if( wrote<0 ){ |
| 25374 | 25388 | /* lastErrno set by seekAndWrite */ |
| 25375 | 25389 | return SQLITE_IOERR_WRITE; |
| 25376 | 25390 | }else{ |
| 25377 | 25391 | pFile->lastErrno = 0; /* not a system error */ |
| 25378 | 25392 | return SQLITE_FULL; |
| 25379 | 25393 | } |
| 25380 | 25394 | } |
| 25395 | + | |
| 25381 | 25396 | return SQLITE_OK; |
| 25382 | 25397 | } |
| 25383 | 25398 | |
| 25384 | 25399 | #ifdef SQLITE_TEST |
| 25385 | 25400 | /* |
| @@ -25577,16 +25592,27 @@ | ||
| 25577 | 25592 | |
| 25578 | 25593 | /* |
| 25579 | 25594 | ** Truncate an open file to a specified size |
| 25580 | 25595 | */ |
| 25581 | 25596 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 25597 | + unixFile *pFile = (unixFile *)id; | |
| 25582 | 25598 | int rc; |
| 25583 | - assert( id ); | |
| 25599 | + assert( pFile ); | |
| 25584 | 25600 | 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); | |
| 25586 | 25612 | if( rc ){ |
| 25587 | - ((unixFile*)id)->lastErrno = errno; | |
| 25613 | + pFile->lastErrno = errno; | |
| 25588 | 25614 | return SQLITE_IOERR_TRUNCATE; |
| 25589 | 25615 | }else{ |
| 25590 | 25616 | #ifndef NDEBUG |
| 25591 | 25617 | /* If we are doing a normal write to a database file (as opposed to |
| 25592 | 25618 | ** doing a hot-journal rollback or a write to some file other than a |
| @@ -25593,12 +25619,12 @@ | ||
| 25593 | 25619 | ** normal database file) and we truncate the file to zero length, |
| 25594 | 25620 | ** that effectively updates the change counter. This might happen |
| 25595 | 25621 | ** when restoring a database using the backup API from a zero-length |
| 25596 | 25622 | ** source. |
| 25597 | 25623 | */ |
| 25598 | - if( ((unixFile*)id)->inNormalWrite && nByte==0 ){ | |
| 25599 | - ((unixFile*)id)->transCntrChng = 1; | |
| 25624 | + if( pFile->inNormalWrite && nByte==0 ){ | |
| 25625 | + pFile->transCntrChng = 1; | |
| 25600 | 25626 | } |
| 25601 | 25627 | #endif |
| 25602 | 25628 | |
| 25603 | 25629 | return SQLITE_OK; |
| 25604 | 25630 | } |
| @@ -25637,10 +25663,58 @@ | ||
| 25637 | 25663 | ** proxying locking division. |
| 25638 | 25664 | */ |
| 25639 | 25665 | static int proxyFileControl(sqlite3_file*,int,void*); |
| 25640 | 25666 | #endif |
| 25641 | 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 | +} | |
| 25642 | 25716 | |
| 25643 | 25717 | /* |
| 25644 | 25718 | ** Information and control of an open file handle. |
| 25645 | 25719 | */ |
| 25646 | 25720 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| @@ -25650,18 +25724,17 @@ | ||
| 25650 | 25724 | return SQLITE_OK; |
| 25651 | 25725 | } |
| 25652 | 25726 | case SQLITE_LAST_ERRNO: { |
| 25653 | 25727 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 25654 | 25728 | return SQLITE_OK; |
| 25729 | + } | |
| 25730 | + case SQLITE_FCNTL_CHUNK_SIZE: { | |
| 25731 | + ((unixFile*)id)->szChunk = *(int *)pArg; | |
| 25732 | + return SQLITE_OK; | |
| 25655 | 25733 | } |
| 25656 | 25734 | 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); | |
| 25663 | 25736 | } |
| 25664 | 25737 | #ifndef NDEBUG |
| 25665 | 25738 | /* The pager calls this method to signal that it has done |
| 25666 | 25739 | ** a rollback and that the database is therefore unchanged and |
| 25667 | 25740 | ** it hence it is OK for the transaction change counter to be |
| @@ -28522,11 +28595,11 @@ | ||
| 28522 | 28595 | }else{ |
| 28523 | 28596 | if( pCtx->conchFile ){ |
| 28524 | 28597 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
| 28525 | 28598 | sqlite3_free(pCtx->conchFile); |
| 28526 | 28599 | } |
| 28527 | - sqlite3_free(pCtx->lockProxyPath); | |
| 28600 | + sqlite3DbFree(0, pCtx->lockProxyPath); | |
| 28528 | 28601 | sqlite3_free(pCtx->conchFilePath); |
| 28529 | 28602 | sqlite3_free(pCtx); |
| 28530 | 28603 | } |
| 28531 | 28604 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 28532 | 28605 | (rc==SQLITE_OK ? "ok" : "failed"))); |
| @@ -28713,13 +28786,13 @@ | ||
| 28713 | 28786 | } |
| 28714 | 28787 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 28715 | 28788 | if( rc ) return rc; |
| 28716 | 28789 | sqlite3_free(conchFile); |
| 28717 | 28790 | } |
| 28718 | - sqlite3_free(pCtx->lockProxyPath); | |
| 28791 | + sqlite3DbFree(0, pCtx->lockProxyPath); | |
| 28719 | 28792 | sqlite3_free(pCtx->conchFilePath); |
| 28720 | - sqlite3_free(pCtx->dbPath); | |
| 28793 | + sqlite3DbFree(0, pCtx->dbPath); | |
| 28721 | 28794 | /* restore the original locking context and pMethod then close it */ |
| 28722 | 28795 | pFile->lockingContext = pCtx->oldLockingContext; |
| 28723 | 28796 | pFile->pMethod = pCtx->pOldMethod; |
| 28724 | 28797 | sqlite3_free(pCtx); |
| 28725 | 28798 | return pFile->pMethod->xClose(id); |
| @@ -29161,10 +29234,11 @@ | ||
| 29161 | 29234 | short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
| 29162 | 29235 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 29163 | 29236 | DWORD sectorSize; /* Sector size of the device file is on */ |
| 29164 | 29237 | winShm *pShm; /* Instance of shared memory on this file */ |
| 29165 | 29238 | const char *zPath; /* Full pathname of this file */ |
| 29239 | + int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */ | |
| 29166 | 29240 | #if SQLITE_OS_WINCE |
| 29167 | 29241 | WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ |
| 29168 | 29242 | HANDLE hMutex; /* Mutex used to control access to shared lock */ |
| 29169 | 29243 | HANDLE hShared; /* Shared memory segment used for locking */ |
| 29170 | 29244 | winceLock local; /* Locks obtained by this instance of winFile */ |
| @@ -29671,10 +29745,46 @@ | ||
| 29671 | 29745 | |
| 29672 | 29746 | /***************************************************************************** |
| 29673 | 29747 | ** The next group of routines implement the I/O methods specified |
| 29674 | 29748 | ** by the sqlite3_io_methods object. |
| 29675 | 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 | +} | |
| 29676 | 29786 | |
| 29677 | 29787 | /* |
| 29678 | 29788 | ** Close a file. |
| 29679 | 29789 | ** |
| 29680 | 29790 | ** It is reported that an attempt to close a handle might sometimes |
| @@ -29714,17 +29824,10 @@ | ||
| 29714 | 29824 | OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed")); |
| 29715 | 29825 | OpenCounter(-1); |
| 29716 | 29826 | return rc ? SQLITE_OK : SQLITE_IOERR; |
| 29717 | 29827 | } |
| 29718 | 29828 | |
| 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 | 29829 | /* |
| 29727 | 29830 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 29728 | 29831 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 29729 | 29832 | ** wrong. |
| 29730 | 29833 | */ |
| @@ -29732,112 +29835,108 @@ | ||
| 29732 | 29835 | sqlite3_file *id, /* File to read from */ |
| 29733 | 29836 | void *pBuf, /* Write content into this buffer */ |
| 29734 | 29837 | int amt, /* Number of bytes to read */ |
| 29735 | 29838 | sqlite3_int64 offset /* Begin reading at this offset */ |
| 29736 | 29839 | ){ |
| 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 */ | |
| 29743 | 29842 | |
| 29744 | 29843 | assert( id!=0 ); |
| 29745 | 29844 | SimulateIOError(return SQLITE_IOERR_READ); |
| 29746 | 29845 | 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) ){ | |
| 29750 | 29848 | return SQLITE_FULL; |
| 29751 | 29849 | } |
| 29752 | - if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){ | |
| 29850 | + if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ | |
| 29753 | 29851 | pFile->lastErrno = GetLastError(); |
| 29754 | 29852 | return SQLITE_IOERR_READ; |
| 29755 | 29853 | } |
| 29756 | - if( got==(DWORD)amt ){ | |
| 29757 | - return SQLITE_OK; | |
| 29758 | - }else{ | |
| 29854 | + if( nRead<(DWORD)amt ){ | |
| 29759 | 29855 | /* 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); | |
| 29761 | 29857 | return SQLITE_IOERR_SHORT_READ; |
| 29762 | 29858 | } |
| 29859 | + | |
| 29860 | + return SQLITE_OK; | |
| 29763 | 29861 | } |
| 29764 | 29862 | |
| 29765 | 29863 | /* |
| 29766 | 29864 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 29767 | 29865 | ** or some other error code on failure. |
| 29768 | 29866 | */ |
| 29769 | 29867 | 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 */ | |
| 29774 | 29872 | ){ |
| 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 ); | |
| 29783 | 29878 | SimulateIOError(return SQLITE_IOERR_WRITE); |
| 29784 | 29879 | 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; | |
| 29811 | 29904 | } |
| 29812 | 29905 | return SQLITE_OK; |
| 29813 | 29906 | } |
| 29814 | 29907 | |
| 29815 | 29908 | /* |
| 29816 | 29909 | ** Truncate an open file to a specified size |
| 29817 | 29910 | */ |
| 29818 | 29911 | 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 | + | |
| 29827 | 29917 | OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte)); |
| 29828 | 29918 | 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) ){ | |
| 29832 | 29931 | 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) ){ | |
| 29835 | 29933 | pFile->lastErrno = GetLastError(); |
| 29836 | 29934 | rc = SQLITE_IOERR_TRUNCATE; |
| 29837 | 29935 | } |
| 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")); | |
| 29839 | 29938 | return rc; |
| 29840 | 29939 | } |
| 29841 | 29940 | |
| 29842 | 29941 | #ifdef SQLITE_TEST |
| 29843 | 29942 | /* |
| @@ -30197,10 +30296,14 @@ | ||
| 30197 | 30296 | return SQLITE_OK; |
| 30198 | 30297 | } |
| 30199 | 30298 | case SQLITE_LAST_ERRNO: { |
| 30200 | 30299 | *(int*)pArg = (int)((winFile*)id)->lastErrno; |
| 30201 | 30300 | return SQLITE_OK; |
| 30301 | + } | |
| 30302 | + case SQLITE_FCNTL_CHUNK_SIZE: { | |
| 30303 | + ((winFile*)id)->szChunk = *(int *)pArg; | |
| 30304 | + return SQLITE_OK; | |
| 30202 | 30305 | } |
| 30203 | 30306 | case SQLITE_FCNTL_SIZE_HINT: { |
| 30204 | 30307 | sqlite3_int64 sz = *(sqlite3_int64*)pArg; |
| 30205 | 30308 | SimulateIOErrorBenign(1); |
| 30206 | 30309 | winTruncate(id, sz); |
| @@ -31749,11 +31852,11 @@ | ||
| 31749 | 31852 | ** start of a transaction, and is thus usually less than a few thousand, |
| 31750 | 31853 | ** but can be as large as 2 billion for a really big database. |
| 31751 | 31854 | */ |
| 31752 | 31855 | |
| 31753 | 31856 | /* 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 | |
| 31755 | 31858 | |
| 31756 | 31859 | /* Round the union size down to the nearest pointer boundary, since that's how |
| 31757 | 31860 | ** it will be aligned within the Bitvec struct. */ |
| 31758 | 31861 | #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) |
| 31759 | 31862 | |
| @@ -33964,11 +34067,11 @@ | ||
| 33964 | 34067 | # define sqlite3WalOpen(x,y,z) 0 |
| 33965 | 34068 | # define sqlite3WalClose(w,x,y,z) 0 |
| 33966 | 34069 | # define sqlite3WalBeginReadTransaction(y,z) 0 |
| 33967 | 34070 | # define sqlite3WalEndReadTransaction(z) |
| 33968 | 34071 | # define sqlite3WalRead(v,w,x,y,z) 0 |
| 33969 | -# define sqlite3WalDbsize(y,z) | |
| 34072 | +# define sqlite3WalDbsize(y) 0 | |
| 33970 | 34073 | # define sqlite3WalBeginWriteTransaction(y) 0 |
| 33971 | 34074 | # define sqlite3WalEndWriteTransaction(x) 0 |
| 33972 | 34075 | # define sqlite3WalUndo(x,y,z) 0 |
| 33973 | 34076 | # define sqlite3WalSavepoint(y,z) |
| 33974 | 34077 | # define sqlite3WalSavepointUndo(y,z) 0 |
| @@ -34000,13 +34103,12 @@ | ||
| 34000 | 34103 | SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal); |
| 34001 | 34104 | |
| 34002 | 34105 | /* Read a page from the write-ahead log, if it is present. */ |
| 34003 | 34106 | SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut); |
| 34004 | 34107 | |
| 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); | |
| 34008 | 34110 | |
| 34009 | 34111 | /* Obtain or release the WRITER lock. */ |
| 34010 | 34112 | SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal); |
| 34011 | 34113 | SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal); |
| 34012 | 34114 | |
| @@ -34048,12 +34150,16 @@ | ||
| 34048 | 34150 | #endif /* _WAL_H_ */ |
| 34049 | 34151 | |
| 34050 | 34152 | /************** End of wal.h *************************************************/ |
| 34051 | 34153 | /************** Continuing where we left off in pager.c **********************/ |
| 34052 | 34154 | |
| 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. | |
| 34055 | 34161 | ** |
| 34056 | 34162 | ** Within this comment block, a page is deemed to have been synced |
| 34057 | 34163 | ** automatically as soon as it is written when PRAGMA synchronous=OFF. |
| 34058 | 34164 | ** Otherwise, the page is not synced until the xSync method of the VFS |
| 34059 | 34165 | ** is called successfully on the file containing the page. |
| @@ -34083,11 +34189,11 @@ | ||
| 34083 | 34189 | ** both the content in the database when the rollback journal was written |
| 34084 | 34190 | ** and the content in the database at the beginning of the current |
| 34085 | 34191 | ** transaction. |
| 34086 | 34192 | ** |
| 34087 | 34193 | ** (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. | |
| 34089 | 34195 | ** |
| 34090 | 34196 | ** (4) Reads from the database file are either aligned on a page boundary and |
| 34091 | 34197 | ** an integer multiple of the page size in length or are taken from the |
| 34092 | 34198 | ** first 100 bytes of the database file. |
| 34093 | 34199 | ** |
| @@ -34114,11 +34220,12 @@ | ||
| 34114 | 34220 | ** method is a no-op, but that does not change the fact the SQLite will |
| 34115 | 34221 | ** invoke it.) |
| 34116 | 34222 | ** |
| 34117 | 34223 | ** (9) Whenever the database file is modified, at least one bit in the range |
| 34118 | 34224 | ** 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. | |
| 34120 | 34227 | ** |
| 34121 | 34228 | ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less |
| 34122 | 34229 | ** than one billion transactions. |
| 34123 | 34230 | ** |
| 34124 | 34231 | ** (11) A database file is well-formed at the beginning and at the conclusion |
| @@ -34127,11 +34234,12 @@ | ||
| 34127 | 34234 | ** (12) An EXCLUSIVE lock is held on the database file when writing to |
| 34128 | 34235 | ** the database file. |
| 34129 | 34236 | ** |
| 34130 | 34237 | ** (13) A SHARED lock is held on the database file while reading any |
| 34131 | 34238 | ** content out of the database file. |
| 34132 | -*/ | |
| 34239 | +** | |
| 34240 | +******************************************************************************/ | |
| 34133 | 34241 | |
| 34134 | 34242 | /* |
| 34135 | 34243 | ** Macros for troubleshooting. Normally turned off |
| 34136 | 34244 | */ |
| 34137 | 34245 | #if 0 |
| @@ -34152,62 +34260,283 @@ | ||
| 34152 | 34260 | */ |
| 34153 | 34261 | #define PAGERID(p) ((int)(p->fd)) |
| 34154 | 34262 | #define FILEHANDLEID(fd) ((int)fd) |
| 34155 | 34263 | |
| 34156 | 34264 | /* |
| 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) | |
| 34209 | 34538 | |
| 34210 | 34539 | /* |
| 34211 | 34540 | ** A macro used for invoking the codec if there is one |
| 34212 | 34541 | */ |
| 34213 | 34542 | #ifdef SQLITE_HAS_CODEC |
| @@ -34253,37 +34582,32 @@ | ||
| 34253 | 34582 | u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ |
| 34254 | 34583 | #endif |
| 34255 | 34584 | }; |
| 34256 | 34585 | |
| 34257 | 34586 | /* |
| 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. | |
| 34285 | 34609 | ** |
| 34286 | 34610 | ** changeCountDone |
| 34287 | 34611 | ** |
| 34288 | 34612 | ** This boolean variable is used to make sure that the change-counter |
| 34289 | 34613 | ** (the 4-byte header field at byte offset 24 of the database file) is |
| @@ -34298,28 +34622,10 @@ | ||
| 34298 | 34622 | ** |
| 34299 | 34623 | ** This mechanism means that when running in exclusive mode, a connection |
| 34300 | 34624 | ** need only update the change-counter once, for the first transaction |
| 34301 | 34625 | ** committed. |
| 34302 | 34626 | ** |
| 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 | 34627 | ** setMaster |
| 34322 | 34628 | ** |
| 34323 | 34629 | ** When PagerCommitPhaseOne() is called to commit a transaction, it may |
| 34324 | 34630 | ** (or may not) specify a master-journal name to be written into the |
| 34325 | 34631 | ** journal file before it is synced to disk. |
| @@ -34326,84 +34632,146 @@ | ||
| 34326 | 34632 | ** |
| 34327 | 34633 | ** Whether or not a journal file contains a master-journal pointer affects |
| 34328 | 34634 | ** the way in which the journal file is finalized after the transaction is |
| 34329 | 34635 | ** committed or rolled back when running in "journal_mode=PERSIST" mode. |
| 34330 | 34636 | ** 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. | |
| 34335 | 34641 | ** |
| 34336 | 34642 | ** Journal files that contain master journal pointers cannot be finalized |
| 34337 | 34643 | ** simply by overwriting the first journal-header with zeroes, as the |
| 34338 | 34644 | ** master journal pointer could interfere with hot-journal rollback of any |
| 34339 | 34645 | ** subsequently interrupted transaction that reuses the journal file. |
| 34340 | 34646 | ** |
| 34341 | 34647 | ** The flag is cleared as soon as the journal file is finalized (either |
| 34342 | 34648 | ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the |
| 34343 | 34649 | ** 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). | |
| 34345 | 34651 | ** |
| 34346 | 34652 | ** doNotSpill, doNotSyncSpill |
| 34347 | 34653 | ** |
| 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. | |
| 34361 | 34669 | ** |
| 34362 | 34670 | ** subjInMemory |
| 34363 | 34671 | ** |
| 34364 | 34672 | ** This is a boolean variable. If true, then any required sub-journal |
| 34365 | 34673 | ** is opened as an in-memory journal file. If false, then in-memory |
| 34366 | 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. | |
| 34367 | 34739 | */ |
| 34368 | 34740 | struct Pager { |
| 34369 | 34741 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
| 34370 | 34742 | 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 */ | |
| 34372 | 34744 | u8 useJournal; /* Use a rollback journal on this file */ |
| 34373 | 34745 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
| 34374 | 34746 | u8 noSync; /* Do not sync the journal if true */ |
| 34375 | 34747 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
| 34376 | 34748 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
| 34377 | 34749 | u8 tempFile; /* zFilename is a temporary file */ |
| 34378 | 34750 | u8 readOnly; /* True for a read-only database */ |
| 34379 | 34751 | u8 memDb; /* True to inhibit all file I/O */ |
| 34380 | 34752 | |
| 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. | |
| 34391 | 34761 | */ |
| 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 */ | |
| 34396 | 34764 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
| 34397 | 34765 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
| 34398 | 34766 | u8 doNotSpill; /* Do not spill the cache when non-zero */ |
| 34399 | 34767 | u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */ |
| 34400 | - u8 dbSizeValid; /* Set when dbSize is correct */ | |
| 34401 | 34768 | u8 subjInMemory; /* True to use in-memory sub-journals */ |
| 34402 | 34769 | Pgno dbSize; /* Number of pages in the database */ |
| 34403 | 34770 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 34404 | 34771 | Pgno dbFileSize; /* Number of pages in the database file */ |
| 34772 | + Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ | |
| 34405 | 34773 | int errCode; /* One of several kinds of errors */ |
| 34406 | 34774 | int nRec; /* Pages journalled since last j-header written */ |
| 34407 | 34775 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 34408 | 34776 | u32 nSubRec; /* Number of records written to sub-journal */ |
| 34409 | 34777 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
| @@ -34410,21 +34778,25 @@ | ||
| 34410 | 34778 | sqlite3_file *fd; /* File descriptor for database */ |
| 34411 | 34779 | sqlite3_file *jfd; /* File descriptor for main journal */ |
| 34412 | 34780 | sqlite3_file *sjfd; /* File descriptor for sub-journal */ |
| 34413 | 34781 | i64 journalOff; /* Current write offset in the journal file */ |
| 34414 | 34782 | 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 */ | |
| 34416 | 34784 | PagerSavepoint *aSavepoint; /* Array of active savepoints */ |
| 34417 | 34785 | int nSavepoint; /* Number of elements in aSavepoint[] */ |
| 34418 | 34786 | 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 | + ***************************************************************************/ | |
| 34420 | 34790 | |
| 34421 | 34791 | u16 nExtra; /* Add this many bytes to each in-memory page */ |
| 34422 | 34792 | i16 nReserve; /* Number of unused bytes at end of each page */ |
| 34423 | 34793 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
| 34794 | + u32 sectorSize; /* Assumed sector size during rollback */ | |
| 34424 | 34795 | int pageSize; /* Number of bytes in a page */ |
| 34425 | 34796 | Pgno mxPgno; /* Maximum allowed size of the database */ |
| 34797 | + i64 journalSizeLimit; /* Size limit for persistent journal files */ | |
| 34426 | 34798 | char *zFilename; /* Name of the database file */ |
| 34427 | 34799 | char *zJournal; /* Name of the journal file */ |
| 34428 | 34800 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 34429 | 34801 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 34430 | 34802 | #ifdef SQLITE_TEST |
| @@ -34438,11 +34810,10 @@ | ||
| 34438 | 34810 | void (*xCodecFree)(void*); /* Destructor for the codec */ |
| 34439 | 34811 | void *pCodec; /* First argument to xCodec... methods */ |
| 34440 | 34812 | #endif |
| 34441 | 34813 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
| 34442 | 34814 | PCache *pPCache; /* Pointer to page cache object */ |
| 34443 | - sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ | |
| 34444 | 34815 | #ifndef SQLITE_OMIT_WAL |
| 34445 | 34816 | Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ |
| 34446 | 34817 | char *zWal; /* File name for write-ahead log */ |
| 34447 | 34818 | #endif |
| 34448 | 34819 | }; |
| @@ -34517,26 +34888,225 @@ | ||
| 34517 | 34888 | /* |
| 34518 | 34889 | ** The maximum legal page number is (2^31 - 1). |
| 34519 | 34890 | */ |
| 34520 | 34891 | #define PAGER_MAX_PGNO 2147483647 |
| 34521 | 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 | + | |
| 34522 | 34923 | #ifndef NDEBUG |
| 34523 | 34924 | /* |
| 34524 | 34925 | ** Usage: |
| 34525 | 34926 | ** |
| 34526 | 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. | |
| 34527 | 34931 | */ |
| 34528 | -static int assert_pager_state(Pager *pPager){ | |
| 34932 | +static int assert_pager_state(Pager *p){ | |
| 34933 | + Pager *pPager = p; | |
| 34529 | 34934 | |
| 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 | + ); | |
| 34532 | 34944 | |
| 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 | + } | |
| 34535 | 35056 | |
| 34536 | 35057 | return 1; |
| 34537 | 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 | +} | |
| 34538 | 35108 | #endif |
| 34539 | 35109 | |
| 34540 | 35110 | /* |
| 34541 | 35111 | ** Return true if it is necessary to write page *pPg into the sub-journal. |
| 34542 | 35112 | ** A page needs to be written into the sub-journal if there exists one |
| @@ -34584,10 +35154,11 @@ | ||
| 34584 | 35154 | |
| 34585 | 35155 | /* |
| 34586 | 35156 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 34587 | 35157 | */ |
| 34588 | 35158 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
| 35159 | + | |
| 34589 | 35160 | |
| 34590 | 35161 | /* |
| 34591 | 35162 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 34592 | 35163 | ** on success or an error code is something goes wrong. |
| 34593 | 35164 | */ |
| @@ -34596,31 +35167,57 @@ | ||
| 34596 | 35167 | put32bits(ac, val); |
| 34597 | 35168 | return sqlite3OsWrite(fd, ac, 4, offset); |
| 34598 | 35169 | } |
| 34599 | 35170 | |
| 34600 | 35171 | /* |
| 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 | +} | |
| 34613 | 35196 | |
| 34614 | 35197 | /* |
| 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. | |
| 34616 | 35206 | */ |
| 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 | + } | |
| 34620 | 35217 | } |
| 34621 | - return sqlite3OsUnlock(pFd, eLock); | |
| 35218 | + return rc; | |
| 34622 | 35219 | } |
| 34623 | 35220 | |
| 34624 | 35221 | /* |
| 34625 | 35222 | ** This function determines whether or not the atomic-write optimization |
| 34626 | 35223 | ** can be used with this pager. The optimization can be used if: |
| @@ -34865,11 +35462,11 @@ | ||
| 34865 | 35462 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
| 34866 | 35463 | */ |
| 34867 | 35464 | static int writeJournalHdr(Pager *pPager){ |
| 34868 | 35465 | int rc = SQLITE_OK; /* Return code */ |
| 34869 | 35466 | 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 */ | |
| 34871 | 35468 | u32 nWrite; /* Bytes of header sector written */ |
| 34872 | 35469 | int ii; /* Loop counter */ |
| 34873 | 35470 | |
| 34874 | 35471 | assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ |
| 34875 | 35472 | |
| @@ -34908,11 +35505,11 @@ | ||
| 34908 | 35505 | ** |
| 34909 | 35506 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 34910 | 35507 | ** that garbage data is never appended to the journal file. |
| 34911 | 35508 | */ |
| 34912 | 35509 | assert( isOpen(pPager->fd) || pPager->noSync ); |
| 34913 | - if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) | |
| 35510 | + if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) | |
| 34914 | 35511 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 34915 | 35512 | ){ |
| 34916 | 35513 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
| 34917 | 35514 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 34918 | 35515 | }else{ |
| @@ -35032,11 +35629,10 @@ | ||
| 35032 | 35629 | } |
| 35033 | 35630 | |
| 35034 | 35631 | if( pPager->journalOff==0 ){ |
| 35035 | 35632 | u32 iPageSize; /* Page-size field of journal header */ |
| 35036 | 35633 | u32 iSectorSize; /* Sector-size field of journal header */ |
| 35037 | - u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */ | |
| 35038 | 35634 | |
| 35039 | 35635 | /* Read the page-size and sector-size journal header fields. */ |
| 35040 | 35636 | if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize)) |
| 35041 | 35637 | || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize)) |
| 35042 | 35638 | ){ |
| @@ -35062,14 +35658,12 @@ | ||
| 35062 | 35658 | |
| 35063 | 35659 | /* Update the page-size to match the value read from the journal. |
| 35064 | 35660 | ** Use a testcase() macro to make sure that malloc failure within |
| 35065 | 35661 | ** PagerSetPagesize() is tested. |
| 35066 | 35662 | */ |
| 35067 | - iPageSize16 = (u16)iPageSize; | |
| 35068 | - rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1); | |
| 35663 | + rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1); | |
| 35069 | 35664 | testcase( rc!=SQLITE_OK ); |
| 35070 | - assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize ); | |
| 35071 | 35665 | |
| 35072 | 35666 | /* Update the assumed sector-size to match the value used by |
| 35073 | 35667 | ** the process that created this journal. If this journal was |
| 35074 | 35668 | ** created by a process other than this one, then this routine |
| 35075 | 35669 | ** is being called from within pager_playback(). The local value |
| @@ -35108,10 +35702,12 @@ | ||
| 35108 | 35702 | i64 iHdrOff; /* Offset of header in journal file */ |
| 35109 | 35703 | i64 jrnlSize; /* Size of journal file on disk */ |
| 35110 | 35704 | u32 cksum = 0; /* Checksum of string zMaster */ |
| 35111 | 35705 | |
| 35112 | 35706 | assert( pPager->setMaster==0 ); |
| 35707 | + assert( !pagerUseWal(pPager) ); | |
| 35708 | + | |
| 35113 | 35709 | if( !zMaster |
| 35114 | 35710 | || pPager->journalMode==PAGER_JOURNALMODE_MEMORY |
| 35115 | 35711 | || pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 35116 | 35712 | ){ |
| 35117 | 35713 | return SQLITE_OK; |
| @@ -35144,11 +35740,10 @@ | ||
| 35144 | 35740 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8))) |
| 35145 | 35741 | ){ |
| 35146 | 35742 | return rc; |
| 35147 | 35743 | } |
| 35148 | 35744 | pPager->journalOff += (nMaster+20); |
| 35149 | - pPager->needSync = !pPager->noSync; | |
| 35150 | 35745 | |
| 35151 | 35746 | /* If the pager is in peristent-journal mode, then the physical |
| 35152 | 35747 | ** journal-file may extend past the end of the master-journal name |
| 35153 | 35748 | ** and 8 bytes of magic data just written to the file. This is |
| 35154 | 35749 | ** dangerous because the code to rollback a hot-journal file |
| @@ -35180,21 +35775,15 @@ | ||
| 35180 | 35775 | (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p); |
| 35181 | 35776 | return p; |
| 35182 | 35777 | } |
| 35183 | 35778 | |
| 35184 | 35779 | /* |
| 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. | |
| 35189 | 35781 | */ |
| 35190 | 35782 | 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); | |
| 35196 | 35785 | } |
| 35197 | 35786 | |
| 35198 | 35787 | /* |
| 35199 | 35788 | ** Free all structures in the Pager.aSavepoint[] array and set both |
| 35200 | 35789 | ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal |
| @@ -35233,38 +35822,43 @@ | ||
| 35233 | 35822 | } |
| 35234 | 35823 | return rc; |
| 35235 | 35824 | } |
| 35236 | 35825 | |
| 35237 | 35826 | /* |
| 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. | |
| 35256 | 35830 | ** |
| 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). | |
| 35262 | 35842 | */ |
| 35263 | 35843 | 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() */ | |
| 35266 | 35860 | int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0; |
| 35267 | 35861 | |
| 35268 | 35862 | /* If the operating system support deletion of open files, then |
| 35269 | 35863 | ** close the journal file when dropping the database lock. Otherwise |
| 35270 | 35864 | ** another connection with journal_mode=delete might delete the file |
| @@ -35280,62 +35874,60 @@ | ||
| 35280 | 35874 | || 1!=(pPager->journalMode & 5) |
| 35281 | 35875 | ){ |
| 35282 | 35876 | sqlite3OsClose(pPager->jfd); |
| 35283 | 35877 | } |
| 35284 | 35878 | |
| 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 ); | |
| 35318 | 35894 | pPager->changeCountDone = 0; |
| 35319 | - pPager->state = PAGER_UNLOCK; | |
| 35320 | - pPager->dbModified = 0; | |
| 35895 | + pPager->eState = PAGER_OPEN; | |
| 35321 | 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; | |
| 35322 | 35914 | } |
| 35323 | 35915 | |
| 35324 | 35916 | /* |
| 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 | |
| 35337 | 35929 | ** cannot be trusted. This state can be cleared by completely discarding |
| 35338 | 35930 | ** the contents of the pager-cache. If a transaction was active when |
| 35339 | 35931 | ** the persistent error occurred, then the rollback journal may need |
| 35340 | 35932 | ** to be replayed to restore the contents of the database file (as if |
| 35341 | 35933 | ** it were a hot-journal). |
| @@ -35348,49 +35940,25 @@ | ||
| 35348 | 35940 | pPager->errCode==SQLITE_OK || |
| 35349 | 35941 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 35350 | 35942 | ); |
| 35351 | 35943 | if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ |
| 35352 | 35944 | pPager->errCode = rc; |
| 35945 | + pPager->eState = PAGER_ERROR; | |
| 35353 | 35946 | } |
| 35354 | 35947 | return rc; |
| 35355 | 35948 | } |
| 35356 | 35949 | |
| 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 | 35950 | /* |
| 35384 | 35951 | ** This routine ends a transaction. A transaction is usually ended by |
| 35385 | 35952 | ** either a COMMIT or a ROLLBACK operation. This routine may be called |
| 35386 | 35953 | ** after rollback of a hot-journal, or if an error occurs while opening |
| 35387 | 35954 | ** the journal file or writing the very first journal-header of a |
| 35388 | 35955 | ** database transaction. |
| 35389 | 35956 | ** |
| 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. | |
| 35392 | 35960 | ** |
| 35393 | 35961 | ** Otherwise, any active savepoints are released. |
| 35394 | 35962 | ** |
| 35395 | 35963 | ** If the journal file is open, then it is "finalized". Once a journal |
| 35396 | 35964 | ** file has been finalized it is not possible to use it to roll back a |
| @@ -35417,17 +35985,13 @@ | ||
| 35417 | 35985 | ** If the pager is running in exclusive mode, this method of finalizing |
| 35418 | 35986 | ** the journal file is never used. Instead, if the journalMode is |
| 35419 | 35987 | ** DELETE and the pager is in exclusive mode, the method described under |
| 35420 | 35988 | ** journalMode==PERSIST is used instead. |
| 35421 | 35989 | ** |
| 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. | |
| 35429 | 35993 | ** |
| 35430 | 35994 | ** SQLITE_OK is returned if no error occurs. If an error occurs during |
| 35431 | 35995 | ** any of the IO operations to finalize the journal file or unlock the |
| 35432 | 35996 | ** database then the IO error code is returned to the user. If the |
| 35433 | 35997 | ** operation to finalize the journal file fails, then the code still |
| @@ -35438,15 +36002,30 @@ | ||
| 35438 | 36002 | */ |
| 35439 | 36003 | static int pager_end_transaction(Pager *pPager, int hasMaster){ |
| 35440 | 36004 | int rc = SQLITE_OK; /* Error code from journal finalization operation */ |
| 35441 | 36005 | int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ |
| 35442 | 36006 | |
| 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 ){ | |
| 35444 | 36023 | return SQLITE_OK; |
| 35445 | 36024 | } |
| 36025 | + | |
| 35446 | 36026 | releaseAllSavepoints(pPager); |
| 35447 | - | |
| 35448 | 36027 | assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); |
| 35449 | 36028 | if( isOpen(pPager->jfd) ){ |
| 35450 | 36029 | assert( !pagerUseWal(pPager) ); |
| 35451 | 36030 | |
| 35452 | 36031 | /* Finalize the journal file. */ |
| @@ -35458,18 +36037,15 @@ | ||
| 35458 | 36037 | rc = SQLITE_OK; |
| 35459 | 36038 | }else{ |
| 35460 | 36039 | rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 35461 | 36040 | } |
| 35462 | 36041 | pPager->journalOff = 0; |
| 35463 | - pPager->journalStarted = 0; | |
| 35464 | 36042 | }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 35465 | 36043 | || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) |
| 35466 | 36044 | ){ |
| 35467 | 36045 | rc = zeroJournalHdr(pPager, hasMaster); |
| 35468 | - pager_error(pPager, rc); | |
| 35469 | 36046 | pPager->journalOff = 0; |
| 35470 | - pPager->journalStarted = 0; | |
| 35471 | 36047 | }else{ |
| 35472 | 36048 | /* This branch may be executed with Pager.journalMode==MEMORY if |
| 35473 | 36049 | ** a hot-journal was just rolled back. In this case the journal |
| 35474 | 36050 | ** file should be closed and deleted. If this connection writes to |
| 35475 | 36051 | ** the database file, it will do so using an in-memory journal. |
| @@ -35490,43 +36066,63 @@ | ||
| 35490 | 36066 | } |
| 35491 | 36067 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 35492 | 36068 | pPager->pInJournal = 0; |
| 35493 | 36069 | pPager->nRec = 0; |
| 35494 | 36070 | sqlite3PcacheCleanAll(pPager->pPCache); |
| 36071 | + sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); | |
| 35495 | 36072 | |
| 35496 | 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 | + */ | |
| 35497 | 36078 | rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); |
| 35498 | 36079 | 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); | |
| 35510 | 36085 | 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; | |
| 35525 | 36089 | |
| 35526 | 36090 | return (rc==SQLITE_OK?rc2:rc); |
| 35527 | 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 | +} | |
| 35528 | 36124 | |
| 35529 | 36125 | /* |
| 35530 | 36126 | ** Parameter aData must point to a buffer of pPager->pageSize bytes |
| 35531 | 36127 | ** of data. Compute and return a checksum based ont the contents of the |
| 35532 | 36128 | ** page of data and the current value of pPager->cksumInit. |
| @@ -35574,13 +36170,12 @@ | ||
| 35574 | 36170 | ** Read a single page from either the journal file (if isMainJrnl==1) or |
| 35575 | 36171 | ** from the sub-journal (if isMainJrnl==0) and playback that page. |
| 35576 | 36172 | ** The page begins at offset *pOffset into the file. The *pOffset |
| 35577 | 36173 | ** value is increased to the start of the next page in the journal. |
| 35578 | 36174 | ** |
| 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. | |
| 35582 | 36177 | ** |
| 35583 | 36178 | ** If the page number of the page record read from the (sub-)journal file |
| 35584 | 36179 | ** is greater than the current value of Pager.dbSize, then playback is |
| 35585 | 36180 | ** skipped and SQLITE_OK is returned. |
| 35586 | 36181 | ** |
| @@ -35629,10 +36224,21 @@ | ||
| 35629 | 36224 | assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ |
| 35630 | 36225 | |
| 35631 | 36226 | aData = pPager->pTmpSpace; |
| 35632 | 36227 | assert( aData ); /* Temp storage must have already been allocated */ |
| 35633 | 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 ); | |
| 35634 | 36240 | |
| 35635 | 36241 | /* Read the page number and page data from the journal or sub-journal |
| 35636 | 36242 | ** file. Return an error code to the caller if an IO error occurs. |
| 35637 | 36243 | */ |
| 35638 | 36244 | jfd = isMainJrnl ? pPager->jfd : pPager->sjfd; |
| @@ -35666,20 +36272,19 @@ | ||
| 35666 | 36272 | ** rollback, then don't bother to play it back again. |
| 35667 | 36273 | */ |
| 35668 | 36274 | if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ |
| 35669 | 36275 | return rc; |
| 35670 | 36276 | } |
| 35671 | - assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); | |
| 35672 | 36277 | |
| 35673 | 36278 | /* When playing back page 1, restore the nReserve setting |
| 35674 | 36279 | */ |
| 35675 | 36280 | if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){ |
| 35676 | 36281 | pPager->nReserve = ((u8*)aData)[20]; |
| 35677 | 36282 | pagerReportSize(pPager); |
| 35678 | 36283 | } |
| 35679 | 36284 | |
| 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 | |
| 35681 | 36286 | ** page in the pager cache. In this case just update the pager cache, |
| 35682 | 36287 | ** not the database file. The page is left marked dirty in this case. |
| 35683 | 36288 | ** |
| 35684 | 36289 | ** An exception to the above rule: If the database is in no-sync mode |
| 35685 | 36290 | ** and a page is moved during an incremental vacuum then the page may |
| @@ -35686,12 +36291,15 @@ | ||
| 35686 | 36291 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 35687 | 36292 | ** during a Movepage() call, then the page may not be in the cache |
| 35688 | 36293 | ** either. So the condition described in the above paragraph is not |
| 35689 | 36294 | ** assert()able. |
| 35690 | 36295 | ** |
| 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. | |
| 35693 | 36301 | ** |
| 35694 | 36302 | ** Ticket #1171: The statement journal might contain page content that is |
| 35695 | 36303 | ** different from the page content at the start of the transaction. |
| 35696 | 36304 | ** This occurs when a page is changed prior to the start of a statement |
| 35697 | 36305 | ** then changed again within the statement. When rolling back such a |
| @@ -35713,21 +36321,22 @@ | ||
| 35713 | 36321 | pPg = 0; |
| 35714 | 36322 | }else{ |
| 35715 | 36323 | pPg = pager_lookup(pPager, pgno); |
| 35716 | 36324 | } |
| 35717 | 36325 | assert( pPg || !MEMDB ); |
| 36326 | + assert( pPager->eState!=PAGER_OPEN || pPg==0 ); | |
| 35718 | 36327 | PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", |
| 35719 | 36328 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), |
| 35720 | 36329 | (isMainJrnl?"main-journal":"sub-journal") |
| 35721 | 36330 | )); |
| 35722 | 36331 | if( isMainJrnl ){ |
| 35723 | 36332 | isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); |
| 35724 | 36333 | }else{ |
| 35725 | 36334 | isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); |
| 35726 | 36335 | } |
| 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) | |
| 35729 | 36338 | && isSynced |
| 35730 | 36339 | ){ |
| 35731 | 36340 | i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 35732 | 36341 | testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); |
| 35733 | 36342 | assert( !pagerUseWal(pPager) ); |
| @@ -35953,14 +36562,14 @@ | ||
| 35953 | 36562 | /* |
| 35954 | 36563 | ** This function is used to change the actual size of the database |
| 35955 | 36564 | ** file in the file-system. This only happens when committing a transaction, |
| 35956 | 36565 | ** or rolling back a transaction (including rolling back a hot-journal). |
| 35957 | 36566 | ** |
| 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 | |
| 35962 | 36571 | ** xTruncate() method to truncate it. |
| 35963 | 36572 | ** |
| 35964 | 36573 | ** Or, it might might be the case that the file on disk is smaller than |
| 35965 | 36574 | ** nPage pages. Some operating system implementations can get confused if |
| 35966 | 36575 | ** you try to truncate a file to some size that is larger than it |
| @@ -35970,12 +36579,18 @@ | ||
| 35970 | 36579 | ** If successful, return SQLITE_OK. If an IO error occurs while modifying |
| 35971 | 36580 | ** the database file, return the error code to the caller. |
| 35972 | 36581 | */ |
| 35973 | 36582 | static int pager_truncate(Pager *pPager, Pgno nPage){ |
| 35974 | 36583 | 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 | + ){ | |
| 35976 | 36590 | i64 currentSize, newSize; |
| 36591 | + assert( pPager->eLock==EXCLUSIVE_LOCK ); | |
| 35977 | 36592 | /* TODO: Is it safe to use Pager.dbFileSize here? */ |
| 35978 | 36593 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 35979 | 36594 | newSize = pPager->pageSize*(i64)nPage; |
| 35980 | 36595 | if( rc==SQLITE_OK && currentSize!=newSize ){ |
| 35981 | 36596 | if( currentSize>newSize ){ |
| @@ -36095,11 +36710,11 @@ | ||
| 36095 | 36710 | /* Figure out how many records are in the journal. Abort early if |
| 36096 | 36711 | ** the journal is empty. |
| 36097 | 36712 | */ |
| 36098 | 36713 | assert( isOpen(pPager->jfd) ); |
| 36099 | 36714 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
| 36100 | - if( rc!=SQLITE_OK || szJ==0 ){ | |
| 36715 | + if( rc!=SQLITE_OK ){ | |
| 36101 | 36716 | goto end_playback; |
| 36102 | 36717 | } |
| 36103 | 36718 | |
| 36104 | 36719 | /* Read the master journal name from the journal, if it is present. |
| 36105 | 36720 | ** If a master journal file name is specified, but the file is not |
| @@ -36129,11 +36744,11 @@ | ||
| 36129 | 36744 | ** occurs. |
| 36130 | 36745 | */ |
| 36131 | 36746 | while( 1 ){ |
| 36132 | 36747 | /* Read the next journal header from the journal file. If there are |
| 36133 | 36748 | ** 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. | |
| 36135 | 36750 | ** This indicates nothing more needs to be rolled back. |
| 36136 | 36751 | */ |
| 36137 | 36752 | rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg); |
| 36138 | 36753 | if( rc!=SQLITE_OK ){ |
| 36139 | 36754 | if( rc==SQLITE_DONE ){ |
| @@ -36243,14 +36858,13 @@ | ||
| 36243 | 36858 | if( rc==SQLITE_OK ){ |
| 36244 | 36859 | zMaster = pPager->pTmpSpace; |
| 36245 | 36860 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 36246 | 36861 | testcase( rc!=SQLITE_OK ); |
| 36247 | 36862 | } |
| 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 | + ){ | |
| 36252 | 36866 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 36253 | 36867 | } |
| 36254 | 36868 | if( rc==SQLITE_OK ){ |
| 36255 | 36869 | rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
| 36256 | 36870 | testcase( rc!=SQLITE_OK ); |
| @@ -36288,11 +36902,11 @@ | ||
| 36288 | 36902 | Pgno pgno = pPg->pgno; /* Page number to read */ |
| 36289 | 36903 | int rc = SQLITE_OK; /* Return code */ |
| 36290 | 36904 | int isInWal = 0; /* True if page is in log file */ |
| 36291 | 36905 | int pgsz = pPager->pageSize; /* Number of bytes to read */ |
| 36292 | 36906 | |
| 36293 | - assert( pPager->state>=PAGER_SHARED && !MEMDB ); | |
| 36907 | + assert( pPager->eState>=PAGER_READER && !MEMDB ); | |
| 36294 | 36908 | assert( isOpen(pPager->fd) ); |
| 36295 | 36909 | |
| 36296 | 36910 | if( NEVER(!isOpen(pPager->fd)) ){ |
| 36297 | 36911 | assert( pPager->tempFile ); |
| 36298 | 36912 | memset(pPg->pData, 0, pPager->pageSize); |
| @@ -36451,31 +37065,82 @@ | ||
| 36451 | 37065 | static int pagerBeginReadTransaction(Pager *pPager){ |
| 36452 | 37066 | int rc; /* Return code */ |
| 36453 | 37067 | int changed = 0; /* True if cache must be reset */ |
| 36454 | 37068 | |
| 36455 | 37069 | assert( pagerUseWal(pPager) ); |
| 37070 | + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); | |
| 36456 | 37071 | |
| 36457 | 37072 | /* sqlite3WalEndReadTransaction() was not called for the previous |
| 36458 | 37073 | ** transaction in locking_mode=EXCLUSIVE. So call it now. If we |
| 36459 | 37074 | ** are in locking_mode=NORMAL and EndRead() was previously called, |
| 36460 | 37075 | ** the duplicate call is harmless. |
| 36461 | 37076 | */ |
| 36462 | 37077 | sqlite3WalEndReadTransaction(pPager->pWal); |
| 36463 | 37078 | |
| 36464 | 37079 | 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 | + } | |
| 36474 | 37083 | |
| 36475 | 37084 | return rc; |
| 36476 | 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 | + | |
| 36477 | 37142 | |
| 36478 | 37143 | /* |
| 36479 | 37144 | ** Check if the *-wal file that corresponds to the database opened by pPager |
| 36480 | 37145 | ** exists if the database is not empy, or verify that the *-wal file does |
| 36481 | 37146 | ** not exist (by deleting it) if the database file is empty. |
| @@ -36485,25 +37150,26 @@ | ||
| 36485 | 37150 | ** if no error occurs, make sure Pager.journalMode is not set to |
| 36486 | 37151 | ** PAGER_JOURNALMODE_WAL. |
| 36487 | 37152 | ** |
| 36488 | 37153 | ** Return SQLITE_OK or an error code. |
| 36489 | 37154 | ** |
| 36490 | -** If the WAL file is opened, also open a snapshot (read transaction). | |
| 36491 | -** | |
| 36492 | 37155 | ** The caller must hold a SHARED lock on the database file to call this |
| 36493 | 37156 | ** function. Because an EXCLUSIVE lock on the db file is required to delete |
| 36494 | 37157 | ** a WAL on a none-empty database, this ensures there is no race condition |
| 36495 | 37158 | ** between the xAccess() below and an xDelete() being executed by some |
| 36496 | 37159 | ** other connection. |
| 36497 | 37160 | */ |
| 36498 | 37161 | static int pagerOpenWalIfPresent(Pager *pPager){ |
| 36499 | 37162 | int rc = SQLITE_OK; |
| 37163 | + assert( pPager->eState==PAGER_OPEN ); | |
| 37164 | + assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock ); | |
| 37165 | + | |
| 36500 | 37166 | if( !pPager->tempFile ){ |
| 36501 | 37167 | 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); | |
| 36505 | 37171 | if( rc ) return rc; |
| 36506 | 37172 | if( nPage==0 ){ |
| 36507 | 37173 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); |
| 36508 | 37174 | isWal = 0; |
| 36509 | 37175 | }else{ |
| @@ -36511,15 +37177,12 @@ | ||
| 36511 | 37177 | pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal |
| 36512 | 37178 | ); |
| 36513 | 37179 | } |
| 36514 | 37180 | if( rc==SQLITE_OK ){ |
| 36515 | 37181 | if( isWal ){ |
| 36516 | - pager_reset(pPager); | |
| 37182 | + testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); | |
| 36517 | 37183 | rc = sqlite3PagerOpenWal(pPager, 0); |
| 36518 | - if( rc==SQLITE_OK ){ | |
| 36519 | - rc = pagerBeginReadTransaction(pPager); | |
| 36520 | - } | |
| 36521 | 37184 | }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ |
| 36522 | 37185 | pPager->journalMode = PAGER_JOURNALMODE_DELETE; |
| 36523 | 37186 | } |
| 36524 | 37187 | } |
| 36525 | 37188 | } |
| @@ -36567,11 +37230,12 @@ | ||
| 36567 | 37230 | i64 szJ; /* Effective size of the main journal */ |
| 36568 | 37231 | i64 iHdrOff; /* End of first segment of main-journal records */ |
| 36569 | 37232 | int rc = SQLITE_OK; /* Return code */ |
| 36570 | 37233 | Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ |
| 36571 | 37234 | |
| 36572 | - assert( pPager->state>=PAGER_SHARED ); | |
| 37235 | + assert( pPager->eState!=PAGER_ERROR ); | |
| 37236 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); | |
| 36573 | 37237 | |
| 36574 | 37238 | /* Allocate a bitvec to use to store the set of pages rolled back */ |
| 36575 | 37239 | if( pSavepoint ){ |
| 36576 | 37240 | pDone = sqlite3BitvecCreate(pSavepoint->nOrig); |
| 36577 | 37241 | if( !pDone ){ |
| @@ -36706,11 +37370,10 @@ | ||
| 36706 | 37370 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 36707 | 37371 | SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){ |
| 36708 | 37372 | pPager->noSync = (level==1 || pPager->tempFile) ?1:0; |
| 36709 | 37373 | pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; |
| 36710 | 37374 | pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
| 36711 | - if( pPager->noSync ) pPager->needSync = 0; | |
| 36712 | 37375 | } |
| 36713 | 37376 | #endif |
| 36714 | 37377 | |
| 36715 | 37378 | /* |
| 36716 | 37379 | ** The following global variable is incremented whenever the library |
| @@ -36788,11 +37451,11 @@ | ||
| 36788 | 37451 | ** Change the page size used by the Pager object. The new page size |
| 36789 | 37452 | ** is passed in *pPageSize. |
| 36790 | 37453 | ** |
| 36791 | 37454 | ** If the pager is in the error state when this function is called, it |
| 36792 | 37455 | ** 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). | |
| 36794 | 37457 | ** |
| 36795 | 37458 | ** Otherwise, if all of the following are true: |
| 36796 | 37459 | ** |
| 36797 | 37460 | ** * the new page size (value of *pPageSize) is valid (a power |
| 36798 | 37461 | ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and |
| @@ -36812,32 +37475,52 @@ | ||
| 36812 | 37475 | ** If the page size is not changed, either because one of the enumerated |
| 36813 | 37476 | ** conditions above is not true, the pager was in error state when this |
| 36814 | 37477 | ** function was called, or because the memory allocation attempt failed, |
| 36815 | 37478 | ** then *pPageSize is set to the old, retained page size before returning. |
| 36816 | 37479 | */ |
| 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 ){ | |
| 36839 | 37522 | if( nReserve<0 ) nReserve = pPager->nReserve; |
| 36840 | 37523 | assert( nReserve>=0 && nReserve<1000 ); |
| 36841 | 37524 | pPager->nReserve = (i16)nReserve; |
| 36842 | 37525 | pagerReportSize(pPager); |
| 36843 | 37526 | } |
| @@ -36862,17 +37545,15 @@ | ||
| 36862 | 37545 | ** maximum page count below the current size of the database. |
| 36863 | 37546 | ** |
| 36864 | 37547 | ** Regardless of mxPage, return the current maximum page count. |
| 36865 | 37548 | */ |
| 36866 | 37549 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 36867 | - int nPage; | |
| 36868 | 37550 | if( mxPage>0 ){ |
| 36869 | 37551 | pPager->mxPgno = mxPage; |
| 36870 | 37552 | } |
| 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; | |
| 36874 | 37555 | } |
| 36875 | 37556 | return pPager->mxPgno; |
| 36876 | 37557 | } |
| 36877 | 37558 | |
| 36878 | 37559 | /* |
| @@ -36933,70 +37614,20 @@ | ||
| 36933 | 37614 | } |
| 36934 | 37615 | return rc; |
| 36935 | 37616 | } |
| 36936 | 37617 | |
| 36937 | 37618 | /* |
| 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 | +** | |
| 36940 | 37622 | ** However, if the file is between 1 and <page-size> bytes in size, then |
| 36941 | 37623 | ** 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; | |
| 36998 | 37629 | } |
| 36999 | 37630 | |
| 37000 | 37631 | |
| 37001 | 37632 | /* |
| 37002 | 37633 | ** Try to obtain a lock of type locktype on the database file. If |
| @@ -37013,42 +37644,23 @@ | ||
| 37013 | 37644 | ** variable to locktype before returning. |
| 37014 | 37645 | */ |
| 37015 | 37646 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 37016 | 37647 | int rc; /* Return code */ |
| 37017 | 37648 | |
| 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 | 37649 | /* Check that this is either a no-op (because the requested lock is |
| 37030 | 37650 | ** already held, or one of the transistions that the busy-handler |
| 37031 | 37651 | ** may be invoked during, according to the comment above |
| 37032 | 37652 | ** sqlite3PagerSetBusyhandler(). |
| 37033 | 37653 | */ |
| 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) | |
| 37037 | 37657 | ); |
| 37038 | 37658 | |
| 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) ); | |
| 37050 | 37662 | return rc; |
| 37051 | 37663 | } |
| 37052 | 37664 | |
| 37053 | 37665 | /* |
| 37054 | 37666 | ** Function assertTruncateConstraint(pPager) checks that one of the |
| @@ -37089,13 +37701,12 @@ | ||
| 37089 | 37701 | ** function does not actually modify the database file on disk. It |
| 37090 | 37702 | ** just sets the internal state of the pager object so that the |
| 37091 | 37703 | ** truncation will be done when the current transaction is committed. |
| 37092 | 37704 | */ |
| 37093 | 37705 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 37094 | - assert( pPager->dbSizeValid ); | |
| 37095 | 37706 | assert( pPager->dbSize>=nPage ); |
| 37096 | - assert( pPager->state>=PAGER_RESERVED ); | |
| 37707 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); | |
| 37097 | 37708 | pPager->dbSize = nPage; |
| 37098 | 37709 | assertTruncateConstraint(pPager); |
| 37099 | 37710 | } |
| 37100 | 37711 | |
| 37101 | 37712 | |
| @@ -37141,11 +37752,11 @@ | ||
| 37141 | 37752 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ |
| 37142 | 37753 | u8 *pTmp = (u8 *)pPager->pTmpSpace; |
| 37143 | 37754 | |
| 37144 | 37755 | disable_simulated_io_errors(); |
| 37145 | 37756 | sqlite3BeginBenignMalloc(); |
| 37146 | - pPager->errCode = 0; | |
| 37757 | + /* pPager->errCode = 0; */ | |
| 37147 | 37758 | pPager->exclusiveMode = 0; |
| 37148 | 37759 | #ifndef SQLITE_OMIT_WAL |
| 37149 | 37760 | sqlite3WalClose(pPager->pWal, |
| 37150 | 37761 | (pPager->noSync ? 0 : pPager->sync_flags), |
| 37151 | 37762 | pPager->pageSize, pTmp |
| @@ -37154,18 +37765,23 @@ | ||
| 37154 | 37765 | #endif |
| 37155 | 37766 | pager_reset(pPager); |
| 37156 | 37767 | if( MEMDB ){ |
| 37157 | 37768 | pager_unlock(pPager); |
| 37158 | 37769 | }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. | |
| 37164 | 37780 | */ |
| 37165 | 37781 | if( isOpen(pPager->jfd) ){ |
| 37166 | - pPager->errCode = pagerSyncHotJournal(pPager); | |
| 37782 | + pager_error(pPager, pagerSyncHotJournal(pPager)); | |
| 37167 | 37783 | } |
| 37168 | 37784 | pagerUnlockAndRollback(pPager); |
| 37169 | 37785 | } |
| 37170 | 37786 | sqlite3EndBenignMalloc(); |
| 37171 | 37787 | enable_simulated_io_errors(); |
| @@ -37206,13 +37822,13 @@ | ||
| 37206 | 37822 | /* |
| 37207 | 37823 | ** Sync the journal. In other words, make sure all the pages that have |
| 37208 | 37824 | ** been written to the journal have actually reached the surface of the |
| 37209 | 37825 | ** disk and can be restored in the event of a hot-journal rollback. |
| 37210 | 37826 | ** |
| 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: | |
| 37214 | 37830 | ** |
| 37215 | 37831 | ** * If the journal file is an in-memory journal file, no action need |
| 37216 | 37832 | ** be taken. |
| 37217 | 37833 | ** |
| 37218 | 37834 | ** * Otherwise, if the device does not support the SAFE_APPEND property, |
| @@ -37232,22 +37848,29 @@ | ||
| 37232 | 37848 | ** <update nRec field> |
| 37233 | 37849 | ** } |
| 37234 | 37850 | ** if( NOT SEQUENTIAL ) xSync(<journal file>); |
| 37235 | 37851 | ** } |
| 37236 | 37852 | ** |
| 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 | 37853 | ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every |
| 37241 | 37854 | ** page currently held in memory before returning SQLITE_OK. If an IO |
| 37242 | 37855 | ** error is encountered, then the IO error code is returned to the caller. |
| 37243 | 37856 | */ |
| 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 ){ | |
| 37246 | 37870 | 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 ){ | |
| 37249 | 37872 | const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 37250 | 37873 | assert( isOpen(pPager->jfd) ); |
| 37251 | 37874 | |
| 37252 | 37875 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
| 37253 | 37876 | /* This block deals with an obscure problem. If the last connection |
| @@ -37318,21 +37941,29 @@ | ||
| 37318 | 37941 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 37319 | 37942 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 37320 | 37943 | ); |
| 37321 | 37944 | if( rc!=SQLITE_OK ) return rc; |
| 37322 | 37945 | } |
| 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 | + } | |
| 37332 | 37956 | } |
| 37333 | 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) ); | |
| 37334 | 37965 | return SQLITE_OK; |
| 37335 | 37966 | } |
| 37336 | 37967 | |
| 37337 | 37968 | /* |
| 37338 | 37969 | ** The argument is the first in a linked list of dirty pages connected |
| @@ -37365,31 +37996,16 @@ | ||
| 37365 | 37996 | ** If everything is successful, SQLITE_OK is returned. If an IO error |
| 37366 | 37997 | ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot |
| 37367 | 37998 | ** be obtained, SQLITE_BUSY is returned. |
| 37368 | 37999 | */ |
| 37369 | 38000 | 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. */ | |
| 37388 | 38004 | 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 ); | |
| 37391 | 38007 | |
| 37392 | 38008 | /* If the file is a temp-file has not yet been opened, open it now. It |
| 37393 | 38009 | ** is not possible for rc to be other than SQLITE_OK if this branch |
| 37394 | 38010 | ** is taken, as pager_wait_on_lock() is a no-op for temp-files. |
| 37395 | 38011 | */ |
| @@ -37400,13 +38016,14 @@ | ||
| 37400 | 38016 | |
| 37401 | 38017 | /* Before the first write, give the VFS a hint of what the final |
| 37402 | 38018 | ** file size will be. |
| 37403 | 38019 | */ |
| 37404 | 38020 | 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 ){ | |
| 37406 | 38022 | sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; |
| 37407 | 38023 | sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); |
| 38024 | + pPager->dbHintSize = pPager->dbSize; | |
| 37408 | 38025 | } |
| 37409 | 38026 | |
| 37410 | 38027 | while( rc==SQLITE_OK && pList ){ |
| 37411 | 38028 | Pgno pgno = pList->pgno; |
| 37412 | 38029 | |
| @@ -37419,10 +38036,12 @@ | ||
| 37419 | 38036 | ** set (set by sqlite3PagerDontWrite()). |
| 37420 | 38037 | */ |
| 37421 | 38038 | if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ |
| 37422 | 38039 | i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ |
| 37423 | 38040 | char *pData; /* Data to write */ |
| 38041 | + | |
| 38042 | + assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); | |
| 37424 | 38043 | |
| 37425 | 38044 | /* Encode the database */ |
| 37426 | 38045 | CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); |
| 37427 | 38046 | |
| 37428 | 38047 | /* Write out the page data. */ |
| @@ -37562,13 +38181,18 @@ | ||
| 37562 | 38181 | ** pages belonging to the same sector. |
| 37563 | 38182 | ** |
| 37564 | 38183 | ** The doNotSpill flag inhibits all cache spilling regardless of whether |
| 37565 | 38184 | ** or not a sync is required. This is set during a rollback. |
| 37566 | 38185 | ** |
| 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. | |
| 37568 | 38192 | */ |
| 37569 | - if( pPager->errCode ) return SQLITE_OK; | |
| 38193 | + if( NEVER(pPager->errCode) ) return SQLITE_OK; | |
| 37570 | 38194 | if( pPager->doNotSpill ) return SQLITE_OK; |
| 37571 | 38195 | if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ |
| 37572 | 38196 | return SQLITE_OK; |
| 37573 | 38197 | } |
| 37574 | 38198 | |
| @@ -37582,20 +38206,14 @@ | ||
| 37582 | 38206 | rc = pagerWalFrames(pPager, pPg, 0, 0, 0); |
| 37583 | 38207 | } |
| 37584 | 38208 | }else{ |
| 37585 | 38209 | |
| 37586 | 38210 | /* 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); | |
| 37597 | 38215 | } |
| 37598 | 38216 | |
| 37599 | 38217 | /* If the page number of this page is larger than the current size of |
| 37600 | 38218 | ** the database image, it may need to be written to the sub-journal. |
| 37601 | 38219 | ** This is because the call to pager_write_pagelist() below will not |
| @@ -37629,10 +38247,11 @@ | ||
| 37629 | 38247 | rc = subjournalPage(pPg); |
| 37630 | 38248 | } |
| 37631 | 38249 | |
| 37632 | 38250 | /* Write the contents of the page out to the database file. */ |
| 37633 | 38251 | if( rc==SQLITE_OK ){ |
| 38252 | + assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); | |
| 37634 | 38253 | rc = pager_write_pagelist(pPager, pPg); |
| 37635 | 38254 | } |
| 37636 | 38255 | } |
| 37637 | 38256 | |
| 37638 | 38257 | /* Mark the page as clean. */ |
| @@ -37639,11 +38258,11 @@ | ||
| 37639 | 38258 | if( rc==SQLITE_OK ){ |
| 37640 | 38259 | PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno)); |
| 37641 | 38260 | sqlite3PcacheMakeClean(pPg); |
| 37642 | 38261 | } |
| 37643 | 38262 | |
| 37644 | - return pager_error(pPager, rc); | |
| 38263 | + return pager_error(pPager, rc); | |
| 37645 | 38264 | } |
| 37646 | 38265 | |
| 37647 | 38266 | |
| 37648 | 38267 | /* |
| 37649 | 38268 | ** Allocate and initialize a new Pager object and put a pointer to it |
| @@ -37694,11 +38313,11 @@ | ||
| 37694 | 38313 | char *zPathname = 0; /* Full path to database file */ |
| 37695 | 38314 | int nPathname = 0; /* Number of bytes in zPathname */ |
| 37696 | 38315 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ |
| 37697 | 38316 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */ |
| 37698 | 38317 | 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 */ | |
| 37700 | 38319 | |
| 37701 | 38320 | /* Figure out how much space is required for each journal file-handle |
| 37702 | 38321 | ** (there are two of them, the main journal and the sub-journal). This |
| 37703 | 38322 | ** is the maximum space required for an in-memory journal file handle |
| 37704 | 38323 | ** and a regular journal file-handle. Note that a "regular journal-handle" |
| @@ -37829,11 +38448,11 @@ | ||
| 37829 | 38448 | assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE); |
| 37830 | 38449 | if( szPageDflt<pPager->sectorSize ){ |
| 37831 | 38450 | if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 37832 | 38451 | szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
| 37833 | 38452 | }else{ |
| 37834 | - szPageDflt = (u16)pPager->sectorSize; | |
| 38453 | + szPageDflt = (u32)pPager->sectorSize; | |
| 37835 | 38454 | } |
| 37836 | 38455 | } |
| 37837 | 38456 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 37838 | 38457 | { |
| 37839 | 38458 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| @@ -37857,11 +38476,12 @@ | ||
| 37857 | 38476 | ** This branch is also run for an in-memory database. An in-memory |
| 37858 | 38477 | ** database is the same as a temp-file that is never written out to |
| 37859 | 38478 | ** disk and uses an in-memory rollback journal. |
| 37860 | 38479 | */ |
| 37861 | 38480 | tempFile = 1; |
| 37862 | - pPager->state = PAGER_EXCLUSIVE; | |
| 38481 | + pPager->eState = PAGER_READER; | |
| 38482 | + pPager->eLock = EXCLUSIVE_LOCK; | |
| 37863 | 38483 | readOnly = (vfsFlags&SQLITE_OPEN_READONLY); |
| 37864 | 38484 | } |
| 37865 | 38485 | |
| 37866 | 38486 | /* The following call to PagerSetPagesize() serves to set the value of |
| 37867 | 38487 | ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. |
| @@ -37894,27 +38514,27 @@ | ||
| 37894 | 38514 | pPager->useJournal = (u8)useJournal; |
| 37895 | 38515 | pPager->noReadlock = (noReadlock && readOnly) ?1:0; |
| 37896 | 38516 | /* pPager->stmtOpen = 0; */ |
| 37897 | 38517 | /* pPager->stmtInUse = 0; */ |
| 37898 | 38518 | /* pPager->nRef = 0; */ |
| 37899 | - pPager->dbSizeValid = (u8)memDb; | |
| 37900 | 38519 | /* pPager->stmtSize = 0; */ |
| 37901 | 38520 | /* pPager->stmtJSize = 0; */ |
| 37902 | 38521 | /* pPager->nPage = 0; */ |
| 37903 | 38522 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
| 37904 | 38523 | /* pPager->state = PAGER_UNLOCK; */ |
| 38524 | +#if 0 | |
| 37905 | 38525 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
| 38526 | +#endif | |
| 37906 | 38527 | /* pPager->errMask = 0; */ |
| 37907 | 38528 | pPager->tempFile = (u8)tempFile; |
| 37908 | 38529 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 37909 | 38530 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 37910 | 38531 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 37911 | 38532 | pPager->exclusiveMode = (u8)tempFile; |
| 37912 | 38533 | pPager->changeCountDone = pPager->tempFile; |
| 37913 | 38534 | pPager->memDb = (u8)memDb; |
| 37914 | 38535 | pPager->readOnly = (u8)readOnly; |
| 37915 | - /* pPager->needSync = 0; */ | |
| 37916 | 38536 | assert( useJournal || pPager->tempFile ); |
| 37917 | 38537 | pPager->noSync = pPager->tempFile; |
| 37918 | 38538 | pPager->fullSync = pPager->noSync ?0:1; |
| 37919 | 38539 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
| 37920 | 38540 | /* pPager->pFirst = 0; */ |
| @@ -37975,24 +38595,24 @@ | ||
| 37975 | 38595 | sqlite3_vfs * const pVfs = pPager->pVfs; |
| 37976 | 38596 | int rc = SQLITE_OK; /* Return code */ |
| 37977 | 38597 | int exists = 1; /* True if a journal file is present */ |
| 37978 | 38598 | int jrnlOpen = !!isOpen(pPager->jfd); |
| 37979 | 38599 | |
| 37980 | - assert( pPager!=0 ); | |
| 37981 | 38600 | assert( pPager->useJournal ); |
| 37982 | 38601 | assert( isOpen(pPager->fd) ); |
| 37983 | - assert( pPager->state <= PAGER_SHARED ); | |
| 38602 | + assert( pPager->eState==PAGER_OPEN ); | |
| 38603 | + | |
| 37984 | 38604 | assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) & |
| 37985 | 38605 | SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
| 37986 | 38606 | )); |
| 37987 | 38607 | |
| 37988 | 38608 | *pExists = 0; |
| 37989 | 38609 | if( !jrnlOpen ){ |
| 37990 | 38610 | rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 37991 | 38611 | } |
| 37992 | 38612 | 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 */ | |
| 37994 | 38614 | |
| 37995 | 38615 | /* Race condition here: Another process might have been holding the |
| 37996 | 38616 | ** the RESERVED lock and have a journal open at the sqlite3OsAccess() |
| 37997 | 38617 | ** call above, but then delete the journal and drop the lock before |
| 37998 | 38618 | ** we get to the following sqlite3OsCheckReservedLock() call. If that |
| @@ -38000,25 +38620,25 @@ | ||
| 38000 | 38620 | ** in fact there is none. This results in a false-positive which will |
| 38001 | 38621 | ** be dealt with by the playback routine. Ticket #3883. |
| 38002 | 38622 | */ |
| 38003 | 38623 | rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 38004 | 38624 | if( rc==SQLITE_OK && !locked ){ |
| 38005 | - int nPage; | |
| 38625 | + Pgno nPage; /* Number of pages in database file */ | |
| 38006 | 38626 | |
| 38007 | 38627 | /* Check the size of the database file. If it consists of 0 pages, |
| 38008 | 38628 | ** then delete the journal file. See the header comment above for |
| 38009 | 38629 | ** the reasoning here. Delete the obsolete journal file under |
| 38010 | 38630 | ** a RESERVED lock to avoid race conditions and to avoid violating |
| 38011 | 38631 | ** [H33020]. |
| 38012 | 38632 | */ |
| 38013 | - rc = sqlite3PagerPagecount(pPager, &nPage); | |
| 38633 | + rc = pagerPagecount(pPager, &nPage); | |
| 38014 | 38634 | if( rc==SQLITE_OK ){ |
| 38015 | 38635 | if( nPage==0 ){ |
| 38016 | 38636 | sqlite3BeginBenignMalloc(); |
| 38017 | - if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){ | |
| 38637 | + if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){ | |
| 38018 | 38638 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 38019 | - sqlite3OsUnlock(pPager->fd, SHARED_LOCK); | |
| 38639 | + pagerUnlockDb(pPager, SHARED_LOCK); | |
| 38020 | 38640 | } |
| 38021 | 38641 | sqlite3EndBenignMalloc(); |
| 38022 | 38642 | }else{ |
| 38023 | 38643 | /* The journal file exists and no other connection has a reserved |
| 38024 | 38644 | ** or greater lock on the database file. Now check that there is |
| @@ -38067,11 +38687,11 @@ | ||
| 38067 | 38687 | ** has been successfully called. If a shared-lock is already held when |
| 38068 | 38688 | ** this function is called, it is a no-op. |
| 38069 | 38689 | ** |
| 38070 | 38690 | ** The following operations are also performed by this function. |
| 38071 | 38691 | ** |
| 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 | |
| 38073 | 38693 | ** on the database file), then an attempt is made to obtain a |
| 38074 | 38694 | ** SHARED lock on the database file. Immediately after obtaining |
| 38075 | 38695 | ** the SHARED lock, the file-system is checked for a hot-journal, |
| 38076 | 38696 | ** which is played back if present. Following any hot-journal |
| 38077 | 38697 | ** rollback, the contents of the cache are validated by checking |
| @@ -38082,70 +38702,51 @@ | ||
| 38082 | 38702 | ** no outstanding references to any pages, and is in the error state, |
| 38083 | 38703 | ** then an attempt is made to clear the error state by discarding |
| 38084 | 38704 | ** the contents of the page cache and rolling back any open journal |
| 38085 | 38705 | ** file. |
| 38086 | 38706 | ** |
| 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. | |
| 38095 | 38710 | */ |
| 38096 | 38711 | SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ |
| 38097 | 38712 | int rc = SQLITE_OK; /* Return code */ |
| 38098 | - int isErrorReset = 0; /* True if recovering from error state */ | |
| 38099 | 38713 | |
| 38100 | 38714 | /* 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 | + */ | |
| 38102 | 38719 | assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 38720 | + assert( assert_pager_state(pPager) ); | |
| 38721 | + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); | |
| 38103 | 38722 | if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } |
| 38104 | 38723 | |
| 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 | + | |
| 38122 | 38727 | 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 ){ | |
| 38128 | 38731 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 38129 | 38732 | 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; | |
| 38132 | 38735 | } |
| 38133 | 38736 | } |
| 38134 | - assert( pPager->state>=SHARED_LOCK ); | |
| 38135 | 38737 | |
| 38136 | 38738 | /* If a journal file exists, and there is no RESERVED lock on the |
| 38137 | 38739 | ** database file, then it either needs to be played back or deleted. |
| 38138 | 38740 | */ |
| 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 ){ | |
| 38147 | 38748 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 38148 | 38749 | ** important that a RESERVED lock is not obtained on the way to the |
| 38149 | 38750 | ** EXCLUSIVE lock. If it were, another process might open the |
| 38150 | 38751 | ** database file, detect the RESERVED lock, and conclude that the |
| 38151 | 38752 | ** database is safe to read while this process is still rolling the |
| @@ -38153,62 +38754,49 @@ | ||
| 38153 | 38754 | ** |
| 38154 | 38755 | ** Because the intermediate RESERVED lock is not requested, any |
| 38155 | 38756 | ** other process attempting to access the database file will get to |
| 38156 | 38757 | ** this point in the code and fail to obtain its own EXCLUSIVE lock |
| 38157 | 38758 | ** 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 | + } | |
| 38210 | 38798 | |
| 38211 | 38799 | /* Playback and delete the journal. Drop the database write |
| 38212 | 38800 | ** lock and reacquire the read lock. Purge the cache before |
| 38213 | 38801 | ** playing back the hot-journal so that we don't end up with |
| 38214 | 38802 | ** an inconsistent cache. Sync the hot journal before playing |
| @@ -38215,25 +38803,50 @@ | ||
| 38215 | 38803 | ** it back since the process that crashed and left the hot journal |
| 38216 | 38804 | ** probably did not sync it and we are required to always sync |
| 38217 | 38805 | ** the journal before playing it back. |
| 38218 | 38806 | */ |
| 38219 | 38807 | if( isOpen(pPager->jfd) ){ |
| 38808 | + assert( rc==SQLITE_OK ); | |
| 38220 | 38809 | rc = pagerSyncHotJournal(pPager); |
| 38221 | 38810 | if( rc==SQLITE_OK ){ |
| 38222 | 38811 | 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) | |
| 38231 | 38842 | ); |
| 38232 | 38843 | } |
| 38233 | 38844 | |
| 38234 | - if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){ | |
| 38845 | + if( !pPager->tempFile | |
| 38846 | + && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) | |
| 38847 | + ){ | |
| 38235 | 38848 | /* The shared-lock has just been acquired on the database file |
| 38236 | 38849 | ** and there are already pages in the cache (from a previous |
| 38237 | 38850 | ** read or write transaction). Check to see if the database |
| 38238 | 38851 | ** has been modified. If the database has changed, flush the |
| 38239 | 38852 | ** cache. |
| @@ -38246,18 +38859,15 @@ | ||
| 38246 | 38859 | ** |
| 38247 | 38860 | ** There is a vanishingly small chance that a change will not be |
| 38248 | 38861 | ** detected. The chance of an undetected change is so small that |
| 38249 | 38862 | ** it can be neglected. |
| 38250 | 38863 | */ |
| 38251 | - int nPage = 0; | |
| 38864 | + Pgno nPage = 0; | |
| 38252 | 38865 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 38253 | - sqlite3PagerPagecount(pPager, &nPage); | |
| 38254 | 38866 | |
| 38255 | - if( pPager->errCode ){ | |
| 38256 | - rc = pPager->errCode; | |
| 38257 | - goto failed; | |
| 38258 | - } | |
| 38867 | + rc = pagerPagecount(pPager, &nPage); | |
| 38868 | + if( rc ) goto failed; | |
| 38259 | 38869 | |
| 38260 | 38870 | if( nPage>0 ){ |
| 38261 | 38871 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 38262 | 38872 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 38263 | 38873 | if( rc!=SQLITE_OK ){ |
| @@ -38269,22 +38879,34 @@ | ||
| 38269 | 38879 | |
| 38270 | 38880 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
| 38271 | 38881 | pager_reset(pPager); |
| 38272 | 38882 | } |
| 38273 | 38883 | } |
| 38274 | - assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED ); | |
| 38275 | 38884 | |
| 38276 | 38885 | /* If there is a WAL file in the file-system, open this database in WAL |
| 38277 | 38886 | ** mode. Otherwise, the following function call is a no-op. |
| 38278 | 38887 | */ |
| 38279 | 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); | |
| 38280 | 38899 | } |
| 38281 | 38900 | |
| 38282 | 38901 | failed: |
| 38283 | 38902 | if( rc!=SQLITE_OK ){ |
| 38284 | - /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ | |
| 38903 | + assert( !MEMDB ); | |
| 38285 | 38904 | pager_unlock(pPager); |
| 38905 | + assert( pPager->eState==PAGER_OPEN ); | |
| 38906 | + }else{ | |
| 38907 | + pPager->eState = PAGER_READER; | |
| 38286 | 38908 | } |
| 38287 | 38909 | return rc; |
| 38288 | 38910 | } |
| 38289 | 38911 | |
| 38290 | 38912 | /* |
| @@ -38294,13 +38916,11 @@ | ||
| 38294 | 38916 | ** Except, in locking_mode=EXCLUSIVE when there is nothing to in |
| 38295 | 38917 | ** the rollback journal, the unlock is not performed and there is |
| 38296 | 38918 | ** nothing to rollback, so this routine is a no-op. |
| 38297 | 38919 | */ |
| 38298 | 38920 | static void pagerUnlockIfUnused(Pager *pPager){ |
| 38299 | - if( (sqlite3PcacheRefCount(pPager->pPCache)==0) | |
| 38300 | - && (!pPager->exclusiveMode || pPager->journalOff>0) | |
| 38301 | - ){ | |
| 38921 | + if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ | |
| 38302 | 38922 | pagerUnlockAndRollback(pPager); |
| 38303 | 38923 | } |
| 38304 | 38924 | } |
| 38305 | 38925 | |
| 38306 | 38926 | /* |
| @@ -38360,20 +38980,20 @@ | ||
| 38360 | 38980 | int noContent /* Do not bother reading content from disk if true */ |
| 38361 | 38981 | ){ |
| 38362 | 38982 | int rc; |
| 38363 | 38983 | PgHdr *pPg; |
| 38364 | 38984 | |
| 38985 | + assert( pPager->eState>=PAGER_READER ); | |
| 38365 | 38986 | assert( assert_pager_state(pPager) ); |
| 38366 | - assert( pPager->state>PAGER_UNLOCK ); | |
| 38367 | 38987 | |
| 38368 | 38988 | if( pgno==0 ){ |
| 38369 | 38989 | return SQLITE_CORRUPT_BKPT; |
| 38370 | 38990 | } |
| 38371 | 38991 | |
| 38372 | 38992 | /* If the pager is in the error state, return an error immediately. |
| 38373 | 38993 | ** Otherwise, request the page from the PCache layer. */ |
| 38374 | - if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){ | |
| 38994 | + if( pPager->errCode!=SQLITE_OK ){ | |
| 38375 | 38995 | rc = pPager->errCode; |
| 38376 | 38996 | }else{ |
| 38377 | 38997 | rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage); |
| 38378 | 38998 | } |
| 38379 | 38999 | |
| @@ -38395,11 +39015,10 @@ | ||
| 38395 | 39015 | return SQLITE_OK; |
| 38396 | 39016 | |
| 38397 | 39017 | }else{ |
| 38398 | 39018 | /* The pager cache has created a new page. Its content needs to |
| 38399 | 39019 | ** be initialized. */ |
| 38400 | - int nMax; | |
| 38401 | 39020 | |
| 38402 | 39021 | PAGER_INCR(pPager->nMiss); |
| 38403 | 39022 | pPg = *ppPage; |
| 38404 | 39023 | pPg->pPager = pPager; |
| 38405 | 39024 | |
| @@ -38408,16 +39027,11 @@ | ||
| 38408 | 39027 | if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ |
| 38409 | 39028 | rc = SQLITE_CORRUPT_BKPT; |
| 38410 | 39029 | goto pager_acquire_err; |
| 38411 | 39030 | } |
| 38412 | 39031 | |
| 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) ){ | |
| 38419 | 39033 | if( pgno>pPager->mxPgno ){ |
| 38420 | 39034 | rc = SQLITE_FULL; |
| 38421 | 39035 | goto pager_acquire_err; |
| 38422 | 39036 | } |
| 38423 | 39037 | if( noContent ){ |
| @@ -38464,13 +39078,11 @@ | ||
| 38464 | 39078 | } |
| 38465 | 39079 | |
| 38466 | 39080 | /* |
| 38467 | 39081 | ** Acquire a page if it is already in the in-memory cache. Do |
| 38468 | 39082 | ** 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. | |
| 38472 | 39084 | ** |
| 38473 | 39085 | ** See also sqlite3PagerGet(). The difference between this routine |
| 38474 | 39086 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
| 38475 | 39087 | ** in the page if the page is not already in cache. This routine |
| 38476 | 39088 | ** returns NULL if the page is not in cache or if a disk I/O error |
| @@ -38479,11 +39091,11 @@ | ||
| 38479 | 39091 | SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
| 38480 | 39092 | PgHdr *pPg = 0; |
| 38481 | 39093 | assert( pPager!=0 ); |
| 38482 | 39094 | assert( pgno!=0 ); |
| 38483 | 39095 | assert( pPager->pPCache!=0 ); |
| 38484 | - assert( pPager->state > PAGER_UNLOCK ); | |
| 39096 | + assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR ); | |
| 38485 | 39097 | sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); |
| 38486 | 39098 | return pPg; |
| 38487 | 39099 | } |
| 38488 | 39100 | |
| 38489 | 39101 | /* |
| @@ -38524,73 +39136,71 @@ | ||
| 38524 | 39136 | ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or |
| 38525 | 39137 | ** an IO error code if opening or writing the journal file fails. |
| 38526 | 39138 | */ |
| 38527 | 39139 | static int pager_open_journal(Pager *pPager){ |
| 38528 | 39140 | int rc = SQLITE_OK; /* Return code */ |
| 38529 | - int nPage; /* Size of database file */ | |
| 38530 | 39141 | sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ |
| 38531 | 39142 | |
| 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) ); | |
| 38535 | 39145 | assert( pPager->pInJournal==0 ); |
| 38536 | 39146 | |
| 38537 | 39147 | /* If already in the error state, this function is a no-op. But on |
| 38538 | 39148 | ** the other hand, this routine is never called if we are already in |
| 38539 | 39149 | ** an error state. */ |
| 38540 | 39150 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38541 | 39151 | |
| 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 | + } | |
| 38586 | 39192 | } |
| 38587 | 39193 | |
| 38588 | 39194 | if( rc!=SQLITE_OK ){ |
| 38589 | 39195 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 38590 | 39196 | pPager->pInJournal = 0; |
| 39197 | + }else{ | |
| 39198 | + assert( pPager->eState==PAGER_WRITER_LOCKED ); | |
| 39199 | + pPager->eState = PAGER_WRITER_CACHEMOD; | |
| 38591 | 39200 | } |
| 39201 | + | |
| 38592 | 39202 | return rc; |
| 38593 | 39203 | } |
| 38594 | 39204 | |
| 38595 | 39205 | /* |
| 38596 | 39206 | ** Begin a write-transaction on the specified pager object. If a |
| @@ -38599,18 +39209,10 @@ | ||
| 38599 | 39209 | ** If the exFlag argument is false, then acquire at least a RESERVED |
| 38600 | 39210 | ** lock on the database file. If exFlag is true, then acquire at least |
| 38601 | 39211 | ** an EXCLUSIVE lock. If such a lock is already held, no locking |
| 38602 | 39212 | ** functions need be called. |
| 38603 | 39213 | ** |
| 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 | 39214 | ** If the subjInMemory argument is non-zero, then any sub-journal opened |
| 38613 | 39215 | ** within this transaction will be opened as an in-memory file. This |
| 38614 | 39216 | ** has no effect if the sub-journal is already opened (as it may be when |
| 38615 | 39217 | ** running in exclusive mode) or if the transaction does not require a |
| 38616 | 39218 | ** sub-journal. If the subjInMemory argument is zero, then any required |
| @@ -38617,24 +39219,24 @@ | ||
| 38617 | 39219 | ** sub-journal is implemented in-memory if pPager is an in-memory database, |
| 38618 | 39220 | ** or using a temporary file otherwise. |
| 38619 | 39221 | */ |
| 38620 | 39222 | SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ |
| 38621 | 39223 | 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 ); | |
| 38623 | 39227 | pPager->subjInMemory = (u8)subjInMemory; |
| 38624 | 39228 | |
| 38625 | - if( pPager->state==PAGER_SHARED ){ | |
| 39229 | + if( ALWAYS(pPager->eState==PAGER_READER) ){ | |
| 38626 | 39230 | assert( pPager->pInJournal==0 ); |
| 38627 | - assert( !MEMDB && !pPager->tempFile ); | |
| 38628 | 39231 | |
| 38629 | 39232 | if( pagerUseWal(pPager) ){ |
| 38630 | 39233 | /* If the pager is configured to use locking_mode=exclusive, and an |
| 38631 | 39234 | ** exclusive lock on the database is not already held, obtain it now. |
| 38632 | 39235 | */ |
| 38633 | 39236 | 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); | |
| 38636 | 39238 | if( rc!=SQLITE_OK ){ |
| 38637 | 39239 | return rc; |
| 38638 | 39240 | } |
| 38639 | 39241 | sqlite3WalExclusiveMode(pPager->pWal, 1); |
| 38640 | 39242 | } |
| @@ -38641,56 +39243,44 @@ | ||
| 38641 | 39243 | |
| 38642 | 39244 | /* Grab the write lock on the log file. If successful, upgrade to |
| 38643 | 39245 | ** PAGER_RESERVED state. Otherwise, return an error code to the caller. |
| 38644 | 39246 | ** The busy-handler is not invoked if another connection already |
| 38645 | 39247 | ** 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 | 39248 | */ |
| 38653 | 39249 | 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 | 39250 | }else{ |
| 38663 | 39251 | /* Obtain a RESERVED lock on the database file. If the exFlag parameter |
| 38664 | 39252 | ** is true, then immediately upgrade this to an EXCLUSIVE lock. The |
| 38665 | 39253 | ** busy-handler callback can be used when upgrading to the EXCLUSIVE |
| 38666 | 39254 | ** lock, but not when obtaining the RESERVED lock. |
| 38667 | 39255 | */ |
| 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) ); | |
| 38692 | 39282 | } |
| 38693 | 39283 | |
| 38694 | 39284 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
| 38695 | 39285 | return rc; |
| 38696 | 39286 | } |
| @@ -38705,110 +39295,98 @@ | ||
| 38705 | 39295 | static int pager_write(PgHdr *pPg){ |
| 38706 | 39296 | void *pData = pPg->pData; |
| 38707 | 39297 | Pager *pPager = pPg->pPager; |
| 38708 | 39298 | int rc = SQLITE_OK; |
| 38709 | 39299 | |
| 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. | |
| 38712 | 39303 | */ |
| 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) ); | |
| 38714 | 39309 | |
| 38715 | 39310 | /* 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. */ | |
| 38718 | 39312 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38719 | 39313 | |
| 38720 | 39314 | /* Higher-level routines never call this function if database is not |
| 38721 | 39315 | ** writable. But check anyway, just for robustness. */ |
| 38722 | 39316 | if( NEVER(pPager->readOnly) ) return SQLITE_PERM; |
| 38723 | 39317 | |
| 38724 | - assert( !pPager->setMaster ); | |
| 38725 | - | |
| 38726 | 39318 | CHECK_PAGE(pPg); |
| 38727 | 39319 | |
| 38728 | 39320 | /* Mark the page as dirty. If the page has already been written |
| 38729 | 39321 | ** to the journal then we can return right away. |
| 38730 | 39322 | */ |
| 38731 | 39323 | sqlite3PcacheMakeDirty(pPg); |
| 38732 | 39324 | if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ |
| 38733 | 39325 | assert( !pagerUseWal(pPager) ); |
| 38734 | - pPager->dbModified = 1; | |
| 39326 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); | |
| 38735 | 39327 | }else{ |
| 38736 | 39328 | |
| 38737 | 39329 | /* 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 | |
| 38739 | 39331 | ** or both. |
| 38740 | 39332 | ** |
| 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. | |
| 38744 | 39336 | */ |
| 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 ){ | |
| 38751 | 39338 | rc = pager_open_journal(pPager); |
| 38752 | 39339 | if( rc!=SQLITE_OK ) return rc; |
| 38753 | 39340 | } |
| 38754 | - pPager->dbModified = 1; | |
| 39341 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); | |
| 39342 | + assert( assert_pager_state(pPager) ); | |
| 38755 | 39343 | |
| 38756 | 39344 | /* The transaction journal now exists and we have a RESERVED or an |
| 38757 | 39345 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 38758 | 39346 | ** the transaction journal if it is not there already. |
| 38759 | 39347 | */ |
| 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) ){ | |
| 38763 | 39351 | u32 cksum; |
| 38764 | 39352 | char *pData2; |
| 39353 | + i64 iOff = pPager->journalOff; | |
| 38765 | 39354 | |
| 38766 | 39355 | /* We should never write to the journal file the page that |
| 38767 | 39356 | ** contains the database locks. The following assert verifies |
| 38768 | 39357 | ** that we do not. */ |
| 38769 | 39358 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 38770 | 39359 | |
| 38771 | - assert( pPager->journalHdr <= pPager->journalOff ); | |
| 39360 | + assert( pPager->journalHdr<=pPager->journalOff ); | |
| 38772 | 39361 | CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); |
| 38773 | 39362 | 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 | + | |
| 38784 | 39380 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 38785 | 39381 | pPager->journalOff, pPager->pageSize)); |
| 38786 | 39382 | PAGER_INCR(sqlite3_pager_writej_count); |
| 38787 | 39383 | PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 38788 | 39384 | PAGERID(pPager), pPg->pgno, |
| 38789 | 39385 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); |
| 38790 | 39386 | |
| 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; | |
| 38810 | 39388 | pPager->nRec++; |
| 38811 | 39389 | assert( pPager->pInJournal!=0 ); |
| 38812 | 39390 | rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 38813 | 39391 | testcase( rc==SQLITE_NOMEM ); |
| 38814 | 39392 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| @@ -38816,13 +39394,12 @@ | ||
| 38816 | 39394 | if( rc!=SQLITE_OK ){ |
| 38817 | 39395 | assert( rc==SQLITE_NOMEM ); |
| 38818 | 39396 | return rc; |
| 38819 | 39397 | } |
| 38820 | 39398 | }else{ |
| 38821 | - if( !pPager->journalStarted && !pPager->noSync ){ | |
| 39399 | + if( pPager->eState!=PAGER_WRITER_DBMOD ){ | |
| 38822 | 39400 | pPg->flags |= PGHDR_NEED_SYNC; |
| 38823 | - pPager->needSync = 1; | |
| 38824 | 39401 | } |
| 38825 | 39402 | PAGERTRACE(("APPEND %d page %d needSync=%d\n", |
| 38826 | 39403 | PAGERID(pPager), pPg->pgno, |
| 38827 | 39404 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); |
| 38828 | 39405 | } |
| @@ -38838,11 +39415,10 @@ | ||
| 38838 | 39415 | } |
| 38839 | 39416 | } |
| 38840 | 39417 | |
| 38841 | 39418 | /* Update the database size and return. |
| 38842 | 39419 | */ |
| 38843 | - assert( pPager->state>=PAGER_SHARED ); | |
| 38844 | 39420 | if( pPager->dbSize<pPg->pgno ){ |
| 38845 | 39421 | pPager->dbSize = pPg->pgno; |
| 38846 | 39422 | } |
| 38847 | 39423 | return rc; |
| 38848 | 39424 | } |
| @@ -38866,10 +39442,14 @@ | ||
| 38866 | 39442 | |
| 38867 | 39443 | PgHdr *pPg = pDbPage; |
| 38868 | 39444 | Pager *pPager = pPg->pPager; |
| 38869 | 39445 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 38870 | 39446 | |
| 39447 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); | |
| 39448 | + assert( pPager->eState!=PAGER_ERROR ); | |
| 39449 | + assert( assert_pager_state(pPager) ); | |
| 39450 | + | |
| 38871 | 39451 | if( nPagePerSector>1 ){ |
| 38872 | 39452 | Pgno nPageCount; /* Total number of pages in database file */ |
| 38873 | 39453 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 38874 | 39454 | int nPage = 0; /* Number of pages starting at pg1 to journal */ |
| 38875 | 39455 | int ii; /* Loop counter */ |
| @@ -38887,23 +39467,21 @@ | ||
| 38887 | 39467 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 38888 | 39468 | ** of the first page of the sector pPg is located on. |
| 38889 | 39469 | */ |
| 38890 | 39470 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 38891 | 39471 | |
| 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); | |
| 38905 | 39483 | |
| 38906 | 39484 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 38907 | 39485 | Pgno pg = pg1+ii; |
| 38908 | 39486 | PgHdr *pPage; |
| 38909 | 39487 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
| @@ -38911,11 +39489,10 @@ | ||
| 38911 | 39489 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
| 38912 | 39490 | if( rc==SQLITE_OK ){ |
| 38913 | 39491 | rc = pager_write(pPage); |
| 38914 | 39492 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
| 38915 | 39493 | needSync = 1; |
| 38916 | - assert(pPager->needSync); | |
| 38917 | 39494 | } |
| 38918 | 39495 | sqlite3PagerUnref(pPage); |
| 38919 | 39496 | } |
| 38920 | 39497 | } |
| 38921 | 39498 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
| @@ -38931,19 +39508,18 @@ | ||
| 38931 | 39508 | ** writing to any of these nPage pages may damage the others, the |
| 38932 | 39509 | ** journal file must contain sync()ed copies of all of them |
| 38933 | 39510 | ** before any of them can be written out to the database file. |
| 38934 | 39511 | */ |
| 38935 | 39512 | if( rc==SQLITE_OK && needSync ){ |
| 38936 | - assert( !MEMDB && pPager->noSync==0 ); | |
| 39513 | + assert( !MEMDB ); | |
| 38937 | 39514 | for(ii=0; ii<nPage; ii++){ |
| 38938 | 39515 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
| 38939 | 39516 | if( pPage ){ |
| 38940 | 39517 | pPage->flags |= PGHDR_NEED_SYNC; |
| 38941 | 39518 | sqlite3PagerUnref(pPage); |
| 38942 | 39519 | } |
| 38943 | 39520 | } |
| 38944 | - assert(pPager->needSync); | |
| 38945 | 39521 | } |
| 38946 | 39522 | |
| 38947 | 39523 | assert( pPager->doNotSyncSpill==1 ); |
| 38948 | 39524 | pPager->doNotSyncSpill--; |
| 38949 | 39525 | }else{ |
| @@ -39005,10 +39581,15 @@ | ||
| 39005 | 39581 | ** by writing an updated version of page 1 using a call to the |
| 39006 | 39582 | ** sqlite3OsWrite() function. |
| 39007 | 39583 | */ |
| 39008 | 39584 | static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ |
| 39009 | 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) ); | |
| 39010 | 39591 | |
| 39011 | 39592 | /* Declare and initialize constant integer 'isDirect'. If the |
| 39012 | 39593 | ** atomic-write optimization is enabled in this build, then isDirect |
| 39013 | 39594 | ** is initialized to the value passed as the isDirectMode parameter |
| 39014 | 39595 | ** to this function. Otherwise, it is always set to zero. |
| @@ -39024,11 +39605,10 @@ | ||
| 39024 | 39605 | UNUSED_PARAMETER(isDirectMode); |
| 39025 | 39606 | #else |
| 39026 | 39607 | # define DIRECT_MODE isDirectMode |
| 39027 | 39608 | #endif |
| 39028 | 39609 | |
| 39029 | - assert( pPager->state>=PAGER_RESERVED ); | |
| 39030 | 39610 | if( !pPager->changeCountDone && pPager->dbSize>0 ){ |
| 39031 | 39611 | PgHdr *pPgHdr; /* Reference to page 1 */ |
| 39032 | 39612 | u32 change_counter; /* Initial value of change-counter field */ |
| 39033 | 39613 | |
| 39034 | 39614 | assert( !pPager->tempFile && isOpen(pPager->fd) ); |
| @@ -39109,13 +39689,17 @@ | ||
| 39109 | 39689 | ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is |
| 39110 | 39690 | ** returned. |
| 39111 | 39691 | */ |
| 39112 | 39692 | SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){ |
| 39113 | 39693 | 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) ); | |
| 39115 | 39699 | if( 0==pagerUseWal(pPager) ){ |
| 39116 | - rc = pager_wait_on_lock(pPager, PAGER_EXCLUSIVE); | |
| 39700 | + rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); | |
| 39117 | 39701 | } |
| 39118 | 39702 | return rc; |
| 39119 | 39703 | } |
| 39120 | 39704 | |
| 39121 | 39705 | /* |
| @@ -39149,26 +39733,33 @@ | ||
| 39149 | 39733 | const char *zMaster, /* If not NULL, the master journal name */ |
| 39150 | 39734 | int noSync /* True to omit the xSync on the db file */ |
| 39151 | 39735 | ){ |
| 39152 | 39736 | int rc = SQLITE_OK; /* Return code */ |
| 39153 | 39737 | |
| 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) ); | |
| 39156 | 39744 | |
| 39157 | 39745 | /* If a prior error occurred, report that error again. */ |
| 39158 | - if( pPager->errCode ) return pPager->errCode; | |
| 39746 | + if( NEVER(pPager->errCode) ) return pPager->errCode; | |
| 39159 | 39747 | |
| 39160 | 39748 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 39161 | 39749 | pPager->zFilename, zMaster, pPager->dbSize)); |
| 39162 | 39750 | |
| 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 ){ | |
| 39164 | 39755 | /* If this is an in-memory db, or no pages have been written to, or this |
| 39165 | 39756 | ** function has already been called, it is mostly a no-op. However, any |
| 39166 | 39757 | ** backup in progress needs to be restarted. |
| 39167 | 39758 | */ |
| 39168 | 39759 | sqlite3BackupRestart(pPager->pBackup); |
| 39169 | - }else if( pPager->dbModified ){ | |
| 39760 | + }else{ | |
| 39170 | 39761 | if( pagerUseWal(pPager) ){ |
| 39171 | 39762 | PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); |
| 39172 | 39763 | if( pList ){ |
| 39173 | 39764 | rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, |
| 39174 | 39765 | (pPager->fullSync ? pPager->sync_flags : 0) |
| @@ -39207,11 +39798,11 @@ | ||
| 39207 | 39798 | || pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 39208 | 39799 | || pPager->journalMode==PAGER_JOURNALMODE_WAL |
| 39209 | 39800 | ); |
| 39210 | 39801 | if( !zMaster && isOpen(pPager->jfd) |
| 39211 | 39802 | && pPager->journalOff==jrnlBufferSize(pPager) |
| 39212 | - && pPager->dbSize>=pPager->dbFileSize | |
| 39803 | + && pPager->dbSize>=pPager->dbOrigSize | |
| 39213 | 39804 | && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) |
| 39214 | 39805 | ){ |
| 39215 | 39806 | /* Update the db file change counter via the direct-write method. The |
| 39216 | 39807 | ** following call will modify the in-memory representation of page 1 |
| 39217 | 39808 | ** to include the updated change counter and then write page 1 |
| @@ -39237,17 +39828,14 @@ | ||
| 39237 | 39828 | ** Before reading the pages with page numbers larger than the |
| 39238 | 39829 | ** current value of Pager.dbSize, set dbSize back to the value |
| 39239 | 39830 | ** that it took at the start of the transaction. Otherwise, the |
| 39240 | 39831 | ** calls to sqlite3PagerGet() return zeroed pages instead of |
| 39241 | 39832 | ** 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 | 39833 | */ |
| 39246 | 39834 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 39247 | 39835 | if( pPager->dbSize<pPager->dbOrigSize |
| 39248 | - && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF) | |
| 39836 | + && pPager->journalMode!=PAGER_JOURNALMODE_OFF | |
| 39249 | 39837 | ){ |
| 39250 | 39838 | Pgno i; /* Iterator variable */ |
| 39251 | 39839 | const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */ |
| 39252 | 39840 | const Pgno dbSize = pPager->dbSize; /* Database image size */ |
| 39253 | 39841 | pPager->dbSize = pPager->dbOrigSize; |
| @@ -39270,18 +39858,24 @@ | ||
| 39270 | 39858 | ** or if zMaster is NULL (no master journal), then this call is a no-op. |
| 39271 | 39859 | */ |
| 39272 | 39860 | rc = writeMasterJournal(pPager, zMaster); |
| 39273 | 39861 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39274 | 39862 | |
| 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. | |
| 39278 | 39873 | */ |
| 39279 | - rc = syncJournal(pPager); | |
| 39874 | + rc = syncJournal(pPager, 0); | |
| 39280 | 39875 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39281 | 39876 | |
| 39282 | - /* Write all dirty pages to the database file. */ | |
| 39283 | 39877 | rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); |
| 39284 | 39878 | if( rc!=SQLITE_OK ){ |
| 39285 | 39879 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 39286 | 39880 | goto commit_phase_one_exit; |
| 39287 | 39881 | } |
| @@ -39290,11 +39884,11 @@ | ||
| 39290 | 39884 | /* If the file on disk is not the same size as the database image, |
| 39291 | 39885 | ** then use pager_truncate to grow or shrink the file here. |
| 39292 | 39886 | */ |
| 39293 | 39887 | if( pPager->dbSize!=pPager->dbFileSize ){ |
| 39294 | 39888 | Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); |
| 39295 | - assert( pPager->state>=PAGER_EXCLUSIVE ); | |
| 39889 | + assert( pPager->eState==PAGER_WRITER_DBMOD ); | |
| 39296 | 39890 | rc = pager_truncate(pPager, nNew); |
| 39297 | 39891 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39298 | 39892 | } |
| 39299 | 39893 | |
| 39300 | 39894 | /* Finally, sync the database file. */ |
| @@ -39301,16 +39895,16 @@ | ||
| 39301 | 39895 | if( !pPager->noSync && !noSync ){ |
| 39302 | 39896 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 39303 | 39897 | } |
| 39304 | 39898 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 39305 | 39899 | } |
| 39306 | - | |
| 39307 | - assert( pPager->state!=PAGER_SYNCED ); | |
| 39308 | - pPager->state = PAGER_SYNCED; | |
| 39309 | 39900 | } |
| 39310 | 39901 | |
| 39311 | 39902 | commit_phase_one_exit: |
| 39903 | + if( rc==SQLITE_OK && !pagerUseWal(pPager) ){ | |
| 39904 | + pPager->eState = PAGER_WRITER_FINISHED; | |
| 39905 | + } | |
| 39312 | 39906 | return rc; |
| 39313 | 39907 | } |
| 39314 | 39908 | |
| 39315 | 39909 | |
| 39316 | 39910 | /* |
| @@ -39334,16 +39928,15 @@ | ||
| 39334 | 39928 | /* This routine should not be called if a prior error has occurred. |
| 39335 | 39929 | ** But if (due to a coding error elsewhere in the system) it does get |
| 39336 | 39930 | ** called, just return the same error code without doing anything. */ |
| 39337 | 39931 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 39338 | 39932 | |
| 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) ); | |
| 39345 | 39938 | |
| 39346 | 39939 | /* An optimization. If the database was not actually modified during |
| 39347 | 39940 | ** this transaction, the pager is running in exclusive-mode and is |
| 39348 | 39941 | ** using persistent journals, then this function is a no-op. |
| 39349 | 39942 | ** |
| @@ -39352,106 +39945,80 @@ | ||
| 39352 | 39945 | ** a hot-journal during hot-journal rollback, 0 changes will be made |
| 39353 | 39946 | ** to the database file. So there is no need to zero the journal |
| 39354 | 39947 | ** header. Since the pager is in exclusive mode, there is no need |
| 39355 | 39948 | ** to drop any locks either. |
| 39356 | 39949 | */ |
| 39357 | - if( pPager->dbModified==0 && pPager->exclusiveMode | |
| 39950 | + if( pPager->eState==PAGER_WRITER_LOCKED | |
| 39951 | + && pPager->exclusiveMode | |
| 39358 | 39952 | && pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 39359 | 39953 | ){ |
| 39360 | 39954 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff ); |
| 39955 | + pPager->eState = PAGER_READER; | |
| 39361 | 39956 | return SQLITE_OK; |
| 39362 | 39957 | } |
| 39363 | 39958 | |
| 39364 | 39959 | PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); |
| 39365 | - assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified ); | |
| 39366 | 39960 | rc = pager_end_transaction(pPager, pPager->setMaster); |
| 39367 | 39961 | return pager_error(pPager, rc); |
| 39368 | 39962 | } |
| 39369 | 39963 | |
| 39370 | 39964 | /* |
| 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. | |
| 39372 | 39969 | ** |
| 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: | |
| 39374 | 39974 | ** |
| 39375 | 39975 | ** 1) It rolls back the journal file, restoring all database file and |
| 39376 | 39976 | ** in-memory cache pages to the state they were in when the transaction |
| 39377 | 39977 | ** was opened, and |
| 39978 | +** | |
| 39378 | 39979 | ** 2) It finalizes the journal file, so that it is not used for hot |
| 39379 | 39980 | ** rollback at any point in the future. |
| 39380 | 39981 | ** |
| 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. | |
| 39412 | 39989 | */ |
| 39413 | 39990 | SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){ |
| 39414 | 39991 | int rc = SQLITE_OK; /* Return code */ |
| 39415 | 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 | + | |
| 39416 | 40002 | if( pagerUseWal(pPager) ){ |
| 39417 | 40003 | int rc2; |
| 39418 | - | |
| 39419 | 40004 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); |
| 39420 | 40005 | rc2 = pager_end_transaction(pPager, pPager->setMaster); |
| 39421 | 40006 | 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); | |
| 39453 | 40020 | } |
| 39454 | 40021 | |
| 39455 | 40022 | /* |
| 39456 | 40023 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 39457 | 40024 | ** if the database is (in theory) writable. |
| @@ -39493,12 +40060,12 @@ | ||
| 39493 | 40060 | SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){ |
| 39494 | 40061 | static int a[11]; |
| 39495 | 40062 | a[0] = sqlite3PcacheRefCount(pPager->pPCache); |
| 39496 | 40063 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 39497 | 40064 | 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; | |
| 39500 | 40067 | a[5] = pPager->errCode; |
| 39501 | 40068 | a[6] = pPager->nHit; |
| 39502 | 40069 | a[7] = pPager->nMiss; |
| 39503 | 40070 | a[8] = 0; /* Used to be pPager->nOvfl */ |
| 39504 | 40071 | a[9] = pPager->nRead; |
| @@ -39525,18 +40092,17 @@ | ||
| 39525 | 40092 | ** returned. Otherwise, SQLITE_OK. |
| 39526 | 40093 | */ |
| 39527 | 40094 | SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 39528 | 40095 | int rc = SQLITE_OK; /* Return code */ |
| 39529 | 40096 | int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ |
| 40097 | + | |
| 40098 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); | |
| 40099 | + assert( assert_pager_state(pPager) ); | |
| 39530 | 40100 | |
| 39531 | 40101 | if( nSavepoint>nCurrent && pPager->useJournal ){ |
| 39532 | 40102 | int ii; /* Iterator variable */ |
| 39533 | 40103 | 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 | 40104 | |
| 39539 | 40105 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 39540 | 40106 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 39541 | 40107 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 39542 | 40108 | */ |
| @@ -39549,18 +40115,18 @@ | ||
| 39549 | 40115 | memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); |
| 39550 | 40116 | pPager->aSavepoint = aNew; |
| 39551 | 40117 | |
| 39552 | 40118 | /* Populate the PagerSavepoint structures just allocated. */ |
| 39553 | 40119 | for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 39554 | - aNew[ii].nOrig = nPage; | |
| 40120 | + aNew[ii].nOrig = pPager->dbSize; | |
| 39555 | 40121 | if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 39556 | 40122 | aNew[ii].iOffset = pPager->journalOff; |
| 39557 | 40123 | }else{ |
| 39558 | 40124 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 39559 | 40125 | } |
| 39560 | 40126 | aNew[ii].iSubRec = pPager->nSubRec; |
| 39561 | - aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage); | |
| 40127 | + aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); | |
| 39562 | 40128 | if( !aNew[ii].pInSavepoint ){ |
| 39563 | 40129 | return SQLITE_NOMEM; |
| 39564 | 40130 | } |
| 39565 | 40131 | if( pagerUseWal(pPager) ){ |
| 39566 | 40132 | sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); |
| @@ -39603,16 +40169,16 @@ | ||
| 39603 | 40169 | ** This function may return SQLITE_NOMEM if a memory allocation fails, |
| 39604 | 40170 | ** or an IO error code if an IO error occurs while rolling back a |
| 39605 | 40171 | ** savepoint. If no errors occur, SQLITE_OK is returned. |
| 39606 | 40172 | */ |
| 39607 | 40173 | SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ |
| 39608 | - int rc = SQLITE_OK; | |
| 40174 | + int rc = pPager->errCode; /* Return code */ | |
| 39609 | 40175 | |
| 39610 | 40176 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 39611 | 40177 | assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); |
| 39612 | 40178 | |
| 39613 | - if( iSavepoint<pPager->nSavepoint ){ | |
| 40179 | + if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){ | |
| 39614 | 40180 | int ii; /* Iterator variable */ |
| 39615 | 40181 | int nNew; /* Number of remaining savepoints after this op. */ |
| 39616 | 40182 | |
| 39617 | 40183 | /* Figure out how many savepoints will still be active after this |
| 39618 | 40184 | ** operation. Store this value in nNew. Then free resources associated |
| @@ -39644,12 +40210,12 @@ | ||
| 39644 | 40210 | else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){ |
| 39645 | 40211 | PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; |
| 39646 | 40212 | rc = pagerPlaybackSavepoint(pPager, pSavepoint); |
| 39647 | 40213 | assert(rc!=SQLITE_DONE); |
| 39648 | 40214 | } |
| 39649 | - | |
| 39650 | 40215 | } |
| 40216 | + | |
| 39651 | 40217 | return rc; |
| 39652 | 40218 | } |
| 39653 | 40219 | |
| 39654 | 40220 | /* |
| 39655 | 40221 | ** Return the full pathname of the database file. |
| @@ -39743,10 +40309,14 @@ | ||
| 39743 | 40309 | Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */ |
| 39744 | 40310 | int rc; /* Return code */ |
| 39745 | 40311 | Pgno origPgno; /* The original page number */ |
| 39746 | 40312 | |
| 39747 | 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) ); | |
| 39748 | 40318 | |
| 39749 | 40319 | /* In order to be able to rollback, an in-memory database must journal |
| 39750 | 40320 | ** the page we are moving from. |
| 39751 | 40321 | */ |
| 39752 | 40322 | if( MEMDB ){ |
| @@ -39792,15 +40362,14 @@ | ||
| 39792 | 40362 | */ |
| 39793 | 40363 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
| 39794 | 40364 | needSyncPgno = pPg->pgno; |
| 39795 | 40365 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
| 39796 | 40366 | assert( pPg->flags&PGHDR_DIRTY ); |
| 39797 | - assert( pPager->needSync ); | |
| 39798 | 40367 | } |
| 39799 | 40368 | |
| 39800 | 40369 | /* 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 | |
| 39802 | 40371 | ** page pgno before the 'move' operation, it needs to be retained |
| 39803 | 40372 | ** for the page moved there. |
| 39804 | 40373 | */ |
| 39805 | 40374 | pPg->flags &= ~PGHDR_NEED_SYNC; |
| 39806 | 40375 | pPgOld = pager_lookup(pPager, pgno); |
| @@ -39808,67 +40377,59 @@ | ||
| 39808 | 40377 | if( pPgOld ){ |
| 39809 | 40378 | pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
| 39810 | 40379 | if( MEMDB ){ |
| 39811 | 40380 | /* Do not discard pages from an in-memory database since we might |
| 39812 | 40381 | ** need to rollback later. Just move the page out of the way. */ |
| 39813 | - assert( pPager->dbSizeValid ); | |
| 39814 | 40382 | sqlite3PcacheMove(pPgOld, pPager->dbSize+1); |
| 39815 | 40383 | }else{ |
| 39816 | 40384 | sqlite3PcacheDrop(pPgOld); |
| 39817 | 40385 | } |
| 39818 | 40386 | } |
| 39819 | 40387 | |
| 39820 | 40388 | origPgno = pPg->pgno; |
| 39821 | 40389 | sqlite3PcacheMove(pPg, pgno); |
| 39822 | 40390 | 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 | + } | |
| 39824 | 40401 | |
| 39825 | 40402 | if( needSyncPgno ){ |
| 39826 | 40403 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 39827 | 40404 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 39828 | 40405 | ** Currently, no such page exists in the page-cache and the |
| 39829 | 40406 | ** "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 | |
| 39831 | 40408 | ** flag. |
| 39832 | 40409 | ** |
| 39833 | 40410 | ** If the attempt to load the page into the page-cache fails, (due |
| 39834 | 40411 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
| 39835 | 40412 | ** array. Otherwise, if the page is loaded and written again in |
| 39836 | 40413 | ** this transaction, it may be written to the database file before |
| 39837 | 40414 | ** it is synced into the journal file. This way, it may end up in |
| 39838 | 40415 | ** 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 | 40416 | */ |
| 39843 | 40417 | PgHdr *pPgHdr; |
| 39844 | - assert( pPager->needSync ); | |
| 39845 | 40418 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
| 39846 | 40419 | if( rc!=SQLITE_OK ){ |
| 39847 | 40420 | if( needSyncPgno<=pPager->dbOrigSize ){ |
| 39848 | 40421 | assert( pPager->pTmpSpace!=0 ); |
| 39849 | 40422 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace); |
| 39850 | 40423 | } |
| 39851 | 40424 | return rc; |
| 39852 | 40425 | } |
| 39853 | - pPager->needSync = 1; | |
| 39854 | - assert( pPager->noSync==0 && !MEMDB ); | |
| 39855 | 40426 | pPgHdr->flags |= PGHDR_NEED_SYNC; |
| 39856 | 40427 | sqlite3PcacheMakeDirty(pPgHdr); |
| 39857 | 40428 | sqlite3PagerUnref(pPgHdr); |
| 39858 | 40429 | } |
| 39859 | 40430 | |
| 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 | 40431 | return SQLITE_OK; |
| 39871 | 40432 | } |
| 39872 | 40433 | #endif |
| 39873 | 40434 | |
| 39874 | 40435 | /* |
| @@ -39929,10 +40490,17 @@ | ||
| 39929 | 40490 | ** |
| 39930 | 40491 | ** The returned indicate the current (possibly updated) journal-mode. |
| 39931 | 40492 | */ |
| 39932 | 40493 | SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ |
| 39933 | 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 | + | |
| 39934 | 40502 | |
| 39935 | 40503 | /* The eMode parameter is always valid */ |
| 39936 | 40504 | assert( eMode==PAGER_JOURNALMODE_DELETE |
| 39937 | 40505 | || eMode==PAGER_JOURNALMODE_TRUNCATE |
| 39938 | 40506 | || eMode==PAGER_JOURNALMODE_PERSIST |
| @@ -39955,24 +40523,17 @@ | ||
| 39955 | 40523 | eMode = eOld; |
| 39956 | 40524 | } |
| 39957 | 40525 | } |
| 39958 | 40526 | |
| 39959 | 40527 | 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 | 40528 | |
| 39969 | 40529 | /* Change the journal mode. */ |
| 40530 | + assert( pPager->eState!=PAGER_ERROR ); | |
| 39970 | 40531 | pPager->journalMode = (u8)eMode; |
| 39971 | 40532 | |
| 39972 | 40533 | /* 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, | |
| 39974 | 40535 | ** delete the journal file. |
| 39975 | 40536 | */ |
| 39976 | 40537 | assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); |
| 39977 | 40538 | assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); |
| 39978 | 40539 | assert( (PAGER_JOURNALMODE_DELETE & 5)==0 ); |
| @@ -39989,28 +40550,34 @@ | ||
| 39989 | 40550 | ** |
| 39990 | 40551 | ** Before deleting the journal file, obtain a RESERVED lock on the |
| 39991 | 40552 | ** database file. This ensures that the journal file is not deleted |
| 39992 | 40553 | ** while it is in use by some other client. |
| 39993 | 40554 | */ |
| 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 ){ | |
| 40004 | 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 ); | |
| 40005 | 40578 | } |
| 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 | 40579 | } |
| 40013 | 40580 | } |
| 40014 | 40581 | |
| 40015 | 40582 | /* Return the new journal mode */ |
| 40016 | 40583 | return (int)pPager->journalMode; |
| @@ -40027,11 +40594,12 @@ | ||
| 40027 | 40594 | ** Return TRUE if the pager is in a state where it is OK to change the |
| 40028 | 40595 | ** journalmode. Journalmode changes can only happen when the database |
| 40029 | 40596 | ** is unmodified. |
| 40030 | 40597 | */ |
| 40031 | 40598 | 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; | |
| 40033 | 40601 | if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0; |
| 40034 | 40602 | return 1; |
| 40035 | 40603 | } |
| 40036 | 40604 | |
| 40037 | 40605 | /* |
| @@ -40092,39 +40660,46 @@ | ||
| 40092 | 40660 | ** |
| 40093 | 40661 | ** If the pager passed as the first argument is open on a real database |
| 40094 | 40662 | ** file (not a temp file or an in-memory database), and the WAL file |
| 40095 | 40663 | ** is not already open, make an attempt to open it now. If successful, |
| 40096 | 40664 | ** 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 | |
| 40098 | 40666 | ** not modified in either case. |
| 40099 | 40667 | ** |
| 40100 | 40668 | ** 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 | |
| 40102 | 40670 | ** without doing anything. |
| 40103 | 40671 | */ |
| 40104 | 40672 | SQLITE_PRIVATE int sqlite3PagerOpenWal( |
| 40105 | 40673 | 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 */ | |
| 40107 | 40675 | ){ |
| 40108 | 40676 | int rc = SQLITE_OK; /* Return code */ |
| 40109 | 40677 | |
| 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) ); | |
| 40112 | 40683 | |
| 40113 | 40684 | if( !pPager->tempFile && !pPager->pWal ){ |
| 40114 | 40685 | if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN; |
| 40686 | + | |
| 40687 | + /* Close any rollback journal previously open */ | |
| 40688 | + sqlite3OsClose(pPager->jfd); | |
| 40115 | 40689 | |
| 40116 | 40690 | /* Open the connection to the log file. If this operation fails, |
| 40117 | 40691 | ** (e.g. due to malloc() failure), unlock the database file and |
| 40118 | 40692 | ** return an error code. |
| 40119 | 40693 | */ |
| 40120 | 40694 | rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal); |
| 40121 | 40695 | if( rc==SQLITE_OK ){ |
| 40122 | 40696 | pPager->journalMode = PAGER_JOURNALMODE_WAL; |
| 40697 | + pPager->eState = PAGER_OPEN; | |
| 40123 | 40698 | } |
| 40124 | 40699 | }else{ |
| 40125 | - *pisOpen = 1; | |
| 40700 | + *pbOpen = 1; | |
| 40126 | 40701 | } |
| 40127 | 40702 | |
| 40128 | 40703 | return rc; |
| 40129 | 40704 | } |
| 40130 | 40705 | |
| @@ -40146,11 +40721,11 @@ | ||
| 40146 | 40721 | ** it may need to be checkpointed before the connection can switch to |
| 40147 | 40722 | ** rollback mode. Open it now so this can happen. |
| 40148 | 40723 | */ |
| 40149 | 40724 | if( !pPager->pWal ){ |
| 40150 | 40725 | int logexists = 0; |
| 40151 | - rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED); | |
| 40726 | + rc = pagerLockDb(pPager, SHARED_LOCK); | |
| 40152 | 40727 | if( rc==SQLITE_OK ){ |
| 40153 | 40728 | rc = sqlite3OsAccess( |
| 40154 | 40729 | pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists |
| 40155 | 40730 | ); |
| 40156 | 40731 | } |
| @@ -40162,21 +40737,21 @@ | ||
| 40162 | 40737 | |
| 40163 | 40738 | /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on |
| 40164 | 40739 | ** the database file, the log and log-summary files will be deleted. |
| 40165 | 40740 | */ |
| 40166 | 40741 | if( rc==SQLITE_OK && pPager->pWal ){ |
| 40167 | - rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE); | |
| 40742 | + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); | |
| 40168 | 40743 | if( rc==SQLITE_OK ){ |
| 40169 | 40744 | rc = sqlite3WalClose(pPager->pWal, |
| 40170 | 40745 | (pPager->noSync ? 0 : pPager->sync_flags), |
| 40171 | 40746 | pPager->pageSize, (u8*)pPager->pTmpSpace |
| 40172 | 40747 | ); |
| 40173 | 40748 | pPager->pWal = 0; |
| 40174 | 40749 | }else{ |
| 40175 | 40750 | /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock |
| 40176 | 40751 | ** that we did get back to SHARED. */ |
| 40177 | - sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED); | |
| 40752 | + pagerUnlockDb(pPager, SQLITE_LOCK_SHARED); | |
| 40178 | 40753 | } |
| 40179 | 40754 | } |
| 40180 | 40755 | return rc; |
| 40181 | 40756 | } |
| 40182 | 40757 | |
| @@ -40492,18 +41067,22 @@ | ||
| 40492 | 41067 | /* |
| 40493 | 41068 | ** The following object holds a copy of the wal-index header content. |
| 40494 | 41069 | ** |
| 40495 | 41070 | ** The actual header in the wal-index consists of two copies of this |
| 40496 | 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. | |
| 40497 | 41076 | */ |
| 40498 | 41077 | struct WalIndexHdr { |
| 40499 | 41078 | u32 iVersion; /* Wal-index version */ |
| 40500 | 41079 | u32 unused; /* Unused (padding) field */ |
| 40501 | 41080 | u32 iChange; /* Counter incremented each transaction */ |
| 40502 | 41081 | u8 isInit; /* 1 when initialized */ |
| 40503 | 41082 | 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 */ | |
| 40505 | 41084 | u32 mxFrame; /* Index of last valid frame in the WAL */ |
| 40506 | 41085 | u32 nPage; /* Size of database in pages */ |
| 40507 | 41086 | u32 aFrameCksum[2]; /* Checksum of last frame in log */ |
| 40508 | 41087 | u32 aSalt[2]; /* Two salt values copied from WAL header */ |
| 40509 | 41088 | u32 aCksum[2]; /* Checksum over all prior fields */ |
| @@ -40610,11 +41189,11 @@ | ||
| 40610 | 41189 | sqlite3_file *pDbFd; /* File handle for the database file */ |
| 40611 | 41190 | sqlite3_file *pWalFd; /* File handle for WAL file */ |
| 40612 | 41191 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
| 40613 | 41192 | int nWiData; /* Size of array apWiData */ |
| 40614 | 41193 | volatile u32 **apWiData; /* Pointer to wal-index content in memory */ |
| 40615 | - u16 szPage; /* Database page size */ | |
| 41194 | + u32 szPage; /* Database page size */ | |
| 40616 | 41195 | i16 readLock; /* Which read lock is being held. -1 for none */ |
| 40617 | 41196 | u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ |
| 40618 | 41197 | u8 writeLock; /* True if in a write transaction */ |
| 40619 | 41198 | u8 ckptLock; /* True if holding a checkpoint lock */ |
| 40620 | 41199 | u8 readOnly; /* True if the WAL file is open read-only */ |
| @@ -41281,11 +41860,11 @@ | ||
| 41281 | 41860 | || szPage<512 |
| 41282 | 41861 | ){ |
| 41283 | 41862 | goto finished; |
| 41284 | 41863 | } |
| 41285 | 41864 | pWal->hdr.bigEndCksum = (u8)(magic&0x00000001); |
| 41286 | - pWal->szPage = (u16)szPage; | |
| 41865 | + pWal->szPage = szPage; | |
| 41287 | 41866 | pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); |
| 41288 | 41867 | memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); |
| 41289 | 41868 | |
| 41290 | 41869 | /* Verify that the WAL header checksum is correct */ |
| 41291 | 41870 | walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, |
| @@ -41331,11 +41910,13 @@ | ||
| 41331 | 41910 | |
| 41332 | 41911 | /* If nTruncate is non-zero, this is a commit record. */ |
| 41333 | 41912 | if( nTruncate ){ |
| 41334 | 41913 | pWal->hdr.mxFrame = iFrame; |
| 41335 | 41914 | 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 ); | |
| 41337 | 41918 | aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; |
| 41338 | 41919 | aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; |
| 41339 | 41920 | } |
| 41340 | 41921 | } |
| 41341 | 41922 | |
| @@ -41356,10 +41937,21 @@ | ||
| 41356 | 41937 | */ |
| 41357 | 41938 | pInfo = walCkptInfo(pWal); |
| 41358 | 41939 | pInfo->nBackfill = 0; |
| 41359 | 41940 | pInfo->aReadMark[0] = 0; |
| 41360 | 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 | + } | |
| 41361 | 41953 | } |
| 41362 | 41954 | |
| 41363 | 41955 | recovery_error: |
| 41364 | 41956 | WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); |
| 41365 | 41957 | walUnlockExclusive(pWal, iLock, nLock); |
| @@ -41716,18 +42308,22 @@ | ||
| 41716 | 42308 | int sync_flags, /* Flags for OsSync() (or 0) */ |
| 41717 | 42309 | int nBuf, /* Size of zBuf in bytes */ |
| 41718 | 42310 | u8 *zBuf /* Temporary buffer to use */ |
| 41719 | 42311 | ){ |
| 41720 | 42312 | int rc; /* Return code */ |
| 41721 | - int szPage = pWal->hdr.szPage; /* Database page-size */ | |
| 42313 | + int szPage; /* Database page-size */ | |
| 41722 | 42314 | WalIterator *pIter = 0; /* Wal iterator context */ |
| 41723 | 42315 | u32 iDbpage = 0; /* Next database page to write */ |
| 41724 | 42316 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
| 41725 | 42317 | u32 mxSafeFrame; /* Max frame that can be backfilled */ |
| 42318 | + u32 mxPage; /* Max database page to write */ | |
| 41726 | 42319 | int i; /* Loop counter */ |
| 41727 | 42320 | volatile WalCkptInfo *pInfo; /* The checkpoint status information */ |
| 41728 | 42321 | |
| 42322 | + szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); | |
| 42323 | + testcase( szPage<=32768 ); | |
| 42324 | + testcase( szPage>=65536 ); | |
| 41729 | 42325 | if( pWal->hdr.mxFrame==0 ) return SQLITE_OK; |
| 41730 | 42326 | |
| 41731 | 42327 | /* Allocate the iterator */ |
| 41732 | 42328 | rc = walIteratorInit(pWal, &pIter); |
| 41733 | 42329 | if( rc!=SQLITE_OK ){ |
| @@ -41734,11 +42330,11 @@ | ||
| 41734 | 42330 | return rc; |
| 41735 | 42331 | } |
| 41736 | 42332 | assert( pIter ); |
| 41737 | 42333 | |
| 41738 | 42334 | /*** TODO: Move this test out to the caller. Make it an assert() here ***/ |
| 41739 | - if( pWal->hdr.szPage!=nBuf ){ | |
| 42335 | + if( szPage!=nBuf ){ | |
| 41740 | 42336 | rc = SQLITE_CORRUPT_BKPT; |
| 41741 | 42337 | goto walcheckpoint_out; |
| 41742 | 42338 | } |
| 41743 | 42339 | |
| 41744 | 42340 | /* Compute in mxSafeFrame the index of the last frame of the WAL that is |
| @@ -41745,10 +42341,11 @@ | ||
| 41745 | 42341 | ** safe to write into the database. Frames beyond mxSafeFrame might |
| 41746 | 42342 | ** overwrite database pages that are in use by active readers and thus |
| 41747 | 42343 | ** cannot be backfilled from the WAL. |
| 41748 | 42344 | */ |
| 41749 | 42345 | mxSafeFrame = pWal->hdr.mxFrame; |
| 42346 | + mxPage = pWal->hdr.nPage; | |
| 41750 | 42347 | pInfo = walCkptInfo(pWal); |
| 41751 | 42348 | for(i=1; i<WAL_NREADER; i++){ |
| 41752 | 42349 | u32 y = pInfo->aReadMark[i]; |
| 41753 | 42350 | if( mxSafeFrame>=y ){ |
| 41754 | 42351 | assert( y<=pWal->hdr.mxFrame ); |
| @@ -41765,22 +42362,34 @@ | ||
| 41765 | 42362 | } |
| 41766 | 42363 | |
| 41767 | 42364 | if( pInfo->nBackfill<mxSafeFrame |
| 41768 | 42365 | && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK |
| 41769 | 42366 | ){ |
| 42367 | + i64 nSize; /* Current size of database file */ | |
| 41770 | 42368 | u32 nBackfill = pInfo->nBackfill; |
| 41771 | 42369 | |
| 41772 | 42370 | /* Sync the WAL to disk */ |
| 41773 | 42371 | if( sync_flags ){ |
| 41774 | 42372 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 41775 | 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 | + } | |
| 41776 | 42385 | |
| 41777 | 42386 | /* Iterate through the contents of the WAL, copying data to the db file. */ |
| 41778 | 42387 | while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
| 41779 | 42388 | i64 iOffset; |
| 41780 | 42389 | assert( walFramePgno(pWal, iFrame)==iDbpage ); |
| 41781 | - if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue; | |
| 42390 | + if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue; | |
| 41782 | 42391 | iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE; |
| 41783 | 42392 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ |
| 41784 | 42393 | rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset); |
| 41785 | 42394 | if( rc!=SQLITE_OK ) break; |
| 41786 | 42395 | iOffset = (iDbpage-1)*(i64)szPage; |
| @@ -41883,11 +42492,11 @@ | ||
| 41883 | 42492 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
| 41884 | 42493 | |
| 41885 | 42494 | /* The first page of the wal-index must be mapped at this point. */ |
| 41886 | 42495 | assert( pWal->nWiData>0 && pWal->apWiData[0] ); |
| 41887 | 42496 | |
| 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 | |
| 41889 | 42498 | ** same area of shared memory on a different CPU in a SMP, |
| 41890 | 42499 | ** meaning it is possible that an inconsistent snapshot is read |
| 41891 | 42500 | ** from the file. If this happens, return non-zero. |
| 41892 | 42501 | ** |
| 41893 | 42502 | ** There are two copies of the header at the beginning of the wal-index. |
| @@ -41912,11 +42521,13 @@ | ||
| 41912 | 42521 | } |
| 41913 | 42522 | |
| 41914 | 42523 | if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){ |
| 41915 | 42524 | *pChanged = 1; |
| 41916 | 42525 | 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 ); | |
| 41918 | 42529 | } |
| 41919 | 42530 | |
| 41920 | 42531 | /* The header was successfully read. Return zero. */ |
| 41921 | 42532 | return 0; |
| 41922 | 42533 | } |
| @@ -42231,10 +42842,11 @@ | ||
| 42231 | 42842 | /* |
| 42232 | 42843 | ** Finish with a read transaction. All this does is release the |
| 42233 | 42844 | ** read-lock. |
| 42234 | 42845 | */ |
| 42235 | 42846 | SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){ |
| 42847 | + sqlite3WalEndWriteTransaction(pWal); | |
| 42236 | 42848 | if( pWal->readLock>=0 ){ |
| 42237 | 42849 | walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); |
| 42238 | 42850 | pWal->readLock = -1; |
| 42239 | 42851 | } |
| 42240 | 42852 | } |
| @@ -42341,11 +42953,17 @@ | ||
| 42341 | 42953 | |
| 42342 | 42954 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 42343 | 42955 | ** required page. Read and return data from the log file. |
| 42344 | 42956 | */ |
| 42345 | 42957 | 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; | |
| 42347 | 42965 | *pInWal = 1; |
| 42348 | 42966 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ |
| 42349 | 42967 | return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset); |
| 42350 | 42968 | } |
| 42351 | 42969 | |
| @@ -42353,15 +42971,17 @@ | ||
| 42353 | 42971 | return SQLITE_OK; |
| 42354 | 42972 | } |
| 42355 | 42973 | |
| 42356 | 42974 | |
| 42357 | 42975 | /* |
| 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). | |
| 42359 | 42977 | */ |
| 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; | |
| 42363 | 42983 | } |
| 42364 | 42984 | |
| 42365 | 42985 | |
| 42366 | 42986 | /* |
| 42367 | 42987 | ** This function starts a write transaction on the WAL. |
| @@ -42433,11 +43053,11 @@ | ||
| 42433 | 43053 | ** Otherwise, if the callback function does not return an error, this |
| 42434 | 43054 | ** function returns SQLITE_OK. |
| 42435 | 43055 | */ |
| 42436 | 43056 | SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
| 42437 | 43057 | int rc = SQLITE_OK; |
| 42438 | - if( pWal->writeLock ){ | |
| 43058 | + if( ALWAYS(pWal->writeLock) ){ | |
| 42439 | 43059 | Pgno iMax = pWal->hdr.mxFrame; |
| 42440 | 43060 | Pgno iFrame; |
| 42441 | 43061 | |
| 42442 | 43062 | /* Restore the clients cache of the wal-index header to the state it |
| 42443 | 43063 | ** was in before the client began writing to the database. |
| @@ -42622,11 +43242,11 @@ | ||
| 42622 | 43242 | memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); |
| 42623 | 43243 | walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); |
| 42624 | 43244 | sqlite3Put4byte(&aWalHdr[24], aCksum[0]); |
| 42625 | 43245 | sqlite3Put4byte(&aWalHdr[28], aCksum[1]); |
| 42626 | 43246 | |
| 42627 | - pWal->szPage = (u16)szPage; | |
| 43247 | + pWal->szPage = szPage; | |
| 42628 | 43248 | pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; |
| 42629 | 43249 | pWal->hdr.aFrameCksum[0] = aCksum[0]; |
| 42630 | 43250 | pWal->hdr.aFrameCksum[1] = aCksum[1]; |
| 42631 | 43251 | |
| 42632 | 43252 | rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); |
| @@ -42717,11 +43337,13 @@ | ||
| 42717 | 43337 | rc = walIndexAppend(pWal, iFrame, pLast->pgno); |
| 42718 | 43338 | } |
| 42719 | 43339 | |
| 42720 | 43340 | if( rc==SQLITE_OK ){ |
| 42721 | 43341 | /* 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 ); | |
| 42723 | 43345 | pWal->hdr.mxFrame = iFrame; |
| 42724 | 43346 | if( isCommit ){ |
| 42725 | 43347 | pWal->hdr.iChange++; |
| 42726 | 43348 | pWal->hdr.nPage = nTruncate; |
| 42727 | 43349 | } |
| @@ -42929,11 +43551,11 @@ | ||
| 42929 | 43551 | ** |
| 42930 | 43552 | ** FORMAT DETAILS |
| 42931 | 43553 | ** |
| 42932 | 43554 | ** The file is divided into pages. The first page is called page 1, |
| 42933 | 43555 | ** 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. | |
| 42935 | 43557 | ** Each page can be either a btree page, a freelist page, an overflow |
| 42936 | 43558 | ** page, or a pointer-map page. |
| 42937 | 43559 | ** |
| 42938 | 43560 | ** The first page is always a btree page. The first 100 bytes of the first |
| 42939 | 43561 | ** page contain a special header (the "file header") that describes the file. |
| @@ -43295,18 +43917,18 @@ | ||
| 43295 | 43917 | u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 43296 | 43918 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43297 | 43919 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 43298 | 43920 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 43299 | 43921 | #endif |
| 43300 | - u16 pageSize; /* Total number of bytes on a page */ | |
| 43301 | - u16 usableSize; /* Number of usable bytes on each page */ | |
| 43302 | 43922 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 43303 | 43923 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 43304 | 43924 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 43305 | 43925 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 43306 | 43926 | u8 inTransaction; /* Transaction state */ |
| 43307 | 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 */ | |
| 43308 | 43930 | int nTransaction; /* Number of open transactions (read + write) */ |
| 43309 | 43931 | u32 nPage; /* Number of pages in the database */ |
| 43310 | 43932 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 43311 | 43933 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 43312 | 43934 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ |
| @@ -43901,11 +44523,20 @@ | ||
| 43901 | 44523 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 43902 | 44524 | #else |
| 43903 | 44525 | # define TRACE(X) |
| 43904 | 44526 | #endif |
| 43905 | 44527 | |
| 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) | |
| 43907 | 44538 | |
| 43908 | 44539 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 43909 | 44540 | /* |
| 43910 | 44541 | ** A list of BtShared objects that are eligible for participation |
| 43911 | 44542 | ** in shared cache. This variable has file scope during normal builds, |
| @@ -45023,21 +45654,21 @@ | ||
| 45023 | 45654 | assert( nByte < usableSize-8 ); |
| 45024 | 45655 | |
| 45025 | 45656 | nFrag = data[hdr+7]; |
| 45026 | 45657 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 45027 | 45658 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 45028 | - top = get2byte(&data[hdr+5]); | |
| 45659 | + top = get2byteNotZero(&data[hdr+5]); | |
| 45029 | 45660 | if( gap>top ) return SQLITE_CORRUPT_BKPT; |
| 45030 | 45661 | testcase( gap+2==top ); |
| 45031 | 45662 | testcase( gap+1==top ); |
| 45032 | 45663 | testcase( gap==top ); |
| 45033 | 45664 | |
| 45034 | 45665 | if( nFrag>=60 ){ |
| 45035 | 45666 | /* Always defragment highly fragmented pages */ |
| 45036 | 45667 | rc = defragmentPage(pPage); |
| 45037 | 45668 | if( rc ) return rc; |
| 45038 | - top = get2byte(&data[hdr+5]); | |
| 45669 | + top = get2byteNotZero(&data[hdr+5]); | |
| 45039 | 45670 | }else if( gap+2<=top ){ |
| 45040 | 45671 | /* Search the freelist looking for a free slot big enough to satisfy |
| 45041 | 45672 | ** the request. The allocation is made from the first free slot in |
| 45042 | 45673 | ** the list that is large enough to accomadate it. |
| 45043 | 45674 | */ |
| @@ -45075,11 +45706,11 @@ | ||
| 45075 | 45706 | */ |
| 45076 | 45707 | testcase( gap+2+nByte==top ); |
| 45077 | 45708 | if( gap+2+nByte>top ){ |
| 45078 | 45709 | rc = defragmentPage(pPage); |
| 45079 | 45710 | if( rc ) return rc; |
| 45080 | - top = get2byte(&data[hdr+5]); | |
| 45711 | + top = get2byteNotZero(&data[hdr+5]); | |
| 45081 | 45712 | assert( gap+nByte<=top ); |
| 45082 | 45713 | } |
| 45083 | 45714 | |
| 45084 | 45715 | |
| 45085 | 45716 | /* Allocate memory from the gap in between the cell pointer array |
| @@ -45241,28 +45872,28 @@ | ||
| 45241 | 45872 | if( !pPage->isInit ){ |
| 45242 | 45873 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 45243 | 45874 | u8 hdr; /* Offset to beginning of page header */ |
| 45244 | 45875 | u8 *data; /* Equal to pPage->aData */ |
| 45245 | 45876 | 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 */ | |
| 45250 | 45881 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 45251 | 45882 | int iCellLast; /* Last possible cell or freeblock offset */ |
| 45252 | 45883 | |
| 45253 | 45884 | pBt = pPage->pBt; |
| 45254 | 45885 | |
| 45255 | 45886 | hdr = pPage->hdrOffset; |
| 45256 | 45887 | data = pPage->aData; |
| 45257 | 45888 | 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); | |
| 45260 | 45891 | pPage->nOverflow = 0; |
| 45261 | 45892 | usableSize = pBt->usableSize; |
| 45262 | 45893 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 45263 | - top = get2byte(&data[hdr+5]); | |
| 45894 | + top = get2byteNotZero(&data[hdr+5]); | |
| 45264 | 45895 | pPage->nCell = get2byte(&data[hdr+3]); |
| 45265 | 45896 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 45266 | 45897 | /* To many cells for a single page. The page must be corrupt */ |
| 45267 | 45898 | return SQLITE_CORRUPT_BKPT; |
| 45268 | 45899 | } |
| @@ -45362,12 +45993,12 @@ | ||
| 45362 | 45993 | pPage->nFree = pBt->usableSize - first; |
| 45363 | 45994 | decodeFlags(pPage, flags); |
| 45364 | 45995 | pPage->hdrOffset = hdr; |
| 45365 | 45996 | pPage->cellOffset = first; |
| 45366 | 45997 | 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); | |
| 45369 | 46000 | pPage->nCell = 0; |
| 45370 | 46001 | pPage->isInit = 1; |
| 45371 | 46002 | } |
| 45372 | 46003 | |
| 45373 | 46004 | |
| @@ -45671,11 +46302,11 @@ | ||
| 45671 | 46302 | pBt->pPage1 = 0; |
| 45672 | 46303 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 45673 | 46304 | #ifdef SQLITE_SECURE_DELETE |
| 45674 | 46305 | pBt->secureDelete = 1; |
| 45675 | 46306 | #endif |
| 45676 | - pBt->pageSize = get2byte(&zDbHeader[16]); | |
| 46307 | + pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); | |
| 45677 | 46308 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 45678 | 46309 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 45679 | 46310 | pBt->pageSize = 0; |
| 45680 | 46311 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 45681 | 46312 | /* If the magic name ":memory:" will create an in-memory database, then |
| @@ -45985,11 +46616,11 @@ | ||
| 45985 | 46616 | assert( nReserve>=0 && nReserve<=255 ); |
| 45986 | 46617 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 45987 | 46618 | ((pageSize-1)&pageSize)==0 ){ |
| 45988 | 46619 | assert( (pageSize & 7)==0 ); |
| 45989 | 46620 | assert( !pBt->pPage1 && !pBt->pCursor ); |
| 45990 | - pBt->pageSize = (u16)pageSize; | |
| 46621 | + pBt->pageSize = (u32)pageSize; | |
| 45991 | 46622 | freeTempSpace(pBt); |
| 45992 | 46623 | } |
| 45993 | 46624 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
| 45994 | 46625 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
| 45995 | 46626 | if( iFix ) pBt->pageSizeFixed = 1; |
| @@ -46120,19 +46751,17 @@ | ||
| 46120 | 46751 | |
| 46121 | 46752 | /* Do some checking to help insure the file we opened really is |
| 46122 | 46753 | ** a valid database file. |
| 46123 | 46754 | */ |
| 46124 | 46755 | 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); | |
| 46128 | 46757 | if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ |
| 46129 | 46758 | nPage = nPageFile; |
| 46130 | 46759 | } |
| 46131 | 46760 | if( nPage>0 ){ |
| 46132 | - int pageSize; | |
| 46133 | - int usableSize; | |
| 46761 | + u32 pageSize; | |
| 46762 | + u32 usableSize; | |
| 46134 | 46763 | u8 *page1 = pPage1->aData; |
| 46135 | 46764 | rc = SQLITE_NOTADB; |
| 46136 | 46765 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 46137 | 46766 | goto page1_init_failed; |
| 46138 | 46767 | } |
| @@ -46179,13 +46808,14 @@ | ||
| 46179 | 46808 | ** version 3.6.0, we require them to be fixed. |
| 46180 | 46809 | */ |
| 46181 | 46810 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 46182 | 46811 | goto page1_init_failed; |
| 46183 | 46812 | } |
| 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 | |
| 46187 | 46817 | ){ |
| 46188 | 46818 | goto page1_init_failed; |
| 46189 | 46819 | } |
| 46190 | 46820 | assert( (pageSize & 7)==0 ); |
| 46191 | 46821 | usableSize = pageSize - page1[20]; |
| @@ -46195,12 +46825,12 @@ | ||
| 46195 | 46825 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 46196 | 46826 | ** zero and return SQLITE_OK. The caller will call this function |
| 46197 | 46827 | ** again with the correct page-size. |
| 46198 | 46828 | */ |
| 46199 | 46829 | releasePage(pPage1); |
| 46200 | - pBt->usableSize = (u16)usableSize; | |
| 46201 | - pBt->pageSize = (u16)pageSize; | |
| 46830 | + pBt->usableSize = usableSize; | |
| 46831 | + pBt->pageSize = pageSize; | |
| 46202 | 46832 | freeTempSpace(pBt); |
| 46203 | 46833 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 46204 | 46834 | pageSize-usableSize); |
| 46205 | 46835 | return rc; |
| 46206 | 46836 | } |
| @@ -46209,12 +46839,12 @@ | ||
| 46209 | 46839 | goto page1_init_failed; |
| 46210 | 46840 | } |
| 46211 | 46841 | if( usableSize<480 ){ |
| 46212 | 46842 | goto page1_init_failed; |
| 46213 | 46843 | } |
| 46214 | - pBt->pageSize = (u16)pageSize; | |
| 46215 | - pBt->usableSize = (u16)usableSize; | |
| 46844 | + pBt->pageSize = pageSize; | |
| 46845 | + pBt->usableSize = usableSize; | |
| 46216 | 46846 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 46217 | 46847 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
| 46218 | 46848 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
| 46219 | 46849 | #endif |
| 46220 | 46850 | } |
| @@ -46286,11 +46916,12 @@ | ||
| 46286 | 46916 | data = pP1->aData; |
| 46287 | 46917 | rc = sqlite3PagerWrite(pP1->pDbPage); |
| 46288 | 46918 | if( rc ) return rc; |
| 46289 | 46919 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 46290 | 46920 | assert( sizeof(zMagicHeader)==16 ); |
| 46291 | - put2byte(&data[16], pBt->pageSize); | |
| 46921 | + data[16] = (pBt->pageSize>>8)&0xff; | |
| 46922 | + data[17] = (pBt->pageSize>>16)&0xff; | |
| 46292 | 46923 | data[18] = 1; |
| 46293 | 46924 | data[19] = 1; |
| 46294 | 46925 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 46295 | 46926 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
| 46296 | 46927 | data[21] = 64; |
| @@ -48297,13 +48928,13 @@ | ||
| 48297 | 48928 | c = +1; |
| 48298 | 48929 | } |
| 48299 | 48930 | pCur->validNKey = 1; |
| 48300 | 48931 | pCur->info.nKey = nCellKey; |
| 48301 | 48932 | }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 | |
| 48303 | 48934 | ** 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 | |
| 48305 | 48936 | ** varint. This information is used to attempt to avoid parsing |
| 48306 | 48937 | ** the entire cell by checking for the cases where the record is |
| 48307 | 48938 | ** stored entirely within the b-tree page by inspecting the first |
| 48308 | 48939 | ** 2 bytes of the cell. |
| 48309 | 48940 | */ |
| @@ -49193,11 +49824,11 @@ | ||
| 49193 | 49824 | ** |
| 49194 | 49825 | ** "sz" must be the number of bytes in the cell. |
| 49195 | 49826 | */ |
| 49196 | 49827 | static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
| 49197 | 49828 | 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 */ | |
| 49199 | 49830 | u8 *data; /* pPage->aData */ |
| 49200 | 49831 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 49201 | 49832 | int rc; /* The return code */ |
| 49202 | 49833 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
| 49203 | 49834 | |
| @@ -49211,11 +49842,11 @@ | ||
| 49211 | 49842 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 49212 | 49843 | pc = get2byte(ptr); |
| 49213 | 49844 | hdr = pPage->hdrOffset; |
| 49214 | 49845 | testcase( pc==get2byte(&data[hdr+5]) ); |
| 49215 | 49846 | 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 ){ | |
| 49217 | 49848 | *pRC = SQLITE_CORRUPT_BKPT; |
| 49218 | 49849 | return; |
| 49219 | 49850 | } |
| 49220 | 49851 | rc = freeSpace(pPage, pc, sz); |
| 49221 | 49852 | if( rc ){ |
| @@ -49268,11 +49899,11 @@ | ||
| 49268 | 49899 | int nSkip = (iChild ? 4 : 0); |
| 49269 | 49900 | |
| 49270 | 49901 | if( *pRC ) return; |
| 49271 | 49902 | |
| 49272 | 49903 | 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 ); | |
| 49274 | 49905 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
| 49275 | 49906 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 49276 | 49907 | /* The cell should normally be sized correctly. However, when moving a |
| 49277 | 49908 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 49278 | 49909 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| @@ -49348,16 +49979,16 @@ | ||
| 49348 | 49979 | const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 49349 | 49980 | const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
| 49350 | 49981 | |
| 49351 | 49982 | assert( pPage->nOverflow==0 ); |
| 49352 | 49983 | 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); | |
| 49354 | 49985 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 49355 | 49986 | |
| 49356 | 49987 | /* Check that the page has just been zeroed by zeroPage() */ |
| 49357 | 49988 | assert( pPage->nCell==0 ); |
| 49358 | - assert( get2byte(&data[hdr+5])==nUsable ); | |
| 49989 | + assert( get2byteNotZero(&data[hdr+5])==nUsable ); | |
| 49359 | 49990 | |
| 49360 | 49991 | pCellptr = &data[pPage->cellOffset + nCell*2]; |
| 49361 | 49992 | cellbody = nUsable; |
| 49362 | 49993 | for(i=nCell-1; i>=0; i--){ |
| 49363 | 49994 | pCellptr -= 2; |
| @@ -49419,11 +50050,12 @@ | ||
| 49419 | 50050 | |
| 49420 | 50051 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 49421 | 50052 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 49422 | 50053 | assert( pPage->nOverflow==1 ); |
| 49423 | 50054 | |
| 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; | |
| 49425 | 50057 | |
| 49426 | 50058 | /* Allocate a new page. This page will become the right-sibling of |
| 49427 | 50059 | ** pPage. Make the parent page writable, so that the new divider cell |
| 49428 | 50060 | ** may be inserted. If both these operations are successful, proceed. |
| 49429 | 50061 | */ |
| @@ -49748,11 +50380,11 @@ | ||
| 49748 | 50380 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 49749 | 50381 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 49750 | 50382 | ** is allocated. */ |
| 49751 | 50383 | if( pBt->secureDelete ){ |
| 49752 | 50384 | 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 ){ | |
| 49754 | 50386 | rc = SQLITE_CORRUPT_BKPT; |
| 49755 | 50387 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 49756 | 50388 | goto balance_cleanup; |
| 49757 | 50389 | }else{ |
| 49758 | 50390 | memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); |
| @@ -51317,11 +51949,11 @@ | ||
| 51317 | 51949 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 51318 | 51950 | if( pCheck->pBt->autoVacuum ){ |
| 51319 | 51951 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 51320 | 51952 | } |
| 51321 | 51953 | #endif |
| 51322 | - if( n>pCheck->pBt->usableSize/4-2 ){ | |
| 51954 | + if( n>(int)pCheck->pBt->usableSize/4-2 ){ | |
| 51323 | 51955 | checkAppendMsg(pCheck, zContext, |
| 51324 | 51956 | "freelist leaf count too big on page %d", iPage); |
| 51325 | 51957 | N--; |
| 51326 | 51958 | }else{ |
| 51327 | 51959 | for(i=0; i<n; i++){ |
| @@ -51528,24 +52160,24 @@ | ||
| 51528 | 52160 | hdr = pPage->hdrOffset; |
| 51529 | 52161 | hit = sqlite3PageMalloc( pBt->pageSize ); |
| 51530 | 52162 | if( hit==0 ){ |
| 51531 | 52163 | pCheck->mallocFailed = 1; |
| 51532 | 52164 | }else{ |
| 51533 | - u16 contentOffset = get2byte(&data[hdr+5]); | |
| 52165 | + int contentOffset = get2byteNotZero(&data[hdr+5]); | |
| 51534 | 52166 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 51535 | 52167 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 51536 | 52168 | memset(hit, 1, contentOffset); |
| 51537 | 52169 | nCell = get2byte(&data[hdr+3]); |
| 51538 | 52170 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 51539 | 52171 | for(i=0; i<nCell; i++){ |
| 51540 | 52172 | int pc = get2byte(&data[cellStart+i*2]); |
| 51541 | - u16 size = 1024; | |
| 52173 | + u32 size = 65536; | |
| 51542 | 52174 | int j; |
| 51543 | 52175 | if( pc<=usableSize-4 ){ |
| 51544 | 52176 | size = cellSizePtr(pPage, &data[pc]); |
| 51545 | 52177 | } |
| 51546 | - if( (pc+size-1)>=usableSize ){ | |
| 52178 | + if( (int)(pc+size-1)>=usableSize ){ | |
| 51547 | 52179 | checkAppendMsg(pCheck, 0, |
| 51548 | 52180 | "Corruption detected in cell %d on page %d",i,iPage); |
| 51549 | 52181 | }else{ |
| 51550 | 52182 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 51551 | 52183 | } |
| @@ -55769,12 +56401,21 @@ | ||
| 55769 | 56401 | mrc = p->rc & 0xff; |
| 55770 | 56402 | assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ |
| 55771 | 56403 | isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
| 55772 | 56404 | || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
| 55773 | 56405 | 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. | |
| 55776 | 56417 | */ |
| 55777 | 56418 | if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ |
| 55778 | 56419 | if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ |
| 55779 | 56420 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 55780 | 56421 | }else{ |
| @@ -63200,11 +63841,11 @@ | ||
| 63200 | 63841 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 63201 | 63842 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 63202 | 63843 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 63203 | 63844 | ** the result is false whereas it would be true with IdxGT. |
| 63204 | 63845 | */ |
| 63205 | -/* Opcode: IdxLT P1 P2 P3 * P5 | |
| 63846 | +/* Opcode: IdxLT P1 P2 P3 P4 P5 | |
| 63206 | 63847 | ** |
| 63207 | 63848 | ** The P4 register values beginning with P3 form an unpacked index |
| 63208 | 63849 | ** key that omits the ROWID. Compare this key value against the index |
| 63209 | 63850 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
| 63210 | 63851 | ** |
| @@ -67547,17 +68188,18 @@ | ||
| 67547 | 68188 | assert( z[0]=='?' ); |
| 67548 | 68189 | pExpr->iColumn = (ynVar)(++pParse->nVar); |
| 67549 | 68190 | }else if( z[0]=='?' ){ |
| 67550 | 68191 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 67551 | 68192 | ** use it as the variable number */ |
| 67552 | - int i = atoi((char*)&z[1]); | |
| 68193 | + i64 i; | |
| 68194 | + int bOk = sqlite3Atoi64(&z[1], &i); | |
| 67553 | 68195 | pExpr->iColumn = (ynVar)i; |
| 67554 | 68196 | testcase( i==0 ); |
| 67555 | 68197 | testcase( i==1 ); |
| 67556 | 68198 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 67557 | 68199 | 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] ){ | |
| 67559 | 68201 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 67560 | 68202 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
| 67561 | 68203 | } |
| 67562 | 68204 | if( i>pParse->nVar ){ |
| 67563 | 68205 | pParse->nVar = i; |
| @@ -72092,10 +72734,11 @@ | ||
| 72092 | 72734 | } |
| 72093 | 72735 | } |
| 72094 | 72736 | sqlite3DbFree(db, pIdx->aSample); |
| 72095 | 72737 | } |
| 72096 | 72738 | #else |
| 72739 | + UNUSED_PARAMETER(db); | |
| 72097 | 72740 | UNUSED_PARAMETER(pIdx); |
| 72098 | 72741 | #endif |
| 72099 | 72742 | } |
| 72100 | 72743 | |
| 72101 | 72744 | /* |
| @@ -87738,11 +88381,11 @@ | ||
| 87738 | 88381 | ** be sent. |
| 87739 | 88382 | ** |
| 87740 | 88383 | ** regReturn is the number of the register holding the subroutine |
| 87741 | 88384 | ** return address. |
| 87742 | 88385 | ** |
| 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 | |
| 87744 | 88387 | ** records the previous output. mem[regPrev] is a flag that is false |
| 87745 | 88388 | ** if there has been no previous output. If regPrev>0 then code is |
| 87746 | 88389 | ** generated to suppress duplicates. pKeyInfo is used for comparing |
| 87747 | 88390 | ** keys. |
| 87748 | 88391 | ** |
| @@ -88435,16 +89078,17 @@ | ||
| 88435 | 89078 | ** (1) The subquery and the outer query do not both use aggregates. |
| 88436 | 89079 | ** |
| 88437 | 89080 | ** (2) The subquery is not an aggregate or the outer query is not a join. |
| 88438 | 89081 | ** |
| 88439 | 89082 | ** (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) | |
| 88441 | 89084 | ** |
| 88442 | -** (4) The subquery is not DISTINCT or the outer query is not a join. | |
| 89085 | +** (4) The subquery is not DISTINCT. | |
| 88443 | 89086 | ** |
| 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. | |
| 88446 | 89090 | ** |
| 88447 | 89091 | ** (6) The subquery does not use aggregates or the outer query is not |
| 88448 | 89092 | ** DISTINCT. |
| 88449 | 89093 | ** |
| 88450 | 89094 | ** (7) The subquery has a FROM clause. |
| @@ -88460,13 +89104,13 @@ | ||
| 88460 | 89104 | ** (11) The subquery and the outer query do not both have ORDER BY clauses. |
| 88461 | 89105 | ** |
| 88462 | 89106 | ** (**) Not implemented. Subsumed into restriction (3). Was previously |
| 88463 | 89107 | ** a separate restriction deriving from ticket #350. |
| 88464 | 89108 | ** |
| 88465 | -** (13) The subquery and outer query do not both use LIMIT | |
| 89109 | +** (13) The subquery and outer query do not both use LIMIT. | |
| 88466 | 89110 | ** |
| 88467 | -** (14) The subquery does not use OFFSET | |
| 89111 | +** (14) The subquery does not use OFFSET. | |
| 88468 | 89112 | ** |
| 88469 | 89113 | ** (15) The outer query is not part of a compound select or the |
| 88470 | 89114 | ** subquery does not have a LIMIT clause. |
| 88471 | 89115 | ** (See ticket #2339 and ticket [02a8e81d44]). |
| 88472 | 89116 | ** |
| @@ -88553,13 +89197,13 @@ | ||
| 88553 | 89197 | if( pSub->pOffset ) return 0; /* Restriction (14) */ |
| 88554 | 89198 | if( p->pRightmost && pSub->pLimit ){ |
| 88555 | 89199 | return 0; /* Restriction (15) */ |
| 88556 | 89200 | } |
| 88557 | 89201 | 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) */ | |
| 88561 | 89205 | } |
| 88562 | 89206 | if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ |
| 88563 | 89207 | return 0; /* Restriction (6) */ |
| 88564 | 89208 | } |
| 88565 | 89209 | if( p->pOrderBy && pSub->pOrderBy ){ |
| @@ -93225,11 +93869,11 @@ | ||
| 93225 | 93869 | pParse->pNewTable->aCol = 0; |
| 93226 | 93870 | } |
| 93227 | 93871 | db->pVTab = 0; |
| 93228 | 93872 | }else{ |
| 93229 | 93873 | sqlite3Error(db, SQLITE_ERROR, zErr); |
| 93230 | - sqlite3_free(zErr); | |
| 93874 | + sqlite3DbFree(db, zErr); | |
| 93231 | 93875 | rc = SQLITE_ERROR; |
| 93232 | 93876 | } |
| 93233 | 93877 | pParse->declareVtab = 0; |
| 93234 | 93878 | |
| 93235 | 93879 | if( pParse->pVdbe ){ |
| @@ -96859,39 +97503,39 @@ | ||
| 96859 | 97503 | ** |
| 96860 | 97504 | ** This case is also used when there are no WHERE clause |
| 96861 | 97505 | ** constraints but an index is selected anyway, in order |
| 96862 | 97506 | ** to force the output order to conform to an ORDER BY. |
| 96863 | 97507 | */ |
| 96864 | - int aStartOp[] = { | |
| 97508 | + static const u8 aStartOp[] = { | |
| 96865 | 97509 | 0, |
| 96866 | 97510 | 0, |
| 96867 | 97511 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 96868 | 97512 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 96869 | 97513 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 96870 | 97514 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 96871 | 97515 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 96872 | 97516 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 96873 | 97517 | }; |
| 96874 | - int aEndOp[] = { | |
| 97518 | + static const u8 aEndOp[] = { | |
| 96875 | 97519 | OP_Noop, /* 0: (!end_constraints) */ |
| 96876 | 97520 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 96877 | 97521 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 96878 | 97522 | }; |
| 96879 | - int nEq = pLevel->plan.nEq; | |
| 97523 | + int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ | |
| 96880 | 97524 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 96881 | 97525 | int regBase; /* Base register holding constraint values */ |
| 96882 | 97526 | int r1; /* Temp register */ |
| 96883 | 97527 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 96884 | 97528 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 96885 | 97529 | int startEq; /* True if range start uses ==, >= or <= */ |
| 96886 | 97530 | int endEq; /* True if range end uses ==, >= or <= */ |
| 96887 | 97531 | int start_constraints; /* Start of range is constrained */ |
| 96888 | 97532 | 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 */ | |
| 96893 | 97537 | char *zStartAff; /* Affinity for start of range constraint */ |
| 96894 | 97538 | char *zEndAff; /* Affinity for end of range constraint */ |
| 96895 | 97539 | |
| 96896 | 97540 | pIdx = pLevel->plan.u.pIdx; |
| 96897 | 97541 | iIdxCur = pLevel->iIdxCur; |
| @@ -108720,11 +109364,11 @@ | ||
| 108720 | 109364 | ** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following |
| 108721 | 109365 | ** two forward declarations are for functions declared in these files |
| 108722 | 109366 | ** used to retrieve the respective implementations. |
| 108723 | 109367 | ** |
| 108724 | 109368 | ** 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. | |
| 108726 | 109370 | ** Function ...PorterTokenizerModule() sets *pModule to point to the |
| 108727 | 109371 | ** porter tokenizer/stemmer implementation. |
| 108728 | 109372 | */ |
| 108729 | 109373 | SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| 108730 | 109374 | SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| @@ -108922,11 +109566,11 @@ | ||
| 108922 | 109566 | ** is defined to accept an argument of type char, and always returns 0 for |
| 108923 | 109567 | ** any values that fall outside of the range of the unsigned char type (i.e. |
| 108924 | 109568 | ** negative values). |
| 108925 | 109569 | */ |
| 108926 | 109570 | 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'; | |
| 108928 | 109572 | } |
| 108929 | 109573 | |
| 108930 | 109574 | /* |
| 108931 | 109575 | ** Extract the next token from buffer z (length n) using the tokenizer |
| 108932 | 109576 | ** and other information (column names etc.) in pParse. Create an Fts3Expr |
| @@ -111309,10 +111953,13 @@ | ||
| 111309 | 111953 | |
| 111310 | 111954 | |
| 111311 | 111955 | static int simpleDelim(simple_tokenizer *t, unsigned char c){ |
| 111312 | 111956 | return c<0x80 && t->delim[c]; |
| 111313 | 111957 | } |
| 111958 | +static int fts3_isalnum(int x){ | |
| 111959 | + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); | |
| 111960 | +} | |
| 111314 | 111961 | |
| 111315 | 111962 | /* |
| 111316 | 111963 | ** Create a new tokenizer instance. |
| 111317 | 111964 | */ |
| 111318 | 111965 | static int simpleCreate( |
| @@ -111343,11 +111990,11 @@ | ||
| 111343 | 111990 | } |
| 111344 | 111991 | } else { |
| 111345 | 111992 | /* Mark non-alphanumeric ASCII characters as delimiters */ |
| 111346 | 111993 | int i; |
| 111347 | 111994 | for(i=1; i<0x80; i++){ |
| 111348 | - t->delim[i] = !isalnum(i) ? -1 : 0; | |
| 111995 | + t->delim[i] = !fts3_isalnum(i) ? -1 : 0; | |
| 111349 | 111996 | } |
| 111350 | 111997 | } |
| 111351 | 111998 | |
| 111352 | 111999 | *ppTokenizer = &t->base; |
| 111353 | 112000 | return SQLITE_OK; |
| @@ -111449,11 +112096,11 @@ | ||
| 111449 | 112096 | for(i=0; i<n; i++){ |
| 111450 | 112097 | /* TODO(shess) This needs expansion to handle UTF-8 |
| 111451 | 112098 | ** case-insensitivity. |
| 111452 | 112099 | */ |
| 111453 | 112100 | 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); | |
| 111455 | 112102 | } |
| 111456 | 112103 | *ppToken = c->pToken; |
| 111457 | 112104 | *pnBytes = n; |
| 111458 | 112105 | *piStartOffset = iStartOffset; |
| 111459 | 112106 | *piEndOffset = c->iOffset; |
| @@ -116355,15 +117002,14 @@ | ||
| 116355 | 117002 | ** least desirable): |
| 116356 | 117003 | ** |
| 116357 | 117004 | ** idxNum idxStr Strategy |
| 116358 | 117005 | ** ------------------------------------------------ |
| 116359 | 117006 | ** 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. | |
| 116362 | 117008 | ** ------------------------------------------------ |
| 116363 | 117009 | ** |
| 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 | |
| 116365 | 117011 | ** 2 is used, idxStr is formatted to contain 2 bytes for each |
| 116366 | 117012 | ** constraint used. The first two bytes of idxStr correspond to |
| 116367 | 117013 | ** the constraint in sqlite3_index_info.aConstraintUsage[] with |
| 116368 | 117014 | ** (argvIndex==1) etc. |
| 116369 | 117015 | ** |
| 116370 | 117016 |
| --- 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, ¤tSize); |
| 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, ¤tSize); |
| 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 @@ | ||
| 95 | 95 | ** be held constant and Z will be incremented or else Y will be incremented |
| 96 | 96 | ** and Z will be reset to zero. |
| 97 | 97 | ** |
| 98 | 98 | ** Since version 3.6.18, SQLite source code has been stored in the |
| 99 | 99 | ** <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 | |
| 101 | 101 | ** a string which identifies a particular check-in of SQLite |
| 102 | 102 | ** within its configuration management system. ^The SQLITE_SOURCE_ID |
| 103 | 103 | ** string contains the date and time of the check-in (UTC) and an SHA1 |
| 104 | 104 | ** hash of the entire source tree. |
| 105 | 105 | ** |
| @@ -107,11 +107,11 @@ | ||
| 107 | 107 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 108 | 108 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 109 | 109 | */ |
| 110 | 110 | #define SQLITE_VERSION "3.7.1" |
| 111 | 111 | #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" | |
| 113 | 113 | |
| 114 | 114 | /* |
| 115 | 115 | ** CAPI3REF: Run-Time Library Version Numbers |
| 116 | 116 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 117 | 117 | ** |
| @@ -152,19 +152,19 @@ | ||
| 152 | 152 | ** ^The sqlite3_compileoption_used() function returns 0 or 1 |
| 153 | 153 | ** indicating whether the specified option was defined at |
| 154 | 154 | ** compile time. ^The SQLITE_ prefix may be omitted from the |
| 155 | 155 | ** option name passed to sqlite3_compileoption_used(). |
| 156 | 156 | ** |
| 157 | -** ^The sqlite3_compileoption_get() function allows interating | |
| 157 | +** ^The sqlite3_compileoption_get() function allows iterating | |
| 158 | 158 | ** over the list of options that were defined at compile time by |
| 159 | 159 | ** returning the N-th compile time option string. ^If N is out of range, |
| 160 | 160 | ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ |
| 161 | 161 | ** prefix is omitted from any strings returned by |
| 162 | 162 | ** sqlite3_compileoption_get(). |
| 163 | 163 | ** |
| 164 | 164 | ** ^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 | |
| 166 | 166 | ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. |
| 167 | 167 | ** |
| 168 | 168 | ** See also: SQL functions [sqlite_compileoption_used()] and |
| 169 | 169 | ** [sqlite_compileoption_get()] and the [compile_options pragma]. |
| 170 | 170 | */ |
| @@ -266,11 +266,11 @@ | ||
| 266 | 266 | /* |
| 267 | 267 | ** CAPI3REF: Closing A Database Connection |
| 268 | 268 | ** |
| 269 | 269 | ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object. |
| 270 | 270 | ** ^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. | |
| 272 | 272 | ** |
| 273 | 273 | ** Applications must [sqlite3_finalize | finalize] all [prepared statements] |
| 274 | 274 | ** and [sqlite3_blob_close | close] all [BLOB handles] associated with |
| 275 | 275 | ** the [sqlite3] object prior to attempting to close the object. ^If |
| 276 | 276 | ** sqlite3_close() is called on a [database connection] that still has |
| @@ -693,16 +693,25 @@ | ||
| 693 | 693 | ** layer a hint of how large the database file will grow to be during the |
| 694 | 694 | ** current transaction. This hint is not guaranteed to be accurate but it |
| 695 | 695 | ** is often close. The underlying VFS might choose to preallocate database |
| 696 | 696 | ** file space based on this hint in order to help writes to the database |
| 697 | 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. | |
| 698 | 706 | */ |
| 699 | 707 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 700 | 708 | #define SQLITE_GET_LOCKPROXYFILE 2 |
| 701 | 709 | #define SQLITE_SET_LOCKPROXYFILE 3 |
| 702 | 710 | #define SQLITE_LAST_ERRNO 4 |
| 703 | 711 | #define SQLITE_FCNTL_SIZE_HINT 5 |
| 712 | +#define SQLITE_FCNTL_CHUNK_SIZE 6 | |
| 704 | 713 | |
| 705 | 714 | /* |
| 706 | 715 | ** CAPI3REF: Mutex Handle |
| 707 | 716 | ** |
| 708 | 717 | ** The mutex module within SQLite defines [sqlite3_mutex] to be an |
| @@ -2661,11 +2670,11 @@ | ||
| 2661 | 2670 | ** <li> @VVV |
| 2662 | 2671 | ** <li> $VVV |
| 2663 | 2672 | ** </ul> |
| 2664 | 2673 | ** |
| 2665 | 2674 | ** 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 | |
| 2667 | 2676 | ** parameters (also called "host parameter names" or "SQL parameters") |
| 2668 | 2677 | ** can be set using the sqlite3_bind_*() routines defined here. |
| 2669 | 2678 | ** |
| 2670 | 2679 | ** ^The first argument to the sqlite3_bind_*() routines is always |
| 2671 | 2680 | ** a pointer to the [sqlite3_stmt] object returned from |
| @@ -3440,11 +3449,11 @@ | ||
| 3440 | 3449 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); |
| 3441 | 3450 | |
| 3442 | 3451 | /* |
| 3443 | 3452 | ** CAPI3REF: Obtain Aggregate Function Context |
| 3444 | 3453 | ** |
| 3445 | -** Implementions of aggregate SQL functions use this | |
| 3454 | +** Implementations of aggregate SQL functions use this | |
| 3446 | 3455 | ** routine to allocate memory for storing their state. |
| 3447 | 3456 | ** |
| 3448 | 3457 | ** ^The first time the sqlite3_aggregate_context(C,N) routine is called |
| 3449 | 3458 | ** for a particular aggregate function, SQLite |
| 3450 | 3459 | ** allocates N of memory, zeroes out that memory, and returns a pointer |
| @@ -3712,11 +3721,11 @@ | ||
| 3712 | 3721 | ** the routine expects pointers to 16-bit word aligned strings |
| 3713 | 3722 | ** of UTF-16 in the native byte order. |
| 3714 | 3723 | ** |
| 3715 | 3724 | ** A pointer to the user supplied routine must be passed as the fifth |
| 3716 | 3725 | ** 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). | |
| 3718 | 3727 | ** ^Each time the application supplied function is invoked, it is passed |
| 3719 | 3728 | ** as its first parameter a copy of the void* passed as the fourth argument |
| 3720 | 3729 | ** to sqlite3_create_collation() or sqlite3_create_collation16(). |
| 3721 | 3730 | ** |
| 3722 | 3731 | ** ^The remaining arguments to the application-supplied routine are two strings, |
| @@ -4930,11 +4939,11 @@ | ||
| 4930 | 4939 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 4931 | 4940 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 4932 | 4941 | ** it is passed a NULL pointer). |
| 4933 | 4942 | ** |
| 4934 | 4943 | ** 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 | |
| 4936 | 4945 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 4937 | 4946 | ** xMutexInit() must be no-ops. |
| 4938 | 4947 | ** |
| 4939 | 4948 | ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 4940 | 4949 | ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| @@ -5100,11 +5109,11 @@ | ||
| 5100 | 5109 | |
| 5101 | 5110 | /* |
| 5102 | 5111 | ** CAPI3REF: SQLite Runtime Status |
| 5103 | 5112 | ** |
| 5104 | 5113 | ** ^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 | |
| 5106 | 5115 | ** highwater marks. ^The first argument is an integer code for |
| 5107 | 5116 | ** the specific parameter to measure. ^(Recognized integer codes |
| 5108 | 5117 | ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^ |
| 5109 | 5118 | ** ^The current value of the parameter is returned into *pCurrent. |
| 5110 | 5119 | ** ^The highest recorded value is returned in *pHighwater. ^If the |
| @@ -5226,11 +5235,11 @@ | ||
| 5226 | 5235 | ** ^This interface is used to retrieve runtime status information |
| 5227 | 5236 | ** about a single [database connection]. ^The first argument is the |
| 5228 | 5237 | ** database connection object to be interrogated. ^The second argument |
| 5229 | 5238 | ** is an integer constant, taken from the set of |
| 5230 | 5239 | ** [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 | |
| 5232 | 5241 | ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely |
| 5233 | 5242 | ** to grow in future releases of SQLite. |
| 5234 | 5243 | ** |
| 5235 | 5244 | ** ^The current value of the requested parameter is written into *pCur |
| 5236 | 5245 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| @@ -5648,11 +5657,11 @@ | ||
| 5648 | 5657 | ** |
| 5649 | 5658 | ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b> |
| 5650 | 5659 | ** |
| 5651 | 5660 | ** ^Each call to sqlite3_backup_step() sets two values inside |
| 5652 | 5661 | ** 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. | |
| 5654 | 5663 | ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces |
| 5655 | 5664 | ** retrieve these two values, respectively. |
| 5656 | 5665 | ** |
| 5657 | 5666 | ** ^The values returned by these functions are only updated by |
| 5658 | 5667 | ** sqlite3_backup_step(). ^If the source database is modified during a backup |
| @@ -5744,11 +5753,11 @@ | ||
| 5744 | 5753 | ** ^(There may be at most one unlock-notify callback registered by a |
| 5745 | 5754 | ** blocked connection. If sqlite3_unlock_notify() is called when the |
| 5746 | 5755 | ** blocked connection already has a registered unlock-notify callback, |
| 5747 | 5756 | ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is |
| 5748 | 5757 | ** 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 | |
| 5750 | 5759 | ** unlock-notify callback may also be canceled by closing the blocked |
| 5751 | 5760 | ** connection using [sqlite3_close()]. |
| 5752 | 5761 | ** |
| 5753 | 5762 | ** The unlock-notify callback is not reentrant. If an application invokes |
| 5754 | 5763 | ** any sqlite3_xxx API functions from within an unlock-notify callback, a |
| @@ -5826,11 +5835,11 @@ | ||
| 5826 | 5835 | /* |
| 5827 | 5836 | ** CAPI3REF: String Comparison |
| 5828 | 5837 | ** |
| 5829 | 5838 | ** ^The [sqlite3_strnicmp()] API allows applications and extensions to |
| 5830 | 5839 | ** 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 | |
| 5832 | 5841 | ** that SQLite uses internally when comparing identifiers. |
| 5833 | 5842 | */ |
| 5834 | 5843 | SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); |
| 5835 | 5844 | |
| 5836 | 5845 | /* |
| 5837 | 5846 |
| --- 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 |