| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.7.1. By combining all the individual C code files into this |
| 3 | +** version 3.7.2. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a one translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% are more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -213,24 +213,25 @@ |
| 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 | | -** 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. |
| 221 | +** Earlier versions of SQLite allowed the user to change this value at |
| 222 | +** compile time. This is no longer permitted, on the grounds that it creates |
| 223 | +** a library that is technically incompatible with an SQLite library |
| 224 | +** compiled with a different limit. If a process operating on a database |
| 225 | +** with a page-size of 65536 bytes crashes, then an instance of SQLite |
| 226 | +** compiled with the default page-size limit will not be able to rollback |
| 227 | +** the aborted transaction. This could lead to database corruption. |
| 228 | 228 | */ |
| 229 | | -#ifndef SQLITE_MAX_PAGE_SIZE |
| 230 | | -# define SQLITE_MAX_PAGE_SIZE 32768 |
| 229 | +#ifdef SQLITE_MAX_PAGE_SIZE |
| 230 | +# undef SQLITE_MAX_PAGE_SIZE |
| 231 | 231 | #endif |
| 232 | +#define SQLITE_MAX_PAGE_SIZE 65536 |
| 232 | 233 | |
| 233 | 234 | |
| 234 | 235 | /* |
| 235 | 236 | ** The default size of a database page. |
| 236 | 237 | */ |
| | @@ -631,23 +632,23 @@ |
| 631 | 632 | ** be held constant and Z will be incremented or else Y will be incremented |
| 632 | 633 | ** and Z will be reset to zero. |
| 633 | 634 | ** |
| 634 | 635 | ** Since version 3.6.18, SQLite source code has been stored in the |
| 635 | 636 | ** <a href="http://www.fossil-scm.org/">Fossil configuration management |
| 636 | | -** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to |
| 637 | +** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to |
| 637 | 638 | ** a string which identifies a particular check-in of SQLite |
| 638 | 639 | ** within its configuration management system. ^The SQLITE_SOURCE_ID |
| 639 | 640 | ** string contains the date and time of the check-in (UTC) and an SHA1 |
| 640 | 641 | ** hash of the entire source tree. |
| 641 | 642 | ** |
| 642 | 643 | ** See also: [sqlite3_libversion()], |
| 643 | 644 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 644 | 645 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 645 | 646 | */ |
| 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" |
| 647 | +#define SQLITE_VERSION "3.7.2" |
| 648 | +#define SQLITE_VERSION_NUMBER 3007002 |
| 649 | +#define SQLITE_SOURCE_ID "2010-08-23 18:52:01 42537b60566f288167f1b5864a5435986838e3a3" |
| 649 | 650 | |
| 650 | 651 | /* |
| 651 | 652 | ** CAPI3REF: Run-Time Library Version Numbers |
| 652 | 653 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 653 | 654 | ** |
| | @@ -688,19 +689,19 @@ |
| 688 | 689 | ** ^The sqlite3_compileoption_used() function returns 0 or 1 |
| 689 | 690 | ** indicating whether the specified option was defined at |
| 690 | 691 | ** compile time. ^The SQLITE_ prefix may be omitted from the |
| 691 | 692 | ** option name passed to sqlite3_compileoption_used(). |
| 692 | 693 | ** |
| 693 | | -** ^The sqlite3_compileoption_get() function allows interating |
| 694 | +** ^The sqlite3_compileoption_get() function allows iterating |
| 694 | 695 | ** over the list of options that were defined at compile time by |
| 695 | 696 | ** returning the N-th compile time option string. ^If N is out of range, |
| 696 | 697 | ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ |
| 697 | 698 | ** prefix is omitted from any strings returned by |
| 698 | 699 | ** sqlite3_compileoption_get(). |
| 699 | 700 | ** |
| 700 | 701 | ** ^Support for the diagnostic functions sqlite3_compileoption_used() |
| 701 | | -** and sqlite3_compileoption_get() may be omitted by specifing the |
| 702 | +** and sqlite3_compileoption_get() may be omitted by specifying the |
| 702 | 703 | ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. |
| 703 | 704 | ** |
| 704 | 705 | ** See also: SQL functions [sqlite_compileoption_used()] and |
| 705 | 706 | ** [sqlite_compileoption_get()] and the [compile_options pragma]. |
| 706 | 707 | */ |
| | @@ -802,11 +803,11 @@ |
| 802 | 803 | /* |
| 803 | 804 | ** CAPI3REF: Closing A Database Connection |
| 804 | 805 | ** |
| 805 | 806 | ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object. |
| 806 | 807 | ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is |
| 807 | | -** successfullly destroyed and all associated resources are deallocated. |
| 808 | +** successfully destroyed and all associated resources are deallocated. |
| 808 | 809 | ** |
| 809 | 810 | ** Applications must [sqlite3_finalize | finalize] all [prepared statements] |
| 810 | 811 | ** and [sqlite3_blob_close | close] all [BLOB handles] associated with |
| 811 | 812 | ** the [sqlite3] object prior to attempting to close the object. ^If |
| 812 | 813 | ** sqlite3_close() is called on a [database connection] that still has |
| | @@ -1229,16 +1230,25 @@ |
| 1229 | 1230 | ** layer a hint of how large the database file will grow to be during the |
| 1230 | 1231 | ** current transaction. This hint is not guaranteed to be accurate but it |
| 1231 | 1232 | ** is often close. The underlying VFS might choose to preallocate database |
| 1232 | 1233 | ** file space based on this hint in order to help writes to the database |
| 1233 | 1234 | ** file run faster. |
| 1235 | +** |
| 1236 | +** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS |
| 1237 | +** extends and truncates the database file in chunks of a size specified |
| 1238 | +** by the user. The fourth argument to [sqlite3_file_control()] should |
| 1239 | +** point to an integer (type int) containing the new chunk-size to use |
| 1240 | +** for the nominated database. Allocating database file space in large |
| 1241 | +** chunks (say 1MB at a time), may reduce file-system fragmentation and |
| 1242 | +** improve performance on some systems. |
| 1234 | 1243 | */ |
| 1235 | 1244 | #define SQLITE_FCNTL_LOCKSTATE 1 |
| 1236 | 1245 | #define SQLITE_GET_LOCKPROXYFILE 2 |
| 1237 | 1246 | #define SQLITE_SET_LOCKPROXYFILE 3 |
| 1238 | 1247 | #define SQLITE_LAST_ERRNO 4 |
| 1239 | 1248 | #define SQLITE_FCNTL_SIZE_HINT 5 |
| 1249 | +#define SQLITE_FCNTL_CHUNK_SIZE 6 |
| 1240 | 1250 | |
| 1241 | 1251 | /* |
| 1242 | 1252 | ** CAPI3REF: Mutex Handle |
| 1243 | 1253 | ** |
| 1244 | 1254 | ** The mutex module within SQLite defines [sqlite3_mutex] to be an |
| | @@ -3197,11 +3207,11 @@ |
| 3197 | 3207 | ** <li> @VVV |
| 3198 | 3208 | ** <li> $VVV |
| 3199 | 3209 | ** </ul> |
| 3200 | 3210 | ** |
| 3201 | 3211 | ** In the templates above, NNN represents an integer literal, |
| 3202 | | -** and VVV represents an alphanumeric identifer.)^ ^The values of these |
| 3212 | +** and VVV represents an alphanumeric identifier.)^ ^The values of these |
| 3203 | 3213 | ** parameters (also called "host parameter names" or "SQL parameters") |
| 3204 | 3214 | ** can be set using the sqlite3_bind_*() routines defined here. |
| 3205 | 3215 | ** |
| 3206 | 3216 | ** ^The first argument to the sqlite3_bind_*() routines is always |
| 3207 | 3217 | ** a pointer to the [sqlite3_stmt] object returned from |
| | @@ -3976,11 +3986,11 @@ |
| 3976 | 3986 | SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); |
| 3977 | 3987 | |
| 3978 | 3988 | /* |
| 3979 | 3989 | ** CAPI3REF: Obtain Aggregate Function Context |
| 3980 | 3990 | ** |
| 3981 | | -** Implementions of aggregate SQL functions use this |
| 3991 | +** Implementations of aggregate SQL functions use this |
| 3982 | 3992 | ** routine to allocate memory for storing their state. |
| 3983 | 3993 | ** |
| 3984 | 3994 | ** ^The first time the sqlite3_aggregate_context(C,N) routine is called |
| 3985 | 3995 | ** for a particular aggregate function, SQLite |
| 3986 | 3996 | ** allocates N of memory, zeroes out that memory, and returns a pointer |
| | @@ -4248,11 +4258,11 @@ |
| 4248 | 4258 | ** the routine expects pointers to 16-bit word aligned strings |
| 4249 | 4259 | ** of UTF-16 in the native byte order. |
| 4250 | 4260 | ** |
| 4251 | 4261 | ** A pointer to the user supplied routine must be passed as the fifth |
| 4252 | 4262 | ** argument. ^If it is NULL, this is the same as deleting the collation |
| 4253 | | -** sequence (so that SQLite cannot call it anymore). |
| 4263 | +** sequence (so that SQLite cannot call it any more). |
| 4254 | 4264 | ** ^Each time the application supplied function is invoked, it is passed |
| 4255 | 4265 | ** as its first parameter a copy of the void* passed as the fourth argument |
| 4256 | 4266 | ** to sqlite3_create_collation() or sqlite3_create_collation16(). |
| 4257 | 4267 | ** |
| 4258 | 4268 | ** ^The remaining arguments to the application-supplied routine are two strings, |
| | @@ -5466,11 +5476,11 @@ |
| 5466 | 5476 | ** of passing a NULL pointer instead of a valid mutex handle are undefined |
| 5467 | 5477 | ** (i.e. it is acceptable to provide an implementation that segfaults if |
| 5468 | 5478 | ** it is passed a NULL pointer). |
| 5469 | 5479 | ** |
| 5470 | 5480 | ** The xMutexInit() method must be threadsafe. ^It must be harmless to |
| 5471 | | -** invoke xMutexInit() mutiple times within the same process and without |
| 5481 | +** invoke xMutexInit() multiple times within the same process and without |
| 5472 | 5482 | ** intervening calls to xMutexEnd(). Second and subsequent calls to |
| 5473 | 5483 | ** xMutexInit() must be no-ops. |
| 5474 | 5484 | ** |
| 5475 | 5485 | ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] |
| 5476 | 5486 | ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory |
| | @@ -5636,11 +5646,11 @@ |
| 5636 | 5646 | |
| 5637 | 5647 | /* |
| 5638 | 5648 | ** CAPI3REF: SQLite Runtime Status |
| 5639 | 5649 | ** |
| 5640 | 5650 | ** ^This interface is used to retrieve runtime status information |
| 5641 | | -** about the preformance of SQLite, and optionally to reset various |
| 5651 | +** about the performance of SQLite, and optionally to reset various |
| 5642 | 5652 | ** highwater marks. ^The first argument is an integer code for |
| 5643 | 5653 | ** the specific parameter to measure. ^(Recognized integer codes |
| 5644 | 5654 | ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^ |
| 5645 | 5655 | ** ^The current value of the parameter is returned into *pCurrent. |
| 5646 | 5656 | ** ^The highest recorded value is returned in *pHighwater. ^If the |
| | @@ -5762,11 +5772,11 @@ |
| 5762 | 5772 | ** ^This interface is used to retrieve runtime status information |
| 5763 | 5773 | ** about a single [database connection]. ^The first argument is the |
| 5764 | 5774 | ** database connection object to be interrogated. ^The second argument |
| 5765 | 5775 | ** is an integer constant, taken from the set of |
| 5766 | 5776 | ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that |
| 5767 | | -** determiness the parameter to interrogate. The set of |
| 5777 | +** determines the parameter to interrogate. The set of |
| 5768 | 5778 | ** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely |
| 5769 | 5779 | ** to grow in future releases of SQLite. |
| 5770 | 5780 | ** |
| 5771 | 5781 | ** ^The current value of the requested parameter is written into *pCur |
| 5772 | 5782 | ** and the highest instantaneous value is written into *pHiwtr. ^If |
| | @@ -6184,11 +6194,11 @@ |
| 6184 | 6194 | ** |
| 6185 | 6195 | ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b> |
| 6186 | 6196 | ** |
| 6187 | 6197 | ** ^Each call to sqlite3_backup_step() sets two values inside |
| 6188 | 6198 | ** 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. |
| 6199 | +** up and the total number of pages in the source database file. |
| 6190 | 6200 | ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces |
| 6191 | 6201 | ** retrieve these two values, respectively. |
| 6192 | 6202 | ** |
| 6193 | 6203 | ** ^The values returned by these functions are only updated by |
| 6194 | 6204 | ** sqlite3_backup_step(). ^If the source database is modified during a backup |
| | @@ -6280,11 +6290,11 @@ |
| 6280 | 6290 | ** ^(There may be at most one unlock-notify callback registered by a |
| 6281 | 6291 | ** blocked connection. If sqlite3_unlock_notify() is called when the |
| 6282 | 6292 | ** blocked connection already has a registered unlock-notify callback, |
| 6283 | 6293 | ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is |
| 6284 | 6294 | ** called with a NULL pointer as its second argument, then any existing |
| 6285 | | -** unlock-notify callback is cancelled. ^The blocked connections |
| 6295 | +** unlock-notify callback is canceled. ^The blocked connections |
| 6286 | 6296 | ** unlock-notify callback may also be canceled by closing the blocked |
| 6287 | 6297 | ** connection using [sqlite3_close()]. |
| 6288 | 6298 | ** |
| 6289 | 6299 | ** The unlock-notify callback is not reentrant. If an application invokes |
| 6290 | 6300 | ** any sqlite3_xxx API functions from within an unlock-notify callback, a |
| | @@ -6362,11 +6372,11 @@ |
| 6362 | 6372 | /* |
| 6363 | 6373 | ** CAPI3REF: String Comparison |
| 6364 | 6374 | ** |
| 6365 | 6375 | ** ^The [sqlite3_strnicmp()] API allows applications and extensions to |
| 6366 | 6376 | ** compare the contents of two buffers containing UTF-8 strings in a |
| 6367 | | -** case-indendent fashion, using the same definition of case independence |
| 6377 | +** case-independent fashion, using the same definition of case independence |
| 6368 | 6378 | ** that SQLite uses internally when comparing identifiers. |
| 6369 | 6379 | */ |
| 6370 | 6380 | SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); |
| 6371 | 6381 | |
| 6372 | 6382 | /* |
| | @@ -7868,11 +7878,11 @@ |
| 7868 | 7878 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager); |
| 7869 | 7879 | SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); |
| 7870 | 7880 | |
| 7871 | 7881 | /* Functions used to configure a Pager object. */ |
| 7872 | 7882 | SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); |
| 7873 | | -SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int); |
| 7883 | +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int); |
| 7874 | 7884 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); |
| 7875 | 7885 | SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); |
| 7876 | 7886 | SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int); |
| 7877 | 7887 | SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int); |
| 7878 | 7888 | SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int); |
| | @@ -7895,11 +7905,11 @@ |
| 7895 | 7905 | SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*); |
| 7896 | 7906 | SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); |
| 7897 | 7907 | SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); |
| 7898 | 7908 | |
| 7899 | 7909 | /* Functions used to manage pager transactions and savepoints. */ |
| 7900 | | -SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*); |
| 7910 | +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*); |
| 7901 | 7911 | SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int); |
| 7902 | 7912 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); |
| 7903 | 7913 | SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*); |
| 7904 | 7914 | SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager); |
| 7905 | 7915 | SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*); |
| | @@ -9183,13 +9193,13 @@ |
| 9183 | 9193 | ** argument to sqlite3VdbeKeyCompare and is used to control the |
| 9184 | 9194 | ** comparison of the two index keys. |
| 9185 | 9195 | */ |
| 9186 | 9196 | struct KeyInfo { |
| 9187 | 9197 | sqlite3 *db; /* The database connection */ |
| 9188 | | - u8 enc; /* Text encoding - one of the TEXT_Utf* values */ |
| 9198 | + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ |
| 9189 | 9199 | u16 nField; /* Number of entries in aColl[] */ |
| 9190 | | - u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */ |
| 9200 | + u8 *aSortOrder; /* Sort order for each column. May be NULL */ |
| 9191 | 9201 | CollSeq *aColl[1]; /* Collating sequence for each term of the key */ |
| 9192 | 9202 | }; |
| 9193 | 9203 | |
| 9194 | 9204 | /* |
| 9195 | 9205 | ** An instance of the following structure holds information about a |
| | @@ -14331,11 +14341,11 @@ |
| 14331 | 14341 | |
| 14332 | 14342 | /* |
| 14333 | 14343 | ** Set the "type" of an allocation. |
| 14334 | 14344 | */ |
| 14335 | 14345 | SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){ |
| 14336 | | - if( p ){ |
| 14346 | + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 14337 | 14347 | struct MemBlockHdr *pHdr; |
| 14338 | 14348 | pHdr = sqlite3MemsysGetHeader(p); |
| 14339 | 14349 | assert( pHdr->iForeGuard==FOREGUARD ); |
| 14340 | 14350 | pHdr->eType = eType; |
| 14341 | 14351 | } |
| | @@ -14350,11 +14360,11 @@ |
| 14350 | 14360 | ** |
| 14351 | 14361 | ** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 14352 | 14362 | */ |
| 14353 | 14363 | SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 14354 | 14364 | int rc = 1; |
| 14355 | | - if( p ){ |
| 14365 | + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 14356 | 14366 | struct MemBlockHdr *pHdr; |
| 14357 | 14367 | pHdr = sqlite3MemsysGetHeader(p); |
| 14358 | 14368 | assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 14359 | 14369 | if( (pHdr->eType&eType)==0 ){ |
| 14360 | 14370 | rc = 0; |
| | @@ -14372,11 +14382,11 @@ |
| 14372 | 14382 | ** |
| 14373 | 14383 | ** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 14374 | 14384 | */ |
| 14375 | 14385 | SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){ |
| 14376 | 14386 | int rc = 1; |
| 14377 | | - if( p ){ |
| 14387 | + if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 14378 | 14388 | struct MemBlockHdr *pHdr; |
| 14379 | 14389 | pHdr = sqlite3MemsysGetHeader(p); |
| 14380 | 14390 | assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ |
| 14381 | 14391 | if( (pHdr->eType&eType)!=0 ){ |
| 14382 | 14392 | rc = 0; |
| | @@ -16624,10 +16634,11 @@ |
| 16624 | 16634 | #else |
| 16625 | 16635 | /* Use the built-in recursive mutexes if they are available. |
| 16626 | 16636 | */ |
| 16627 | 16637 | pthread_mutex_lock(&p->mutex); |
| 16628 | 16638 | #if SQLITE_MUTEX_NREF |
| 16639 | + assert( p->nRef>0 || p->owner==0 ); |
| 16629 | 16640 | p->owner = pthread_self(); |
| 16630 | 16641 | p->nRef++; |
| 16631 | 16642 | #endif |
| 16632 | 16643 | #endif |
| 16633 | 16644 | |
| | @@ -16696,10 +16707,11 @@ |
| 16696 | 16707 | */ |
| 16697 | 16708 | static void pthreadMutexLeave(sqlite3_mutex *p){ |
| 16698 | 16709 | assert( pthreadMutexHeld(p) ); |
| 16699 | 16710 | #if SQLITE_MUTEX_NREF |
| 16700 | 16711 | p->nRef--; |
| 16712 | + if( p->nRef==0 ) p->owner = 0; |
| 16701 | 16713 | #endif |
| 16702 | 16714 | assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 16703 | 16715 | |
| 16704 | 16716 | #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 16705 | 16717 | if( p->nRef==0 ){ |
| | @@ -16960,11 +16972,11 @@ |
| 16960 | 16972 | ** allocated mutex. SQLite is careful to deallocate every |
| 16961 | 16973 | ** mutex that it allocates. |
| 16962 | 16974 | */ |
| 16963 | 16975 | static void winMutexFree(sqlite3_mutex *p){ |
| 16964 | 16976 | assert( p ); |
| 16965 | | - assert( p->nRef==0 ); |
| 16977 | + assert( p->nRef==0 && p->owner==0 ); |
| 16966 | 16978 | assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 16967 | 16979 | DeleteCriticalSection(&p->mutex); |
| 16968 | 16980 | sqlite3_free(p); |
| 16969 | 16981 | } |
| 16970 | 16982 | |
| | @@ -16984,10 +16996,11 @@ |
| 16984 | 16996 | DWORD tid = GetCurrentThreadId(); |
| 16985 | 16997 | assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); |
| 16986 | 16998 | #endif |
| 16987 | 16999 | EnterCriticalSection(&p->mutex); |
| 16988 | 17000 | #ifdef SQLITE_DEBUG |
| 17001 | + assert( p->nRef>0 || p->owner==0 ); |
| 16989 | 17002 | p->owner = tid; |
| 16990 | 17003 | p->nRef++; |
| 16991 | 17004 | if( p->trace ){ |
| 16992 | 17005 | printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); |
| 16993 | 17006 | } |
| | @@ -17037,10 +17050,11 @@ |
| 17037 | 17050 | #ifndef NDEBUG |
| 17038 | 17051 | DWORD tid = GetCurrentThreadId(); |
| 17039 | 17052 | assert( p->nRef>0 ); |
| 17040 | 17053 | assert( p->owner==tid ); |
| 17041 | 17054 | p->nRef--; |
| 17055 | + if( p->nRef==0 ) p->owner = 0; |
| 17042 | 17056 | assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 17043 | 17057 | #endif |
| 17044 | 17058 | LeaveCriticalSection(&p->mutex); |
| 17045 | 17059 | #ifdef SQLITE_DEBUG |
| 17046 | 17060 | if( p->trace ){ |
| | @@ -22609,10 +22623,11 @@ |
| 22609 | 22623 | void *lockingContext; /* Locking style specific state */ |
| 22610 | 22624 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 22611 | 22625 | int fileFlags; /* Miscellanous flags */ |
| 22612 | 22626 | const char *zPath; /* Name of the file */ |
| 22613 | 22627 | unixShm *pShm; /* Shared memory segment information */ |
| 22628 | + int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
| 22614 | 22629 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 22615 | 22630 | int openFlags; /* The flags specified at open() */ |
| 22616 | 22631 | #endif |
| 22617 | 22632 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
| 22618 | 22633 | unsigned fsFlags; /* cached details from statfs() */ |
| | @@ -25367,19 +25382,21 @@ |
| 25367 | 25382 | offset += wrote; |
| 25368 | 25383 | pBuf = &((char*)pBuf)[wrote]; |
| 25369 | 25384 | } |
| 25370 | 25385 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 25371 | 25386 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 25387 | + |
| 25372 | 25388 | if( amt>0 ){ |
| 25373 | 25389 | if( wrote<0 ){ |
| 25374 | 25390 | /* lastErrno set by seekAndWrite */ |
| 25375 | 25391 | return SQLITE_IOERR_WRITE; |
| 25376 | 25392 | }else{ |
| 25377 | 25393 | pFile->lastErrno = 0; /* not a system error */ |
| 25378 | 25394 | return SQLITE_FULL; |
| 25379 | 25395 | } |
| 25380 | 25396 | } |
| 25397 | + |
| 25381 | 25398 | return SQLITE_OK; |
| 25382 | 25399 | } |
| 25383 | 25400 | |
| 25384 | 25401 | #ifdef SQLITE_TEST |
| 25385 | 25402 | /* |
| | @@ -25577,16 +25594,27 @@ |
| 25577 | 25594 | |
| 25578 | 25595 | /* |
| 25579 | 25596 | ** Truncate an open file to a specified size |
| 25580 | 25597 | */ |
| 25581 | 25598 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 25599 | + unixFile *pFile = (unixFile *)id; |
| 25582 | 25600 | int rc; |
| 25583 | | - assert( id ); |
| 25601 | + assert( pFile ); |
| 25584 | 25602 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 25585 | | - rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
| 25603 | + |
| 25604 | + /* If the user has configured a chunk-size for this file, truncate the |
| 25605 | + ** file so that it consists of an integer number of chunks (i.e. the |
| 25606 | + ** actual file size after the operation may be larger than the requested |
| 25607 | + ** size). |
| 25608 | + */ |
| 25609 | + if( pFile->szChunk ){ |
| 25610 | + nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 25611 | + } |
| 25612 | + |
| 25613 | + rc = ftruncate(pFile->h, (off_t)nByte); |
| 25586 | 25614 | if( rc ){ |
| 25587 | | - ((unixFile*)id)->lastErrno = errno; |
| 25615 | + pFile->lastErrno = errno; |
| 25588 | 25616 | return SQLITE_IOERR_TRUNCATE; |
| 25589 | 25617 | }else{ |
| 25590 | 25618 | #ifndef NDEBUG |
| 25591 | 25619 | /* If we are doing a normal write to a database file (as opposed to |
| 25592 | 25620 | ** doing a hot-journal rollback or a write to some file other than a |
| | @@ -25593,12 +25621,12 @@ |
| 25593 | 25621 | ** normal database file) and we truncate the file to zero length, |
| 25594 | 25622 | ** that effectively updates the change counter. This might happen |
| 25595 | 25623 | ** when restoring a database using the backup API from a zero-length |
| 25596 | 25624 | ** source. |
| 25597 | 25625 | */ |
| 25598 | | - if( ((unixFile*)id)->inNormalWrite && nByte==0 ){ |
| 25599 | | - ((unixFile*)id)->transCntrChng = 1; |
| 25626 | + if( pFile->inNormalWrite && nByte==0 ){ |
| 25627 | + pFile->transCntrChng = 1; |
| 25600 | 25628 | } |
| 25601 | 25629 | #endif |
| 25602 | 25630 | |
| 25603 | 25631 | return SQLITE_OK; |
| 25604 | 25632 | } |
| | @@ -25637,10 +25665,58 @@ |
| 25637 | 25665 | ** proxying locking division. |
| 25638 | 25666 | */ |
| 25639 | 25667 | static int proxyFileControl(sqlite3_file*,int,void*); |
| 25640 | 25668 | #endif |
| 25641 | 25669 | |
| 25670 | +/* |
| 25671 | +** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 25672 | +** file-control operation. |
| 25673 | +** |
| 25674 | +** If the user has configured a chunk-size for this file, it could be |
| 25675 | +** that the file needs to be extended at this point. Otherwise, the |
| 25676 | +** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix. |
| 25677 | +*/ |
| 25678 | +static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 25679 | + if( pFile->szChunk ){ |
| 25680 | + i64 nSize; /* Required file size */ |
| 25681 | + struct stat buf; /* Used to hold return values of fstat() */ |
| 25682 | + |
| 25683 | + if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
| 25684 | + |
| 25685 | + nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 25686 | + if( nSize>(i64)buf.st_size ){ |
| 25687 | +#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 25688 | + if( posix_fallocate(pFile->h, buf.st_size, nSize-buf.st_size) ){ |
| 25689 | + return SQLITE_IOERR_WRITE; |
| 25690 | + } |
| 25691 | +#else |
| 25692 | + /* If the OS does not have posix_fallocate(), fake it. First use |
| 25693 | + ** ftruncate() to set the file size, then write a single byte to |
| 25694 | + ** the last byte in each block within the extended region. This |
| 25695 | + ** is the same technique used by glibc to implement posix_fallocate() |
| 25696 | + ** on systems that do not have a real fallocate() system call. |
| 25697 | + */ |
| 25698 | + int nBlk = buf.st_blksize; /* File-system block size */ |
| 25699 | + i64 iWrite; /* Next offset to write to */ |
| 25700 | + int nWrite; /* Return value from seekAndWrite() */ |
| 25701 | + |
| 25702 | + if( ftruncate(pFile->h, nSize) ){ |
| 25703 | + pFile->lastErrno = errno; |
| 25704 | + return SQLITE_IOERR_TRUNCATE; |
| 25705 | + } |
| 25706 | + iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
| 25707 | + do { |
| 25708 | + nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 25709 | + iWrite += nBlk; |
| 25710 | + } while( nWrite==1 && iWrite<nSize ); |
| 25711 | + if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
| 25712 | +#endif |
| 25713 | + } |
| 25714 | + } |
| 25715 | + |
| 25716 | + return SQLITE_OK; |
| 25717 | +} |
| 25642 | 25718 | |
| 25643 | 25719 | /* |
| 25644 | 25720 | ** Information and control of an open file handle. |
| 25645 | 25721 | */ |
| 25646 | 25722 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| | @@ -25650,18 +25726,17 @@ |
| 25650 | 25726 | return SQLITE_OK; |
| 25651 | 25727 | } |
| 25652 | 25728 | case SQLITE_LAST_ERRNO: { |
| 25653 | 25729 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 25654 | 25730 | return SQLITE_OK; |
| 25731 | + } |
| 25732 | + case SQLITE_FCNTL_CHUNK_SIZE: { |
| 25733 | + ((unixFile*)id)->szChunk = *(int *)pArg; |
| 25734 | + return SQLITE_OK; |
| 25655 | 25735 | } |
| 25656 | 25736 | 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; |
| 25737 | + return fcntlSizeHint((unixFile *)id, *(i64 *)pArg); |
| 25663 | 25738 | } |
| 25664 | 25739 | #ifndef NDEBUG |
| 25665 | 25740 | /* The pager calls this method to signal that it has done |
| 25666 | 25741 | ** a rollback and that the database is therefore unchanged and |
| 25667 | 25742 | ** it hence it is OK for the transaction change counter to be |
| | @@ -28522,11 +28597,11 @@ |
| 28522 | 28597 | }else{ |
| 28523 | 28598 | if( pCtx->conchFile ){ |
| 28524 | 28599 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
| 28525 | 28600 | sqlite3_free(pCtx->conchFile); |
| 28526 | 28601 | } |
| 28527 | | - sqlite3_free(pCtx->lockProxyPath); |
| 28602 | + sqlite3DbFree(0, pCtx->lockProxyPath); |
| 28528 | 28603 | sqlite3_free(pCtx->conchFilePath); |
| 28529 | 28604 | sqlite3_free(pCtx); |
| 28530 | 28605 | } |
| 28531 | 28606 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 28532 | 28607 | (rc==SQLITE_OK ? "ok" : "failed"))); |
| | @@ -28713,13 +28788,13 @@ |
| 28713 | 28788 | } |
| 28714 | 28789 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 28715 | 28790 | if( rc ) return rc; |
| 28716 | 28791 | sqlite3_free(conchFile); |
| 28717 | 28792 | } |
| 28718 | | - sqlite3_free(pCtx->lockProxyPath); |
| 28793 | + sqlite3DbFree(0, pCtx->lockProxyPath); |
| 28719 | 28794 | sqlite3_free(pCtx->conchFilePath); |
| 28720 | | - sqlite3_free(pCtx->dbPath); |
| 28795 | + sqlite3DbFree(0, pCtx->dbPath); |
| 28721 | 28796 | /* restore the original locking context and pMethod then close it */ |
| 28722 | 28797 | pFile->lockingContext = pCtx->oldLockingContext; |
| 28723 | 28798 | pFile->pMethod = pCtx->pOldMethod; |
| 28724 | 28799 | sqlite3_free(pCtx); |
| 28725 | 28800 | return pFile->pMethod->xClose(id); |
| | @@ -29161,10 +29236,11 @@ |
| 29161 | 29236 | short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
| 29162 | 29237 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 29163 | 29238 | DWORD sectorSize; /* Sector size of the device file is on */ |
| 29164 | 29239 | winShm *pShm; /* Instance of shared memory on this file */ |
| 29165 | 29240 | const char *zPath; /* Full pathname of this file */ |
| 29241 | + int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */ |
| 29166 | 29242 | #if SQLITE_OS_WINCE |
| 29167 | 29243 | WCHAR *zDeleteOnClose; /* Name of file to delete when closing */ |
| 29168 | 29244 | HANDLE hMutex; /* Mutex used to control access to shared lock */ |
| 29169 | 29245 | HANDLE hShared; /* Shared memory segment used for locking */ |
| 29170 | 29246 | winceLock local; /* Locks obtained by this instance of winFile */ |
| | @@ -29671,10 +29747,46 @@ |
| 29671 | 29747 | |
| 29672 | 29748 | /***************************************************************************** |
| 29673 | 29749 | ** The next group of routines implement the I/O methods specified |
| 29674 | 29750 | ** by the sqlite3_io_methods object. |
| 29675 | 29751 | ******************************************************************************/ |
| 29752 | + |
| 29753 | +/* |
| 29754 | +** Some microsoft compilers lack this definition. |
| 29755 | +*/ |
| 29756 | +#ifndef INVALID_SET_FILE_POINTER |
| 29757 | +# define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 29758 | +#endif |
| 29759 | + |
| 29760 | +/* |
| 29761 | +** Move the current position of the file handle passed as the first |
| 29762 | +** argument to offset iOffset within the file. If successful, return 0. |
| 29763 | +** Otherwise, set pFile->lastErrno and return non-zero. |
| 29764 | +*/ |
| 29765 | +static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){ |
| 29766 | + LONG upperBits; /* Most sig. 32 bits of new offset */ |
| 29767 | + LONG lowerBits; /* Least sig. 32 bits of new offset */ |
| 29768 | + DWORD dwRet; /* Value returned by SetFilePointer() */ |
| 29769 | + |
| 29770 | + upperBits = (LONG)((iOffset>>32) & 0x7fffffff); |
| 29771 | + lowerBits = (LONG)(iOffset & 0xffffffff); |
| 29772 | + |
| 29773 | + /* API oddity: If successful, SetFilePointer() returns a dword |
| 29774 | + ** containing the lower 32-bits of the new file-offset. Or, if it fails, |
| 29775 | + ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, |
| 29776 | + ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine |
| 29777 | + ** whether an error has actually occured, it is also necessary to call |
| 29778 | + ** GetLastError(). |
| 29779 | + */ |
| 29780 | + dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 29781 | + if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){ |
| 29782 | + pFile->lastErrno = GetLastError(); |
| 29783 | + return 1; |
| 29784 | + } |
| 29785 | + |
| 29786 | + return 0; |
| 29787 | +} |
| 29676 | 29788 | |
| 29677 | 29789 | /* |
| 29678 | 29790 | ** Close a file. |
| 29679 | 29791 | ** |
| 29680 | 29792 | ** It is reported that an attempt to close a handle might sometimes |
| | @@ -29714,17 +29826,10 @@ |
| 29714 | 29826 | OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed")); |
| 29715 | 29827 | OpenCounter(-1); |
| 29716 | 29828 | return rc ? SQLITE_OK : SQLITE_IOERR; |
| 29717 | 29829 | } |
| 29718 | 29830 | |
| 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 | 29831 | /* |
| 29727 | 29832 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 29728 | 29833 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 29729 | 29834 | ** wrong. |
| 29730 | 29835 | */ |
| | @@ -29732,112 +29837,108 @@ |
| 29732 | 29837 | sqlite3_file *id, /* File to read from */ |
| 29733 | 29838 | void *pBuf, /* Write content into this buffer */ |
| 29734 | 29839 | int amt, /* Number of bytes to read */ |
| 29735 | 29840 | sqlite3_int64 offset /* Begin reading at this offset */ |
| 29736 | 29841 | ){ |
| 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; |
| 29842 | + winFile *pFile = (winFile*)id; /* file handle */ |
| 29843 | + DWORD nRead; /* Number of bytes actually read from file */ |
| 29743 | 29844 | |
| 29744 | 29845 | assert( id!=0 ); |
| 29745 | 29846 | SimulateIOError(return SQLITE_IOERR_READ); |
| 29746 | 29847 | 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; |
| 29848 | + |
| 29849 | + if( seekWinFile(pFile, offset) ){ |
| 29750 | 29850 | return SQLITE_FULL; |
| 29751 | 29851 | } |
| 29752 | | - if( !ReadFile(pFile->h, pBuf, amt, &got, 0) ){ |
| 29852 | + if( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ |
| 29753 | 29853 | pFile->lastErrno = GetLastError(); |
| 29754 | 29854 | return SQLITE_IOERR_READ; |
| 29755 | 29855 | } |
| 29756 | | - if( got==(DWORD)amt ){ |
| 29757 | | - return SQLITE_OK; |
| 29758 | | - }else{ |
| 29856 | + if( nRead<(DWORD)amt ){ |
| 29759 | 29857 | /* Unread parts of the buffer must be zero-filled */ |
| 29760 | | - memset(&((char*)pBuf)[got], 0, amt-got); |
| 29858 | + memset(&((char*)pBuf)[nRead], 0, amt-nRead); |
| 29761 | 29859 | return SQLITE_IOERR_SHORT_READ; |
| 29762 | 29860 | } |
| 29861 | + |
| 29862 | + return SQLITE_OK; |
| 29763 | 29863 | } |
| 29764 | 29864 | |
| 29765 | 29865 | /* |
| 29766 | 29866 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 29767 | 29867 | ** or some other error code on failure. |
| 29768 | 29868 | */ |
| 29769 | 29869 | 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 */ |
| 29870 | + sqlite3_file *id, /* File to write into */ |
| 29871 | + const void *pBuf, /* The bytes to be written */ |
| 29872 | + int amt, /* Number of bytes to write */ |
| 29873 | + sqlite3_int64 offset /* Offset into the file to begin writing at */ |
| 29774 | 29874 | ){ |
| 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 ); |
| 29875 | + int rc; /* True if error has occured, else false */ |
| 29876 | + winFile *pFile = (winFile*)id; /* File handle */ |
| 29877 | + |
| 29878 | + assert( amt>0 ); |
| 29879 | + assert( pFile ); |
| 29783 | 29880 | SimulateIOError(return SQLITE_IOERR_WRITE); |
| 29784 | 29881 | 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 | | - } |
| 29882 | + |
| 29883 | + OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype)); |
| 29884 | + |
| 29885 | + rc = seekWinFile(pFile, offset); |
| 29886 | + if( rc==0 ){ |
| 29887 | + u8 *aRem = (u8 *)pBuf; /* Data yet to be written */ |
| 29888 | + int nRem = amt; /* Number of bytes yet to be written */ |
| 29889 | + DWORD nWrite; /* Bytes written by each WriteFile() call */ |
| 29890 | + |
| 29891 | + while( nRem>0 && WriteFile(pFile->h, aRem, nRem, &nWrite, 0) && nWrite>0 ){ |
| 29892 | + aRem += nWrite; |
| 29893 | + nRem -= nWrite; |
| 29894 | + } |
| 29895 | + if( nRem>0 ){ |
| 29896 | + pFile->lastErrno = GetLastError(); |
| 29897 | + rc = 1; |
| 29898 | + } |
| 29899 | + } |
| 29900 | + |
| 29901 | + if( rc ){ |
| 29902 | + if( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ){ |
| 29903 | + return SQLITE_FULL; |
| 29904 | + } |
| 29905 | + return SQLITE_IOERR_WRITE; |
| 29811 | 29906 | } |
| 29812 | 29907 | return SQLITE_OK; |
| 29813 | 29908 | } |
| 29814 | 29909 | |
| 29815 | 29910 | /* |
| 29816 | 29911 | ** Truncate an open file to a specified size |
| 29817 | 29912 | */ |
| 29818 | 29913 | 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 ); |
| 29914 | + winFile *pFile = (winFile*)id; /* File handle object */ |
| 29915 | + int rc = SQLITE_OK; /* Return code for this function */ |
| 29916 | + |
| 29917 | + assert( pFile ); |
| 29918 | + |
| 29827 | 29919 | OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte)); |
| 29828 | 29920 | 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; |
| 29921 | + |
| 29922 | + /* If the user has configured a chunk-size for this file, truncate the |
| 29923 | + ** file so that it consists of an integer number of chunks (i.e. the |
| 29924 | + ** actual file size after the operation may be larger than the requested |
| 29925 | + ** size). |
| 29926 | + */ |
| 29927 | + if( pFile->szChunk ){ |
| 29928 | + nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 29929 | + } |
| 29930 | + |
| 29931 | + /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ |
| 29932 | + if( seekWinFile(pFile, nByte) ){ |
| 29832 | 29933 | rc = SQLITE_IOERR_TRUNCATE; |
| 29833 | | - /* SetEndOfFile will fail if nByte is negative */ |
| 29834 | | - }else if( !SetEndOfFile(pFile->h) ){ |
| 29934 | + }else if( 0==SetEndOfFile(pFile->h) ){ |
| 29835 | 29935 | pFile->lastErrno = GetLastError(); |
| 29836 | 29936 | rc = SQLITE_IOERR_TRUNCATE; |
| 29837 | 29937 | } |
| 29838 | | - OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc==SQLITE_OK ? "ok" : "failed")); |
| 29938 | + |
| 29939 | + OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok")); |
| 29839 | 29940 | return rc; |
| 29840 | 29941 | } |
| 29841 | 29942 | |
| 29842 | 29943 | #ifdef SQLITE_TEST |
| 29843 | 29944 | /* |
| | @@ -30197,10 +30298,14 @@ |
| 30197 | 30298 | return SQLITE_OK; |
| 30198 | 30299 | } |
| 30199 | 30300 | case SQLITE_LAST_ERRNO: { |
| 30200 | 30301 | *(int*)pArg = (int)((winFile*)id)->lastErrno; |
| 30201 | 30302 | return SQLITE_OK; |
| 30303 | + } |
| 30304 | + case SQLITE_FCNTL_CHUNK_SIZE: { |
| 30305 | + ((winFile*)id)->szChunk = *(int *)pArg; |
| 30306 | + return SQLITE_OK; |
| 30202 | 30307 | } |
| 30203 | 30308 | case SQLITE_FCNTL_SIZE_HINT: { |
| 30204 | 30309 | sqlite3_int64 sz = *(sqlite3_int64*)pArg; |
| 30205 | 30310 | SimulateIOErrorBenign(1); |
| 30206 | 30311 | winTruncate(id, sz); |
| | @@ -31749,11 +31854,11 @@ |
| 31749 | 31854 | ** start of a transaction, and is thus usually less than a few thousand, |
| 31750 | 31855 | ** but can be as large as 2 billion for a really big database. |
| 31751 | 31856 | */ |
| 31752 | 31857 | |
| 31753 | 31858 | /* Size of the Bitvec structure in bytes. */ |
| 31754 | | -#define BITVEC_SZ (sizeof(void*)*128) /* 512 on 32bit. 1024 on 64bit */ |
| 31859 | +#define BITVEC_SZ 512 |
| 31755 | 31860 | |
| 31756 | 31861 | /* Round the union size down to the nearest pointer boundary, since that's how |
| 31757 | 31862 | ** it will be aligned within the Bitvec struct. */ |
| 31758 | 31863 | #define BITVEC_USIZE (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) |
| 31759 | 31864 | |
| | @@ -32907,10 +33012,29 @@ |
| 32907 | 33012 | sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize); |
| 32908 | 33013 | sqlite3_free(p); |
| 32909 | 33014 | } |
| 32910 | 33015 | } |
| 32911 | 33016 | |
| 33017 | +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 33018 | +/* |
| 33019 | +** Return the size of a pache allocation |
| 33020 | +*/ |
| 33021 | +static int pcache1MemSize(void *p){ |
| 33022 | + assert( sqlite3_mutex_held(pcache1.mutex) ); |
| 33023 | + if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 33024 | + return pcache1.szSlot; |
| 33025 | + }else{ |
| 33026 | + int iSize; |
| 33027 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 33028 | + sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 33029 | + iSize = sqlite3MallocSize(p); |
| 33030 | + sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 33031 | + return iSize; |
| 33032 | + } |
| 33033 | +} |
| 33034 | +#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 33035 | + |
| 32912 | 33036 | /* |
| 32913 | 33037 | ** Allocate a new page object initially associated with cache pCache. |
| 32914 | 33038 | */ |
| 32915 | 33039 | static PgHdr1 *pcache1AllocPage(PCache1 *pCache){ |
| 32916 | 33040 | int nByte = sizeof(PgHdr1) + pCache->szPage; |
| | @@ -33455,11 +33579,11 @@ |
| 33455 | 33579 | int nFree = 0; |
| 33456 | 33580 | if( pcache1.pStart==0 ){ |
| 33457 | 33581 | PgHdr1 *p; |
| 33458 | 33582 | pcache1EnterMutex(); |
| 33459 | 33583 | while( (nReq<0 || nFree<nReq) && (p=pcache1.pLruTail) ){ |
| 33460 | | - nFree += sqlite3MallocSize(PGHDR1_TO_PAGE(p)); |
| 33584 | + nFree += pcache1MemSize(PGHDR1_TO_PAGE(p)); |
| 33461 | 33585 | pcache1PinPage(p); |
| 33462 | 33586 | pcache1RemoveFromHash(p); |
| 33463 | 33587 | pcache1FreePage(p); |
| 33464 | 33588 | } |
| 33465 | 33589 | pcache1LeaveMutex(); |
| | @@ -33964,11 +34088,11 @@ |
| 33964 | 34088 | # define sqlite3WalOpen(x,y,z) 0 |
| 33965 | 34089 | # define sqlite3WalClose(w,x,y,z) 0 |
| 33966 | 34090 | # define sqlite3WalBeginReadTransaction(y,z) 0 |
| 33967 | 34091 | # define sqlite3WalEndReadTransaction(z) |
| 33968 | 34092 | # define sqlite3WalRead(v,w,x,y,z) 0 |
| 33969 | | -# define sqlite3WalDbsize(y,z) |
| 34093 | +# define sqlite3WalDbsize(y) 0 |
| 33970 | 34094 | # define sqlite3WalBeginWriteTransaction(y) 0 |
| 33971 | 34095 | # define sqlite3WalEndWriteTransaction(x) 0 |
| 33972 | 34096 | # define sqlite3WalUndo(x,y,z) 0 |
| 33973 | 34097 | # define sqlite3WalSavepoint(y,z) |
| 33974 | 34098 | # define sqlite3WalSavepointUndo(y,z) 0 |
| | @@ -34000,13 +34124,12 @@ |
| 34000 | 34124 | SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal); |
| 34001 | 34125 | |
| 34002 | 34126 | /* Read a page from the write-ahead log, if it is present. */ |
| 34003 | 34127 | SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut); |
| 34004 | 34128 | |
| 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); |
| 34129 | +/* If the WAL is not empty, return the size of the database. */ |
| 34130 | +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal); |
| 34008 | 34131 | |
| 34009 | 34132 | /* Obtain or release the WRITER lock. */ |
| 34010 | 34133 | SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal); |
| 34011 | 34134 | SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal); |
| 34012 | 34135 | |
| | @@ -34048,12 +34171,16 @@ |
| 34048 | 34171 | #endif /* _WAL_H_ */ |
| 34049 | 34172 | |
| 34050 | 34173 | /************** End of wal.h *************************************************/ |
| 34051 | 34174 | /************** Continuing where we left off in pager.c **********************/ |
| 34052 | 34175 | |
| 34053 | | -/* |
| 34054 | | -******************** NOTES ON THE DESIGN OF THE PAGER ************************ |
| 34176 | + |
| 34177 | +/******************* NOTES ON THE DESIGN OF THE PAGER ************************ |
| 34178 | +** |
| 34179 | +** This comment block describes invariants that hold when using a rollback |
| 34180 | +** journal. These invariants do not apply for journal_mode=WAL, |
| 34181 | +** journal_mode=MEMORY, or journal_mode=OFF. |
| 34055 | 34182 | ** |
| 34056 | 34183 | ** Within this comment block, a page is deemed to have been synced |
| 34057 | 34184 | ** automatically as soon as it is written when PRAGMA synchronous=OFF. |
| 34058 | 34185 | ** Otherwise, the page is not synced until the xSync method of the VFS |
| 34059 | 34186 | ** is called successfully on the file containing the page. |
| | @@ -34083,11 +34210,11 @@ |
| 34083 | 34210 | ** both the content in the database when the rollback journal was written |
| 34084 | 34211 | ** and the content in the database at the beginning of the current |
| 34085 | 34212 | ** transaction. |
| 34086 | 34213 | ** |
| 34087 | 34214 | ** (3) Writes to the database file are an integer multiple of the page size |
| 34088 | | -** in length and are aligned to a page boundary. |
| 34215 | +** in length and are aligned on a page boundary. |
| 34089 | 34216 | ** |
| 34090 | 34217 | ** (4) Reads from the database file are either aligned on a page boundary and |
| 34091 | 34218 | ** an integer multiple of the page size in length or are taken from the |
| 34092 | 34219 | ** first 100 bytes of the database file. |
| 34093 | 34220 | ** |
| | @@ -34114,11 +34241,12 @@ |
| 34114 | 34241 | ** method is a no-op, but that does not change the fact the SQLite will |
| 34115 | 34242 | ** invoke it.) |
| 34116 | 34243 | ** |
| 34117 | 34244 | ** (9) Whenever the database file is modified, at least one bit in the range |
| 34118 | 34245 | ** of bytes from 24 through 39 inclusive will be changed prior to releasing |
| 34119 | | -** the EXCLUSIVE lock. |
| 34246 | +** the EXCLUSIVE lock, thus signaling other connections on the same |
| 34247 | +** database to flush their caches. |
| 34120 | 34248 | ** |
| 34121 | 34249 | ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less |
| 34122 | 34250 | ** than one billion transactions. |
| 34123 | 34251 | ** |
| 34124 | 34252 | ** (11) A database file is well-formed at the beginning and at the conclusion |
| | @@ -34127,11 +34255,12 @@ |
| 34127 | 34255 | ** (12) An EXCLUSIVE lock is held on the database file when writing to |
| 34128 | 34256 | ** the database file. |
| 34129 | 34257 | ** |
| 34130 | 34258 | ** (13) A SHARED lock is held on the database file while reading any |
| 34131 | 34259 | ** content out of the database file. |
| 34132 | | -*/ |
| 34260 | +** |
| 34261 | +******************************************************************************/ |
| 34133 | 34262 | |
| 34134 | 34263 | /* |
| 34135 | 34264 | ** Macros for troubleshooting. Normally turned off |
| 34136 | 34265 | */ |
| 34137 | 34266 | #if 0 |
| | @@ -34152,62 +34281,283 @@ |
| 34152 | 34281 | */ |
| 34153 | 34282 | #define PAGERID(p) ((int)(p->fd)) |
| 34154 | 34283 | #define FILEHANDLEID(fd) ((int)fd) |
| 34155 | 34284 | |
| 34156 | 34285 | /* |
| 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 |
| 34286 | +** The Pager.eState variable stores the current 'state' of a pager. A |
| 34287 | +** pager may be in any one of the seven states shown in the following |
| 34288 | +** state diagram. |
| 34289 | +** |
| 34290 | +** OPEN <------+------+ |
| 34291 | +** | | | |
| 34292 | +** V | | |
| 34293 | +** +---------> READER-------+ | |
| 34294 | +** | | | |
| 34295 | +** | V | |
| 34296 | +** |<-------WRITER_LOCKED------> ERROR |
| 34297 | +** | | ^ |
| 34298 | +** | V | |
| 34299 | +** |<------WRITER_CACHEMOD-------->| |
| 34300 | +** | | | |
| 34301 | +** | V | |
| 34302 | +** |<-------WRITER_DBMOD---------->| |
| 34303 | +** | | | |
| 34304 | +** | V | |
| 34305 | +** +<------WRITER_FINISHED-------->+ |
| 34306 | +** |
| 34307 | +** |
| 34308 | +** List of state transitions and the C [function] that performs each: |
| 34309 | +** |
| 34310 | +** OPEN -> READER [sqlite3PagerSharedLock] |
| 34311 | +** READER -> OPEN [pager_unlock] |
| 34312 | +** |
| 34313 | +** READER -> WRITER_LOCKED [sqlite3PagerBegin] |
| 34314 | +** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal] |
| 34315 | +** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal] |
| 34316 | +** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne] |
| 34317 | +** WRITER_*** -> READER [pager_end_transaction] |
| 34318 | +** |
| 34319 | +** WRITER_*** -> ERROR [pager_error] |
| 34320 | +** ERROR -> OPEN [pager_unlock] |
| 34321 | +** |
| 34322 | +** |
| 34323 | +** OPEN: |
| 34324 | +** |
| 34325 | +** The pager starts up in this state. Nothing is guaranteed in this |
| 34326 | +** state - the file may or may not be locked and the database size is |
| 34327 | +** unknown. The database may not be read or written. |
| 34328 | +** |
| 34329 | +** * No read or write transaction is active. |
| 34330 | +** * Any lock, or no lock at all, may be held on the database file. |
| 34331 | +** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted. |
| 34332 | +** |
| 34333 | +** READER: |
| 34334 | +** |
| 34335 | +** In this state all the requirements for reading the database in |
| 34336 | +** rollback (non-WAL) mode are met. Unless the pager is (or recently |
| 34337 | +** was) in exclusive-locking mode, a user-level read transaction is |
| 34338 | +** open. The database size is known in this state. |
| 34339 | +** |
| 34340 | +** A connection running with locking_mode=normal enters this state when |
| 34341 | +** it opens a read-transaction on the database and returns to state |
| 34342 | +** OPEN after the read-transaction is completed. However a connection |
| 34343 | +** running in locking_mode=exclusive (including temp databases) remains in |
| 34344 | +** this state even after the read-transaction is closed. The only way |
| 34345 | +** a locking_mode=exclusive connection can transition from READER to OPEN |
| 34346 | +** is via the ERROR state (see below). |
| 34347 | +** |
| 34348 | +** * A read transaction may be active (but a write-transaction cannot). |
| 34349 | +** * A SHARED or greater lock is held on the database file. |
| 34350 | +** * The dbSize variable may be trusted (even if a user-level read |
| 34351 | +** transaction is not active). The dbOrigSize and dbFileSize variables |
| 34352 | +** may not be trusted at this point. |
| 34353 | +** * If the database is a WAL database, then the WAL connection is open. |
| 34354 | +** * Even if a read-transaction is not open, it is guaranteed that |
| 34355 | +** there is no hot-journal in the file-system. |
| 34356 | +** |
| 34357 | +** WRITER_LOCKED: |
| 34358 | +** |
| 34359 | +** The pager moves to this state from READER when a write-transaction |
| 34360 | +** is first opened on the database. In WRITER_LOCKED state, all locks |
| 34361 | +** required to start a write-transaction are held, but no actual |
| 34362 | +** modifications to the cache or database have taken place. |
| 34363 | +** |
| 34364 | +** In rollback mode, a RESERVED or (if the transaction was opened with |
| 34365 | +** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when |
| 34366 | +** moving to this state, but the journal file is not written to or opened |
| 34367 | +** to in this state. If the transaction is committed or rolled back while |
| 34368 | +** in WRITER_LOCKED state, all that is required is to unlock the database |
| 34369 | +** file. |
| 34370 | +** |
| 34371 | +** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file. |
| 34372 | +** If the connection is running with locking_mode=exclusive, an attempt |
| 34373 | +** is made to obtain an EXCLUSIVE lock on the database file. |
| 34374 | +** |
| 34375 | +** * A write transaction is active. |
| 34376 | +** * If the connection is open in rollback-mode, a RESERVED or greater |
| 34377 | +** lock is held on the database file. |
| 34378 | +** * If the connection is open in WAL-mode, a WAL write transaction |
| 34379 | +** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully |
| 34380 | +** called). |
| 34381 | +** * The dbSize, dbOrigSize and dbFileSize variables are all valid. |
| 34382 | +** * The contents of the pager cache have not been modified. |
| 34383 | +** * The journal file may or may not be open. |
| 34384 | +** * Nothing (not even the first header) has been written to the journal. |
| 34385 | +** |
| 34386 | +** WRITER_CACHEMOD: |
| 34387 | +** |
| 34388 | +** A pager moves from WRITER_LOCKED state to this state when a page is |
| 34389 | +** first modified by the upper layer. In rollback mode the journal file |
| 34390 | +** is opened (if it is not already open) and a header written to the |
| 34391 | +** start of it. The database file on disk has not been modified. |
| 34392 | +** |
| 34393 | +** * A write transaction is active. |
| 34394 | +** * A RESERVED or greater lock is held on the database file. |
| 34395 | +** * The journal file is open and the first header has been written |
| 34396 | +** to it, but the header has not been synced to disk. |
| 34397 | +** * The contents of the page cache have been modified. |
| 34398 | +** |
| 34399 | +** WRITER_DBMOD: |
| 34400 | +** |
| 34401 | +** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state |
| 34402 | +** when it modifies the contents of the database file. WAL connections |
| 34403 | +** never enter this state (since they do not modify the database file, |
| 34404 | +** just the log file). |
| 34405 | +** |
| 34406 | +** * A write transaction is active. |
| 34407 | +** * An EXCLUSIVE or greater lock is held on the database file. |
| 34408 | +** * The journal file is open and the first header has been written |
| 34409 | +** and synced to disk. |
| 34410 | +** * The contents of the page cache have been modified (and possibly |
| 34411 | +** written to disk). |
| 34412 | +** |
| 34413 | +** WRITER_FINISHED: |
| 34414 | +** |
| 34415 | +** It is not possible for a WAL connection to enter this state. |
| 34416 | +** |
| 34417 | +** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD |
| 34418 | +** state after the entire transaction has been successfully written into the |
| 34419 | +** database file. In this state the transaction may be committed simply |
| 34420 | +** by finalizing the journal file. Once in WRITER_FINISHED state, it is |
| 34421 | +** not possible to modify the database further. At this point, the upper |
| 34422 | +** layer must either commit or rollback the transaction. |
| 34423 | +** |
| 34424 | +** * A write transaction is active. |
| 34425 | +** * An EXCLUSIVE or greater lock is held on the database file. |
| 34426 | +** * All writing and syncing of journal and database data has finished. |
| 34427 | +** If no error occured, all that remains is to finalize the journal to |
| 34428 | +** commit the transaction. If an error did occur, the caller will need |
| 34429 | +** to rollback the transaction. |
| 34430 | +** |
| 34431 | +** ERROR: |
| 34432 | +** |
| 34433 | +** The ERROR state is entered when an IO or disk-full error (including |
| 34434 | +** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it |
| 34435 | +** difficult to be sure that the in-memory pager state (cache contents, |
| 34436 | +** db size etc.) are consistent with the contents of the file-system. |
| 34437 | +** |
| 34438 | +** Temporary pager files may enter the ERROR state, but in-memory pagers |
| 34439 | +** cannot. |
| 34440 | +** |
| 34441 | +** For example, if an IO error occurs while performing a rollback, |
| 34442 | +** the contents of the page-cache may be left in an inconsistent state. |
| 34443 | +** At this point it would be dangerous to change back to READER state |
| 34444 | +** (as usually happens after a rollback). Any subsequent readers might |
| 34445 | +** report database corruption (due to the inconsistent cache), and if |
| 34446 | +** they upgrade to writers, they may inadvertently corrupt the database |
| 34447 | +** file. To avoid this hazard, the pager switches into the ERROR state |
| 34448 | +** instead of READER following such an error. |
| 34449 | +** |
| 34450 | +** Once it has entered the ERROR state, any attempt to use the pager |
| 34451 | +** to read or write data returns an error. Eventually, once all |
| 34452 | +** outstanding transactions have been abandoned, the pager is able to |
| 34453 | +** transition back to OPEN state, discarding the contents of the |
| 34454 | +** page-cache and any other in-memory state at the same time. Everything |
| 34455 | +** is reloaded from disk (and, if necessary, hot-journal rollback peformed) |
| 34456 | +** when a read-transaction is next opened on the pager (transitioning |
| 34457 | +** the pager into READER state). At that point the system has recovered |
| 34458 | +** from the error. |
| 34459 | +** |
| 34460 | +** Specifically, the pager jumps into the ERROR state if: |
| 34461 | +** |
| 34462 | +** 1. An error occurs while attempting a rollback. This happens in |
| 34463 | +** function sqlite3PagerRollback(). |
| 34464 | +** |
| 34465 | +** 2. An error occurs while attempting to finalize a journal file |
| 34466 | +** following a commit in function sqlite3PagerCommitPhaseTwo(). |
| 34467 | +** |
| 34468 | +** 3. An error occurs while attempting to write to the journal or |
| 34469 | +** database file in function pagerStress() in order to free up |
| 34470 | +** memory. |
| 34471 | +** |
| 34472 | +** In other cases, the error is returned to the b-tree layer. The b-tree |
| 34473 | +** layer then attempts a rollback operation. If the error condition |
| 34474 | +** persists, the pager enters the ERROR state via condition (1) above. |
| 34475 | +** |
| 34476 | +** Condition (3) is necessary because it can be triggered by a read-only |
| 34477 | +** statement executed within a transaction. In this case, if the error |
| 34478 | +** code were simply returned to the user, the b-tree layer would not |
| 34479 | +** automatically attempt a rollback, as it assumes that an error in a |
| 34480 | +** read-only statement cannot leave the pager in an internally inconsistent |
| 34481 | +** state. |
| 34482 | +** |
| 34483 | +** * The Pager.errCode variable is set to something other than SQLITE_OK. |
| 34484 | +** * There are one or more outstanding references to pages (after the |
| 34485 | +** last reference is dropped the pager should move back to OPEN state). |
| 34486 | +** * The pager is not an in-memory pager. |
| 34487 | +** |
| 34488 | +** |
| 34489 | +** Notes: |
| 34490 | +** |
| 34491 | +** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the |
| 34492 | +** connection is open in WAL mode. A WAL connection is always in one |
| 34493 | +** of the first four states. |
| 34494 | +** |
| 34495 | +** * Normally, a connection open in exclusive mode is never in PAGER_OPEN |
| 34496 | +** state. There are two exceptions: immediately after exclusive-mode has |
| 34497 | +** been turned on (and before any read or write transactions are |
| 34498 | +** executed), and when the pager is leaving the "error state". |
| 34499 | +** |
| 34500 | +** * See also: assert_pager_state(). |
| 34501 | +*/ |
| 34502 | +#define PAGER_OPEN 0 |
| 34503 | +#define PAGER_READER 1 |
| 34504 | +#define PAGER_WRITER_LOCKED 2 |
| 34505 | +#define PAGER_WRITER_CACHEMOD 3 |
| 34506 | +#define PAGER_WRITER_DBMOD 4 |
| 34507 | +#define PAGER_WRITER_FINISHED 5 |
| 34508 | +#define PAGER_ERROR 6 |
| 34509 | + |
| 34510 | +/* |
| 34511 | +** The Pager.eLock variable is almost always set to one of the |
| 34512 | +** following locking-states, according to the lock currently held on |
| 34513 | +** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. |
| 34514 | +** This variable is kept up to date as locks are taken and released by |
| 34515 | +** the pagerLockDb() and pagerUnlockDb() wrappers. |
| 34516 | +** |
| 34517 | +** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY |
| 34518 | +** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not |
| 34519 | +** the operation was successful. In these circumstances pagerLockDb() and |
| 34520 | +** pagerUnlockDb() take a conservative approach - eLock is always updated |
| 34521 | +** when unlocking the file, and only updated when locking the file if the |
| 34522 | +** VFS call is successful. This way, the Pager.eLock variable may be set |
| 34523 | +** to a less exclusive (lower) value than the lock that is actually held |
| 34524 | +** at the system level, but it is never set to a more exclusive value. |
| 34525 | +** |
| 34526 | +** This is usually safe. If an xUnlock fails or appears to fail, there may |
| 34527 | +** be a few redundant xLock() calls or a lock may be held for longer than |
| 34528 | +** required, but nothing really goes wrong. |
| 34529 | +** |
| 34530 | +** The exception is when the database file is unlocked as the pager moves |
| 34531 | +** from ERROR to OPEN state. At this point there may be a hot-journal file |
| 34532 | +** in the file-system that needs to be rolled back (as part of a OPEN->SHARED |
| 34533 | +** transition, by the same pager or any other). If the call to xUnlock() |
| 34534 | +** fails at this point and the pager is left holding an EXCLUSIVE lock, this |
| 34535 | +** can confuse the call to xCheckReservedLock() call made later as part |
| 34536 | +** of hot-journal detection. |
| 34537 | +** |
| 34538 | +** xCheckReservedLock() is defined as returning true "if there is a RESERVED |
| 34539 | +** lock held by this process or any others". So xCheckReservedLock may |
| 34540 | +** return true because the caller itself is holding an EXCLUSIVE lock (but |
| 34541 | +** doesn't know it because of a previous error in xUnlock). If this happens |
| 34542 | +** a hot-journal may be mistaken for a journal being created by an active |
| 34543 | +** transaction in another process, causing SQLite to read from the database |
| 34544 | +** without rolling it back. |
| 34545 | +** |
| 34546 | +** To work around this, if a call to xUnlock() fails when unlocking the |
| 34547 | +** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It |
| 34548 | +** is only changed back to a real locking state after a successful call |
| 34549 | +** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition |
| 34550 | +** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK |
| 34551 | +** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE |
| 34552 | +** lock on the database file before attempting to roll it back. See function |
| 34553 | +** PagerSharedLock() for more detail. |
| 34554 | +** |
| 34555 | +** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in |
| 34556 | +** PAGER_OPEN state. |
| 34557 | +*/ |
| 34558 | +#define UNKNOWN_LOCK (EXCLUSIVE_LOCK+1) |
| 34209 | 34559 | |
| 34210 | 34560 | /* |
| 34211 | 34561 | ** A macro used for invoking the codec if there is one |
| 34212 | 34562 | */ |
| 34213 | 34563 | #ifdef SQLITE_HAS_CODEC |
| | @@ -34253,37 +34603,32 @@ |
| 34253 | 34603 | u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */ |
| 34254 | 34604 | #endif |
| 34255 | 34605 | }; |
| 34256 | 34606 | |
| 34257 | 34607 | /* |
| 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? |
| 34608 | +** A open page cache is an instance of struct Pager. A description of |
| 34609 | +** some of the more important member variables follows: |
| 34610 | +** |
| 34611 | +** eState |
| 34612 | +** |
| 34613 | +** The current 'state' of the pager object. See the comment and state |
| 34614 | +** diagram above for a description of the pager state. |
| 34615 | +** |
| 34616 | +** eLock |
| 34617 | +** |
| 34618 | +** For a real on-disk database, the current lock held on the database file - |
| 34619 | +** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK. |
| 34620 | +** |
| 34621 | +** For a temporary or in-memory database (neither of which require any |
| 34622 | +** locks), this variable is always set to EXCLUSIVE_LOCK. Since such |
| 34623 | +** databases always have Pager.exclusiveMode==1, this tricks the pager |
| 34624 | +** logic into thinking that it already has all the locks it will ever |
| 34625 | +** need (and no reason to release them). |
| 34626 | +** |
| 34627 | +** In some (obscure) circumstances, this variable may also be set to |
| 34628 | +** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for |
| 34629 | +** details. |
| 34285 | 34630 | ** |
| 34286 | 34631 | ** changeCountDone |
| 34287 | 34632 | ** |
| 34288 | 34633 | ** This boolean variable is used to make sure that the change-counter |
| 34289 | 34634 | ** (the 4-byte header field at byte offset 24 of the database file) is |
| | @@ -34298,28 +34643,10 @@ |
| 34298 | 34643 | ** |
| 34299 | 34644 | ** This mechanism means that when running in exclusive mode, a connection |
| 34300 | 34645 | ** need only update the change-counter once, for the first transaction |
| 34301 | 34646 | ** committed. |
| 34302 | 34647 | ** |
| 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 | 34648 | ** setMaster |
| 34322 | 34649 | ** |
| 34323 | 34650 | ** When PagerCommitPhaseOne() is called to commit a transaction, it may |
| 34324 | 34651 | ** (or may not) specify a master-journal name to be written into the |
| 34325 | 34652 | ** journal file before it is synced to disk. |
| | @@ -34326,84 +34653,146 @@ |
| 34326 | 34653 | ** |
| 34327 | 34654 | ** Whether or not a journal file contains a master-journal pointer affects |
| 34328 | 34655 | ** the way in which the journal file is finalized after the transaction is |
| 34329 | 34656 | ** committed or rolled back when running in "journal_mode=PERSIST" mode. |
| 34330 | 34657 | ** 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. |
| 34658 | +** finalized by overwriting the first journal header with zeroes. If |
| 34659 | +** it does contain a master-journal pointer the journal file is finalized |
| 34660 | +** by truncating it to zero bytes, just as if the connection were |
| 34661 | +** running in "journal_mode=truncate" mode. |
| 34335 | 34662 | ** |
| 34336 | 34663 | ** Journal files that contain master journal pointers cannot be finalized |
| 34337 | 34664 | ** simply by overwriting the first journal-header with zeroes, as the |
| 34338 | 34665 | ** master journal pointer could interfere with hot-journal rollback of any |
| 34339 | 34666 | ** subsequently interrupted transaction that reuses the journal file. |
| 34340 | 34667 | ** |
| 34341 | 34668 | ** The flag is cleared as soon as the journal file is finalized (either |
| 34342 | 34669 | ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the |
| 34343 | 34670 | ** journal file from being successfully finalized, the setMaster flag |
| 34344 | | -** is cleared anyway. |
| 34671 | +** is cleared anyway (and the pager will move to ERROR state). |
| 34345 | 34672 | ** |
| 34346 | 34673 | ** doNotSpill, doNotSyncSpill |
| 34347 | 34674 | ** |
| 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". |
| 34675 | +** These two boolean variables control the behaviour of cache-spills |
| 34676 | +** (calls made by the pcache module to the pagerStress() routine to |
| 34677 | +** write cached data to the file-system in order to free up memory). |
| 34678 | +** |
| 34679 | +** When doNotSpill is non-zero, writing to the database from pagerStress() |
| 34680 | +** is disabled altogether. This is done in a very obscure case that |
| 34681 | +** comes up during savepoint rollback that requires the pcache module |
| 34682 | +** to allocate a new page to prevent the journal file from being written |
| 34683 | +** while it is being traversed by code in pager_playback(). |
| 34684 | +** |
| 34685 | +** If doNotSyncSpill is non-zero, writing to the database from pagerStress() |
| 34686 | +** is permitted, but syncing the journal file is not. This flag is set |
| 34687 | +** by sqlite3PagerWrite() when the file-system sector-size is larger than |
| 34688 | +** the database page-size in order to prevent a journal sync from happening |
| 34689 | +** in between the journalling of two pages on the same sector. |
| 34361 | 34690 | ** |
| 34362 | 34691 | ** subjInMemory |
| 34363 | 34692 | ** |
| 34364 | 34693 | ** This is a boolean variable. If true, then any required sub-journal |
| 34365 | 34694 | ** is opened as an in-memory journal file. If false, then in-memory |
| 34366 | 34695 | ** sub-journals are only used for in-memory pager files. |
| 34696 | +** |
| 34697 | +** This variable is updated by the upper layer each time a new |
| 34698 | +** write-transaction is opened. |
| 34699 | +** |
| 34700 | +** dbSize, dbOrigSize, dbFileSize |
| 34701 | +** |
| 34702 | +** Variable dbSize is set to the number of pages in the database file. |
| 34703 | +** It is valid in PAGER_READER and higher states (all states except for |
| 34704 | +** OPEN and ERROR). |
| 34705 | +** |
| 34706 | +** dbSize is set based on the size of the database file, which may be |
| 34707 | +** larger than the size of the database (the value stored at offset |
| 34708 | +** 28 of the database header by the btree). If the size of the file |
| 34709 | +** is not an integer multiple of the page-size, the value stored in |
| 34710 | +** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2). |
| 34711 | +** Except, any file that is greater than 0 bytes in size is considered |
| 34712 | +** to have at least one page. (i.e. a 1KB file with 2K page-size leads |
| 34713 | +** to dbSize==1). |
| 34714 | +** |
| 34715 | +** During a write-transaction, if pages with page-numbers greater than |
| 34716 | +** dbSize are modified in the cache, dbSize is updated accordingly. |
| 34717 | +** Similarly, if the database is truncated using PagerTruncateImage(), |
| 34718 | +** dbSize is updated. |
| 34719 | +** |
| 34720 | +** Variables dbOrigSize and dbFileSize are valid in states |
| 34721 | +** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize |
| 34722 | +** variable at the start of the transaction. It is used during rollback, |
| 34723 | +** and to determine whether or not pages need to be journalled before |
| 34724 | +** being modified. |
| 34725 | +** |
| 34726 | +** Throughout a write-transaction, dbFileSize contains the size of |
| 34727 | +** the file on disk in pages. It is set to a copy of dbSize when the |
| 34728 | +** write-transaction is first opened, and updated when VFS calls are made |
| 34729 | +** to write or truncate the database file on disk. |
| 34730 | +** |
| 34731 | +** The only reason the dbFileSize variable is required is to suppress |
| 34732 | +** unnecessary calls to xTruncate() after committing a transaction. If, |
| 34733 | +** when a transaction is committed, the dbFileSize variable indicates |
| 34734 | +** that the database file is larger than the database image (Pager.dbSize), |
| 34735 | +** pager_truncate() is called. The pager_truncate() call uses xFilesize() |
| 34736 | +** to measure the database file on disk, and then truncates it if required. |
| 34737 | +** dbFileSize is not used when rolling back a transaction. In this case |
| 34738 | +** pager_truncate() is called unconditionally (which means there may be |
| 34739 | +** a call to xFilesize() that is not strictly required). In either case, |
| 34740 | +** pager_truncate() may cause the file to become smaller or larger. |
| 34741 | +** |
| 34742 | +** dbHintSize |
| 34743 | +** |
| 34744 | +** The dbHintSize variable is used to limit the number of calls made to |
| 34745 | +** the VFS xFileControl(FCNTL_SIZE_HINT) method. |
| 34746 | +** |
| 34747 | +** dbHintSize is set to a copy of the dbSize variable when a |
| 34748 | +** write-transaction is opened (at the same time as dbFileSize and |
| 34749 | +** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called, |
| 34750 | +** dbHintSize is increased to the number of pages that correspond to the |
| 34751 | +** size-hint passed to the method call. See pager_write_pagelist() for |
| 34752 | +** details. |
| 34753 | +** |
| 34754 | +** errCode |
| 34755 | +** |
| 34756 | +** The Pager.errCode variable is only ever used in PAGER_ERROR state. It |
| 34757 | +** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode |
| 34758 | +** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX |
| 34759 | +** sub-codes. |
| 34367 | 34760 | */ |
| 34368 | 34761 | struct Pager { |
| 34369 | 34762 | sqlite3_vfs *pVfs; /* OS functions to use for IO */ |
| 34370 | 34763 | u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */ |
| 34371 | | - u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */ |
| 34764 | + u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */ |
| 34372 | 34765 | u8 useJournal; /* Use a rollback journal on this file */ |
| 34373 | 34766 | u8 noReadlock; /* Do not bother to obtain readlocks */ |
| 34374 | 34767 | u8 noSync; /* Do not sync the journal if true */ |
| 34375 | 34768 | u8 fullSync; /* Do extra syncs of the journal for robustness */ |
| 34376 | 34769 | u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */ |
| 34377 | 34770 | u8 tempFile; /* zFilename is a temporary file */ |
| 34378 | 34771 | u8 readOnly; /* True for a read-only database */ |
| 34379 | 34772 | u8 memDb; /* True to inhibit all file I/O */ |
| 34380 | 34773 | |
| 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. |
| 34774 | + /************************************************************************** |
| 34775 | + ** The following block contains those class members that change during |
| 34776 | + ** routine opertion. Class members not in this block are either fixed |
| 34777 | + ** when the pager is first created or else only change when there is a |
| 34778 | + ** significant mode change (such as changing the page_size, locking_mode, |
| 34779 | + ** or the journal_mode). From another view, these class members describe |
| 34780 | + ** the "state" of the pager, while other class members describe the |
| 34781 | + ** "configuration" of the pager. |
| 34391 | 34782 | */ |
| 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 */ |
| 34783 | + u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */ |
| 34784 | + u8 eLock; /* Current lock held on database file */ |
| 34396 | 34785 | u8 changeCountDone; /* Set after incrementing the change-counter */ |
| 34397 | 34786 | u8 setMaster; /* True if a m-j name has been written to jrnl */ |
| 34398 | 34787 | u8 doNotSpill; /* Do not spill the cache when non-zero */ |
| 34399 | 34788 | u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */ |
| 34400 | | - u8 dbSizeValid; /* Set when dbSize is correct */ |
| 34401 | 34789 | u8 subjInMemory; /* True to use in-memory sub-journals */ |
| 34402 | 34790 | Pgno dbSize; /* Number of pages in the database */ |
| 34403 | 34791 | Pgno dbOrigSize; /* dbSize before the current transaction */ |
| 34404 | 34792 | Pgno dbFileSize; /* Number of pages in the database file */ |
| 34793 | + Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */ |
| 34405 | 34794 | int errCode; /* One of several kinds of errors */ |
| 34406 | 34795 | int nRec; /* Pages journalled since last j-header written */ |
| 34407 | 34796 | u32 cksumInit; /* Quasi-random value added to every checksum */ |
| 34408 | 34797 | u32 nSubRec; /* Number of records written to sub-journal */ |
| 34409 | 34798 | Bitvec *pInJournal; /* One bit for each page in the database file */ |
| | @@ -34410,21 +34799,25 @@ |
| 34410 | 34799 | sqlite3_file *fd; /* File descriptor for database */ |
| 34411 | 34800 | sqlite3_file *jfd; /* File descriptor for main journal */ |
| 34412 | 34801 | sqlite3_file *sjfd; /* File descriptor for sub-journal */ |
| 34413 | 34802 | i64 journalOff; /* Current write offset in the journal file */ |
| 34414 | 34803 | i64 journalHdr; /* Byte offset to previous journal header */ |
| 34415 | | - i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 34804 | + sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ |
| 34416 | 34805 | PagerSavepoint *aSavepoint; /* Array of active savepoints */ |
| 34417 | 34806 | int nSavepoint; /* Number of elements in aSavepoint[] */ |
| 34418 | 34807 | char dbFileVers[16]; /* Changes whenever database file changes */ |
| 34419 | | - u32 sectorSize; /* Assumed sector size during rollback */ |
| 34808 | + /* |
| 34809 | + ** End of the routinely-changing class members |
| 34810 | + ***************************************************************************/ |
| 34420 | 34811 | |
| 34421 | 34812 | u16 nExtra; /* Add this many bytes to each in-memory page */ |
| 34422 | 34813 | i16 nReserve; /* Number of unused bytes at end of each page */ |
| 34423 | 34814 | u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */ |
| 34815 | + u32 sectorSize; /* Assumed sector size during rollback */ |
| 34424 | 34816 | int pageSize; /* Number of bytes in a page */ |
| 34425 | 34817 | Pgno mxPgno; /* Maximum allowed size of the database */ |
| 34818 | + i64 journalSizeLimit; /* Size limit for persistent journal files */ |
| 34426 | 34819 | char *zFilename; /* Name of the database file */ |
| 34427 | 34820 | char *zJournal; /* Name of the journal file */ |
| 34428 | 34821 | int (*xBusyHandler)(void*); /* Function to call when busy */ |
| 34429 | 34822 | void *pBusyHandlerArg; /* Context argument for xBusyHandler */ |
| 34430 | 34823 | #ifdef SQLITE_TEST |
| | @@ -34438,11 +34831,10 @@ |
| 34438 | 34831 | void (*xCodecFree)(void*); /* Destructor for the codec */ |
| 34439 | 34832 | void *pCodec; /* First argument to xCodec... methods */ |
| 34440 | 34833 | #endif |
| 34441 | 34834 | char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ |
| 34442 | 34835 | PCache *pPCache; /* Pointer to page cache object */ |
| 34443 | | - sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */ |
| 34444 | 34836 | #ifndef SQLITE_OMIT_WAL |
| 34445 | 34837 | Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */ |
| 34446 | 34838 | char *zWal; /* File name for write-ahead log */ |
| 34447 | 34839 | #endif |
| 34448 | 34840 | }; |
| | @@ -34517,26 +34909,225 @@ |
| 34517 | 34909 | /* |
| 34518 | 34910 | ** The maximum legal page number is (2^31 - 1). |
| 34519 | 34911 | */ |
| 34520 | 34912 | #define PAGER_MAX_PGNO 2147483647 |
| 34521 | 34913 | |
| 34914 | +/* |
| 34915 | +** The argument to this macro is a file descriptor (type sqlite3_file*). |
| 34916 | +** Return 0 if it is not open, or non-zero (but not 1) if it is. |
| 34917 | +** |
| 34918 | +** This is so that expressions can be written as: |
| 34919 | +** |
| 34920 | +** if( isOpen(pPager->jfd) ){ ... |
| 34921 | +** |
| 34922 | +** instead of |
| 34923 | +** |
| 34924 | +** if( pPager->jfd->pMethods ){ ... |
| 34925 | +*/ |
| 34926 | +#define isOpen(pFd) ((pFd)->pMethods) |
| 34927 | + |
| 34928 | +/* |
| 34929 | +** Return true if this pager uses a write-ahead log instead of the usual |
| 34930 | +** rollback journal. Otherwise false. |
| 34931 | +*/ |
| 34932 | +#ifndef SQLITE_OMIT_WAL |
| 34933 | +static int pagerUseWal(Pager *pPager){ |
| 34934 | + return (pPager->pWal!=0); |
| 34935 | +} |
| 34936 | +#else |
| 34937 | +# define pagerUseWal(x) 0 |
| 34938 | +# define pagerRollbackWal(x) 0 |
| 34939 | +# define pagerWalFrames(v,w,x,y,z) 0 |
| 34940 | +# define pagerOpenWalIfPresent(z) SQLITE_OK |
| 34941 | +# define pagerBeginReadTransaction(z) SQLITE_OK |
| 34942 | +#endif |
| 34943 | + |
| 34522 | 34944 | #ifndef NDEBUG |
| 34523 | 34945 | /* |
| 34524 | 34946 | ** Usage: |
| 34525 | 34947 | ** |
| 34526 | 34948 | ** assert( assert_pager_state(pPager) ); |
| 34949 | +** |
| 34950 | +** This function runs many asserts to try to find inconsistencies in |
| 34951 | +** the internal state of the Pager object. |
| 34527 | 34952 | */ |
| 34528 | | -static int assert_pager_state(Pager *pPager){ |
| 34953 | +static int assert_pager_state(Pager *p){ |
| 34954 | + Pager *pPager = p; |
| 34529 | 34955 | |
| 34530 | | - /* A temp-file is always in PAGER_EXCLUSIVE or PAGER_SYNCED state. */ |
| 34531 | | - assert( pPager->tempFile==0 || pPager->state>=PAGER_EXCLUSIVE ); |
| 34956 | + /* State must be valid. */ |
| 34957 | + assert( p->eState==PAGER_OPEN |
| 34958 | + || p->eState==PAGER_READER |
| 34959 | + || p->eState==PAGER_WRITER_LOCKED |
| 34960 | + || p->eState==PAGER_WRITER_CACHEMOD |
| 34961 | + || p->eState==PAGER_WRITER_DBMOD |
| 34962 | + || p->eState==PAGER_WRITER_FINISHED |
| 34963 | + || p->eState==PAGER_ERROR |
| 34964 | + ); |
| 34532 | 34965 | |
| 34533 | | - /* The changeCountDone flag is always set for temp-files */ |
| 34534 | | - assert( pPager->tempFile==0 || pPager->changeCountDone ); |
| 34966 | + /* Regardless of the current state, a temp-file connection always behaves |
| 34967 | + ** as if it has an exclusive lock on the database file. It never updates |
| 34968 | + ** the change-counter field, so the changeCountDone flag is always set. |
| 34969 | + */ |
| 34970 | + assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK ); |
| 34971 | + assert( p->tempFile==0 || pPager->changeCountDone ); |
| 34972 | + |
| 34973 | + /* If the useJournal flag is clear, the journal-mode must be "OFF". |
| 34974 | + ** And if the journal-mode is "OFF", the journal file must not be open. |
| 34975 | + */ |
| 34976 | + assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal ); |
| 34977 | + assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) ); |
| 34978 | + |
| 34979 | + /* Check that MEMDB implies noSync. And an in-memory journal. Since |
| 34980 | + ** this means an in-memory pager performs no IO at all, it cannot encounter |
| 34981 | + ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing |
| 34982 | + ** a journal file. (although the in-memory journal implementation may |
| 34983 | + ** return SQLITE_IOERR_NOMEM while the journal file is being written). It |
| 34984 | + ** is therefore not possible for an in-memory pager to enter the ERROR |
| 34985 | + ** state. |
| 34986 | + */ |
| 34987 | + if( MEMDB ){ |
| 34988 | + assert( p->noSync ); |
| 34989 | + assert( p->journalMode==PAGER_JOURNALMODE_OFF |
| 34990 | + || p->journalMode==PAGER_JOURNALMODE_MEMORY |
| 34991 | + ); |
| 34992 | + assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN ); |
| 34993 | + assert( pagerUseWal(p)==0 ); |
| 34994 | + } |
| 34995 | + |
| 34996 | + /* If changeCountDone is set, a RESERVED lock or greater must be held |
| 34997 | + ** on the file. |
| 34998 | + */ |
| 34999 | + assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK ); |
| 35000 | + assert( p->eLock!=PENDING_LOCK ); |
| 35001 | + |
| 35002 | + switch( p->eState ){ |
| 35003 | + case PAGER_OPEN: |
| 35004 | + assert( !MEMDB ); |
| 35005 | + assert( pPager->errCode==SQLITE_OK ); |
| 35006 | + assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile ); |
| 35007 | + break; |
| 35008 | + |
| 35009 | + case PAGER_READER: |
| 35010 | + assert( pPager->errCode==SQLITE_OK ); |
| 35011 | + assert( p->eLock!=UNKNOWN_LOCK ); |
| 35012 | + assert( p->eLock>=SHARED_LOCK || p->noReadlock ); |
| 35013 | + break; |
| 35014 | + |
| 35015 | + case PAGER_WRITER_LOCKED: |
| 35016 | + assert( p->eLock!=UNKNOWN_LOCK ); |
| 35017 | + assert( pPager->errCode==SQLITE_OK ); |
| 35018 | + if( !pagerUseWal(pPager) ){ |
| 35019 | + assert( p->eLock>=RESERVED_LOCK ); |
| 35020 | + } |
| 35021 | + assert( pPager->dbSize==pPager->dbOrigSize ); |
| 35022 | + assert( pPager->dbOrigSize==pPager->dbFileSize ); |
| 35023 | + assert( pPager->dbOrigSize==pPager->dbHintSize ); |
| 35024 | + assert( pPager->setMaster==0 ); |
| 35025 | + break; |
| 35026 | + |
| 35027 | + case PAGER_WRITER_CACHEMOD: |
| 35028 | + assert( p->eLock!=UNKNOWN_LOCK ); |
| 35029 | + assert( pPager->errCode==SQLITE_OK ); |
| 35030 | + if( !pagerUseWal(pPager) ){ |
| 35031 | + /* It is possible that if journal_mode=wal here that neither the |
| 35032 | + ** journal file nor the WAL file are open. This happens during |
| 35033 | + ** a rollback transaction that switches from journal_mode=off |
| 35034 | + ** to journal_mode=wal. |
| 35035 | + */ |
| 35036 | + assert( p->eLock>=RESERVED_LOCK ); |
| 35037 | + assert( isOpen(p->jfd) |
| 35038 | + || p->journalMode==PAGER_JOURNALMODE_OFF |
| 35039 | + || p->journalMode==PAGER_JOURNALMODE_WAL |
| 35040 | + ); |
| 35041 | + } |
| 35042 | + assert( pPager->dbOrigSize==pPager->dbFileSize ); |
| 35043 | + assert( pPager->dbOrigSize==pPager->dbHintSize ); |
| 35044 | + break; |
| 35045 | + |
| 35046 | + case PAGER_WRITER_DBMOD: |
| 35047 | + assert( p->eLock==EXCLUSIVE_LOCK ); |
| 35048 | + assert( pPager->errCode==SQLITE_OK ); |
| 35049 | + assert( !pagerUseWal(pPager) ); |
| 35050 | + assert( p->eLock>=EXCLUSIVE_LOCK ); |
| 35051 | + assert( isOpen(p->jfd) |
| 35052 | + || p->journalMode==PAGER_JOURNALMODE_OFF |
| 35053 | + || p->journalMode==PAGER_JOURNALMODE_WAL |
| 35054 | + ); |
| 35055 | + assert( pPager->dbOrigSize<=pPager->dbHintSize ); |
| 35056 | + break; |
| 35057 | + |
| 35058 | + case PAGER_WRITER_FINISHED: |
| 35059 | + assert( p->eLock==EXCLUSIVE_LOCK ); |
| 35060 | + assert( pPager->errCode==SQLITE_OK ); |
| 35061 | + assert( !pagerUseWal(pPager) ); |
| 35062 | + assert( isOpen(p->jfd) |
| 35063 | + || p->journalMode==PAGER_JOURNALMODE_OFF |
| 35064 | + || p->journalMode==PAGER_JOURNALMODE_WAL |
| 35065 | + ); |
| 35066 | + break; |
| 35067 | + |
| 35068 | + case PAGER_ERROR: |
| 35069 | + /* There must be at least one outstanding reference to the pager if |
| 35070 | + ** in ERROR state. Otherwise the pager should have already dropped |
| 35071 | + ** back to OPEN state. |
| 35072 | + */ |
| 35073 | + assert( pPager->errCode!=SQLITE_OK ); |
| 35074 | + assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); |
| 35075 | + break; |
| 35076 | + } |
| 34535 | 35077 | |
| 34536 | 35078 | return 1; |
| 34537 | 35079 | } |
| 35080 | + |
| 35081 | +/* |
| 35082 | +** Return a pointer to a human readable string in a static buffer |
| 35083 | +** containing the state of the Pager object passed as an argument. This |
| 35084 | +** is intended to be used within debuggers. For example, as an alternative |
| 35085 | +** to "print *pPager" in gdb: |
| 35086 | +** |
| 35087 | +** (gdb) printf "%s", print_pager_state(pPager) |
| 35088 | +*/ |
| 35089 | +static char *print_pager_state(Pager *p){ |
| 35090 | + static char zRet[1024]; |
| 35091 | + |
| 35092 | + sqlite3_snprintf(1024, zRet, |
| 35093 | + "Filename: %s\n" |
| 35094 | + "State: %s errCode=%d\n" |
| 35095 | + "Lock: %s\n" |
| 35096 | + "Locking mode: locking_mode=%s\n" |
| 35097 | + "Journal mode: journal_mode=%s\n" |
| 35098 | + "Backing store: tempFile=%d memDb=%d useJournal=%d\n" |
| 35099 | + "Journal: journalOff=%lld journalHdr=%lld\n" |
| 35100 | + "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n" |
| 35101 | + , p->zFilename |
| 35102 | + , p->eState==PAGER_OPEN ? "OPEN" : |
| 35103 | + p->eState==PAGER_READER ? "READER" : |
| 35104 | + p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" : |
| 35105 | + p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" : |
| 35106 | + p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" : |
| 35107 | + p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" : |
| 35108 | + p->eState==PAGER_ERROR ? "ERROR" : "?error?" |
| 35109 | + , (int)p->errCode |
| 35110 | + , p->eLock==NO_LOCK ? "NO_LOCK" : |
| 35111 | + p->eLock==RESERVED_LOCK ? "RESERVED" : |
| 35112 | + p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" : |
| 35113 | + p->eLock==SHARED_LOCK ? "SHARED" : |
| 35114 | + p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?" |
| 35115 | + , p->exclusiveMode ? "exclusive" : "normal" |
| 35116 | + , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" : |
| 35117 | + p->journalMode==PAGER_JOURNALMODE_OFF ? "off" : |
| 35118 | + p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" : |
| 35119 | + p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" : |
| 35120 | + p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" : |
| 35121 | + p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?" |
| 35122 | + , (int)p->tempFile, (int)p->memDb, (int)p->useJournal |
| 35123 | + , p->journalOff, p->journalHdr |
| 35124 | + , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize |
| 35125 | + ); |
| 35126 | + |
| 35127 | + return zRet; |
| 35128 | +} |
| 34538 | 35129 | #endif |
| 34539 | 35130 | |
| 34540 | 35131 | /* |
| 34541 | 35132 | ** Return true if it is necessary to write page *pPg into the sub-journal. |
| 34542 | 35133 | ** A page needs to be written into the sub-journal if there exists one |
| | @@ -34584,10 +35175,11 @@ |
| 34584 | 35175 | |
| 34585 | 35176 | /* |
| 34586 | 35177 | ** Write a 32-bit integer into a string buffer in big-endian byte order. |
| 34587 | 35178 | */ |
| 34588 | 35179 | #define put32bits(A,B) sqlite3Put4byte((u8*)A,B) |
| 35180 | + |
| 34589 | 35181 | |
| 34590 | 35182 | /* |
| 34591 | 35183 | ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK |
| 34592 | 35184 | ** on success or an error code is something goes wrong. |
| 34593 | 35185 | */ |
| | @@ -34596,31 +35188,57 @@ |
| 34596 | 35188 | put32bits(ac, val); |
| 34597 | 35189 | return sqlite3OsWrite(fd, ac, 4, offset); |
| 34598 | 35190 | } |
| 34599 | 35191 | |
| 34600 | 35192 | /* |
| 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) |
| 35193 | +** Unlock the database file to level eLock, which must be either NO_LOCK |
| 35194 | +** or SHARED_LOCK. Regardless of whether or not the call to xUnlock() |
| 35195 | +** succeeds, set the Pager.eLock variable to match the (attempted) new lock. |
| 35196 | +** |
| 35197 | +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is |
| 35198 | +** called, do not modify it. See the comment above the #define of |
| 35199 | +** UNKNOWN_LOCK for an explanation of this. |
| 35200 | +*/ |
| 35201 | +static int pagerUnlockDb(Pager *pPager, int eLock){ |
| 35202 | + int rc = SQLITE_OK; |
| 35203 | + |
| 35204 | + assert( !pPager->exclusiveMode ); |
| 35205 | + assert( eLock==NO_LOCK || eLock==SHARED_LOCK ); |
| 35206 | + assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 ); |
| 35207 | + if( isOpen(pPager->fd) ){ |
| 35208 | + assert( pPager->eLock>=eLock ); |
| 35209 | + rc = sqlite3OsUnlock(pPager->fd, eLock); |
| 35210 | + if( pPager->eLock!=UNKNOWN_LOCK ){ |
| 35211 | + pPager->eLock = (u8)eLock; |
| 35212 | + } |
| 35213 | + IOTRACE(("UNLOCK %p %d\n", pPager, eLock)) |
| 35214 | + } |
| 35215 | + return rc; |
| 35216 | +} |
| 34613 | 35217 | |
| 34614 | 35218 | /* |
| 34615 | | -** If file pFd is open, call sqlite3OsUnlock() on it. |
| 35219 | +** Lock the database file to level eLock, which must be either SHARED_LOCK, |
| 35220 | +** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the |
| 35221 | +** Pager.eLock variable to the new locking state. |
| 35222 | +** |
| 35223 | +** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is |
| 35224 | +** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. |
| 35225 | +** See the comment above the #define of UNKNOWN_LOCK for an explanation |
| 35226 | +** of this. |
| 34616 | 35227 | */ |
| 34617 | | -static int osUnlock(sqlite3_file *pFd, int eLock){ |
| 34618 | | - if( !isOpen(pFd) ){ |
| 34619 | | - return SQLITE_OK; |
| 35228 | +static int pagerLockDb(Pager *pPager, int eLock){ |
| 35229 | + int rc = SQLITE_OK; |
| 35230 | + |
| 35231 | + assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK ); |
| 35232 | + if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){ |
| 35233 | + rc = sqlite3OsLock(pPager->fd, eLock); |
| 35234 | + if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){ |
| 35235 | + pPager->eLock = (u8)eLock; |
| 35236 | + IOTRACE(("LOCK %p %d\n", pPager, eLock)) |
| 35237 | + } |
| 34620 | 35238 | } |
| 34621 | | - return sqlite3OsUnlock(pFd, eLock); |
| 35239 | + return rc; |
| 34622 | 35240 | } |
| 34623 | 35241 | |
| 34624 | 35242 | /* |
| 34625 | 35243 | ** This function determines whether or not the atomic-write optimization |
| 34626 | 35244 | ** can be used with this pager. The optimization can be used if: |
| | @@ -34692,17 +35310,18 @@ |
| 34692 | 35310 | ** that the page is either dirty or still matches the calculated page-hash. |
| 34693 | 35311 | */ |
| 34694 | 35312 | #define CHECK_PAGE(x) checkPage(x) |
| 34695 | 35313 | static void checkPage(PgHdr *pPg){ |
| 34696 | 35314 | Pager *pPager = pPg->pPager; |
| 34697 | | - assert( !pPg->pageHash || pPager->errCode |
| 34698 | | - || (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); |
| 35315 | + assert( pPager->eState!=PAGER_ERROR ); |
| 35316 | + assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) ); |
| 34699 | 35317 | } |
| 34700 | 35318 | |
| 34701 | 35319 | #else |
| 34702 | 35320 | #define pager_datahash(X,Y) 0 |
| 34703 | 35321 | #define pager_pagehash(X) 0 |
| 35322 | +#define pager_set_pagehash(X) |
| 34704 | 35323 | #define CHECK_PAGE(x) |
| 34705 | 35324 | #endif /* SQLITE_CHECK_PAGES */ |
| 34706 | 35325 | |
| 34707 | 35326 | /* |
| 34708 | 35327 | ** When this is called the journal file for pager pPager must be open. |
| | @@ -34865,11 +35484,11 @@ |
| 34865 | 35484 | ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space. |
| 34866 | 35485 | */ |
| 34867 | 35486 | static int writeJournalHdr(Pager *pPager){ |
| 34868 | 35487 | int rc = SQLITE_OK; /* Return code */ |
| 34869 | 35488 | char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */ |
| 34870 | | - u32 nHeader = pPager->pageSize; /* Size of buffer pointed to by zHeader */ |
| 35489 | + u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */ |
| 34871 | 35490 | u32 nWrite; /* Bytes of header sector written */ |
| 34872 | 35491 | int ii; /* Loop counter */ |
| 34873 | 35492 | |
| 34874 | 35493 | assert( isOpen(pPager->jfd) ); /* Journal file must be open. */ |
| 34875 | 35494 | |
| | @@ -34908,11 +35527,11 @@ |
| 34908 | 35527 | ** |
| 34909 | 35528 | ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees |
| 34910 | 35529 | ** that garbage data is never appended to the journal file. |
| 34911 | 35530 | */ |
| 34912 | 35531 | assert( isOpen(pPager->fd) || pPager->noSync ); |
| 34913 | | - if( (pPager->noSync) || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) |
| 35532 | + if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY) |
| 34914 | 35533 | || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) |
| 34915 | 35534 | ){ |
| 34916 | 35535 | memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic)); |
| 34917 | 35536 | put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff); |
| 34918 | 35537 | }else{ |
| | @@ -35032,18 +35651,25 @@ |
| 35032 | 35651 | } |
| 35033 | 35652 | |
| 35034 | 35653 | if( pPager->journalOff==0 ){ |
| 35035 | 35654 | u32 iPageSize; /* Page-size field of journal header */ |
| 35036 | 35655 | u32 iSectorSize; /* Sector-size field of journal header */ |
| 35037 | | - u16 iPageSize16; /* Copy of iPageSize in 16-bit variable */ |
| 35038 | 35656 | |
| 35039 | 35657 | /* Read the page-size and sector-size journal header fields. */ |
| 35040 | 35658 | if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize)) |
| 35041 | 35659 | || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize)) |
| 35042 | 35660 | ){ |
| 35043 | 35661 | return rc; |
| 35044 | 35662 | } |
| 35663 | + |
| 35664 | + /* Versions of SQLite prior to 3.5.8 set the page-size field of the |
| 35665 | + ** journal header to zero. In this case, assume that the Pager.pageSize |
| 35666 | + ** variable is already set to the correct page size. |
| 35667 | + */ |
| 35668 | + if( iPageSize==0 ){ |
| 35669 | + iPageSize = pPager->pageSize; |
| 35670 | + } |
| 35045 | 35671 | |
| 35046 | 35672 | /* Check that the values read from the page-size and sector-size fields |
| 35047 | 35673 | ** are within range. To be 'in range', both values need to be a power |
| 35048 | 35674 | ** of two greater than or equal to 512 or 32, and not greater than their |
| 35049 | 35675 | ** respective compile time maximum limits. |
| | @@ -35062,14 +35688,12 @@ |
| 35062 | 35688 | |
| 35063 | 35689 | /* Update the page-size to match the value read from the journal. |
| 35064 | 35690 | ** Use a testcase() macro to make sure that malloc failure within |
| 35065 | 35691 | ** PagerSetPagesize() is tested. |
| 35066 | 35692 | */ |
| 35067 | | - iPageSize16 = (u16)iPageSize; |
| 35068 | | - rc = sqlite3PagerSetPagesize(pPager, &iPageSize16, -1); |
| 35693 | + rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1); |
| 35069 | 35694 | testcase( rc!=SQLITE_OK ); |
| 35070 | | - assert( rc!=SQLITE_OK || iPageSize16==(u16)iPageSize ); |
| 35071 | 35695 | |
| 35072 | 35696 | /* Update the assumed sector-size to match the value used by |
| 35073 | 35697 | ** the process that created this journal. If this journal was |
| 35074 | 35698 | ** created by a process other than this one, then this routine |
| 35075 | 35699 | ** is being called from within pager_playback(). The local value |
| | @@ -35108,10 +35732,12 @@ |
| 35108 | 35732 | i64 iHdrOff; /* Offset of header in journal file */ |
| 35109 | 35733 | i64 jrnlSize; /* Size of journal file on disk */ |
| 35110 | 35734 | u32 cksum = 0; /* Checksum of string zMaster */ |
| 35111 | 35735 | |
| 35112 | 35736 | assert( pPager->setMaster==0 ); |
| 35737 | + assert( !pagerUseWal(pPager) ); |
| 35738 | + |
| 35113 | 35739 | if( !zMaster |
| 35114 | 35740 | || pPager->journalMode==PAGER_JOURNALMODE_MEMORY |
| 35115 | 35741 | || pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 35116 | 35742 | ){ |
| 35117 | 35743 | return SQLITE_OK; |
| | @@ -35144,11 +35770,10 @@ |
| 35144 | 35770 | || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8))) |
| 35145 | 35771 | ){ |
| 35146 | 35772 | return rc; |
| 35147 | 35773 | } |
| 35148 | 35774 | pPager->journalOff += (nMaster+20); |
| 35149 | | - pPager->needSync = !pPager->noSync; |
| 35150 | 35775 | |
| 35151 | 35776 | /* If the pager is in peristent-journal mode, then the physical |
| 35152 | 35777 | ** journal-file may extend past the end of the master-journal name |
| 35153 | 35778 | ** and 8 bytes of magic data just written to the file. This is |
| 35154 | 35779 | ** dangerous because the code to rollback a hot-journal file |
| | @@ -35180,21 +35805,15 @@ |
| 35180 | 35805 | (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p); |
| 35181 | 35806 | return p; |
| 35182 | 35807 | } |
| 35183 | 35808 | |
| 35184 | 35809 | /* |
| 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? |
| 35810 | +** Discard the entire contents of the in-memory page-cache. |
| 35189 | 35811 | */ |
| 35190 | 35812 | 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 | | - } |
| 35813 | + sqlite3BackupRestart(pPager->pBackup); |
| 35814 | + sqlite3PcacheClear(pPager->pPCache); |
| 35196 | 35815 | } |
| 35197 | 35816 | |
| 35198 | 35817 | /* |
| 35199 | 35818 | ** Free all structures in the Pager.aSavepoint[] array and set both |
| 35200 | 35819 | ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal |
| | @@ -35233,38 +35852,43 @@ |
| 35233 | 35852 | } |
| 35234 | 35853 | return rc; |
| 35235 | 35854 | } |
| 35236 | 35855 | |
| 35237 | 35856 | /* |
| 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. |
| 35857 | +** This function is a no-op if the pager is in exclusive mode and not |
| 35858 | +** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN |
| 35859 | +** state. |
| 35256 | 35860 | ** |
| 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. |
| 35861 | +** If the pager is not in exclusive-access mode, the database file is |
| 35862 | +** completely unlocked. If the file is unlocked and the file-system does |
| 35863 | +** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is |
| 35864 | +** closed (if it is open). |
| 35865 | +** |
| 35866 | +** If the pager is in ERROR state when this function is called, the |
| 35867 | +** contents of the pager cache are discarded before switching back to |
| 35868 | +** the OPEN state. Regardless of whether the pager is in exclusive-mode |
| 35869 | +** or not, any journal file left in the file-system will be treated |
| 35870 | +** as a hot-journal and rolled back the next time a read-transaction |
| 35871 | +** is opened (by this or by any other connection). |
| 35262 | 35872 | */ |
| 35263 | 35873 | static void pager_unlock(Pager *pPager){ |
| 35264 | | - if( !pPager->exclusiveMode ){ |
| 35265 | | - int rc = SQLITE_OK; /* Return code */ |
| 35874 | + |
| 35875 | + assert( pPager->eState==PAGER_READER |
| 35876 | + || pPager->eState==PAGER_OPEN |
| 35877 | + || pPager->eState==PAGER_ERROR |
| 35878 | + ); |
| 35879 | + |
| 35880 | + sqlite3BitvecDestroy(pPager->pInJournal); |
| 35881 | + pPager->pInJournal = 0; |
| 35882 | + releaseAllSavepoints(pPager); |
| 35883 | + |
| 35884 | + if( pagerUseWal(pPager) ){ |
| 35885 | + assert( !isOpen(pPager->jfd) ); |
| 35886 | + sqlite3WalEndReadTransaction(pPager->pWal); |
| 35887 | + pPager->eState = PAGER_OPEN; |
| 35888 | + }else if( !pPager->exclusiveMode ){ |
| 35889 | + int rc; /* Error code returned by pagerUnlockDb() */ |
| 35266 | 35890 | int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0; |
| 35267 | 35891 | |
| 35268 | 35892 | /* If the operating system support deletion of open files, then |
| 35269 | 35893 | ** close the journal file when dropping the database lock. Otherwise |
| 35270 | 35894 | ** another connection with journal_mode=delete might delete the file |
| | @@ -35280,62 +35904,60 @@ |
| 35280 | 35904 | || 1!=(pPager->journalMode & 5) |
| 35281 | 35905 | ){ |
| 35282 | 35906 | sqlite3OsClose(pPager->jfd); |
| 35283 | 35907 | } |
| 35284 | 35908 | |
| 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 | | - |
| 35909 | + /* If the pager is in the ERROR state and the call to unlock the database |
| 35910 | + ** file fails, set the current lock to UNKNOWN_LOCK. See the comment |
| 35911 | + ** above the #define for UNKNOWN_LOCK for an explanation of why this |
| 35912 | + ** is necessary. |
| 35913 | + */ |
| 35914 | + rc = pagerUnlockDb(pPager, NO_LOCK); |
| 35915 | + if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){ |
| 35916 | + pPager->eLock = UNKNOWN_LOCK; |
| 35917 | + } |
| 35918 | + |
| 35919 | + /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here |
| 35920 | + ** without clearing the error code. This is intentional - the error |
| 35921 | + ** code is cleared and the cache reset in the block below. |
| 35922 | + */ |
| 35923 | + assert( pPager->errCode || pPager->eState!=PAGER_ERROR ); |
| 35318 | 35924 | pPager->changeCountDone = 0; |
| 35319 | | - pPager->state = PAGER_UNLOCK; |
| 35320 | | - pPager->dbModified = 0; |
| 35925 | + pPager->eState = PAGER_OPEN; |
| 35321 | 35926 | } |
| 35927 | + |
| 35928 | + /* If Pager.errCode is set, the contents of the pager cache cannot be |
| 35929 | + ** trusted. Now that there are no outstanding references to the pager, |
| 35930 | + ** it can safely move back to PAGER_OPEN state. This happens in both |
| 35931 | + ** normal and exclusive-locking mode. |
| 35932 | + */ |
| 35933 | + if( pPager->errCode ){ |
| 35934 | + assert( !MEMDB ); |
| 35935 | + pager_reset(pPager); |
| 35936 | + pPager->changeCountDone = pPager->tempFile; |
| 35937 | + pPager->eState = PAGER_OPEN; |
| 35938 | + pPager->errCode = SQLITE_OK; |
| 35939 | + } |
| 35940 | + |
| 35941 | + pPager->journalOff = 0; |
| 35942 | + pPager->journalHdr = 0; |
| 35943 | + pPager->setMaster = 0; |
| 35322 | 35944 | } |
| 35323 | 35945 | |
| 35324 | 35946 | /* |
| 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 |
| 35947 | +** This function is called whenever an IOERR or FULL error that requires |
| 35948 | +** the pager to transition into the ERROR state may ahve occurred. |
| 35949 | +** The first argument is a pointer to the pager structure, the second |
| 35950 | +** the error-code about to be returned by a pager API function. The |
| 35951 | +** value returned is a copy of the second argument to this function. |
| 35952 | +** |
| 35953 | +** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the |
| 35954 | +** IOERR sub-codes, the pager enters the ERROR state and the error code |
| 35955 | +** is stored in Pager.errCode. While the pager remains in the ERROR state, |
| 35956 | +** all major API calls on the Pager will immediately return Pager.errCode. |
| 35957 | +** |
| 35958 | +** The ERROR state indicates that the contents of the pager-cache |
| 35337 | 35959 | ** cannot be trusted. This state can be cleared by completely discarding |
| 35338 | 35960 | ** the contents of the pager-cache. If a transaction was active when |
| 35339 | 35961 | ** the persistent error occurred, then the rollback journal may need |
| 35340 | 35962 | ** to be replayed to restore the contents of the database file (as if |
| 35341 | 35963 | ** it were a hot-journal). |
| | @@ -35348,49 +35970,25 @@ |
| 35348 | 35970 | pPager->errCode==SQLITE_OK || |
| 35349 | 35971 | (pPager->errCode & 0xff)==SQLITE_IOERR |
| 35350 | 35972 | ); |
| 35351 | 35973 | if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){ |
| 35352 | 35974 | pPager->errCode = rc; |
| 35975 | + pPager->eState = PAGER_ERROR; |
| 35353 | 35976 | } |
| 35354 | 35977 | return rc; |
| 35355 | 35978 | } |
| 35356 | 35979 | |
| 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 | 35980 | /* |
| 35384 | 35981 | ** This routine ends a transaction. A transaction is usually ended by |
| 35385 | 35982 | ** either a COMMIT or a ROLLBACK operation. This routine may be called |
| 35386 | 35983 | ** after rollback of a hot-journal, or if an error occurs while opening |
| 35387 | 35984 | ** the journal file or writing the very first journal-header of a |
| 35388 | 35985 | ** database transaction. |
| 35389 | 35986 | ** |
| 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). |
| 35987 | +** This routine is never called in PAGER_ERROR state. If it is called |
| 35988 | +** in PAGER_NONE or PAGER_SHARED state and the lock held is less |
| 35989 | +** exclusive than a RESERVED lock, it is a no-op. |
| 35392 | 35990 | ** |
| 35393 | 35991 | ** Otherwise, any active savepoints are released. |
| 35394 | 35992 | ** |
| 35395 | 35993 | ** If the journal file is open, then it is "finalized". Once a journal |
| 35396 | 35994 | ** file has been finalized it is not possible to use it to roll back a |
| | @@ -35417,17 +36015,13 @@ |
| 35417 | 36015 | ** If the pager is running in exclusive mode, this method of finalizing |
| 35418 | 36016 | ** the journal file is never used. Instead, if the journalMode is |
| 35419 | 36017 | ** DELETE and the pager is in exclusive mode, the method described under |
| 35420 | 36018 | ** journalMode==PERSIST is used instead. |
| 35421 | 36019 | ** |
| 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. |
| 36020 | +** After the journal is finalized, the pager moves to PAGER_READER state. |
| 36021 | +** If running in non-exclusive rollback mode, the lock on the file is |
| 36022 | +** downgraded to a SHARED_LOCK. |
| 35429 | 36023 | ** |
| 35430 | 36024 | ** SQLITE_OK is returned if no error occurs. If an error occurs during |
| 35431 | 36025 | ** any of the IO operations to finalize the journal file or unlock the |
| 35432 | 36026 | ** database then the IO error code is returned to the user. If the |
| 35433 | 36027 | ** operation to finalize the journal file fails, then the code still |
| | @@ -35438,15 +36032,30 @@ |
| 35438 | 36032 | */ |
| 35439 | 36033 | static int pager_end_transaction(Pager *pPager, int hasMaster){ |
| 35440 | 36034 | int rc = SQLITE_OK; /* Error code from journal finalization operation */ |
| 35441 | 36035 | int rc2 = SQLITE_OK; /* Error code from db file unlock operation */ |
| 35442 | 36036 | |
| 35443 | | - if( pPager->state<PAGER_RESERVED ){ |
| 36037 | + /* Do nothing if the pager does not have an open write transaction |
| 36038 | + ** or at least a RESERVED lock. This function may be called when there |
| 36039 | + ** is no write-transaction active but a RESERVED or greater lock is |
| 36040 | + ** held under two circumstances: |
| 36041 | + ** |
| 36042 | + ** 1. After a successful hot-journal rollback, it is called with |
| 36043 | + ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK. |
| 36044 | + ** |
| 36045 | + ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE |
| 36046 | + ** lock switches back to locking_mode=normal and then executes a |
| 36047 | + ** read-transaction, this function is called with eState==PAGER_READER |
| 36048 | + ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed. |
| 36049 | + */ |
| 36050 | + assert( assert_pager_state(pPager) ); |
| 36051 | + assert( pPager->eState!=PAGER_ERROR ); |
| 36052 | + if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){ |
| 35444 | 36053 | return SQLITE_OK; |
| 35445 | 36054 | } |
| 36055 | + |
| 35446 | 36056 | releaseAllSavepoints(pPager); |
| 35447 | | - |
| 35448 | 36057 | assert( isOpen(pPager->jfd) || pPager->pInJournal==0 ); |
| 35449 | 36058 | if( isOpen(pPager->jfd) ){ |
| 35450 | 36059 | assert( !pagerUseWal(pPager) ); |
| 35451 | 36060 | |
| 35452 | 36061 | /* Finalize the journal file. */ |
| | @@ -35458,18 +36067,15 @@ |
| 35458 | 36067 | rc = SQLITE_OK; |
| 35459 | 36068 | }else{ |
| 35460 | 36069 | rc = sqlite3OsTruncate(pPager->jfd, 0); |
| 35461 | 36070 | } |
| 35462 | 36071 | pPager->journalOff = 0; |
| 35463 | | - pPager->journalStarted = 0; |
| 35464 | 36072 | }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 35465 | 36073 | || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) |
| 35466 | 36074 | ){ |
| 35467 | 36075 | rc = zeroJournalHdr(pPager, hasMaster); |
| 35468 | | - pager_error(pPager, rc); |
| 35469 | 36076 | pPager->journalOff = 0; |
| 35470 | | - pPager->journalStarted = 0; |
| 35471 | 36077 | }else{ |
| 35472 | 36078 | /* This branch may be executed with Pager.journalMode==MEMORY if |
| 35473 | 36079 | ** a hot-journal was just rolled back. In this case the journal |
| 35474 | 36080 | ** file should be closed and deleted. If this connection writes to |
| 35475 | 36081 | ** the database file, it will do so using an in-memory journal. |
| | @@ -35481,52 +36087,80 @@ |
| 35481 | 36087 | sqlite3OsClose(pPager->jfd); |
| 35482 | 36088 | if( !pPager->tempFile ){ |
| 35483 | 36089 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 35484 | 36090 | } |
| 35485 | 36091 | } |
| 36092 | + } |
| 35486 | 36093 | |
| 35487 | 36094 | #ifdef SQLITE_CHECK_PAGES |
| 35488 | | - sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
| 36095 | + sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash); |
| 36096 | + if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){ |
| 36097 | + PgHdr *p = pager_lookup(pPager, 1); |
| 36098 | + if( p ){ |
| 36099 | + p->pageHash = 0; |
| 36100 | + sqlite3PagerUnref(p); |
| 36101 | + } |
| 36102 | + } |
| 35489 | 36103 | #endif |
| 35490 | | - } |
| 36104 | + |
| 35491 | 36105 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 35492 | 36106 | pPager->pInJournal = 0; |
| 35493 | 36107 | pPager->nRec = 0; |
| 35494 | 36108 | sqlite3PcacheCleanAll(pPager->pPCache); |
| 36109 | + sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize); |
| 35495 | 36110 | |
| 35496 | 36111 | if( pagerUseWal(pPager) ){ |
| 36112 | + /* Drop the WAL write-lock, if any. Also, if the connection was in |
| 36113 | + ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE |
| 36114 | + ** lock held on the database file. |
| 36115 | + */ |
| 35497 | 36116 | rc2 = sqlite3WalEndWriteTransaction(pPager->pWal); |
| 35498 | 36117 | 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; |
| 36118 | + } |
| 36119 | + if( !pPager->exclusiveMode |
| 36120 | + && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0)) |
| 36121 | + ){ |
| 36122 | + rc2 = pagerUnlockDb(pPager, SHARED_LOCK); |
| 35510 | 36123 | 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 | | - } |
| 36124 | + } |
| 36125 | + pPager->eState = PAGER_READER; |
| 36126 | + pPager->setMaster = 0; |
| 35525 | 36127 | |
| 35526 | 36128 | return (rc==SQLITE_OK?rc2:rc); |
| 35527 | 36129 | } |
| 36130 | + |
| 36131 | +/* |
| 36132 | +** Execute a rollback if a transaction is active and unlock the |
| 36133 | +** database file. |
| 36134 | +** |
| 36135 | +** If the pager has already entered the ERROR state, do not attempt |
| 36136 | +** the rollback at this time. Instead, pager_unlock() is called. The |
| 36137 | +** call to pager_unlock() will discard all in-memory pages, unlock |
| 36138 | +** the database file and move the pager back to OPEN state. If this |
| 36139 | +** means that there is a hot-journal left in the file-system, the next |
| 36140 | +** connection to obtain a shared lock on the pager (which may be this one) |
| 36141 | +** will roll it back. |
| 36142 | +** |
| 36143 | +** If the pager has not already entered the ERROR state, but an IO or |
| 36144 | +** malloc error occurs during a rollback, then this will itself cause |
| 36145 | +** the pager to enter the ERROR state. Which will be cleared by the |
| 36146 | +** call to pager_unlock(), as described above. |
| 36147 | +*/ |
| 36148 | +static void pagerUnlockAndRollback(Pager *pPager){ |
| 36149 | + if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){ |
| 36150 | + assert( assert_pager_state(pPager) ); |
| 36151 | + if( pPager->eState>=PAGER_WRITER_LOCKED ){ |
| 36152 | + sqlite3BeginBenignMalloc(); |
| 36153 | + sqlite3PagerRollback(pPager); |
| 36154 | + sqlite3EndBenignMalloc(); |
| 36155 | + }else if( !pPager->exclusiveMode ){ |
| 36156 | + assert( pPager->eState==PAGER_READER ); |
| 36157 | + pager_end_transaction(pPager, 0); |
| 36158 | + } |
| 36159 | + } |
| 36160 | + pager_unlock(pPager); |
| 36161 | +} |
| 35528 | 36162 | |
| 35529 | 36163 | /* |
| 35530 | 36164 | ** Parameter aData must point to a buffer of pPager->pageSize bytes |
| 35531 | 36165 | ** of data. Compute and return a checksum based ont the contents of the |
| 35532 | 36166 | ** page of data and the current value of pPager->cksumInit. |
| | @@ -35574,13 +36208,12 @@ |
| 35574 | 36208 | ** Read a single page from either the journal file (if isMainJrnl==1) or |
| 35575 | 36209 | ** from the sub-journal (if isMainJrnl==0) and playback that page. |
| 35576 | 36210 | ** The page begins at offset *pOffset into the file. The *pOffset |
| 35577 | 36211 | ** value is increased to the start of the next page in the journal. |
| 35578 | 36212 | ** |
| 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. |
| 36213 | +** The main rollback journal uses checksums - the statement journal does |
| 36214 | +** not. |
| 35582 | 36215 | ** |
| 35583 | 36216 | ** If the page number of the page record read from the (sub-)journal file |
| 35584 | 36217 | ** is greater than the current value of Pager.dbSize, then playback is |
| 35585 | 36218 | ** skipped and SQLITE_OK is returned. |
| 35586 | 36219 | ** |
| | @@ -35629,10 +36262,21 @@ |
| 35629 | 36262 | assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */ |
| 35630 | 36263 | |
| 35631 | 36264 | aData = pPager->pTmpSpace; |
| 35632 | 36265 | assert( aData ); /* Temp storage must have already been allocated */ |
| 35633 | 36266 | assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) ); |
| 36267 | + |
| 36268 | + /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction |
| 36269 | + ** or savepoint rollback done at the request of the caller) or this is |
| 36270 | + ** a hot-journal rollback. If it is a hot-journal rollback, the pager |
| 36271 | + ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback |
| 36272 | + ** only reads from the main journal, not the sub-journal. |
| 36273 | + */ |
| 36274 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD |
| 36275 | + || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK) |
| 36276 | + ); |
| 36277 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl ); |
| 35634 | 36278 | |
| 35635 | 36279 | /* Read the page number and page data from the journal or sub-journal |
| 35636 | 36280 | ** file. Return an error code to the caller if an IO error occurs. |
| 35637 | 36281 | */ |
| 35638 | 36282 | jfd = isMainJrnl ? pPager->jfd : pPager->sjfd; |
| | @@ -35666,20 +36310,19 @@ |
| 35666 | 36310 | ** rollback, then don't bother to play it back again. |
| 35667 | 36311 | */ |
| 35668 | 36312 | if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){ |
| 35669 | 36313 | return rc; |
| 35670 | 36314 | } |
| 35671 | | - assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE ); |
| 35672 | 36315 | |
| 35673 | 36316 | /* When playing back page 1, restore the nReserve setting |
| 35674 | 36317 | */ |
| 35675 | 36318 | if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){ |
| 35676 | 36319 | pPager->nReserve = ((u8*)aData)[20]; |
| 35677 | 36320 | pagerReportSize(pPager); |
| 35678 | 36321 | } |
| 35679 | 36322 | |
| 35680 | | - /* If the pager is in RESERVED state, then there must be a copy of this |
| 36323 | + /* If the pager is in CACHEMOD state, then there must be a copy of this |
| 35681 | 36324 | ** page in the pager cache. In this case just update the pager cache, |
| 35682 | 36325 | ** not the database file. The page is left marked dirty in this case. |
| 35683 | 36326 | ** |
| 35684 | 36327 | ** An exception to the above rule: If the database is in no-sync mode |
| 35685 | 36328 | ** and a page is moved during an incremental vacuum then the page may |
| | @@ -35686,12 +36329,15 @@ |
| 35686 | 36329 | ** not be in the pager cache. Later: if a malloc() or IO error occurs |
| 35687 | 36330 | ** during a Movepage() call, then the page may not be in the cache |
| 35688 | 36331 | ** either. So the condition described in the above paragraph is not |
| 35689 | 36332 | ** assert()able. |
| 35690 | 36333 | ** |
| 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. |
| 36334 | + ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the |
| 36335 | + ** pager cache if it exists and the main file. The page is then marked |
| 36336 | + ** not dirty. Since this code is only executed in PAGER_OPEN state for |
| 36337 | + ** a hot-journal rollback, it is guaranteed that the page-cache is empty |
| 36338 | + ** if the pager is in OPEN state. |
| 35693 | 36339 | ** |
| 35694 | 36340 | ** Ticket #1171: The statement journal might contain page content that is |
| 35695 | 36341 | ** different from the page content at the start of the transaction. |
| 35696 | 36342 | ** This occurs when a page is changed prior to the start of a statement |
| 35697 | 36343 | ** then changed again within the statement. When rolling back such a |
| | @@ -35713,21 +36359,22 @@ |
| 35713 | 36359 | pPg = 0; |
| 35714 | 36360 | }else{ |
| 35715 | 36361 | pPg = pager_lookup(pPager, pgno); |
| 35716 | 36362 | } |
| 35717 | 36363 | assert( pPg || !MEMDB ); |
| 36364 | + assert( pPager->eState!=PAGER_OPEN || pPg==0 ); |
| 35718 | 36365 | PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n", |
| 35719 | 36366 | PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData), |
| 35720 | 36367 | (isMainJrnl?"main-journal":"sub-journal") |
| 35721 | 36368 | )); |
| 35722 | 36369 | if( isMainJrnl ){ |
| 35723 | 36370 | isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr); |
| 35724 | 36371 | }else{ |
| 35725 | 36372 | isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC)); |
| 35726 | 36373 | } |
| 35727 | | - if( (pPager->state>=PAGER_EXCLUSIVE) |
| 35728 | | - && isOpen(pPager->fd) |
| 36374 | + if( isOpen(pPager->fd) |
| 36375 | + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 35729 | 36376 | && isSynced |
| 35730 | 36377 | ){ |
| 35731 | 36378 | i64 ofst = (pgno-1)*(i64)pPager->pageSize; |
| 35732 | 36379 | testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 ); |
| 35733 | 36380 | assert( !pagerUseWal(pPager) ); |
| | @@ -35799,13 +36446,12 @@ |
| 35799 | 36446 | ** database corruption may ensue. |
| 35800 | 36447 | */ |
| 35801 | 36448 | assert( !pagerUseWal(pPager) ); |
| 35802 | 36449 | sqlite3PcacheMakeClean(pPg); |
| 35803 | 36450 | } |
| 35804 | | -#ifdef SQLITE_CHECK_PAGES |
| 35805 | | - pPg->pageHash = pager_pagehash(pPg); |
| 35806 | | -#endif |
| 36451 | + pager_set_pagehash(pPg); |
| 36452 | + |
| 35807 | 36453 | /* If this was page 1, then restore the value of Pager.dbFileVers. |
| 35808 | 36454 | ** Do this before any decoding. */ |
| 35809 | 36455 | if( pgno==1 ){ |
| 35810 | 36456 | memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); |
| 35811 | 36457 | } |
| | @@ -35953,14 +36599,14 @@ |
| 35953 | 36599 | /* |
| 35954 | 36600 | ** This function is used to change the actual size of the database |
| 35955 | 36601 | ** file in the file-system. This only happens when committing a transaction, |
| 35956 | 36602 | ** or rolling back a transaction (including rolling back a hot-journal). |
| 35957 | 36603 | ** |
| 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 |
| 36604 | +** If the main database file is not open, or the pager is not in either |
| 36605 | +** DBMOD or OPEN state, this function is a no-op. Otherwise, the size |
| 36606 | +** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). |
| 36607 | +** If the file on disk is currently larger than nPage pages, then use the VFS |
| 35962 | 36608 | ** xTruncate() method to truncate it. |
| 35963 | 36609 | ** |
| 35964 | 36610 | ** Or, it might might be the case that the file on disk is smaller than |
| 35965 | 36611 | ** nPage pages. Some operating system implementations can get confused if |
| 35966 | 36612 | ** you try to truncate a file to some size that is larger than it |
| | @@ -35970,12 +36616,18 @@ |
| 35970 | 36616 | ** If successful, return SQLITE_OK. If an IO error occurs while modifying |
| 35971 | 36617 | ** the database file, return the error code to the caller. |
| 35972 | 36618 | */ |
| 35973 | 36619 | static int pager_truncate(Pager *pPager, Pgno nPage){ |
| 35974 | 36620 | int rc = SQLITE_OK; |
| 35975 | | - if( pPager->state>=PAGER_EXCLUSIVE && isOpen(pPager->fd) ){ |
| 36621 | + assert( pPager->eState!=PAGER_ERROR ); |
| 36622 | + assert( pPager->eState!=PAGER_READER ); |
| 36623 | + |
| 36624 | + if( isOpen(pPager->fd) |
| 36625 | + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 36626 | + ){ |
| 35976 | 36627 | i64 currentSize, newSize; |
| 36628 | + assert( pPager->eLock==EXCLUSIVE_LOCK ); |
| 35977 | 36629 | /* TODO: Is it safe to use Pager.dbFileSize here? */ |
| 35978 | 36630 | rc = sqlite3OsFileSize(pPager->fd, ¤tSize); |
| 35979 | 36631 | newSize = pPager->pageSize*(i64)nPage; |
| 35980 | 36632 | if( rc==SQLITE_OK && currentSize!=newSize ){ |
| 35981 | 36633 | if( currentSize>newSize ){ |
| | @@ -36095,11 +36747,11 @@ |
| 36095 | 36747 | /* Figure out how many records are in the journal. Abort early if |
| 36096 | 36748 | ** the journal is empty. |
| 36097 | 36749 | */ |
| 36098 | 36750 | assert( isOpen(pPager->jfd) ); |
| 36099 | 36751 | rc = sqlite3OsFileSize(pPager->jfd, &szJ); |
| 36100 | | - if( rc!=SQLITE_OK || szJ==0 ){ |
| 36752 | + if( rc!=SQLITE_OK ){ |
| 36101 | 36753 | goto end_playback; |
| 36102 | 36754 | } |
| 36103 | 36755 | |
| 36104 | 36756 | /* Read the master journal name from the journal, if it is present. |
| 36105 | 36757 | ** If a master journal file name is specified, but the file is not |
| | @@ -36129,11 +36781,11 @@ |
| 36129 | 36781 | ** occurs. |
| 36130 | 36782 | */ |
| 36131 | 36783 | while( 1 ){ |
| 36132 | 36784 | /* Read the next journal header from the journal file. If there are |
| 36133 | 36785 | ** 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. |
| 36786 | + ** it is corrupted, then a process must have failed while writing it. |
| 36135 | 36787 | ** This indicates nothing more needs to be rolled back. |
| 36136 | 36788 | */ |
| 36137 | 36789 | rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg); |
| 36138 | 36790 | if( rc!=SQLITE_OK ){ |
| 36139 | 36791 | if( rc==SQLITE_DONE ){ |
| | @@ -36243,14 +36895,13 @@ |
| 36243 | 36895 | if( rc==SQLITE_OK ){ |
| 36244 | 36896 | zMaster = pPager->pTmpSpace; |
| 36245 | 36897 | rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1); |
| 36246 | 36898 | testcase( rc!=SQLITE_OK ); |
| 36247 | 36899 | } |
| 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 ){ |
| 36900 | + if( rc==SQLITE_OK && !pPager->noSync |
| 36901 | + && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) |
| 36902 | + ){ |
| 36252 | 36903 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 36253 | 36904 | } |
| 36254 | 36905 | if( rc==SQLITE_OK ){ |
| 36255 | 36906 | rc = pager_end_transaction(pPager, zMaster[0]!='\0'); |
| 36256 | 36907 | testcase( rc!=SQLITE_OK ); |
| | @@ -36288,11 +36939,11 @@ |
| 36288 | 36939 | Pgno pgno = pPg->pgno; /* Page number to read */ |
| 36289 | 36940 | int rc = SQLITE_OK; /* Return code */ |
| 36290 | 36941 | int isInWal = 0; /* True if page is in log file */ |
| 36291 | 36942 | int pgsz = pPager->pageSize; /* Number of bytes to read */ |
| 36292 | 36943 | |
| 36293 | | - assert( pPager->state>=PAGER_SHARED && !MEMDB ); |
| 36944 | + assert( pPager->eState>=PAGER_READER && !MEMDB ); |
| 36294 | 36945 | assert( isOpen(pPager->fd) ); |
| 36295 | 36946 | |
| 36296 | 36947 | if( NEVER(!isOpen(pPager->fd)) ){ |
| 36297 | 36948 | assert( pPager->tempFile ); |
| 36298 | 36949 | memset(pPg->pData, 0, pPager->pageSize); |
| | @@ -36435,10 +37086,18 @@ |
| 36435 | 37086 | PgHdr *p; |
| 36436 | 37087 | for(p=pList; p; p=p->pDirty){ |
| 36437 | 37088 | sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData); |
| 36438 | 37089 | } |
| 36439 | 37090 | } |
| 37091 | + |
| 37092 | +#ifdef SQLITE_CHECK_PAGES |
| 37093 | + { |
| 37094 | + PgHdr *p; |
| 37095 | + for(p=pList; p; p=p->pDirty) pager_set_pagehash(p); |
| 37096 | + } |
| 37097 | +#endif |
| 37098 | + |
| 36440 | 37099 | return rc; |
| 36441 | 37100 | } |
| 36442 | 37101 | |
| 36443 | 37102 | /* |
| 36444 | 37103 | ** Begin a read transaction on the WAL. |
| | @@ -36451,31 +37110,82 @@ |
| 36451 | 37110 | static int pagerBeginReadTransaction(Pager *pPager){ |
| 36452 | 37111 | int rc; /* Return code */ |
| 36453 | 37112 | int changed = 0; /* True if cache must be reset */ |
| 36454 | 37113 | |
| 36455 | 37114 | assert( pagerUseWal(pPager) ); |
| 37115 | + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); |
| 36456 | 37116 | |
| 36457 | 37117 | /* sqlite3WalEndReadTransaction() was not called for the previous |
| 36458 | 37118 | ** transaction in locking_mode=EXCLUSIVE. So call it now. If we |
| 36459 | 37119 | ** are in locking_mode=NORMAL and EndRead() was previously called, |
| 36460 | 37120 | ** the duplicate call is harmless. |
| 36461 | 37121 | */ |
| 36462 | 37122 | sqlite3WalEndReadTransaction(pPager->pWal); |
| 36463 | 37123 | |
| 36464 | 37124 | 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; |
| 37125 | + if( rc==SQLITE_OK && changed ){ |
| 37126 | + pager_reset(pPager); |
| 37127 | + } |
| 36474 | 37128 | |
| 36475 | 37129 | return rc; |
| 36476 | 37130 | } |
| 37131 | + |
| 37132 | +/* |
| 37133 | +** This function is called as part of the transition from PAGER_OPEN |
| 37134 | +** to PAGER_READER state to determine the size of the database file |
| 37135 | +** in pages (assuming the page size currently stored in Pager.pageSize). |
| 37136 | +** |
| 37137 | +** If no error occurs, SQLITE_OK is returned and the size of the database |
| 37138 | +** in pages is stored in *pnPage. Otherwise, an error code (perhaps |
| 37139 | +** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified. |
| 37140 | +*/ |
| 37141 | +static int pagerPagecount(Pager *pPager, Pgno *pnPage){ |
| 37142 | + Pgno nPage; /* Value to return via *pnPage */ |
| 37143 | + |
| 37144 | + /* Query the WAL sub-system for the database size. The WalDbsize() |
| 37145 | + ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or |
| 37146 | + ** if the database size is not available. The database size is not |
| 37147 | + ** available from the WAL sub-system if the log file is empty or |
| 37148 | + ** contains no valid committed transactions. |
| 37149 | + */ |
| 37150 | + assert( pPager->eState==PAGER_OPEN ); |
| 37151 | + assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock ); |
| 37152 | + nPage = sqlite3WalDbsize(pPager->pWal); |
| 37153 | + |
| 37154 | + /* If the database size was not available from the WAL sub-system, |
| 37155 | + ** determine it based on the size of the database file. If the size |
| 37156 | + ** of the database file is not an integer multiple of the page-size, |
| 37157 | + ** round down to the nearest page. Except, any file larger than 0 |
| 37158 | + ** bytes in size is considered to contain at least one page. |
| 37159 | + */ |
| 37160 | + if( nPage==0 ){ |
| 37161 | + i64 n = 0; /* Size of db file in bytes */ |
| 37162 | + assert( isOpen(pPager->fd) || pPager->tempFile ); |
| 37163 | + if( isOpen(pPager->fd) ){ |
| 37164 | + int rc = sqlite3OsFileSize(pPager->fd, &n); |
| 37165 | + if( rc!=SQLITE_OK ){ |
| 37166 | + return rc; |
| 37167 | + } |
| 37168 | + } |
| 37169 | + nPage = (Pgno)(n / pPager->pageSize); |
| 37170 | + if( nPage==0 && n>0 ){ |
| 37171 | + nPage = 1; |
| 37172 | + } |
| 37173 | + } |
| 37174 | + |
| 37175 | + /* If the current number of pages in the file is greater than the |
| 37176 | + ** configured maximum pager number, increase the allowed limit so |
| 37177 | + ** that the file can be read. |
| 37178 | + */ |
| 37179 | + if( nPage>pPager->mxPgno ){ |
| 37180 | + pPager->mxPgno = (Pgno)nPage; |
| 37181 | + } |
| 37182 | + |
| 37183 | + *pnPage = nPage; |
| 37184 | + return SQLITE_OK; |
| 37185 | +} |
| 37186 | + |
| 36477 | 37187 | |
| 36478 | 37188 | /* |
| 36479 | 37189 | ** Check if the *-wal file that corresponds to the database opened by pPager |
| 36480 | 37190 | ** exists if the database is not empy, or verify that the *-wal file does |
| 36481 | 37191 | ** not exist (by deleting it) if the database file is empty. |
| | @@ -36485,25 +37195,26 @@ |
| 36485 | 37195 | ** if no error occurs, make sure Pager.journalMode is not set to |
| 36486 | 37196 | ** PAGER_JOURNALMODE_WAL. |
| 36487 | 37197 | ** |
| 36488 | 37198 | ** Return SQLITE_OK or an error code. |
| 36489 | 37199 | ** |
| 36490 | | -** If the WAL file is opened, also open a snapshot (read transaction). |
| 36491 | | -** |
| 36492 | 37200 | ** The caller must hold a SHARED lock on the database file to call this |
| 36493 | 37201 | ** function. Because an EXCLUSIVE lock on the db file is required to delete |
| 36494 | 37202 | ** a WAL on a none-empty database, this ensures there is no race condition |
| 36495 | 37203 | ** between the xAccess() below and an xDelete() being executed by some |
| 36496 | 37204 | ** other connection. |
| 36497 | 37205 | */ |
| 36498 | 37206 | static int pagerOpenWalIfPresent(Pager *pPager){ |
| 36499 | 37207 | int rc = SQLITE_OK; |
| 37208 | + assert( pPager->eState==PAGER_OPEN ); |
| 37209 | + assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock ); |
| 37210 | + |
| 36500 | 37211 | if( !pPager->tempFile ){ |
| 36501 | 37212 | 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); |
| 37213 | + Pgno nPage; /* Size of the database file */ |
| 37214 | + |
| 37215 | + rc = pagerPagecount(pPager, &nPage); |
| 36505 | 37216 | if( rc ) return rc; |
| 36506 | 37217 | if( nPage==0 ){ |
| 36507 | 37218 | rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); |
| 36508 | 37219 | isWal = 0; |
| 36509 | 37220 | }else{ |
| | @@ -36511,15 +37222,12 @@ |
| 36511 | 37222 | pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal |
| 36512 | 37223 | ); |
| 36513 | 37224 | } |
| 36514 | 37225 | if( rc==SQLITE_OK ){ |
| 36515 | 37226 | if( isWal ){ |
| 36516 | | - pager_reset(pPager); |
| 37227 | + testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); |
| 36517 | 37228 | rc = sqlite3PagerOpenWal(pPager, 0); |
| 36518 | | - if( rc==SQLITE_OK ){ |
| 36519 | | - rc = pagerBeginReadTransaction(pPager); |
| 36520 | | - } |
| 36521 | 37229 | }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ |
| 36522 | 37230 | pPager->journalMode = PAGER_JOURNALMODE_DELETE; |
| 36523 | 37231 | } |
| 36524 | 37232 | } |
| 36525 | 37233 | } |
| | @@ -36567,11 +37275,12 @@ |
| 36567 | 37275 | i64 szJ; /* Effective size of the main journal */ |
| 36568 | 37276 | i64 iHdrOff; /* End of first segment of main-journal records */ |
| 36569 | 37277 | int rc = SQLITE_OK; /* Return code */ |
| 36570 | 37278 | Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */ |
| 36571 | 37279 | |
| 36572 | | - assert( pPager->state>=PAGER_SHARED ); |
| 37280 | + assert( pPager->eState!=PAGER_ERROR ); |
| 37281 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 36573 | 37282 | |
| 36574 | 37283 | /* Allocate a bitvec to use to store the set of pages rolled back */ |
| 36575 | 37284 | if( pSavepoint ){ |
| 36576 | 37285 | pDone = sqlite3BitvecCreate(pSavepoint->nOrig); |
| 36577 | 37286 | if( !pDone ){ |
| | @@ -36706,11 +37415,10 @@ |
| 36706 | 37415 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 36707 | 37416 | SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int bFullFsync){ |
| 36708 | 37417 | pPager->noSync = (level==1 || pPager->tempFile) ?1:0; |
| 36709 | 37418 | pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0; |
| 36710 | 37419 | pPager->sync_flags = (bFullFsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL); |
| 36711 | | - if( pPager->noSync ) pPager->needSync = 0; |
| 36712 | 37420 | } |
| 36713 | 37421 | #endif |
| 36714 | 37422 | |
| 36715 | 37423 | /* |
| 36716 | 37424 | ** The following global variable is incremented whenever the library |
| | @@ -36788,11 +37496,11 @@ |
| 36788 | 37496 | ** Change the page size used by the Pager object. The new page size |
| 36789 | 37497 | ** is passed in *pPageSize. |
| 36790 | 37498 | ** |
| 36791 | 37499 | ** If the pager is in the error state when this function is called, it |
| 36792 | 37500 | ** 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). |
| 37501 | +** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL). |
| 36794 | 37502 | ** |
| 36795 | 37503 | ** Otherwise, if all of the following are true: |
| 36796 | 37504 | ** |
| 36797 | 37505 | ** * the new page size (value of *pPageSize) is valid (a power |
| 36798 | 37506 | ** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and |
| | @@ -36812,32 +37520,52 @@ |
| 36812 | 37520 | ** If the page size is not changed, either because one of the enumerated |
| 36813 | 37521 | ** conditions above is not true, the pager was in error state when this |
| 36814 | 37522 | ** function was called, or because the memory allocation attempt failed, |
| 36815 | 37523 | ** then *pPageSize is set to the old, retained page size before returning. |
| 36816 | 37524 | */ |
| 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; |
| 37525 | +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ |
| 37526 | + int rc = SQLITE_OK; |
| 37527 | + |
| 37528 | + /* It is not possible to do a full assert_pager_state() here, as this |
| 37529 | + ** function may be called from within PagerOpen(), before the state |
| 37530 | + ** of the Pager object is internally consistent. |
| 37531 | + ** |
| 37532 | + ** At one point this function returned an error if the pager was in |
| 37533 | + ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that |
| 37534 | + ** there is at least one outstanding page reference, this function |
| 37535 | + ** is a no-op for that case anyhow. |
| 37536 | + */ |
| 37537 | + |
| 37538 | + u32 pageSize = *pPageSize; |
| 37539 | + assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); |
| 37540 | + if( (pPager->memDb==0 || pPager->dbSize==0) |
| 37541 | + && sqlite3PcacheRefCount(pPager->pPCache)==0 |
| 37542 | + && pageSize && pageSize!=(u32)pPager->pageSize |
| 37543 | + ){ |
| 37544 | + char *pNew = NULL; /* New temp space */ |
| 37545 | + i64 nByte = 0; |
| 37546 | + |
| 37547 | + if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){ |
| 37548 | + rc = sqlite3OsFileSize(pPager->fd, &nByte); |
| 37549 | + } |
| 37550 | + if( rc==SQLITE_OK ){ |
| 37551 | + pNew = (char *)sqlite3PageMalloc(pageSize); |
| 37552 | + if( !pNew ) rc = SQLITE_NOMEM; |
| 37553 | + } |
| 37554 | + |
| 37555 | + if( rc==SQLITE_OK ){ |
| 37556 | + pager_reset(pPager); |
| 37557 | + pPager->dbSize = (Pgno)(nByte/pageSize); |
| 37558 | + pPager->pageSize = pageSize; |
| 37559 | + sqlite3PageFree(pPager->pTmpSpace); |
| 37560 | + pPager->pTmpSpace = pNew; |
| 37561 | + sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 37562 | + } |
| 37563 | + } |
| 37564 | + |
| 37565 | + *pPageSize = pPager->pageSize; |
| 37566 | + if( rc==SQLITE_OK ){ |
| 36839 | 37567 | if( nReserve<0 ) nReserve = pPager->nReserve; |
| 36840 | 37568 | assert( nReserve>=0 && nReserve<1000 ); |
| 36841 | 37569 | pPager->nReserve = (i16)nReserve; |
| 36842 | 37570 | pagerReportSize(pPager); |
| 36843 | 37571 | } |
| | @@ -36862,17 +37590,15 @@ |
| 36862 | 37590 | ** maximum page count below the current size of the database. |
| 36863 | 37591 | ** |
| 36864 | 37592 | ** Regardless of mxPage, return the current maximum page count. |
| 36865 | 37593 | */ |
| 36866 | 37594 | SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){ |
| 36867 | | - int nPage; |
| 36868 | 37595 | if( mxPage>0 ){ |
| 36869 | 37596 | pPager->mxPgno = mxPage; |
| 36870 | 37597 | } |
| 36871 | | - if( pPager->state!=PAGER_UNLOCK ){ |
| 36872 | | - sqlite3PagerPagecount(pPager, &nPage); |
| 36873 | | - assert( (int)pPager->mxPgno>=nPage ); |
| 37598 | + if( pPager->eState!=PAGER_OPEN && pPager->mxPgno<pPager->dbSize ){ |
| 37599 | + pPager->mxPgno = pPager->dbSize; |
| 36874 | 37600 | } |
| 36875 | 37601 | return pPager->mxPgno; |
| 36876 | 37602 | } |
| 36877 | 37603 | |
| 36878 | 37604 | /* |
| | @@ -36933,70 +37659,20 @@ |
| 36933 | 37659 | } |
| 36934 | 37660 | return rc; |
| 36935 | 37661 | } |
| 36936 | 37662 | |
| 36937 | 37663 | /* |
| 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>). |
| 37664 | +** This function may only be called when a read-transaction is open on |
| 37665 | +** the pager. It returns the total number of pages in the database. |
| 37666 | +** |
| 36940 | 37667 | ** However, if the file is between 1 and <page-size> bytes in size, then |
| 36941 | 37668 | ** 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; |
| 37669 | +*/ |
| 37670 | +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){ |
| 37671 | + assert( pPager->eState>=PAGER_READER ); |
| 37672 | + assert( pPager->eState!=PAGER_WRITER_FINISHED ); |
| 37673 | + *pnPage = (int)pPager->dbSize; |
| 36998 | 37674 | } |
| 36999 | 37675 | |
| 37000 | 37676 | |
| 37001 | 37677 | /* |
| 37002 | 37678 | ** Try to obtain a lock of type locktype on the database file. If |
| | @@ -37013,42 +37689,23 @@ |
| 37013 | 37689 | ** variable to locktype before returning. |
| 37014 | 37690 | */ |
| 37015 | 37691 | static int pager_wait_on_lock(Pager *pPager, int locktype){ |
| 37016 | 37692 | int rc; /* Return code */ |
| 37017 | 37693 | |
| 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 | 37694 | /* Check that this is either a no-op (because the requested lock is |
| 37030 | 37695 | ** already held, or one of the transistions that the busy-handler |
| 37031 | 37696 | ** may be invoked during, according to the comment above |
| 37032 | 37697 | ** sqlite3PagerSetBusyhandler(). |
| 37033 | 37698 | */ |
| 37034 | | - assert( (pPager->state>=locktype) |
| 37035 | | - || (pPager->state==PAGER_UNLOCK && locktype==PAGER_SHARED) |
| 37036 | | - || (pPager->state==PAGER_RESERVED && locktype==PAGER_EXCLUSIVE) |
| 37699 | + assert( (pPager->eLock>=locktype) |
| 37700 | + || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK) |
| 37701 | + || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK) |
| 37037 | 37702 | ); |
| 37038 | 37703 | |
| 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 | | - } |
| 37704 | + do { |
| 37705 | + rc = pagerLockDb(pPager, locktype); |
| 37706 | + }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) ); |
| 37050 | 37707 | return rc; |
| 37051 | 37708 | } |
| 37052 | 37709 | |
| 37053 | 37710 | /* |
| 37054 | 37711 | ** Function assertTruncateConstraint(pPager) checks that one of the |
| | @@ -37089,13 +37746,12 @@ |
| 37089 | 37746 | ** function does not actually modify the database file on disk. It |
| 37090 | 37747 | ** just sets the internal state of the pager object so that the |
| 37091 | 37748 | ** truncation will be done when the current transaction is committed. |
| 37092 | 37749 | */ |
| 37093 | 37750 | SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){ |
| 37094 | | - assert( pPager->dbSizeValid ); |
| 37095 | 37751 | assert( pPager->dbSize>=nPage ); |
| 37096 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 37752 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 37097 | 37753 | pPager->dbSize = nPage; |
| 37098 | 37754 | assertTruncateConstraint(pPager); |
| 37099 | 37755 | } |
| 37100 | 37756 | |
| 37101 | 37757 | |
| | @@ -37141,11 +37797,11 @@ |
| 37141 | 37797 | SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ |
| 37142 | 37798 | u8 *pTmp = (u8 *)pPager->pTmpSpace; |
| 37143 | 37799 | |
| 37144 | 37800 | disable_simulated_io_errors(); |
| 37145 | 37801 | sqlite3BeginBenignMalloc(); |
| 37146 | | - pPager->errCode = 0; |
| 37802 | + /* pPager->errCode = 0; */ |
| 37147 | 37803 | pPager->exclusiveMode = 0; |
| 37148 | 37804 | #ifndef SQLITE_OMIT_WAL |
| 37149 | 37805 | sqlite3WalClose(pPager->pWal, |
| 37150 | 37806 | (pPager->noSync ? 0 : pPager->sync_flags), |
| 37151 | 37807 | pPager->pageSize, pTmp |
| | @@ -37154,18 +37810,23 @@ |
| 37154 | 37810 | #endif |
| 37155 | 37811 | pager_reset(pPager); |
| 37156 | 37812 | if( MEMDB ){ |
| 37157 | 37813 | pager_unlock(pPager); |
| 37158 | 37814 | }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. |
| 37815 | + /* If it is open, sync the journal file before calling UnlockAndRollback. |
| 37816 | + ** If this is not done, then an unsynced portion of the open journal |
| 37817 | + ** file may be played back into the database. If a power failure occurs |
| 37818 | + ** while this is happening, the database could become corrupt. |
| 37819 | + ** |
| 37820 | + ** If an error occurs while trying to sync the journal, shift the pager |
| 37821 | + ** into the ERROR state. This causes UnlockAndRollback to unlock the |
| 37822 | + ** database and close the journal file without attempting to roll it |
| 37823 | + ** back or finalize it. The next database user will have to do hot-journal |
| 37824 | + ** rollback before accessing the database file. |
| 37164 | 37825 | */ |
| 37165 | 37826 | if( isOpen(pPager->jfd) ){ |
| 37166 | | - pPager->errCode = pagerSyncHotJournal(pPager); |
| 37827 | + pager_error(pPager, pagerSyncHotJournal(pPager)); |
| 37167 | 37828 | } |
| 37168 | 37829 | pagerUnlockAndRollback(pPager); |
| 37169 | 37830 | } |
| 37170 | 37831 | sqlite3EndBenignMalloc(); |
| 37171 | 37832 | enable_simulated_io_errors(); |
| | @@ -37206,13 +37867,13 @@ |
| 37206 | 37867 | /* |
| 37207 | 37868 | ** Sync the journal. In other words, make sure all the pages that have |
| 37208 | 37869 | ** been written to the journal have actually reached the surface of the |
| 37209 | 37870 | ** disk and can be restored in the event of a hot-journal rollback. |
| 37210 | 37871 | ** |
| 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: |
| 37872 | +** If the Pager.noSync flag is set, then this function is a no-op. |
| 37873 | +** Otherwise, the actions required depend on the journal-mode and the |
| 37874 | +** device characteristics of the the file-system, as follows: |
| 37214 | 37875 | ** |
| 37215 | 37876 | ** * If the journal file is an in-memory journal file, no action need |
| 37216 | 37877 | ** be taken. |
| 37217 | 37878 | ** |
| 37218 | 37879 | ** * Otherwise, if the device does not support the SAFE_APPEND property, |
| | @@ -37232,22 +37893,29 @@ |
| 37232 | 37893 | ** <update nRec field> |
| 37233 | 37894 | ** } |
| 37234 | 37895 | ** if( NOT SEQUENTIAL ) xSync(<journal file>); |
| 37235 | 37896 | ** } |
| 37236 | 37897 | ** |
| 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 | 37898 | ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every |
| 37241 | 37899 | ** page currently held in memory before returning SQLITE_OK. If an IO |
| 37242 | 37900 | ** error is encountered, then the IO error code is returned to the caller. |
| 37243 | 37901 | */ |
| 37244 | | -static int syncJournal(Pager *pPager){ |
| 37245 | | - if( pPager->needSync ){ |
| 37902 | +static int syncJournal(Pager *pPager, int newHdr){ |
| 37903 | + int rc; /* Return code */ |
| 37904 | + |
| 37905 | + assert( pPager->eState==PAGER_WRITER_CACHEMOD |
| 37906 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 37907 | + ); |
| 37908 | + assert( assert_pager_state(pPager) ); |
| 37909 | + assert( !pagerUseWal(pPager) ); |
| 37910 | + |
| 37911 | + rc = sqlite3PagerExclusiveLock(pPager); |
| 37912 | + if( rc!=SQLITE_OK ) return rc; |
| 37913 | + |
| 37914 | + if( !pPager->noSync ){ |
| 37246 | 37915 | assert( !pPager->tempFile ); |
| 37247 | | - if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ |
| 37248 | | - int rc; /* Return code */ |
| 37916 | + if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){ |
| 37249 | 37917 | const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| 37250 | 37918 | assert( isOpen(pPager->jfd) ); |
| 37251 | 37919 | |
| 37252 | 37920 | if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
| 37253 | 37921 | /* This block deals with an obscure problem. If the last connection |
| | @@ -37318,21 +37986,29 @@ |
| 37318 | 37986 | rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| |
| 37319 | 37987 | (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0) |
| 37320 | 37988 | ); |
| 37321 | 37989 | if( rc!=SQLITE_OK ) return rc; |
| 37322 | 37990 | } |
| 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); |
| 37991 | + |
| 37992 | + pPager->journalHdr = pPager->journalOff; |
| 37993 | + if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){ |
| 37994 | + pPager->nRec = 0; |
| 37995 | + rc = writeJournalHdr(pPager); |
| 37996 | + if( rc!=SQLITE_OK ) return rc; |
| 37997 | + } |
| 37998 | + }else{ |
| 37999 | + pPager->journalHdr = pPager->journalOff; |
| 38000 | + } |
| 37332 | 38001 | } |
| 37333 | 38002 | |
| 38003 | + /* Unless the pager is in noSync mode, the journal file was just |
| 38004 | + ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on |
| 38005 | + ** all pages. |
| 38006 | + */ |
| 38007 | + sqlite3PcacheClearSyncFlags(pPager->pPCache); |
| 38008 | + pPager->eState = PAGER_WRITER_DBMOD; |
| 38009 | + assert( assert_pager_state(pPager) ); |
| 37334 | 38010 | return SQLITE_OK; |
| 37335 | 38011 | } |
| 37336 | 38012 | |
| 37337 | 38013 | /* |
| 37338 | 38014 | ** The argument is the first in a linked list of dirty pages connected |
| | @@ -37365,31 +38041,16 @@ |
| 37365 | 38041 | ** If everything is successful, SQLITE_OK is returned. If an IO error |
| 37366 | 38042 | ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot |
| 37367 | 38043 | ** be obtained, SQLITE_BUSY is returned. |
| 37368 | 38044 | */ |
| 37369 | 38045 | 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 | | - */ |
| 38046 | + int rc = SQLITE_OK; /* Return code */ |
| 38047 | + |
| 38048 | + /* This function is only called for rollback pagers in WRITER_DBMOD state. */ |
| 37388 | 38049 | assert( !pagerUseWal(pPager) ); |
| 37389 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 37390 | | - rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 38050 | + assert( pPager->eState==PAGER_WRITER_DBMOD ); |
| 38051 | + assert( pPager->eLock==EXCLUSIVE_LOCK ); |
| 37391 | 38052 | |
| 37392 | 38053 | /* If the file is a temp-file has not yet been opened, open it now. It |
| 37393 | 38054 | ** is not possible for rc to be other than SQLITE_OK if this branch |
| 37394 | 38055 | ** is taken, as pager_wait_on_lock() is a no-op for temp-files. |
| 37395 | 38056 | */ |
| | @@ -37400,13 +38061,14 @@ |
| 37400 | 38061 | |
| 37401 | 38062 | /* Before the first write, give the VFS a hint of what the final |
| 37402 | 38063 | ** file size will be. |
| 37403 | 38064 | */ |
| 37404 | 38065 | assert( rc!=SQLITE_OK || isOpen(pPager->fd) ); |
| 37405 | | - if( rc==SQLITE_OK && pPager->dbSize>(pPager->dbOrigSize+1) ){ |
| 38066 | + if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){ |
| 37406 | 38067 | sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize; |
| 37407 | 38068 | sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile); |
| 38069 | + pPager->dbHintSize = pPager->dbSize; |
| 37408 | 38070 | } |
| 37409 | 38071 | |
| 37410 | 38072 | while( rc==SQLITE_OK && pList ){ |
| 37411 | 38073 | Pgno pgno = pList->pgno; |
| 37412 | 38074 | |
| | @@ -37419,10 +38081,12 @@ |
| 37419 | 38081 | ** set (set by sqlite3PagerDontWrite()). |
| 37420 | 38082 | */ |
| 37421 | 38083 | if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){ |
| 37422 | 38084 | i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */ |
| 37423 | 38085 | char *pData; /* Data to write */ |
| 38086 | + |
| 38087 | + assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); |
| 37424 | 38088 | |
| 37425 | 38089 | /* Encode the database */ |
| 37426 | 38090 | CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData); |
| 37427 | 38091 | |
| 37428 | 38092 | /* Write out the page data. */ |
| | @@ -37448,13 +38112,11 @@ |
| 37448 | 38112 | PAGER_INCR(sqlite3_pager_writedb_count); |
| 37449 | 38113 | PAGER_INCR(pPager->nWrite); |
| 37450 | 38114 | }else{ |
| 37451 | 38115 | PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno)); |
| 37452 | 38116 | } |
| 37453 | | -#ifdef SQLITE_CHECK_PAGES |
| 37454 | | - pList->pageHash = pager_pagehash(pList); |
| 37455 | | -#endif |
| 38117 | + pager_set_pagehash(pList); |
| 37456 | 38118 | pList = pList->pDirty; |
| 37457 | 38119 | } |
| 37458 | 38120 | |
| 37459 | 38121 | return rc; |
| 37460 | 38122 | } |
| | @@ -37562,13 +38224,18 @@ |
| 37562 | 38224 | ** pages belonging to the same sector. |
| 37563 | 38225 | ** |
| 37564 | 38226 | ** The doNotSpill flag inhibits all cache spilling regardless of whether |
| 37565 | 38227 | ** or not a sync is required. This is set during a rollback. |
| 37566 | 38228 | ** |
| 37567 | | - ** Spilling is also inhibited when in an error state. |
| 38229 | + ** Spilling is also prohibited when in an error state since that could |
| 38230 | + ** lead to database corruption. In the current implementaton it |
| 38231 | + ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1 |
| 38232 | + ** while in the error state, hence it is impossible for this routine to |
| 38233 | + ** be called in the error state. Nevertheless, we include a NEVER() |
| 38234 | + ** test for the error state as a safeguard against future changes. |
| 37568 | 38235 | */ |
| 37569 | | - if( pPager->errCode ) return SQLITE_OK; |
| 38236 | + if( NEVER(pPager->errCode) ) return SQLITE_OK; |
| 37570 | 38237 | if( pPager->doNotSpill ) return SQLITE_OK; |
| 37571 | 38238 | if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){ |
| 37572 | 38239 | return SQLITE_OK; |
| 37573 | 38240 | } |
| 37574 | 38241 | |
| | @@ -37582,20 +38249,14 @@ |
| 37582 | 38249 | rc = pagerWalFrames(pPager, pPg, 0, 0, 0); |
| 37583 | 38250 | } |
| 37584 | 38251 | }else{ |
| 37585 | 38252 | |
| 37586 | 38253 | /* 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 | | - } |
| 38254 | + if( pPg->flags&PGHDR_NEED_SYNC |
| 38255 | + || pPager->eState==PAGER_WRITER_CACHEMOD |
| 38256 | + ){ |
| 38257 | + rc = syncJournal(pPager, 1); |
| 37597 | 38258 | } |
| 37598 | 38259 | |
| 37599 | 38260 | /* If the page number of this page is larger than the current size of |
| 37600 | 38261 | ** the database image, it may need to be written to the sub-journal. |
| 37601 | 38262 | ** This is because the call to pager_write_pagelist() below will not |
| | @@ -37629,10 +38290,11 @@ |
| 37629 | 38290 | rc = subjournalPage(pPg); |
| 37630 | 38291 | } |
| 37631 | 38292 | |
| 37632 | 38293 | /* Write the contents of the page out to the database file. */ |
| 37633 | 38294 | if( rc==SQLITE_OK ){ |
| 38295 | + assert( (pPg->flags&PGHDR_NEED_SYNC)==0 ); |
| 37634 | 38296 | rc = pager_write_pagelist(pPager, pPg); |
| 37635 | 38297 | } |
| 37636 | 38298 | } |
| 37637 | 38299 | |
| 37638 | 38300 | /* Mark the page as clean. */ |
| | @@ -37639,11 +38301,11 @@ |
| 37639 | 38301 | if( rc==SQLITE_OK ){ |
| 37640 | 38302 | PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno)); |
| 37641 | 38303 | sqlite3PcacheMakeClean(pPg); |
| 37642 | 38304 | } |
| 37643 | 38305 | |
| 37644 | | - return pager_error(pPager, rc); |
| 38306 | + return pager_error(pPager, rc); |
| 37645 | 38307 | } |
| 37646 | 38308 | |
| 37647 | 38309 | |
| 37648 | 38310 | /* |
| 37649 | 38311 | ** Allocate and initialize a new Pager object and put a pointer to it |
| | @@ -37694,11 +38356,11 @@ |
| 37694 | 38356 | char *zPathname = 0; /* Full path to database file */ |
| 37695 | 38357 | int nPathname = 0; /* Number of bytes in zPathname */ |
| 37696 | 38358 | int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */ |
| 37697 | 38359 | int noReadlock = (flags & PAGER_NO_READLOCK)!=0; /* True to omit read-lock */ |
| 37698 | 38360 | int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */ |
| 37699 | | - u16 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ |
| 38361 | + u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE; /* Default page size */ |
| 37700 | 38362 | |
| 37701 | 38363 | /* Figure out how much space is required for each journal file-handle |
| 37702 | 38364 | ** (there are two of them, the main journal and the sub-journal). This |
| 37703 | 38365 | ** is the maximum space required for an in-memory journal file handle |
| 37704 | 38366 | ** and a regular journal file-handle. Note that a "regular journal-handle" |
| | @@ -37829,11 +38491,11 @@ |
| 37829 | 38491 | assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE); |
| 37830 | 38492 | if( szPageDflt<pPager->sectorSize ){ |
| 37831 | 38493 | if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){ |
| 37832 | 38494 | szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE; |
| 37833 | 38495 | }else{ |
| 37834 | | - szPageDflt = (u16)pPager->sectorSize; |
| 38496 | + szPageDflt = (u32)pPager->sectorSize; |
| 37835 | 38497 | } |
| 37836 | 38498 | } |
| 37837 | 38499 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 37838 | 38500 | { |
| 37839 | 38501 | int iDc = sqlite3OsDeviceCharacteristics(pPager->fd); |
| | @@ -37857,11 +38519,12 @@ |
| 37857 | 38519 | ** This branch is also run for an in-memory database. An in-memory |
| 37858 | 38520 | ** database is the same as a temp-file that is never written out to |
| 37859 | 38521 | ** disk and uses an in-memory rollback journal. |
| 37860 | 38522 | */ |
| 37861 | 38523 | tempFile = 1; |
| 37862 | | - pPager->state = PAGER_EXCLUSIVE; |
| 38524 | + pPager->eState = PAGER_READER; |
| 38525 | + pPager->eLock = EXCLUSIVE_LOCK; |
| 37863 | 38526 | readOnly = (vfsFlags&SQLITE_OPEN_READONLY); |
| 37864 | 38527 | } |
| 37865 | 38528 | |
| 37866 | 38529 | /* The following call to PagerSetPagesize() serves to set the value of |
| 37867 | 38530 | ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer. |
| | @@ -37894,27 +38557,27 @@ |
| 37894 | 38557 | pPager->useJournal = (u8)useJournal; |
| 37895 | 38558 | pPager->noReadlock = (noReadlock && readOnly) ?1:0; |
| 37896 | 38559 | /* pPager->stmtOpen = 0; */ |
| 37897 | 38560 | /* pPager->stmtInUse = 0; */ |
| 37898 | 38561 | /* pPager->nRef = 0; */ |
| 37899 | | - pPager->dbSizeValid = (u8)memDb; |
| 37900 | 38562 | /* pPager->stmtSize = 0; */ |
| 37901 | 38563 | /* pPager->stmtJSize = 0; */ |
| 37902 | 38564 | /* pPager->nPage = 0; */ |
| 37903 | 38565 | pPager->mxPgno = SQLITE_MAX_PAGE_COUNT; |
| 37904 | 38566 | /* pPager->state = PAGER_UNLOCK; */ |
| 38567 | +#if 0 |
| 37905 | 38568 | assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) ); |
| 38569 | +#endif |
| 37906 | 38570 | /* pPager->errMask = 0; */ |
| 37907 | 38571 | pPager->tempFile = (u8)tempFile; |
| 37908 | 38572 | assert( tempFile==PAGER_LOCKINGMODE_NORMAL |
| 37909 | 38573 | || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE ); |
| 37910 | 38574 | assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 ); |
| 37911 | 38575 | pPager->exclusiveMode = (u8)tempFile; |
| 37912 | 38576 | pPager->changeCountDone = pPager->tempFile; |
| 37913 | 38577 | pPager->memDb = (u8)memDb; |
| 37914 | 38578 | pPager->readOnly = (u8)readOnly; |
| 37915 | | - /* pPager->needSync = 0; */ |
| 37916 | 38579 | assert( useJournal || pPager->tempFile ); |
| 37917 | 38580 | pPager->noSync = pPager->tempFile; |
| 37918 | 38581 | pPager->fullSync = pPager->noSync ?0:1; |
| 37919 | 38582 | pPager->sync_flags = SQLITE_SYNC_NORMAL; |
| 37920 | 38583 | /* pPager->pFirst = 0; */ |
| | @@ -37975,24 +38638,24 @@ |
| 37975 | 38638 | sqlite3_vfs * const pVfs = pPager->pVfs; |
| 37976 | 38639 | int rc = SQLITE_OK; /* Return code */ |
| 37977 | 38640 | int exists = 1; /* True if a journal file is present */ |
| 37978 | 38641 | int jrnlOpen = !!isOpen(pPager->jfd); |
| 37979 | 38642 | |
| 37980 | | - assert( pPager!=0 ); |
| 37981 | 38643 | assert( pPager->useJournal ); |
| 37982 | 38644 | assert( isOpen(pPager->fd) ); |
| 37983 | | - assert( pPager->state <= PAGER_SHARED ); |
| 38645 | + assert( pPager->eState==PAGER_OPEN ); |
| 38646 | + |
| 37984 | 38647 | assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) & |
| 37985 | 38648 | SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
| 37986 | 38649 | )); |
| 37987 | 38650 | |
| 37988 | 38651 | *pExists = 0; |
| 37989 | 38652 | if( !jrnlOpen ){ |
| 37990 | 38653 | rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists); |
| 37991 | 38654 | } |
| 37992 | 38655 | if( rc==SQLITE_OK && exists ){ |
| 37993 | | - int locked; /* True if some process holds a RESERVED lock */ |
| 38656 | + int locked = 0; /* True if some process holds a RESERVED lock */ |
| 37994 | 38657 | |
| 37995 | 38658 | /* Race condition here: Another process might have been holding the |
| 37996 | 38659 | ** the RESERVED lock and have a journal open at the sqlite3OsAccess() |
| 37997 | 38660 | ** call above, but then delete the journal and drop the lock before |
| 37998 | 38661 | ** we get to the following sqlite3OsCheckReservedLock() call. If that |
| | @@ -38000,25 +38663,25 @@ |
| 38000 | 38663 | ** in fact there is none. This results in a false-positive which will |
| 38001 | 38664 | ** be dealt with by the playback routine. Ticket #3883. |
| 38002 | 38665 | */ |
| 38003 | 38666 | rc = sqlite3OsCheckReservedLock(pPager->fd, &locked); |
| 38004 | 38667 | if( rc==SQLITE_OK && !locked ){ |
| 38005 | | - int nPage; |
| 38668 | + Pgno nPage; /* Number of pages in database file */ |
| 38006 | 38669 | |
| 38007 | 38670 | /* Check the size of the database file. If it consists of 0 pages, |
| 38008 | 38671 | ** then delete the journal file. See the header comment above for |
| 38009 | 38672 | ** the reasoning here. Delete the obsolete journal file under |
| 38010 | 38673 | ** a RESERVED lock to avoid race conditions and to avoid violating |
| 38011 | 38674 | ** [H33020]. |
| 38012 | 38675 | */ |
| 38013 | | - rc = sqlite3PagerPagecount(pPager, &nPage); |
| 38676 | + rc = pagerPagecount(pPager, &nPage); |
| 38014 | 38677 | if( rc==SQLITE_OK ){ |
| 38015 | 38678 | if( nPage==0 ){ |
| 38016 | 38679 | sqlite3BeginBenignMalloc(); |
| 38017 | | - if( sqlite3OsLock(pPager->fd, RESERVED_LOCK)==SQLITE_OK ){ |
| 38680 | + if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){ |
| 38018 | 38681 | sqlite3OsDelete(pVfs, pPager->zJournal, 0); |
| 38019 | | - sqlite3OsUnlock(pPager->fd, SHARED_LOCK); |
| 38682 | + pagerUnlockDb(pPager, SHARED_LOCK); |
| 38020 | 38683 | } |
| 38021 | 38684 | sqlite3EndBenignMalloc(); |
| 38022 | 38685 | }else{ |
| 38023 | 38686 | /* The journal file exists and no other connection has a reserved |
| 38024 | 38687 | ** or greater lock on the database file. Now check that there is |
| | @@ -38067,11 +38730,11 @@ |
| 38067 | 38730 | ** has been successfully called. If a shared-lock is already held when |
| 38068 | 38731 | ** this function is called, it is a no-op. |
| 38069 | 38732 | ** |
| 38070 | 38733 | ** The following operations are also performed by this function. |
| 38071 | 38734 | ** |
| 38072 | | -** 1) If the pager is currently in PAGER_UNLOCK state (no lock held |
| 38735 | +** 1) If the pager is currently in PAGER_OPEN state (no lock held |
| 38073 | 38736 | ** on the database file), then an attempt is made to obtain a |
| 38074 | 38737 | ** SHARED lock on the database file. Immediately after obtaining |
| 38075 | 38738 | ** the SHARED lock, the file-system is checked for a hot-journal, |
| 38076 | 38739 | ** which is played back if present. Following any hot-journal |
| 38077 | 38740 | ** rollback, the contents of the cache are validated by checking |
| | @@ -38082,70 +38745,51 @@ |
| 38082 | 38745 | ** no outstanding references to any pages, and is in the error state, |
| 38083 | 38746 | ** then an attempt is made to clear the error state by discarding |
| 38084 | 38747 | ** the contents of the page cache and rolling back any open journal |
| 38085 | 38748 | ** file. |
| 38086 | 38749 | ** |
| 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. |
| 38750 | +** If everything is successful, SQLITE_OK is returned. If an IO error |
| 38751 | +** occurs while locking the database, checking for a hot-journal file or |
| 38752 | +** rolling back a journal file, the IO error code is returned. |
| 38095 | 38753 | */ |
| 38096 | 38754 | SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){ |
| 38097 | 38755 | int rc = SQLITE_OK; /* Return code */ |
| 38098 | | - int isErrorReset = 0; /* True if recovering from error state */ |
| 38099 | 38756 | |
| 38100 | 38757 | /* This routine is only called from b-tree and only when there are no |
| 38101 | | - ** outstanding pages */ |
| 38758 | + ** outstanding pages. This implies that the pager state should either |
| 38759 | + ** be OPEN or READER. READER is only possible if the pager is or was in |
| 38760 | + ** exclusive access mode. |
| 38761 | + */ |
| 38102 | 38762 | assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 38763 | + assert( assert_pager_state(pPager) ); |
| 38764 | + assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER ); |
| 38103 | 38765 | if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; } |
| 38104 | 38766 | |
| 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; |
| 38767 | + if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){ |
| 38768 | + int bHotJournal = 1; /* True if there exists a hot journal-file */ |
| 38769 | + |
| 38122 | 38770 | assert( !MEMDB ); |
| 38123 | | - assert( sqlite3PcacheRefCount(pPager->pPCache)==0 ); |
| 38124 | | - if( pPager->noReadlock ){ |
| 38125 | | - assert( pPager->readOnly ); |
| 38126 | | - pPager->state = PAGER_SHARED; |
| 38127 | | - }else{ |
| 38771 | + assert( pPager->noReadlock==0 || pPager->readOnly ); |
| 38772 | + |
| 38773 | + if( pPager->noReadlock==0 ){ |
| 38128 | 38774 | rc = pager_wait_on_lock(pPager, SHARED_LOCK); |
| 38129 | 38775 | if( rc!=SQLITE_OK ){ |
| 38130 | | - assert( pPager->state==PAGER_UNLOCK ); |
| 38131 | | - return pager_error(pPager, rc); |
| 38776 | + assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK ); |
| 38777 | + goto failed; |
| 38132 | 38778 | } |
| 38133 | 38779 | } |
| 38134 | | - assert( pPager->state>=SHARED_LOCK ); |
| 38135 | 38780 | |
| 38136 | 38781 | /* If a journal file exists, and there is no RESERVED lock on the |
| 38137 | 38782 | ** database file, then it either needs to be played back or deleted. |
| 38138 | 38783 | */ |
| 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 ){ |
| 38784 | + if( pPager->eLock<=SHARED_LOCK ){ |
| 38785 | + rc = hasHotJournal(pPager, &bHotJournal); |
| 38786 | + } |
| 38787 | + if( rc!=SQLITE_OK ){ |
| 38788 | + goto failed; |
| 38789 | + } |
| 38790 | + if( bHotJournal ){ |
| 38147 | 38791 | /* Get an EXCLUSIVE lock on the database file. At this point it is |
| 38148 | 38792 | ** important that a RESERVED lock is not obtained on the way to the |
| 38149 | 38793 | ** EXCLUSIVE lock. If it were, another process might open the |
| 38150 | 38794 | ** database file, detect the RESERVED lock, and conclude that the |
| 38151 | 38795 | ** database is safe to read while this process is still rolling the |
| | @@ -38153,62 +38797,49 @@ |
| 38153 | 38797 | ** |
| 38154 | 38798 | ** Because the intermediate RESERVED lock is not requested, any |
| 38155 | 38799 | ** other process attempting to access the database file will get to |
| 38156 | 38800 | ** this point in the code and fail to obtain its own EXCLUSIVE lock |
| 38157 | 38801 | ** 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. */ |
| 38802 | + ** |
| 38803 | + ** Unless the pager is in locking_mode=exclusive mode, the lock is |
| 38804 | + ** downgraded to SHARED_LOCK before this function returns. |
| 38805 | + */ |
| 38806 | + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); |
| 38807 | + if( rc!=SQLITE_OK ){ |
| 38808 | + goto failed; |
| 38809 | + } |
| 38810 | + |
| 38811 | + /* If it is not already open and the file exists on disk, open the |
| 38812 | + ** journal for read/write access. Write access is required because |
| 38813 | + ** in exclusive-access mode the file descriptor will be kept open |
| 38814 | + ** and possibly used for a transaction later on. Also, write-access |
| 38815 | + ** is usually required to finalize the journal in journal_mode=persist |
| 38816 | + ** mode (and also for journal_mode=truncate on some systems). |
| 38817 | + ** |
| 38818 | + ** If the journal does not exist, it usually means that some |
| 38819 | + ** other connection managed to get in and roll it back before |
| 38820 | + ** this connection obtained the exclusive lock above. Or, it |
| 38821 | + ** may mean that the pager was in the error-state when this |
| 38822 | + ** function was called and the journal file does not exist. |
| 38823 | + */ |
| 38824 | + if( !isOpen(pPager->jfd) ){ |
| 38825 | + sqlite3_vfs * const pVfs = pPager->pVfs; |
| 38826 | + int bExists; /* True if journal file exists */ |
| 38827 | + rc = sqlite3OsAccess( |
| 38828 | + pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists); |
| 38829 | + if( rc==SQLITE_OK && bExists ){ |
| 38830 | + int fout = 0; |
| 38831 | + int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; |
| 38832 | + assert( !pPager->tempFile ); |
| 38833 | + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout); |
| 38834 | + assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); |
| 38835 | + if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){ |
| 38836 | + rc = SQLITE_CANTOPEN_BKPT; |
| 38837 | + sqlite3OsClose(pPager->jfd); |
| 38838 | + } |
| 38839 | + } |
| 38840 | + } |
| 38210 | 38841 | |
| 38211 | 38842 | /* Playback and delete the journal. Drop the database write |
| 38212 | 38843 | ** lock and reacquire the read lock. Purge the cache before |
| 38213 | 38844 | ** playing back the hot-journal so that we don't end up with |
| 38214 | 38845 | ** an inconsistent cache. Sync the hot journal before playing |
| | @@ -38215,25 +38846,50 @@ |
| 38215 | 38846 | ** it back since the process that crashed and left the hot journal |
| 38216 | 38847 | ** probably did not sync it and we are required to always sync |
| 38217 | 38848 | ** the journal before playing it back. |
| 38218 | 38849 | */ |
| 38219 | 38850 | if( isOpen(pPager->jfd) ){ |
| 38851 | + assert( rc==SQLITE_OK ); |
| 38220 | 38852 | rc = pagerSyncHotJournal(pPager); |
| 38221 | 38853 | if( rc==SQLITE_OK ){ |
| 38222 | 38854 | 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) |
| 38855 | + pPager->eState = PAGER_OPEN; |
| 38856 | + } |
| 38857 | + }else if( !pPager->exclusiveMode ){ |
| 38858 | + pagerUnlockDb(pPager, SHARED_LOCK); |
| 38859 | + } |
| 38860 | + |
| 38861 | + if( rc!=SQLITE_OK ){ |
| 38862 | + /* This branch is taken if an error occurs while trying to open |
| 38863 | + ** or roll back a hot-journal while holding an EXCLUSIVE lock. The |
| 38864 | + ** pager_unlock() routine will be called before returning to unlock |
| 38865 | + ** the file. If the unlock attempt fails, then Pager.eLock must be |
| 38866 | + ** set to UNKNOWN_LOCK (see the comment above the #define for |
| 38867 | + ** UNKNOWN_LOCK above for an explanation). |
| 38868 | + ** |
| 38869 | + ** In order to get pager_unlock() to do this, set Pager.eState to |
| 38870 | + ** PAGER_ERROR now. This is not actually counted as a transition |
| 38871 | + ** to ERROR state in the state diagram at the top of this file, |
| 38872 | + ** since we know that the same call to pager_unlock() will very |
| 38873 | + ** shortly transition the pager object to the OPEN state. Calling |
| 38874 | + ** assert_pager_state() would fail now, as it should not be possible |
| 38875 | + ** to be in ERROR state when there are zero outstanding page |
| 38876 | + ** references. |
| 38877 | + */ |
| 38878 | + pager_error(pPager, rc); |
| 38879 | + goto failed; |
| 38880 | + } |
| 38881 | + |
| 38882 | + assert( pPager->eState==PAGER_OPEN ); |
| 38883 | + assert( (pPager->eLock==SHARED_LOCK) |
| 38884 | + || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK) |
| 38231 | 38885 | ); |
| 38232 | 38886 | } |
| 38233 | 38887 | |
| 38234 | | - if( pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0 ){ |
| 38888 | + if( !pPager->tempFile |
| 38889 | + && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) |
| 38890 | + ){ |
| 38235 | 38891 | /* The shared-lock has just been acquired on the database file |
| 38236 | 38892 | ** and there are already pages in the cache (from a previous |
| 38237 | 38893 | ** read or write transaction). Check to see if the database |
| 38238 | 38894 | ** has been modified. If the database has changed, flush the |
| 38239 | 38895 | ** cache. |
| | @@ -38246,18 +38902,15 @@ |
| 38246 | 38902 | ** |
| 38247 | 38903 | ** There is a vanishingly small chance that a change will not be |
| 38248 | 38904 | ** detected. The chance of an undetected change is so small that |
| 38249 | 38905 | ** it can be neglected. |
| 38250 | 38906 | */ |
| 38251 | | - int nPage = 0; |
| 38907 | + Pgno nPage = 0; |
| 38252 | 38908 | char dbFileVers[sizeof(pPager->dbFileVers)]; |
| 38253 | | - sqlite3PagerPagecount(pPager, &nPage); |
| 38254 | 38909 | |
| 38255 | | - if( pPager->errCode ){ |
| 38256 | | - rc = pPager->errCode; |
| 38257 | | - goto failed; |
| 38258 | | - } |
| 38910 | + rc = pagerPagecount(pPager, &nPage); |
| 38911 | + if( rc ) goto failed; |
| 38259 | 38912 | |
| 38260 | 38913 | if( nPage>0 ){ |
| 38261 | 38914 | IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers))); |
| 38262 | 38915 | rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24); |
| 38263 | 38916 | if( rc!=SQLITE_OK ){ |
| | @@ -38269,22 +38922,34 @@ |
| 38269 | 38922 | |
| 38270 | 38923 | if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){ |
| 38271 | 38924 | pager_reset(pPager); |
| 38272 | 38925 | } |
| 38273 | 38926 | } |
| 38274 | | - assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED ); |
| 38275 | 38927 | |
| 38276 | 38928 | /* If there is a WAL file in the file-system, open this database in WAL |
| 38277 | 38929 | ** mode. Otherwise, the following function call is a no-op. |
| 38278 | 38930 | */ |
| 38279 | 38931 | rc = pagerOpenWalIfPresent(pPager); |
| 38932 | + assert( pPager->pWal==0 || rc==SQLITE_OK ); |
| 38933 | + } |
| 38934 | + |
| 38935 | + if( pagerUseWal(pPager) ){ |
| 38936 | + assert( rc==SQLITE_OK ); |
| 38937 | + rc = pagerBeginReadTransaction(pPager); |
| 38938 | + } |
| 38939 | + |
| 38940 | + if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){ |
| 38941 | + rc = pagerPagecount(pPager, &pPager->dbSize); |
| 38280 | 38942 | } |
| 38281 | 38943 | |
| 38282 | 38944 | failed: |
| 38283 | 38945 | if( rc!=SQLITE_OK ){ |
| 38284 | | - /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */ |
| 38946 | + assert( !MEMDB ); |
| 38285 | 38947 | pager_unlock(pPager); |
| 38948 | + assert( pPager->eState==PAGER_OPEN ); |
| 38949 | + }else{ |
| 38950 | + pPager->eState = PAGER_READER; |
| 38286 | 38951 | } |
| 38287 | 38952 | return rc; |
| 38288 | 38953 | } |
| 38289 | 38954 | |
| 38290 | 38955 | /* |
| | @@ -38294,13 +38959,11 @@ |
| 38294 | 38959 | ** Except, in locking_mode=EXCLUSIVE when there is nothing to in |
| 38295 | 38960 | ** the rollback journal, the unlock is not performed and there is |
| 38296 | 38961 | ** nothing to rollback, so this routine is a no-op. |
| 38297 | 38962 | */ |
| 38298 | 38963 | static void pagerUnlockIfUnused(Pager *pPager){ |
| 38299 | | - if( (sqlite3PcacheRefCount(pPager->pPCache)==0) |
| 38300 | | - && (!pPager->exclusiveMode || pPager->journalOff>0) |
| 38301 | | - ){ |
| 38964 | + if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){ |
| 38302 | 38965 | pagerUnlockAndRollback(pPager); |
| 38303 | 38966 | } |
| 38304 | 38967 | } |
| 38305 | 38968 | |
| 38306 | 38969 | /* |
| | @@ -38360,20 +39023,20 @@ |
| 38360 | 39023 | int noContent /* Do not bother reading content from disk if true */ |
| 38361 | 39024 | ){ |
| 38362 | 39025 | int rc; |
| 38363 | 39026 | PgHdr *pPg; |
| 38364 | 39027 | |
| 39028 | + assert( pPager->eState>=PAGER_READER ); |
| 38365 | 39029 | assert( assert_pager_state(pPager) ); |
| 38366 | | - assert( pPager->state>PAGER_UNLOCK ); |
| 38367 | 39030 | |
| 38368 | 39031 | if( pgno==0 ){ |
| 38369 | 39032 | return SQLITE_CORRUPT_BKPT; |
| 38370 | 39033 | } |
| 38371 | 39034 | |
| 38372 | 39035 | /* If the pager is in the error state, return an error immediately. |
| 38373 | 39036 | ** Otherwise, request the page from the PCache layer. */ |
| 38374 | | - if( pPager->errCode!=SQLITE_OK && pPager->errCode!=SQLITE_FULL ){ |
| 39037 | + if( pPager->errCode!=SQLITE_OK ){ |
| 38375 | 39038 | rc = pPager->errCode; |
| 38376 | 39039 | }else{ |
| 38377 | 39040 | rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage); |
| 38378 | 39041 | } |
| 38379 | 39042 | |
| | @@ -38395,11 +39058,10 @@ |
| 38395 | 39058 | return SQLITE_OK; |
| 38396 | 39059 | |
| 38397 | 39060 | }else{ |
| 38398 | 39061 | /* The pager cache has created a new page. Its content needs to |
| 38399 | 39062 | ** be initialized. */ |
| 38400 | | - int nMax; |
| 38401 | 39063 | |
| 38402 | 39064 | PAGER_INCR(pPager->nMiss); |
| 38403 | 39065 | pPg = *ppPage; |
| 38404 | 39066 | pPg->pPager = pPager; |
| 38405 | 39067 | |
| | @@ -38408,16 +39070,11 @@ |
| 38408 | 39070 | if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){ |
| 38409 | 39071 | rc = SQLITE_CORRUPT_BKPT; |
| 38410 | 39072 | goto pager_acquire_err; |
| 38411 | 39073 | } |
| 38412 | 39074 | |
| 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) ){ |
| 39075 | + if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){ |
| 38419 | 39076 | if( pgno>pPager->mxPgno ){ |
| 38420 | 39077 | rc = SQLITE_FULL; |
| 38421 | 39078 | goto pager_acquire_err; |
| 38422 | 39079 | } |
| 38423 | 39080 | if( noContent ){ |
| | @@ -38443,13 +39100,11 @@ |
| 38443 | 39100 | rc = readDbPage(pPg); |
| 38444 | 39101 | if( rc!=SQLITE_OK ){ |
| 38445 | 39102 | goto pager_acquire_err; |
| 38446 | 39103 | } |
| 38447 | 39104 | } |
| 38448 | | -#ifdef SQLITE_CHECK_PAGES |
| 38449 | | - pPg->pageHash = pager_pagehash(pPg); |
| 38450 | | -#endif |
| 39105 | + pager_set_pagehash(pPg); |
| 38451 | 39106 | } |
| 38452 | 39107 | |
| 38453 | 39108 | return SQLITE_OK; |
| 38454 | 39109 | |
| 38455 | 39110 | pager_acquire_err: |
| | @@ -38464,13 +39119,11 @@ |
| 38464 | 39119 | } |
| 38465 | 39120 | |
| 38466 | 39121 | /* |
| 38467 | 39122 | ** Acquire a page if it is already in the in-memory cache. Do |
| 38468 | 39123 | ** 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. |
| 39124 | +** or 0 if the page is not in cache. |
| 38472 | 39125 | ** |
| 38473 | 39126 | ** See also sqlite3PagerGet(). The difference between this routine |
| 38474 | 39127 | ** and sqlite3PagerGet() is that _get() will go to the disk and read |
| 38475 | 39128 | ** in the page if the page is not already in cache. This routine |
| 38476 | 39129 | ** returns NULL if the page is not in cache or if a disk I/O error |
| | @@ -38479,11 +39132,11 @@ |
| 38479 | 39132 | SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){ |
| 38480 | 39133 | PgHdr *pPg = 0; |
| 38481 | 39134 | assert( pPager!=0 ); |
| 38482 | 39135 | assert( pgno!=0 ); |
| 38483 | 39136 | assert( pPager->pPCache!=0 ); |
| 38484 | | - assert( pPager->state > PAGER_UNLOCK ); |
| 39137 | + assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR ); |
| 38485 | 39138 | sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg); |
| 38486 | 39139 | return pPg; |
| 38487 | 39140 | } |
| 38488 | 39141 | |
| 38489 | 39142 | /* |
| | @@ -38524,73 +39177,71 @@ |
| 38524 | 39177 | ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or |
| 38525 | 39178 | ** an IO error code if opening or writing the journal file fails. |
| 38526 | 39179 | */ |
| 38527 | 39180 | static int pager_open_journal(Pager *pPager){ |
| 38528 | 39181 | int rc = SQLITE_OK; /* Return code */ |
| 38529 | | - int nPage; /* Size of database file */ |
| 38530 | 39182 | sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */ |
| 38531 | 39183 | |
| 38532 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 38533 | | - assert( pPager->useJournal ); |
| 38534 | | - assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF ); |
| 39184 | + assert( pPager->eState==PAGER_WRITER_LOCKED ); |
| 39185 | + assert( assert_pager_state(pPager) ); |
| 38535 | 39186 | assert( pPager->pInJournal==0 ); |
| 38536 | 39187 | |
| 38537 | 39188 | /* If already in the error state, this function is a no-op. But on |
| 38538 | 39189 | ** the other hand, this routine is never called if we are already in |
| 38539 | 39190 | ** an error state. */ |
| 38540 | 39191 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38541 | 39192 | |
| 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); |
| 39193 | + if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){ |
| 39194 | + pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); |
| 39195 | + if( pPager->pInJournal==0 ){ |
| 39196 | + return SQLITE_NOMEM; |
| 39197 | + } |
| 39198 | + |
| 39199 | + /* Open the journal file if it is not already open. */ |
| 39200 | + if( !isOpen(pPager->jfd) ){ |
| 39201 | + if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){ |
| 39202 | + sqlite3MemJournalOpen(pPager->jfd); |
| 39203 | + }else{ |
| 39204 | + const int flags = /* VFS flags to open journal file */ |
| 39205 | + SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
| 39206 | + (pPager->tempFile ? |
| 39207 | + (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL): |
| 39208 | + (SQLITE_OPEN_MAIN_JOURNAL) |
| 39209 | + ); |
| 39210 | + #ifdef SQLITE_ENABLE_ATOMIC_WRITE |
| 39211 | + rc = sqlite3JournalOpen( |
| 39212 | + pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager) |
| 39213 | + ); |
| 39214 | + #else |
| 39215 | + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0); |
| 39216 | + #endif |
| 39217 | + } |
| 39218 | + assert( rc!=SQLITE_OK || isOpen(pPager->jfd) ); |
| 39219 | + } |
| 39220 | + |
| 39221 | + |
| 39222 | + /* Write the first journal header to the journal file and open |
| 39223 | + ** the sub-journal if necessary. |
| 39224 | + */ |
| 39225 | + if( rc==SQLITE_OK ){ |
| 39226 | + /* TODO: Check if all of these are really required. */ |
| 39227 | + pPager->nRec = 0; |
| 39228 | + pPager->journalOff = 0; |
| 39229 | + pPager->setMaster = 0; |
| 39230 | + pPager->journalHdr = 0; |
| 39231 | + rc = writeJournalHdr(pPager); |
| 39232 | + } |
| 38586 | 39233 | } |
| 38587 | 39234 | |
| 38588 | 39235 | if( rc!=SQLITE_OK ){ |
| 38589 | 39236 | sqlite3BitvecDestroy(pPager->pInJournal); |
| 38590 | 39237 | pPager->pInJournal = 0; |
| 39238 | + }else{ |
| 39239 | + assert( pPager->eState==PAGER_WRITER_LOCKED ); |
| 39240 | + pPager->eState = PAGER_WRITER_CACHEMOD; |
| 38591 | 39241 | } |
| 39242 | + |
| 38592 | 39243 | return rc; |
| 38593 | 39244 | } |
| 38594 | 39245 | |
| 38595 | 39246 | /* |
| 38596 | 39247 | ** Begin a write-transaction on the specified pager object. If a |
| | @@ -38599,18 +39250,10 @@ |
| 38599 | 39250 | ** If the exFlag argument is false, then acquire at least a RESERVED |
| 38600 | 39251 | ** lock on the database file. If exFlag is true, then acquire at least |
| 38601 | 39252 | ** an EXCLUSIVE lock. If such a lock is already held, no locking |
| 38602 | 39253 | ** functions need be called. |
| 38603 | 39254 | ** |
| 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 | 39255 | ** If the subjInMemory argument is non-zero, then any sub-journal opened |
| 38613 | 39256 | ** within this transaction will be opened as an in-memory file. This |
| 38614 | 39257 | ** has no effect if the sub-journal is already opened (as it may be when |
| 38615 | 39258 | ** running in exclusive mode) or if the transaction does not require a |
| 38616 | 39259 | ** sub-journal. If the subjInMemory argument is zero, then any required |
| | @@ -38617,24 +39260,24 @@ |
| 38617 | 39260 | ** sub-journal is implemented in-memory if pPager is an in-memory database, |
| 38618 | 39261 | ** or using a temporary file otherwise. |
| 38619 | 39262 | */ |
| 38620 | 39263 | SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){ |
| 38621 | 39264 | int rc = SQLITE_OK; |
| 38622 | | - assert( pPager->state!=PAGER_UNLOCK ); |
| 39265 | + |
| 39266 | + if( pPager->errCode ) return pPager->errCode; |
| 39267 | + assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR ); |
| 38623 | 39268 | pPager->subjInMemory = (u8)subjInMemory; |
| 38624 | 39269 | |
| 38625 | | - if( pPager->state==PAGER_SHARED ){ |
| 39270 | + if( ALWAYS(pPager->eState==PAGER_READER) ){ |
| 38626 | 39271 | assert( pPager->pInJournal==0 ); |
| 38627 | | - assert( !MEMDB && !pPager->tempFile ); |
| 38628 | 39272 | |
| 38629 | 39273 | if( pagerUseWal(pPager) ){ |
| 38630 | 39274 | /* If the pager is configured to use locking_mode=exclusive, and an |
| 38631 | 39275 | ** exclusive lock on the database is not already held, obtain it now. |
| 38632 | 39276 | */ |
| 38633 | 39277 | if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){ |
| 38634 | | - rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK); |
| 38635 | | - pPager->state = PAGER_SHARED; |
| 39278 | + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); |
| 38636 | 39279 | if( rc!=SQLITE_OK ){ |
| 38637 | 39280 | return rc; |
| 38638 | 39281 | } |
| 38639 | 39282 | sqlite3WalExclusiveMode(pPager->pWal, 1); |
| 38640 | 39283 | } |
| | @@ -38641,56 +39284,44 @@ |
| 38641 | 39284 | |
| 38642 | 39285 | /* Grab the write lock on the log file. If successful, upgrade to |
| 38643 | 39286 | ** PAGER_RESERVED state. Otherwise, return an error code to the caller. |
| 38644 | 39287 | ** The busy-handler is not invoked if another connection already |
| 38645 | 39288 | ** 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 | 39289 | */ |
| 38653 | 39290 | 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 | 39291 | }else{ |
| 38663 | 39292 | /* Obtain a RESERVED lock on the database file. If the exFlag parameter |
| 38664 | 39293 | ** is true, then immediately upgrade this to an EXCLUSIVE lock. The |
| 38665 | 39294 | ** busy-handler callback can be used when upgrading to the EXCLUSIVE |
| 38666 | 39295 | ** lock, but not when obtaining the RESERVED lock. |
| 38667 | 39296 | */ |
| 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 | | - } |
| 39297 | + rc = pagerLockDb(pPager, RESERVED_LOCK); |
| 39298 | + if( rc==SQLITE_OK && exFlag ){ |
| 39299 | + rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 39300 | + } |
| 39301 | + } |
| 39302 | + |
| 39303 | + if( rc==SQLITE_OK ){ |
| 39304 | + /* Change to WRITER_LOCKED state. |
| 39305 | + ** |
| 39306 | + ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD |
| 39307 | + ** when it has an open transaction, but never to DBMOD or FINISHED. |
| 39308 | + ** This is because in those states the code to roll back savepoint |
| 39309 | + ** transactions may copy data from the sub-journal into the database |
| 39310 | + ** file as well as into the page cache. Which would be incorrect in |
| 39311 | + ** WAL mode. |
| 39312 | + */ |
| 39313 | + pPager->eState = PAGER_WRITER_LOCKED; |
| 39314 | + pPager->dbHintSize = pPager->dbSize; |
| 39315 | + pPager->dbFileSize = pPager->dbSize; |
| 39316 | + pPager->dbOrigSize = pPager->dbSize; |
| 39317 | + pPager->journalOff = 0; |
| 39318 | + } |
| 39319 | + |
| 39320 | + assert( rc==SQLITE_OK || pPager->eState==PAGER_READER ); |
| 39321 | + assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED ); |
| 39322 | + assert( assert_pager_state(pPager) ); |
| 38692 | 39323 | } |
| 38693 | 39324 | |
| 38694 | 39325 | PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager))); |
| 38695 | 39326 | return rc; |
| 38696 | 39327 | } |
| | @@ -38705,110 +39336,98 @@ |
| 38705 | 39336 | static int pager_write(PgHdr *pPg){ |
| 38706 | 39337 | void *pData = pPg->pData; |
| 38707 | 39338 | Pager *pPager = pPg->pPager; |
| 38708 | 39339 | int rc = SQLITE_OK; |
| 38709 | 39340 | |
| 38710 | | - /* This routine is not called unless a transaction has already been |
| 38711 | | - ** started. |
| 39341 | + /* This routine is not called unless a write-transaction has already |
| 39342 | + ** been started. The journal file may or may not be open at this point. |
| 39343 | + ** It is never called in the ERROR state. |
| 38712 | 39344 | */ |
| 38713 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 39345 | + assert( pPager->eState==PAGER_WRITER_LOCKED |
| 39346 | + || pPager->eState==PAGER_WRITER_CACHEMOD |
| 39347 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 39348 | + ); |
| 39349 | + assert( assert_pager_state(pPager) ); |
| 38714 | 39350 | |
| 38715 | 39351 | /* If an error has been previously detected, report the same error |
| 38716 | | - ** again. |
| 38717 | | - */ |
| 39352 | + ** again. This should not happen, but the check provides robustness. */ |
| 38718 | 39353 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 38719 | 39354 | |
| 38720 | 39355 | /* Higher-level routines never call this function if database is not |
| 38721 | 39356 | ** writable. But check anyway, just for robustness. */ |
| 38722 | 39357 | if( NEVER(pPager->readOnly) ) return SQLITE_PERM; |
| 38723 | 39358 | |
| 38724 | | - assert( !pPager->setMaster ); |
| 38725 | | - |
| 38726 | 39359 | CHECK_PAGE(pPg); |
| 38727 | 39360 | |
| 38728 | 39361 | /* Mark the page as dirty. If the page has already been written |
| 38729 | 39362 | ** to the journal then we can return right away. |
| 38730 | 39363 | */ |
| 38731 | 39364 | sqlite3PcacheMakeDirty(pPg); |
| 38732 | 39365 | if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){ |
| 38733 | 39366 | assert( !pagerUseWal(pPager) ); |
| 38734 | | - pPager->dbModified = 1; |
| 39367 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 38735 | 39368 | }else{ |
| 38736 | 39369 | |
| 38737 | 39370 | /* If we get this far, it means that the page needs to be |
| 38738 | | - ** written to the transaction journal or the ckeckpoint journal |
| 39371 | + ** written to the transaction journal or the checkpoint journal |
| 38739 | 39372 | ** or both. |
| 38740 | 39373 | ** |
| 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. |
| 39374 | + ** Higher level routines have already obtained the necessary locks |
| 39375 | + ** to begin the write-transaction, but the rollback journal might not |
| 39376 | + ** yet be open. Open it now if this is the case. |
| 38744 | 39377 | */ |
| 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 ); |
| 39378 | + if( pPager->eState==PAGER_WRITER_LOCKED ){ |
| 38751 | 39379 | rc = pager_open_journal(pPager); |
| 38752 | 39380 | if( rc!=SQLITE_OK ) return rc; |
| 38753 | 39381 | } |
| 38754 | | - pPager->dbModified = 1; |
| 39382 | + assert( pPager->eState>=PAGER_WRITER_CACHEMOD ); |
| 39383 | + assert( assert_pager_state(pPager) ); |
| 38755 | 39384 | |
| 38756 | 39385 | /* The transaction journal now exists and we have a RESERVED or an |
| 38757 | 39386 | ** EXCLUSIVE lock on the main database file. Write the current page to |
| 38758 | 39387 | ** the transaction journal if it is not there already. |
| 38759 | 39388 | */ |
| 38760 | | - if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){ |
| 38761 | | - assert( !pagerUseWal(pPager) ); |
| 38762 | | - if( pPg->pgno<=pPager->dbOrigSize ){ |
| 39389 | + if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){ |
| 39390 | + assert( pagerUseWal(pPager)==0 ); |
| 39391 | + if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){ |
| 38763 | 39392 | u32 cksum; |
| 38764 | 39393 | char *pData2; |
| 39394 | + i64 iOff = pPager->journalOff; |
| 38765 | 39395 | |
| 38766 | 39396 | /* We should never write to the journal file the page that |
| 38767 | 39397 | ** contains the database locks. The following assert verifies |
| 38768 | 39398 | ** that we do not. */ |
| 38769 | 39399 | assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) ); |
| 38770 | 39400 | |
| 38771 | | - assert( pPager->journalHdr <= pPager->journalOff ); |
| 39401 | + assert( pPager->journalHdr<=pPager->journalOff ); |
| 38772 | 39402 | CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2); |
| 38773 | 39403 | 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 | | - } |
| 39404 | + |
| 39405 | + /* Even if an IO or diskfull error occurs while journalling the |
| 39406 | + ** page in the block above, set the need-sync flag for the page. |
| 39407 | + ** Otherwise, when the transaction is rolled back, the logic in |
| 39408 | + ** playback_one_page() will think that the page needs to be restored |
| 39409 | + ** in the database file. And if an IO error occurs while doing so, |
| 39410 | + ** then corruption may follow. |
| 39411 | + */ |
| 39412 | + pPg->flags |= PGHDR_NEED_SYNC; |
| 39413 | + |
| 39414 | + rc = write32bits(pPager->jfd, iOff, pPg->pgno); |
| 39415 | + if( rc!=SQLITE_OK ) return rc; |
| 39416 | + rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4); |
| 39417 | + if( rc!=SQLITE_OK ) return rc; |
| 39418 | + rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum); |
| 39419 | + if( rc!=SQLITE_OK ) return rc; |
| 39420 | + |
| 38784 | 39421 | IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, |
| 38785 | 39422 | pPager->journalOff, pPager->pageSize)); |
| 38786 | 39423 | PAGER_INCR(sqlite3_pager_writej_count); |
| 38787 | 39424 | PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n", |
| 38788 | 39425 | PAGERID(pPager), pPg->pgno, |
| 38789 | 39426 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg))); |
| 38790 | 39427 | |
| 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 | | - |
| 39428 | + pPager->journalOff += 8 + pPager->pageSize; |
| 38810 | 39429 | pPager->nRec++; |
| 38811 | 39430 | assert( pPager->pInJournal!=0 ); |
| 38812 | 39431 | rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); |
| 38813 | 39432 | testcase( rc==SQLITE_NOMEM ); |
| 38814 | 39433 | assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); |
| | @@ -38816,13 +39435,12 @@ |
| 38816 | 39435 | if( rc!=SQLITE_OK ){ |
| 38817 | 39436 | assert( rc==SQLITE_NOMEM ); |
| 38818 | 39437 | return rc; |
| 38819 | 39438 | } |
| 38820 | 39439 | }else{ |
| 38821 | | - if( !pPager->journalStarted && !pPager->noSync ){ |
| 39440 | + if( pPager->eState!=PAGER_WRITER_DBMOD ){ |
| 38822 | 39441 | pPg->flags |= PGHDR_NEED_SYNC; |
| 38823 | | - pPager->needSync = 1; |
| 38824 | 39442 | } |
| 38825 | 39443 | PAGERTRACE(("APPEND %d page %d needSync=%d\n", |
| 38826 | 39444 | PAGERID(pPager), pPg->pgno, |
| 38827 | 39445 | ((pPg->flags&PGHDR_NEED_SYNC)?1:0))); |
| 38828 | 39446 | } |
| | @@ -38838,11 +39456,10 @@ |
| 38838 | 39456 | } |
| 38839 | 39457 | } |
| 38840 | 39458 | |
| 38841 | 39459 | /* Update the database size and return. |
| 38842 | 39460 | */ |
| 38843 | | - assert( pPager->state>=PAGER_SHARED ); |
| 38844 | 39461 | if( pPager->dbSize<pPg->pgno ){ |
| 38845 | 39462 | pPager->dbSize = pPg->pgno; |
| 38846 | 39463 | } |
| 38847 | 39464 | return rc; |
| 38848 | 39465 | } |
| | @@ -38866,10 +39483,14 @@ |
| 38866 | 39483 | |
| 38867 | 39484 | PgHdr *pPg = pDbPage; |
| 38868 | 39485 | Pager *pPager = pPg->pPager; |
| 38869 | 39486 | Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize); |
| 38870 | 39487 | |
| 39488 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 39489 | + assert( pPager->eState!=PAGER_ERROR ); |
| 39490 | + assert( assert_pager_state(pPager) ); |
| 39491 | + |
| 38871 | 39492 | if( nPagePerSector>1 ){ |
| 38872 | 39493 | Pgno nPageCount; /* Total number of pages in database file */ |
| 38873 | 39494 | Pgno pg1; /* First page of the sector pPg is located on. */ |
| 38874 | 39495 | int nPage = 0; /* Number of pages starting at pg1 to journal */ |
| 38875 | 39496 | int ii; /* Loop counter */ |
| | @@ -38887,23 +39508,21 @@ |
| 38887 | 39508 | ** an integer power of 2. It sets variable pg1 to the identifier |
| 38888 | 39509 | ** of the first page of the sector pPg is located on. |
| 38889 | 39510 | */ |
| 38890 | 39511 | pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1; |
| 38891 | 39512 | |
| 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 | | - } |
| 39513 | + nPageCount = pPager->dbSize; |
| 39514 | + if( pPg->pgno>nPageCount ){ |
| 39515 | + nPage = (pPg->pgno - pg1)+1; |
| 39516 | + }else if( (pg1+nPagePerSector-1)>nPageCount ){ |
| 39517 | + nPage = nPageCount+1-pg1; |
| 39518 | + }else{ |
| 39519 | + nPage = nPagePerSector; |
| 39520 | + } |
| 39521 | + assert(nPage>0); |
| 39522 | + assert(pg1<=pPg->pgno); |
| 39523 | + assert((pg1+nPage)>pPg->pgno); |
| 38905 | 39524 | |
| 38906 | 39525 | for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){ |
| 38907 | 39526 | Pgno pg = pg1+ii; |
| 38908 | 39527 | PgHdr *pPage; |
| 38909 | 39528 | if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ |
| | @@ -38911,11 +39530,10 @@ |
| 38911 | 39530 | rc = sqlite3PagerGet(pPager, pg, &pPage); |
| 38912 | 39531 | if( rc==SQLITE_OK ){ |
| 38913 | 39532 | rc = pager_write(pPage); |
| 38914 | 39533 | if( pPage->flags&PGHDR_NEED_SYNC ){ |
| 38915 | 39534 | needSync = 1; |
| 38916 | | - assert(pPager->needSync); |
| 38917 | 39535 | } |
| 38918 | 39536 | sqlite3PagerUnref(pPage); |
| 38919 | 39537 | } |
| 38920 | 39538 | } |
| 38921 | 39539 | }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ |
| | @@ -38931,19 +39549,18 @@ |
| 38931 | 39549 | ** writing to any of these nPage pages may damage the others, the |
| 38932 | 39550 | ** journal file must contain sync()ed copies of all of them |
| 38933 | 39551 | ** before any of them can be written out to the database file. |
| 38934 | 39552 | */ |
| 38935 | 39553 | if( rc==SQLITE_OK && needSync ){ |
| 38936 | | - assert( !MEMDB && pPager->noSync==0 ); |
| 39554 | + assert( !MEMDB ); |
| 38937 | 39555 | for(ii=0; ii<nPage; ii++){ |
| 38938 | 39556 | PgHdr *pPage = pager_lookup(pPager, pg1+ii); |
| 38939 | 39557 | if( pPage ){ |
| 38940 | 39558 | pPage->flags |= PGHDR_NEED_SYNC; |
| 38941 | 39559 | sqlite3PagerUnref(pPage); |
| 38942 | 39560 | } |
| 38943 | 39561 | } |
| 38944 | | - assert(pPager->needSync); |
| 38945 | 39562 | } |
| 38946 | 39563 | |
| 38947 | 39564 | assert( pPager->doNotSyncSpill==1 ); |
| 38948 | 39565 | pPager->doNotSyncSpill--; |
| 38949 | 39566 | }else{ |
| | @@ -38981,13 +39598,11 @@ |
| 38981 | 39598 | Pager *pPager = pPg->pPager; |
| 38982 | 39599 | if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){ |
| 38983 | 39600 | PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager))); |
| 38984 | 39601 | IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno)) |
| 38985 | 39602 | pPg->flags |= PGHDR_DONT_WRITE; |
| 38986 | | -#ifdef SQLITE_CHECK_PAGES |
| 38987 | | - pPg->pageHash = pager_pagehash(pPg); |
| 38988 | | -#endif |
| 39603 | + pager_set_pagehash(pPg); |
| 38989 | 39604 | } |
| 38990 | 39605 | } |
| 38991 | 39606 | |
| 38992 | 39607 | /* |
| 38993 | 39608 | ** This routine is called to increment the value of the database file |
| | @@ -39005,10 +39620,15 @@ |
| 39005 | 39620 | ** by writing an updated version of page 1 using a call to the |
| 39006 | 39621 | ** sqlite3OsWrite() function. |
| 39007 | 39622 | */ |
| 39008 | 39623 | static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ |
| 39009 | 39624 | int rc = SQLITE_OK; |
| 39625 | + |
| 39626 | + assert( pPager->eState==PAGER_WRITER_CACHEMOD |
| 39627 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 39628 | + ); |
| 39629 | + assert( assert_pager_state(pPager) ); |
| 39010 | 39630 | |
| 39011 | 39631 | /* Declare and initialize constant integer 'isDirect'. If the |
| 39012 | 39632 | ** atomic-write optimization is enabled in this build, then isDirect |
| 39013 | 39633 | ** is initialized to the value passed as the isDirectMode parameter |
| 39014 | 39634 | ** to this function. Otherwise, it is always set to zero. |
| | @@ -39024,11 +39644,10 @@ |
| 39024 | 39644 | UNUSED_PARAMETER(isDirectMode); |
| 39025 | 39645 | #else |
| 39026 | 39646 | # define DIRECT_MODE isDirectMode |
| 39027 | 39647 | #endif |
| 39028 | 39648 | |
| 39029 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 39030 | 39649 | if( !pPager->changeCountDone && pPager->dbSize>0 ){ |
| 39031 | 39650 | PgHdr *pPgHdr; /* Reference to page 1 */ |
| 39032 | 39651 | u32 change_counter; /* Initial value of change-counter field */ |
| 39033 | 39652 | |
| 39034 | 39653 | assert( !pPager->tempFile && isOpen(pPager->fd) ); |
| | @@ -39109,13 +39728,17 @@ |
| 39109 | 39728 | ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is |
| 39110 | 39729 | ** returned. |
| 39111 | 39730 | */ |
| 39112 | 39731 | SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){ |
| 39113 | 39732 | int rc = SQLITE_OK; |
| 39114 | | - assert( pPager->state>=PAGER_RESERVED ); |
| 39733 | + assert( pPager->eState==PAGER_WRITER_CACHEMOD |
| 39734 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 39735 | + || pPager->eState==PAGER_WRITER_LOCKED |
| 39736 | + ); |
| 39737 | + assert( assert_pager_state(pPager) ); |
| 39115 | 39738 | if( 0==pagerUseWal(pPager) ){ |
| 39116 | | - rc = pager_wait_on_lock(pPager, PAGER_EXCLUSIVE); |
| 39739 | + rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK); |
| 39117 | 39740 | } |
| 39118 | 39741 | return rc; |
| 39119 | 39742 | } |
| 39120 | 39743 | |
| 39121 | 39744 | /* |
| | @@ -39149,26 +39772,33 @@ |
| 39149 | 39772 | const char *zMaster, /* If not NULL, the master journal name */ |
| 39150 | 39773 | int noSync /* True to omit the xSync on the db file */ |
| 39151 | 39774 | ){ |
| 39152 | 39775 | int rc = SQLITE_OK; /* Return code */ |
| 39153 | 39776 | |
| 39154 | | - /* The dbOrigSize is never set if journal_mode=OFF */ |
| 39155 | | - assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 ); |
| 39777 | + assert( pPager->eState==PAGER_WRITER_LOCKED |
| 39778 | + || pPager->eState==PAGER_WRITER_CACHEMOD |
| 39779 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 39780 | + || pPager->eState==PAGER_ERROR |
| 39781 | + ); |
| 39782 | + assert( assert_pager_state(pPager) ); |
| 39156 | 39783 | |
| 39157 | 39784 | /* If a prior error occurred, report that error again. */ |
| 39158 | | - if( pPager->errCode ) return pPager->errCode; |
| 39785 | + if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 39159 | 39786 | |
| 39160 | 39787 | PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", |
| 39161 | 39788 | pPager->zFilename, zMaster, pPager->dbSize)); |
| 39162 | 39789 | |
| 39163 | | - if( MEMDB && pPager->dbModified ){ |
| 39790 | + /* If no database changes have been made, return early. */ |
| 39791 | + if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK; |
| 39792 | + |
| 39793 | + if( MEMDB ){ |
| 39164 | 39794 | /* If this is an in-memory db, or no pages have been written to, or this |
| 39165 | 39795 | ** function has already been called, it is mostly a no-op. However, any |
| 39166 | 39796 | ** backup in progress needs to be restarted. |
| 39167 | 39797 | */ |
| 39168 | 39798 | sqlite3BackupRestart(pPager->pBackup); |
| 39169 | | - }else if( pPager->dbModified ){ |
| 39799 | + }else{ |
| 39170 | 39800 | if( pagerUseWal(pPager) ){ |
| 39171 | 39801 | PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache); |
| 39172 | 39802 | if( pList ){ |
| 39173 | 39803 | rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1, |
| 39174 | 39804 | (pPager->fullSync ? pPager->sync_flags : 0) |
| | @@ -39207,11 +39837,11 @@ |
| 39207 | 39837 | || pPager->journalMode==PAGER_JOURNALMODE_OFF |
| 39208 | 39838 | || pPager->journalMode==PAGER_JOURNALMODE_WAL |
| 39209 | 39839 | ); |
| 39210 | 39840 | if( !zMaster && isOpen(pPager->jfd) |
| 39211 | 39841 | && pPager->journalOff==jrnlBufferSize(pPager) |
| 39212 | | - && pPager->dbSize>=pPager->dbFileSize |
| 39842 | + && pPager->dbSize>=pPager->dbOrigSize |
| 39213 | 39843 | && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty) |
| 39214 | 39844 | ){ |
| 39215 | 39845 | /* Update the db file change counter via the direct-write method. The |
| 39216 | 39846 | ** following call will modify the in-memory representation of page 1 |
| 39217 | 39847 | ** to include the updated change counter and then write page 1 |
| | @@ -39237,17 +39867,14 @@ |
| 39237 | 39867 | ** Before reading the pages with page numbers larger than the |
| 39238 | 39868 | ** current value of Pager.dbSize, set dbSize back to the value |
| 39239 | 39869 | ** that it took at the start of the transaction. Otherwise, the |
| 39240 | 39870 | ** calls to sqlite3PagerGet() return zeroed pages instead of |
| 39241 | 39871 | ** 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 | 39872 | */ |
| 39246 | 39873 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 39247 | 39874 | if( pPager->dbSize<pPager->dbOrigSize |
| 39248 | | - && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF) |
| 39875 | + && pPager->journalMode!=PAGER_JOURNALMODE_OFF |
| 39249 | 39876 | ){ |
| 39250 | 39877 | Pgno i; /* Iterator variable */ |
| 39251 | 39878 | const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */ |
| 39252 | 39879 | const Pgno dbSize = pPager->dbSize; /* Database image size */ |
| 39253 | 39880 | pPager->dbSize = pPager->dbOrigSize; |
| | @@ -39270,18 +39897,24 @@ |
| 39270 | 39897 | ** or if zMaster is NULL (no master journal), then this call is a no-op. |
| 39271 | 39898 | */ |
| 39272 | 39899 | rc = writeMasterJournal(pPager, zMaster); |
| 39273 | 39900 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39274 | 39901 | |
| 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. |
| 39902 | + /* Sync the journal file and write all dirty pages to the database. |
| 39903 | + ** If the atomic-update optimization is being used, this sync will not |
| 39904 | + ** create the journal file or perform any real IO. |
| 39905 | + ** |
| 39906 | + ** Because the change-counter page was just modified, unless the |
| 39907 | + ** atomic-update optimization is used it is almost certain that the |
| 39908 | + ** journal requires a sync here. However, in locking_mode=exclusive |
| 39909 | + ** on a system under memory pressure it is just possible that this is |
| 39910 | + ** not the case. In this case it is likely enough that the redundant |
| 39911 | + ** xSync() call will be changed to a no-op by the OS anyhow. |
| 39278 | 39912 | */ |
| 39279 | | - rc = syncJournal(pPager); |
| 39913 | + rc = syncJournal(pPager, 0); |
| 39280 | 39914 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39281 | 39915 | |
| 39282 | | - /* Write all dirty pages to the database file. */ |
| 39283 | 39916 | rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache)); |
| 39284 | 39917 | if( rc!=SQLITE_OK ){ |
| 39285 | 39918 | assert( rc!=SQLITE_IOERR_BLOCKED ); |
| 39286 | 39919 | goto commit_phase_one_exit; |
| 39287 | 39920 | } |
| | @@ -39290,11 +39923,11 @@ |
| 39290 | 39923 | /* If the file on disk is not the same size as the database image, |
| 39291 | 39924 | ** then use pager_truncate to grow or shrink the file here. |
| 39292 | 39925 | */ |
| 39293 | 39926 | if( pPager->dbSize!=pPager->dbFileSize ){ |
| 39294 | 39927 | Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)); |
| 39295 | | - assert( pPager->state>=PAGER_EXCLUSIVE ); |
| 39928 | + assert( pPager->eState==PAGER_WRITER_DBMOD ); |
| 39296 | 39929 | rc = pager_truncate(pPager, nNew); |
| 39297 | 39930 | if( rc!=SQLITE_OK ) goto commit_phase_one_exit; |
| 39298 | 39931 | } |
| 39299 | 39932 | |
| 39300 | 39933 | /* Finally, sync the database file. */ |
| | @@ -39301,16 +39934,16 @@ |
| 39301 | 39934 | if( !pPager->noSync && !noSync ){ |
| 39302 | 39935 | rc = sqlite3OsSync(pPager->fd, pPager->sync_flags); |
| 39303 | 39936 | } |
| 39304 | 39937 | IOTRACE(("DBSYNC %p\n", pPager)) |
| 39305 | 39938 | } |
| 39306 | | - |
| 39307 | | - assert( pPager->state!=PAGER_SYNCED ); |
| 39308 | | - pPager->state = PAGER_SYNCED; |
| 39309 | 39939 | } |
| 39310 | 39940 | |
| 39311 | 39941 | commit_phase_one_exit: |
| 39942 | + if( rc==SQLITE_OK && !pagerUseWal(pPager) ){ |
| 39943 | + pPager->eState = PAGER_WRITER_FINISHED; |
| 39944 | + } |
| 39312 | 39945 | return rc; |
| 39313 | 39946 | } |
| 39314 | 39947 | |
| 39315 | 39948 | |
| 39316 | 39949 | /* |
| | @@ -39334,16 +39967,15 @@ |
| 39334 | 39967 | /* This routine should not be called if a prior error has occurred. |
| 39335 | 39968 | ** But if (due to a coding error elsewhere in the system) it does get |
| 39336 | 39969 | ** called, just return the same error code without doing anything. */ |
| 39337 | 39970 | if( NEVER(pPager->errCode) ) return pPager->errCode; |
| 39338 | 39971 | |
| 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; |
| 39972 | + assert( pPager->eState==PAGER_WRITER_LOCKED |
| 39973 | + || pPager->eState==PAGER_WRITER_FINISHED |
| 39974 | + || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD) |
| 39975 | + ); |
| 39976 | + assert( assert_pager_state(pPager) ); |
| 39345 | 39977 | |
| 39346 | 39978 | /* An optimization. If the database was not actually modified during |
| 39347 | 39979 | ** this transaction, the pager is running in exclusive-mode and is |
| 39348 | 39980 | ** using persistent journals, then this function is a no-op. |
| 39349 | 39981 | ** |
| | @@ -39352,106 +39984,80 @@ |
| 39352 | 39984 | ** a hot-journal during hot-journal rollback, 0 changes will be made |
| 39353 | 39985 | ** to the database file. So there is no need to zero the journal |
| 39354 | 39986 | ** header. Since the pager is in exclusive mode, there is no need |
| 39355 | 39987 | ** to drop any locks either. |
| 39356 | 39988 | */ |
| 39357 | | - if( pPager->dbModified==0 && pPager->exclusiveMode |
| 39989 | + if( pPager->eState==PAGER_WRITER_LOCKED |
| 39990 | + && pPager->exclusiveMode |
| 39358 | 39991 | && pPager->journalMode==PAGER_JOURNALMODE_PERSIST |
| 39359 | 39992 | ){ |
| 39360 | 39993 | assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff ); |
| 39994 | + pPager->eState = PAGER_READER; |
| 39361 | 39995 | return SQLITE_OK; |
| 39362 | 39996 | } |
| 39363 | 39997 | |
| 39364 | 39998 | PAGERTRACE(("COMMIT %d\n", PAGERID(pPager))); |
| 39365 | | - assert( pPager->state==PAGER_SYNCED || MEMDB || !pPager->dbModified ); |
| 39366 | 39999 | rc = pager_end_transaction(pPager, pPager->setMaster); |
| 39367 | 40000 | return pager_error(pPager, rc); |
| 39368 | 40001 | } |
| 39369 | 40002 | |
| 39370 | 40003 | /* |
| 39371 | | -** Rollback all changes. The database falls back to PAGER_SHARED mode. |
| 40004 | +** If a write transaction is open, then all changes made within the |
| 40005 | +** transaction are reverted and the current write-transaction is closed. |
| 40006 | +** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR |
| 40007 | +** state if an error occurs. |
| 39372 | 40008 | ** |
| 39373 | | -** This function performs two tasks: |
| 40009 | +** If the pager is already in PAGER_ERROR state when this function is called, |
| 40010 | +** it returns Pager.errCode immediately. No work is performed in this case. |
| 40011 | +** |
| 40012 | +** Otherwise, in rollback mode, this function performs two functions: |
| 39374 | 40013 | ** |
| 39375 | 40014 | ** 1) It rolls back the journal file, restoring all database file and |
| 39376 | 40015 | ** in-memory cache pages to the state they were in when the transaction |
| 39377 | 40016 | ** was opened, and |
| 40017 | +** |
| 39378 | 40018 | ** 2) It finalizes the journal file, so that it is not used for hot |
| 39379 | 40019 | ** rollback at any point in the future. |
| 39380 | 40020 | ** |
| 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). |
| 40021 | +** Finalization of the journal file (task 2) is only performed if the |
| 40022 | +** rollback is successful. |
| 40023 | +** |
| 40024 | +** In WAL mode, all cache-entries containing data modified within the |
| 40025 | +** current transaction are either expelled from the cache or reverted to |
| 40026 | +** their pre-transaction state by re-reading data from the database or |
| 40027 | +** WAL files. The WAL transaction is then closed. |
| 39412 | 40028 | */ |
| 39413 | 40029 | SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){ |
| 39414 | 40030 | int rc = SQLITE_OK; /* Return code */ |
| 39415 | 40031 | PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager))); |
| 40032 | + |
| 40033 | + /* PagerRollback() is a no-op if called in READER or OPEN state. If |
| 40034 | + ** the pager is already in the ERROR state, the rollback is not |
| 40035 | + ** attempted here. Instead, the error code is returned to the caller. |
| 40036 | + */ |
| 40037 | + assert( assert_pager_state(pPager) ); |
| 40038 | + if( pPager->eState==PAGER_ERROR ) return pPager->errCode; |
| 40039 | + if( pPager->eState<=PAGER_READER ) return SQLITE_OK; |
| 40040 | + |
| 39416 | 40041 | if( pagerUseWal(pPager) ){ |
| 39417 | 40042 | int rc2; |
| 39418 | | - |
| 39419 | 40043 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1); |
| 39420 | 40044 | rc2 = pager_end_transaction(pPager, pPager->setMaster); |
| 39421 | 40045 | 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; |
| 40046 | + }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){ |
| 40047 | + rc = pager_end_transaction(pPager, 0); |
| 40048 | + }else{ |
| 40049 | + rc = pager_playback(pPager, 0); |
| 40050 | + } |
| 40051 | + |
| 40052 | + assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK ); |
| 40053 | + assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR ); |
| 40054 | + |
| 40055 | + /* If an error occurs during a ROLLBACK, we can no longer trust the pager |
| 40056 | + ** cache. So call pager_error() on the way out to make any error persistent. |
| 40057 | + */ |
| 40058 | + return pager_error(pPager, rc); |
| 39453 | 40059 | } |
| 39454 | 40060 | |
| 39455 | 40061 | /* |
| 39456 | 40062 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 39457 | 40063 | ** if the database is (in theory) writable. |
| | @@ -39493,12 +40099,12 @@ |
| 39493 | 40099 | SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){ |
| 39494 | 40100 | static int a[11]; |
| 39495 | 40101 | a[0] = sqlite3PcacheRefCount(pPager->pPCache); |
| 39496 | 40102 | a[1] = sqlite3PcachePagecount(pPager->pPCache); |
| 39497 | 40103 | a[2] = sqlite3PcacheGetCachesize(pPager->pPCache); |
| 39498 | | - a[3] = pPager->dbSizeValid ? (int) pPager->dbSize : -1; |
| 39499 | | - a[4] = pPager->state; |
| 40104 | + a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize; |
| 40105 | + a[4] = pPager->eState; |
| 39500 | 40106 | a[5] = pPager->errCode; |
| 39501 | 40107 | a[6] = pPager->nHit; |
| 39502 | 40108 | a[7] = pPager->nMiss; |
| 39503 | 40109 | a[8] = 0; /* Used to be pPager->nOvfl */ |
| 39504 | 40110 | a[9] = pPager->nRead; |
| | @@ -39525,18 +40131,17 @@ |
| 39525 | 40131 | ** returned. Otherwise, SQLITE_OK. |
| 39526 | 40132 | */ |
| 39527 | 40133 | SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){ |
| 39528 | 40134 | int rc = SQLITE_OK; /* Return code */ |
| 39529 | 40135 | int nCurrent = pPager->nSavepoint; /* Current number of savepoints */ |
| 40136 | + |
| 40137 | + assert( pPager->eState>=PAGER_WRITER_LOCKED ); |
| 40138 | + assert( assert_pager_state(pPager) ); |
| 39530 | 40139 | |
| 39531 | 40140 | if( nSavepoint>nCurrent && pPager->useJournal ){ |
| 39532 | 40141 | int ii; /* Iterator variable */ |
| 39533 | 40142 | 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 | 40143 | |
| 39539 | 40144 | /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM |
| 39540 | 40145 | ** if the allocation fails. Otherwise, zero the new portion in case a |
| 39541 | 40146 | ** malloc failure occurs while populating it in the for(...) loop below. |
| 39542 | 40147 | */ |
| | @@ -39549,18 +40154,18 @@ |
| 39549 | 40154 | memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint)); |
| 39550 | 40155 | pPager->aSavepoint = aNew; |
| 39551 | 40156 | |
| 39552 | 40157 | /* Populate the PagerSavepoint structures just allocated. */ |
| 39553 | 40158 | for(ii=nCurrent; ii<nSavepoint; ii++){ |
| 39554 | | - aNew[ii].nOrig = nPage; |
| 40159 | + aNew[ii].nOrig = pPager->dbSize; |
| 39555 | 40160 | if( isOpen(pPager->jfd) && pPager->journalOff>0 ){ |
| 39556 | 40161 | aNew[ii].iOffset = pPager->journalOff; |
| 39557 | 40162 | }else{ |
| 39558 | 40163 | aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager); |
| 39559 | 40164 | } |
| 39560 | 40165 | aNew[ii].iSubRec = pPager->nSubRec; |
| 39561 | | - aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage); |
| 40166 | + aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize); |
| 39562 | 40167 | if( !aNew[ii].pInSavepoint ){ |
| 39563 | 40168 | return SQLITE_NOMEM; |
| 39564 | 40169 | } |
| 39565 | 40170 | if( pagerUseWal(pPager) ){ |
| 39566 | 40171 | sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData); |
| | @@ -39603,16 +40208,16 @@ |
| 39603 | 40208 | ** This function may return SQLITE_NOMEM if a memory allocation fails, |
| 39604 | 40209 | ** or an IO error code if an IO error occurs while rolling back a |
| 39605 | 40210 | ** savepoint. If no errors occur, SQLITE_OK is returned. |
| 39606 | 40211 | */ |
| 39607 | 40212 | SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){ |
| 39608 | | - int rc = SQLITE_OK; |
| 40213 | + int rc = pPager->errCode; /* Return code */ |
| 39609 | 40214 | |
| 39610 | 40215 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 39611 | 40216 | assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK ); |
| 39612 | 40217 | |
| 39613 | | - if( iSavepoint<pPager->nSavepoint ){ |
| 40218 | + if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){ |
| 39614 | 40219 | int ii; /* Iterator variable */ |
| 39615 | 40220 | int nNew; /* Number of remaining savepoints after this op. */ |
| 39616 | 40221 | |
| 39617 | 40222 | /* Figure out how many savepoints will still be active after this |
| 39618 | 40223 | ** operation. Store this value in nNew. Then free resources associated |
| | @@ -39644,12 +40249,12 @@ |
| 39644 | 40249 | else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){ |
| 39645 | 40250 | PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1]; |
| 39646 | 40251 | rc = pagerPlaybackSavepoint(pPager, pSavepoint); |
| 39647 | 40252 | assert(rc!=SQLITE_DONE); |
| 39648 | 40253 | } |
| 39649 | | - |
| 39650 | 40254 | } |
| 40255 | + |
| 39651 | 40256 | return rc; |
| 39652 | 40257 | } |
| 39653 | 40258 | |
| 39654 | 40259 | /* |
| 39655 | 40260 | ** Return the full pathname of the database file. |
| | @@ -39743,10 +40348,14 @@ |
| 39743 | 40348 | Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */ |
| 39744 | 40349 | int rc; /* Return code */ |
| 39745 | 40350 | Pgno origPgno; /* The original page number */ |
| 39746 | 40351 | |
| 39747 | 40352 | assert( pPg->nRef>0 ); |
| 40353 | + assert( pPager->eState==PAGER_WRITER_CACHEMOD |
| 40354 | + || pPager->eState==PAGER_WRITER_DBMOD |
| 40355 | + ); |
| 40356 | + assert( assert_pager_state(pPager) ); |
| 39748 | 40357 | |
| 39749 | 40358 | /* In order to be able to rollback, an in-memory database must journal |
| 39750 | 40359 | ** the page we are moving from. |
| 39751 | 40360 | */ |
| 39752 | 40361 | if( MEMDB ){ |
| | @@ -39792,15 +40401,14 @@ |
| 39792 | 40401 | */ |
| 39793 | 40402 | if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){ |
| 39794 | 40403 | needSyncPgno = pPg->pgno; |
| 39795 | 40404 | assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize ); |
| 39796 | 40405 | assert( pPg->flags&PGHDR_DIRTY ); |
| 39797 | | - assert( pPager->needSync ); |
| 39798 | 40406 | } |
| 39799 | 40407 | |
| 39800 | 40408 | /* 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 |
| 40409 | + ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for |
| 39802 | 40410 | ** page pgno before the 'move' operation, it needs to be retained |
| 39803 | 40411 | ** for the page moved there. |
| 39804 | 40412 | */ |
| 39805 | 40413 | pPg->flags &= ~PGHDR_NEED_SYNC; |
| 39806 | 40414 | pPgOld = pager_lookup(pPager, pgno); |
| | @@ -39808,67 +40416,59 @@ |
| 39808 | 40416 | if( pPgOld ){ |
| 39809 | 40417 | pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC); |
| 39810 | 40418 | if( MEMDB ){ |
| 39811 | 40419 | /* Do not discard pages from an in-memory database since we might |
| 39812 | 40420 | ** need to rollback later. Just move the page out of the way. */ |
| 39813 | | - assert( pPager->dbSizeValid ); |
| 39814 | 40421 | sqlite3PcacheMove(pPgOld, pPager->dbSize+1); |
| 39815 | 40422 | }else{ |
| 39816 | 40423 | sqlite3PcacheDrop(pPgOld); |
| 39817 | 40424 | } |
| 39818 | 40425 | } |
| 39819 | 40426 | |
| 39820 | 40427 | origPgno = pPg->pgno; |
| 39821 | 40428 | sqlite3PcacheMove(pPg, pgno); |
| 39822 | 40429 | sqlite3PcacheMakeDirty(pPg); |
| 39823 | | - pPager->dbModified = 1; |
| 40430 | + |
| 40431 | + /* For an in-memory database, make sure the original page continues |
| 40432 | + ** to exist, in case the transaction needs to roll back. Use pPgOld |
| 40433 | + ** as the original page since it has already been allocated. |
| 40434 | + */ |
| 40435 | + if( MEMDB ){ |
| 40436 | + assert( pPgOld ); |
| 40437 | + sqlite3PcacheMove(pPgOld, origPgno); |
| 40438 | + sqlite3PagerUnref(pPgOld); |
| 40439 | + } |
| 39824 | 40440 | |
| 39825 | 40441 | if( needSyncPgno ){ |
| 39826 | 40442 | /* If needSyncPgno is non-zero, then the journal file needs to be |
| 39827 | 40443 | ** sync()ed before any data is written to database file page needSyncPgno. |
| 39828 | 40444 | ** Currently, no such page exists in the page-cache and the |
| 39829 | 40445 | ** "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 |
| 40446 | + ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC |
| 39831 | 40447 | ** flag. |
| 39832 | 40448 | ** |
| 39833 | 40449 | ** If the attempt to load the page into the page-cache fails, (due |
| 39834 | 40450 | ** to a malloc() or IO failure), clear the bit in the pInJournal[] |
| 39835 | 40451 | ** array. Otherwise, if the page is loaded and written again in |
| 39836 | 40452 | ** this transaction, it may be written to the database file before |
| 39837 | 40453 | ** it is synced into the journal file. This way, it may end up in |
| 39838 | 40454 | ** 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 | 40455 | */ |
| 39843 | 40456 | PgHdr *pPgHdr; |
| 39844 | | - assert( pPager->needSync ); |
| 39845 | 40457 | rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); |
| 39846 | 40458 | if( rc!=SQLITE_OK ){ |
| 39847 | 40459 | if( needSyncPgno<=pPager->dbOrigSize ){ |
| 39848 | 40460 | assert( pPager->pTmpSpace!=0 ); |
| 39849 | 40461 | sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace); |
| 39850 | 40462 | } |
| 39851 | 40463 | return rc; |
| 39852 | 40464 | } |
| 39853 | | - pPager->needSync = 1; |
| 39854 | | - assert( pPager->noSync==0 && !MEMDB ); |
| 39855 | 40465 | pPgHdr->flags |= PGHDR_NEED_SYNC; |
| 39856 | 40466 | sqlite3PcacheMakeDirty(pPgHdr); |
| 39857 | 40467 | sqlite3PagerUnref(pPgHdr); |
| 39858 | 40468 | } |
| 39859 | 40469 | |
| 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 | 40470 | return SQLITE_OK; |
| 39871 | 40471 | } |
| 39872 | 40472 | #endif |
| 39873 | 40473 | |
| 39874 | 40474 | /* |
| | @@ -39929,10 +40529,17 @@ |
| 39929 | 40529 | ** |
| 39930 | 40530 | ** The returned indicate the current (possibly updated) journal-mode. |
| 39931 | 40531 | */ |
| 39932 | 40532 | SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ |
| 39933 | 40533 | u8 eOld = pPager->journalMode; /* Prior journalmode */ |
| 40534 | + |
| 40535 | +#ifdef SQLITE_DEBUG |
| 40536 | + /* The print_pager_state() routine is intended to be used by the debugger |
| 40537 | + ** only. We invoke it once here to suppress a compiler warning. */ |
| 40538 | + print_pager_state(pPager); |
| 40539 | +#endif |
| 40540 | + |
| 39934 | 40541 | |
| 39935 | 40542 | /* The eMode parameter is always valid */ |
| 39936 | 40543 | assert( eMode==PAGER_JOURNALMODE_DELETE |
| 39937 | 40544 | || eMode==PAGER_JOURNALMODE_TRUNCATE |
| 39938 | 40545 | || eMode==PAGER_JOURNALMODE_PERSIST |
| | @@ -39955,24 +40562,17 @@ |
| 39955 | 40562 | eMode = eOld; |
| 39956 | 40563 | } |
| 39957 | 40564 | } |
| 39958 | 40565 | |
| 39959 | 40566 | 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 | 40567 | |
| 39969 | 40568 | /* Change the journal mode. */ |
| 40569 | + assert( pPager->eState!=PAGER_ERROR ); |
| 39970 | 40570 | pPager->journalMode = (u8)eMode; |
| 39971 | 40571 | |
| 39972 | 40572 | /* When transistioning from TRUNCATE or PERSIST to any other journal |
| 39973 | | - ** mode except WAL (and we are not in locking_mode=EXCLUSIVE) then |
| 40573 | + ** mode except WAL, unless the pager is in locking_mode=exclusive mode, |
| 39974 | 40574 | ** delete the journal file. |
| 39975 | 40575 | */ |
| 39976 | 40576 | assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 ); |
| 39977 | 40577 | assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 ); |
| 39978 | 40578 | assert( (PAGER_JOURNALMODE_DELETE & 5)==0 ); |
| | @@ -39989,28 +40589,34 @@ |
| 39989 | 40589 | ** |
| 39990 | 40590 | ** Before deleting the journal file, obtain a RESERVED lock on the |
| 39991 | 40591 | ** database file. This ensures that the journal file is not deleted |
| 39992 | 40592 | ** while it is in use by some other client. |
| 39993 | 40593 | */ |
| 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 ){ |
| 40594 | + sqlite3OsClose(pPager->jfd); |
| 40595 | + if( pPager->eLock>=RESERVED_LOCK ){ |
| 40004 | 40596 | sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 40597 | + }else{ |
| 40598 | + int rc = SQLITE_OK; |
| 40599 | + int state = pPager->eState; |
| 40600 | + assert( state==PAGER_OPEN || state==PAGER_READER ); |
| 40601 | + if( state==PAGER_OPEN ){ |
| 40602 | + rc = sqlite3PagerSharedLock(pPager); |
| 40603 | + } |
| 40604 | + if( pPager->eState==PAGER_READER ){ |
| 40605 | + assert( rc==SQLITE_OK ); |
| 40606 | + rc = pagerLockDb(pPager, RESERVED_LOCK); |
| 40607 | + } |
| 40608 | + if( rc==SQLITE_OK ){ |
| 40609 | + sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); |
| 40610 | + } |
| 40611 | + if( rc==SQLITE_OK && state==PAGER_READER ){ |
| 40612 | + pagerUnlockDb(pPager, SHARED_LOCK); |
| 40613 | + }else if( state==PAGER_OPEN ){ |
| 40614 | + pager_unlock(pPager); |
| 40615 | + } |
| 40616 | + assert( state==pPager->eState ); |
| 40005 | 40617 | } |
| 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 | 40618 | } |
| 40013 | 40619 | } |
| 40014 | 40620 | |
| 40015 | 40621 | /* Return the new journal mode */ |
| 40016 | 40622 | return (int)pPager->journalMode; |
| | @@ -40027,11 +40633,12 @@ |
| 40027 | 40633 | ** Return TRUE if the pager is in a state where it is OK to change the |
| 40028 | 40634 | ** journalmode. Journalmode changes can only happen when the database |
| 40029 | 40635 | ** is unmodified. |
| 40030 | 40636 | */ |
| 40031 | 40637 | SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){ |
| 40032 | | - if( pPager->dbModified ) return 0; |
| 40638 | + assert( assert_pager_state(pPager) ); |
| 40639 | + if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0; |
| 40033 | 40640 | if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0; |
| 40034 | 40641 | return 1; |
| 40035 | 40642 | } |
| 40036 | 40643 | |
| 40037 | 40644 | /* |
| | @@ -40092,39 +40699,46 @@ |
| 40092 | 40699 | ** |
| 40093 | 40700 | ** If the pager passed as the first argument is open on a real database |
| 40094 | 40701 | ** file (not a temp file or an in-memory database), and the WAL file |
| 40095 | 40702 | ** is not already open, make an attempt to open it now. If successful, |
| 40096 | 40703 | ** 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 |
| 40704 | +** not support the xShmXXX() methods, return an error code. *pbOpen is |
| 40098 | 40705 | ** not modified in either case. |
| 40099 | 40706 | ** |
| 40100 | 40707 | ** 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 |
| 40708 | +** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK |
| 40102 | 40709 | ** without doing anything. |
| 40103 | 40710 | */ |
| 40104 | 40711 | SQLITE_PRIVATE int sqlite3PagerOpenWal( |
| 40105 | 40712 | Pager *pPager, /* Pager object */ |
| 40106 | | - int *pisOpen /* OUT: Set to true if call is a no-op */ |
| 40713 | + int *pbOpen /* OUT: Set to true if call is a no-op */ |
| 40107 | 40714 | ){ |
| 40108 | 40715 | int rc = SQLITE_OK; /* Return code */ |
| 40109 | 40716 | |
| 40110 | | - assert( pPager->state>=PAGER_SHARED ); |
| 40111 | | - assert( (pisOpen==0 && !pPager->tempFile && !pPager->pWal) || *pisOpen==0 ); |
| 40717 | + assert( assert_pager_state(pPager) ); |
| 40718 | + assert( pPager->eState==PAGER_OPEN || pbOpen ); |
| 40719 | + assert( pPager->eState==PAGER_READER || !pbOpen ); |
| 40720 | + assert( pbOpen==0 || *pbOpen==0 ); |
| 40721 | + assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) ); |
| 40112 | 40722 | |
| 40113 | 40723 | if( !pPager->tempFile && !pPager->pWal ){ |
| 40114 | 40724 | if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN; |
| 40725 | + |
| 40726 | + /* Close any rollback journal previously open */ |
| 40727 | + sqlite3OsClose(pPager->jfd); |
| 40115 | 40728 | |
| 40116 | 40729 | /* Open the connection to the log file. If this operation fails, |
| 40117 | 40730 | ** (e.g. due to malloc() failure), unlock the database file and |
| 40118 | 40731 | ** return an error code. |
| 40119 | 40732 | */ |
| 40120 | 40733 | rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, &pPager->pWal); |
| 40121 | 40734 | if( rc==SQLITE_OK ){ |
| 40122 | 40735 | pPager->journalMode = PAGER_JOURNALMODE_WAL; |
| 40736 | + pPager->eState = PAGER_OPEN; |
| 40123 | 40737 | } |
| 40124 | 40738 | }else{ |
| 40125 | | - *pisOpen = 1; |
| 40739 | + *pbOpen = 1; |
| 40126 | 40740 | } |
| 40127 | 40741 | |
| 40128 | 40742 | return rc; |
| 40129 | 40743 | } |
| 40130 | 40744 | |
| | @@ -40146,11 +40760,11 @@ |
| 40146 | 40760 | ** it may need to be checkpointed before the connection can switch to |
| 40147 | 40761 | ** rollback mode. Open it now so this can happen. |
| 40148 | 40762 | */ |
| 40149 | 40763 | if( !pPager->pWal ){ |
| 40150 | 40764 | int logexists = 0; |
| 40151 | | - rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED); |
| 40765 | + rc = pagerLockDb(pPager, SHARED_LOCK); |
| 40152 | 40766 | if( rc==SQLITE_OK ){ |
| 40153 | 40767 | rc = sqlite3OsAccess( |
| 40154 | 40768 | pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists |
| 40155 | 40769 | ); |
| 40156 | 40770 | } |
| | @@ -40162,21 +40776,21 @@ |
| 40162 | 40776 | |
| 40163 | 40777 | /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on |
| 40164 | 40778 | ** the database file, the log and log-summary files will be deleted. |
| 40165 | 40779 | */ |
| 40166 | 40780 | if( rc==SQLITE_OK && pPager->pWal ){ |
| 40167 | | - rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE); |
| 40781 | + rc = pagerLockDb(pPager, EXCLUSIVE_LOCK); |
| 40168 | 40782 | if( rc==SQLITE_OK ){ |
| 40169 | 40783 | rc = sqlite3WalClose(pPager->pWal, |
| 40170 | 40784 | (pPager->noSync ? 0 : pPager->sync_flags), |
| 40171 | 40785 | pPager->pageSize, (u8*)pPager->pTmpSpace |
| 40172 | 40786 | ); |
| 40173 | 40787 | pPager->pWal = 0; |
| 40174 | 40788 | }else{ |
| 40175 | 40789 | /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock |
| 40176 | 40790 | ** that we did get back to SHARED. */ |
| 40177 | | - sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED); |
| 40791 | + pagerUnlockDb(pPager, SQLITE_LOCK_SHARED); |
| 40178 | 40792 | } |
| 40179 | 40793 | } |
| 40180 | 40794 | return rc; |
| 40181 | 40795 | } |
| 40182 | 40796 | |
| | @@ -40492,18 +41106,22 @@ |
| 40492 | 41106 | /* |
| 40493 | 41107 | ** The following object holds a copy of the wal-index header content. |
| 40494 | 41108 | ** |
| 40495 | 41109 | ** The actual header in the wal-index consists of two copies of this |
| 40496 | 41110 | ** object. |
| 41111 | +** |
| 41112 | +** The szPage value can be any power of 2 between 512 and 32768, inclusive. |
| 41113 | +** Or it can be 1 to represent a 65536-byte page. The latter case was |
| 41114 | +** added in 3.7.1 when support for 64K pages was added. |
| 40497 | 41115 | */ |
| 40498 | 41116 | struct WalIndexHdr { |
| 40499 | 41117 | u32 iVersion; /* Wal-index version */ |
| 40500 | 41118 | u32 unused; /* Unused (padding) field */ |
| 40501 | 41119 | u32 iChange; /* Counter incremented each transaction */ |
| 40502 | 41120 | u8 isInit; /* 1 when initialized */ |
| 40503 | 41121 | u8 bigEndCksum; /* True if checksums in WAL are big-endian */ |
| 40504 | | - u16 szPage; /* Database page size in bytes */ |
| 41122 | + u16 szPage; /* Database page size in bytes. 1==64K */ |
| 40505 | 41123 | u32 mxFrame; /* Index of last valid frame in the WAL */ |
| 40506 | 41124 | u32 nPage; /* Size of database in pages */ |
| 40507 | 41125 | u32 aFrameCksum[2]; /* Checksum of last frame in log */ |
| 40508 | 41126 | u32 aSalt[2]; /* Two salt values copied from WAL header */ |
| 40509 | 41127 | u32 aCksum[2]; /* Checksum over all prior fields */ |
| | @@ -40610,11 +41228,11 @@ |
| 40610 | 41228 | sqlite3_file *pDbFd; /* File handle for the database file */ |
| 40611 | 41229 | sqlite3_file *pWalFd; /* File handle for WAL file */ |
| 40612 | 41230 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
| 40613 | 41231 | int nWiData; /* Size of array apWiData */ |
| 40614 | 41232 | volatile u32 **apWiData; /* Pointer to wal-index content in memory */ |
| 40615 | | - u16 szPage; /* Database page size */ |
| 41233 | + u32 szPage; /* Database page size */ |
| 40616 | 41234 | i16 readLock; /* Which read lock is being held. -1 for none */ |
| 40617 | 41235 | u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ |
| 40618 | 41236 | u8 writeLock; /* True if in a write transaction */ |
| 40619 | 41237 | u8 ckptLock; /* True if holding a checkpoint lock */ |
| 40620 | 41238 | u8 readOnly; /* True if the WAL file is open read-only */ |
| | @@ -41281,11 +41899,11 @@ |
| 41281 | 41899 | || szPage<512 |
| 41282 | 41900 | ){ |
| 41283 | 41901 | goto finished; |
| 41284 | 41902 | } |
| 41285 | 41903 | pWal->hdr.bigEndCksum = (u8)(magic&0x00000001); |
| 41286 | | - pWal->szPage = (u16)szPage; |
| 41904 | + pWal->szPage = szPage; |
| 41287 | 41905 | pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); |
| 41288 | 41906 | memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); |
| 41289 | 41907 | |
| 41290 | 41908 | /* Verify that the WAL header checksum is correct */ |
| 41291 | 41909 | walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, |
| | @@ -41331,11 +41949,13 @@ |
| 41331 | 41949 | |
| 41332 | 41950 | /* If nTruncate is non-zero, this is a commit record. */ |
| 41333 | 41951 | if( nTruncate ){ |
| 41334 | 41952 | pWal->hdr.mxFrame = iFrame; |
| 41335 | 41953 | pWal->hdr.nPage = nTruncate; |
| 41336 | | - pWal->hdr.szPage = (u16)szPage; |
| 41954 | + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); |
| 41955 | + testcase( szPage<=32768 ); |
| 41956 | + testcase( szPage>=65536 ); |
| 41337 | 41957 | aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; |
| 41338 | 41958 | aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; |
| 41339 | 41959 | } |
| 41340 | 41960 | } |
| 41341 | 41961 | |
| | @@ -41356,10 +41976,21 @@ |
| 41356 | 41976 | */ |
| 41357 | 41977 | pInfo = walCkptInfo(pWal); |
| 41358 | 41978 | pInfo->nBackfill = 0; |
| 41359 | 41979 | pInfo->aReadMark[0] = 0; |
| 41360 | 41980 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 41981 | + |
| 41982 | + /* If more than one frame was recovered from the log file, report an |
| 41983 | + ** event via sqlite3_log(). This is to help with identifying performance |
| 41984 | + ** problems caused by applications routinely shutting down without |
| 41985 | + ** checkpointing the log file. |
| 41986 | + */ |
| 41987 | + if( pWal->hdr.nPage ){ |
| 41988 | + sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s", |
| 41989 | + pWal->hdr.nPage, pWal->zWalName |
| 41990 | + ); |
| 41991 | + } |
| 41361 | 41992 | } |
| 41362 | 41993 | |
| 41363 | 41994 | recovery_error: |
| 41364 | 41995 | WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); |
| 41365 | 41996 | walUnlockExclusive(pWal, iLock, nLock); |
| | @@ -41716,18 +42347,22 @@ |
| 41716 | 42347 | int sync_flags, /* Flags for OsSync() (or 0) */ |
| 41717 | 42348 | int nBuf, /* Size of zBuf in bytes */ |
| 41718 | 42349 | u8 *zBuf /* Temporary buffer to use */ |
| 41719 | 42350 | ){ |
| 41720 | 42351 | int rc; /* Return code */ |
| 41721 | | - int szPage = pWal->hdr.szPage; /* Database page-size */ |
| 42352 | + int szPage; /* Database page-size */ |
| 41722 | 42353 | WalIterator *pIter = 0; /* Wal iterator context */ |
| 41723 | 42354 | u32 iDbpage = 0; /* Next database page to write */ |
| 41724 | 42355 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
| 41725 | 42356 | u32 mxSafeFrame; /* Max frame that can be backfilled */ |
| 42357 | + u32 mxPage; /* Max database page to write */ |
| 41726 | 42358 | int i; /* Loop counter */ |
| 41727 | 42359 | volatile WalCkptInfo *pInfo; /* The checkpoint status information */ |
| 41728 | 42360 | |
| 42361 | + szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 42362 | + testcase( szPage<=32768 ); |
| 42363 | + testcase( szPage>=65536 ); |
| 41729 | 42364 | if( pWal->hdr.mxFrame==0 ) return SQLITE_OK; |
| 41730 | 42365 | |
| 41731 | 42366 | /* Allocate the iterator */ |
| 41732 | 42367 | rc = walIteratorInit(pWal, &pIter); |
| 41733 | 42368 | if( rc!=SQLITE_OK ){ |
| | @@ -41734,11 +42369,11 @@ |
| 41734 | 42369 | return rc; |
| 41735 | 42370 | } |
| 41736 | 42371 | assert( pIter ); |
| 41737 | 42372 | |
| 41738 | 42373 | /*** TODO: Move this test out to the caller. Make it an assert() here ***/ |
| 41739 | | - if( pWal->hdr.szPage!=nBuf ){ |
| 42374 | + if( szPage!=nBuf ){ |
| 41740 | 42375 | rc = SQLITE_CORRUPT_BKPT; |
| 41741 | 42376 | goto walcheckpoint_out; |
| 41742 | 42377 | } |
| 41743 | 42378 | |
| 41744 | 42379 | /* Compute in mxSafeFrame the index of the last frame of the WAL that is |
| | @@ -41745,10 +42380,11 @@ |
| 41745 | 42380 | ** safe to write into the database. Frames beyond mxSafeFrame might |
| 41746 | 42381 | ** overwrite database pages that are in use by active readers and thus |
| 41747 | 42382 | ** cannot be backfilled from the WAL. |
| 41748 | 42383 | */ |
| 41749 | 42384 | mxSafeFrame = pWal->hdr.mxFrame; |
| 42385 | + mxPage = pWal->hdr.nPage; |
| 41750 | 42386 | pInfo = walCkptInfo(pWal); |
| 41751 | 42387 | for(i=1; i<WAL_NREADER; i++){ |
| 41752 | 42388 | u32 y = pInfo->aReadMark[i]; |
| 41753 | 42389 | if( mxSafeFrame>=y ){ |
| 41754 | 42390 | assert( y<=pWal->hdr.mxFrame ); |
| | @@ -41765,22 +42401,34 @@ |
| 41765 | 42401 | } |
| 41766 | 42402 | |
| 41767 | 42403 | if( pInfo->nBackfill<mxSafeFrame |
| 41768 | 42404 | && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK |
| 41769 | 42405 | ){ |
| 42406 | + i64 nSize; /* Current size of database file */ |
| 41770 | 42407 | u32 nBackfill = pInfo->nBackfill; |
| 41771 | 42408 | |
| 41772 | 42409 | /* Sync the WAL to disk */ |
| 41773 | 42410 | if( sync_flags ){ |
| 41774 | 42411 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 41775 | 42412 | } |
| 42413 | + |
| 42414 | + /* If the database file may grow as a result of this checkpoint, hint |
| 42415 | + ** about the eventual size of the db file to the VFS layer. |
| 42416 | + */ |
| 42417 | + if( rc==SQLITE_OK ){ |
| 42418 | + i64 nReq = ((i64)mxPage * szPage); |
| 42419 | + rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); |
| 42420 | + if( rc==SQLITE_OK && nSize<nReq ){ |
| 42421 | + sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq); |
| 42422 | + } |
| 42423 | + } |
| 41776 | 42424 | |
| 41777 | 42425 | /* Iterate through the contents of the WAL, copying data to the db file. */ |
| 41778 | 42426 | while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
| 41779 | 42427 | i64 iOffset; |
| 41780 | 42428 | assert( walFramePgno(pWal, iFrame)==iDbpage ); |
| 41781 | | - if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue; |
| 42429 | + if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue; |
| 41782 | 42430 | iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE; |
| 41783 | 42431 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ |
| 41784 | 42432 | rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset); |
| 41785 | 42433 | if( rc!=SQLITE_OK ) break; |
| 41786 | 42434 | iOffset = (iDbpage-1)*(i64)szPage; |
| | @@ -41883,11 +42531,11 @@ |
| 41883 | 42531 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
| 41884 | 42532 | |
| 41885 | 42533 | /* The first page of the wal-index must be mapped at this point. */ |
| 41886 | 42534 | assert( pWal->nWiData>0 && pWal->apWiData[0] ); |
| 41887 | 42535 | |
| 41888 | | - /* Read the header. This might happen currently with a write to the |
| 42536 | + /* Read the header. This might happen concurrently with a write to the |
| 41889 | 42537 | ** same area of shared memory on a different CPU in a SMP, |
| 41890 | 42538 | ** meaning it is possible that an inconsistent snapshot is read |
| 41891 | 42539 | ** from the file. If this happens, return non-zero. |
| 41892 | 42540 | ** |
| 41893 | 42541 | ** There are two copies of the header at the beginning of the wal-index. |
| | @@ -41912,11 +42560,13 @@ |
| 41912 | 42560 | } |
| 41913 | 42561 | |
| 41914 | 42562 | if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){ |
| 41915 | 42563 | *pChanged = 1; |
| 41916 | 42564 | memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr)); |
| 41917 | | - pWal->szPage = pWal->hdr.szPage; |
| 42565 | + pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 42566 | + testcase( pWal->szPage<=32768 ); |
| 42567 | + testcase( pWal->szPage>=65536 ); |
| 41918 | 42568 | } |
| 41919 | 42569 | |
| 41920 | 42570 | /* The header was successfully read. Return zero. */ |
| 41921 | 42571 | return 0; |
| 41922 | 42572 | } |
| | @@ -42231,10 +42881,11 @@ |
| 42231 | 42881 | /* |
| 42232 | 42882 | ** Finish with a read transaction. All this does is release the |
| 42233 | 42883 | ** read-lock. |
| 42234 | 42884 | */ |
| 42235 | 42885 | SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){ |
| 42886 | + sqlite3WalEndWriteTransaction(pWal); |
| 42236 | 42887 | if( pWal->readLock>=0 ){ |
| 42237 | 42888 | walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); |
| 42238 | 42889 | pWal->readLock = -1; |
| 42239 | 42890 | } |
| 42240 | 42891 | } |
| | @@ -42341,11 +42992,17 @@ |
| 42341 | 42992 | |
| 42342 | 42993 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 42343 | 42994 | ** required page. Read and return data from the log file. |
| 42344 | 42995 | */ |
| 42345 | 42996 | if( iRead ){ |
| 42346 | | - i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE; |
| 42997 | + int sz; |
| 42998 | + i64 iOffset; |
| 42999 | + sz = pWal->hdr.szPage; |
| 43000 | + sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16); |
| 43001 | + testcase( sz<=32768 ); |
| 43002 | + testcase( sz>=65536 ); |
| 43003 | + iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE; |
| 42347 | 43004 | *pInWal = 1; |
| 42348 | 43005 | /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ |
| 42349 | 43006 | return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset); |
| 42350 | 43007 | } |
| 42351 | 43008 | |
| | @@ -42353,15 +43010,17 @@ |
| 42353 | 43010 | return SQLITE_OK; |
| 42354 | 43011 | } |
| 42355 | 43012 | |
| 42356 | 43013 | |
| 42357 | 43014 | /* |
| 42358 | | -** Set *pPgno to the size of the database file (or zero, if unknown). |
| 43015 | +** Return the size of the database in pages (or zero, if unknown). |
| 42359 | 43016 | */ |
| 42360 | | -SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){ |
| 42361 | | - assert( pWal->readLock>=0 || pWal->lockError ); |
| 42362 | | - *pPgno = pWal->hdr.nPage; |
| 43017 | +SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){ |
| 43018 | + if( pWal && ALWAYS(pWal->readLock>=0) ){ |
| 43019 | + return pWal->hdr.nPage; |
| 43020 | + } |
| 43021 | + return 0; |
| 42363 | 43022 | } |
| 42364 | 43023 | |
| 42365 | 43024 | |
| 42366 | 43025 | /* |
| 42367 | 43026 | ** This function starts a write transaction on the WAL. |
| | @@ -42433,11 +43092,11 @@ |
| 42433 | 43092 | ** Otherwise, if the callback function does not return an error, this |
| 42434 | 43093 | ** function returns SQLITE_OK. |
| 42435 | 43094 | */ |
| 42436 | 43095 | SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
| 42437 | 43096 | int rc = SQLITE_OK; |
| 42438 | | - if( pWal->writeLock ){ |
| 43097 | + if( ALWAYS(pWal->writeLock) ){ |
| 42439 | 43098 | Pgno iMax = pWal->hdr.mxFrame; |
| 42440 | 43099 | Pgno iFrame; |
| 42441 | 43100 | |
| 42442 | 43101 | /* Restore the clients cache of the wal-index header to the state it |
| 42443 | 43102 | ** was in before the client began writing to the database. |
| | @@ -42622,11 +43281,11 @@ |
| 42622 | 43281 | memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); |
| 42623 | 43282 | walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); |
| 42624 | 43283 | sqlite3Put4byte(&aWalHdr[24], aCksum[0]); |
| 42625 | 43284 | sqlite3Put4byte(&aWalHdr[28], aCksum[1]); |
| 42626 | 43285 | |
| 42627 | | - pWal->szPage = (u16)szPage; |
| 43286 | + pWal->szPage = szPage; |
| 42628 | 43287 | pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; |
| 42629 | 43288 | pWal->hdr.aFrameCksum[0] = aCksum[0]; |
| 42630 | 43289 | pWal->hdr.aFrameCksum[1] = aCksum[1]; |
| 42631 | 43290 | |
| 42632 | 43291 | rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); |
| | @@ -42717,11 +43376,13 @@ |
| 42717 | 43376 | rc = walIndexAppend(pWal, iFrame, pLast->pgno); |
| 42718 | 43377 | } |
| 42719 | 43378 | |
| 42720 | 43379 | if( rc==SQLITE_OK ){ |
| 42721 | 43380 | /* Update the private copy of the header. */ |
| 42722 | | - pWal->hdr.szPage = (u16)szPage; |
| 43381 | + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); |
| 43382 | + testcase( szPage<=32768 ); |
| 43383 | + testcase( szPage>=65536 ); |
| 42723 | 43384 | pWal->hdr.mxFrame = iFrame; |
| 42724 | 43385 | if( isCommit ){ |
| 42725 | 43386 | pWal->hdr.iChange++; |
| 42726 | 43387 | pWal->hdr.nPage = nTruncate; |
| 42727 | 43388 | } |
| | @@ -42929,11 +43590,11 @@ |
| 42929 | 43590 | ** |
| 42930 | 43591 | ** FORMAT DETAILS |
| 42931 | 43592 | ** |
| 42932 | 43593 | ** The file is divided into pages. The first page is called page 1, |
| 42933 | 43594 | ** 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. |
| 43595 | +** "no such page". The page size can be any power of 2 between 512 and 65536. |
| 42935 | 43596 | ** Each page can be either a btree page, a freelist page, an overflow |
| 42936 | 43597 | ** page, or a pointer-map page. |
| 42937 | 43598 | ** |
| 42938 | 43599 | ** The first page is always a btree page. The first 100 bytes of the first |
| 42939 | 43600 | ** page contain a special header (the "file header") that describes the file. |
| | @@ -43295,18 +43956,18 @@ |
| 43295 | 43956 | u8 initiallyEmpty; /* Database is empty at start of transaction */ |
| 43296 | 43957 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 43297 | 43958 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 43298 | 43959 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 43299 | 43960 | #endif |
| 43300 | | - u16 pageSize; /* Total number of bytes on a page */ |
| 43301 | | - u16 usableSize; /* Number of usable bytes on each page */ |
| 43302 | 43961 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 43303 | 43962 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 43304 | 43963 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 43305 | 43964 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 43306 | 43965 | u8 inTransaction; /* Transaction state */ |
| 43307 | 43966 | u8 doNotUseWAL; /* If true, do not open write-ahead-log file */ |
| 43967 | + u32 pageSize; /* Total number of bytes on a page */ |
| 43968 | + u32 usableSize; /* Number of usable bytes on each page */ |
| 43308 | 43969 | int nTransaction; /* Number of open transactions (read + write) */ |
| 43309 | 43970 | u32 nPage; /* Number of pages in the database */ |
| 43310 | 43971 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 43311 | 43972 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 43312 | 43973 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ |
| | @@ -43901,11 +44562,20 @@ |
| 43901 | 44562 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 43902 | 44563 | #else |
| 43903 | 44564 | # define TRACE(X) |
| 43904 | 44565 | #endif |
| 43905 | 44566 | |
| 43906 | | - |
| 44567 | +/* |
| 44568 | +** Extract a 2-byte big-endian integer from an array of unsigned bytes. |
| 44569 | +** But if the value is zero, make it 65536. |
| 44570 | +** |
| 44571 | +** This routine is used to extract the "offset to cell content area" value |
| 44572 | +** from the header of a btree page. If the page size is 65536 and the page |
| 44573 | +** is empty, the offset should be 65536, but the 2-byte value stores zero. |
| 44574 | +** This routine makes the necessary adjustment to 65536. |
| 44575 | +*/ |
| 44576 | +#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1) |
| 43907 | 44577 | |
| 43908 | 44578 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 43909 | 44579 | /* |
| 43910 | 44580 | ** A list of BtShared objects that are eligible for participation |
| 43911 | 44581 | ** in shared cache. This variable has file scope during normal builds, |
| | @@ -44590,15 +45260,20 @@ |
| 44590 | 45260 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 44591 | 45261 | /* |
| 44592 | 45262 | ** Given a page number of a regular database page, return the page |
| 44593 | 45263 | ** number for the pointer-map page that contains the entry for the |
| 44594 | 45264 | ** input page number. |
| 45265 | +** |
| 45266 | +** Return 0 (not a valid page) for pgno==1 since there is |
| 45267 | +** no pointer map associated with page 1. The integrity_check logic |
| 45268 | +** requires that ptrmapPageno(*,1)!=1. |
| 44595 | 45269 | */ |
| 44596 | 45270 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
| 44597 | 45271 | int nPagesPerMapPage; |
| 44598 | 45272 | Pgno iPtrMap, ret; |
| 44599 | 45273 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 45274 | + if( pgno<2 ) return 0; |
| 44600 | 45275 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 44601 | 45276 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 44602 | 45277 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
| 44603 | 45278 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 44604 | 45279 | ret++; |
| | @@ -45023,21 +45698,21 @@ |
| 45023 | 45698 | assert( nByte < usableSize-8 ); |
| 45024 | 45699 | |
| 45025 | 45700 | nFrag = data[hdr+7]; |
| 45026 | 45701 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 45027 | 45702 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 45028 | | - top = get2byte(&data[hdr+5]); |
| 45703 | + top = get2byteNotZero(&data[hdr+5]); |
| 45029 | 45704 | if( gap>top ) return SQLITE_CORRUPT_BKPT; |
| 45030 | 45705 | testcase( gap+2==top ); |
| 45031 | 45706 | testcase( gap+1==top ); |
| 45032 | 45707 | testcase( gap==top ); |
| 45033 | 45708 | |
| 45034 | 45709 | if( nFrag>=60 ){ |
| 45035 | 45710 | /* Always defragment highly fragmented pages */ |
| 45036 | 45711 | rc = defragmentPage(pPage); |
| 45037 | 45712 | if( rc ) return rc; |
| 45038 | | - top = get2byte(&data[hdr+5]); |
| 45713 | + top = get2byteNotZero(&data[hdr+5]); |
| 45039 | 45714 | }else if( gap+2<=top ){ |
| 45040 | 45715 | /* Search the freelist looking for a free slot big enough to satisfy |
| 45041 | 45716 | ** the request. The allocation is made from the first free slot in |
| 45042 | 45717 | ** the list that is large enough to accomadate it. |
| 45043 | 45718 | */ |
| | @@ -45075,11 +45750,11 @@ |
| 45075 | 45750 | */ |
| 45076 | 45751 | testcase( gap+2+nByte==top ); |
| 45077 | 45752 | if( gap+2+nByte>top ){ |
| 45078 | 45753 | rc = defragmentPage(pPage); |
| 45079 | 45754 | if( rc ) return rc; |
| 45080 | | - top = get2byte(&data[hdr+5]); |
| 45755 | + top = get2byteNotZero(&data[hdr+5]); |
| 45081 | 45756 | assert( gap+nByte<=top ); |
| 45082 | 45757 | } |
| 45083 | 45758 | |
| 45084 | 45759 | |
| 45085 | 45760 | /* Allocate memory from the gap in between the cell pointer array |
| | @@ -45241,28 +45916,28 @@ |
| 45241 | 45916 | if( !pPage->isInit ){ |
| 45242 | 45917 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 45243 | 45918 | u8 hdr; /* Offset to beginning of page header */ |
| 45244 | 45919 | u8 *data; /* Equal to pPage->aData */ |
| 45245 | 45920 | BtShared *pBt; /* The main btree structure */ |
| 45246 | | - u16 usableSize; /* Amount of usable space on each page */ |
| 45921 | + int usableSize; /* Amount of usable space on each page */ |
| 45247 | 45922 | 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 */ |
| 45923 | + int nFree; /* Number of unused bytes on the page */ |
| 45924 | + int top; /* First byte of the cell content area */ |
| 45250 | 45925 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 45251 | 45926 | int iCellLast; /* Last possible cell or freeblock offset */ |
| 45252 | 45927 | |
| 45253 | 45928 | pBt = pPage->pBt; |
| 45254 | 45929 | |
| 45255 | 45930 | hdr = pPage->hdrOffset; |
| 45256 | 45931 | data = pPage->aData; |
| 45257 | 45932 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 45258 | | - assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 45259 | | - pPage->maskPage = pBt->pageSize - 1; |
| 45933 | + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 45934 | + pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 45260 | 45935 | pPage->nOverflow = 0; |
| 45261 | 45936 | usableSize = pBt->usableSize; |
| 45262 | 45937 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 45263 | | - top = get2byte(&data[hdr+5]); |
| 45938 | + top = get2byteNotZero(&data[hdr+5]); |
| 45264 | 45939 | pPage->nCell = get2byte(&data[hdr+3]); |
| 45265 | 45940 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 45266 | 45941 | /* To many cells for a single page. The page must be corrupt */ |
| 45267 | 45942 | return SQLITE_CORRUPT_BKPT; |
| 45268 | 45943 | } |
| | @@ -45357,17 +46032,17 @@ |
| 45357 | 46032 | data[hdr] = (char)flags; |
| 45358 | 46033 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); |
| 45359 | 46034 | memset(&data[hdr+1], 0, 4); |
| 45360 | 46035 | data[hdr+7] = 0; |
| 45361 | 46036 | put2byte(&data[hdr+5], pBt->usableSize); |
| 45362 | | - pPage->nFree = pBt->usableSize - first; |
| 46037 | + pPage->nFree = (u16)(pBt->usableSize - first); |
| 45363 | 46038 | decodeFlags(pPage, flags); |
| 45364 | 46039 | pPage->hdrOffset = hdr; |
| 45365 | 46040 | pPage->cellOffset = first; |
| 45366 | 46041 | pPage->nOverflow = 0; |
| 45367 | | - assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 45368 | | - pPage->maskPage = pBt->pageSize - 1; |
| 46042 | + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 46043 | + pPage->maskPage = (u16)(pBt->pageSize - 1); |
| 45369 | 46044 | pPage->nCell = 0; |
| 45370 | 46045 | pPage->isInit = 1; |
| 45371 | 46046 | } |
| 45372 | 46047 | |
| 45373 | 46048 | |
| | @@ -45671,11 +46346,11 @@ |
| 45671 | 46346 | pBt->pPage1 = 0; |
| 45672 | 46347 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 45673 | 46348 | #ifdef SQLITE_SECURE_DELETE |
| 45674 | 46349 | pBt->secureDelete = 1; |
| 45675 | 46350 | #endif |
| 45676 | | - pBt->pageSize = get2byte(&zDbHeader[16]); |
| 46351 | + pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
| 45677 | 46352 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 45678 | 46353 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
| 45679 | 46354 | pBt->pageSize = 0; |
| 45680 | 46355 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 45681 | 46356 | /* If the magic name ":memory:" will create an in-memory database, then |
| | @@ -45985,11 +46660,11 @@ |
| 45985 | 46660 | assert( nReserve>=0 && nReserve<=255 ); |
| 45986 | 46661 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 45987 | 46662 | ((pageSize-1)&pageSize)==0 ){ |
| 45988 | 46663 | assert( (pageSize & 7)==0 ); |
| 45989 | 46664 | assert( !pBt->pPage1 && !pBt->pCursor ); |
| 45990 | | - pBt->pageSize = (u16)pageSize; |
| 46665 | + pBt->pageSize = (u32)pageSize; |
| 45991 | 46666 | freeTempSpace(pBt); |
| 45992 | 46667 | } |
| 45993 | 46668 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
| 45994 | 46669 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
| 45995 | 46670 | if( iFix ) pBt->pageSizeFixed = 1; |
| | @@ -46120,19 +46795,17 @@ |
| 46120 | 46795 | |
| 46121 | 46796 | /* Do some checking to help insure the file we opened really is |
| 46122 | 46797 | ** a valid database file. |
| 46123 | 46798 | */ |
| 46124 | 46799 | nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
| 46125 | | - if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){; |
| 46126 | | - goto page1_init_failed; |
| 46127 | | - } |
| 46800 | + sqlite3PagerPagecount(pBt->pPager, &nPageFile); |
| 46128 | 46801 | if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ |
| 46129 | 46802 | nPage = nPageFile; |
| 46130 | 46803 | } |
| 46131 | 46804 | if( nPage>0 ){ |
| 46132 | | - int pageSize; |
| 46133 | | - int usableSize; |
| 46805 | + u32 pageSize; |
| 46806 | + u32 usableSize; |
| 46134 | 46807 | u8 *page1 = pPage1->aData; |
| 46135 | 46808 | rc = SQLITE_NOTADB; |
| 46136 | 46809 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
| 46137 | 46810 | goto page1_init_failed; |
| 46138 | 46811 | } |
| | @@ -46179,28 +46852,29 @@ |
| 46179 | 46852 | ** version 3.6.0, we require them to be fixed. |
| 46180 | 46853 | */ |
| 46181 | 46854 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 46182 | 46855 | goto page1_init_failed; |
| 46183 | 46856 | } |
| 46184 | | - pageSize = get2byte(&page1[16]); |
| 46185 | | - if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 46186 | | - (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 46857 | + pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 46858 | + if( ((pageSize-1)&pageSize)!=0 |
| 46859 | + || pageSize>SQLITE_MAX_PAGE_SIZE |
| 46860 | + || pageSize<=256 |
| 46187 | 46861 | ){ |
| 46188 | 46862 | goto page1_init_failed; |
| 46189 | 46863 | } |
| 46190 | 46864 | assert( (pageSize & 7)==0 ); |
| 46191 | 46865 | usableSize = pageSize - page1[20]; |
| 46192 | | - if( pageSize!=pBt->pageSize ){ |
| 46866 | + if( (u32)pageSize!=pBt->pageSize ){ |
| 46193 | 46867 | /* After reading the first page of the database assuming a page size |
| 46194 | 46868 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 46195 | 46869 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 46196 | 46870 | ** zero and return SQLITE_OK. The caller will call this function |
| 46197 | 46871 | ** again with the correct page-size. |
| 46198 | 46872 | */ |
| 46199 | 46873 | releasePage(pPage1); |
| 46200 | | - pBt->usableSize = (u16)usableSize; |
| 46201 | | - pBt->pageSize = (u16)pageSize; |
| 46874 | + pBt->usableSize = usableSize; |
| 46875 | + pBt->pageSize = pageSize; |
| 46202 | 46876 | freeTempSpace(pBt); |
| 46203 | 46877 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 46204 | 46878 | pageSize-usableSize); |
| 46205 | 46879 | return rc; |
| 46206 | 46880 | } |
| | @@ -46209,12 +46883,12 @@ |
| 46209 | 46883 | goto page1_init_failed; |
| 46210 | 46884 | } |
| 46211 | 46885 | if( usableSize<480 ){ |
| 46212 | 46886 | goto page1_init_failed; |
| 46213 | 46887 | } |
| 46214 | | - pBt->pageSize = (u16)pageSize; |
| 46215 | | - pBt->usableSize = (u16)usableSize; |
| 46888 | + pBt->pageSize = pageSize; |
| 46889 | + pBt->usableSize = usableSize; |
| 46216 | 46890 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 46217 | 46891 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
| 46218 | 46892 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
| 46219 | 46893 | #endif |
| 46220 | 46894 | } |
| | @@ -46226,18 +46900,18 @@ |
| 46226 | 46900 | ** 2-byte pointer to the cell |
| 46227 | 46901 | ** 4-byte child pointer |
| 46228 | 46902 | ** 9-byte nKey value |
| 46229 | 46903 | ** 4-byte nData value |
| 46230 | 46904 | ** 4-byte overflow page pointer |
| 46231 | | - ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 46905 | + ** So a cell consists of a 2-byte pointer, a header which is as much as |
| 46232 | 46906 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 46233 | 46907 | ** page pointer. |
| 46234 | 46908 | */ |
| 46235 | | - pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 46236 | | - pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
| 46237 | | - pBt->maxLeaf = pBt->usableSize - 35; |
| 46238 | | - pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
| 46909 | + pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); |
| 46910 | + pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); |
| 46911 | + pBt->maxLeaf = (u16)(pBt->usableSize - 35); |
| 46912 | + pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); |
| 46239 | 46913 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
| 46240 | 46914 | pBt->pPage1 = pPage1; |
| 46241 | 46915 | pBt->nPage = nPage; |
| 46242 | 46916 | return SQLITE_OK; |
| 46243 | 46917 | |
| | @@ -46286,11 +46960,12 @@ |
| 46286 | 46960 | data = pP1->aData; |
| 46287 | 46961 | rc = sqlite3PagerWrite(pP1->pDbPage); |
| 46288 | 46962 | if( rc ) return rc; |
| 46289 | 46963 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 46290 | 46964 | assert( sizeof(zMagicHeader)==16 ); |
| 46291 | | - put2byte(&data[16], pBt->pageSize); |
| 46965 | + data[16] = (u8)((pBt->pageSize>>8)&0xff); |
| 46966 | + data[17] = (u8)((pBt->pageSize>>16)&0xff); |
| 46292 | 46967 | data[18] = 1; |
| 46293 | 46968 | data[19] = 1; |
| 46294 | 46969 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 46295 | 46970 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
| 46296 | 46971 | data[21] = 64; |
| | @@ -48297,13 +48972,13 @@ |
| 48297 | 48972 | c = +1; |
| 48298 | 48973 | } |
| 48299 | 48974 | pCur->validNKey = 1; |
| 48300 | 48975 | pCur->info.nKey = nCellKey; |
| 48301 | 48976 | }else{ |
| 48302 | | - /* The maximum supported page-size is 32768 bytes. This means that |
| 48977 | + /* The maximum supported page-size is 65536 bytes. This means that |
| 48303 | 48978 | ** 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 |
| 48979 | + ** page is less than 16384 bytes and may be stored as a 2-byte |
| 48305 | 48980 | ** varint. This information is used to attempt to avoid parsing |
| 48306 | 48981 | ** the entire cell by checking for the cases where the record is |
| 48307 | 48982 | ** stored entirely within the b-tree page by inspecting the first |
| 48308 | 48983 | ** 2 bytes of the cell. |
| 48309 | 48984 | */ |
| | @@ -48662,10 +49337,14 @@ |
| 48662 | 49337 | } |
| 48663 | 49338 | if( k==0 ){ |
| 48664 | 49339 | if( !pPrevTrunk ){ |
| 48665 | 49340 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 48666 | 49341 | }else{ |
| 49342 | + rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
| 49343 | + if( rc!=SQLITE_OK ){ |
| 49344 | + goto end_allocate_page; |
| 49345 | + } |
| 48667 | 49346 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 48668 | 49347 | } |
| 48669 | 49348 | }else{ |
| 48670 | 49349 | /* The trunk page is required by the caller but it contains |
| 48671 | 49350 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| | @@ -48968,11 +49647,11 @@ |
| 48968 | 49647 | BtShared *pBt = pPage->pBt; |
| 48969 | 49648 | CellInfo info; |
| 48970 | 49649 | Pgno ovflPgno; |
| 48971 | 49650 | int rc; |
| 48972 | 49651 | int nOvfl; |
| 48973 | | - u16 ovflPageSize; |
| 49652 | + u32 ovflPageSize; |
| 48974 | 49653 | |
| 48975 | 49654 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 48976 | 49655 | btreeParseCellPtr(pPage, pCell, &info); |
| 48977 | 49656 | if( info.iOverflow==0 ){ |
| 48978 | 49657 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
| | @@ -49193,11 +49872,11 @@ |
| 49193 | 49872 | ** |
| 49194 | 49873 | ** "sz" must be the number of bytes in the cell. |
| 49195 | 49874 | */ |
| 49196 | 49875 | static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
| 49197 | 49876 | int i; /* Loop counter */ |
| 49198 | | - int pc; /* Offset to cell content of cell being deleted */ |
| 49877 | + u32 pc; /* Offset to cell content of cell being deleted */ |
| 49199 | 49878 | u8 *data; /* pPage->aData */ |
| 49200 | 49879 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 49201 | 49880 | int rc; /* The return code */ |
| 49202 | 49881 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
| 49203 | 49882 | |
| | @@ -49211,11 +49890,11 @@ |
| 49211 | 49890 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 49212 | 49891 | pc = get2byte(ptr); |
| 49213 | 49892 | hdr = pPage->hdrOffset; |
| 49214 | 49893 | testcase( pc==get2byte(&data[hdr+5]) ); |
| 49215 | 49894 | testcase( pc+sz==pPage->pBt->usableSize ); |
| 49216 | | - if( pc < get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ |
| 49895 | + if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ |
| 49217 | 49896 | *pRC = SQLITE_CORRUPT_BKPT; |
| 49218 | 49897 | return; |
| 49219 | 49898 | } |
| 49220 | 49899 | rc = freeSpace(pPage, pc, sz); |
| 49221 | 49900 | if( rc ){ |
| | @@ -49268,11 +49947,11 @@ |
| 49268 | 49947 | int nSkip = (iChild ? 4 : 0); |
| 49269 | 49948 | |
| 49270 | 49949 | if( *pRC ) return; |
| 49271 | 49950 | |
| 49272 | 49951 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 49273 | | - assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 49952 | + assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 ); |
| 49274 | 49953 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
| 49275 | 49954 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 49276 | 49955 | /* The cell should normally be sized correctly. However, when moving a |
| 49277 | 49956 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 49278 | 49957 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| | @@ -49348,16 +50027,16 @@ |
| 49348 | 50027 | const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 49349 | 50028 | const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
| 49350 | 50029 | |
| 49351 | 50030 | assert( pPage->nOverflow==0 ); |
| 49352 | 50031 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 49353 | | - assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 50032 | + assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921); |
| 49354 | 50033 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 49355 | 50034 | |
| 49356 | 50035 | /* Check that the page has just been zeroed by zeroPage() */ |
| 49357 | 50036 | assert( pPage->nCell==0 ); |
| 49358 | | - assert( get2byte(&data[hdr+5])==nUsable ); |
| 50037 | + assert( get2byteNotZero(&data[hdr+5])==nUsable ); |
| 49359 | 50038 | |
| 49360 | 50039 | pCellptr = &data[pPage->cellOffset + nCell*2]; |
| 49361 | 50040 | cellbody = nUsable; |
| 49362 | 50041 | for(i=nCell-1; i>=0; i--){ |
| 49363 | 50042 | pCellptr -= 2; |
| | @@ -49419,10 +50098,11 @@ |
| 49419 | 50098 | |
| 49420 | 50099 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 49421 | 50100 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 49422 | 50101 | assert( pPage->nOverflow==1 ); |
| 49423 | 50102 | |
| 50103 | + /* This error condition is now caught prior to reaching this function */ |
| 49424 | 50104 | if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT; |
| 49425 | 50105 | |
| 49426 | 50106 | /* Allocate a new page. This page will become the right-sibling of |
| 49427 | 50107 | ** pPage. Make the parent page writable, so that the new divider cell |
| 49428 | 50108 | ** may be inserted. If both these operations are successful, proceed. |
| | @@ -49748,11 +50428,11 @@ |
| 49748 | 50428 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 49749 | 50429 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 49750 | 50430 | ** is allocated. */ |
| 49751 | 50431 | if( pBt->secureDelete ){ |
| 49752 | 50432 | int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
| 49753 | | - if( (iOff+szNew[i])>pBt->usableSize ){ |
| 50433 | + if( (iOff+szNew[i])>(int)pBt->usableSize ){ |
| 49754 | 50434 | rc = SQLITE_CORRUPT_BKPT; |
| 49755 | 50435 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 49756 | 50436 | goto balance_cleanup; |
| 49757 | 50437 | }else{ |
| 49758 | 50438 | memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); |
| | @@ -49827,11 +50507,11 @@ |
| 49827 | 50507 | u8 *pTemp; |
| 49828 | 50508 | assert( nCell<nMaxCells ); |
| 49829 | 50509 | szCell[nCell] = sz; |
| 49830 | 50510 | pTemp = &aSpace1[iSpace1]; |
| 49831 | 50511 | iSpace1 += sz; |
| 49832 | | - assert( sz<=pBt->pageSize/4 ); |
| 50512 | + assert( sz<=pBt->maxLocal+23 ); |
| 49833 | 50513 | assert( iSpace1<=pBt->pageSize ); |
| 49834 | 50514 | memcpy(pTemp, apDiv[i], sz); |
| 49835 | 50515 | apCell[nCell] = pTemp+leafCorrection; |
| 49836 | 50516 | assert( leafCorrection==0 || leafCorrection==4 ); |
| 49837 | 50517 | szCell[nCell] = szCell[nCell] - leafCorrection; |
| | @@ -50073,11 +50753,11 @@ |
| 50073 | 50753 | assert(leafCorrection==4); |
| 50074 | 50754 | sz = cellSizePtr(pParent, pCell); |
| 50075 | 50755 | } |
| 50076 | 50756 | } |
| 50077 | 50757 | iOvflSpace += sz; |
| 50078 | | - assert( sz<=pBt->pageSize/4 ); |
| 50758 | + assert( sz<=pBt->maxLocal+23 ); |
| 50079 | 50759 | assert( iOvflSpace<=pBt->pageSize ); |
| 50080 | 50760 | insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
| 50081 | 50761 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 50082 | 50762 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 50083 | 50763 | |
| | @@ -51317,11 +51997,11 @@ |
| 51317 | 51997 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 51318 | 51998 | if( pCheck->pBt->autoVacuum ){ |
| 51319 | 51999 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 51320 | 52000 | } |
| 51321 | 52001 | #endif |
| 51322 | | - if( n>pCheck->pBt->usableSize/4-2 ){ |
| 52002 | + if( n>(int)pCheck->pBt->usableSize/4-2 ){ |
| 51323 | 52003 | checkAppendMsg(pCheck, zContext, |
| 51324 | 52004 | "freelist leaf count too big on page %d", iPage); |
| 51325 | 52005 | N--; |
| 51326 | 52006 | }else{ |
| 51327 | 52007 | for(i=0; i<n; i++){ |
| | @@ -51528,24 +52208,24 @@ |
| 51528 | 52208 | hdr = pPage->hdrOffset; |
| 51529 | 52209 | hit = sqlite3PageMalloc( pBt->pageSize ); |
| 51530 | 52210 | if( hit==0 ){ |
| 51531 | 52211 | pCheck->mallocFailed = 1; |
| 51532 | 52212 | }else{ |
| 51533 | | - u16 contentOffset = get2byte(&data[hdr+5]); |
| 52213 | + int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 51534 | 52214 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 51535 | 52215 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 51536 | 52216 | memset(hit, 1, contentOffset); |
| 51537 | 52217 | nCell = get2byte(&data[hdr+3]); |
| 51538 | 52218 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 51539 | 52219 | for(i=0; i<nCell; i++){ |
| 51540 | 52220 | int pc = get2byte(&data[cellStart+i*2]); |
| 51541 | | - u16 size = 1024; |
| 52221 | + u32 size = 65536; |
| 51542 | 52222 | int j; |
| 51543 | 52223 | if( pc<=usableSize-4 ){ |
| 51544 | 52224 | size = cellSizePtr(pPage, &data[pc]); |
| 51545 | 52225 | } |
| 51546 | | - if( (pc+size-1)>=usableSize ){ |
| 52226 | + if( (int)(pc+size-1)>=usableSize ){ |
| 51547 | 52227 | checkAppendMsg(pCheck, 0, |
| 51548 | 52228 | "Corruption detected in cell %d on page %d",i,iPage); |
| 51549 | 52229 | }else{ |
| 51550 | 52230 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 51551 | 52231 | } |
| | @@ -52164,10 +52844,19 @@ |
| 52164 | 52844 | ** page sizes of the source and destination differ. |
| 52165 | 52845 | */ |
| 52166 | 52846 | if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){ |
| 52167 | 52847 | rc = SQLITE_READONLY; |
| 52168 | 52848 | } |
| 52849 | + |
| 52850 | +#ifdef SQLITE_HAS_CODEC |
| 52851 | + /* Backup is not possible if the page size of the destination is changing |
| 52852 | + ** a a codec is in use. |
| 52853 | + */ |
| 52854 | + if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){ |
| 52855 | + rc = SQLITE_READONLY; |
| 52856 | + } |
| 52857 | +#endif |
| 52169 | 52858 | |
| 52170 | 52859 | /* This loop runs once for each destination page spanned by the source |
| 52171 | 52860 | ** page. For each iteration, variable iOff is set to the byte offset |
| 52172 | 52861 | ** of the destination page. |
| 52173 | 52862 | */ |
| | @@ -55769,12 +56458,21 @@ |
| 55769 | 56458 | mrc = p->rc & 0xff; |
| 55770 | 56459 | assert( p->rc!=SQLITE_IOERR_BLOCKED ); /* This error no longer exists */ |
| 55771 | 56460 | isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
| 55772 | 56461 | || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
| 55773 | 56462 | if( isSpecialError ){ |
| 55774 | | - /* If the query was read-only, we need do no rollback at all. Otherwise, |
| 55775 | | - ** proceed with the special handling. |
| 56463 | + /* If the query was read-only and the error code is SQLITE_INTERRUPT, |
| 56464 | + ** no rollback is necessary. Otherwise, at least a savepoint |
| 56465 | + ** transaction must be rolled back to restore the database to a |
| 56466 | + ** consistent state. |
| 56467 | + ** |
| 56468 | + ** Even if the statement is read-only, it is important to perform |
| 56469 | + ** a statement or transaction rollback operation. If the error |
| 56470 | + ** occured while writing to the journal, sub-journal or database |
| 56471 | + ** file as part of an effort to free up cache space (see function |
| 56472 | + ** pagerStress() in pager.c), the rollback is required to restore |
| 56473 | + ** the pager to a consistent state. |
| 55776 | 56474 | */ |
| 55777 | 56475 | if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ |
| 55778 | 56476 | if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ |
| 55779 | 56477 | eStatementOp = SAVEPOINT_ROLLBACK; |
| 55780 | 56478 | }else{ |
| | @@ -63200,11 +63898,11 @@ |
| 63200 | 63898 | ** If P5 is non-zero then the key value is increased by an epsilon |
| 63201 | 63899 | ** prior to the comparison. This make the opcode work like IdxGT except |
| 63202 | 63900 | ** that if the key from register P3 is a prefix of the key in the cursor, |
| 63203 | 63901 | ** the result is false whereas it would be true with IdxGT. |
| 63204 | 63902 | */ |
| 63205 | | -/* Opcode: IdxLT P1 P2 P3 * P5 |
| 63903 | +/* Opcode: IdxLT P1 P2 P3 P4 P5 |
| 63206 | 63904 | ** |
| 63207 | 63905 | ** The P4 register values beginning with P3 form an unpacked index |
| 63208 | 63906 | ** key that omits the ROWID. Compare this key value against the index |
| 63209 | 63907 | ** that P1 is currently pointing to, ignoring the ROWID on the P1 index. |
| 63210 | 63908 | ** |
| | @@ -67547,22 +68245,23 @@ |
| 67547 | 68245 | assert( z[0]=='?' ); |
| 67548 | 68246 | pExpr->iColumn = (ynVar)(++pParse->nVar); |
| 67549 | 68247 | }else if( z[0]=='?' ){ |
| 67550 | 68248 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 67551 | 68249 | ** use it as the variable number */ |
| 67552 | | - int i = atoi((char*)&z[1]); |
| 68250 | + i64 i; |
| 68251 | + int bOk = sqlite3Atoi64(&z[1], &i); |
| 67553 | 68252 | pExpr->iColumn = (ynVar)i; |
| 67554 | 68253 | testcase( i==0 ); |
| 67555 | 68254 | testcase( i==1 ); |
| 67556 | 68255 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 67557 | 68256 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
| 67558 | | - if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 68257 | + if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 67559 | 68258 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 67560 | 68259 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
| 67561 | 68260 | } |
| 67562 | 68261 | if( i>pParse->nVar ){ |
| 67563 | | - pParse->nVar = i; |
| 68262 | + pParse->nVar = (int)i; |
| 67564 | 68263 | } |
| 67565 | 68264 | }else{ |
| 67566 | 68265 | /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable |
| 67567 | 68266 | ** number as the prior appearance of the same name, or if the name |
| 67568 | 68267 | ** has never appeared before, reuse the same variable number |
| | @@ -72092,10 +72791,11 @@ |
| 72092 | 72791 | } |
| 72093 | 72792 | } |
| 72094 | 72793 | sqlite3DbFree(db, pIdx->aSample); |
| 72095 | 72794 | } |
| 72096 | 72795 | #else |
| 72796 | + UNUSED_PARAMETER(db); |
| 72097 | 72797 | UNUSED_PARAMETER(pIdx); |
| 72098 | 72798 | #endif |
| 72099 | 72799 | } |
| 72100 | 72800 | |
| 72101 | 72801 | /* |
| | @@ -87738,11 +88438,11 @@ |
| 87738 | 88438 | ** be sent. |
| 87739 | 88439 | ** |
| 87740 | 88440 | ** regReturn is the number of the register holding the subroutine |
| 87741 | 88441 | ** return address. |
| 87742 | 88442 | ** |
| 87743 | | -** If regPrev>0 then it is a the first register in a vector that |
| 88443 | +** If regPrev>0 then it is the first register in a vector that |
| 87744 | 88444 | ** records the previous output. mem[regPrev] is a flag that is false |
| 87745 | 88445 | ** if there has been no previous output. If regPrev>0 then code is |
| 87746 | 88446 | ** generated to suppress duplicates. pKeyInfo is used for comparing |
| 87747 | 88447 | ** keys. |
| 87748 | 88448 | ** |
| | @@ -88435,16 +89135,17 @@ |
| 88435 | 89135 | ** (1) The subquery and the outer query do not both use aggregates. |
| 88436 | 89136 | ** |
| 88437 | 89137 | ** (2) The subquery is not an aggregate or the outer query is not a join. |
| 88438 | 89138 | ** |
| 88439 | 89139 | ** (3) The subquery is not the right operand of a left outer join |
| 88440 | | -** (Originally ticket #306. Strenghtened by ticket #3300) |
| 89140 | +** (Originally ticket #306. Strengthened by ticket #3300) |
| 88441 | 89141 | ** |
| 88442 | | -** (4) The subquery is not DISTINCT or the outer query is not a join. |
| 89142 | +** (4) The subquery is not DISTINCT. |
| 88443 | 89143 | ** |
| 88444 | | -** (5) The subquery is not DISTINCT or the outer query does not use |
| 88445 | | -** aggregates. |
| 89144 | +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT |
| 89145 | +** sub-queries that were excluded from this optimization. Restriction |
| 89146 | +** (4) has since been expanded to exclude all DISTINCT subqueries. |
| 88446 | 89147 | ** |
| 88447 | 89148 | ** (6) The subquery does not use aggregates or the outer query is not |
| 88448 | 89149 | ** DISTINCT. |
| 88449 | 89150 | ** |
| 88450 | 89151 | ** (7) The subquery has a FROM clause. |
| | @@ -88460,13 +89161,13 @@ |
| 88460 | 89161 | ** (11) The subquery and the outer query do not both have ORDER BY clauses. |
| 88461 | 89162 | ** |
| 88462 | 89163 | ** (**) Not implemented. Subsumed into restriction (3). Was previously |
| 88463 | 89164 | ** a separate restriction deriving from ticket #350. |
| 88464 | 89165 | ** |
| 88465 | | -** (13) The subquery and outer query do not both use LIMIT |
| 89166 | +** (13) The subquery and outer query do not both use LIMIT. |
| 88466 | 89167 | ** |
| 88467 | | -** (14) The subquery does not use OFFSET |
| 89168 | +** (14) The subquery does not use OFFSET. |
| 88468 | 89169 | ** |
| 88469 | 89170 | ** (15) The outer query is not part of a compound select or the |
| 88470 | 89171 | ** subquery does not have a LIMIT clause. |
| 88471 | 89172 | ** (See ticket #2339 and ticket [02a8e81d44]). |
| 88472 | 89173 | ** |
| | @@ -88553,13 +89254,13 @@ |
| 88553 | 89254 | if( pSub->pOffset ) return 0; /* Restriction (14) */ |
| 88554 | 89255 | if( p->pRightmost && pSub->pLimit ){ |
| 88555 | 89256 | return 0; /* Restriction (15) */ |
| 88556 | 89257 | } |
| 88557 | 89258 | 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; |
| 89259 | + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */ |
| 89260 | + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ |
| 89261 | + return 0; /* Restrictions (8)(9) */ |
| 88561 | 89262 | } |
| 88562 | 89263 | if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ |
| 88563 | 89264 | return 0; /* Restriction (6) */ |
| 88564 | 89265 | } |
| 88565 | 89266 | if( p->pOrderBy && pSub->pOrderBy ){ |
| | @@ -93225,11 +93926,11 @@ |
| 93225 | 93926 | pParse->pNewTable->aCol = 0; |
| 93226 | 93927 | } |
| 93227 | 93928 | db->pVTab = 0; |
| 93228 | 93929 | }else{ |
| 93229 | 93930 | sqlite3Error(db, SQLITE_ERROR, zErr); |
| 93230 | | - sqlite3_free(zErr); |
| 93931 | + sqlite3DbFree(db, zErr); |
| 93231 | 93932 | rc = SQLITE_ERROR; |
| 93232 | 93933 | } |
| 93233 | 93934 | pParse->declareVtab = 0; |
| 93234 | 93935 | |
| 93235 | 93936 | if( pParse->pVdbe ){ |
| | @@ -96859,39 +97560,39 @@ |
| 96859 | 97560 | ** |
| 96860 | 97561 | ** This case is also used when there are no WHERE clause |
| 96861 | 97562 | ** constraints but an index is selected anyway, in order |
| 96862 | 97563 | ** to force the output order to conform to an ORDER BY. |
| 96863 | 97564 | */ |
| 96864 | | - int aStartOp[] = { |
| 97565 | + static const u8 aStartOp[] = { |
| 96865 | 97566 | 0, |
| 96866 | 97567 | 0, |
| 96867 | 97568 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 96868 | 97569 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 96869 | 97570 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 96870 | 97571 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 96871 | 97572 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 96872 | 97573 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 96873 | 97574 | }; |
| 96874 | | - int aEndOp[] = { |
| 97575 | + static const u8 aEndOp[] = { |
| 96875 | 97576 | OP_Noop, /* 0: (!end_constraints) */ |
| 96876 | 97577 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 96877 | 97578 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 96878 | 97579 | }; |
| 96879 | | - int nEq = pLevel->plan.nEq; |
| 97580 | + int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ |
| 96880 | 97581 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
| 96881 | 97582 | int regBase; /* Base register holding constraint values */ |
| 96882 | 97583 | int r1; /* Temp register */ |
| 96883 | 97584 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 96884 | 97585 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 96885 | 97586 | int startEq; /* True if range start uses ==, >= or <= */ |
| 96886 | 97587 | int endEq; /* True if range end uses ==, >= or <= */ |
| 96887 | 97588 | int start_constraints; /* Start of range is constrained */ |
| 96888 | 97589 | 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 */ |
| 97590 | + Index *pIdx; /* The index we will be using */ |
| 97591 | + int iIdxCur; /* The VDBE cursor for the index */ |
| 97592 | + int nExtraReg = 0; /* Number of extra registers needed */ |
| 97593 | + int op; /* Instruction opcode */ |
| 96893 | 97594 | char *zStartAff; /* Affinity for start of range constraint */ |
| 96894 | 97595 | char *zEndAff; /* Affinity for end of range constraint */ |
| 96895 | 97596 | |
| 96896 | 97597 | pIdx = pLevel->plan.u.pIdx; |
| 96897 | 97598 | iIdxCur = pLevel->iIdxCur; |
| | @@ -108720,11 +109421,11 @@ |
| 108720 | 109421 | ** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following |
| 108721 | 109422 | ** two forward declarations are for functions declared in these files |
| 108722 | 109423 | ** used to retrieve the respective implementations. |
| 108723 | 109424 | ** |
| 108724 | 109425 | ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed |
| 108725 | | -** to by the argument to point a the "simple" tokenizer implementation. |
| 109426 | +** to by the argument to point to the "simple" tokenizer implementation. |
| 108726 | 109427 | ** Function ...PorterTokenizerModule() sets *pModule to point to the |
| 108727 | 109428 | ** porter tokenizer/stemmer implementation. |
| 108728 | 109429 | */ |
| 108729 | 109430 | SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| 108730 | 109431 | SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); |
| | @@ -108922,11 +109623,11 @@ |
| 108922 | 109623 | ** is defined to accept an argument of type char, and always returns 0 for |
| 108923 | 109624 | ** any values that fall outside of the range of the unsigned char type (i.e. |
| 108924 | 109625 | ** negative values). |
| 108925 | 109626 | */ |
| 108926 | 109627 | static int fts3isspace(char c){ |
| 108927 | | - return (c&0x80)==0 ? isspace(c) : 0; |
| 109628 | + return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; |
| 108928 | 109629 | } |
| 108929 | 109630 | |
| 108930 | 109631 | /* |
| 108931 | 109632 | ** Extract the next token from buffer z (length n) using the tokenizer |
| 108932 | 109633 | ** and other information (column names etc.) in pParse. Create an Fts3Expr |
| | @@ -111309,10 +112010,13 @@ |
| 111309 | 112010 | |
| 111310 | 112011 | |
| 111311 | 112012 | static int simpleDelim(simple_tokenizer *t, unsigned char c){ |
| 111312 | 112013 | return c<0x80 && t->delim[c]; |
| 111313 | 112014 | } |
| 112015 | +static int fts3_isalnum(int x){ |
| 112016 | + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); |
| 112017 | +} |
| 111314 | 112018 | |
| 111315 | 112019 | /* |
| 111316 | 112020 | ** Create a new tokenizer instance. |
| 111317 | 112021 | */ |
| 111318 | 112022 | static int simpleCreate( |
| | @@ -111343,11 +112047,11 @@ |
| 111343 | 112047 | } |
| 111344 | 112048 | } else { |
| 111345 | 112049 | /* Mark non-alphanumeric ASCII characters as delimiters */ |
| 111346 | 112050 | int i; |
| 111347 | 112051 | for(i=1; i<0x80; i++){ |
| 111348 | | - t->delim[i] = !isalnum(i) ? -1 : 0; |
| 112052 | + t->delim[i] = !fts3_isalnum(i) ? -1 : 0; |
| 111349 | 112053 | } |
| 111350 | 112054 | } |
| 111351 | 112055 | |
| 111352 | 112056 | *ppTokenizer = &t->base; |
| 111353 | 112057 | return SQLITE_OK; |
| | @@ -111449,11 +112153,11 @@ |
| 111449 | 112153 | for(i=0; i<n; i++){ |
| 111450 | 112154 | /* TODO(shess) This needs expansion to handle UTF-8 |
| 111451 | 112155 | ** case-insensitivity. |
| 111452 | 112156 | */ |
| 111453 | 112157 | unsigned char ch = p[iStartOffset+i]; |
| 111454 | | - c->pToken[i] = (char)(ch<0x80 ? tolower(ch) : ch); |
| 112158 | + c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch); |
| 111455 | 112159 | } |
| 111456 | 112160 | *ppToken = c->pToken; |
| 111457 | 112161 | *pnBytes = n; |
| 111458 | 112162 | *piStartOffset = iStartOffset; |
| 111459 | 112163 | *piEndOffset = c->iOffset; |
| | @@ -116355,15 +117059,14 @@ |
| 116355 | 117059 | ** least desirable): |
| 116356 | 117060 | ** |
| 116357 | 117061 | ** idxNum idxStr Strategy |
| 116358 | 117062 | ** ------------------------------------------------ |
| 116359 | 117063 | ** 1 Unused Direct lookup by rowid. |
| 116360 | | -** 2 See below R-tree query. |
| 116361 | | -** 3 Unused Full table scan. |
| 117064 | +** 2 See below R-tree query or full-table scan. |
| 116362 | 117065 | ** ------------------------------------------------ |
| 116363 | 117066 | ** |
| 116364 | | -** If strategy 1 or 3 is used, then idxStr is not meaningful. If strategy |
| 117067 | +** If strategy 1 is used, then idxStr is not meaningful. If strategy |
| 116365 | 117068 | ** 2 is used, idxStr is formatted to contain 2 bytes for each |
| 116366 | 117069 | ** constraint used. The first two bytes of idxStr correspond to |
| 116367 | 117070 | ** the constraint in sqlite3_index_info.aConstraintUsage[] with |
| 116368 | 117071 | ** (argvIndex==1) etc. |
| 116369 | 117072 | ** |
| 116370 | 117073 | |