Fossil SCM

Update to the latest version of SQLite - one that includes the fix for the corruption bug associated with the file-size in byte 28 of the database header. Also fix a bug in the graph generator, and change an index to help timeline run faster for branch queries.

drh 2010-06-17 01:37 trunk
Commit 5129d32a3719fdc98c10c7afa522dfef86aa26c4
4 files changed +5 -2 +1 -1 +6617 -1857 +176 -13
+5 -2
--- src/graph.c
+++ src/graph.c
@@ -289,18 +289,21 @@
289289
}else {
290290
if( pRow->iRail>=0 ) continue;
291291
}
292292
if( pRow->nParent==0 || hashFind(p,pRow->aParent[0])==0 ){
293293
if( omitDescenders ){
294
- pRow->iRail = findFreeRail(p, pRow->idx, pRow->idx, 0, 0);
294
+ pRow->iRail = findFreeRail(p, pRow->idx, pRow->idx, inUse, 0);
295295
}else{
296296
pRow->iRail = ++p->mxRail;
297297
}
298298
mask = 1<<(pRow->iRail);
299299
if( omitDescenders ){
300
- pRow->railInUse |= mask;
301300
if( pRow->pNext ) pRow->pNext->railInUse |= mask;
301
+ for(pDesc=pRow; pDesc; pDesc=pDesc->pPrev){
302
+ pDesc->railInUse |= mask;
303
+ if( pDesc->zBranch==pRow->zBranch && pDesc->isLeaf ) break;
304
+ }
302305
}else{
303306
pRow->bDescender = pRow->nParent>0;
304307
for(pDesc=pRow; pDesc; pDesc=pDesc->pNext){
305308
pDesc->railInUse |= mask;
306309
}
307310
--- src/graph.c
+++ src/graph.c
@@ -289,18 +289,21 @@
289 }else {
290 if( pRow->iRail>=0 ) continue;
291 }
292 if( pRow->nParent==0 || hashFind(p,pRow->aParent[0])==0 ){
293 if( omitDescenders ){
294 pRow->iRail = findFreeRail(p, pRow->idx, pRow->idx, 0, 0);
295 }else{
296 pRow->iRail = ++p->mxRail;
297 }
298 mask = 1<<(pRow->iRail);
299 if( omitDescenders ){
300 pRow->railInUse |= mask;
301 if( pRow->pNext ) pRow->pNext->railInUse |= mask;
 
 
 
 
302 }else{
303 pRow->bDescender = pRow->nParent>0;
304 for(pDesc=pRow; pDesc; pDesc=pDesc->pNext){
305 pDesc->railInUse |= mask;
306 }
307
--- src/graph.c
+++ src/graph.c
@@ -289,18 +289,21 @@
289 }else {
290 if( pRow->iRail>=0 ) continue;
291 }
292 if( pRow->nParent==0 || hashFind(p,pRow->aParent[0])==0 ){
293 if( omitDescenders ){
294 pRow->iRail = findFreeRail(p, pRow->idx, pRow->idx, inUse, 0);
295 }else{
296 pRow->iRail = ++p->mxRail;
297 }
298 mask = 1<<(pRow->iRail);
299 if( omitDescenders ){
 
300 if( pRow->pNext ) pRow->pNext->railInUse |= mask;
301 for(pDesc=pRow; pDesc; pDesc=pDesc->pPrev){
302 pDesc->railInUse |= mask;
303 if( pDesc->zBranch==pRow->zBranch && pDesc->isLeaf ) break;
304 }
305 }else{
306 pRow->bDescender = pRow->nParent>0;
307 for(pDesc=pRow; pDesc; pDesc=pDesc->pNext){
308 pDesc->railInUse |= mask;
309 }
310
+1 -1
--- src/schema.c
+++ src/schema.c
@@ -216,11 +216,11 @@
216216
@ cid INTEGER REFERENCES blob, -- Child manifest
217217
@ isprim BOOLEAN, -- pid is the primary parent of cid
218218
@ mtime DATETIME, -- the date/time stamp on cid
219219
@ UNIQUE(pid, cid)
220220
@ );
221
-@ CREATE INDEX plink_i2 ON plink(cid);
221
+@ CREATE INDEX plink_i2 ON plink(cid,pid);
222222
@
223223
@ -- Events used to generate a timeline
224224
@ --
225225
@ CREATE TABLE event(
226226
@ type TEXT, -- Type of event
227227
--- src/schema.c
+++ src/schema.c
@@ -216,11 +216,11 @@
216 @ cid INTEGER REFERENCES blob, -- Child manifest
217 @ isprim BOOLEAN, -- pid is the primary parent of cid
218 @ mtime DATETIME, -- the date/time stamp on cid
219 @ UNIQUE(pid, cid)
220 @ );
221 @ CREATE INDEX plink_i2 ON plink(cid);
222 @
223 @ -- Events used to generate a timeline
224 @ --
225 @ CREATE TABLE event(
226 @ type TEXT, -- Type of event
227
--- src/schema.c
+++ src/schema.c
@@ -216,11 +216,11 @@
216 @ cid INTEGER REFERENCES blob, -- Child manifest
217 @ isprim BOOLEAN, -- pid is the primary parent of cid
218 @ mtime DATETIME, -- the date/time stamp on cid
219 @ UNIQUE(pid, cid)
220 @ );
221 @ CREATE INDEX plink_i2 ON plink(cid,pid);
222 @
223 @ -- Events used to generate a timeline
224 @ --
225 @ CREATE TABLE event(
226 @ type TEXT, -- Type of event
227
+6617 -1857
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.6.23. By combining all the individual C code files into this
3
+** version 3.7.0. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a one translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% are more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -188,10 +188,18 @@
188188
#endif
189189
#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
190190
# define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500
191191
#endif
192192
193
+/*
194
+** The default number of frames to accumulate in the log file before
195
+** checkpointing the database in WAL mode.
196
+*/
197
+#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
198
+# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
199
+#endif
200
+
193201
/*
194202
** The maximum number of attached databases. This must be between 0
195203
** and 30. The upper bound on 30 is because a 32-bit integer bitmap
196204
** is used internally to track attached databases.
197205
*/
@@ -626,13 +634,13 @@
626634
**
627635
** See also: [sqlite3_libversion()],
628636
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
629637
** [sqlite_version()] and [sqlite_source_id()].
630638
*/
631
-#define SQLITE_VERSION "3.6.23"
632
-#define SQLITE_VERSION_NUMBER 3006023
633
-#define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
639
+#define SQLITE_VERSION "3.7.0"
640
+#define SQLITE_VERSION_NUMBER 3007000
641
+#define SQLITE_SOURCE_ID "2010-06-16 12:30:11 ad3209572d0e6afe5c8b52313e334509661045e2"
634642
635643
/*
636644
** CAPI3REF: Run-Time Library Version Numbers
637645
** KEYWORDS: sqlite3_version, sqlite3_sourceid
638646
**
@@ -912,11 +920,11 @@
912920
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
913921
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
914922
#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
915923
#define SQLITE_FULL 13 /* Insertion failed because database is full */
916924
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
917
-#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
925
+#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
918926
#define SQLITE_EMPTY 16 /* Database is empty */
919927
#define SQLITE_SCHEMA 17 /* The database schema changed */
920928
#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
921929
#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
922930
#define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -968,11 +976,16 @@
968976
#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
969977
#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
970978
#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
971979
#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
972980
#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
973
-#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
981
+#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
982
+#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
983
+#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
984
+#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
985
+#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
986
+#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
974987
975988
/*
976989
** CAPI3REF: Flags For File Open Operations
977990
**
978991
** These bit values are intended for use in the
@@ -1177,10 +1190,17 @@
11771190
int (*xUnlock)(sqlite3_file*, int);
11781191
int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
11791192
int (*xFileControl)(sqlite3_file*, int op, void *pArg);
11801193
int (*xSectorSize)(sqlite3_file*);
11811194
int (*xDeviceCharacteristics)(sqlite3_file*);
1195
+ /* Methods above are valid for version 1 */
1196
+ int (*xShmOpen)(sqlite3_file*);
1197
+ int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1198
+ int (*xShmMap)(sqlite3_file*, int iPage, int pgsz, int, void volatile**);
1199
+ void (*xShmBarrier)(sqlite3_file*);
1200
+ int (*xShmClose)(sqlite3_file*, int deleteFlag);
1201
+ /* Methods above are valid for version 2 */
11821202
/* Additional methods may be added in future releases */
11831203
};
11841204
11851205
/*
11861206
** CAPI3REF: Standard File Control Opcodes
@@ -1194,15 +1214,23 @@
11941214
** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
11951215
** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
11961216
** into an integer that the pArg argument points to. This capability
11971217
** is used during testing and only needs to be supported when SQLITE_TEST
11981218
** is defined.
1219
+**
1220
+** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1221
+** layer a hint of how large the database file will grow to be during the
1222
+** current transaction. This hint is not guaranteed to be accurate but it
1223
+** is often close. The underlying VFS might choose to preallocate database
1224
+** file space based on this hint in order to help writes to the database
1225
+** file run faster.
11991226
*/
12001227
#define SQLITE_FCNTL_LOCKSTATE 1
12011228
#define SQLITE_GET_LOCKPROXYFILE 2
12021229
#define SQLITE_SET_LOCKPROXYFILE 3
12031230
#define SQLITE_LAST_ERRNO 4
1231
+#define SQLITE_FCNTL_SIZE_HINT 5
12041232
12051233
/*
12061234
** CAPI3REF: Mutex Handle
12071235
**
12081236
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1343,11 +1371,11 @@
13431371
** method returns a Julian Day Number for the current date and time.
13441372
**
13451373
*/
13461374
typedef struct sqlite3_vfs sqlite3_vfs;
13471375
struct sqlite3_vfs {
1348
- int iVersion; /* Structure version number */
1376
+ int iVersion; /* Structure version number (currently 2) */
13491377
int szOsFile; /* Size of subclassed sqlite3_file */
13501378
int mxPathname; /* Maximum file pathname length */
13511379
sqlite3_vfs *pNext; /* Next registered VFS */
13521380
const char *zName; /* Name of this virtual file system */
13531381
void *pAppData; /* Pointer to application-specific data */
@@ -1362,12 +1390,21 @@
13621390
void (*xDlClose)(sqlite3_vfs*, void*);
13631391
int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
13641392
int (*xSleep)(sqlite3_vfs*, int microseconds);
13651393
int (*xCurrentTime)(sqlite3_vfs*, double*);
13661394
int (*xGetLastError)(sqlite3_vfs*, int, char *);
1367
- /* New fields may be appended in figure versions. The iVersion
1368
- ** value will increment whenever this happens. */
1395
+ /*
1396
+ ** The methods above are in version 1 of the sqlite_vfs object
1397
+ ** definition. Those that follow are added in version 2 or later
1398
+ */
1399
+ int (*xRename)(sqlite3_vfs*, const char *zOld, const char *zNew, int dirSync);
1400
+ int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1401
+ /*
1402
+ ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1403
+ ** New fields may be appended in figure versions. The iVersion
1404
+ ** value will increment whenever this happens.
1405
+ */
13691406
};
13701407
13711408
/*
13721409
** CAPI3REF: Flags for the xAccess VFS method
13731410
**
@@ -1382,10 +1419,48 @@
13821419
** checks whether the file is readable.
13831420
*/
13841421
#define SQLITE_ACCESS_EXISTS 0
13851422
#define SQLITE_ACCESS_READWRITE 1
13861423
#define SQLITE_ACCESS_READ 2
1424
+
1425
+/*
1426
+** CAPI3REF: Flags for the xShmLock VFS method
1427
+**
1428
+** These integer constants define the various locking operations
1429
+** allowed by the xShmLock method of [sqlite3_io_methods]. The
1430
+** following are the only legal combinations of flags to the
1431
+** xShmLock method:
1432
+**
1433
+** <ul>
1434
+** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1435
+** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1436
+** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1437
+** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1438
+** </ul>
1439
+**
1440
+** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1441
+** was given no the corresponding lock.
1442
+**
1443
+** The xShmLock method can transition between unlocked and SHARED or
1444
+** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1445
+** and EXCLUSIVE.
1446
+*/
1447
+#define SQLITE_SHM_UNLOCK 1
1448
+#define SQLITE_SHM_LOCK 2
1449
+#define SQLITE_SHM_SHARED 4
1450
+#define SQLITE_SHM_EXCLUSIVE 8
1451
+
1452
+/*
1453
+** CAPI3REF: Maximum xShmLock index
1454
+**
1455
+** The xShmLock method on [sqlite3_io_methods] may use values
1456
+** between 0 and this upper bound as its "offset" argument.
1457
+** The SQLite core will never attempt to acquire or release a
1458
+** lock outside of this range
1459
+*/
1460
+#define SQLITE_SHM_NLOCK 8
1461
+
13871462
13881463
/*
13891464
** CAPI3REF: Initialize The SQLite Library
13901465
**
13911466
** ^The sqlite3_initialize() routine initializes the
@@ -3409,10 +3484,18 @@
34093484
** already been [sqlite3_finalize | finalized] or on one that had
34103485
** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
34113486
** be the case that the same database connection is being used by two or
34123487
** more threads at the same moment in time.
34133488
**
3489
+** For all versions of SQLite up to and including 3.6.23.1, it was required
3490
+** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3491
+** [sqlite3_reset()] be called before any subsequent invocation of
3492
+** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3493
+** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3494
+** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3495
+** automatically in this circumstance rather than returning [SQLITE_MISUSE].
3496
+**
34143497
** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
34153498
** API always returns a generic error code, [SQLITE_ERROR], following any
34163499
** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
34173500
** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
34183501
** specific [error codes] that better describes the error.
@@ -5507,11 +5590,12 @@
55075590
#define SQLITE_TESTCTRL_ASSERT 12
55085591
#define SQLITE_TESTCTRL_ALWAYS 13
55095592
#define SQLITE_TESTCTRL_RESERVE 14
55105593
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
55115594
#define SQLITE_TESTCTRL_ISKEYWORD 16
5512
-#define SQLITE_TESTCTRL_LAST 16
5595
+#define SQLITE_TESTCTRL_PGHDRSZ 17
5596
+#define SQLITE_TESTCTRL_LAST 17
55135597
55145598
/*
55155599
** CAPI3REF: SQLite Runtime Status
55165600
**
55175601
** ^This interface is used to retrieve runtime status information
@@ -5971,14 +6055,18 @@
59716055
** then an [error code] is returned. ^As well as [SQLITE_OK] and
59726056
** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
59736057
** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
59746058
** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
59756059
**
5976
-** ^The sqlite3_backup_step() might return [SQLITE_READONLY] if the destination
5977
-** database was opened read-only or if
5978
-** the destination is an in-memory database with a different page size
5979
-** from the source database.
6060
+** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6061
+** <ol>
6062
+** <li> the destination database was opened read-only, or
6063
+** <li> the destination database is using write-ahead-log journaling
6064
+** and the destination and source page sizes differ, or
6065
+** <li> The destination database is an in-memory database and the
6066
+** destination and source page sizes differ.
6067
+** </ol>)^
59806068
**
59816069
** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
59826070
** the [sqlite3_busy_handler | busy-handler function]
59836071
** is invoked (if one is specified). ^If the
59846072
** busy-handler returns non-zero before the lock is available, then
@@ -6242,10 +6330,93 @@
62426330
** a few hundred characters, it will be truncated to the length of the
62436331
** buffer.
62446332
*/
62456333
SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
62466334
6335
+/*
6336
+** CAPI3REF: Write-Ahead Log Commit Hook
6337
+**
6338
+** ^The [sqlite3_wal_hook()] function is used to register a callback that
6339
+** will be invoked each time a database connection commits data to a
6340
+** [write-ahead log] (i.e. whenever a transaction is committed in
6341
+** [journal_mode | journal_mode=WAL mode]).
6342
+**
6343
+** ^The callback is invoked by SQLite after the commit has taken place and
6344
+** the associated write-lock on the database released, so the implementation
6345
+** may read, write or [checkpoint] the database as required.
6346
+**
6347
+** ^The first parameter passed to the callback function when it is invoked
6348
+** is a copy of the third parameter passed to sqlite3_wal_hook() when
6349
+** registering the callback. ^The second is a copy of the database handle.
6350
+** ^The third parameter is the name of the database that was written to -
6351
+** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
6352
+** is the number of pages currently in the write-ahead log file,
6353
+** including those that were just committed.
6354
+**
6355
+** The callback function should normally return [SQLITE_OK]. ^If an error
6356
+** code is returned, that error will propagate back up through the
6357
+** SQLite code base to cause the statement that provoked the callback
6358
+** to report an error, though the commit will have still occurred. If the
6359
+** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
6360
+** that does not correspond to any valid SQLite error code, the results
6361
+** are undefined.
6362
+**
6363
+** A single database handle may have at most a single write-ahead log callback
6364
+** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
6365
+** previously registered write-ahead log callback. ^Note that the
6366
+** [sqlite3_wal_autocheckpoint()] interface and the
6367
+** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
6368
+** those overwrite any prior [sqlite3_wal_hook()] settings.
6369
+*/
6370
+SQLITE_API void *sqlite3_wal_hook(
6371
+ sqlite3*,
6372
+ int(*)(void *,sqlite3*,const char*,int),
6373
+ void*
6374
+);
6375
+
6376
+/*
6377
+** CAPI3REF: Configure an auto-checkpoint
6378
+**
6379
+** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
6380
+** [sqlite3_wal_hook()] that causes any database on [database connection] D
6381
+** to automatically [checkpoint]
6382
+** after committing a transaction if there are N or
6383
+** more frames in the [write-ahead log] file. ^Passing zero or
6384
+** a negative value as the nFrame parameter disables automatic
6385
+** checkpoints entirely.
6386
+**
6387
+** ^The callback registered by this function replaces any existing callback
6388
+** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
6389
+** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
6390
+** configured by this function.
6391
+**
6392
+** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
6393
+** from SQL.
6394
+**
6395
+** ^Every new [database connection] defaults to having the auto-checkpoint
6396
+** enabled with a threshold of 1000 pages. The use of this interface
6397
+** is only necessary if the default setting is found to be suboptimal
6398
+** for a particular application.
6399
+*/
6400
+SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
6401
+
6402
+/*
6403
+** CAPI3REF: Checkpoint a database
6404
+**
6405
+** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
6406
+** on [database connection] D to be [checkpointed]. ^If X is NULL or an
6407
+** empty string, then a checkpoint is run on all databases of
6408
+** connection D. ^If the database connection D is not in
6409
+** [WAL | write-ahead log mode] then this interface is a harmless no-op.
6410
+**
6411
+** ^The [wal_checkpoint pragma] can be used to invoke this interface
6412
+** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6413
+** [wal_autocheckpoint pragma] can be used to cause this interface to be
6414
+** run whenever the WAL reaches a certain size threshold.
6415
+*/
6416
+SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6417
+
62476418
/*
62486419
** Undo the hack that converts floating point types to integer for
62496420
** builds on processors without floating point support.
62506421
*/
62516422
#ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7050,10 +7221,12 @@
70507221
70517222
SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
70527223
SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
70537224
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
70547225
7226
+SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
7227
+
70557228
#ifndef NDEBUG
70567229
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
70577230
#endif
70587231
70597232
#ifndef SQLITE_OMIT_BTREECOUNT
@@ -7396,31 +7569,31 @@
73967569
#define OP_IfPos 113
73977570
#define OP_IfNeg 114
73987571
#define OP_IfZero 115
73997572
#define OP_AggStep 116
74007573
#define OP_AggFinal 117
7401
-#define OP_Vacuum 118
7402
-#define OP_IncrVacuum 119
7403
-#define OP_Expire 120
7404
-#define OP_TableLock 121
7405
-#define OP_VBegin 122
7406
-#define OP_VCreate 123
7407
-#define OP_VDestroy 124
7408
-#define OP_VOpen 125
7409
-#define OP_VFilter 126
7410
-#define OP_VColumn 127
7411
-#define OP_VNext 128
7412
-#define OP_VRename 129
7413
-#define OP_VUpdate 131
7414
-#define OP_Pagecount 132
7415
-#define OP_Trace 133
7416
-#define OP_Noop 134
7417
-#define OP_Explain 135
7574
+#define OP_Checkpoint 118
7575
+#define OP_JournalMode 119
7576
+#define OP_Vacuum 120
7577
+#define OP_IncrVacuum 121
7578
+#define OP_Expire 122
7579
+#define OP_TableLock 123
7580
+#define OP_VBegin 124
7581
+#define OP_VCreate 125
7582
+#define OP_VDestroy 126
7583
+#define OP_VOpen 127
7584
+#define OP_VFilter 128
7585
+#define OP_VColumn 129
7586
+#define OP_VNext 131
7587
+#define OP_VRename 132
7588
+#define OP_VUpdate 133
7589
+#define OP_Pagecount 134
7590
+#define OP_Trace 135
7591
+#define OP_Noop 136
7592
+#define OP_Explain 137
74187593
74197594
/* The following opcode values are never used */
7420
-#define OP_NotUsed_136 136
7421
-#define OP_NotUsed_137 137
74227595
#define OP_NotUsed_138 138
74237596
#define OP_NotUsed_139 139
74247597
#define OP_NotUsed_140 140
74257598
74267599
@@ -7435,11 +7608,11 @@
74357608
#define OPFLG_IN3 0x0010 /* in3: P3 is an input */
74367609
#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
74377610
#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
74387611
#define OPFLG_INITIALIZER {\
74397612
/* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
7440
-/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24, 0x24,\
7613
+/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
74417614
/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
74427615
/* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
74437616
/* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
74447617
/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
74457618
/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
@@ -7448,13 +7621,13 @@
74487621
/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
74497622
/* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
74507623
/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
74517624
/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
74527625
/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
7453
-/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01,\
7454
-/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,\
7455
-/* 128 */ 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,\
7626
+/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
7627
+/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
7628
+/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x00,\
74567629
/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
74577630
/* 144 */ 0x04, 0x04,}
74587631
74597632
/************** End of opcodes.h *********************************************/
74607633
/************** Continuing where we left off in vdbe.h ***********************/
@@ -7596,18 +7769,19 @@
75967769
#define PAGER_LOCKINGMODE_QUERY -1
75977770
#define PAGER_LOCKINGMODE_NORMAL 0
75987771
#define PAGER_LOCKINGMODE_EXCLUSIVE 1
75997772
76007773
/*
7601
-** Valid values for the second argument to sqlite3PagerJournalMode().
7774
+** Numeric constants that encode the journalmode.
76027775
*/
7603
-#define PAGER_JOURNALMODE_QUERY -1
7776
+#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
76047777
#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
76057778
#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
76067779
#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
76077780
#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
76087781
#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
7782
+#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
76097783
76107784
/*
76117785
** The remainder of this file contains the declarations of the functions
76127786
** that make up the Pager sub-system API. See source code comments for
76137787
** a detailed description of each routine.
@@ -7631,11 +7805,13 @@
76317805
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
76327806
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
76337807
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
76347808
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
76357809
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7636
-SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
7810
+SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
7811
+SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
7812
+SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
76377813
SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
76387814
SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
76397815
76407816
/* Functions used to obtain and release page references. */
76417817
SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
@@ -7660,10 +7836,16 @@
76607836
SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
76617837
SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
76627838
SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
76637839
SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
76647840
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
7841
+
7842
+SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager);
7843
+SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
7844
+SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
7845
+SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
7846
+SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
76657847
76667848
/* Functions used to query pager state and configuration. */
76677849
SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
76687850
SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
76697851
SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
@@ -8077,11 +8259,15 @@
80778259
** the incompatibility right away, even running a full regression test.
80788260
** The default location of PENDING_BYTE is the first byte past the
80798261
** 1GB boundary.
80808262
**
80818263
*/
8082
-#define PENDING_BYTE sqlite3PendingByte
8264
+#ifdef SQLITE_OMIT_WSD
8265
+# define PENDING_BYTE (0x40000000)
8266
+#else
8267
+# define PENDING_BYTE sqlite3PendingByte
8268
+#endif
80838269
#define RESERVED_BYTE (PENDING_BYTE+1)
80848270
#define SHARED_FIRST (PENDING_BYTE+2)
80858271
#define SHARED_SIZE 510
80868272
80878273
/*
@@ -8103,10 +8289,15 @@
81038289
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
81048290
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
81058291
#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
81068292
SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
81078293
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
8294
+SQLITE_PRIVATE int sqlite3OsShmOpen(sqlite3_file *id);
8295
+SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
8296
+SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
8297
+SQLITE_PRIVATE int sqlite3OsShmClose(sqlite3_file *id, int);
8298
+SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
81088299
81098300
/*
81108301
** Functions for accessing sqlite3_vfs methods
81118302
*/
81128303
SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -8119,11 +8310,11 @@
81198310
SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
81208311
SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
81218312
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
81228313
SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
81238314
SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
8124
-SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
8315
+SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
81258316
81268317
/*
81278318
** Convenience functions for opening and closing files using
81288319
** sqlite3_malloc() to obtain space for the file-handle structure.
81298320
*/
@@ -8396,10 +8587,14 @@
83968587
int (*xCommitCallback)(void*); /* Invoked at every commit. */
83978588
void *pRollbackArg; /* Argument to xRollbackCallback() */
83988589
void (*xRollbackCallback)(void*); /* Invoked at every commit. */
83998590
void *pUpdateArg;
84008591
void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
8592
+#ifndef SQLITE_OMIT_WAL
8593
+ int (*xWalCallback)(void *, sqlite3 *, const char *, int);
8594
+ void *pWalArg;
8595
+#endif
84018596
void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
84028597
void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
84038598
void *pCollNeededArg;
84048599
sqlite3_value *pErr; /* Most recent error message */
84058600
char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
@@ -8486,10 +8681,11 @@
84868681
#define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
84878682
#define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
84888683
#define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
84898684
#define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
84908685
#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
8686
+#define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
84918687
84928688
/*
84938689
** Bits of the sqlite3.flags field that are used by the
84948690
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
84958691
** These must be the low-order bits of the flags field.
@@ -8497,11 +8693,12 @@
84978693
#define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
84988694
#define SQLITE_ColumnCache 0x02 /* Disable the column cache */
84998695
#define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
85008696
#define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
85018697
#define SQLITE_IndexCover 0x10 /* Disable index covering table */
8502
-#define SQLITE_OptMask 0x1f /* Mask of all disablable opts */
8698
+#define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */
8699
+#define SQLITE_OptMask 0xff /* Mask of all disablable opts */
85038700
85048701
/*
85058702
** Possible values for the sqlite.magic field.
85068703
** The numbers are obtained at random and have no special meaning, other
85078704
** than being distinct from one another.
@@ -10103,11 +10300,12 @@
1010310300
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
1010410301
#endif
1010510302
1010610303
1010710304
#ifndef SQLITE_MUTEX_OMIT
10108
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
10305
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
10306
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
1010910307
SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
1011010308
SQLITE_PRIVATE int sqlite3MutexInit(void);
1011110309
SQLITE_PRIVATE int sqlite3MutexEnd(void);
1011210310
#endif
1011310311
@@ -10235,10 +10433,11 @@
1023510433
SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
1023610434
SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
1023710435
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
1023810436
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
1023910437
SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
10438
+SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
1024010439
SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
1024110440
SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
1024210441
SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
1024310442
SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
1024410443
SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
@@ -10261,10 +10460,11 @@
1026110460
SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
1026210461
SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
1026310462
SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
1026410463
SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
1026510464
SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
10465
+SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
1026610466
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
1026710467
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
1026810468
SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
1026910469
SQLITE_PRIVATE void sqlite3PrngSaveState(void);
1027010470
SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
@@ -10450,15 +10650,17 @@
1045010650
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
1045110651
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
1045210652
SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
1045310653
SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
1045410654
SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
10655
+#ifndef SQLITE_OMIT_WSD
1045510656
SQLITE_PRIVATE int sqlite3PendingByte;
10657
+#endif
1045610658
#endif
1045710659
SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
1045810660
SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
10459
-SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3*);
10661
+SQLITE_PRIVATE void sqlite3AlterFunctions(void);
1046010662
SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
1046110663
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
1046210664
SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
1046310665
SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
1046410666
SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
@@ -10563,10 +10765,13 @@
1056310765
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
1056410766
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
1056510767
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
1056610768
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
1056710769
SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
10770
+SQLITE_PRIVATE const char *sqlite3JournalModename(int);
10771
+SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int);
10772
+SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
1056810773
1056910774
/* Declarations for functions in fkey.c. All of these are replaced by
1057010775
** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
1057110776
** key functionality is available. If OMIT_TRIGGER is defined but
1057210777
** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
@@ -10907,11 +11112,13 @@
1090711112
** IMPORTANT: Changing the pending byte to any value other than
1090811113
** 0x40000000 results in an incompatible database file format!
1090911114
** Changing the pending byte during operating results in undefined
1091011115
** and dileterious behavior.
1091111116
*/
11117
+#ifndef SQLITE_OMIT_WSD
1091211118
SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
11119
+#endif
1091311120
1091411121
/*
1091511122
** Properties of opcodes. The OPFLG_INITIALIZER macro is
1091611123
** created by mkopcodeh.awk during compilation. Data is obtained
1091711124
** from the comments following the "case OP_xxxx:" statements in
@@ -11764,14 +11971,12 @@
1176411971
1176511972
/*
1176611973
** Set the time to the current time reported by the VFS
1176711974
*/
1176811975
static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
11769
- double r;
1177011976
sqlite3 *db = sqlite3_context_db_handle(context);
11771
- sqlite3OsCurrentTime(db->pVfs, &r);
11772
- p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
11977
+ sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
1177311978
p->validJD = 1;
1177411979
}
1177511980
1177611981
/*
1177711982
** Attempt to parse the given string into a Julian Day Number. Return
@@ -12488,26 +12693,19 @@
1248812693
sqlite3_value **argv
1248912694
){
1249012695
time_t t;
1249112696
char *zFormat = (char *)sqlite3_user_data(context);
1249212697
sqlite3 *db;
12493
- double rT;
12698
+ sqlite3_int64 iT;
1249412699
char zBuf[20];
1249512700
1249612701
UNUSED_PARAMETER(argc);
1249712702
UNUSED_PARAMETER(argv);
1249812703
1249912704
db = sqlite3_context_db_handle(context);
12500
- sqlite3OsCurrentTime(db->pVfs, &rT);
12501
-#ifndef SQLITE_OMIT_FLOATING_POINT
12502
- t = 86400.0*(rT - 2440587.5) + 0.5;
12503
-#else
12504
- /* without floating point support, rT will have
12505
- ** already lost fractional day precision.
12506
- */
12507
- t = 86400 * (rT - 2440587) - 43200;
12508
-#endif
12705
+ sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
12706
+ t = iT/1000 - 10000*(sqlite3_int64)21086676;
1250912707
#ifdef HAVE_GMTIME_R
1251012708
{
1251112709
struct tm sNow;
1251212710
gmtime_r(&t, &sNow);
1251312711
strftime(zBuf, 20, zFormat, &sNow);
@@ -12656,10 +12854,31 @@
1265612854
return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
1265712855
}
1265812856
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
1265912857
return id->pMethods->xDeviceCharacteristics(id);
1266012858
}
12859
+SQLITE_PRIVATE int sqlite3OsShmOpen(sqlite3_file *id){
12860
+ return id->pMethods->xShmOpen(id);
12861
+}
12862
+SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
12863
+ return id->pMethods->xShmLock(id, offset, n, flags);
12864
+}
12865
+SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
12866
+ id->pMethods->xShmBarrier(id);
12867
+}
12868
+SQLITE_PRIVATE int sqlite3OsShmClose(sqlite3_file *id, int deleteFlag){
12869
+ return id->pMethods->xShmClose(id, deleteFlag);
12870
+}
12871
+SQLITE_PRIVATE int sqlite3OsShmMap(
12872
+ sqlite3_file *id,
12873
+ int iPage,
12874
+ int pgsz,
12875
+ int isWrite,
12876
+ void volatile **pp
12877
+){
12878
+ return id->pMethods->xShmMap(id, iPage, pgsz, isWrite, pp);
12879
+}
1266112880
1266212881
/*
1266312882
** The next group of routines are convenience wrappers around the
1266412883
** VFS methods.
1266512884
*/
@@ -12719,12 +12938,20 @@
1271912938
return pVfs->xRandomness(pVfs, nByte, zBufOut);
1272012939
}
1272112940
SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
1272212941
return pVfs->xSleep(pVfs, nMicro);
1272312942
}
12724
-SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
12725
- return pVfs->xCurrentTime(pVfs, pTimeOut);
12943
+SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
12944
+ int rc;
12945
+ if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
12946
+ rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
12947
+ }else{
12948
+ double r;
12949
+ rc = pVfs->xCurrentTime(pVfs, &r);
12950
+ *pTimeOut = (sqlite3_int64)(r*86400000.0);
12951
+ }
12952
+ return rc;
1272612953
}
1272712954
1272812955
SQLITE_PRIVATE int sqlite3OsOpenMalloc(
1272912956
sqlite3_vfs *pVfs,
1273012957
const char *zFile,
@@ -14974,27 +15201,30 @@
1497415201
/*
1497515202
** Initialize the mutex system.
1497615203
*/
1497715204
SQLITE_PRIVATE int sqlite3MutexInit(void){
1497815205
int rc = SQLITE_OK;
14979
- if( sqlite3GlobalConfig.bCoreMutex ){
14980
- if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
14981
- /* If the xMutexAlloc method has not been set, then the user did not
14982
- ** install a mutex implementation via sqlite3_config() prior to
14983
- ** sqlite3_initialize() being called. This block copies pointers to
14984
- ** the default implementation into the sqlite3GlobalConfig structure.
14985
- */
14986
- sqlite3_mutex_methods *pFrom = sqlite3DefaultMutex();
14987
- sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
14988
-
14989
- memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
14990
- memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
14991
- sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
14992
- pTo->xMutexAlloc = pFrom->xMutexAlloc;
14993
- }
14994
- rc = sqlite3GlobalConfig.mutex.xMutexInit();
14995
- }
15206
+ if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
15207
+ /* If the xMutexAlloc method has not been set, then the user did not
15208
+ ** install a mutex implementation via sqlite3_config() prior to
15209
+ ** sqlite3_initialize() being called. This block copies pointers to
15210
+ ** the default implementation into the sqlite3GlobalConfig structure.
15211
+ */
15212
+ sqlite3_mutex_methods const *pFrom;
15213
+ sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
15214
+
15215
+ if( sqlite3GlobalConfig.bCoreMutex ){
15216
+ pFrom = sqlite3DefaultMutex();
15217
+ }else{
15218
+ pFrom = sqlite3NoopMutex();
15219
+ }
15220
+ memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
15221
+ memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
15222
+ sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
15223
+ pTo->xMutexAlloc = pFrom->xMutexAlloc;
15224
+ }
15225
+ rc = sqlite3GlobalConfig.mutex.xMutexInit();
1499615226
1499715227
#ifdef SQLITE_DEBUG
1499815228
GLOBAL(int, mutexIsInit) = 1;
1499915229
#endif
1500015230
@@ -15122,68 +15352,69 @@
1512215352
** If compiled with SQLITE_DEBUG, then additional logic is inserted
1512315353
** that does error checking on mutexes to make sure they are being
1512415354
** called correctly.
1512515355
*/
1512615356
15357
+#ifndef SQLITE_MUTEX_OMIT
1512715358
15128
-#if defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG)
15359
+#ifndef SQLITE_DEBUG
1512915360
/*
1513015361
** Stub routines for all mutex methods.
1513115362
**
1513215363
** This routines provide no mutual exclusion or error checking.
1513315364
*/
15134
-static int noopMutexHeld(sqlite3_mutex *p){ return 1; }
15135
-static int noopMutexNotheld(sqlite3_mutex *p){ return 1; }
1513615365
static int noopMutexInit(void){ return SQLITE_OK; }
1513715366
static int noopMutexEnd(void){ return SQLITE_OK; }
1513815367
static sqlite3_mutex *noopMutexAlloc(int id){ return (sqlite3_mutex*)8; }
1513915368
static void noopMutexFree(sqlite3_mutex *p){ return; }
1514015369
static void noopMutexEnter(sqlite3_mutex *p){ return; }
1514115370
static int noopMutexTry(sqlite3_mutex *p){ return SQLITE_OK; }
1514215371
static void noopMutexLeave(sqlite3_mutex *p){ return; }
1514315372
15144
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15145
- static sqlite3_mutex_methods sMutex = {
15373
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
15374
+ static const sqlite3_mutex_methods sMutex = {
1514615375
noopMutexInit,
1514715376
noopMutexEnd,
1514815377
noopMutexAlloc,
1514915378
noopMutexFree,
1515015379
noopMutexEnter,
1515115380
noopMutexTry,
1515215381
noopMutexLeave,
1515315382
15154
- noopMutexHeld,
15155
- noopMutexNotheld
15383
+ 0,
15384
+ 0,
1515615385
};
1515715386
1515815387
return &sMutex;
1515915388
}
15160
-#endif /* defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG) */
15389
+#endif /* !SQLITE_DEBUG */
1516115390
15162
-#if defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG)
15391
+#ifdef SQLITE_DEBUG
1516315392
/*
1516415393
** In this implementation, error checking is provided for testing
1516515394
** and debugging purposes. The mutexes still do not provide any
1516615395
** mutual exclusion.
1516715396
*/
1516815397
1516915398
/*
1517015399
** The mutex object
1517115400
*/
15172
-struct sqlite3_mutex {
15401
+typedef struct sqlite3_debug_mutex {
1517315402
int id; /* The mutex type */
1517415403
int cnt; /* Number of entries without a matching leave */
15175
-};
15404
+} sqlite3_debug_mutex;
1517615405
1517715406
/*
1517815407
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1517915408
** intended for use inside assert() statements.
1518015409
*/
15181
-static int debugMutexHeld(sqlite3_mutex *p){
15410
+static int debugMutexHeld(sqlite3_mutex *pX){
15411
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
1518215412
return p==0 || p->cnt>0;
1518315413
}
15184
-static int debugMutexNotheld(sqlite3_mutex *p){
15414
+static int debugMutexNotheld(sqlite3_mutex *pX){
15415
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
1518515416
return p==0 || p->cnt==0;
1518615417
}
1518715418
1518815419
/*
1518915420
** Initialize and deinitialize the mutex subsystem.
@@ -15195,12 +15426,12 @@
1519515426
** The sqlite3_mutex_alloc() routine allocates a new
1519615427
** mutex and returns a pointer to it. If it returns NULL
1519715428
** that means that a mutex could not be allocated.
1519815429
*/
1519915430
static sqlite3_mutex *debugMutexAlloc(int id){
15200
- static sqlite3_mutex aStatic[6];
15201
- sqlite3_mutex *pNew = 0;
15431
+ static sqlite3_debug_mutex aStatic[6];
15432
+ sqlite3_debug_mutex *pNew = 0;
1520215433
switch( id ){
1520315434
case SQLITE_MUTEX_FAST:
1520415435
case SQLITE_MUTEX_RECURSIVE: {
1520515436
pNew = sqlite3Malloc(sizeof(*pNew));
1520615437
if( pNew ){
@@ -15215,17 +15446,18 @@
1521515446
pNew = &aStatic[id-2];
1521615447
pNew->id = id;
1521715448
break;
1521815449
}
1521915450
}
15220
- return pNew;
15451
+ return (sqlite3_mutex*)pNew;
1522115452
}
1522215453
1522315454
/*
1522415455
** This routine deallocates a previously allocated mutex.
1522515456
*/
15226
-static void debugMutexFree(sqlite3_mutex *p){
15457
+static void debugMutexFree(sqlite3_mutex *pX){
15458
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
1522715459
assert( p->cnt==0 );
1522815460
assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
1522915461
sqlite3_free(p);
1523015462
}
1523115463
@@ -15238,16 +15470,18 @@
1523815470
** be entered multiple times by the same thread. In such cases the,
1523915471
** mutex must be exited an equal number of times before another thread
1524015472
** can enter. If the same thread tries to enter any other kind of mutex
1524115473
** more than once, the behavior is undefined.
1524215474
*/
15243
-static void debugMutexEnter(sqlite3_mutex *p){
15244
- assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
15475
+static void debugMutexEnter(sqlite3_mutex *pX){
15476
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15477
+ assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
1524515478
p->cnt++;
1524615479
}
15247
-static int debugMutexTry(sqlite3_mutex *p){
15248
- assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
15480
+static int debugMutexTry(sqlite3_mutex *pX){
15481
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15482
+ assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
1524915483
p->cnt++;
1525015484
return SQLITE_OK;
1525115485
}
1525215486
1525315487
/*
@@ -15254,18 +15488,19 @@
1525415488
** The sqlite3_mutex_leave() routine exits a mutex that was
1525515489
** previously entered by the same thread. The behavior
1525615490
** is undefined if the mutex is not currently entered or
1525715491
** is not currently allocated. SQLite will never do either.
1525815492
*/
15259
-static void debugMutexLeave(sqlite3_mutex *p){
15260
- assert( debugMutexHeld(p) );
15493
+static void debugMutexLeave(sqlite3_mutex *pX){
15494
+ sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15495
+ assert( debugMutexHeld(pX) );
1526115496
p->cnt--;
15262
- assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
15497
+ assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
1526315498
}
1526415499
15265
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15266
- static sqlite3_mutex_methods sMutex = {
15500
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
15501
+ static const sqlite3_mutex_methods sMutex = {
1526715502
debugMutexInit,
1526815503
debugMutexEnd,
1526915504
debugMutexAlloc,
1527015505
debugMutexFree,
1527115506
debugMutexEnter,
@@ -15276,11 +15511,22 @@
1527615511
debugMutexNotheld
1527715512
};
1527815513
1527915514
return &sMutex;
1528015515
}
15281
-#endif /* defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG) */
15516
+#endif /* SQLITE_DEBUG */
15517
+
15518
+/*
15519
+** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
15520
+** is used regardless of the run-time threadsafety setting.
15521
+*/
15522
+#ifdef SQLITE_MUTEX_NOOP
15523
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
15524
+ return sqliteNoopMutex();
15525
+}
15526
+#endif /* SQLITE_MUTEX_NOOP */
15527
+#endif /* SQLITE_MUTEX_OMIT */
1528215528
1528315529
/************** End of mutex_noop.c ******************************************/
1528415530
/************** Begin file mutex_os2.c ***************************************/
1528515531
/*
1528615532
** 2007 August 28
@@ -15532,12 +15778,12 @@
1553215778
p->nRef--;
1553315779
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1553415780
DosReleaseMutexSem(p->mutex);
1553515781
}
1553615782
15537
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15538
- static sqlite3_mutex_methods sMutex = {
15783
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
15784
+ static const sqlite3_mutex_methods sMutex = {
1553915785
os2MutexInit,
1554015786
os2MutexEnd,
1554115787
os2MutexAlloc,
1554215788
os2MutexFree,
1554315789
os2MutexEnter,
@@ -15578,27 +15824,37 @@
1557815824
*/
1557915825
#ifdef SQLITE_MUTEX_PTHREADS
1558015826
1558115827
#include <pthread.h>
1558215828
15829
+/*
15830
+** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
15831
+** are necessary under two condidtions: (1) Debug builds and (2) using
15832
+** home-grown mutexes. Encapsulate these conditions into a single #define.
15833
+*/
15834
+#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
15835
+# define SQLITE_MUTEX_NREF 1
15836
+#else
15837
+# define SQLITE_MUTEX_NREF 0
15838
+#endif
1558315839
1558415840
/*
1558515841
** Each recursive mutex is an instance of the following structure.
1558615842
*/
1558715843
struct sqlite3_mutex {
1558815844
pthread_mutex_t mutex; /* Mutex controlling the lock */
15845
+#if SQLITE_MUTEX_NREF
1558915846
int id; /* Mutex type */
15590
- int nRef; /* Number of entrances */
15591
- pthread_t owner; /* Thread that is within this mutex */
15592
-#ifdef SQLITE_DEBUG
15847
+ volatile int nRef; /* Number of entrances */
15848
+ volatile pthread_t owner; /* Thread that is within this mutex */
1559315849
int trace; /* True to trace changes */
1559415850
#endif
1559515851
};
15596
-#ifdef SQLITE_DEBUG
15852
+#if SQLITE_MUTEX_NREF
1559715853
#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
1559815854
#else
15599
-#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
15855
+#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
1560015856
#endif
1560115857
1560215858
/*
1560315859
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
1560415860
** intended for use only inside assert() statements. On some platforms,
@@ -15696,27 +15952,33 @@
1569615952
pthread_mutexattr_init(&recursiveAttr);
1569715953
pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
1569815954
pthread_mutex_init(&p->mutex, &recursiveAttr);
1569915955
pthread_mutexattr_destroy(&recursiveAttr);
1570015956
#endif
15957
+#if SQLITE_MUTEX_NREF
1570115958
p->id = iType;
15959
+#endif
1570215960
}
1570315961
break;
1570415962
}
1570515963
case SQLITE_MUTEX_FAST: {
1570615964
p = sqlite3MallocZero( sizeof(*p) );
1570715965
if( p ){
15966
+#if SQLITE_MUTEX_NREF
1570815967
p->id = iType;
15968
+#endif
1570915969
pthread_mutex_init(&p->mutex, 0);
1571015970
}
1571115971
break;
1571215972
}
1571315973
default: {
1571415974
assert( iType-2 >= 0 );
1571515975
assert( iType-2 < ArraySize(staticMutexes) );
1571615976
p = &staticMutexes[iType-2];
15977
+#if SQLITE_MUTEX_NREF
1571715978
p->id = iType;
15979
+#endif
1571815980
break;
1571915981
}
1572015982
}
1572115983
return p;
1572215984
}
@@ -15772,12 +16034,14 @@
1577216034
}
1577316035
#else
1577416036
/* Use the built-in recursive mutexes if they are available.
1577516037
*/
1577616038
pthread_mutex_lock(&p->mutex);
16039
+#if SQLITE_MUTEX_NREF
1577716040
p->owner = pthread_self();
1577816041
p->nRef++;
16042
+#endif
1577916043
#endif
1578016044
1578116045
#ifdef SQLITE_DEBUG
1578216046
if( p->trace ){
1578316047
printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
@@ -15815,12 +16079,14 @@
1581516079
}
1581616080
#else
1581716081
/* Use the built-in recursive mutexes if they are available.
1581816082
*/
1581916083
if( pthread_mutex_trylock(&p->mutex)==0 ){
16084
+#if SQLITE_MUTEX_NREF
1582016085
p->owner = pthread_self();
1582116086
p->nRef++;
16087
+#endif
1582216088
rc = SQLITE_OK;
1582316089
}else{
1582416090
rc = SQLITE_BUSY;
1582516091
}
1582616092
#endif
@@ -15839,11 +16105,13 @@
1583916105
** is undefined if the mutex is not currently entered or
1584016106
** is not currently allocated. SQLite will never do either.
1584116107
*/
1584216108
static void pthreadMutexLeave(sqlite3_mutex *p){
1584316109
assert( pthreadMutexHeld(p) );
16110
+#if SQLITE_MUTEX_NREF
1584416111
p->nRef--;
16112
+#endif
1584516113
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
1584616114
1584716115
#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
1584816116
if( p->nRef==0 ){
1584916117
pthread_mutex_unlock(&p->mutex);
@@ -15857,12 +16125,12 @@
1585716125
printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
1585816126
}
1585916127
#endif
1586016128
}
1586116129
15862
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15863
- static sqlite3_mutex_methods sMutex = {
16130
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16131
+ static const sqlite3_mutex_methods sMutex = {
1586416132
pthreadMutexInit,
1586516133
pthreadMutexEnd,
1586616134
pthreadMutexAlloc,
1586716135
pthreadMutexFree,
1586816136
pthreadMutexEnter,
@@ -15908,21 +16176,21 @@
1590816176
** Each recursive mutex is an instance of the following structure.
1590916177
*/
1591016178
struct sqlite3_mutex {
1591116179
CRITICAL_SECTION mutex; /* Mutex controlling the lock */
1591216180
int id; /* Mutex type */
15913
- int nRef; /* Number of enterances */
15914
- DWORD owner; /* Thread holding this mutex */
1591516181
#ifdef SQLITE_DEBUG
16182
+ volatile int nRef; /* Number of enterances */
16183
+ volatile DWORD owner; /* Thread holding this mutex */
1591616184
int trace; /* True to trace changes */
1591716185
#endif
1591816186
};
1591916187
#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
1592016188
#ifdef SQLITE_DEBUG
1592116189
#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
1592216190
#else
15923
-#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0 }
16191
+#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
1592416192
#endif
1592516193
1592616194
/*
1592716195
** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
1592816196
** or WinCE. Return false (zero) for Win95, Win98, or WinME.
@@ -16074,21 +16342,25 @@
1607416342
switch( iType ){
1607516343
case SQLITE_MUTEX_FAST:
1607616344
case SQLITE_MUTEX_RECURSIVE: {
1607716345
p = sqlite3MallocZero( sizeof(*p) );
1607816346
if( p ){
16347
+#ifdef SQLITE_DEBUG
1607916348
p->id = iType;
16349
+#endif
1608016350
InitializeCriticalSection(&p->mutex);
1608116351
}
1608216352
break;
1608316353
}
1608416354
default: {
1608516355
assert( winMutex_isInit==1 );
1608616356
assert( iType-2 >= 0 );
1608716357
assert( iType-2 < ArraySize(winMutex_staticMutexes) );
1608816358
p = &winMutex_staticMutexes[iType-2];
16359
+#ifdef SQLITE_DEBUG
1608916360
p->id = iType;
16361
+#endif
1609016362
break;
1609116363
}
1609216364
}
1609316365
return p;
1609416366
}
@@ -16117,16 +16389,18 @@
1611716389
** mutex must be exited an equal number of times before another thread
1611816390
** can enter. If the same thread tries to enter any other kind of mutex
1611916391
** more than once, the behavior is undefined.
1612016392
*/
1612116393
static void winMutexEnter(sqlite3_mutex *p){
16394
+#ifdef SQLITE_DEBUG
1612216395
DWORD tid = GetCurrentThreadId();
1612316396
assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
16397
+#endif
1612416398
EnterCriticalSection(&p->mutex);
16399
+#ifdef SQLITE_DEBUG
1612516400
p->owner = tid;
1612616401
p->nRef++;
16127
-#ifdef SQLITE_DEBUG
1612816402
if( p->trace ){
1612916403
printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
1613016404
}
1613116405
#endif
1613216406
}
@@ -16171,25 +16445,25 @@
1617116445
** is not currently allocated. SQLite will never do either.
1617216446
*/
1617316447
static void winMutexLeave(sqlite3_mutex *p){
1617416448
#ifndef NDEBUG
1617516449
DWORD tid = GetCurrentThreadId();
16176
-#endif
1617716450
assert( p->nRef>0 );
1617816451
assert( p->owner==tid );
1617916452
p->nRef--;
1618016453
assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
16454
+#endif
1618116455
LeaveCriticalSection(&p->mutex);
1618216456
#ifdef SQLITE_DEBUG
1618316457
if( p->trace ){
1618416458
printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
1618516459
}
1618616460
#endif
1618716461
}
1618816462
16189
-SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
16190
- static sqlite3_mutex_methods sMutex = {
16463
+SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16464
+ static const sqlite3_mutex_methods sMutex = {
1619116465
winMutexInit,
1619216466
winMutexEnd,
1619316467
winMutexAlloc,
1619416468
winMutexFree,
1619516469
winMutexEnter,
@@ -20557,30 +20831,30 @@
2055720831
/* 113 */ "IfPos",
2055820832
/* 114 */ "IfNeg",
2055920833
/* 115 */ "IfZero",
2056020834
/* 116 */ "AggStep",
2056120835
/* 117 */ "AggFinal",
20562
- /* 118 */ "Vacuum",
20563
- /* 119 */ "IncrVacuum",
20564
- /* 120 */ "Expire",
20565
- /* 121 */ "TableLock",
20566
- /* 122 */ "VBegin",
20567
- /* 123 */ "VCreate",
20568
- /* 124 */ "VDestroy",
20569
- /* 125 */ "VOpen",
20570
- /* 126 */ "VFilter",
20571
- /* 127 */ "VColumn",
20572
- /* 128 */ "VNext",
20573
- /* 129 */ "VRename",
20836
+ /* 118 */ "Checkpoint",
20837
+ /* 119 */ "JournalMode",
20838
+ /* 120 */ "Vacuum",
20839
+ /* 121 */ "IncrVacuum",
20840
+ /* 122 */ "Expire",
20841
+ /* 123 */ "TableLock",
20842
+ /* 124 */ "VBegin",
20843
+ /* 125 */ "VCreate",
20844
+ /* 126 */ "VDestroy",
20845
+ /* 127 */ "VOpen",
20846
+ /* 128 */ "VFilter",
20847
+ /* 129 */ "VColumn",
2057420848
/* 130 */ "Real",
20575
- /* 131 */ "VUpdate",
20576
- /* 132 */ "Pagecount",
20577
- /* 133 */ "Trace",
20578
- /* 134 */ "Noop",
20579
- /* 135 */ "Explain",
20580
- /* 136 */ "NotUsed_136",
20581
- /* 137 */ "NotUsed_137",
20849
+ /* 131 */ "VNext",
20850
+ /* 132 */ "VRename",
20851
+ /* 133 */ "VUpdate",
20852
+ /* 134 */ "Pagecount",
20853
+ /* 135 */ "Trace",
20854
+ /* 136 */ "Noop",
20855
+ /* 137 */ "Explain",
2058220856
/* 138 */ "NotUsed_138",
2058320857
/* 139 */ "NotUsed_139",
2058420858
/* 140 */ "NotUsed_140",
2058520859
/* 141 */ "ToText",
2058620860
/* 142 */ "ToBlob",
@@ -20681,27 +20955,13 @@
2068120955
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2068220956
#endif
2068320957
2068420958
#ifdef SQLITE_DEBUG
2068520959
SQLITE_PRIVATE int sqlite3OSTrace = 0;
20686
-#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
20687
-#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
20688
-#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
20689
-#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
20690
-#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
20691
-#define OSTRACE6(X,Y,Z,A,B,C) \
20692
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
20693
-#define OSTRACE7(X,Y,Z,A,B,C,D) \
20694
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
20960
+#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
2069520961
#else
20696
-#define OSTRACE1(X)
20697
-#define OSTRACE2(X,Y)
20698
-#define OSTRACE3(X,Y,Z)
20699
-#define OSTRACE4(X,Y,Z,A)
20700
-#define OSTRACE5(X,Y,Z,A,B)
20701
-#define OSTRACE6(X,Y,Z,A,B,C)
20702
-#define OSTRACE7(X,Y,Z,A,B,C,D)
20962
+#define OSTRACE(X)
2070320963
#endif
2070420964
2070520965
/*
2070620966
** Macros for performance tracing. Normally turned off. Only works
2070720967
** on i486 hardware.
@@ -20893,11 +21153,11 @@
2089321153
*/
2089421154
static int os2Close( sqlite3_file *id ){
2089521155
APIRET rc = NO_ERROR;
2089621156
os2File *pFile;
2089721157
if( id && (pFile = (os2File*)id) != 0 ){
20898
- OSTRACE2( "CLOSE %d\n", pFile->h );
21158
+ OSTRACE(( "CLOSE %d\n", pFile->h ));
2089921159
rc = DosClose( pFile->h );
2090021160
pFile->locktype = NO_LOCK;
2090121161
if( pFile->pathToDel != NULL ){
2090221162
rc = DosForceDelete( (PSZ)pFile->pathToDel );
2090321163
free( pFile->pathToDel );
@@ -20924,11 +21184,11 @@
2092421184
ULONG fileLocation = 0L;
2092521185
ULONG got;
2092621186
os2File *pFile = (os2File*)id;
2092721187
assert( id!=0 );
2092821188
SimulateIOError( return SQLITE_IOERR_READ );
20929
- OSTRACE3( "READ %d lock=%d\n", pFile->h, pFile->locktype );
21189
+ OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
2093021190
if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
2093121191
return SQLITE_IOERR;
2093221192
}
2093321193
if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
2093421194
return SQLITE_IOERR_READ;
@@ -20957,11 +21217,11 @@
2095721217
ULONG wrote;
2095821218
os2File *pFile = (os2File*)id;
2095921219
assert( id!=0 );
2096021220
SimulateIOError( return SQLITE_IOERR_WRITE );
2096121221
SimulateDiskfullError( return SQLITE_FULL );
20962
- OSTRACE3( "WRITE %d lock=%d\n", pFile->h, pFile->locktype );
21222
+ OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
2096321223
if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
2096421224
return SQLITE_IOERR;
2096521225
}
2096621226
assert( amt>0 );
2096721227
while( amt > 0 &&
@@ -20979,11 +21239,11 @@
2097921239
** Truncate an open file to a specified size
2098021240
*/
2098121241
static int os2Truncate( sqlite3_file *id, i64 nByte ){
2098221242
APIRET rc = NO_ERROR;
2098321243
os2File *pFile = (os2File*)id;
20984
- OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte );
21244
+ OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
2098521245
SimulateIOError( return SQLITE_IOERR_TRUNCATE );
2098621246
rc = DosSetFileSize( pFile->h, nByte );
2098721247
return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
2098821248
}
2098921249
@@ -20999,11 +21259,11 @@
2099921259
/*
2100021260
** Make sure all writes to a particular file are committed to disk.
2100121261
*/
2100221262
static int os2Sync( sqlite3_file *id, int flags ){
2100321263
os2File *pFile = (os2File*)id;
21004
- OSTRACE3( "SYNC %d lock=%d\n", pFile->h, pFile->locktype );
21264
+ OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
2100521265
#ifdef SQLITE_TEST
2100621266
if( flags & SQLITE_SYNC_FULL){
2100721267
sqlite3_fullsync_count++;
2100821268
}
2100921269
sqlite3_sync_count++;
@@ -21049,11 +21309,11 @@
2104921309
LockArea.lOffset = SHARED_FIRST;
2105021310
LockArea.lRange = SHARED_SIZE;
2105121311
UnlockArea.lOffset = 0L;
2105221312
UnlockArea.lRange = 0L;
2105321313
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21054
- OSTRACE3( "GETREADLOCK %d res=%d\n", pFile->h, res );
21314
+ OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
2105521315
return res;
2105621316
}
2105721317
2105821318
/*
2105921319
** Undo a readlock
@@ -21067,11 +21327,11 @@
2106721327
LockArea.lOffset = 0L;
2106821328
LockArea.lRange = 0L;
2106921329
UnlockArea.lOffset = SHARED_FIRST;
2107021330
UnlockArea.lRange = SHARED_SIZE;
2107121331
res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21072
- OSTRACE3( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res );
21332
+ OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
2107321333
return res;
2107421334
}
2107521335
2107621336
/*
2107721337
** Lock the file with the lock specified by parameter locktype - one
@@ -21108,18 +21368,18 @@
2110821368
UnlockArea;
2110921369
os2File *pFile = (os2File*)id;
2111021370
memset(&LockArea, 0, sizeof(LockArea));
2111121371
memset(&UnlockArea, 0, sizeof(UnlockArea));
2111221372
assert( pFile!=0 );
21113
- OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
21373
+ OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
2111421374
2111521375
/* If there is already a lock of this type or more restrictive on the
2111621376
** os2File, do nothing. Don't use the end_lock: exit path, as
2111721377
** sqlite3_mutex_enter() hasn't been called yet.
2111821378
*/
2111921379
if( pFile->locktype>=locktype ){
21120
- OSTRACE3( "LOCK %d %d ok (already held)\n", pFile->h, locktype );
21380
+ OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
2112121381
return SQLITE_OK;
2112221382
}
2112321383
2112421384
/* Make sure the locking sequence is correct
2112521385
*/
@@ -21142,11 +21402,11 @@
2114221402
2114321403
/* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
2114421404
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
2114521405
if( res == NO_ERROR ){
2114621406
gotPendingLock = 1;
21147
- OSTRACE3( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res );
21407
+ OSTRACE(( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res ));
2114821408
}
2114921409
}
2115021410
2115121411
/* Acquire a shared lock
2115221412
*/
@@ -21154,11 +21414,11 @@
2115421414
assert( pFile->locktype==NO_LOCK );
2115521415
res = getReadLock(pFile);
2115621416
if( res == NO_ERROR ){
2115721417
newLocktype = SHARED_LOCK;
2115821418
}
21159
- OSTRACE3( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res );
21419
+ OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
2116021420
}
2116121421
2116221422
/* Acquire a RESERVED lock
2116321423
*/
2116421424
if( locktype==RESERVED_LOCK && res == NO_ERROR ){
@@ -21169,39 +21429,40 @@
2116921429
UnlockArea.lRange = 0L;
2117021430
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
2117121431
if( res == NO_ERROR ){
2117221432
newLocktype = RESERVED_LOCK;
2117321433
}
21174
- OSTRACE3( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res );
21434
+ OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
2117521435
}
2117621436
2117721437
/* Acquire a PENDING lock
2117821438
*/
2117921439
if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
2118021440
newLocktype = PENDING_LOCK;
2118121441
gotPendingLock = 0;
21182
- OSTRACE2( "LOCK %d acquire pending lock. pending lock boolean unset.\n", pFile->h );
21442
+ OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
21443
+ pFile->h ));
2118321444
}
2118421445
2118521446
/* Acquire an EXCLUSIVE lock
2118621447
*/
2118721448
if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
2118821449
assert( pFile->locktype>=SHARED_LOCK );
2118921450
res = unlockReadLock(pFile);
21190
- OSTRACE2( "unreadlock = %d\n", res );
21451
+ OSTRACE(( "unreadlock = %d\n", res ));
2119121452
LockArea.lOffset = SHARED_FIRST;
2119221453
LockArea.lRange = SHARED_SIZE;
2119321454
UnlockArea.lOffset = 0L;
2119421455
UnlockArea.lRange = 0L;
2119521456
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
2119621457
if( res == NO_ERROR ){
2119721458
newLocktype = EXCLUSIVE_LOCK;
2119821459
}else{
21199
- OSTRACE2( "OS/2 error-code = %d\n", res );
21460
+ OSTRACE(( "OS/2 error-code = %d\n", res ));
2120021461
getReadLock(pFile);
2120121462
}
21202
- OSTRACE3( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res );
21463
+ OSTRACE(( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res ));
2120321464
}
2120421465
2120521466
/* If we are holding a PENDING lock that ought to be released, then
2120621467
** release it now.
2120721468
*/
@@ -21210,25 +21471,25 @@
2121021471
LockArea.lOffset = 0L;
2121121472
LockArea.lRange = 0L;
2121221473
UnlockArea.lOffset = PENDING_BYTE;
2121321474
UnlockArea.lRange = 1L;
2121421475
r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21215
- OSTRACE3( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r );
21476
+ OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
2121621477
}
2121721478
2121821479
/* Update the state of the lock has held in the file descriptor then
2121921480
** return the appropriate result code.
2122021481
*/
2122121482
if( res == NO_ERROR ){
2122221483
rc = SQLITE_OK;
2122321484
}else{
21224
- OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
21225
- locktype, newLocktype );
21485
+ OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
21486
+ locktype, newLocktype ));
2122621487
rc = SQLITE_BUSY;
2122721488
}
2122821489
pFile->locktype = newLocktype;
21229
- OSTRACE3( "LOCK %d now %d\n", pFile->h, pFile->locktype );
21490
+ OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
2123021491
return rc;
2123121492
}
2123221493
2123321494
/*
2123421495
** This routine checks if there is a RESERVED lock held on the specified
@@ -21239,11 +21500,11 @@
2123921500
int r = 0;
2124021501
os2File *pFile = (os2File*)id;
2124121502
assert( pFile!=0 );
2124221503
if( pFile->locktype>=RESERVED_LOCK ){
2124321504
r = 1;
21244
- OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, r );
21505
+ OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
2124521506
}else{
2124621507
FILELOCK LockArea,
2124721508
UnlockArea;
2124821509
APIRET rc = NO_ERROR;
2124921510
memset(&LockArea, 0, sizeof(LockArea));
@@ -21251,22 +21512,22 @@
2125121512
LockArea.lOffset = RESERVED_BYTE;
2125221513
LockArea.lRange = 1L;
2125321514
UnlockArea.lOffset = 0L;
2125421515
UnlockArea.lRange = 0L;
2125521516
rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21256
- OSTRACE3( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc );
21517
+ OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
2125721518
if( rc == NO_ERROR ){
2125821519
APIRET rcu = NO_ERROR; /* return code for unlocking */
2125921520
LockArea.lOffset = 0L;
2126021521
LockArea.lRange = 0L;
2126121522
UnlockArea.lOffset = RESERVED_BYTE;
2126221523
UnlockArea.lRange = 1L;
2126321524
rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21264
- OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu );
21525
+ OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
2126521526
}
2126621527
r = !(rc == NO_ERROR);
21267
- OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r );
21528
+ OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
2126821529
}
2126921530
*pOut = r;
2127021531
return SQLITE_OK;
2127121532
}
2127221533
@@ -21290,48 +21551,49 @@
2129021551
UnlockArea;
2129121552
memset(&LockArea, 0, sizeof(LockArea));
2129221553
memset(&UnlockArea, 0, sizeof(UnlockArea));
2129321554
assert( pFile!=0 );
2129421555
assert( locktype<=SHARED_LOCK );
21295
- OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
21556
+ OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
2129621557
type = pFile->locktype;
2129721558
if( type>=EXCLUSIVE_LOCK ){
2129821559
LockArea.lOffset = 0L;
2129921560
LockArea.lRange = 0L;
2130021561
UnlockArea.lOffset = SHARED_FIRST;
2130121562
UnlockArea.lRange = SHARED_SIZE;
2130221563
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21303
- OSTRACE3( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res );
21564
+ OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
2130421565
if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
2130521566
/* This should never happen. We should always be able to
2130621567
** reacquire the read lock */
21307
- OSTRACE3( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype );
21568
+ OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
2130821569
rc = SQLITE_IOERR_UNLOCK;
2130921570
}
2131021571
}
2131121572
if( type>=RESERVED_LOCK ){
2131221573
LockArea.lOffset = 0L;
2131321574
LockArea.lRange = 0L;
2131421575
UnlockArea.lOffset = RESERVED_BYTE;
2131521576
UnlockArea.lRange = 1L;
2131621577
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21317
- OSTRACE3( "UNLOCK %d reserved res=%d\n", pFile->h, res );
21578
+ OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
2131821579
}
2131921580
if( locktype==NO_LOCK && type>=SHARED_LOCK ){
2132021581
res = unlockReadLock(pFile);
21321
- OSTRACE5( "UNLOCK %d is %d want %d res=%d\n", pFile->h, type, locktype, res );
21582
+ OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
21583
+ pFile->h, type, locktype, res ));
2132221584
}
2132321585
if( type>=PENDING_LOCK ){
2132421586
LockArea.lOffset = 0L;
2132521587
LockArea.lRange = 0L;
2132621588
UnlockArea.lOffset = PENDING_BYTE;
2132721589
UnlockArea.lRange = 1L;
2132821590
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21329
- OSTRACE3( "UNLOCK %d pending res=%d\n", pFile->h, res );
21591
+ OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
2133021592
}
2133121593
pFile->locktype = locktype;
21332
- OSTRACE3( "UNLOCK %d now %d\n", pFile->h, pFile->locktype );
21594
+ OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
2133321595
return rc;
2133421596
}
2133521597
2133621598
/*
2133721599
** Control and query of the open file handle.
@@ -21338,11 +21600,12 @@
2133821600
*/
2133921601
static int os2FileControl(sqlite3_file *id, int op, void *pArg){
2134021602
switch( op ){
2134121603
case SQLITE_FCNTL_LOCKSTATE: {
2134221604
*(int*)pArg = ((os2File*)id)->locktype;
21343
- OSTRACE3( "FCNTL_LOCKSTATE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
21605
+ OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
21606
+ ((os2File*)id)->h, ((os2File*)id)->locktype ));
2134421607
return SQLITE_OK;
2134521608
}
2134621609
}
2134721610
return SQLITE_ERROR;
2134821611
}
@@ -21525,11 +21788,11 @@
2152521788
sqlite3_randomness( 20, &zBuf[j] );
2152621789
for( i = 0; i < 20; i++, j++ ){
2152721790
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2152821791
}
2152921792
zBuf[j] = 0;
21530
- OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
21793
+ OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
2153121794
return SQLITE_OK;
2153221795
}
2153321796
2153421797
2153521798
/*
@@ -21588,47 +21851,47 @@
2158821851
}
2158921852
2159021853
2159121854
memset( pFile, 0, sizeof(*pFile) );
2159221855
21593
- OSTRACE2( "OPEN want %d\n", flags );
21856
+ OSTRACE( "OPEN want %d\n", flags ));
2159421857
2159521858
if( flags & SQLITE_OPEN_READWRITE ){
2159621859
ulOpenMode |= OPEN_ACCESS_READWRITE;
21597
- OSTRACE1( "OPEN read/write\n" );
21860
+ OSTRACE(( "OPEN read/write\n" ));
2159821861
}else{
2159921862
ulOpenMode |= OPEN_ACCESS_READONLY;
21600
- OSTRACE1( "OPEN read only\n" );
21863
+ OSTRACE(( "OPEN read only\n" ));
2160121864
}
2160221865
2160321866
if( flags & SQLITE_OPEN_CREATE ){
2160421867
ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
21605
- OSTRACE1( "OPEN open new/create\n" );
21868
+ OSTRACE(( "OPEN open new/create\n" ));
2160621869
}else{
2160721870
ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
21608
- OSTRACE1( "OPEN open existing\n" );
21871
+ OSTRACE(( "OPEN open existing\n" ));
2160921872
}
2161021873
2161121874
if( flags & SQLITE_OPEN_MAIN_DB ){
2161221875
ulOpenMode |= OPEN_SHARE_DENYNONE;
21613
- OSTRACE1( "OPEN share read/write\n" );
21876
+ OSTRACE(( "OPEN share read/write\n" ));
2161421877
}else{
2161521878
ulOpenMode |= OPEN_SHARE_DENYWRITE;
21616
- OSTRACE1( "OPEN share read only\n" );
21879
+ OSTRACE(( "OPEN share read only\n" ));
2161721880
}
2161821881
2161921882
if( flags & SQLITE_OPEN_DELETEONCLOSE ){
2162021883
char pathUtf8[CCHMAXPATH];
2162121884
#ifdef NDEBUG /* when debugging we want to make sure it is deleted */
2162221885
ulFileAttribute = FILE_HIDDEN;
2162321886
#endif
2162421887
os2FullPathname( pVfs, zName, CCHMAXPATH, pathUtf8 );
2162521888
pFile->pathToDel = convertUtf8PathToCp( pathUtf8 );
21626
- OSTRACE1( "OPEN hidden/delete on close file attributes\n" );
21889
+ OSTRACE(( "OPEN hidden/delete on close file attributes\n" ));
2162721890
}else{
2162821891
pFile->pathToDel = NULL;
21629
- OSTRACE1( "OPEN normal file attribute\n" );
21892
+ OSTRACE(( "OPEN normal file attribute\n" ));
2163021893
}
2163121894
2163221895
/* always open in random access mode for possibly better speed */
2163321896
ulOpenMode |= OPEN_FLAGS_RANDOM;
2163421897
ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
@@ -21643,17 +21906,18 @@
2164321906
ulOpenFlags,
2164421907
ulOpenMode,
2164521908
(PEAOP2)NULL );
2164621909
free( zNameCp );
2164721910
if( rc != NO_ERROR ){
21648
- OSTRACE7( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
21649
- rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode );
21911
+ OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
21912
+ rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode ));
2165021913
if( pFile->pathToDel )
2165121914
free( pFile->pathToDel );
2165221915
pFile->pathToDel = NULL;
2165321916
if( flags & SQLITE_OPEN_READWRITE ){
21654
- OSTRACE2( "OPEN %d Invalid handle\n", ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) );
21917
+ OSTRACE(( "OPEN %d Invalid handle\n",
21918
+ ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) ));
2165521919
return os2Open( pVfs, zName, id,
2165621920
((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
2165721921
pOutFlags );
2165821922
}else{
2165921923
return SQLITE_CANTOPEN;
@@ -21665,11 +21929,11 @@
2166521929
}
2166621930
2166721931
pFile->pMethod = &os2IoMethod;
2166821932
pFile->h = h;
2166921933
OpenCounter(+1);
21670
- OSTRACE3( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags );
21934
+ OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
2167121935
return SQLITE_OK;
2167221936
}
2167321937
2167421938
/*
2167521939
** Delete the named file.
@@ -21682,11 +21946,11 @@
2168221946
APIRET rc = NO_ERROR;
2168321947
char *zFilenameCp = convertUtf8PathToCp( zFilename );
2168421948
SimulateIOError( return SQLITE_IOERR_DELETE );
2168521949
rc = DosDelete( (PSZ)zFilenameCp );
2168621950
free( zFilenameCp );
21687
- OSTRACE2( "DELETE \"%s\"\n", zFilename );
21951
+ OSTRACE(( "DELETE \"%s\"\n", zFilename ));
2168821952
return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_DELETE;
2168921953
}
2169021954
2169121955
/*
2169221956
** Check the existance and status of a file.
@@ -21703,21 +21967,21 @@
2170321967
2170421968
memset( &fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo) );
2170521969
rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
2170621970
&fsts3ConfigInfo, sizeof(FILESTATUS3) );
2170721971
free( zFilenameCp );
21708
- OSTRACE4( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
21709
- fsts3ConfigInfo.attrFile, flags, rc );
21972
+ OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
21973
+ fsts3ConfigInfo.attrFile, flags, rc ));
2171021974
switch( flags ){
2171121975
case SQLITE_ACCESS_READ:
2171221976
case SQLITE_ACCESS_EXISTS:
2171321977
rc = (rc == NO_ERROR);
21714
- OSTRACE3( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc );
21978
+ OSTRACE(( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc));
2171521979
break;
2171621980
case SQLITE_ACCESS_READWRITE:
2171721981
rc = (rc == NO_ERROR) && ( (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0 );
21718
- OSTRACE3( "ACCESS %s access of read/write rc=%d\n", zFilename, rc );
21982
+ OSTRACE(( "ACCESS %s access of read/write rc=%d\n", zFilename, rc ));
2171921983
break;
2172021984
default:
2172121985
assert( !"Invalid flags argument" );
2172221986
}
2172321987
*pOut = rc;
@@ -21923,11 +22187,11 @@
2192322187
os2DlSym, /* xDlSym */
2192422188
os2DlClose, /* xDlClose */
2192522189
os2Randomness, /* xRandomness */
2192622190
os2Sleep, /* xSleep */
2192722191
os2CurrentTime, /* xCurrentTime */
21928
- os2GetLastError /* xGetLastError */
22192
+ os2GetLastError, /* xGetLastError */
2192922193
};
2193022194
sqlite3_vfs_register(&os2Vfs, 1);
2193122195
initUconvObjects();
2193222196
return SQLITE_OK;
2193322197
}
@@ -22057,10 +22321,11 @@
2205722321
#include <sys/stat.h>
2205822322
#include <fcntl.h>
2205922323
#include <unistd.h>
2206022324
#include <sys/time.h>
2206122325
#include <errno.h>
22326
+#include <sys/mman.h>
2206222327
2206322328
#if SQLITE_ENABLE_LOCKING_STYLE
2206422329
# include <sys/ioctl.h>
2206522330
# if OS_VXWORKS
2206622331
# include <semaphore.h>
@@ -22111,18 +22376,22 @@
2211122376
** Only set the lastErrno if the error code is a real error and not
2211222377
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
2211322378
*/
2211422379
#define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
2211522380
22381
+/* Forward references */
22382
+typedef struct unixShm unixShm; /* Connection shared memory */
22383
+typedef struct unixShmNode unixShmNode; /* Shared memory instance */
22384
+typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
22385
+typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
2211622386
2211722387
/*
2211822388
** Sometimes, after a file handle is closed by SQLite, the file descriptor
2211922389
** cannot be closed immediately. In these cases, instances of the following
2212022390
** structure are used to store the file descriptor while waiting for an
2212122391
** opportunity to either close or reuse it.
2212222392
*/
22123
-typedef struct UnixUnusedFd UnixUnusedFd;
2212422393
struct UnixUnusedFd {
2212522394
int fd; /* File descriptor to close */
2212622395
int flags; /* Flags this file descriptor was opened with */
2212722396
UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
2212822397
};
@@ -22132,31 +22401,29 @@
2213222401
** VFS implementations.
2213322402
*/
2213422403
typedef struct unixFile unixFile;
2213522404
struct unixFile {
2213622405
sqlite3_io_methods const *pMethod; /* Always the first entry */
22137
- struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */
22138
- struct unixLockInfo *pLock; /* Info about locks on this inode */
22139
- int h; /* The file descriptor */
22140
- int dirfd; /* File descriptor for the directory */
22141
- unsigned char locktype; /* The type of lock held on this fd */
22142
- int lastErrno; /* The unix errno from the last I/O error */
22143
- void *lockingContext; /* Locking style specific state */
22144
- UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22145
- int fileFlags; /* Miscellanous flags */
22406
+ unixInodeInfo *pInode; /* Info about locks on this inode */
22407
+ int h; /* The file descriptor */
22408
+ int dirfd; /* File descriptor for the directory */
22409
+ unsigned char eFileLock; /* The type of lock held on this fd */
22410
+ int lastErrno; /* The unix errno from last I/O error */
22411
+ void *lockingContext; /* Locking style specific state */
22412
+ UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22413
+ int fileFlags; /* Miscellanous flags */
22414
+ const char *zPath; /* Name of the file */
22415
+ unixShm *pShm; /* Shared memory segment information */
2214622416
#if SQLITE_ENABLE_LOCKING_STYLE
22147
- int openFlags; /* The flags specified at open() */
22417
+ int openFlags; /* The flags specified at open() */
2214822418
#endif
2214922419
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
22150
- unsigned fsFlags; /* cached details from statfs() */
22151
-#endif
22152
-#if SQLITE_THREADSAFE && defined(__linux__)
22153
- pthread_t tid; /* The thread that "owns" this unixFile */
22420
+ unsigned fsFlags; /* cached details from statfs() */
2215422421
#endif
2215522422
#if OS_VXWORKS
22156
- int isDelete; /* Delete on close if true */
22157
- struct vxworksFileId *pId; /* Unique file ID */
22423
+ int isDelete; /* Delete on close if true */
22424
+ struct vxworksFileId *pId; /* Unique file ID */
2215822425
#endif
2215922426
#ifndef NDEBUG
2216022427
/* The next group of variables are used to track whether or not the
2216122428
** transaction counter in bytes 24-27 of database files are updated
2216222429
** whenever any part of the database changes. An assertion fault will
@@ -22217,27 +22484,13 @@
2221722484
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2221822485
#endif
2221922486
2222022487
#ifdef SQLITE_DEBUG
2222122488
SQLITE_PRIVATE int sqlite3OSTrace = 0;
22222
-#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
22223
-#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
22224
-#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
22225
-#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
22226
-#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
22227
-#define OSTRACE6(X,Y,Z,A,B,C) \
22228
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
22229
-#define OSTRACE7(X,Y,Z,A,B,C,D) \
22230
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
22489
+#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
2223122490
#else
22232
-#define OSTRACE1(X)
22233
-#define OSTRACE2(X,Y)
22234
-#define OSTRACE3(X,Y,Z)
22235
-#define OSTRACE4(X,Y,Z,A)
22236
-#define OSTRACE5(X,Y,Z,A,B)
22237
-#define OSTRACE6(X,Y,Z,A,B,C)
22238
-#define OSTRACE7(X,Y,Z,A,B,C,D)
22491
+#define OSTRACE(X)
2223922492
#endif
2224022493
2224122494
/*
2224222495
** Macros for performance tracing. Normally turned off. Only works
2224322496
** on i486 hardware.
@@ -22443,11 +22696,11 @@
2244322696
#endif
2244422697
2244522698
2244622699
/*
2244722700
** Helper functions to obtain and relinquish the global mutex. The
22448
-** global mutex is used to protect the unixOpenCnt, unixLockInfo and
22701
+** global mutex is used to protect the unixInodeInfo and
2244922702
** vxworksFileId objects used by this file, all of which may be
2245022703
** shared by multiple threads.
2245122704
**
2245222705
** Function unixMutexHeld() is used to assert() that the global mutex
2245322706
** is held when required. This function is only used as part of assert()
@@ -22474,12 +22727,12 @@
2247422727
/*
2247522728
** Helper function for printing out trace information from debugging
2247622729
** binaries. This returns the string represetation of the supplied
2247722730
** integer lock-type.
2247822731
*/
22479
-static const char *locktypeName(int locktype){
22480
- switch( locktype ){
22732
+static const char *azFileLock(int eFileLock){
22733
+ switch( eFileLock ){
2248122734
case NO_LOCK: return "NONE";
2248222735
case SHARED_LOCK: return "SHARED";
2248322736
case RESERVED_LOCK: return "RESERVED";
2248422737
case PENDING_LOCK: return "PENDING";
2248522738
case EXCLUSIVE_LOCK: return "EXCLUSIVE";
@@ -22812,17 +23065,16 @@
2281223065
**
2281323066
** But wait: there are yet more problems with POSIX advisory locks.
2281423067
**
2281523068
** If you close a file descriptor that points to a file that has locks,
2281623069
** all locks on that file that are owned by the current process are
22817
-** released. To work around this problem, each unixFile structure contains
22818
-** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure
22819
-** per open inode, which means that multiple unixFile can point to a single
22820
-** unixOpenCnt. When an attempt is made to close an unixFile, if there are
23070
+** released. To work around this problem, each unixInodeInfo object
23071
+** maintains a count of the number of pending locks on tha inode.
23072
+** When an attempt is made to close an unixFile, if there are
2282123073
** other unixFile open on the same inode that are holding locks, the call
2282223074
** to close() the file descriptor is deferred until all of the locks clear.
22823
-** The unixOpenCnt structure keeps a list of file descriptors that need to
23075
+** The unixInodeInfo structure keeps a list of file descriptors that need to
2282423076
** be closed and that list is walked (and cleared) when the last lock
2282523077
** clears.
2282623078
**
2282723079
** Yet another problem: LinuxThreads do not play well with posix locks.
2282823080
**
@@ -22833,50 +23085,23 @@
2283323085
** if the appliation uses the newer Native Posix Thread Library (NPTL)
2283423086
** on linux - with NPTL a lock created by thread A can override locks
2283523087
** in thread B. But there is no way to know at compile-time which
2283623088
** threading library is being used. So there is no way to know at
2283723089
** compile-time whether or not thread A can override locks on thread B.
22838
-** We have to do a run-time check to discover the behavior of the
23090
+** One has to do a run-time check to discover the behavior of the
2283923091
** current process.
2284023092
**
22841
-** On systems where thread A is unable to modify locks created by
22842
-** thread B, we have to keep track of which thread created each
22843
-** lock. Hence there is an extra field in the key to the unixLockInfo
22844
-** structure to record this information. And on those systems it
22845
-** is illegal to begin a transaction in one thread and finish it
22846
-** in another. For this latter restriction, there is no work-around.
22847
-** It is a limitation of LinuxThreads.
22848
-*/
22849
-
22850
-/*
22851
-** Set or check the unixFile.tid field. This field is set when an unixFile
22852
-** is first opened. All subsequent uses of the unixFile verify that the
22853
-** same thread is operating on the unixFile. Some operating systems do
22854
-** not allow locks to be overridden by other threads and that restriction
22855
-** means that sqlite3* database handles cannot be moved from one thread
22856
-** to another while locks are held.
22857
-**
22858
-** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
22859
-** another as long as we are running on a system that supports threads
22860
-** overriding each others locks (which is now the most common behavior)
22861
-** or if no locks are held. But the unixFile.pLock field needs to be
22862
-** recomputed because its key includes the thread-id. See the
22863
-** transferOwnership() function below for additional information
22864
-*/
22865
-#if SQLITE_THREADSAFE && defined(__linux__)
22866
-# define SET_THREADID(X) (X)->tid = pthread_self()
22867
-# define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
22868
- !pthread_equal((X)->tid, pthread_self()))
22869
-#else
22870
-# define SET_THREADID(X)
22871
-# define CHECK_THREADID(X) 0
22872
-#endif
23093
+** SQLite used to support LinuxThreads. But support for LinuxThreads
23094
+** was dropped beginning with version 3.7.0. SQLite will still work with
23095
+** LinuxThreads provided that (1) there is no more than one connection
23096
+** per database file in the same process and (2) database connections
23097
+** do not move across threads.
23098
+*/
2287323099
2287423100
/*
2287523101
** An instance of the following structure serves as the key used
22876
-** to locate a particular unixOpenCnt structure given its inode. This
22877
-** is the same as the unixLockKey except that the thread ID is omitted.
23102
+** to locate a particular unixInodeInfo object.
2287823103
*/
2287923104
struct unixFileId {
2288023105
dev_t dev; /* Device number */
2288123106
#if OS_VXWORKS
2288223107
struct vxworksFileId *pId; /* Unique file ID for vxworks. */
@@ -22883,260 +23108,122 @@
2288323108
#else
2288423109
ino_t ino; /* Inode number */
2288523110
#endif
2288623111
};
2288723112
22888
-/*
22889
-** An instance of the following structure serves as the key used
22890
-** to locate a particular unixLockInfo structure given its inode.
22891
-**
22892
-** If threads cannot override each others locks (LinuxThreads), then we
22893
-** set the unixLockKey.tid field to the thread ID. If threads can override
22894
-** each others locks (Posix and NPTL) then tid is always set to zero.
22895
-** tid is omitted if we compile without threading support or on an OS
22896
-** other than linux.
22897
-*/
22898
-struct unixLockKey {
22899
- struct unixFileId fid; /* Unique identifier for the file */
22900
-#if SQLITE_THREADSAFE && defined(__linux__)
22901
- pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */
22902
-#endif
22903
-};
22904
-
2290523113
/*
2290623114
** An instance of the following structure is allocated for each open
2290723115
** inode. Or, on LinuxThreads, there is one of these structures for
2290823116
** each inode opened by each thread.
2290923117
**
2291023118
** A single inode can have multiple file descriptors, so each unixFile
2291123119
** structure contains a pointer to an instance of this object and this
2291223120
** object keeps a count of the number of unixFile pointing to it.
2291323121
*/
22914
-struct unixLockInfo {
22915
- struct unixLockKey lockKey; /* The lookup key */
22916
- int cnt; /* Number of SHARED locks held */
22917
- int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
22918
- int nRef; /* Number of pointers to this structure */
22919
-#if defined(SQLITE_ENABLE_LOCKING_STYLE)
22920
- unsigned long long sharedByte; /* for AFP simulated shared lock */
22921
-#endif
22922
- struct unixLockInfo *pNext; /* List of all unixLockInfo objects */
22923
- struct unixLockInfo *pPrev; /* .... doubly linked */
22924
-};
22925
-
22926
-/*
22927
-** An instance of the following structure is allocated for each open
22928
-** inode. This structure keeps track of the number of locks on that
22929
-** inode. If a close is attempted against an inode that is holding
22930
-** locks, the close is deferred until all locks clear by adding the
22931
-** file descriptor to be closed to the pending list.
22932
-**
22933
-** TODO: Consider changing this so that there is only a single file
22934
-** descriptor for each open file, even when it is opened multiple times.
22935
-** The close() system call would only occur when the last database
22936
-** using the file closes.
22937
-*/
22938
-struct unixOpenCnt {
22939
- struct unixFileId fileId; /* The lookup key */
22940
- int nRef; /* Number of pointers to this structure */
22941
- int nLock; /* Number of outstanding locks */
22942
- UnixUnusedFd *pUnused; /* Unused file descriptors to close */
22943
-#if OS_VXWORKS
22944
- sem_t *pSem; /* Named POSIX semaphore */
22945
- char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
22946
-#endif
22947
- struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */
22948
-};
22949
-
22950
-/*
22951
-** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash
22952
-** tables. But the number of objects is rarely more than a dozen and
22953
-** never exceeds a few thousand. And lookup is not on a critical
22954
-** path so a simple linked list will suffice.
22955
-*/
22956
-static struct unixLockInfo *lockList = 0;
22957
-static struct unixOpenCnt *openList = 0;
22958
-
22959
-/*
22960
-** This variable remembers whether or not threads can override each others
22961
-** locks.
22962
-**
22963
-** 0: No. Threads cannot override each others locks. (LinuxThreads)
22964
-** 1: Yes. Threads can override each others locks. (Posix & NLPT)
22965
-** -1: We don't know yet.
22966
-**
22967
-** On some systems, we know at compile-time if threads can override each
22968
-** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
22969
-** will be set appropriately. On other systems, we have to check at
22970
-** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
22971
-** undefined.
22972
-**
22973
-** This variable normally has file scope only. But during testing, we make
22974
-** it a global so that the test code can change its value in order to verify
22975
-** that the right stuff happens in either case.
22976
-*/
22977
-#if SQLITE_THREADSAFE && defined(__linux__)
22978
-# ifndef SQLITE_THREAD_OVERRIDE_LOCK
22979
-# define SQLITE_THREAD_OVERRIDE_LOCK -1
22980
-# endif
22981
-# ifdef SQLITE_TEST
22982
-int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
22983
-# else
22984
-static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
22985
-# endif
22986
-#endif
22987
-
22988
-/*
22989
-** This structure holds information passed into individual test
22990
-** threads by the testThreadLockingBehavior() routine.
22991
-*/
22992
-struct threadTestData {
22993
- int fd; /* File to be locked */
22994
- struct flock lock; /* The locking operation */
22995
- int result; /* Result of the locking operation */
22996
-};
22997
-
22998
-#if SQLITE_THREADSAFE && defined(__linux__)
22999
-/*
23000
-** This function is used as the main routine for a thread launched by
23001
-** testThreadLockingBehavior(). It tests whether the shared-lock obtained
23002
-** by the main thread in testThreadLockingBehavior() conflicts with a
23003
-** hypothetical write-lock obtained by this thread on the same file.
23004
-**
23005
-** The write-lock is not actually acquired, as this is not possible if
23006
-** the file is open in read-only mode (see ticket #3472).
23007
-*/
23008
-static void *threadLockingTest(void *pArg){
23009
- struct threadTestData *pData = (struct threadTestData*)pArg;
23010
- pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
23011
- return pArg;
23012
-}
23013
-#endif /* SQLITE_THREADSAFE && defined(__linux__) */
23014
-
23015
-
23016
-#if SQLITE_THREADSAFE && defined(__linux__)
23017
-/*
23018
-** This procedure attempts to determine whether or not threads
23019
-** can override each others locks then sets the
23020
-** threadsOverrideEachOthersLocks variable appropriately.
23021
-*/
23022
-static void testThreadLockingBehavior(int fd_orig){
23023
- int fd;
23024
- int rc;
23025
- struct threadTestData d;
23026
- struct flock l;
23027
- pthread_t t;
23028
-
23029
- fd = dup(fd_orig);
23030
- if( fd<0 ) return;
23031
- memset(&l, 0, sizeof(l));
23032
- l.l_type = F_RDLCK;
23033
- l.l_len = 1;
23034
- l.l_start = 0;
23035
- l.l_whence = SEEK_SET;
23036
- rc = fcntl(fd_orig, F_SETLK, &l);
23037
- if( rc!=0 ) return;
23038
- memset(&d, 0, sizeof(d));
23039
- d.fd = fd;
23040
- d.lock = l;
23041
- d.lock.l_type = F_WRLCK;
23042
- if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){
23043
- pthread_join(t, 0);
23044
- }
23045
- close(fd);
23046
- if( d.result!=0 ) return;
23047
- threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
23048
-}
23049
-#endif /* SQLITE_THREADSAFE && defined(__linux__) */
23050
-
23051
-/*
23052
-** Release a unixLockInfo structure previously allocated by findLockInfo().
23053
-**
23054
-** The mutex entered using the unixEnterMutex() function must be held
23055
-** when this function is called.
23056
-*/
23057
-static void releaseLockInfo(struct unixLockInfo *pLock){
23058
- assert( unixMutexHeld() );
23059
- if( pLock ){
23060
- pLock->nRef--;
23061
- if( pLock->nRef==0 ){
23062
- if( pLock->pPrev ){
23063
- assert( pLock->pPrev->pNext==pLock );
23064
- pLock->pPrev->pNext = pLock->pNext;
23065
- }else{
23066
- assert( lockList==pLock );
23067
- lockList = pLock->pNext;
23068
- }
23069
- if( pLock->pNext ){
23070
- assert( pLock->pNext->pPrev==pLock );
23071
- pLock->pNext->pPrev = pLock->pPrev;
23072
- }
23073
- sqlite3_free(pLock);
23074
- }
23075
- }
23076
-}
23077
-
23078
-/*
23079
-** Release a unixOpenCnt structure previously allocated by findLockInfo().
23080
-**
23081
-** The mutex entered using the unixEnterMutex() function must be held
23082
-** when this function is called.
23083
-*/
23084
-static void releaseOpenCnt(struct unixOpenCnt *pOpen){
23085
- assert( unixMutexHeld() );
23086
- if( pOpen ){
23087
- pOpen->nRef--;
23088
- if( pOpen->nRef==0 ){
23089
- if( pOpen->pPrev ){
23090
- assert( pOpen->pPrev->pNext==pOpen );
23091
- pOpen->pPrev->pNext = pOpen->pNext;
23092
- }else{
23093
- assert( openList==pOpen );
23094
- openList = pOpen->pNext;
23095
- }
23096
- if( pOpen->pNext ){
23097
- assert( pOpen->pNext->pPrev==pOpen );
23098
- pOpen->pNext->pPrev = pOpen->pPrev;
23099
- }
23100
-#if SQLITE_THREADSAFE && defined(__linux__)
23101
- assert( !pOpen->pUnused || threadsOverrideEachOthersLocks==0 );
23102
-#endif
23103
-
23104
- /* If pOpen->pUnused is not null, then memory and file-descriptors
23105
- ** are leaked.
23106
- **
23107
- ** This will only happen if, under Linuxthreads, the user has opened
23108
- ** a transaction in one thread, then attempts to close the database
23109
- ** handle from another thread (without first unlocking the db file).
23110
- ** This is a misuse. */
23111
- sqlite3_free(pOpen);
23112
- }
23113
- }
23114
-}
23115
-
23116
-/*
23117
-** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
23118
-** describes that file descriptor. Create new ones if necessary. The
23119
-** return values might be uninitialized if an error occurs.
23122
+struct unixInodeInfo {
23123
+ struct unixFileId fileId; /* The lookup key */
23124
+ int nShared; /* Number of SHARED locks held */
23125
+ int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
23126
+ int nRef; /* Number of pointers to this structure */
23127
+ unixShmNode *pShmNode; /* Shared memory associated with this inode */
23128
+ int nLock; /* Number of outstanding file locks */
23129
+ UnixUnusedFd *pUnused; /* Unused file descriptors to close */
23130
+ unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
23131
+ unixInodeInfo *pPrev; /* .... doubly linked */
23132
+#if defined(SQLITE_ENABLE_LOCKING_STYLE)
23133
+ unsigned long long sharedByte; /* for AFP simulated shared lock */
23134
+#endif
23135
+#if OS_VXWORKS
23136
+ sem_t *pSem; /* Named POSIX semaphore */
23137
+ char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
23138
+#endif
23139
+};
23140
+
23141
+/*
23142
+** A lists of all unixInodeInfo objects.
23143
+*/
23144
+static unixInodeInfo *inodeList = 0;
23145
+
23146
+/*
23147
+** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
23148
+** If all such file descriptors are closed without error, the list is
23149
+** cleared and SQLITE_OK returned.
23150
+**
23151
+** Otherwise, if an error occurs, then successfully closed file descriptor
23152
+** entries are removed from the list, and SQLITE_IOERR_CLOSE returned.
23153
+** not deleted and SQLITE_IOERR_CLOSE returned.
23154
+*/
23155
+static int closePendingFds(unixFile *pFile){
23156
+ int rc = SQLITE_OK;
23157
+ unixInodeInfo *pInode = pFile->pInode;
23158
+ UnixUnusedFd *pError = 0;
23159
+ UnixUnusedFd *p;
23160
+ UnixUnusedFd *pNext;
23161
+ for(p=pInode->pUnused; p; p=pNext){
23162
+ pNext = p->pNext;
23163
+ if( close(p->fd) ){
23164
+ pFile->lastErrno = errno;
23165
+ rc = SQLITE_IOERR_CLOSE;
23166
+ p->pNext = pError;
23167
+ pError = p;
23168
+ }else{
23169
+ sqlite3_free(p);
23170
+ }
23171
+ }
23172
+ pInode->pUnused = pError;
23173
+ return rc;
23174
+}
23175
+
23176
+/*
23177
+** Release a unixInodeInfo structure previously allocated by findInodeInfo().
23178
+**
23179
+** The mutex entered using the unixEnterMutex() function must be held
23180
+** when this function is called.
23181
+*/
23182
+static void releaseInodeInfo(unixFile *pFile){
23183
+ unixInodeInfo *pInode = pFile->pInode;
23184
+ assert( unixMutexHeld() );
23185
+ if( pInode ){
23186
+ pInode->nRef--;
23187
+ if( pInode->nRef==0 ){
23188
+ assert( pInode->pShmNode==0 );
23189
+ closePendingFds(pFile);
23190
+ if( pInode->pPrev ){
23191
+ assert( pInode->pPrev->pNext==pInode );
23192
+ pInode->pPrev->pNext = pInode->pNext;
23193
+ }else{
23194
+ assert( inodeList==pInode );
23195
+ inodeList = pInode->pNext;
23196
+ }
23197
+ if( pInode->pNext ){
23198
+ assert( pInode->pNext->pPrev==pInode );
23199
+ pInode->pNext->pPrev = pInode->pPrev;
23200
+ }
23201
+ sqlite3_free(pInode);
23202
+ }
23203
+ }
23204
+}
23205
+
23206
+/*
23207
+** Given a file descriptor, locate the unixInodeInfo object that
23208
+** describes that file descriptor. Create a new one if necessary. The
23209
+** return value might be uninitialized if an error occurs.
2312023210
**
2312123211
** The mutex entered using the unixEnterMutex() function must be held
2312223212
** when this function is called.
2312323213
**
2312423214
** Return an appropriate error code.
2312523215
*/
23126
-static int findLockInfo(
23216
+static int findInodeInfo(
2312723217
unixFile *pFile, /* Unix file with file desc used in the key */
23128
- struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */
23129
- struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */
23218
+ unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
2313023219
){
2313123220
int rc; /* System call return code */
2313223221
int fd; /* The file descriptor for pFile */
23133
- struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
23134
- struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
23222
+ struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
2313523223
struct stat statbuf; /* Low-level file information */
23136
- struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */
23137
- struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
23224
+ unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
2313823225
2313923226
assert( unixMutexHeld() );
2314023227
2314123228
/* Get low-level information about the file that we can used to
2314223229
** create a unique name for the file.
@@ -23174,126 +23261,39 @@
2317423261
return SQLITE_IOERR;
2317523262
}
2317623263
}
2317723264
#endif
2317823265
23179
- memset(&lockKey, 0, sizeof(lockKey));
23180
- lockKey.fid.dev = statbuf.st_dev;
23266
+ memset(&fileId, 0, sizeof(fileId));
23267
+ fileId.dev = statbuf.st_dev;
2318123268
#if OS_VXWORKS
23182
- lockKey.fid.pId = pFile->pId;
23183
-#else
23184
- lockKey.fid.ino = statbuf.st_ino;
23185
-#endif
23186
-#if SQLITE_THREADSAFE && defined(__linux__)
23187
- if( threadsOverrideEachOthersLocks<0 ){
23188
- testThreadLockingBehavior(fd);
23189
- }
23190
- lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
23191
-#endif
23192
- fileId = lockKey.fid;
23193
- if( ppLock!=0 ){
23194
- pLock = lockList;
23195
- while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
23196
- pLock = pLock->pNext;
23197
- }
23198
- if( pLock==0 ){
23199
- pLock = sqlite3_malloc( sizeof(*pLock) );
23200
- if( pLock==0 ){
23201
- rc = SQLITE_NOMEM;
23202
- goto exit_findlockinfo;
23203
- }
23204
- memcpy(&pLock->lockKey,&lockKey,sizeof(lockKey));
23205
- pLock->nRef = 1;
23206
- pLock->cnt = 0;
23207
- pLock->locktype = 0;
23208
-#if defined(SQLITE_ENABLE_LOCKING_STYLE)
23209
- pLock->sharedByte = 0;
23210
-#endif
23211
- pLock->pNext = lockList;
23212
- pLock->pPrev = 0;
23213
- if( lockList ) lockList->pPrev = pLock;
23214
- lockList = pLock;
23215
- }else{
23216
- pLock->nRef++;
23217
- }
23218
- *ppLock = pLock;
23219
- }
23220
- if( ppOpen!=0 ){
23221
- pOpen = openList;
23222
- while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
23223
- pOpen = pOpen->pNext;
23224
- }
23225
- if( pOpen==0 ){
23226
- pOpen = sqlite3_malloc( sizeof(*pOpen) );
23227
- if( pOpen==0 ){
23228
- releaseLockInfo(pLock);
23229
- rc = SQLITE_NOMEM;
23230
- goto exit_findlockinfo;
23231
- }
23232
- memset(pOpen, 0, sizeof(*pOpen));
23233
- pOpen->fileId = fileId;
23234
- pOpen->nRef = 1;
23235
- pOpen->pNext = openList;
23236
- if( openList ) openList->pPrev = pOpen;
23237
- openList = pOpen;
23238
- }else{
23239
- pOpen->nRef++;
23240
- }
23241
- *ppOpen = pOpen;
23242
- }
23243
-
23244
-exit_findlockinfo:
23245
- return rc;
23246
-}
23247
-
23248
-/*
23249
-** If we are currently in a different thread than the thread that the
23250
-** unixFile argument belongs to, then transfer ownership of the unixFile
23251
-** over to the current thread.
23252
-**
23253
-** A unixFile is only owned by a thread on systems that use LinuxThreads.
23254
-**
23255
-** Ownership transfer is only allowed if the unixFile is currently unlocked.
23256
-** If the unixFile is locked and an ownership is wrong, then return
23257
-** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
23258
-*/
23259
-#if SQLITE_THREADSAFE && defined(__linux__)
23260
-static int transferOwnership(unixFile *pFile){
23261
- int rc;
23262
- pthread_t hSelf;
23263
- if( threadsOverrideEachOthersLocks ){
23264
- /* Ownership transfers not needed on this system */
23265
- return SQLITE_OK;
23266
- }
23267
- hSelf = pthread_self();
23268
- if( pthread_equal(pFile->tid, hSelf) ){
23269
- /* We are still in the same thread */
23270
- OSTRACE1("No-transfer, same thread\n");
23271
- return SQLITE_OK;
23272
- }
23273
- if( pFile->locktype!=NO_LOCK ){
23274
- /* We cannot change ownership while we are holding a lock! */
23275
- return SQLITE_MISUSE_BKPT;
23276
- }
23277
- OSTRACE4("Transfer ownership of %d from %d to %d\n",
23278
- pFile->h, pFile->tid, hSelf);
23279
- pFile->tid = hSelf;
23280
- if (pFile->pLock != NULL) {
23281
- releaseLockInfo(pFile->pLock);
23282
- rc = findLockInfo(pFile, &pFile->pLock, 0);
23283
- OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
23284
- locktypeName(pFile->locktype),
23285
- locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
23286
- return rc;
23287
- } else {
23288
- return SQLITE_OK;
23289
- }
23290
-}
23291
-#else /* if not SQLITE_THREADSAFE */
23292
- /* On single-threaded builds, ownership transfer is a no-op */
23293
-# define transferOwnership(X) SQLITE_OK
23294
-#endif /* SQLITE_THREADSAFE */
23269
+ fileId.pId = pFile->pId;
23270
+#else
23271
+ fileId.ino = statbuf.st_ino;
23272
+#endif
23273
+ pInode = inodeList;
23274
+ while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
23275
+ pInode = pInode->pNext;
23276
+ }
23277
+ if( pInode==0 ){
23278
+ pInode = sqlite3_malloc( sizeof(*pInode) );
23279
+ if( pInode==0 ){
23280
+ return SQLITE_NOMEM;
23281
+ }
23282
+ memset(pInode, 0, sizeof(*pInode));
23283
+ memcpy(&pInode->fileId, &fileId, sizeof(fileId));
23284
+ pInode->nRef = 1;
23285
+ pInode->pNext = inodeList;
23286
+ pInode->pPrev = 0;
23287
+ if( inodeList ) inodeList->pPrev = pInode;
23288
+ inodeList = pInode;
23289
+ }else{
23290
+ pInode->nRef++;
23291
+ }
23292
+ *ppInode = pInode;
23293
+ return SQLITE_OK;
23294
+}
2329523295
2329623296
2329723297
/*
2329823298
** This routine checks if there is a RESERVED lock held on the specified
2329923299
** file by this or any other process. If such a lock is held, set *pResOut
@@ -23306,14 +23306,14 @@
2330623306
unixFile *pFile = (unixFile*)id;
2330723307
2330823308
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2330923309
2331023310
assert( pFile );
23311
- unixEnterMutex(); /* Because pFile->pLock is shared across threads */
23311
+ unixEnterMutex(); /* Because pFile->pInode is shared across threads */
2331223312
2331323313
/* Check if a thread in this process holds such a lock */
23314
- if( pFile->pLock->locktype>SHARED_LOCK ){
23314
+ if( pFile->pInode->eFileLock>SHARED_LOCK ){
2331523315
reserved = 1;
2331623316
}
2331723317
2331823318
/* Otherwise see if some other process holds it.
2331923319
*/
@@ -23333,18 +23333,18 @@
2333323333
}
2333423334
}
2333523335
#endif
2333623336
2333723337
unixLeaveMutex();
23338
- OSTRACE4("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved);
23338
+ OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
2333923339
2334023340
*pResOut = reserved;
2334123341
return rc;
2334223342
}
2334323343
2334423344
/*
23345
-** Lock the file with the lock specified by parameter locktype - one
23345
+** Lock the file with the lock specified by parameter eFileLock - one
2334623346
** of the following:
2334723347
**
2334823348
** (1) SHARED_LOCK
2334923349
** (2) RESERVED_LOCK
2335023350
** (3) PENDING_LOCK
@@ -23363,11 +23363,11 @@
2336323363
** PENDING -> EXCLUSIVE
2336423364
**
2336523365
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2336623366
** routine to lower a locking level.
2336723367
*/
23368
-static int unixLock(sqlite3_file *id, int locktype){
23368
+static int unixLock(sqlite3_file *id, int eFileLock){
2336923369
/* The following describes the implementation of the various locks and
2337023370
** lock transitions in terms of the POSIX advisory shared and exclusive
2337123371
** lock primitives (called read-locks and write-locks below, to avoid
2337223372
** confusion with SQLite lock names). The algorithms are complicated
2337323373
** slightly in order to be compatible with windows systems simultaneously
@@ -23404,74 +23404,66 @@
2340423404
** locking a random byte from a range, concurrent SHARED locks may exist
2340523405
** even if the locking primitive used is always a write-lock.
2340623406
*/
2340723407
int rc = SQLITE_OK;
2340823408
unixFile *pFile = (unixFile*)id;
23409
- struct unixLockInfo *pLock = pFile->pLock;
23409
+ unixInodeInfo *pInode = pFile->pInode;
2341023410
struct flock lock;
2341123411
int s = 0;
2341223412
int tErrno = 0;
2341323413
2341423414
assert( pFile );
23415
- OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
23416
- locktypeName(locktype), locktypeName(pFile->locktype),
23417
- locktypeName(pLock->locktype), pLock->cnt , getpid());
23415
+ OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
23416
+ azFileLock(eFileLock), azFileLock(pFile->eFileLock),
23417
+ azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
2341823418
2341923419
/* If there is already a lock of this type or more restrictive on the
2342023420
** unixFile, do nothing. Don't use the end_lock: exit path, as
2342123421
** unixEnterMutex() hasn't been called yet.
2342223422
*/
23423
- if( pFile->locktype>=locktype ){
23424
- OSTRACE3("LOCK %d %s ok (already held) (unix)\n", pFile->h,
23425
- locktypeName(locktype));
23423
+ if( pFile->eFileLock>=eFileLock ){
23424
+ OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
23425
+ azFileLock(eFileLock)));
2342623426
return SQLITE_OK;
2342723427
}
2342823428
2342923429
/* Make sure the locking sequence is correct.
2343023430
** (1) We never move from unlocked to anything higher than shared lock.
2343123431
** (2) SQLite never explicitly requests a pendig lock.
2343223432
** (3) A shared lock is always held when a reserve lock is requested.
2343323433
*/
23434
- assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
23435
- assert( locktype!=PENDING_LOCK );
23436
- assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
23434
+ assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
23435
+ assert( eFileLock!=PENDING_LOCK );
23436
+ assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
2343723437
23438
- /* This mutex is needed because pFile->pLock is shared across threads
23438
+ /* This mutex is needed because pFile->pInode is shared across threads
2343923439
*/
2344023440
unixEnterMutex();
23441
-
23442
- /* Make sure the current thread owns the pFile.
23443
- */
23444
- rc = transferOwnership(pFile);
23445
- if( rc!=SQLITE_OK ){
23446
- unixLeaveMutex();
23447
- return rc;
23448
- }
23449
- pLock = pFile->pLock;
23441
+ pInode = pFile->pInode;
2345023442
2345123443
/* If some thread using this PID has a lock via a different unixFile*
2345223444
** handle that precludes the requested lock, return BUSY.
2345323445
*/
23454
- if( (pFile->locktype!=pLock->locktype &&
23455
- (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
23446
+ if( (pFile->eFileLock!=pInode->eFileLock &&
23447
+ (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
2345623448
){
2345723449
rc = SQLITE_BUSY;
2345823450
goto end_lock;
2345923451
}
2346023452
2346123453
/* If a SHARED lock is requested, and some thread using this PID already
2346223454
** has a SHARED or RESERVED lock, then increment reference counts and
2346323455
** return SQLITE_OK.
2346423456
*/
23465
- if( locktype==SHARED_LOCK &&
23466
- (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
23467
- assert( locktype==SHARED_LOCK );
23468
- assert( pFile->locktype==0 );
23469
- assert( pLock->cnt>0 );
23470
- pFile->locktype = SHARED_LOCK;
23471
- pLock->cnt++;
23472
- pFile->pOpen->nLock++;
23457
+ if( eFileLock==SHARED_LOCK &&
23458
+ (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
23459
+ assert( eFileLock==SHARED_LOCK );
23460
+ assert( pFile->eFileLock==0 );
23461
+ assert( pInode->nShared>0 );
23462
+ pFile->eFileLock = SHARED_LOCK;
23463
+ pInode->nShared++;
23464
+ pInode->nLock++;
2347323465
goto end_lock;
2347423466
}
2347523467
2347623468
2347723469
/* A PENDING lock is needed before acquiring a SHARED lock and before
@@ -23478,14 +23470,14 @@
2347823470
** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2347923471
** be released.
2348023472
*/
2348123473
lock.l_len = 1L;
2348223474
lock.l_whence = SEEK_SET;
23483
- if( locktype==SHARED_LOCK
23484
- || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
23475
+ if( eFileLock==SHARED_LOCK
23476
+ || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
2348523477
){
23486
- lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
23478
+ lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
2348723479
lock.l_start = PENDING_BYTE;
2348823480
s = fcntl(pFile->h, F_SETLK, &lock);
2348923481
if( s==(-1) ){
2349023482
tErrno = errno;
2349123483
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -23498,13 +23490,13 @@
2349823490
2349923491
2350023492
/* If control gets to this point, then actually go ahead and make
2350123493
** operating system calls for the specified lock.
2350223494
*/
23503
- if( locktype==SHARED_LOCK ){
23504
- assert( pLock->cnt==0 );
23505
- assert( pLock->locktype==0 );
23495
+ if( eFileLock==SHARED_LOCK ){
23496
+ assert( pInode->nShared==0 );
23497
+ assert( pInode->eFileLock==0 );
2350623498
2350723499
/* Now get the read-lock */
2350823500
lock.l_start = SHARED_FIRST;
2350923501
lock.l_len = SHARED_SIZE;
2351023502
if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
@@ -23529,26 +23521,26 @@
2352923521
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
2353023522
if( IS_LOCK_ERROR(rc) ){
2353123523
pFile->lastErrno = tErrno;
2353223524
}
2353323525
}else{
23534
- pFile->locktype = SHARED_LOCK;
23535
- pFile->pOpen->nLock++;
23536
- pLock->cnt = 1;
23526
+ pFile->eFileLock = SHARED_LOCK;
23527
+ pInode->nLock++;
23528
+ pInode->nShared = 1;
2353723529
}
23538
- }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
23530
+ }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
2353923531
/* We are trying for an exclusive lock but another thread in this
2354023532
** same process is still holding a shared lock. */
2354123533
rc = SQLITE_BUSY;
2354223534
}else{
2354323535
/* The request was for a RESERVED or EXCLUSIVE lock. It is
2354423536
** assumed that there is a SHARED or greater lock on the file
2354523537
** already.
2354623538
*/
23547
- assert( 0!=pFile->locktype );
23539
+ assert( 0!=pFile->eFileLock );
2354823540
lock.l_type = F_WRLCK;
23549
- switch( locktype ){
23541
+ switch( eFileLock ){
2355023542
case RESERVED_LOCK:
2355123543
lock.l_start = RESERVED_BYTE;
2355223544
break;
2355323545
case EXCLUSIVE_LOCK:
2355423546
lock.l_start = SHARED_FIRST;
@@ -23573,80 +23565,50 @@
2357323565
** transitioning from a SHARED to a RESERVED lock. The change
2357423566
** from SHARED to RESERVED marks the beginning of a normal
2357523567
** write operation (not a hot journal rollback).
2357623568
*/
2357723569
if( rc==SQLITE_OK
23578
- && pFile->locktype<=SHARED_LOCK
23579
- && locktype==RESERVED_LOCK
23570
+ && pFile->eFileLock<=SHARED_LOCK
23571
+ && eFileLock==RESERVED_LOCK
2358023572
){
2358123573
pFile->transCntrChng = 0;
2358223574
pFile->dbUpdate = 0;
2358323575
pFile->inNormalWrite = 1;
2358423576
}
2358523577
#endif
2358623578
2358723579
2358823580
if( rc==SQLITE_OK ){
23589
- pFile->locktype = locktype;
23590
- pLock->locktype = locktype;
23591
- }else if( locktype==EXCLUSIVE_LOCK ){
23592
- pFile->locktype = PENDING_LOCK;
23593
- pLock->locktype = PENDING_LOCK;
23581
+ pFile->eFileLock = eFileLock;
23582
+ pInode->eFileLock = eFileLock;
23583
+ }else if( eFileLock==EXCLUSIVE_LOCK ){
23584
+ pFile->eFileLock = PENDING_LOCK;
23585
+ pInode->eFileLock = PENDING_LOCK;
2359423586
}
2359523587
2359623588
end_lock:
2359723589
unixLeaveMutex();
23598
- OSTRACE4("LOCK %d %s %s (unix)\n", pFile->h, locktypeName(locktype),
23599
- rc==SQLITE_OK ? "ok" : "failed");
23600
- return rc;
23601
-}
23602
-
23603
-/*
23604
-** Close all file descriptors accumuated in the unixOpenCnt->pUnused list.
23605
-** If all such file descriptors are closed without error, the list is
23606
-** cleared and SQLITE_OK returned.
23607
-**
23608
-** Otherwise, if an error occurs, then successfully closed file descriptor
23609
-** entries are removed from the list, and SQLITE_IOERR_CLOSE returned.
23610
-** not deleted and SQLITE_IOERR_CLOSE returned.
23611
-*/
23612
-static int closePendingFds(unixFile *pFile){
23613
- int rc = SQLITE_OK;
23614
- struct unixOpenCnt *pOpen = pFile->pOpen;
23615
- UnixUnusedFd *pError = 0;
23616
- UnixUnusedFd *p;
23617
- UnixUnusedFd *pNext;
23618
- for(p=pOpen->pUnused; p; p=pNext){
23619
- pNext = p->pNext;
23620
- if( close(p->fd) ){
23621
- pFile->lastErrno = errno;
23622
- rc = SQLITE_IOERR_CLOSE;
23623
- p->pNext = pError;
23624
- pError = p;
23625
- }else{
23626
- sqlite3_free(p);
23627
- }
23628
- }
23629
- pOpen->pUnused = pError;
23590
+ OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
23591
+ rc==SQLITE_OK ? "ok" : "failed"));
2363023592
return rc;
2363123593
}
2363223594
2363323595
/*
2363423596
** Add the file descriptor used by file handle pFile to the corresponding
2363523597
** pUnused list.
2363623598
*/
2363723599
static void setPendingFd(unixFile *pFile){
23638
- struct unixOpenCnt *pOpen = pFile->pOpen;
23600
+ unixInodeInfo *pInode = pFile->pInode;
2363923601
UnixUnusedFd *p = pFile->pUnused;
23640
- p->pNext = pOpen->pUnused;
23641
- pOpen->pUnused = p;
23602
+ p->pNext = pInode->pUnused;
23603
+ pInode->pUnused = p;
2364223604
pFile->h = -1;
2364323605
pFile->pUnused = 0;
2364423606
}
2364523607
2364623608
/*
23647
-** Lower the locking level on file descriptor pFile to locktype. locktype
23609
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2364823610
** must be either NO_LOCK or SHARED_LOCK.
2364923611
**
2365023612
** If the locking level of the file descriptor is already at or below
2365123613
** the requested locking level, this routine is a no-op.
2365223614
**
@@ -23654,35 +23616,33 @@
2365423616
** the byte range is divided into 2 parts and the first part is unlocked then
2365523617
** set to a read lock, then the other part is simply unlocked. This works
2365623618
** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
2365723619
** remove the write lock on a region when a read lock is set.
2365823620
*/
23659
-static int _posixUnlock(sqlite3_file *id, int locktype, int handleNFSUnlock){
23621
+static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
2366023622
unixFile *pFile = (unixFile*)id;
23661
- struct unixLockInfo *pLock;
23623
+ unixInodeInfo *pInode;
2366223624
struct flock lock;
2366323625
int rc = SQLITE_OK;
2366423626
int h;
2366523627
int tErrno; /* Error code from system call errors */
2366623628
2366723629
assert( pFile );
23668
- OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, locktype,
23669
- pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
23670
-
23671
- assert( locktype<=SHARED_LOCK );
23672
- if( pFile->locktype<=locktype ){
23673
- return SQLITE_OK;
23674
- }
23675
- if( CHECK_THREADID(pFile) ){
23676
- return SQLITE_MISUSE_BKPT;
23630
+ OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
23631
+ pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
23632
+ getpid()));
23633
+
23634
+ assert( eFileLock<=SHARED_LOCK );
23635
+ if( pFile->eFileLock<=eFileLock ){
23636
+ return SQLITE_OK;
2367723637
}
2367823638
unixEnterMutex();
2367923639
h = pFile->h;
23680
- pLock = pFile->pLock;
23681
- assert( pLock->cnt!=0 );
23682
- if( pFile->locktype>SHARED_LOCK ){
23683
- assert( pLock->locktype==pFile->locktype );
23640
+ pInode = pFile->pInode;
23641
+ assert( pInode->nShared!=0 );
23642
+ if( pFile->eFileLock>SHARED_LOCK ){
23643
+ assert( pInode->eFileLock==pFile->eFileLock );
2368423644
SimulateIOErrorBenign(1);
2368523645
SimulateIOError( h=(-1) )
2368623646
SimulateIOErrorBenign(0);
2368723647
2368823648
#ifndef NDEBUG
@@ -23692,13 +23652,15 @@
2369223652
** file changed. If the transaction counter is not updated,
2369323653
** other connections to the same file might not realize that
2369423654
** the file has changed and hence might not know to flush their
2369523655
** cache. The use of a stale cache can lead to database corruption.
2369623656
*/
23657
+#if 0
2369723658
assert( pFile->inNormalWrite==0
2369823659
|| pFile->dbUpdate==0
2369923660
|| pFile->transCntrChng==1 );
23661
+#endif
2370023662
pFile->inNormalWrite = 0;
2370123663
#endif
2370223664
2370323665
/* downgrading to a shared lock on NFS involves clearing the write lock
2370423666
** before establishing the readlock - to avoid a race condition we downgrade
@@ -23707,11 +23669,11 @@
2370723669
** 1: [WWWWW]
2370823670
** 2: [....W]
2370923671
** 3: [RRRRW]
2371023672
** 4: [RRRR.]
2371123673
*/
23712
- if( locktype==SHARED_LOCK ){
23674
+ if( eFileLock==SHARED_LOCK ){
2371323675
if( handleNFSUnlock ){
2371423676
off_t divSize = SHARED_SIZE - 1;
2371523677
2371623678
lock.l_type = F_UNLCK;
2371723679
lock.l_whence = SEEK_SET;
@@ -23767,78 +23729,75 @@
2376723729
lock.l_type = F_UNLCK;
2376823730
lock.l_whence = SEEK_SET;
2376923731
lock.l_start = PENDING_BYTE;
2377023732
lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
2377123733
if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23772
- pLock->locktype = SHARED_LOCK;
23734
+ pInode->eFileLock = SHARED_LOCK;
2377323735
}else{
2377423736
tErrno = errno;
2377523737
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2377623738
if( IS_LOCK_ERROR(rc) ){
2377723739
pFile->lastErrno = tErrno;
2377823740
}
2377923741
goto end_unlock;
2378023742
}
2378123743
}
23782
- if( locktype==NO_LOCK ){
23783
- struct unixOpenCnt *pOpen;
23784
-
23744
+ if( eFileLock==NO_LOCK ){
2378523745
/* Decrement the shared lock counter. Release the lock using an
2378623746
** OS call only when all threads in this same process have released
2378723747
** the lock.
2378823748
*/
23789
- pLock->cnt--;
23790
- if( pLock->cnt==0 ){
23749
+ pInode->nShared--;
23750
+ if( pInode->nShared==0 ){
2379123751
lock.l_type = F_UNLCK;
2379223752
lock.l_whence = SEEK_SET;
2379323753
lock.l_start = lock.l_len = 0L;
2379423754
SimulateIOErrorBenign(1);
2379523755
SimulateIOError( h=(-1) )
2379623756
SimulateIOErrorBenign(0);
2379723757
if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23798
- pLock->locktype = NO_LOCK;
23758
+ pInode->eFileLock = NO_LOCK;
2379923759
}else{
2380023760
tErrno = errno;
2380123761
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
2380223762
if( IS_LOCK_ERROR(rc) ){
2380323763
pFile->lastErrno = tErrno;
2380423764
}
23805
- pLock->locktype = NO_LOCK;
23806
- pFile->locktype = NO_LOCK;
23765
+ pInode->eFileLock = NO_LOCK;
23766
+ pFile->eFileLock = NO_LOCK;
2380723767
}
2380823768
}
2380923769
2381023770
/* Decrement the count of locks against this same file. When the
2381123771
** count reaches zero, close any other file descriptors whose close
2381223772
** was deferred because of outstanding locks.
2381323773
*/
23814
- pOpen = pFile->pOpen;
23815
- pOpen->nLock--;
23816
- assert( pOpen->nLock>=0 );
23817
- if( pOpen->nLock==0 ){
23774
+ pInode->nLock--;
23775
+ assert( pInode->nLock>=0 );
23776
+ if( pInode->nLock==0 ){
2381823777
int rc2 = closePendingFds(pFile);
2381923778
if( rc==SQLITE_OK ){
2382023779
rc = rc2;
2382123780
}
2382223781
}
2382323782
}
2382423783
2382523784
end_unlock:
2382623785
unixLeaveMutex();
23827
- if( rc==SQLITE_OK ) pFile->locktype = locktype;
23786
+ if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
2382823787
return rc;
2382923788
}
2383023789
2383123790
/*
23832
-** Lower the locking level on file descriptor pFile to locktype. locktype
23791
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2383323792
** must be either NO_LOCK or SHARED_LOCK.
2383423793
**
2383523794
** If the locking level of the file descriptor is already at or below
2383623795
** the requested locking level, this routine is a no-op.
2383723796
*/
23838
-static int unixUnlock(sqlite3_file *id, int locktype){
23839
- return _posixUnlock(id, locktype, 0);
23797
+static int unixUnlock(sqlite3_file *id, int eFileLock){
23798
+ return _posixUnlock(id, eFileLock, 0);
2384023799
}
2384123800
2384223801
/*
2384323802
** This function performs the parts of the "close file" operation
2384423803
** common to all locking schemes. It closes the directory and file
@@ -23875,11 +23834,11 @@
2387523834
}
2387623835
vxworksReleaseFileId(pFile->pId);
2387723836
pFile->pId = 0;
2387823837
}
2387923838
#endif
23880
- OSTRACE2("CLOSE %-3d\n", pFile->h);
23839
+ OSTRACE(("CLOSE %-3d\n", pFile->h));
2388123840
OpenCounter(-1);
2388223841
sqlite3_free(pFile->pUnused);
2388323842
memset(pFile, 0, sizeof(unixFile));
2388423843
}
2388523844
return SQLITE_OK;
@@ -23892,20 +23851,19 @@
2389223851
int rc = SQLITE_OK;
2389323852
if( id ){
2389423853
unixFile *pFile = (unixFile *)id;
2389523854
unixUnlock(id, NO_LOCK);
2389623855
unixEnterMutex();
23897
- if( pFile->pOpen && pFile->pOpen->nLock ){
23856
+ if( pFile->pInode && pFile->pInode->nLock ){
2389823857
/* If there are outstanding locks, do not actually close the file just
2389923858
** yet because that would clear those locks. Instead, add the file
23900
- ** descriptor to pOpen->pUnused list. It will be automatically closed
23859
+ ** descriptor to pInode->pUnused list. It will be automatically closed
2390123860
** when the last lock is cleared.
2390223861
*/
2390323862
setPendingFd(pFile);
2390423863
}
23905
- releaseLockInfo(pFile->pLock);
23906
- releaseOpenCnt(pFile->pOpen);
23864
+ releaseInodeInfo(pFile);
2390723865
rc = closeUnixFile(id);
2390823866
unixLeaveMutex();
2390923867
}
2391023868
return rc;
2391123869
}
@@ -24000,26 +23958,26 @@
2400023958
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2400123959
2400223960
assert( pFile );
2400323961
2400423962
/* Check if a thread in this process holds such a lock */
24005
- if( pFile->locktype>SHARED_LOCK ){
23963
+ if( pFile->eFileLock>SHARED_LOCK ){
2400623964
/* Either this connection or some other connection in the same process
2400723965
** holds a lock on the file. No need to check further. */
2400823966
reserved = 1;
2400923967
}else{
2401023968
/* The lock is held if and only if the lockfile exists */
2401123969
const char *zLockFile = (const char*)pFile->lockingContext;
2401223970
reserved = access(zLockFile, 0)==0;
2401323971
}
24014
- OSTRACE4("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved);
23972
+ OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
2401523973
*pResOut = reserved;
2401623974
return rc;
2401723975
}
2401823976
2401923977
/*
24020
-** Lock the file with the lock specified by parameter locktype - one
23978
+** Lock the file with the lock specified by parameter eFileLock - one
2402123979
** of the following:
2402223980
**
2402323981
** (1) SHARED_LOCK
2402423982
** (2) RESERVED_LOCK
2402523983
** (3) PENDING_LOCK
@@ -24041,22 +23999,22 @@
2404123999
** routine to lower a locking level.
2404224000
**
2404324001
** With dotfile locking, we really only support state (4): EXCLUSIVE.
2404424002
** But we track the other locking levels internally.
2404524003
*/
24046
-static int dotlockLock(sqlite3_file *id, int locktype) {
24004
+static int dotlockLock(sqlite3_file *id, int eFileLock) {
2404724005
unixFile *pFile = (unixFile*)id;
2404824006
int fd;
2404924007
char *zLockFile = (char *)pFile->lockingContext;
2405024008
int rc = SQLITE_OK;
2405124009
2405224010
2405324011
/* If we have any lock, then the lock file already exists. All we have
2405424012
** to do is adjust our internal record of the lock level.
2405524013
*/
24056
- if( pFile->locktype > NO_LOCK ){
24057
- pFile->locktype = locktype;
24014
+ if( pFile->eFileLock > NO_LOCK ){
24015
+ pFile->eFileLock = eFileLock;
2405824016
#if !OS_VXWORKS
2405924017
/* Always update the timestamp on the old file */
2406024018
utimes(zLockFile, NULL);
2406124019
#endif
2406224020
return SQLITE_OK;
@@ -24081,47 +24039,47 @@
2408124039
pFile->lastErrno = errno;
2408224040
rc = SQLITE_IOERR_CLOSE;
2408324041
}
2408424042
2408524043
/* got it, set the type and return ok */
24086
- pFile->locktype = locktype;
24044
+ pFile->eFileLock = eFileLock;
2408724045
return rc;
2408824046
}
2408924047
2409024048
/*
24091
-** Lower the locking level on file descriptor pFile to locktype. locktype
24049
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2409224050
** must be either NO_LOCK or SHARED_LOCK.
2409324051
**
2409424052
** If the locking level of the file descriptor is already at or below
2409524053
** the requested locking level, this routine is a no-op.
2409624054
**
2409724055
** When the locking level reaches NO_LOCK, delete the lock file.
2409824056
*/
24099
-static int dotlockUnlock(sqlite3_file *id, int locktype) {
24057
+static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
2410024058
unixFile *pFile = (unixFile*)id;
2410124059
char *zLockFile = (char *)pFile->lockingContext;
2410224060
2410324061
assert( pFile );
24104
- OSTRACE5("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, locktype,
24105
- pFile->locktype, getpid());
24106
- assert( locktype<=SHARED_LOCK );
24062
+ OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
24063
+ pFile->eFileLock, getpid()));
24064
+ assert( eFileLock<=SHARED_LOCK );
2410724065
2410824066
/* no-op if possible */
24109
- if( pFile->locktype==locktype ){
24067
+ if( pFile->eFileLock==eFileLock ){
2411024068
return SQLITE_OK;
2411124069
}
2411224070
2411324071
/* To downgrade to shared, simply update our internal notion of the
2411424072
** lock state. No need to mess with the file on disk.
2411524073
*/
24116
- if( locktype==SHARED_LOCK ){
24117
- pFile->locktype = SHARED_LOCK;
24074
+ if( eFileLock==SHARED_LOCK ){
24075
+ pFile->eFileLock = SHARED_LOCK;
2411824076
return SQLITE_OK;
2411924077
}
2412024078
2412124079
/* To fully unlock the database, delete the lock file */
24122
- assert( locktype==NO_LOCK );
24080
+ assert( eFileLock==NO_LOCK );
2412324081
if( unlink(zLockFile) ){
2412424082
int rc = 0;
2412524083
int tErrno = errno;
2412624084
if( ENOENT != tErrno ){
2412724085
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
@@ -24129,11 +24087,11 @@
2412924087
if( IS_LOCK_ERROR(rc) ){
2413024088
pFile->lastErrno = tErrno;
2413124089
}
2413224090
return rc;
2413324091
}
24134
- pFile->locktype = NO_LOCK;
24092
+ pFile->eFileLock = NO_LOCK;
2413524093
return SQLITE_OK;
2413624094
}
2413724095
2413824096
/*
2413924097
** Close a file. Make sure the lock has been released before closing.
@@ -24182,11 +24140,11 @@
2418224140
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2418324141
2418424142
assert( pFile );
2418524143
2418624144
/* Check if a thread in this process holds such a lock */
24187
- if( pFile->locktype>SHARED_LOCK ){
24145
+ if( pFile->eFileLock>SHARED_LOCK ){
2418824146
reserved = 1;
2418924147
}
2419024148
2419124149
/* Otherwise see if some other process holds it. */
2419224150
if( !reserved ){
@@ -24213,11 +24171,11 @@
2421324171
pFile->lastErrno = tErrno;
2421424172
rc = lrc;
2421524173
}
2421624174
}
2421724175
}
24218
- OSTRACE4("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved);
24176
+ OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
2421924177
2422024178
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2422124179
if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2422224180
rc = SQLITE_OK;
2422324181
reserved=1;
@@ -24226,11 +24184,11 @@
2422624184
*pResOut = reserved;
2422724185
return rc;
2422824186
}
2422924187
2423024188
/*
24231
-** Lock the file with the lock specified by parameter locktype - one
24189
+** Lock the file with the lock specified by parameter eFileLock - one
2423224190
** of the following:
2423324191
**
2423424192
** (1) SHARED_LOCK
2423524193
** (2) RESERVED_LOCK
2423624194
** (3) PENDING_LOCK
@@ -24254,20 +24212,20 @@
2425424212
** access the file.
2425524213
**
2425624214
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2425724215
** routine to lower a locking level.
2425824216
*/
24259
-static int flockLock(sqlite3_file *id, int locktype) {
24217
+static int flockLock(sqlite3_file *id, int eFileLock) {
2426024218
int rc = SQLITE_OK;
2426124219
unixFile *pFile = (unixFile*)id;
2426224220
2426324221
assert( pFile );
2426424222
2426524223
/* if we already have a lock, it is exclusive.
2426624224
** Just adjust level and punt on outta here. */
24267
- if (pFile->locktype > NO_LOCK) {
24268
- pFile->locktype = locktype;
24225
+ if (pFile->eFileLock > NO_LOCK) {
24226
+ pFile->eFileLock = eFileLock;
2426924227
return SQLITE_OK;
2427024228
}
2427124229
2427224230
/* grab an exclusive lock */
2427324231
@@ -24278,14 +24236,14 @@
2427824236
if( IS_LOCK_ERROR(rc) ){
2427924237
pFile->lastErrno = tErrno;
2428024238
}
2428124239
} else {
2428224240
/* got it, set the type and return ok */
24283
- pFile->locktype = locktype;
24241
+ pFile->eFileLock = eFileLock;
2428424242
}
24285
- OSTRACE4("LOCK %d %s %s (flock)\n", pFile->h, locktypeName(locktype),
24286
- rc==SQLITE_OK ? "ok" : "failed");
24243
+ OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
24244
+ rc==SQLITE_OK ? "ok" : "failed"));
2428724245
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
2428824246
if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
2428924247
rc = SQLITE_BUSY;
2429024248
}
2429124249
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
@@ -24292,32 +24250,32 @@
2429224250
return rc;
2429324251
}
2429424252
2429524253
2429624254
/*
24297
-** Lower the locking level on file descriptor pFile to locktype. locktype
24255
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2429824256
** must be either NO_LOCK or SHARED_LOCK.
2429924257
**
2430024258
** If the locking level of the file descriptor is already at or below
2430124259
** the requested locking level, this routine is a no-op.
2430224260
*/
24303
-static int flockUnlock(sqlite3_file *id, int locktype) {
24261
+static int flockUnlock(sqlite3_file *id, int eFileLock) {
2430424262
unixFile *pFile = (unixFile*)id;
2430524263
2430624264
assert( pFile );
24307
- OSTRACE5("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, locktype,
24308
- pFile->locktype, getpid());
24309
- assert( locktype<=SHARED_LOCK );
24265
+ OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
24266
+ pFile->eFileLock, getpid()));
24267
+ assert( eFileLock<=SHARED_LOCK );
2431024268
2431124269
/* no-op if possible */
24312
- if( pFile->locktype==locktype ){
24270
+ if( pFile->eFileLock==eFileLock ){
2431324271
return SQLITE_OK;
2431424272
}
2431524273
2431624274
/* shared can just be set because we always have an exclusive */
24317
- if (locktype==SHARED_LOCK) {
24318
- pFile->locktype = locktype;
24275
+ if (eFileLock==SHARED_LOCK) {
24276
+ pFile->eFileLock = eFileLock;
2431924277
return SQLITE_OK;
2432024278
}
2432124279
2432224280
/* no, really, unlock. */
2432324281
int rc = flock(pFile->h, LOCK_UN);
@@ -24333,11 +24291,11 @@
2433324291
}
2433424292
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
2433524293
2433624294
return r;
2433724295
} else {
24338
- pFile->locktype = NO_LOCK;
24296
+ pFile->eFileLock = NO_LOCK;
2433924297
return SQLITE_OK;
2434024298
}
2434124299
}
2434224300
2434324301
/*
@@ -24381,41 +24339,41 @@
2438124339
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2438224340
2438324341
assert( pFile );
2438424342
2438524343
/* Check if a thread in this process holds such a lock */
24386
- if( pFile->locktype>SHARED_LOCK ){
24344
+ if( pFile->eFileLock>SHARED_LOCK ){
2438724345
reserved = 1;
2438824346
}
2438924347
2439024348
/* Otherwise see if some other process holds it. */
2439124349
if( !reserved ){
24392
- sem_t *pSem = pFile->pOpen->pSem;
24350
+ sem_t *pSem = pFile->pInode->pSem;
2439324351
struct stat statBuf;
2439424352
2439524353
if( sem_trywait(pSem)==-1 ){
2439624354
int tErrno = errno;
2439724355
if( EAGAIN != tErrno ){
2439824356
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
2439924357
pFile->lastErrno = tErrno;
2440024358
} else {
2440124359
/* someone else has the lock when we are in NO_LOCK */
24402
- reserved = (pFile->locktype < SHARED_LOCK);
24360
+ reserved = (pFile->eFileLock < SHARED_LOCK);
2440324361
}
2440424362
}else{
2440524363
/* we could have it if we want it */
2440624364
sem_post(pSem);
2440724365
}
2440824366
}
24409
- OSTRACE4("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved);
24367
+ OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
2441024368
2441124369
*pResOut = reserved;
2441224370
return rc;
2441324371
}
2441424372
2441524373
/*
24416
-** Lock the file with the lock specified by parameter locktype - one
24374
+** Lock the file with the lock specified by parameter eFileLock - one
2441724375
** of the following:
2441824376
**
2441924377
** (1) SHARED_LOCK
2442024378
** (2) RESERVED_LOCK
2442124379
** (3) PENDING_LOCK
@@ -24439,20 +24397,20 @@
2443924397
** access the file.
2444024398
**
2444124399
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2444224400
** routine to lower a locking level.
2444324401
*/
24444
-static int semLock(sqlite3_file *id, int locktype) {
24402
+static int semLock(sqlite3_file *id, int eFileLock) {
2444524403
unixFile *pFile = (unixFile*)id;
2444624404
int fd;
24447
- sem_t *pSem = pFile->pOpen->pSem;
24405
+ sem_t *pSem = pFile->pInode->pSem;
2444824406
int rc = SQLITE_OK;
2444924407
2445024408
/* if we already have a lock, it is exclusive.
2445124409
** Just adjust level and punt on outta here. */
24452
- if (pFile->locktype > NO_LOCK) {
24453
- pFile->locktype = locktype;
24410
+ if (pFile->eFileLock > NO_LOCK) {
24411
+ pFile->eFileLock = eFileLock;
2445424412
rc = SQLITE_OK;
2445524413
goto sem_end_lock;
2445624414
}
2445724415
2445824416
/* lock semaphore now but bail out when already locked. */
@@ -24460,41 +24418,41 @@
2446024418
rc = SQLITE_BUSY;
2446124419
goto sem_end_lock;
2446224420
}
2446324421
2446424422
/* got it, set the type and return ok */
24465
- pFile->locktype = locktype;
24423
+ pFile->eFileLock = eFileLock;
2446624424
2446724425
sem_end_lock:
2446824426
return rc;
2446924427
}
2447024428
2447124429
/*
24472
-** Lower the locking level on file descriptor pFile to locktype. locktype
24430
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2447324431
** must be either NO_LOCK or SHARED_LOCK.
2447424432
**
2447524433
** If the locking level of the file descriptor is already at or below
2447624434
** the requested locking level, this routine is a no-op.
2447724435
*/
24478
-static int semUnlock(sqlite3_file *id, int locktype) {
24436
+static int semUnlock(sqlite3_file *id, int eFileLock) {
2447924437
unixFile *pFile = (unixFile*)id;
24480
- sem_t *pSem = pFile->pOpen->pSem;
24438
+ sem_t *pSem = pFile->pInode->pSem;
2448124439
2448224440
assert( pFile );
2448324441
assert( pSem );
24484
- OSTRACE5("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, locktype,
24485
- pFile->locktype, getpid());
24486
- assert( locktype<=SHARED_LOCK );
24442
+ OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
24443
+ pFile->eFileLock, getpid()));
24444
+ assert( eFileLock<=SHARED_LOCK );
2448724445
2448824446
/* no-op if possible */
24489
- if( pFile->locktype==locktype ){
24447
+ if( pFile->eFileLock==eFileLock ){
2449024448
return SQLITE_OK;
2449124449
}
2449224450
2449324451
/* shared can just be set because we always have an exclusive */
24494
- if (locktype==SHARED_LOCK) {
24495
- pFile->locktype = locktype;
24452
+ if (eFileLock==SHARED_LOCK) {
24453
+ pFile->eFileLock = eFileLock;
2449624454
return SQLITE_OK;
2449724455
}
2449824456
2449924457
/* no, really unlock. */
2450024458
if ( sem_post(pSem)==-1 ) {
@@ -24503,11 +24461,11 @@
2450324461
if( IS_LOCK_ERROR(rc) ){
2450424462
pFile->lastErrno = tErrno;
2450524463
}
2450624464
return rc;
2450724465
}
24508
- pFile->locktype = NO_LOCK;
24466
+ pFile->eFileLock = NO_LOCK;
2450924467
return SQLITE_OK;
2451024468
}
2451124469
2451224470
/*
2451324471
** Close a file.
@@ -24516,12 +24474,11 @@
2451624474
if( id ){
2451724475
unixFile *pFile = (unixFile*)id;
2451824476
semUnlock(id, NO_LOCK);
2451924477
assert( pFile );
2452024478
unixEnterMutex();
24521
- releaseLockInfo(pFile->pLock);
24522
- releaseOpenCnt(pFile->pOpen);
24479
+ releaseInodeInfo(pFile);
2452324480
unixLeaveMutex();
2452424481
closeUnixFile(id);
2452524482
}
2452624483
return SQLITE_OK;
2452724484
}
@@ -24586,19 +24543,19 @@
2458624543
pb.startEndFlag = 0;
2458724544
pb.offset = offset;
2458824545
pb.length = length;
2458924546
pb.fd = pFile->h;
2459024547
24591
- OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
24548
+ OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
2459224549
(setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
24593
- offset, length);
24550
+ offset, length));
2459424551
err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
2459524552
if ( err==-1 ) {
2459624553
int rc;
2459724554
int tErrno = errno;
24598
- OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
24599
- path, tErrno, strerror(tErrno));
24555
+ OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
24556
+ path, tErrno, strerror(tErrno)));
2460024557
#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
2460124558
rc = SQLITE_BUSY;
2460224559
#else
2460324560
rc = sqliteErrorFromPosixError(tErrno,
2460424561
setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
@@ -24629,14 +24586,14 @@
2462924586
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2463024587
if( context->reserved ){
2463124588
*pResOut = 1;
2463224589
return SQLITE_OK;
2463324590
}
24634
- unixEnterMutex(); /* Because pFile->pLock is shared across threads */
24591
+ unixEnterMutex(); /* Because pFile->pInode is shared across threads */
2463524592
2463624593
/* Check if a thread in this process holds such a lock */
24637
- if( pFile->pLock->locktype>SHARED_LOCK ){
24594
+ if( pFile->pInode->eFileLock>SHARED_LOCK ){
2463824595
reserved = 1;
2463924596
}
2464024597
2464124598
/* Otherwise see if some other process holds it.
2464224599
*/
@@ -24655,18 +24612,18 @@
2465524612
rc=lrc;
2465624613
}
2465724614
}
2465824615
2465924616
unixLeaveMutex();
24660
- OSTRACE4("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved);
24617
+ OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
2466124618
2466224619
*pResOut = reserved;
2466324620
return rc;
2466424621
}
2466524622
2466624623
/*
24667
-** Lock the file with the lock specified by parameter locktype - one
24624
+** Lock the file with the lock specified by parameter eFileLock - one
2466824625
** of the following:
2466924626
**
2467024627
** (1) SHARED_LOCK
2467124628
** (2) RESERVED_LOCK
2467224629
** (3) PENDING_LOCK
@@ -24685,84 +24642,76 @@
2468524642
** PENDING -> EXCLUSIVE
2468624643
**
2468724644
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2468824645
** routine to lower a locking level.
2468924646
*/
24690
-static int afpLock(sqlite3_file *id, int locktype){
24647
+static int afpLock(sqlite3_file *id, int eFileLock){
2469124648
int rc = SQLITE_OK;
2469224649
unixFile *pFile = (unixFile*)id;
24693
- struct unixLockInfo *pLock = pFile->pLock;
24650
+ unixInodeInfo *pInode = pFile->pInode;
2469424651
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2469524652
2469624653
assert( pFile );
24697
- OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
24698
- locktypeName(locktype), locktypeName(pFile->locktype),
24699
- locktypeName(pLock->locktype), pLock->cnt , getpid());
24654
+ OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
24655
+ azFileLock(eFileLock), azFileLock(pFile->eFileLock),
24656
+ azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
2470024657
2470124658
/* If there is already a lock of this type or more restrictive on the
2470224659
** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
2470324660
** unixEnterMutex() hasn't been called yet.
2470424661
*/
24705
- if( pFile->locktype>=locktype ){
24706
- OSTRACE3("LOCK %d %s ok (already held) (afp)\n", pFile->h,
24707
- locktypeName(locktype));
24662
+ if( pFile->eFileLock>=eFileLock ){
24663
+ OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
24664
+ azFileLock(eFileLock)));
2470824665
return SQLITE_OK;
2470924666
}
2471024667
2471124668
/* Make sure the locking sequence is correct
2471224669
** (1) We never move from unlocked to anything higher than shared lock.
2471324670
** (2) SQLite never explicitly requests a pendig lock.
2471424671
** (3) A shared lock is always held when a reserve lock is requested.
2471524672
*/
24716
- assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
24717
- assert( locktype!=PENDING_LOCK );
24718
- assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
24673
+ assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
24674
+ assert( eFileLock!=PENDING_LOCK );
24675
+ assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
2471924676
24720
- /* This mutex is needed because pFile->pLock is shared across threads
24677
+ /* This mutex is needed because pFile->pInode is shared across threads
2472124678
*/
2472224679
unixEnterMutex();
24723
-
24724
- /* Make sure the current thread owns the pFile.
24725
- */
24726
- rc = transferOwnership(pFile);
24727
- if( rc!=SQLITE_OK ){
24728
- unixLeaveMutex();
24729
- return rc;
24730
- }
24731
- pLock = pFile->pLock;
24680
+ pInode = pFile->pInode;
2473224681
2473324682
/* If some thread using this PID has a lock via a different unixFile*
2473424683
** handle that precludes the requested lock, return BUSY.
2473524684
*/
24736
- if( (pFile->locktype!=pLock->locktype &&
24737
- (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
24685
+ if( (pFile->eFileLock!=pInode->eFileLock &&
24686
+ (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
2473824687
){
2473924688
rc = SQLITE_BUSY;
2474024689
goto afp_end_lock;
2474124690
}
2474224691
2474324692
/* If a SHARED lock is requested, and some thread using this PID already
2474424693
** has a SHARED or RESERVED lock, then increment reference counts and
2474524694
** return SQLITE_OK.
2474624695
*/
24747
- if( locktype==SHARED_LOCK &&
24748
- (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
24749
- assert( locktype==SHARED_LOCK );
24750
- assert( pFile->locktype==0 );
24751
- assert( pLock->cnt>0 );
24752
- pFile->locktype = SHARED_LOCK;
24753
- pLock->cnt++;
24754
- pFile->pOpen->nLock++;
24696
+ if( eFileLock==SHARED_LOCK &&
24697
+ (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
24698
+ assert( eFileLock==SHARED_LOCK );
24699
+ assert( pFile->eFileLock==0 );
24700
+ assert( pInode->nShared>0 );
24701
+ pFile->eFileLock = SHARED_LOCK;
24702
+ pInode->nShared++;
24703
+ pInode->nLock++;
2475524704
goto afp_end_lock;
2475624705
}
2475724706
2475824707
/* A PENDING lock is needed before acquiring a SHARED lock and before
2475924708
** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
2476024709
** be released.
2476124710
*/
24762
- if( locktype==SHARED_LOCK
24763
- || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
24711
+ if( eFileLock==SHARED_LOCK
24712
+ || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
2476424713
){
2476524714
int failed;
2476624715
failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
2476724716
if (failed) {
2476824717
rc = failed;
@@ -24771,24 +24720,24 @@
2477124720
}
2477224721
2477324722
/* If control gets to this point, then actually go ahead and make
2477424723
** operating system calls for the specified lock.
2477524724
*/
24776
- if( locktype==SHARED_LOCK ){
24725
+ if( eFileLock==SHARED_LOCK ){
2477724726
int lrc1, lrc2, lrc1Errno;
2477824727
long lk, mask;
2477924728
24780
- assert( pLock->cnt==0 );
24781
- assert( pLock->locktype==0 );
24729
+ assert( pInode->nShared==0 );
24730
+ assert( pInode->eFileLock==0 );
2478224731
2478324732
mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
2478424733
/* Now get the read-lock SHARED_LOCK */
2478524734
/* note that the quality of the randomness doesn't matter that much */
2478624735
lk = random();
24787
- pLock->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
24736
+ pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
2478824737
lrc1 = afpSetLock(context->dbPath, pFile,
24789
- SHARED_FIRST+pLock->sharedByte, 1, 1);
24738
+ SHARED_FIRST+pInode->sharedByte, 1, 1);
2479024739
if( IS_LOCK_ERROR(lrc1) ){
2479124740
lrc1Errno = pFile->lastErrno;
2479224741
}
2479324742
/* Drop the temporary PENDING lock */
2479424743
lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
@@ -24801,46 +24750,46 @@
2480124750
rc = lrc2;
2480224751
goto afp_end_lock;
2480324752
} else if( lrc1 != SQLITE_OK ) {
2480424753
rc = lrc1;
2480524754
} else {
24806
- pFile->locktype = SHARED_LOCK;
24807
- pFile->pOpen->nLock++;
24808
- pLock->cnt = 1;
24755
+ pFile->eFileLock = SHARED_LOCK;
24756
+ pInode->nLock++;
24757
+ pInode->nShared = 1;
2480924758
}
24810
- }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
24759
+ }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
2481124760
/* We are trying for an exclusive lock but another thread in this
2481224761
** same process is still holding a shared lock. */
2481324762
rc = SQLITE_BUSY;
2481424763
}else{
2481524764
/* The request was for a RESERVED or EXCLUSIVE lock. It is
2481624765
** assumed that there is a SHARED or greater lock on the file
2481724766
** already.
2481824767
*/
2481924768
int failed = 0;
24820
- assert( 0!=pFile->locktype );
24821
- if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
24769
+ assert( 0!=pFile->eFileLock );
24770
+ if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
2482224771
/* Acquire a RESERVED lock */
2482324772
failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
2482424773
if( !failed ){
2482524774
context->reserved = 1;
2482624775
}
2482724776
}
24828
- if (!failed && locktype == EXCLUSIVE_LOCK) {
24777
+ if (!failed && eFileLock == EXCLUSIVE_LOCK) {
2482924778
/* Acquire an EXCLUSIVE lock */
2483024779
2483124780
/* Remove the shared lock before trying the range. we'll need to
2483224781
** reestablish the shared lock if we can't get the afpUnlock
2483324782
*/
2483424783
if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
24835
- pLock->sharedByte, 1, 0)) ){
24784
+ pInode->sharedByte, 1, 0)) ){
2483624785
int failed2 = SQLITE_OK;
2483724786
/* now attemmpt to get the exclusive lock range */
2483824787
failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
2483924788
SHARED_SIZE, 1);
2484024789
if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
24841
- SHARED_FIRST + pLock->sharedByte, 1, 1)) ){
24790
+ SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
2484224791
/* Can't reestablish the shared lock. Sqlite can't deal, this is
2484324792
** a critical I/O error
2484424793
*/
2484524794
rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
2484624795
SQLITE_IOERR_LOCK;
@@ -24854,57 +24803,55 @@
2485424803
rc = failed;
2485524804
}
2485624805
}
2485724806
2485824807
if( rc==SQLITE_OK ){
24859
- pFile->locktype = locktype;
24860
- pLock->locktype = locktype;
24861
- }else if( locktype==EXCLUSIVE_LOCK ){
24862
- pFile->locktype = PENDING_LOCK;
24863
- pLock->locktype = PENDING_LOCK;
24808
+ pFile->eFileLock = eFileLock;
24809
+ pInode->eFileLock = eFileLock;
24810
+ }else if( eFileLock==EXCLUSIVE_LOCK ){
24811
+ pFile->eFileLock = PENDING_LOCK;
24812
+ pInode->eFileLock = PENDING_LOCK;
2486424813
}
2486524814
2486624815
afp_end_lock:
2486724816
unixLeaveMutex();
24868
- OSTRACE4("LOCK %d %s %s (afp)\n", pFile->h, locktypeName(locktype),
24869
- rc==SQLITE_OK ? "ok" : "failed");
24817
+ OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
24818
+ rc==SQLITE_OK ? "ok" : "failed"));
2487024819
return rc;
2487124820
}
2487224821
2487324822
/*
24874
-** Lower the locking level on file descriptor pFile to locktype. locktype
24823
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2487524824
** must be either NO_LOCK or SHARED_LOCK.
2487624825
**
2487724826
** If the locking level of the file descriptor is already at or below
2487824827
** the requested locking level, this routine is a no-op.
2487924828
*/
24880
-static int afpUnlock(sqlite3_file *id, int locktype) {
24829
+static int afpUnlock(sqlite3_file *id, int eFileLock) {
2488124830
int rc = SQLITE_OK;
2488224831
unixFile *pFile = (unixFile*)id;
24883
- struct unixLockInfo *pLock;
24832
+ unixInodeInfo *pInode;
2488424833
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
2488524834
int skipShared = 0;
2488624835
#ifdef SQLITE_TEST
2488724836
int h = pFile->h;
2488824837
#endif
2488924838
2489024839
assert( pFile );
24891
- OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, locktype,
24892
- pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
24893
-
24894
- assert( locktype<=SHARED_LOCK );
24895
- if( pFile->locktype<=locktype ){
24896
- return SQLITE_OK;
24897
- }
24898
- if( CHECK_THREADID(pFile) ){
24899
- return SQLITE_MISUSE_BKPT;
24840
+ OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
24841
+ pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
24842
+ getpid()));
24843
+
24844
+ assert( eFileLock<=SHARED_LOCK );
24845
+ if( pFile->eFileLock<=eFileLock ){
24846
+ return SQLITE_OK;
2490024847
}
2490124848
unixEnterMutex();
24902
- pLock = pFile->pLock;
24903
- assert( pLock->cnt!=0 );
24904
- if( pFile->locktype>SHARED_LOCK ){
24905
- assert( pLock->locktype==pFile->locktype );
24849
+ pInode = pFile->pInode;
24850
+ assert( pInode->nShared!=0 );
24851
+ if( pFile->eFileLock>SHARED_LOCK ){
24852
+ assert( pInode->eFileLock==pFile->eFileLock );
2490624853
SimulateIOErrorBenign(1);
2490724854
SimulateIOError( h=(-1) )
2490824855
SimulateIOErrorBenign(0);
2490924856
2491024857
#ifndef NDEBUG
@@ -24920,66 +24867,64 @@
2492024867
|| pFile->dbUpdate==0
2492124868
|| pFile->transCntrChng==1 );
2492224869
pFile->inNormalWrite = 0;
2492324870
#endif
2492424871
24925
- if( pFile->locktype==EXCLUSIVE_LOCK ){
24872
+ if( pFile->eFileLock==EXCLUSIVE_LOCK ){
2492624873
rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
24927
- if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1) ){
24874
+ if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
2492824875
/* only re-establish the shared lock if necessary */
24929
- int sharedLockByte = SHARED_FIRST+pLock->sharedByte;
24876
+ int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
2493024877
rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
2493124878
} else {
2493224879
skipShared = 1;
2493324880
}
2493424881
}
24935
- if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
24882
+ if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
2493624883
rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
2493724884
}
24938
- if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK && context->reserved ){
24885
+ if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
2493924886
rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
2494024887
if( !rc ){
2494124888
context->reserved = 0;
2494224889
}
2494324890
}
24944
- if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1)){
24945
- pLock->locktype = SHARED_LOCK;
24891
+ if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
24892
+ pInode->eFileLock = SHARED_LOCK;
2494624893
}
2494724894
}
24948
- if( rc==SQLITE_OK && locktype==NO_LOCK ){
24895
+ if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
2494924896
2495024897
/* Decrement the shared lock counter. Release the lock using an
2495124898
** OS call only when all threads in this same process have released
2495224899
** the lock.
2495324900
*/
24954
- unsigned long long sharedLockByte = SHARED_FIRST+pLock->sharedByte;
24955
- pLock->cnt--;
24956
- if( pLock->cnt==0 ){
24901
+ unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
24902
+ pInode->nShared--;
24903
+ if( pInode->nShared==0 ){
2495724904
SimulateIOErrorBenign(1);
2495824905
SimulateIOError( h=(-1) )
2495924906
SimulateIOErrorBenign(0);
2496024907
if( !skipShared ){
2496124908
rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
2496224909
}
2496324910
if( !rc ){
24964
- pLock->locktype = NO_LOCK;
24965
- pFile->locktype = NO_LOCK;
24911
+ pInode->eFileLock = NO_LOCK;
24912
+ pFile->eFileLock = NO_LOCK;
2496624913
}
2496724914
}
2496824915
if( rc==SQLITE_OK ){
24969
- struct unixOpenCnt *pOpen = pFile->pOpen;
24970
-
24971
- pOpen->nLock--;
24972
- assert( pOpen->nLock>=0 );
24973
- if( pOpen->nLock==0 ){
24916
+ pInode->nLock--;
24917
+ assert( pInode->nLock>=0 );
24918
+ if( pInode->nLock==0 ){
2497424919
rc = closePendingFds(pFile);
2497524920
}
2497624921
}
2497724922
}
2497824923
2497924924
unixLeaveMutex();
24980
- if( rc==SQLITE_OK ) pFile->locktype = locktype;
24925
+ if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
2498124926
return rc;
2498224927
}
2498324928
2498424929
/*
2498524930
** Close a file & cleanup AFP specific locking context
@@ -24988,20 +24933,19 @@
2498824933
int rc = SQLITE_OK;
2498924934
if( id ){
2499024935
unixFile *pFile = (unixFile*)id;
2499124936
afpUnlock(id, NO_LOCK);
2499224937
unixEnterMutex();
24993
- if( pFile->pOpen && pFile->pOpen->nLock ){
24938
+ if( pFile->pInode && pFile->pInode->nLock ){
2499424939
/* If there are outstanding locks, do not actually close the file just
2499524940
** yet because that would clear those locks. Instead, add the file
24996
- ** descriptor to pOpen->aPending. It will be automatically closed when
24941
+ ** descriptor to pInode->aPending. It will be automatically closed when
2499724942
** the last lock is cleared.
2499824943
*/
2499924944
setPendingFd(pFile);
2500024945
}
25001
- releaseLockInfo(pFile->pLock);
25002
- releaseOpenCnt(pFile->pOpen);
24946
+ releaseInodeInfo(pFile);
2500324947
sqlite3_free(pFile->lockingContext);
2500424948
rc = closeUnixFile(id);
2500524949
unixLeaveMutex();
2500624950
}
2500724951
return rc;
@@ -25020,18 +24964,18 @@
2502024964
/******************************************************************************
2502124965
*************************** Begin NFS Locking ********************************/
2502224966
2502324967
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2502424968
/*
25025
- ** Lower the locking level on file descriptor pFile to locktype. locktype
24969
+ ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2502624970
** must be either NO_LOCK or SHARED_LOCK.
2502724971
**
2502824972
** If the locking level of the file descriptor is already at or below
2502924973
** the requested locking level, this routine is a no-op.
2503024974
*/
25031
-static int nfsUnlock(sqlite3_file *id, int locktype){
25032
- return _posixUnlock(id, locktype, 1);
24975
+static int nfsUnlock(sqlite3_file *id, int eFileLock){
24976
+ return _posixUnlock(id, eFileLock, 1);
2503324977
}
2503424978
2503524979
#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
2503624980
/*
2503724981
** The code above is the NFS lock implementation. The code is specific
@@ -25091,11 +25035,11 @@
2509125035
#endif
2509225036
TIMER_END;
2509325037
if( got<0 ){
2509425038
((unixFile*)id)->lastErrno = errno;
2509525039
}
25096
- OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
25040
+ OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
2509725041
return got;
2509825042
}
2509925043
2510025044
/*
2510125045
** Read data from a file into a buffer. Return SQLITE_OK if all
@@ -25112,14 +25056,16 @@
2511225056
int got;
2511325057
assert( id );
2511425058
2511525059
/* If this is a database file (not a journal, master-journal or temp
2511625060
** file), the bytes in the locking range should never be read or written. */
25061
+#if 0
2511725062
assert( pFile->pUnused==0
2511825063
|| offset>=PENDING_BYTE+512
2511925064
|| offset+amt<=PENDING_BYTE
2512025065
);
25066
+#endif
2512125067
2512225068
got = seekAndRead(pFile, offset, pBuf, amt);
2512325069
if( got==amt ){
2512425070
return SQLITE_OK;
2512525071
}else if( got<0 ){
@@ -25165,11 +25111,11 @@
2516525111
TIMER_END;
2516625112
if( got<0 ){
2516725113
((unixFile*)id)->lastErrno = errno;
2516825114
}
2516925115
25170
- OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
25116
+ OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
2517125117
return got;
2517225118
}
2517325119
2517425120
2517525121
/*
@@ -25187,14 +25133,16 @@
2518725133
assert( id );
2518825134
assert( amt>0 );
2518925135
2519025136
/* If this is a database file (not a journal, master-journal or temp
2519125137
** file), the bytes in the locking range should never be read or written. */
25138
+#if 0
2519225139
assert( pFile->pUnused==0
2519325140
|| offset>=PENDING_BYTE+512
2519425141
|| offset+amt<=PENDING_BYTE
2519525142
);
25143
+#endif
2519625144
2519725145
#ifndef NDEBUG
2519825146
/* If we are doing a normal write to a database file (as opposed to
2519925147
** doing a hot-journal rollback or a write to some file other than a
2520025148
** normal database file) then record the fact that the database
@@ -25389,21 +25337,21 @@
2538925337
** line is to test that doing so does not cause any problems.
2539025338
*/
2539125339
SimulateDiskfullError( return SQLITE_FULL );
2539225340
2539325341
assert( pFile );
25394
- OSTRACE2("SYNC %-3d\n", pFile->h);
25342
+ OSTRACE(("SYNC %-3d\n", pFile->h));
2539525343
rc = full_fsync(pFile->h, isFullsync, isDataOnly);
2539625344
SimulateIOError( rc=1 );
2539725345
if( rc ){
2539825346
pFile->lastErrno = errno;
2539925347
return SQLITE_IOERR_FSYNC;
2540025348
}
2540125349
if( pFile->dirfd>=0 ){
2540225350
int err;
25403
- OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
25404
- HAVE_FULLFSYNC, isFullsync);
25351
+ OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
25352
+ HAVE_FULLFSYNC, isFullsync));
2540525353
#ifndef SQLITE_DISABLE_DIRSYNC
2540625354
/* The directory sync is only attempted if full_fsync is
2540725355
** turned off or unavailable. If a full_fsync occurred above,
2540825356
** then the directory sync is superfluous.
2540925357
*/
@@ -25471,11 +25419,11 @@
2547125419
((unixFile*)id)->lastErrno = errno;
2547225420
return SQLITE_IOERR_FSTAT;
2547325421
}
2547425422
*pSize = buf.st_size;
2547525423
25476
- /* When opening a zero-size database, the findLockInfo() procedure
25424
+ /* When opening a zero-size database, the findInodeInfo() procedure
2547725425
** writes a single byte into that file in order to work around a bug
2547825426
** in the OS-X msdos filesystem. In order to avoid problems with upper
2547925427
** layers, we need to report this file size as zero even though it is
2548025428
** really 1. Ticket #3260.
2548125429
*/
@@ -25498,16 +25446,24 @@
2549825446
** Information and control of an open file handle.
2549925447
*/
2550025448
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
2550125449
switch( op ){
2550225450
case SQLITE_FCNTL_LOCKSTATE: {
25503
- *(int*)pArg = ((unixFile*)id)->locktype;
25451
+ *(int*)pArg = ((unixFile*)id)->eFileLock;
2550425452
return SQLITE_OK;
2550525453
}
2550625454
case SQLITE_LAST_ERRNO: {
2550725455
*(int*)pArg = ((unixFile*)id)->lastErrno;
2550825456
return SQLITE_OK;
25457
+ }
25458
+ case SQLITE_FCNTL_SIZE_HINT: {
25459
+#if 0 /* No performance advantage seen on Linux */
25460
+ sqlite3_int64 szFile = *(sqlite3_int64*)pArg;
25461
+ unixFile *pFile = (unixFile*)id;
25462
+ ftruncate(pFile->h, szFile);
25463
+#endif
25464
+ return SQLITE_OK;
2550925465
}
2551025466
#ifndef NDEBUG
2551125467
/* The pager calls this method to signal that it has done
2551225468
** a rollback and that the database is therefore unchanged and
2551325469
** it hence it is OK for the transaction change counter to be
@@ -25549,10 +25505,571 @@
2554925505
static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
2555025506
UNUSED_PARAMETER(NotUsed);
2555125507
return 0;
2555225508
}
2555325509
25510
+#ifndef SQLITE_OMIT_WAL
25511
+
25512
+
25513
+/*
25514
+** Object used to represent an shared memory buffer.
25515
+**
25516
+** When multiple threads all reference the same wal-index, each thread
25517
+** has its own unixShm object, but they all point to a single instance
25518
+** of this unixShmNode object. In other words, each wal-index is opened
25519
+** only once per process.
25520
+**
25521
+** Each unixShmNode object is connected to a single unixInodeInfo object.
25522
+** We could coalesce this object into unixInodeInfo, but that would mean
25523
+** every open file that does not use shared memory (in other words, most
25524
+** open files) would have to carry around this extra information. So
25525
+** the unixInodeInfo object contains a pointer to this unixShmNode object
25526
+** and the unixShmNode object is created only when needed.
25527
+**
25528
+** unixMutexHeld() must be true when creating or destroying
25529
+** this object or while reading or writing the following fields:
25530
+**
25531
+** nRef
25532
+**
25533
+** The following fields are read-only after the object is created:
25534
+**
25535
+** fid
25536
+** zFilename
25537
+**
25538
+** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
25539
+** unixMutexHeld() is true when reading or writing any other field
25540
+** in this structure.
25541
+*/
25542
+struct unixShmNode {
25543
+ unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
25544
+ sqlite3_mutex *mutex; /* Mutex to access this object */
25545
+ char *zFilename; /* Name of the mmapped file */
25546
+ int h; /* Open file descriptor */
25547
+ int szRegion; /* Size of shared-memory regions */
25548
+ int nRegion; /* Size of array apRegion */
25549
+ char **apRegion; /* Array of mapped shared-memory regions */
25550
+ int nRef; /* Number of unixShm objects pointing to this */
25551
+ unixShm *pFirst; /* All unixShm objects pointing to this */
25552
+#ifdef SQLITE_DEBUG
25553
+ u8 exclMask; /* Mask of exclusive locks held */
25554
+ u8 sharedMask; /* Mask of shared locks held */
25555
+ u8 nextShmId; /* Next available unixShm.id value */
25556
+#endif
25557
+};
25558
+
25559
+/*
25560
+** Structure used internally by this VFS to record the state of an
25561
+** open shared memory connection.
25562
+**
25563
+** The following fields are initialized when this object is created and
25564
+** are read-only thereafter:
25565
+**
25566
+** unixShm.pFile
25567
+** unixShm.id
25568
+**
25569
+** All other fields are read/write. The unixShm.pFile->mutex must be held
25570
+** while accessing any read/write fields.
25571
+*/
25572
+struct unixShm {
25573
+ unixShmNode *pShmNode; /* The underlying unixShmNode object */
25574
+ unixShm *pNext; /* Next unixShm with the same unixShmNode */
25575
+ u8 hasMutex; /* True if holding the unixShmNode mutex */
25576
+ u16 sharedMask; /* Mask of shared locks held */
25577
+ u16 exclMask; /* Mask of exclusive locks held */
25578
+#ifdef SQLITE_DEBUG
25579
+ u8 id; /* Id of this connection within its unixShmNode */
25580
+#endif
25581
+};
25582
+
25583
+/*
25584
+** Constants used for locking
25585
+*/
25586
+#define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25587
+#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25588
+
25589
+/*
25590
+** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25591
+**
25592
+** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
25593
+** otherwise.
25594
+*/
25595
+static int unixShmSystemLock(
25596
+ unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
25597
+ int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
25598
+ int ofst, /* First byte of the locking range */
25599
+ int n /* Number of bytes to lock */
25600
+){
25601
+ struct flock f; /* The posix advisory locking structure */
25602
+ int rc = SQLITE_OK; /* Result code form fcntl() */
25603
+
25604
+ /* Access to the unixShmNode object is serialized by the caller */
25605
+ assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
25606
+
25607
+ /* Shared locks never span more than one byte */
25608
+ assert( n==1 || lockType!=F_RDLCK );
25609
+
25610
+ /* Locks are within range */
25611
+ assert( n>=1 && n<SQLITE_SHM_NLOCK );
25612
+
25613
+ /* Initialize the locking parameters */
25614
+ memset(&f, 0, sizeof(f));
25615
+ f.l_type = lockType;
25616
+ f.l_whence = SEEK_SET;
25617
+ f.l_start = ofst;
25618
+ f.l_len = n;
25619
+
25620
+ rc = fcntl(pShmNode->h, F_SETLK, &f);
25621
+ rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
25622
+
25623
+ /* Update the global lock state and do debug tracing */
25624
+#ifdef SQLITE_DEBUG
25625
+ { u16 mask;
25626
+ OSTRACE(("SHM-LOCK "));
25627
+ mask = (1<<(ofst+n)) - (1<<ofst);
25628
+ if( rc==SQLITE_OK ){
25629
+ if( lockType==F_UNLCK ){
25630
+ OSTRACE(("unlock %d ok", ofst));
25631
+ pShmNode->exclMask &= ~mask;
25632
+ pShmNode->sharedMask &= ~mask;
25633
+ }else if( lockType==F_RDLCK ){
25634
+ OSTRACE(("read-lock %d ok", ofst));
25635
+ pShmNode->exclMask &= ~mask;
25636
+ pShmNode->sharedMask |= mask;
25637
+ }else{
25638
+ assert( lockType==F_WRLCK );
25639
+ OSTRACE(("write-lock %d ok", ofst));
25640
+ pShmNode->exclMask |= mask;
25641
+ pShmNode->sharedMask &= ~mask;
25642
+ }
25643
+ }else{
25644
+ if( lockType==F_UNLCK ){
25645
+ OSTRACE(("unlock %d failed", ofst));
25646
+ }else if( lockType==F_RDLCK ){
25647
+ OSTRACE(("read-lock failed"));
25648
+ }else{
25649
+ assert( lockType==F_WRLCK );
25650
+ OSTRACE(("write-lock %d failed", ofst));
25651
+ }
25652
+ }
25653
+ OSTRACE((" - afterwards %03x,%03x\n",
25654
+ pShmNode->sharedMask, pShmNode->exclMask));
25655
+ }
25656
+#endif
25657
+
25658
+ return rc;
25659
+}
25660
+
25661
+
25662
+/*
25663
+** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
25664
+**
25665
+** This is not a VFS shared-memory method; it is a utility function called
25666
+** by VFS shared-memory methods.
25667
+*/
25668
+static void unixShmPurge(unixFile *pFd){
25669
+ unixShmNode *p = pFd->pInode->pShmNode;
25670
+ assert( unixMutexHeld() );
25671
+ if( p && p->nRef==0 ){
25672
+ int i;
25673
+ assert( p->pInode==pFd->pInode );
25674
+ if( p->mutex ) sqlite3_mutex_free(p->mutex);
25675
+ for(i=0; i<p->nRegion; i++){
25676
+ munmap(p->apRegion[i], p->szRegion);
25677
+ }
25678
+ sqlite3_free(p->apRegion);
25679
+ if( p->h>=0 ) close(p->h);
25680
+ p->pInode->pShmNode = 0;
25681
+ sqlite3_free(p);
25682
+ }
25683
+}
25684
+
25685
+/* Forward reference */
25686
+static const char *unixTempFileDir(int);
25687
+
25688
+/*
25689
+** Open a shared-memory area. This particular implementation uses
25690
+** mmapped files.
25691
+**
25692
+** zName is a filename used to identify the shared-memory area. The
25693
+** implementation does not (and perhaps should not) use this name
25694
+** directly, but rather use it as a template for finding an appropriate
25695
+** name for the shared-memory storage. In this implementation, the
25696
+** string "-index" is appended to zName and used as the name of the
25697
+** mmapped file.
25698
+**
25699
+** When opening a new shared-memory file, if no other instances of that
25700
+** file are currently open, in this process or in other processes, then
25701
+** the file must be truncated to zero length or have its header cleared.
25702
+*/
25703
+static int unixShmOpen(
25704
+ sqlite3_file *fd /* The file descriptor of the associated database */
25705
+){
25706
+ struct unixShm *p = 0; /* The connection to be opened */
25707
+ struct unixShmNode *pShmNode = 0; /* The underlying mmapped file */
25708
+ int rc; /* Result code */
25709
+ struct unixFile *pDbFd; /* Underlying database file */
25710
+ unixInodeInfo *pInode; /* The inode of fd */
25711
+ const char *zTempDir; /* Directory for temporary files */
25712
+ int nTempDir; /* Size of the zTempDir string */
25713
+
25714
+ /* Allocate space for the new sqlite3_shm object.
25715
+ */
25716
+ p = sqlite3_malloc( sizeof(*p) );
25717
+ if( p==0 ) return SQLITE_NOMEM;
25718
+ memset(p, 0, sizeof(*p));
25719
+ pDbFd = (struct unixFile*)fd;
25720
+ assert( pDbFd->pShm==0 );
25721
+
25722
+ /* Check to see if a unixShmNode object already exists. Reuse an existing
25723
+ ** one if present. Create a new one if necessary.
25724
+ */
25725
+ unixEnterMutex();
25726
+ pInode = pDbFd->pInode;
25727
+ pShmNode = pInode->pShmNode;
25728
+ if( pShmNode==0 ){
25729
+ zTempDir = unixTempFileDir(1);
25730
+ if( zTempDir==0 ){
25731
+ unixLeaveMutex();
25732
+ sqlite3_free(p);
25733
+ return SQLITE_CANTOPEN_NOTEMPDIR;
25734
+ }
25735
+ nTempDir = strlen(zTempDir);
25736
+ pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nTempDir + 50 );
25737
+ if( pShmNode==0 ){
25738
+ rc = SQLITE_NOMEM;
25739
+ goto shm_open_err;
25740
+ }
25741
+ memset(pShmNode, 0, sizeof(*pShmNode));
25742
+ pShmNode->zFilename = (char*)&pShmNode[1];
25743
+ sqlite3_snprintf(nTempDir+50, pShmNode->zFilename,
25744
+ "%s/sqlite-wi-%x-%x", zTempDir,
25745
+ (u32)pInode->fileId.dev, (u32)pInode->fileId.ino);
25746
+ pShmNode->h = -1;
25747
+ pDbFd->pInode->pShmNode = pShmNode;
25748
+ pShmNode->pInode = pDbFd->pInode;
25749
+ pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
25750
+ if( pShmNode->mutex==0 ){
25751
+ rc = SQLITE_NOMEM;
25752
+ goto shm_open_err;
25753
+ }
25754
+
25755
+ pShmNode->h = open(pShmNode->zFilename, O_RDWR|O_CREAT, 0664);
25756
+ if( pShmNode->h<0 ){
25757
+ rc = SQLITE_CANTOPEN_BKPT;
25758
+ goto shm_open_err;
25759
+ }
25760
+
25761
+ /* Check to see if another process is holding the dead-man switch.
25762
+ ** If not, truncate the file to zero length.
25763
+ */
25764
+ rc = SQLITE_OK;
25765
+ if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
25766
+ if( ftruncate(pShmNode->h, 0) ){
25767
+ rc = SQLITE_IOERR_SHMOPEN;
25768
+ }
25769
+ }
25770
+ if( rc==SQLITE_OK ){
25771
+ rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
25772
+ }
25773
+ if( rc ) goto shm_open_err;
25774
+ }
25775
+
25776
+ /* Make the new connection a child of the unixShmNode */
25777
+ p->pShmNode = pShmNode;
25778
+ p->pNext = pShmNode->pFirst;
25779
+#ifdef SQLITE_DEBUG
25780
+ p->id = pShmNode->nextShmId++;
25781
+#endif
25782
+ pShmNode->pFirst = p;
25783
+ pShmNode->nRef++;
25784
+ pDbFd->pShm = p;
25785
+ unixLeaveMutex();
25786
+ return SQLITE_OK;
25787
+
25788
+ /* Jump here on any error */
25789
+shm_open_err:
25790
+ unixShmPurge(pDbFd); /* This call frees pShmNode if required */
25791
+ sqlite3_free(p);
25792
+ unixLeaveMutex();
25793
+ return rc;
25794
+}
25795
+
25796
+/*
25797
+** Close a connection to shared-memory. Delete the underlying
25798
+** storage if deleteFlag is true.
25799
+*/
25800
+static int unixShmClose(
25801
+ sqlite3_file *fd, /* The underlying database file */
25802
+ int deleteFlag /* Delete shared-memory if true */
25803
+){
25804
+ unixShm *p; /* The connection to be closed */
25805
+ unixShmNode *pShmNode; /* The underlying shared-memory file */
25806
+ unixShm **pp; /* For looping over sibling connections */
25807
+ unixFile *pDbFd; /* The underlying database file */
25808
+
25809
+ pDbFd = (unixFile*)fd;
25810
+ p = pDbFd->pShm;
25811
+ if( p==0 ) return SQLITE_OK;
25812
+ pShmNode = p->pShmNode;
25813
+
25814
+ assert( pShmNode==pDbFd->pInode->pShmNode );
25815
+ assert( pShmNode->pInode==pDbFd->pInode );
25816
+
25817
+ /* Remove connection p from the set of connections associated
25818
+ ** with pShmNode */
25819
+ sqlite3_mutex_enter(pShmNode->mutex);
25820
+ for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
25821
+ *pp = p->pNext;
25822
+
25823
+ /* Free the connection p */
25824
+ sqlite3_free(p);
25825
+ pDbFd->pShm = 0;
25826
+ sqlite3_mutex_leave(pShmNode->mutex);
25827
+
25828
+ /* If pShmNode->nRef has reached 0, then close the underlying
25829
+ ** shared-memory file, too */
25830
+ unixEnterMutex();
25831
+ assert( pShmNode->nRef>0 );
25832
+ pShmNode->nRef--;
25833
+ if( pShmNode->nRef==0 ){
25834
+ if( deleteFlag ) unlink(pShmNode->zFilename);
25835
+ unixShmPurge(pDbFd);
25836
+ }
25837
+ unixLeaveMutex();
25838
+
25839
+ return SQLITE_OK;
25840
+}
25841
+
25842
+/*
25843
+** Change the lock state for a shared-memory segment.
25844
+**
25845
+** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
25846
+** different here than in posix. In xShmLock(), one can go from unlocked
25847
+** to shared and back or from unlocked to exclusive and back. But one may
25848
+** not go from shared to exclusive or from exclusive to shared.
25849
+*/
25850
+static int unixShmLock(
25851
+ sqlite3_file *fd, /* Database file holding the shared memory */
25852
+ int ofst, /* First lock to acquire or release */
25853
+ int n, /* Number of locks to acquire or release */
25854
+ int flags /* What to do with the lock */
25855
+){
25856
+ unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
25857
+ unixShm *p = pDbFd->pShm; /* The shared memory being locked */
25858
+ unixShm *pX; /* For looping over all siblings */
25859
+ unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
25860
+ int rc = SQLITE_OK; /* Result code */
25861
+ u16 mask; /* Mask of locks to take or release */
25862
+
25863
+ assert( pShmNode==pDbFd->pInode->pShmNode );
25864
+ assert( pShmNode->pInode==pDbFd->pInode );
25865
+ assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
25866
+ assert( n>=1 );
25867
+ assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
25868
+ || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
25869
+ || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
25870
+ || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
25871
+ assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
25872
+
25873
+ mask = (1<<(ofst+n)) - (1<<ofst);
25874
+ assert( n>1 || mask==(1<<ofst) );
25875
+ sqlite3_mutex_enter(pShmNode->mutex);
25876
+ if( flags & SQLITE_SHM_UNLOCK ){
25877
+ u16 allMask = 0; /* Mask of locks held by siblings */
25878
+
25879
+ /* See if any siblings hold this same lock */
25880
+ for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25881
+ if( pX==p ) continue;
25882
+ assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
25883
+ allMask |= pX->sharedMask;
25884
+ }
25885
+
25886
+ /* Unlock the system-level locks */
25887
+ if( (mask & allMask)==0 ){
25888
+ rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
25889
+ }else{
25890
+ rc = SQLITE_OK;
25891
+ }
25892
+
25893
+ /* Undo the local locks */
25894
+ if( rc==SQLITE_OK ){
25895
+ p->exclMask &= ~mask;
25896
+ p->sharedMask &= ~mask;
25897
+ }
25898
+ }else if( flags & SQLITE_SHM_SHARED ){
25899
+ u16 allShared = 0; /* Union of locks held by connections other than "p" */
25900
+
25901
+ /* Find out which shared locks are already held by sibling connections.
25902
+ ** If any sibling already holds an exclusive lock, go ahead and return
25903
+ ** SQLITE_BUSY.
25904
+ */
25905
+ for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25906
+ if( (pX->exclMask & mask)!=0 ){
25907
+ rc = SQLITE_BUSY;
25908
+ break;
25909
+ }
25910
+ allShared |= pX->sharedMask;
25911
+ }
25912
+
25913
+ /* Get shared locks at the system level, if necessary */
25914
+ if( rc==SQLITE_OK ){
25915
+ if( (allShared & mask)==0 ){
25916
+ rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
25917
+ }else{
25918
+ rc = SQLITE_OK;
25919
+ }
25920
+ }
25921
+
25922
+ /* Get the local shared locks */
25923
+ if( rc==SQLITE_OK ){
25924
+ p->sharedMask |= mask;
25925
+ }
25926
+ }else{
25927
+ /* Make sure no sibling connections hold locks that will block this
25928
+ ** lock. If any do, return SQLITE_BUSY right away.
25929
+ */
25930
+ for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25931
+ if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
25932
+ rc = SQLITE_BUSY;
25933
+ break;
25934
+ }
25935
+ }
25936
+
25937
+ /* Get the exclusive locks at the system level. Then if successful
25938
+ ** also mark the local connection as being locked.
25939
+ */
25940
+ if( rc==SQLITE_OK ){
25941
+ rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
25942
+ if( rc==SQLITE_OK ){
25943
+ assert( (p->sharedMask & mask)==0 );
25944
+ p->exclMask |= mask;
25945
+ }
25946
+ }
25947
+ }
25948
+ sqlite3_mutex_leave(pShmNode->mutex);
25949
+ OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
25950
+ p->id, getpid(), p->sharedMask, p->exclMask));
25951
+ return rc;
25952
+}
25953
+
25954
+/*
25955
+** Implement a memory barrier or memory fence on shared memory.
25956
+**
25957
+** All loads and stores begun before the barrier must complete before
25958
+** any load or store begun after the barrier.
25959
+*/
25960
+static void unixShmBarrier(
25961
+ sqlite3_file *fd /* Database file holding the shared memory */
25962
+){
25963
+ unixEnterMutex();
25964
+ unixLeaveMutex();
25965
+}
25966
+
25967
+/*
25968
+** This function is called to obtain a pointer to region iRegion of the
25969
+** shared-memory associated with the database file fd. Shared-memory regions
25970
+** are numbered starting from zero. Each shared-memory region is szRegion
25971
+** bytes in size.
25972
+**
25973
+** If an error occurs, an error code is returned and *pp is set to NULL.
25974
+**
25975
+** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
25976
+** region has not been allocated (by any client, including one running in a
25977
+** separate process), then *pp is set to NULL and SQLITE_OK returned. If
25978
+** isWrite is non-zero and the requested shared-memory region has not yet
25979
+** been allocated, it is allocated by this function.
25980
+**
25981
+** If the shared-memory region has already been allocated or is allocated by
25982
+** this call as described above, then it is mapped into this processes
25983
+** address space (if it is not already), *pp is set to point to the mapped
25984
+** memory and SQLITE_OK returned.
25985
+*/
25986
+static int unixShmMap(
25987
+ sqlite3_file *fd, /* Handle open on database file */
25988
+ int iRegion, /* Region to retrieve */
25989
+ int szRegion, /* Size of regions */
25990
+ int isWrite, /* True to extend file if necessary */
25991
+ void volatile **pp /* OUT: Mapped memory */
25992
+){
25993
+ unixFile *pDbFd = (unixFile*)fd;
25994
+ unixShm *p = pDbFd->pShm;
25995
+ unixShmNode *pShmNode = p->pShmNode;
25996
+ int rc = SQLITE_OK;
25997
+
25998
+ sqlite3_mutex_enter(pShmNode->mutex);
25999
+ assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
26000
+
26001
+ if( pShmNode->nRegion<=iRegion ){
26002
+ char **apNew; /* New apRegion[] array */
26003
+ int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
26004
+ struct stat sStat; /* Used by fstat() */
26005
+
26006
+ pShmNode->szRegion = szRegion;
26007
+
26008
+ /* The requested region is not mapped into this processes address space.
26009
+ ** Check to see if it has been allocated (i.e. if the wal-index file is
26010
+ ** large enough to contain the requested region).
26011
+ */
26012
+ if( fstat(pShmNode->h, &sStat) ){
26013
+ rc = SQLITE_IOERR_SHMSIZE;
26014
+ goto shmpage_out;
26015
+ }
26016
+
26017
+ if( sStat.st_size<nByte ){
26018
+ /* The requested memory region does not exist. If isWrite is set to
26019
+ ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
26020
+ **
26021
+ ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
26022
+ ** the requested memory region.
26023
+ */
26024
+ if( !isWrite ) goto shmpage_out;
26025
+ if( ftruncate(pShmNode->h, nByte) ){
26026
+ rc = SQLITE_IOERR_SHMSIZE;
26027
+ goto shmpage_out;
26028
+ }
26029
+ }
26030
+
26031
+ /* Map the requested memory region into this processes address space. */
26032
+ apNew = (char **)sqlite3_realloc(
26033
+ pShmNode->apRegion, (iRegion+1)*sizeof(char *)
26034
+ );
26035
+ if( !apNew ){
26036
+ rc = SQLITE_IOERR_NOMEM;
26037
+ goto shmpage_out;
26038
+ }
26039
+ pShmNode->apRegion = apNew;
26040
+ while(pShmNode->nRegion<=iRegion){
26041
+ void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE,
26042
+ MAP_SHARED, pShmNode->h, iRegion*szRegion
26043
+ );
26044
+ if( pMem==MAP_FAILED ){
26045
+ rc = SQLITE_IOERR;
26046
+ goto shmpage_out;
26047
+ }
26048
+ pShmNode->apRegion[pShmNode->nRegion] = pMem;
26049
+ pShmNode->nRegion++;
26050
+ }
26051
+ }
26052
+
26053
+shmpage_out:
26054
+ if( pShmNode->nRegion>iRegion ){
26055
+ *pp = pShmNode->apRegion[iRegion];
26056
+ }else{
26057
+ *pp = 0;
26058
+ }
26059
+ sqlite3_mutex_leave(pShmNode->mutex);
26060
+ return rc;
26061
+}
26062
+
26063
+#else
26064
+# define unixShmOpen 0
26065
+# define unixShmLock 0
26066
+# define unixShmMap 0
26067
+# define unixShmBarrier 0
26068
+# define unixShmClose 0
26069
+#endif /* #ifndef SQLITE_OMIT_WAL */
26070
+
2555426071
/*
2555526072
** Here ends the implementation of all sqlite3_file methods.
2555626073
**
2555726074
********************** End sqlite3_file Methods *******************************
2555826075
******************************************************************************/
@@ -25589,13 +26106,13 @@
2558926106
** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
2559026107
**
2559126108
** * An I/O method finder function called FINDER that returns a pointer
2559226109
** to the METHOD object in the previous bullet.
2559326110
*/
25594
-#define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
26111
+#define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
2559526112
static const sqlite3_io_methods METHOD = { \
25596
- 1, /* iVersion */ \
26113
+ VERSION, /* iVersion */ \
2559726114
CLOSE, /* xClose */ \
2559826115
unixRead, /* xRead */ \
2559926116
unixWrite, /* xWrite */ \
2560026117
unixTruncate, /* xTruncate */ \
2560126118
unixSync, /* xSync */ \
@@ -25603,11 +26120,16 @@
2560326120
LOCK, /* xLock */ \
2560426121
UNLOCK, /* xUnlock */ \
2560526122
CKLOCK, /* xCheckReservedLock */ \
2560626123
unixFileControl, /* xFileControl */ \
2560726124
unixSectorSize, /* xSectorSize */ \
25608
- unixDeviceCharacteristics /* xDeviceCapabilities */ \
26125
+ unixDeviceCharacteristics, /* xDeviceCapabilities */ \
26126
+ unixShmOpen, /* xShmOpen */ \
26127
+ unixShmLock, /* xShmLock */ \
26128
+ unixShmMap, /* xShmMap */ \
26129
+ unixShmBarrier, /* xShmBarrier */ \
26130
+ unixShmClose /* xShmClose */ \
2560926131
}; \
2561026132
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
2561126133
UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
2561226134
return &METHOD; \
2561326135
} \
@@ -25620,26 +26142,29 @@
2562026142
** are also created.
2562126143
*/
2562226144
IOMETHODS(
2562326145
posixIoFinder, /* Finder function name */
2562426146
posixIoMethods, /* sqlite3_io_methods object name */
26147
+ 2, /* ShmOpen is enabled */
2562526148
unixClose, /* xClose method */
2562626149
unixLock, /* xLock method */
2562726150
unixUnlock, /* xUnlock method */
2562826151
unixCheckReservedLock /* xCheckReservedLock method */
2562926152
)
2563026153
IOMETHODS(
2563126154
nolockIoFinder, /* Finder function name */
2563226155
nolockIoMethods, /* sqlite3_io_methods object name */
26156
+ 1, /* ShmOpen is disabled */
2563326157
nolockClose, /* xClose method */
2563426158
nolockLock, /* xLock method */
2563526159
nolockUnlock, /* xUnlock method */
2563626160
nolockCheckReservedLock /* xCheckReservedLock method */
2563726161
)
2563826162
IOMETHODS(
2563926163
dotlockIoFinder, /* Finder function name */
2564026164
dotlockIoMethods, /* sqlite3_io_methods object name */
26165
+ 1, /* ShmOpen is disabled */
2564126166
dotlockClose, /* xClose method */
2564226167
dotlockLock, /* xLock method */
2564326168
dotlockUnlock, /* xUnlock method */
2564426169
dotlockCheckReservedLock /* xCheckReservedLock method */
2564526170
)
@@ -25646,10 +26171,11 @@
2564626171
2564726172
#if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
2564826173
IOMETHODS(
2564926174
flockIoFinder, /* Finder function name */
2565026175
flockIoMethods, /* sqlite3_io_methods object name */
26176
+ 1, /* ShmOpen is disabled */
2565126177
flockClose, /* xClose method */
2565226178
flockLock, /* xLock method */
2565326179
flockUnlock, /* xUnlock method */
2565426180
flockCheckReservedLock /* xCheckReservedLock method */
2565526181
)
@@ -25657,10 +26183,11 @@
2565726183
2565826184
#if OS_VXWORKS
2565926185
IOMETHODS(
2566026186
semIoFinder, /* Finder function name */
2566126187
semIoMethods, /* sqlite3_io_methods object name */
26188
+ 1, /* ShmOpen is disabled */
2566226189
semClose, /* xClose method */
2566326190
semLock, /* xLock method */
2566426191
semUnlock, /* xUnlock method */
2566526192
semCheckReservedLock /* xCheckReservedLock method */
2566626193
)
@@ -25668,10 +26195,11 @@
2566826195
2566926196
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2567026197
IOMETHODS(
2567126198
afpIoFinder, /* Finder function name */
2567226199
afpIoMethods, /* sqlite3_io_methods object name */
26200
+ 1, /* ShmOpen is disabled */
2567326201
afpClose, /* xClose method */
2567426202
afpLock, /* xLock method */
2567526203
afpUnlock, /* xUnlock method */
2567626204
afpCheckReservedLock /* xCheckReservedLock method */
2567726205
)
@@ -25692,10 +26220,11 @@
2569226220
static int proxyUnlock(sqlite3_file*, int);
2569326221
static int proxyCheckReservedLock(sqlite3_file*, int*);
2569426222
IOMETHODS(
2569526223
proxyIoFinder, /* Finder function name */
2569626224
proxyIoMethods, /* sqlite3_io_methods object name */
26225
+ 1, /* ShmOpen is disabled */
2569726226
proxyClose, /* xClose method */
2569826227
proxyLock, /* xLock method */
2569926228
proxyUnlock, /* xUnlock method */
2570026229
proxyCheckReservedLock /* xCheckReservedLock method */
2570126230
)
@@ -25704,10 +26233,11 @@
2570426233
/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
2570526234
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2570626235
IOMETHODS(
2570726236
nfsIoFinder, /* Finder function name */
2570826237
nfsIoMethods, /* sqlite3_io_methods object name */
26238
+ 1, /* ShmOpen is disabled */
2570926239
unixClose, /* xClose method */
2571026240
unixLock, /* xLock method */
2571126241
nfsUnlock, /* xUnlock method */
2571226242
unixCheckReservedLock /* xCheckReservedLock method */
2571326243
)
@@ -25844,23 +26374,23 @@
2584426374
){
2584526375
const sqlite3_io_methods *pLockingStyle;
2584626376
unixFile *pNew = (unixFile *)pId;
2584726377
int rc = SQLITE_OK;
2584826378
25849
- assert( pNew->pLock==NULL );
25850
- assert( pNew->pOpen==NULL );
26379
+ assert( pNew->pInode==NULL );
2585126380
2585226381
/* Parameter isDelete is only used on vxworks. Express this explicitly
2585326382
** here to prevent compiler warnings about unused parameters.
2585426383
*/
2585526384
UNUSED_PARAMETER(isDelete);
2585626385
25857
- OSTRACE3("OPEN %-3d %s\n", h, zFilename);
26386
+ OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2585826387
pNew->h = h;
2585926388
pNew->dirfd = dirfd;
25860
- SET_THREADID(pNew);
2586126389
pNew->fileFlags = 0;
26390
+ assert( zFilename==0 || zFilename[0]=='/' ); /* Never a relative pathname */
26391
+ pNew->zPath = zFilename;
2586226392
2586326393
#if OS_VXWORKS
2586426394
pNew->pId = vxworksFindFileId(zFilename);
2586526395
if( pNew->pId==0 ){
2586626396
noLock = 1;
@@ -25884,23 +26414,23 @@
2588426414
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
2588526415
|| pLockingStyle == &nfsIoMethods
2588626416
#endif
2588726417
){
2588826418
unixEnterMutex();
25889
- rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
26419
+ rc = findInodeInfo(pNew, &pNew->pInode);
2589026420
if( rc!=SQLITE_OK ){
25891
- /* If an error occured in findLockInfo(), close the file descriptor
25892
- ** immediately, before releasing the mutex. findLockInfo() may fail
26421
+ /* If an error occured in findInodeInfo(), close the file descriptor
26422
+ ** immediately, before releasing the mutex. findInodeInfo() may fail
2589326423
** in two scenarios:
2589426424
**
2589526425
** (a) A call to fstat() failed.
2589626426
** (b) A malloc failed.
2589726427
**
2589826428
** Scenario (b) may only occur if the process is holding no other
2589926429
** file descriptors open on the same file. If there were other file
2590026430
** descriptors on this file, then no malloc would be required by
25901
- ** findLockInfo(). If this is the case, it is quite safe to close
26431
+ ** findInodeInfo(). If this is the case, it is quite safe to close
2590226432
** handle h - as it is guaranteed that no posix locks will be released
2590326433
** by doing so.
2590426434
**
2590526435
** If scenario (a) caused the error then things are not so safe. The
2590626436
** implicit assumption here is that if fstat() fails, things are in
@@ -25927,11 +26457,11 @@
2592726457
** copy of the filename. */
2592826458
pCtx->dbPath = zFilename;
2592926459
pCtx->reserved = 0;
2593026460
srandomdev();
2593126461
unixEnterMutex();
25932
- rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
26462
+ rc = findInodeInfo(pNew, &pNew->pInode);
2593326463
if( rc!=SQLITE_OK ){
2593426464
sqlite3_free(pNew->lockingContext);
2593526465
close(h);
2593626466
h = -1;
2593726467
}
@@ -25960,22 +26490,22 @@
2596026490
else if( pLockingStyle == &semIoMethods ){
2596126491
/* Named semaphore locking uses the file path so it needs to be
2596226492
** included in the semLockingContext
2596326493
*/
2596426494
unixEnterMutex();
25965
- rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
25966
- if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
25967
- char *zSemName = pNew->pOpen->aSemName;
26495
+ rc = findInodeInfo(pNew, &pNew->pInode);
26496
+ if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
26497
+ char *zSemName = pNew->pInode->aSemName;
2596826498
int n;
2596926499
sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
2597026500
pNew->pId->zCanonicalName);
2597126501
for( n=1; zSemName[n]; n++ )
2597226502
if( zSemName[n]=='/' ) zSemName[n] = '_';
25973
- pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
25974
- if( pNew->pOpen->pSem == SEM_FAILED ){
26503
+ pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
26504
+ if( pNew->pInode->pSem == SEM_FAILED ){
2597526505
rc = SQLITE_NOMEM;
25976
- pNew->pOpen->aSemName[0] = '\0';
26506
+ pNew->pInode->aSemName[0] = '\0';
2597726507
}
2597826508
}
2597926509
unixLeaveMutex();
2598026510
}
2598126511
#endif
@@ -26022,58 +26552,75 @@
2602226552
fd = open(zDirname, O_RDONLY|O_BINARY, 0);
2602326553
if( fd>=0 ){
2602426554
#ifdef FD_CLOEXEC
2602526555
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
2602626556
#endif
26027
- OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
26557
+ OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
2602826558
}
2602926559
}
2603026560
*pFd = fd;
2603126561
return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT);
2603226562
}
2603326563
2603426564
/*
26035
-** Create a temporary file name in zBuf. zBuf must be allocated
26036
-** by the calling process and must be big enough to hold at least
26037
-** pVfs->mxPathname bytes.
26565
+** Return the name of a directory in which to put temporary files.
26566
+** If no suitable temporary file directory can be found, return NULL.
2603826567
*/
26039
-static int getTempname(int nBuf, char *zBuf){
26568
+static const char *unixTempFileDir(int allowShm){
2604026569
static const char *azDirs[] = {
2604126570
0,
2604226571
0,
2604326572
"/var/tmp",
2604426573
"/usr/tmp",
2604526574
"/tmp",
26046
- ".",
26575
+ 0 /* List terminator */
2604726576
};
26577
+ unsigned int i;
26578
+ struct stat buf;
26579
+ const char *zDir = 0;
26580
+
26581
+ azDirs[0] = sqlite3_temp_directory;
26582
+ if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
26583
+
26584
+ if( allowShm ){
26585
+ zDir = "/dev/shm";
26586
+ i = 2; /* Skip the app-defined temp locations for shared-memory */
26587
+ }else{
26588
+ zDir = azDirs[0];
26589
+ i = 1;
26590
+ }
26591
+ for(; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
26592
+ if( zDir==0 ) continue;
26593
+ if( stat(zDir, &buf) ) continue;
26594
+ if( !S_ISDIR(buf.st_mode) ) continue;
26595
+ if( !allowShm && access(zDir, 07) ) continue;
26596
+ break;
26597
+ }
26598
+ return zDir;
26599
+}
26600
+
26601
+/*
26602
+** Create a temporary file name in zBuf. zBuf must be allocated
26603
+** by the calling process and must be big enough to hold at least
26604
+** pVfs->mxPathname bytes.
26605
+*/
26606
+static int unixGetTempname(int nBuf, char *zBuf){
2604826607
static const unsigned char zChars[] =
2604926608
"abcdefghijklmnopqrstuvwxyz"
2605026609
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2605126610
"0123456789";
2605226611
unsigned int i, j;
26053
- struct stat buf;
26054
- const char *zDir = ".";
26612
+ const char *zDir;
2605526613
2605626614
/* It's odd to simulate an io-error here, but really this is just
2605726615
** using the io-error infrastructure to test that SQLite handles this
2605826616
** function failing.
2605926617
*/
2606026618
SimulateIOError( return SQLITE_IOERR );
2606126619
26062
- azDirs[0] = sqlite3_temp_directory;
26063
- if (NULL == azDirs[1]) {
26064
- azDirs[1] = getenv("TMPDIR");
26065
- }
26066
-
26067
- for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
26068
- if( azDirs[i]==0 ) continue;
26069
- if( stat(azDirs[i], &buf) ) continue;
26070
- if( !S_ISDIR(buf.st_mode) ) continue;
26071
- if( access(azDirs[i], 07) ) continue;
26072
- zDir = azDirs[i];
26073
- break;
26074
- }
26620
+ zDir = unixTempFileDir(0);
26621
+ if( zDir==0 ) zDir = ".";
2607526622
2607626623
/* Check that the output buffer is large enough for the temporary file
2607726624
** name. If it is not, return SQLITE_ERROR.
2607826625
*/
2607926626
if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
@@ -26135,21 +26682,21 @@
2613526682
** descriptor on the same path, fail, and return an error to SQLite.
2613626683
**
2613726684
** Even if a subsequent open() call does succeed, the consequences of
2613826685
** not searching for a resusable file descriptor are not dire. */
2613926686
if( 0==stat(zPath, &sStat) ){
26140
- struct unixOpenCnt *pOpen;
26687
+ unixInodeInfo *pInode;
2614126688
2614226689
unixEnterMutex();
26143
- pOpen = openList;
26144
- while( pOpen && (pOpen->fileId.dev!=sStat.st_dev
26145
- || pOpen->fileId.ino!=sStat.st_ino) ){
26146
- pOpen = pOpen->pNext;
26690
+ pInode = inodeList;
26691
+ while( pInode && (pInode->fileId.dev!=sStat.st_dev
26692
+ || pInode->fileId.ino!=sStat.st_ino) ){
26693
+ pInode = pInode->pNext;
2614726694
}
26148
- if( pOpen ){
26695
+ if( pInode ){
2614926696
UnixUnusedFd **pp;
26150
- for(pp=&pOpen->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
26697
+ for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
2615126698
pUnused = *pp;
2615226699
if( pUnused ){
2615326700
*pp = pUnused->pNext;
2615426701
}
2615526702
}
@@ -26259,11 +26806,11 @@
2625926806
}
2626026807
p->pUnused = pUnused;
2626126808
}else if( !zName ){
2626226809
/* If zName is NULL, the upper layer is requesting a temp file. */
2626326810
assert(isDelete && !isOpenDirectory);
26264
- rc = getTempname(MAX_PATHNAME+1, zTmpname);
26811
+ rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
2626526812
if( rc!=SQLITE_OK ){
2626626813
return rc;
2626726814
}
2626826815
zName = zTmpname;
2626926816
}
@@ -26279,11 +26826,11 @@
2627926826
openFlags |= (O_LARGEFILE|O_BINARY);
2628026827
2628126828
if( fd<0 ){
2628226829
mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
2628326830
fd = open(zName, openFlags, openMode);
26284
- OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
26831
+ OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
2628526832
if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
2628626833
/* Failed to open the file for read/write access. Try read-only. */
2628726834
flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
2628826835
openFlags &= ~(O_RDWR|O_CREAT);
2628926836
flags |= SQLITE_OPEN_READONLY;
@@ -26663,41 +27210,54 @@
2666327210
** sqlite3OsCurrentTime() during testing.
2666427211
*/
2666527212
#ifdef SQLITE_TEST
2666627213
SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
2666727214
#endif
27215
+
27216
+/*
27217
+** Find the current time (in Universal Coordinated Time). Write into *piNow
27218
+** the current time and date as a Julian Day number times 86_400_000. In
27219
+** other words, write into *piNow the number of milliseconds since the Julian
27220
+** epoch of noon in Greenwich on November 24, 4714 B.C according to the
27221
+** proleptic Gregorian calendar.
27222
+**
27223
+** On success, return 0. Return 1 if the time and date cannot be found.
27224
+*/
27225
+static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
27226
+ static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
27227
+#if defined(NO_GETTOD)
27228
+ time_t t;
27229
+ time(&t);
27230
+ *piNow = ((sqlite3_int64)i)*1000 + unixEpoch;
27231
+#elif OS_VXWORKS
27232
+ struct timespec sNow;
27233
+ clock_gettime(CLOCK_REALTIME, &sNow);
27234
+ *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
27235
+#else
27236
+ struct timeval sNow;
27237
+ gettimeofday(&sNow, 0);
27238
+ *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
27239
+#endif
27240
+
27241
+#ifdef SQLITE_TEST
27242
+ if( sqlite3_current_time ){
27243
+ *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
27244
+ }
27245
+#endif
27246
+ UNUSED_PARAMETER(NotUsed);
27247
+ return 0;
27248
+}
2666827249
2666927250
/*
2667027251
** Find the current time (in Universal Coordinated Time). Write the
2667127252
** current time and date as a Julian Day number into *prNow and
2667227253
** return 0. Return 1 if the time and date cannot be found.
2667327254
*/
2667427255
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
26675
-#if defined(SQLITE_OMIT_FLOATING_POINT)
26676
- time_t t;
26677
- time(&t);
26678
- *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10;
26679
-#elif defined(NO_GETTOD)
26680
- time_t t;
26681
- time(&t);
26682
- *prNow = t/86400.0 + 2440587.5;
26683
-#elif OS_VXWORKS
26684
- struct timespec sNow;
26685
- clock_gettime(CLOCK_REALTIME, &sNow);
26686
- *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
26687
-#else
26688
- struct timeval sNow;
26689
- gettimeofday(&sNow, 0);
26690
- *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
26691
-#endif
26692
-
26693
-#ifdef SQLITE_TEST
26694
- if( sqlite3_current_time ){
26695
- *prNow = sqlite3_current_time/86400.0 + 2440587.5;
26696
- }
26697
-#endif
26698
- UNUSED_PARAMETER(NotUsed);
27256
+ sqlite3_int64 i;
27257
+ unixCurrentTimeInt64(0, &i);
27258
+ *prNow = i/86400000.0;
2669927259
return 0;
2670027260
}
2670127261
2670227262
/*
2670327263
** We added the xGetLastError() method with the intention of providing
@@ -26710,10 +27270,11 @@
2671027270
UNUSED_PARAMETER(NotUsed);
2671127271
UNUSED_PARAMETER(NotUsed2);
2671227272
UNUSED_PARAMETER(NotUsed3);
2671327273
return 0;
2671427274
}
27275
+
2671527276
2671627277
/*
2671727278
************************ End of sqlite3_vfs methods ***************************
2671827279
******************************************************************************/
2671927280
@@ -26901,12 +27462,12 @@
2690127462
len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
2690227463
#else
2690327464
# ifdef _CS_DARWIN_USER_TEMP_DIR
2690427465
{
2690527466
if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
26906
- OSTRACE4("GETLOCKPATH failed %s errno=%d pid=%d\n",
26907
- lPath, errno, getpid());
27467
+ OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
27468
+ lPath, errno, getpid()));
2690827469
return SQLITE_IOERR_LOCK;
2690927470
}
2691027471
len = strlcat(lPath, "sqliteplocks", maxLen);
2691127472
}
2691227473
# else
@@ -26924,11 +27485,11 @@
2692427485
char c = dbPath[i];
2692527486
lPath[i+len] = (c=='/')?'_':c;
2692627487
}
2692727488
lPath[i+len]='\0';
2692827489
strlcat(lPath, ":auto:", maxLen);
26929
- OSTRACE3("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid());
27490
+ OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid()));
2693027491
return SQLITE_OK;
2693127492
}
2693227493
2693327494
/*
2693427495
** Creates the lock file and any missing directories in lockPath
@@ -26949,22 +27510,22 @@
2694927510
|| (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
2695027511
buf[i]='\0';
2695127512
if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
2695227513
int err=errno;
2695327514
if( err!=EEXIST ) {
26954
- OSTRACE5("CREATELOCKPATH FAILED creating %s, "
27515
+ OSTRACE(("CREATELOCKPATH FAILED creating %s, "
2695527516
"'%s' proxy lock path=%s pid=%d\n",
26956
- buf, strerror(err), lockPath, getpid());
27517
+ buf, strerror(err), lockPath, getpid()));
2695727518
return err;
2695827519
}
2695927520
}
2696027521
}
2696127522
start=i+1;
2696227523
}
2696327524
buf[i] = lockPath[i];
2696427525
}
26965
- OSTRACE3("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid());
27526
+ OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid()));
2696627527
return 0;
2696727528
}
2696827529
2696927530
/*
2697027531
** Create a new VFS file descriptor (stored in memory obtained from
@@ -27252,12 +27813,12 @@
2725227813
int hostIdMatch = 0;
2725327814
int readLen = 0;
2725427815
int tryOldLockPath = 0;
2725527816
int forceNewLockPath = 0;
2725627817
27257
- OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
27258
- (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
27818
+ OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
27819
+ (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
2725927820
2726027821
rc = proxyGetHostID(myHostID, &pError);
2726127822
if( (rc&0xff)==SQLITE_IOERR ){
2726227823
pFile->lastErrno = pError;
2726327824
goto end_takeconch;
@@ -27333,11 +27894,11 @@
2733327894
** has a shared lock already), if the host id matches, use the big
2733427895
** stick.
2733527896
*/
2733627897
futimes(conchFile->h, NULL);
2733727898
if( hostIdMatch && !createConch ){
27338
- if( conchFile->pLock && conchFile->pLock->cnt>1 ){
27899
+ if( conchFile->pInode && conchFile->pInode->nShared>1 ){
2733927900
/* We are trying for an exclusive lock but another thread in this
2734027901
** same process is still holding a shared lock. */
2734127902
rc = SQLITE_BUSY;
2734227903
} else {
2734327904
rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
@@ -27389,11 +27950,11 @@
2738927950
}
2739027951
}
2739127952
conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
2739227953
2739327954
end_takeconch:
27394
- OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
27955
+ OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
2739527956
if( rc==SQLITE_OK && pFile->openFlags ){
2739627957
if( pFile->h>=0 ){
2739727958
#ifdef STRICT_CLOSE_ERROR
2739827959
if( close(pFile->h) ){
2739927960
pFile->lastErrno = errno;
@@ -27404,11 +27965,11 @@
2740427965
#endif
2740527966
}
2740627967
pFile->h = -1;
2740727968
int fd = open(pCtx->dbPath, pFile->openFlags,
2740827969
SQLITE_DEFAULT_FILE_PERMISSIONS);
27409
- OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
27970
+ OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
2741027971
if( fd>=0 ){
2741127972
pFile->h = fd;
2741227973
}else{
2741327974
rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
2741427975
during locking */
@@ -27446,35 +28007,37 @@
2744628007
afpCtx->dbPath = pCtx->lockProxyPath;
2744728008
}
2744828009
} else {
2744928010
conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
2745028011
}
27451
- OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
28012
+ OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
28013
+ rc==SQLITE_OK?"ok":"failed"));
2745228014
return rc;
27453
- } while (1); /* in case we need to retry the :auto: lock file - we should never get here except via the 'continue' call. */
28015
+ } while (1); /* in case we need to retry the :auto: lock file -
28016
+ ** we should never get here except via the 'continue' call. */
2745428017
}
2745528018
}
2745628019
2745728020
/*
2745828021
** If pFile holds a lock on a conch file, then release that lock.
2745928022
*/
2746028023
static int proxyReleaseConch(unixFile *pFile){
27461
- int rc; /* Subroutine return code */
28024
+ int rc = SQLITE_OK; /* Subroutine return code */
2746228025
proxyLockingContext *pCtx; /* The locking context for the proxy lock */
2746328026
unixFile *conchFile; /* Name of the conch file */
2746428027
2746528028
pCtx = (proxyLockingContext *)pFile->lockingContext;
2746628029
conchFile = pCtx->conchFile;
27467
- OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
28030
+ OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
2746828031
(pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
27469
- getpid());
28032
+ getpid()));
2747028033
if( pCtx->conchHeld>0 ){
2747128034
rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
2747228035
}
2747328036
pCtx->conchHeld = 0;
27474
- OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
27475
- (rc==SQLITE_OK ? "ok" : "failed"));
28037
+ OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
28038
+ (rc==SQLITE_OK ? "ok" : "failed")));
2747628039
return rc;
2747728040
}
2747828041
2747928042
/*
2748028043
** Given the name of a database file, compute the name of its conch file.
@@ -27527,11 +28090,11 @@
2752728090
static int switchLockProxyPath(unixFile *pFile, const char *path) {
2752828091
proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
2752928092
char *oldPath = pCtx->lockProxyPath;
2753028093
int rc = SQLITE_OK;
2753128094
27532
- if( pFile->locktype!=NO_LOCK ){
28095
+ if( pFile->eFileLock!=NO_LOCK ){
2753328096
return SQLITE_BUSY;
2753428097
}
2753528098
2753628099
/* nothing to do if the path is NULL, :auto: or matches the existing path */
2753728100
if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
@@ -27594,22 +28157,22 @@
2759428157
proxyLockingContext *pCtx;
2759528158
char dbPath[MAXPATHLEN+1]; /* Name of the database file */
2759628159
char *lockPath=NULL;
2759728160
int rc = SQLITE_OK;
2759828161
27599
- if( pFile->locktype!=NO_LOCK ){
28162
+ if( pFile->eFileLock!=NO_LOCK ){
2760028163
return SQLITE_BUSY;
2760128164
}
2760228165
proxyGetDbPathForUnixFile(pFile, dbPath);
2760328166
if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
2760428167
lockPath=NULL;
2760528168
}else{
2760628169
lockPath=(char *)path;
2760728170
}
2760828171
27609
- OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
27610
- (lockPath ? lockPath : ":auto:"), getpid());
28172
+ OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
28173
+ (lockPath ? lockPath : ":auto:"), getpid()));
2761128174
2761228175
pCtx = sqlite3_malloc( sizeof(*pCtx) );
2761328176
if( pCtx==0 ){
2761428177
return SQLITE_NOMEM;
2761528178
}
@@ -27665,12 +28228,12 @@
2766528228
}
2766628229
sqlite3_free(pCtx->lockProxyPath);
2766728230
sqlite3_free(pCtx->conchFilePath);
2766828231
sqlite3_free(pCtx);
2766928232
}
27670
- OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
27671
- (rc==SQLITE_OK ? "ok" : "failed"));
28233
+ OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
28234
+ (rc==SQLITE_OK ? "ok" : "failed")));
2767228235
return rc;
2767328236
}
2767428237
2767528238
2767628239
/*
@@ -27761,11 +28324,11 @@
2776128324
}
2776228325
return rc;
2776328326
}
2776428327
2776528328
/*
27766
-** Lock the file with the lock specified by parameter locktype - one
28329
+** Lock the file with the lock specified by parameter eFileLock - one
2776728330
** of the following:
2776828331
**
2776928332
** (1) SHARED_LOCK
2777028333
** (2) RESERVED_LOCK
2777128334
** (3) PENDING_LOCK
@@ -27784,43 +28347,43 @@
2778428347
** PENDING -> EXCLUSIVE
2778528348
**
2778628349
** This routine will only increase a lock. Use the sqlite3OsUnlock()
2778728350
** routine to lower a locking level.
2778828351
*/
27789
-static int proxyLock(sqlite3_file *id, int locktype) {
28352
+static int proxyLock(sqlite3_file *id, int eFileLock) {
2779028353
unixFile *pFile = (unixFile*)id;
2779128354
int rc = proxyTakeConch(pFile);
2779228355
if( rc==SQLITE_OK ){
2779328356
proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2779428357
if( pCtx->conchHeld>0 ){
2779528358
unixFile *proxy = pCtx->lockProxy;
27796
- rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
27797
- pFile->locktype = proxy->locktype;
28359
+ rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
28360
+ pFile->eFileLock = proxy->eFileLock;
2779828361
}else{
2779928362
/* conchHeld < 0 is lockless */
2780028363
}
2780128364
}
2780228365
return rc;
2780328366
}
2780428367
2780528368
2780628369
/*
27807
-** Lower the locking level on file descriptor pFile to locktype. locktype
28370
+** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
2780828371
** must be either NO_LOCK or SHARED_LOCK.
2780928372
**
2781028373
** If the locking level of the file descriptor is already at or below
2781128374
** the requested locking level, this routine is a no-op.
2781228375
*/
27813
-static int proxyUnlock(sqlite3_file *id, int locktype) {
28376
+static int proxyUnlock(sqlite3_file *id, int eFileLock) {
2781428377
unixFile *pFile = (unixFile*)id;
2781528378
int rc = proxyTakeConch(pFile);
2781628379
if( rc==SQLITE_OK ){
2781728380
proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
2781828381
if( pCtx->conchHeld>0 ){
2781928382
unixFile *proxy = pCtx->lockProxy;
27820
- rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
27821
- pFile->locktype = proxy->locktype;
28383
+ rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
28384
+ pFile->eFileLock = proxy->eFileLock;
2782228385
}else{
2782328386
/* conchHeld < 0 is lockless */
2782428387
}
2782528388
}
2782628389
return rc;
@@ -27911,11 +28474,11 @@
2791128474
** more than that; it looks at the filesystem type that hosts the
2791228475
** database file and tries to choose an locking method appropriate for
2791328476
** that filesystem time.
2791428477
*/
2791528478
#define UNIXVFS(VFSNAME, FINDER) { \
27916
- 1, /* iVersion */ \
28479
+ 2, /* iVersion */ \
2791728480
sizeof(unixFile), /* szOsFile */ \
2791828481
MAX_PATHNAME, /* mxPathname */ \
2791928482
0, /* pNext */ \
2792028483
VFSNAME, /* zName */ \
2792128484
(void*)&FINDER, /* pAppData */ \
@@ -27928,11 +28491,13 @@
2792828491
unixDlSym, /* xDlSym */ \
2792928492
unixDlClose, /* xDlClose */ \
2793028493
unixRandomness, /* xRandomness */ \
2793128494
unixSleep, /* xSleep */ \
2793228495
unixCurrentTime, /* xCurrentTime */ \
27933
- unixGetLastError /* xGetLastError */ \
28496
+ unixGetLastError, /* xGetLastError */ \
28497
+ 0, /* xRename */ \
28498
+ unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
2793428499
}
2793528500
2793628501
/*
2793728502
** All default VFSes for unix are contained in the following array.
2793828503
**
@@ -28079,27 +28644,13 @@
2807928644
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2808028645
#endif
2808128646
2808228647
#ifdef SQLITE_DEBUG
2808328648
SQLITE_PRIVATE int sqlite3OSTrace = 0;
28084
-#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
28085
-#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
28086
-#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
28087
-#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
28088
-#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
28089
-#define OSTRACE6(X,Y,Z,A,B,C) \
28090
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
28091
-#define OSTRACE7(X,Y,Z,A,B,C,D) \
28092
- if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
28649
+#define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
2809328650
#else
28094
-#define OSTRACE1(X)
28095
-#define OSTRACE2(X,Y)
28096
-#define OSTRACE3(X,Y,Z)
28097
-#define OSTRACE4(X,Y,Z,A)
28098
-#define OSTRACE5(X,Y,Z,A,B)
28099
-#define OSTRACE6(X,Y,Z,A,B,C)
28100
-#define OSTRACE7(X,Y,Z,A,B,C,D)
28651
+#define OSTRACE(X)
2810128652
#endif
2810228653
2810328654
/*
2810428655
** Macros for performance tracing. Normally turned off. Only works
2810528656
** on i486 hardware.
@@ -28281,10 +28832,14 @@
2828128832
#if SQLITE_OS_WINCE
2828228833
# define AreFileApisANSI() 1
2828328834
# define FormatMessageW(a,b,c,d,e,f,g) 0
2828428835
#endif
2828528836
28837
+/* Forward references */
28838
+typedef struct winShm winShm; /* A connection to shared-memory */
28839
+typedef struct winShmNode winShmNode; /* A region of shared-memory */
28840
+
2828628841
/*
2828728842
** WinCE lacks native support for file locking so we have to fake it
2828828843
** with some code of our own.
2828928844
*/
2829028845
#if SQLITE_OS_WINCE
@@ -28300,16 +28855,19 @@
2830028855
** The winFile structure is a subclass of sqlite3_file* specific to the win32
2830128856
** portability layer.
2830228857
*/
2830328858
typedef struct winFile winFile;
2830428859
struct winFile {
28305
- const sqlite3_io_methods *pMethod;/* Must be first */
28860
+ const sqlite3_io_methods *pMethod; /*** Must be first ***/
28861
+ sqlite3_vfs *pVfs; /* The VFS used to open this file */
2830628862
HANDLE h; /* Handle for accessing the file */
2830728863
unsigned char locktype; /* Type of lock currently held on this file */
2830828864
short sharedLockByte; /* Randomly chosen byte used as a shared lock */
2830928865
DWORD lastErrno; /* The Windows errno from the last I/O error */
2831028866
DWORD sectorSize; /* Sector size of the device file is on */
28867
+ winShm *pShm; /* Instance of shared memory on this file */
28868
+ const char *zPath; /* Full pathname of this file */
2831128869
#if SQLITE_OS_WINCE
2831228870
WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
2831328871
HANDLE hMutex; /* Mutex used to control access to shared lock */
2831428872
HANDLE hShared; /* Shared memory segment used for locking */
2831528873
winceLock local; /* Locks obtained by this instance of winFile */
@@ -28833,11 +29391,12 @@
2883329391
static int winClose(sqlite3_file *id){
2883429392
int rc, cnt = 0;
2883529393
winFile *pFile = (winFile*)id;
2883629394
2883729395
assert( id!=0 );
28838
- OSTRACE2("CLOSE %d\n", pFile->h);
29396
+ assert( pFile->pShm==0 );
29397
+ OSTRACE(("CLOSE %d\n", pFile->h));
2883929398
do{
2884029399
rc = CloseHandle(pFile->h);
2884129400
}while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
2884229401
#if SQLITE_OS_WINCE
2884329402
#define WINCE_DELETION_ATTEMPTS 3
@@ -28852,10 +29411,11 @@
2885229411
Sleep(100); /* Wait a little before trying again */
2885329412
}
2885429413
free(pFile->zDeleteOnClose);
2885529414
}
2885629415
#endif
29416
+ OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
2885729417
OpenCounter(-1);
2885829418
return rc ? SQLITE_OK : SQLITE_IOERR;
2885929419
}
2886029420
2886129421
/*
@@ -28883,11 +29443,11 @@
2888329443
DWORD error;
2888429444
DWORD got;
2888529445
2888629446
assert( id!=0 );
2888729447
SimulateIOError(return SQLITE_IOERR_READ);
28888
- OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype);
29448
+ OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
2888929449
rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
2889029450
if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
2889129451
pFile->lastErrno = error;
2889229452
return SQLITE_FULL;
2889329453
}
@@ -28922,11 +29482,11 @@
2892229482
DWORD wrote = 0;
2892329483
2892429484
assert( id!=0 );
2892529485
SimulateIOError(return SQLITE_IOERR_WRITE);
2892629486
SimulateDiskfullError(return SQLITE_FULL);
28927
- OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype);
29487
+ OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
2892829488
rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
2892929489
if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
2893029490
pFile->lastErrno = error;
2893129491
return SQLITE_FULL;
2893229492
}
@@ -28950,28 +29510,29 @@
2895029510
** Truncate an open file to a specified size
2895129511
*/
2895229512
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
2895329513
LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
2895429514
LONG lowerBits = (LONG)(nByte & 0xffffffff);
28955
- DWORD rc;
29515
+ DWORD dwRet;
2895629516
winFile *pFile = (winFile*)id;
2895729517
DWORD error;
29518
+ int rc = SQLITE_OK;
2895829519
2895929520
assert( id!=0 );
28960
- OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte);
29521
+ OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
2896129522
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
28962
- rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
28963
- if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29523
+ dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29524
+ if( dwRet==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
2896429525
pFile->lastErrno = error;
28965
- return SQLITE_IOERR_TRUNCATE;
28966
- }
29526
+ rc = SQLITE_IOERR_TRUNCATE;
2896729527
/* SetEndOfFile will fail if nByte is negative */
28968
- if( !SetEndOfFile(pFile->h) ){
29528
+ }else if( !SetEndOfFile(pFile->h) ){
2896929529
pFile->lastErrno = GetLastError();
28970
- return SQLITE_IOERR_TRUNCATE;
29530
+ rc = SQLITE_IOERR_TRUNCATE;
2897129531
}
28972
- return SQLITE_OK;
29532
+ OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc==SQLITE_OK ? "ok" : "failed"));
29533
+ return rc;
2897329534
}
2897429535
2897529536
#ifdef SQLITE_TEST
2897629537
/*
2897729538
** Count the number of fullsyncs and normal syncs. This is used to test
@@ -28987,11 +29548,11 @@
2898729548
static int winSync(sqlite3_file *id, int flags){
2898829549
#ifndef SQLITE_NO_SYNC
2898929550
winFile *pFile = (winFile*)id;
2899029551
2899129552
assert( id!=0 );
28992
- OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype);
29553
+ OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
2899329554
#else
2899429555
UNUSED_PARAMETER(id);
2899529556
#endif
2899629557
#ifndef SQLITE_TEST
2899729558
UNUSED_PARAMETER(flags);
@@ -29128,12 +29689,12 @@
2912829689
int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
2912929690
winFile *pFile = (winFile*)id;
2913029691
DWORD error = NO_ERROR;
2913129692
2913229693
assert( id!=0 );
29133
- OSTRACE5("LOCK %d %d was %d(%d)\n",
29134
- pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
29694
+ OSTRACE(("LOCK %d %d was %d(%d)\n",
29695
+ pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
2913529696
2913629697
/* If there is already a lock of this type or more restrictive on the
2913729698
** OsFile, do nothing. Don't use the end_lock: exit path, as
2913829699
** sqlite3OsEnterMutex() hasn't been called yet.
2913929700
*/
@@ -29159,11 +29720,11 @@
2915929720
int cnt = 3;
2916029721
while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
2916129722
/* Try 3 times to get the pending lock. The pending lock might be
2916229723
** held by another reader process who will release it momentarily.
2916329724
*/
29164
- OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
29725
+ OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
2916529726
Sleep(1);
2916629727
}
2916729728
gotPendingLock = res;
2916829729
if( !res ){
2916929730
error = GetLastError();
@@ -29204,17 +29765,17 @@
2920429765
/* Acquire an EXCLUSIVE lock
2920529766
*/
2920629767
if( locktype==EXCLUSIVE_LOCK && res ){
2920729768
assert( pFile->locktype>=SHARED_LOCK );
2920829769
res = unlockReadLock(pFile);
29209
- OSTRACE2("unreadlock = %d\n", res);
29770
+ OSTRACE(("unreadlock = %d\n", res));
2921029771
res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
2921129772
if( res ){
2921229773
newLocktype = EXCLUSIVE_LOCK;
2921329774
}else{
2921429775
error = GetLastError();
29215
- OSTRACE2("error-code = %d\n", error);
29776
+ OSTRACE(("error-code = %d\n", error));
2921629777
getReadLock(pFile);
2921729778
}
2921829779
}
2921929780
2922029781
/* If we are holding a PENDING lock that ought to be released, then
@@ -29228,12 +29789,12 @@
2922829789
** return the appropriate result code.
2922929790
*/
2923029791
if( res ){
2923129792
rc = SQLITE_OK;
2923229793
}else{
29233
- OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
29234
- locktype, newLocktype);
29794
+ OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
29795
+ locktype, newLocktype));
2923529796
pFile->lastErrno = error;
2923629797
rc = SQLITE_BUSY;
2923729798
}
2923829799
pFile->locktype = (u8)newLocktype;
2923929800
return rc;
@@ -29249,18 +29810,18 @@
2924929810
winFile *pFile = (winFile*)id;
2925029811
2925129812
assert( id!=0 );
2925229813
if( pFile->locktype>=RESERVED_LOCK ){
2925329814
rc = 1;
29254
- OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
29815
+ OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
2925529816
}else{
2925629817
rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
2925729818
if( rc ){
2925829819
UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
2925929820
}
2926029821
rc = !rc;
29261
- OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
29822
+ OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
2926229823
}
2926329824
*pResOut = rc;
2926429825
return SQLITE_OK;
2926529826
}
2926629827
@@ -29279,12 +29840,12 @@
2927929840
int type;
2928029841
winFile *pFile = (winFile*)id;
2928129842
int rc = SQLITE_OK;
2928229843
assert( pFile!=0 );
2928329844
assert( locktype<=SHARED_LOCK );
29284
- OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
29285
- pFile->locktype, pFile->sharedLockByte);
29845
+ OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
29846
+ pFile->locktype, pFile->sharedLockByte));
2928629847
type = pFile->locktype;
2928729848
if( type>=EXCLUSIVE_LOCK ){
2928829849
UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
2928929850
if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
2929029851
/* This should never happen. We should always be able to
@@ -29316,10 +29877,15 @@
2931629877
}
2931729878
case SQLITE_LAST_ERRNO: {
2931829879
*(int*)pArg = (int)((winFile*)id)->lastErrno;
2931929880
return SQLITE_OK;
2932029881
}
29882
+ case SQLITE_FCNTL_SIZE_HINT: {
29883
+ sqlite3_int64 sz = *(sqlite3_int64*)pArg;
29884
+ winTruncate(id, sz);
29885
+ return SQLITE_OK;
29886
+ }
2932129887
}
2932229888
return SQLITE_ERROR;
2932329889
}
2932429890
2932529891
/*
@@ -29343,16 +29909,535 @@
2934329909
static int winDeviceCharacteristics(sqlite3_file *id){
2934429910
UNUSED_PARAMETER(id);
2934529911
return 0;
2934629912
}
2934729913
29914
+/****************************************************************************
29915
+********************************* Shared Memory *****************************
29916
+**
29917
+** The next subdivision of code manages the shared-memory primitives.
29918
+*/
29919
+#ifndef SQLITE_OMIT_WAL
29920
+
29921
+/*
29922
+** Helper functions to obtain and relinquish the global mutex. The
29923
+** global mutex is used to protect the winLockInfo objects used by
29924
+** this file, all of which may be shared by multiple threads.
29925
+**
29926
+** Function winShmMutexHeld() is used to assert() that the global mutex
29927
+** is held when required. This function is only used as part of assert()
29928
+** statements. e.g.
29929
+**
29930
+** winShmEnterMutex()
29931
+** assert( winShmMutexHeld() );
29932
+** winEnterLeave()
29933
+*/
29934
+static void winShmEnterMutex(void){
29935
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29936
+}
29937
+static void winShmLeaveMutex(void){
29938
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29939
+}
29940
+#ifdef SQLITE_DEBUG
29941
+static int winShmMutexHeld(void) {
29942
+ return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29943
+}
29944
+#endif
29945
+
29946
+/*
29947
+** Object used to represent a single file opened and mmapped to provide
29948
+** shared memory. When multiple threads all reference the same
29949
+** log-summary, each thread has its own winFile object, but they all
29950
+** point to a single instance of this object. In other words, each
29951
+** log-summary is opened only once per process.
29952
+**
29953
+** winShmMutexHeld() must be true when creating or destroying
29954
+** this object or while reading or writing the following fields:
29955
+**
29956
+** nRef
29957
+** pNext
29958
+**
29959
+** The following fields are read-only after the object is created:
29960
+**
29961
+** fid
29962
+** zFilename
29963
+**
29964
+** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
29965
+** winShmMutexHeld() is true when reading or writing any other field
29966
+** in this structure.
29967
+**
29968
+** To avoid deadlocks, mutex and mutexBuf are always released in the
29969
+** reverse order that they are acquired. mutexBuf is always acquired
29970
+** first and released last. This invariant is check by asserting
29971
+** sqlite3_mutex_notheld() on mutex whenever mutexBuf is acquired or
29972
+** released.
29973
+*/
29974
+struct winShmNode {
29975
+ sqlite3_mutex *mutex; /* Mutex to access this object */
29976
+ char *zFilename; /* Name of the file */
29977
+ winFile hFile; /* File handle from winOpen */
29978
+
29979
+ int szRegion; /* Size of shared-memory regions */
29980
+ int nRegion; /* Size of array apRegion */
29981
+ struct ShmRegion {
29982
+ HANDLE hMap; /* File handle from CreateFileMapping */
29983
+ void *pMap;
29984
+ } *aRegion;
29985
+ DWORD lastErrno; /* The Windows errno from the last I/O error */
29986
+
29987
+ int nRef; /* Number of winShm objects pointing to this */
29988
+ winShm *pFirst; /* All winShm objects pointing to this */
29989
+ winShmNode *pNext; /* Next in list of all winShmNode objects */
29990
+#ifdef SQLITE_DEBUG
29991
+ u8 nextShmId; /* Next available winShm.id value */
29992
+#endif
29993
+};
29994
+
29995
+/*
29996
+** A global array of all winShmNode objects.
29997
+**
29998
+** The winShmMutexHeld() must be true while reading or writing this list.
29999
+*/
30000
+static winShmNode *winShmNodeList = 0;
30001
+
30002
+/*
30003
+** Structure used internally by this VFS to record the state of an
30004
+** open shared memory connection.
30005
+**
30006
+** winShm.pFile->mutex must be held while reading or writing the
30007
+** winShm.pNext and winShm.locks[] elements.
30008
+**
30009
+** The winShm.pFile element is initialized when the object is created
30010
+** and is read-only thereafter.
30011
+*/
30012
+struct winShm {
30013
+ winShmNode *pShmNode; /* The underlying winShmNode object */
30014
+ winShm *pNext; /* Next winShm with the same winShmNode */
30015
+ u8 hasMutex; /* True if holding the winShmNode mutex */
30016
+ u8 hasMutexBuf; /* True if holding pFile->mutexBuf */
30017
+#ifdef SQLITE_DEBUG
30018
+ u8 id; /* Id of this connection with its winShmNode */
30019
+#endif
30020
+};
30021
+
30022
+/*
30023
+** Constants used for locking
30024
+*/
30025
+#define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30026
+#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30027
+
30028
+/*
30029
+** Apply advisory locks for all n bytes beginning at ofst.
30030
+*/
30031
+#define _SHM_UNLCK 1
30032
+#define _SHM_RDLCK 2
30033
+#define _SHM_WRLCK 3
30034
+static int winShmSystemLock(
30035
+ winShmNode *pFile, /* Apply locks to this open shared-memory segment */
30036
+ int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
30037
+ int ofst, /* Offset to first byte to be locked/unlocked */
30038
+ int nByte /* Number of bytes to lock or unlock */
30039
+){
30040
+ OVERLAPPED ovlp;
30041
+ DWORD dwFlags;
30042
+ int rc = 0; /* Result code form Lock/UnlockFileEx() */
30043
+
30044
+ /* Access to the winShmNode object is serialized by the caller */
30045
+ assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
30046
+
30047
+ /* Initialize the locking parameters */
30048
+ dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
30049
+ if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
30050
+
30051
+ /* Find the first bit in lockMask that is set */
30052
+ memset(&ovlp, 0, sizeof(OVERLAPPED));
30053
+ ovlp.Offset = ofst;
30054
+
30055
+ /* Release/Acquire the system-level lock */
30056
+ if( lockType==_SHM_UNLCK ){
30057
+ rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
30058
+ }else{
30059
+ rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
30060
+ }
30061
+ if( !rc ){
30062
+ OSTRACE(("SHM-LOCK %d %s ERROR 0x%08lx\n",
30063
+ pFile->hFile.h,
30064
+ lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
30065
+ GetLastError()));
30066
+ }
30067
+ rc = (rc!=0) ? SQLITE_OK : SQLITE_BUSY;
30068
+
30069
+ return rc;
30070
+}
30071
+
30072
+/* Forward references to VFS methods */
30073
+static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
30074
+static int winDelete(sqlite3_vfs *,const char*,int);
30075
+
30076
+/*
30077
+** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
30078
+**
30079
+** This is not a VFS shared-memory method; it is a utility function called
30080
+** by VFS shared-memory methods.
30081
+*/
30082
+static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
30083
+ winShmNode **pp;
30084
+ winShmNode *p;
30085
+ assert( winShmMutexHeld() );
30086
+ pp = &winShmNodeList;
30087
+ while( (p = *pp)!=0 ){
30088
+ if( p->nRef==0 ){
30089
+ int i;
30090
+ if( p->mutex ) sqlite3_mutex_free(p->mutex);
30091
+ for(i=0; i<p->nRegion; i++){
30092
+ UnmapViewOfFile(p->aRegion[i].pMap);
30093
+ CloseHandle(p->aRegion[i].hMap);
30094
+ }
30095
+ if( p->hFile.h != INVALID_HANDLE_VALUE ) {
30096
+ winClose((sqlite3_file *)&p->hFile);
30097
+ }
30098
+ if( deleteFlag ) winDelete(pVfs, p->zFilename, 0);
30099
+ *pp = p->pNext;
30100
+ sqlite3_free(p->aRegion);
30101
+ sqlite3_free(p);
30102
+ }else{
30103
+ pp = &p->pNext;
30104
+ }
30105
+ }
30106
+}
30107
+
30108
+/*
30109
+** Open a shared-memory area. This particular implementation uses
30110
+** mmapped files.
30111
+**
30112
+** zName is a filename used to identify the shared-memory area. The
30113
+** implementation does not (and perhaps should not) use this name
30114
+** directly, but rather use it as a template for finding an appropriate
30115
+** name for the shared-memory storage. In this implementation, the
30116
+** string "-index" is appended to zName and used as the name of the
30117
+** mmapped file.
30118
+**
30119
+** When opening a new shared-memory file, if no other instances of that
30120
+** file are currently open, in this process or in other processes, then
30121
+** the file must be truncated to zero length or have its header cleared.
30122
+*/
30123
+static int winShmOpen(
30124
+ sqlite3_file *fd /* The file to which to attach shared memory */
30125
+){
30126
+ struct winFile *pDbFd; /* Database to which to attach SHM */
30127
+ struct winShm *p; /* The connection to be opened */
30128
+ struct winShmNode *pShmNode = 0; /* The underlying mmapped file */
30129
+ int rc; /* Result code */
30130
+ struct winShmNode *pNew; /* Newly allocated winShmNode */
30131
+ int nName; /* Size of zName in bytes */
30132
+
30133
+ pDbFd = (winFile*)fd;
30134
+ assert( pDbFd->pShm==0 ); /* Not previously opened */
30135
+
30136
+ /* Allocate space for the new sqlite3_shm object. Also speculatively
30137
+ ** allocate space for a new winShmNode and filename.
30138
+ */
30139
+ p = sqlite3_malloc( sizeof(*p) );
30140
+ if( p==0 ) return SQLITE_NOMEM;
30141
+ memset(p, 0, sizeof(*p));
30142
+ nName = sqlite3Strlen30(pDbFd->zPath);
30143
+ pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
30144
+ if( pNew==0 ){
30145
+ sqlite3_free(p);
30146
+ return SQLITE_NOMEM;
30147
+ }
30148
+ memset(pNew, 0, sizeof(*pNew));
30149
+ pNew->zFilename = (char*)&pNew[1];
30150
+ sqlite3_snprintf(nName+15, pNew->zFilename, "%s-wal-index", pDbFd->zPath);
30151
+
30152
+ /* Look to see if there is an existing winShmNode that can be used.
30153
+ ** If no matching winShmNode currently exists, create a new one.
30154
+ */
30155
+ winShmEnterMutex();
30156
+ for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
30157
+ /* TBD need to come up with better match here. Perhaps
30158
+ ** use FILE_ID_BOTH_DIR_INFO Structure.
30159
+ */
30160
+ if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
30161
+ }
30162
+ if( pShmNode ){
30163
+ sqlite3_free(pNew);
30164
+ }else{
30165
+ pShmNode = pNew;
30166
+ pNew = 0;
30167
+ ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
30168
+ pShmNode->pNext = winShmNodeList;
30169
+ winShmNodeList = pShmNode;
30170
+
30171
+ pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
30172
+ if( pShmNode->mutex==0 ){
30173
+ rc = SQLITE_NOMEM;
30174
+ goto shm_open_err;
30175
+ }
30176
+ rc = winOpen(pDbFd->pVfs,
30177
+ pShmNode->zFilename, /* Name of the file (UTF-8) */
30178
+ (sqlite3_file*)&pShmNode->hFile, /* File handle here */
30179
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
30180
+ 0);
30181
+ if( SQLITE_OK!=rc ){
30182
+ rc = SQLITE_CANTOPEN_BKPT;
30183
+ goto shm_open_err;
30184
+ }
30185
+
30186
+ /* Check to see if another process is holding the dead-man switch.
30187
+ ** If not, truncate the file to zero length.
30188
+ */
30189
+ if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
30190
+ rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
30191
+ }
30192
+ if( rc==SQLITE_OK ){
30193
+ winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
30194
+ rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
30195
+ }
30196
+ if( rc ) goto shm_open_err;
30197
+ }
30198
+
30199
+ /* Make the new connection a child of the winShmNode */
30200
+ p->pShmNode = pShmNode;
30201
+ p->pNext = pShmNode->pFirst;
30202
+#ifdef SQLITE_DEBUG
30203
+ p->id = pShmNode->nextShmId++;
30204
+#endif
30205
+ pShmNode->pFirst = p;
30206
+ pShmNode->nRef++;
30207
+ pDbFd->pShm = p;
30208
+ winShmLeaveMutex();
30209
+ return SQLITE_OK;
30210
+
30211
+ /* Jump here on any error */
30212
+shm_open_err:
30213
+ winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
30214
+ winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
30215
+ sqlite3_free(p);
30216
+ sqlite3_free(pNew);
30217
+ winShmLeaveMutex();
30218
+ return rc;
30219
+}
30220
+
30221
+/*
30222
+** Close a connection to shared-memory. Delete the underlying
30223
+** storage if deleteFlag is true.
30224
+*/
30225
+static int winShmClose(
30226
+ sqlite3_file *fd, /* Database holding shared memory */
30227
+ int deleteFlag /* Delete after closing if true */
30228
+){
30229
+ winFile *pDbFd; /* Database holding shared-memory */
30230
+ winShm *p; /* The connection to be closed */
30231
+ winShmNode *pShmNode; /* The underlying shared-memory file */
30232
+ winShm **pp; /* For looping over sibling connections */
30233
+
30234
+ pDbFd = (winFile*)fd;
30235
+ p = pDbFd->pShm;
30236
+ pShmNode = p->pShmNode;
30237
+
30238
+ /* Remove connection p from the set of connections associated
30239
+ ** with pShmNode */
30240
+ sqlite3_mutex_enter(pShmNode->mutex);
30241
+ for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
30242
+ *pp = p->pNext;
30243
+
30244
+ /* Free the connection p */
30245
+ sqlite3_free(p);
30246
+ pDbFd->pShm = 0;
30247
+ sqlite3_mutex_leave(pShmNode->mutex);
30248
+
30249
+ /* If pShmNode->nRef has reached 0, then close the underlying
30250
+ ** shared-memory file, too */
30251
+ winShmEnterMutex();
30252
+ assert( pShmNode->nRef>0 );
30253
+ pShmNode->nRef--;
30254
+ if( pShmNode->nRef==0 ){
30255
+ winShmPurge(pDbFd->pVfs, deleteFlag);
30256
+ }
30257
+ winShmLeaveMutex();
30258
+
30259
+ return SQLITE_OK;
30260
+}
30261
+
30262
+/*
30263
+** This function is called to obtain a pointer to region iRegion of the
30264
+** shared-memory associated with the database file fd. Shared-memory regions
30265
+** are numbered starting from zero. Each shared-memory region is szRegion
30266
+** bytes in size.
30267
+**
30268
+** If an error occurs, an error code is returned and *pp is set to NULL.
30269
+**
30270
+** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
30271
+** region has not been allocated (by any client, including one running in a
30272
+** separate process), then *pp is set to NULL and SQLITE_OK returned. If
30273
+** isWrite is non-zero and the requested shared-memory region has not yet
30274
+** been allocated, it is allocated by this function.
30275
+**
30276
+** If the shared-memory region has already been allocated or is allocated by
30277
+** this call as described above, then it is mapped into this processes
30278
+** address space (if it is not already), *pp is set to point to the mapped
30279
+** memory and SQLITE_OK returned.
30280
+*/
30281
+static int winShmMap(
30282
+ sqlite3_file *fd, /* Handle open on database file */
30283
+ int iRegion, /* Region to retrieve */
30284
+ int szRegion, /* Size of regions */
30285
+ int isWrite, /* True to extend file if necessary */
30286
+ void volatile **pp /* OUT: Mapped memory */
30287
+){
30288
+ winFile *pDbFd = (winFile*)fd;
30289
+ winShm *p = pDbFd->pShm;
30290
+ winShmNode *pShmNode = p->pShmNode;
30291
+ int rc = SQLITE_OK;
30292
+
30293
+ sqlite3_mutex_enter(pShmNode->mutex);
30294
+ assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
30295
+
30296
+ if( pShmNode->nRegion<=iRegion ){
30297
+ struct ShmRegion *apNew; /* New aRegion[] array */
30298
+ int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
30299
+ sqlite3_int64 sz; /* Current size of wal-index file */
30300
+
30301
+ pShmNode->szRegion = szRegion;
30302
+
30303
+ /* The requested region is not mapped into this processes address space.
30304
+ ** Check to see if it has been allocated (i.e. if the wal-index file is
30305
+ ** large enough to contain the requested region).
30306
+ */
30307
+ rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
30308
+ if( rc!=SQLITE_OK ){
30309
+ goto shmpage_out;
30310
+ }
30311
+
30312
+ if( sz<nByte ){
30313
+ /* The requested memory region does not exist. If isWrite is set to
30314
+ ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
30315
+ **
30316
+ ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
30317
+ ** the requested memory region.
30318
+ */
30319
+ if( !isWrite ) goto shmpage_out;
30320
+ rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
30321
+ if( rc!=SQLITE_OK ){
30322
+ goto shmpage_out;
30323
+ }
30324
+ }
30325
+
30326
+ /* Map the requested memory region into this processes address space. */
30327
+ apNew = (struct ShmRegion *)sqlite3_realloc(
30328
+ pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
30329
+ );
30330
+ if( !apNew ){
30331
+ rc = SQLITE_IOERR_NOMEM;
30332
+ goto shmpage_out;
30333
+ }
30334
+ pShmNode->aRegion = apNew;
30335
+
30336
+ while( pShmNode->nRegion<=iRegion ){
30337
+ HANDLE hMap; /* file-mapping handle */
30338
+ void *pMap = 0; /* Mapped memory region */
30339
+
30340
+ hMap = CreateFileMapping(pShmNode->hFile.h,
30341
+ NULL, PAGE_READWRITE, 0, nByte, NULL
30342
+ );
30343
+ if( hMap ){
30344
+ pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
30345
+ 0, 0, nByte
30346
+ );
30347
+ }
30348
+ if( !pMap ){
30349
+ pShmNode->lastErrno = GetLastError();
30350
+ rc = SQLITE_IOERR;
30351
+ if( hMap ) CloseHandle(hMap);
30352
+ goto shmpage_out;
30353
+ }
30354
+
30355
+ pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
30356
+ pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
30357
+ pShmNode->nRegion++;
30358
+ }
30359
+ }
30360
+
30361
+shmpage_out:
30362
+ if( pShmNode->nRegion>iRegion ){
30363
+ char *p = (char *)pShmNode->aRegion[iRegion].pMap;
30364
+ *pp = (void *)&p[iRegion*szRegion];
30365
+ }else{
30366
+ *pp = 0;
30367
+ }
30368
+ sqlite3_mutex_leave(pShmNode->mutex);
30369
+ return rc;
30370
+}
30371
+
30372
+/*
30373
+** Change the lock state for a shared-memory segment.
30374
+*/
30375
+static int winShmLock(
30376
+ sqlite3_file *fd, /* Database file holding the shared memory */
30377
+ int ofst, /* First lock to acquire or release */
30378
+ int n, /* Number of locks to acquire or release */
30379
+ int flags /* What to do with the lock */
30380
+){
30381
+ winFile *pDbFd = (winFile*)fd;
30382
+ winShm *p = pDbFd->pShm;
30383
+ winShmNode *pShmNode = p->pShmNode;
30384
+ int rc = SQLITE_PROTOCOL;
30385
+
30386
+ assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
30387
+ assert( n>=1 );
30388
+ assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
30389
+ || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
30390
+ || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
30391
+ || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
30392
+ assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
30393
+
30394
+ sqlite3_mutex_enter(pShmNode->mutex);
30395
+ if( flags & SQLITE_SHM_UNLOCK ){
30396
+ rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
30397
+ }else if( flags & SQLITE_SHM_SHARED ){
30398
+ rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
30399
+ }else{
30400
+ rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
30401
+ }
30402
+ sqlite3_mutex_leave(pShmNode->mutex);
30403
+ OSTRACE(("SHM-LOCK shmid-%d, pid-%d %s\n",
30404
+ p->id, (int)GetCurrentProcessId(), rc ? "failed" : "ok"));
30405
+ return rc;
30406
+}
30407
+
30408
+/*
30409
+** Implement a memory barrier or memory fence on shared memory.
30410
+**
30411
+** All loads and stores begun before the barrier must complete before
30412
+** any load or store begun after the barrier.
30413
+*/
30414
+static void winShmBarrier(
30415
+ sqlite3_file *fd /* Database holding the shared memory */
30416
+){
30417
+ /* MemoryBarrier(); // does not work -- do not know why not */
30418
+ winShmEnterMutex();
30419
+ winShmLeaveMutex();
30420
+}
30421
+
30422
+#else
30423
+# define winShmOpen 0
30424
+# define winShmMap 0
30425
+# define winShmLock 0
30426
+# define winShmBarrier 0
30427
+# define winShmClose 0
30428
+#endif /* #ifndef SQLITE_OMIT_WAL */
30429
+/*
30430
+***************************** End Shared Memory *****************************
30431
+****************************************************************************/
30432
+
2934830433
/*
2934930434
** This vector defines all the methods that can operate on an
2935030435
** sqlite3_file for win32.
2935130436
*/
2935230437
static const sqlite3_io_methods winIoMethod = {
29353
- 1, /* iVersion */
30438
+ 2, /* iVersion */
2935430439
winClose,
2935530440
winRead,
2935630441
winWrite,
2935730442
winTruncate,
2935830443
winSync,
@@ -29360,11 +30445,16 @@
2936030445
winLock,
2936130446
winUnlock,
2936230447
winCheckReservedLock,
2936330448
winFileControl,
2936430449
winSectorSize,
29365
- winDeviceCharacteristics
30450
+ winDeviceCharacteristics,
30451
+ winShmOpen, /* xShmOpen */
30452
+ winShmLock, /* xShmLock */
30453
+ winShmMap, /* xShmMap */
30454
+ winShmBarrier, /* xShmBarrier */
30455
+ winShmClose /* xShmClose */
2936630456
};
2936730457
2936830458
/***************************************************************************
2936930459
** Here ends the I/O methods that form the sqlite3_io_methods object.
2937030460
**
@@ -29442,11 +30532,11 @@
2944230532
sqlite3_randomness(20, &zBuf[j]);
2944330533
for(i=0; i<20; i++, j++){
2944430534
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2944530535
}
2944630536
zBuf[j] = 0;
29447
- OSTRACE2("TEMP FILENAME: %s\n", zBuf);
30537
+ OSTRACE(("TEMP FILENAME: %s\n", zBuf));
2944830538
return SQLITE_OK;
2944930539
}
2945030540
2945130541
/*
2945230542
** The return value of getLastErrorMsg
@@ -29533,10 +30623,12 @@
2953330623
const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
2953430624
char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
2953530625
2953630626
assert( id!=0 );
2953730627
UNUSED_PARAMETER(pVfs);
30628
+
30629
+ pFile->h = INVALID_HANDLE_VALUE;
2953830630
2953930631
/* If the second argument to this function is NULL, generate a
2954030632
** temporary file name to use
2954130633
*/
2954230634
if( !zUtf8Name ){
@@ -29615,10 +30707,13 @@
2961530707
dwFlagsAndAttributes,
2961630708
NULL
2961730709
);
2961830710
#endif
2961930711
}
30712
+ OSTRACE(("OPEN %d %s 0x%lx %s\n",
30713
+ h, zName, dwDesiredAccess,
30714
+ h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
2962030715
if( h==INVALID_HANDLE_VALUE ){
2962130716
free(zConverted);
2962230717
if( flags & SQLITE_OPEN_READWRITE ){
2962330718
return winOpen(pVfs, zName, id,
2962430719
((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
@@ -29635,10 +30730,13 @@
2963530730
}
2963630731
memset(pFile, 0, sizeof(*pFile));
2963730732
pFile->pMethod = &winIoMethod;
2963830733
pFile->h = h;
2963930734
pFile->lastErrno = NO_ERROR;
30735
+ pFile->pVfs = pVfs;
30736
+ pFile->pShm = 0;
30737
+ pFile->zPath = zName;
2964030738
pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
2964130739
#if SQLITE_OS_WINCE
2964230740
if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
2964330741
(SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
2964430742
&& !winceCreateLock(zName, pFile)
@@ -29706,11 +30804,14 @@
2970630804
&& (++cnt < MX_DELETION_ATTEMPTS)
2970730805
&& (Sleep(100), 1) );
2970830806
#endif
2970930807
}
2971030808
free(zConverted);
29711
- OSTRACE2("DELETE \"%s\"\n", zFilename);
30809
+ OSTRACE(("DELETE \"%s\" %s\n", zFilename,
30810
+ ( (rc==INVALID_FILE_ATTRIBUTES) && (error==ERROR_FILE_NOT_FOUND)) ?
30811
+ "ok" : "failed" ));
30812
+
2971230813
return ( (rc == INVALID_FILE_ATTRIBUTES)
2971330814
&& (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
2971430815
}
2971530816
2971630817
/*
@@ -29999,38 +31100,36 @@
2999931100
UNUSED_PARAMETER(pVfs);
3000031101
return ((microsec+999)/1000)*1000;
3000131102
}
3000231103
3000331104
/*
30004
-** The following variable, if set to a non-zero value, becomes the result
30005
-** returned from sqlite3OsCurrentTime(). This is used for testing.
31105
+** The following variable, if set to a non-zero value, is interpreted as
31106
+** the number of seconds since 1970 and is used to set the result of
31107
+** sqlite3OsCurrentTime() during testing.
3000631108
*/
3000731109
#ifdef SQLITE_TEST
30008
-SQLITE_API int sqlite3_current_time = 0;
31110
+SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
3000931111
#endif
3001031112
3001131113
/*
30012
-** Find the current time (in Universal Coordinated Time). Write the
30013
-** current time and date as a Julian Day number into *prNow and
30014
-** return 0. Return 1 if the time and date cannot be found.
31114
+** Find the current time (in Universal Coordinated Time). Write into *piNow
31115
+** the current time and date as a Julian Day number times 86_400_000. In
31116
+** other words, write into *piNow the number of milliseconds since the Julian
31117
+** epoch of noon in Greenwich on November 24, 4714 B.C according to the
31118
+** proleptic Gregorian calendar.
31119
+**
31120
+** On success, return 0. Return 1 if the time and date cannot be found.
3001531121
*/
30016
-int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
30017
- FILETIME ft;
31122
+static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
3001831123
/* FILETIME structure is a 64-bit value representing the number of
3001931124
100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
3002031125
*/
30021
- sqlite3_int64 timeW; /* Whole days */
30022
- sqlite3_int64 timeF; /* Fractional Days */
30023
-
30024
- /* Number of 100-nanosecond intervals in a single day */
30025
- static const sqlite3_int64 ntuPerDay =
30026
- 10000000*(sqlite3_int64)86400;
30027
-
30028
- /* Number of 100-nanosecond intervals in half of a day */
30029
- static const sqlite3_int64 ntuPerHalfDay =
30030
- 10000000*(sqlite3_int64)43200;
30031
-
31126
+ FILETIME ft;
31127
+ static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
31128
+#ifdef SQLITE_TEST
31129
+ static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
31130
+#endif
3003231131
/* 2^32 - to avoid use of LL and warnings in gcc */
3003331132
static const sqlite3_int64 max32BitValue =
3003431133
(sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
3003531134
3003631135
#if SQLITE_OS_WINCE
@@ -30041,26 +31140,38 @@
3004131140
return 1;
3004231141
}
3004331142
#else
3004431143
GetSystemTimeAsFileTime( &ft );
3004531144
#endif
30046
- UNUSED_PARAMETER(pVfs);
30047
- timeW = (((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + (sqlite3_int64)ft.dwLowDateTime;
30048
- timeF = timeW % ntuPerDay; /* fractional days (100-nanoseconds) */
30049
- timeW = timeW / ntuPerDay; /* whole days */
30050
- timeW = timeW + 2305813; /* add whole days (from 2305813.5) */
30051
- timeF = timeF + ntuPerHalfDay; /* add half a day (from 2305813.5) */
30052
- timeW = timeW + (timeF/ntuPerDay); /* add whole day if half day made one */
30053
- timeF = timeF % ntuPerDay; /* compute new fractional days */
30054
- *prNow = (double)timeW + ((double)timeF / (double)ntuPerDay);
31145
+
31146
+ *piNow = winFiletimeEpoch +
31147
+ ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
31148
+ (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)1000;
31149
+
3005531150
#ifdef SQLITE_TEST
3005631151
if( sqlite3_current_time ){
30057
- *prNow = ((double)sqlite3_current_time + (double)43200) / (double)86400 + (double)2440587;
31152
+ *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
3005831153
}
3005931154
#endif
31155
+ UNUSED_PARAMETER(pVfs);
3006031156
return 0;
3006131157
}
31158
+
31159
+/*
31160
+** Find the current time (in Universal Coordinated Time). Write the
31161
+** current time and date as a Julian Day number into *prNow and
31162
+** return 0. Return 1 if the time and date cannot be found.
31163
+*/
31164
+int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
31165
+ int rc;
31166
+ sqlite3_int64 i;
31167
+ rc = winCurrentTimeInt64(pVfs, &i);
31168
+ if( !rc ){
31169
+ *prNow = i/86400000.0;
31170
+ }
31171
+ return rc;
31172
+}
3006231173
3006331174
/*
3006431175
** The idea is that this function works like a combination of
3006531176
** GetLastError() and FormatMessage() on windows (or errno and
3006631177
** strerror_r() on unix). After an error is returned by an OS
@@ -30092,35 +31203,38 @@
3009231203
*/
3009331204
static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
3009431205
UNUSED_PARAMETER(pVfs);
3009531206
return getLastErrorMsg(nBuf, zBuf);
3009631207
}
31208
+
31209
+
3009731210
3009831211
/*
3009931212
** Initialize and deinitialize the operating system interface.
3010031213
*/
3010131214
SQLITE_API int sqlite3_os_init(void){
3010231215
static sqlite3_vfs winVfs = {
30103
- 1, /* iVersion */
30104
- sizeof(winFile), /* szOsFile */
30105
- MAX_PATH, /* mxPathname */
30106
- 0, /* pNext */
30107
- "win32", /* zName */
30108
- 0, /* pAppData */
30109
-
30110
- winOpen, /* xOpen */
30111
- winDelete, /* xDelete */
30112
- winAccess, /* xAccess */
30113
- winFullPathname, /* xFullPathname */
30114
- winDlOpen, /* xDlOpen */
30115
- winDlError, /* xDlError */
30116
- winDlSym, /* xDlSym */
30117
- winDlClose, /* xDlClose */
30118
- winRandomness, /* xRandomness */
30119
- winSleep, /* xSleep */
30120
- winCurrentTime, /* xCurrentTime */
30121
- winGetLastError /* xGetLastError */
31216
+ 2, /* iVersion */
31217
+ sizeof(winFile), /* szOsFile */
31218
+ MAX_PATH, /* mxPathname */
31219
+ 0, /* pNext */
31220
+ "win32", /* zName */
31221
+ 0, /* pAppData */
31222
+ winOpen, /* xOpen */
31223
+ winDelete, /* xDelete */
31224
+ winAccess, /* xAccess */
31225
+ winFullPathname, /* xFullPathname */
31226
+ winDlOpen, /* xDlOpen */
31227
+ winDlError, /* xDlError */
31228
+ winDlSym, /* xDlSym */
31229
+ winDlClose, /* xDlClose */
31230
+ winRandomness, /* xRandomness */
31231
+ winSleep, /* xSleep */
31232
+ winCurrentTime, /* xCurrentTime */
31233
+ winGetLastError, /* xGetLastError */
31234
+ 0, /* xRename */
31235
+ winCurrentTimeInt64, /* xCurrentTimeInt64 */
3012231236
};
3012331237
3012431238
sqlite3_vfs_register(&winVfs, 1);
3012531239
return SQLITE_OK;
3012631240
}
@@ -30801,19 +31915,21 @@
3080131915
pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
3080231916
}
3080331917
3080431918
if( pPage ){
3080531919
if( !pPage->pData ){
30806
- memset(pPage, 0, sizeof(PgHdr) + pCache->szExtra);
30807
- pPage->pExtra = (void*)&pPage[1];
30808
- pPage->pData = (void *)&((char *)pPage)[sizeof(PgHdr) + pCache->szExtra];
31920
+ memset(pPage, 0, sizeof(PgHdr));
31921
+ pPage->pData = (void *)&pPage[1];
31922
+ pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
31923
+ memset(pPage->pExtra, 0, pCache->szExtra);
3080931924
pPage->pCache = pCache;
3081031925
pPage->pgno = pgno;
3081131926
}
3081231927
assert( pPage->pCache==pCache );
3081331928
assert( pPage->pgno==pgno );
30814
- assert( pPage->pExtra==(void *)&pPage[1] );
31929
+ assert( pPage->pData==(void *)&pPage[1] );
31930
+ assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
3081531931
3081631932
if( 0==pPage->nRef ){
3081731933
pCache->nRef++;
3081831934
}
3081931935
pPage->nRef++;
@@ -31840,11 +32956,11 @@
3184032956
** This function is called during initialization (sqlite3_initialize()) to
3184132957
** install the default pluggable cache module, assuming the user has not
3184232958
** already provided an alternative.
3184332959
*/
3184432960
SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
31845
- static sqlite3_pcache_methods defaultMethods = {
32961
+ static const sqlite3_pcache_methods defaultMethods = {
3184632962
0, /* pArg */
3184732963
pcache1Init, /* xInit */
3184832964
pcache1Shutdown, /* xShutdown */
3184932965
pcache1Create, /* xCreate */
3185032966
pcache1Cachesize, /* xCachesize */
@@ -32353,10 +33469,120 @@
3235333469
** locking to prevent two processes from writing the same database
3235433470
** file simultaneously, or one process from reading the database while
3235533471
** another is writing.
3235633472
*/
3235733473
#ifndef SQLITE_OMIT_DISKIO
33474
+/************** Include wal.h in the middle of pager.c ***********************/
33475
+/************** Begin file wal.h *********************************************/
33476
+/*
33477
+** 2010 February 1
33478
+**
33479
+** The author disclaims copyright to this source code. In place of
33480
+** a legal notice, here is a blessing:
33481
+**
33482
+** May you do good and not evil.
33483
+** May you find forgiveness for yourself and forgive others.
33484
+** May you share freely, never taking more than you give.
33485
+**
33486
+*************************************************************************
33487
+** This header file defines the interface to the write-ahead logging
33488
+** system. Refer to the comments below and the header comment attached to
33489
+** the implementation of each function in log.c for further details.
33490
+*/
33491
+
33492
+#ifndef _WAL_H_
33493
+#define _WAL_H_
33494
+
33495
+
33496
+#ifdef SQLITE_OMIT_WAL
33497
+# define sqlite3WalOpen(x,y,z) 0
33498
+# define sqlite3WalClose(w,x,y,z) 0
33499
+# define sqlite3WalBeginReadTransaction(y,z) 0
33500
+# define sqlite3WalEndReadTransaction(z)
33501
+# define sqlite3WalRead(v,w,x,y,z) 0
33502
+# define sqlite3WalDbsize(y,z)
33503
+# define sqlite3WalBeginWriteTransaction(y) 0
33504
+# define sqlite3WalEndWRiteTransaction(x) 0
33505
+# define sqlite3WalUndo(x,y,z) 0
33506
+# define sqlite3WalSavepoint(y,z)
33507
+# define sqlite3WalSavepointUndo(y,z) 0
33508
+# define sqlite3WalFrames(u,v,w,x,y,z) 0
33509
+# define sqlite3WalCheckpoint(u,v,w,x) 0
33510
+# define sqlite3WalCallback(z) 0
33511
+#else
33512
+
33513
+#define WAL_SAVEPOINT_NDATA 4
33514
+
33515
+/* Connection to a write-ahead log (WAL) file.
33516
+** There is one object of this type for each pager.
33517
+*/
33518
+typedef struct Wal Wal;
33519
+
33520
+/* Open and close a connection to a write-ahead log. */
33521
+SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *zName, Wal**);
33522
+SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
33523
+
33524
+/* Used by readers to open (lock) and close (unlock) a snapshot. A
33525
+** snapshot is like a read-transaction. It is the state of the database
33526
+** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
33527
+** preserves the current state even if the other threads or processes
33528
+** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
33529
+** transaction and releases the lock.
33530
+*/
33531
+SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
33532
+SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
33533
+
33534
+/* Read a page from the write-ahead log, if it is present. */
33535
+SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
33536
+
33537
+/* Return the size of the database as it existed at the beginning
33538
+** of the snapshot */
33539
+SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno);
33540
+
33541
+/* Obtain or release the WRITER lock. */
33542
+SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
33543
+SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
33544
+
33545
+/* Undo any frames written (but not committed) to the log */
33546
+SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
33547
+
33548
+/* Return an integer that records the current (uncommitted) write
33549
+** position in the WAL */
33550
+SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
33551
+
33552
+/* Move the write position of the WAL back to iFrame. Called in
33553
+** response to a ROLLBACK TO command. */
33554
+SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
33555
+
33556
+/* Write a frame or frames to the log. */
33557
+SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
33558
+
33559
+/* Copy pages from the log to the database file */
33560
+SQLITE_PRIVATE int sqlite3WalCheckpoint(
33561
+ Wal *pWal, /* Write-ahead log connection */
33562
+ int sync_flags, /* Flags to sync db file with (or 0) */
33563
+ int nBuf, /* Size of buffer nBuf */
33564
+ u8 *zBuf /* Temporary buffer to use */
33565
+);
33566
+
33567
+/* Return the value to pass to a sqlite3_wal_hook callback, the
33568
+** number of frames in the WAL at the point of the last commit since
33569
+** sqlite3WalCallback() was called. If no commits have occurred since
33570
+** the last call, then return 0.
33571
+*/
33572
+SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
33573
+
33574
+/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
33575
+** by the pager layer on the database file.
33576
+*/
33577
+SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
33578
+
33579
+#endif /* ifndef SQLITE_OMIT_WAL */
33580
+#endif /* _WAL_H_ */
33581
+
33582
+/************** End of wal.h *************************************************/
33583
+/************** Continuing where we left off in pager.c **********************/
3235833584
3235933585
/*
3236033586
******************** NOTES ON THE DESIGN OF THE PAGER ************************
3236133587
**
3236233588
** Within this comment block, a page is deemed to have been synced
@@ -32553,10 +33779,11 @@
3255333779
i64 iOffset; /* Starting offset in main journal */
3255433780
i64 iHdrOffset; /* See above */
3255533781
Bitvec *pInSavepoint; /* Set of pages in this savepoint */
3255633782
Pgno nOrig; /* Original number of pages in file */
3255733783
Pgno iSubRec; /* Index of first record in sub-journal */
33784
+ u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
3255833785
};
3255933786
3256033787
/*
3256133788
** A open page cache is an instance of the following structure.
3256233789
**
@@ -32730,10 +33957,13 @@
3273033957
void *pCodec; /* First argument to xCodec... methods */
3273133958
#endif
3273233959
char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
3273333960
PCache *pPCache; /* Pointer to page cache object */
3273433961
sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
33962
+#ifndef SQLITE_OMIT_WAL
33963
+ Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
33964
+#endif
3273533965
};
3273633966
3273733967
/*
3273833968
** The following global variables hold counters used for
3273933969
** testing purposes only. These variables do not exist in
@@ -33518,10 +34748,26 @@
3351834748
}
3351934749
}
3352034750
return rc;
3352134751
}
3352234752
34753
+/*
34754
+** Return true if this pager uses a write-ahead log instead of the usual
34755
+** rollback journal. Otherwise false.
34756
+*/
34757
+#ifndef SQLITE_OMIT_WAL
34758
+static int pagerUseWal(Pager *pPager){
34759
+ return (pPager->pWal!=0);
34760
+}
34761
+#else
34762
+# define pagerUseWal(x) 0
34763
+# define pagerRollbackWal(x) 0
34764
+# define pagerWalFrames(v,w,x,y,z) 0
34765
+# define pagerOpenWalIfPresent(z) SQLITE_OK
34766
+# define pagerBeginReadTransaction(z) SQLITE_OK
34767
+#endif
34768
+
3352334769
/*
3352434770
** Unlock the database file. This function is a no-op if the pager
3352534771
** is in exclusive mode.
3352634772
**
3352734773
** If the pager is currently in error state, discard the contents of
@@ -33530,11 +34776,11 @@
3353034776
** on the pager file (by this or any other process), it will be
3353134777
** treated as a hot-journal and rolled back.
3353234778
*/
3353334779
static void pager_unlock(Pager *pPager){
3353434780
if( !pPager->exclusiveMode ){
33535
- int rc; /* Return code */
34781
+ int rc = SQLITE_OK; /* Return code */
3353634782
3353734783
/* Always close the journal file when dropping the database lock.
3353834784
** Otherwise, another connection with journal_mode=delete might
3353934785
** delete the file out from under us.
3354034786
*/
@@ -33549,11 +34795,15 @@
3354934795
** until the change-counter check fails in PagerSharedLock().
3355034796
** Clearing the page size cache here is being conservative.
3355134797
*/
3355234798
pPager->dbSizeValid = 0;
3355334799
33554
- rc = osUnlock(pPager->fd, NO_LOCK);
34800
+ if( pagerUseWal(pPager) ){
34801
+ sqlite3WalEndReadTransaction(pPager->pWal);
34802
+ }else{
34803
+ rc = osUnlock(pPager->fd, NO_LOCK);
34804
+ }
3355534805
if( rc ){
3355634806
pPager->errCode = rc;
3355734807
}
3355834808
IOTRACE(("UNLOCK %p\n", pPager))
3355934809
@@ -33698,10 +34948,11 @@
3369834948
}
3369934949
releaseAllSavepoints(pPager);
3370034950
3370134951
assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
3370234952
if( isOpen(pPager->jfd) ){
34953
+ assert( !pagerUseWal(pPager) );
3370334954
3370434955
/* Finalize the journal file. */
3370534956
if( sqlite3IsMemJournal(pPager->jfd) ){
3370634957
assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
3370734958
sqlite3OsClose(pPager->jfd);
@@ -33711,24 +34962,26 @@
3371134962
}else{
3371234963
rc = sqlite3OsTruncate(pPager->jfd, 0);
3371334964
}
3371434965
pPager->journalOff = 0;
3371534966
pPager->journalStarted = 0;
33716
- }else if( pPager->exclusiveMode
33717
- || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
34967
+ }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
34968
+ || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
3371834969
){
3371934970
rc = zeroJournalHdr(pPager, hasMaster);
3372034971
pager_error(pPager, rc);
3372134972
pPager->journalOff = 0;
3372234973
pPager->journalStarted = 0;
3372334974
}else{
3372434975
/* This branch may be executed with Pager.journalMode==MEMORY if
3372534976
** a hot-journal was just rolled back. In this case the journal
3372634977
** file should be closed and deleted. If this connection writes to
33727
- ** the database file, it will do so using an in-memory journal. */
34978
+ ** the database file, it will do so using an in-memory journal.
34979
+ */
3372834980
assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
3372934981
|| pPager->journalMode==PAGER_JOURNALMODE_MEMORY
34982
+ || pPager->journalMode==PAGER_JOURNALMODE_WAL
3373034983
);
3373134984
sqlite3OsClose(pPager->jfd);
3373234985
if( !pPager->tempFile ){
3373334986
rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
3373434987
}
@@ -33741,11 +34994,22 @@
3374134994
sqlite3BitvecDestroy(pPager->pInJournal);
3374234995
pPager->pInJournal = 0;
3374334996
pPager->nRec = 0;
3374434997
sqlite3PcacheCleanAll(pPager->pPCache);
3374534998
33746
- if( !pPager->exclusiveMode ){
34999
+ if( pagerUseWal(pPager) ){
35000
+ rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
35001
+ assert( rc2==SQLITE_OK );
35002
+ pPager->state = PAGER_SHARED;
35003
+
35004
+ /* If the connection was in locking_mode=exclusive mode but is no longer,
35005
+ ** drop the EXCLUSIVE lock held on the database file.
35006
+ */
35007
+ if( !pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, 0) ){
35008
+ rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35009
+ }
35010
+ }else if( !pPager->exclusiveMode ){
3374735011
rc2 = osUnlock(pPager->fd, SHARED_LOCK);
3374835012
pPager->state = PAGER_SHARED;
3374935013
pPager->changeCountDone = 0;
3375035014
}else if( pPager->state==PAGER_SYNCED ){
3375135015
pPager->state = PAGER_EXCLUSIVE;
@@ -33852,10 +35116,11 @@
3385235116
assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
3385335117
assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
3385435118
3385535119
aData = pPager->pTmpSpace;
3385635120
assert( aData ); /* Temp storage must have already been allocated */
35121
+ assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
3385735122
3385835123
/* Read the page number and page data from the journal or sub-journal
3385935124
** file. Return an error code to the caller if an IO error occurs.
3386035125
*/
3386135126
jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -33921,11 +35186,15 @@
3392135186
**
3392235187
** 2008-04-14: When attempting to vacuum a corrupt database file, it
3392335188
** is possible to fail a statement on a database that does not yet exist.
3392435189
** Do not attempt to write if database file has never been opened.
3392535190
*/
33926
- pPg = pager_lookup(pPager, pgno);
35191
+ if( pagerUseWal(pPager) ){
35192
+ pPg = 0;
35193
+ }else{
35194
+ pPg = pager_lookup(pPager, pgno);
35195
+ }
3392735196
assert( pPg || !MEMDB );
3392835197
PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
3392935198
PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
3393035199
(isMainJrnl?"main-journal":"sub-journal")
3393135200
));
@@ -33938,10 +35207,11 @@
3393835207
&& isOpen(pPager->fd)
3393935208
&& isSynced
3394035209
){
3394135210
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
3394235211
testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
35212
+ assert( !pagerUseWal(pPager) );
3394335213
rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
3394435214
if( pgno>pPager->dbFileSize ){
3394535215
pPager->dbFileSize = pgno;
3394635216
}
3394735217
if( pPager->pBackup ){
@@ -34002,10 +35272,11 @@
3400235272
** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
3400335273
** be written out into the database file before its journal file
3400435274
** segment is synced. If a crash occurs during or following this,
3400535275
** database corruption may ensue.
3400635276
*/
35277
+ assert( !pagerUseWal(pPager) );
3400735278
sqlite3PcacheMakeClean(pPg);
3400835279
}
3400935280
#ifdef SQLITE_CHECK_PAGES
3401035281
pPg->pageHash = pager_pagehash(pPg);
3401135282
#endif
@@ -34453,10 +35724,13 @@
3445335724
testcase( rc!=SQLITE_OK );
3445435725
}
3445535726
if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
3445635727
rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
3445735728
}
35729
+ if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
35730
+ rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
35731
+ }
3445835732
if( rc==SQLITE_OK ){
3445935733
rc = pager_end_transaction(pPager, zMaster[0]!='\0');
3446035734
testcase( rc!=SQLITE_OK );
3446135735
}
3446235736
if( rc==SQLITE_OK && zMaster[0] && res ){
@@ -34473,10 +35747,269 @@
3447335747
*/
3447435748
setSectorSize(pPager);
3447535749
return rc;
3447635750
}
3447735751
35752
+
35753
+/*
35754
+** Read the content for page pPg out of the database file and into
35755
+** pPg->pData. A shared lock or greater must be held on the database
35756
+** file before this function is called.
35757
+**
35758
+** If page 1 is read, then the value of Pager.dbFileVers[] is set to
35759
+** the value read from the database file.
35760
+**
35761
+** If an IO error occurs, then the IO error is returned to the caller.
35762
+** Otherwise, SQLITE_OK is returned.
35763
+*/
35764
+static int readDbPage(PgHdr *pPg){
35765
+ Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
35766
+ Pgno pgno = pPg->pgno; /* Page number to read */
35767
+ int rc = SQLITE_OK; /* Return code */
35768
+ int isInWal = 0; /* True if page is in log file */
35769
+ int pgsz = pPager->pageSize; /* Number of bytes to read */
35770
+
35771
+ assert( pPager->state>=PAGER_SHARED && !MEMDB );
35772
+ assert( isOpen(pPager->fd) );
35773
+
35774
+ if( NEVER(!isOpen(pPager->fd)) ){
35775
+ assert( pPager->tempFile );
35776
+ memset(pPg->pData, 0, pPager->pageSize);
35777
+ return SQLITE_OK;
35778
+ }
35779
+
35780
+ if( pagerUseWal(pPager) ){
35781
+ /* Try to pull the page from the write-ahead log. */
35782
+ rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
35783
+ }
35784
+ if( rc==SQLITE_OK && !isInWal ){
35785
+ i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
35786
+ rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
35787
+ if( rc==SQLITE_IOERR_SHORT_READ ){
35788
+ rc = SQLITE_OK;
35789
+ }
35790
+ }
35791
+
35792
+ if( pgno==1 ){
35793
+ if( rc ){
35794
+ /* If the read is unsuccessful, set the dbFileVers[] to something
35795
+ ** that will never be a valid file version. dbFileVers[] is a copy
35796
+ ** of bytes 24..39 of the database. Bytes 28..31 should always be
35797
+ ** zero or the size of the database in page. Bytes 32..35 and 35..39
35798
+ ** should be page numbers which are never 0xffffffff. So filling
35799
+ ** pPager->dbFileVers[] with all 0xff bytes should suffice.
35800
+ **
35801
+ ** For an encrypted database, the situation is more complex: bytes
35802
+ ** 24..39 of the database are white noise. But the probability of
35803
+ ** white noising equaling 16 bytes of 0xff is vanishingly small so
35804
+ ** we should still be ok.
35805
+ */
35806
+ memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
35807
+ }else{
35808
+ u8 *dbFileVers = &((u8*)pPg->pData)[24];
35809
+ memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
35810
+ }
35811
+ }
35812
+ CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
35813
+
35814
+ PAGER_INCR(sqlite3_pager_readdb_count);
35815
+ PAGER_INCR(pPager->nRead);
35816
+ IOTRACE(("PGIN %p %d\n", pPager, pgno));
35817
+ PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
35818
+ PAGERID(pPager), pgno, pager_pagehash(pPg)));
35819
+
35820
+ return rc;
35821
+}
35822
+
35823
+#ifndef SQLITE_OMIT_WAL
35824
+/*
35825
+** This function is invoked once for each page that has already been
35826
+** written into the log file when a WAL transaction is rolled back.
35827
+** Parameter iPg is the page number of said page. The pCtx argument
35828
+** is actually a pointer to the Pager structure.
35829
+**
35830
+** If page iPg is present in the cache, and has no outstanding references,
35831
+** it is discarded. Otherwise, if there are one or more outstanding
35832
+** references, the page content is reloaded from the database. If the
35833
+** attempt to reload content from the database is required and fails,
35834
+** return an SQLite error code. Otherwise, SQLITE_OK.
35835
+*/
35836
+static int pagerUndoCallback(void *pCtx, Pgno iPg){
35837
+ int rc = SQLITE_OK;
35838
+ Pager *pPager = (Pager *)pCtx;
35839
+ PgHdr *pPg;
35840
+
35841
+ pPg = sqlite3PagerLookup(pPager, iPg);
35842
+ if( pPg ){
35843
+ if( sqlite3PcachePageRefcount(pPg)==1 ){
35844
+ sqlite3PcacheDrop(pPg);
35845
+ }else{
35846
+ rc = readDbPage(pPg);
35847
+ if( rc==SQLITE_OK ){
35848
+ pPager->xReiniter(pPg);
35849
+ }
35850
+ sqlite3PagerUnref(pPg);
35851
+ }
35852
+ }
35853
+
35854
+ /* Normally, if a transaction is rolled back, any backup processes are
35855
+ ** updated as data is copied out of the rollback journal and into the
35856
+ ** database. This is not generally possible with a WAL database, as
35857
+ ** rollback involves simply truncating the log file. Therefore, if one
35858
+ ** or more frames have already been written to the log (and therefore
35859
+ ** also copied into the backup databases) as part of this transaction,
35860
+ ** the backups must be restarted.
35861
+ */
35862
+ sqlite3BackupRestart(pPager->pBackup);
35863
+
35864
+ return rc;
35865
+}
35866
+
35867
+/*
35868
+** This function is called to rollback a transaction on a WAL database.
35869
+*/
35870
+static int pagerRollbackWal(Pager *pPager){
35871
+ int rc; /* Return Code */
35872
+ PgHdr *pList; /* List of dirty pages to revert */
35873
+
35874
+ /* For all pages in the cache that are currently dirty or have already
35875
+ ** been written (but not committed) to the log file, do one of the
35876
+ ** following:
35877
+ **
35878
+ ** + Discard the cached page (if refcount==0), or
35879
+ ** + Reload page content from the database (if refcount>0).
35880
+ */
35881
+ pPager->dbSize = pPager->dbOrigSize;
35882
+ rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
35883
+ pList = sqlite3PcacheDirtyList(pPager->pPCache);
35884
+ while( pList && rc==SQLITE_OK ){
35885
+ PgHdr *pNext = pList->pDirty;
35886
+ rc = pagerUndoCallback((void *)pPager, pList->pgno);
35887
+ pList = pNext;
35888
+ }
35889
+
35890
+ return rc;
35891
+}
35892
+
35893
+/*
35894
+** This function is a wrapper around sqlite3WalFrames(). As well as logging
35895
+** the contents of the list of pages headed by pList (connected by pDirty),
35896
+** this function notifies any active backup processes that the pages have
35897
+** changed.
35898
+*/
35899
+static int pagerWalFrames(
35900
+ Pager *pPager, /* Pager object */
35901
+ PgHdr *pList, /* List of frames to log */
35902
+ Pgno nTruncate, /* Database size after this commit */
35903
+ int isCommit, /* True if this is a commit */
35904
+ int sync_flags /* Flags to pass to OsSync() (or 0) */
35905
+){
35906
+ int rc; /* Return code */
35907
+
35908
+ assert( pPager->pWal );
35909
+ rc = sqlite3WalFrames(pPager->pWal,
35910
+ pPager->pageSize, pList, nTruncate, isCommit, sync_flags
35911
+ );
35912
+ if( rc==SQLITE_OK && pPager->pBackup ){
35913
+ PgHdr *p;
35914
+ for(p=pList; p; p=p->pDirty){
35915
+ sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
35916
+ }
35917
+ }
35918
+ return rc;
35919
+}
35920
+
35921
+/*
35922
+** Begin a read transaction on the WAL.
35923
+**
35924
+** This routine used to be called "pagerOpenSnapshot()" because it essentially
35925
+** makes a snapshot of the database at the current point in time and preserves
35926
+** that snapshot for use by the reader in spite of concurrently changes by
35927
+** other writers or checkpointers.
35928
+*/
35929
+static int pagerBeginReadTransaction(Pager *pPager){
35930
+ int rc; /* Return code */
35931
+ int changed = 0; /* True if cache must be reset */
35932
+
35933
+ assert( pagerUseWal(pPager) );
35934
+
35935
+ /* sqlite3WalEndReadTransaction() was not called for the previous
35936
+ ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
35937
+ ** are in locking_mode=NORMAL and EndRead() was previously called,
35938
+ ** the duplicate call is harmless.
35939
+ */
35940
+ sqlite3WalEndReadTransaction(pPager->pWal);
35941
+
35942
+ rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
35943
+ if( rc==SQLITE_OK ){
35944
+ int dummy;
35945
+ if( changed ){
35946
+ pager_reset(pPager);
35947
+ assert( pPager->errCode || pPager->dbSizeValid==0 );
35948
+ }
35949
+ rc = sqlite3PagerPagecount(pPager, &dummy);
35950
+ }
35951
+ pPager->state = PAGER_SHARED;
35952
+
35953
+ return rc;
35954
+}
35955
+
35956
+/*
35957
+** Check if the *-wal file that corresponds to the database opened by pPager
35958
+** exists. Assuming no error occurs, set *pExists to 1 if the file exists,
35959
+** or 0 otherwise and return SQLITE_OK. If an IO or OOM error occurs, return
35960
+** an SQLite error code.
35961
+*/
35962
+static int pagerHasWAL(Pager *pPager, int *pExists){
35963
+ int rc; /* Return code */
35964
+ char *zWal; /* Name of the WAL file */
35965
+
35966
+ assert( !pPager->tempFile );
35967
+ zWal = sqlite3_mprintf("%s-wal", pPager->zFilename);
35968
+ if( !zWal ){
35969
+ rc = SQLITE_NOMEM;
35970
+ }else{
35971
+ rc = sqlite3OsAccess(pPager->pVfs, zWal, SQLITE_ACCESS_EXISTS, pExists);
35972
+ sqlite3_free(zWal);
35973
+ }
35974
+ return rc;
35975
+}
35976
+
35977
+/*
35978
+** Check if the *-wal file that corresponds to the database opened by pPager
35979
+** exists. If it does, open the pager in WAL mode. Otherwise, if no error
35980
+** occurs, make sure Pager.journalMode is not set to PAGER_JOURNALMODE_WAL.
35981
+** If an IO or OOM error occurs, return an SQLite error code.
35982
+**
35983
+** If the WAL file is opened, also open a snapshot (read transaction).
35984
+**
35985
+** The caller must hold a SHARED lock on the database file to call this
35986
+** function. Because an EXCLUSIVE lock on the db file is required to delete
35987
+** a WAL, this ensures there is no race condition between the xAccess()
35988
+** below and an xDelete() being executed by some other connection.
35989
+*/
35990
+static int pagerOpenWalIfPresent(Pager *pPager){
35991
+ int rc = SQLITE_OK;
35992
+ if( !pPager->tempFile ){
35993
+ int isWal; /* True if WAL file exists */
35994
+ rc = pagerHasWAL(pPager, &isWal);
35995
+ if( rc==SQLITE_OK ){
35996
+ if( isWal ){
35997
+ pager_reset(pPager);
35998
+ rc = sqlite3PagerOpenWal(pPager, 0);
35999
+ if( rc==SQLITE_OK ){
36000
+ rc = pagerBeginReadTransaction(pPager);
36001
+ }
36002
+ }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
36003
+ pPager->journalMode = PAGER_JOURNALMODE_DELETE;
36004
+ }
36005
+ }
36006
+ }
36007
+ return rc;
36008
+}
36009
+#endif
36010
+
3447836011
/*
3447936012
** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
3448036013
** the entire master journal file. The case pSavepoint==NULL occurs when
3448136014
** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
3448236015
** savepoint.
@@ -34529,26 +36062,31 @@
3452936062
3453036063
/* Set the database size back to the value it was before the savepoint
3453136064
** being reverted was opened.
3453236065
*/
3453336066
pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
36067
+
36068
+ if( !pSavepoint && pagerUseWal(pPager) ){
36069
+ return pagerRollbackWal(pPager);
36070
+ }
3453436071
3453536072
/* Use pPager->journalOff as the effective size of the main rollback
3453636073
** journal. The actual file might be larger than this in
3453736074
** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
3453836075
** past pPager->journalOff is off-limits to us.
3453936076
*/
3454036077
szJ = pPager->journalOff;
36078
+ assert( pagerUseWal(pPager)==0 || szJ==0 );
3454136079
3454236080
/* Begin by rolling back records from the main journal starting at
3454336081
** PagerSavepoint.iOffset and continuing to the next journal header.
3454436082
** There might be records in the main journal that have a page number
3454536083
** greater than the current database size (pPager->dbSize) but those
3454636084
** will be skipped automatically. Pages are added to pDone as they
3454736085
** are played back.
3454836086
*/
34549
- if( pSavepoint ){
36087
+ if( pSavepoint && !pagerUseWal(pPager) ){
3455036088
iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
3455136089
pPager->journalOff = pSavepoint->iOffset;
3455236090
while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
3455336091
rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
3455436092
}
@@ -34591,10 +36129,14 @@
3459136129
** will be skipped. Out-of-range pages are also skipped.
3459236130
*/
3459336131
if( pSavepoint ){
3459436132
u32 ii; /* Loop counter */
3459536133
i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
36134
+
36135
+ if( pagerUseWal(pPager) ){
36136
+ rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
36137
+ }
3459636138
for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
3459736139
assert( offset==ii*(4+pPager->pageSize) );
3459836140
rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
3459936141
}
3460036142
assert( rc!=SQLITE_DONE );
@@ -34602,10 +36144,11 @@
3460236144
3460336145
sqlite3BitvecDestroy(pDone);
3460436146
if( rc==SQLITE_OK ){
3460536147
pPager->journalOff = szJ;
3460636148
}
36149
+
3460736150
return rc;
3460836151
}
3460936152
3461036153
/*
3461136154
** Change the maximum number of in-memory pages that are allowed.
@@ -34818,12 +36361,14 @@
3481836361
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
3481936362
int nPage;
3482036363
if( mxPage>0 ){
3482136364
pPager->mxPgno = mxPage;
3482236365
}
34823
- sqlite3PagerPagecount(pPager, &nPage);
34824
- assert( pPager->mxPgno>=nPage );
36366
+ if( pPager->state!=PAGER_UNLOCK ){
36367
+ sqlite3PagerPagecount(pPager, &nPage);
36368
+ assert( pPager->mxPgno>=nPage );
36369
+ }
3482536370
return pPager->mxPgno;
3482636371
}
3482736372
3482836373
/*
3482936374
** The following set of routines are used to disable the simulated
@@ -34865,10 +36410,26 @@
3486536410
*/
3486636411
SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
3486736412
int rc = SQLITE_OK;
3486836413
memset(pDest, 0, N);
3486936414
assert( isOpen(pPager->fd) || pPager->tempFile );
36415
+
36416
+ /* This routine is only called by btree immediately after creating
36417
+ ** the Pager object. There has not been an opportunity to transition
36418
+ ** to WAL mode yet.
36419
+ */
36420
+ assert( !pagerUseWal(pPager) );
36421
+#if 0
36422
+ if( pagerUseWal(pPager) ){
36423
+ int isInWal = 0;
36424
+ rc = sqlite3WalRead(pPager->pWal, 1, &isInWal, N, pDest);
36425
+ if( rc!=SQLITE_OK || isInWal ){
36426
+ return rc;
36427
+ }
36428
+ }
36429
+#endif
36430
+
3487036431
if( isOpen(pPager->fd) ){
3487136432
IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
3487236433
rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
3487336434
if( rc==SQLITE_IOERR_SHORT_READ ){
3487436435
rc = SQLITE_OK;
@@ -34891,28 +36452,36 @@
3489136452
**
3489236453
** Otherwise, if everything is successful, then SQLITE_OK is returned
3489336454
** and *pnPage is set to the number of pages in the database.
3489436455
*/
3489536456
SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
34896
- Pgno nPage; /* Value to return via *pnPage */
36457
+ Pgno nPage = 0; /* Value to return via *pnPage */
3489736458
3489836459
/* Determine the number of pages in the file. Store this in nPage. */
3489936460
if( pPager->dbSizeValid ){
3490036461
nPage = pPager->dbSize;
3490136462
}else{
3490236463
int rc; /* Error returned by OsFileSize() */
3490336464
i64 n = 0; /* File size in bytes returned by OsFileSize() */
3490436465
34905
- assert( isOpen(pPager->fd) || pPager->tempFile );
34906
- if( isOpen(pPager->fd) && (0 != (rc = sqlite3OsFileSize(pPager->fd, &n))) ){
34907
- pager_error(pPager, rc);
34908
- return rc;
34909
- }
34910
- if( n>0 && n<pPager->pageSize ){
34911
- nPage = 1;
34912
- }else{
34913
- nPage = (Pgno)(n / pPager->pageSize);
36466
+ if( pagerUseWal(pPager) && pPager->state!=PAGER_UNLOCK ){
36467
+ sqlite3WalDbsize(pPager->pWal, &nPage);
36468
+ }
36469
+
36470
+ if( nPage==0 ){
36471
+ assert( isOpen(pPager->fd) || pPager->tempFile );
36472
+ if( isOpen(pPager->fd) ){
36473
+ if( SQLITE_OK!=(rc = sqlite3OsFileSize(pPager->fd, &n)) ){
36474
+ pager_error(pPager, rc);
36475
+ return rc;
36476
+ }
36477
+ }
36478
+ if( n>0 && n<pPager->pageSize ){
36479
+ nPage = 1;
36480
+ }else{
36481
+ nPage = (Pgno)(n / pPager->pageSize);
36482
+ }
3491436483
}
3491536484
if( pPager->state!=PAGER_UNLOCK ){
3491636485
pPager->dbSize = nPage;
3491736486
pPager->dbFileSize = nPage;
3491836487
pPager->dbSizeValid = 1;
@@ -35030,10 +36599,11 @@
3503036599
assert( pPager->dbSize>=nPage );
3503136600
assert( pPager->state>=PAGER_RESERVED );
3503236601
pPager->dbSize = nPage;
3503336602
assertTruncateConstraint(pPager);
3503436603
}
36604
+
3503536605
3503636606
/*
3503736607
** This function is called before attempting a hot-journal rollback. It
3503836608
** syncs the journal file to disk, then sets pPager->journalHdr to the
3503936609
** size of the journal file so that the pager_playback() routine knows
@@ -35071,14 +36641,23 @@
3507136641
** is made to roll it back. If an error occurs during the rollback
3507236642
** a hot journal may be left in the filesystem but no error is returned
3507336643
** to the caller.
3507436644
*/
3507536645
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
36646
+ u8 *pTmp = (u8 *)pPager->pTmpSpace;
36647
+
3507636648
disable_simulated_io_errors();
3507736649
sqlite3BeginBenignMalloc();
3507836650
pPager->errCode = 0;
3507936651
pPager->exclusiveMode = 0;
36652
+#ifndef SQLITE_OMIT_WAL
36653
+ sqlite3WalClose(pPager->pWal,
36654
+ (pPager->noSync ? 0 : pPager->sync_flags),
36655
+ pPager->pageSize, pTmp
36656
+ );
36657
+ pPager->pWal = 0;
36658
+#endif
3508036659
pager_reset(pPager);
3508136660
if( MEMDB ){
3508236661
pager_unlock(pPager);
3508336662
}else{
3508436663
/* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
@@ -35095,11 +36674,11 @@
3509536674
sqlite3EndBenignMalloc();
3509636675
enable_simulated_io_errors();
3509736676
PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
3509836677
IOTRACE(("CLOSE %p\n", pPager))
3509936678
sqlite3OsClose(pPager->fd);
35100
- sqlite3PageFree(pPager->pTmpSpace);
36679
+ sqlite3PageFree(pTmp);
3510136680
sqlite3PcacheClose(pPager->pPCache);
3510236681
3510336682
#ifdef SQLITE_HAS_CODEC
3510436683
if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
3510536684
#endif
@@ -35196,14 +36775,14 @@
3519636775
** as a temporary buffer to inspect the first couple of bytes of
3519736776
** the potential journal header.
3519836777
*/
3519936778
i64 iNextHdrOffset;
3520036779
u8 aMagic[8];
35201
- u8 zHeader[sizeof(aJournalMagic)+4];
36780
+ u8 zHeader[sizeof(aJournalMagic)+4];
3520236781
35203
- memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
35204
- put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
36782
+ memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
36783
+ put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
3520536784
3520636785
iNextHdrOffset = journalHdrOffset(pPager);
3520736786
rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
3520836787
if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
3520936788
static const u8 zerobyte = 0;
@@ -35231,11 +36810,11 @@
3523136810
if( rc!=SQLITE_OK ) return rc;
3523236811
}
3523336812
IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
3523436813
rc = sqlite3OsWrite(
3523536814
pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
35236
- );
36815
+ );
3523736816
if( rc!=SQLITE_OK ) return rc;
3523836817
}
3523936818
if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
3524036819
PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
3524136820
IOTRACE(("JSYNC %p\n", pPager))
@@ -35311,10 +36890,11 @@
3531136890
** is unchanged and we can rollback without having to playback the
3531236891
** journal into the original database file. Once we transition to
3531336892
** EXCLUSIVE, it means the database file has been changed and any rollback
3531436893
** will require a journal playback.
3531536894
*/
36895
+ assert( !pagerUseWal(pList->pPager) );
3531636896
assert( pPager->state>=PAGER_RESERVED );
3531736897
rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3531836898
3531936899
/* If the file is a temp-file has not yet been opened, open it now. It
3532036900
** is not possible for rc to be other than SQLITE_OK if this branch
@@ -35322,10 +36902,18 @@
3532236902
*/
3532336903
if( !isOpen(pPager->fd) ){
3532436904
assert( pPager->tempFile && rc==SQLITE_OK );
3532536905
rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
3532636906
}
36907
+
36908
+ /* Before the first write, give the VFS a hint of what the final
36909
+ ** file size will be.
36910
+ */
36911
+ if( pPager->dbSize > (pPager->dbOrigSize+1) && isOpen(pPager->fd) ){
36912
+ sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
36913
+ sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
36914
+ }
3532736915
3532836916
while( rc==SQLITE_OK && pList ){
3532936917
Pgno pgno = pList->pgno;
3533036918
3533136919
/* If there are dirty pages in the page cache with page numbers greater
@@ -35374,10 +36962,30 @@
3537436962
pList = pList->pDirty;
3537536963
}
3537636964
3537736965
return rc;
3537836966
}
36967
+
36968
+/*
36969
+** Ensure that the sub-journal file is open. If it is already open, this
36970
+** function is a no-op.
36971
+**
36972
+** SQLITE_OK is returned if everything goes according to plan. An
36973
+** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
36974
+** fails.
36975
+*/
36976
+static int openSubJournal(Pager *pPager){
36977
+ int rc = SQLITE_OK;
36978
+ if( !isOpen(pPager->sjfd) ){
36979
+ if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
36980
+ sqlite3MemJournalOpen(pPager->sjfd);
36981
+ }else{
36982
+ rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
36983
+ }
36984
+ }
36985
+ return rc;
36986
+}
3537936987
3538036988
/*
3538136989
** Append a record of the current state of page pPg to the sub-journal.
3538236990
** It is the callers responsibility to use subjRequiresPage() to check
3538336991
** that it is really required before calling this function.
@@ -35391,32 +36999,44 @@
3539136999
** bitvec.
3539237000
*/
3539337001
static int subjournalPage(PgHdr *pPg){
3539437002
int rc = SQLITE_OK;
3539537003
Pager *pPager = pPg->pPager;
35396
- if( isOpen(pPager->sjfd) ){
35397
- void *pData = pPg->pData;
35398
- i64 offset = pPager->nSubRec*(4+pPager->pageSize);
35399
- char *pData2;
35400
-
35401
- CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
35402
- PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
35403
-
35404
- assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
35405
- rc = write32bits(pPager->sjfd, offset, pPg->pgno);
37004
+ if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
37005
+
37006
+ /* Open the sub-journal, if it has not already been opened */
37007
+ assert( pPager->useJournal );
37008
+ assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
37009
+ assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
37010
+ assert( pagerUseWal(pPager)
37011
+ || pageInJournal(pPg)
37012
+ || pPg->pgno>pPager->dbOrigSize
37013
+ );
37014
+ rc = openSubJournal(pPager);
37015
+
37016
+ /* If the sub-journal was opened successfully (or was already open),
37017
+ ** write the journal record into the file. */
3540637018
if( rc==SQLITE_OK ){
35407
- rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
37019
+ void *pData = pPg->pData;
37020
+ i64 offset = pPager->nSubRec*(4+pPager->pageSize);
37021
+ char *pData2;
37022
+
37023
+ CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
37024
+ PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
37025
+ rc = write32bits(pPager->sjfd, offset, pPg->pgno);
37026
+ if( rc==SQLITE_OK ){
37027
+ rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
37028
+ }
3540837029
}
3540937030
}
3541037031
if( rc==SQLITE_OK ){
3541137032
pPager->nSubRec++;
3541237033
assert( pPager->nSavepoint>0 );
3541337034
rc = addToSavepointBitvecs(pPager, pPg->pgno);
3541437035
}
3541537036
return rc;
3541637037
}
35417
-
3541837038
3541937039
/*
3542037040
** This function is called by the pcache layer when it has reached some
3542137041
** soft memory limit. The first argument is a pointer to a Pager object
3542237042
** (cast as a void*). The pager is always 'purgeable' (not an in-memory
@@ -35440,78 +37060,88 @@
3544037060
int rc = SQLITE_OK;
3544137061
3544237062
assert( pPg->pPager==pPager );
3544337063
assert( pPg->flags&PGHDR_DIRTY );
3544437064
35445
- /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
35446
- ** is journalling a set of two or more database pages that are stored
35447
- ** on the same disk sector. Syncing the journal is not allowed while
35448
- ** this is happening as it is important that all members of such a
35449
- ** set of pages are synced to disk together. So, if the page this function
35450
- ** is trying to make clean will require a journal sync and the doNotSync
35451
- ** flag is set, return without doing anything. The pcache layer will
35452
- ** just have to go ahead and allocate a new page buffer instead of
35453
- ** reusing pPg.
35454
- **
35455
- ** Similarly, if the pager has already entered the error state, do not
35456
- ** try to write the contents of pPg to disk.
35457
- */
35458
- if( NEVER(pPager->errCode)
35459
- || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
35460
- ){
35461
- return SQLITE_OK;
35462
- }
35463
-
35464
- /* Sync the journal file if required. */
35465
- if( pPg->flags&PGHDR_NEED_SYNC ){
35466
- rc = syncJournal(pPager);
35467
- if( rc==SQLITE_OK && pPager->fullSync &&
35468
- !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
35469
- !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
35470
- ){
35471
- pPager->nRec = 0;
35472
- rc = writeJournalHdr(pPager);
35473
- }
35474
- }
35475
-
35476
- /* If the page number of this page is larger than the current size of
35477
- ** the database image, it may need to be written to the sub-journal.
35478
- ** This is because the call to pager_write_pagelist() below will not
35479
- ** actually write data to the file in this case.
35480
- **
35481
- ** Consider the following sequence of events:
35482
- **
35483
- ** BEGIN;
35484
- ** <journal page X>
35485
- ** <modify page X>
35486
- ** SAVEPOINT sp;
35487
- ** <shrink database file to Y pages>
35488
- ** pagerStress(page X)
35489
- ** ROLLBACK TO sp;
35490
- **
35491
- ** If (X>Y), then when pagerStress is called page X will not be written
35492
- ** out to the database file, but will be dropped from the cache. Then,
35493
- ** following the "ROLLBACK TO sp" statement, reading page X will read
35494
- ** data from the database file. This will be the copy of page X as it
35495
- ** was when the transaction started, not as it was when "SAVEPOINT sp"
35496
- ** was executed.
35497
- **
35498
- ** The solution is to write the current data for page X into the
35499
- ** sub-journal file now (if it is not already there), so that it will
35500
- ** be restored to its current value when the "ROLLBACK TO sp" is
35501
- ** executed.
35502
- */
35503
- if( NEVER(
35504
- rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
35505
- ) ){
35506
- rc = subjournalPage(pPg);
35507
- }
35508
-
35509
- /* Write the contents of the page out to the database file. */
35510
- if( rc==SQLITE_OK ){
35511
- pPg->pDirty = 0;
35512
- rc = pager_write_pagelist(pPg);
37065
+ pPg->pDirty = 0;
37066
+ if( pagerUseWal(pPager) ){
37067
+ /* Write a single frame for this page to the log. */
37068
+ if( subjRequiresPage(pPg) ){
37069
+ rc = subjournalPage(pPg);
37070
+ }
37071
+ if( rc==SQLITE_OK ){
37072
+ rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
37073
+ }
37074
+ }else{
37075
+ /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
37076
+ ** is journalling a set of two or more database pages that are stored
37077
+ ** on the same disk sector. Syncing the journal is not allowed while
37078
+ ** this is happening as it is important that all members of such a
37079
+ ** set of pages are synced to disk together. So, if the page this function
37080
+ ** is trying to make clean will require a journal sync and the doNotSync
37081
+ ** flag is set, return without doing anything. The pcache layer will
37082
+ ** just have to go ahead and allocate a new page buffer instead of
37083
+ ** reusing pPg.
37084
+ **
37085
+ ** Similarly, if the pager has already entered the error state, do not
37086
+ ** try to write the contents of pPg to disk.
37087
+ */
37088
+ if( NEVER(pPager->errCode)
37089
+ || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37090
+ ){
37091
+ return SQLITE_OK;
37092
+ }
37093
+
37094
+ /* Sync the journal file if required. */
37095
+ if( pPg->flags&PGHDR_NEED_SYNC ){
37096
+ rc = syncJournal(pPager);
37097
+ if( rc==SQLITE_OK && pPager->fullSync &&
37098
+ !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37099
+ !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37100
+ ){
37101
+ pPager->nRec = 0;
37102
+ rc = writeJournalHdr(pPager);
37103
+ }
37104
+ }
37105
+
37106
+ /* If the page number of this page is larger than the current size of
37107
+ ** the database image, it may need to be written to the sub-journal.
37108
+ ** This is because the call to pager_write_pagelist() below will not
37109
+ ** actually write data to the file in this case.
37110
+ **
37111
+ ** Consider the following sequence of events:
37112
+ **
37113
+ ** BEGIN;
37114
+ ** <journal page X>
37115
+ ** <modify page X>
37116
+ ** SAVEPOINT sp;
37117
+ ** <shrink database file to Y pages>
37118
+ ** pagerStress(page X)
37119
+ ** ROLLBACK TO sp;
37120
+ **
37121
+ ** If (X>Y), then when pagerStress is called page X will not be written
37122
+ ** out to the database file, but will be dropped from the cache. Then,
37123
+ ** following the "ROLLBACK TO sp" statement, reading page X will read
37124
+ ** data from the database file. This will be the copy of page X as it
37125
+ ** was when the transaction started, not as it was when "SAVEPOINT sp"
37126
+ ** was executed.
37127
+ **
37128
+ ** The solution is to write the current data for page X into the
37129
+ ** sub-journal file now (if it is not already there), so that it will
37130
+ ** be restored to its current value when the "ROLLBACK TO sp" is
37131
+ ** executed.
37132
+ */
37133
+ if( NEVER(
37134
+ rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
37135
+ ) ){
37136
+ rc = subjournalPage(pPg);
37137
+ }
37138
+
37139
+ /* Write the contents of the page out to the database file. */
37140
+ if( rc==SQLITE_OK ){
37141
+ rc = pager_write_pagelist(pPg);
37142
+ }
3551337143
}
3551437144
3551537145
/* Mark the page as clean. */
3551637146
if( rc==SQLITE_OK ){
3551737147
PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
@@ -35915,71 +37545,10 @@
3591537545
}
3591637546
3591737547
return rc;
3591837548
}
3591937549
35920
-/*
35921
-** Read the content for page pPg out of the database file and into
35922
-** pPg->pData. A shared lock or greater must be held on the database
35923
-** file before this function is called.
35924
-**
35925
-** If page 1 is read, then the value of Pager.dbFileVers[] is set to
35926
-** the value read from the database file.
35927
-**
35928
-** If an IO error occurs, then the IO error is returned to the caller.
35929
-** Otherwise, SQLITE_OK is returned.
35930
-*/
35931
-static int readDbPage(PgHdr *pPg){
35932
- Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
35933
- Pgno pgno = pPg->pgno; /* Page number to read */
35934
- int rc; /* Return code */
35935
- i64 iOffset; /* Byte offset of file to read from */
35936
-
35937
- assert( pPager->state>=PAGER_SHARED && !MEMDB );
35938
- assert( isOpen(pPager->fd) );
35939
-
35940
- if( NEVER(!isOpen(pPager->fd)) ){
35941
- assert( pPager->tempFile );
35942
- memset(pPg->pData, 0, pPager->pageSize);
35943
- return SQLITE_OK;
35944
- }
35945
- iOffset = (pgno-1)*(i64)pPager->pageSize;
35946
- rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
35947
- if( rc==SQLITE_IOERR_SHORT_READ ){
35948
- rc = SQLITE_OK;
35949
- }
35950
- if( pgno==1 ){
35951
- if( rc ){
35952
- /* If the read is unsuccessful, set the dbFileVers[] to something
35953
- ** that will never be a valid file version. dbFileVers[] is a copy
35954
- ** of bytes 24..39 of the database. Bytes 28..31 should always be
35955
- ** zero. Bytes 32..35 and 35..39 should be page numbers which are
35956
- ** never 0xffffffff. So filling pPager->dbFileVers[] with all 0xff
35957
- ** bytes should suffice.
35958
- **
35959
- ** For an encrypted database, the situation is more complex: bytes
35960
- ** 24..39 of the database are white noise. But the probability of
35961
- ** white noising equaling 16 bytes of 0xff is vanishingly small so
35962
- ** we should still be ok.
35963
- */
35964
- memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
35965
- }else{
35966
- u8 *dbFileVers = &((u8*)pPg->pData)[24];
35967
- memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
35968
- }
35969
- }
35970
- CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
35971
-
35972
- PAGER_INCR(sqlite3_pager_readdb_count);
35973
- PAGER_INCR(pPager->nRead);
35974
- IOTRACE(("PGIN %p %d\n", pPager, pgno));
35975
- PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
35976
- PAGERID(pPager), pgno, pager_pagehash(pPg)));
35977
-
35978
- return rc;
35979
-}
35980
-
3598137550
/*
3598237551
** This function is called to obtain a shared lock on the database file.
3598337552
** It is illegal to call sqlite3PagerAcquire() until after this function
3598437553
** has been successfully called. If a shared-lock is already held when
3598537554
** this function is called, it is a no-op.
@@ -36029,11 +37598,13 @@
3602937598
}
3603037599
pPager->errCode = SQLITE_OK;
3603137600
pager_reset(pPager);
3603237601
}
3603337602
36034
- if( pPager->state==PAGER_UNLOCK || isErrorReset ){
37603
+ if( pagerUseWal(pPager) ){
37604
+ rc = pagerBeginReadTransaction(pPager);
37605
+ }else if( pPager->state==PAGER_UNLOCK || isErrorReset ){
3603537606
sqlite3_vfs * const pVfs = pPager->pVfs;
3603637607
int isHotJournal = 0;
3603737608
assert( !MEMDB );
3603837609
assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
3603937610
if( pPager->noReadlock ){
@@ -36118,11 +37689,11 @@
3611837689
** rollback journal at this time. */
3611937690
pPager->journalStarted = 0;
3612037691
pPager->journalOff = 0;
3612137692
pPager->setMaster = 0;
3612237693
pPager->journalHdr = 0;
36123
-
37694
+
3612437695
/* Make sure the journal file has been synced to disk. */
3612537696
3612637697
/* Playback and delete the journal. Drop the database write
3612737698
** lock and reacquire the read lock. Purge the cache before
3612837699
** playing back the hot-journal so that we don't end up with
@@ -36185,10 +37756,15 @@
3618537756
if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
3618637757
pager_reset(pPager);
3618737758
}
3618837759
}
3618937760
assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
37761
+
37762
+ /* If there is a WAL file in the file-system, open this database in WAL
37763
+ ** mode. Otherwise, the following function call is a no-op.
37764
+ */
37765
+ rc = pagerOpenWalIfPresent(pPager);
3619037766
}
3619137767
3619237768
failed:
3619337769
if( rc!=SQLITE_OK ){
3619437770
/* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
@@ -36325,12 +37901,12 @@
3632537901
goto pager_acquire_err;
3632637902
}
3632737903
3632837904
if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
3632937905
if( pgno>pPager->mxPgno ){
36330
- rc = SQLITE_FULL;
36331
- goto pager_acquire_err;
37906
+ rc = SQLITE_FULL;
37907
+ goto pager_acquire_err;
3633237908
}
3633337909
if( noContent ){
3633437910
/* Failure to set the bits in the InJournal bit-vectors is benign.
3633537911
** It merely means that we might do some extra work to journal a
3633637912
** page that does not need to be journaled. Nevertheless, be sure
@@ -36410,31 +37986,10 @@
3641037986
sqlite3PcacheRelease(pPg);
3641137987
pagerUnlockIfUnused(pPager);
3641237988
}
3641337989
}
3641437990
36415
-/*
36416
-** If the main journal file has already been opened, ensure that the
36417
-** sub-journal file is open too. If the main journal is not open,
36418
-** this function is a no-op.
36419
-**
36420
-** SQLITE_OK is returned if everything goes according to plan.
36421
-** An SQLITE_IOERR_XXX error code is returned if a call to
36422
-** sqlite3OsOpen() fails.
36423
-*/
36424
-static int openSubJournal(Pager *pPager){
36425
- int rc = SQLITE_OK;
36426
- if( isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ){
36427
- if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
36428
- sqlite3MemJournalOpen(pPager->sjfd);
36429
- }else{
36430
- rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
36431
- }
36432
- }
36433
- return rc;
36434
-}
36435
-
3643637991
/*
3643737992
** This function is called at the start of every write transaction.
3643837993
** There must already be a RESERVED or EXCLUSIVE lock on the database
3643937994
** file when this routine is called.
3644037995
**
@@ -36513,13 +38068,10 @@
3651338068
pPager->journalOff = 0;
3651438069
pPager->setMaster = 0;
3651538070
pPager->journalHdr = 0;
3651638071
rc = writeJournalHdr(pPager);
3651738072
}
36518
- if( rc==SQLITE_OK && pPager->nSavepoint ){
36519
- rc = openSubJournal(pPager);
36520
- }
3652138073
3652238074
if( rc!=SQLITE_OK ){
3652338075
sqlite3BitvecDestroy(pPager->pInJournal);
3652438076
pPager->pInJournal = 0;
3652538077
}
@@ -36553,24 +38105,60 @@
3655338105
*/
3655438106
SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
3655538107
int rc = SQLITE_OK;
3655638108
assert( pPager->state!=PAGER_UNLOCK );
3655738109
pPager->subjInMemory = (u8)subjInMemory;
38110
+
3655838111
if( pPager->state==PAGER_SHARED ){
3655938112
assert( pPager->pInJournal==0 );
3656038113
assert( !MEMDB && !pPager->tempFile );
3656138114
36562
- /* Obtain a RESERVED lock on the database file. If the exFlag parameter
36563
- ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
36564
- ** busy-handler callback can be used when upgrading to the EXCLUSIVE
36565
- ** lock, but not when obtaining the RESERVED lock.
36566
- */
36567
- rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
36568
- if( rc==SQLITE_OK ){
36569
- pPager->state = PAGER_RESERVED;
36570
- if( exFlag ){
36571
- rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38115
+ if( pagerUseWal(pPager) ){
38116
+ /* If the pager is configured to use locking_mode=exclusive, and an
38117
+ ** exclusive lock on the database is not already held, obtain it now.
38118
+ */
38119
+ if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
38120
+ rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38121
+ pPager->state = PAGER_SHARED;
38122
+ if( rc!=SQLITE_OK ){
38123
+ return rc;
38124
+ }
38125
+ sqlite3WalExclusiveMode(pPager->pWal, 1);
38126
+ }
38127
+
38128
+ /* Grab the write lock on the log file. If successful, upgrade to
38129
+ ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
38130
+ ** The busy-handler is not invoked if another connection already
38131
+ ** holds the write-lock. If possible, the upper layer will call it.
38132
+ **
38133
+ ** WAL mode sets Pager.state to PAGER_RESERVED when it has an open
38134
+ ** transaction, but never to PAGER_EXCLUSIVE. This is because in
38135
+ ** PAGER_EXCLUSIVE state the code to roll back savepoint transactions
38136
+ ** may copy data from the sub-journal into the database file as well
38137
+ ** as into the page cache. Which would be incorrect in WAL mode.
38138
+ */
38139
+ rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
38140
+ if( rc==SQLITE_OK ){
38141
+ pPager->dbOrigSize = pPager->dbSize;
38142
+ pPager->state = PAGER_RESERVED;
38143
+ pPager->journalOff = 0;
38144
+ }
38145
+
38146
+ assert( rc!=SQLITE_OK || pPager->state==PAGER_RESERVED );
38147
+ assert( rc==SQLITE_OK || pPager->state==PAGER_SHARED );
38148
+ }else{
38149
+ /* Obtain a RESERVED lock on the database file. If the exFlag parameter
38150
+ ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
38151
+ ** busy-handler callback can be used when upgrading to the EXCLUSIVE
38152
+ ** lock, but not when obtaining the RESERVED lock.
38153
+ */
38154
+ rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
38155
+ if( rc==SQLITE_OK ){
38156
+ pPager->state = PAGER_RESERVED;
38157
+ if( exFlag ){
38158
+ rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38159
+ }
3657238160
}
3657338161
}
3657438162
3657538163
/* No need to open the journal file at this time. It will be
3657638164
** opened before it is written to. If we defer opening the journal,
@@ -36582,10 +38170,11 @@
3658238170
** time a (read or write) transaction was successfully concluded
3658338171
** by this connection. Instead of deleting the journal file it was
3658438172
** kept open and either was truncated to 0 bytes or its header was
3658538173
** overwritten with zeros.
3658638174
*/
38175
+ assert( pagerUseWal(pPager)==0 );
3658738176
assert( pPager->nRec==0 );
3658838177
assert( pPager->dbOrigSize==0 );
3658938178
assert( pPager->pInJournal==0 );
3659038179
rc = pager_open_journal(pPager);
3659138180
}
@@ -36636,10 +38225,11 @@
3663638225
/* Mark the page as dirty. If the page has already been written
3663738226
** to the journal then we can return right away.
3663838227
*/
3663938228
sqlite3PcacheMakeDirty(pPg);
3664038229
if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
38230
+ assert( !pagerUseWal(pPager) );
3664138231
pPager->dbModified = 1;
3664238232
}else{
3664338233
3664438234
/* If we get this far, it means that the page needs to be
3664538235
** written to the transaction journal or the ckeckpoint journal
@@ -36651,11 +38241,14 @@
3665138241
*/
3665238242
rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
3665338243
if( rc!=SQLITE_OK ){
3665438244
return rc;
3665538245
}
36656
- if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
38246
+ if( !isOpen(pPager->jfd)
38247
+ && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38248
+ && !pagerUseWal(pPager)
38249
+ ){
3665738250
assert( pPager->useJournal );
3665838251
rc = pager_open_journal(pPager);
3665938252
if( rc!=SQLITE_OK ) return rc;
3666038253
}
3666138254
pPager->dbModified = 1;
@@ -36663,10 +38256,11 @@
3666338256
/* The transaction journal now exists and we have a RESERVED or an
3666438257
** EXCLUSIVE lock on the main database file. Write the current page to
3666538258
** the transaction journal if it is not there already.
3666638259
*/
3666738260
if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
38261
+ assert( !pagerUseWal(pPager) );
3666838262
if( pPg->pgno<=pPager->dbOrigSize ){
3666938263
u32 cksum;
3667038264
char *pData2;
3667138265
3667238266
/* We should never write to the journal file the page that
@@ -36954,11 +38548,14 @@
3695438548
/* Increment the value just read and write it back to byte 24. */
3695538549
change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
3695638550
change_counter++;
3695738551
put32bits(((char*)pPgHdr->pData)+24, change_counter);
3695838552
36959
- /* Also store the SQLite version number in bytes 96..99 */
38553
+ /* Also store the SQLite version number in bytes 96..99 and in
38554
+ ** bytes 92..95 store the change counter for which the version number
38555
+ ** is valid. */
38556
+ put32bits(((char*)pPgHdr->pData)+92, change_counter);
3696038557
put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
3696138558
3696238559
/* If running in direct mode, write the contents of page 1 to the file. */
3696338560
if( DIRECT_MODE ){
3696438561
const void *zBuf = pPgHdr->pData;
@@ -37043,132 +38640,143 @@
3704338640
** function has already been called, it is mostly a no-op. However, any
3704438641
** backup in progress needs to be restarted.
3704538642
*/
3704638643
sqlite3BackupRestart(pPager->pBackup);
3704738644
}else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){
37048
-
37049
- /* The following block updates the change-counter. Exactly how it
37050
- ** does this depends on whether or not the atomic-update optimization
37051
- ** was enabled at compile time, and if this transaction meets the
37052
- ** runtime criteria to use the operation:
37053
- **
37054
- ** * The file-system supports the atomic-write property for
37055
- ** blocks of size page-size, and
37056
- ** * This commit is not part of a multi-file transaction, and
37057
- ** * Exactly one page has been modified and store in the journal file.
37058
- **
37059
- ** If the optimization was not enabled at compile time, then the
37060
- ** pager_incr_changecounter() function is called to update the change
37061
- ** counter in 'indirect-mode'. If the optimization is compiled in but
37062
- ** is not applicable to this transaction, call sqlite3JournalCreate()
37063
- ** to make sure the journal file has actually been created, then call
37064
- ** pager_incr_changecounter() to update the change-counter in indirect
37065
- ** mode.
37066
- **
37067
- ** Otherwise, if the optimization is both enabled and applicable,
37068
- ** then call pager_incr_changecounter() to update the change-counter
37069
- ** in 'direct' mode. In this case the journal file will never be
37070
- ** created for this transaction.
37071
- */
37072
-#ifdef SQLITE_ENABLE_ATOMIC_WRITE
37073
- PgHdr *pPg;
37074
- assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
37075
- if( !zMaster && isOpen(pPager->jfd)
37076
- && pPager->journalOff==jrnlBufferSize(pPager)
37077
- && pPager->dbSize>=pPager->dbFileSize
37078
- && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
37079
- ){
37080
- /* Update the db file change counter via the direct-write method. The
37081
- ** following call will modify the in-memory representation of page 1
37082
- ** to include the updated change counter and then write page 1
37083
- ** directly to the database file. Because of the atomic-write
37084
- ** property of the host file-system, this is safe.
37085
- */
37086
- rc = pager_incr_changecounter(pPager, 1);
37087
- }else{
37088
- rc = sqlite3JournalCreate(pPager->jfd);
37089
- if( rc==SQLITE_OK ){
37090
- rc = pager_incr_changecounter(pPager, 0);
37091
- }
37092
- }
37093
-#else
37094
- rc = pager_incr_changecounter(pPager, 0);
37095
-#endif
37096
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37097
-
37098
- /* If this transaction has made the database smaller, then all pages
37099
- ** being discarded by the truncation must be written to the journal
37100
- ** file. This can only happen in auto-vacuum mode.
37101
- **
37102
- ** Before reading the pages with page numbers larger than the
37103
- ** current value of Pager.dbSize, set dbSize back to the value
37104
- ** that it took at the start of the transaction. Otherwise, the
37105
- ** calls to sqlite3PagerGet() return zeroed pages instead of
37106
- ** reading data from the database file.
37107
- **
37108
- ** When journal_mode==OFF the dbOrigSize is always zero, so this
37109
- ** block never runs if journal_mode=OFF.
37110
- */
37111
-#ifndef SQLITE_OMIT_AUTOVACUUM
37112
- if( pPager->dbSize<pPager->dbOrigSize
37113
- && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
37114
- ){
37115
- Pgno i; /* Iterator variable */
37116
- const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
37117
- const Pgno dbSize = pPager->dbSize; /* Database image size */
37118
- pPager->dbSize = pPager->dbOrigSize;
37119
- for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
37120
- if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
37121
- PgHdr *pPage; /* Page to journal */
37122
- rc = sqlite3PagerGet(pPager, i, &pPage);
37123
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37124
- rc = sqlite3PagerWrite(pPage);
37125
- sqlite3PagerUnref(pPage);
37126
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37127
- }
37128
- }
37129
- pPager->dbSize = dbSize;
37130
- }
37131
-#endif
37132
-
37133
- /* Write the master journal name into the journal file. If a master
37134
- ** journal file name has already been written to the journal file,
37135
- ** or if zMaster is NULL (no master journal), then this call is a no-op.
37136
- */
37137
- rc = writeMasterJournal(pPager, zMaster);
37138
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37139
-
37140
- /* Sync the journal file. If the atomic-update optimization is being
37141
- ** used, this call will not create the journal file or perform any
37142
- ** real IO.
37143
- */
37144
- rc = syncJournal(pPager);
37145
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37146
-
37147
- /* Write all dirty pages to the database file. */
37148
- rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
37149
- if( rc!=SQLITE_OK ){
37150
- assert( rc!=SQLITE_IOERR_BLOCKED );
37151
- goto commit_phase_one_exit;
37152
- }
37153
- sqlite3PcacheCleanAll(pPager->pPCache);
37154
-
37155
- /* If the file on disk is not the same size as the database image,
37156
- ** then use pager_truncate to grow or shrink the file here.
37157
- */
37158
- if( pPager->dbSize!=pPager->dbFileSize ){
37159
- Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
37160
- assert( pPager->state>=PAGER_EXCLUSIVE );
37161
- rc = pager_truncate(pPager, nNew);
37162
- if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37163
- }
37164
-
37165
- /* Finally, sync the database file. */
37166
- if( !pPager->noSync && !noSync ){
37167
- rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
37168
- }
37169
- IOTRACE(("DBSYNC %p\n", pPager))
38645
+ if( pagerUseWal(pPager) ){
38646
+ PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
38647
+ if( pList ){
38648
+ rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
38649
+ (pPager->fullSync ? pPager->sync_flags : 0)
38650
+ );
38651
+ }
38652
+ if( rc==SQLITE_OK ){
38653
+ sqlite3PcacheCleanAll(pPager->pPCache);
38654
+ }
38655
+ }else{
38656
+ /* The following block updates the change-counter. Exactly how it
38657
+ ** does this depends on whether or not the atomic-update optimization
38658
+ ** was enabled at compile time, and if this transaction meets the
38659
+ ** runtime criteria to use the operation:
38660
+ **
38661
+ ** * The file-system supports the atomic-write property for
38662
+ ** blocks of size page-size, and
38663
+ ** * This commit is not part of a multi-file transaction, and
38664
+ ** * Exactly one page has been modified and store in the journal file.
38665
+ **
38666
+ ** If the optimization was not enabled at compile time, then the
38667
+ ** pager_incr_changecounter() function is called to update the change
38668
+ ** counter in 'indirect-mode'. If the optimization is compiled in but
38669
+ ** is not applicable to this transaction, call sqlite3JournalCreate()
38670
+ ** to make sure the journal file has actually been created, then call
38671
+ ** pager_incr_changecounter() to update the change-counter in indirect
38672
+ ** mode.
38673
+ **
38674
+ ** Otherwise, if the optimization is both enabled and applicable,
38675
+ ** then call pager_incr_changecounter() to update the change-counter
38676
+ ** in 'direct' mode. In this case the journal file will never be
38677
+ ** created for this transaction.
38678
+ */
38679
+ #ifdef SQLITE_ENABLE_ATOMIC_WRITE
38680
+ PgHdr *pPg;
38681
+ assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
38682
+ if( !zMaster && isOpen(pPager->jfd)
38683
+ && pPager->journalOff==jrnlBufferSize(pPager)
38684
+ && pPager->dbSize>=pPager->dbFileSize
38685
+ && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
38686
+ ){
38687
+ /* Update the db file change counter via the direct-write method. The
38688
+ ** following call will modify the in-memory representation of page 1
38689
+ ** to include the updated change counter and then write page 1
38690
+ ** directly to the database file. Because of the atomic-write
38691
+ ** property of the host file-system, this is safe.
38692
+ */
38693
+ rc = pager_incr_changecounter(pPager, 1);
38694
+ }else{
38695
+ rc = sqlite3JournalCreate(pPager->jfd);
38696
+ if( rc==SQLITE_OK ){
38697
+ rc = pager_incr_changecounter(pPager, 0);
38698
+ }
38699
+ }
38700
+ #else
38701
+ rc = pager_incr_changecounter(pPager, 0);
38702
+ #endif
38703
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38704
+
38705
+ /* If this transaction has made the database smaller, then all pages
38706
+ ** being discarded by the truncation must be written to the journal
38707
+ ** file. This can only happen in auto-vacuum mode.
38708
+ **
38709
+ ** Before reading the pages with page numbers larger than the
38710
+ ** current value of Pager.dbSize, set dbSize back to the value
38711
+ ** that it took at the start of the transaction. Otherwise, the
38712
+ ** calls to sqlite3PagerGet() return zeroed pages instead of
38713
+ ** reading data from the database file.
38714
+ **
38715
+ ** When journal_mode==OFF the dbOrigSize is always zero, so this
38716
+ ** block never runs if journal_mode=OFF.
38717
+ */
38718
+ #ifndef SQLITE_OMIT_AUTOVACUUM
38719
+ if( pPager->dbSize<pPager->dbOrigSize
38720
+ && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
38721
+ ){
38722
+ Pgno i; /* Iterator variable */
38723
+ const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
38724
+ const Pgno dbSize = pPager->dbSize; /* Database image size */
38725
+ pPager->dbSize = pPager->dbOrigSize;
38726
+ for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
38727
+ if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
38728
+ PgHdr *pPage; /* Page to journal */
38729
+ rc = sqlite3PagerGet(pPager, i, &pPage);
38730
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38731
+ rc = sqlite3PagerWrite(pPage);
38732
+ sqlite3PagerUnref(pPage);
38733
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38734
+ }
38735
+ }
38736
+ pPager->dbSize = dbSize;
38737
+ }
38738
+ #endif
38739
+
38740
+ /* Write the master journal name into the journal file. If a master
38741
+ ** journal file name has already been written to the journal file,
38742
+ ** or if zMaster is NULL (no master journal), then this call is a no-op.
38743
+ */
38744
+ rc = writeMasterJournal(pPager, zMaster);
38745
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38746
+
38747
+ /* Sync the journal file. If the atomic-update optimization is being
38748
+ ** used, this call will not create the journal file or perform any
38749
+ ** real IO.
38750
+ */
38751
+ rc = syncJournal(pPager);
38752
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38753
+
38754
+ /* Write all dirty pages to the database file. */
38755
+ rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
38756
+ if( rc!=SQLITE_OK ){
38757
+ assert( rc!=SQLITE_IOERR_BLOCKED );
38758
+ goto commit_phase_one_exit;
38759
+ }
38760
+ sqlite3PcacheCleanAll(pPager->pPCache);
38761
+
38762
+ /* If the file on disk is not the same size as the database image,
38763
+ ** then use pager_truncate to grow or shrink the file here.
38764
+ */
38765
+ if( pPager->dbSize!=pPager->dbFileSize ){
38766
+ Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
38767
+ assert( pPager->state>=PAGER_EXCLUSIVE );
38768
+ rc = pager_truncate(pPager, nNew);
38769
+ if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38770
+ }
38771
+
38772
+ /* Finally, sync the database file. */
38773
+ if( !pPager->noSync && !noSync ){
38774
+ rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
38775
+ }
38776
+ IOTRACE(("DBSYNC %p\n", pPager))
38777
+ }
3717038778
3717138779
pPager->state = PAGER_SYNCED;
3717238780
}
3717338781
3717438782
commit_phase_one_exit:
@@ -37273,11 +38881,18 @@
3727338881
** hot-journal rollback).
3727438882
*/
3727538883
SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
3727638884
int rc = SQLITE_OK; /* Return code */
3727738885
PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
37278
- if( !pPager->dbModified || !isOpen(pPager->jfd) ){
38886
+ if( pagerUseWal(pPager) ){
38887
+ int rc2;
38888
+
38889
+ rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
38890
+ rc2 = pager_end_transaction(pPager, pPager->setMaster);
38891
+ if( rc==SQLITE_OK ) rc = rc2;
38892
+ rc = pager_error(pPager, rc);
38893
+ }else if( !pPager->dbModified || !isOpen(pPager->jfd) ){
3727938894
rc = pager_end_transaction(pPager, pPager->setMaster);
3728038895
}else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3728138896
if( pPager->state>=PAGER_EXCLUSIVE ){
3728238897
pager_playback(pPager, 0);
3728338898
}
@@ -37399,11 +39014,10 @@
3739939014
if( !aNew ){
3740039015
return SQLITE_NOMEM;
3740139016
}
3740239017
memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
3740339018
pPager->aSavepoint = aNew;
37404
- pPager->nSavepoint = nSavepoint;
3740539019
3740639020
/* Populate the PagerSavepoint structures just allocated. */
3740739021
for(ii=nCurrent; ii<nSavepoint; ii++){
3740839022
aNew[ii].nOrig = nPage;
3740939023
if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
@@ -37414,14 +39028,16 @@
3741439028
aNew[ii].iSubRec = pPager->nSubRec;
3741539029
aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage);
3741639030
if( !aNew[ii].pInSavepoint ){
3741739031
return SQLITE_NOMEM;
3741839032
}
39033
+ if( pagerUseWal(pPager) ){
39034
+ sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
39035
+ }
39036
+ pPager->nSavepoint = ii+1;
3741939037
}
37420
-
37421
- /* Open the sub-journal, if it is not already opened. */
37422
- rc = openSubJournal(pPager);
39038
+ assert( pPager->nSavepoint==nSavepoint );
3742339039
assertTruncateConstraint(pPager);
3742439040
}
3742539041
3742639042
return rc;
3742739043
}
@@ -37491,11 +39107,11 @@
3749139107
/* Else this is a rollback operation, playback the specified savepoint.
3749239108
** If this is a temp-file, it is possible that the journal file has
3749339109
** not yet been opened. In this case there have been no changes to
3749439110
** the database file, so the playback operation can be skipped.
3749539111
*/
37496
- else if( isOpen(pPager->jfd) ){
39112
+ else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
3749739113
PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
3749839114
rc = pagerPlaybackSavepoint(pPager, pSavepoint);
3749939115
assert(rc!=SQLITE_DONE);
3750039116
}
3750139117
@@ -37760,60 +39376,106 @@
3776039376
}
3776139377
return (int)pPager->exclusiveMode;
3776239378
}
3776339379
3776439380
/*
37765
-** Get/set the journal-mode for this pager. Parameter eMode must be one of:
39381
+** Set the journal-mode for this pager. Parameter eMode must be one of:
3776639382
**
37767
-** PAGER_JOURNALMODE_QUERY
3776839383
** PAGER_JOURNALMODE_DELETE
3776939384
** PAGER_JOURNALMODE_TRUNCATE
3777039385
** PAGER_JOURNALMODE_PERSIST
3777139386
** PAGER_JOURNALMODE_OFF
3777239387
** PAGER_JOURNALMODE_MEMORY
39388
+** PAGER_JOURNALMODE_WAL
3777339389
**
37774
-** If the parameter is not _QUERY, then the journal_mode is set to the
37775
-** value specified if the change is allowed. The change is disallowed
37776
-** for the following reasons:
39390
+** The journalmode is set to the value specified if the change is allowed.
39391
+** The change may be disallowed for the following reasons:
3777739392
**
3777839393
** * An in-memory database can only have its journal_mode set to _OFF
3777939394
** or _MEMORY.
3778039395
**
37781
-** * The journal mode may not be changed while a transaction is active.
39396
+** * Temporary databases cannot have _WAL journalmode.
3778239397
**
3778339398
** The returned indicate the current (possibly updated) journal-mode.
3778439399
*/
37785
-SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *pPager, int eMode){
37786
- assert( eMode==PAGER_JOURNALMODE_QUERY
37787
- || eMode==PAGER_JOURNALMODE_DELETE
39400
+SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
39401
+ u8 eOld = pPager->journalMode; /* Prior journalmode */
39402
+
39403
+ /* The eMode parameter is always valid */
39404
+ assert( eMode==PAGER_JOURNALMODE_DELETE
3778839405
|| eMode==PAGER_JOURNALMODE_TRUNCATE
3778939406
|| eMode==PAGER_JOURNALMODE_PERSIST
3779039407
|| eMode==PAGER_JOURNALMODE_OFF
39408
+ || eMode==PAGER_JOURNALMODE_WAL
3779139409
|| eMode==PAGER_JOURNALMODE_MEMORY );
37792
- assert( PAGER_JOURNALMODE_QUERY<0 );
37793
- if( eMode>=0
37794
- && (!MEMDB || eMode==PAGER_JOURNALMODE_MEMORY
37795
- || eMode==PAGER_JOURNALMODE_OFF)
37796
- && !pPager->dbModified
37797
- && (!isOpen(pPager->jfd) || 0==pPager->journalOff)
37798
- ){
37799
- if( isOpen(pPager->jfd) ){
39410
+
39411
+ /* Do not allow the journalmode of a TEMP database to be changed to WAL
39412
+ */
39413
+ if( pPager->tempFile && eMode==PAGER_JOURNALMODE_WAL ){
39414
+ assert( eOld!=PAGER_JOURNALMODE_WAL );
39415
+ eMode = eOld;
39416
+ }
39417
+
39418
+ /* Do allow the journalmode of an in-memory database to be set to
39419
+ ** anything other than MEMORY or OFF
39420
+ */
39421
+ if( MEMDB ){
39422
+ assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
39423
+ if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
39424
+ eMode = eOld;
39425
+ }
39426
+ }
39427
+
39428
+ if( eMode!=eOld ){
39429
+ /* When changing between rollback modes, close the journal file prior
39430
+ ** to the change. But when changing from a rollback mode to WAL, keep
39431
+ ** the journal open since there is a rollback-style transaction in play
39432
+ ** used to convert the version numbers in the btree header.
39433
+ */
39434
+ if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
3780039435
sqlite3OsClose(pPager->jfd);
3780139436
}
37802
- assert( (PAGER_JOURNALMODE_TRUNCATE & 1)==1 );
37803
- assert( (PAGER_JOURNALMODE_PERSIST & 1)==1 );
37804
- assert( (PAGER_JOURNALMODE_DELETE & 1)==0 );
37805
- assert( (PAGER_JOURNALMODE_MEMORY & 1)==0 );
37806
- assert( (PAGER_JOURNALMODE_OFF & 1)==0 );
37807
- if( (pPager->journalMode & 1)==1 && (eMode & 1)==0
37808
- && !pPager->exclusiveMode ){
39437
+
39438
+ /* Change the journal mode. */
39439
+ pPager->journalMode = (u8)eMode;
39440
+
39441
+ /* When transistioning from TRUNCATE or PERSIST to any other journal
39442
+ ** mode (and we are not in locking_mode=EXCLUSIVE) then delete the
39443
+ ** journal file.
39444
+ */
39445
+ assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
39446
+ assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
39447
+ assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
39448
+ assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
39449
+ assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
39450
+ assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
39451
+ if( (eOld & 5)==1 && (eMode & 5)!=1 && !pPager->exclusiveMode ){
3780939452
sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
3781039453
}
37811
- pPager->journalMode = (u8)eMode;
3781239454
}
39455
+
39456
+ /* Return the new journal mode */
39457
+ return (int)pPager->journalMode;
39458
+}
39459
+
39460
+/*
39461
+** Return the current journal mode.
39462
+*/
39463
+SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
3781339464
return (int)pPager->journalMode;
3781439465
}
39466
+
39467
+/*
39468
+** Return TRUE if the pager is in a state where it is OK to change the
39469
+** journalmode. Journalmode changes can only happen when the database
39470
+** is unmodified.
39471
+*/
39472
+SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
39473
+ if( pPager->dbModified ) return 0;
39474
+ if( isOpen(pPager->jfd) && pPager->journalOff>0 ) return 0;
39475
+ return 1;
39476
+}
3781539477
3781639478
/*
3781739479
** Get/set the size-limit used for persistent journal files.
3781839480
**
3781939481
** Setting the size limit to -1 means no limit is enforced.
@@ -37833,14 +39495,2621 @@
3783339495
** sqlite3BackupUpdate() only.
3783439496
*/
3783539497
SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
3783639498
return &pPager->pBackup;
3783739499
}
39500
+
39501
+#ifndef SQLITE_OMIT_WAL
39502
+/*
39503
+** This function is called when the user invokes "PRAGMA checkpoint".
39504
+*/
39505
+SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager){
39506
+ int rc = SQLITE_OK;
39507
+ if( pPager->pWal ){
39508
+ u8 *zBuf = (u8 *)pPager->pTmpSpace;
39509
+ rc = sqlite3WalCheckpoint(pPager->pWal,
39510
+ (pPager->noSync ? 0 : pPager->sync_flags),
39511
+ pPager->pageSize, zBuf
39512
+ );
39513
+ }
39514
+ return rc;
39515
+}
39516
+
39517
+SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
39518
+ return sqlite3WalCallback(pPager->pWal);
39519
+}
39520
+
39521
+/*
39522
+** Return true if the underlying VFS for the given pager supports the
39523
+** primitives necessary for write-ahead logging.
39524
+*/
39525
+SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
39526
+ const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
39527
+ return pMethods->iVersion>=2 && pMethods->xShmOpen!=0;
39528
+}
39529
+
39530
+/*
39531
+** Open a connection to the write-ahead log file for pager pPager. If
39532
+** the log connection is already open, this function is a no-op.
39533
+**
39534
+** The caller must be holding a SHARED lock on the database file to call
39535
+** this function.
39536
+*/
39537
+SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen){
39538
+ int rc = SQLITE_OK; /* Return code */
39539
+
39540
+ assert( pPager->state>=PAGER_SHARED );
39541
+ if( !pPager->pWal ){
39542
+ if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
39543
+
39544
+ /* Open the connection to the log file. If this operation fails,
39545
+ ** (e.g. due to malloc() failure), unlock the database file and
39546
+ ** return an error code.
39547
+ */
39548
+ rc = sqlite3WalOpen(pPager->pVfs, pPager->fd,
39549
+ pPager->zFilename, &pPager->pWal);
39550
+ if( rc==SQLITE_OK ){
39551
+ pPager->journalMode = PAGER_JOURNALMODE_WAL;
39552
+ }
39553
+ }else{
39554
+ *pisOpen = 1;
39555
+ }
39556
+
39557
+ return rc;
39558
+}
39559
+
39560
+/*
39561
+** This function is called to close the connection to the log file prior
39562
+** to switching from WAL to rollback mode.
39563
+**
39564
+** Before closing the log file, this function attempts to take an
39565
+** EXCLUSIVE lock on the database file. If this cannot be obtained, an
39566
+** error (SQLITE_BUSY) is returned and the log connection is not closed.
39567
+** If successful, the EXCLUSIVE lock is not released before returning.
39568
+*/
39569
+SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
39570
+ int rc = SQLITE_OK;
39571
+
39572
+ assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
39573
+
39574
+ /* If the log file is not already open, but does exist in the file-system,
39575
+ ** it may need to be checkpointed before the connection can switch to
39576
+ ** rollback mode. Open it now so this can happen.
39577
+ */
39578
+ if( !pPager->pWal ){
39579
+ int logexists = 0;
39580
+ rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED);
39581
+ if( rc==SQLITE_OK ){
39582
+ rc = pagerHasWAL(pPager, &logexists);
39583
+ }
39584
+ if( rc==SQLITE_OK && logexists ){
39585
+ rc = sqlite3WalOpen(pPager->pVfs, pPager->fd,
39586
+ pPager->zFilename, &pPager->pWal);
39587
+ }
39588
+ }
39589
+
39590
+ /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
39591
+ ** the database file, the log and log-summary files will be deleted.
39592
+ */
39593
+ if( rc==SQLITE_OK && pPager->pWal ){
39594
+ rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE);
39595
+ if( rc==SQLITE_OK ){
39596
+ rc = sqlite3WalClose(pPager->pWal,
39597
+ (pPager->noSync ? 0 : pPager->sync_flags),
39598
+ pPager->pageSize, (u8*)pPager->pTmpSpace
39599
+ );
39600
+ pPager->pWal = 0;
39601
+ }else{
39602
+ /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
39603
+ ** that we did get back to SHARED. */
39604
+ sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
39605
+ }
39606
+ }
39607
+ return rc;
39608
+}
39609
+#endif
3783839610
3783939611
#endif /* SQLITE_OMIT_DISKIO */
3784039612
3784139613
/************** End of pager.c ***********************************************/
39614
+/************** Begin file wal.c *********************************************/
39615
+/*
39616
+** 2010 February 1
39617
+**
39618
+** The author disclaims copyright to this source code. In place of
39619
+** a legal notice, here is a blessing:
39620
+**
39621
+** May you do good and not evil.
39622
+** May you find forgiveness for yourself and forgive others.
39623
+** May you share freely, never taking more than you give.
39624
+**
39625
+*************************************************************************
39626
+**
39627
+** This file contains the implementation of a write-ahead log (WAL) used in
39628
+** "journal_mode=WAL" mode.
39629
+**
39630
+** WRITE-AHEAD LOG (WAL) FILE FORMAT
39631
+**
39632
+** A WAL file consists of a header followed by zero or more "frames".
39633
+** Each frame records the revised content of a single page from the
39634
+** database file. All changes to the database are recorded by writing
39635
+** frames into the WAL. Transactions commit when a frame is written that
39636
+** contains a commit marker. A single WAL can and usually does record
39637
+** multiple transactions. Periodically, the content of the WAL is
39638
+** transferred back into the database file in an operation called a
39639
+** "checkpoint".
39640
+**
39641
+** A single WAL file can be used multiple times. In other words, the
39642
+** WAL can fill up with frames and then be checkpointed and then new
39643
+** frames can overwrite the old ones. A WAL always grows from beginning
39644
+** toward the end. Checksums and counters attached to each frame are
39645
+** used to determine which frames within the WAL are valid and which
39646
+** are leftovers from prior checkpoints.
39647
+**
39648
+** The WAL header is 24 bytes in size and consists of the following six
39649
+** big-endian 32-bit unsigned integer values:
39650
+**
39651
+** 0: Magic number. 0x377f0682 or 0x377f0683
39652
+** 4: File format version. Currently 3007000
39653
+** 8: Database page size. Example: 1024
39654
+** 12: Checkpoint sequence number
39655
+** 16: Salt-1, random integer incremented with each checkpoint
39656
+** 20: Salt-2, a different random integer changing with each ckpt
39657
+**
39658
+** Immediately following the wal-header are zero or more frames. Each
39659
+** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39660
+** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39661
+** integer values, as follows:
39662
+**
39663
+** 0: Page number.
39664
+** 4: For commit records, the size of the database image in pages
39665
+** after the commit. For all other records, zero.
39666
+** 8: Salt-1 (copied from the header)
39667
+** 12: Salt-2 (copied from the header)
39668
+** 16: Checksum-1.
39669
+** 20: Checksum-2.
39670
+**
39671
+** A frame is considered valid if and only if the following conditions are
39672
+** true:
39673
+**
39674
+** (1) The salt-1 and salt-2 values in the frame-header match
39675
+** salt values in the wal-header
39676
+**
39677
+** (2) The checksum values in the final 8 bytes of the frame-header
39678
+** exactly match the checksum computed consecutively on the
39679
+** WAL header and the first 8 bytes and the content of all frames
39680
+** up to and including the current frame.
39681
+**
39682
+** The checksum is computed using 32-bit big-endian integers if the
39683
+** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
39684
+** is computed using little-endian if the magic number is 0x377f0682.
39685
+** The checksum values are always stored in the frame header in a
39686
+** big-endian format regardless of which byte order is used to compute
39687
+** the checksum. The checksum is computed by interpreting the input as
39688
+** an even number of unsigned 32-bit integers: x[0] through x[N]. The
39689
+** algorithm used for the checksum is as follows:
39690
+**
39691
+** for i from 0 to n-1 step 2:
39692
+** s0 += x[i] + s1;
39693
+** s1 += x[i+1] + s0;
39694
+** endfor
39695
+**
39696
+** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39697
+** WAL is transferred into the database, then the database is VFS.xSync-ed.
39698
+** The VFS.xSync operations serve as write barriers - all writes launched
39699
+** before the xSync must complete before any write that launches after the
39700
+** xSync begins.
39701
+**
39702
+** After each checkpoint, the salt-1 value is incremented and the salt-2
39703
+** value is randomized. This prevents old and new frames in the WAL from
39704
+** being considered valid at the same time and being checkpointing together
39705
+** following a crash.
39706
+**
39707
+** READER ALGORITHM
39708
+**
39709
+** To read a page from the database (call it page number P), a reader
39710
+** first checks the WAL to see if it contains page P. If so, then the
39711
+** last valid instance of page P that is a followed by a commit frame
39712
+** or is a commit frame itself becomes the value read. If the WAL
39713
+** contains no copies of page P that are valid and which are a commit
39714
+** frame or are followed by a commit frame, then page P is read from
39715
+** the database file.
39716
+**
39717
+** To start a read transaction, the reader records the index of the last
39718
+** valid frame in the WAL. The reader uses this recorded "mxFrame" value
39719
+** for all subsequent read operations. New transactions can be appended
39720
+** to the WAL, but as long as the reader uses its original mxFrame value
39721
+** and ignores the newly appended content, it will see a consistent snapshot
39722
+** of the database from a single point in time. This technique allows
39723
+** multiple concurrent readers to view different versions of the database
39724
+** content simultaneously.
39725
+**
39726
+** The reader algorithm in the previous paragraphs works correctly, but
39727
+** because frames for page P can appear anywhere within the WAL, the
39728
+** reader has to scan the entire WAL looking for page P frames. If the
39729
+** WAL is large (multiple megabytes is typical) that scan can be slow,
39730
+** and read performance suffers. To overcome this problem, a separate
39731
+** data structure called the wal-index is maintained to expedite the
39732
+** search for frames of a particular page.
39733
+**
39734
+** WAL-INDEX FORMAT
39735
+**
39736
+** Conceptually, the wal-index is shared memory, though VFS implementations
39737
+** might choose to implement the wal-index using a mmapped file. Because
39738
+** the wal-index is shared memory, SQLite does not support journal_mode=WAL
39739
+** on a network filesystem. All users of the database must be able to
39740
+** share memory.
39741
+**
39742
+** The wal-index is transient. After a crash, the wal-index can (and should
39743
+** be) reconstructed from the original WAL file. In fact, the VFS is required
39744
+** to either truncate or zero the header of the wal-index when the last
39745
+** connection to it closes. Because the wal-index is transient, it can
39746
+** use an architecture-specific format; it does not have to be cross-platform.
39747
+** Hence, unlike the database and WAL file formats which store all values
39748
+** as big endian, the wal-index can store multi-byte values in the native
39749
+** byte order of the host computer.
39750
+**
39751
+** The purpose of the wal-index is to answer this question quickly: Given
39752
+** a page number P, return the index of the last frame for page P in the WAL,
39753
+** or return NULL if there are no frames for page P in the WAL.
39754
+**
39755
+** The wal-index consists of a header region, followed by an one or
39756
+** more index blocks.
39757
+**
39758
+** The wal-index header contains the total number of frames within the WAL
39759
+** in the the mxFrame field.
39760
+**
39761
+** Each index block except for the first contains information on
39762
+** HASHTABLE_NPAGE frames. The first index block contains information on
39763
+** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
39764
+** HASHTABLE_NPAGE are selected so that together the wal-index header and
39765
+** first index block are the same size as all other index blocks in the
39766
+** wal-index.
39767
+**
39768
+** Each index block contains two sections, a page-mapping that contains the
39769
+** database page number associated with each wal frame, and a hash-table
39770
+** that allows readers to query an index block for a specific page number.
39771
+** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
39772
+** for the first index block) 32-bit page numbers. The first entry in the
39773
+** first index-block contains the database page number corresponding to the
39774
+** first frame in the WAL file. The first entry in the second index block
39775
+** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
39776
+** the log, and so on.
39777
+**
39778
+** The last index block in a wal-index usually contains less than the full
39779
+** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
39780
+** depending on the contents of the WAL file. This does not change the
39781
+** allocated size of the page-mapping array - the page-mapping array merely
39782
+** contains unused entries.
39783
+**
39784
+** Even without using the hash table, the last frame for page P
39785
+** can be found by scanning the page-mapping sections of each index block
39786
+** starting with the last index block and moving toward the first, and
39787
+** within each index block, starting at the end and moving toward the
39788
+** beginning. The first entry that equals P corresponds to the frame
39789
+** holding the content for that page.
39790
+**
39791
+** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
39792
+** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
39793
+** hash table for each page number in the mapping section, so the hash
39794
+** table is never more than half full. The expected number of collisions
39795
+** prior to finding a match is 1. Each entry of the hash table is an
39796
+** 1-based index of an entry in the mapping section of the same
39797
+** index block. Let K be the 1-based index of the largest entry in
39798
+** the mapping section. (For index blocks other than the last, K will
39799
+** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
39800
+** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
39801
+** contain a value of 0.
39802
+**
39803
+** To look for page P in the hash table, first compute a hash iKey on
39804
+** P as follows:
39805
+**
39806
+** iKey = (P * 383) % HASHTABLE_NSLOT
39807
+**
39808
+** Then start scanning entries of the hash table, starting with iKey
39809
+** (wrapping around to the beginning when the end of the hash table is
39810
+** reached) until an unused hash slot is found. Let the first unused slot
39811
+** be at index iUnused. (iUnused might be less than iKey if there was
39812
+** wrap-around.) Because the hash table is never more than half full,
39813
+** the search is guaranteed to eventually hit an unused entry. Let
39814
+** iMax be the value between iKey and iUnused, closest to iUnused,
39815
+** where aHash[iMax]==P. If there is no iMax entry (if there exists
39816
+** no hash slot such that aHash[i]==p) then page P is not in the
39817
+** current index block. Otherwise the iMax-th mapping entry of the
39818
+** current index block corresponds to the last entry that references
39819
+** page P.
39820
+**
39821
+** A hash search begins with the last index block and moves toward the
39822
+** first index block, looking for entries corresponding to page P. On
39823
+** average, only two or three slots in each index block need to be
39824
+** examined in order to either find the last entry for page P, or to
39825
+** establish that no such entry exists in the block. Each index block
39826
+** holds over 4000 entries. So two or three index blocks are sufficient
39827
+** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
39828
+** comparisons (on average) suffice to either locate a frame in the
39829
+** WAL or to establish that the frame does not exist in the WAL. This
39830
+** is much faster than scanning the entire 10MB WAL.
39831
+**
39832
+** Note that entries are added in order of increasing K. Hence, one
39833
+** reader might be using some value K0 and a second reader that started
39834
+** at a later time (after additional transactions were added to the WAL
39835
+** and to the wal-index) might be using a different value K1, where K1>K0.
39836
+** Both readers can use the same hash table and mapping section to get
39837
+** the correct result. There may be entries in the hash table with
39838
+** K>K0 but to the first reader, those entries will appear to be unused
39839
+** slots in the hash table and so the first reader will get an answer as
39840
+** if no values greater than K0 had ever been inserted into the hash table
39841
+** in the first place - which is what reader one wants. Meanwhile, the
39842
+** second reader using K1 will see additional values that were inserted
39843
+** later, which is exactly what reader two wants.
39844
+**
39845
+** When a rollback occurs, the value of K is decreased. Hash table entries
39846
+** that correspond to frames greater than the new K value are removed
39847
+** from the hash table at this point.
39848
+*/
39849
+#ifndef SQLITE_OMIT_WAL
39850
+
39851
+
39852
+/*
39853
+** Trace output macros
39854
+*/
39855
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
39856
+SQLITE_PRIVATE int sqlite3WalTrace = 0;
39857
+# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39858
+#else
39859
+# define WALTRACE(X)
39860
+#endif
39861
+
39862
+
39863
+/*
39864
+** Indices of various locking bytes. WAL_NREADER is the number
39865
+** of available reader locks and should be at least 3.
39866
+*/
39867
+#define WAL_WRITE_LOCK 0
39868
+#define WAL_ALL_BUT_WRITE 1
39869
+#define WAL_CKPT_LOCK 1
39870
+#define WAL_RECOVER_LOCK 2
39871
+#define WAL_READ_LOCK(I) (3+(I))
39872
+#define WAL_NREADER (SQLITE_SHM_NLOCK-3)
39873
+
39874
+
39875
+/* Object declarations */
39876
+typedef struct WalIndexHdr WalIndexHdr;
39877
+typedef struct WalIterator WalIterator;
39878
+typedef struct WalCkptInfo WalCkptInfo;
39879
+
39880
+
39881
+/*
39882
+** The following object holds a copy of the wal-index header content.
39883
+**
39884
+** The actual header in the wal-index consists of two copies of this
39885
+** object.
39886
+*/
39887
+struct WalIndexHdr {
39888
+ u32 iChange; /* Counter incremented each transaction */
39889
+ u8 isInit; /* 1 when initialized */
39890
+ u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39891
+ u16 szPage; /* Database page size in bytes */
39892
+ u32 mxFrame; /* Index of last valid frame in the WAL */
39893
+ u32 nPage; /* Size of database in pages */
39894
+ u32 aFrameCksum[2]; /* Checksum of last frame in log */
39895
+ u32 aSalt[2]; /* Two salt values copied from WAL header */
39896
+ u32 aCksum[2]; /* Checksum over all prior fields */
39897
+};
39898
+
39899
+/*
39900
+** A copy of the following object occurs in the wal-index immediately
39901
+** following the second copy of the WalIndexHdr. This object stores
39902
+** information used by checkpoint.
39903
+**
39904
+** nBackfill is the number of frames in the WAL that have been written
39905
+** back into the database. (We call the act of moving content from WAL to
39906
+** database "backfilling".) The nBackfill number is never greater than
39907
+** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
39908
+** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
39909
+** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
39910
+** mxFrame back to zero when the WAL is reset.
39911
+**
39912
+** There is one entry in aReadMark[] for each reader lock. If a reader
39913
+** holds read-lock K, then the value in aReadMark[K] is no greater than
39914
+** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
39915
+** for any aReadMark[] means that entry is unused. aReadMark[0] is
39916
+** a special case; its value is never used and it exists as a place-holder
39917
+** to avoid having to offset aReadMark[] indexs by one. Readers holding
39918
+** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
39919
+** directly from the database.
39920
+**
39921
+** The value of aReadMark[K] may only be changed by a thread that
39922
+** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
39923
+** aReadMark[K] cannot changed while there is a reader is using that mark
39924
+** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
39925
+**
39926
+** The checkpointer may only transfer frames from WAL to database where
39927
+** the frame numbers are less than or equal to every aReadMark[] that is
39928
+** in use (that is, every aReadMark[j] for which there is a corresponding
39929
+** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
39930
+** largest value and will increase an unused aReadMark[] to mxFrame if there
39931
+** is not already an aReadMark[] equal to mxFrame. The exception to the
39932
+** previous sentence is when nBackfill equals mxFrame (meaning that everything
39933
+** in the WAL has been backfilled into the database) then new readers
39934
+** will choose aReadMark[0] which has value 0 and hence such reader will
39935
+** get all their all content directly from the database file and ignore
39936
+** the WAL.
39937
+**
39938
+** Writers normally append new frames to the end of the WAL. However,
39939
+** if nBackfill equals mxFrame (meaning that all WAL content has been
39940
+** written back into the database) and if no readers are using the WAL
39941
+** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
39942
+** the writer will first "reset" the WAL back to the beginning and start
39943
+** writing new content beginning at frame 1.
39944
+**
39945
+** We assume that 32-bit loads are atomic and so no locks are needed in
39946
+** order to read from any aReadMark[] entries.
39947
+*/
39948
+struct WalCkptInfo {
39949
+ u32 nBackfill; /* Number of WAL frames backfilled into DB */
39950
+ u32 aReadMark[WAL_NREADER]; /* Reader marks */
39951
+};
39952
+#define READMARK_NOT_USED 0xffffffff
39953
+
39954
+
39955
+/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
39956
+** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
39957
+** only support mandatory file-locks, we do not read or write data
39958
+** from the region of the file on which locks are applied.
39959
+*/
39960
+#define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
39961
+#define WALINDEX_LOCK_RESERVED 16
39962
+#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
39963
+
39964
+/* Size of header before each frame in wal */
39965
+#define WAL_FRAME_HDRSIZE 24
39966
+
39967
+/* Size of write ahead log header */
39968
+#define WAL_HDRSIZE 24
39969
+
39970
+/* WAL magic value. Either this value, or the same value with the least
39971
+** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
39972
+** big-endian format in the first 4 bytes of a WAL file.
39973
+**
39974
+** If the LSB is set, then the checksums for each frame within the WAL
39975
+** file are calculated by treating all data as an array of 32-bit
39976
+** big-endian words. Otherwise, they are calculated by interpreting
39977
+** all data as 32-bit little-endian words.
39978
+*/
39979
+#define WAL_MAGIC 0x377f0682
39980
+
39981
+/*
39982
+** Return the offset of frame iFrame in the write-ahead log file,
39983
+** assuming a database page size of szPage bytes. The offset returned
39984
+** is to the start of the write-ahead log frame-header.
39985
+*/
39986
+#define walFrameOffset(iFrame, szPage) ( \
39987
+ WAL_HDRSIZE + ((iFrame)-1)*((szPage)+WAL_FRAME_HDRSIZE) \
39988
+)
39989
+
39990
+/*
39991
+** An open write-ahead log file is represented by an instance of the
39992
+** following object.
39993
+*/
39994
+struct Wal {
39995
+ sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
39996
+ sqlite3_file *pDbFd; /* File handle for the database file */
39997
+ sqlite3_file *pWalFd; /* File handle for WAL file */
39998
+ u32 iCallback; /* Value to pass to log callback (or 0) */
39999
+ int nWiData; /* Size of array apWiData */
40000
+ volatile u32 **apWiData; /* Pointer to wal-index content in memory */
40001
+ u16 szPage; /* Database page size */
40002
+ i16 readLock; /* Which read lock is being held. -1 for none */
40003
+ u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
40004
+ u8 isWIndexOpen; /* True if ShmOpen() called on pDbFd */
40005
+ u8 writeLock; /* True if in a write transaction */
40006
+ u8 ckptLock; /* True if holding a checkpoint lock */
40007
+ WalIndexHdr hdr; /* Wal-index header for current transaction */
40008
+ char *zWalName; /* Name of WAL file */
40009
+ u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
40010
+#ifdef SQLITE_DEBUG
40011
+ u8 lockError; /* True if a locking error has occurred */
40012
+#endif
40013
+};
40014
+
40015
+/*
40016
+** Each page of the wal-index mapping contains a hash-table made up of
40017
+** an array of HASHTABLE_NSLOT elements of the following type.
40018
+*/
40019
+typedef u16 ht_slot;
40020
+
40021
+/*
40022
+** This structure is used to implement an iterator that loops through
40023
+** all frames in the WAL in database page order. Where two or more frames
40024
+** correspond to the same database page, the iterator visits only the
40025
+** frame most recently written to the WAL (in other words, the frame with
40026
+** the largest index).
40027
+**
40028
+** The internals of this structure are only accessed by:
40029
+**
40030
+** walIteratorInit() - Create a new iterator,
40031
+** walIteratorNext() - Step an iterator,
40032
+** walIteratorFree() - Free an iterator.
40033
+**
40034
+** This functionality is used by the checkpoint code (see walCheckpoint()).
40035
+*/
40036
+struct WalIterator {
40037
+ int iPrior; /* Last result returned from the iterator */
40038
+ int nSegment; /* Size of the aSegment[] array */
40039
+ struct WalSegment {
40040
+ int iNext; /* Next slot in aIndex[] not yet returned */
40041
+ ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
40042
+ u32 *aPgno; /* Array of page numbers. */
40043
+ int nEntry; /* Max size of aPgno[] and aIndex[] arrays */
40044
+ int iZero; /* Frame number associated with aPgno[0] */
40045
+ } aSegment[1]; /* One for every 32KB page in the WAL */
40046
+};
40047
+
40048
+/*
40049
+** Define the parameters of the hash tables in the wal-index file. There
40050
+** is a hash-table following every HASHTABLE_NPAGE page numbers in the
40051
+** wal-index.
40052
+**
40053
+** Changing any of these constants will alter the wal-index format and
40054
+** create incompatibilities.
40055
+*/
40056
+#define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
40057
+#define HASHTABLE_HASH_1 383 /* Should be prime */
40058
+#define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
40059
+
40060
+/*
40061
+** The block of page numbers associated with the first hash-table in a
40062
+** wal-index is smaller than usual. This is so that there is a complete
40063
+** hash-table on each aligned 32KB page of the wal-index.
40064
+*/
40065
+#define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
40066
+
40067
+/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
40068
+#define WALINDEX_PGSZ ( \
40069
+ sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
40070
+)
40071
+
40072
+/*
40073
+** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
40074
+** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
40075
+** numbered from zero.
40076
+**
40077
+** If this call is successful, *ppPage is set to point to the wal-index
40078
+** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
40079
+** then an SQLite error code is returned and *ppPage is set to 0.
40080
+*/
40081
+static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
40082
+ int rc = SQLITE_OK;
40083
+
40084
+ /* Enlarge the pWal->apWiData[] array if required */
40085
+ if( pWal->nWiData<=iPage ){
40086
+ int nByte = sizeof(u32 *)*(iPage+1);
40087
+ volatile u32 **apNew;
40088
+ apNew = (volatile u32 **)sqlite3_realloc(pWal->apWiData, nByte);
40089
+ if( !apNew ){
40090
+ *ppPage = 0;
40091
+ return SQLITE_NOMEM;
40092
+ }
40093
+ memset(&apNew[pWal->nWiData], 0, sizeof(u32 *)*(iPage+1-pWal->nWiData));
40094
+ pWal->apWiData = apNew;
40095
+ pWal->nWiData = iPage+1;
40096
+ }
40097
+
40098
+ /* Request a pointer to the required page from the VFS */
40099
+ if( pWal->apWiData[iPage]==0 ){
40100
+ rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
40101
+ pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
40102
+ );
40103
+ }
40104
+
40105
+ *ppPage = pWal->apWiData[iPage];
40106
+ assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
40107
+ return rc;
40108
+}
40109
+
40110
+/*
40111
+** Return a pointer to the WalCkptInfo structure in the wal-index.
40112
+*/
40113
+static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
40114
+ assert( pWal->nWiData>0 && pWal->apWiData[0] );
40115
+ return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
40116
+}
40117
+
40118
+/*
40119
+** Return a pointer to the WalIndexHdr structure in the wal-index.
40120
+*/
40121
+static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
40122
+ assert( pWal->nWiData>0 && pWal->apWiData[0] );
40123
+ return (volatile WalIndexHdr*)pWal->apWiData[0];
40124
+}
40125
+
40126
+/*
40127
+** The argument to this macro must be of type u32. On a little-endian
40128
+** architecture, it returns the u32 value that results from interpreting
40129
+** the 4 bytes as a big-endian value. On a big-endian architecture, it
40130
+** returns the value that would be produced by intepreting the 4 bytes
40131
+** of the input value as a little-endian integer.
40132
+*/
40133
+#define BYTESWAP32(x) ( \
40134
+ (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
40135
+ + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
40136
+)
40137
+
40138
+/*
40139
+** Generate or extend an 8 byte checksum based on the data in
40140
+** array aByte[] and the initial values of aIn[0] and aIn[1] (or
40141
+** initial values of 0 and 0 if aIn==NULL).
40142
+**
40143
+** The checksum is written back into aOut[] before returning.
40144
+**
40145
+** nByte must be a positive multiple of 8.
40146
+*/
40147
+static void walChecksumBytes(
40148
+ int nativeCksum, /* True for native byte-order, false for non-native */
40149
+ u8 *a, /* Content to be checksummed */
40150
+ int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
40151
+ const u32 *aIn, /* Initial checksum value input */
40152
+ u32 *aOut /* OUT: Final checksum value output */
40153
+){
40154
+ u32 s1, s2;
40155
+ u32 *aData = (u32 *)a;
40156
+ u32 *aEnd = (u32 *)&a[nByte];
40157
+
40158
+ if( aIn ){
40159
+ s1 = aIn[0];
40160
+ s2 = aIn[1];
40161
+ }else{
40162
+ s1 = s2 = 0;
40163
+ }
40164
+
40165
+ assert( nByte>=8 );
40166
+ assert( (nByte&0x00000007)==0 );
40167
+
40168
+ if( nativeCksum ){
40169
+ do {
40170
+ s1 += *aData++ + s2;
40171
+ s2 += *aData++ + s1;
40172
+ }while( aData<aEnd );
40173
+ }else{
40174
+ do {
40175
+ s1 += BYTESWAP32(aData[0]) + s2;
40176
+ s2 += BYTESWAP32(aData[1]) + s1;
40177
+ aData += 2;
40178
+ }while( aData<aEnd );
40179
+ }
40180
+
40181
+ aOut[0] = s1;
40182
+ aOut[1] = s2;
40183
+}
40184
+
40185
+/*
40186
+** Write the header information in pWal->hdr into the wal-index.
40187
+**
40188
+** The checksum on pWal->hdr is updated before it is written.
40189
+*/
40190
+static void walIndexWriteHdr(Wal *pWal){
40191
+ volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40192
+ const int nCksum = offsetof(WalIndexHdr, aCksum);
40193
+
40194
+ assert( pWal->writeLock );
40195
+ pWal->hdr.isInit = 1;
40196
+ walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40197
+ memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40198
+ sqlite3OsShmBarrier(pWal->pDbFd);
40199
+ memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40200
+}
40201
+
40202
+/*
40203
+** This function encodes a single frame header and writes it to a buffer
40204
+** supplied by the caller. A frame-header is made up of a series of
40205
+** 4-byte big-endian integers, as follows:
40206
+**
40207
+** 0: Page number.
40208
+** 4: For commit records, the size of the database image in pages
40209
+** after the commit. For all other records, zero.
40210
+** 8: Salt-1 (copied from the wal-header)
40211
+** 12: Salt-2 (copied from the wal-header)
40212
+** 16: Checksum-1.
40213
+** 20: Checksum-2.
40214
+*/
40215
+static void walEncodeFrame(
40216
+ Wal *pWal, /* The write-ahead log */
40217
+ u32 iPage, /* Database page number for frame */
40218
+ u32 nTruncate, /* New db size (or 0 for non-commit frames) */
40219
+ u8 *aData, /* Pointer to page data */
40220
+ u8 *aFrame /* OUT: Write encoded frame here */
40221
+){
40222
+ int nativeCksum; /* True for native byte-order checksums */
40223
+ u32 *aCksum = pWal->hdr.aFrameCksum;
40224
+ assert( WAL_FRAME_HDRSIZE==24 );
40225
+ sqlite3Put4byte(&aFrame[0], iPage);
40226
+ sqlite3Put4byte(&aFrame[4], nTruncate);
40227
+ memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
40228
+
40229
+ nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
40230
+ walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
40231
+ walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
40232
+
40233
+ sqlite3Put4byte(&aFrame[16], aCksum[0]);
40234
+ sqlite3Put4byte(&aFrame[20], aCksum[1]);
40235
+}
40236
+
40237
+/*
40238
+** Check to see if the frame with header in aFrame[] and content
40239
+** in aData[] is valid. If it is a valid frame, fill *piPage and
40240
+** *pnTruncate and return true. Return if the frame is not valid.
40241
+*/
40242
+static int walDecodeFrame(
40243
+ Wal *pWal, /* The write-ahead log */
40244
+ u32 *piPage, /* OUT: Database page number for frame */
40245
+ u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
40246
+ u8 *aData, /* Pointer to page data (for checksum) */
40247
+ u8 *aFrame /* Frame data */
40248
+){
40249
+ int nativeCksum; /* True for native byte-order checksums */
40250
+ u32 *aCksum = pWal->hdr.aFrameCksum;
40251
+ u32 pgno; /* Page number of the frame */
40252
+ assert( WAL_FRAME_HDRSIZE==24 );
40253
+
40254
+ /* A frame is only valid if the salt values in the frame-header
40255
+ ** match the salt values in the wal-header.
40256
+ */
40257
+ if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
40258
+ return 0;
40259
+ }
40260
+
40261
+ /* A frame is only valid if the page number is creater than zero.
40262
+ */
40263
+ pgno = sqlite3Get4byte(&aFrame[0]);
40264
+ if( pgno==0 ){
40265
+ return 0;
40266
+ }
40267
+
40268
+ /* A frame is only valid if a checksum of the first 16 bytes
40269
+ ** of the frame-header, and the frame-data matches
40270
+ ** the checksum in the last 8 bytes of the frame-header.
40271
+ */
40272
+ nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
40273
+ walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
40274
+ walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
40275
+ if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
40276
+ || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
40277
+ ){
40278
+ /* Checksum failed. */
40279
+ return 0;
40280
+ }
40281
+
40282
+ /* If we reach this point, the frame is valid. Return the page number
40283
+ ** and the new database size.
40284
+ */
40285
+ *piPage = pgno;
40286
+ *pnTruncate = sqlite3Get4byte(&aFrame[4]);
40287
+ return 1;
40288
+}
40289
+
40290
+
40291
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
40292
+/*
40293
+** Names of locks. This routine is used to provide debugging output and is not
40294
+** a part of an ordinary build.
40295
+*/
40296
+static const char *walLockName(int lockIdx){
40297
+ if( lockIdx==WAL_WRITE_LOCK ){
40298
+ return "WRITE-LOCK";
40299
+ }else if( lockIdx==WAL_CKPT_LOCK ){
40300
+ return "CKPT-LOCK";
40301
+ }else if( lockIdx==WAL_RECOVER_LOCK ){
40302
+ return "RECOVER-LOCK";
40303
+ }else{
40304
+ static char zName[15];
40305
+ sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
40306
+ lockIdx-WAL_READ_LOCK(0));
40307
+ return zName;
40308
+ }
40309
+}
40310
+#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
40311
+
40312
+
40313
+/*
40314
+** Set or release locks on the WAL. Locks are either shared or exclusive.
40315
+** A lock cannot be moved directly between shared and exclusive - it must go
40316
+** through the unlocked state first.
40317
+**
40318
+** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
40319
+*/
40320
+static int walLockShared(Wal *pWal, int lockIdx){
40321
+ int rc;
40322
+ if( pWal->exclusiveMode ) return SQLITE_OK;
40323
+ rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
40324
+ SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
40325
+ WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
40326
+ walLockName(lockIdx), rc ? "failed" : "ok"));
40327
+ VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
40328
+ return rc;
40329
+}
40330
+static void walUnlockShared(Wal *pWal, int lockIdx){
40331
+ if( pWal->exclusiveMode ) return;
40332
+ (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
40333
+ SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
40334
+ WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
40335
+}
40336
+static int walLockExclusive(Wal *pWal, int lockIdx, int n){
40337
+ int rc;
40338
+ if( pWal->exclusiveMode ) return SQLITE_OK;
40339
+ rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
40340
+ SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
40341
+ WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
40342
+ walLockName(lockIdx), n, rc ? "failed" : "ok"));
40343
+ VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
40344
+ return rc;
40345
+}
40346
+static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
40347
+ if( pWal->exclusiveMode ) return;
40348
+ (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
40349
+ SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
40350
+ WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
40351
+ walLockName(lockIdx), n));
40352
+}
40353
+
40354
+/*
40355
+** Compute a hash on a page number. The resulting hash value must land
40356
+** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
40357
+** the hash to the next value in the event of a collision.
40358
+*/
40359
+static int walHash(u32 iPage){
40360
+ assert( iPage>0 );
40361
+ assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
40362
+ return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
40363
+}
40364
+static int walNextHash(int iPriorHash){
40365
+ return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
40366
+}
40367
+
40368
+/*
40369
+** Return pointers to the hash table and page number array stored on
40370
+** page iHash of the wal-index. The wal-index is broken into 32KB pages
40371
+** numbered starting from 0.
40372
+**
40373
+** Set output variable *paHash to point to the start of the hash table
40374
+** in the wal-index file. Set *piZero to one less than the frame
40375
+** number of the first frame indexed by this hash table. If a
40376
+** slot in the hash table is set to N, it refers to frame number
40377
+** (*piZero+N) in the log.
40378
+**
40379
+** Finally, set *paPgno so that *paPgno[1] is the page number of the
40380
+** first frame indexed by the hash table, frame (*piZero+1).
40381
+*/
40382
+static int walHashGet(
40383
+ Wal *pWal, /* WAL handle */
40384
+ int iHash, /* Find the iHash'th table */
40385
+ volatile ht_slot **paHash, /* OUT: Pointer to hash index */
40386
+ volatile u32 **paPgno, /* OUT: Pointer to page number array */
40387
+ u32 *piZero /* OUT: Frame associated with *paPgno[0] */
40388
+){
40389
+ int rc; /* Return code */
40390
+ volatile u32 *aPgno;
40391
+
40392
+ rc = walIndexPage(pWal, iHash, &aPgno);
40393
+ assert( rc==SQLITE_OK || iHash>0 );
40394
+
40395
+ if( rc==SQLITE_OK ){
40396
+ u32 iZero;
40397
+ volatile ht_slot *aHash;
40398
+
40399
+ aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
40400
+ if( iHash==0 ){
40401
+ aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
40402
+ iZero = 0;
40403
+ }else{
40404
+ iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
40405
+ }
40406
+
40407
+ *paPgno = &aPgno[-1];
40408
+ *paHash = aHash;
40409
+ *piZero = iZero;
40410
+ }
40411
+ return rc;
40412
+}
40413
+
40414
+/*
40415
+** Return the number of the wal-index page that contains the hash-table
40416
+** and page-number array that contain entries corresponding to WAL frame
40417
+** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
40418
+** are numbered starting from 0.
40419
+*/
40420
+static int walFramePage(u32 iFrame){
40421
+ int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
40422
+ assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
40423
+ && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
40424
+ && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
40425
+ && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
40426
+ && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
40427
+ );
40428
+ return iHash;
40429
+}
40430
+
40431
+/*
40432
+** Return the page number associated with frame iFrame in this WAL.
40433
+*/
40434
+static u32 walFramePgno(Wal *pWal, u32 iFrame){
40435
+ int iHash = walFramePage(iFrame);
40436
+ if( iHash==0 ){
40437
+ return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
40438
+ }
40439
+ return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
40440
+}
40441
+
40442
+/*
40443
+** Remove entries from the hash table that point to WAL slots greater
40444
+** than pWal->hdr.mxFrame.
40445
+**
40446
+** This function is called whenever pWal->hdr.mxFrame is decreased due
40447
+** to a rollback or savepoint.
40448
+**
40449
+** At most only the hash table containing pWal->hdr.mxFrame needs to be
40450
+** updated. Any later hash tables will be automatically cleared when
40451
+** pWal->hdr.mxFrame advances to the point where those hash tables are
40452
+** actually needed.
40453
+*/
40454
+static void walCleanupHash(Wal *pWal){
40455
+ volatile ht_slot *aHash; /* Pointer to hash table to clear */
40456
+ volatile u32 *aPgno; /* Page number array for hash table */
40457
+ u32 iZero; /* frame == (aHash[x]+iZero) */
40458
+ int iLimit = 0; /* Zero values greater than this */
40459
+ int nByte; /* Number of bytes to zero in aPgno[] */
40460
+ int i; /* Used to iterate through aHash[] */
40461
+
40462
+ assert( pWal->writeLock );
40463
+ testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
40464
+ testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
40465
+ testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
40466
+
40467
+ if( pWal->hdr.mxFrame==0 ) return;
40468
+
40469
+ /* Obtain pointers to the hash-table and page-number array containing
40470
+ ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
40471
+ ** that the page said hash-table and array reside on is already mapped.
40472
+ */
40473
+ assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
40474
+ assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
40475
+ walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
40476
+
40477
+ /* Zero all hash-table entries that correspond to frame numbers greater
40478
+ ** than pWal->hdr.mxFrame.
40479
+ */
40480
+ iLimit = pWal->hdr.mxFrame - iZero;
40481
+ assert( iLimit>0 );
40482
+ for(i=0; i<HASHTABLE_NSLOT; i++){
40483
+ if( aHash[i]>iLimit ){
40484
+ aHash[i] = 0;
40485
+ }
40486
+ }
40487
+
40488
+ /* Zero the entries in the aPgno array that correspond to frames with
40489
+ ** frame numbers greater than pWal->hdr.mxFrame.
40490
+ */
40491
+ nByte = ((char *)aHash - (char *)&aPgno[iLimit+1]);
40492
+ memset((void *)&aPgno[iLimit+1], 0, nByte);
40493
+
40494
+#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
40495
+ /* Verify that the every entry in the mapping region is still reachable
40496
+ ** via the hash table even after the cleanup.
40497
+ */
40498
+ if( iLimit ){
40499
+ int i; /* Loop counter */
40500
+ int iKey; /* Hash key */
40501
+ for(i=1; i<=iLimit; i++){
40502
+ for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
40503
+ if( aHash[iKey]==i ) break;
40504
+ }
40505
+ assert( aHash[iKey]==i );
40506
+ }
40507
+ }
40508
+#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
40509
+}
40510
+
40511
+
40512
+/*
40513
+** Set an entry in the wal-index that will map database page number
40514
+** pPage into WAL frame iFrame.
40515
+*/
40516
+static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
40517
+ int rc; /* Return code */
40518
+ u32 iZero; /* One less than frame number of aPgno[1] */
40519
+ volatile u32 *aPgno; /* Page number array */
40520
+ volatile ht_slot *aHash; /* Hash table */
40521
+
40522
+ rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
40523
+
40524
+ /* Assuming the wal-index file was successfully mapped, populate the
40525
+ ** page number array and hash table entry.
40526
+ */
40527
+ if( rc==SQLITE_OK ){
40528
+ int iKey; /* Hash table key */
40529
+ int idx; /* Value to write to hash-table slot */
40530
+ TESTONLY( int nCollide = 0; /* Number of hash collisions */ )
40531
+
40532
+ idx = iFrame - iZero;
40533
+ assert( idx <= HASHTABLE_NSLOT/2 + 1 );
40534
+
40535
+ /* If this is the first entry to be added to this hash-table, zero the
40536
+ ** entire hash table and aPgno[] array before proceding.
40537
+ */
40538
+ if( idx==1 ){
40539
+ int nByte = (u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1];
40540
+ memset((void*)&aPgno[1], 0, nByte);
40541
+ }
40542
+
40543
+ /* If the entry in aPgno[] is already set, then the previous writer
40544
+ ** must have exited unexpectedly in the middle of a transaction (after
40545
+ ** writing one or more dirty pages to the WAL to free up memory).
40546
+ ** Remove the remnants of that writers uncommitted transaction from
40547
+ ** the hash-table before writing any new entries.
40548
+ */
40549
+ if( aPgno[idx] ){
40550
+ walCleanupHash(pWal);
40551
+ assert( !aPgno[idx] );
40552
+ }
40553
+
40554
+ /* Write the aPgno[] array entry and the hash-table slot. */
40555
+ for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
40556
+ assert( nCollide++ < idx );
40557
+ }
40558
+ aPgno[idx] = iPage;
40559
+ aHash[iKey] = idx;
40560
+
40561
+#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
40562
+ /* Verify that the number of entries in the hash table exactly equals
40563
+ ** the number of entries in the mapping region.
40564
+ */
40565
+ {
40566
+ int i; /* Loop counter */
40567
+ int nEntry = 0; /* Number of entries in the hash table */
40568
+ for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
40569
+ assert( nEntry==idx );
40570
+ }
40571
+
40572
+ /* Verify that the every entry in the mapping region is reachable
40573
+ ** via the hash table. This turns out to be a really, really expensive
40574
+ ** thing to check, so only do this occasionally - not on every
40575
+ ** iteration.
40576
+ */
40577
+ if( (idx&0x3ff)==0 ){
40578
+ int i; /* Loop counter */
40579
+ for(i=1; i<=idx; i++){
40580
+ for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
40581
+ if( aHash[iKey]==i ) break;
40582
+ }
40583
+ assert( aHash[iKey]==i );
40584
+ }
40585
+ }
40586
+#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
40587
+ }
40588
+
40589
+
40590
+ return rc;
40591
+}
40592
+
40593
+
40594
+/*
40595
+** Recover the wal-index by reading the write-ahead log file.
40596
+**
40597
+** This routine first tries to establish an exclusive lock on the
40598
+** wal-index to prevent other threads/processes from doing anything
40599
+** with the WAL or wal-index while recovery is running. The
40600
+** WAL_RECOVER_LOCK is also held so that other threads will know
40601
+** that this thread is running recovery. If unable to establish
40602
+** the necessary locks, this routine returns SQLITE_BUSY.
40603
+*/
40604
+static int walIndexRecover(Wal *pWal){
40605
+ int rc; /* Return Code */
40606
+ i64 nSize; /* Size of log file */
40607
+ u32 aFrameCksum[2] = {0, 0};
40608
+ int iLock; /* Lock offset to lock for checkpoint */
40609
+ int nLock; /* Number of locks to hold */
40610
+
40611
+ /* Obtain an exclusive lock on all byte in the locking range not already
40612
+ ** locked by the caller. The caller is guaranteed to have locked the
40613
+ ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
40614
+ ** If successful, the same bytes that are locked here are unlocked before
40615
+ ** this function returns.
40616
+ */
40617
+ assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
40618
+ assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
40619
+ assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
40620
+ assert( pWal->writeLock );
40621
+ iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
40622
+ nLock = SQLITE_SHM_NLOCK - iLock;
40623
+ rc = walLockExclusive(pWal, iLock, nLock);
40624
+ if( rc ){
40625
+ return rc;
40626
+ }
40627
+ WALTRACE(("WAL%p: recovery begin...\n", pWal));
40628
+
40629
+ memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
40630
+
40631
+ rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
40632
+ if( rc!=SQLITE_OK ){
40633
+ goto recovery_error;
40634
+ }
40635
+
40636
+ if( nSize>WAL_HDRSIZE ){
40637
+ u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
40638
+ u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
40639
+ int szFrame; /* Number of bytes in buffer aFrame[] */
40640
+ u8 *aData; /* Pointer to data part of aFrame buffer */
40641
+ int iFrame; /* Index of last frame read */
40642
+ i64 iOffset; /* Next offset to read from log file */
40643
+ int szPage; /* Page size according to the log */
40644
+ u32 magic; /* Magic value read from WAL header */
40645
+
40646
+ /* Read in the WAL header. */
40647
+ rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40648
+ if( rc!=SQLITE_OK ){
40649
+ goto recovery_error;
40650
+ }
40651
+
40652
+ /* If the database page size is not a power of two, or is greater than
40653
+ ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
40654
+ ** data. Similarly, if the 'magic' value is invalid, ignore the whole
40655
+ ** WAL file.
40656
+ */
40657
+ magic = sqlite3Get4byte(&aBuf[0]);
40658
+ szPage = sqlite3Get4byte(&aBuf[8]);
40659
+ if( (magic&0xFFFFFFFE)!=WAL_MAGIC
40660
+ || szPage&(szPage-1)
40661
+ || szPage>SQLITE_MAX_PAGE_SIZE
40662
+ || szPage<512
40663
+ ){
40664
+ goto finished;
40665
+ }
40666
+ pWal->hdr.bigEndCksum = (magic&0x00000001);
40667
+ pWal->szPage = szPage;
40668
+ pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40669
+ memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40670
+ walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40671
+ aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40672
+ );
40673
+
40674
+ /* Malloc a buffer to read frames into. */
40675
+ szFrame = szPage + WAL_FRAME_HDRSIZE;
40676
+ aFrame = (u8 *)sqlite3_malloc(szFrame);
40677
+ if( !aFrame ){
40678
+ rc = SQLITE_NOMEM;
40679
+ goto recovery_error;
40680
+ }
40681
+ aData = &aFrame[WAL_FRAME_HDRSIZE];
40682
+
40683
+ /* Read all frames from the log file. */
40684
+ iFrame = 0;
40685
+ for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
40686
+ u32 pgno; /* Database page number for frame */
40687
+ u32 nTruncate; /* dbsize field from frame header */
40688
+ int isValid; /* True if this frame is valid */
40689
+
40690
+ /* Read and decode the next log frame. */
40691
+ rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
40692
+ if( rc!=SQLITE_OK ) break;
40693
+ isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
40694
+ if( !isValid ) break;
40695
+ rc = walIndexAppend(pWal, ++iFrame, pgno);
40696
+ if( rc!=SQLITE_OK ) break;
40697
+
40698
+ /* If nTruncate is non-zero, this is a commit record. */
40699
+ if( nTruncate ){
40700
+ pWal->hdr.mxFrame = iFrame;
40701
+ pWal->hdr.nPage = nTruncate;
40702
+ pWal->hdr.szPage = szPage;
40703
+ aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
40704
+ aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
40705
+ }
40706
+ }
40707
+
40708
+ sqlite3_free(aFrame);
40709
+ }
40710
+
40711
+finished:
40712
+ if( rc==SQLITE_OK ){
40713
+ volatile WalCkptInfo *pInfo;
40714
+ int i;
40715
+ pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
40716
+ pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
40717
+ walIndexWriteHdr(pWal);
40718
+
40719
+ /* Reset the checkpoint-header. This is safe because this thread is
40720
+ ** currently holding locks that exclude all other readers, writers and
40721
+ ** checkpointers.
40722
+ */
40723
+ pInfo = walCkptInfo(pWal);
40724
+ pInfo->nBackfill = 0;
40725
+ pInfo->aReadMark[0] = 0;
40726
+ for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
40727
+ }
40728
+
40729
+recovery_error:
40730
+ WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
40731
+ walUnlockExclusive(pWal, iLock, nLock);
40732
+ return rc;
40733
+}
40734
+
40735
+/*
40736
+** Close an open wal-index.
40737
+*/
40738
+static void walIndexClose(Wal *pWal, int isDelete){
40739
+ if( pWal->isWIndexOpen ){
40740
+ sqlite3OsShmClose(pWal->pDbFd, isDelete);
40741
+ pWal->isWIndexOpen = 0;
40742
+ }
40743
+}
40744
+
40745
+/*
40746
+** Open a connection to the WAL file associated with database zDbName.
40747
+** The database file must already be opened on connection pDbFd.
40748
+**
40749
+** A SHARED lock should be held on the database file when this function
40750
+** is called. The purpose of this SHARED lock is to prevent any other
40751
+** client from unlinking the WAL or wal-index file. If another process
40752
+** were to do this just after this client opened one of these files, the
40753
+** system would be badly broken.
40754
+**
40755
+** If the log file is successfully opened, SQLITE_OK is returned and
40756
+** *ppWal is set to point to a new WAL handle. If an error occurs,
40757
+** an SQLite error code is returned and *ppWal is left unmodified.
40758
+*/
40759
+SQLITE_PRIVATE int sqlite3WalOpen(
40760
+ sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
40761
+ sqlite3_file *pDbFd, /* The open database file */
40762
+ const char *zDbName, /* Name of the database file */
40763
+ Wal **ppWal /* OUT: Allocated Wal handle */
40764
+){
40765
+ int rc; /* Return Code */
40766
+ Wal *pRet; /* Object to allocate and return */
40767
+ int flags; /* Flags passed to OsOpen() */
40768
+ char *zWal; /* Name of write-ahead log file */
40769
+ int nWal; /* Length of zWal in bytes */
40770
+
40771
+ assert( zDbName && zDbName[0] );
40772
+ assert( pDbFd );
40773
+
40774
+ /* In the amalgamation, the os_unix.c and os_win.c source files come before
40775
+ ** this source file. Verify that the #defines of the locking byte offsets
40776
+ ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
40777
+ */
40778
+#ifdef WIN_SHM_BASE
40779
+ assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
40780
+#endif
40781
+#ifdef UNIX_SHM_BASE
40782
+ assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
40783
+#endif
40784
+
40785
+
40786
+ /* Allocate an instance of struct Wal to return. */
40787
+ *ppWal = 0;
40788
+ nWal = sqlite3Strlen30(zDbName) + 5;
40789
+ pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal);
40790
+ if( !pRet ){
40791
+ return SQLITE_NOMEM;
40792
+ }
40793
+
40794
+ pRet->pVfs = pVfs;
40795
+ pRet->pWalFd = (sqlite3_file *)&pRet[1];
40796
+ pRet->pDbFd = pDbFd;
40797
+ pRet->readLock = -1;
40798
+ sqlite3_randomness(8, &pRet->hdr.aSalt);
40799
+ pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd;
40800
+ sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName);
40801
+ rc = sqlite3OsShmOpen(pDbFd);
40802
+
40803
+ /* Open file handle on the write-ahead log file. */
40804
+ if( rc==SQLITE_OK ){
40805
+ pRet->isWIndexOpen = 1;
40806
+ flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL);
40807
+ rc = sqlite3OsOpen(pVfs, zWal, pRet->pWalFd, flags, &flags);
40808
+ }
40809
+
40810
+ if( rc!=SQLITE_OK ){
40811
+ walIndexClose(pRet, 0);
40812
+ sqlite3OsClose(pRet->pWalFd);
40813
+ sqlite3_free(pRet);
40814
+ }else{
40815
+ *ppWal = pRet;
40816
+ WALTRACE(("WAL%d: opened\n", pRet));
40817
+ }
40818
+ return rc;
40819
+}
40820
+
40821
+/*
40822
+** Find the smallest page number out of all pages held in the WAL that
40823
+** has not been returned by any prior invocation of this method on the
40824
+** same WalIterator object. Write into *piFrame the frame index where
40825
+** that page was last written into the WAL. Write into *piPage the page
40826
+** number.
40827
+**
40828
+** Return 0 on success. If there are no pages in the WAL with a page
40829
+** number larger than *piPage, then return 1.
40830
+*/
40831
+static int walIteratorNext(
40832
+ WalIterator *p, /* Iterator */
40833
+ u32 *piPage, /* OUT: The page number of the next page */
40834
+ u32 *piFrame /* OUT: Wal frame index of next page */
40835
+){
40836
+ u32 iMin; /* Result pgno must be greater than iMin */
40837
+ u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
40838
+ int i; /* For looping through segments */
40839
+
40840
+ iMin = p->iPrior;
40841
+ assert( iMin<0xffffffff );
40842
+ for(i=p->nSegment-1; i>=0; i--){
40843
+ struct WalSegment *pSegment = &p->aSegment[i];
40844
+ while( pSegment->iNext<pSegment->nEntry ){
40845
+ u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
40846
+ if( iPg>iMin ){
40847
+ if( iPg<iRet ){
40848
+ iRet = iPg;
40849
+ *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
40850
+ }
40851
+ break;
40852
+ }
40853
+ pSegment->iNext++;
40854
+ }
40855
+ }
40856
+
40857
+ *piPage = p->iPrior = iRet;
40858
+ return (iRet==0xFFFFFFFF);
40859
+}
40860
+
40861
+
40862
+static void walMergesort(
40863
+ u32 *aContent, /* Pages in wal */
40864
+ ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
40865
+ ht_slot *aList, /* IN/OUT: List to sort */
40866
+ int *pnList /* IN/OUT: Number of elements in aList[] */
40867
+){
40868
+ int nList = *pnList;
40869
+ if( nList>1 ){
40870
+ int nLeft = nList / 2; /* Elements in left list */
40871
+ int nRight = nList - nLeft; /* Elements in right list */
40872
+ int iLeft = 0; /* Current index in aLeft */
40873
+ int iRight = 0; /* Current index in aright */
40874
+ int iOut = 0; /* Current index in output buffer */
40875
+ ht_slot *aLeft = aList; /* Left list */
40876
+ ht_slot *aRight = aList+nLeft;/* Right list */
40877
+
40878
+ /* TODO: Change to non-recursive version. */
40879
+ walMergesort(aContent, aBuffer, aLeft, &nLeft);
40880
+ walMergesort(aContent, aBuffer, aRight, &nRight);
40881
+
40882
+ while( iRight<nRight || iLeft<nLeft ){
40883
+ ht_slot logpage;
40884
+ Pgno dbpage;
40885
+
40886
+ if( (iLeft<nLeft)
40887
+ && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
40888
+ ){
40889
+ logpage = aLeft[iLeft++];
40890
+ }else{
40891
+ logpage = aRight[iRight++];
40892
+ }
40893
+ dbpage = aContent[logpage];
40894
+
40895
+ aBuffer[iOut++] = logpage;
40896
+ if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
40897
+
40898
+ assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
40899
+ assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
40900
+ }
40901
+ memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
40902
+ *pnList = iOut;
40903
+ }
40904
+
40905
+#ifdef SQLITE_DEBUG
40906
+ {
40907
+ int i;
40908
+ for(i=1; i<*pnList; i++){
40909
+ assert( aContent[aList[i]] > aContent[aList[i-1]] );
40910
+ }
40911
+ }
40912
+#endif
40913
+}
40914
+
40915
+/*
40916
+** Free an iterator allocated by walIteratorInit().
40917
+*/
40918
+static void walIteratorFree(WalIterator *p){
40919
+ sqlite3_free(p);
40920
+}
40921
+
40922
+/*
40923
+** Map the wal-index into memory owned by this thread, if it is not
40924
+** mapped already. Then construct a WalInterator object that can be
40925
+** used to loop over all pages in the WAL in ascending order.
40926
+**
40927
+** On success, make *pp point to the newly allocated WalInterator object
40928
+** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error
40929
+** code.
40930
+**
40931
+** The calling routine should invoke walIteratorFree() to destroy the
40932
+** WalIterator object when it has finished with it. The caller must
40933
+** also unmap the wal-index. But the wal-index must not be unmapped
40934
+** prior to the WalIterator object being destroyed.
40935
+*/
40936
+static int walIteratorInit(Wal *pWal, WalIterator **pp){
40937
+ WalIterator *p; /* Return value */
40938
+ int nSegment; /* Number of segments to merge */
40939
+ u32 iLast; /* Last frame in log */
40940
+ int nByte; /* Number of bytes to allocate */
40941
+ int i; /* Iterator variable */
40942
+ ht_slot *aTmp; /* Temp space used by merge-sort */
40943
+ ht_slot *aSpace; /* Space at the end of the allocation */
40944
+
40945
+ /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other
40946
+ ** thread is able to write to shared memory while this routine is
40947
+ ** running (or, indeed, while the WalIterator object exists). Hence,
40948
+ ** we can cast off the volatile qualification from shared memory
40949
+ */
40950
+ assert( pWal->ckptLock );
40951
+ iLast = pWal->hdr.mxFrame;
40952
+
40953
+ /* Allocate space for the WalIterator object */
40954
+ nSegment = walFramePage(iLast) + 1;
40955
+ nByte = sizeof(WalIterator)
40956
+ + nSegment*(sizeof(struct WalSegment))
40957
+ + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot));
40958
+ p = (WalIterator *)sqlite3_malloc(nByte);
40959
+ if( !p ){
40960
+ return SQLITE_NOMEM;
40961
+ }
40962
+ memset(p, 0, nByte);
40963
+
40964
+ /* Allocate space for the WalIterator object */
40965
+ p->nSegment = nSegment;
40966
+ aSpace = (ht_slot *)&p->aSegment[nSegment];
40967
+ aTmp = &aSpace[HASHTABLE_NPAGE*nSegment];
40968
+ for(i=0; i<nSegment; i++){
40969
+ volatile ht_slot *aHash;
40970
+ int j;
40971
+ u32 iZero;
40972
+ int nEntry;
40973
+ volatile u32 *aPgno;
40974
+ int rc;
40975
+
40976
+ rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
40977
+ if( rc!=SQLITE_OK ){
40978
+ walIteratorFree(p);
40979
+ return rc;
40980
+ }
40981
+ aPgno++;
40982
+ nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
40983
+ iZero++;
40984
+
40985
+ for(j=0; j<nEntry; j++){
40986
+ aSpace[j] = j;
40987
+ }
40988
+ walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry);
40989
+ p->aSegment[i].iZero = iZero;
40990
+ p->aSegment[i].nEntry = nEntry;
40991
+ p->aSegment[i].aIndex = aSpace;
40992
+ p->aSegment[i].aPgno = (u32 *)aPgno;
40993
+ aSpace += HASHTABLE_NPAGE;
40994
+ }
40995
+ assert( aSpace==aTmp );
40996
+
40997
+ /* Return the fully initialized WalIterator object */
40998
+ *pp = p;
40999
+ return SQLITE_OK ;
41000
+}
41001
+
41002
+/*
41003
+** Copy as much content as we can from the WAL back into the database file
41004
+** in response to an sqlite3_wal_checkpoint() request or the equivalent.
41005
+**
41006
+** The amount of information copies from WAL to database might be limited
41007
+** by active readers. This routine will never overwrite a database page
41008
+** that a concurrent reader might be using.
41009
+**
41010
+** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
41011
+** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
41012
+** checkpoints are always run by a background thread or background
41013
+** process, foreground threads will never block on a lengthy fsync call.
41014
+**
41015
+** Fsync is called on the WAL before writing content out of the WAL and
41016
+** into the database. This ensures that if the new content is persistent
41017
+** in the WAL and can be recovered following a power-loss or hard reset.
41018
+**
41019
+** Fsync is also called on the database file if (and only if) the entire
41020
+** WAL content is copied into the database file. This second fsync makes
41021
+** it safe to delete the WAL since the new content will persist in the
41022
+** database file.
41023
+**
41024
+** This routine uses and updates the nBackfill field of the wal-index header.
41025
+** This is the only routine tha will increase the value of nBackfill.
41026
+** (A WAL reset or recovery will revert nBackfill to zero, but not increase
41027
+** its value.)
41028
+**
41029
+** The caller must be holding sufficient locks to ensure that no other
41030
+** checkpoint is running (in any other thread or process) at the same
41031
+** time.
41032
+*/
41033
+static int walCheckpoint(
41034
+ Wal *pWal, /* Wal connection */
41035
+ int sync_flags, /* Flags for OsSync() (or 0) */
41036
+ int nBuf, /* Size of zBuf in bytes */
41037
+ u8 *zBuf /* Temporary buffer to use */
41038
+){
41039
+ int rc; /* Return code */
41040
+ int szPage = pWal->hdr.szPage; /* Database page-size */
41041
+ WalIterator *pIter = 0; /* Wal iterator context */
41042
+ u32 iDbpage = 0; /* Next database page to write */
41043
+ u32 iFrame = 0; /* Wal frame containing data for iDbpage */
41044
+ u32 mxSafeFrame; /* Max frame that can be backfilled */
41045
+ int i; /* Loop counter */
41046
+ volatile WalCkptInfo *pInfo; /* The checkpoint status information */
41047
+
41048
+ /* Allocate the iterator */
41049
+ rc = walIteratorInit(pWal, &pIter);
41050
+ if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){
41051
+ goto walcheckpoint_out;
41052
+ }
41053
+
41054
+ /*** TODO: Move this test out to the caller. Make it an assert() here ***/
41055
+ if( pWal->hdr.szPage!=nBuf ){
41056
+ rc = SQLITE_CORRUPT_BKPT;
41057
+ goto walcheckpoint_out;
41058
+ }
41059
+
41060
+ /* Compute in mxSafeFrame the index of the last frame of the WAL that is
41061
+ ** safe to write into the database. Frames beyond mxSafeFrame might
41062
+ ** overwrite database pages that are in use by active readers and thus
41063
+ ** cannot be backfilled from the WAL.
41064
+ */
41065
+ mxSafeFrame = pWal->hdr.mxFrame;
41066
+ pInfo = walCkptInfo(pWal);
41067
+ for(i=1; i<WAL_NREADER; i++){
41068
+ u32 y = pInfo->aReadMark[i];
41069
+ if( mxSafeFrame>=y ){
41070
+ assert( y<=pWal->hdr.mxFrame );
41071
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
41072
+ if( rc==SQLITE_OK ){
41073
+ pInfo->aReadMark[i] = READMARK_NOT_USED;
41074
+ walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
41075
+ }else if( rc==SQLITE_BUSY ){
41076
+ mxSafeFrame = y;
41077
+ }else{
41078
+ goto walcheckpoint_out;
41079
+ }
41080
+ }
41081
+ }
41082
+
41083
+ if( pInfo->nBackfill<mxSafeFrame
41084
+ && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
41085
+ ){
41086
+ u32 nBackfill = pInfo->nBackfill;
41087
+
41088
+ /* Sync the WAL to disk */
41089
+ if( sync_flags ){
41090
+ rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
41091
+ }
41092
+
41093
+ /* Iterate through the contents of the WAL, copying data to the db file. */
41094
+ while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
41095
+ assert( walFramePgno(pWal, iFrame)==iDbpage );
41096
+ if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue;
41097
+ rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage,
41098
+ walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE
41099
+ );
41100
+ if( rc!=SQLITE_OK ) break;
41101
+ rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, (iDbpage-1)*szPage);
41102
+ if( rc!=SQLITE_OK ) break;
41103
+ }
41104
+
41105
+ /* If work was actually accomplished... */
41106
+ if( rc==SQLITE_OK ){
41107
+ if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
41108
+ rc = sqlite3OsTruncate(pWal->pDbFd, ((i64)pWal->hdr.nPage*(i64)szPage));
41109
+ if( rc==SQLITE_OK && sync_flags ){
41110
+ rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
41111
+ }
41112
+ }
41113
+ if( rc==SQLITE_OK ){
41114
+ pInfo->nBackfill = mxSafeFrame;
41115
+ }
41116
+ }
41117
+
41118
+ /* Release the reader lock held while backfilling */
41119
+ walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
41120
+ }else if( rc==SQLITE_BUSY ){
41121
+ /* Reset the return code so as not to report a checkpoint failure
41122
+ ** just because active readers prevent any backfill.
41123
+ */
41124
+ rc = SQLITE_OK;
41125
+ }
41126
+
41127
+ walcheckpoint_out:
41128
+ walIteratorFree(pIter);
41129
+ return rc;
41130
+}
41131
+
41132
+/*
41133
+** Close a connection to a log file.
41134
+*/
41135
+SQLITE_PRIVATE int sqlite3WalClose(
41136
+ Wal *pWal, /* Wal to close */
41137
+ int sync_flags, /* Flags to pass to OsSync() (or 0) */
41138
+ int nBuf,
41139
+ u8 *zBuf /* Buffer of at least nBuf bytes */
41140
+){
41141
+ int rc = SQLITE_OK;
41142
+ if( pWal ){
41143
+ int isDelete = 0; /* True to unlink wal and wal-index files */
41144
+
41145
+ /* If an EXCLUSIVE lock can be obtained on the database file (using the
41146
+ ** ordinary, rollback-mode locking methods, this guarantees that the
41147
+ ** connection associated with this log file is the only connection to
41148
+ ** the database. In this case checkpoint the database and unlink both
41149
+ ** the wal and wal-index files.
41150
+ **
41151
+ ** The EXCLUSIVE lock is not released before returning.
41152
+ */
41153
+ rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
41154
+ if( rc==SQLITE_OK ){
41155
+ pWal->exclusiveMode = 1;
41156
+ rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf);
41157
+ if( rc==SQLITE_OK ){
41158
+ isDelete = 1;
41159
+ }
41160
+ }
41161
+
41162
+ walIndexClose(pWal, isDelete);
41163
+ sqlite3OsClose(pWal->pWalFd);
41164
+ if( isDelete ){
41165
+ sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
41166
+ }
41167
+ WALTRACE(("WAL%p: closed\n", pWal));
41168
+ sqlite3_free(pWal->apWiData);
41169
+ sqlite3_free(pWal);
41170
+ }
41171
+ return rc;
41172
+}
41173
+
41174
+/*
41175
+** Try to read the wal-index header. Return 0 on success and 1 if
41176
+** there is a problem.
41177
+**
41178
+** The wal-index is in shared memory. Another thread or process might
41179
+** be writing the header at the same time this procedure is trying to
41180
+** read it, which might result in inconsistency. A dirty read is detected
41181
+** by verifying that both copies of the header are the same and also by
41182
+** a checksum on the header.
41183
+**
41184
+** If and only if the read is consistent and the header is different from
41185
+** pWal->hdr, then pWal->hdr is updated to the content of the new header
41186
+** and *pChanged is set to 1.
41187
+**
41188
+** If the checksum cannot be verified return non-zero. If the header
41189
+** is read successfully and the checksum verified, return zero.
41190
+*/
41191
+int walIndexTryHdr(Wal *pWal, int *pChanged){
41192
+ u32 aCksum[2]; /* Checksum on the header content */
41193
+ WalIndexHdr h1, h2; /* Two copies of the header content */
41194
+ WalIndexHdr volatile *aHdr; /* Header in shared memory */
41195
+
41196
+ /* The first page of the wal-index must be mapped at this point. */
41197
+ assert( pWal->nWiData>0 && pWal->apWiData[0] );
41198
+
41199
+ /* Read the header. This might happen currently with a write to the
41200
+ ** same area of shared memory on a different CPU in a SMP,
41201
+ ** meaning it is possible that an inconsistent snapshot is read
41202
+ ** from the file. If this happens, return non-zero.
41203
+ **
41204
+ ** There are two copies of the header at the beginning of the wal-index.
41205
+ ** When reading, read [0] first then [1]. Writes are in the reverse order.
41206
+ ** Memory barriers are used to prevent the compiler or the hardware from
41207
+ ** reordering the reads and writes.
41208
+ */
41209
+ aHdr = walIndexHdr(pWal);
41210
+ memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
41211
+ sqlite3OsShmBarrier(pWal->pDbFd);
41212
+ memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
41213
+
41214
+ if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
41215
+ return 1; /* Dirty read */
41216
+ }
41217
+ if( h1.isInit==0 ){
41218
+ return 1; /* Malformed header - probably all zeros */
41219
+ }
41220
+ walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
41221
+ if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
41222
+ return 1; /* Checksum does not match */
41223
+ }
41224
+
41225
+ if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
41226
+ *pChanged = 1;
41227
+ memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
41228
+ pWal->szPage = pWal->hdr.szPage;
41229
+ }
41230
+
41231
+ /* The header was successfully read. Return zero. */
41232
+ return 0;
41233
+}
41234
+
41235
+/*
41236
+** Read the wal-index header from the wal-index and into pWal->hdr.
41237
+** If the wal-header appears to be corrupt, try to recover the log
41238
+** before returning.
41239
+**
41240
+** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41241
+** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41242
+** to 0.
41243
+**
41244
+** This routine also maps the wal-index content into memory and assigns
41245
+** ownership of that mapping to the current thread. In some implementations,
41246
+** only one thread at a time can hold a mapping of the wal-index. Hence,
41247
+** the caller should strive to invoke walIndexUnmap() as soon as possible
41248
+** after this routine returns.
41249
+**
41250
+** If the wal-index header is successfully read, return SQLITE_OK.
41251
+** Otherwise an SQLite error code.
41252
+*/
41253
+static int walIndexReadHdr(Wal *pWal, int *pChanged){
41254
+ int rc; /* Return code */
41255
+ int badHdr; /* True if a header read failed */
41256
+ volatile u32 *page0;
41257
+
41258
+ /* Ensure that page 0 of the wal-index (the page that contains the
41259
+ ** wal-index header) is mapped. Return early if an error occurs here.
41260
+ */
41261
+ assert( pChanged );
41262
+ rc = walIndexPage(pWal, 0, &page0);
41263
+ if( rc!=SQLITE_OK ){
41264
+ return rc;
41265
+ };
41266
+ assert( page0 || pWal->writeLock==0 );
41267
+
41268
+ /* If the first page of the wal-index has been mapped, try to read the
41269
+ ** wal-index header immediately, without holding any lock. This usually
41270
+ ** works, but may fail if the wal-index header is corrupt or currently
41271
+ ** being modified by another user.
41272
+ */
41273
+ badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41274
+
41275
+ /* If the first attempt failed, it might have been due to a race
41276
+ ** with a writer. So get a WRITE lock and try again.
41277
+ */
41278
+ assert( badHdr==0 || pWal->writeLock==0 );
41279
+ if( badHdr && SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
41280
+ pWal->writeLock = 1;
41281
+ if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
41282
+ badHdr = walIndexTryHdr(pWal, pChanged);
41283
+ if( badHdr ){
41284
+ /* If the wal-index header is still malformed even while holding
41285
+ ** a WRITE lock, it can only mean that the header is corrupted and
41286
+ ** needs to be reconstructed. So run recovery to do exactly that.
41287
+ */
41288
+ rc = walIndexRecover(pWal);
41289
+ *pChanged = 1;
41290
+ }
41291
+ }
41292
+ pWal->writeLock = 0;
41293
+ walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41294
+ }
41295
+
41296
+ return rc;
41297
+}
41298
+
41299
+/*
41300
+** This is the value that walTryBeginRead returns when it needs to
41301
+** be retried.
41302
+*/
41303
+#define WAL_RETRY (-1)
41304
+
41305
+/*
41306
+** Attempt to start a read transaction. This might fail due to a race or
41307
+** other transient condition. When that happens, it returns WAL_RETRY to
41308
+** indicate to the caller that it is safe to retry immediately.
41309
+**
41310
+** On success return SQLITE_OK. On a permantent failure (such an
41311
+** I/O error or an SQLITE_BUSY because another process is running
41312
+** recovery) return a positive error code.
41313
+**
41314
+** On success, this routine obtains a read lock on
41315
+** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41316
+** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41317
+** that means the Wal does not hold any read lock. The reader must not
41318
+** access any database page that is modified by a WAL frame up to and
41319
+** including frame number aReadMark[pWal->readLock]. The reader will
41320
+** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41321
+** Or if pWal->readLock==0, then the reader will ignore the WAL
41322
+** completely and get all content directly from the database file.
41323
+** When the read transaction is completed, the caller must release the
41324
+** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41325
+**
41326
+** This routine uses the nBackfill and aReadMark[] fields of the header
41327
+** to select a particular WAL_READ_LOCK() that strives to let the
41328
+** checkpoint process do as much work as possible. This routine might
41329
+** update values of the aReadMark[] array in the header, but if it does
41330
+** so it takes care to hold an exclusive lock on the corresponding
41331
+** WAL_READ_LOCK() while changing values.
41332
+*/
41333
+static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
41334
+ volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
41335
+ u32 mxReadMark; /* Largest aReadMark[] value */
41336
+ int mxI; /* Index of largest aReadMark[] value */
41337
+ int i; /* Loop counter */
41338
+ int rc = SQLITE_OK; /* Return code */
41339
+
41340
+ assert( pWal->readLock<0 ); /* Not currently locked */
41341
+
41342
+ /* Take steps to avoid spinning forever if there is a protocol error. */
41343
+ if( cnt>5 ){
41344
+ if( cnt>100 ) return SQLITE_PROTOCOL;
41345
+ sqlite3OsSleep(pWal->pVfs, 1);
41346
+ }
41347
+
41348
+ if( !useWal ){
41349
+ rc = walIndexReadHdr(pWal, pChanged);
41350
+ if( rc==SQLITE_BUSY ){
41351
+ /* If there is not a recovery running in another thread or process
41352
+ ** then convert BUSY errors to WAL_RETRY. If recovery is known to
41353
+ ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
41354
+ ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
41355
+ ** would be technically correct. But the race is benign since with
41356
+ ** WAL_RETRY this routine will be called again and will probably be
41357
+ ** right on the second iteration.
41358
+ */
41359
+ rc = walLockShared(pWal, WAL_RECOVER_LOCK);
41360
+ if( rc==SQLITE_OK ){
41361
+ walUnlockShared(pWal, WAL_RECOVER_LOCK);
41362
+ rc = WAL_RETRY;
41363
+ }else if( rc==SQLITE_BUSY ){
41364
+ rc = SQLITE_BUSY_RECOVERY;
41365
+ }
41366
+ }
41367
+ }
41368
+ if( rc!=SQLITE_OK ){
41369
+ return rc;
41370
+ }
41371
+
41372
+ pInfo = walCkptInfo(pWal);
41373
+ if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41374
+ /* The WAL has been completely backfilled (or it is empty).
41375
+ ** and can be safely ignored.
41376
+ */
41377
+ rc = walLockShared(pWal, WAL_READ_LOCK(0));
41378
+ sqlite3OsShmBarrier(pWal->pDbFd);
41379
+ if( rc==SQLITE_OK ){
41380
+ if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
41381
+ /* It is not safe to allow the reader to continue here if frames
41382
+ ** may have been appended to the log before READ_LOCK(0) was obtained.
41383
+ ** When holding READ_LOCK(0), the reader ignores the entire log file,
41384
+ ** which implies that the database file contains a trustworthy
41385
+ ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
41386
+ ** happening, this is usually correct.
41387
+ **
41388
+ ** However, if frames have been appended to the log (or if the log
41389
+ ** is wrapped and written for that matter) before the READ_LOCK(0)
41390
+ ** is obtained, that is not necessarily true. A checkpointer may
41391
+ ** have started to backfill the appended frames but crashed before
41392
+ ** it finished. Leaving a corrupt image in the database file.
41393
+ */
41394
+ walUnlockShared(pWal, WAL_READ_LOCK(0));
41395
+ return WAL_RETRY;
41396
+ }
41397
+ pWal->readLock = 0;
41398
+ return SQLITE_OK;
41399
+ }else if( rc!=SQLITE_BUSY ){
41400
+ return rc;
41401
+ }
41402
+ }
41403
+
41404
+ /* If we get this far, it means that the reader will want to use
41405
+ ** the WAL to get at content from recent commits. The job now is
41406
+ ** to select one of the aReadMark[] entries that is closest to
41407
+ ** but not exceeding pWal->hdr.mxFrame and lock that entry.
41408
+ */
41409
+ mxReadMark = 0;
41410
+ mxI = 0;
41411
+ for(i=1; i<WAL_NREADER; i++){
41412
+ u32 thisMark = pInfo->aReadMark[i];
41413
+ if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
41414
+ assert( thisMark!=READMARK_NOT_USED );
41415
+ mxReadMark = thisMark;
41416
+ mxI = i;
41417
+ }
41418
+ }
41419
+ if( mxI==0 ){
41420
+ /* If we get here, it means that all of the aReadMark[] entries between
41421
+ ** 1 and WAL_NREADER-1 are zero. Try to initialize aReadMark[1] to
41422
+ ** be mxFrame, then retry.
41423
+ */
41424
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(1), 1);
41425
+ if( rc==SQLITE_OK ){
41426
+ pInfo->aReadMark[1] = pWal->hdr.mxFrame;
41427
+ walUnlockExclusive(pWal, WAL_READ_LOCK(1), 1);
41428
+ rc = WAL_RETRY;
41429
+ }else if( rc==SQLITE_BUSY ){
41430
+ rc = WAL_RETRY;
41431
+ }
41432
+ return rc;
41433
+ }else{
41434
+ if( mxReadMark < pWal->hdr.mxFrame ){
41435
+ for(i=1; i<WAL_NREADER; i++){
41436
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
41437
+ if( rc==SQLITE_OK ){
41438
+ mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
41439
+ mxI = i;
41440
+ walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
41441
+ break;
41442
+ }else if( rc!=SQLITE_BUSY ){
41443
+ return rc;
41444
+ }
41445
+ }
41446
+ }
41447
+
41448
+ rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
41449
+ if( rc ){
41450
+ return rc==SQLITE_BUSY ? WAL_RETRY : rc;
41451
+ }
41452
+ /* Now that the read-lock has been obtained, check that neither the
41453
+ ** value in the aReadMark[] array or the contents of the wal-index
41454
+ ** header have changed.
41455
+ **
41456
+ ** It is necessary to check that the wal-index header did not change
41457
+ ** between the time it was read and when the shared-lock was obtained
41458
+ ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
41459
+ ** that the log file may have been wrapped by a writer, or that frames
41460
+ ** that occur later in the log than pWal->hdr.mxFrame may have been
41461
+ ** copied into the database by a checkpointer. If either of these things
41462
+ ** happened, then reading the database with the current value of
41463
+ ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
41464
+ ** instead.
41465
+ **
41466
+ ** This does not guarantee that the copy of the wal-index header is up to
41467
+ ** date before proceeding. That would not be possible without somehow
41468
+ ** blocking writers. It only guarantees that a dangerous checkpoint or
41469
+ ** log-wrap (either of which would require an exclusive lock on
41470
+ ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
41471
+ */
41472
+ sqlite3OsShmBarrier(pWal->pDbFd);
41473
+ if( pInfo->aReadMark[mxI]!=mxReadMark
41474
+ || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
41475
+ ){
41476
+ walUnlockShared(pWal, WAL_READ_LOCK(mxI));
41477
+ return WAL_RETRY;
41478
+ }else{
41479
+ assert( mxReadMark<=pWal->hdr.mxFrame );
41480
+ pWal->readLock = mxI;
41481
+ }
41482
+ }
41483
+ return rc;
41484
+}
41485
+
41486
+/*
41487
+** Begin a read transaction on the database.
41488
+**
41489
+** This routine used to be called sqlite3OpenSnapshot() and with good reason:
41490
+** it takes a snapshot of the state of the WAL and wal-index for the current
41491
+** instant in time. The current thread will continue to use this snapshot.
41492
+** Other threads might append new content to the WAL and wal-index but
41493
+** that extra content is ignored by the current thread.
41494
+**
41495
+** If the database contents have changes since the previous read
41496
+** transaction, then *pChanged is set to 1 before returning. The
41497
+** Pager layer will use this to know that is cache is stale and
41498
+** needs to be flushed.
41499
+*/
41500
+SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
41501
+ int rc; /* Return code */
41502
+ int cnt = 0; /* Number of TryBeginRead attempts */
41503
+
41504
+ do{
41505
+ rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
41506
+ }while( rc==WAL_RETRY );
41507
+ return rc;
41508
+}
41509
+
41510
+/*
41511
+** Finish with a read transaction. All this does is release the
41512
+** read-lock.
41513
+*/
41514
+SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
41515
+ if( pWal->readLock>=0 ){
41516
+ walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
41517
+ pWal->readLock = -1;
41518
+ }
41519
+}
41520
+
41521
+/*
41522
+** Read a page from the WAL, if it is present in the WAL and if the
41523
+** current read transaction is configured to use the WAL.
41524
+**
41525
+** The *pInWal is set to 1 if the requested page is in the WAL and
41526
+** has been loaded. Or *pInWal is set to 0 if the page was not in
41527
+** the WAL and needs to be read out of the database.
41528
+*/
41529
+SQLITE_PRIVATE int sqlite3WalRead(
41530
+ Wal *pWal, /* WAL handle */
41531
+ Pgno pgno, /* Database page number to read data for */
41532
+ int *pInWal, /* OUT: True if data is read from WAL */
41533
+ int nOut, /* Size of buffer pOut in bytes */
41534
+ u8 *pOut /* Buffer to write page data to */
41535
+){
41536
+ u32 iRead = 0; /* If !=0, WAL frame to return data from */
41537
+ u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
41538
+ int iHash; /* Used to loop through N hash tables */
41539
+
41540
+ /* This routine is only be called from within a read transaction. */
41541
+ assert( pWal->readLock>=0 || pWal->lockError );
41542
+
41543
+ /* If the "last page" field of the wal-index header snapshot is 0, then
41544
+ ** no data will be read from the wal under any circumstances. Return early
41545
+ ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41546
+ ** pWal->readLock==0, then the WAL is ignored by the reader so
41547
+ ** return early, as if the WAL were empty.
41548
+ */
41549
+ if( iLast==0 || pWal->readLock==0 ){
41550
+ *pInWal = 0;
41551
+ return SQLITE_OK;
41552
+ }
41553
+
41554
+ /* Search the hash table or tables for an entry matching page number
41555
+ ** pgno. Each iteration of the following for() loop searches one
41556
+ ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41557
+ **
41558
+ ** This code may run concurrently to the code in walIndexAppend()
41559
+ ** that adds entries to the wal-index (and possibly to this hash
41560
+ ** table). This means the value just read from the hash
41561
+ ** slot (aHash[iKey]) may have been added before or after the
41562
+ ** current read transaction was opened. Values added after the
41563
+ ** read transaction was opened may have been written incorrectly -
41564
+ ** i.e. these slots may contain garbage data. However, we assume
41565
+ ** that any slots written before the current read transaction was
41566
+ ** opened remain unmodified.
41567
+ **
41568
+ ** For the reasons above, the if(...) condition featured in the inner
41569
+ ** loop of the following block is more stringent that would be required
41570
+ ** if we had exclusive access to the hash-table:
41571
+ **
41572
+ ** (aPgno[iFrame]==pgno):
41573
+ ** This condition filters out normal hash-table collisions.
41574
+ **
41575
+ ** (iFrame<=iLast):
41576
+ ** This condition filters out entries that were added to the hash
41577
+ ** table after the current read-transaction had started.
41578
+ */
41579
+ for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
41580
+ volatile ht_slot *aHash; /* Pointer to hash table */
41581
+ volatile u32 *aPgno; /* Pointer to array of page numbers */
41582
+ u32 iZero; /* Frame number corresponding to aPgno[0] */
41583
+ int iKey; /* Hash slot index */
41584
+ int rc;
41585
+
41586
+ rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
41587
+ if( rc!=SQLITE_OK ){
41588
+ return rc;
41589
+ }
41590
+ for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
41591
+ u32 iFrame = aHash[iKey] + iZero;
41592
+ if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
41593
+ assert( iFrame>iRead );
41594
+ iRead = iFrame;
41595
+ }
41596
+ }
41597
+ }
41598
+
41599
+#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
41600
+ /* If expensive assert() statements are available, do a linear search
41601
+ ** of the wal-index file content. Make sure the results agree with the
41602
+ ** result obtained using the hash indexes above. */
41603
+ {
41604
+ u32 iRead2 = 0;
41605
+ u32 iTest;
41606
+ for(iTest=iLast; iTest>0; iTest--){
41607
+ if( walFramePgno(pWal, iTest)==pgno ){
41608
+ iRead2 = iTest;
41609
+ break;
41610
+ }
41611
+ }
41612
+ assert( iRead==iRead2 );
41613
+ }
41614
+#endif
41615
+
41616
+ /* If iRead is non-zero, then it is the log frame number that contains the
41617
+ ** required page. Read and return data from the log file.
41618
+ */
41619
+ if( iRead ){
41620
+ i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE;
41621
+ *pInWal = 1;
41622
+ return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
41623
+ }
41624
+
41625
+ *pInWal = 0;
41626
+ return SQLITE_OK;
41627
+}
41628
+
41629
+
41630
+/*
41631
+** Set *pPgno to the size of the database file (or zero, if unknown).
41632
+*/
41633
+SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
41634
+ assert( pWal->readLock>=0 || pWal->lockError );
41635
+ *pPgno = pWal->hdr.nPage;
41636
+}
41637
+
41638
+
41639
+/*
41640
+** This function starts a write transaction on the WAL.
41641
+**
41642
+** A read transaction must have already been started by a prior call
41643
+** to sqlite3WalBeginReadTransaction().
41644
+**
41645
+** If another thread or process has written into the database since
41646
+** the read transaction was started, then it is not possible for this
41647
+** thread to write as doing so would cause a fork. So this routine
41648
+** returns SQLITE_BUSY in that case and no write transaction is started.
41649
+**
41650
+** There can only be a single writer active at a time.
41651
+*/
41652
+SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
41653
+ int rc;
41654
+
41655
+ /* Cannot start a write transaction without first holding a read
41656
+ ** transaction. */
41657
+ assert( pWal->readLock>=0 );
41658
+
41659
+ /* Only one writer allowed at a time. Get the write lock. Return
41660
+ ** SQLITE_BUSY if unable.
41661
+ */
41662
+ rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
41663
+ if( rc ){
41664
+ return rc;
41665
+ }
41666
+ pWal->writeLock = 1;
41667
+
41668
+ /* If another connection has written to the database file since the
41669
+ ** time the read transaction on this connection was started, then
41670
+ ** the write is disallowed.
41671
+ */
41672
+ if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
41673
+ walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41674
+ pWal->writeLock = 0;
41675
+ rc = SQLITE_BUSY;
41676
+ }
41677
+
41678
+ return rc;
41679
+}
41680
+
41681
+/*
41682
+** End a write transaction. The commit has already been done. This
41683
+** routine merely releases the lock.
41684
+*/
41685
+SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
41686
+ walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41687
+ pWal->writeLock = 0;
41688
+ return SQLITE_OK;
41689
+}
41690
+
41691
+/*
41692
+** If any data has been written (but not committed) to the log file, this
41693
+** function moves the write-pointer back to the start of the transaction.
41694
+**
41695
+** Additionally, the callback function is invoked for each frame written
41696
+** to the WAL since the start of the transaction. If the callback returns
41697
+** other than SQLITE_OK, it is not invoked again and the error code is
41698
+** returned to the caller.
41699
+**
41700
+** Otherwise, if the callback function does not return an error, this
41701
+** function returns SQLITE_OK.
41702
+*/
41703
+SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
41704
+ int rc = SQLITE_OK;
41705
+ if( pWal->writeLock ){
41706
+ Pgno iMax = pWal->hdr.mxFrame;
41707
+ Pgno iFrame;
41708
+
41709
+ /* Restore the clients cache of the wal-index header to the state it
41710
+ ** was in before the client began writing to the database.
41711
+ */
41712
+ memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
41713
+
41714
+ for(iFrame=pWal->hdr.mxFrame+1;
41715
+ ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
41716
+ iFrame++
41717
+ ){
41718
+ /* This call cannot fail. Unless the page for which the page number
41719
+ ** is passed as the second argument is (a) in the cache and
41720
+ ** (b) has an outstanding reference, then xUndo is either a no-op
41721
+ ** (if (a) is false) or simply expels the page from the cache (if (b)
41722
+ ** is false).
41723
+ **
41724
+ ** If the upper layer is doing a rollback, it is guaranteed that there
41725
+ ** are no outstanding references to any page other than page 1. And
41726
+ ** page 1 is never written to the log until the transaction is
41727
+ ** committed. As a result, the call to xUndo may not fail.
41728
+ */
41729
+ assert( walFramePgno(pWal, iFrame)!=1 );
41730
+ rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
41731
+ }
41732
+ walCleanupHash(pWal);
41733
+ }
41734
+ assert( rc==SQLITE_OK );
41735
+ return rc;
41736
+}
41737
+
41738
+/*
41739
+** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
41740
+** values. This function populates the array with values required to
41741
+** "rollback" the write position of the WAL handle back to the current
41742
+** point in the event of a savepoint rollback (via WalSavepointUndo()).
41743
+*/
41744
+SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
41745
+ assert( pWal->writeLock );
41746
+ aWalData[0] = pWal->hdr.mxFrame;
41747
+ aWalData[1] = pWal->hdr.aFrameCksum[0];
41748
+ aWalData[2] = pWal->hdr.aFrameCksum[1];
41749
+ aWalData[3] = pWal->nCkpt;
41750
+}
41751
+
41752
+/*
41753
+** Move the write position of the WAL back to the point identified by
41754
+** the values in the aWalData[] array. aWalData must point to an array
41755
+** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
41756
+** by a call to WalSavepoint().
41757
+*/
41758
+SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
41759
+ int rc = SQLITE_OK;
41760
+
41761
+ assert( pWal->writeLock );
41762
+ assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
41763
+
41764
+ if( aWalData[3]!=pWal->nCkpt ){
41765
+ /* This savepoint was opened immediately after the write-transaction
41766
+ ** was started. Right after that, the writer decided to wrap around
41767
+ ** to the start of the log. Update the savepoint values to match.
41768
+ */
41769
+ aWalData[0] = 0;
41770
+ aWalData[3] = pWal->nCkpt;
41771
+ }
41772
+
41773
+ if( aWalData[0]<pWal->hdr.mxFrame ){
41774
+ pWal->hdr.mxFrame = aWalData[0];
41775
+ pWal->hdr.aFrameCksum[0] = aWalData[1];
41776
+ pWal->hdr.aFrameCksum[1] = aWalData[2];
41777
+ walCleanupHash(pWal);
41778
+ }
41779
+
41780
+ return rc;
41781
+}
41782
+
41783
+/*
41784
+** This function is called just before writing a set of frames to the log
41785
+** file (see sqlite3WalFrames()). It checks to see if, instead of appending
41786
+** to the current log file, it is possible to overwrite the start of the
41787
+** existing log file with the new frames (i.e. "reset" the log). If so,
41788
+** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
41789
+** unchanged.
41790
+**
41791
+** SQLITE_OK is returned if no error is encountered (regardless of whether
41792
+** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
41793
+** if some error
41794
+*/
41795
+static int walRestartLog(Wal *pWal){
41796
+ int rc = SQLITE_OK;
41797
+ int cnt;
41798
+
41799
+ if( pWal->readLock==0 ){
41800
+ volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
41801
+ assert( pInfo->nBackfill==pWal->hdr.mxFrame );
41802
+ if( pInfo->nBackfill>0 ){
41803
+ rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
41804
+ if( rc==SQLITE_OK ){
41805
+ /* If all readers are using WAL_READ_LOCK(0) (in other words if no
41806
+ ** readers are currently using the WAL), then the transactions
41807
+ ** frames will overwrite the start of the existing log. Update the
41808
+ ** wal-index header to reflect this.
41809
+ **
41810
+ ** In theory it would be Ok to update the cache of the header only
41811
+ ** at this point. But updating the actual wal-index header is also
41812
+ ** safe and means there is no special case for sqlite3WalUndo()
41813
+ ** to handle if this transaction is rolled back.
41814
+ */
41815
+ int i; /* Loop counter */
41816
+ u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
41817
+ pWal->nCkpt++;
41818
+ pWal->hdr.mxFrame = 0;
41819
+ sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
41820
+ sqlite3_randomness(4, &aSalt[1]);
41821
+ walIndexWriteHdr(pWal);
41822
+ pInfo->nBackfill = 0;
41823
+ for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
41824
+ assert( pInfo->aReadMark[0]==0 );
41825
+ walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
41826
+ }
41827
+ }
41828
+ walUnlockShared(pWal, WAL_READ_LOCK(0));
41829
+ pWal->readLock = -1;
41830
+ cnt = 0;
41831
+ do{
41832
+ int notUsed;
41833
+ rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
41834
+ }while( rc==WAL_RETRY );
41835
+ }
41836
+ return rc;
41837
+}
41838
+
41839
+/*
41840
+** Write a set of frames to the log. The caller must hold the write-lock
41841
+** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
41842
+*/
41843
+SQLITE_PRIVATE int sqlite3WalFrames(
41844
+ Wal *pWal, /* Wal handle to write to */
41845
+ int szPage, /* Database page-size in bytes */
41846
+ PgHdr *pList, /* List of dirty pages to write */
41847
+ Pgno nTruncate, /* Database size after this commit */
41848
+ int isCommit, /* True if this is a commit */
41849
+ int sync_flags /* Flags to pass to OsSync() (or 0) */
41850
+){
41851
+ int rc; /* Used to catch return codes */
41852
+ u32 iFrame; /* Next frame address */
41853
+ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
41854
+ PgHdr *p; /* Iterator to run through pList with. */
41855
+ PgHdr *pLast = 0; /* Last frame in list */
41856
+ int nLast = 0; /* Number of extra copies of last page */
41857
+
41858
+ assert( pList );
41859
+ assert( pWal->writeLock );
41860
+
41861
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
41862
+ { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
41863
+ WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
41864
+ pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
41865
+ }
41866
+#endif
41867
+
41868
+ /* See if it is possible to write these frames into the start of the
41869
+ ** log file, instead of appending to it at pWal->hdr.mxFrame.
41870
+ */
41871
+ if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
41872
+ return rc;
41873
+ }
41874
+
41875
+ /* If this is the first frame written into the log, write the WAL
41876
+ ** header to the start of the WAL file. See comments at the top of
41877
+ ** this source file for a description of the WAL header format.
41878
+ */
41879
+ iFrame = pWal->hdr.mxFrame;
41880
+ if( iFrame==0 ){
41881
+ u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
41882
+ sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41883
+ sqlite3Put4byte(&aWalHdr[4], 3007000);
41884
+ sqlite3Put4byte(&aWalHdr[8], szPage);
41885
+ pWal->szPage = szPage;
41886
+ pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
41887
+ sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
41888
+ memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
41889
+ rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
41890
+ WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
41891
+ if( rc!=SQLITE_OK ){
41892
+ return rc;
41893
+ }
41894
+ walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
41895
+ }
41896
+ assert( pWal->szPage==szPage );
41897
+
41898
+ /* Write the log file. */
41899
+ for(p=pList; p; p=p->pDirty){
41900
+ u32 nDbsize; /* Db-size field for frame header */
41901
+ i64 iOffset; /* Write offset in log file */
41902
+
41903
+ iOffset = walFrameOffset(++iFrame, szPage);
41904
+
41905
+ /* Populate and write the frame header */
41906
+ nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
41907
+ walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame);
41908
+ rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41909
+ if( rc!=SQLITE_OK ){
41910
+ return rc;
41911
+ }
41912
+
41913
+ /* Write the page data */
41914
+ rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame));
41915
+ if( rc!=SQLITE_OK ){
41916
+ return rc;
41917
+ }
41918
+ pLast = p;
41919
+ }
41920
+
41921
+ /* Sync the log file if the 'isSync' flag was specified. */
41922
+ if( sync_flags ){
41923
+ i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
41924
+ i64 iOffset = walFrameOffset(iFrame+1, szPage);
41925
+
41926
+ assert( isCommit );
41927
+ assert( iSegment>0 );
41928
+
41929
+ iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
41930
+ while( iOffset<iSegment ){
41931
+ walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame);
41932
+ rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41933
+ if( rc!=SQLITE_OK ){
41934
+ return rc;
41935
+ }
41936
+
41937
+ iOffset += WAL_FRAME_HDRSIZE;
41938
+ rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset);
41939
+ if( rc!=SQLITE_OK ){
41940
+ return rc;
41941
+ }
41942
+ nLast++;
41943
+ iOffset += szPage;
41944
+ }
41945
+
41946
+ rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
41947
+ }
41948
+
41949
+ /* Append data to the wal-index. It is not necessary to lock the
41950
+ ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
41951
+ ** guarantees that there are no other writers, and no data that may
41952
+ ** be in use by existing readers is being overwritten.
41953
+ */
41954
+ iFrame = pWal->hdr.mxFrame;
41955
+ for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
41956
+ iFrame++;
41957
+ rc = walIndexAppend(pWal, iFrame, p->pgno);
41958
+ }
41959
+ while( nLast>0 && rc==SQLITE_OK ){
41960
+ iFrame++;
41961
+ nLast--;
41962
+ rc = walIndexAppend(pWal, iFrame, pLast->pgno);
41963
+ }
41964
+
41965
+ if( rc==SQLITE_OK ){
41966
+ /* Update the private copy of the header. */
41967
+ pWal->hdr.szPage = szPage;
41968
+ pWal->hdr.mxFrame = iFrame;
41969
+ if( isCommit ){
41970
+ pWal->hdr.iChange++;
41971
+ pWal->hdr.nPage = nTruncate;
41972
+ }
41973
+ /* If this is a commit, update the wal-index header too. */
41974
+ if( isCommit ){
41975
+ walIndexWriteHdr(pWal);
41976
+ pWal->iCallback = iFrame;
41977
+ }
41978
+ }
41979
+
41980
+ WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
41981
+ return rc;
41982
+}
41983
+
41984
+/*
41985
+** This routine is called to implement sqlite3_wal_checkpoint() and
41986
+** related interfaces.
41987
+**
41988
+** Obtain a CHECKPOINT lock and then backfill as much information as
41989
+** we can from WAL into the database.
41990
+*/
41991
+SQLITE_PRIVATE int sqlite3WalCheckpoint(
41992
+ Wal *pWal, /* Wal connection */
41993
+ int sync_flags, /* Flags to sync db file with (or 0) */
41994
+ int nBuf, /* Size of temporary buffer */
41995
+ u8 *zBuf /* Temporary buffer to use */
41996
+){
41997
+ int rc; /* Return code */
41998
+ int isChanged = 0; /* True if a new wal-index header is loaded */
41999
+
42000
+ assert( pWal->ckptLock==0 );
42001
+
42002
+ WALTRACE(("WAL%p: checkpoint begins\n", pWal));
42003
+ rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
42004
+ if( rc ){
42005
+ /* Usually this is SQLITE_BUSY meaning that another thread or process
42006
+ ** is already running a checkpoint, or maybe a recovery. But it might
42007
+ ** also be SQLITE_IOERR. */
42008
+ return rc;
42009
+ }
42010
+ pWal->ckptLock = 1;
42011
+
42012
+ /* Copy data from the log to the database file. */
42013
+ rc = walIndexReadHdr(pWal, &isChanged);
42014
+ if( rc==SQLITE_OK ){
42015
+ rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf);
42016
+ }
42017
+ if( isChanged ){
42018
+ /* If a new wal-index header was loaded before the checkpoint was
42019
+ ** performed, then the pager-cache associated with pWal is now
42020
+ ** out of date. So zero the cached wal-index header to ensure that
42021
+ ** next time the pager opens a snapshot on this database it knows that
42022
+ ** the cache needs to be reset.
42023
+ */
42024
+ memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
42025
+ }
42026
+
42027
+ /* Release the locks. */
42028
+ walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
42029
+ pWal->ckptLock = 0;
42030
+ WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
42031
+ return rc;
42032
+}
42033
+
42034
+/* Return the value to pass to a sqlite3_wal_hook callback, the
42035
+** number of frames in the WAL at the point of the last commit since
42036
+** sqlite3WalCallback() was called. If no commits have occurred since
42037
+** the last call, then return 0.
42038
+*/
42039
+SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
42040
+ u32 ret = 0;
42041
+ if( pWal ){
42042
+ ret = pWal->iCallback;
42043
+ pWal->iCallback = 0;
42044
+ }
42045
+ return (int)ret;
42046
+}
42047
+
42048
+/*
42049
+** This function is called to change the WAL subsystem into or out
42050
+** of locking_mode=EXCLUSIVE.
42051
+**
42052
+** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
42053
+** into locking_mode=NORMAL. This means that we must acquire a lock
42054
+** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
42055
+** or if the acquisition of the lock fails, then return 0. If the
42056
+** transition out of exclusive-mode is successful, return 1. This
42057
+** operation must occur while the pager is still holding the exclusive
42058
+** lock on the main database file.
42059
+**
42060
+** If op is one, then change from locking_mode=NORMAL into
42061
+** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
42062
+** be released. Return 1 if the transition is made and 0 if the
42063
+** WAL is already in exclusive-locking mode - meaning that this
42064
+** routine is a no-op. The pager must already hold the exclusive lock
42065
+** on the main database file before invoking this operation.
42066
+**
42067
+** If op is negative, then do a dry-run of the op==1 case but do
42068
+** not actually change anything. The pager uses this to see if it
42069
+** should acquire the database exclusive lock prior to invoking
42070
+** the op==1 case.
42071
+*/
42072
+SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
42073
+ int rc;
42074
+ assert( pWal->writeLock==0 );
42075
+
42076
+ /* pWal->readLock is usually set, but might be -1 if there was a
42077
+ ** prior error while attempting to acquire are read-lock. This cannot
42078
+ ** happen if the connection is actually in exclusive mode (as no xShmLock
42079
+ ** locks are taken in this case). Nor should the pager attempt to
42080
+ ** upgrade to exclusive-mode following such an error.
42081
+ */
42082
+ assert( pWal->readLock>=0 || pWal->lockError );
42083
+ assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
42084
+
42085
+ if( op==0 ){
42086
+ if( pWal->exclusiveMode ){
42087
+ pWal->exclusiveMode = 0;
42088
+ if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
42089
+ pWal->exclusiveMode = 1;
42090
+ }
42091
+ rc = pWal->exclusiveMode==0;
42092
+ }else{
42093
+ /* Already in locking_mode=NORMAL */
42094
+ rc = 0;
42095
+ }
42096
+ }else if( op>0 ){
42097
+ assert( pWal->exclusiveMode==0 );
42098
+ assert( pWal->readLock>=0 );
42099
+ walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
42100
+ pWal->exclusiveMode = 1;
42101
+ rc = 1;
42102
+ }else{
42103
+ rc = pWal->exclusiveMode==0;
42104
+ }
42105
+ return rc;
42106
+}
42107
+
42108
+#endif /* #ifndef SQLITE_OMIT_WAL */
42109
+
42110
+/************** End of wal.c *************************************************/
3784242111
/************** Begin file btmutex.c *****************************************/
3784342112
/*
3784442113
** 2007 August 27
3784542114
**
3784642115
** The author disclaims copyright to this source code. In place of
@@ -38278,10 +42547,11 @@
3827842547
u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
3827942548
u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
3828042549
u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
3828142550
u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
3828242551
u8 inTransaction; /* Transaction state */
42552
+ u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
3828342553
int nTransaction; /* Number of open transactions (read + write) */
3828442554
u32 nPage; /* Number of pages in the database */
3828542555
void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
3828642556
void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
3828742557
sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -41094,11 +45364,11 @@
4109445364
*/
4109545365
nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
4109645366
if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){;
4109745367
goto page1_init_failed;
4109845368
}
41099
- if( nPage==0 ){
45369
+ if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
4110045370
nPage = nPageFile;
4110145371
}
4110245372
if( nPage>0 ){
4110345373
int pageSize;
4110445374
int usableSize;
@@ -41105,16 +45375,46 @@
4110545375
u8 *page1 = pPage1->aData;
4110645376
rc = SQLITE_NOTADB;
4110745377
if( memcmp(page1, zMagicHeader, 16)!=0 ){
4110845378
goto page1_init_failed;
4110945379
}
45380
+
45381
+#ifdef SQLITE_OMIT_WAL
4111045382
if( page1[18]>1 ){
4111145383
pBt->readOnly = 1;
4111245384
}
4111345385
if( page1[19]>1 ){
4111445386
goto page1_init_failed;
4111545387
}
45388
+#else
45389
+ if( page1[18]>2 ){
45390
+ pBt->readOnly = 1;
45391
+ }
45392
+ if( page1[19]>2 ){
45393
+ goto page1_init_failed;
45394
+ }
45395
+
45396
+ /* If the write version is set to 2, this database should be accessed
45397
+ ** in WAL mode. If the log is not already open, open it now. Then
45398
+ ** return SQLITE_OK and return without populating BtShared.pPage1.
45399
+ ** The caller detects this and calls this function again. This is
45400
+ ** required as the version of page 1 currently in the page1 buffer
45401
+ ** may not be the latest version - there may be a newer one in the log
45402
+ ** file.
45403
+ */
45404
+ if( page1[19]==2 && pBt->doNotUseWAL==0 ){
45405
+ int isOpen = 0;
45406
+ rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
45407
+ if( rc!=SQLITE_OK ){
45408
+ goto page1_init_failed;
45409
+ }else if( isOpen==0 ){
45410
+ releasePage(pPage1);
45411
+ return SQLITE_OK;
45412
+ }
45413
+ rc = SQLITE_NOTADB;
45414
+ }
45415
+#endif
4111645416
4111745417
/* The maximum embedded fraction must be exactly 25%. And the minimum
4111845418
** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
4111945419
** The original design allowed these amounts to vary, but as of
4112045420
** version 3.6.0, we require them to be fixed.
@@ -41360,11 +45660,11 @@
4136045660
}
4136145661
4136245662
if( rc!=SQLITE_OK ){
4136345663
unlockBtreeIfUnused(pBt);
4136445664
}
41365
- }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
45665
+ }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
4136645666
btreeInvokeBusyHandler(pBt) );
4136745667
4136845668
if( rc==SQLITE_OK ){
4136945669
if( p->inTrans==TRANS_NONE ){
4137045670
pBt->nTransaction++;
@@ -46807,10 +51107,46 @@
4680751107
assert(!pCur->isIncrblobHandle);
4680851108
assert(!pCur->aOverflow);
4680951109
pCur->isIncrblobHandle = 1;
4681051110
}
4681151111
#endif
51112
+
51113
+/*
51114
+** Set both the "read version" (single byte at byte offset 18) and
51115
+** "write version" (single byte at byte offset 19) fields in the database
51116
+** header to iVersion.
51117
+*/
51118
+SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
51119
+ BtShared *pBt = pBtree->pBt;
51120
+ int rc; /* Return code */
51121
+
51122
+ assert( pBtree->inTrans==TRANS_NONE );
51123
+ assert( iVersion==1 || iVersion==2 );
51124
+
51125
+ /* If setting the version fields to 1, do not automatically open the
51126
+ ** WAL connection, even if the version fields are currently set to 2.
51127
+ */
51128
+ pBt->doNotUseWAL = (iVersion==1);
51129
+
51130
+ rc = sqlite3BtreeBeginTrans(pBtree, 0);
51131
+ if( rc==SQLITE_OK ){
51132
+ u8 *aData = pBt->pPage1->aData;
51133
+ if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
51134
+ rc = sqlite3BtreeBeginTrans(pBtree, 2);
51135
+ if( rc==SQLITE_OK ){
51136
+ rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51137
+ if( rc==SQLITE_OK ){
51138
+ aData[18] = (u8)iVersion;
51139
+ aData[19] = (u8)iVersion;
51140
+ }
51141
+ }
51142
+ }
51143
+ }
51144
+
51145
+ pBt->doNotUseWAL = 0;
51146
+ return rc;
51147
+}
4681251148
4681351149
/************** End of btree.c ***********************************************/
4681451150
/************** Begin file backup.c ******************************************/
4681551151
/*
4681651152
** 2009 January 28
@@ -47027,11 +51363,11 @@
4702751363
assert( zSrcData );
4702851364
4702951365
/* Catch the case where the destination is an in-memory database and the
4703051366
** page sizes of the source and destination differ.
4703151367
*/
47032
- if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(sqlite3BtreePager(p->pDest)) ){
51368
+ if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
4703351369
rc = SQLITE_READONLY;
4703451370
}
4703551371
4703651372
/* This loop runs once for each destination page spanned by the source
4703751373
** page. For each iteration, variable iOff is set to the byte offset
@@ -47097,10 +51433,13 @@
4709751433
/*
4709851434
** Copy nPage pages from the source b-tree to the destination.
4709951435
*/
4710051436
SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
4710151437
int rc;
51438
+ int destMode; /* Destination journal mode */
51439
+ int pgszSrc = 0; /* Source page size */
51440
+ int pgszDest = 0; /* Destination page size */
4710251441
4710351442
sqlite3_mutex_enter(p->pSrcDb->mutex);
4710451443
sqlite3BtreeEnter(p->pSrc);
4710551444
if( p->pDestDb ){
4710651445
sqlite3_mutex_enter(p->pDestDb->mutex);
@@ -47137,10 +51476,19 @@
4713751476
*/
4713851477
if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
4713951478
rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
4714051479
bCloseTrans = 1;
4714151480
}
51481
+
51482
+ /* Do not allow backup if the destination database is in WAL mode
51483
+ ** and the page sizes are different between source and destination */
51484
+ pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
51485
+ pgszDest = sqlite3BtreeGetPageSize(p->pDest);
51486
+ destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
51487
+ if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
51488
+ rc = SQLITE_READONLY;
51489
+ }
4714251490
4714351491
/* Now that there is a read-lock on the source database, query the
4714451492
** source pager for the number of pages in the database.
4714551493
*/
4714651494
nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
@@ -47173,12 +51521,10 @@
4717351521
** same schema version.
4717451522
*/
4717551523
if( rc==SQLITE_DONE
4717651524
&& (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
4717751525
){
47178
- const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc);
47179
- const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest);
4718051526
int nDestTruncate;
4718151527
4718251528
if( p->pDestDb ){
4718351529
sqlite3ResetInternalSchema(p->pDestDb, 0);
4718451530
}
@@ -47193,56 +51539,58 @@
4719351539
** sqlite3PagerTruncateImage() here so that any pages in the
4719451540
** destination file that lie beyond the nDestTruncate page mark are
4719551541
** journalled by PagerCommitPhaseOne() before they are destroyed
4719651542
** by the file truncation.
4719751543
*/
47198
- if( nSrcPagesize<nDestPagesize ){
47199
- int ratio = nDestPagesize/nSrcPagesize;
51544
+ assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
51545
+ assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
51546
+ if( pgszSrc<pgszDest ){
51547
+ int ratio = pgszDest/pgszSrc;
4720051548
nDestTruncate = (nSrcPage+ratio-1)/ratio;
4720151549
if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
4720251550
nDestTruncate--;
4720351551
}
4720451552
}else{
47205
- nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize);
51553
+ nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
4720651554
}
4720751555
sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
4720851556
47209
- if( nSrcPagesize<nDestPagesize ){
51557
+ if( pgszSrc<pgszDest ){
4721051558
/* If the source page-size is smaller than the destination page-size,
4721151559
** two extra things may need to happen:
4721251560
**
4721351561
** * The destination may need to be truncated, and
4721451562
**
4721551563
** * Data stored on the pages immediately following the
4721651564
** pending-byte page in the source database may need to be
4721751565
** copied into the destination database.
4721851566
*/
47219
- const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage;
51567
+ const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
4722051568
sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
4722151569
4722251570
assert( pFile );
47223
- assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || (
51571
+ assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
4722451572
nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
47225
- && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize
51573
+ && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
4722651574
));
4722751575
if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
4722851576
&& SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
4722951577
&& SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
4723051578
){
4723151579
i64 iOff;
47232
- i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize);
51580
+ i64 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
4723351581
for(
47234
- iOff=PENDING_BYTE+nSrcPagesize;
51582
+ iOff=PENDING_BYTE+pgszSrc;
4723551583
rc==SQLITE_OK && iOff<iEnd;
47236
- iOff+=nSrcPagesize
51584
+ iOff+=pgszSrc
4723751585
){
4723851586
PgHdr *pSrcPg = 0;
47239
- const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1);
51587
+ const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
4724051588
rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
4724151589
if( rc==SQLITE_OK ){
4724251590
u8 *zData = sqlite3PagerGetData(pSrcPg);
47243
- rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff);
51591
+ rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
4724451592
}
4724551593
sqlite3PagerUnref(pSrcPg);
4724651594
}
4724751595
}
4724851596
}else{
@@ -49366,13 +53714,16 @@
4936653714
** Return the opcode for a given address. If the address is -1, then
4936753715
** return the most recently inserted opcode.
4936853716
**
4936953717
** If a memory allocation error has occurred prior to the calling of this
4937053718
** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
49371
-** is readable and writable, but it has no effect. The return of a dummy
49372
-** opcode allows the call to continue functioning after a OOM fault without
49373
-** having to check to see if the return from this routine is a valid pointer.
53719
+** is readable but not writable, though it is cast to a writable value.
53720
+** The return of a dummy opcode allows the call to continue functioning
53721
+** after a OOM fault without having to check to see if the return from
53722
+** this routine is a valid pointer. But because the dummy.opcode is 0,
53723
+** dummy will never be written to. This is verified by code inspection and
53724
+** by running with Valgrind.
4937453725
**
4937553726
** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
4937653727
** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
4937753728
** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
4937853729
** a new VDBE is created. So we are free to set addr to p->nOp-1 without
@@ -49379,21 +53730,21 @@
4937953730
** having to double-check to make sure that the result is non-negative. But
4938053731
** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
4938153732
** check the value of p->nOp-1 before continuing.
4938253733
*/
4938353734
SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
49384
- static VdbeOp dummy;
53735
+ static const VdbeOp dummy;
4938553736
assert( p->magic==VDBE_MAGIC_INIT );
4938653737
if( addr<0 ){
4938753738
#ifdef SQLITE_OMIT_TRACE
49388
- if( p->nOp==0 ) return &dummy;
53739
+ if( p->nOp==0 ) return (VdbeOp*)&dummy;
4938953740
#endif
4939053741
addr = p->nOp - 1;
4939153742
}
4939253743
assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
4939353744
if( p->db->mallocFailed ){
49394
- return &dummy;
53745
+ return (VdbeOp*)&dummy;
4939553746
}else{
4939653747
return &p->aOp[addr];
4939753748
}
4939853749
}
4939953750
@@ -49502,10 +53853,15 @@
4950253853
}
4950353854
#endif
4950453855
4950553856
/*
4950653857
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
53858
+**
53859
+** The prepared statement has to know in advance which Btree objects
53860
+** will be used so that it can acquire mutexes on them all in sorted
53861
+** order (via sqlite3VdbeMutexArrayEnter(). Mutexes are acquired
53862
+** in order (and released in reverse order) to avoid deadlocks.
4950753863
*/
4950853864
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
4950953865
int mask;
4951053866
assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
4951153867
assert( i<(int)sizeof(p->btreeMask)*8 );
@@ -50001,10 +54357,11 @@
5000154357
p->magic = VDBE_MAGIC_RUN;
5000254358
p->nChange = 0;
5000354359
p->cacheCtr = 1;
5000454360
p->minWriteFileFormat = 255;
5000554361
p->iStatement = 0;
54362
+ p->nFkConstraint = 0;
5000654363
#ifdef VDBE_PROFILE
5000754364
{
5000854365
int i;
5000954366
for(i=0; i<p->nOp; i++){
5001054367
p->aOp[i].cnt = 0;
@@ -50689,14 +55046,21 @@
5068955046
** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in
5069055047
** the following code.
5069155048
*/
5069255049
if( eStatementOp ){
5069355050
rc = sqlite3VdbeCloseStatement(p, eStatementOp);
50694
- if( rc && (NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT) ){
50695
- p->rc = rc;
50696
- sqlite3DbFree(db, p->zErrMsg);
50697
- p->zErrMsg = 0;
55051
+ if( rc ){
55052
+ assert( eStatementOp==SAVEPOINT_ROLLBACK );
55053
+ if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){
55054
+ p->rc = rc;
55055
+ sqlite3DbFree(db, p->zErrMsg);
55056
+ p->zErrMsg = 0;
55057
+ }
55058
+ invalidateCursorsOnModifiedBtrees(db);
55059
+ sqlite3RollbackAll(db);
55060
+ sqlite3CloseSavepoints(db);
55061
+ db->autoCommit = 1;
5069855062
}
5069955063
}
5070055064
5070155065
/* If this was an INSERT, UPDATE or DELETE and no statement transaction
5070255066
** has been rolled back, update the database connection change-counter.
@@ -51976,10 +56340,31 @@
5197656340
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
5197756341
sqlite3VdbeMemSetNull(&pCtx->s);
5197856342
pCtx->isError = SQLITE_NOMEM;
5197956343
pCtx->s.db->mallocFailed = 1;
5198056344
}
56345
+
56346
+/*
56347
+** This function is called after a transaction has been committed. It
56348
+** invokes callbacks registered with sqlite3_wal_hook() as required.
56349
+*/
56350
+static int doWalCallbacks(sqlite3 *db){
56351
+ int rc = SQLITE_OK;
56352
+#ifndef SQLITE_OMIT_WAL
56353
+ int i;
56354
+ for(i=0; i<db->nDb; i++){
56355
+ Btree *pBt = db->aDb[i].pBt;
56356
+ if( pBt ){
56357
+ int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
56358
+ if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
56359
+ rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
56360
+ }
56361
+ }
56362
+ }
56363
+#endif
56364
+ return rc;
56365
+}
5198156366
5198256367
/*
5198356368
** Execute the statement pStmt, either until a row of data is ready, the
5198456369
** statement is completely executed or an error occurs.
5198556370
**
@@ -51992,13 +56377,16 @@
5199256377
sqlite3 *db;
5199356378
int rc;
5199456379
5199556380
assert(p);
5199656381
if( p->magic!=VDBE_MAGIC_RUN ){
51997
- sqlite3_log(SQLITE_MISUSE,
51998
- "attempt to step a halted statement: [%s]", p->zSql);
51999
- return SQLITE_MISUSE_BKPT;
56382
+ /* We used to require that sqlite3_reset() be called before retrying
56383
+ ** sqlite3_step() after any error. But after 3.6.23, we changed this
56384
+ ** so that sqlite3_reset() would be called automatically instead of
56385
+ ** throwing the error.
56386
+ */
56387
+ sqlite3_reset((sqlite3_stmt*)p);
5200056388
}
5200156389
5200256390
/* Check that malloc() has not failed. If it has, return early. */
5200356391
db = p->db;
5200456392
if( db->mallocFailed ){
@@ -52022,13 +56410,11 @@
5202256410
5202356411
assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
5202456412
5202556413
#ifndef SQLITE_OMIT_TRACE
5202656414
if( db->xProfile && !db->init.busy ){
52027
- double rNow;
52028
- sqlite3OsCurrentTime(db->pVfs, &rNow);
52029
- p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
56415
+ sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
5203056416
}
5203156417
#endif
5203256418
5203356419
db->activeVdbeCnt++;
5203456420
if( p->readOnly==0 ) db->writeVdbeCnt++;
@@ -52045,19 +56431,23 @@
5204556431
5204656432
#ifndef SQLITE_OMIT_TRACE
5204756433
/* Invoke the profile callback if there is one
5204856434
*/
5204956435
if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
52050
- double rNow;
52051
- u64 elapseTime;
52052
-
52053
- sqlite3OsCurrentTime(db->pVfs, &rNow);
52054
- elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
52055
- elapseTime -= p->startTime;
52056
- db->xProfile(db->pProfileArg, p->zSql, elapseTime);
56436
+ sqlite3_int64 iNow;
56437
+ sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
56438
+ db->xProfile(db->pProfileArg, p->zSql, iNow - p->startTime);
5205756439
}
5205856440
#endif
56441
+
56442
+ if( rc==SQLITE_DONE ){
56443
+ assert( p->rc==SQLITE_OK );
56444
+ p->rc = doWalCallbacks(db);
56445
+ if( p->rc!=SQLITE_OK ){
56446
+ rc = SQLITE_ERROR;
56447
+ }
56448
+ }
5205956449
5206056450
db->errCode = rc;
5206156451
if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
5206256452
p->rc = SQLITE_NOMEM;
5206356453
}
@@ -53638,26 +58028,10 @@
5363858028
** flag on jump instructions, we get a (small) speed improvement.
5363958029
*/
5364058030
#define CHECK_FOR_INTERRUPT \
5364158031
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
5364258032
53643
-#ifdef SQLITE_DEBUG
53644
-static int fileExists(sqlite3 *db, const char *zFile){
53645
- int res = 0;
53646
- int rc = SQLITE_OK;
53647
-#ifdef SQLITE_TEST
53648
- /* If we are currently testing IO errors, then do not call OsAccess() to
53649
- ** test for the presence of zFile. This is because any IO error that
53650
- ** occurs here will not be reported, causing the test to fail.
53651
- */
53652
- extern int sqlite3_io_error_pending;
53653
- if( sqlite3_io_error_pending<=0 )
53654
-#endif
53655
- rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
53656
- return (res && rc==SQLITE_OK);
53657
-}
53658
-#endif
5365958033
5366058034
#ifndef NDEBUG
5366158035
/*
5366258036
** This function is only called from within an assert() expression. It
5366358037
** checks that the sqlite3.nTransaction variable is correctly set to
@@ -53744,13 +58118,10 @@
5374458118
union vdbeExecUnion {
5374558119
struct OP_Yield_stack_vars {
5374658120
int pcDest;
5374758121
} aa;
5374858122
struct OP_Variable_stack_vars {
53749
- int p1; /* Variable to copy from */
53750
- int p2; /* Register to copy to */
53751
- int n; /* Number of values left to copy */
5375258123
Mem *pVar; /* Value being transferred */
5375358124
} ab;
5375458125
struct OP_Move_stack_vars {
5375558126
char *zMalloc; /* Holding variable for allocated memory */
5375658127
int n; /* Number of registers left to copy */
@@ -54071,22 +58442,29 @@
5407158442
sqlite3_value **apVal;
5407258443
} cb;
5407358444
struct OP_AggFinal_stack_vars {
5407458445
Mem *pMem;
5407558446
} cc;
58447
+ struct OP_JournalMode_stack_vars {
58448
+ Btree *pBt; /* Btree to change journal mode of */
58449
+ Pager *pPager; /* Pager associated with pBt */
58450
+ int eNew; /* New journal mode */
58451
+ int eOld; /* The old journal mode */
58452
+ const char *zFilename; /* Name of database file for pPager */
58453
+ } cd;
5407658454
struct OP_IncrVacuum_stack_vars {
5407758455
Btree *pBt;
54078
- } cd;
58456
+ } ce;
5407958457
struct OP_VBegin_stack_vars {
5408058458
VTable *pVTab;
54081
- } ce;
58459
+ } cf;
5408258460
struct OP_VOpen_stack_vars {
5408358461
VdbeCursor *pCur;
5408458462
sqlite3_vtab_cursor *pVtabCursor;
5408558463
sqlite3_vtab *pVtab;
5408658464
sqlite3_module *pModule;
54087
- } cf;
58465
+ } cg;
5408858466
struct OP_VFilter_stack_vars {
5408958467
int nArg;
5409058468
int iQuery;
5409158469
const sqlite3_module *pModule;
5409258470
Mem *pQuery;
@@ -54095,39 +58473,39 @@
5409558473
sqlite3_vtab *pVtab;
5409658474
VdbeCursor *pCur;
5409758475
int res;
5409858476
int i;
5409958477
Mem **apArg;
54100
- } cg;
58478
+ } ch;
5410158479
struct OP_VColumn_stack_vars {
5410258480
sqlite3_vtab *pVtab;
5410358481
const sqlite3_module *pModule;
5410458482
Mem *pDest;
5410558483
sqlite3_context sContext;
54106
- } ch;
58484
+ } ci;
5410758485
struct OP_VNext_stack_vars {
5410858486
sqlite3_vtab *pVtab;
5410958487
const sqlite3_module *pModule;
5411058488
int res;
5411158489
VdbeCursor *pCur;
54112
- } ci;
58490
+ } cj;
5411358491
struct OP_VRename_stack_vars {
5411458492
sqlite3_vtab *pVtab;
5411558493
Mem *pName;
54116
- } cj;
58494
+ } ck;
5411758495
struct OP_VUpdate_stack_vars {
5411858496
sqlite3_vtab *pVtab;
5411958497
sqlite3_module *pModule;
5412058498
int nArg;
5412158499
int i;
5412258500
sqlite_int64 rowid;
5412358501
Mem **apArg;
5412458502
Mem *pX;
54125
- } ck;
58503
+ } cl;
5412658504
struct OP_Trace_stack_vars {
5412758505
char *zTrace;
54128
- } cl;
58506
+ } cm;
5412958507
} u;
5413058508
/* End automatically generated code
5413158509
********************************************************************/
5413258510
5413358511
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -54147,23 +58525,18 @@
5414758525
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
5414858526
checkProgress = db->xProgress!=0;
5414958527
#endif
5415058528
#ifdef SQLITE_DEBUG
5415158529
sqlite3BeginBenignMalloc();
54152
- if( p->pc==0
54153
- && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
54154
- ){
58530
+ if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
5415558531
int i;
5415658532
printf("VDBE Program Listing:\n");
5415758533
sqlite3VdbePrintSql(p);
5415858534
for(i=0; i<p->nOp; i++){
5415958535
sqlite3VdbePrintOp(stdout, i, &aOp[i]);
5416058536
}
5416158537
}
54162
- if( fileExists(db, "vdbe_trace") ){
54163
- p->trace = stdout;
54164
- }
5416558538
sqlite3EndBenignMalloc();
5416658539
#endif
5416758540
for(pc=p->pc; rc==SQLITE_OK; pc++){
5416858541
assert( pc>=0 && pc<p->nOp );
5416958542
if( db->mallocFailed ) goto no_mem;
@@ -54181,17 +58554,10 @@
5418158554
printf("VDBE Execution Trace:\n");
5418258555
sqlite3VdbePrintSql(p);
5418358556
}
5418458557
sqlite3VdbePrintOp(p->trace, pc, pOp);
5418558558
}
54186
- if( p->trace==0 && pc==0 ){
54187
- sqlite3BeginBenignMalloc();
54188
- if( fileExists(db, "vdbe_sqltrace") ){
54189
- sqlite3VdbePrintSql(p);
54190
- }
54191
- sqlite3EndBenignMalloc();
54192
- }
5419358559
#endif
5419458560
5419558561
5419658562
/* Check to see if we need to simulate an interrupt. This only happens
5419758563
** if we have a special test build.
@@ -54544,44 +58910,29 @@
5454458910
pOut->enc = encoding;
5454558911
UPDATE_MAX_BLOBSIZE(pOut);
5454658912
break;
5454758913
}
5454858914
54549
-/* Opcode: Variable P1 P2 P3 P4 *
58915
+/* Opcode: Variable P1 P2 * P4 *
5455058916
**
54551
-** Transfer the values of bound parameters P1..P1+P3-1 into registers
54552
-** P2..P2+P3-1.
58917
+** Transfer the values of bound parameter P1 into register P2
5455358918
**
5455458919
** If the parameter is named, then its name appears in P4 and P3==1.
5455558920
** The P4 value is used by sqlite3_bind_parameter_name().
5455658921
*/
54557
-case OP_Variable: {
58922
+case OP_Variable: { /* out2-prerelease */
5455858923
#if 0 /* local variables moved into u.ab */
54559
- int p1; /* Variable to copy from */
54560
- int p2; /* Register to copy to */
54561
- int n; /* Number of values left to copy */
5456258924
Mem *pVar; /* Value being transferred */
5456358925
#endif /* local variables moved into u.ab */
5456458926
54565
- u.ab.p1 = pOp->p1 - 1;
54566
- u.ab.p2 = pOp->p2;
54567
- u.ab.n = pOp->p3;
54568
- assert( u.ab.p1>=0 && u.ab.p1+u.ab.n<=p->nVar );
54569
- assert( u.ab.p2>=1 && u.ab.p2+u.ab.n-1<=p->nMem );
54570
- assert( pOp->p4.z==0 || pOp->p3==1 || pOp->p3==0 );
54571
-
54572
- while( u.ab.n-- > 0 ){
54573
- u.ab.pVar = &p->aVar[u.ab.p1++];
54574
- if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
54575
- goto too_big;
54576
- }
54577
- pOut = &aMem[u.ab.p2++];
54578
- sqlite3VdbeMemReleaseExternal(pOut);
54579
- pOut->flags = MEM_Null;
54580
- sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
54581
- UPDATE_MAX_BLOBSIZE(pOut);
54582
- }
58927
+ assert( pOp->p1>0 && pOp->p1<=p->nVar );
58928
+ u.ab.pVar = &p->aVar[pOp->p1 - 1];
58929
+ if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
58930
+ goto too_big;
58931
+ }
58932
+ sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
58933
+ UPDATE_MAX_BLOBSIZE(pOut);
5458358934
break;
5458458935
}
5458558936
5458658937
/* Opcode: Move P1 P2 P3 * *
5458758938
**
@@ -58849,10 +63200,151 @@
5884963200
goto too_big;
5885063201
}
5885163202
break;
5885263203
}
5885363204
63205
+#ifndef SQLITE_OMIT_WAL
63206
+/* Opcode: Checkpoint P1 * * * *
63207
+**
63208
+** Checkpoint database P1. This is a no-op if P1 is not currently in
63209
+** WAL mode.
63210
+*/
63211
+case OP_Checkpoint: {
63212
+ rc = sqlite3Checkpoint(db, pOp->p1);
63213
+ break;
63214
+};
63215
+#endif
63216
+
63217
+/* Opcode: JournalMode P1 P2 P3 * P5
63218
+**
63219
+** Change the journal mode of database P1 to P3. P3 must be one of the
63220
+** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
63221
+** modes (delete, truncate, persist, off and memory), this is a simple
63222
+** operation. No IO is required.
63223
+**
63224
+** If changing into or out of WAL mode the procedure is more complicated.
63225
+**
63226
+** Write a string containing the final journal-mode to register P2.
63227
+**
63228
+** If an attempt to change in to or out of WAL mode fails because another
63229
+** connection also has the same database open, then an SQLITE_BUSY error
63230
+** is raised if P5==0, or of P5!=0 the journal mode changed is skipped
63231
+** without signaling the error.
63232
+*/
63233
+case OP_JournalMode: { /* out2-prerelease */
63234
+#if 0 /* local variables moved into u.cd */
63235
+ Btree *pBt; /* Btree to change journal mode of */
63236
+ Pager *pPager; /* Pager associated with pBt */
63237
+ int eNew; /* New journal mode */
63238
+ int eOld; /* The old journal mode */
63239
+ const char *zFilename; /* Name of database file for pPager */
63240
+#endif /* local variables moved into u.cd */
63241
+
63242
+ u.cd.eNew = pOp->p3;
63243
+ assert( u.cd.eNew==PAGER_JOURNALMODE_DELETE
63244
+ || u.cd.eNew==PAGER_JOURNALMODE_TRUNCATE
63245
+ || u.cd.eNew==PAGER_JOURNALMODE_PERSIST
63246
+ || u.cd.eNew==PAGER_JOURNALMODE_OFF
63247
+ || u.cd.eNew==PAGER_JOURNALMODE_MEMORY
63248
+ || u.cd.eNew==PAGER_JOURNALMODE_WAL
63249
+ || u.cd.eNew==PAGER_JOURNALMODE_QUERY
63250
+ );
63251
+ assert( pOp->p1>=0 && pOp->p1<db->nDb );
63252
+
63253
+ /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
63254
+ ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
63255
+ ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
63256
+ ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
63257
+ ** is not called when the statement is prepared because it requires the
63258
+ ** iDb index of the database as a parameter, and the database has not
63259
+ ** yet been attached so that index is unavailable. We have to wait
63260
+ ** until runtime (now) to get the mutex on the newly attached database.
63261
+ ** No other mutexes are required by the ATTACH command so this is safe
63262
+ ** to do.
63263
+ */
63264
+ assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
63265
+ if( p->aMutex.nMutex==0 ){
63266
+ /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
63267
+ ** database. */
63268
+ sqlite3VdbeUsesBtree(p, pOp->p1);
63269
+ sqlite3VdbeMutexArrayEnter(p);
63270
+ }
63271
+
63272
+ u.cd.pBt = db->aDb[pOp->p1].pBt;
63273
+ u.cd.pPager = sqlite3BtreePager(u.cd.pBt);
63274
+ u.cd.eOld = sqlite3PagerGetJournalMode(u.cd.pPager);
63275
+ if( u.cd.eNew==PAGER_JOURNALMODE_QUERY ) u.cd.eNew = u.cd.eOld;
63276
+ if( !sqlite3PagerOkToChangeJournalMode(u.cd.pPager) ) u.cd.eNew = u.cd.eOld;
63277
+
63278
+#ifndef SQLITE_OMIT_WAL
63279
+ u.cd.zFilename = sqlite3PagerFilename(u.cd.pPager);
63280
+
63281
+ /* Do not allow a transition to journal_mode=WAL for a database
63282
+ ** in temporary storage or if the VFS does not support xShmOpen.
63283
+ */
63284
+ if( u.cd.eNew==PAGER_JOURNALMODE_WAL
63285
+ && (u.cd.zFilename[0]==0 /* Temp file */
63286
+ || !sqlite3PagerWalSupported(u.cd.pPager)) /* No xShmOpen support */
63287
+ ){
63288
+ u.cd.eNew = u.cd.eOld;
63289
+ }
63290
+
63291
+ if( (u.cd.eNew!=u.cd.eOld)
63292
+ && (u.cd.eOld==PAGER_JOURNALMODE_WAL || u.cd.eNew==PAGER_JOURNALMODE_WAL)
63293
+ ){
63294
+ if( !db->autoCommit || db->activeVdbeCnt>1 ){
63295
+ rc = SQLITE_ERROR;
63296
+ sqlite3SetString(&p->zErrMsg, db,
63297
+ "cannot change %s wal mode from within a transaction",
63298
+ (u.cd.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
63299
+ );
63300
+ break;
63301
+ }else{
63302
+
63303
+ if( u.cd.eOld==PAGER_JOURNALMODE_WAL ){
63304
+ /* If leaving WAL mode, close the log file. If successful, the call
63305
+ ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
63306
+ ** file. An EXCLUSIVE lock may still be held on the database file
63307
+ ** after a successful return.
63308
+ */
63309
+ rc = sqlite3PagerCloseWal(u.cd.pPager);
63310
+ if( rc==SQLITE_OK ){
63311
+ sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63312
+ }else if( rc==SQLITE_BUSY && pOp->p5==0 ){
63313
+ goto abort_due_to_error;
63314
+ }
63315
+ }else{
63316
+ sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_DELETE);
63317
+ rc = SQLITE_OK;
63318
+ }
63319
+
63320
+ /* Open a transaction on the database file. Regardless of the journal
63321
+ ** mode, this transaction always uses a rollback journal.
63322
+ */
63323
+ assert( sqlite3BtreeIsInTrans(u.cd.pBt)==0 );
63324
+ if( rc==SQLITE_OK ){
63325
+ rc = sqlite3BtreeSetVersion(u.cd.pBt,
63326
+ (u.cd.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
63327
+ if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error;
63328
+ }
63329
+ if( rc==SQLITE_BUSY ){
63330
+ u.cd.eNew = u.cd.eOld;
63331
+ rc = SQLITE_OK;
63332
+ }
63333
+ }
63334
+ }
63335
+#endif /* ifndef SQLITE_OMIT_WAL */
63336
+
63337
+ u.cd.eNew = sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63338
+ pOut = &aMem[pOp->p2];
63339
+ pOut->flags = MEM_Str|MEM_Static|MEM_Term;
63340
+ pOut->z = (char *)sqlite3JournalModename(u.cd.eNew);
63341
+ pOut->n = sqlite3Strlen30(pOut->z);
63342
+ pOut->enc = SQLITE_UTF8;
63343
+ sqlite3VdbeChangeEncoding(pOut, encoding);
63344
+ break;
63345
+};
5885463346
5885563347
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
5885663348
/* Opcode: Vacuum * * * * *
5885763349
**
5885863350
** Vacuum the entire database. This opcode will cause other virtual
@@ -58871,18 +63363,18 @@
5887163363
** Perform a single step of the incremental vacuum procedure on
5887263364
** the P1 database. If the vacuum has finished, jump to instruction
5887363365
** P2. Otherwise, fall through to the next instruction.
5887463366
*/
5887563367
case OP_IncrVacuum: { /* jump */
58876
-#if 0 /* local variables moved into u.cd */
63368
+#if 0 /* local variables moved into u.ce */
5887763369
Btree *pBt;
58878
-#endif /* local variables moved into u.cd */
63370
+#endif /* local variables moved into u.ce */
5887963371
5888063372
assert( pOp->p1>=0 && pOp->p1<db->nDb );
5888163373
assert( (p->btreeMask & (1<<pOp->p1))!=0 );
58882
- u.cd.pBt = db->aDb[pOp->p1].pBt;
58883
- rc = sqlite3BtreeIncrVacuum(u.cd.pBt);
63374
+ u.ce.pBt = db->aDb[pOp->p1].pBt;
63375
+ rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
5888463376
if( rc==SQLITE_DONE ){
5888563377
pc = pOp->p2 - 1;
5888663378
rc = SQLITE_OK;
5888763379
}
5888863380
break;
@@ -58948,19 +63440,19 @@
5894863440
** Also, whether or not P4 is set, check that this is not being called from
5894963441
** within a callback to a virtual table xSync() method. If it is, the error
5895063442
** code will be set to SQLITE_LOCKED.
5895163443
*/
5895263444
case OP_VBegin: {
58953
-#if 0 /* local variables moved into u.ce */
63445
+#if 0 /* local variables moved into u.cf */
5895463446
VTable *pVTab;
58955
-#endif /* local variables moved into u.ce */
58956
- u.ce.pVTab = pOp->p4.pVtab;
58957
- rc = sqlite3VtabBegin(db, u.ce.pVTab);
58958
- if( u.ce.pVTab ){
63447
+#endif /* local variables moved into u.cf */
63448
+ u.cf.pVTab = pOp->p4.pVtab;
63449
+ rc = sqlite3VtabBegin(db, u.cf.pVTab);
63450
+ if( u.cf.pVTab ){
5895963451
sqlite3DbFree(db, p->zErrMsg);
58960
- p->zErrMsg = u.ce.pVTab->pVtab->zErrMsg;
58961
- u.ce.pVTab->pVtab->zErrMsg = 0;
63452
+ p->zErrMsg = u.cf.pVTab->pVtab->zErrMsg;
63453
+ u.cf.pVTab->pVtab->zErrMsg = 0;
5896263454
}
5896363455
break;
5896463456
}
5896563457
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5896663458
@@ -58996,38 +63488,38 @@
5899663488
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
5899763489
** P1 is a cursor number. This opcode opens a cursor to the virtual
5899863490
** table and stores that cursor in P1.
5899963491
*/
5900063492
case OP_VOpen: {
59001
-#if 0 /* local variables moved into u.cf */
63493
+#if 0 /* local variables moved into u.cg */
5900263494
VdbeCursor *pCur;
5900363495
sqlite3_vtab_cursor *pVtabCursor;
5900463496
sqlite3_vtab *pVtab;
5900563497
sqlite3_module *pModule;
59006
-#endif /* local variables moved into u.cf */
59007
-
59008
- u.cf.pCur = 0;
59009
- u.cf.pVtabCursor = 0;
59010
- u.cf.pVtab = pOp->p4.pVtab->pVtab;
59011
- u.cf.pModule = (sqlite3_module *)u.cf.pVtab->pModule;
59012
- assert(u.cf.pVtab && u.cf.pModule);
59013
- rc = u.cf.pModule->xOpen(u.cf.pVtab, &u.cf.pVtabCursor);
63498
+#endif /* local variables moved into u.cg */
63499
+
63500
+ u.cg.pCur = 0;
63501
+ u.cg.pVtabCursor = 0;
63502
+ u.cg.pVtab = pOp->p4.pVtab->pVtab;
63503
+ u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
63504
+ assert(u.cg.pVtab && u.cg.pModule);
63505
+ rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
5901463506
sqlite3DbFree(db, p->zErrMsg);
59015
- p->zErrMsg = u.cf.pVtab->zErrMsg;
59016
- u.cf.pVtab->zErrMsg = 0;
63507
+ p->zErrMsg = u.cg.pVtab->zErrMsg;
63508
+ u.cg.pVtab->zErrMsg = 0;
5901763509
if( SQLITE_OK==rc ){
5901863510
/* Initialize sqlite3_vtab_cursor base class */
59019
- u.cf.pVtabCursor->pVtab = u.cf.pVtab;
63511
+ u.cg.pVtabCursor->pVtab = u.cg.pVtab;
5902063512
5902163513
/* Initialise vdbe cursor object */
59022
- u.cf.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
59023
- if( u.cf.pCur ){
59024
- u.cf.pCur->pVtabCursor = u.cf.pVtabCursor;
59025
- u.cf.pCur->pModule = u.cf.pVtabCursor->pVtab->pModule;
63514
+ u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
63515
+ if( u.cg.pCur ){
63516
+ u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
63517
+ u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
5902663518
}else{
5902763519
db->mallocFailed = 1;
59028
- u.cf.pModule->xClose(u.cf.pVtabCursor);
63520
+ u.cg.pModule->xClose(u.cg.pVtabCursor);
5902963521
}
5903063522
}
5903163523
break;
5903263524
}
5903363525
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59050,11 +63542,11 @@
5905063542
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
5905163543
**
5905263544
** A jump is made to P2 if the result set after filtering would be empty.
5905363545
*/
5905463546
case OP_VFilter: { /* jump */
59055
-#if 0 /* local variables moved into u.cg */
63547
+#if 0 /* local variables moved into u.ch */
5905663548
int nArg;
5905763549
int iQuery;
5905863550
const sqlite3_module *pModule;
5905963551
Mem *pQuery;
5906063552
Mem *pArgc;
@@ -59062,50 +63554,50 @@
5906263554
sqlite3_vtab *pVtab;
5906363555
VdbeCursor *pCur;
5906463556
int res;
5906563557
int i;
5906663558
Mem **apArg;
59067
-#endif /* local variables moved into u.cg */
59068
-
59069
- u.cg.pQuery = &aMem[pOp->p3];
59070
- u.cg.pArgc = &u.cg.pQuery[1];
59071
- u.cg.pCur = p->apCsr[pOp->p1];
59072
- REGISTER_TRACE(pOp->p3, u.cg.pQuery);
59073
- assert( u.cg.pCur->pVtabCursor );
59074
- u.cg.pVtabCursor = u.cg.pCur->pVtabCursor;
59075
- u.cg.pVtab = u.cg.pVtabCursor->pVtab;
59076
- u.cg.pModule = u.cg.pVtab->pModule;
63559
+#endif /* local variables moved into u.ch */
63560
+
63561
+ u.ch.pQuery = &aMem[pOp->p3];
63562
+ u.ch.pArgc = &u.ch.pQuery[1];
63563
+ u.ch.pCur = p->apCsr[pOp->p1];
63564
+ REGISTER_TRACE(pOp->p3, u.ch.pQuery);
63565
+ assert( u.ch.pCur->pVtabCursor );
63566
+ u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
63567
+ u.ch.pVtab = u.ch.pVtabCursor->pVtab;
63568
+ u.ch.pModule = u.ch.pVtab->pModule;
5907763569
5907863570
/* Grab the index number and argc parameters */
59079
- assert( (u.cg.pQuery->flags&MEM_Int)!=0 && u.cg.pArgc->flags==MEM_Int );
59080
- u.cg.nArg = (int)u.cg.pArgc->u.i;
59081
- u.cg.iQuery = (int)u.cg.pQuery->u.i;
63571
+ assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
63572
+ u.ch.nArg = (int)u.ch.pArgc->u.i;
63573
+ u.ch.iQuery = (int)u.ch.pQuery->u.i;
5908263574
5908363575
/* Invoke the xFilter method */
5908463576
{
59085
- u.cg.res = 0;
59086
- u.cg.apArg = p->apArg;
59087
- for(u.cg.i = 0; u.cg.i<u.cg.nArg; u.cg.i++){
59088
- u.cg.apArg[u.cg.i] = &u.cg.pArgc[u.cg.i+1];
59089
- sqlite3VdbeMemStoreType(u.cg.apArg[u.cg.i]);
63577
+ u.ch.res = 0;
63578
+ u.ch.apArg = p->apArg;
63579
+ for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
63580
+ u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
63581
+ sqlite3VdbeMemStoreType(u.ch.apArg[u.ch.i]);
5909063582
}
5909163583
5909263584
p->inVtabMethod = 1;
59093
- rc = u.cg.pModule->xFilter(u.cg.pVtabCursor, u.cg.iQuery, pOp->p4.z, u.cg.nArg, u.cg.apArg);
63585
+ rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
5909463586
p->inVtabMethod = 0;
5909563587
sqlite3DbFree(db, p->zErrMsg);
59096
- p->zErrMsg = u.cg.pVtab->zErrMsg;
59097
- u.cg.pVtab->zErrMsg = 0;
63588
+ p->zErrMsg = u.ch.pVtab->zErrMsg;
63589
+ u.ch.pVtab->zErrMsg = 0;
5909863590
if( rc==SQLITE_OK ){
59099
- u.cg.res = u.cg.pModule->xEof(u.cg.pVtabCursor);
63591
+ u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
5910063592
}
5910163593
59102
- if( u.cg.res ){
63594
+ if( u.ch.res ){
5910363595
pc = pOp->p2 - 1;
5910463596
}
5910563597
}
59106
- u.cg.pCur->nullRow = 0;
63598
+ u.ch.pCur->nullRow = 0;
5910763599
5910863600
break;
5910963601
}
5911063602
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5911163603
@@ -59115,56 +63607,56 @@
5911563607
** Store the value of the P2-th column of
5911663608
** the row of the virtual-table that the
5911763609
** P1 cursor is pointing to into register P3.
5911863610
*/
5911963611
case OP_VColumn: {
59120
-#if 0 /* local variables moved into u.ch */
63612
+#if 0 /* local variables moved into u.ci */
5912163613
sqlite3_vtab *pVtab;
5912263614
const sqlite3_module *pModule;
5912363615
Mem *pDest;
5912463616
sqlite3_context sContext;
59125
-#endif /* local variables moved into u.ch */
63617
+#endif /* local variables moved into u.ci */
5912663618
5912763619
VdbeCursor *pCur = p->apCsr[pOp->p1];
5912863620
assert( pCur->pVtabCursor );
5912963621
assert( pOp->p3>0 && pOp->p3<=p->nMem );
59130
- u.ch.pDest = &aMem[pOp->p3];
63622
+ u.ci.pDest = &aMem[pOp->p3];
5913163623
if( pCur->nullRow ){
59132
- sqlite3VdbeMemSetNull(u.ch.pDest);
63624
+ sqlite3VdbeMemSetNull(u.ci.pDest);
5913363625
break;
5913463626
}
59135
- u.ch.pVtab = pCur->pVtabCursor->pVtab;
59136
- u.ch.pModule = u.ch.pVtab->pModule;
59137
- assert( u.ch.pModule->xColumn );
59138
- memset(&u.ch.sContext, 0, sizeof(u.ch.sContext));
63627
+ u.ci.pVtab = pCur->pVtabCursor->pVtab;
63628
+ u.ci.pModule = u.ci.pVtab->pModule;
63629
+ assert( u.ci.pModule->xColumn );
63630
+ memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
5913963631
5914063632
/* The output cell may already have a buffer allocated. Move
59141
- ** the current contents to u.ch.sContext.s so in case the user-function
63633
+ ** the current contents to u.ci.sContext.s so in case the user-function
5914263634
** can use the already allocated buffer instead of allocating a
5914363635
** new one.
5914463636
*/
59145
- sqlite3VdbeMemMove(&u.ch.sContext.s, u.ch.pDest);
59146
- MemSetTypeFlag(&u.ch.sContext.s, MEM_Null);
63637
+ sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
63638
+ MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
5914763639
59148
- rc = u.ch.pModule->xColumn(pCur->pVtabCursor, &u.ch.sContext, pOp->p2);
63640
+ rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
5914963641
sqlite3DbFree(db, p->zErrMsg);
59150
- p->zErrMsg = u.ch.pVtab->zErrMsg;
59151
- u.ch.pVtab->zErrMsg = 0;
59152
- if( u.ch.sContext.isError ){
59153
- rc = u.ch.sContext.isError;
63642
+ p->zErrMsg = u.ci.pVtab->zErrMsg;
63643
+ u.ci.pVtab->zErrMsg = 0;
63644
+ if( u.ci.sContext.isError ){
63645
+ rc = u.ci.sContext.isError;
5915463646
}
5915563647
5915663648
/* Copy the result of the function to the P3 register. We
5915763649
** do this regardless of whether or not an error occurred to ensure any
59158
- ** dynamic allocation in u.ch.sContext.s (a Mem struct) is released.
63650
+ ** dynamic allocation in u.ci.sContext.s (a Mem struct) is released.
5915963651
*/
59160
- sqlite3VdbeChangeEncoding(&u.ch.sContext.s, encoding);
59161
- sqlite3VdbeMemMove(u.ch.pDest, &u.ch.sContext.s);
59162
- REGISTER_TRACE(pOp->p3, u.ch.pDest);
59163
- UPDATE_MAX_BLOBSIZE(u.ch.pDest);
63652
+ sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
63653
+ sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
63654
+ REGISTER_TRACE(pOp->p3, u.ci.pDest);
63655
+ UPDATE_MAX_BLOBSIZE(u.ci.pDest);
5916463656
59165
- if( sqlite3VdbeMemTooBig(u.ch.pDest) ){
63657
+ if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
5916663658
goto too_big;
5916763659
}
5916863660
break;
5916963661
}
5917063662
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59175,44 +63667,44 @@
5917563667
** Advance virtual table P1 to the next row in its result set and
5917663668
** jump to instruction P2. Or, if the virtual table has reached
5917763669
** the end of its result set, then fall through to the next instruction.
5917863670
*/
5917963671
case OP_VNext: { /* jump */
59180
-#if 0 /* local variables moved into u.ci */
63672
+#if 0 /* local variables moved into u.cj */
5918163673
sqlite3_vtab *pVtab;
5918263674
const sqlite3_module *pModule;
5918363675
int res;
5918463676
VdbeCursor *pCur;
59185
-#endif /* local variables moved into u.ci */
63677
+#endif /* local variables moved into u.cj */
5918663678
59187
- u.ci.res = 0;
59188
- u.ci.pCur = p->apCsr[pOp->p1];
59189
- assert( u.ci.pCur->pVtabCursor );
59190
- if( u.ci.pCur->nullRow ){
63679
+ u.cj.res = 0;
63680
+ u.cj.pCur = p->apCsr[pOp->p1];
63681
+ assert( u.cj.pCur->pVtabCursor );
63682
+ if( u.cj.pCur->nullRow ){
5919163683
break;
5919263684
}
59193
- u.ci.pVtab = u.ci.pCur->pVtabCursor->pVtab;
59194
- u.ci.pModule = u.ci.pVtab->pModule;
59195
- assert( u.ci.pModule->xNext );
63685
+ u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
63686
+ u.cj.pModule = u.cj.pVtab->pModule;
63687
+ assert( u.cj.pModule->xNext );
5919663688
5919763689
/* Invoke the xNext() method of the module. There is no way for the
5919863690
** underlying implementation to return an error if one occurs during
5919963691
** xNext(). Instead, if an error occurs, true is returned (indicating that
5920063692
** data is available) and the error code returned when xColumn or
5920163693
** some other method is next invoked on the save virtual table cursor.
5920263694
*/
5920363695
p->inVtabMethod = 1;
59204
- rc = u.ci.pModule->xNext(u.ci.pCur->pVtabCursor);
63696
+ rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
5920563697
p->inVtabMethod = 0;
5920663698
sqlite3DbFree(db, p->zErrMsg);
59207
- p->zErrMsg = u.ci.pVtab->zErrMsg;
59208
- u.ci.pVtab->zErrMsg = 0;
63699
+ p->zErrMsg = u.cj.pVtab->zErrMsg;
63700
+ u.cj.pVtab->zErrMsg = 0;
5920963701
if( rc==SQLITE_OK ){
59210
- u.ci.res = u.ci.pModule->xEof(u.ci.pCur->pVtabCursor);
63702
+ u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
5921163703
}
5921263704
59213
- if( !u.ci.res ){
63705
+ if( !u.cj.res ){
5921463706
/* If there is data, jump to P2 */
5921563707
pc = pOp->p2 - 1;
5921663708
}
5921763709
break;
5921863710
}
@@ -59224,24 +63716,24 @@
5922463716
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
5922563717
** This opcode invokes the corresponding xRename method. The value
5922663718
** in register P1 is passed as the zName argument to the xRename method.
5922763719
*/
5922863720
case OP_VRename: {
59229
-#if 0 /* local variables moved into u.cj */
63721
+#if 0 /* local variables moved into u.ck */
5923063722
sqlite3_vtab *pVtab;
5923163723
Mem *pName;
59232
-#endif /* local variables moved into u.cj */
59233
-
59234
- u.cj.pVtab = pOp->p4.pVtab->pVtab;
59235
- u.cj.pName = &aMem[pOp->p1];
59236
- assert( u.cj.pVtab->pModule->xRename );
59237
- REGISTER_TRACE(pOp->p1, u.cj.pName);
59238
- assert( u.cj.pName->flags & MEM_Str );
59239
- rc = u.cj.pVtab->pModule->xRename(u.cj.pVtab, u.cj.pName->z);
63724
+#endif /* local variables moved into u.ck */
63725
+
63726
+ u.ck.pVtab = pOp->p4.pVtab->pVtab;
63727
+ u.ck.pName = &aMem[pOp->p1];
63728
+ assert( u.ck.pVtab->pModule->xRename );
63729
+ REGISTER_TRACE(pOp->p1, u.ck.pName);
63730
+ assert( u.ck.pName->flags & MEM_Str );
63731
+ rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
5924063732
sqlite3DbFree(db, p->zErrMsg);
59241
- p->zErrMsg = u.cj.pVtab->zErrMsg;
59242
- u.cj.pVtab->zErrMsg = 0;
63733
+ p->zErrMsg = u.ck.pVtab->zErrMsg;
63734
+ u.ck.pVtab->zErrMsg = 0;
5924363735
5924463736
break;
5924563737
}
5924663738
#endif
5924763739
@@ -59268,39 +63760,39 @@
5926863760
** P1 is a boolean flag. If it is set to true and the xUpdate call
5926963761
** is successful, then the value returned by sqlite3_last_insert_rowid()
5927063762
** is set to the value of the rowid for the row just inserted.
5927163763
*/
5927263764
case OP_VUpdate: {
59273
-#if 0 /* local variables moved into u.ck */
63765
+#if 0 /* local variables moved into u.cl */
5927463766
sqlite3_vtab *pVtab;
5927563767
sqlite3_module *pModule;
5927663768
int nArg;
5927763769
int i;
5927863770
sqlite_int64 rowid;
5927963771
Mem **apArg;
5928063772
Mem *pX;
59281
-#endif /* local variables moved into u.ck */
63773
+#endif /* local variables moved into u.cl */
5928263774
59283
- u.ck.pVtab = pOp->p4.pVtab->pVtab;
59284
- u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
59285
- u.ck.nArg = pOp->p2;
63775
+ u.cl.pVtab = pOp->p4.pVtab->pVtab;
63776
+ u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
63777
+ u.cl.nArg = pOp->p2;
5928663778
assert( pOp->p4type==P4_VTAB );
59287
- if( ALWAYS(u.ck.pModule->xUpdate) ){
59288
- u.ck.apArg = p->apArg;
59289
- u.ck.pX = &aMem[pOp->p3];
59290
- for(u.ck.i=0; u.ck.i<u.ck.nArg; u.ck.i++){
59291
- sqlite3VdbeMemStoreType(u.ck.pX);
59292
- u.ck.apArg[u.ck.i] = u.ck.pX;
59293
- u.ck.pX++;
59294
- }
59295
- rc = u.ck.pModule->xUpdate(u.ck.pVtab, u.ck.nArg, u.ck.apArg, &u.ck.rowid);
63779
+ if( ALWAYS(u.cl.pModule->xUpdate) ){
63780
+ u.cl.apArg = p->apArg;
63781
+ u.cl.pX = &aMem[pOp->p3];
63782
+ for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
63783
+ sqlite3VdbeMemStoreType(u.cl.pX);
63784
+ u.cl.apArg[u.cl.i] = u.cl.pX;
63785
+ u.cl.pX++;
63786
+ }
63787
+ rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
5929663788
sqlite3DbFree(db, p->zErrMsg);
59297
- p->zErrMsg = u.ck.pVtab->zErrMsg;
59298
- u.ck.pVtab->zErrMsg = 0;
63789
+ p->zErrMsg = u.cl.pVtab->zErrMsg;
63790
+ u.cl.pVtab->zErrMsg = 0;
5929963791
if( rc==SQLITE_OK && pOp->p1 ){
59300
- assert( u.ck.nArg>1 && u.ck.apArg[0] && (u.ck.apArg[0]->flags&MEM_Null) );
59301
- db->lastRowid = u.ck.rowid;
63792
+ assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
63793
+ db->lastRowid = u.cl.rowid;
5930263794
}
5930363795
p->nChange++;
5930463796
}
5930563797
break;
5930663798
}
@@ -59322,24 +63814,24 @@
5932263814
**
5932363815
** If tracing is enabled (by the sqlite3_trace()) interface, then
5932463816
** the UTF-8 string contained in P4 is emitted on the trace callback.
5932563817
*/
5932663818
case OP_Trace: {
59327
-#if 0 /* local variables moved into u.cl */
63819
+#if 0 /* local variables moved into u.cm */
5932863820
char *zTrace;
59329
-#endif /* local variables moved into u.cl */
63821
+#endif /* local variables moved into u.cm */
5933063822
59331
- u.cl.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
59332
- if( u.cl.zTrace ){
63823
+ u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
63824
+ if( u.cm.zTrace ){
5933363825
if( db->xTrace ){
59334
- char *z = sqlite3VdbeExpandSql(p, u.cl.zTrace);
63826
+ char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace);
5933563827
db->xTrace(db->pTraceArg, z);
5933663828
sqlite3DbFree(db, z);
5933763829
}
5933863830
#ifdef SQLITE_DEBUG
5933963831
if( (db->flags & SQLITE_SqlTrace)!=0 ){
59340
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cl.zTrace);
63832
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace);
5934163833
}
5934263834
#endif /* SQLITE_DEBUG */
5934363835
}
5934463836
break;
5934563837
}
@@ -60295,11 +64787,11 @@
6029564787
}
6029664788
6029764789
/*
6029864790
** Table of methods for MemJournal sqlite3_file object.
6029964791
*/
60300
-static struct sqlite3_io_methods MemJournalMethods = {
64792
+static const struct sqlite3_io_methods MemJournalMethods = {
6030164793
1, /* iVersion */
6030264794
memjrnlClose, /* xClose */
6030364795
memjrnlRead, /* xRead */
6030464796
memjrnlWrite, /* xWrite */
6030564797
memjrnlTruncate, /* xTruncate */
@@ -60318,11 +64810,11 @@
6031864810
*/
6031964811
SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
6032064812
MemJournal *p = (MemJournal *)pJfd;
6032164813
assert( EIGHT_BYTE_ALIGNMENT(p) );
6032264814
memset(p, 0, sqlite3MemJournalSize());
60323
- p->pMethod = &MemJournalMethods;
64815
+ p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
6032464816
}
6032564817
6032664818
/*
6032764819
** Return true if the file-handle passed as an argument is
6032864820
** an in-memory journal
@@ -63760,10 +68252,31 @@
6376068252
if( p->iReg==iReg ){
6376168253
p->tempReg = 0;
6376268254
}
6376368255
}
6376468256
}
68257
+
68258
+/*
68259
+** Generate code to extract the value of the iCol-th column of a table.
68260
+*/
68261
+SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
68262
+ Vdbe *v, /* The VDBE under construction */
68263
+ Table *pTab, /* The table containing the value */
68264
+ int iTabCur, /* The cursor for this table */
68265
+ int iCol, /* Index of the column to extract */
68266
+ int regOut /* Extract the valud into this register */
68267
+){
68268
+ if( iCol<0 || iCol==pTab->iPKey ){
68269
+ sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
68270
+ }else{
68271
+ int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
68272
+ sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
68273
+ }
68274
+ if( iCol>=0 ){
68275
+ sqlite3ColumnDefault(v, pTab, iCol, regOut);
68276
+ }
68277
+}
6376568278
6376668279
/*
6376768280
** Generate code that will extract the iColumn-th column from
6376868281
** table pTab and store the column value in a register. An effort
6376968282
** is made to store the column value in register iReg, but this is
@@ -63789,17 +68302,11 @@
6378968302
sqlite3ExprCachePinRegister(pParse, p->iReg);
6379068303
return p->iReg;
6379168304
}
6379268305
}
6379368306
assert( v!=0 );
63794
- if( iColumn<0 ){
63795
- sqlite3VdbeAddOp2(v, OP_Rowid, iTable, iReg);
63796
- }else if( ALWAYS(pTab!=0) ){
63797
- int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
63798
- sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
63799
- sqlite3ColumnDefault(v, pTab, iColumn, iReg);
63800
- }
68307
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
6380168308
sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
6380268309
return iReg;
6380368310
}
6380468311
6380568312
/*
@@ -64032,31 +68539,16 @@
6403268539
sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
6403368540
break;
6403468541
}
6403568542
#endif
6403668543
case TK_VARIABLE: {
64037
- VdbeOp *pOp;
6403868544
assert( !ExprHasProperty(pExpr, EP_IntValue) );
6403968545
assert( pExpr->u.zToken!=0 );
6404068546
assert( pExpr->u.zToken[0]!=0 );
64041
- if( pExpr->u.zToken[1]==0
64042
- && (pOp = sqlite3VdbeGetOp(v, -1))->opcode==OP_Variable
64043
- && pOp->p1+pOp->p3==pExpr->iColumn
64044
- && pOp->p2+pOp->p3==target
64045
- && pOp->p4.z==0
64046
- ){
64047
- /* If the previous instruction was a copy of the previous unnamed
64048
- ** parameter into the previous register, then simply increment the
64049
- ** repeat count on the prior instruction rather than making a new
64050
- ** instruction.
64051
- */
64052
- pOp->p3++;
64053
- }else{
64054
- sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iColumn, target, 1);
64055
- if( pExpr->u.zToken[1]!=0 ){
64056
- sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
64057
- }
68547
+ sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
68548
+ if( pExpr->u.zToken[1]!=0 ){
68549
+ sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
6405868550
}
6405968551
break;
6406068552
}
6406168553
case TK_REGISTER: {
6406268554
inReg = pExpr->iTable;
@@ -65119,11 +69611,10 @@
6511969611
** this routine is used, it does not hurt to get an extra 2 - that
6512069612
** just might result in some slightly slower code. But returning
6512169613
** an incorrect 0 or 1 could lead to a malfunction.
6512269614
*/
6512369615
SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
65124
- int i;
6512569616
if( pA==0||pB==0 ){
6512669617
return pB==pA ? 0 : 2;
6512769618
}
6512869619
assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
6512969620
assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
@@ -65132,22 +69623,11 @@
6513269623
}
6513369624
if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
6513469625
if( pA->op!=pB->op ) return 2;
6513569626
if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
6513669627
if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
65137
-
65138
- if( pA->x.pList && pB->x.pList ){
65139
- if( pA->x.pList->nExpr!=pB->x.pList->nExpr ) return 2;
65140
- for(i=0; i<pA->x.pList->nExpr; i++){
65141
- Expr *pExprA = pA->x.pList->a[i].pExpr;
65142
- Expr *pExprB = pB->x.pList->a[i].pExpr;
65143
- if( sqlite3ExprCompare(pExprA, pExprB) ) return 2;
65144
- }
65145
- }else if( pA->x.pList || pB->x.pList ){
65146
- return 2;
65147
- }
65148
-
69628
+ if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
6514969629
if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
6515069630
if( ExprHasProperty(pA, EP_IntValue) ){
6515169631
if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
6515269632
return 2;
6515369633
}
@@ -65160,10 +69640,35 @@
6516069640
if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
6516169641
if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
6516269642
return 0;
6516369643
}
6516469644
69645
+/*
69646
+** Compare two ExprList objects. Return 0 if they are identical and
69647
+** non-zero if they differ in any way.
69648
+**
69649
+** This routine might return non-zero for equivalent ExprLists. The
69650
+** only consequence will be disabled optimizations. But this routine
69651
+** must never return 0 if the two ExprList objects are different, or
69652
+** a malfunction will result.
69653
+**
69654
+** Two NULL pointers are considered to be the same. But a NULL pointer
69655
+** always differs from a non-NULL pointer.
69656
+*/
69657
+SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
69658
+ int i;
69659
+ if( pA==0 && pB==0 ) return 0;
69660
+ if( pA==0 || pB==0 ) return 1;
69661
+ if( pA->nExpr!=pB->nExpr ) return 1;
69662
+ for(i=0; i<pA->nExpr; i++){
69663
+ Expr *pExprA = pA->a[i].pExpr;
69664
+ Expr *pExprB = pB->a[i].pExpr;
69665
+ if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
69666
+ if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
69667
+ }
69668
+ return 0;
69669
+}
6516569670
6516669671
/*
6516769672
** Add a new element to the pAggInfo->aCol[] array. Return the index of
6516869673
** the new element. Return a negative number if malloc fails.
6516969674
*/
@@ -65656,21 +70161,27 @@
6565670161
#endif /* !SQLITE_OMIT_TRIGGER */
6565770162
6565870163
/*
6565970164
** Register built-in functions used to help implement ALTER TABLE
6566070165
*/
65661
-SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3 *db){
65662
- sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
65663
- renameTableFunc, 0, 0);
70166
+SQLITE_PRIVATE void sqlite3AlterFunctions(void){
70167
+ static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
70168
+ FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
6566470169
#ifndef SQLITE_OMIT_TRIGGER
65665
- sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
65666
- renameTriggerFunc, 0, 0);
70170
+ FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
6566770171
#endif
6566870172
#ifndef SQLITE_OMIT_FOREIGN_KEY
65669
- sqlite3CreateFunc(db, "sqlite_rename_parent", 3, SQLITE_UTF8, 0,
65670
- renameParentFunc, 0, 0);
70173
+ FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
6567170174
#endif
70175
+ };
70176
+ int i;
70177
+ FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
70178
+ FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
70179
+
70180
+ for(i=0; i<ArraySize(aAlterTableFuncs); i++){
70181
+ sqlite3FuncDefInsert(pHash, &aFunc[i]);
70182
+ }
6567270183
}
6567370184
6567470185
/*
6567570186
** This function is used to create the text of expressions of the form:
6567670187
**
@@ -65810,19 +70321,22 @@
6581070321
Vdbe *v;
6581170322
#ifndef SQLITE_OMIT_TRIGGER
6581270323
char *zWhere = 0; /* Where clause to locate temp triggers */
6581370324
#endif
6581470325
VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
65815
-
70326
+ int savedDbFlags; /* Saved value of db->flags */
70327
+
70328
+ savedDbFlags = db->flags;
6581670329
if( NEVER(db->mallocFailed) ) goto exit_rename_table;
6581770330
assert( pSrc->nSrc==1 );
6581870331
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
6581970332
6582070333
pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
6582170334
if( !pTab ) goto exit_rename_table;
6582270335
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
6582370336
zDb = db->aDb[iDb].zName;
70337
+ db->flags |= SQLITE_PreferBuiltin;
6582470338
6582570339
/* Get a NULL terminated version of the new table name. */
6582670340
zName = sqlite3NameFromToken(db, pName);
6582770341
if( !zName ) goto exit_rename_table;
6582870342
@@ -65986,10 +70500,11 @@
6598670500
reloadTableSchema(pParse, pTab, zName);
6598770501
6598870502
exit_rename_table:
6598970503
sqlite3SrcListDelete(db, pSrc);
6599070504
sqlite3DbFree(db, zName);
70505
+ db->flags = savedDbFlags;
6599170506
}
6599270507
6599370508
6599470509
/*
6599570510
** Generate code to make sure the file format number is at least minFormat.
@@ -66105,21 +70620,24 @@
6610570620
6610670621
/* Modify the CREATE TABLE statement. */
6610770622
zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
6610870623
if( zCol ){
6610970624
char *zEnd = &zCol[pColDef->n-1];
70625
+ int savedDbFlags = db->flags;
6611070626
while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
6611170627
*zEnd-- = '\0';
6611270628
}
70629
+ db->flags |= SQLITE_PreferBuiltin;
6611370630
sqlite3NestedParse(pParse,
6611470631
"UPDATE \"%w\".%s SET "
6611570632
"sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
6611670633
"WHERE type = 'table' AND name = %Q",
6611770634
zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
6611870635
zTab
6611970636
);
6612070637
sqlite3DbFree(db, zCol);
70638
+ db->flags = savedDbFlags;
6612170639
}
6612270640
6612370641
/* If the default value of the new column is NULL, then set the file
6612470642
** format to 2. If the default value of the new column is not NULL,
6612570643
** the file format becomes 3.
@@ -66261,11 +70779,11 @@
6626170779
Parse *pParse, /* Parsing context */
6626270780
int iDb, /* The database we are looking in */
6626370781
int iStatCur, /* Open the sqlite_stat1 table on this cursor */
6626470782
const char *zWhere /* Delete entries associated with this table */
6626570783
){
66266
- static struct {
70784
+ static const struct {
6626770785
const char *zName;
6626870786
const char *zCols;
6626970787
} aTable[] = {
6627070788
{ "sqlite_stat1", "tbl,idx,stat" },
6627170789
#ifdef SQLITE_ENABLE_STAT2
@@ -67019,11 +71537,12 @@
6701971537
"attached databases must use the same text encoding as main database");
6702071538
rc = SQLITE_ERROR;
6702171539
}
6702271540
pPager = sqlite3BtreePager(aNew->pBt);
6702371541
sqlite3PagerLockingMode(pPager, db->dfltLockMode);
67024
- sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
71542
+ /* journal_mode set by the OP_JournalMode opcode that will following
71543
+ ** the OP_Function opcode that invoked this function. */
6702571544
sqlite3BtreeSecureDelete(aNew->pBt,
6702671545
sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
6702771546
}
6702871547
aNew->safety_level = 3;
6702971548
aNew->zName = sqlite3DbStrDup(db, zName);
@@ -67164,11 +71683,11 @@
6716471683
** sqlite_detach() or sqlite_attach() SQL user functions.
6716571684
*/
6716671685
static void codeAttach(
6716771686
Parse *pParse, /* The parser context */
6716871687
int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
67169
- FuncDef *pFunc, /* FuncDef wrapper for detachFunc() or attachFunc() */
71688
+ FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
6717071689
Expr *pAuthArg, /* Expression to pass to authorization callback */
6717171690
Expr *pFilename, /* Name of database file */
6717271691
Expr *pDbname, /* Name of the database to use internally */
6717371692
Expr *pKey /* Database key for encryption extension */
6717471693
){
@@ -67214,10 +71733,21 @@
6721471733
if( v ){
6721571734
sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
6721671735
assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
6721771736
sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
6721871737
sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
71738
+
71739
+ if( type==SQLITE_ATTACH ){
71740
+ /* On an attach, also set the journal mode. Note that
71741
+ ** sqlite3VdbeUsesBtree() is not call here since the iDb index
71742
+ ** will be out of range prior to the new database being attached.
71743
+ ** The OP_JournalMode opcode will all sqlite3VdbeUsesBtree() for us.
71744
+ */
71745
+ sqlite3VdbeAddOp3(v, OP_JournalMode, db->nDb, regArgs+3,
71746
+ db->dfltJournalMode);
71747
+ sqlite3VdbeChangeP5(v, 1);
71748
+ }
6721971749
6722071750
/* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
6722171751
** statement only). For DETACH, set it to false (expire all existing
6722271752
** statements).
6722371753
*/
@@ -67234,11 +71764,11 @@
6723471764
** Called by the parser to compile a DETACH statement.
6723571765
**
6723671766
** DETACH pDbname
6723771767
*/
6723871768
SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
67239
- static FuncDef detach_func = {
71769
+ static const FuncDef detach_func = {
6724071770
1, /* nArg */
6724171771
SQLITE_UTF8, /* iPrefEnc */
6724271772
0, /* flags */
6724371773
0, /* pUserData */
6724471774
0, /* pNext */
@@ -67255,11 +71785,11 @@
6725571785
** Called by the parser to compile an ATTACH statement.
6725671786
**
6725771787
** ATTACH p AS pDbname KEY pKey
6725871788
*/
6725971789
SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
67260
- static FuncDef attach_func = {
71790
+ static const FuncDef attach_func = {
6726171791
3, /* nArg */
6726271792
SQLITE_UTF8, /* iPrefEnc */
6726371793
0, /* flags */
6726471794
0, /* pUserData */
6726571795
0, /* pNext */
@@ -71050,11 +75580,11 @@
7105075580
SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
7105175581
char *zName = sqlite3NameFromToken(pParse->db, pName);
7105275582
if( zName ){
7105375583
Vdbe *v = sqlite3GetVdbe(pParse);
7105475584
#ifndef SQLITE_OMIT_AUTHORIZATION
71055
- static const char *az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
75585
+ static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
7105675586
assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
7105775587
#endif
7105875588
if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
7105975589
sqlite3DbFree(pParse->db, zName);
7106075590
return;
@@ -71090,11 +75620,11 @@
7109075620
assert( db->aDb[1].pSchema );
7109175621
if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
7109275622
db->mallocFailed = 1;
7109375623
return 1;
7109475624
}
71095
- sqlite3PagerJournalMode(sqlite3BtreePager(pBt), db->dfltJournalMode);
75625
+ sqlite3PagerSetJournalMode(sqlite3BtreePager(pBt), db->dfltJournalMode);
7109675626
}
7109775627
return 0;
7109875628
}
7109975629
7110075630
/*
@@ -71729,19 +76259,24 @@
7172976259
}
7173076260
p = p->pNext;
7173176261
}
7173276262
7173376263
/* If no match is found, search the built-in functions.
76264
+ **
76265
+ ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
76266
+ ** functions even if a prior app-defined function was found. And give
76267
+ ** priority to built-in functions.
7173476268
**
7173576269
** Except, if createFlag is true, that means that we are trying to
7173676270
** install a new function. Whatever FuncDef structure is returned will
7173776271
** have fields overwritten with new information appropriate for the
7173876272
** new function. But the FuncDefs for built-in functions are read-only.
7173976273
** So we must not search for built-ins when creating a new function.
7174076274
*/
71741
- if( !createFlag && !pBest ){
76275
+ if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
7174276276
FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
76277
+ bestScore = 0;
7174376278
p = functionSearch(pHash, h, zName, nName);
7174476279
while( p ){
7174576280
int score = matchQuality(p, nArg, enc);
7174676281
if( score>bestScore ){
7174776282
pBest = p;
@@ -72337,13 +76872,11 @@
7233776872
/* Populate the OLD.* pseudo-table register array. These values will be
7233876873
** used by any BEFORE and AFTER triggers that exist. */
7233976874
sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
7234076875
for(iCol=0; iCol<pTab->nCol; iCol++){
7234176876
if( mask==0xffffffff || mask&(1<<iCol) ){
72342
- int iTarget = iOld + iCol + 1;
72343
- sqlite3VdbeAddOp3(v, OP_Column, iCur, iCol, iTarget);
72344
- sqlite3ColumnDefault(v, pTab, iCol, iTarget);
76877
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
7234576878
}
7234676879
}
7234776880
7234876881
/* Invoke BEFORE DELETE trigger programs. */
7234976882
sqlite3CodeRowTrigger(pParse, pTrigger,
@@ -73875,24 +78408,19 @@
7387578408
}
7387678409
}
7387778410
}
7387878411
7387978412
/*
73880
-** This function registered all of the above C functions as SQL
73881
-** functions. This should be the only routine in this file with
73882
-** external linkage.
78413
+** This routine does per-connection function registration. Most
78414
+** of the built-in functions above are part of the global function set.
78415
+** This routine only deals with those that are not global.
7388378416
*/
7388478417
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
73885
-#ifndef SQLITE_OMIT_ALTERTABLE
73886
- sqlite3AlterFunctions(db);
73887
-#endif
73888
- if( !db->mallocFailed ){
73889
- int rc = sqlite3_overload_function(db, "MATCH", 2);
73890
- assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
73891
- if( rc==SQLITE_NOMEM ){
73892
- db->mallocFailed = 1;
73893
- }
78418
+ int rc = sqlite3_overload_function(db, "MATCH", 2);
78419
+ assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
78420
+ if( rc==SQLITE_NOMEM ){
78421
+ db->mallocFailed = 1;
7389478422
}
7389578423
}
7389678424
7389778425
/*
7389878426
** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -74056,10 +78584,13 @@
7405678584
7405778585
for(i=0; i<ArraySize(aBuiltinFunc); i++){
7405878586
sqlite3FuncDefInsert(pHash, &aFunc[i]);
7405978587
}
7406078588
sqlite3RegisterDateTimeFunctions();
78589
+#ifndef SQLITE_OMIT_ALTERTABLE
78590
+ sqlite3AlterFunctions();
78591
+#endif
7406178592
}
7406278593
7406378594
/************** End of func.c ************************************************/
7406478595
/************** Begin file fkey.c ********************************************/
7406578596
/*
@@ -78480,10 +83011,35 @@
7848083011
}
7848183012
return zName;
7848283013
}
7848383014
#endif
7848483015
83016
+
83017
+/*
83018
+** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
83019
+** defined in pager.h. This function returns the associated lowercase
83020
+** journal-mode name.
83021
+*/
83022
+SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
83023
+ static char * const azModeName[] = {
83024
+ "delete", "persist", "off", "truncate", "memory"
83025
+#ifndef SQLITE_OMIT_WAL
83026
+ , "wal"
83027
+#endif
83028
+ };
83029
+ assert( PAGER_JOURNALMODE_DELETE==0 );
83030
+ assert( PAGER_JOURNALMODE_PERSIST==1 );
83031
+ assert( PAGER_JOURNALMODE_OFF==2 );
83032
+ assert( PAGER_JOURNALMODE_TRUNCATE==3 );
83033
+ assert( PAGER_JOURNALMODE_MEMORY==4 );
83034
+ assert( PAGER_JOURNALMODE_WAL==5 );
83035
+ assert( eMode>=0 && eMode<=ArraySize(azModeName) );
83036
+
83037
+ if( eMode==ArraySize(azModeName) ) return 0;
83038
+ return azModeName[eMode];
83039
+}
83040
+
7848583041
/*
7848683042
** Process a pragma statement.
7848783043
**
7848883044
** Pragmas are of this form:
7848983045
**
@@ -78552,15 +83108,15 @@
7855283108
** page cache size. The value returned is the maximum number of
7855383109
** pages in the page cache. The second form sets both the current
7855483110
** page cache size value and the persistent page cache size value
7855583111
** stored in the database file.
7855683112
**
78557
- ** The default cache size is stored in meta-value 2 of page 1 of the
78558
- ** database file. The cache size is actually the absolute value of
78559
- ** this memory location. The sign of meta-value 2 determines the
78560
- ** synchronous setting. A negative value means synchronous is off
78561
- ** and a positive value means synchronous is on.
83113
+ ** Older versions of SQLite would set the default cache size to a
83114
+ ** negative number to indicate synchronous=OFF. These days, synchronous
83115
+ ** is always on by default regardless of the sign of the default cache
83116
+ ** size. But continue to take the absolute value of the default cache
83117
+ ** size of historical compatibility.
7856283118
*/
7856383119
if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
7856483120
static const VdbeOpList getCacheSize[] = {
7856583121
{ OP_Transaction, 0, 0, 0}, /* 0 */
7856683122
{ OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
@@ -78585,14 +83141,10 @@
7858583141
}else{
7858683142
int size = atoi(zRight);
7858783143
if( size<0 ) size = -size;
7858883144
sqlite3BeginWriteOperation(pParse, 0, iDb);
7858983145
sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
78590
- sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, BTREE_DEFAULT_CACHE_SIZE);
78591
- addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
78592
- sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
78593
- sqlite3VdbeJumpHere(v, addr);
7859483146
sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
7859583147
pDb->pSchema->cache_size = size;
7859683148
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
7859783149
}
7859883150
}else
@@ -78733,66 +83285,62 @@
7873383285
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
7873483286
}else
7873583287
7873683288
/*
7873783289
** PRAGMA [database.]journal_mode
78738
- ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
83290
+ ** PRAGMA [database.]journal_mode =
83291
+ ** (delete|persist|off|truncate|memory|wal|off)
7873983292
*/
7874083293
if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
78741
- int eMode;
78742
- static char * const azModeName[] = {
78743
- "delete", "persist", "off", "truncate", "memory"
78744
- };
83294
+ int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
83295
+
83296
+ sqlite3VdbeSetNumCols(v, 1);
83297
+ sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
7874583298
7874683299
if( zRight==0 ){
7874783300
eMode = PAGER_JOURNALMODE_QUERY;
7874883301
}else{
83302
+ const char *zMode;
7874983303
int n = sqlite3Strlen30(zRight);
78750
- eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
78751
- while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
78752
- eMode--;
83304
+ for(eMode=0; (zMode = sqlite3JournalModename(eMode)); eMode++){
83305
+ if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
83306
+ }
83307
+ if( !zMode ){
83308
+ eMode = PAGER_JOURNALMODE_QUERY;
7875383309
}
7875483310
}
7875583311
if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
7875683312
/* Simple "PRAGMA journal_mode;" statement. This is a query for
7875783313
** the current default journal mode (which may be different to
7875883314
** the journal-mode of the main database).
7875983315
*/
7876083316
eMode = db->dfltJournalMode;
83317
+ sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
83318
+ sqlite3VdbeChangeP4(v, -1, sqlite3JournalModename(eMode), P4_STATIC);
7876183319
}else{
78762
- Pager *pPager;
83320
+ int ii;
83321
+
7876383322
if( pId2->n==0 ){
78764
- /* This indicates that no database name was specified as part
78765
- ** of the PRAGMA command. In this case the journal-mode must be
78766
- ** set on all attached databases, as well as the main db file.
83323
+ /* When there is no database name before the "journal_mode" keyword
83324
+ ** in the PRAGMA, then the journal-mode will be set on
83325
+ ** all attached databases, as well as the main db file.
7876783326
**
7876883327
** Also, the sqlite3.dfltJournalMode variable is set so that
7876983328
** any subsequently attached databases also use the specified
7877083329
** journal mode.
7877183330
*/
78772
- int ii;
78773
- assert(pDb==&db->aDb[0]);
78774
- for(ii=1; ii<db->nDb; ii++){
78775
- if( db->aDb[ii].pBt ){
78776
- pPager = sqlite3BtreePager(db->aDb[ii].pBt);
78777
- sqlite3PagerJournalMode(pPager, eMode);
78778
- }
78779
- }
7878083331
db->dfltJournalMode = (u8)eMode;
7878183332
}
78782
- pPager = sqlite3BtreePager(pDb->pBt);
78783
- eMode = sqlite3PagerJournalMode(pPager, eMode);
78784
- }
78785
- assert( eMode==PAGER_JOURNALMODE_DELETE
78786
- || eMode==PAGER_JOURNALMODE_TRUNCATE
78787
- || eMode==PAGER_JOURNALMODE_PERSIST
78788
- || eMode==PAGER_JOURNALMODE_OFF
78789
- || eMode==PAGER_JOURNALMODE_MEMORY );
78790
- sqlite3VdbeSetNumCols(v, 1);
78791
- sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
78792
- sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
78793
- azModeName[eMode], P4_STATIC);
83333
+
83334
+ for(ii=db->nDb-1; ii>=0; ii--){
83335
+ if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
83336
+ sqlite3VdbeUsesBtree(v, ii);
83337
+ sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
83338
+ }
83339
+ }
83340
+ }
83341
+
7879483342
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
7879583343
}else
7879683344
7879783345
/*
7879883346
** PRAGMA [database.]journal_size_limit
@@ -79605,10 +84153,40 @@
7960584153
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
7960684154
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
7960784155
}
7960884156
}else
7960984157
#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
84158
+
84159
+#ifndef SQLITE_OMIT_WAL
84160
+ /*
84161
+ ** PRAGMA [database.]wal_checkpoint
84162
+ **
84163
+ ** Checkpoint the database.
84164
+ */
84165
+ if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
84166
+ if( sqlite3ReadSchema(pParse) ) goto pragma_out;
84167
+ sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
84168
+ }else
84169
+
84170
+ /*
84171
+ ** PRAGMA wal_autocheckpoint
84172
+ ** PRAGMA wal_autocheckpoint = N
84173
+ **
84174
+ ** Configure a database connection to automatically checkpoint a database
84175
+ ** after accumulating N frames in the log. Or query for the current value
84176
+ ** of N.
84177
+ */
84178
+ if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
84179
+ if( zRight ){
84180
+ int nAuto = atoi(zRight);
84181
+ sqlite3_wal_autocheckpoint(db, nAuto);
84182
+ }
84183
+ returnSingleInt(pParse, "wal_autocheckpoint",
84184
+ db->xWalCallback==sqlite3WalDefaultHook ?
84185
+ SQLITE_PTR_TO_INT(db->pWalArg) : 0);
84186
+ }else
84187
+#endif
7961084188
7961184189
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
7961284190
/*
7961384191
** Report the current state of file logs for all databases
7961484192
*/
@@ -84278,10 +88856,22 @@
8427888856
p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
8427988857
pGroupBy = p->pGroupBy;
8428088858
p->selFlags &= ~SF_Distinct;
8428188859
isDistinct = 0;
8428288860
}
88861
+
88862
+ /* If there is both a GROUP BY and an ORDER BY clause and they are
88863
+ ** identical, then disable the ORDER BY clause since the GROUP BY
88864
+ ** will cause elements to come out in the correct order. This is
88865
+ ** an optimization - the correct answer should result regardless.
88866
+ ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
88867
+ ** to disable this optimization for testing purposes.
88868
+ */
88869
+ if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
88870
+ && (db->flags & SQLITE_GroupByOrder)==0 ){
88871
+ pOrderBy = 0;
88872
+ }
8428388873
8428488874
/* If there is an ORDER BY clause, then this sorting
8428588875
** index might end up being unused if the data can be
8428688876
** extracted in pre-sorted order. If that is the case, then the
8428788877
** OP_OpenEphemeral instruction will be changed to an OP_Noop once
@@ -86161,11 +90751,11 @@
8616190751
**
8616290752
** May you do good and not evil.
8616390753
** May you find forgiveness for yourself and forgive others.
8616490754
** May you share freely, never taking more than you give.
8616590755
**
86166
-*************************************************************************
90756
+sqlite*************************************************************************
8616790757
** This file contains C code routines that are called by the parser
8616890758
** to handle UPDATE statements.
8616990759
*/
8617090760
8617190761
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -86548,12 +91138,11 @@
8654891138
oldmask |= sqlite3TriggerColmask(pParse,
8654991139
pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
8655091140
);
8655191141
for(i=0; i<pTab->nCol; i++){
8655291142
if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<<i)) ){
86553
- sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regOld+i);
86554
- sqlite3ColumnDefault(v, pTab, i, regOld+i);
91143
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
8655591144
}else{
8655691145
sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
8655791146
}
8655891147
}
8655991148
if( chngRowid==0 ){
@@ -86922,11 +91511,12 @@
8692291511
int saved_nChange; /* Saved value of db->nChange */
8692391512
int saved_nTotalChange; /* Saved value of db->nTotalChange */
8692491513
void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
8692591514
Db *pDb = 0; /* Database to detach at end of vacuum */
8692691515
int isMemDb; /* True if vacuuming a :memory: database */
86927
- int nRes;
91516
+ int nRes; /* Bytes of reserved space at the end of each page */
91517
+ int nDb; /* Number of attached databases */
8692891518
8692991519
if( !db->autoCommit ){
8693091520
sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
8693191521
return SQLITE_ERROR;
8693291522
}
@@ -86936,11 +91526,11 @@
8693691526
** disable CHECK and foreign key constraints. */
8693791527
saved_flags = db->flags;
8693891528
saved_nChange = db->nChange;
8693991529
saved_nTotalChange = db->nTotalChange;
8694091530
saved_xTrace = db->xTrace;
86941
- db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
91531
+ db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
8694291532
db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
8694391533
db->xTrace = 0;
8694491534
8694591535
pMain = db->aDb[0].pBt;
8694691536
isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
@@ -86957,19 +91547,22 @@
8695791547
** actually occurs when doing a vacuum since the vacuum_db is initially
8695891548
** empty. Only the journal header is written. Apparently it takes more
8695991549
** time to parse and run the PRAGMA to turn journalling off than it does
8696091550
** to write the journal header file.
8696191551
*/
91552
+ nDb = db->nDb;
8696291553
if( sqlite3TempInMemory(db) ){
8696391554
zSql = "ATTACH ':memory:' AS vacuum_db;";
8696491555
}else{
8696591556
zSql = "ATTACH '' AS vacuum_db;";
8696691557
}
8696791558
rc = execSql(db, pzErrMsg, zSql);
91559
+ if( db->nDb>nDb ){
91560
+ pDb = &db->aDb[db->nDb-1];
91561
+ assert( strcmp(pDb->zName,"vacuum_db")==0 );
91562
+ }
8696891563
if( rc!=SQLITE_OK ) goto end_of_vacuum;
86969
- pDb = &db->aDb[db->nDb-1];
86970
- assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
8697191564
pTemp = db->aDb[db->nDb-1].pBt;
8697291565
8697391566
/* The call to execSql() to attach the temp database has left the file
8697491567
** locked (as there was more than one active statement when the transaction
8697591568
** to read the schema was concluded. Unlock it here so that this doesn't
@@ -86986,10 +91579,16 @@
8698691579
char *zKey;
8698791580
sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
8698891581
if( nKey ) db->nextPagesize = 0;
8698991582
}
8699091583
#endif
91584
+
91585
+ /* Do not attempt to change the page size for a WAL database */
91586
+ if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
91587
+ ==PAGER_JOURNALMODE_WAL ){
91588
+ db->nextPagesize = 0;
91589
+ }
8699191590
8699291591
if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
8699391592
|| (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
8699491593
|| NEVER(db->mallocFailed)
8699591594
){
@@ -87123,10 +91722,11 @@
8712391722
/* Restore the original value of db->flags */
8712491723
db->flags = saved_flags;
8712591724
db->nChange = saved_nChange;
8712691725
db->nTotalChange = saved_nTotalChange;
8712791726
db->xTrace = saved_xTrace;
91727
+ sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
8712891728
8712991729
/* Currently there is an SQL level transaction open on the vacuum
8713091730
** database. No locks are held on any other files (since the main file
8713191731
** was committed at the btree level). So it safe to end the transaction
8713291732
** by manually setting the autoCommit flag to true and detaching the
@@ -90988,11 +95588,11 @@
9098895588
** as we can without disabling too much. If we disabled in (1), we'd get
9098995589
** the wrong answer. See ticket #813.
9099095590
*/
9099195591
static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
9099295592
if( pTerm
90993
- && ALWAYS((pTerm->wtFlags & TERM_CODED)==0)
95593
+ && (pTerm->wtFlags & TERM_CODED)==0
9099495594
&& (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
9099595595
){
9099695596
pTerm->wtFlags |= TERM_CODED;
9099795597
if( pTerm->iParent>=0 ){
9099895598
WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
@@ -91186,11 +95786,13 @@
9118695786
for(j=0; j<nEq; j++){
9118795787
int r1;
9118895788
int k = pIdx->aiColumn[j];
9118995789
pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
9119095790
if( NEVER(pTerm==0) ) break;
91191
- assert( (pTerm->wtFlags & TERM_CODED)==0 );
95791
+ /* The following true for indices with redundant columns.
95792
+ ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
95793
+ testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
9119295794
r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
9119395795
if( r1!=regBase+j ){
9119495796
if( nReg==1 ){
9119595797
sqlite3ReleaseTempReg(pParse, regBase);
9119695798
regBase = r1;
@@ -91473,11 +96075,12 @@
9147396075
int nConstraint; /* Number of constraint terms */
9147496076
Index *pIdx; /* The index we will be using */
9147596077
int iIdxCur; /* The VDBE cursor for the index */
9147696078
int nExtraReg = 0; /* Number of extra registers needed */
9147796079
int op; /* Instruction opcode */
91478
- char *zAff;
96080
+ char *zStartAff; /* Affinity for start of range constraint */
96081
+ char *zEndAff; /* Affinity for end of range constraint */
9147996082
9148096083
pIdx = pLevel->plan.u.pIdx;
9148196084
iIdxCur = pLevel->iIdxCur;
9148296085
k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
9148396086
@@ -91514,12 +96117,13 @@
9151496117
/* Generate code to evaluate all constraint terms using == or IN
9151596118
** and store the values of those terms in an array of registers
9151696119
** starting at regBase.
9151796120
*/
9151896121
regBase = codeAllEqualityTerms(
91519
- pParse, pLevel, pWC, notReady, nExtraReg, &zAff
96122
+ pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
9152096123
);
96124
+ zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
9152196125
addrNxt = pLevel->addrNxt;
9152296126
9152396127
/* If we are doing a reverse order scan on an ascending index, or
9152496128
** a forward order scan on a descending index, interchange the
9152596129
** start and end terms (pRangeStart and pRangeEnd).
@@ -91540,29 +96144,29 @@
9154096144
nConstraint = nEq;
9154196145
if( pRangeStart ){
9154296146
Expr *pRight = pRangeStart->pExpr->pRight;
9154396147
sqlite3ExprCode(pParse, pRight, regBase+nEq);
9154496148
sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
91545
- if( zAff ){
91546
- if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
96149
+ if( zStartAff ){
96150
+ if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
9154796151
/* Since the comparison is to be performed with no conversions
9154896152
** applied to the operands, set the affinity to apply to pRight to
9154996153
** SQLITE_AFF_NONE. */
91550
- zAff[nConstraint] = SQLITE_AFF_NONE;
96154
+ zStartAff[nEq] = SQLITE_AFF_NONE;
9155196155
}
91552
- if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
91553
- zAff[nConstraint] = SQLITE_AFF_NONE;
96156
+ if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
96157
+ zStartAff[nEq] = SQLITE_AFF_NONE;
9155496158
}
9155596159
}
9155696160
nConstraint++;
9155796161
}else if( isMinQuery ){
9155896162
sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
9155996163
nConstraint++;
9156096164
startEq = 0;
9156196165
start_constraints = 1;
9156296166
}
91563
- codeApplyAffinity(pParse, regBase, nConstraint, zAff);
96167
+ codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
9156496168
op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
9156596169
assert( op!=0 );
9156696170
testcase( op==OP_Rewind );
9156796171
testcase( op==OP_Last );
9156896172
testcase( op==OP_SeekGt );
@@ -91578,25 +96182,26 @@
9157896182
if( pRangeEnd ){
9157996183
Expr *pRight = pRangeEnd->pExpr->pRight;
9158096184
sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
9158196185
sqlite3ExprCode(pParse, pRight, regBase+nEq);
9158296186
sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
91583
- if( zAff ){
91584
- if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
96187
+ if( zEndAff ){
96188
+ if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
9158596189
/* Since the comparison is to be performed with no conversions
9158696190
** applied to the operands, set the affinity to apply to pRight to
9158796191
** SQLITE_AFF_NONE. */
91588
- zAff[nConstraint] = SQLITE_AFF_NONE;
96192
+ zEndAff[nEq] = SQLITE_AFF_NONE;
9158996193
}
91590
- if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
91591
- zAff[nConstraint] = SQLITE_AFF_NONE;
96194
+ if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
96195
+ zEndAff[nEq] = SQLITE_AFF_NONE;
9159296196
}
9159396197
}
91594
- codeApplyAffinity(pParse, regBase, nEq+1, zAff);
96198
+ codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
9159596199
nConstraint++;
9159696200
}
91597
- sqlite3DbFree(pParse->db, zAff);
96201
+ sqlite3DbFree(pParse->db, zStartAff);
96202
+ sqlite3DbFree(pParse->db, zEndAff);
9159896203
9159996204
/* Top of the loop body */
9160096205
pLevel->p2 = sqlite3VdbeCurrentAddr(v);
9160196206
9160296207
/* Check if the index cursor is past the end of the range. */
@@ -97936,11 +102541,11 @@
97936102541
/* SQLITE_IOERR */ "disk I/O error",
97937102542
/* SQLITE_CORRUPT */ "database disk image is malformed",
97938102543
/* SQLITE_NOTFOUND */ 0,
97939102544
/* SQLITE_FULL */ "database or disk is full",
97940102545
/* SQLITE_CANTOPEN */ "unable to open database file",
97941
- /* SQLITE_PROTOCOL */ 0,
102546
+ /* SQLITE_PROTOCOL */ "locking protocol",
97942102547
/* SQLITE_EMPTY */ "table contains no data",
97943102548
/* SQLITE_SCHEMA */ "database schema has changed",
97944102549
/* SQLITE_TOOBIG */ "string or blob too big",
97945102550
/* SQLITE_CONSTRAINT */ "constraint failed",
97946102551
/* SQLITE_MISMATCH */ "datatype mismatch",
@@ -98346,10 +102951,149 @@
98346102951
db->pRollbackArg = pArg;
98347102952
sqlite3_mutex_leave(db->mutex);
98348102953
return pRet;
98349102954
}
98350102955
102956
+#ifndef SQLITE_OMIT_WAL
102957
+/*
102958
+** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
102959
+** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
102960
+** is greater than sqlite3.pWalArg cast to an integer (the value configured by
102961
+** wal_autocheckpoint()).
102962
+*/
102963
+SQLITE_PRIVATE int sqlite3WalDefaultHook(
102964
+ void *pClientData, /* Argument */
102965
+ sqlite3 *db, /* Connection */
102966
+ const char *zDb, /* Database */
102967
+ int nFrame /* Size of WAL */
102968
+){
102969
+ if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
102970
+ sqlite3BeginBenignMalloc();
102971
+ sqlite3_wal_checkpoint(db, zDb);
102972
+ sqlite3EndBenignMalloc();
102973
+ }
102974
+ return SQLITE_OK;
102975
+}
102976
+#endif /* SQLITE_OMIT_WAL */
102977
+
102978
+/*
102979
+** Configure an sqlite3_wal_hook() callback to automatically checkpoint
102980
+** a database after committing a transaction if there are nFrame or
102981
+** more frames in the log file. Passing zero or a negative value as the
102982
+** nFrame parameter disables automatic checkpoints entirely.
102983
+**
102984
+** The callback registered by this function replaces any existing callback
102985
+** registered using sqlite3_wal_hook(). Likewise, registering a callback
102986
+** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
102987
+** configured by this function.
102988
+*/
102989
+SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
102990
+#ifndef SQLITE_OMIT_WAL
102991
+ if( nFrame>0 ){
102992
+ sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
102993
+ }else{
102994
+ sqlite3_wal_hook(db, 0, 0);
102995
+ }
102996
+#endif
102997
+ return SQLITE_OK;
102998
+}
102999
+
103000
+/*
103001
+** Register a callback to be invoked each time a transaction is written
103002
+** into the write-ahead-log by this database connection.
103003
+*/
103004
+SQLITE_API void *sqlite3_wal_hook(
103005
+ sqlite3 *db, /* Attach the hook to this db handle */
103006
+ int(*xCallback)(void *, sqlite3*, const char*, int),
103007
+ void *pArg /* First argument passed to xCallback() */
103008
+){
103009
+#ifndef SQLITE_OMIT_WAL
103010
+ void *pRet;
103011
+ sqlite3_mutex_enter(db->mutex);
103012
+ pRet = db->pWalArg;
103013
+ db->xWalCallback = xCallback;
103014
+ db->pWalArg = pArg;
103015
+ sqlite3_mutex_leave(db->mutex);
103016
+ return pRet;
103017
+#else
103018
+ return 0;
103019
+#endif
103020
+}
103021
+
103022
+
103023
+/*
103024
+** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
103025
+** to contains a zero-length string, all attached databases are
103026
+** checkpointed.
103027
+*/
103028
+SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
103029
+#ifdef SQLITE_OMIT_WAL
103030
+ return SQLITE_OK;
103031
+#else
103032
+ int rc; /* Return code */
103033
+ int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
103034
+
103035
+ sqlite3_mutex_enter(db->mutex);
103036
+ if( zDb && zDb[0] ){
103037
+ iDb = sqlite3FindDbName(db, zDb);
103038
+ }
103039
+ if( iDb<0 ){
103040
+ rc = SQLITE_ERROR;
103041
+ sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
103042
+ }else{
103043
+ rc = sqlite3Checkpoint(db, iDb);
103044
+ sqlite3Error(db, rc, 0);
103045
+ }
103046
+ rc = sqlite3ApiExit(db, rc);
103047
+ sqlite3_mutex_leave(db->mutex);
103048
+ return rc;
103049
+#endif
103050
+}
103051
+
103052
+#ifndef SQLITE_OMIT_WAL
103053
+/*
103054
+** Run a checkpoint on database iDb. This is a no-op if database iDb is
103055
+** not currently open in WAL mode.
103056
+**
103057
+** If a transaction is open on the database being checkpointed, this
103058
+** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
103059
+** an error occurs while running the checkpoint, an SQLite error code is
103060
+** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
103061
+**
103062
+** The mutex on database handle db should be held by the caller. The mutex
103063
+** associated with the specific b-tree being checkpointed is taken by
103064
+** this function while the checkpoint is running.
103065
+**
103066
+** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
103067
+** checkpointed. If an error is encountered it is returned immediately -
103068
+** no attempt is made to checkpoint any remaining databases.
103069
+*/
103070
+SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb){
103071
+ int rc = SQLITE_OK; /* Return code */
103072
+ int i; /* Used to iterate through attached dbs */
103073
+
103074
+ assert( sqlite3_mutex_held(db->mutex) );
103075
+
103076
+ for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
103077
+ if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
103078
+ Btree *pBt = db->aDb[i].pBt;
103079
+ if( pBt ){
103080
+ if( sqlite3BtreeIsInReadTrans(pBt) ){
103081
+ rc = SQLITE_LOCKED;
103082
+ }else{
103083
+ sqlite3BtreeEnter(pBt);
103084
+ rc = sqlite3PagerCheckpoint(sqlite3BtreePager(pBt));
103085
+ sqlite3BtreeLeave(pBt);
103086
+ }
103087
+ }
103088
+ }
103089
+ }
103090
+
103091
+ return rc;
103092
+}
103093
+#endif /* SQLITE_OMIT_WAL */
103094
+
98351103095
/*
98352103096
** This function returns true if main-memory should be used instead of
98353103097
** a temporary file for transient pager files and statement journals.
98354103098
** The value returned depends on the value of db->temp_store (runtime
98355103099
** parameter) and the compile time value of SQLITE_TEMP_STORE. The
@@ -98906,10 +103650,12 @@
98906103650
98907103651
/* Enable the lookaside-malloc subsystem */
98908103652
setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
98909103653
sqlite3GlobalConfig.nLookaside);
98910103654
103655
+ sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
103656
+
98911103657
opendb_out:
98912103658
if( db ){
98913103659
assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
98914103660
sqlite3_mutex_leave(db->mutex);
98915103661
}
@@ -99400,13 +104146,17 @@
99400104146
** an incompatible database file format. Changing the PENDING byte
99401104147
** while any database connection is open results in undefined and
99402104148
** dileterious behavior.
99403104149
*/
99404104150
case SQLITE_TESTCTRL_PENDING_BYTE: {
99405
- unsigned int newVal = va_arg(ap, unsigned int);
99406
- rc = sqlite3PendingByte;
99407
- if( newVal ) sqlite3PendingByte = newVal;
104151
+ rc = PENDING_BYTE;
104152
+#ifndef SQLITE_OMIT_WSD
104153
+ {
104154
+ unsigned int newVal = va_arg(ap, unsigned int);
104155
+ if( newVal ) sqlite3PendingByte = newVal;
104156
+ }
104157
+#endif
99408104158
break;
99409104159
}
99410104160
99411104161
/*
99412104162
** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
@@ -99505,10 +104255,19 @@
99505104255
int n = sqlite3Strlen30(zWord);
99506104256
rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
99507104257
break;
99508104258
}
99509104259
#endif
104260
+
104261
+ /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
104262
+ **
104263
+ ** Return the size of a pcache header in bytes.
104264
+ */
104265
+ case SQLITE_TESTCTRL_PGHDRSZ: {
104266
+ rc = sizeof(PgHdr);
104267
+ break;
104268
+ }
99510104269
99511104270
}
99512104271
va_end(ap);
99513104272
#endif /* SQLITE_OMIT_BUILTIN_TEST */
99514104273
return rc;
@@ -99671,10 +104430,11 @@
99671104430
sqlite3_mutex_enter(db->mutex);
99672104431
enterMutex();
99673104432
99674104433
if( xNotify==0 ){
99675104434
removeFromBlockedList(db);
104435
+ db->pBlockingConnection = 0;
99676104436
db->pUnlockConnection = 0;
99677104437
db->xUnlockNotify = 0;
99678104438
db->pUnlockArg = 0;
99679104439
}else if( 0==db->pBlockingConnection ){
99680104440
/* The blocking transaction has been concluded. Or there never was a
99681104441
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.6.23. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -188,10 +188,18 @@
188 #endif
189 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
190 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500
191 #endif
192
 
 
 
 
 
 
 
 
193 /*
194 ** The maximum number of attached databases. This must be between 0
195 ** and 30. The upper bound on 30 is because a 32-bit integer bitmap
196 ** is used internally to track attached databases.
197 */
@@ -626,13 +634,13 @@
626 **
627 ** See also: [sqlite3_libversion()],
628 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
629 ** [sqlite_version()] and [sqlite_source_id()].
630 */
631 #define SQLITE_VERSION "3.6.23"
632 #define SQLITE_VERSION_NUMBER 3006023
633 #define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
634
635 /*
636 ** CAPI3REF: Run-Time Library Version Numbers
637 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
638 **
@@ -912,11 +920,11 @@
912 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
913 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
914 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
915 #define SQLITE_FULL 13 /* Insertion failed because database is full */
916 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
917 #define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
918 #define SQLITE_EMPTY 16 /* Database is empty */
919 #define SQLITE_SCHEMA 17 /* The database schema changed */
920 #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
921 #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
922 #define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -968,11 +976,16 @@
968 #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
969 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
970 #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
971 #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
972 #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
973 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
 
 
 
 
 
974
975 /*
976 ** CAPI3REF: Flags For File Open Operations
977 **
978 ** These bit values are intended for use in the
@@ -1177,10 +1190,17 @@
1177 int (*xUnlock)(sqlite3_file*, int);
1178 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1179 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1180 int (*xSectorSize)(sqlite3_file*);
1181 int (*xDeviceCharacteristics)(sqlite3_file*);
 
 
 
 
 
 
 
1182 /* Additional methods may be added in future releases */
1183 };
1184
1185 /*
1186 ** CAPI3REF: Standard File Control Opcodes
@@ -1194,15 +1214,23 @@
1194 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1195 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1196 ** into an integer that the pArg argument points to. This capability
1197 ** is used during testing and only needs to be supported when SQLITE_TEST
1198 ** is defined.
 
 
 
 
 
 
 
1199 */
1200 #define SQLITE_FCNTL_LOCKSTATE 1
1201 #define SQLITE_GET_LOCKPROXYFILE 2
1202 #define SQLITE_SET_LOCKPROXYFILE 3
1203 #define SQLITE_LAST_ERRNO 4
 
1204
1205 /*
1206 ** CAPI3REF: Mutex Handle
1207 **
1208 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1343,11 +1371,11 @@
1343 ** method returns a Julian Day Number for the current date and time.
1344 **
1345 */
1346 typedef struct sqlite3_vfs sqlite3_vfs;
1347 struct sqlite3_vfs {
1348 int iVersion; /* Structure version number */
1349 int szOsFile; /* Size of subclassed sqlite3_file */
1350 int mxPathname; /* Maximum file pathname length */
1351 sqlite3_vfs *pNext; /* Next registered VFS */
1352 const char *zName; /* Name of this virtual file system */
1353 void *pAppData; /* Pointer to application-specific data */
@@ -1362,12 +1390,21 @@
1362 void (*xDlClose)(sqlite3_vfs*, void*);
1363 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1364 int (*xSleep)(sqlite3_vfs*, int microseconds);
1365 int (*xCurrentTime)(sqlite3_vfs*, double*);
1366 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1367 /* New fields may be appended in figure versions. The iVersion
1368 ** value will increment whenever this happens. */
 
 
 
 
 
 
 
 
 
1369 };
1370
1371 /*
1372 ** CAPI3REF: Flags for the xAccess VFS method
1373 **
@@ -1382,10 +1419,48 @@
1382 ** checks whether the file is readable.
1383 */
1384 #define SQLITE_ACCESS_EXISTS 0
1385 #define SQLITE_ACCESS_READWRITE 1
1386 #define SQLITE_ACCESS_READ 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1387
1388 /*
1389 ** CAPI3REF: Initialize The SQLite Library
1390 **
1391 ** ^The sqlite3_initialize() routine initializes the
@@ -3409,10 +3484,18 @@
3409 ** already been [sqlite3_finalize | finalized] or on one that had
3410 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3411 ** be the case that the same database connection is being used by two or
3412 ** more threads at the same moment in time.
3413 **
 
 
 
 
 
 
 
 
3414 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3415 ** API always returns a generic error code, [SQLITE_ERROR], following any
3416 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3417 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3418 ** specific [error codes] that better describes the error.
@@ -5507,11 +5590,12 @@
5507 #define SQLITE_TESTCTRL_ASSERT 12
5508 #define SQLITE_TESTCTRL_ALWAYS 13
5509 #define SQLITE_TESTCTRL_RESERVE 14
5510 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
5511 #define SQLITE_TESTCTRL_ISKEYWORD 16
5512 #define SQLITE_TESTCTRL_LAST 16
 
5513
5514 /*
5515 ** CAPI3REF: SQLite Runtime Status
5516 **
5517 ** ^This interface is used to retrieve runtime status information
@@ -5971,14 +6055,18 @@
5971 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
5972 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
5973 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
5974 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
5975 **
5976 ** ^The sqlite3_backup_step() might return [SQLITE_READONLY] if the destination
5977 ** database was opened read-only or if
5978 ** the destination is an in-memory database with a different page size
5979 ** from the source database.
 
 
 
 
5980 **
5981 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
5982 ** the [sqlite3_busy_handler | busy-handler function]
5983 ** is invoked (if one is specified). ^If the
5984 ** busy-handler returns non-zero before the lock is available, then
@@ -6242,10 +6330,93 @@
6242 ** a few hundred characters, it will be truncated to the length of the
6243 ** buffer.
6244 */
6245 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
6246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6247 /*
6248 ** Undo the hack that converts floating point types to integer for
6249 ** builds on processors without floating point support.
6250 */
6251 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7050,10 +7221,12 @@
7050
7051 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
7052 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
7053 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
7054
 
 
7055 #ifndef NDEBUG
7056 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
7057 #endif
7058
7059 #ifndef SQLITE_OMIT_BTREECOUNT
@@ -7396,31 +7569,31 @@
7396 #define OP_IfPos 113
7397 #define OP_IfNeg 114
7398 #define OP_IfZero 115
7399 #define OP_AggStep 116
7400 #define OP_AggFinal 117
7401 #define OP_Vacuum 118
7402 #define OP_IncrVacuum 119
7403 #define OP_Expire 120
7404 #define OP_TableLock 121
7405 #define OP_VBegin 122
7406 #define OP_VCreate 123
7407 #define OP_VDestroy 124
7408 #define OP_VOpen 125
7409 #define OP_VFilter 126
7410 #define OP_VColumn 127
7411 #define OP_VNext 128
7412 #define OP_VRename 129
7413 #define OP_VUpdate 131
7414 #define OP_Pagecount 132
7415 #define OP_Trace 133
7416 #define OP_Noop 134
7417 #define OP_Explain 135
 
 
7418
7419 /* The following opcode values are never used */
7420 #define OP_NotUsed_136 136
7421 #define OP_NotUsed_137 137
7422 #define OP_NotUsed_138 138
7423 #define OP_NotUsed_139 139
7424 #define OP_NotUsed_140 140
7425
7426
@@ -7435,11 +7608,11 @@
7435 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
7436 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
7437 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
7438 #define OPFLG_INITIALIZER {\
7439 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
7440 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24, 0x24,\
7441 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
7442 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
7443 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
7444 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
7445 /* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
@@ -7448,13 +7621,13 @@
7448 /* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
7449 /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
7450 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
7451 /* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
7452 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
7453 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01,\
7454 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,\
7455 /* 128 */ 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,\
7456 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
7457 /* 144 */ 0x04, 0x04,}
7458
7459 /************** End of opcodes.h *********************************************/
7460 /************** Continuing where we left off in vdbe.h ***********************/
@@ -7596,18 +7769,19 @@
7596 #define PAGER_LOCKINGMODE_QUERY -1
7597 #define PAGER_LOCKINGMODE_NORMAL 0
7598 #define PAGER_LOCKINGMODE_EXCLUSIVE 1
7599
7600 /*
7601 ** Valid values for the second argument to sqlite3PagerJournalMode().
7602 */
7603 #define PAGER_JOURNALMODE_QUERY -1
7604 #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
7605 #define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
7606 #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
7607 #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
7608 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
 
7609
7610 /*
7611 ** The remainder of this file contains the declarations of the functions
7612 ** that make up the Pager sub-system API. See source code comments for
7613 ** a detailed description of each routine.
@@ -7631,11 +7805,13 @@
7631 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
7632 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7633 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7634 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7635 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7636 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
 
 
7637 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
7638 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
7639
7640 /* Functions used to obtain and release page references. */
7641 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
@@ -7660,10 +7836,16 @@
7660 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
7661 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
7662 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
7663 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
7664 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
 
 
 
 
 
 
7665
7666 /* Functions used to query pager state and configuration. */
7667 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
7668 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
7669 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
@@ -8077,11 +8259,15 @@
8077 ** the incompatibility right away, even running a full regression test.
8078 ** The default location of PENDING_BYTE is the first byte past the
8079 ** 1GB boundary.
8080 **
8081 */
8082 #define PENDING_BYTE sqlite3PendingByte
 
 
 
 
8083 #define RESERVED_BYTE (PENDING_BYTE+1)
8084 #define SHARED_FIRST (PENDING_BYTE+2)
8085 #define SHARED_SIZE 510
8086
8087 /*
@@ -8103,10 +8289,15 @@
8103 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
8104 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
8105 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
8106 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
8107 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
 
 
 
 
 
8108
8109 /*
8110 ** Functions for accessing sqlite3_vfs methods
8111 */
8112 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -8119,11 +8310,11 @@
8119 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
8120 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
8121 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
8122 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
8123 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
8124 SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
8125
8126 /*
8127 ** Convenience functions for opening and closing files using
8128 ** sqlite3_malloc() to obtain space for the file-handle structure.
8129 */
@@ -8396,10 +8587,14 @@
8396 int (*xCommitCallback)(void*); /* Invoked at every commit. */
8397 void *pRollbackArg; /* Argument to xRollbackCallback() */
8398 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
8399 void *pUpdateArg;
8400 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
 
 
 
 
8401 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
8402 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
8403 void *pCollNeededArg;
8404 sqlite3_value *pErr; /* Most recent error message */
8405 char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
@@ -8486,10 +8681,11 @@
8486 #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
8487 #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
8488 #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
8489 #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
8490 #define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
 
8491
8492 /*
8493 ** Bits of the sqlite3.flags field that are used by the
8494 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
8495 ** These must be the low-order bits of the flags field.
@@ -8497,11 +8693,12 @@
8497 #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
8498 #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
8499 #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
8500 #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
8501 #define SQLITE_IndexCover 0x10 /* Disable index covering table */
8502 #define SQLITE_OptMask 0x1f /* Mask of all disablable opts */
 
8503
8504 /*
8505 ** Possible values for the sqlite.magic field.
8506 ** The numbers are obtained at random and have no special meaning, other
8507 ** than being distinct from one another.
@@ -10103,11 +10300,12 @@
10103 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10104 #endif
10105
10106
10107 #ifndef SQLITE_MUTEX_OMIT
10108 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
 
10109 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
10110 SQLITE_PRIVATE int sqlite3MutexInit(void);
10111 SQLITE_PRIVATE int sqlite3MutexEnd(void);
10112 #endif
10113
@@ -10235,10 +10433,11 @@
10235 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
10236 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
10237 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
10238 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
10239 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
 
10240 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
10241 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
10242 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
10243 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
10244 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
@@ -10261,10 +10460,11 @@
10261 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
10262 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
10263 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
10264 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
10265 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
 
10266 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
10267 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
10268 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
10269 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
10270 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
@@ -10450,15 +10650,17 @@
10450 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
10451 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
10452 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
10453 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
10454 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
 
10455 SQLITE_PRIVATE int sqlite3PendingByte;
 
10456 #endif
10457 SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
10458 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
10459 SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3*);
10460 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
10461 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
10462 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
10463 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
10464 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
@@ -10563,10 +10765,13 @@
10563 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
10564 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
10565 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
10566 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
10567 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
 
 
 
10568
10569 /* Declarations for functions in fkey.c. All of these are replaced by
10570 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
10571 ** key functionality is available. If OMIT_TRIGGER is defined but
10572 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
@@ -10907,11 +11112,13 @@
10907 ** IMPORTANT: Changing the pending byte to any value other than
10908 ** 0x40000000 results in an incompatible database file format!
10909 ** Changing the pending byte during operating results in undefined
10910 ** and dileterious behavior.
10911 */
 
10912 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
 
10913
10914 /*
10915 ** Properties of opcodes. The OPFLG_INITIALIZER macro is
10916 ** created by mkopcodeh.awk during compilation. Data is obtained
10917 ** from the comments following the "case OP_xxxx:" statements in
@@ -11764,14 +11971,12 @@
11764
11765 /*
11766 ** Set the time to the current time reported by the VFS
11767 */
11768 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
11769 double r;
11770 sqlite3 *db = sqlite3_context_db_handle(context);
11771 sqlite3OsCurrentTime(db->pVfs, &r);
11772 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
11773 p->validJD = 1;
11774 }
11775
11776 /*
11777 ** Attempt to parse the given string into a Julian Day Number. Return
@@ -12488,26 +12693,19 @@
12488 sqlite3_value **argv
12489 ){
12490 time_t t;
12491 char *zFormat = (char *)sqlite3_user_data(context);
12492 sqlite3 *db;
12493 double rT;
12494 char zBuf[20];
12495
12496 UNUSED_PARAMETER(argc);
12497 UNUSED_PARAMETER(argv);
12498
12499 db = sqlite3_context_db_handle(context);
12500 sqlite3OsCurrentTime(db->pVfs, &rT);
12501 #ifndef SQLITE_OMIT_FLOATING_POINT
12502 t = 86400.0*(rT - 2440587.5) + 0.5;
12503 #else
12504 /* without floating point support, rT will have
12505 ** already lost fractional day precision.
12506 */
12507 t = 86400 * (rT - 2440587) - 43200;
12508 #endif
12509 #ifdef HAVE_GMTIME_R
12510 {
12511 struct tm sNow;
12512 gmtime_r(&t, &sNow);
12513 strftime(zBuf, 20, zFormat, &sNow);
@@ -12656,10 +12854,31 @@
12656 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
12657 }
12658 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
12659 return id->pMethods->xDeviceCharacteristics(id);
12660 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12661
12662 /*
12663 ** The next group of routines are convenience wrappers around the
12664 ** VFS methods.
12665 */
@@ -12719,12 +12938,20 @@
12719 return pVfs->xRandomness(pVfs, nByte, zBufOut);
12720 }
12721 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
12722 return pVfs->xSleep(pVfs, nMicro);
12723 }
12724 SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
12725 return pVfs->xCurrentTime(pVfs, pTimeOut);
 
 
 
 
 
 
 
 
12726 }
12727
12728 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
12729 sqlite3_vfs *pVfs,
12730 const char *zFile,
@@ -14974,27 +15201,30 @@
14974 /*
14975 ** Initialize the mutex system.
14976 */
14977 SQLITE_PRIVATE int sqlite3MutexInit(void){
14978 int rc = SQLITE_OK;
14979 if( sqlite3GlobalConfig.bCoreMutex ){
14980 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
14981 /* If the xMutexAlloc method has not been set, then the user did not
14982 ** install a mutex implementation via sqlite3_config() prior to
14983 ** sqlite3_initialize() being called. This block copies pointers to
14984 ** the default implementation into the sqlite3GlobalConfig structure.
14985 */
14986 sqlite3_mutex_methods *pFrom = sqlite3DefaultMutex();
14987 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
14988
14989 memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
14990 memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
14991 sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
14992 pTo->xMutexAlloc = pFrom->xMutexAlloc;
14993 }
14994 rc = sqlite3GlobalConfig.mutex.xMutexInit();
14995 }
 
 
 
14996
14997 #ifdef SQLITE_DEBUG
14998 GLOBAL(int, mutexIsInit) = 1;
14999 #endif
15000
@@ -15122,68 +15352,69 @@
15122 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
15123 ** that does error checking on mutexes to make sure they are being
15124 ** called correctly.
15125 */
15126
 
15127
15128 #if defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG)
15129 /*
15130 ** Stub routines for all mutex methods.
15131 **
15132 ** This routines provide no mutual exclusion or error checking.
15133 */
15134 static int noopMutexHeld(sqlite3_mutex *p){ return 1; }
15135 static int noopMutexNotheld(sqlite3_mutex *p){ return 1; }
15136 static int noopMutexInit(void){ return SQLITE_OK; }
15137 static int noopMutexEnd(void){ return SQLITE_OK; }
15138 static sqlite3_mutex *noopMutexAlloc(int id){ return (sqlite3_mutex*)8; }
15139 static void noopMutexFree(sqlite3_mutex *p){ return; }
15140 static void noopMutexEnter(sqlite3_mutex *p){ return; }
15141 static int noopMutexTry(sqlite3_mutex *p){ return SQLITE_OK; }
15142 static void noopMutexLeave(sqlite3_mutex *p){ return; }
15143
15144 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15145 static sqlite3_mutex_methods sMutex = {
15146 noopMutexInit,
15147 noopMutexEnd,
15148 noopMutexAlloc,
15149 noopMutexFree,
15150 noopMutexEnter,
15151 noopMutexTry,
15152 noopMutexLeave,
15153
15154 noopMutexHeld,
15155 noopMutexNotheld
15156 };
15157
15158 return &sMutex;
15159 }
15160 #endif /* defined(SQLITE_MUTEX_NOOP) && !defined(SQLITE_DEBUG) */
15161
15162 #if defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG)
15163 /*
15164 ** In this implementation, error checking is provided for testing
15165 ** and debugging purposes. The mutexes still do not provide any
15166 ** mutual exclusion.
15167 */
15168
15169 /*
15170 ** The mutex object
15171 */
15172 struct sqlite3_mutex {
15173 int id; /* The mutex type */
15174 int cnt; /* Number of entries without a matching leave */
15175 };
15176
15177 /*
15178 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15179 ** intended for use inside assert() statements.
15180 */
15181 static int debugMutexHeld(sqlite3_mutex *p){
 
15182 return p==0 || p->cnt>0;
15183 }
15184 static int debugMutexNotheld(sqlite3_mutex *p){
 
15185 return p==0 || p->cnt==0;
15186 }
15187
15188 /*
15189 ** Initialize and deinitialize the mutex subsystem.
@@ -15195,12 +15426,12 @@
15195 ** The sqlite3_mutex_alloc() routine allocates a new
15196 ** mutex and returns a pointer to it. If it returns NULL
15197 ** that means that a mutex could not be allocated.
15198 */
15199 static sqlite3_mutex *debugMutexAlloc(int id){
15200 static sqlite3_mutex aStatic[6];
15201 sqlite3_mutex *pNew = 0;
15202 switch( id ){
15203 case SQLITE_MUTEX_FAST:
15204 case SQLITE_MUTEX_RECURSIVE: {
15205 pNew = sqlite3Malloc(sizeof(*pNew));
15206 if( pNew ){
@@ -15215,17 +15446,18 @@
15215 pNew = &aStatic[id-2];
15216 pNew->id = id;
15217 break;
15218 }
15219 }
15220 return pNew;
15221 }
15222
15223 /*
15224 ** This routine deallocates a previously allocated mutex.
15225 */
15226 static void debugMutexFree(sqlite3_mutex *p){
 
15227 assert( p->cnt==0 );
15228 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
15229 sqlite3_free(p);
15230 }
15231
@@ -15238,16 +15470,18 @@
15238 ** be entered multiple times by the same thread. In such cases the,
15239 ** mutex must be exited an equal number of times before another thread
15240 ** can enter. If the same thread tries to enter any other kind of mutex
15241 ** more than once, the behavior is undefined.
15242 */
15243 static void debugMutexEnter(sqlite3_mutex *p){
15244 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
 
15245 p->cnt++;
15246 }
15247 static int debugMutexTry(sqlite3_mutex *p){
15248 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
 
15249 p->cnt++;
15250 return SQLITE_OK;
15251 }
15252
15253 /*
@@ -15254,18 +15488,19 @@
15254 ** The sqlite3_mutex_leave() routine exits a mutex that was
15255 ** previously entered by the same thread. The behavior
15256 ** is undefined if the mutex is not currently entered or
15257 ** is not currently allocated. SQLite will never do either.
15258 */
15259 static void debugMutexLeave(sqlite3_mutex *p){
15260 assert( debugMutexHeld(p) );
 
15261 p->cnt--;
15262 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(p) );
15263 }
15264
15265 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15266 static sqlite3_mutex_methods sMutex = {
15267 debugMutexInit,
15268 debugMutexEnd,
15269 debugMutexAlloc,
15270 debugMutexFree,
15271 debugMutexEnter,
@@ -15276,11 +15511,22 @@
15276 debugMutexNotheld
15277 };
15278
15279 return &sMutex;
15280 }
15281 #endif /* defined(SQLITE_MUTEX_NOOP) && defined(SQLITE_DEBUG) */
 
 
 
 
 
 
 
 
 
 
 
15282
15283 /************** End of mutex_noop.c ******************************************/
15284 /************** Begin file mutex_os2.c ***************************************/
15285 /*
15286 ** 2007 August 28
@@ -15532,12 +15778,12 @@
15532 p->nRef--;
15533 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
15534 DosReleaseMutexSem(p->mutex);
15535 }
15536
15537 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15538 static sqlite3_mutex_methods sMutex = {
15539 os2MutexInit,
15540 os2MutexEnd,
15541 os2MutexAlloc,
15542 os2MutexFree,
15543 os2MutexEnter,
@@ -15578,27 +15824,37 @@
15578 */
15579 #ifdef SQLITE_MUTEX_PTHREADS
15580
15581 #include <pthread.h>
15582
 
 
 
 
 
 
 
 
 
 
15583
15584 /*
15585 ** Each recursive mutex is an instance of the following structure.
15586 */
15587 struct sqlite3_mutex {
15588 pthread_mutex_t mutex; /* Mutex controlling the lock */
 
15589 int id; /* Mutex type */
15590 int nRef; /* Number of entrances */
15591 pthread_t owner; /* Thread that is within this mutex */
15592 #ifdef SQLITE_DEBUG
15593 int trace; /* True to trace changes */
15594 #endif
15595 };
15596 #ifdef SQLITE_DEBUG
15597 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
15598 #else
15599 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0 }
15600 #endif
15601
15602 /*
15603 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15604 ** intended for use only inside assert() statements. On some platforms,
@@ -15696,27 +15952,33 @@
15696 pthread_mutexattr_init(&recursiveAttr);
15697 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
15698 pthread_mutex_init(&p->mutex, &recursiveAttr);
15699 pthread_mutexattr_destroy(&recursiveAttr);
15700 #endif
 
15701 p->id = iType;
 
15702 }
15703 break;
15704 }
15705 case SQLITE_MUTEX_FAST: {
15706 p = sqlite3MallocZero( sizeof(*p) );
15707 if( p ){
 
15708 p->id = iType;
 
15709 pthread_mutex_init(&p->mutex, 0);
15710 }
15711 break;
15712 }
15713 default: {
15714 assert( iType-2 >= 0 );
15715 assert( iType-2 < ArraySize(staticMutexes) );
15716 p = &staticMutexes[iType-2];
 
15717 p->id = iType;
 
15718 break;
15719 }
15720 }
15721 return p;
15722 }
@@ -15772,12 +16034,14 @@
15772 }
15773 #else
15774 /* Use the built-in recursive mutexes if they are available.
15775 */
15776 pthread_mutex_lock(&p->mutex);
 
15777 p->owner = pthread_self();
15778 p->nRef++;
 
15779 #endif
15780
15781 #ifdef SQLITE_DEBUG
15782 if( p->trace ){
15783 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
@@ -15815,12 +16079,14 @@
15815 }
15816 #else
15817 /* Use the built-in recursive mutexes if they are available.
15818 */
15819 if( pthread_mutex_trylock(&p->mutex)==0 ){
 
15820 p->owner = pthread_self();
15821 p->nRef++;
 
15822 rc = SQLITE_OK;
15823 }else{
15824 rc = SQLITE_BUSY;
15825 }
15826 #endif
@@ -15839,11 +16105,13 @@
15839 ** is undefined if the mutex is not currently entered or
15840 ** is not currently allocated. SQLite will never do either.
15841 */
15842 static void pthreadMutexLeave(sqlite3_mutex *p){
15843 assert( pthreadMutexHeld(p) );
 
15844 p->nRef--;
 
15845 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
15846
15847 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
15848 if( p->nRef==0 ){
15849 pthread_mutex_unlock(&p->mutex);
@@ -15857,12 +16125,12 @@
15857 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
15858 }
15859 #endif
15860 }
15861
15862 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
15863 static sqlite3_mutex_methods sMutex = {
15864 pthreadMutexInit,
15865 pthreadMutexEnd,
15866 pthreadMutexAlloc,
15867 pthreadMutexFree,
15868 pthreadMutexEnter,
@@ -15908,21 +16176,21 @@
15908 ** Each recursive mutex is an instance of the following structure.
15909 */
15910 struct sqlite3_mutex {
15911 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
15912 int id; /* Mutex type */
15913 int nRef; /* Number of enterances */
15914 DWORD owner; /* Thread holding this mutex */
15915 #ifdef SQLITE_DEBUG
 
 
15916 int trace; /* True to trace changes */
15917 #endif
15918 };
15919 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
15920 #ifdef SQLITE_DEBUG
15921 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
15922 #else
15923 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0 }
15924 #endif
15925
15926 /*
15927 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
15928 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
@@ -16074,21 +16342,25 @@
16074 switch( iType ){
16075 case SQLITE_MUTEX_FAST:
16076 case SQLITE_MUTEX_RECURSIVE: {
16077 p = sqlite3MallocZero( sizeof(*p) );
16078 if( p ){
 
16079 p->id = iType;
 
16080 InitializeCriticalSection(&p->mutex);
16081 }
16082 break;
16083 }
16084 default: {
16085 assert( winMutex_isInit==1 );
16086 assert( iType-2 >= 0 );
16087 assert( iType-2 < ArraySize(winMutex_staticMutexes) );
16088 p = &winMutex_staticMutexes[iType-2];
 
16089 p->id = iType;
 
16090 break;
16091 }
16092 }
16093 return p;
16094 }
@@ -16117,16 +16389,18 @@
16117 ** mutex must be exited an equal number of times before another thread
16118 ** can enter. If the same thread tries to enter any other kind of mutex
16119 ** more than once, the behavior is undefined.
16120 */
16121 static void winMutexEnter(sqlite3_mutex *p){
 
16122 DWORD tid = GetCurrentThreadId();
16123 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
 
16124 EnterCriticalSection(&p->mutex);
 
16125 p->owner = tid;
16126 p->nRef++;
16127 #ifdef SQLITE_DEBUG
16128 if( p->trace ){
16129 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16130 }
16131 #endif
16132 }
@@ -16171,25 +16445,25 @@
16171 ** is not currently allocated. SQLite will never do either.
16172 */
16173 static void winMutexLeave(sqlite3_mutex *p){
16174 #ifndef NDEBUG
16175 DWORD tid = GetCurrentThreadId();
16176 #endif
16177 assert( p->nRef>0 );
16178 assert( p->owner==tid );
16179 p->nRef--;
16180 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
 
16181 LeaveCriticalSection(&p->mutex);
16182 #ifdef SQLITE_DEBUG
16183 if( p->trace ){
16184 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16185 }
16186 #endif
16187 }
16188
16189 SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void){
16190 static sqlite3_mutex_methods sMutex = {
16191 winMutexInit,
16192 winMutexEnd,
16193 winMutexAlloc,
16194 winMutexFree,
16195 winMutexEnter,
@@ -20557,30 +20831,30 @@
20557 /* 113 */ "IfPos",
20558 /* 114 */ "IfNeg",
20559 /* 115 */ "IfZero",
20560 /* 116 */ "AggStep",
20561 /* 117 */ "AggFinal",
20562 /* 118 */ "Vacuum",
20563 /* 119 */ "IncrVacuum",
20564 /* 120 */ "Expire",
20565 /* 121 */ "TableLock",
20566 /* 122 */ "VBegin",
20567 /* 123 */ "VCreate",
20568 /* 124 */ "VDestroy",
20569 /* 125 */ "VOpen",
20570 /* 126 */ "VFilter",
20571 /* 127 */ "VColumn",
20572 /* 128 */ "VNext",
20573 /* 129 */ "VRename",
20574 /* 130 */ "Real",
20575 /* 131 */ "VUpdate",
20576 /* 132 */ "Pagecount",
20577 /* 133 */ "Trace",
20578 /* 134 */ "Noop",
20579 /* 135 */ "Explain",
20580 /* 136 */ "NotUsed_136",
20581 /* 137 */ "NotUsed_137",
20582 /* 138 */ "NotUsed_138",
20583 /* 139 */ "NotUsed_139",
20584 /* 140 */ "NotUsed_140",
20585 /* 141 */ "ToText",
20586 /* 142 */ "ToBlob",
@@ -20681,27 +20955,13 @@
20681 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
20682 #endif
20683
20684 #ifdef SQLITE_DEBUG
20685 SQLITE_PRIVATE int sqlite3OSTrace = 0;
20686 #define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
20687 #define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
20688 #define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
20689 #define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
20690 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
20691 #define OSTRACE6(X,Y,Z,A,B,C) \
20692 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
20693 #define OSTRACE7(X,Y,Z,A,B,C,D) \
20694 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
20695 #else
20696 #define OSTRACE1(X)
20697 #define OSTRACE2(X,Y)
20698 #define OSTRACE3(X,Y,Z)
20699 #define OSTRACE4(X,Y,Z,A)
20700 #define OSTRACE5(X,Y,Z,A,B)
20701 #define OSTRACE6(X,Y,Z,A,B,C)
20702 #define OSTRACE7(X,Y,Z,A,B,C,D)
20703 #endif
20704
20705 /*
20706 ** Macros for performance tracing. Normally turned off. Only works
20707 ** on i486 hardware.
@@ -20893,11 +21153,11 @@
20893 */
20894 static int os2Close( sqlite3_file *id ){
20895 APIRET rc = NO_ERROR;
20896 os2File *pFile;
20897 if( id && (pFile = (os2File*)id) != 0 ){
20898 OSTRACE2( "CLOSE %d\n", pFile->h );
20899 rc = DosClose( pFile->h );
20900 pFile->locktype = NO_LOCK;
20901 if( pFile->pathToDel != NULL ){
20902 rc = DosForceDelete( (PSZ)pFile->pathToDel );
20903 free( pFile->pathToDel );
@@ -20924,11 +21184,11 @@
20924 ULONG fileLocation = 0L;
20925 ULONG got;
20926 os2File *pFile = (os2File*)id;
20927 assert( id!=0 );
20928 SimulateIOError( return SQLITE_IOERR_READ );
20929 OSTRACE3( "READ %d lock=%d\n", pFile->h, pFile->locktype );
20930 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
20931 return SQLITE_IOERR;
20932 }
20933 if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
20934 return SQLITE_IOERR_READ;
@@ -20957,11 +21217,11 @@
20957 ULONG wrote;
20958 os2File *pFile = (os2File*)id;
20959 assert( id!=0 );
20960 SimulateIOError( return SQLITE_IOERR_WRITE );
20961 SimulateDiskfullError( return SQLITE_FULL );
20962 OSTRACE3( "WRITE %d lock=%d\n", pFile->h, pFile->locktype );
20963 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
20964 return SQLITE_IOERR;
20965 }
20966 assert( amt>0 );
20967 while( amt > 0 &&
@@ -20979,11 +21239,11 @@
20979 ** Truncate an open file to a specified size
20980 */
20981 static int os2Truncate( sqlite3_file *id, i64 nByte ){
20982 APIRET rc = NO_ERROR;
20983 os2File *pFile = (os2File*)id;
20984 OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte );
20985 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
20986 rc = DosSetFileSize( pFile->h, nByte );
20987 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
20988 }
20989
@@ -20999,11 +21259,11 @@
20999 /*
21000 ** Make sure all writes to a particular file are committed to disk.
21001 */
21002 static int os2Sync( sqlite3_file *id, int flags ){
21003 os2File *pFile = (os2File*)id;
21004 OSTRACE3( "SYNC %d lock=%d\n", pFile->h, pFile->locktype );
21005 #ifdef SQLITE_TEST
21006 if( flags & SQLITE_SYNC_FULL){
21007 sqlite3_fullsync_count++;
21008 }
21009 sqlite3_sync_count++;
@@ -21049,11 +21309,11 @@
21049 LockArea.lOffset = SHARED_FIRST;
21050 LockArea.lRange = SHARED_SIZE;
21051 UnlockArea.lOffset = 0L;
21052 UnlockArea.lRange = 0L;
21053 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21054 OSTRACE3( "GETREADLOCK %d res=%d\n", pFile->h, res );
21055 return res;
21056 }
21057
21058 /*
21059 ** Undo a readlock
@@ -21067,11 +21327,11 @@
21067 LockArea.lOffset = 0L;
21068 LockArea.lRange = 0L;
21069 UnlockArea.lOffset = SHARED_FIRST;
21070 UnlockArea.lRange = SHARED_SIZE;
21071 res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21072 OSTRACE3( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res );
21073 return res;
21074 }
21075
21076 /*
21077 ** Lock the file with the lock specified by parameter locktype - one
@@ -21108,18 +21368,18 @@
21108 UnlockArea;
21109 os2File *pFile = (os2File*)id;
21110 memset(&LockArea, 0, sizeof(LockArea));
21111 memset(&UnlockArea, 0, sizeof(UnlockArea));
21112 assert( pFile!=0 );
21113 OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
21114
21115 /* If there is already a lock of this type or more restrictive on the
21116 ** os2File, do nothing. Don't use the end_lock: exit path, as
21117 ** sqlite3_mutex_enter() hasn't been called yet.
21118 */
21119 if( pFile->locktype>=locktype ){
21120 OSTRACE3( "LOCK %d %d ok (already held)\n", pFile->h, locktype );
21121 return SQLITE_OK;
21122 }
21123
21124 /* Make sure the locking sequence is correct
21125 */
@@ -21142,11 +21402,11 @@
21142
21143 /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
21144 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
21145 if( res == NO_ERROR ){
21146 gotPendingLock = 1;
21147 OSTRACE3( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res );
21148 }
21149 }
21150
21151 /* Acquire a shared lock
21152 */
@@ -21154,11 +21414,11 @@
21154 assert( pFile->locktype==NO_LOCK );
21155 res = getReadLock(pFile);
21156 if( res == NO_ERROR ){
21157 newLocktype = SHARED_LOCK;
21158 }
21159 OSTRACE3( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res );
21160 }
21161
21162 /* Acquire a RESERVED lock
21163 */
21164 if( locktype==RESERVED_LOCK && res == NO_ERROR ){
@@ -21169,39 +21429,40 @@
21169 UnlockArea.lRange = 0L;
21170 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21171 if( res == NO_ERROR ){
21172 newLocktype = RESERVED_LOCK;
21173 }
21174 OSTRACE3( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res );
21175 }
21176
21177 /* Acquire a PENDING lock
21178 */
21179 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
21180 newLocktype = PENDING_LOCK;
21181 gotPendingLock = 0;
21182 OSTRACE2( "LOCK %d acquire pending lock. pending lock boolean unset.\n", pFile->h );
 
21183 }
21184
21185 /* Acquire an EXCLUSIVE lock
21186 */
21187 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
21188 assert( pFile->locktype>=SHARED_LOCK );
21189 res = unlockReadLock(pFile);
21190 OSTRACE2( "unreadlock = %d\n", res );
21191 LockArea.lOffset = SHARED_FIRST;
21192 LockArea.lRange = SHARED_SIZE;
21193 UnlockArea.lOffset = 0L;
21194 UnlockArea.lRange = 0L;
21195 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21196 if( res == NO_ERROR ){
21197 newLocktype = EXCLUSIVE_LOCK;
21198 }else{
21199 OSTRACE2( "OS/2 error-code = %d\n", res );
21200 getReadLock(pFile);
21201 }
21202 OSTRACE3( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res );
21203 }
21204
21205 /* If we are holding a PENDING lock that ought to be released, then
21206 ** release it now.
21207 */
@@ -21210,25 +21471,25 @@
21210 LockArea.lOffset = 0L;
21211 LockArea.lRange = 0L;
21212 UnlockArea.lOffset = PENDING_BYTE;
21213 UnlockArea.lRange = 1L;
21214 r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21215 OSTRACE3( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r );
21216 }
21217
21218 /* Update the state of the lock has held in the file descriptor then
21219 ** return the appropriate result code.
21220 */
21221 if( res == NO_ERROR ){
21222 rc = SQLITE_OK;
21223 }else{
21224 OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
21225 locktype, newLocktype );
21226 rc = SQLITE_BUSY;
21227 }
21228 pFile->locktype = newLocktype;
21229 OSTRACE3( "LOCK %d now %d\n", pFile->h, pFile->locktype );
21230 return rc;
21231 }
21232
21233 /*
21234 ** This routine checks if there is a RESERVED lock held on the specified
@@ -21239,11 +21500,11 @@
21239 int r = 0;
21240 os2File *pFile = (os2File*)id;
21241 assert( pFile!=0 );
21242 if( pFile->locktype>=RESERVED_LOCK ){
21243 r = 1;
21244 OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, r );
21245 }else{
21246 FILELOCK LockArea,
21247 UnlockArea;
21248 APIRET rc = NO_ERROR;
21249 memset(&LockArea, 0, sizeof(LockArea));
@@ -21251,22 +21512,22 @@
21251 LockArea.lOffset = RESERVED_BYTE;
21252 LockArea.lRange = 1L;
21253 UnlockArea.lOffset = 0L;
21254 UnlockArea.lRange = 0L;
21255 rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21256 OSTRACE3( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc );
21257 if( rc == NO_ERROR ){
21258 APIRET rcu = NO_ERROR; /* return code for unlocking */
21259 LockArea.lOffset = 0L;
21260 LockArea.lRange = 0L;
21261 UnlockArea.lOffset = RESERVED_BYTE;
21262 UnlockArea.lRange = 1L;
21263 rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21264 OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu );
21265 }
21266 r = !(rc == NO_ERROR);
21267 OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r );
21268 }
21269 *pOut = r;
21270 return SQLITE_OK;
21271 }
21272
@@ -21290,48 +21551,49 @@
21290 UnlockArea;
21291 memset(&LockArea, 0, sizeof(LockArea));
21292 memset(&UnlockArea, 0, sizeof(UnlockArea));
21293 assert( pFile!=0 );
21294 assert( locktype<=SHARED_LOCK );
21295 OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
21296 type = pFile->locktype;
21297 if( type>=EXCLUSIVE_LOCK ){
21298 LockArea.lOffset = 0L;
21299 LockArea.lRange = 0L;
21300 UnlockArea.lOffset = SHARED_FIRST;
21301 UnlockArea.lRange = SHARED_SIZE;
21302 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21303 OSTRACE3( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res );
21304 if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
21305 /* This should never happen. We should always be able to
21306 ** reacquire the read lock */
21307 OSTRACE3( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype );
21308 rc = SQLITE_IOERR_UNLOCK;
21309 }
21310 }
21311 if( type>=RESERVED_LOCK ){
21312 LockArea.lOffset = 0L;
21313 LockArea.lRange = 0L;
21314 UnlockArea.lOffset = RESERVED_BYTE;
21315 UnlockArea.lRange = 1L;
21316 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21317 OSTRACE3( "UNLOCK %d reserved res=%d\n", pFile->h, res );
21318 }
21319 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
21320 res = unlockReadLock(pFile);
21321 OSTRACE5( "UNLOCK %d is %d want %d res=%d\n", pFile->h, type, locktype, res );
 
21322 }
21323 if( type>=PENDING_LOCK ){
21324 LockArea.lOffset = 0L;
21325 LockArea.lRange = 0L;
21326 UnlockArea.lOffset = PENDING_BYTE;
21327 UnlockArea.lRange = 1L;
21328 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21329 OSTRACE3( "UNLOCK %d pending res=%d\n", pFile->h, res );
21330 }
21331 pFile->locktype = locktype;
21332 OSTRACE3( "UNLOCK %d now %d\n", pFile->h, pFile->locktype );
21333 return rc;
21334 }
21335
21336 /*
21337 ** Control and query of the open file handle.
@@ -21338,11 +21600,12 @@
21338 */
21339 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
21340 switch( op ){
21341 case SQLITE_FCNTL_LOCKSTATE: {
21342 *(int*)pArg = ((os2File*)id)->locktype;
21343 OSTRACE3( "FCNTL_LOCKSTATE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
 
21344 return SQLITE_OK;
21345 }
21346 }
21347 return SQLITE_ERROR;
21348 }
@@ -21525,11 +21788,11 @@
21525 sqlite3_randomness( 20, &zBuf[j] );
21526 for( i = 0; i < 20; i++, j++ ){
21527 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
21528 }
21529 zBuf[j] = 0;
21530 OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
21531 return SQLITE_OK;
21532 }
21533
21534
21535 /*
@@ -21588,47 +21851,47 @@
21588 }
21589
21590
21591 memset( pFile, 0, sizeof(*pFile) );
21592
21593 OSTRACE2( "OPEN want %d\n", flags );
21594
21595 if( flags & SQLITE_OPEN_READWRITE ){
21596 ulOpenMode |= OPEN_ACCESS_READWRITE;
21597 OSTRACE1( "OPEN read/write\n" );
21598 }else{
21599 ulOpenMode |= OPEN_ACCESS_READONLY;
21600 OSTRACE1( "OPEN read only\n" );
21601 }
21602
21603 if( flags & SQLITE_OPEN_CREATE ){
21604 ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
21605 OSTRACE1( "OPEN open new/create\n" );
21606 }else{
21607 ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
21608 OSTRACE1( "OPEN open existing\n" );
21609 }
21610
21611 if( flags & SQLITE_OPEN_MAIN_DB ){
21612 ulOpenMode |= OPEN_SHARE_DENYNONE;
21613 OSTRACE1( "OPEN share read/write\n" );
21614 }else{
21615 ulOpenMode |= OPEN_SHARE_DENYWRITE;
21616 OSTRACE1( "OPEN share read only\n" );
21617 }
21618
21619 if( flags & SQLITE_OPEN_DELETEONCLOSE ){
21620 char pathUtf8[CCHMAXPATH];
21621 #ifdef NDEBUG /* when debugging we want to make sure it is deleted */
21622 ulFileAttribute = FILE_HIDDEN;
21623 #endif
21624 os2FullPathname( pVfs, zName, CCHMAXPATH, pathUtf8 );
21625 pFile->pathToDel = convertUtf8PathToCp( pathUtf8 );
21626 OSTRACE1( "OPEN hidden/delete on close file attributes\n" );
21627 }else{
21628 pFile->pathToDel = NULL;
21629 OSTRACE1( "OPEN normal file attribute\n" );
21630 }
21631
21632 /* always open in random access mode for possibly better speed */
21633 ulOpenMode |= OPEN_FLAGS_RANDOM;
21634 ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
@@ -21643,17 +21906,18 @@
21643 ulOpenFlags,
21644 ulOpenMode,
21645 (PEAOP2)NULL );
21646 free( zNameCp );
21647 if( rc != NO_ERROR ){
21648 OSTRACE7( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
21649 rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode );
21650 if( pFile->pathToDel )
21651 free( pFile->pathToDel );
21652 pFile->pathToDel = NULL;
21653 if( flags & SQLITE_OPEN_READWRITE ){
21654 OSTRACE2( "OPEN %d Invalid handle\n", ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) );
 
21655 return os2Open( pVfs, zName, id,
21656 ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
21657 pOutFlags );
21658 }else{
21659 return SQLITE_CANTOPEN;
@@ -21665,11 +21929,11 @@
21665 }
21666
21667 pFile->pMethod = &os2IoMethod;
21668 pFile->h = h;
21669 OpenCounter(+1);
21670 OSTRACE3( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags );
21671 return SQLITE_OK;
21672 }
21673
21674 /*
21675 ** Delete the named file.
@@ -21682,11 +21946,11 @@
21682 APIRET rc = NO_ERROR;
21683 char *zFilenameCp = convertUtf8PathToCp( zFilename );
21684 SimulateIOError( return SQLITE_IOERR_DELETE );
21685 rc = DosDelete( (PSZ)zFilenameCp );
21686 free( zFilenameCp );
21687 OSTRACE2( "DELETE \"%s\"\n", zFilename );
21688 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_DELETE;
21689 }
21690
21691 /*
21692 ** Check the existance and status of a file.
@@ -21703,21 +21967,21 @@
21703
21704 memset( &fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo) );
21705 rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
21706 &fsts3ConfigInfo, sizeof(FILESTATUS3) );
21707 free( zFilenameCp );
21708 OSTRACE4( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
21709 fsts3ConfigInfo.attrFile, flags, rc );
21710 switch( flags ){
21711 case SQLITE_ACCESS_READ:
21712 case SQLITE_ACCESS_EXISTS:
21713 rc = (rc == NO_ERROR);
21714 OSTRACE3( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc );
21715 break;
21716 case SQLITE_ACCESS_READWRITE:
21717 rc = (rc == NO_ERROR) && ( (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0 );
21718 OSTRACE3( "ACCESS %s access of read/write rc=%d\n", zFilename, rc );
21719 break;
21720 default:
21721 assert( !"Invalid flags argument" );
21722 }
21723 *pOut = rc;
@@ -21923,11 +22187,11 @@
21923 os2DlSym, /* xDlSym */
21924 os2DlClose, /* xDlClose */
21925 os2Randomness, /* xRandomness */
21926 os2Sleep, /* xSleep */
21927 os2CurrentTime, /* xCurrentTime */
21928 os2GetLastError /* xGetLastError */
21929 };
21930 sqlite3_vfs_register(&os2Vfs, 1);
21931 initUconvObjects();
21932 return SQLITE_OK;
21933 }
@@ -22057,10 +22321,11 @@
22057 #include <sys/stat.h>
22058 #include <fcntl.h>
22059 #include <unistd.h>
22060 #include <sys/time.h>
22061 #include <errno.h>
 
22062
22063 #if SQLITE_ENABLE_LOCKING_STYLE
22064 # include <sys/ioctl.h>
22065 # if OS_VXWORKS
22066 # include <semaphore.h>
@@ -22111,18 +22376,22 @@
22111 ** Only set the lastErrno if the error code is a real error and not
22112 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
22113 */
22114 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
22115
 
 
 
 
 
22116
22117 /*
22118 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
22119 ** cannot be closed immediately. In these cases, instances of the following
22120 ** structure are used to store the file descriptor while waiting for an
22121 ** opportunity to either close or reuse it.
22122 */
22123 typedef struct UnixUnusedFd UnixUnusedFd;
22124 struct UnixUnusedFd {
22125 int fd; /* File descriptor to close */
22126 int flags; /* Flags this file descriptor was opened with */
22127 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
22128 };
@@ -22132,31 +22401,29 @@
22132 ** VFS implementations.
22133 */
22134 typedef struct unixFile unixFile;
22135 struct unixFile {
22136 sqlite3_io_methods const *pMethod; /* Always the first entry */
22137 struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */
22138 struct unixLockInfo *pLock; /* Info about locks on this inode */
22139 int h; /* The file descriptor */
22140 int dirfd; /* File descriptor for the directory */
22141 unsigned char locktype; /* The type of lock held on this fd */
22142 int lastErrno; /* The unix errno from the last I/O error */
22143 void *lockingContext; /* Locking style specific state */
22144 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22145 int fileFlags; /* Miscellanous flags */
 
22146 #if SQLITE_ENABLE_LOCKING_STYLE
22147 int openFlags; /* The flags specified at open() */
22148 #endif
22149 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
22150 unsigned fsFlags; /* cached details from statfs() */
22151 #endif
22152 #if SQLITE_THREADSAFE && defined(__linux__)
22153 pthread_t tid; /* The thread that "owns" this unixFile */
22154 #endif
22155 #if OS_VXWORKS
22156 int isDelete; /* Delete on close if true */
22157 struct vxworksFileId *pId; /* Unique file ID */
22158 #endif
22159 #ifndef NDEBUG
22160 /* The next group of variables are used to track whether or not the
22161 ** transaction counter in bytes 24-27 of database files are updated
22162 ** whenever any part of the database changes. An assertion fault will
@@ -22217,27 +22484,13 @@
22217 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22218 #endif
22219
22220 #ifdef SQLITE_DEBUG
22221 SQLITE_PRIVATE int sqlite3OSTrace = 0;
22222 #define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
22223 #define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
22224 #define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
22225 #define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
22226 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
22227 #define OSTRACE6(X,Y,Z,A,B,C) \
22228 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
22229 #define OSTRACE7(X,Y,Z,A,B,C,D) \
22230 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
22231 #else
22232 #define OSTRACE1(X)
22233 #define OSTRACE2(X,Y)
22234 #define OSTRACE3(X,Y,Z)
22235 #define OSTRACE4(X,Y,Z,A)
22236 #define OSTRACE5(X,Y,Z,A,B)
22237 #define OSTRACE6(X,Y,Z,A,B,C)
22238 #define OSTRACE7(X,Y,Z,A,B,C,D)
22239 #endif
22240
22241 /*
22242 ** Macros for performance tracing. Normally turned off. Only works
22243 ** on i486 hardware.
@@ -22443,11 +22696,11 @@
22443 #endif
22444
22445
22446 /*
22447 ** Helper functions to obtain and relinquish the global mutex. The
22448 ** global mutex is used to protect the unixOpenCnt, unixLockInfo and
22449 ** vxworksFileId objects used by this file, all of which may be
22450 ** shared by multiple threads.
22451 **
22452 ** Function unixMutexHeld() is used to assert() that the global mutex
22453 ** is held when required. This function is only used as part of assert()
@@ -22474,12 +22727,12 @@
22474 /*
22475 ** Helper function for printing out trace information from debugging
22476 ** binaries. This returns the string represetation of the supplied
22477 ** integer lock-type.
22478 */
22479 static const char *locktypeName(int locktype){
22480 switch( locktype ){
22481 case NO_LOCK: return "NONE";
22482 case SHARED_LOCK: return "SHARED";
22483 case RESERVED_LOCK: return "RESERVED";
22484 case PENDING_LOCK: return "PENDING";
22485 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
@@ -22812,17 +23065,16 @@
22812 **
22813 ** But wait: there are yet more problems with POSIX advisory locks.
22814 **
22815 ** If you close a file descriptor that points to a file that has locks,
22816 ** all locks on that file that are owned by the current process are
22817 ** released. To work around this problem, each unixFile structure contains
22818 ** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure
22819 ** per open inode, which means that multiple unixFile can point to a single
22820 ** unixOpenCnt. When an attempt is made to close an unixFile, if there are
22821 ** other unixFile open on the same inode that are holding locks, the call
22822 ** to close() the file descriptor is deferred until all of the locks clear.
22823 ** The unixOpenCnt structure keeps a list of file descriptors that need to
22824 ** be closed and that list is walked (and cleared) when the last lock
22825 ** clears.
22826 **
22827 ** Yet another problem: LinuxThreads do not play well with posix locks.
22828 **
@@ -22833,50 +23085,23 @@
22833 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
22834 ** on linux - with NPTL a lock created by thread A can override locks
22835 ** in thread B. But there is no way to know at compile-time which
22836 ** threading library is being used. So there is no way to know at
22837 ** compile-time whether or not thread A can override locks on thread B.
22838 ** We have to do a run-time check to discover the behavior of the
22839 ** current process.
22840 **
22841 ** On systems where thread A is unable to modify locks created by
22842 ** thread B, we have to keep track of which thread created each
22843 ** lock. Hence there is an extra field in the key to the unixLockInfo
22844 ** structure to record this information. And on those systems it
22845 ** is illegal to begin a transaction in one thread and finish it
22846 ** in another. For this latter restriction, there is no work-around.
22847 ** It is a limitation of LinuxThreads.
22848 */
22849
22850 /*
22851 ** Set or check the unixFile.tid field. This field is set when an unixFile
22852 ** is first opened. All subsequent uses of the unixFile verify that the
22853 ** same thread is operating on the unixFile. Some operating systems do
22854 ** not allow locks to be overridden by other threads and that restriction
22855 ** means that sqlite3* database handles cannot be moved from one thread
22856 ** to another while locks are held.
22857 **
22858 ** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to
22859 ** another as long as we are running on a system that supports threads
22860 ** overriding each others locks (which is now the most common behavior)
22861 ** or if no locks are held. But the unixFile.pLock field needs to be
22862 ** recomputed because its key includes the thread-id. See the
22863 ** transferOwnership() function below for additional information
22864 */
22865 #if SQLITE_THREADSAFE && defined(__linux__)
22866 # define SET_THREADID(X) (X)->tid = pthread_self()
22867 # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \
22868 !pthread_equal((X)->tid, pthread_self()))
22869 #else
22870 # define SET_THREADID(X)
22871 # define CHECK_THREADID(X) 0
22872 #endif
22873
22874 /*
22875 ** An instance of the following structure serves as the key used
22876 ** to locate a particular unixOpenCnt structure given its inode. This
22877 ** is the same as the unixLockKey except that the thread ID is omitted.
22878 */
22879 struct unixFileId {
22880 dev_t dev; /* Device number */
22881 #if OS_VXWORKS
22882 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
@@ -22883,260 +23108,122 @@
22883 #else
22884 ino_t ino; /* Inode number */
22885 #endif
22886 };
22887
22888 /*
22889 ** An instance of the following structure serves as the key used
22890 ** to locate a particular unixLockInfo structure given its inode.
22891 **
22892 ** If threads cannot override each others locks (LinuxThreads), then we
22893 ** set the unixLockKey.tid field to the thread ID. If threads can override
22894 ** each others locks (Posix and NPTL) then tid is always set to zero.
22895 ** tid is omitted if we compile without threading support or on an OS
22896 ** other than linux.
22897 */
22898 struct unixLockKey {
22899 struct unixFileId fid; /* Unique identifier for the file */
22900 #if SQLITE_THREADSAFE && defined(__linux__)
22901 pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */
22902 #endif
22903 };
22904
22905 /*
22906 ** An instance of the following structure is allocated for each open
22907 ** inode. Or, on LinuxThreads, there is one of these structures for
22908 ** each inode opened by each thread.
22909 **
22910 ** A single inode can have multiple file descriptors, so each unixFile
22911 ** structure contains a pointer to an instance of this object and this
22912 ** object keeps a count of the number of unixFile pointing to it.
22913 */
22914 struct unixLockInfo {
22915 struct unixLockKey lockKey; /* The lookup key */
22916 int cnt; /* Number of SHARED locks held */
22917 int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
22918 int nRef; /* Number of pointers to this structure */
22919 #if defined(SQLITE_ENABLE_LOCKING_STYLE)
22920 unsigned long long sharedByte; /* for AFP simulated shared lock */
22921 #endif
22922 struct unixLockInfo *pNext; /* List of all unixLockInfo objects */
22923 struct unixLockInfo *pPrev; /* .... doubly linked */
22924 };
22925
22926 /*
22927 ** An instance of the following structure is allocated for each open
22928 ** inode. This structure keeps track of the number of locks on that
22929 ** inode. If a close is attempted against an inode that is holding
22930 ** locks, the close is deferred until all locks clear by adding the
22931 ** file descriptor to be closed to the pending list.
22932 **
22933 ** TODO: Consider changing this so that there is only a single file
22934 ** descriptor for each open file, even when it is opened multiple times.
22935 ** The close() system call would only occur when the last database
22936 ** using the file closes.
22937 */
22938 struct unixOpenCnt {
22939 struct unixFileId fileId; /* The lookup key */
22940 int nRef; /* Number of pointers to this structure */
22941 int nLock; /* Number of outstanding locks */
22942 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
22943 #if OS_VXWORKS
22944 sem_t *pSem; /* Named POSIX semaphore */
22945 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
22946 #endif
22947 struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */
22948 };
22949
22950 /*
22951 ** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash
22952 ** tables. But the number of objects is rarely more than a dozen and
22953 ** never exceeds a few thousand. And lookup is not on a critical
22954 ** path so a simple linked list will suffice.
22955 */
22956 static struct unixLockInfo *lockList = 0;
22957 static struct unixOpenCnt *openList = 0;
22958
22959 /*
22960 ** This variable remembers whether or not threads can override each others
22961 ** locks.
22962 **
22963 ** 0: No. Threads cannot override each others locks. (LinuxThreads)
22964 ** 1: Yes. Threads can override each others locks. (Posix & NLPT)
22965 ** -1: We don't know yet.
22966 **
22967 ** On some systems, we know at compile-time if threads can override each
22968 ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro
22969 ** will be set appropriately. On other systems, we have to check at
22970 ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is
22971 ** undefined.
22972 **
22973 ** This variable normally has file scope only. But during testing, we make
22974 ** it a global so that the test code can change its value in order to verify
22975 ** that the right stuff happens in either case.
22976 */
22977 #if SQLITE_THREADSAFE && defined(__linux__)
22978 # ifndef SQLITE_THREAD_OVERRIDE_LOCK
22979 # define SQLITE_THREAD_OVERRIDE_LOCK -1
22980 # endif
22981 # ifdef SQLITE_TEST
22982 int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
22983 # else
22984 static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK;
22985 # endif
22986 #endif
22987
22988 /*
22989 ** This structure holds information passed into individual test
22990 ** threads by the testThreadLockingBehavior() routine.
22991 */
22992 struct threadTestData {
22993 int fd; /* File to be locked */
22994 struct flock lock; /* The locking operation */
22995 int result; /* Result of the locking operation */
22996 };
22997
22998 #if SQLITE_THREADSAFE && defined(__linux__)
22999 /*
23000 ** This function is used as the main routine for a thread launched by
23001 ** testThreadLockingBehavior(). It tests whether the shared-lock obtained
23002 ** by the main thread in testThreadLockingBehavior() conflicts with a
23003 ** hypothetical write-lock obtained by this thread on the same file.
23004 **
23005 ** The write-lock is not actually acquired, as this is not possible if
23006 ** the file is open in read-only mode (see ticket #3472).
23007 */
23008 static void *threadLockingTest(void *pArg){
23009 struct threadTestData *pData = (struct threadTestData*)pArg;
23010 pData->result = fcntl(pData->fd, F_GETLK, &pData->lock);
23011 return pArg;
23012 }
23013 #endif /* SQLITE_THREADSAFE && defined(__linux__) */
23014
23015
23016 #if SQLITE_THREADSAFE && defined(__linux__)
23017 /*
23018 ** This procedure attempts to determine whether or not threads
23019 ** can override each others locks then sets the
23020 ** threadsOverrideEachOthersLocks variable appropriately.
23021 */
23022 static void testThreadLockingBehavior(int fd_orig){
23023 int fd;
23024 int rc;
23025 struct threadTestData d;
23026 struct flock l;
23027 pthread_t t;
23028
23029 fd = dup(fd_orig);
23030 if( fd<0 ) return;
23031 memset(&l, 0, sizeof(l));
23032 l.l_type = F_RDLCK;
23033 l.l_len = 1;
23034 l.l_start = 0;
23035 l.l_whence = SEEK_SET;
23036 rc = fcntl(fd_orig, F_SETLK, &l);
23037 if( rc!=0 ) return;
23038 memset(&d, 0, sizeof(d));
23039 d.fd = fd;
23040 d.lock = l;
23041 d.lock.l_type = F_WRLCK;
23042 if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){
23043 pthread_join(t, 0);
23044 }
23045 close(fd);
23046 if( d.result!=0 ) return;
23047 threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK);
23048 }
23049 #endif /* SQLITE_THREADSAFE && defined(__linux__) */
23050
23051 /*
23052 ** Release a unixLockInfo structure previously allocated by findLockInfo().
23053 **
23054 ** The mutex entered using the unixEnterMutex() function must be held
23055 ** when this function is called.
23056 */
23057 static void releaseLockInfo(struct unixLockInfo *pLock){
23058 assert( unixMutexHeld() );
23059 if( pLock ){
23060 pLock->nRef--;
23061 if( pLock->nRef==0 ){
23062 if( pLock->pPrev ){
23063 assert( pLock->pPrev->pNext==pLock );
23064 pLock->pPrev->pNext = pLock->pNext;
23065 }else{
23066 assert( lockList==pLock );
23067 lockList = pLock->pNext;
23068 }
23069 if( pLock->pNext ){
23070 assert( pLock->pNext->pPrev==pLock );
23071 pLock->pNext->pPrev = pLock->pPrev;
23072 }
23073 sqlite3_free(pLock);
23074 }
23075 }
23076 }
23077
23078 /*
23079 ** Release a unixOpenCnt structure previously allocated by findLockInfo().
23080 **
23081 ** The mutex entered using the unixEnterMutex() function must be held
23082 ** when this function is called.
23083 */
23084 static void releaseOpenCnt(struct unixOpenCnt *pOpen){
23085 assert( unixMutexHeld() );
23086 if( pOpen ){
23087 pOpen->nRef--;
23088 if( pOpen->nRef==0 ){
23089 if( pOpen->pPrev ){
23090 assert( pOpen->pPrev->pNext==pOpen );
23091 pOpen->pPrev->pNext = pOpen->pNext;
23092 }else{
23093 assert( openList==pOpen );
23094 openList = pOpen->pNext;
23095 }
23096 if( pOpen->pNext ){
23097 assert( pOpen->pNext->pPrev==pOpen );
23098 pOpen->pNext->pPrev = pOpen->pPrev;
23099 }
23100 #if SQLITE_THREADSAFE && defined(__linux__)
23101 assert( !pOpen->pUnused || threadsOverrideEachOthersLocks==0 );
23102 #endif
23103
23104 /* If pOpen->pUnused is not null, then memory and file-descriptors
23105 ** are leaked.
23106 **
23107 ** This will only happen if, under Linuxthreads, the user has opened
23108 ** a transaction in one thread, then attempts to close the database
23109 ** handle from another thread (without first unlocking the db file).
23110 ** This is a misuse. */
23111 sqlite3_free(pOpen);
23112 }
23113 }
23114 }
23115
23116 /*
23117 ** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that
23118 ** describes that file descriptor. Create new ones if necessary. The
23119 ** return values might be uninitialized if an error occurs.
23120 **
23121 ** The mutex entered using the unixEnterMutex() function must be held
23122 ** when this function is called.
23123 **
23124 ** Return an appropriate error code.
23125 */
23126 static int findLockInfo(
23127 unixFile *pFile, /* Unix file with file desc used in the key */
23128 struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */
23129 struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */
23130 ){
23131 int rc; /* System call return code */
23132 int fd; /* The file descriptor for pFile */
23133 struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */
23134 struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */
23135 struct stat statbuf; /* Low-level file information */
23136 struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */
23137 struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */
23138
23139 assert( unixMutexHeld() );
23140
23141 /* Get low-level information about the file that we can used to
23142 ** create a unique name for the file.
@@ -23174,126 +23261,39 @@
23174 return SQLITE_IOERR;
23175 }
23176 }
23177 #endif
23178
23179 memset(&lockKey, 0, sizeof(lockKey));
23180 lockKey.fid.dev = statbuf.st_dev;
23181 #if OS_VXWORKS
23182 lockKey.fid.pId = pFile->pId;
23183 #else
23184 lockKey.fid.ino = statbuf.st_ino;
23185 #endif
23186 #if SQLITE_THREADSAFE && defined(__linux__)
23187 if( threadsOverrideEachOthersLocks<0 ){
23188 testThreadLockingBehavior(fd);
23189 }
23190 lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self();
23191 #endif
23192 fileId = lockKey.fid;
23193 if( ppLock!=0 ){
23194 pLock = lockList;
23195 while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){
23196 pLock = pLock->pNext;
23197 }
23198 if( pLock==0 ){
23199 pLock = sqlite3_malloc( sizeof(*pLock) );
23200 if( pLock==0 ){
23201 rc = SQLITE_NOMEM;
23202 goto exit_findlockinfo;
23203 }
23204 memcpy(&pLock->lockKey,&lockKey,sizeof(lockKey));
23205 pLock->nRef = 1;
23206 pLock->cnt = 0;
23207 pLock->locktype = 0;
23208 #if defined(SQLITE_ENABLE_LOCKING_STYLE)
23209 pLock->sharedByte = 0;
23210 #endif
23211 pLock->pNext = lockList;
23212 pLock->pPrev = 0;
23213 if( lockList ) lockList->pPrev = pLock;
23214 lockList = pLock;
23215 }else{
23216 pLock->nRef++;
23217 }
23218 *ppLock = pLock;
23219 }
23220 if( ppOpen!=0 ){
23221 pOpen = openList;
23222 while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){
23223 pOpen = pOpen->pNext;
23224 }
23225 if( pOpen==0 ){
23226 pOpen = sqlite3_malloc( sizeof(*pOpen) );
23227 if( pOpen==0 ){
23228 releaseLockInfo(pLock);
23229 rc = SQLITE_NOMEM;
23230 goto exit_findlockinfo;
23231 }
23232 memset(pOpen, 0, sizeof(*pOpen));
23233 pOpen->fileId = fileId;
23234 pOpen->nRef = 1;
23235 pOpen->pNext = openList;
23236 if( openList ) openList->pPrev = pOpen;
23237 openList = pOpen;
23238 }else{
23239 pOpen->nRef++;
23240 }
23241 *ppOpen = pOpen;
23242 }
23243
23244 exit_findlockinfo:
23245 return rc;
23246 }
23247
23248 /*
23249 ** If we are currently in a different thread than the thread that the
23250 ** unixFile argument belongs to, then transfer ownership of the unixFile
23251 ** over to the current thread.
23252 **
23253 ** A unixFile is only owned by a thread on systems that use LinuxThreads.
23254 **
23255 ** Ownership transfer is only allowed if the unixFile is currently unlocked.
23256 ** If the unixFile is locked and an ownership is wrong, then return
23257 ** SQLITE_MISUSE. SQLITE_OK is returned if everything works.
23258 */
23259 #if SQLITE_THREADSAFE && defined(__linux__)
23260 static int transferOwnership(unixFile *pFile){
23261 int rc;
23262 pthread_t hSelf;
23263 if( threadsOverrideEachOthersLocks ){
23264 /* Ownership transfers not needed on this system */
23265 return SQLITE_OK;
23266 }
23267 hSelf = pthread_self();
23268 if( pthread_equal(pFile->tid, hSelf) ){
23269 /* We are still in the same thread */
23270 OSTRACE1("No-transfer, same thread\n");
23271 return SQLITE_OK;
23272 }
23273 if( pFile->locktype!=NO_LOCK ){
23274 /* We cannot change ownership while we are holding a lock! */
23275 return SQLITE_MISUSE_BKPT;
23276 }
23277 OSTRACE4("Transfer ownership of %d from %d to %d\n",
23278 pFile->h, pFile->tid, hSelf);
23279 pFile->tid = hSelf;
23280 if (pFile->pLock != NULL) {
23281 releaseLockInfo(pFile->pLock);
23282 rc = findLockInfo(pFile, &pFile->pLock, 0);
23283 OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
23284 locktypeName(pFile->locktype),
23285 locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
23286 return rc;
23287 } else {
23288 return SQLITE_OK;
23289 }
23290 }
23291 #else /* if not SQLITE_THREADSAFE */
23292 /* On single-threaded builds, ownership transfer is a no-op */
23293 # define transferOwnership(X) SQLITE_OK
23294 #endif /* SQLITE_THREADSAFE */
23295
23296
23297 /*
23298 ** This routine checks if there is a RESERVED lock held on the specified
23299 ** file by this or any other process. If such a lock is held, set *pResOut
@@ -23306,14 +23306,14 @@
23306 unixFile *pFile = (unixFile*)id;
23307
23308 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
23309
23310 assert( pFile );
23311 unixEnterMutex(); /* Because pFile->pLock is shared across threads */
23312
23313 /* Check if a thread in this process holds such a lock */
23314 if( pFile->pLock->locktype>SHARED_LOCK ){
23315 reserved = 1;
23316 }
23317
23318 /* Otherwise see if some other process holds it.
23319 */
@@ -23333,18 +23333,18 @@
23333 }
23334 }
23335 #endif
23336
23337 unixLeaveMutex();
23338 OSTRACE4("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved);
23339
23340 *pResOut = reserved;
23341 return rc;
23342 }
23343
23344 /*
23345 ** Lock the file with the lock specified by parameter locktype - one
23346 ** of the following:
23347 **
23348 ** (1) SHARED_LOCK
23349 ** (2) RESERVED_LOCK
23350 ** (3) PENDING_LOCK
@@ -23363,11 +23363,11 @@
23363 ** PENDING -> EXCLUSIVE
23364 **
23365 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
23366 ** routine to lower a locking level.
23367 */
23368 static int unixLock(sqlite3_file *id, int locktype){
23369 /* The following describes the implementation of the various locks and
23370 ** lock transitions in terms of the POSIX advisory shared and exclusive
23371 ** lock primitives (called read-locks and write-locks below, to avoid
23372 ** confusion with SQLite lock names). The algorithms are complicated
23373 ** slightly in order to be compatible with windows systems simultaneously
@@ -23404,74 +23404,66 @@
23404 ** locking a random byte from a range, concurrent SHARED locks may exist
23405 ** even if the locking primitive used is always a write-lock.
23406 */
23407 int rc = SQLITE_OK;
23408 unixFile *pFile = (unixFile*)id;
23409 struct unixLockInfo *pLock = pFile->pLock;
23410 struct flock lock;
23411 int s = 0;
23412 int tErrno = 0;
23413
23414 assert( pFile );
23415 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
23416 locktypeName(locktype), locktypeName(pFile->locktype),
23417 locktypeName(pLock->locktype), pLock->cnt , getpid());
23418
23419 /* If there is already a lock of this type or more restrictive on the
23420 ** unixFile, do nothing. Don't use the end_lock: exit path, as
23421 ** unixEnterMutex() hasn't been called yet.
23422 */
23423 if( pFile->locktype>=locktype ){
23424 OSTRACE3("LOCK %d %s ok (already held) (unix)\n", pFile->h,
23425 locktypeName(locktype));
23426 return SQLITE_OK;
23427 }
23428
23429 /* Make sure the locking sequence is correct.
23430 ** (1) We never move from unlocked to anything higher than shared lock.
23431 ** (2) SQLite never explicitly requests a pendig lock.
23432 ** (3) A shared lock is always held when a reserve lock is requested.
23433 */
23434 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
23435 assert( locktype!=PENDING_LOCK );
23436 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
23437
23438 /* This mutex is needed because pFile->pLock is shared across threads
23439 */
23440 unixEnterMutex();
23441
23442 /* Make sure the current thread owns the pFile.
23443 */
23444 rc = transferOwnership(pFile);
23445 if( rc!=SQLITE_OK ){
23446 unixLeaveMutex();
23447 return rc;
23448 }
23449 pLock = pFile->pLock;
23450
23451 /* If some thread using this PID has a lock via a different unixFile*
23452 ** handle that precludes the requested lock, return BUSY.
23453 */
23454 if( (pFile->locktype!=pLock->locktype &&
23455 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
23456 ){
23457 rc = SQLITE_BUSY;
23458 goto end_lock;
23459 }
23460
23461 /* If a SHARED lock is requested, and some thread using this PID already
23462 ** has a SHARED or RESERVED lock, then increment reference counts and
23463 ** return SQLITE_OK.
23464 */
23465 if( locktype==SHARED_LOCK &&
23466 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
23467 assert( locktype==SHARED_LOCK );
23468 assert( pFile->locktype==0 );
23469 assert( pLock->cnt>0 );
23470 pFile->locktype = SHARED_LOCK;
23471 pLock->cnt++;
23472 pFile->pOpen->nLock++;
23473 goto end_lock;
23474 }
23475
23476
23477 /* A PENDING lock is needed before acquiring a SHARED lock and before
@@ -23478,14 +23470,14 @@
23478 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
23479 ** be released.
23480 */
23481 lock.l_len = 1L;
23482 lock.l_whence = SEEK_SET;
23483 if( locktype==SHARED_LOCK
23484 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
23485 ){
23486 lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK);
23487 lock.l_start = PENDING_BYTE;
23488 s = fcntl(pFile->h, F_SETLK, &lock);
23489 if( s==(-1) ){
23490 tErrno = errno;
23491 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -23498,13 +23490,13 @@
23498
23499
23500 /* If control gets to this point, then actually go ahead and make
23501 ** operating system calls for the specified lock.
23502 */
23503 if( locktype==SHARED_LOCK ){
23504 assert( pLock->cnt==0 );
23505 assert( pLock->locktype==0 );
23506
23507 /* Now get the read-lock */
23508 lock.l_start = SHARED_FIRST;
23509 lock.l_len = SHARED_SIZE;
23510 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
@@ -23529,26 +23521,26 @@
23529 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
23530 if( IS_LOCK_ERROR(rc) ){
23531 pFile->lastErrno = tErrno;
23532 }
23533 }else{
23534 pFile->locktype = SHARED_LOCK;
23535 pFile->pOpen->nLock++;
23536 pLock->cnt = 1;
23537 }
23538 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
23539 /* We are trying for an exclusive lock but another thread in this
23540 ** same process is still holding a shared lock. */
23541 rc = SQLITE_BUSY;
23542 }else{
23543 /* The request was for a RESERVED or EXCLUSIVE lock. It is
23544 ** assumed that there is a SHARED or greater lock on the file
23545 ** already.
23546 */
23547 assert( 0!=pFile->locktype );
23548 lock.l_type = F_WRLCK;
23549 switch( locktype ){
23550 case RESERVED_LOCK:
23551 lock.l_start = RESERVED_BYTE;
23552 break;
23553 case EXCLUSIVE_LOCK:
23554 lock.l_start = SHARED_FIRST;
@@ -23573,80 +23565,50 @@
23573 ** transitioning from a SHARED to a RESERVED lock. The change
23574 ** from SHARED to RESERVED marks the beginning of a normal
23575 ** write operation (not a hot journal rollback).
23576 */
23577 if( rc==SQLITE_OK
23578 && pFile->locktype<=SHARED_LOCK
23579 && locktype==RESERVED_LOCK
23580 ){
23581 pFile->transCntrChng = 0;
23582 pFile->dbUpdate = 0;
23583 pFile->inNormalWrite = 1;
23584 }
23585 #endif
23586
23587
23588 if( rc==SQLITE_OK ){
23589 pFile->locktype = locktype;
23590 pLock->locktype = locktype;
23591 }else if( locktype==EXCLUSIVE_LOCK ){
23592 pFile->locktype = PENDING_LOCK;
23593 pLock->locktype = PENDING_LOCK;
23594 }
23595
23596 end_lock:
23597 unixLeaveMutex();
23598 OSTRACE4("LOCK %d %s %s (unix)\n", pFile->h, locktypeName(locktype),
23599 rc==SQLITE_OK ? "ok" : "failed");
23600 return rc;
23601 }
23602
23603 /*
23604 ** Close all file descriptors accumuated in the unixOpenCnt->pUnused list.
23605 ** If all such file descriptors are closed without error, the list is
23606 ** cleared and SQLITE_OK returned.
23607 **
23608 ** Otherwise, if an error occurs, then successfully closed file descriptor
23609 ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned.
23610 ** not deleted and SQLITE_IOERR_CLOSE returned.
23611 */
23612 static int closePendingFds(unixFile *pFile){
23613 int rc = SQLITE_OK;
23614 struct unixOpenCnt *pOpen = pFile->pOpen;
23615 UnixUnusedFd *pError = 0;
23616 UnixUnusedFd *p;
23617 UnixUnusedFd *pNext;
23618 for(p=pOpen->pUnused; p; p=pNext){
23619 pNext = p->pNext;
23620 if( close(p->fd) ){
23621 pFile->lastErrno = errno;
23622 rc = SQLITE_IOERR_CLOSE;
23623 p->pNext = pError;
23624 pError = p;
23625 }else{
23626 sqlite3_free(p);
23627 }
23628 }
23629 pOpen->pUnused = pError;
23630 return rc;
23631 }
23632
23633 /*
23634 ** Add the file descriptor used by file handle pFile to the corresponding
23635 ** pUnused list.
23636 */
23637 static void setPendingFd(unixFile *pFile){
23638 struct unixOpenCnt *pOpen = pFile->pOpen;
23639 UnixUnusedFd *p = pFile->pUnused;
23640 p->pNext = pOpen->pUnused;
23641 pOpen->pUnused = p;
23642 pFile->h = -1;
23643 pFile->pUnused = 0;
23644 }
23645
23646 /*
23647 ** Lower the locking level on file descriptor pFile to locktype. locktype
23648 ** must be either NO_LOCK or SHARED_LOCK.
23649 **
23650 ** If the locking level of the file descriptor is already at or below
23651 ** the requested locking level, this routine is a no-op.
23652 **
@@ -23654,35 +23616,33 @@
23654 ** the byte range is divided into 2 parts and the first part is unlocked then
23655 ** set to a read lock, then the other part is simply unlocked. This works
23656 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
23657 ** remove the write lock on a region when a read lock is set.
23658 */
23659 static int _posixUnlock(sqlite3_file *id, int locktype, int handleNFSUnlock){
23660 unixFile *pFile = (unixFile*)id;
23661 struct unixLockInfo *pLock;
23662 struct flock lock;
23663 int rc = SQLITE_OK;
23664 int h;
23665 int tErrno; /* Error code from system call errors */
23666
23667 assert( pFile );
23668 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, locktype,
23669 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
23670
23671 assert( locktype<=SHARED_LOCK );
23672 if( pFile->locktype<=locktype ){
23673 return SQLITE_OK;
23674 }
23675 if( CHECK_THREADID(pFile) ){
23676 return SQLITE_MISUSE_BKPT;
23677 }
23678 unixEnterMutex();
23679 h = pFile->h;
23680 pLock = pFile->pLock;
23681 assert( pLock->cnt!=0 );
23682 if( pFile->locktype>SHARED_LOCK ){
23683 assert( pLock->locktype==pFile->locktype );
23684 SimulateIOErrorBenign(1);
23685 SimulateIOError( h=(-1) )
23686 SimulateIOErrorBenign(0);
23687
23688 #ifndef NDEBUG
@@ -23692,13 +23652,15 @@
23692 ** file changed. If the transaction counter is not updated,
23693 ** other connections to the same file might not realize that
23694 ** the file has changed and hence might not know to flush their
23695 ** cache. The use of a stale cache can lead to database corruption.
23696 */
 
23697 assert( pFile->inNormalWrite==0
23698 || pFile->dbUpdate==0
23699 || pFile->transCntrChng==1 );
 
23700 pFile->inNormalWrite = 0;
23701 #endif
23702
23703 /* downgrading to a shared lock on NFS involves clearing the write lock
23704 ** before establishing the readlock - to avoid a race condition we downgrade
@@ -23707,11 +23669,11 @@
23707 ** 1: [WWWWW]
23708 ** 2: [....W]
23709 ** 3: [RRRRW]
23710 ** 4: [RRRR.]
23711 */
23712 if( locktype==SHARED_LOCK ){
23713 if( handleNFSUnlock ){
23714 off_t divSize = SHARED_SIZE - 1;
23715
23716 lock.l_type = F_UNLCK;
23717 lock.l_whence = SEEK_SET;
@@ -23767,78 +23729,75 @@
23767 lock.l_type = F_UNLCK;
23768 lock.l_whence = SEEK_SET;
23769 lock.l_start = PENDING_BYTE;
23770 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
23771 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23772 pLock->locktype = SHARED_LOCK;
23773 }else{
23774 tErrno = errno;
23775 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
23776 if( IS_LOCK_ERROR(rc) ){
23777 pFile->lastErrno = tErrno;
23778 }
23779 goto end_unlock;
23780 }
23781 }
23782 if( locktype==NO_LOCK ){
23783 struct unixOpenCnt *pOpen;
23784
23785 /* Decrement the shared lock counter. Release the lock using an
23786 ** OS call only when all threads in this same process have released
23787 ** the lock.
23788 */
23789 pLock->cnt--;
23790 if( pLock->cnt==0 ){
23791 lock.l_type = F_UNLCK;
23792 lock.l_whence = SEEK_SET;
23793 lock.l_start = lock.l_len = 0L;
23794 SimulateIOErrorBenign(1);
23795 SimulateIOError( h=(-1) )
23796 SimulateIOErrorBenign(0);
23797 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23798 pLock->locktype = NO_LOCK;
23799 }else{
23800 tErrno = errno;
23801 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
23802 if( IS_LOCK_ERROR(rc) ){
23803 pFile->lastErrno = tErrno;
23804 }
23805 pLock->locktype = NO_LOCK;
23806 pFile->locktype = NO_LOCK;
23807 }
23808 }
23809
23810 /* Decrement the count of locks against this same file. When the
23811 ** count reaches zero, close any other file descriptors whose close
23812 ** was deferred because of outstanding locks.
23813 */
23814 pOpen = pFile->pOpen;
23815 pOpen->nLock--;
23816 assert( pOpen->nLock>=0 );
23817 if( pOpen->nLock==0 ){
23818 int rc2 = closePendingFds(pFile);
23819 if( rc==SQLITE_OK ){
23820 rc = rc2;
23821 }
23822 }
23823 }
23824
23825 end_unlock:
23826 unixLeaveMutex();
23827 if( rc==SQLITE_OK ) pFile->locktype = locktype;
23828 return rc;
23829 }
23830
23831 /*
23832 ** Lower the locking level on file descriptor pFile to locktype. locktype
23833 ** must be either NO_LOCK or SHARED_LOCK.
23834 **
23835 ** If the locking level of the file descriptor is already at or below
23836 ** the requested locking level, this routine is a no-op.
23837 */
23838 static int unixUnlock(sqlite3_file *id, int locktype){
23839 return _posixUnlock(id, locktype, 0);
23840 }
23841
23842 /*
23843 ** This function performs the parts of the "close file" operation
23844 ** common to all locking schemes. It closes the directory and file
@@ -23875,11 +23834,11 @@
23875 }
23876 vxworksReleaseFileId(pFile->pId);
23877 pFile->pId = 0;
23878 }
23879 #endif
23880 OSTRACE2("CLOSE %-3d\n", pFile->h);
23881 OpenCounter(-1);
23882 sqlite3_free(pFile->pUnused);
23883 memset(pFile, 0, sizeof(unixFile));
23884 }
23885 return SQLITE_OK;
@@ -23892,20 +23851,19 @@
23892 int rc = SQLITE_OK;
23893 if( id ){
23894 unixFile *pFile = (unixFile *)id;
23895 unixUnlock(id, NO_LOCK);
23896 unixEnterMutex();
23897 if( pFile->pOpen && pFile->pOpen->nLock ){
23898 /* If there are outstanding locks, do not actually close the file just
23899 ** yet because that would clear those locks. Instead, add the file
23900 ** descriptor to pOpen->pUnused list. It will be automatically closed
23901 ** when the last lock is cleared.
23902 */
23903 setPendingFd(pFile);
23904 }
23905 releaseLockInfo(pFile->pLock);
23906 releaseOpenCnt(pFile->pOpen);
23907 rc = closeUnixFile(id);
23908 unixLeaveMutex();
23909 }
23910 return rc;
23911 }
@@ -24000,26 +23958,26 @@
24000 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24001
24002 assert( pFile );
24003
24004 /* Check if a thread in this process holds such a lock */
24005 if( pFile->locktype>SHARED_LOCK ){
24006 /* Either this connection or some other connection in the same process
24007 ** holds a lock on the file. No need to check further. */
24008 reserved = 1;
24009 }else{
24010 /* The lock is held if and only if the lockfile exists */
24011 const char *zLockFile = (const char*)pFile->lockingContext;
24012 reserved = access(zLockFile, 0)==0;
24013 }
24014 OSTRACE4("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved);
24015 *pResOut = reserved;
24016 return rc;
24017 }
24018
24019 /*
24020 ** Lock the file with the lock specified by parameter locktype - one
24021 ** of the following:
24022 **
24023 ** (1) SHARED_LOCK
24024 ** (2) RESERVED_LOCK
24025 ** (3) PENDING_LOCK
@@ -24041,22 +23999,22 @@
24041 ** routine to lower a locking level.
24042 **
24043 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
24044 ** But we track the other locking levels internally.
24045 */
24046 static int dotlockLock(sqlite3_file *id, int locktype) {
24047 unixFile *pFile = (unixFile*)id;
24048 int fd;
24049 char *zLockFile = (char *)pFile->lockingContext;
24050 int rc = SQLITE_OK;
24051
24052
24053 /* If we have any lock, then the lock file already exists. All we have
24054 ** to do is adjust our internal record of the lock level.
24055 */
24056 if( pFile->locktype > NO_LOCK ){
24057 pFile->locktype = locktype;
24058 #if !OS_VXWORKS
24059 /* Always update the timestamp on the old file */
24060 utimes(zLockFile, NULL);
24061 #endif
24062 return SQLITE_OK;
@@ -24081,47 +24039,47 @@
24081 pFile->lastErrno = errno;
24082 rc = SQLITE_IOERR_CLOSE;
24083 }
24084
24085 /* got it, set the type and return ok */
24086 pFile->locktype = locktype;
24087 return rc;
24088 }
24089
24090 /*
24091 ** Lower the locking level on file descriptor pFile to locktype. locktype
24092 ** must be either NO_LOCK or SHARED_LOCK.
24093 **
24094 ** If the locking level of the file descriptor is already at or below
24095 ** the requested locking level, this routine is a no-op.
24096 **
24097 ** When the locking level reaches NO_LOCK, delete the lock file.
24098 */
24099 static int dotlockUnlock(sqlite3_file *id, int locktype) {
24100 unixFile *pFile = (unixFile*)id;
24101 char *zLockFile = (char *)pFile->lockingContext;
24102
24103 assert( pFile );
24104 OSTRACE5("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, locktype,
24105 pFile->locktype, getpid());
24106 assert( locktype<=SHARED_LOCK );
24107
24108 /* no-op if possible */
24109 if( pFile->locktype==locktype ){
24110 return SQLITE_OK;
24111 }
24112
24113 /* To downgrade to shared, simply update our internal notion of the
24114 ** lock state. No need to mess with the file on disk.
24115 */
24116 if( locktype==SHARED_LOCK ){
24117 pFile->locktype = SHARED_LOCK;
24118 return SQLITE_OK;
24119 }
24120
24121 /* To fully unlock the database, delete the lock file */
24122 assert( locktype==NO_LOCK );
24123 if( unlink(zLockFile) ){
24124 int rc = 0;
24125 int tErrno = errno;
24126 if( ENOENT != tErrno ){
24127 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
@@ -24129,11 +24087,11 @@
24129 if( IS_LOCK_ERROR(rc) ){
24130 pFile->lastErrno = tErrno;
24131 }
24132 return rc;
24133 }
24134 pFile->locktype = NO_LOCK;
24135 return SQLITE_OK;
24136 }
24137
24138 /*
24139 ** Close a file. Make sure the lock has been released before closing.
@@ -24182,11 +24140,11 @@
24182 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24183
24184 assert( pFile );
24185
24186 /* Check if a thread in this process holds such a lock */
24187 if( pFile->locktype>SHARED_LOCK ){
24188 reserved = 1;
24189 }
24190
24191 /* Otherwise see if some other process holds it. */
24192 if( !reserved ){
@@ -24213,11 +24171,11 @@
24213 pFile->lastErrno = tErrno;
24214 rc = lrc;
24215 }
24216 }
24217 }
24218 OSTRACE4("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved);
24219
24220 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
24221 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
24222 rc = SQLITE_OK;
24223 reserved=1;
@@ -24226,11 +24184,11 @@
24226 *pResOut = reserved;
24227 return rc;
24228 }
24229
24230 /*
24231 ** Lock the file with the lock specified by parameter locktype - one
24232 ** of the following:
24233 **
24234 ** (1) SHARED_LOCK
24235 ** (2) RESERVED_LOCK
24236 ** (3) PENDING_LOCK
@@ -24254,20 +24212,20 @@
24254 ** access the file.
24255 **
24256 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24257 ** routine to lower a locking level.
24258 */
24259 static int flockLock(sqlite3_file *id, int locktype) {
24260 int rc = SQLITE_OK;
24261 unixFile *pFile = (unixFile*)id;
24262
24263 assert( pFile );
24264
24265 /* if we already have a lock, it is exclusive.
24266 ** Just adjust level and punt on outta here. */
24267 if (pFile->locktype > NO_LOCK) {
24268 pFile->locktype = locktype;
24269 return SQLITE_OK;
24270 }
24271
24272 /* grab an exclusive lock */
24273
@@ -24278,14 +24236,14 @@
24278 if( IS_LOCK_ERROR(rc) ){
24279 pFile->lastErrno = tErrno;
24280 }
24281 } else {
24282 /* got it, set the type and return ok */
24283 pFile->locktype = locktype;
24284 }
24285 OSTRACE4("LOCK %d %s %s (flock)\n", pFile->h, locktypeName(locktype),
24286 rc==SQLITE_OK ? "ok" : "failed");
24287 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
24288 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
24289 rc = SQLITE_BUSY;
24290 }
24291 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
@@ -24292,32 +24250,32 @@
24292 return rc;
24293 }
24294
24295
24296 /*
24297 ** Lower the locking level on file descriptor pFile to locktype. locktype
24298 ** must be either NO_LOCK or SHARED_LOCK.
24299 **
24300 ** If the locking level of the file descriptor is already at or below
24301 ** the requested locking level, this routine is a no-op.
24302 */
24303 static int flockUnlock(sqlite3_file *id, int locktype) {
24304 unixFile *pFile = (unixFile*)id;
24305
24306 assert( pFile );
24307 OSTRACE5("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, locktype,
24308 pFile->locktype, getpid());
24309 assert( locktype<=SHARED_LOCK );
24310
24311 /* no-op if possible */
24312 if( pFile->locktype==locktype ){
24313 return SQLITE_OK;
24314 }
24315
24316 /* shared can just be set because we always have an exclusive */
24317 if (locktype==SHARED_LOCK) {
24318 pFile->locktype = locktype;
24319 return SQLITE_OK;
24320 }
24321
24322 /* no, really, unlock. */
24323 int rc = flock(pFile->h, LOCK_UN);
@@ -24333,11 +24291,11 @@
24333 }
24334 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
24335
24336 return r;
24337 } else {
24338 pFile->locktype = NO_LOCK;
24339 return SQLITE_OK;
24340 }
24341 }
24342
24343 /*
@@ -24381,41 +24339,41 @@
24381 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24382
24383 assert( pFile );
24384
24385 /* Check if a thread in this process holds such a lock */
24386 if( pFile->locktype>SHARED_LOCK ){
24387 reserved = 1;
24388 }
24389
24390 /* Otherwise see if some other process holds it. */
24391 if( !reserved ){
24392 sem_t *pSem = pFile->pOpen->pSem;
24393 struct stat statBuf;
24394
24395 if( sem_trywait(pSem)==-1 ){
24396 int tErrno = errno;
24397 if( EAGAIN != tErrno ){
24398 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24399 pFile->lastErrno = tErrno;
24400 } else {
24401 /* someone else has the lock when we are in NO_LOCK */
24402 reserved = (pFile->locktype < SHARED_LOCK);
24403 }
24404 }else{
24405 /* we could have it if we want it */
24406 sem_post(pSem);
24407 }
24408 }
24409 OSTRACE4("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved);
24410
24411 *pResOut = reserved;
24412 return rc;
24413 }
24414
24415 /*
24416 ** Lock the file with the lock specified by parameter locktype - one
24417 ** of the following:
24418 **
24419 ** (1) SHARED_LOCK
24420 ** (2) RESERVED_LOCK
24421 ** (3) PENDING_LOCK
@@ -24439,20 +24397,20 @@
24439 ** access the file.
24440 **
24441 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24442 ** routine to lower a locking level.
24443 */
24444 static int semLock(sqlite3_file *id, int locktype) {
24445 unixFile *pFile = (unixFile*)id;
24446 int fd;
24447 sem_t *pSem = pFile->pOpen->pSem;
24448 int rc = SQLITE_OK;
24449
24450 /* if we already have a lock, it is exclusive.
24451 ** Just adjust level and punt on outta here. */
24452 if (pFile->locktype > NO_LOCK) {
24453 pFile->locktype = locktype;
24454 rc = SQLITE_OK;
24455 goto sem_end_lock;
24456 }
24457
24458 /* lock semaphore now but bail out when already locked. */
@@ -24460,41 +24418,41 @@
24460 rc = SQLITE_BUSY;
24461 goto sem_end_lock;
24462 }
24463
24464 /* got it, set the type and return ok */
24465 pFile->locktype = locktype;
24466
24467 sem_end_lock:
24468 return rc;
24469 }
24470
24471 /*
24472 ** Lower the locking level on file descriptor pFile to locktype. locktype
24473 ** must be either NO_LOCK or SHARED_LOCK.
24474 **
24475 ** If the locking level of the file descriptor is already at or below
24476 ** the requested locking level, this routine is a no-op.
24477 */
24478 static int semUnlock(sqlite3_file *id, int locktype) {
24479 unixFile *pFile = (unixFile*)id;
24480 sem_t *pSem = pFile->pOpen->pSem;
24481
24482 assert( pFile );
24483 assert( pSem );
24484 OSTRACE5("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, locktype,
24485 pFile->locktype, getpid());
24486 assert( locktype<=SHARED_LOCK );
24487
24488 /* no-op if possible */
24489 if( pFile->locktype==locktype ){
24490 return SQLITE_OK;
24491 }
24492
24493 /* shared can just be set because we always have an exclusive */
24494 if (locktype==SHARED_LOCK) {
24495 pFile->locktype = locktype;
24496 return SQLITE_OK;
24497 }
24498
24499 /* no, really unlock. */
24500 if ( sem_post(pSem)==-1 ) {
@@ -24503,11 +24461,11 @@
24503 if( IS_LOCK_ERROR(rc) ){
24504 pFile->lastErrno = tErrno;
24505 }
24506 return rc;
24507 }
24508 pFile->locktype = NO_LOCK;
24509 return SQLITE_OK;
24510 }
24511
24512 /*
24513 ** Close a file.
@@ -24516,12 +24474,11 @@
24516 if( id ){
24517 unixFile *pFile = (unixFile*)id;
24518 semUnlock(id, NO_LOCK);
24519 assert( pFile );
24520 unixEnterMutex();
24521 releaseLockInfo(pFile->pLock);
24522 releaseOpenCnt(pFile->pOpen);
24523 unixLeaveMutex();
24524 closeUnixFile(id);
24525 }
24526 return SQLITE_OK;
24527 }
@@ -24586,19 +24543,19 @@
24586 pb.startEndFlag = 0;
24587 pb.offset = offset;
24588 pb.length = length;
24589 pb.fd = pFile->h;
24590
24591 OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
24592 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
24593 offset, length);
24594 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
24595 if ( err==-1 ) {
24596 int rc;
24597 int tErrno = errno;
24598 OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
24599 path, tErrno, strerror(tErrno));
24600 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
24601 rc = SQLITE_BUSY;
24602 #else
24603 rc = sqliteErrorFromPosixError(tErrno,
24604 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
@@ -24629,14 +24586,14 @@
24629 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24630 if( context->reserved ){
24631 *pResOut = 1;
24632 return SQLITE_OK;
24633 }
24634 unixEnterMutex(); /* Because pFile->pLock is shared across threads */
24635
24636 /* Check if a thread in this process holds such a lock */
24637 if( pFile->pLock->locktype>SHARED_LOCK ){
24638 reserved = 1;
24639 }
24640
24641 /* Otherwise see if some other process holds it.
24642 */
@@ -24655,18 +24612,18 @@
24655 rc=lrc;
24656 }
24657 }
24658
24659 unixLeaveMutex();
24660 OSTRACE4("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved);
24661
24662 *pResOut = reserved;
24663 return rc;
24664 }
24665
24666 /*
24667 ** Lock the file with the lock specified by parameter locktype - one
24668 ** of the following:
24669 **
24670 ** (1) SHARED_LOCK
24671 ** (2) RESERVED_LOCK
24672 ** (3) PENDING_LOCK
@@ -24685,84 +24642,76 @@
24685 ** PENDING -> EXCLUSIVE
24686 **
24687 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24688 ** routine to lower a locking level.
24689 */
24690 static int afpLock(sqlite3_file *id, int locktype){
24691 int rc = SQLITE_OK;
24692 unixFile *pFile = (unixFile*)id;
24693 struct unixLockInfo *pLock = pFile->pLock;
24694 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24695
24696 assert( pFile );
24697 OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
24698 locktypeName(locktype), locktypeName(pFile->locktype),
24699 locktypeName(pLock->locktype), pLock->cnt , getpid());
24700
24701 /* If there is already a lock of this type or more restrictive on the
24702 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
24703 ** unixEnterMutex() hasn't been called yet.
24704 */
24705 if( pFile->locktype>=locktype ){
24706 OSTRACE3("LOCK %d %s ok (already held) (afp)\n", pFile->h,
24707 locktypeName(locktype));
24708 return SQLITE_OK;
24709 }
24710
24711 /* Make sure the locking sequence is correct
24712 ** (1) We never move from unlocked to anything higher than shared lock.
24713 ** (2) SQLite never explicitly requests a pendig lock.
24714 ** (3) A shared lock is always held when a reserve lock is requested.
24715 */
24716 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
24717 assert( locktype!=PENDING_LOCK );
24718 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
24719
24720 /* This mutex is needed because pFile->pLock is shared across threads
24721 */
24722 unixEnterMutex();
24723
24724 /* Make sure the current thread owns the pFile.
24725 */
24726 rc = transferOwnership(pFile);
24727 if( rc!=SQLITE_OK ){
24728 unixLeaveMutex();
24729 return rc;
24730 }
24731 pLock = pFile->pLock;
24732
24733 /* If some thread using this PID has a lock via a different unixFile*
24734 ** handle that precludes the requested lock, return BUSY.
24735 */
24736 if( (pFile->locktype!=pLock->locktype &&
24737 (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK))
24738 ){
24739 rc = SQLITE_BUSY;
24740 goto afp_end_lock;
24741 }
24742
24743 /* If a SHARED lock is requested, and some thread using this PID already
24744 ** has a SHARED or RESERVED lock, then increment reference counts and
24745 ** return SQLITE_OK.
24746 */
24747 if( locktype==SHARED_LOCK &&
24748 (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){
24749 assert( locktype==SHARED_LOCK );
24750 assert( pFile->locktype==0 );
24751 assert( pLock->cnt>0 );
24752 pFile->locktype = SHARED_LOCK;
24753 pLock->cnt++;
24754 pFile->pOpen->nLock++;
24755 goto afp_end_lock;
24756 }
24757
24758 /* A PENDING lock is needed before acquiring a SHARED lock and before
24759 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
24760 ** be released.
24761 */
24762 if( locktype==SHARED_LOCK
24763 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK)
24764 ){
24765 int failed;
24766 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
24767 if (failed) {
24768 rc = failed;
@@ -24771,24 +24720,24 @@
24771 }
24772
24773 /* If control gets to this point, then actually go ahead and make
24774 ** operating system calls for the specified lock.
24775 */
24776 if( locktype==SHARED_LOCK ){
24777 int lrc1, lrc2, lrc1Errno;
24778 long lk, mask;
24779
24780 assert( pLock->cnt==0 );
24781 assert( pLock->locktype==0 );
24782
24783 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
24784 /* Now get the read-lock SHARED_LOCK */
24785 /* note that the quality of the randomness doesn't matter that much */
24786 lk = random();
24787 pLock->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
24788 lrc1 = afpSetLock(context->dbPath, pFile,
24789 SHARED_FIRST+pLock->sharedByte, 1, 1);
24790 if( IS_LOCK_ERROR(lrc1) ){
24791 lrc1Errno = pFile->lastErrno;
24792 }
24793 /* Drop the temporary PENDING lock */
24794 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
@@ -24801,46 +24750,46 @@
24801 rc = lrc2;
24802 goto afp_end_lock;
24803 } else if( lrc1 != SQLITE_OK ) {
24804 rc = lrc1;
24805 } else {
24806 pFile->locktype = SHARED_LOCK;
24807 pFile->pOpen->nLock++;
24808 pLock->cnt = 1;
24809 }
24810 }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){
24811 /* We are trying for an exclusive lock but another thread in this
24812 ** same process is still holding a shared lock. */
24813 rc = SQLITE_BUSY;
24814 }else{
24815 /* The request was for a RESERVED or EXCLUSIVE lock. It is
24816 ** assumed that there is a SHARED or greater lock on the file
24817 ** already.
24818 */
24819 int failed = 0;
24820 assert( 0!=pFile->locktype );
24821 if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) {
24822 /* Acquire a RESERVED lock */
24823 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
24824 if( !failed ){
24825 context->reserved = 1;
24826 }
24827 }
24828 if (!failed && locktype == EXCLUSIVE_LOCK) {
24829 /* Acquire an EXCLUSIVE lock */
24830
24831 /* Remove the shared lock before trying the range. we'll need to
24832 ** reestablish the shared lock if we can't get the afpUnlock
24833 */
24834 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
24835 pLock->sharedByte, 1, 0)) ){
24836 int failed2 = SQLITE_OK;
24837 /* now attemmpt to get the exclusive lock range */
24838 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
24839 SHARED_SIZE, 1);
24840 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
24841 SHARED_FIRST + pLock->sharedByte, 1, 1)) ){
24842 /* Can't reestablish the shared lock. Sqlite can't deal, this is
24843 ** a critical I/O error
24844 */
24845 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
24846 SQLITE_IOERR_LOCK;
@@ -24854,57 +24803,55 @@
24854 rc = failed;
24855 }
24856 }
24857
24858 if( rc==SQLITE_OK ){
24859 pFile->locktype = locktype;
24860 pLock->locktype = locktype;
24861 }else if( locktype==EXCLUSIVE_LOCK ){
24862 pFile->locktype = PENDING_LOCK;
24863 pLock->locktype = PENDING_LOCK;
24864 }
24865
24866 afp_end_lock:
24867 unixLeaveMutex();
24868 OSTRACE4("LOCK %d %s %s (afp)\n", pFile->h, locktypeName(locktype),
24869 rc==SQLITE_OK ? "ok" : "failed");
24870 return rc;
24871 }
24872
24873 /*
24874 ** Lower the locking level on file descriptor pFile to locktype. locktype
24875 ** must be either NO_LOCK or SHARED_LOCK.
24876 **
24877 ** If the locking level of the file descriptor is already at or below
24878 ** the requested locking level, this routine is a no-op.
24879 */
24880 static int afpUnlock(sqlite3_file *id, int locktype) {
24881 int rc = SQLITE_OK;
24882 unixFile *pFile = (unixFile*)id;
24883 struct unixLockInfo *pLock;
24884 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24885 int skipShared = 0;
24886 #ifdef SQLITE_TEST
24887 int h = pFile->h;
24888 #endif
24889
24890 assert( pFile );
24891 OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, locktype,
24892 pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
24893
24894 assert( locktype<=SHARED_LOCK );
24895 if( pFile->locktype<=locktype ){
24896 return SQLITE_OK;
24897 }
24898 if( CHECK_THREADID(pFile) ){
24899 return SQLITE_MISUSE_BKPT;
24900 }
24901 unixEnterMutex();
24902 pLock = pFile->pLock;
24903 assert( pLock->cnt!=0 );
24904 if( pFile->locktype>SHARED_LOCK ){
24905 assert( pLock->locktype==pFile->locktype );
24906 SimulateIOErrorBenign(1);
24907 SimulateIOError( h=(-1) )
24908 SimulateIOErrorBenign(0);
24909
24910 #ifndef NDEBUG
@@ -24920,66 +24867,64 @@
24920 || pFile->dbUpdate==0
24921 || pFile->transCntrChng==1 );
24922 pFile->inNormalWrite = 0;
24923 #endif
24924
24925 if( pFile->locktype==EXCLUSIVE_LOCK ){
24926 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
24927 if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1) ){
24928 /* only re-establish the shared lock if necessary */
24929 int sharedLockByte = SHARED_FIRST+pLock->sharedByte;
24930 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
24931 } else {
24932 skipShared = 1;
24933 }
24934 }
24935 if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){
24936 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
24937 }
24938 if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK && context->reserved ){
24939 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
24940 if( !rc ){
24941 context->reserved = 0;
24942 }
24943 }
24944 if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1)){
24945 pLock->locktype = SHARED_LOCK;
24946 }
24947 }
24948 if( rc==SQLITE_OK && locktype==NO_LOCK ){
24949
24950 /* Decrement the shared lock counter. Release the lock using an
24951 ** OS call only when all threads in this same process have released
24952 ** the lock.
24953 */
24954 unsigned long long sharedLockByte = SHARED_FIRST+pLock->sharedByte;
24955 pLock->cnt--;
24956 if( pLock->cnt==0 ){
24957 SimulateIOErrorBenign(1);
24958 SimulateIOError( h=(-1) )
24959 SimulateIOErrorBenign(0);
24960 if( !skipShared ){
24961 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
24962 }
24963 if( !rc ){
24964 pLock->locktype = NO_LOCK;
24965 pFile->locktype = NO_LOCK;
24966 }
24967 }
24968 if( rc==SQLITE_OK ){
24969 struct unixOpenCnt *pOpen = pFile->pOpen;
24970
24971 pOpen->nLock--;
24972 assert( pOpen->nLock>=0 );
24973 if( pOpen->nLock==0 ){
24974 rc = closePendingFds(pFile);
24975 }
24976 }
24977 }
24978
24979 unixLeaveMutex();
24980 if( rc==SQLITE_OK ) pFile->locktype = locktype;
24981 return rc;
24982 }
24983
24984 /*
24985 ** Close a file & cleanup AFP specific locking context
@@ -24988,20 +24933,19 @@
24988 int rc = SQLITE_OK;
24989 if( id ){
24990 unixFile *pFile = (unixFile*)id;
24991 afpUnlock(id, NO_LOCK);
24992 unixEnterMutex();
24993 if( pFile->pOpen && pFile->pOpen->nLock ){
24994 /* If there are outstanding locks, do not actually close the file just
24995 ** yet because that would clear those locks. Instead, add the file
24996 ** descriptor to pOpen->aPending. It will be automatically closed when
24997 ** the last lock is cleared.
24998 */
24999 setPendingFd(pFile);
25000 }
25001 releaseLockInfo(pFile->pLock);
25002 releaseOpenCnt(pFile->pOpen);
25003 sqlite3_free(pFile->lockingContext);
25004 rc = closeUnixFile(id);
25005 unixLeaveMutex();
25006 }
25007 return rc;
@@ -25020,18 +24964,18 @@
25020 /******************************************************************************
25021 *************************** Begin NFS Locking ********************************/
25022
25023 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25024 /*
25025 ** Lower the locking level on file descriptor pFile to locktype. locktype
25026 ** must be either NO_LOCK or SHARED_LOCK.
25027 **
25028 ** If the locking level of the file descriptor is already at or below
25029 ** the requested locking level, this routine is a no-op.
25030 */
25031 static int nfsUnlock(sqlite3_file *id, int locktype){
25032 return _posixUnlock(id, locktype, 1);
25033 }
25034
25035 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
25036 /*
25037 ** The code above is the NFS lock implementation. The code is specific
@@ -25091,11 +25035,11 @@
25091 #endif
25092 TIMER_END;
25093 if( got<0 ){
25094 ((unixFile*)id)->lastErrno = errno;
25095 }
25096 OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
25097 return got;
25098 }
25099
25100 /*
25101 ** Read data from a file into a buffer. Return SQLITE_OK if all
@@ -25112,14 +25056,16 @@
25112 int got;
25113 assert( id );
25114
25115 /* If this is a database file (not a journal, master-journal or temp
25116 ** file), the bytes in the locking range should never be read or written. */
 
25117 assert( pFile->pUnused==0
25118 || offset>=PENDING_BYTE+512
25119 || offset+amt<=PENDING_BYTE
25120 );
 
25121
25122 got = seekAndRead(pFile, offset, pBuf, amt);
25123 if( got==amt ){
25124 return SQLITE_OK;
25125 }else if( got<0 ){
@@ -25165,11 +25111,11 @@
25165 TIMER_END;
25166 if( got<0 ){
25167 ((unixFile*)id)->lastErrno = errno;
25168 }
25169
25170 OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED);
25171 return got;
25172 }
25173
25174
25175 /*
@@ -25187,14 +25133,16 @@
25187 assert( id );
25188 assert( amt>0 );
25189
25190 /* If this is a database file (not a journal, master-journal or temp
25191 ** file), the bytes in the locking range should never be read or written. */
 
25192 assert( pFile->pUnused==0
25193 || offset>=PENDING_BYTE+512
25194 || offset+amt<=PENDING_BYTE
25195 );
 
25196
25197 #ifndef NDEBUG
25198 /* If we are doing a normal write to a database file (as opposed to
25199 ** doing a hot-journal rollback or a write to some file other than a
25200 ** normal database file) then record the fact that the database
@@ -25389,21 +25337,21 @@
25389 ** line is to test that doing so does not cause any problems.
25390 */
25391 SimulateDiskfullError( return SQLITE_FULL );
25392
25393 assert( pFile );
25394 OSTRACE2("SYNC %-3d\n", pFile->h);
25395 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
25396 SimulateIOError( rc=1 );
25397 if( rc ){
25398 pFile->lastErrno = errno;
25399 return SQLITE_IOERR_FSYNC;
25400 }
25401 if( pFile->dirfd>=0 ){
25402 int err;
25403 OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
25404 HAVE_FULLFSYNC, isFullsync);
25405 #ifndef SQLITE_DISABLE_DIRSYNC
25406 /* The directory sync is only attempted if full_fsync is
25407 ** turned off or unavailable. If a full_fsync occurred above,
25408 ** then the directory sync is superfluous.
25409 */
@@ -25471,11 +25419,11 @@
25471 ((unixFile*)id)->lastErrno = errno;
25472 return SQLITE_IOERR_FSTAT;
25473 }
25474 *pSize = buf.st_size;
25475
25476 /* When opening a zero-size database, the findLockInfo() procedure
25477 ** writes a single byte into that file in order to work around a bug
25478 ** in the OS-X msdos filesystem. In order to avoid problems with upper
25479 ** layers, we need to report this file size as zero even though it is
25480 ** really 1. Ticket #3260.
25481 */
@@ -25498,16 +25446,24 @@
25498 ** Information and control of an open file handle.
25499 */
25500 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
25501 switch( op ){
25502 case SQLITE_FCNTL_LOCKSTATE: {
25503 *(int*)pArg = ((unixFile*)id)->locktype;
25504 return SQLITE_OK;
25505 }
25506 case SQLITE_LAST_ERRNO: {
25507 *(int*)pArg = ((unixFile*)id)->lastErrno;
25508 return SQLITE_OK;
 
 
 
 
 
 
 
 
25509 }
25510 #ifndef NDEBUG
25511 /* The pager calls this method to signal that it has done
25512 ** a rollback and that the database is therefore unchanged and
25513 ** it hence it is OK for the transaction change counter to be
@@ -25549,10 +25505,571 @@
25549 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
25550 UNUSED_PARAMETER(NotUsed);
25551 return 0;
25552 }
25553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25554 /*
25555 ** Here ends the implementation of all sqlite3_file methods.
25556 **
25557 ********************** End sqlite3_file Methods *******************************
25558 ******************************************************************************/
@@ -25589,13 +26106,13 @@
25589 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
25590 **
25591 ** * An I/O method finder function called FINDER that returns a pointer
25592 ** to the METHOD object in the previous bullet.
25593 */
25594 #define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \
25595 static const sqlite3_io_methods METHOD = { \
25596 1, /* iVersion */ \
25597 CLOSE, /* xClose */ \
25598 unixRead, /* xRead */ \
25599 unixWrite, /* xWrite */ \
25600 unixTruncate, /* xTruncate */ \
25601 unixSync, /* xSync */ \
@@ -25603,11 +26120,16 @@
25603 LOCK, /* xLock */ \
25604 UNLOCK, /* xUnlock */ \
25605 CKLOCK, /* xCheckReservedLock */ \
25606 unixFileControl, /* xFileControl */ \
25607 unixSectorSize, /* xSectorSize */ \
25608 unixDeviceCharacteristics /* xDeviceCapabilities */ \
 
 
 
 
 
25609 }; \
25610 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
25611 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
25612 return &METHOD; \
25613 } \
@@ -25620,26 +26142,29 @@
25620 ** are also created.
25621 */
25622 IOMETHODS(
25623 posixIoFinder, /* Finder function name */
25624 posixIoMethods, /* sqlite3_io_methods object name */
 
25625 unixClose, /* xClose method */
25626 unixLock, /* xLock method */
25627 unixUnlock, /* xUnlock method */
25628 unixCheckReservedLock /* xCheckReservedLock method */
25629 )
25630 IOMETHODS(
25631 nolockIoFinder, /* Finder function name */
25632 nolockIoMethods, /* sqlite3_io_methods object name */
 
25633 nolockClose, /* xClose method */
25634 nolockLock, /* xLock method */
25635 nolockUnlock, /* xUnlock method */
25636 nolockCheckReservedLock /* xCheckReservedLock method */
25637 )
25638 IOMETHODS(
25639 dotlockIoFinder, /* Finder function name */
25640 dotlockIoMethods, /* sqlite3_io_methods object name */
 
25641 dotlockClose, /* xClose method */
25642 dotlockLock, /* xLock method */
25643 dotlockUnlock, /* xUnlock method */
25644 dotlockCheckReservedLock /* xCheckReservedLock method */
25645 )
@@ -25646,10 +26171,11 @@
25646
25647 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
25648 IOMETHODS(
25649 flockIoFinder, /* Finder function name */
25650 flockIoMethods, /* sqlite3_io_methods object name */
 
25651 flockClose, /* xClose method */
25652 flockLock, /* xLock method */
25653 flockUnlock, /* xUnlock method */
25654 flockCheckReservedLock /* xCheckReservedLock method */
25655 )
@@ -25657,10 +26183,11 @@
25657
25658 #if OS_VXWORKS
25659 IOMETHODS(
25660 semIoFinder, /* Finder function name */
25661 semIoMethods, /* sqlite3_io_methods object name */
 
25662 semClose, /* xClose method */
25663 semLock, /* xLock method */
25664 semUnlock, /* xUnlock method */
25665 semCheckReservedLock /* xCheckReservedLock method */
25666 )
@@ -25668,10 +26195,11 @@
25668
25669 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25670 IOMETHODS(
25671 afpIoFinder, /* Finder function name */
25672 afpIoMethods, /* sqlite3_io_methods object name */
 
25673 afpClose, /* xClose method */
25674 afpLock, /* xLock method */
25675 afpUnlock, /* xUnlock method */
25676 afpCheckReservedLock /* xCheckReservedLock method */
25677 )
@@ -25692,10 +26220,11 @@
25692 static int proxyUnlock(sqlite3_file*, int);
25693 static int proxyCheckReservedLock(sqlite3_file*, int*);
25694 IOMETHODS(
25695 proxyIoFinder, /* Finder function name */
25696 proxyIoMethods, /* sqlite3_io_methods object name */
 
25697 proxyClose, /* xClose method */
25698 proxyLock, /* xLock method */
25699 proxyUnlock, /* xUnlock method */
25700 proxyCheckReservedLock /* xCheckReservedLock method */
25701 )
@@ -25704,10 +26233,11 @@
25704 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
25705 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25706 IOMETHODS(
25707 nfsIoFinder, /* Finder function name */
25708 nfsIoMethods, /* sqlite3_io_methods object name */
 
25709 unixClose, /* xClose method */
25710 unixLock, /* xLock method */
25711 nfsUnlock, /* xUnlock method */
25712 unixCheckReservedLock /* xCheckReservedLock method */
25713 )
@@ -25844,23 +26374,23 @@
25844 ){
25845 const sqlite3_io_methods *pLockingStyle;
25846 unixFile *pNew = (unixFile *)pId;
25847 int rc = SQLITE_OK;
25848
25849 assert( pNew->pLock==NULL );
25850 assert( pNew->pOpen==NULL );
25851
25852 /* Parameter isDelete is only used on vxworks. Express this explicitly
25853 ** here to prevent compiler warnings about unused parameters.
25854 */
25855 UNUSED_PARAMETER(isDelete);
25856
25857 OSTRACE3("OPEN %-3d %s\n", h, zFilename);
25858 pNew->h = h;
25859 pNew->dirfd = dirfd;
25860 SET_THREADID(pNew);
25861 pNew->fileFlags = 0;
 
 
25862
25863 #if OS_VXWORKS
25864 pNew->pId = vxworksFindFileId(zFilename);
25865 if( pNew->pId==0 ){
25866 noLock = 1;
@@ -25884,23 +26414,23 @@
25884 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25885 || pLockingStyle == &nfsIoMethods
25886 #endif
25887 ){
25888 unixEnterMutex();
25889 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
25890 if( rc!=SQLITE_OK ){
25891 /* If an error occured in findLockInfo(), close the file descriptor
25892 ** immediately, before releasing the mutex. findLockInfo() may fail
25893 ** in two scenarios:
25894 **
25895 ** (a) A call to fstat() failed.
25896 ** (b) A malloc failed.
25897 **
25898 ** Scenario (b) may only occur if the process is holding no other
25899 ** file descriptors open on the same file. If there were other file
25900 ** descriptors on this file, then no malloc would be required by
25901 ** findLockInfo(). If this is the case, it is quite safe to close
25902 ** handle h - as it is guaranteed that no posix locks will be released
25903 ** by doing so.
25904 **
25905 ** If scenario (a) caused the error then things are not so safe. The
25906 ** implicit assumption here is that if fstat() fails, things are in
@@ -25927,11 +26457,11 @@
25927 ** copy of the filename. */
25928 pCtx->dbPath = zFilename;
25929 pCtx->reserved = 0;
25930 srandomdev();
25931 unixEnterMutex();
25932 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
25933 if( rc!=SQLITE_OK ){
25934 sqlite3_free(pNew->lockingContext);
25935 close(h);
25936 h = -1;
25937 }
@@ -25960,22 +26490,22 @@
25960 else if( pLockingStyle == &semIoMethods ){
25961 /* Named semaphore locking uses the file path so it needs to be
25962 ** included in the semLockingContext
25963 */
25964 unixEnterMutex();
25965 rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen);
25966 if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){
25967 char *zSemName = pNew->pOpen->aSemName;
25968 int n;
25969 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
25970 pNew->pId->zCanonicalName);
25971 for( n=1; zSemName[n]; n++ )
25972 if( zSemName[n]=='/' ) zSemName[n] = '_';
25973 pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
25974 if( pNew->pOpen->pSem == SEM_FAILED ){
25975 rc = SQLITE_NOMEM;
25976 pNew->pOpen->aSemName[0] = '\0';
25977 }
25978 }
25979 unixLeaveMutex();
25980 }
25981 #endif
@@ -26022,58 +26552,75 @@
26022 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
26023 if( fd>=0 ){
26024 #ifdef FD_CLOEXEC
26025 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
26026 #endif
26027 OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname);
26028 }
26029 }
26030 *pFd = fd;
26031 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT);
26032 }
26033
26034 /*
26035 ** Create a temporary file name in zBuf. zBuf must be allocated
26036 ** by the calling process and must be big enough to hold at least
26037 ** pVfs->mxPathname bytes.
26038 */
26039 static int getTempname(int nBuf, char *zBuf){
26040 static const char *azDirs[] = {
26041 0,
26042 0,
26043 "/var/tmp",
26044 "/usr/tmp",
26045 "/tmp",
26046 ".",
26047 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26048 static const unsigned char zChars[] =
26049 "abcdefghijklmnopqrstuvwxyz"
26050 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26051 "0123456789";
26052 unsigned int i, j;
26053 struct stat buf;
26054 const char *zDir = ".";
26055
26056 /* It's odd to simulate an io-error here, but really this is just
26057 ** using the io-error infrastructure to test that SQLite handles this
26058 ** function failing.
26059 */
26060 SimulateIOError( return SQLITE_IOERR );
26061
26062 azDirs[0] = sqlite3_temp_directory;
26063 if (NULL == azDirs[1]) {
26064 azDirs[1] = getenv("TMPDIR");
26065 }
26066
26067 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
26068 if( azDirs[i]==0 ) continue;
26069 if( stat(azDirs[i], &buf) ) continue;
26070 if( !S_ISDIR(buf.st_mode) ) continue;
26071 if( access(azDirs[i], 07) ) continue;
26072 zDir = azDirs[i];
26073 break;
26074 }
26075
26076 /* Check that the output buffer is large enough for the temporary file
26077 ** name. If it is not, return SQLITE_ERROR.
26078 */
26079 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
@@ -26135,21 +26682,21 @@
26135 ** descriptor on the same path, fail, and return an error to SQLite.
26136 **
26137 ** Even if a subsequent open() call does succeed, the consequences of
26138 ** not searching for a resusable file descriptor are not dire. */
26139 if( 0==stat(zPath, &sStat) ){
26140 struct unixOpenCnt *pOpen;
26141
26142 unixEnterMutex();
26143 pOpen = openList;
26144 while( pOpen && (pOpen->fileId.dev!=sStat.st_dev
26145 || pOpen->fileId.ino!=sStat.st_ino) ){
26146 pOpen = pOpen->pNext;
26147 }
26148 if( pOpen ){
26149 UnixUnusedFd **pp;
26150 for(pp=&pOpen->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
26151 pUnused = *pp;
26152 if( pUnused ){
26153 *pp = pUnused->pNext;
26154 }
26155 }
@@ -26259,11 +26806,11 @@
26259 }
26260 p->pUnused = pUnused;
26261 }else if( !zName ){
26262 /* If zName is NULL, the upper layer is requesting a temp file. */
26263 assert(isDelete && !isOpenDirectory);
26264 rc = getTempname(MAX_PATHNAME+1, zTmpname);
26265 if( rc!=SQLITE_OK ){
26266 return rc;
26267 }
26268 zName = zTmpname;
26269 }
@@ -26279,11 +26826,11 @@
26279 openFlags |= (O_LARGEFILE|O_BINARY);
26280
26281 if( fd<0 ){
26282 mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
26283 fd = open(zName, openFlags, openMode);
26284 OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags);
26285 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
26286 /* Failed to open the file for read/write access. Try read-only. */
26287 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
26288 openFlags &= ~(O_RDWR|O_CREAT);
26289 flags |= SQLITE_OPEN_READONLY;
@@ -26663,41 +27210,54 @@
26663 ** sqlite3OsCurrentTime() during testing.
26664 */
26665 #ifdef SQLITE_TEST
26666 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
26667 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26668
26669 /*
26670 ** Find the current time (in Universal Coordinated Time). Write the
26671 ** current time and date as a Julian Day number into *prNow and
26672 ** return 0. Return 1 if the time and date cannot be found.
26673 */
26674 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
26675 #if defined(SQLITE_OMIT_FLOATING_POINT)
26676 time_t t;
26677 time(&t);
26678 *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10;
26679 #elif defined(NO_GETTOD)
26680 time_t t;
26681 time(&t);
26682 *prNow = t/86400.0 + 2440587.5;
26683 #elif OS_VXWORKS
26684 struct timespec sNow;
26685 clock_gettime(CLOCK_REALTIME, &sNow);
26686 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0;
26687 #else
26688 struct timeval sNow;
26689 gettimeofday(&sNow, 0);
26690 *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0;
26691 #endif
26692
26693 #ifdef SQLITE_TEST
26694 if( sqlite3_current_time ){
26695 *prNow = sqlite3_current_time/86400.0 + 2440587.5;
26696 }
26697 #endif
26698 UNUSED_PARAMETER(NotUsed);
26699 return 0;
26700 }
26701
26702 /*
26703 ** We added the xGetLastError() method with the intention of providing
@@ -26710,10 +27270,11 @@
26710 UNUSED_PARAMETER(NotUsed);
26711 UNUSED_PARAMETER(NotUsed2);
26712 UNUSED_PARAMETER(NotUsed3);
26713 return 0;
26714 }
 
26715
26716 /*
26717 ************************ End of sqlite3_vfs methods ***************************
26718 ******************************************************************************/
26719
@@ -26901,12 +27462,12 @@
26901 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
26902 #else
26903 # ifdef _CS_DARWIN_USER_TEMP_DIR
26904 {
26905 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
26906 OSTRACE4("GETLOCKPATH failed %s errno=%d pid=%d\n",
26907 lPath, errno, getpid());
26908 return SQLITE_IOERR_LOCK;
26909 }
26910 len = strlcat(lPath, "sqliteplocks", maxLen);
26911 }
26912 # else
@@ -26924,11 +27485,11 @@
26924 char c = dbPath[i];
26925 lPath[i+len] = (c=='/')?'_':c;
26926 }
26927 lPath[i+len]='\0';
26928 strlcat(lPath, ":auto:", maxLen);
26929 OSTRACE3("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid());
26930 return SQLITE_OK;
26931 }
26932
26933 /*
26934 ** Creates the lock file and any missing directories in lockPath
@@ -26949,22 +27510,22 @@
26949 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
26950 buf[i]='\0';
26951 if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
26952 int err=errno;
26953 if( err!=EEXIST ) {
26954 OSTRACE5("CREATELOCKPATH FAILED creating %s, "
26955 "'%s' proxy lock path=%s pid=%d\n",
26956 buf, strerror(err), lockPath, getpid());
26957 return err;
26958 }
26959 }
26960 }
26961 start=i+1;
26962 }
26963 buf[i] = lockPath[i];
26964 }
26965 OSTRACE3("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid());
26966 return 0;
26967 }
26968
26969 /*
26970 ** Create a new VFS file descriptor (stored in memory obtained from
@@ -27252,12 +27813,12 @@
27252 int hostIdMatch = 0;
27253 int readLen = 0;
27254 int tryOldLockPath = 0;
27255 int forceNewLockPath = 0;
27256
27257 OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h,
27258 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid());
27259
27260 rc = proxyGetHostID(myHostID, &pError);
27261 if( (rc&0xff)==SQLITE_IOERR ){
27262 pFile->lastErrno = pError;
27263 goto end_takeconch;
@@ -27333,11 +27894,11 @@
27333 ** has a shared lock already), if the host id matches, use the big
27334 ** stick.
27335 */
27336 futimes(conchFile->h, NULL);
27337 if( hostIdMatch && !createConch ){
27338 if( conchFile->pLock && conchFile->pLock->cnt>1 ){
27339 /* We are trying for an exclusive lock but another thread in this
27340 ** same process is still holding a shared lock. */
27341 rc = SQLITE_BUSY;
27342 } else {
27343 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
@@ -27389,11 +27950,11 @@
27389 }
27390 }
27391 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
27392
27393 end_takeconch:
27394 OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h);
27395 if( rc==SQLITE_OK && pFile->openFlags ){
27396 if( pFile->h>=0 ){
27397 #ifdef STRICT_CLOSE_ERROR
27398 if( close(pFile->h) ){
27399 pFile->lastErrno = errno;
@@ -27404,11 +27965,11 @@
27404 #endif
27405 }
27406 pFile->h = -1;
27407 int fd = open(pCtx->dbPath, pFile->openFlags,
27408 SQLITE_DEFAULT_FILE_PERMISSIONS);
27409 OSTRACE2("TRANSPROXY: OPEN %d\n", fd);
27410 if( fd>=0 ){
27411 pFile->h = fd;
27412 }else{
27413 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
27414 during locking */
@@ -27446,35 +28007,37 @@
27446 afpCtx->dbPath = pCtx->lockProxyPath;
27447 }
27448 } else {
27449 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
27450 }
27451 OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed");
 
27452 return rc;
27453 } while (1); /* in case we need to retry the :auto: lock file - we should never get here except via the 'continue' call. */
 
27454 }
27455 }
27456
27457 /*
27458 ** If pFile holds a lock on a conch file, then release that lock.
27459 */
27460 static int proxyReleaseConch(unixFile *pFile){
27461 int rc; /* Subroutine return code */
27462 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
27463 unixFile *conchFile; /* Name of the conch file */
27464
27465 pCtx = (proxyLockingContext *)pFile->lockingContext;
27466 conchFile = pCtx->conchFile;
27467 OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
27468 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
27469 getpid());
27470 if( pCtx->conchHeld>0 ){
27471 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
27472 }
27473 pCtx->conchHeld = 0;
27474 OSTRACE3("RELEASECONCH %d %s\n", conchFile->h,
27475 (rc==SQLITE_OK ? "ok" : "failed"));
27476 return rc;
27477 }
27478
27479 /*
27480 ** Given the name of a database file, compute the name of its conch file.
@@ -27527,11 +28090,11 @@
27527 static int switchLockProxyPath(unixFile *pFile, const char *path) {
27528 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
27529 char *oldPath = pCtx->lockProxyPath;
27530 int rc = SQLITE_OK;
27531
27532 if( pFile->locktype!=NO_LOCK ){
27533 return SQLITE_BUSY;
27534 }
27535
27536 /* nothing to do if the path is NULL, :auto: or matches the existing path */
27537 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
@@ -27594,22 +28157,22 @@
27594 proxyLockingContext *pCtx;
27595 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
27596 char *lockPath=NULL;
27597 int rc = SQLITE_OK;
27598
27599 if( pFile->locktype!=NO_LOCK ){
27600 return SQLITE_BUSY;
27601 }
27602 proxyGetDbPathForUnixFile(pFile, dbPath);
27603 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
27604 lockPath=NULL;
27605 }else{
27606 lockPath=(char *)path;
27607 }
27608
27609 OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h,
27610 (lockPath ? lockPath : ":auto:"), getpid());
27611
27612 pCtx = sqlite3_malloc( sizeof(*pCtx) );
27613 if( pCtx==0 ){
27614 return SQLITE_NOMEM;
27615 }
@@ -27665,12 +28228,12 @@
27665 }
27666 sqlite3_free(pCtx->lockProxyPath);
27667 sqlite3_free(pCtx->conchFilePath);
27668 sqlite3_free(pCtx);
27669 }
27670 OSTRACE3("TRANSPROXY %d %s\n", pFile->h,
27671 (rc==SQLITE_OK ? "ok" : "failed"));
27672 return rc;
27673 }
27674
27675
27676 /*
@@ -27761,11 +28324,11 @@
27761 }
27762 return rc;
27763 }
27764
27765 /*
27766 ** Lock the file with the lock specified by parameter locktype - one
27767 ** of the following:
27768 **
27769 ** (1) SHARED_LOCK
27770 ** (2) RESERVED_LOCK
27771 ** (3) PENDING_LOCK
@@ -27784,43 +28347,43 @@
27784 ** PENDING -> EXCLUSIVE
27785 **
27786 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
27787 ** routine to lower a locking level.
27788 */
27789 static int proxyLock(sqlite3_file *id, int locktype) {
27790 unixFile *pFile = (unixFile*)id;
27791 int rc = proxyTakeConch(pFile);
27792 if( rc==SQLITE_OK ){
27793 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
27794 if( pCtx->conchHeld>0 ){
27795 unixFile *proxy = pCtx->lockProxy;
27796 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype);
27797 pFile->locktype = proxy->locktype;
27798 }else{
27799 /* conchHeld < 0 is lockless */
27800 }
27801 }
27802 return rc;
27803 }
27804
27805
27806 /*
27807 ** Lower the locking level on file descriptor pFile to locktype. locktype
27808 ** must be either NO_LOCK or SHARED_LOCK.
27809 **
27810 ** If the locking level of the file descriptor is already at or below
27811 ** the requested locking level, this routine is a no-op.
27812 */
27813 static int proxyUnlock(sqlite3_file *id, int locktype) {
27814 unixFile *pFile = (unixFile*)id;
27815 int rc = proxyTakeConch(pFile);
27816 if( rc==SQLITE_OK ){
27817 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
27818 if( pCtx->conchHeld>0 ){
27819 unixFile *proxy = pCtx->lockProxy;
27820 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype);
27821 pFile->locktype = proxy->locktype;
27822 }else{
27823 /* conchHeld < 0 is lockless */
27824 }
27825 }
27826 return rc;
@@ -27911,11 +28474,11 @@
27911 ** more than that; it looks at the filesystem type that hosts the
27912 ** database file and tries to choose an locking method appropriate for
27913 ** that filesystem time.
27914 */
27915 #define UNIXVFS(VFSNAME, FINDER) { \
27916 1, /* iVersion */ \
27917 sizeof(unixFile), /* szOsFile */ \
27918 MAX_PATHNAME, /* mxPathname */ \
27919 0, /* pNext */ \
27920 VFSNAME, /* zName */ \
27921 (void*)&FINDER, /* pAppData */ \
@@ -27928,11 +28491,13 @@
27928 unixDlSym, /* xDlSym */ \
27929 unixDlClose, /* xDlClose */ \
27930 unixRandomness, /* xRandomness */ \
27931 unixSleep, /* xSleep */ \
27932 unixCurrentTime, /* xCurrentTime */ \
27933 unixGetLastError /* xGetLastError */ \
 
 
27934 }
27935
27936 /*
27937 ** All default VFSes for unix are contained in the following array.
27938 **
@@ -28079,27 +28644,13 @@
28079 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
28080 #endif
28081
28082 #ifdef SQLITE_DEBUG
28083 SQLITE_PRIVATE int sqlite3OSTrace = 0;
28084 #define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X)
28085 #define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y)
28086 #define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z)
28087 #define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A)
28088 #define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B)
28089 #define OSTRACE6(X,Y,Z,A,B,C) \
28090 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
28091 #define OSTRACE7(X,Y,Z,A,B,C,D) \
28092 if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
28093 #else
28094 #define OSTRACE1(X)
28095 #define OSTRACE2(X,Y)
28096 #define OSTRACE3(X,Y,Z)
28097 #define OSTRACE4(X,Y,Z,A)
28098 #define OSTRACE5(X,Y,Z,A,B)
28099 #define OSTRACE6(X,Y,Z,A,B,C)
28100 #define OSTRACE7(X,Y,Z,A,B,C,D)
28101 #endif
28102
28103 /*
28104 ** Macros for performance tracing. Normally turned off. Only works
28105 ** on i486 hardware.
@@ -28281,10 +28832,14 @@
28281 #if SQLITE_OS_WINCE
28282 # define AreFileApisANSI() 1
28283 # define FormatMessageW(a,b,c,d,e,f,g) 0
28284 #endif
28285
 
 
 
 
28286 /*
28287 ** WinCE lacks native support for file locking so we have to fake it
28288 ** with some code of our own.
28289 */
28290 #if SQLITE_OS_WINCE
@@ -28300,16 +28855,19 @@
28300 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
28301 ** portability layer.
28302 */
28303 typedef struct winFile winFile;
28304 struct winFile {
28305 const sqlite3_io_methods *pMethod;/* Must be first */
 
28306 HANDLE h; /* Handle for accessing the file */
28307 unsigned char locktype; /* Type of lock currently held on this file */
28308 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
28309 DWORD lastErrno; /* The Windows errno from the last I/O error */
28310 DWORD sectorSize; /* Sector size of the device file is on */
 
 
28311 #if SQLITE_OS_WINCE
28312 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
28313 HANDLE hMutex; /* Mutex used to control access to shared lock */
28314 HANDLE hShared; /* Shared memory segment used for locking */
28315 winceLock local; /* Locks obtained by this instance of winFile */
@@ -28833,11 +29391,12 @@
28833 static int winClose(sqlite3_file *id){
28834 int rc, cnt = 0;
28835 winFile *pFile = (winFile*)id;
28836
28837 assert( id!=0 );
28838 OSTRACE2("CLOSE %d\n", pFile->h);
 
28839 do{
28840 rc = CloseHandle(pFile->h);
28841 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
28842 #if SQLITE_OS_WINCE
28843 #define WINCE_DELETION_ATTEMPTS 3
@@ -28852,10 +29411,11 @@
28852 Sleep(100); /* Wait a little before trying again */
28853 }
28854 free(pFile->zDeleteOnClose);
28855 }
28856 #endif
 
28857 OpenCounter(-1);
28858 return rc ? SQLITE_OK : SQLITE_IOERR;
28859 }
28860
28861 /*
@@ -28883,11 +29443,11 @@
28883 DWORD error;
28884 DWORD got;
28885
28886 assert( id!=0 );
28887 SimulateIOError(return SQLITE_IOERR_READ);
28888 OSTRACE3("READ %d lock=%d\n", pFile->h, pFile->locktype);
28889 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
28890 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
28891 pFile->lastErrno = error;
28892 return SQLITE_FULL;
28893 }
@@ -28922,11 +29482,11 @@
28922 DWORD wrote = 0;
28923
28924 assert( id!=0 );
28925 SimulateIOError(return SQLITE_IOERR_WRITE);
28926 SimulateDiskfullError(return SQLITE_FULL);
28927 OSTRACE3("WRITE %d lock=%d\n", pFile->h, pFile->locktype);
28928 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
28929 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
28930 pFile->lastErrno = error;
28931 return SQLITE_FULL;
28932 }
@@ -28950,28 +29510,29 @@
28950 ** Truncate an open file to a specified size
28951 */
28952 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
28953 LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
28954 LONG lowerBits = (LONG)(nByte & 0xffffffff);
28955 DWORD rc;
28956 winFile *pFile = (winFile*)id;
28957 DWORD error;
 
28958
28959 assert( id!=0 );
28960 OSTRACE3("TRUNCATE %d %lld\n", pFile->h, nByte);
28961 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
28962 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
28963 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
28964 pFile->lastErrno = error;
28965 return SQLITE_IOERR_TRUNCATE;
28966 }
28967 /* SetEndOfFile will fail if nByte is negative */
28968 if( !SetEndOfFile(pFile->h) ){
28969 pFile->lastErrno = GetLastError();
28970 return SQLITE_IOERR_TRUNCATE;
28971 }
28972 return SQLITE_OK;
 
28973 }
28974
28975 #ifdef SQLITE_TEST
28976 /*
28977 ** Count the number of fullsyncs and normal syncs. This is used to test
@@ -28987,11 +29548,11 @@
28987 static int winSync(sqlite3_file *id, int flags){
28988 #ifndef SQLITE_NO_SYNC
28989 winFile *pFile = (winFile*)id;
28990
28991 assert( id!=0 );
28992 OSTRACE3("SYNC %d lock=%d\n", pFile->h, pFile->locktype);
28993 #else
28994 UNUSED_PARAMETER(id);
28995 #endif
28996 #ifndef SQLITE_TEST
28997 UNUSED_PARAMETER(flags);
@@ -29128,12 +29689,12 @@
29128 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
29129 winFile *pFile = (winFile*)id;
29130 DWORD error = NO_ERROR;
29131
29132 assert( id!=0 );
29133 OSTRACE5("LOCK %d %d was %d(%d)\n",
29134 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
29135
29136 /* If there is already a lock of this type or more restrictive on the
29137 ** OsFile, do nothing. Don't use the end_lock: exit path, as
29138 ** sqlite3OsEnterMutex() hasn't been called yet.
29139 */
@@ -29159,11 +29720,11 @@
29159 int cnt = 3;
29160 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
29161 /* Try 3 times to get the pending lock. The pending lock might be
29162 ** held by another reader process who will release it momentarily.
29163 */
29164 OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
29165 Sleep(1);
29166 }
29167 gotPendingLock = res;
29168 if( !res ){
29169 error = GetLastError();
@@ -29204,17 +29765,17 @@
29204 /* Acquire an EXCLUSIVE lock
29205 */
29206 if( locktype==EXCLUSIVE_LOCK && res ){
29207 assert( pFile->locktype>=SHARED_LOCK );
29208 res = unlockReadLock(pFile);
29209 OSTRACE2("unreadlock = %d\n", res);
29210 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
29211 if( res ){
29212 newLocktype = EXCLUSIVE_LOCK;
29213 }else{
29214 error = GetLastError();
29215 OSTRACE2("error-code = %d\n", error);
29216 getReadLock(pFile);
29217 }
29218 }
29219
29220 /* If we are holding a PENDING lock that ought to be released, then
@@ -29228,12 +29789,12 @@
29228 ** return the appropriate result code.
29229 */
29230 if( res ){
29231 rc = SQLITE_OK;
29232 }else{
29233 OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
29234 locktype, newLocktype);
29235 pFile->lastErrno = error;
29236 rc = SQLITE_BUSY;
29237 }
29238 pFile->locktype = (u8)newLocktype;
29239 return rc;
@@ -29249,18 +29810,18 @@
29249 winFile *pFile = (winFile*)id;
29250
29251 assert( id!=0 );
29252 if( pFile->locktype>=RESERVED_LOCK ){
29253 rc = 1;
29254 OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
29255 }else{
29256 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
29257 if( rc ){
29258 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
29259 }
29260 rc = !rc;
29261 OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
29262 }
29263 *pResOut = rc;
29264 return SQLITE_OK;
29265 }
29266
@@ -29279,12 +29840,12 @@
29279 int type;
29280 winFile *pFile = (winFile*)id;
29281 int rc = SQLITE_OK;
29282 assert( pFile!=0 );
29283 assert( locktype<=SHARED_LOCK );
29284 OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
29285 pFile->locktype, pFile->sharedLockByte);
29286 type = pFile->locktype;
29287 if( type>=EXCLUSIVE_LOCK ){
29288 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
29289 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
29290 /* This should never happen. We should always be able to
@@ -29316,10 +29877,15 @@
29316 }
29317 case SQLITE_LAST_ERRNO: {
29318 *(int*)pArg = (int)((winFile*)id)->lastErrno;
29319 return SQLITE_OK;
29320 }
 
 
 
 
 
29321 }
29322 return SQLITE_ERROR;
29323 }
29324
29325 /*
@@ -29343,16 +29909,535 @@
29343 static int winDeviceCharacteristics(sqlite3_file *id){
29344 UNUSED_PARAMETER(id);
29345 return 0;
29346 }
29347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29348 /*
29349 ** This vector defines all the methods that can operate on an
29350 ** sqlite3_file for win32.
29351 */
29352 static const sqlite3_io_methods winIoMethod = {
29353 1, /* iVersion */
29354 winClose,
29355 winRead,
29356 winWrite,
29357 winTruncate,
29358 winSync,
@@ -29360,11 +30445,16 @@
29360 winLock,
29361 winUnlock,
29362 winCheckReservedLock,
29363 winFileControl,
29364 winSectorSize,
29365 winDeviceCharacteristics
 
 
 
 
 
29366 };
29367
29368 /***************************************************************************
29369 ** Here ends the I/O methods that form the sqlite3_io_methods object.
29370 **
@@ -29442,11 +30532,11 @@
29442 sqlite3_randomness(20, &zBuf[j]);
29443 for(i=0; i<20; i++, j++){
29444 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29445 }
29446 zBuf[j] = 0;
29447 OSTRACE2("TEMP FILENAME: %s\n", zBuf);
29448 return SQLITE_OK;
29449 }
29450
29451 /*
29452 ** The return value of getLastErrorMsg
@@ -29533,10 +30623,12 @@
29533 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
29534 char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
29535
29536 assert( id!=0 );
29537 UNUSED_PARAMETER(pVfs);
 
 
29538
29539 /* If the second argument to this function is NULL, generate a
29540 ** temporary file name to use
29541 */
29542 if( !zUtf8Name ){
@@ -29615,10 +30707,13 @@
29615 dwFlagsAndAttributes,
29616 NULL
29617 );
29618 #endif
29619 }
 
 
 
29620 if( h==INVALID_HANDLE_VALUE ){
29621 free(zConverted);
29622 if( flags & SQLITE_OPEN_READWRITE ){
29623 return winOpen(pVfs, zName, id,
29624 ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
@@ -29635,10 +30730,13 @@
29635 }
29636 memset(pFile, 0, sizeof(*pFile));
29637 pFile->pMethod = &winIoMethod;
29638 pFile->h = h;
29639 pFile->lastErrno = NO_ERROR;
 
 
 
29640 pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
29641 #if SQLITE_OS_WINCE
29642 if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
29643 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
29644 && !winceCreateLock(zName, pFile)
@@ -29706,11 +30804,14 @@
29706 && (++cnt < MX_DELETION_ATTEMPTS)
29707 && (Sleep(100), 1) );
29708 #endif
29709 }
29710 free(zConverted);
29711 OSTRACE2("DELETE \"%s\"\n", zFilename);
 
 
 
29712 return ( (rc == INVALID_FILE_ATTRIBUTES)
29713 && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
29714 }
29715
29716 /*
@@ -29999,38 +31100,36 @@
29999 UNUSED_PARAMETER(pVfs);
30000 return ((microsec+999)/1000)*1000;
30001 }
30002
30003 /*
30004 ** The following variable, if set to a non-zero value, becomes the result
30005 ** returned from sqlite3OsCurrentTime(). This is used for testing.
 
30006 */
30007 #ifdef SQLITE_TEST
30008 SQLITE_API int sqlite3_current_time = 0;
30009 #endif
30010
30011 /*
30012 ** Find the current time (in Universal Coordinated Time). Write the
30013 ** current time and date as a Julian Day number into *prNow and
30014 ** return 0. Return 1 if the time and date cannot be found.
 
 
 
 
30015 */
30016 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
30017 FILETIME ft;
30018 /* FILETIME structure is a 64-bit value representing the number of
30019 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
30020 */
30021 sqlite3_int64 timeW; /* Whole days */
30022 sqlite3_int64 timeF; /* Fractional Days */
30023
30024 /* Number of 100-nanosecond intervals in a single day */
30025 static const sqlite3_int64 ntuPerDay =
30026 10000000*(sqlite3_int64)86400;
30027
30028 /* Number of 100-nanosecond intervals in half of a day */
30029 static const sqlite3_int64 ntuPerHalfDay =
30030 10000000*(sqlite3_int64)43200;
30031
30032 /* 2^32 - to avoid use of LL and warnings in gcc */
30033 static const sqlite3_int64 max32BitValue =
30034 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
30035
30036 #if SQLITE_OS_WINCE
@@ -30041,26 +31140,38 @@
30041 return 1;
30042 }
30043 #else
30044 GetSystemTimeAsFileTime( &ft );
30045 #endif
30046 UNUSED_PARAMETER(pVfs);
30047 timeW = (((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + (sqlite3_int64)ft.dwLowDateTime;
30048 timeF = timeW % ntuPerDay; /* fractional days (100-nanoseconds) */
30049 timeW = timeW / ntuPerDay; /* whole days */
30050 timeW = timeW + 2305813; /* add whole days (from 2305813.5) */
30051 timeF = timeF + ntuPerHalfDay; /* add half a day (from 2305813.5) */
30052 timeW = timeW + (timeF/ntuPerDay); /* add whole day if half day made one */
30053 timeF = timeF % ntuPerDay; /* compute new fractional days */
30054 *prNow = (double)timeW + ((double)timeF / (double)ntuPerDay);
30055 #ifdef SQLITE_TEST
30056 if( sqlite3_current_time ){
30057 *prNow = ((double)sqlite3_current_time + (double)43200) / (double)86400 + (double)2440587;
30058 }
30059 #endif
 
30060 return 0;
30061 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30062
30063 /*
30064 ** The idea is that this function works like a combination of
30065 ** GetLastError() and FormatMessage() on windows (or errno and
30066 ** strerror_r() on unix). After an error is returned by an OS
@@ -30092,35 +31203,38 @@
30092 */
30093 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
30094 UNUSED_PARAMETER(pVfs);
30095 return getLastErrorMsg(nBuf, zBuf);
30096 }
 
 
30097
30098 /*
30099 ** Initialize and deinitialize the operating system interface.
30100 */
30101 SQLITE_API int sqlite3_os_init(void){
30102 static sqlite3_vfs winVfs = {
30103 1, /* iVersion */
30104 sizeof(winFile), /* szOsFile */
30105 MAX_PATH, /* mxPathname */
30106 0, /* pNext */
30107 "win32", /* zName */
30108 0, /* pAppData */
30109
30110 winOpen, /* xOpen */
30111 winDelete, /* xDelete */
30112 winAccess, /* xAccess */
30113 winFullPathname, /* xFullPathname */
30114 winDlOpen, /* xDlOpen */
30115 winDlError, /* xDlError */
30116 winDlSym, /* xDlSym */
30117 winDlClose, /* xDlClose */
30118 winRandomness, /* xRandomness */
30119 winSleep, /* xSleep */
30120 winCurrentTime, /* xCurrentTime */
30121 winGetLastError /* xGetLastError */
 
30122 };
30123
30124 sqlite3_vfs_register(&winVfs, 1);
30125 return SQLITE_OK;
30126 }
@@ -30801,19 +31915,21 @@
30801 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
30802 }
30803
30804 if( pPage ){
30805 if( !pPage->pData ){
30806 memset(pPage, 0, sizeof(PgHdr) + pCache->szExtra);
30807 pPage->pExtra = (void*)&pPage[1];
30808 pPage->pData = (void *)&((char *)pPage)[sizeof(PgHdr) + pCache->szExtra];
 
30809 pPage->pCache = pCache;
30810 pPage->pgno = pgno;
30811 }
30812 assert( pPage->pCache==pCache );
30813 assert( pPage->pgno==pgno );
30814 assert( pPage->pExtra==(void *)&pPage[1] );
 
30815
30816 if( 0==pPage->nRef ){
30817 pCache->nRef++;
30818 }
30819 pPage->nRef++;
@@ -31840,11 +32956,11 @@
31840 ** This function is called during initialization (sqlite3_initialize()) to
31841 ** install the default pluggable cache module, assuming the user has not
31842 ** already provided an alternative.
31843 */
31844 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
31845 static sqlite3_pcache_methods defaultMethods = {
31846 0, /* pArg */
31847 pcache1Init, /* xInit */
31848 pcache1Shutdown, /* xShutdown */
31849 pcache1Create, /* xCreate */
31850 pcache1Cachesize, /* xCachesize */
@@ -32353,10 +33469,120 @@
32353 ** locking to prevent two processes from writing the same database
32354 ** file simultaneously, or one process from reading the database while
32355 ** another is writing.
32356 */
32357 #ifndef SQLITE_OMIT_DISKIO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32358
32359 /*
32360 ******************** NOTES ON THE DESIGN OF THE PAGER ************************
32361 **
32362 ** Within this comment block, a page is deemed to have been synced
@@ -32553,10 +33779,11 @@
32553 i64 iOffset; /* Starting offset in main journal */
32554 i64 iHdrOffset; /* See above */
32555 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
32556 Pgno nOrig; /* Original number of pages in file */
32557 Pgno iSubRec; /* Index of first record in sub-journal */
 
32558 };
32559
32560 /*
32561 ** A open page cache is an instance of the following structure.
32562 **
@@ -32730,10 +33957,13 @@
32730 void *pCodec; /* First argument to xCodec... methods */
32731 #endif
32732 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
32733 PCache *pPCache; /* Pointer to page cache object */
32734 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
 
 
 
32735 };
32736
32737 /*
32738 ** The following global variables hold counters used for
32739 ** testing purposes only. These variables do not exist in
@@ -33518,10 +34748,26 @@
33518 }
33519 }
33520 return rc;
33521 }
33522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33523 /*
33524 ** Unlock the database file. This function is a no-op if the pager
33525 ** is in exclusive mode.
33526 **
33527 ** If the pager is currently in error state, discard the contents of
@@ -33530,11 +34776,11 @@
33530 ** on the pager file (by this or any other process), it will be
33531 ** treated as a hot-journal and rolled back.
33532 */
33533 static void pager_unlock(Pager *pPager){
33534 if( !pPager->exclusiveMode ){
33535 int rc; /* Return code */
33536
33537 /* Always close the journal file when dropping the database lock.
33538 ** Otherwise, another connection with journal_mode=delete might
33539 ** delete the file out from under us.
33540 */
@@ -33549,11 +34795,15 @@
33549 ** until the change-counter check fails in PagerSharedLock().
33550 ** Clearing the page size cache here is being conservative.
33551 */
33552 pPager->dbSizeValid = 0;
33553
33554 rc = osUnlock(pPager->fd, NO_LOCK);
 
 
 
 
33555 if( rc ){
33556 pPager->errCode = rc;
33557 }
33558 IOTRACE(("UNLOCK %p\n", pPager))
33559
@@ -33698,10 +34948,11 @@
33698 }
33699 releaseAllSavepoints(pPager);
33700
33701 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
33702 if( isOpen(pPager->jfd) ){
 
33703
33704 /* Finalize the journal file. */
33705 if( sqlite3IsMemJournal(pPager->jfd) ){
33706 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
33707 sqlite3OsClose(pPager->jfd);
@@ -33711,24 +34962,26 @@
33711 }else{
33712 rc = sqlite3OsTruncate(pPager->jfd, 0);
33713 }
33714 pPager->journalOff = 0;
33715 pPager->journalStarted = 0;
33716 }else if( pPager->exclusiveMode
33717 || pPager->journalMode==PAGER_JOURNALMODE_PERSIST
33718 ){
33719 rc = zeroJournalHdr(pPager, hasMaster);
33720 pager_error(pPager, rc);
33721 pPager->journalOff = 0;
33722 pPager->journalStarted = 0;
33723 }else{
33724 /* This branch may be executed with Pager.journalMode==MEMORY if
33725 ** a hot-journal was just rolled back. In this case the journal
33726 ** file should be closed and deleted. If this connection writes to
33727 ** the database file, it will do so using an in-memory journal. */
 
33728 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
33729 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
 
33730 );
33731 sqlite3OsClose(pPager->jfd);
33732 if( !pPager->tempFile ){
33733 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
33734 }
@@ -33741,11 +34994,22 @@
33741 sqlite3BitvecDestroy(pPager->pInJournal);
33742 pPager->pInJournal = 0;
33743 pPager->nRec = 0;
33744 sqlite3PcacheCleanAll(pPager->pPCache);
33745
33746 if( !pPager->exclusiveMode ){
 
 
 
 
 
 
 
 
 
 
 
33747 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
33748 pPager->state = PAGER_SHARED;
33749 pPager->changeCountDone = 0;
33750 }else if( pPager->state==PAGER_SYNCED ){
33751 pPager->state = PAGER_EXCLUSIVE;
@@ -33852,10 +35116,11 @@
33852 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
33853 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
33854
33855 aData = pPager->pTmpSpace;
33856 assert( aData ); /* Temp storage must have already been allocated */
 
33857
33858 /* Read the page number and page data from the journal or sub-journal
33859 ** file. Return an error code to the caller if an IO error occurs.
33860 */
33861 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -33921,11 +35186,15 @@
33921 **
33922 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
33923 ** is possible to fail a statement on a database that does not yet exist.
33924 ** Do not attempt to write if database file has never been opened.
33925 */
33926 pPg = pager_lookup(pPager, pgno);
 
 
 
 
33927 assert( pPg || !MEMDB );
33928 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
33929 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
33930 (isMainJrnl?"main-journal":"sub-journal")
33931 ));
@@ -33938,10 +35207,11 @@
33938 && isOpen(pPager->fd)
33939 && isSynced
33940 ){
33941 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
33942 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
 
33943 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
33944 if( pgno>pPager->dbFileSize ){
33945 pPager->dbFileSize = pgno;
33946 }
33947 if( pPager->pBackup ){
@@ -34002,10 +35272,11 @@
34002 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
34003 ** be written out into the database file before its journal file
34004 ** segment is synced. If a crash occurs during or following this,
34005 ** database corruption may ensue.
34006 */
 
34007 sqlite3PcacheMakeClean(pPg);
34008 }
34009 #ifdef SQLITE_CHECK_PAGES
34010 pPg->pageHash = pager_pagehash(pPg);
34011 #endif
@@ -34453,10 +35724,13 @@
34453 testcase( rc!=SQLITE_OK );
34454 }
34455 if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
34456 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
34457 }
 
 
 
34458 if( rc==SQLITE_OK ){
34459 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
34460 testcase( rc!=SQLITE_OK );
34461 }
34462 if( rc==SQLITE_OK && zMaster[0] && res ){
@@ -34473,10 +35747,269 @@
34473 */
34474 setSectorSize(pPager);
34475 return rc;
34476 }
34477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34478 /*
34479 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
34480 ** the entire master journal file. The case pSavepoint==NULL occurs when
34481 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
34482 ** savepoint.
@@ -34529,26 +36062,31 @@
34529
34530 /* Set the database size back to the value it was before the savepoint
34531 ** being reverted was opened.
34532 */
34533 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
 
 
 
 
34534
34535 /* Use pPager->journalOff as the effective size of the main rollback
34536 ** journal. The actual file might be larger than this in
34537 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
34538 ** past pPager->journalOff is off-limits to us.
34539 */
34540 szJ = pPager->journalOff;
 
34541
34542 /* Begin by rolling back records from the main journal starting at
34543 ** PagerSavepoint.iOffset and continuing to the next journal header.
34544 ** There might be records in the main journal that have a page number
34545 ** greater than the current database size (pPager->dbSize) but those
34546 ** will be skipped automatically. Pages are added to pDone as they
34547 ** are played back.
34548 */
34549 if( pSavepoint ){
34550 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
34551 pPager->journalOff = pSavepoint->iOffset;
34552 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
34553 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
34554 }
@@ -34591,10 +36129,14 @@
34591 ** will be skipped. Out-of-range pages are also skipped.
34592 */
34593 if( pSavepoint ){
34594 u32 ii; /* Loop counter */
34595 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
 
 
 
 
34596 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
34597 assert( offset==ii*(4+pPager->pageSize) );
34598 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
34599 }
34600 assert( rc!=SQLITE_DONE );
@@ -34602,10 +36144,11 @@
34602
34603 sqlite3BitvecDestroy(pDone);
34604 if( rc==SQLITE_OK ){
34605 pPager->journalOff = szJ;
34606 }
 
34607 return rc;
34608 }
34609
34610 /*
34611 ** Change the maximum number of in-memory pages that are allowed.
@@ -34818,12 +36361,14 @@
34818 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
34819 int nPage;
34820 if( mxPage>0 ){
34821 pPager->mxPgno = mxPage;
34822 }
34823 sqlite3PagerPagecount(pPager, &nPage);
34824 assert( pPager->mxPgno>=nPage );
 
 
34825 return pPager->mxPgno;
34826 }
34827
34828 /*
34829 ** The following set of routines are used to disable the simulated
@@ -34865,10 +36410,26 @@
34865 */
34866 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
34867 int rc = SQLITE_OK;
34868 memset(pDest, 0, N);
34869 assert( isOpen(pPager->fd) || pPager->tempFile );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34870 if( isOpen(pPager->fd) ){
34871 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
34872 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
34873 if( rc==SQLITE_IOERR_SHORT_READ ){
34874 rc = SQLITE_OK;
@@ -34891,28 +36452,36 @@
34891 **
34892 ** Otherwise, if everything is successful, then SQLITE_OK is returned
34893 ** and *pnPage is set to the number of pages in the database.
34894 */
34895 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
34896 Pgno nPage; /* Value to return via *pnPage */
34897
34898 /* Determine the number of pages in the file. Store this in nPage. */
34899 if( pPager->dbSizeValid ){
34900 nPage = pPager->dbSize;
34901 }else{
34902 int rc; /* Error returned by OsFileSize() */
34903 i64 n = 0; /* File size in bytes returned by OsFileSize() */
34904
34905 assert( isOpen(pPager->fd) || pPager->tempFile );
34906 if( isOpen(pPager->fd) && (0 != (rc = sqlite3OsFileSize(pPager->fd, &n))) ){
34907 pager_error(pPager, rc);
34908 return rc;
34909 }
34910 if( n>0 && n<pPager->pageSize ){
34911 nPage = 1;
34912 }else{
34913 nPage = (Pgno)(n / pPager->pageSize);
 
 
 
 
 
 
 
 
34914 }
34915 if( pPager->state!=PAGER_UNLOCK ){
34916 pPager->dbSize = nPage;
34917 pPager->dbFileSize = nPage;
34918 pPager->dbSizeValid = 1;
@@ -35030,10 +36599,11 @@
35030 assert( pPager->dbSize>=nPage );
35031 assert( pPager->state>=PAGER_RESERVED );
35032 pPager->dbSize = nPage;
35033 assertTruncateConstraint(pPager);
35034 }
 
35035
35036 /*
35037 ** This function is called before attempting a hot-journal rollback. It
35038 ** syncs the journal file to disk, then sets pPager->journalHdr to the
35039 ** size of the journal file so that the pager_playback() routine knows
@@ -35071,14 +36641,23 @@
35071 ** is made to roll it back. If an error occurs during the rollback
35072 ** a hot journal may be left in the filesystem but no error is returned
35073 ** to the caller.
35074 */
35075 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
 
 
35076 disable_simulated_io_errors();
35077 sqlite3BeginBenignMalloc();
35078 pPager->errCode = 0;
35079 pPager->exclusiveMode = 0;
 
 
 
 
 
 
 
35080 pager_reset(pPager);
35081 if( MEMDB ){
35082 pager_unlock(pPager);
35083 }else{
35084 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
@@ -35095,11 +36674,11 @@
35095 sqlite3EndBenignMalloc();
35096 enable_simulated_io_errors();
35097 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
35098 IOTRACE(("CLOSE %p\n", pPager))
35099 sqlite3OsClose(pPager->fd);
35100 sqlite3PageFree(pPager->pTmpSpace);
35101 sqlite3PcacheClose(pPager->pPCache);
35102
35103 #ifdef SQLITE_HAS_CODEC
35104 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
35105 #endif
@@ -35196,14 +36775,14 @@
35196 ** as a temporary buffer to inspect the first couple of bytes of
35197 ** the potential journal header.
35198 */
35199 i64 iNextHdrOffset;
35200 u8 aMagic[8];
35201 u8 zHeader[sizeof(aJournalMagic)+4];
35202
35203 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
35204 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
35205
35206 iNextHdrOffset = journalHdrOffset(pPager);
35207 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
35208 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
35209 static const u8 zerobyte = 0;
@@ -35231,11 +36810,11 @@
35231 if( rc!=SQLITE_OK ) return rc;
35232 }
35233 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
35234 rc = sqlite3OsWrite(
35235 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
35236 );
35237 if( rc!=SQLITE_OK ) return rc;
35238 }
35239 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
35240 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
35241 IOTRACE(("JSYNC %p\n", pPager))
@@ -35311,10 +36890,11 @@
35311 ** is unchanged and we can rollback without having to playback the
35312 ** journal into the original database file. Once we transition to
35313 ** EXCLUSIVE, it means the database file has been changed and any rollback
35314 ** will require a journal playback.
35315 */
 
35316 assert( pPager->state>=PAGER_RESERVED );
35317 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
35318
35319 /* If the file is a temp-file has not yet been opened, open it now. It
35320 ** is not possible for rc to be other than SQLITE_OK if this branch
@@ -35322,10 +36902,18 @@
35322 */
35323 if( !isOpen(pPager->fd) ){
35324 assert( pPager->tempFile && rc==SQLITE_OK );
35325 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
35326 }
 
 
 
 
 
 
 
 
35327
35328 while( rc==SQLITE_OK && pList ){
35329 Pgno pgno = pList->pgno;
35330
35331 /* If there are dirty pages in the page cache with page numbers greater
@@ -35374,10 +36962,30 @@
35374 pList = pList->pDirty;
35375 }
35376
35377 return rc;
35378 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35379
35380 /*
35381 ** Append a record of the current state of page pPg to the sub-journal.
35382 ** It is the callers responsibility to use subjRequiresPage() to check
35383 ** that it is really required before calling this function.
@@ -35391,32 +36999,44 @@
35391 ** bitvec.
35392 */
35393 static int subjournalPage(PgHdr *pPg){
35394 int rc = SQLITE_OK;
35395 Pager *pPager = pPg->pPager;
35396 if( isOpen(pPager->sjfd) ){
35397 void *pData = pPg->pData;
35398 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
35399 char *pData2;
35400
35401 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
35402 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
35403
35404 assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
35405 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
 
 
 
 
35406 if( rc==SQLITE_OK ){
35407 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
 
 
 
 
 
 
 
 
 
35408 }
35409 }
35410 if( rc==SQLITE_OK ){
35411 pPager->nSubRec++;
35412 assert( pPager->nSavepoint>0 );
35413 rc = addToSavepointBitvecs(pPager, pPg->pgno);
35414 }
35415 return rc;
35416 }
35417
35418
35419 /*
35420 ** This function is called by the pcache layer when it has reached some
35421 ** soft memory limit. The first argument is a pointer to a Pager object
35422 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
@@ -35440,78 +37060,88 @@
35440 int rc = SQLITE_OK;
35441
35442 assert( pPg->pPager==pPager );
35443 assert( pPg->flags&PGHDR_DIRTY );
35444
35445 /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
35446 ** is journalling a set of two or more database pages that are stored
35447 ** on the same disk sector. Syncing the journal is not allowed while
35448 ** this is happening as it is important that all members of such a
35449 ** set of pages are synced to disk together. So, if the page this function
35450 ** is trying to make clean will require a journal sync and the doNotSync
35451 ** flag is set, return without doing anything. The pcache layer will
35452 ** just have to go ahead and allocate a new page buffer instead of
35453 ** reusing pPg.
35454 **
35455 ** Similarly, if the pager has already entered the error state, do not
35456 ** try to write the contents of pPg to disk.
35457 */
35458 if( NEVER(pPager->errCode)
35459 || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
35460 ){
35461 return SQLITE_OK;
35462 }
35463
35464 /* Sync the journal file if required. */
35465 if( pPg->flags&PGHDR_NEED_SYNC ){
35466 rc = syncJournal(pPager);
35467 if( rc==SQLITE_OK && pPager->fullSync &&
35468 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
35469 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
35470 ){
35471 pPager->nRec = 0;
35472 rc = writeJournalHdr(pPager);
35473 }
35474 }
35475
35476 /* If the page number of this page is larger than the current size of
35477 ** the database image, it may need to be written to the sub-journal.
35478 ** This is because the call to pager_write_pagelist() below will not
35479 ** actually write data to the file in this case.
35480 **
35481 ** Consider the following sequence of events:
35482 **
35483 ** BEGIN;
35484 ** <journal page X>
35485 ** <modify page X>
35486 ** SAVEPOINT sp;
35487 ** <shrink database file to Y pages>
35488 ** pagerStress(page X)
35489 ** ROLLBACK TO sp;
35490 **
35491 ** If (X>Y), then when pagerStress is called page X will not be written
35492 ** out to the database file, but will be dropped from the cache. Then,
35493 ** following the "ROLLBACK TO sp" statement, reading page X will read
35494 ** data from the database file. This will be the copy of page X as it
35495 ** was when the transaction started, not as it was when "SAVEPOINT sp"
35496 ** was executed.
35497 **
35498 ** The solution is to write the current data for page X into the
35499 ** sub-journal file now (if it is not already there), so that it will
35500 ** be restored to its current value when the "ROLLBACK TO sp" is
35501 ** executed.
35502 */
35503 if( NEVER(
35504 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
35505 ) ){
35506 rc = subjournalPage(pPg);
35507 }
35508
35509 /* Write the contents of the page out to the database file. */
35510 if( rc==SQLITE_OK ){
35511 pPg->pDirty = 0;
35512 rc = pager_write_pagelist(pPg);
 
 
 
 
 
 
 
 
 
 
35513 }
35514
35515 /* Mark the page as clean. */
35516 if( rc==SQLITE_OK ){
35517 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
@@ -35915,71 +37545,10 @@
35915 }
35916
35917 return rc;
35918 }
35919
35920 /*
35921 ** Read the content for page pPg out of the database file and into
35922 ** pPg->pData. A shared lock or greater must be held on the database
35923 ** file before this function is called.
35924 **
35925 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
35926 ** the value read from the database file.
35927 **
35928 ** If an IO error occurs, then the IO error is returned to the caller.
35929 ** Otherwise, SQLITE_OK is returned.
35930 */
35931 static int readDbPage(PgHdr *pPg){
35932 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
35933 Pgno pgno = pPg->pgno; /* Page number to read */
35934 int rc; /* Return code */
35935 i64 iOffset; /* Byte offset of file to read from */
35936
35937 assert( pPager->state>=PAGER_SHARED && !MEMDB );
35938 assert( isOpen(pPager->fd) );
35939
35940 if( NEVER(!isOpen(pPager->fd)) ){
35941 assert( pPager->tempFile );
35942 memset(pPg->pData, 0, pPager->pageSize);
35943 return SQLITE_OK;
35944 }
35945 iOffset = (pgno-1)*(i64)pPager->pageSize;
35946 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
35947 if( rc==SQLITE_IOERR_SHORT_READ ){
35948 rc = SQLITE_OK;
35949 }
35950 if( pgno==1 ){
35951 if( rc ){
35952 /* If the read is unsuccessful, set the dbFileVers[] to something
35953 ** that will never be a valid file version. dbFileVers[] is a copy
35954 ** of bytes 24..39 of the database. Bytes 28..31 should always be
35955 ** zero. Bytes 32..35 and 35..39 should be page numbers which are
35956 ** never 0xffffffff. So filling pPager->dbFileVers[] with all 0xff
35957 ** bytes should suffice.
35958 **
35959 ** For an encrypted database, the situation is more complex: bytes
35960 ** 24..39 of the database are white noise. But the probability of
35961 ** white noising equaling 16 bytes of 0xff is vanishingly small so
35962 ** we should still be ok.
35963 */
35964 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
35965 }else{
35966 u8 *dbFileVers = &((u8*)pPg->pData)[24];
35967 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
35968 }
35969 }
35970 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
35971
35972 PAGER_INCR(sqlite3_pager_readdb_count);
35973 PAGER_INCR(pPager->nRead);
35974 IOTRACE(("PGIN %p %d\n", pPager, pgno));
35975 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
35976 PAGERID(pPager), pgno, pager_pagehash(pPg)));
35977
35978 return rc;
35979 }
35980
35981 /*
35982 ** This function is called to obtain a shared lock on the database file.
35983 ** It is illegal to call sqlite3PagerAcquire() until after this function
35984 ** has been successfully called. If a shared-lock is already held when
35985 ** this function is called, it is a no-op.
@@ -36029,11 +37598,13 @@
36029 }
36030 pPager->errCode = SQLITE_OK;
36031 pager_reset(pPager);
36032 }
36033
36034 if( pPager->state==PAGER_UNLOCK || isErrorReset ){
 
 
36035 sqlite3_vfs * const pVfs = pPager->pVfs;
36036 int isHotJournal = 0;
36037 assert( !MEMDB );
36038 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
36039 if( pPager->noReadlock ){
@@ -36118,11 +37689,11 @@
36118 ** rollback journal at this time. */
36119 pPager->journalStarted = 0;
36120 pPager->journalOff = 0;
36121 pPager->setMaster = 0;
36122 pPager->journalHdr = 0;
36123
36124 /* Make sure the journal file has been synced to disk. */
36125
36126 /* Playback and delete the journal. Drop the database write
36127 ** lock and reacquire the read lock. Purge the cache before
36128 ** playing back the hot-journal so that we don't end up with
@@ -36185,10 +37756,15 @@
36185 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
36186 pager_reset(pPager);
36187 }
36188 }
36189 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
 
 
 
 
 
36190 }
36191
36192 failed:
36193 if( rc!=SQLITE_OK ){
36194 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
@@ -36325,12 +37901,12 @@
36325 goto pager_acquire_err;
36326 }
36327
36328 if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
36329 if( pgno>pPager->mxPgno ){
36330 rc = SQLITE_FULL;
36331 goto pager_acquire_err;
36332 }
36333 if( noContent ){
36334 /* Failure to set the bits in the InJournal bit-vectors is benign.
36335 ** It merely means that we might do some extra work to journal a
36336 ** page that does not need to be journaled. Nevertheless, be sure
@@ -36410,31 +37986,10 @@
36410 sqlite3PcacheRelease(pPg);
36411 pagerUnlockIfUnused(pPager);
36412 }
36413 }
36414
36415 /*
36416 ** If the main journal file has already been opened, ensure that the
36417 ** sub-journal file is open too. If the main journal is not open,
36418 ** this function is a no-op.
36419 **
36420 ** SQLITE_OK is returned if everything goes according to plan.
36421 ** An SQLITE_IOERR_XXX error code is returned if a call to
36422 ** sqlite3OsOpen() fails.
36423 */
36424 static int openSubJournal(Pager *pPager){
36425 int rc = SQLITE_OK;
36426 if( isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ){
36427 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
36428 sqlite3MemJournalOpen(pPager->sjfd);
36429 }else{
36430 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
36431 }
36432 }
36433 return rc;
36434 }
36435
36436 /*
36437 ** This function is called at the start of every write transaction.
36438 ** There must already be a RESERVED or EXCLUSIVE lock on the database
36439 ** file when this routine is called.
36440 **
@@ -36513,13 +38068,10 @@
36513 pPager->journalOff = 0;
36514 pPager->setMaster = 0;
36515 pPager->journalHdr = 0;
36516 rc = writeJournalHdr(pPager);
36517 }
36518 if( rc==SQLITE_OK && pPager->nSavepoint ){
36519 rc = openSubJournal(pPager);
36520 }
36521
36522 if( rc!=SQLITE_OK ){
36523 sqlite3BitvecDestroy(pPager->pInJournal);
36524 pPager->pInJournal = 0;
36525 }
@@ -36553,24 +38105,60 @@
36553 */
36554 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
36555 int rc = SQLITE_OK;
36556 assert( pPager->state!=PAGER_UNLOCK );
36557 pPager->subjInMemory = (u8)subjInMemory;
 
36558 if( pPager->state==PAGER_SHARED ){
36559 assert( pPager->pInJournal==0 );
36560 assert( !MEMDB && !pPager->tempFile );
36561
36562 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
36563 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
36564 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
36565 ** lock, but not when obtaining the RESERVED lock.
36566 */
36567 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
36568 if( rc==SQLITE_OK ){
36569 pPager->state = PAGER_RESERVED;
36570 if( exFlag ){
36571 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36572 }
36573 }
36574
36575 /* No need to open the journal file at this time. It will be
36576 ** opened before it is written to. If we defer opening the journal,
@@ -36582,10 +38170,11 @@
36582 ** time a (read or write) transaction was successfully concluded
36583 ** by this connection. Instead of deleting the journal file it was
36584 ** kept open and either was truncated to 0 bytes or its header was
36585 ** overwritten with zeros.
36586 */
 
36587 assert( pPager->nRec==0 );
36588 assert( pPager->dbOrigSize==0 );
36589 assert( pPager->pInJournal==0 );
36590 rc = pager_open_journal(pPager);
36591 }
@@ -36636,10 +38225,11 @@
36636 /* Mark the page as dirty. If the page has already been written
36637 ** to the journal then we can return right away.
36638 */
36639 sqlite3PcacheMakeDirty(pPg);
36640 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
 
36641 pPager->dbModified = 1;
36642 }else{
36643
36644 /* If we get this far, it means that the page needs to be
36645 ** written to the transaction journal or the ckeckpoint journal
@@ -36651,11 +38241,14 @@
36651 */
36652 rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
36653 if( rc!=SQLITE_OK ){
36654 return rc;
36655 }
36656 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
 
 
 
36657 assert( pPager->useJournal );
36658 rc = pager_open_journal(pPager);
36659 if( rc!=SQLITE_OK ) return rc;
36660 }
36661 pPager->dbModified = 1;
@@ -36663,10 +38256,11 @@
36663 /* The transaction journal now exists and we have a RESERVED or an
36664 ** EXCLUSIVE lock on the main database file. Write the current page to
36665 ** the transaction journal if it is not there already.
36666 */
36667 if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
 
36668 if( pPg->pgno<=pPager->dbOrigSize ){
36669 u32 cksum;
36670 char *pData2;
36671
36672 /* We should never write to the journal file the page that
@@ -36954,11 +38548,14 @@
36954 /* Increment the value just read and write it back to byte 24. */
36955 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
36956 change_counter++;
36957 put32bits(((char*)pPgHdr->pData)+24, change_counter);
36958
36959 /* Also store the SQLite version number in bytes 96..99 */
 
 
 
36960 put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
36961
36962 /* If running in direct mode, write the contents of page 1 to the file. */
36963 if( DIRECT_MODE ){
36964 const void *zBuf = pPgHdr->pData;
@@ -37043,132 +38640,143 @@
37043 ** function has already been called, it is mostly a no-op. However, any
37044 ** backup in progress needs to be restarted.
37045 */
37046 sqlite3BackupRestart(pPager->pBackup);
37047 }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){
37048
37049 /* The following block updates the change-counter. Exactly how it
37050 ** does this depends on whether or not the atomic-update optimization
37051 ** was enabled at compile time, and if this transaction meets the
37052 ** runtime criteria to use the operation:
37053 **
37054 ** * The file-system supports the atomic-write property for
37055 ** blocks of size page-size, and
37056 ** * This commit is not part of a multi-file transaction, and
37057 ** * Exactly one page has been modified and store in the journal file.
37058 **
37059 ** If the optimization was not enabled at compile time, then the
37060 ** pager_incr_changecounter() function is called to update the change
37061 ** counter in 'indirect-mode'. If the optimization is compiled in but
37062 ** is not applicable to this transaction, call sqlite3JournalCreate()
37063 ** to make sure the journal file has actually been created, then call
37064 ** pager_incr_changecounter() to update the change-counter in indirect
37065 ** mode.
37066 **
37067 ** Otherwise, if the optimization is both enabled and applicable,
37068 ** then call pager_incr_changecounter() to update the change-counter
37069 ** in 'direct' mode. In this case the journal file will never be
37070 ** created for this transaction.
37071 */
37072 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
37073 PgHdr *pPg;
37074 assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
37075 if( !zMaster && isOpen(pPager->jfd)
37076 && pPager->journalOff==jrnlBufferSize(pPager)
37077 && pPager->dbSize>=pPager->dbFileSize
37078 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
37079 ){
37080 /* Update the db file change counter via the direct-write method. The
37081 ** following call will modify the in-memory representation of page 1
37082 ** to include the updated change counter and then write page 1
37083 ** directly to the database file. Because of the atomic-write
37084 ** property of the host file-system, this is safe.
37085 */
37086 rc = pager_incr_changecounter(pPager, 1);
37087 }else{
37088 rc = sqlite3JournalCreate(pPager->jfd);
37089 if( rc==SQLITE_OK ){
37090 rc = pager_incr_changecounter(pPager, 0);
37091 }
37092 }
37093 #else
37094 rc = pager_incr_changecounter(pPager, 0);
37095 #endif
37096 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37097
37098 /* If this transaction has made the database smaller, then all pages
37099 ** being discarded by the truncation must be written to the journal
37100 ** file. This can only happen in auto-vacuum mode.
37101 **
37102 ** Before reading the pages with page numbers larger than the
37103 ** current value of Pager.dbSize, set dbSize back to the value
37104 ** that it took at the start of the transaction. Otherwise, the
37105 ** calls to sqlite3PagerGet() return zeroed pages instead of
37106 ** reading data from the database file.
37107 **
37108 ** When journal_mode==OFF the dbOrigSize is always zero, so this
37109 ** block never runs if journal_mode=OFF.
37110 */
37111 #ifndef SQLITE_OMIT_AUTOVACUUM
37112 if( pPager->dbSize<pPager->dbOrigSize
37113 && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
37114 ){
37115 Pgno i; /* Iterator variable */
37116 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
37117 const Pgno dbSize = pPager->dbSize; /* Database image size */
37118 pPager->dbSize = pPager->dbOrigSize;
37119 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
37120 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
37121 PgHdr *pPage; /* Page to journal */
37122 rc = sqlite3PagerGet(pPager, i, &pPage);
37123 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37124 rc = sqlite3PagerWrite(pPage);
37125 sqlite3PagerUnref(pPage);
37126 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37127 }
37128 }
37129 pPager->dbSize = dbSize;
37130 }
37131 #endif
37132
37133 /* Write the master journal name into the journal file. If a master
37134 ** journal file name has already been written to the journal file,
37135 ** or if zMaster is NULL (no master journal), then this call is a no-op.
37136 */
37137 rc = writeMasterJournal(pPager, zMaster);
37138 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37139
37140 /* Sync the journal file. If the atomic-update optimization is being
37141 ** used, this call will not create the journal file or perform any
37142 ** real IO.
37143 */
37144 rc = syncJournal(pPager);
37145 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37146
37147 /* Write all dirty pages to the database file. */
37148 rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
37149 if( rc!=SQLITE_OK ){
37150 assert( rc!=SQLITE_IOERR_BLOCKED );
37151 goto commit_phase_one_exit;
37152 }
37153 sqlite3PcacheCleanAll(pPager->pPCache);
37154
37155 /* If the file on disk is not the same size as the database image,
37156 ** then use pager_truncate to grow or shrink the file here.
37157 */
37158 if( pPager->dbSize!=pPager->dbFileSize ){
37159 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
37160 assert( pPager->state>=PAGER_EXCLUSIVE );
37161 rc = pager_truncate(pPager, nNew);
37162 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
37163 }
37164
37165 /* Finally, sync the database file. */
37166 if( !pPager->noSync && !noSync ){
37167 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
37168 }
37169 IOTRACE(("DBSYNC %p\n", pPager))
 
 
 
 
 
 
 
 
 
 
 
37170
37171 pPager->state = PAGER_SYNCED;
37172 }
37173
37174 commit_phase_one_exit:
@@ -37273,11 +38881,18 @@
37273 ** hot-journal rollback).
37274 */
37275 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
37276 int rc = SQLITE_OK; /* Return code */
37277 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
37278 if( !pPager->dbModified || !isOpen(pPager->jfd) ){
 
 
 
 
 
 
 
37279 rc = pager_end_transaction(pPager, pPager->setMaster);
37280 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
37281 if( pPager->state>=PAGER_EXCLUSIVE ){
37282 pager_playback(pPager, 0);
37283 }
@@ -37399,11 +39014,10 @@
37399 if( !aNew ){
37400 return SQLITE_NOMEM;
37401 }
37402 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
37403 pPager->aSavepoint = aNew;
37404 pPager->nSavepoint = nSavepoint;
37405
37406 /* Populate the PagerSavepoint structures just allocated. */
37407 for(ii=nCurrent; ii<nSavepoint; ii++){
37408 aNew[ii].nOrig = nPage;
37409 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
@@ -37414,14 +39028,16 @@
37414 aNew[ii].iSubRec = pPager->nSubRec;
37415 aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage);
37416 if( !aNew[ii].pInSavepoint ){
37417 return SQLITE_NOMEM;
37418 }
 
 
 
 
37419 }
37420
37421 /* Open the sub-journal, if it is not already opened. */
37422 rc = openSubJournal(pPager);
37423 assertTruncateConstraint(pPager);
37424 }
37425
37426 return rc;
37427 }
@@ -37491,11 +39107,11 @@
37491 /* Else this is a rollback operation, playback the specified savepoint.
37492 ** If this is a temp-file, it is possible that the journal file has
37493 ** not yet been opened. In this case there have been no changes to
37494 ** the database file, so the playback operation can be skipped.
37495 */
37496 else if( isOpen(pPager->jfd) ){
37497 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
37498 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
37499 assert(rc!=SQLITE_DONE);
37500 }
37501
@@ -37760,60 +39376,106 @@
37760 }
37761 return (int)pPager->exclusiveMode;
37762 }
37763
37764 /*
37765 ** Get/set the journal-mode for this pager. Parameter eMode must be one of:
37766 **
37767 ** PAGER_JOURNALMODE_QUERY
37768 ** PAGER_JOURNALMODE_DELETE
37769 ** PAGER_JOURNALMODE_TRUNCATE
37770 ** PAGER_JOURNALMODE_PERSIST
37771 ** PAGER_JOURNALMODE_OFF
37772 ** PAGER_JOURNALMODE_MEMORY
 
37773 **
37774 ** If the parameter is not _QUERY, then the journal_mode is set to the
37775 ** value specified if the change is allowed. The change is disallowed
37776 ** for the following reasons:
37777 **
37778 ** * An in-memory database can only have its journal_mode set to _OFF
37779 ** or _MEMORY.
37780 **
37781 ** * The journal mode may not be changed while a transaction is active.
37782 **
37783 ** The returned indicate the current (possibly updated) journal-mode.
37784 */
37785 SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *pPager, int eMode){
37786 assert( eMode==PAGER_JOURNALMODE_QUERY
37787 || eMode==PAGER_JOURNALMODE_DELETE
 
 
37788 || eMode==PAGER_JOURNALMODE_TRUNCATE
37789 || eMode==PAGER_JOURNALMODE_PERSIST
37790 || eMode==PAGER_JOURNALMODE_OFF
 
37791 || eMode==PAGER_JOURNALMODE_MEMORY );
37792 assert( PAGER_JOURNALMODE_QUERY<0 );
37793 if( eMode>=0
37794 && (!MEMDB || eMode==PAGER_JOURNALMODE_MEMORY
37795 || eMode==PAGER_JOURNALMODE_OFF)
37796 && !pPager->dbModified
37797 && (!isOpen(pPager->jfd) || 0==pPager->journalOff)
37798 ){
37799 if( isOpen(pPager->jfd) ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37800 sqlite3OsClose(pPager->jfd);
37801 }
37802 assert( (PAGER_JOURNALMODE_TRUNCATE & 1)==1 );
37803 assert( (PAGER_JOURNALMODE_PERSIST & 1)==1 );
37804 assert( (PAGER_JOURNALMODE_DELETE & 1)==0 );
37805 assert( (PAGER_JOURNALMODE_MEMORY & 1)==0 );
37806 assert( (PAGER_JOURNALMODE_OFF & 1)==0 );
37807 if( (pPager->journalMode & 1)==1 && (eMode & 1)==0
37808 && !pPager->exclusiveMode ){
 
 
 
 
 
 
 
 
37809 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
37810 }
37811 pPager->journalMode = (u8)eMode;
37812 }
 
 
 
 
 
 
 
 
 
37813 return (int)pPager->journalMode;
37814 }
 
 
 
 
 
 
 
 
 
 
 
37815
37816 /*
37817 ** Get/set the size-limit used for persistent journal files.
37818 **
37819 ** Setting the size limit to -1 means no limit is enforced.
@@ -37833,14 +39495,2621 @@
37833 ** sqlite3BackupUpdate() only.
37834 */
37835 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
37836 return &pPager->pBackup;
37837 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37838
37839 #endif /* SQLITE_OMIT_DISKIO */
37840
37841 /************** End of pager.c ***********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37842 /************** Begin file btmutex.c *****************************************/
37843 /*
37844 ** 2007 August 27
37845 **
37846 ** The author disclaims copyright to this source code. In place of
@@ -38278,10 +42547,11 @@
38278 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
38279 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
38280 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
38281 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
38282 u8 inTransaction; /* Transaction state */
 
38283 int nTransaction; /* Number of open transactions (read + write) */
38284 u32 nPage; /* Number of pages in the database */
38285 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
38286 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
38287 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -41094,11 +45364,11 @@
41094 */
41095 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
41096 if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){;
41097 goto page1_init_failed;
41098 }
41099 if( nPage==0 ){
41100 nPage = nPageFile;
41101 }
41102 if( nPage>0 ){
41103 int pageSize;
41104 int usableSize;
@@ -41105,16 +45375,46 @@
41105 u8 *page1 = pPage1->aData;
41106 rc = SQLITE_NOTADB;
41107 if( memcmp(page1, zMagicHeader, 16)!=0 ){
41108 goto page1_init_failed;
41109 }
 
 
41110 if( page1[18]>1 ){
41111 pBt->readOnly = 1;
41112 }
41113 if( page1[19]>1 ){
41114 goto page1_init_failed;
41115 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41116
41117 /* The maximum embedded fraction must be exactly 25%. And the minimum
41118 ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
41119 ** The original design allowed these amounts to vary, but as of
41120 ** version 3.6.0, we require them to be fixed.
@@ -41360,11 +45660,11 @@
41360 }
41361
41362 if( rc!=SQLITE_OK ){
41363 unlockBtreeIfUnused(pBt);
41364 }
41365 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
41366 btreeInvokeBusyHandler(pBt) );
41367
41368 if( rc==SQLITE_OK ){
41369 if( p->inTrans==TRANS_NONE ){
41370 pBt->nTransaction++;
@@ -46807,10 +51107,46 @@
46807 assert(!pCur->isIncrblobHandle);
46808 assert(!pCur->aOverflow);
46809 pCur->isIncrblobHandle = 1;
46810 }
46811 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46812
46813 /************** End of btree.c ***********************************************/
46814 /************** Begin file backup.c ******************************************/
46815 /*
46816 ** 2009 January 28
@@ -47027,11 +51363,11 @@
47027 assert( zSrcData );
47028
47029 /* Catch the case where the destination is an in-memory database and the
47030 ** page sizes of the source and destination differ.
47031 */
47032 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(sqlite3BtreePager(p->pDest)) ){
47033 rc = SQLITE_READONLY;
47034 }
47035
47036 /* This loop runs once for each destination page spanned by the source
47037 ** page. For each iteration, variable iOff is set to the byte offset
@@ -47097,10 +51433,13 @@
47097 /*
47098 ** Copy nPage pages from the source b-tree to the destination.
47099 */
47100 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
47101 int rc;
 
 
 
47102
47103 sqlite3_mutex_enter(p->pSrcDb->mutex);
47104 sqlite3BtreeEnter(p->pSrc);
47105 if( p->pDestDb ){
47106 sqlite3_mutex_enter(p->pDestDb->mutex);
@@ -47137,10 +51476,19 @@
47137 */
47138 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
47139 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
47140 bCloseTrans = 1;
47141 }
 
 
 
 
 
 
 
 
 
47142
47143 /* Now that there is a read-lock on the source database, query the
47144 ** source pager for the number of pages in the database.
47145 */
47146 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
@@ -47173,12 +51521,10 @@
47173 ** same schema version.
47174 */
47175 if( rc==SQLITE_DONE
47176 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
47177 ){
47178 const int nSrcPagesize = sqlite3BtreeGetPageSize(p->pSrc);
47179 const int nDestPagesize = sqlite3BtreeGetPageSize(p->pDest);
47180 int nDestTruncate;
47181
47182 if( p->pDestDb ){
47183 sqlite3ResetInternalSchema(p->pDestDb, 0);
47184 }
@@ -47193,56 +51539,58 @@
47193 ** sqlite3PagerTruncateImage() here so that any pages in the
47194 ** destination file that lie beyond the nDestTruncate page mark are
47195 ** journalled by PagerCommitPhaseOne() before they are destroyed
47196 ** by the file truncation.
47197 */
47198 if( nSrcPagesize<nDestPagesize ){
47199 int ratio = nDestPagesize/nSrcPagesize;
 
 
47200 nDestTruncate = (nSrcPage+ratio-1)/ratio;
47201 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
47202 nDestTruncate--;
47203 }
47204 }else{
47205 nDestTruncate = nSrcPage * (nSrcPagesize/nDestPagesize);
47206 }
47207 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
47208
47209 if( nSrcPagesize<nDestPagesize ){
47210 /* If the source page-size is smaller than the destination page-size,
47211 ** two extra things may need to happen:
47212 **
47213 ** * The destination may need to be truncated, and
47214 **
47215 ** * Data stored on the pages immediately following the
47216 ** pending-byte page in the source database may need to be
47217 ** copied into the destination database.
47218 */
47219 const i64 iSize = (i64)nSrcPagesize * (i64)nSrcPage;
47220 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
47221
47222 assert( pFile );
47223 assert( (i64)nDestTruncate*(i64)nDestPagesize >= iSize || (
47224 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
47225 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+nDestPagesize
47226 ));
47227 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
47228 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
47229 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
47230 ){
47231 i64 iOff;
47232 i64 iEnd = MIN(PENDING_BYTE + nDestPagesize, iSize);
47233 for(
47234 iOff=PENDING_BYTE+nSrcPagesize;
47235 rc==SQLITE_OK && iOff<iEnd;
47236 iOff+=nSrcPagesize
47237 ){
47238 PgHdr *pSrcPg = 0;
47239 const Pgno iSrcPg = (Pgno)((iOff/nSrcPagesize)+1);
47240 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
47241 if( rc==SQLITE_OK ){
47242 u8 *zData = sqlite3PagerGetData(pSrcPg);
47243 rc = sqlite3OsWrite(pFile, zData, nSrcPagesize, iOff);
47244 }
47245 sqlite3PagerUnref(pSrcPg);
47246 }
47247 }
47248 }else{
@@ -49366,13 +53714,16 @@
49366 ** Return the opcode for a given address. If the address is -1, then
49367 ** return the most recently inserted opcode.
49368 **
49369 ** If a memory allocation error has occurred prior to the calling of this
49370 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
49371 ** is readable and writable, but it has no effect. The return of a dummy
49372 ** opcode allows the call to continue functioning after a OOM fault without
49373 ** having to check to see if the return from this routine is a valid pointer.
 
 
 
49374 **
49375 ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
49376 ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
49377 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
49378 ** a new VDBE is created. So we are free to set addr to p->nOp-1 without
@@ -49379,21 +53730,21 @@
49379 ** having to double-check to make sure that the result is non-negative. But
49380 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
49381 ** check the value of p->nOp-1 before continuing.
49382 */
49383 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
49384 static VdbeOp dummy;
49385 assert( p->magic==VDBE_MAGIC_INIT );
49386 if( addr<0 ){
49387 #ifdef SQLITE_OMIT_TRACE
49388 if( p->nOp==0 ) return &dummy;
49389 #endif
49390 addr = p->nOp - 1;
49391 }
49392 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
49393 if( p->db->mallocFailed ){
49394 return &dummy;
49395 }else{
49396 return &p->aOp[addr];
49397 }
49398 }
49399
@@ -49502,10 +53853,15 @@
49502 }
49503 #endif
49504
49505 /*
49506 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
 
 
 
 
 
49507 */
49508 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
49509 int mask;
49510 assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
49511 assert( i<(int)sizeof(p->btreeMask)*8 );
@@ -50001,10 +54357,11 @@
50001 p->magic = VDBE_MAGIC_RUN;
50002 p->nChange = 0;
50003 p->cacheCtr = 1;
50004 p->minWriteFileFormat = 255;
50005 p->iStatement = 0;
 
50006 #ifdef VDBE_PROFILE
50007 {
50008 int i;
50009 for(i=0; i<p->nOp; i++){
50010 p->aOp[i].cnt = 0;
@@ -50689,14 +55046,21 @@
50689 ** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in
50690 ** the following code.
50691 */
50692 if( eStatementOp ){
50693 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
50694 if( rc && (NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT) ){
50695 p->rc = rc;
50696 sqlite3DbFree(db, p->zErrMsg);
50697 p->zErrMsg = 0;
 
 
 
 
 
 
 
50698 }
50699 }
50700
50701 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
50702 ** has been rolled back, update the database connection change-counter.
@@ -51976,10 +56340,31 @@
51976 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
51977 sqlite3VdbeMemSetNull(&pCtx->s);
51978 pCtx->isError = SQLITE_NOMEM;
51979 pCtx->s.db->mallocFailed = 1;
51980 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51981
51982 /*
51983 ** Execute the statement pStmt, either until a row of data is ready, the
51984 ** statement is completely executed or an error occurs.
51985 **
@@ -51992,13 +56377,16 @@
51992 sqlite3 *db;
51993 int rc;
51994
51995 assert(p);
51996 if( p->magic!=VDBE_MAGIC_RUN ){
51997 sqlite3_log(SQLITE_MISUSE,
51998 "attempt to step a halted statement: [%s]", p->zSql);
51999 return SQLITE_MISUSE_BKPT;
 
 
 
52000 }
52001
52002 /* Check that malloc() has not failed. If it has, return early. */
52003 db = p->db;
52004 if( db->mallocFailed ){
@@ -52022,13 +56410,11 @@
52022
52023 assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
52024
52025 #ifndef SQLITE_OMIT_TRACE
52026 if( db->xProfile && !db->init.busy ){
52027 double rNow;
52028 sqlite3OsCurrentTime(db->pVfs, &rNow);
52029 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
52030 }
52031 #endif
52032
52033 db->activeVdbeCnt++;
52034 if( p->readOnly==0 ) db->writeVdbeCnt++;
@@ -52045,19 +56431,23 @@
52045
52046 #ifndef SQLITE_OMIT_TRACE
52047 /* Invoke the profile callback if there is one
52048 */
52049 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
52050 double rNow;
52051 u64 elapseTime;
52052
52053 sqlite3OsCurrentTime(db->pVfs, &rNow);
52054 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0);
52055 elapseTime -= p->startTime;
52056 db->xProfile(db->pProfileArg, p->zSql, elapseTime);
52057 }
52058 #endif
 
 
 
 
 
 
 
 
52059
52060 db->errCode = rc;
52061 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
52062 p->rc = SQLITE_NOMEM;
52063 }
@@ -53638,26 +58028,10 @@
53638 ** flag on jump instructions, we get a (small) speed improvement.
53639 */
53640 #define CHECK_FOR_INTERRUPT \
53641 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
53642
53643 #ifdef SQLITE_DEBUG
53644 static int fileExists(sqlite3 *db, const char *zFile){
53645 int res = 0;
53646 int rc = SQLITE_OK;
53647 #ifdef SQLITE_TEST
53648 /* If we are currently testing IO errors, then do not call OsAccess() to
53649 ** test for the presence of zFile. This is because any IO error that
53650 ** occurs here will not be reported, causing the test to fail.
53651 */
53652 extern int sqlite3_io_error_pending;
53653 if( sqlite3_io_error_pending<=0 )
53654 #endif
53655 rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
53656 return (res && rc==SQLITE_OK);
53657 }
53658 #endif
53659
53660 #ifndef NDEBUG
53661 /*
53662 ** This function is only called from within an assert() expression. It
53663 ** checks that the sqlite3.nTransaction variable is correctly set to
@@ -53744,13 +58118,10 @@
53744 union vdbeExecUnion {
53745 struct OP_Yield_stack_vars {
53746 int pcDest;
53747 } aa;
53748 struct OP_Variable_stack_vars {
53749 int p1; /* Variable to copy from */
53750 int p2; /* Register to copy to */
53751 int n; /* Number of values left to copy */
53752 Mem *pVar; /* Value being transferred */
53753 } ab;
53754 struct OP_Move_stack_vars {
53755 char *zMalloc; /* Holding variable for allocated memory */
53756 int n; /* Number of registers left to copy */
@@ -54071,22 +58442,29 @@
54071 sqlite3_value **apVal;
54072 } cb;
54073 struct OP_AggFinal_stack_vars {
54074 Mem *pMem;
54075 } cc;
 
 
 
 
 
 
 
54076 struct OP_IncrVacuum_stack_vars {
54077 Btree *pBt;
54078 } cd;
54079 struct OP_VBegin_stack_vars {
54080 VTable *pVTab;
54081 } ce;
54082 struct OP_VOpen_stack_vars {
54083 VdbeCursor *pCur;
54084 sqlite3_vtab_cursor *pVtabCursor;
54085 sqlite3_vtab *pVtab;
54086 sqlite3_module *pModule;
54087 } cf;
54088 struct OP_VFilter_stack_vars {
54089 int nArg;
54090 int iQuery;
54091 const sqlite3_module *pModule;
54092 Mem *pQuery;
@@ -54095,39 +58473,39 @@
54095 sqlite3_vtab *pVtab;
54096 VdbeCursor *pCur;
54097 int res;
54098 int i;
54099 Mem **apArg;
54100 } cg;
54101 struct OP_VColumn_stack_vars {
54102 sqlite3_vtab *pVtab;
54103 const sqlite3_module *pModule;
54104 Mem *pDest;
54105 sqlite3_context sContext;
54106 } ch;
54107 struct OP_VNext_stack_vars {
54108 sqlite3_vtab *pVtab;
54109 const sqlite3_module *pModule;
54110 int res;
54111 VdbeCursor *pCur;
54112 } ci;
54113 struct OP_VRename_stack_vars {
54114 sqlite3_vtab *pVtab;
54115 Mem *pName;
54116 } cj;
54117 struct OP_VUpdate_stack_vars {
54118 sqlite3_vtab *pVtab;
54119 sqlite3_module *pModule;
54120 int nArg;
54121 int i;
54122 sqlite_int64 rowid;
54123 Mem **apArg;
54124 Mem *pX;
54125 } ck;
54126 struct OP_Trace_stack_vars {
54127 char *zTrace;
54128 } cl;
54129 } u;
54130 /* End automatically generated code
54131 ********************************************************************/
54132
54133 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -54147,23 +58525,18 @@
54147 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
54148 checkProgress = db->xProgress!=0;
54149 #endif
54150 #ifdef SQLITE_DEBUG
54151 sqlite3BeginBenignMalloc();
54152 if( p->pc==0
54153 && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
54154 ){
54155 int i;
54156 printf("VDBE Program Listing:\n");
54157 sqlite3VdbePrintSql(p);
54158 for(i=0; i<p->nOp; i++){
54159 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
54160 }
54161 }
54162 if( fileExists(db, "vdbe_trace") ){
54163 p->trace = stdout;
54164 }
54165 sqlite3EndBenignMalloc();
54166 #endif
54167 for(pc=p->pc; rc==SQLITE_OK; pc++){
54168 assert( pc>=0 && pc<p->nOp );
54169 if( db->mallocFailed ) goto no_mem;
@@ -54181,17 +58554,10 @@
54181 printf("VDBE Execution Trace:\n");
54182 sqlite3VdbePrintSql(p);
54183 }
54184 sqlite3VdbePrintOp(p->trace, pc, pOp);
54185 }
54186 if( p->trace==0 && pc==0 ){
54187 sqlite3BeginBenignMalloc();
54188 if( fileExists(db, "vdbe_sqltrace") ){
54189 sqlite3VdbePrintSql(p);
54190 }
54191 sqlite3EndBenignMalloc();
54192 }
54193 #endif
54194
54195
54196 /* Check to see if we need to simulate an interrupt. This only happens
54197 ** if we have a special test build.
@@ -54544,44 +58910,29 @@
54544 pOut->enc = encoding;
54545 UPDATE_MAX_BLOBSIZE(pOut);
54546 break;
54547 }
54548
54549 /* Opcode: Variable P1 P2 P3 P4 *
54550 **
54551 ** Transfer the values of bound parameters P1..P1+P3-1 into registers
54552 ** P2..P2+P3-1.
54553 **
54554 ** If the parameter is named, then its name appears in P4 and P3==1.
54555 ** The P4 value is used by sqlite3_bind_parameter_name().
54556 */
54557 case OP_Variable: {
54558 #if 0 /* local variables moved into u.ab */
54559 int p1; /* Variable to copy from */
54560 int p2; /* Register to copy to */
54561 int n; /* Number of values left to copy */
54562 Mem *pVar; /* Value being transferred */
54563 #endif /* local variables moved into u.ab */
54564
54565 u.ab.p1 = pOp->p1 - 1;
54566 u.ab.p2 = pOp->p2;
54567 u.ab.n = pOp->p3;
54568 assert( u.ab.p1>=0 && u.ab.p1+u.ab.n<=p->nVar );
54569 assert( u.ab.p2>=1 && u.ab.p2+u.ab.n-1<=p->nMem );
54570 assert( pOp->p4.z==0 || pOp->p3==1 || pOp->p3==0 );
54571
54572 while( u.ab.n-- > 0 ){
54573 u.ab.pVar = &p->aVar[u.ab.p1++];
54574 if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
54575 goto too_big;
54576 }
54577 pOut = &aMem[u.ab.p2++];
54578 sqlite3VdbeMemReleaseExternal(pOut);
54579 pOut->flags = MEM_Null;
54580 sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
54581 UPDATE_MAX_BLOBSIZE(pOut);
54582 }
54583 break;
54584 }
54585
54586 /* Opcode: Move P1 P2 P3 * *
54587 **
@@ -58849,10 +63200,151 @@
58849 goto too_big;
58850 }
58851 break;
58852 }
58853
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58854
58855 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
58856 /* Opcode: Vacuum * * * * *
58857 **
58858 ** Vacuum the entire database. This opcode will cause other virtual
@@ -58871,18 +63363,18 @@
58871 ** Perform a single step of the incremental vacuum procedure on
58872 ** the P1 database. If the vacuum has finished, jump to instruction
58873 ** P2. Otherwise, fall through to the next instruction.
58874 */
58875 case OP_IncrVacuum: { /* jump */
58876 #if 0 /* local variables moved into u.cd */
58877 Btree *pBt;
58878 #endif /* local variables moved into u.cd */
58879
58880 assert( pOp->p1>=0 && pOp->p1<db->nDb );
58881 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
58882 u.cd.pBt = db->aDb[pOp->p1].pBt;
58883 rc = sqlite3BtreeIncrVacuum(u.cd.pBt);
58884 if( rc==SQLITE_DONE ){
58885 pc = pOp->p2 - 1;
58886 rc = SQLITE_OK;
58887 }
58888 break;
@@ -58948,19 +63440,19 @@
58948 ** Also, whether or not P4 is set, check that this is not being called from
58949 ** within a callback to a virtual table xSync() method. If it is, the error
58950 ** code will be set to SQLITE_LOCKED.
58951 */
58952 case OP_VBegin: {
58953 #if 0 /* local variables moved into u.ce */
58954 VTable *pVTab;
58955 #endif /* local variables moved into u.ce */
58956 u.ce.pVTab = pOp->p4.pVtab;
58957 rc = sqlite3VtabBegin(db, u.ce.pVTab);
58958 if( u.ce.pVTab ){
58959 sqlite3DbFree(db, p->zErrMsg);
58960 p->zErrMsg = u.ce.pVTab->pVtab->zErrMsg;
58961 u.ce.pVTab->pVtab->zErrMsg = 0;
58962 }
58963 break;
58964 }
58965 #endif /* SQLITE_OMIT_VIRTUALTABLE */
58966
@@ -58996,38 +63488,38 @@
58996 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
58997 ** P1 is a cursor number. This opcode opens a cursor to the virtual
58998 ** table and stores that cursor in P1.
58999 */
59000 case OP_VOpen: {
59001 #if 0 /* local variables moved into u.cf */
59002 VdbeCursor *pCur;
59003 sqlite3_vtab_cursor *pVtabCursor;
59004 sqlite3_vtab *pVtab;
59005 sqlite3_module *pModule;
59006 #endif /* local variables moved into u.cf */
59007
59008 u.cf.pCur = 0;
59009 u.cf.pVtabCursor = 0;
59010 u.cf.pVtab = pOp->p4.pVtab->pVtab;
59011 u.cf.pModule = (sqlite3_module *)u.cf.pVtab->pModule;
59012 assert(u.cf.pVtab && u.cf.pModule);
59013 rc = u.cf.pModule->xOpen(u.cf.pVtab, &u.cf.pVtabCursor);
59014 sqlite3DbFree(db, p->zErrMsg);
59015 p->zErrMsg = u.cf.pVtab->zErrMsg;
59016 u.cf.pVtab->zErrMsg = 0;
59017 if( SQLITE_OK==rc ){
59018 /* Initialize sqlite3_vtab_cursor base class */
59019 u.cf.pVtabCursor->pVtab = u.cf.pVtab;
59020
59021 /* Initialise vdbe cursor object */
59022 u.cf.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
59023 if( u.cf.pCur ){
59024 u.cf.pCur->pVtabCursor = u.cf.pVtabCursor;
59025 u.cf.pCur->pModule = u.cf.pVtabCursor->pVtab->pModule;
59026 }else{
59027 db->mallocFailed = 1;
59028 u.cf.pModule->xClose(u.cf.pVtabCursor);
59029 }
59030 }
59031 break;
59032 }
59033 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59050,11 +63542,11 @@
59050 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
59051 **
59052 ** A jump is made to P2 if the result set after filtering would be empty.
59053 */
59054 case OP_VFilter: { /* jump */
59055 #if 0 /* local variables moved into u.cg */
59056 int nArg;
59057 int iQuery;
59058 const sqlite3_module *pModule;
59059 Mem *pQuery;
59060 Mem *pArgc;
@@ -59062,50 +63554,50 @@
59062 sqlite3_vtab *pVtab;
59063 VdbeCursor *pCur;
59064 int res;
59065 int i;
59066 Mem **apArg;
59067 #endif /* local variables moved into u.cg */
59068
59069 u.cg.pQuery = &aMem[pOp->p3];
59070 u.cg.pArgc = &u.cg.pQuery[1];
59071 u.cg.pCur = p->apCsr[pOp->p1];
59072 REGISTER_TRACE(pOp->p3, u.cg.pQuery);
59073 assert( u.cg.pCur->pVtabCursor );
59074 u.cg.pVtabCursor = u.cg.pCur->pVtabCursor;
59075 u.cg.pVtab = u.cg.pVtabCursor->pVtab;
59076 u.cg.pModule = u.cg.pVtab->pModule;
59077
59078 /* Grab the index number and argc parameters */
59079 assert( (u.cg.pQuery->flags&MEM_Int)!=0 && u.cg.pArgc->flags==MEM_Int );
59080 u.cg.nArg = (int)u.cg.pArgc->u.i;
59081 u.cg.iQuery = (int)u.cg.pQuery->u.i;
59082
59083 /* Invoke the xFilter method */
59084 {
59085 u.cg.res = 0;
59086 u.cg.apArg = p->apArg;
59087 for(u.cg.i = 0; u.cg.i<u.cg.nArg; u.cg.i++){
59088 u.cg.apArg[u.cg.i] = &u.cg.pArgc[u.cg.i+1];
59089 sqlite3VdbeMemStoreType(u.cg.apArg[u.cg.i]);
59090 }
59091
59092 p->inVtabMethod = 1;
59093 rc = u.cg.pModule->xFilter(u.cg.pVtabCursor, u.cg.iQuery, pOp->p4.z, u.cg.nArg, u.cg.apArg);
59094 p->inVtabMethod = 0;
59095 sqlite3DbFree(db, p->zErrMsg);
59096 p->zErrMsg = u.cg.pVtab->zErrMsg;
59097 u.cg.pVtab->zErrMsg = 0;
59098 if( rc==SQLITE_OK ){
59099 u.cg.res = u.cg.pModule->xEof(u.cg.pVtabCursor);
59100 }
59101
59102 if( u.cg.res ){
59103 pc = pOp->p2 - 1;
59104 }
59105 }
59106 u.cg.pCur->nullRow = 0;
59107
59108 break;
59109 }
59110 #endif /* SQLITE_OMIT_VIRTUALTABLE */
59111
@@ -59115,56 +63607,56 @@
59115 ** Store the value of the P2-th column of
59116 ** the row of the virtual-table that the
59117 ** P1 cursor is pointing to into register P3.
59118 */
59119 case OP_VColumn: {
59120 #if 0 /* local variables moved into u.ch */
59121 sqlite3_vtab *pVtab;
59122 const sqlite3_module *pModule;
59123 Mem *pDest;
59124 sqlite3_context sContext;
59125 #endif /* local variables moved into u.ch */
59126
59127 VdbeCursor *pCur = p->apCsr[pOp->p1];
59128 assert( pCur->pVtabCursor );
59129 assert( pOp->p3>0 && pOp->p3<=p->nMem );
59130 u.ch.pDest = &aMem[pOp->p3];
59131 if( pCur->nullRow ){
59132 sqlite3VdbeMemSetNull(u.ch.pDest);
59133 break;
59134 }
59135 u.ch.pVtab = pCur->pVtabCursor->pVtab;
59136 u.ch.pModule = u.ch.pVtab->pModule;
59137 assert( u.ch.pModule->xColumn );
59138 memset(&u.ch.sContext, 0, sizeof(u.ch.sContext));
59139
59140 /* The output cell may already have a buffer allocated. Move
59141 ** the current contents to u.ch.sContext.s so in case the user-function
59142 ** can use the already allocated buffer instead of allocating a
59143 ** new one.
59144 */
59145 sqlite3VdbeMemMove(&u.ch.sContext.s, u.ch.pDest);
59146 MemSetTypeFlag(&u.ch.sContext.s, MEM_Null);
59147
59148 rc = u.ch.pModule->xColumn(pCur->pVtabCursor, &u.ch.sContext, pOp->p2);
59149 sqlite3DbFree(db, p->zErrMsg);
59150 p->zErrMsg = u.ch.pVtab->zErrMsg;
59151 u.ch.pVtab->zErrMsg = 0;
59152 if( u.ch.sContext.isError ){
59153 rc = u.ch.sContext.isError;
59154 }
59155
59156 /* Copy the result of the function to the P3 register. We
59157 ** do this regardless of whether or not an error occurred to ensure any
59158 ** dynamic allocation in u.ch.sContext.s (a Mem struct) is released.
59159 */
59160 sqlite3VdbeChangeEncoding(&u.ch.sContext.s, encoding);
59161 sqlite3VdbeMemMove(u.ch.pDest, &u.ch.sContext.s);
59162 REGISTER_TRACE(pOp->p3, u.ch.pDest);
59163 UPDATE_MAX_BLOBSIZE(u.ch.pDest);
59164
59165 if( sqlite3VdbeMemTooBig(u.ch.pDest) ){
59166 goto too_big;
59167 }
59168 break;
59169 }
59170 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59175,44 +63667,44 @@
59175 ** Advance virtual table P1 to the next row in its result set and
59176 ** jump to instruction P2. Or, if the virtual table has reached
59177 ** the end of its result set, then fall through to the next instruction.
59178 */
59179 case OP_VNext: { /* jump */
59180 #if 0 /* local variables moved into u.ci */
59181 sqlite3_vtab *pVtab;
59182 const sqlite3_module *pModule;
59183 int res;
59184 VdbeCursor *pCur;
59185 #endif /* local variables moved into u.ci */
59186
59187 u.ci.res = 0;
59188 u.ci.pCur = p->apCsr[pOp->p1];
59189 assert( u.ci.pCur->pVtabCursor );
59190 if( u.ci.pCur->nullRow ){
59191 break;
59192 }
59193 u.ci.pVtab = u.ci.pCur->pVtabCursor->pVtab;
59194 u.ci.pModule = u.ci.pVtab->pModule;
59195 assert( u.ci.pModule->xNext );
59196
59197 /* Invoke the xNext() method of the module. There is no way for the
59198 ** underlying implementation to return an error if one occurs during
59199 ** xNext(). Instead, if an error occurs, true is returned (indicating that
59200 ** data is available) and the error code returned when xColumn or
59201 ** some other method is next invoked on the save virtual table cursor.
59202 */
59203 p->inVtabMethod = 1;
59204 rc = u.ci.pModule->xNext(u.ci.pCur->pVtabCursor);
59205 p->inVtabMethod = 0;
59206 sqlite3DbFree(db, p->zErrMsg);
59207 p->zErrMsg = u.ci.pVtab->zErrMsg;
59208 u.ci.pVtab->zErrMsg = 0;
59209 if( rc==SQLITE_OK ){
59210 u.ci.res = u.ci.pModule->xEof(u.ci.pCur->pVtabCursor);
59211 }
59212
59213 if( !u.ci.res ){
59214 /* If there is data, jump to P2 */
59215 pc = pOp->p2 - 1;
59216 }
59217 break;
59218 }
@@ -59224,24 +63716,24 @@
59224 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
59225 ** This opcode invokes the corresponding xRename method. The value
59226 ** in register P1 is passed as the zName argument to the xRename method.
59227 */
59228 case OP_VRename: {
59229 #if 0 /* local variables moved into u.cj */
59230 sqlite3_vtab *pVtab;
59231 Mem *pName;
59232 #endif /* local variables moved into u.cj */
59233
59234 u.cj.pVtab = pOp->p4.pVtab->pVtab;
59235 u.cj.pName = &aMem[pOp->p1];
59236 assert( u.cj.pVtab->pModule->xRename );
59237 REGISTER_TRACE(pOp->p1, u.cj.pName);
59238 assert( u.cj.pName->flags & MEM_Str );
59239 rc = u.cj.pVtab->pModule->xRename(u.cj.pVtab, u.cj.pName->z);
59240 sqlite3DbFree(db, p->zErrMsg);
59241 p->zErrMsg = u.cj.pVtab->zErrMsg;
59242 u.cj.pVtab->zErrMsg = 0;
59243
59244 break;
59245 }
59246 #endif
59247
@@ -59268,39 +63760,39 @@
59268 ** P1 is a boolean flag. If it is set to true and the xUpdate call
59269 ** is successful, then the value returned by sqlite3_last_insert_rowid()
59270 ** is set to the value of the rowid for the row just inserted.
59271 */
59272 case OP_VUpdate: {
59273 #if 0 /* local variables moved into u.ck */
59274 sqlite3_vtab *pVtab;
59275 sqlite3_module *pModule;
59276 int nArg;
59277 int i;
59278 sqlite_int64 rowid;
59279 Mem **apArg;
59280 Mem *pX;
59281 #endif /* local variables moved into u.ck */
59282
59283 u.ck.pVtab = pOp->p4.pVtab->pVtab;
59284 u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
59285 u.ck.nArg = pOp->p2;
59286 assert( pOp->p4type==P4_VTAB );
59287 if( ALWAYS(u.ck.pModule->xUpdate) ){
59288 u.ck.apArg = p->apArg;
59289 u.ck.pX = &aMem[pOp->p3];
59290 for(u.ck.i=0; u.ck.i<u.ck.nArg; u.ck.i++){
59291 sqlite3VdbeMemStoreType(u.ck.pX);
59292 u.ck.apArg[u.ck.i] = u.ck.pX;
59293 u.ck.pX++;
59294 }
59295 rc = u.ck.pModule->xUpdate(u.ck.pVtab, u.ck.nArg, u.ck.apArg, &u.ck.rowid);
59296 sqlite3DbFree(db, p->zErrMsg);
59297 p->zErrMsg = u.ck.pVtab->zErrMsg;
59298 u.ck.pVtab->zErrMsg = 0;
59299 if( rc==SQLITE_OK && pOp->p1 ){
59300 assert( u.ck.nArg>1 && u.ck.apArg[0] && (u.ck.apArg[0]->flags&MEM_Null) );
59301 db->lastRowid = u.ck.rowid;
59302 }
59303 p->nChange++;
59304 }
59305 break;
59306 }
@@ -59322,24 +63814,24 @@
59322 **
59323 ** If tracing is enabled (by the sqlite3_trace()) interface, then
59324 ** the UTF-8 string contained in P4 is emitted on the trace callback.
59325 */
59326 case OP_Trace: {
59327 #if 0 /* local variables moved into u.cl */
59328 char *zTrace;
59329 #endif /* local variables moved into u.cl */
59330
59331 u.cl.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
59332 if( u.cl.zTrace ){
59333 if( db->xTrace ){
59334 char *z = sqlite3VdbeExpandSql(p, u.cl.zTrace);
59335 db->xTrace(db->pTraceArg, z);
59336 sqlite3DbFree(db, z);
59337 }
59338 #ifdef SQLITE_DEBUG
59339 if( (db->flags & SQLITE_SqlTrace)!=0 ){
59340 sqlite3DebugPrintf("SQL-trace: %s\n", u.cl.zTrace);
59341 }
59342 #endif /* SQLITE_DEBUG */
59343 }
59344 break;
59345 }
@@ -60295,11 +64787,11 @@
60295 }
60296
60297 /*
60298 ** Table of methods for MemJournal sqlite3_file object.
60299 */
60300 static struct sqlite3_io_methods MemJournalMethods = {
60301 1, /* iVersion */
60302 memjrnlClose, /* xClose */
60303 memjrnlRead, /* xRead */
60304 memjrnlWrite, /* xWrite */
60305 memjrnlTruncate, /* xTruncate */
@@ -60318,11 +64810,11 @@
60318 */
60319 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
60320 MemJournal *p = (MemJournal *)pJfd;
60321 assert( EIGHT_BYTE_ALIGNMENT(p) );
60322 memset(p, 0, sqlite3MemJournalSize());
60323 p->pMethod = &MemJournalMethods;
60324 }
60325
60326 /*
60327 ** Return true if the file-handle passed as an argument is
60328 ** an in-memory journal
@@ -63760,10 +68252,31 @@
63760 if( p->iReg==iReg ){
63761 p->tempReg = 0;
63762 }
63763 }
63764 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63765
63766 /*
63767 ** Generate code that will extract the iColumn-th column from
63768 ** table pTab and store the column value in a register. An effort
63769 ** is made to store the column value in register iReg, but this is
@@ -63789,17 +68302,11 @@
63789 sqlite3ExprCachePinRegister(pParse, p->iReg);
63790 return p->iReg;
63791 }
63792 }
63793 assert( v!=0 );
63794 if( iColumn<0 ){
63795 sqlite3VdbeAddOp2(v, OP_Rowid, iTable, iReg);
63796 }else if( ALWAYS(pTab!=0) ){
63797 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
63798 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
63799 sqlite3ColumnDefault(v, pTab, iColumn, iReg);
63800 }
63801 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
63802 return iReg;
63803 }
63804
63805 /*
@@ -64032,31 +68539,16 @@
64032 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
64033 break;
64034 }
64035 #endif
64036 case TK_VARIABLE: {
64037 VdbeOp *pOp;
64038 assert( !ExprHasProperty(pExpr, EP_IntValue) );
64039 assert( pExpr->u.zToken!=0 );
64040 assert( pExpr->u.zToken[0]!=0 );
64041 if( pExpr->u.zToken[1]==0
64042 && (pOp = sqlite3VdbeGetOp(v, -1))->opcode==OP_Variable
64043 && pOp->p1+pOp->p3==pExpr->iColumn
64044 && pOp->p2+pOp->p3==target
64045 && pOp->p4.z==0
64046 ){
64047 /* If the previous instruction was a copy of the previous unnamed
64048 ** parameter into the previous register, then simply increment the
64049 ** repeat count on the prior instruction rather than making a new
64050 ** instruction.
64051 */
64052 pOp->p3++;
64053 }else{
64054 sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iColumn, target, 1);
64055 if( pExpr->u.zToken[1]!=0 ){
64056 sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
64057 }
64058 }
64059 break;
64060 }
64061 case TK_REGISTER: {
64062 inReg = pExpr->iTable;
@@ -65119,11 +69611,10 @@
65119 ** this routine is used, it does not hurt to get an extra 2 - that
65120 ** just might result in some slightly slower code. But returning
65121 ** an incorrect 0 or 1 could lead to a malfunction.
65122 */
65123 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
65124 int i;
65125 if( pA==0||pB==0 ){
65126 return pB==pA ? 0 : 2;
65127 }
65128 assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
65129 assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
@@ -65132,22 +69623,11 @@
65132 }
65133 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
65134 if( pA->op!=pB->op ) return 2;
65135 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
65136 if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
65137
65138 if( pA->x.pList && pB->x.pList ){
65139 if( pA->x.pList->nExpr!=pB->x.pList->nExpr ) return 2;
65140 for(i=0; i<pA->x.pList->nExpr; i++){
65141 Expr *pExprA = pA->x.pList->a[i].pExpr;
65142 Expr *pExprB = pB->x.pList->a[i].pExpr;
65143 if( sqlite3ExprCompare(pExprA, pExprB) ) return 2;
65144 }
65145 }else if( pA->x.pList || pB->x.pList ){
65146 return 2;
65147 }
65148
65149 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
65150 if( ExprHasProperty(pA, EP_IntValue) ){
65151 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
65152 return 2;
65153 }
@@ -65160,10 +69640,35 @@
65160 if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
65161 if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
65162 return 0;
65163 }
65164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65165
65166 /*
65167 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
65168 ** the new element. Return a negative number if malloc fails.
65169 */
@@ -65656,21 +70161,27 @@
65656 #endif /* !SQLITE_OMIT_TRIGGER */
65657
65658 /*
65659 ** Register built-in functions used to help implement ALTER TABLE
65660 */
65661 SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3 *db){
65662 sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
65663 renameTableFunc, 0, 0);
65664 #ifndef SQLITE_OMIT_TRIGGER
65665 sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
65666 renameTriggerFunc, 0, 0);
65667 #endif
65668 #ifndef SQLITE_OMIT_FOREIGN_KEY
65669 sqlite3CreateFunc(db, "sqlite_rename_parent", 3, SQLITE_UTF8, 0,
65670 renameParentFunc, 0, 0);
65671 #endif
 
 
 
 
 
 
 
 
65672 }
65673
65674 /*
65675 ** This function is used to create the text of expressions of the form:
65676 **
@@ -65810,19 +70321,22 @@
65810 Vdbe *v;
65811 #ifndef SQLITE_OMIT_TRIGGER
65812 char *zWhere = 0; /* Where clause to locate temp triggers */
65813 #endif
65814 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
65815
 
 
65816 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
65817 assert( pSrc->nSrc==1 );
65818 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
65819
65820 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
65821 if( !pTab ) goto exit_rename_table;
65822 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
65823 zDb = db->aDb[iDb].zName;
 
65824
65825 /* Get a NULL terminated version of the new table name. */
65826 zName = sqlite3NameFromToken(db, pName);
65827 if( !zName ) goto exit_rename_table;
65828
@@ -65986,10 +70500,11 @@
65986 reloadTableSchema(pParse, pTab, zName);
65987
65988 exit_rename_table:
65989 sqlite3SrcListDelete(db, pSrc);
65990 sqlite3DbFree(db, zName);
 
65991 }
65992
65993
65994 /*
65995 ** Generate code to make sure the file format number is at least minFormat.
@@ -66105,21 +70620,24 @@
66105
66106 /* Modify the CREATE TABLE statement. */
66107 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
66108 if( zCol ){
66109 char *zEnd = &zCol[pColDef->n-1];
 
66110 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
66111 *zEnd-- = '\0';
66112 }
 
66113 sqlite3NestedParse(pParse,
66114 "UPDATE \"%w\".%s SET "
66115 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
66116 "WHERE type = 'table' AND name = %Q",
66117 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
66118 zTab
66119 );
66120 sqlite3DbFree(db, zCol);
 
66121 }
66122
66123 /* If the default value of the new column is NULL, then set the file
66124 ** format to 2. If the default value of the new column is not NULL,
66125 ** the file format becomes 3.
@@ -66261,11 +70779,11 @@
66261 Parse *pParse, /* Parsing context */
66262 int iDb, /* The database we are looking in */
66263 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
66264 const char *zWhere /* Delete entries associated with this table */
66265 ){
66266 static struct {
66267 const char *zName;
66268 const char *zCols;
66269 } aTable[] = {
66270 { "sqlite_stat1", "tbl,idx,stat" },
66271 #ifdef SQLITE_ENABLE_STAT2
@@ -67019,11 +71537,12 @@
67019 "attached databases must use the same text encoding as main database");
67020 rc = SQLITE_ERROR;
67021 }
67022 pPager = sqlite3BtreePager(aNew->pBt);
67023 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
67024 sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
 
67025 sqlite3BtreeSecureDelete(aNew->pBt,
67026 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
67027 }
67028 aNew->safety_level = 3;
67029 aNew->zName = sqlite3DbStrDup(db, zName);
@@ -67164,11 +71683,11 @@
67164 ** sqlite_detach() or sqlite_attach() SQL user functions.
67165 */
67166 static void codeAttach(
67167 Parse *pParse, /* The parser context */
67168 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
67169 FuncDef *pFunc, /* FuncDef wrapper for detachFunc() or attachFunc() */
67170 Expr *pAuthArg, /* Expression to pass to authorization callback */
67171 Expr *pFilename, /* Name of database file */
67172 Expr *pDbname, /* Name of the database to use internally */
67173 Expr *pKey /* Database key for encryption extension */
67174 ){
@@ -67214,10 +71733,21 @@
67214 if( v ){
67215 sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
67216 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
67217 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
67218 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
 
 
 
 
 
 
 
 
 
 
 
67219
67220 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
67221 ** statement only). For DETACH, set it to false (expire all existing
67222 ** statements).
67223 */
@@ -67234,11 +71764,11 @@
67234 ** Called by the parser to compile a DETACH statement.
67235 **
67236 ** DETACH pDbname
67237 */
67238 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
67239 static FuncDef detach_func = {
67240 1, /* nArg */
67241 SQLITE_UTF8, /* iPrefEnc */
67242 0, /* flags */
67243 0, /* pUserData */
67244 0, /* pNext */
@@ -67255,11 +71785,11 @@
67255 ** Called by the parser to compile an ATTACH statement.
67256 **
67257 ** ATTACH p AS pDbname KEY pKey
67258 */
67259 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
67260 static FuncDef attach_func = {
67261 3, /* nArg */
67262 SQLITE_UTF8, /* iPrefEnc */
67263 0, /* flags */
67264 0, /* pUserData */
67265 0, /* pNext */
@@ -71050,11 +75580,11 @@
71050 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
71051 char *zName = sqlite3NameFromToken(pParse->db, pName);
71052 if( zName ){
71053 Vdbe *v = sqlite3GetVdbe(pParse);
71054 #ifndef SQLITE_OMIT_AUTHORIZATION
71055 static const char *az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
71056 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
71057 #endif
71058 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
71059 sqlite3DbFree(pParse->db, zName);
71060 return;
@@ -71090,11 +75620,11 @@
71090 assert( db->aDb[1].pSchema );
71091 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
71092 db->mallocFailed = 1;
71093 return 1;
71094 }
71095 sqlite3PagerJournalMode(sqlite3BtreePager(pBt), db->dfltJournalMode);
71096 }
71097 return 0;
71098 }
71099
71100 /*
@@ -71729,19 +76259,24 @@
71729 }
71730 p = p->pNext;
71731 }
71732
71733 /* If no match is found, search the built-in functions.
 
 
 
 
71734 **
71735 ** Except, if createFlag is true, that means that we are trying to
71736 ** install a new function. Whatever FuncDef structure is returned will
71737 ** have fields overwritten with new information appropriate for the
71738 ** new function. But the FuncDefs for built-in functions are read-only.
71739 ** So we must not search for built-ins when creating a new function.
71740 */
71741 if( !createFlag && !pBest ){
71742 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
 
71743 p = functionSearch(pHash, h, zName, nName);
71744 while( p ){
71745 int score = matchQuality(p, nArg, enc);
71746 if( score>bestScore ){
71747 pBest = p;
@@ -72337,13 +76872,11 @@
72337 /* Populate the OLD.* pseudo-table register array. These values will be
72338 ** used by any BEFORE and AFTER triggers that exist. */
72339 sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
72340 for(iCol=0; iCol<pTab->nCol; iCol++){
72341 if( mask==0xffffffff || mask&(1<<iCol) ){
72342 int iTarget = iOld + iCol + 1;
72343 sqlite3VdbeAddOp3(v, OP_Column, iCur, iCol, iTarget);
72344 sqlite3ColumnDefault(v, pTab, iCol, iTarget);
72345 }
72346 }
72347
72348 /* Invoke BEFORE DELETE trigger programs. */
72349 sqlite3CodeRowTrigger(pParse, pTrigger,
@@ -73875,24 +78408,19 @@
73875 }
73876 }
73877 }
73878
73879 /*
73880 ** This function registered all of the above C functions as SQL
73881 ** functions. This should be the only routine in this file with
73882 ** external linkage.
73883 */
73884 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
73885 #ifndef SQLITE_OMIT_ALTERTABLE
73886 sqlite3AlterFunctions(db);
73887 #endif
73888 if( !db->mallocFailed ){
73889 int rc = sqlite3_overload_function(db, "MATCH", 2);
73890 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
73891 if( rc==SQLITE_NOMEM ){
73892 db->mallocFailed = 1;
73893 }
73894 }
73895 }
73896
73897 /*
73898 ** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -74056,10 +78584,13 @@
74056
74057 for(i=0; i<ArraySize(aBuiltinFunc); i++){
74058 sqlite3FuncDefInsert(pHash, &aFunc[i]);
74059 }
74060 sqlite3RegisterDateTimeFunctions();
 
 
 
74061 }
74062
74063 /************** End of func.c ************************************************/
74064 /************** Begin file fkey.c ********************************************/
74065 /*
@@ -78480,10 +83011,35 @@
78480 }
78481 return zName;
78482 }
78483 #endif
78484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78485 /*
78486 ** Process a pragma statement.
78487 **
78488 ** Pragmas are of this form:
78489 **
@@ -78552,15 +83108,15 @@
78552 ** page cache size. The value returned is the maximum number of
78553 ** pages in the page cache. The second form sets both the current
78554 ** page cache size value and the persistent page cache size value
78555 ** stored in the database file.
78556 **
78557 ** The default cache size is stored in meta-value 2 of page 1 of the
78558 ** database file. The cache size is actually the absolute value of
78559 ** this memory location. The sign of meta-value 2 determines the
78560 ** synchronous setting. A negative value means synchronous is off
78561 ** and a positive value means synchronous is on.
78562 */
78563 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
78564 static const VdbeOpList getCacheSize[] = {
78565 { OP_Transaction, 0, 0, 0}, /* 0 */
78566 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
@@ -78585,14 +83141,10 @@
78585 }else{
78586 int size = atoi(zRight);
78587 if( size<0 ) size = -size;
78588 sqlite3BeginWriteOperation(pParse, 0, iDb);
78589 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
78590 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, BTREE_DEFAULT_CACHE_SIZE);
78591 addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0);
78592 sqlite3VdbeAddOp2(v, OP_Integer, -size, 1);
78593 sqlite3VdbeJumpHere(v, addr);
78594 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
78595 pDb->pSchema->cache_size = size;
78596 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
78597 }
78598 }else
@@ -78733,66 +83285,62 @@
78733 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
78734 }else
78735
78736 /*
78737 ** PRAGMA [database.]journal_mode
78738 ** PRAGMA [database.]journal_mode = (delete|persist|off|truncate|memory)
 
78739 */
78740 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
78741 int eMode;
78742 static char * const azModeName[] = {
78743 "delete", "persist", "off", "truncate", "memory"
78744 };
78745
78746 if( zRight==0 ){
78747 eMode = PAGER_JOURNALMODE_QUERY;
78748 }else{
 
78749 int n = sqlite3Strlen30(zRight);
78750 eMode = sizeof(azModeName)/sizeof(azModeName[0]) - 1;
78751 while( eMode>=0 && sqlite3StrNICmp(zRight, azModeName[eMode], n)!=0 ){
78752 eMode--;
 
 
78753 }
78754 }
78755 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
78756 /* Simple "PRAGMA journal_mode;" statement. This is a query for
78757 ** the current default journal mode (which may be different to
78758 ** the journal-mode of the main database).
78759 */
78760 eMode = db->dfltJournalMode;
 
 
78761 }else{
78762 Pager *pPager;
 
78763 if( pId2->n==0 ){
78764 /* This indicates that no database name was specified as part
78765 ** of the PRAGMA command. In this case the journal-mode must be
78766 ** set on all attached databases, as well as the main db file.
78767 **
78768 ** Also, the sqlite3.dfltJournalMode variable is set so that
78769 ** any subsequently attached databases also use the specified
78770 ** journal mode.
78771 */
78772 int ii;
78773 assert(pDb==&db->aDb[0]);
78774 for(ii=1; ii<db->nDb; ii++){
78775 if( db->aDb[ii].pBt ){
78776 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
78777 sqlite3PagerJournalMode(pPager, eMode);
78778 }
78779 }
78780 db->dfltJournalMode = (u8)eMode;
78781 }
78782 pPager = sqlite3BtreePager(pDb->pBt);
78783 eMode = sqlite3PagerJournalMode(pPager, eMode);
78784 }
78785 assert( eMode==PAGER_JOURNALMODE_DELETE
78786 || eMode==PAGER_JOURNALMODE_TRUNCATE
78787 || eMode==PAGER_JOURNALMODE_PERSIST
78788 || eMode==PAGER_JOURNALMODE_OFF
78789 || eMode==PAGER_JOURNALMODE_MEMORY );
78790 sqlite3VdbeSetNumCols(v, 1);
78791 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
78792 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0,
78793 azModeName[eMode], P4_STATIC);
78794 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
78795 }else
78796
78797 /*
78798 ** PRAGMA [database.]journal_size_limit
@@ -79605,10 +84153,40 @@
79605 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
79606 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
79607 }
79608 }else
79609 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79610
79611 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
79612 /*
79613 ** Report the current state of file logs for all databases
79614 */
@@ -84278,10 +88856,22 @@
84278 p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
84279 pGroupBy = p->pGroupBy;
84280 p->selFlags &= ~SF_Distinct;
84281 isDistinct = 0;
84282 }
 
 
 
 
 
 
 
 
 
 
 
 
84283
84284 /* If there is an ORDER BY clause, then this sorting
84285 ** index might end up being unused if the data can be
84286 ** extracted in pre-sorted order. If that is the case, then the
84287 ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
@@ -86161,11 +90751,11 @@
86161 **
86162 ** May you do good and not evil.
86163 ** May you find forgiveness for yourself and forgive others.
86164 ** May you share freely, never taking more than you give.
86165 **
86166 *************************************************************************
86167 ** This file contains C code routines that are called by the parser
86168 ** to handle UPDATE statements.
86169 */
86170
86171 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -86548,12 +91138,11 @@
86548 oldmask |= sqlite3TriggerColmask(pParse,
86549 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
86550 );
86551 for(i=0; i<pTab->nCol; i++){
86552 if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<<i)) ){
86553 sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regOld+i);
86554 sqlite3ColumnDefault(v, pTab, i, regOld+i);
86555 }else{
86556 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
86557 }
86558 }
86559 if( chngRowid==0 ){
@@ -86922,11 +91511,12 @@
86922 int saved_nChange; /* Saved value of db->nChange */
86923 int saved_nTotalChange; /* Saved value of db->nTotalChange */
86924 void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
86925 Db *pDb = 0; /* Database to detach at end of vacuum */
86926 int isMemDb; /* True if vacuuming a :memory: database */
86927 int nRes;
 
86928
86929 if( !db->autoCommit ){
86930 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
86931 return SQLITE_ERROR;
86932 }
@@ -86936,11 +91526,11 @@
86936 ** disable CHECK and foreign key constraints. */
86937 saved_flags = db->flags;
86938 saved_nChange = db->nChange;
86939 saved_nTotalChange = db->nTotalChange;
86940 saved_xTrace = db->xTrace;
86941 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
86942 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
86943 db->xTrace = 0;
86944
86945 pMain = db->aDb[0].pBt;
86946 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
@@ -86957,19 +91547,22 @@
86957 ** actually occurs when doing a vacuum since the vacuum_db is initially
86958 ** empty. Only the journal header is written. Apparently it takes more
86959 ** time to parse and run the PRAGMA to turn journalling off than it does
86960 ** to write the journal header file.
86961 */
 
86962 if( sqlite3TempInMemory(db) ){
86963 zSql = "ATTACH ':memory:' AS vacuum_db;";
86964 }else{
86965 zSql = "ATTACH '' AS vacuum_db;";
86966 }
86967 rc = execSql(db, pzErrMsg, zSql);
 
 
 
 
86968 if( rc!=SQLITE_OK ) goto end_of_vacuum;
86969 pDb = &db->aDb[db->nDb-1];
86970 assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
86971 pTemp = db->aDb[db->nDb-1].pBt;
86972
86973 /* The call to execSql() to attach the temp database has left the file
86974 ** locked (as there was more than one active statement when the transaction
86975 ** to read the schema was concluded. Unlock it here so that this doesn't
@@ -86986,10 +91579,16 @@
86986 char *zKey;
86987 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
86988 if( nKey ) db->nextPagesize = 0;
86989 }
86990 #endif
 
 
 
 
 
 
86991
86992 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
86993 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
86994 || NEVER(db->mallocFailed)
86995 ){
@@ -87123,10 +91722,11 @@
87123 /* Restore the original value of db->flags */
87124 db->flags = saved_flags;
87125 db->nChange = saved_nChange;
87126 db->nTotalChange = saved_nTotalChange;
87127 db->xTrace = saved_xTrace;
 
87128
87129 /* Currently there is an SQL level transaction open on the vacuum
87130 ** database. No locks are held on any other files (since the main file
87131 ** was committed at the btree level). So it safe to end the transaction
87132 ** by manually setting the autoCommit flag to true and detaching the
@@ -90988,11 +95588,11 @@
90988 ** as we can without disabling too much. If we disabled in (1), we'd get
90989 ** the wrong answer. See ticket #813.
90990 */
90991 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
90992 if( pTerm
90993 && ALWAYS((pTerm->wtFlags & TERM_CODED)==0)
90994 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
90995 ){
90996 pTerm->wtFlags |= TERM_CODED;
90997 if( pTerm->iParent>=0 ){
90998 WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
@@ -91186,11 +95786,13 @@
91186 for(j=0; j<nEq; j++){
91187 int r1;
91188 int k = pIdx->aiColumn[j];
91189 pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
91190 if( NEVER(pTerm==0) ) break;
91191 assert( (pTerm->wtFlags & TERM_CODED)==0 );
 
 
91192 r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
91193 if( r1!=regBase+j ){
91194 if( nReg==1 ){
91195 sqlite3ReleaseTempReg(pParse, regBase);
91196 regBase = r1;
@@ -91473,11 +96075,12 @@
91473 int nConstraint; /* Number of constraint terms */
91474 Index *pIdx; /* The index we will be using */
91475 int iIdxCur; /* The VDBE cursor for the index */
91476 int nExtraReg = 0; /* Number of extra registers needed */
91477 int op; /* Instruction opcode */
91478 char *zAff;
 
91479
91480 pIdx = pLevel->plan.u.pIdx;
91481 iIdxCur = pLevel->iIdxCur;
91482 k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
91483
@@ -91514,12 +96117,13 @@
91514 /* Generate code to evaluate all constraint terms using == or IN
91515 ** and store the values of those terms in an array of registers
91516 ** starting at regBase.
91517 */
91518 regBase = codeAllEqualityTerms(
91519 pParse, pLevel, pWC, notReady, nExtraReg, &zAff
91520 );
 
91521 addrNxt = pLevel->addrNxt;
91522
91523 /* If we are doing a reverse order scan on an ascending index, or
91524 ** a forward order scan on a descending index, interchange the
91525 ** start and end terms (pRangeStart and pRangeEnd).
@@ -91540,29 +96144,29 @@
91540 nConstraint = nEq;
91541 if( pRangeStart ){
91542 Expr *pRight = pRangeStart->pExpr->pRight;
91543 sqlite3ExprCode(pParse, pRight, regBase+nEq);
91544 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
91545 if( zAff ){
91546 if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
91547 /* Since the comparison is to be performed with no conversions
91548 ** applied to the operands, set the affinity to apply to pRight to
91549 ** SQLITE_AFF_NONE. */
91550 zAff[nConstraint] = SQLITE_AFF_NONE;
91551 }
91552 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
91553 zAff[nConstraint] = SQLITE_AFF_NONE;
91554 }
91555 }
91556 nConstraint++;
91557 }else if( isMinQuery ){
91558 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
91559 nConstraint++;
91560 startEq = 0;
91561 start_constraints = 1;
91562 }
91563 codeApplyAffinity(pParse, regBase, nConstraint, zAff);
91564 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
91565 assert( op!=0 );
91566 testcase( op==OP_Rewind );
91567 testcase( op==OP_Last );
91568 testcase( op==OP_SeekGt );
@@ -91578,25 +96182,26 @@
91578 if( pRangeEnd ){
91579 Expr *pRight = pRangeEnd->pExpr->pRight;
91580 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
91581 sqlite3ExprCode(pParse, pRight, regBase+nEq);
91582 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
91583 if( zAff ){
91584 if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
91585 /* Since the comparison is to be performed with no conversions
91586 ** applied to the operands, set the affinity to apply to pRight to
91587 ** SQLITE_AFF_NONE. */
91588 zAff[nConstraint] = SQLITE_AFF_NONE;
91589 }
91590 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
91591 zAff[nConstraint] = SQLITE_AFF_NONE;
91592 }
91593 }
91594 codeApplyAffinity(pParse, regBase, nEq+1, zAff);
91595 nConstraint++;
91596 }
91597 sqlite3DbFree(pParse->db, zAff);
 
91598
91599 /* Top of the loop body */
91600 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
91601
91602 /* Check if the index cursor is past the end of the range. */
@@ -97936,11 +102541,11 @@
97936 /* SQLITE_IOERR */ "disk I/O error",
97937 /* SQLITE_CORRUPT */ "database disk image is malformed",
97938 /* SQLITE_NOTFOUND */ 0,
97939 /* SQLITE_FULL */ "database or disk is full",
97940 /* SQLITE_CANTOPEN */ "unable to open database file",
97941 /* SQLITE_PROTOCOL */ 0,
97942 /* SQLITE_EMPTY */ "table contains no data",
97943 /* SQLITE_SCHEMA */ "database schema has changed",
97944 /* SQLITE_TOOBIG */ "string or blob too big",
97945 /* SQLITE_CONSTRAINT */ "constraint failed",
97946 /* SQLITE_MISMATCH */ "datatype mismatch",
@@ -98346,10 +102951,149 @@
98346 db->pRollbackArg = pArg;
98347 sqlite3_mutex_leave(db->mutex);
98348 return pRet;
98349 }
98350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98351 /*
98352 ** This function returns true if main-memory should be used instead of
98353 ** a temporary file for transient pager files and statement journals.
98354 ** The value returned depends on the value of db->temp_store (runtime
98355 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
@@ -98906,10 +103650,12 @@
98906
98907 /* Enable the lookaside-malloc subsystem */
98908 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
98909 sqlite3GlobalConfig.nLookaside);
98910
 
 
98911 opendb_out:
98912 if( db ){
98913 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
98914 sqlite3_mutex_leave(db->mutex);
98915 }
@@ -99400,13 +104146,17 @@
99400 ** an incompatible database file format. Changing the PENDING byte
99401 ** while any database connection is open results in undefined and
99402 ** dileterious behavior.
99403 */
99404 case SQLITE_TESTCTRL_PENDING_BYTE: {
99405 unsigned int newVal = va_arg(ap, unsigned int);
99406 rc = sqlite3PendingByte;
99407 if( newVal ) sqlite3PendingByte = newVal;
 
 
 
 
99408 break;
99409 }
99410
99411 /*
99412 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
@@ -99505,10 +104255,19 @@
99505 int n = sqlite3Strlen30(zWord);
99506 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
99507 break;
99508 }
99509 #endif
 
 
 
 
 
 
 
 
 
99510
99511 }
99512 va_end(ap);
99513 #endif /* SQLITE_OMIT_BUILTIN_TEST */
99514 return rc;
@@ -99671,10 +104430,11 @@
99671 sqlite3_mutex_enter(db->mutex);
99672 enterMutex();
99673
99674 if( xNotify==0 ){
99675 removeFromBlockedList(db);
 
99676 db->pUnlockConnection = 0;
99677 db->xUnlockNotify = 0;
99678 db->pUnlockArg = 0;
99679 }else if( 0==db->pBlockingConnection ){
99680 /* The blocking transaction has been concluded. Or there never was a
99681
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a one translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% are more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -188,10 +188,18 @@
188 #endif
189 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
190 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500
191 #endif
192
193 /*
194 ** The default number of frames to accumulate in the log file before
195 ** checkpointing the database in WAL mode.
196 */
197 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
198 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
199 #endif
200
201 /*
202 ** The maximum number of attached databases. This must be between 0
203 ** and 30. The upper bound on 30 is because a 32-bit integer bitmap
204 ** is used internally to track attached databases.
205 */
@@ -626,13 +634,13 @@
634 **
635 ** See also: [sqlite3_libversion()],
636 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637 ** [sqlite_version()] and [sqlite_source_id()].
638 */
639 #define SQLITE_VERSION "3.7.0"
640 #define SQLITE_VERSION_NUMBER 3007000
641 #define SQLITE_SOURCE_ID "2010-06-16 12:30:11 ad3209572d0e6afe5c8b52313e334509661045e2"
642
643 /*
644 ** CAPI3REF: Run-Time Library Version Numbers
645 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
646 **
@@ -912,11 +920,11 @@
920 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
921 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
922 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
923 #define SQLITE_FULL 13 /* Insertion failed because database is full */
924 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
925 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
926 #define SQLITE_EMPTY 16 /* Database is empty */
927 #define SQLITE_SCHEMA 17 /* The database schema changed */
928 #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
929 #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
930 #define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -968,11 +976,16 @@
976 #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
977 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
978 #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
979 #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
980 #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
981 #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
982 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
983 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
984 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
985 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
986 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
987
988 /*
989 ** CAPI3REF: Flags For File Open Operations
990 **
991 ** These bit values are intended for use in the
@@ -1177,10 +1190,17 @@
1190 int (*xUnlock)(sqlite3_file*, int);
1191 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1192 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1193 int (*xSectorSize)(sqlite3_file*);
1194 int (*xDeviceCharacteristics)(sqlite3_file*);
1195 /* Methods above are valid for version 1 */
1196 int (*xShmOpen)(sqlite3_file*);
1197 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1198 int (*xShmMap)(sqlite3_file*, int iPage, int pgsz, int, void volatile**);
1199 void (*xShmBarrier)(sqlite3_file*);
1200 int (*xShmClose)(sqlite3_file*, int deleteFlag);
1201 /* Methods above are valid for version 2 */
1202 /* Additional methods may be added in future releases */
1203 };
1204
1205 /*
1206 ** CAPI3REF: Standard File Control Opcodes
@@ -1194,15 +1214,23 @@
1214 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1215 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1216 ** into an integer that the pArg argument points to. This capability
1217 ** is used during testing and only needs to be supported when SQLITE_TEST
1218 ** is defined.
1219 **
1220 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1221 ** layer a hint of how large the database file will grow to be during the
1222 ** current transaction. This hint is not guaranteed to be accurate but it
1223 ** is often close. The underlying VFS might choose to preallocate database
1224 ** file space based on this hint in order to help writes to the database
1225 ** file run faster.
1226 */
1227 #define SQLITE_FCNTL_LOCKSTATE 1
1228 #define SQLITE_GET_LOCKPROXYFILE 2
1229 #define SQLITE_SET_LOCKPROXYFILE 3
1230 #define SQLITE_LAST_ERRNO 4
1231 #define SQLITE_FCNTL_SIZE_HINT 5
1232
1233 /*
1234 ** CAPI3REF: Mutex Handle
1235 **
1236 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1343,11 +1371,11 @@
1371 ** method returns a Julian Day Number for the current date and time.
1372 **
1373 */
1374 typedef struct sqlite3_vfs sqlite3_vfs;
1375 struct sqlite3_vfs {
1376 int iVersion; /* Structure version number (currently 2) */
1377 int szOsFile; /* Size of subclassed sqlite3_file */
1378 int mxPathname; /* Maximum file pathname length */
1379 sqlite3_vfs *pNext; /* Next registered VFS */
1380 const char *zName; /* Name of this virtual file system */
1381 void *pAppData; /* Pointer to application-specific data */
@@ -1362,12 +1390,21 @@
1390 void (*xDlClose)(sqlite3_vfs*, void*);
1391 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1392 int (*xSleep)(sqlite3_vfs*, int microseconds);
1393 int (*xCurrentTime)(sqlite3_vfs*, double*);
1394 int (*xGetLastError)(sqlite3_vfs*, int, char *);
1395 /*
1396 ** The methods above are in version 1 of the sqlite_vfs object
1397 ** definition. Those that follow are added in version 2 or later
1398 */
1399 int (*xRename)(sqlite3_vfs*, const char *zOld, const char *zNew, int dirSync);
1400 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1401 /*
1402 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1403 ** New fields may be appended in figure versions. The iVersion
1404 ** value will increment whenever this happens.
1405 */
1406 };
1407
1408 /*
1409 ** CAPI3REF: Flags for the xAccess VFS method
1410 **
@@ -1382,10 +1419,48 @@
1419 ** checks whether the file is readable.
1420 */
1421 #define SQLITE_ACCESS_EXISTS 0
1422 #define SQLITE_ACCESS_READWRITE 1
1423 #define SQLITE_ACCESS_READ 2
1424
1425 /*
1426 ** CAPI3REF: Flags for the xShmLock VFS method
1427 **
1428 ** These integer constants define the various locking operations
1429 ** allowed by the xShmLock method of [sqlite3_io_methods]. The
1430 ** following are the only legal combinations of flags to the
1431 ** xShmLock method:
1432 **
1433 ** <ul>
1434 ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1435 ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1436 ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1437 ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1438 ** </ul>
1439 **
1440 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1441 ** was given no the corresponding lock.
1442 **
1443 ** The xShmLock method can transition between unlocked and SHARED or
1444 ** between unlocked and EXCLUSIVE. It cannot transition between SHARED
1445 ** and EXCLUSIVE.
1446 */
1447 #define SQLITE_SHM_UNLOCK 1
1448 #define SQLITE_SHM_LOCK 2
1449 #define SQLITE_SHM_SHARED 4
1450 #define SQLITE_SHM_EXCLUSIVE 8
1451
1452 /*
1453 ** CAPI3REF: Maximum xShmLock index
1454 **
1455 ** The xShmLock method on [sqlite3_io_methods] may use values
1456 ** between 0 and this upper bound as its "offset" argument.
1457 ** The SQLite core will never attempt to acquire or release a
1458 ** lock outside of this range
1459 */
1460 #define SQLITE_SHM_NLOCK 8
1461
1462
1463 /*
1464 ** CAPI3REF: Initialize The SQLite Library
1465 **
1466 ** ^The sqlite3_initialize() routine initializes the
@@ -3409,10 +3484,18 @@
3484 ** already been [sqlite3_finalize | finalized] or on one that had
3485 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
3486 ** be the case that the same database connection is being used by two or
3487 ** more threads at the same moment in time.
3488 **
3489 ** For all versions of SQLite up to and including 3.6.23.1, it was required
3490 ** after sqlite3_step() returned anything other than [SQLITE_ROW] that
3491 ** [sqlite3_reset()] be called before any subsequent invocation of
3492 ** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
3493 ** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
3494 ** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
3495 ** automatically in this circumstance rather than returning [SQLITE_MISUSE].
3496 **
3497 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3498 ** API always returns a generic error code, [SQLITE_ERROR], following any
3499 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
3500 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3501 ** specific [error codes] that better describes the error.
@@ -5507,11 +5590,12 @@
5590 #define SQLITE_TESTCTRL_ASSERT 12
5591 #define SQLITE_TESTCTRL_ALWAYS 13
5592 #define SQLITE_TESTCTRL_RESERVE 14
5593 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
5594 #define SQLITE_TESTCTRL_ISKEYWORD 16
5595 #define SQLITE_TESTCTRL_PGHDRSZ 17
5596 #define SQLITE_TESTCTRL_LAST 17
5597
5598 /*
5599 ** CAPI3REF: SQLite Runtime Status
5600 **
5601 ** ^This interface is used to retrieve runtime status information
@@ -5971,14 +6055,18 @@
6055 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
6056 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6057 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6058 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6059 **
6060 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6061 ** <ol>
6062 ** <li> the destination database was opened read-only, or
6063 ** <li> the destination database is using write-ahead-log journaling
6064 ** and the destination and source page sizes differ, or
6065 ** <li> The destination database is an in-memory database and the
6066 ** destination and source page sizes differ.
6067 ** </ol>)^
6068 **
6069 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6070 ** the [sqlite3_busy_handler | busy-handler function]
6071 ** is invoked (if one is specified). ^If the
6072 ** busy-handler returns non-zero before the lock is available, then
@@ -6242,10 +6330,93 @@
6330 ** a few hundred characters, it will be truncated to the length of the
6331 ** buffer.
6332 */
6333 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
6334
6335 /*
6336 ** CAPI3REF: Write-Ahead Log Commit Hook
6337 **
6338 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
6339 ** will be invoked each time a database connection commits data to a
6340 ** [write-ahead log] (i.e. whenever a transaction is committed in
6341 ** [journal_mode | journal_mode=WAL mode]).
6342 **
6343 ** ^The callback is invoked by SQLite after the commit has taken place and
6344 ** the associated write-lock on the database released, so the implementation
6345 ** may read, write or [checkpoint] the database as required.
6346 **
6347 ** ^The first parameter passed to the callback function when it is invoked
6348 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
6349 ** registering the callback. ^The second is a copy of the database handle.
6350 ** ^The third parameter is the name of the database that was written to -
6351 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
6352 ** is the number of pages currently in the write-ahead log file,
6353 ** including those that were just committed.
6354 **
6355 ** The callback function should normally return [SQLITE_OK]. ^If an error
6356 ** code is returned, that error will propagate back up through the
6357 ** SQLite code base to cause the statement that provoked the callback
6358 ** to report an error, though the commit will have still occurred. If the
6359 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
6360 ** that does not correspond to any valid SQLite error code, the results
6361 ** are undefined.
6362 **
6363 ** A single database handle may have at most a single write-ahead log callback
6364 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
6365 ** previously registered write-ahead log callback. ^Note that the
6366 ** [sqlite3_wal_autocheckpoint()] interface and the
6367 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
6368 ** those overwrite any prior [sqlite3_wal_hook()] settings.
6369 */
6370 SQLITE_API void *sqlite3_wal_hook(
6371 sqlite3*,
6372 int(*)(void *,sqlite3*,const char*,int),
6373 void*
6374 );
6375
6376 /*
6377 ** CAPI3REF: Configure an auto-checkpoint
6378 **
6379 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
6380 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
6381 ** to automatically [checkpoint]
6382 ** after committing a transaction if there are N or
6383 ** more frames in the [write-ahead log] file. ^Passing zero or
6384 ** a negative value as the nFrame parameter disables automatic
6385 ** checkpoints entirely.
6386 **
6387 ** ^The callback registered by this function replaces any existing callback
6388 ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
6389 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
6390 ** configured by this function.
6391 **
6392 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
6393 ** from SQL.
6394 **
6395 ** ^Every new [database connection] defaults to having the auto-checkpoint
6396 ** enabled with a threshold of 1000 pages. The use of this interface
6397 ** is only necessary if the default setting is found to be suboptimal
6398 ** for a particular application.
6399 */
6400 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
6401
6402 /*
6403 ** CAPI3REF: Checkpoint a database
6404 **
6405 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
6406 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
6407 ** empty string, then a checkpoint is run on all databases of
6408 ** connection D. ^If the database connection D is not in
6409 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
6410 **
6411 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
6412 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
6413 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
6414 ** run whenever the WAL reaches a certain size threshold.
6415 */
6416 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
6417
6418 /*
6419 ** Undo the hack that converts floating point types to integer for
6420 ** builds on processors without floating point support.
6421 */
6422 #ifdef SQLITE_OMIT_FLOATING_POINT
@@ -7050,10 +7221,12 @@
7221
7222 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
7223 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
7224 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
7225
7226 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
7227
7228 #ifndef NDEBUG
7229 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
7230 #endif
7231
7232 #ifndef SQLITE_OMIT_BTREECOUNT
@@ -7396,31 +7569,31 @@
7569 #define OP_IfPos 113
7570 #define OP_IfNeg 114
7571 #define OP_IfZero 115
7572 #define OP_AggStep 116
7573 #define OP_AggFinal 117
7574 #define OP_Checkpoint 118
7575 #define OP_JournalMode 119
7576 #define OP_Vacuum 120
7577 #define OP_IncrVacuum 121
7578 #define OP_Expire 122
7579 #define OP_TableLock 123
7580 #define OP_VBegin 124
7581 #define OP_VCreate 125
7582 #define OP_VDestroy 126
7583 #define OP_VOpen 127
7584 #define OP_VFilter 128
7585 #define OP_VColumn 129
7586 #define OP_VNext 131
7587 #define OP_VRename 132
7588 #define OP_VUpdate 133
7589 #define OP_Pagecount 134
7590 #define OP_Trace 135
7591 #define OP_Noop 136
7592 #define OP_Explain 137
7593
7594 /* The following opcode values are never used */
 
 
7595 #define OP_NotUsed_138 138
7596 #define OP_NotUsed_139 139
7597 #define OP_NotUsed_140 140
7598
7599
@@ -7435,11 +7608,11 @@
7608 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
7609 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
7610 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
7611 #define OPFLG_INITIALIZER {\
7612 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
7613 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
7614 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
7615 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
7616 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
7617 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
7618 /* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
@@ -7448,13 +7621,13 @@
7621 /* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
7622 /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
7623 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
7624 /* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
7625 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
7626 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
7627 /* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
7628 /* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x00,\
7629 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
7630 /* 144 */ 0x04, 0x04,}
7631
7632 /************** End of opcodes.h *********************************************/
7633 /************** Continuing where we left off in vdbe.h ***********************/
@@ -7596,18 +7769,19 @@
7769 #define PAGER_LOCKINGMODE_QUERY -1
7770 #define PAGER_LOCKINGMODE_NORMAL 0
7771 #define PAGER_LOCKINGMODE_EXCLUSIVE 1
7772
7773 /*
7774 ** Numeric constants that encode the journalmode.
7775 */
7776 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
7777 #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
7778 #define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
7779 #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
7780 #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
7781 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
7782 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
7783
7784 /*
7785 ** The remainder of this file contains the declarations of the functions
7786 ** that make up the Pager sub-system API. See source code comments for
7787 ** a detailed description of each routine.
@@ -7631,11 +7805,13 @@
7805 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
7806 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
7807 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
7808 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
7809 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
7810 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
7811 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
7812 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
7813 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
7814 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
7815
7816 /* Functions used to obtain and release page references. */
7817 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
@@ -7660,10 +7836,16 @@
7836 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
7837 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
7838 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
7839 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
7840 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
7841
7842 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager);
7843 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
7844 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
7845 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
7846 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
7847
7848 /* Functions used to query pager state and configuration. */
7849 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
7850 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
7851 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
@@ -8077,11 +8259,15 @@
8259 ** the incompatibility right away, even running a full regression test.
8260 ** The default location of PENDING_BYTE is the first byte past the
8261 ** 1GB boundary.
8262 **
8263 */
8264 #ifdef SQLITE_OMIT_WSD
8265 # define PENDING_BYTE (0x40000000)
8266 #else
8267 # define PENDING_BYTE sqlite3PendingByte
8268 #endif
8269 #define RESERVED_BYTE (PENDING_BYTE+1)
8270 #define SHARED_FIRST (PENDING_BYTE+2)
8271 #define SHARED_SIZE 510
8272
8273 /*
@@ -8103,10 +8289,15 @@
8289 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
8290 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
8291 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
8292 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
8293 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
8294 SQLITE_PRIVATE int sqlite3OsShmOpen(sqlite3_file *id);
8295 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
8296 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
8297 SQLITE_PRIVATE int sqlite3OsShmClose(sqlite3_file *id, int);
8298 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
8299
8300 /*
8301 ** Functions for accessing sqlite3_vfs methods
8302 */
8303 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -8119,11 +8310,11 @@
8310 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
8311 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
8312 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
8313 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
8314 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
8315 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
8316
8317 /*
8318 ** Convenience functions for opening and closing files using
8319 ** sqlite3_malloc() to obtain space for the file-handle structure.
8320 */
@@ -8396,10 +8587,14 @@
8587 int (*xCommitCallback)(void*); /* Invoked at every commit. */
8588 void *pRollbackArg; /* Argument to xRollbackCallback() */
8589 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
8590 void *pUpdateArg;
8591 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
8592 #ifndef SQLITE_OMIT_WAL
8593 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
8594 void *pWalArg;
8595 #endif
8596 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
8597 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
8598 void *pCollNeededArg;
8599 sqlite3_value *pErr; /* Most recent error message */
8600 char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
@@ -8486,10 +8681,11 @@
8681 #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
8682 #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
8683 #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
8684 #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
8685 #define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
8686 #define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
8687
8688 /*
8689 ** Bits of the sqlite3.flags field that are used by the
8690 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
8691 ** These must be the low-order bits of the flags field.
@@ -8497,11 +8693,12 @@
8693 #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
8694 #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
8695 #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
8696 #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
8697 #define SQLITE_IndexCover 0x10 /* Disable index covering table */
8698 #define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */
8699 #define SQLITE_OptMask 0xff /* Mask of all disablable opts */
8700
8701 /*
8702 ** Possible values for the sqlite.magic field.
8703 ** The numbers are obtained at random and have no special meaning, other
8704 ** than being distinct from one another.
@@ -10103,11 +10300,12 @@
10300 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
10301 #endif
10302
10303
10304 #ifndef SQLITE_MUTEX_OMIT
10305 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
10306 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void);
10307 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
10308 SQLITE_PRIVATE int sqlite3MutexInit(void);
10309 SQLITE_PRIVATE int sqlite3MutexEnd(void);
10310 #endif
10311
@@ -10235,10 +10433,11 @@
10433 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
10434 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
10435 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
10436 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
10437 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
10438 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
10439 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
10440 SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
10441 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
10442 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
10443 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
@@ -10261,10 +10460,11 @@
10460 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
10461 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
10462 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
10463 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
10464 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
10465 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
10466 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
10467 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
10468 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
10469 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
10470 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
@@ -10450,15 +10650,17 @@
10650 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
10651 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
10652 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
10653 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
10654 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
10655 #ifndef SQLITE_OMIT_WSD
10656 SQLITE_PRIVATE int sqlite3PendingByte;
10657 #endif
10658 #endif
10659 SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
10660 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
10661 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
10662 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
10663 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
10664 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
10665 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
10666 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
@@ -10563,10 +10765,13 @@
10765 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
10766 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
10767 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
10768 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
10769 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
10770 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
10771 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int);
10772 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
10773
10774 /* Declarations for functions in fkey.c. All of these are replaced by
10775 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
10776 ** key functionality is available. If OMIT_TRIGGER is defined but
10777 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
@@ -10907,11 +11112,13 @@
11112 ** IMPORTANT: Changing the pending byte to any value other than
11113 ** 0x40000000 results in an incompatible database file format!
11114 ** Changing the pending byte during operating results in undefined
11115 ** and dileterious behavior.
11116 */
11117 #ifndef SQLITE_OMIT_WSD
11118 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
11119 #endif
11120
11121 /*
11122 ** Properties of opcodes. The OPFLG_INITIALIZER macro is
11123 ** created by mkopcodeh.awk during compilation. Data is obtained
11124 ** from the comments following the "case OP_xxxx:" statements in
@@ -11764,14 +11971,12 @@
11971
11972 /*
11973 ** Set the time to the current time reported by the VFS
11974 */
11975 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
 
11976 sqlite3 *db = sqlite3_context_db_handle(context);
11977 sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
 
11978 p->validJD = 1;
11979 }
11980
11981 /*
11982 ** Attempt to parse the given string into a Julian Day Number. Return
@@ -12488,26 +12693,19 @@
12693 sqlite3_value **argv
12694 ){
12695 time_t t;
12696 char *zFormat = (char *)sqlite3_user_data(context);
12697 sqlite3 *db;
12698 sqlite3_int64 iT;
12699 char zBuf[20];
12700
12701 UNUSED_PARAMETER(argc);
12702 UNUSED_PARAMETER(argv);
12703
12704 db = sqlite3_context_db_handle(context);
12705 sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
12706 t = iT/1000 - 10000*(sqlite3_int64)21086676;
 
 
 
 
 
 
 
12707 #ifdef HAVE_GMTIME_R
12708 {
12709 struct tm sNow;
12710 gmtime_r(&t, &sNow);
12711 strftime(zBuf, 20, zFormat, &sNow);
@@ -12656,10 +12854,31 @@
12854 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
12855 }
12856 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
12857 return id->pMethods->xDeviceCharacteristics(id);
12858 }
12859 SQLITE_PRIVATE int sqlite3OsShmOpen(sqlite3_file *id){
12860 return id->pMethods->xShmOpen(id);
12861 }
12862 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
12863 return id->pMethods->xShmLock(id, offset, n, flags);
12864 }
12865 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
12866 id->pMethods->xShmBarrier(id);
12867 }
12868 SQLITE_PRIVATE int sqlite3OsShmClose(sqlite3_file *id, int deleteFlag){
12869 return id->pMethods->xShmClose(id, deleteFlag);
12870 }
12871 SQLITE_PRIVATE int sqlite3OsShmMap(
12872 sqlite3_file *id,
12873 int iPage,
12874 int pgsz,
12875 int isWrite,
12876 void volatile **pp
12877 ){
12878 return id->pMethods->xShmMap(id, iPage, pgsz, isWrite, pp);
12879 }
12880
12881 /*
12882 ** The next group of routines are convenience wrappers around the
12883 ** VFS methods.
12884 */
@@ -12719,12 +12938,20 @@
12938 return pVfs->xRandomness(pVfs, nByte, zBufOut);
12939 }
12940 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
12941 return pVfs->xSleep(pVfs, nMicro);
12942 }
12943 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
12944 int rc;
12945 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
12946 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
12947 }else{
12948 double r;
12949 rc = pVfs->xCurrentTime(pVfs, &r);
12950 *pTimeOut = (sqlite3_int64)(r*86400000.0);
12951 }
12952 return rc;
12953 }
12954
12955 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
12956 sqlite3_vfs *pVfs,
12957 const char *zFile,
@@ -14974,27 +15201,30 @@
15201 /*
15202 ** Initialize the mutex system.
15203 */
15204 SQLITE_PRIVATE int sqlite3MutexInit(void){
15205 int rc = SQLITE_OK;
15206 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
15207 /* If the xMutexAlloc method has not been set, then the user did not
15208 ** install a mutex implementation via sqlite3_config() prior to
15209 ** sqlite3_initialize() being called. This block copies pointers to
15210 ** the default implementation into the sqlite3GlobalConfig structure.
15211 */
15212 sqlite3_mutex_methods const *pFrom;
15213 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
15214
15215 if( sqlite3GlobalConfig.bCoreMutex ){
15216 pFrom = sqlite3DefaultMutex();
15217 }else{
15218 pFrom = sqlite3NoopMutex();
15219 }
15220 memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
15221 memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
15222 sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
15223 pTo->xMutexAlloc = pFrom->xMutexAlloc;
15224 }
15225 rc = sqlite3GlobalConfig.mutex.xMutexInit();
15226
15227 #ifdef SQLITE_DEBUG
15228 GLOBAL(int, mutexIsInit) = 1;
15229 #endif
15230
@@ -15122,68 +15352,69 @@
15352 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
15353 ** that does error checking on mutexes to make sure they are being
15354 ** called correctly.
15355 */
15356
15357 #ifndef SQLITE_MUTEX_OMIT
15358
15359 #ifndef SQLITE_DEBUG
15360 /*
15361 ** Stub routines for all mutex methods.
15362 **
15363 ** This routines provide no mutual exclusion or error checking.
15364 */
 
 
15365 static int noopMutexInit(void){ return SQLITE_OK; }
15366 static int noopMutexEnd(void){ return SQLITE_OK; }
15367 static sqlite3_mutex *noopMutexAlloc(int id){ return (sqlite3_mutex*)8; }
15368 static void noopMutexFree(sqlite3_mutex *p){ return; }
15369 static void noopMutexEnter(sqlite3_mutex *p){ return; }
15370 static int noopMutexTry(sqlite3_mutex *p){ return SQLITE_OK; }
15371 static void noopMutexLeave(sqlite3_mutex *p){ return; }
15372
15373 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
15374 static const sqlite3_mutex_methods sMutex = {
15375 noopMutexInit,
15376 noopMutexEnd,
15377 noopMutexAlloc,
15378 noopMutexFree,
15379 noopMutexEnter,
15380 noopMutexTry,
15381 noopMutexLeave,
15382
15383 0,
15384 0,
15385 };
15386
15387 return &sMutex;
15388 }
15389 #endif /* !SQLITE_DEBUG */
15390
15391 #ifdef SQLITE_DEBUG
15392 /*
15393 ** In this implementation, error checking is provided for testing
15394 ** and debugging purposes. The mutexes still do not provide any
15395 ** mutual exclusion.
15396 */
15397
15398 /*
15399 ** The mutex object
15400 */
15401 typedef struct sqlite3_debug_mutex {
15402 int id; /* The mutex type */
15403 int cnt; /* Number of entries without a matching leave */
15404 } sqlite3_debug_mutex;
15405
15406 /*
15407 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15408 ** intended for use inside assert() statements.
15409 */
15410 static int debugMutexHeld(sqlite3_mutex *pX){
15411 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15412 return p==0 || p->cnt>0;
15413 }
15414 static int debugMutexNotheld(sqlite3_mutex *pX){
15415 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15416 return p==0 || p->cnt==0;
15417 }
15418
15419 /*
15420 ** Initialize and deinitialize the mutex subsystem.
@@ -15195,12 +15426,12 @@
15426 ** The sqlite3_mutex_alloc() routine allocates a new
15427 ** mutex and returns a pointer to it. If it returns NULL
15428 ** that means that a mutex could not be allocated.
15429 */
15430 static sqlite3_mutex *debugMutexAlloc(int id){
15431 static sqlite3_debug_mutex aStatic[6];
15432 sqlite3_debug_mutex *pNew = 0;
15433 switch( id ){
15434 case SQLITE_MUTEX_FAST:
15435 case SQLITE_MUTEX_RECURSIVE: {
15436 pNew = sqlite3Malloc(sizeof(*pNew));
15437 if( pNew ){
@@ -15215,17 +15446,18 @@
15446 pNew = &aStatic[id-2];
15447 pNew->id = id;
15448 break;
15449 }
15450 }
15451 return (sqlite3_mutex*)pNew;
15452 }
15453
15454 /*
15455 ** This routine deallocates a previously allocated mutex.
15456 */
15457 static void debugMutexFree(sqlite3_mutex *pX){
15458 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15459 assert( p->cnt==0 );
15460 assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
15461 sqlite3_free(p);
15462 }
15463
@@ -15238,16 +15470,18 @@
15470 ** be entered multiple times by the same thread. In such cases the,
15471 ** mutex must be exited an equal number of times before another thread
15472 ** can enter. If the same thread tries to enter any other kind of mutex
15473 ** more than once, the behavior is undefined.
15474 */
15475 static void debugMutexEnter(sqlite3_mutex *pX){
15476 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15477 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
15478 p->cnt++;
15479 }
15480 static int debugMutexTry(sqlite3_mutex *pX){
15481 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15482 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
15483 p->cnt++;
15484 return SQLITE_OK;
15485 }
15486
15487 /*
@@ -15254,18 +15488,19 @@
15488 ** The sqlite3_mutex_leave() routine exits a mutex that was
15489 ** previously entered by the same thread. The behavior
15490 ** is undefined if the mutex is not currently entered or
15491 ** is not currently allocated. SQLite will never do either.
15492 */
15493 static void debugMutexLeave(sqlite3_mutex *pX){
15494 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
15495 assert( debugMutexHeld(pX) );
15496 p->cnt--;
15497 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
15498 }
15499
15500 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
15501 static const sqlite3_mutex_methods sMutex = {
15502 debugMutexInit,
15503 debugMutexEnd,
15504 debugMutexAlloc,
15505 debugMutexFree,
15506 debugMutexEnter,
@@ -15276,11 +15511,22 @@
15511 debugMutexNotheld
15512 };
15513
15514 return &sMutex;
15515 }
15516 #endif /* SQLITE_DEBUG */
15517
15518 /*
15519 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
15520 ** is used regardless of the run-time threadsafety setting.
15521 */
15522 #ifdef SQLITE_MUTEX_NOOP
15523 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
15524 return sqliteNoopMutex();
15525 }
15526 #endif /* SQLITE_MUTEX_NOOP */
15527 #endif /* SQLITE_MUTEX_OMIT */
15528
15529 /************** End of mutex_noop.c ******************************************/
15530 /************** Begin file mutex_os2.c ***************************************/
15531 /*
15532 ** 2007 August 28
@@ -15532,12 +15778,12 @@
15778 p->nRef--;
15779 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
15780 DosReleaseMutexSem(p->mutex);
15781 }
15782
15783 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
15784 static const sqlite3_mutex_methods sMutex = {
15785 os2MutexInit,
15786 os2MutexEnd,
15787 os2MutexAlloc,
15788 os2MutexFree,
15789 os2MutexEnter,
@@ -15578,27 +15824,37 @@
15824 */
15825 #ifdef SQLITE_MUTEX_PTHREADS
15826
15827 #include <pthread.h>
15828
15829 /*
15830 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
15831 ** are necessary under two condidtions: (1) Debug builds and (2) using
15832 ** home-grown mutexes. Encapsulate these conditions into a single #define.
15833 */
15834 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
15835 # define SQLITE_MUTEX_NREF 1
15836 #else
15837 # define SQLITE_MUTEX_NREF 0
15838 #endif
15839
15840 /*
15841 ** Each recursive mutex is an instance of the following structure.
15842 */
15843 struct sqlite3_mutex {
15844 pthread_mutex_t mutex; /* Mutex controlling the lock */
15845 #if SQLITE_MUTEX_NREF
15846 int id; /* Mutex type */
15847 volatile int nRef; /* Number of entrances */
15848 volatile pthread_t owner; /* Thread that is within this mutex */
 
15849 int trace; /* True to trace changes */
15850 #endif
15851 };
15852 #if SQLITE_MUTEX_NREF
15853 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
15854 #else
15855 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
15856 #endif
15857
15858 /*
15859 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
15860 ** intended for use only inside assert() statements. On some platforms,
@@ -15696,27 +15952,33 @@
15952 pthread_mutexattr_init(&recursiveAttr);
15953 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
15954 pthread_mutex_init(&p->mutex, &recursiveAttr);
15955 pthread_mutexattr_destroy(&recursiveAttr);
15956 #endif
15957 #if SQLITE_MUTEX_NREF
15958 p->id = iType;
15959 #endif
15960 }
15961 break;
15962 }
15963 case SQLITE_MUTEX_FAST: {
15964 p = sqlite3MallocZero( sizeof(*p) );
15965 if( p ){
15966 #if SQLITE_MUTEX_NREF
15967 p->id = iType;
15968 #endif
15969 pthread_mutex_init(&p->mutex, 0);
15970 }
15971 break;
15972 }
15973 default: {
15974 assert( iType-2 >= 0 );
15975 assert( iType-2 < ArraySize(staticMutexes) );
15976 p = &staticMutexes[iType-2];
15977 #if SQLITE_MUTEX_NREF
15978 p->id = iType;
15979 #endif
15980 break;
15981 }
15982 }
15983 return p;
15984 }
@@ -15772,12 +16034,14 @@
16034 }
16035 #else
16036 /* Use the built-in recursive mutexes if they are available.
16037 */
16038 pthread_mutex_lock(&p->mutex);
16039 #if SQLITE_MUTEX_NREF
16040 p->owner = pthread_self();
16041 p->nRef++;
16042 #endif
16043 #endif
16044
16045 #ifdef SQLITE_DEBUG
16046 if( p->trace ){
16047 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
@@ -15815,12 +16079,14 @@
16079 }
16080 #else
16081 /* Use the built-in recursive mutexes if they are available.
16082 */
16083 if( pthread_mutex_trylock(&p->mutex)==0 ){
16084 #if SQLITE_MUTEX_NREF
16085 p->owner = pthread_self();
16086 p->nRef++;
16087 #endif
16088 rc = SQLITE_OK;
16089 }else{
16090 rc = SQLITE_BUSY;
16091 }
16092 #endif
@@ -15839,11 +16105,13 @@
16105 ** is undefined if the mutex is not currently entered or
16106 ** is not currently allocated. SQLite will never do either.
16107 */
16108 static void pthreadMutexLeave(sqlite3_mutex *p){
16109 assert( pthreadMutexHeld(p) );
16110 #if SQLITE_MUTEX_NREF
16111 p->nRef--;
16112 #endif
16113 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
16114
16115 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
16116 if( p->nRef==0 ){
16117 pthread_mutex_unlock(&p->mutex);
@@ -15857,12 +16125,12 @@
16125 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16126 }
16127 #endif
16128 }
16129
16130 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16131 static const sqlite3_mutex_methods sMutex = {
16132 pthreadMutexInit,
16133 pthreadMutexEnd,
16134 pthreadMutexAlloc,
16135 pthreadMutexFree,
16136 pthreadMutexEnter,
@@ -15908,21 +16176,21 @@
16176 ** Each recursive mutex is an instance of the following structure.
16177 */
16178 struct sqlite3_mutex {
16179 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
16180 int id; /* Mutex type */
 
 
16181 #ifdef SQLITE_DEBUG
16182 volatile int nRef; /* Number of enterances */
16183 volatile DWORD owner; /* Thread holding this mutex */
16184 int trace; /* True to trace changes */
16185 #endif
16186 };
16187 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
16188 #ifdef SQLITE_DEBUG
16189 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
16190 #else
16191 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
16192 #endif
16193
16194 /*
16195 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
16196 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
@@ -16074,21 +16342,25 @@
16342 switch( iType ){
16343 case SQLITE_MUTEX_FAST:
16344 case SQLITE_MUTEX_RECURSIVE: {
16345 p = sqlite3MallocZero( sizeof(*p) );
16346 if( p ){
16347 #ifdef SQLITE_DEBUG
16348 p->id = iType;
16349 #endif
16350 InitializeCriticalSection(&p->mutex);
16351 }
16352 break;
16353 }
16354 default: {
16355 assert( winMutex_isInit==1 );
16356 assert( iType-2 >= 0 );
16357 assert( iType-2 < ArraySize(winMutex_staticMutexes) );
16358 p = &winMutex_staticMutexes[iType-2];
16359 #ifdef SQLITE_DEBUG
16360 p->id = iType;
16361 #endif
16362 break;
16363 }
16364 }
16365 return p;
16366 }
@@ -16117,16 +16389,18 @@
16389 ** mutex must be exited an equal number of times before another thread
16390 ** can enter. If the same thread tries to enter any other kind of mutex
16391 ** more than once, the behavior is undefined.
16392 */
16393 static void winMutexEnter(sqlite3_mutex *p){
16394 #ifdef SQLITE_DEBUG
16395 DWORD tid = GetCurrentThreadId();
16396 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
16397 #endif
16398 EnterCriticalSection(&p->mutex);
16399 #ifdef SQLITE_DEBUG
16400 p->owner = tid;
16401 p->nRef++;
 
16402 if( p->trace ){
16403 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16404 }
16405 #endif
16406 }
@@ -16171,25 +16445,25 @@
16445 ** is not currently allocated. SQLite will never do either.
16446 */
16447 static void winMutexLeave(sqlite3_mutex *p){
16448 #ifndef NDEBUG
16449 DWORD tid = GetCurrentThreadId();
 
16450 assert( p->nRef>0 );
16451 assert( p->owner==tid );
16452 p->nRef--;
16453 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
16454 #endif
16455 LeaveCriticalSection(&p->mutex);
16456 #ifdef SQLITE_DEBUG
16457 if( p->trace ){
16458 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
16459 }
16460 #endif
16461 }
16462
16463 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
16464 static const sqlite3_mutex_methods sMutex = {
16465 winMutexInit,
16466 winMutexEnd,
16467 winMutexAlloc,
16468 winMutexFree,
16469 winMutexEnter,
@@ -20557,30 +20831,30 @@
20831 /* 113 */ "IfPos",
20832 /* 114 */ "IfNeg",
20833 /* 115 */ "IfZero",
20834 /* 116 */ "AggStep",
20835 /* 117 */ "AggFinal",
20836 /* 118 */ "Checkpoint",
20837 /* 119 */ "JournalMode",
20838 /* 120 */ "Vacuum",
20839 /* 121 */ "IncrVacuum",
20840 /* 122 */ "Expire",
20841 /* 123 */ "TableLock",
20842 /* 124 */ "VBegin",
20843 /* 125 */ "VCreate",
20844 /* 126 */ "VDestroy",
20845 /* 127 */ "VOpen",
20846 /* 128 */ "VFilter",
20847 /* 129 */ "VColumn",
20848 /* 130 */ "Real",
20849 /* 131 */ "VNext",
20850 /* 132 */ "VRename",
20851 /* 133 */ "VUpdate",
20852 /* 134 */ "Pagecount",
20853 /* 135 */ "Trace",
20854 /* 136 */ "Noop",
20855 /* 137 */ "Explain",
20856 /* 138 */ "NotUsed_138",
20857 /* 139 */ "NotUsed_139",
20858 /* 140 */ "NotUsed_140",
20859 /* 141 */ "ToText",
20860 /* 142 */ "ToBlob",
@@ -20681,27 +20955,13 @@
20955 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
20956 #endif
20957
20958 #ifdef SQLITE_DEBUG
20959 SQLITE_PRIVATE int sqlite3OSTrace = 0;
20960 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
 
 
 
 
 
20961 #else
20962 #define OSTRACE(X)
 
 
 
 
 
 
20963 #endif
20964
20965 /*
20966 ** Macros for performance tracing. Normally turned off. Only works
20967 ** on i486 hardware.
@@ -20893,11 +21153,11 @@
21153 */
21154 static int os2Close( sqlite3_file *id ){
21155 APIRET rc = NO_ERROR;
21156 os2File *pFile;
21157 if( id && (pFile = (os2File*)id) != 0 ){
21158 OSTRACE(( "CLOSE %d\n", pFile->h ));
21159 rc = DosClose( pFile->h );
21160 pFile->locktype = NO_LOCK;
21161 if( pFile->pathToDel != NULL ){
21162 rc = DosForceDelete( (PSZ)pFile->pathToDel );
21163 free( pFile->pathToDel );
@@ -20924,11 +21184,11 @@
21184 ULONG fileLocation = 0L;
21185 ULONG got;
21186 os2File *pFile = (os2File*)id;
21187 assert( id!=0 );
21188 SimulateIOError( return SQLITE_IOERR_READ );
21189 OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
21190 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
21191 return SQLITE_IOERR;
21192 }
21193 if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
21194 return SQLITE_IOERR_READ;
@@ -20957,11 +21217,11 @@
21217 ULONG wrote;
21218 os2File *pFile = (os2File*)id;
21219 assert( id!=0 );
21220 SimulateIOError( return SQLITE_IOERR_WRITE );
21221 SimulateDiskfullError( return SQLITE_FULL );
21222 OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
21223 if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
21224 return SQLITE_IOERR;
21225 }
21226 assert( amt>0 );
21227 while( amt > 0 &&
@@ -20979,11 +21239,11 @@
21239 ** Truncate an open file to a specified size
21240 */
21241 static int os2Truncate( sqlite3_file *id, i64 nByte ){
21242 APIRET rc = NO_ERROR;
21243 os2File *pFile = (os2File*)id;
21244 OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
21245 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
21246 rc = DosSetFileSize( pFile->h, nByte );
21247 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
21248 }
21249
@@ -20999,11 +21259,11 @@
21259 /*
21260 ** Make sure all writes to a particular file are committed to disk.
21261 */
21262 static int os2Sync( sqlite3_file *id, int flags ){
21263 os2File *pFile = (os2File*)id;
21264 OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
21265 #ifdef SQLITE_TEST
21266 if( flags & SQLITE_SYNC_FULL){
21267 sqlite3_fullsync_count++;
21268 }
21269 sqlite3_sync_count++;
@@ -21049,11 +21309,11 @@
21309 LockArea.lOffset = SHARED_FIRST;
21310 LockArea.lRange = SHARED_SIZE;
21311 UnlockArea.lOffset = 0L;
21312 UnlockArea.lRange = 0L;
21313 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21314 OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
21315 return res;
21316 }
21317
21318 /*
21319 ** Undo a readlock
@@ -21067,11 +21327,11 @@
21327 LockArea.lOffset = 0L;
21328 LockArea.lRange = 0L;
21329 UnlockArea.lOffset = SHARED_FIRST;
21330 UnlockArea.lRange = SHARED_SIZE;
21331 res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
21332 OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
21333 return res;
21334 }
21335
21336 /*
21337 ** Lock the file with the lock specified by parameter locktype - one
@@ -21108,18 +21368,18 @@
21368 UnlockArea;
21369 os2File *pFile = (os2File*)id;
21370 memset(&LockArea, 0, sizeof(LockArea));
21371 memset(&UnlockArea, 0, sizeof(UnlockArea));
21372 assert( pFile!=0 );
21373 OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
21374
21375 /* If there is already a lock of this type or more restrictive on the
21376 ** os2File, do nothing. Don't use the end_lock: exit path, as
21377 ** sqlite3_mutex_enter() hasn't been called yet.
21378 */
21379 if( pFile->locktype>=locktype ){
21380 OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
21381 return SQLITE_OK;
21382 }
21383
21384 /* Make sure the locking sequence is correct
21385 */
@@ -21142,11 +21402,11 @@
21402
21403 /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
21404 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
21405 if( res == NO_ERROR ){
21406 gotPendingLock = 1;
21407 OSTRACE(( "LOCK %d pending lock boolean set. res=%d\n", pFile->h, res ));
21408 }
21409 }
21410
21411 /* Acquire a shared lock
21412 */
@@ -21154,11 +21414,11 @@
21414 assert( pFile->locktype==NO_LOCK );
21415 res = getReadLock(pFile);
21416 if( res == NO_ERROR ){
21417 newLocktype = SHARED_LOCK;
21418 }
21419 OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
21420 }
21421
21422 /* Acquire a RESERVED lock
21423 */
21424 if( locktype==RESERVED_LOCK && res == NO_ERROR ){
@@ -21169,39 +21429,40 @@
21429 UnlockArea.lRange = 0L;
21430 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21431 if( res == NO_ERROR ){
21432 newLocktype = RESERVED_LOCK;
21433 }
21434 OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
21435 }
21436
21437 /* Acquire a PENDING lock
21438 */
21439 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
21440 newLocktype = PENDING_LOCK;
21441 gotPendingLock = 0;
21442 OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
21443 pFile->h ));
21444 }
21445
21446 /* Acquire an EXCLUSIVE lock
21447 */
21448 if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
21449 assert( pFile->locktype>=SHARED_LOCK );
21450 res = unlockReadLock(pFile);
21451 OSTRACE(( "unreadlock = %d\n", res ));
21452 LockArea.lOffset = SHARED_FIRST;
21453 LockArea.lRange = SHARED_SIZE;
21454 UnlockArea.lOffset = 0L;
21455 UnlockArea.lRange = 0L;
21456 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21457 if( res == NO_ERROR ){
21458 newLocktype = EXCLUSIVE_LOCK;
21459 }else{
21460 OSTRACE(( "OS/2 error-code = %d\n", res ));
21461 getReadLock(pFile);
21462 }
21463 OSTRACE(( "LOCK %d acquire exclusive lock. res=%d\n", pFile->h, res ));
21464 }
21465
21466 /* If we are holding a PENDING lock that ought to be released, then
21467 ** release it now.
21468 */
@@ -21210,25 +21471,25 @@
21471 LockArea.lOffset = 0L;
21472 LockArea.lRange = 0L;
21473 UnlockArea.lOffset = PENDING_BYTE;
21474 UnlockArea.lRange = 1L;
21475 r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21476 OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
21477 }
21478
21479 /* Update the state of the lock has held in the file descriptor then
21480 ** return the appropriate result code.
21481 */
21482 if( res == NO_ERROR ){
21483 rc = SQLITE_OK;
21484 }else{
21485 OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
21486 locktype, newLocktype ));
21487 rc = SQLITE_BUSY;
21488 }
21489 pFile->locktype = newLocktype;
21490 OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
21491 return rc;
21492 }
21493
21494 /*
21495 ** This routine checks if there is a RESERVED lock held on the specified
@@ -21239,11 +21500,11 @@
21500 int r = 0;
21501 os2File *pFile = (os2File*)id;
21502 assert( pFile!=0 );
21503 if( pFile->locktype>=RESERVED_LOCK ){
21504 r = 1;
21505 OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
21506 }else{
21507 FILELOCK LockArea,
21508 UnlockArea;
21509 APIRET rc = NO_ERROR;
21510 memset(&LockArea, 0, sizeof(LockArea));
@@ -21251,22 +21512,22 @@
21512 LockArea.lOffset = RESERVED_BYTE;
21513 LockArea.lRange = 1L;
21514 UnlockArea.lOffset = 0L;
21515 UnlockArea.lRange = 0L;
21516 rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21517 OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
21518 if( rc == NO_ERROR ){
21519 APIRET rcu = NO_ERROR; /* return code for unlocking */
21520 LockArea.lOffset = 0L;
21521 LockArea.lRange = 0L;
21522 UnlockArea.lOffset = RESERVED_BYTE;
21523 UnlockArea.lRange = 1L;
21524 rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21525 OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
21526 }
21527 r = !(rc == NO_ERROR);
21528 OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
21529 }
21530 *pOut = r;
21531 return SQLITE_OK;
21532 }
21533
@@ -21290,48 +21551,49 @@
21551 UnlockArea;
21552 memset(&LockArea, 0, sizeof(LockArea));
21553 memset(&UnlockArea, 0, sizeof(UnlockArea));
21554 assert( pFile!=0 );
21555 assert( locktype<=SHARED_LOCK );
21556 OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
21557 type = pFile->locktype;
21558 if( type>=EXCLUSIVE_LOCK ){
21559 LockArea.lOffset = 0L;
21560 LockArea.lRange = 0L;
21561 UnlockArea.lOffset = SHARED_FIRST;
21562 UnlockArea.lRange = SHARED_SIZE;
21563 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21564 OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
21565 if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
21566 /* This should never happen. We should always be able to
21567 ** reacquire the read lock */
21568 OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
21569 rc = SQLITE_IOERR_UNLOCK;
21570 }
21571 }
21572 if( type>=RESERVED_LOCK ){
21573 LockArea.lOffset = 0L;
21574 LockArea.lRange = 0L;
21575 UnlockArea.lOffset = RESERVED_BYTE;
21576 UnlockArea.lRange = 1L;
21577 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21578 OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
21579 }
21580 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
21581 res = unlockReadLock(pFile);
21582 OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
21583 pFile->h, type, locktype, res ));
21584 }
21585 if( type>=PENDING_LOCK ){
21586 LockArea.lOffset = 0L;
21587 LockArea.lRange = 0L;
21588 UnlockArea.lOffset = PENDING_BYTE;
21589 UnlockArea.lRange = 1L;
21590 res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
21591 OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
21592 }
21593 pFile->locktype = locktype;
21594 OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
21595 return rc;
21596 }
21597
21598 /*
21599 ** Control and query of the open file handle.
@@ -21338,11 +21600,12 @@
21600 */
21601 static int os2FileControl(sqlite3_file *id, int op, void *pArg){
21602 switch( op ){
21603 case SQLITE_FCNTL_LOCKSTATE: {
21604 *(int*)pArg = ((os2File*)id)->locktype;
21605 OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
21606 ((os2File*)id)->h, ((os2File*)id)->locktype ));
21607 return SQLITE_OK;
21608 }
21609 }
21610 return SQLITE_ERROR;
21611 }
@@ -21525,11 +21788,11 @@
21788 sqlite3_randomness( 20, &zBuf[j] );
21789 for( i = 0; i < 20; i++, j++ ){
21790 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
21791 }
21792 zBuf[j] = 0;
21793 OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
21794 return SQLITE_OK;
21795 }
21796
21797
21798 /*
@@ -21588,47 +21851,47 @@
21851 }
21852
21853
21854 memset( pFile, 0, sizeof(*pFile) );
21855
21856 OSTRACE( "OPEN want %d\n", flags ));
21857
21858 if( flags & SQLITE_OPEN_READWRITE ){
21859 ulOpenMode |= OPEN_ACCESS_READWRITE;
21860 OSTRACE(( "OPEN read/write\n" ));
21861 }else{
21862 ulOpenMode |= OPEN_ACCESS_READONLY;
21863 OSTRACE(( "OPEN read only\n" ));
21864 }
21865
21866 if( flags & SQLITE_OPEN_CREATE ){
21867 ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
21868 OSTRACE(( "OPEN open new/create\n" ));
21869 }else{
21870 ulOpenFlags |= OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
21871 OSTRACE(( "OPEN open existing\n" ));
21872 }
21873
21874 if( flags & SQLITE_OPEN_MAIN_DB ){
21875 ulOpenMode |= OPEN_SHARE_DENYNONE;
21876 OSTRACE(( "OPEN share read/write\n" ));
21877 }else{
21878 ulOpenMode |= OPEN_SHARE_DENYWRITE;
21879 OSTRACE(( "OPEN share read only\n" ));
21880 }
21881
21882 if( flags & SQLITE_OPEN_DELETEONCLOSE ){
21883 char pathUtf8[CCHMAXPATH];
21884 #ifdef NDEBUG /* when debugging we want to make sure it is deleted */
21885 ulFileAttribute = FILE_HIDDEN;
21886 #endif
21887 os2FullPathname( pVfs, zName, CCHMAXPATH, pathUtf8 );
21888 pFile->pathToDel = convertUtf8PathToCp( pathUtf8 );
21889 OSTRACE(( "OPEN hidden/delete on close file attributes\n" ));
21890 }else{
21891 pFile->pathToDel = NULL;
21892 OSTRACE(( "OPEN normal file attribute\n" ));
21893 }
21894
21895 /* always open in random access mode for possibly better speed */
21896 ulOpenMode |= OPEN_FLAGS_RANDOM;
21897 ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR;
@@ -21643,17 +21906,18 @@
21906 ulOpenFlags,
21907 ulOpenMode,
21908 (PEAOP2)NULL );
21909 free( zNameCp );
21910 if( rc != NO_ERROR ){
21911 OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulAttr=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
21912 rc, zName, ulAction, ulFileAttribute, ulOpenFlags, ulOpenMode ));
21913 if( pFile->pathToDel )
21914 free( pFile->pathToDel );
21915 pFile->pathToDel = NULL;
21916 if( flags & SQLITE_OPEN_READWRITE ){
21917 OSTRACE(( "OPEN %d Invalid handle\n",
21918 ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE) ));
21919 return os2Open( pVfs, zName, id,
21920 ((flags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE),
21921 pOutFlags );
21922 }else{
21923 return SQLITE_CANTOPEN;
@@ -21665,11 +21929,11 @@
21929 }
21930
21931 pFile->pMethod = &os2IoMethod;
21932 pFile->h = h;
21933 OpenCounter(+1);
21934 OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
21935 return SQLITE_OK;
21936 }
21937
21938 /*
21939 ** Delete the named file.
@@ -21682,11 +21946,11 @@
21946 APIRET rc = NO_ERROR;
21947 char *zFilenameCp = convertUtf8PathToCp( zFilename );
21948 SimulateIOError( return SQLITE_IOERR_DELETE );
21949 rc = DosDelete( (PSZ)zFilenameCp );
21950 free( zFilenameCp );
21951 OSTRACE(( "DELETE \"%s\"\n", zFilename ));
21952 return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_DELETE;
21953 }
21954
21955 /*
21956 ** Check the existance and status of a file.
@@ -21703,21 +21967,21 @@
21967
21968 memset( &fsts3ConfigInfo, 0, sizeof(fsts3ConfigInfo) );
21969 rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
21970 &fsts3ConfigInfo, sizeof(FILESTATUS3) );
21971 free( zFilenameCp );
21972 OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
21973 fsts3ConfigInfo.attrFile, flags, rc ));
21974 switch( flags ){
21975 case SQLITE_ACCESS_READ:
21976 case SQLITE_ACCESS_EXISTS:
21977 rc = (rc == NO_ERROR);
21978 OSTRACE(( "ACCESS %s access of read and exists rc=%d\n", zFilename, rc));
21979 break;
21980 case SQLITE_ACCESS_READWRITE:
21981 rc = (rc == NO_ERROR) && ( (fsts3ConfigInfo.attrFile & FILE_READONLY) == 0 );
21982 OSTRACE(( "ACCESS %s access of read/write rc=%d\n", zFilename, rc ));
21983 break;
21984 default:
21985 assert( !"Invalid flags argument" );
21986 }
21987 *pOut = rc;
@@ -21923,11 +22187,11 @@
22187 os2DlSym, /* xDlSym */
22188 os2DlClose, /* xDlClose */
22189 os2Randomness, /* xRandomness */
22190 os2Sleep, /* xSleep */
22191 os2CurrentTime, /* xCurrentTime */
22192 os2GetLastError, /* xGetLastError */
22193 };
22194 sqlite3_vfs_register(&os2Vfs, 1);
22195 initUconvObjects();
22196 return SQLITE_OK;
22197 }
@@ -22057,10 +22321,11 @@
22321 #include <sys/stat.h>
22322 #include <fcntl.h>
22323 #include <unistd.h>
22324 #include <sys/time.h>
22325 #include <errno.h>
22326 #include <sys/mman.h>
22327
22328 #if SQLITE_ENABLE_LOCKING_STYLE
22329 # include <sys/ioctl.h>
22330 # if OS_VXWORKS
22331 # include <semaphore.h>
@@ -22111,18 +22376,22 @@
22376 ** Only set the lastErrno if the error code is a real error and not
22377 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
22378 */
22379 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
22380
22381 /* Forward references */
22382 typedef struct unixShm unixShm; /* Connection shared memory */
22383 typedef struct unixShmNode unixShmNode; /* Shared memory instance */
22384 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
22385 typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
22386
22387 /*
22388 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
22389 ** cannot be closed immediately. In these cases, instances of the following
22390 ** structure are used to store the file descriptor while waiting for an
22391 ** opportunity to either close or reuse it.
22392 */
 
22393 struct UnixUnusedFd {
22394 int fd; /* File descriptor to close */
22395 int flags; /* Flags this file descriptor was opened with */
22396 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
22397 };
@@ -22132,31 +22401,29 @@
22401 ** VFS implementations.
22402 */
22403 typedef struct unixFile unixFile;
22404 struct unixFile {
22405 sqlite3_io_methods const *pMethod; /* Always the first entry */
22406 unixInodeInfo *pInode; /* Info about locks on this inode */
22407 int h; /* The file descriptor */
22408 int dirfd; /* File descriptor for the directory */
22409 unsigned char eFileLock; /* The type of lock held on this fd */
22410 int lastErrno; /* The unix errno from last I/O error */
22411 void *lockingContext; /* Locking style specific state */
22412 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22413 int fileFlags; /* Miscellanous flags */
22414 const char *zPath; /* Name of the file */
22415 unixShm *pShm; /* Shared memory segment information */
22416 #if SQLITE_ENABLE_LOCKING_STYLE
22417 int openFlags; /* The flags specified at open() */
22418 #endif
22419 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
22420 unsigned fsFlags; /* cached details from statfs() */
 
 
 
22421 #endif
22422 #if OS_VXWORKS
22423 int isDelete; /* Delete on close if true */
22424 struct vxworksFileId *pId; /* Unique file ID */
22425 #endif
22426 #ifndef NDEBUG
22427 /* The next group of variables are used to track whether or not the
22428 ** transaction counter in bytes 24-27 of database files are updated
22429 ** whenever any part of the database changes. An assertion fault will
@@ -22217,27 +22484,13 @@
22484 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22485 #endif
22486
22487 #ifdef SQLITE_DEBUG
22488 SQLITE_PRIVATE int sqlite3OSTrace = 0;
22489 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
 
 
 
 
 
22490 #else
22491 #define OSTRACE(X)
 
 
 
 
 
 
22492 #endif
22493
22494 /*
22495 ** Macros for performance tracing. Normally turned off. Only works
22496 ** on i486 hardware.
@@ -22443,11 +22696,11 @@
22696 #endif
22697
22698
22699 /*
22700 ** Helper functions to obtain and relinquish the global mutex. The
22701 ** global mutex is used to protect the unixInodeInfo and
22702 ** vxworksFileId objects used by this file, all of which may be
22703 ** shared by multiple threads.
22704 **
22705 ** Function unixMutexHeld() is used to assert() that the global mutex
22706 ** is held when required. This function is only used as part of assert()
@@ -22474,12 +22727,12 @@
22727 /*
22728 ** Helper function for printing out trace information from debugging
22729 ** binaries. This returns the string represetation of the supplied
22730 ** integer lock-type.
22731 */
22732 static const char *azFileLock(int eFileLock){
22733 switch( eFileLock ){
22734 case NO_LOCK: return "NONE";
22735 case SHARED_LOCK: return "SHARED";
22736 case RESERVED_LOCK: return "RESERVED";
22737 case PENDING_LOCK: return "PENDING";
22738 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
@@ -22812,17 +23065,16 @@
23065 **
23066 ** But wait: there are yet more problems with POSIX advisory locks.
23067 **
23068 ** If you close a file descriptor that points to a file that has locks,
23069 ** all locks on that file that are owned by the current process are
23070 ** released. To work around this problem, each unixInodeInfo object
23071 ** maintains a count of the number of pending locks on tha inode.
23072 ** When an attempt is made to close an unixFile, if there are
 
23073 ** other unixFile open on the same inode that are holding locks, the call
23074 ** to close() the file descriptor is deferred until all of the locks clear.
23075 ** The unixInodeInfo structure keeps a list of file descriptors that need to
23076 ** be closed and that list is walked (and cleared) when the last lock
23077 ** clears.
23078 **
23079 ** Yet another problem: LinuxThreads do not play well with posix locks.
23080 **
@@ -22833,50 +23085,23 @@
23085 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
23086 ** on linux - with NPTL a lock created by thread A can override locks
23087 ** in thread B. But there is no way to know at compile-time which
23088 ** threading library is being used. So there is no way to know at
23089 ** compile-time whether or not thread A can override locks on thread B.
23090 ** One has to do a run-time check to discover the behavior of the
23091 ** current process.
23092 **
23093 ** SQLite used to support LinuxThreads. But support for LinuxThreads
23094 ** was dropped beginning with version 3.7.0. SQLite will still work with
23095 ** LinuxThreads provided that (1) there is no more than one connection
23096 ** per database file in the same process and (2) database connections
23097 ** do not move across threads.
23098 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23099
23100 /*
23101 ** An instance of the following structure serves as the key used
23102 ** to locate a particular unixInodeInfo object.
 
23103 */
23104 struct unixFileId {
23105 dev_t dev; /* Device number */
23106 #if OS_VXWORKS
23107 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
@@ -22883,260 +23108,122 @@
23108 #else
23109 ino_t ino; /* Inode number */
23110 #endif
23111 };
23112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23113 /*
23114 ** An instance of the following structure is allocated for each open
23115 ** inode. Or, on LinuxThreads, there is one of these structures for
23116 ** each inode opened by each thread.
23117 **
23118 ** A single inode can have multiple file descriptors, so each unixFile
23119 ** structure contains a pointer to an instance of this object and this
23120 ** object keeps a count of the number of unixFile pointing to it.
23121 */
23122 struct unixInodeInfo {
23123 struct unixFileId fileId; /* The lookup key */
23124 int nShared; /* Number of SHARED locks held */
23125 int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
23126 int nRef; /* Number of pointers to this structure */
23127 unixShmNode *pShmNode; /* Shared memory associated with this inode */
23128 int nLock; /* Number of outstanding file locks */
23129 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
23130 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
23131 unixInodeInfo *pPrev; /* .... doubly linked */
23132 #if defined(SQLITE_ENABLE_LOCKING_STYLE)
23133 unsigned long long sharedByte; /* for AFP simulated shared lock */
23134 #endif
23135 #if OS_VXWORKS
23136 sem_t *pSem; /* Named POSIX semaphore */
23137 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
23138 #endif
23139 };
23140
23141 /*
23142 ** A lists of all unixInodeInfo objects.
23143 */
23144 static unixInodeInfo *inodeList = 0;
23145
23146 /*
23147 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
23148 ** If all such file descriptors are closed without error, the list is
23149 ** cleared and SQLITE_OK returned.
23150 **
23151 ** Otherwise, if an error occurs, then successfully closed file descriptor
23152 ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned.
23153 ** not deleted and SQLITE_IOERR_CLOSE returned.
23154 */
23155 static int closePendingFds(unixFile *pFile){
23156 int rc = SQLITE_OK;
23157 unixInodeInfo *pInode = pFile->pInode;
23158 UnixUnusedFd *pError = 0;
23159 UnixUnusedFd *p;
23160 UnixUnusedFd *pNext;
23161 for(p=pInode->pUnused; p; p=pNext){
23162 pNext = p->pNext;
23163 if( close(p->fd) ){
23164 pFile->lastErrno = errno;
23165 rc = SQLITE_IOERR_CLOSE;
23166 p->pNext = pError;
23167 pError = p;
23168 }else{
23169 sqlite3_free(p);
23170 }
23171 }
23172 pInode->pUnused = pError;
23173 return rc;
23174 }
23175
23176 /*
23177 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
23178 **
23179 ** The mutex entered using the unixEnterMutex() function must be held
23180 ** when this function is called.
23181 */
23182 static void releaseInodeInfo(unixFile *pFile){
23183 unixInodeInfo *pInode = pFile->pInode;
23184 assert( unixMutexHeld() );
23185 if( pInode ){
23186 pInode->nRef--;
23187 if( pInode->nRef==0 ){
23188 assert( pInode->pShmNode==0 );
23189 closePendingFds(pFile);
23190 if( pInode->pPrev ){
23191 assert( pInode->pPrev->pNext==pInode );
23192 pInode->pPrev->pNext = pInode->pNext;
23193 }else{
23194 assert( inodeList==pInode );
23195 inodeList = pInode->pNext;
23196 }
23197 if( pInode->pNext ){
23198 assert( pInode->pNext->pPrev==pInode );
23199 pInode->pNext->pPrev = pInode->pPrev;
23200 }
23201 sqlite3_free(pInode);
23202 }
23203 }
23204 }
23205
23206 /*
23207 ** Given a file descriptor, locate the unixInodeInfo object that
23208 ** describes that file descriptor. Create a new one if necessary. The
23209 ** return value might be uninitialized if an error occurs.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23210 **
23211 ** The mutex entered using the unixEnterMutex() function must be held
23212 ** when this function is called.
23213 **
23214 ** Return an appropriate error code.
23215 */
23216 static int findInodeInfo(
23217 unixFile *pFile, /* Unix file with file desc used in the key */
23218 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
 
23219 ){
23220 int rc; /* System call return code */
23221 int fd; /* The file descriptor for pFile */
23222 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
 
23223 struct stat statbuf; /* Low-level file information */
23224 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
 
23225
23226 assert( unixMutexHeld() );
23227
23228 /* Get low-level information about the file that we can used to
23229 ** create a unique name for the file.
@@ -23174,126 +23261,39 @@
23261 return SQLITE_IOERR;
23262 }
23263 }
23264 #endif
23265
23266 memset(&fileId, 0, sizeof(fileId));
23267 fileId.dev = statbuf.st_dev;
23268 #if OS_VXWORKS
23269 fileId.pId = pFile->pId;
23270 #else
23271 fileId.ino = statbuf.st_ino;
23272 #endif
23273 pInode = inodeList;
23274 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
23275 pInode = pInode->pNext;
23276 }
23277 if( pInode==0 ){
23278 pInode = sqlite3_malloc( sizeof(*pInode) );
23279 if( pInode==0 ){
23280 return SQLITE_NOMEM;
23281 }
23282 memset(pInode, 0, sizeof(*pInode));
23283 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
23284 pInode->nRef = 1;
23285 pInode->pNext = inodeList;
23286 pInode->pPrev = 0;
23287 if( inodeList ) inodeList->pPrev = pInode;
23288 inodeList = pInode;
23289 }else{
23290 pInode->nRef++;
23291 }
23292 *ppInode = pInode;
23293 return SQLITE_OK;
23294 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23295
23296
23297 /*
23298 ** This routine checks if there is a RESERVED lock held on the specified
23299 ** file by this or any other process. If such a lock is held, set *pResOut
@@ -23306,14 +23306,14 @@
23306 unixFile *pFile = (unixFile*)id;
23307
23308 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
23309
23310 assert( pFile );
23311 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
23312
23313 /* Check if a thread in this process holds such a lock */
23314 if( pFile->pInode->eFileLock>SHARED_LOCK ){
23315 reserved = 1;
23316 }
23317
23318 /* Otherwise see if some other process holds it.
23319 */
@@ -23333,18 +23333,18 @@
23333 }
23334 }
23335 #endif
23336
23337 unixLeaveMutex();
23338 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
23339
23340 *pResOut = reserved;
23341 return rc;
23342 }
23343
23344 /*
23345 ** Lock the file with the lock specified by parameter eFileLock - one
23346 ** of the following:
23347 **
23348 ** (1) SHARED_LOCK
23349 ** (2) RESERVED_LOCK
23350 ** (3) PENDING_LOCK
@@ -23363,11 +23363,11 @@
23363 ** PENDING -> EXCLUSIVE
23364 **
23365 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
23366 ** routine to lower a locking level.
23367 */
23368 static int unixLock(sqlite3_file *id, int eFileLock){
23369 /* The following describes the implementation of the various locks and
23370 ** lock transitions in terms of the POSIX advisory shared and exclusive
23371 ** lock primitives (called read-locks and write-locks below, to avoid
23372 ** confusion with SQLite lock names). The algorithms are complicated
23373 ** slightly in order to be compatible with windows systems simultaneously
@@ -23404,74 +23404,66 @@
23404 ** locking a random byte from a range, concurrent SHARED locks may exist
23405 ** even if the locking primitive used is always a write-lock.
23406 */
23407 int rc = SQLITE_OK;
23408 unixFile *pFile = (unixFile*)id;
23409 unixInodeInfo *pInode = pFile->pInode;
23410 struct flock lock;
23411 int s = 0;
23412 int tErrno = 0;
23413
23414 assert( pFile );
23415 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
23416 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
23417 azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
23418
23419 /* If there is already a lock of this type or more restrictive on the
23420 ** unixFile, do nothing. Don't use the end_lock: exit path, as
23421 ** unixEnterMutex() hasn't been called yet.
23422 */
23423 if( pFile->eFileLock>=eFileLock ){
23424 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
23425 azFileLock(eFileLock)));
23426 return SQLITE_OK;
23427 }
23428
23429 /* Make sure the locking sequence is correct.
23430 ** (1) We never move from unlocked to anything higher than shared lock.
23431 ** (2) SQLite never explicitly requests a pendig lock.
23432 ** (3) A shared lock is always held when a reserve lock is requested.
23433 */
23434 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
23435 assert( eFileLock!=PENDING_LOCK );
23436 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
23437
23438 /* This mutex is needed because pFile->pInode is shared across threads
23439 */
23440 unixEnterMutex();
23441 pInode = pFile->pInode;
 
 
 
 
 
 
 
 
23442
23443 /* If some thread using this PID has a lock via a different unixFile*
23444 ** handle that precludes the requested lock, return BUSY.
23445 */
23446 if( (pFile->eFileLock!=pInode->eFileLock &&
23447 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
23448 ){
23449 rc = SQLITE_BUSY;
23450 goto end_lock;
23451 }
23452
23453 /* If a SHARED lock is requested, and some thread using this PID already
23454 ** has a SHARED or RESERVED lock, then increment reference counts and
23455 ** return SQLITE_OK.
23456 */
23457 if( eFileLock==SHARED_LOCK &&
23458 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
23459 assert( eFileLock==SHARED_LOCK );
23460 assert( pFile->eFileLock==0 );
23461 assert( pInode->nShared>0 );
23462 pFile->eFileLock = SHARED_LOCK;
23463 pInode->nShared++;
23464 pInode->nLock++;
23465 goto end_lock;
23466 }
23467
23468
23469 /* A PENDING lock is needed before acquiring a SHARED lock and before
@@ -23478,14 +23470,14 @@
23470 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
23471 ** be released.
23472 */
23473 lock.l_len = 1L;
23474 lock.l_whence = SEEK_SET;
23475 if( eFileLock==SHARED_LOCK
23476 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
23477 ){
23478 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
23479 lock.l_start = PENDING_BYTE;
23480 s = fcntl(pFile->h, F_SETLK, &lock);
23481 if( s==(-1) ){
23482 tErrno = errno;
23483 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -23498,13 +23490,13 @@
23490
23491
23492 /* If control gets to this point, then actually go ahead and make
23493 ** operating system calls for the specified lock.
23494 */
23495 if( eFileLock==SHARED_LOCK ){
23496 assert( pInode->nShared==0 );
23497 assert( pInode->eFileLock==0 );
23498
23499 /* Now get the read-lock */
23500 lock.l_start = SHARED_FIRST;
23501 lock.l_len = SHARED_SIZE;
23502 if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){
@@ -23529,26 +23521,26 @@
23521 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
23522 if( IS_LOCK_ERROR(rc) ){
23523 pFile->lastErrno = tErrno;
23524 }
23525 }else{
23526 pFile->eFileLock = SHARED_LOCK;
23527 pInode->nLock++;
23528 pInode->nShared = 1;
23529 }
23530 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
23531 /* We are trying for an exclusive lock but another thread in this
23532 ** same process is still holding a shared lock. */
23533 rc = SQLITE_BUSY;
23534 }else{
23535 /* The request was for a RESERVED or EXCLUSIVE lock. It is
23536 ** assumed that there is a SHARED or greater lock on the file
23537 ** already.
23538 */
23539 assert( 0!=pFile->eFileLock );
23540 lock.l_type = F_WRLCK;
23541 switch( eFileLock ){
23542 case RESERVED_LOCK:
23543 lock.l_start = RESERVED_BYTE;
23544 break;
23545 case EXCLUSIVE_LOCK:
23546 lock.l_start = SHARED_FIRST;
@@ -23573,80 +23565,50 @@
23565 ** transitioning from a SHARED to a RESERVED lock. The change
23566 ** from SHARED to RESERVED marks the beginning of a normal
23567 ** write operation (not a hot journal rollback).
23568 */
23569 if( rc==SQLITE_OK
23570 && pFile->eFileLock<=SHARED_LOCK
23571 && eFileLock==RESERVED_LOCK
23572 ){
23573 pFile->transCntrChng = 0;
23574 pFile->dbUpdate = 0;
23575 pFile->inNormalWrite = 1;
23576 }
23577 #endif
23578
23579
23580 if( rc==SQLITE_OK ){
23581 pFile->eFileLock = eFileLock;
23582 pInode->eFileLock = eFileLock;
23583 }else if( eFileLock==EXCLUSIVE_LOCK ){
23584 pFile->eFileLock = PENDING_LOCK;
23585 pInode->eFileLock = PENDING_LOCK;
23586 }
23587
23588 end_lock:
23589 unixLeaveMutex();
23590 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
23591 rc==SQLITE_OK ? "ok" : "failed"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23592 return rc;
23593 }
23594
23595 /*
23596 ** Add the file descriptor used by file handle pFile to the corresponding
23597 ** pUnused list.
23598 */
23599 static void setPendingFd(unixFile *pFile){
23600 unixInodeInfo *pInode = pFile->pInode;
23601 UnixUnusedFd *p = pFile->pUnused;
23602 p->pNext = pInode->pUnused;
23603 pInode->pUnused = p;
23604 pFile->h = -1;
23605 pFile->pUnused = 0;
23606 }
23607
23608 /*
23609 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
23610 ** must be either NO_LOCK or SHARED_LOCK.
23611 **
23612 ** If the locking level of the file descriptor is already at or below
23613 ** the requested locking level, this routine is a no-op.
23614 **
@@ -23654,35 +23616,33 @@
23616 ** the byte range is divided into 2 parts and the first part is unlocked then
23617 ** set to a read lock, then the other part is simply unlocked. This works
23618 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
23619 ** remove the write lock on a region when a read lock is set.
23620 */
23621 static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
23622 unixFile *pFile = (unixFile*)id;
23623 unixInodeInfo *pInode;
23624 struct flock lock;
23625 int rc = SQLITE_OK;
23626 int h;
23627 int tErrno; /* Error code from system call errors */
23628
23629 assert( pFile );
23630 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
23631 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
23632 getpid()));
23633
23634 assert( eFileLock<=SHARED_LOCK );
23635 if( pFile->eFileLock<=eFileLock ){
23636 return SQLITE_OK;
 
 
23637 }
23638 unixEnterMutex();
23639 h = pFile->h;
23640 pInode = pFile->pInode;
23641 assert( pInode->nShared!=0 );
23642 if( pFile->eFileLock>SHARED_LOCK ){
23643 assert( pInode->eFileLock==pFile->eFileLock );
23644 SimulateIOErrorBenign(1);
23645 SimulateIOError( h=(-1) )
23646 SimulateIOErrorBenign(0);
23647
23648 #ifndef NDEBUG
@@ -23692,13 +23652,15 @@
23652 ** file changed. If the transaction counter is not updated,
23653 ** other connections to the same file might not realize that
23654 ** the file has changed and hence might not know to flush their
23655 ** cache. The use of a stale cache can lead to database corruption.
23656 */
23657 #if 0
23658 assert( pFile->inNormalWrite==0
23659 || pFile->dbUpdate==0
23660 || pFile->transCntrChng==1 );
23661 #endif
23662 pFile->inNormalWrite = 0;
23663 #endif
23664
23665 /* downgrading to a shared lock on NFS involves clearing the write lock
23666 ** before establishing the readlock - to avoid a race condition we downgrade
@@ -23707,11 +23669,11 @@
23669 ** 1: [WWWWW]
23670 ** 2: [....W]
23671 ** 3: [RRRRW]
23672 ** 4: [RRRR.]
23673 */
23674 if( eFileLock==SHARED_LOCK ){
23675 if( handleNFSUnlock ){
23676 off_t divSize = SHARED_SIZE - 1;
23677
23678 lock.l_type = F_UNLCK;
23679 lock.l_whence = SEEK_SET;
@@ -23767,78 +23729,75 @@
23729 lock.l_type = F_UNLCK;
23730 lock.l_whence = SEEK_SET;
23731 lock.l_start = PENDING_BYTE;
23732 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
23733 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23734 pInode->eFileLock = SHARED_LOCK;
23735 }else{
23736 tErrno = errno;
23737 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
23738 if( IS_LOCK_ERROR(rc) ){
23739 pFile->lastErrno = tErrno;
23740 }
23741 goto end_unlock;
23742 }
23743 }
23744 if( eFileLock==NO_LOCK ){
 
 
23745 /* Decrement the shared lock counter. Release the lock using an
23746 ** OS call only when all threads in this same process have released
23747 ** the lock.
23748 */
23749 pInode->nShared--;
23750 if( pInode->nShared==0 ){
23751 lock.l_type = F_UNLCK;
23752 lock.l_whence = SEEK_SET;
23753 lock.l_start = lock.l_len = 0L;
23754 SimulateIOErrorBenign(1);
23755 SimulateIOError( h=(-1) )
23756 SimulateIOErrorBenign(0);
23757 if( fcntl(h, F_SETLK, &lock)!=(-1) ){
23758 pInode->eFileLock = NO_LOCK;
23759 }else{
23760 tErrno = errno;
23761 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
23762 if( IS_LOCK_ERROR(rc) ){
23763 pFile->lastErrno = tErrno;
23764 }
23765 pInode->eFileLock = NO_LOCK;
23766 pFile->eFileLock = NO_LOCK;
23767 }
23768 }
23769
23770 /* Decrement the count of locks against this same file. When the
23771 ** count reaches zero, close any other file descriptors whose close
23772 ** was deferred because of outstanding locks.
23773 */
23774 pInode->nLock--;
23775 assert( pInode->nLock>=0 );
23776 if( pInode->nLock==0 ){
 
23777 int rc2 = closePendingFds(pFile);
23778 if( rc==SQLITE_OK ){
23779 rc = rc2;
23780 }
23781 }
23782 }
23783
23784 end_unlock:
23785 unixLeaveMutex();
23786 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
23787 return rc;
23788 }
23789
23790 /*
23791 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
23792 ** must be either NO_LOCK or SHARED_LOCK.
23793 **
23794 ** If the locking level of the file descriptor is already at or below
23795 ** the requested locking level, this routine is a no-op.
23796 */
23797 static int unixUnlock(sqlite3_file *id, int eFileLock){
23798 return _posixUnlock(id, eFileLock, 0);
23799 }
23800
23801 /*
23802 ** This function performs the parts of the "close file" operation
23803 ** common to all locking schemes. It closes the directory and file
@@ -23875,11 +23834,11 @@
23834 }
23835 vxworksReleaseFileId(pFile->pId);
23836 pFile->pId = 0;
23837 }
23838 #endif
23839 OSTRACE(("CLOSE %-3d\n", pFile->h));
23840 OpenCounter(-1);
23841 sqlite3_free(pFile->pUnused);
23842 memset(pFile, 0, sizeof(unixFile));
23843 }
23844 return SQLITE_OK;
@@ -23892,20 +23851,19 @@
23851 int rc = SQLITE_OK;
23852 if( id ){
23853 unixFile *pFile = (unixFile *)id;
23854 unixUnlock(id, NO_LOCK);
23855 unixEnterMutex();
23856 if( pFile->pInode && pFile->pInode->nLock ){
23857 /* If there are outstanding locks, do not actually close the file just
23858 ** yet because that would clear those locks. Instead, add the file
23859 ** descriptor to pInode->pUnused list. It will be automatically closed
23860 ** when the last lock is cleared.
23861 */
23862 setPendingFd(pFile);
23863 }
23864 releaseInodeInfo(pFile);
 
23865 rc = closeUnixFile(id);
23866 unixLeaveMutex();
23867 }
23868 return rc;
23869 }
@@ -24000,26 +23958,26 @@
23958 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
23959
23960 assert( pFile );
23961
23962 /* Check if a thread in this process holds such a lock */
23963 if( pFile->eFileLock>SHARED_LOCK ){
23964 /* Either this connection or some other connection in the same process
23965 ** holds a lock on the file. No need to check further. */
23966 reserved = 1;
23967 }else{
23968 /* The lock is held if and only if the lockfile exists */
23969 const char *zLockFile = (const char*)pFile->lockingContext;
23970 reserved = access(zLockFile, 0)==0;
23971 }
23972 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
23973 *pResOut = reserved;
23974 return rc;
23975 }
23976
23977 /*
23978 ** Lock the file with the lock specified by parameter eFileLock - one
23979 ** of the following:
23980 **
23981 ** (1) SHARED_LOCK
23982 ** (2) RESERVED_LOCK
23983 ** (3) PENDING_LOCK
@@ -24041,22 +23999,22 @@
23999 ** routine to lower a locking level.
24000 **
24001 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
24002 ** But we track the other locking levels internally.
24003 */
24004 static int dotlockLock(sqlite3_file *id, int eFileLock) {
24005 unixFile *pFile = (unixFile*)id;
24006 int fd;
24007 char *zLockFile = (char *)pFile->lockingContext;
24008 int rc = SQLITE_OK;
24009
24010
24011 /* If we have any lock, then the lock file already exists. All we have
24012 ** to do is adjust our internal record of the lock level.
24013 */
24014 if( pFile->eFileLock > NO_LOCK ){
24015 pFile->eFileLock = eFileLock;
24016 #if !OS_VXWORKS
24017 /* Always update the timestamp on the old file */
24018 utimes(zLockFile, NULL);
24019 #endif
24020 return SQLITE_OK;
@@ -24081,47 +24039,47 @@
24039 pFile->lastErrno = errno;
24040 rc = SQLITE_IOERR_CLOSE;
24041 }
24042
24043 /* got it, set the type and return ok */
24044 pFile->eFileLock = eFileLock;
24045 return rc;
24046 }
24047
24048 /*
24049 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
24050 ** must be either NO_LOCK or SHARED_LOCK.
24051 **
24052 ** If the locking level of the file descriptor is already at or below
24053 ** the requested locking level, this routine is a no-op.
24054 **
24055 ** When the locking level reaches NO_LOCK, delete the lock file.
24056 */
24057 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
24058 unixFile *pFile = (unixFile*)id;
24059 char *zLockFile = (char *)pFile->lockingContext;
24060
24061 assert( pFile );
24062 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
24063 pFile->eFileLock, getpid()));
24064 assert( eFileLock<=SHARED_LOCK );
24065
24066 /* no-op if possible */
24067 if( pFile->eFileLock==eFileLock ){
24068 return SQLITE_OK;
24069 }
24070
24071 /* To downgrade to shared, simply update our internal notion of the
24072 ** lock state. No need to mess with the file on disk.
24073 */
24074 if( eFileLock==SHARED_LOCK ){
24075 pFile->eFileLock = SHARED_LOCK;
24076 return SQLITE_OK;
24077 }
24078
24079 /* To fully unlock the database, delete the lock file */
24080 assert( eFileLock==NO_LOCK );
24081 if( unlink(zLockFile) ){
24082 int rc = 0;
24083 int tErrno = errno;
24084 if( ENOENT != tErrno ){
24085 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
@@ -24129,11 +24087,11 @@
24087 if( IS_LOCK_ERROR(rc) ){
24088 pFile->lastErrno = tErrno;
24089 }
24090 return rc;
24091 }
24092 pFile->eFileLock = NO_LOCK;
24093 return SQLITE_OK;
24094 }
24095
24096 /*
24097 ** Close a file. Make sure the lock has been released before closing.
@@ -24182,11 +24140,11 @@
24140 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24141
24142 assert( pFile );
24143
24144 /* Check if a thread in this process holds such a lock */
24145 if( pFile->eFileLock>SHARED_LOCK ){
24146 reserved = 1;
24147 }
24148
24149 /* Otherwise see if some other process holds it. */
24150 if( !reserved ){
@@ -24213,11 +24171,11 @@
24171 pFile->lastErrno = tErrno;
24172 rc = lrc;
24173 }
24174 }
24175 }
24176 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
24177
24178 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
24179 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
24180 rc = SQLITE_OK;
24181 reserved=1;
@@ -24226,11 +24184,11 @@
24184 *pResOut = reserved;
24185 return rc;
24186 }
24187
24188 /*
24189 ** Lock the file with the lock specified by parameter eFileLock - one
24190 ** of the following:
24191 **
24192 ** (1) SHARED_LOCK
24193 ** (2) RESERVED_LOCK
24194 ** (3) PENDING_LOCK
@@ -24254,20 +24212,20 @@
24212 ** access the file.
24213 **
24214 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24215 ** routine to lower a locking level.
24216 */
24217 static int flockLock(sqlite3_file *id, int eFileLock) {
24218 int rc = SQLITE_OK;
24219 unixFile *pFile = (unixFile*)id;
24220
24221 assert( pFile );
24222
24223 /* if we already have a lock, it is exclusive.
24224 ** Just adjust level and punt on outta here. */
24225 if (pFile->eFileLock > NO_LOCK) {
24226 pFile->eFileLock = eFileLock;
24227 return SQLITE_OK;
24228 }
24229
24230 /* grab an exclusive lock */
24231
@@ -24278,14 +24236,14 @@
24236 if( IS_LOCK_ERROR(rc) ){
24237 pFile->lastErrno = tErrno;
24238 }
24239 } else {
24240 /* got it, set the type and return ok */
24241 pFile->eFileLock = eFileLock;
24242 }
24243 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
24244 rc==SQLITE_OK ? "ok" : "failed"));
24245 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
24246 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
24247 rc = SQLITE_BUSY;
24248 }
24249 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
@@ -24292,32 +24250,32 @@
24250 return rc;
24251 }
24252
24253
24254 /*
24255 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
24256 ** must be either NO_LOCK or SHARED_LOCK.
24257 **
24258 ** If the locking level of the file descriptor is already at or below
24259 ** the requested locking level, this routine is a no-op.
24260 */
24261 static int flockUnlock(sqlite3_file *id, int eFileLock) {
24262 unixFile *pFile = (unixFile*)id;
24263
24264 assert( pFile );
24265 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
24266 pFile->eFileLock, getpid()));
24267 assert( eFileLock<=SHARED_LOCK );
24268
24269 /* no-op if possible */
24270 if( pFile->eFileLock==eFileLock ){
24271 return SQLITE_OK;
24272 }
24273
24274 /* shared can just be set because we always have an exclusive */
24275 if (eFileLock==SHARED_LOCK) {
24276 pFile->eFileLock = eFileLock;
24277 return SQLITE_OK;
24278 }
24279
24280 /* no, really, unlock. */
24281 int rc = flock(pFile->h, LOCK_UN);
@@ -24333,11 +24291,11 @@
24291 }
24292 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
24293
24294 return r;
24295 } else {
24296 pFile->eFileLock = NO_LOCK;
24297 return SQLITE_OK;
24298 }
24299 }
24300
24301 /*
@@ -24381,41 +24339,41 @@
24339 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24340
24341 assert( pFile );
24342
24343 /* Check if a thread in this process holds such a lock */
24344 if( pFile->eFileLock>SHARED_LOCK ){
24345 reserved = 1;
24346 }
24347
24348 /* Otherwise see if some other process holds it. */
24349 if( !reserved ){
24350 sem_t *pSem = pFile->pInode->pSem;
24351 struct stat statBuf;
24352
24353 if( sem_trywait(pSem)==-1 ){
24354 int tErrno = errno;
24355 if( EAGAIN != tErrno ){
24356 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
24357 pFile->lastErrno = tErrno;
24358 } else {
24359 /* someone else has the lock when we are in NO_LOCK */
24360 reserved = (pFile->eFileLock < SHARED_LOCK);
24361 }
24362 }else{
24363 /* we could have it if we want it */
24364 sem_post(pSem);
24365 }
24366 }
24367 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
24368
24369 *pResOut = reserved;
24370 return rc;
24371 }
24372
24373 /*
24374 ** Lock the file with the lock specified by parameter eFileLock - one
24375 ** of the following:
24376 **
24377 ** (1) SHARED_LOCK
24378 ** (2) RESERVED_LOCK
24379 ** (3) PENDING_LOCK
@@ -24439,20 +24397,20 @@
24397 ** access the file.
24398 **
24399 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24400 ** routine to lower a locking level.
24401 */
24402 static int semLock(sqlite3_file *id, int eFileLock) {
24403 unixFile *pFile = (unixFile*)id;
24404 int fd;
24405 sem_t *pSem = pFile->pInode->pSem;
24406 int rc = SQLITE_OK;
24407
24408 /* if we already have a lock, it is exclusive.
24409 ** Just adjust level and punt on outta here. */
24410 if (pFile->eFileLock > NO_LOCK) {
24411 pFile->eFileLock = eFileLock;
24412 rc = SQLITE_OK;
24413 goto sem_end_lock;
24414 }
24415
24416 /* lock semaphore now but bail out when already locked. */
@@ -24460,41 +24418,41 @@
24418 rc = SQLITE_BUSY;
24419 goto sem_end_lock;
24420 }
24421
24422 /* got it, set the type and return ok */
24423 pFile->eFileLock = eFileLock;
24424
24425 sem_end_lock:
24426 return rc;
24427 }
24428
24429 /*
24430 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
24431 ** must be either NO_LOCK or SHARED_LOCK.
24432 **
24433 ** If the locking level of the file descriptor is already at or below
24434 ** the requested locking level, this routine is a no-op.
24435 */
24436 static int semUnlock(sqlite3_file *id, int eFileLock) {
24437 unixFile *pFile = (unixFile*)id;
24438 sem_t *pSem = pFile->pInode->pSem;
24439
24440 assert( pFile );
24441 assert( pSem );
24442 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
24443 pFile->eFileLock, getpid()));
24444 assert( eFileLock<=SHARED_LOCK );
24445
24446 /* no-op if possible */
24447 if( pFile->eFileLock==eFileLock ){
24448 return SQLITE_OK;
24449 }
24450
24451 /* shared can just be set because we always have an exclusive */
24452 if (eFileLock==SHARED_LOCK) {
24453 pFile->eFileLock = eFileLock;
24454 return SQLITE_OK;
24455 }
24456
24457 /* no, really unlock. */
24458 if ( sem_post(pSem)==-1 ) {
@@ -24503,11 +24461,11 @@
24461 if( IS_LOCK_ERROR(rc) ){
24462 pFile->lastErrno = tErrno;
24463 }
24464 return rc;
24465 }
24466 pFile->eFileLock = NO_LOCK;
24467 return SQLITE_OK;
24468 }
24469
24470 /*
24471 ** Close a file.
@@ -24516,12 +24474,11 @@
24474 if( id ){
24475 unixFile *pFile = (unixFile*)id;
24476 semUnlock(id, NO_LOCK);
24477 assert( pFile );
24478 unixEnterMutex();
24479 releaseInodeInfo(pFile);
 
24480 unixLeaveMutex();
24481 closeUnixFile(id);
24482 }
24483 return SQLITE_OK;
24484 }
@@ -24586,19 +24543,19 @@
24543 pb.startEndFlag = 0;
24544 pb.offset = offset;
24545 pb.length = length;
24546 pb.fd = pFile->h;
24547
24548 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
24549 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
24550 offset, length));
24551 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
24552 if ( err==-1 ) {
24553 int rc;
24554 int tErrno = errno;
24555 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
24556 path, tErrno, strerror(tErrno)));
24557 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
24558 rc = SQLITE_BUSY;
24559 #else
24560 rc = sqliteErrorFromPosixError(tErrno,
24561 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
@@ -24629,14 +24586,14 @@
24586 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24587 if( context->reserved ){
24588 *pResOut = 1;
24589 return SQLITE_OK;
24590 }
24591 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
24592
24593 /* Check if a thread in this process holds such a lock */
24594 if( pFile->pInode->eFileLock>SHARED_LOCK ){
24595 reserved = 1;
24596 }
24597
24598 /* Otherwise see if some other process holds it.
24599 */
@@ -24655,18 +24612,18 @@
24612 rc=lrc;
24613 }
24614 }
24615
24616 unixLeaveMutex();
24617 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
24618
24619 *pResOut = reserved;
24620 return rc;
24621 }
24622
24623 /*
24624 ** Lock the file with the lock specified by parameter eFileLock - one
24625 ** of the following:
24626 **
24627 ** (1) SHARED_LOCK
24628 ** (2) RESERVED_LOCK
24629 ** (3) PENDING_LOCK
@@ -24685,84 +24642,76 @@
24642 ** PENDING -> EXCLUSIVE
24643 **
24644 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
24645 ** routine to lower a locking level.
24646 */
24647 static int afpLock(sqlite3_file *id, int eFileLock){
24648 int rc = SQLITE_OK;
24649 unixFile *pFile = (unixFile*)id;
24650 unixInodeInfo *pInode = pFile->pInode;
24651 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24652
24653 assert( pFile );
24654 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
24655 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
24656 azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
24657
24658 /* If there is already a lock of this type or more restrictive on the
24659 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
24660 ** unixEnterMutex() hasn't been called yet.
24661 */
24662 if( pFile->eFileLock>=eFileLock ){
24663 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
24664 azFileLock(eFileLock)));
24665 return SQLITE_OK;
24666 }
24667
24668 /* Make sure the locking sequence is correct
24669 ** (1) We never move from unlocked to anything higher than shared lock.
24670 ** (2) SQLite never explicitly requests a pendig lock.
24671 ** (3) A shared lock is always held when a reserve lock is requested.
24672 */
24673 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
24674 assert( eFileLock!=PENDING_LOCK );
24675 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
24676
24677 /* This mutex is needed because pFile->pInode is shared across threads
24678 */
24679 unixEnterMutex();
24680 pInode = pFile->pInode;
 
 
 
 
 
 
 
 
24681
24682 /* If some thread using this PID has a lock via a different unixFile*
24683 ** handle that precludes the requested lock, return BUSY.
24684 */
24685 if( (pFile->eFileLock!=pInode->eFileLock &&
24686 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
24687 ){
24688 rc = SQLITE_BUSY;
24689 goto afp_end_lock;
24690 }
24691
24692 /* If a SHARED lock is requested, and some thread using this PID already
24693 ** has a SHARED or RESERVED lock, then increment reference counts and
24694 ** return SQLITE_OK.
24695 */
24696 if( eFileLock==SHARED_LOCK &&
24697 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
24698 assert( eFileLock==SHARED_LOCK );
24699 assert( pFile->eFileLock==0 );
24700 assert( pInode->nShared>0 );
24701 pFile->eFileLock = SHARED_LOCK;
24702 pInode->nShared++;
24703 pInode->nLock++;
24704 goto afp_end_lock;
24705 }
24706
24707 /* A PENDING lock is needed before acquiring a SHARED lock and before
24708 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
24709 ** be released.
24710 */
24711 if( eFileLock==SHARED_LOCK
24712 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
24713 ){
24714 int failed;
24715 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
24716 if (failed) {
24717 rc = failed;
@@ -24771,24 +24720,24 @@
24720 }
24721
24722 /* If control gets to this point, then actually go ahead and make
24723 ** operating system calls for the specified lock.
24724 */
24725 if( eFileLock==SHARED_LOCK ){
24726 int lrc1, lrc2, lrc1Errno;
24727 long lk, mask;
24728
24729 assert( pInode->nShared==0 );
24730 assert( pInode->eFileLock==0 );
24731
24732 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
24733 /* Now get the read-lock SHARED_LOCK */
24734 /* note that the quality of the randomness doesn't matter that much */
24735 lk = random();
24736 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
24737 lrc1 = afpSetLock(context->dbPath, pFile,
24738 SHARED_FIRST+pInode->sharedByte, 1, 1);
24739 if( IS_LOCK_ERROR(lrc1) ){
24740 lrc1Errno = pFile->lastErrno;
24741 }
24742 /* Drop the temporary PENDING lock */
24743 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
@@ -24801,46 +24750,46 @@
24750 rc = lrc2;
24751 goto afp_end_lock;
24752 } else if( lrc1 != SQLITE_OK ) {
24753 rc = lrc1;
24754 } else {
24755 pFile->eFileLock = SHARED_LOCK;
24756 pInode->nLock++;
24757 pInode->nShared = 1;
24758 }
24759 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
24760 /* We are trying for an exclusive lock but another thread in this
24761 ** same process is still holding a shared lock. */
24762 rc = SQLITE_BUSY;
24763 }else{
24764 /* The request was for a RESERVED or EXCLUSIVE lock. It is
24765 ** assumed that there is a SHARED or greater lock on the file
24766 ** already.
24767 */
24768 int failed = 0;
24769 assert( 0!=pFile->eFileLock );
24770 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
24771 /* Acquire a RESERVED lock */
24772 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
24773 if( !failed ){
24774 context->reserved = 1;
24775 }
24776 }
24777 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
24778 /* Acquire an EXCLUSIVE lock */
24779
24780 /* Remove the shared lock before trying the range. we'll need to
24781 ** reestablish the shared lock if we can't get the afpUnlock
24782 */
24783 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
24784 pInode->sharedByte, 1, 0)) ){
24785 int failed2 = SQLITE_OK;
24786 /* now attemmpt to get the exclusive lock range */
24787 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
24788 SHARED_SIZE, 1);
24789 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
24790 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
24791 /* Can't reestablish the shared lock. Sqlite can't deal, this is
24792 ** a critical I/O error
24793 */
24794 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
24795 SQLITE_IOERR_LOCK;
@@ -24854,57 +24803,55 @@
24803 rc = failed;
24804 }
24805 }
24806
24807 if( rc==SQLITE_OK ){
24808 pFile->eFileLock = eFileLock;
24809 pInode->eFileLock = eFileLock;
24810 }else if( eFileLock==EXCLUSIVE_LOCK ){
24811 pFile->eFileLock = PENDING_LOCK;
24812 pInode->eFileLock = PENDING_LOCK;
24813 }
24814
24815 afp_end_lock:
24816 unixLeaveMutex();
24817 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
24818 rc==SQLITE_OK ? "ok" : "failed"));
24819 return rc;
24820 }
24821
24822 /*
24823 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
24824 ** must be either NO_LOCK or SHARED_LOCK.
24825 **
24826 ** If the locking level of the file descriptor is already at or below
24827 ** the requested locking level, this routine is a no-op.
24828 */
24829 static int afpUnlock(sqlite3_file *id, int eFileLock) {
24830 int rc = SQLITE_OK;
24831 unixFile *pFile = (unixFile*)id;
24832 unixInodeInfo *pInode;
24833 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
24834 int skipShared = 0;
24835 #ifdef SQLITE_TEST
24836 int h = pFile->h;
24837 #endif
24838
24839 assert( pFile );
24840 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
24841 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
24842 getpid()));
24843
24844 assert( eFileLock<=SHARED_LOCK );
24845 if( pFile->eFileLock<=eFileLock ){
24846 return SQLITE_OK;
 
 
24847 }
24848 unixEnterMutex();
24849 pInode = pFile->pInode;
24850 assert( pInode->nShared!=0 );
24851 if( pFile->eFileLock>SHARED_LOCK ){
24852 assert( pInode->eFileLock==pFile->eFileLock );
24853 SimulateIOErrorBenign(1);
24854 SimulateIOError( h=(-1) )
24855 SimulateIOErrorBenign(0);
24856
24857 #ifndef NDEBUG
@@ -24920,66 +24867,64 @@
24867 || pFile->dbUpdate==0
24868 || pFile->transCntrChng==1 );
24869 pFile->inNormalWrite = 0;
24870 #endif
24871
24872 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
24873 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
24874 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
24875 /* only re-establish the shared lock if necessary */
24876 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
24877 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
24878 } else {
24879 skipShared = 1;
24880 }
24881 }
24882 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
24883 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
24884 }
24885 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
24886 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
24887 if( !rc ){
24888 context->reserved = 0;
24889 }
24890 }
24891 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
24892 pInode->eFileLock = SHARED_LOCK;
24893 }
24894 }
24895 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
24896
24897 /* Decrement the shared lock counter. Release the lock using an
24898 ** OS call only when all threads in this same process have released
24899 ** the lock.
24900 */
24901 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
24902 pInode->nShared--;
24903 if( pInode->nShared==0 ){
24904 SimulateIOErrorBenign(1);
24905 SimulateIOError( h=(-1) )
24906 SimulateIOErrorBenign(0);
24907 if( !skipShared ){
24908 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
24909 }
24910 if( !rc ){
24911 pInode->eFileLock = NO_LOCK;
24912 pFile->eFileLock = NO_LOCK;
24913 }
24914 }
24915 if( rc==SQLITE_OK ){
24916 pInode->nLock--;
24917 assert( pInode->nLock>=0 );
24918 if( pInode->nLock==0 ){
 
 
24919 rc = closePendingFds(pFile);
24920 }
24921 }
24922 }
24923
24924 unixLeaveMutex();
24925 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
24926 return rc;
24927 }
24928
24929 /*
24930 ** Close a file & cleanup AFP specific locking context
@@ -24988,20 +24933,19 @@
24933 int rc = SQLITE_OK;
24934 if( id ){
24935 unixFile *pFile = (unixFile*)id;
24936 afpUnlock(id, NO_LOCK);
24937 unixEnterMutex();
24938 if( pFile->pInode && pFile->pInode->nLock ){
24939 /* If there are outstanding locks, do not actually close the file just
24940 ** yet because that would clear those locks. Instead, add the file
24941 ** descriptor to pInode->aPending. It will be automatically closed when
24942 ** the last lock is cleared.
24943 */
24944 setPendingFd(pFile);
24945 }
24946 releaseInodeInfo(pFile);
 
24947 sqlite3_free(pFile->lockingContext);
24948 rc = closeUnixFile(id);
24949 unixLeaveMutex();
24950 }
24951 return rc;
@@ -25020,18 +24964,18 @@
24964 /******************************************************************************
24965 *************************** Begin NFS Locking ********************************/
24966
24967 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
24968 /*
24969 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
24970 ** must be either NO_LOCK or SHARED_LOCK.
24971 **
24972 ** If the locking level of the file descriptor is already at or below
24973 ** the requested locking level, this routine is a no-op.
24974 */
24975 static int nfsUnlock(sqlite3_file *id, int eFileLock){
24976 return _posixUnlock(id, eFileLock, 1);
24977 }
24978
24979 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
24980 /*
24981 ** The code above is the NFS lock implementation. The code is specific
@@ -25091,11 +25035,11 @@
25035 #endif
25036 TIMER_END;
25037 if( got<0 ){
25038 ((unixFile*)id)->lastErrno = errno;
25039 }
25040 OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
25041 return got;
25042 }
25043
25044 /*
25045 ** Read data from a file into a buffer. Return SQLITE_OK if all
@@ -25112,14 +25056,16 @@
25056 int got;
25057 assert( id );
25058
25059 /* If this is a database file (not a journal, master-journal or temp
25060 ** file), the bytes in the locking range should never be read or written. */
25061 #if 0
25062 assert( pFile->pUnused==0
25063 || offset>=PENDING_BYTE+512
25064 || offset+amt<=PENDING_BYTE
25065 );
25066 #endif
25067
25068 got = seekAndRead(pFile, offset, pBuf, amt);
25069 if( got==amt ){
25070 return SQLITE_OK;
25071 }else if( got<0 ){
@@ -25165,11 +25111,11 @@
25111 TIMER_END;
25112 if( got<0 ){
25113 ((unixFile*)id)->lastErrno = errno;
25114 }
25115
25116 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
25117 return got;
25118 }
25119
25120
25121 /*
@@ -25187,14 +25133,16 @@
25133 assert( id );
25134 assert( amt>0 );
25135
25136 /* If this is a database file (not a journal, master-journal or temp
25137 ** file), the bytes in the locking range should never be read or written. */
25138 #if 0
25139 assert( pFile->pUnused==0
25140 || offset>=PENDING_BYTE+512
25141 || offset+amt<=PENDING_BYTE
25142 );
25143 #endif
25144
25145 #ifndef NDEBUG
25146 /* If we are doing a normal write to a database file (as opposed to
25147 ** doing a hot-journal rollback or a write to some file other than a
25148 ** normal database file) then record the fact that the database
@@ -25389,21 +25337,21 @@
25337 ** line is to test that doing so does not cause any problems.
25338 */
25339 SimulateDiskfullError( return SQLITE_FULL );
25340
25341 assert( pFile );
25342 OSTRACE(("SYNC %-3d\n", pFile->h));
25343 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
25344 SimulateIOError( rc=1 );
25345 if( rc ){
25346 pFile->lastErrno = errno;
25347 return SQLITE_IOERR_FSYNC;
25348 }
25349 if( pFile->dirfd>=0 ){
25350 int err;
25351 OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
25352 HAVE_FULLFSYNC, isFullsync));
25353 #ifndef SQLITE_DISABLE_DIRSYNC
25354 /* The directory sync is only attempted if full_fsync is
25355 ** turned off or unavailable. If a full_fsync occurred above,
25356 ** then the directory sync is superfluous.
25357 */
@@ -25471,11 +25419,11 @@
25419 ((unixFile*)id)->lastErrno = errno;
25420 return SQLITE_IOERR_FSTAT;
25421 }
25422 *pSize = buf.st_size;
25423
25424 /* When opening a zero-size database, the findInodeInfo() procedure
25425 ** writes a single byte into that file in order to work around a bug
25426 ** in the OS-X msdos filesystem. In order to avoid problems with upper
25427 ** layers, we need to report this file size as zero even though it is
25428 ** really 1. Ticket #3260.
25429 */
@@ -25498,16 +25446,24 @@
25446 ** Information and control of an open file handle.
25447 */
25448 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
25449 switch( op ){
25450 case SQLITE_FCNTL_LOCKSTATE: {
25451 *(int*)pArg = ((unixFile*)id)->eFileLock;
25452 return SQLITE_OK;
25453 }
25454 case SQLITE_LAST_ERRNO: {
25455 *(int*)pArg = ((unixFile*)id)->lastErrno;
25456 return SQLITE_OK;
25457 }
25458 case SQLITE_FCNTL_SIZE_HINT: {
25459 #if 0 /* No performance advantage seen on Linux */
25460 sqlite3_int64 szFile = *(sqlite3_int64*)pArg;
25461 unixFile *pFile = (unixFile*)id;
25462 ftruncate(pFile->h, szFile);
25463 #endif
25464 return SQLITE_OK;
25465 }
25466 #ifndef NDEBUG
25467 /* The pager calls this method to signal that it has done
25468 ** a rollback and that the database is therefore unchanged and
25469 ** it hence it is OK for the transaction change counter to be
@@ -25549,10 +25505,571 @@
25505 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
25506 UNUSED_PARAMETER(NotUsed);
25507 return 0;
25508 }
25509
25510 #ifndef SQLITE_OMIT_WAL
25511
25512
25513 /*
25514 ** Object used to represent an shared memory buffer.
25515 **
25516 ** When multiple threads all reference the same wal-index, each thread
25517 ** has its own unixShm object, but they all point to a single instance
25518 ** of this unixShmNode object. In other words, each wal-index is opened
25519 ** only once per process.
25520 **
25521 ** Each unixShmNode object is connected to a single unixInodeInfo object.
25522 ** We could coalesce this object into unixInodeInfo, but that would mean
25523 ** every open file that does not use shared memory (in other words, most
25524 ** open files) would have to carry around this extra information. So
25525 ** the unixInodeInfo object contains a pointer to this unixShmNode object
25526 ** and the unixShmNode object is created only when needed.
25527 **
25528 ** unixMutexHeld() must be true when creating or destroying
25529 ** this object or while reading or writing the following fields:
25530 **
25531 ** nRef
25532 **
25533 ** The following fields are read-only after the object is created:
25534 **
25535 ** fid
25536 ** zFilename
25537 **
25538 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
25539 ** unixMutexHeld() is true when reading or writing any other field
25540 ** in this structure.
25541 */
25542 struct unixShmNode {
25543 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
25544 sqlite3_mutex *mutex; /* Mutex to access this object */
25545 char *zFilename; /* Name of the mmapped file */
25546 int h; /* Open file descriptor */
25547 int szRegion; /* Size of shared-memory regions */
25548 int nRegion; /* Size of array apRegion */
25549 char **apRegion; /* Array of mapped shared-memory regions */
25550 int nRef; /* Number of unixShm objects pointing to this */
25551 unixShm *pFirst; /* All unixShm objects pointing to this */
25552 #ifdef SQLITE_DEBUG
25553 u8 exclMask; /* Mask of exclusive locks held */
25554 u8 sharedMask; /* Mask of shared locks held */
25555 u8 nextShmId; /* Next available unixShm.id value */
25556 #endif
25557 };
25558
25559 /*
25560 ** Structure used internally by this VFS to record the state of an
25561 ** open shared memory connection.
25562 **
25563 ** The following fields are initialized when this object is created and
25564 ** are read-only thereafter:
25565 **
25566 ** unixShm.pFile
25567 ** unixShm.id
25568 **
25569 ** All other fields are read/write. The unixShm.pFile->mutex must be held
25570 ** while accessing any read/write fields.
25571 */
25572 struct unixShm {
25573 unixShmNode *pShmNode; /* The underlying unixShmNode object */
25574 unixShm *pNext; /* Next unixShm with the same unixShmNode */
25575 u8 hasMutex; /* True if holding the unixShmNode mutex */
25576 u16 sharedMask; /* Mask of shared locks held */
25577 u16 exclMask; /* Mask of exclusive locks held */
25578 #ifdef SQLITE_DEBUG
25579 u8 id; /* Id of this connection within its unixShmNode */
25580 #endif
25581 };
25582
25583 /*
25584 ** Constants used for locking
25585 */
25586 #define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25587 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25588
25589 /*
25590 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25591 **
25592 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
25593 ** otherwise.
25594 */
25595 static int unixShmSystemLock(
25596 unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
25597 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
25598 int ofst, /* First byte of the locking range */
25599 int n /* Number of bytes to lock */
25600 ){
25601 struct flock f; /* The posix advisory locking structure */
25602 int rc = SQLITE_OK; /* Result code form fcntl() */
25603
25604 /* Access to the unixShmNode object is serialized by the caller */
25605 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
25606
25607 /* Shared locks never span more than one byte */
25608 assert( n==1 || lockType!=F_RDLCK );
25609
25610 /* Locks are within range */
25611 assert( n>=1 && n<SQLITE_SHM_NLOCK );
25612
25613 /* Initialize the locking parameters */
25614 memset(&f, 0, sizeof(f));
25615 f.l_type = lockType;
25616 f.l_whence = SEEK_SET;
25617 f.l_start = ofst;
25618 f.l_len = n;
25619
25620 rc = fcntl(pShmNode->h, F_SETLK, &f);
25621 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
25622
25623 /* Update the global lock state and do debug tracing */
25624 #ifdef SQLITE_DEBUG
25625 { u16 mask;
25626 OSTRACE(("SHM-LOCK "));
25627 mask = (1<<(ofst+n)) - (1<<ofst);
25628 if( rc==SQLITE_OK ){
25629 if( lockType==F_UNLCK ){
25630 OSTRACE(("unlock %d ok", ofst));
25631 pShmNode->exclMask &= ~mask;
25632 pShmNode->sharedMask &= ~mask;
25633 }else if( lockType==F_RDLCK ){
25634 OSTRACE(("read-lock %d ok", ofst));
25635 pShmNode->exclMask &= ~mask;
25636 pShmNode->sharedMask |= mask;
25637 }else{
25638 assert( lockType==F_WRLCK );
25639 OSTRACE(("write-lock %d ok", ofst));
25640 pShmNode->exclMask |= mask;
25641 pShmNode->sharedMask &= ~mask;
25642 }
25643 }else{
25644 if( lockType==F_UNLCK ){
25645 OSTRACE(("unlock %d failed", ofst));
25646 }else if( lockType==F_RDLCK ){
25647 OSTRACE(("read-lock failed"));
25648 }else{
25649 assert( lockType==F_WRLCK );
25650 OSTRACE(("write-lock %d failed", ofst));
25651 }
25652 }
25653 OSTRACE((" - afterwards %03x,%03x\n",
25654 pShmNode->sharedMask, pShmNode->exclMask));
25655 }
25656 #endif
25657
25658 return rc;
25659 }
25660
25661
25662 /*
25663 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
25664 **
25665 ** This is not a VFS shared-memory method; it is a utility function called
25666 ** by VFS shared-memory methods.
25667 */
25668 static void unixShmPurge(unixFile *pFd){
25669 unixShmNode *p = pFd->pInode->pShmNode;
25670 assert( unixMutexHeld() );
25671 if( p && p->nRef==0 ){
25672 int i;
25673 assert( p->pInode==pFd->pInode );
25674 if( p->mutex ) sqlite3_mutex_free(p->mutex);
25675 for(i=0; i<p->nRegion; i++){
25676 munmap(p->apRegion[i], p->szRegion);
25677 }
25678 sqlite3_free(p->apRegion);
25679 if( p->h>=0 ) close(p->h);
25680 p->pInode->pShmNode = 0;
25681 sqlite3_free(p);
25682 }
25683 }
25684
25685 /* Forward reference */
25686 static const char *unixTempFileDir(int);
25687
25688 /*
25689 ** Open a shared-memory area. This particular implementation uses
25690 ** mmapped files.
25691 **
25692 ** zName is a filename used to identify the shared-memory area. The
25693 ** implementation does not (and perhaps should not) use this name
25694 ** directly, but rather use it as a template for finding an appropriate
25695 ** name for the shared-memory storage. In this implementation, the
25696 ** string "-index" is appended to zName and used as the name of the
25697 ** mmapped file.
25698 **
25699 ** When opening a new shared-memory file, if no other instances of that
25700 ** file are currently open, in this process or in other processes, then
25701 ** the file must be truncated to zero length or have its header cleared.
25702 */
25703 static int unixShmOpen(
25704 sqlite3_file *fd /* The file descriptor of the associated database */
25705 ){
25706 struct unixShm *p = 0; /* The connection to be opened */
25707 struct unixShmNode *pShmNode = 0; /* The underlying mmapped file */
25708 int rc; /* Result code */
25709 struct unixFile *pDbFd; /* Underlying database file */
25710 unixInodeInfo *pInode; /* The inode of fd */
25711 const char *zTempDir; /* Directory for temporary files */
25712 int nTempDir; /* Size of the zTempDir string */
25713
25714 /* Allocate space for the new sqlite3_shm object.
25715 */
25716 p = sqlite3_malloc( sizeof(*p) );
25717 if( p==0 ) return SQLITE_NOMEM;
25718 memset(p, 0, sizeof(*p));
25719 pDbFd = (struct unixFile*)fd;
25720 assert( pDbFd->pShm==0 );
25721
25722 /* Check to see if a unixShmNode object already exists. Reuse an existing
25723 ** one if present. Create a new one if necessary.
25724 */
25725 unixEnterMutex();
25726 pInode = pDbFd->pInode;
25727 pShmNode = pInode->pShmNode;
25728 if( pShmNode==0 ){
25729 zTempDir = unixTempFileDir(1);
25730 if( zTempDir==0 ){
25731 unixLeaveMutex();
25732 sqlite3_free(p);
25733 return SQLITE_CANTOPEN_NOTEMPDIR;
25734 }
25735 nTempDir = strlen(zTempDir);
25736 pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nTempDir + 50 );
25737 if( pShmNode==0 ){
25738 rc = SQLITE_NOMEM;
25739 goto shm_open_err;
25740 }
25741 memset(pShmNode, 0, sizeof(*pShmNode));
25742 pShmNode->zFilename = (char*)&pShmNode[1];
25743 sqlite3_snprintf(nTempDir+50, pShmNode->zFilename,
25744 "%s/sqlite-wi-%x-%x", zTempDir,
25745 (u32)pInode->fileId.dev, (u32)pInode->fileId.ino);
25746 pShmNode->h = -1;
25747 pDbFd->pInode->pShmNode = pShmNode;
25748 pShmNode->pInode = pDbFd->pInode;
25749 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
25750 if( pShmNode->mutex==0 ){
25751 rc = SQLITE_NOMEM;
25752 goto shm_open_err;
25753 }
25754
25755 pShmNode->h = open(pShmNode->zFilename, O_RDWR|O_CREAT, 0664);
25756 if( pShmNode->h<0 ){
25757 rc = SQLITE_CANTOPEN_BKPT;
25758 goto shm_open_err;
25759 }
25760
25761 /* Check to see if another process is holding the dead-man switch.
25762 ** If not, truncate the file to zero length.
25763 */
25764 rc = SQLITE_OK;
25765 if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
25766 if( ftruncate(pShmNode->h, 0) ){
25767 rc = SQLITE_IOERR_SHMOPEN;
25768 }
25769 }
25770 if( rc==SQLITE_OK ){
25771 rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
25772 }
25773 if( rc ) goto shm_open_err;
25774 }
25775
25776 /* Make the new connection a child of the unixShmNode */
25777 p->pShmNode = pShmNode;
25778 p->pNext = pShmNode->pFirst;
25779 #ifdef SQLITE_DEBUG
25780 p->id = pShmNode->nextShmId++;
25781 #endif
25782 pShmNode->pFirst = p;
25783 pShmNode->nRef++;
25784 pDbFd->pShm = p;
25785 unixLeaveMutex();
25786 return SQLITE_OK;
25787
25788 /* Jump here on any error */
25789 shm_open_err:
25790 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
25791 sqlite3_free(p);
25792 unixLeaveMutex();
25793 return rc;
25794 }
25795
25796 /*
25797 ** Close a connection to shared-memory. Delete the underlying
25798 ** storage if deleteFlag is true.
25799 */
25800 static int unixShmClose(
25801 sqlite3_file *fd, /* The underlying database file */
25802 int deleteFlag /* Delete shared-memory if true */
25803 ){
25804 unixShm *p; /* The connection to be closed */
25805 unixShmNode *pShmNode; /* The underlying shared-memory file */
25806 unixShm **pp; /* For looping over sibling connections */
25807 unixFile *pDbFd; /* The underlying database file */
25808
25809 pDbFd = (unixFile*)fd;
25810 p = pDbFd->pShm;
25811 if( p==0 ) return SQLITE_OK;
25812 pShmNode = p->pShmNode;
25813
25814 assert( pShmNode==pDbFd->pInode->pShmNode );
25815 assert( pShmNode->pInode==pDbFd->pInode );
25816
25817 /* Remove connection p from the set of connections associated
25818 ** with pShmNode */
25819 sqlite3_mutex_enter(pShmNode->mutex);
25820 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
25821 *pp = p->pNext;
25822
25823 /* Free the connection p */
25824 sqlite3_free(p);
25825 pDbFd->pShm = 0;
25826 sqlite3_mutex_leave(pShmNode->mutex);
25827
25828 /* If pShmNode->nRef has reached 0, then close the underlying
25829 ** shared-memory file, too */
25830 unixEnterMutex();
25831 assert( pShmNode->nRef>0 );
25832 pShmNode->nRef--;
25833 if( pShmNode->nRef==0 ){
25834 if( deleteFlag ) unlink(pShmNode->zFilename);
25835 unixShmPurge(pDbFd);
25836 }
25837 unixLeaveMutex();
25838
25839 return SQLITE_OK;
25840 }
25841
25842 /*
25843 ** Change the lock state for a shared-memory segment.
25844 **
25845 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
25846 ** different here than in posix. In xShmLock(), one can go from unlocked
25847 ** to shared and back or from unlocked to exclusive and back. But one may
25848 ** not go from shared to exclusive or from exclusive to shared.
25849 */
25850 static int unixShmLock(
25851 sqlite3_file *fd, /* Database file holding the shared memory */
25852 int ofst, /* First lock to acquire or release */
25853 int n, /* Number of locks to acquire or release */
25854 int flags /* What to do with the lock */
25855 ){
25856 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
25857 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
25858 unixShm *pX; /* For looping over all siblings */
25859 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
25860 int rc = SQLITE_OK; /* Result code */
25861 u16 mask; /* Mask of locks to take or release */
25862
25863 assert( pShmNode==pDbFd->pInode->pShmNode );
25864 assert( pShmNode->pInode==pDbFd->pInode );
25865 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
25866 assert( n>=1 );
25867 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
25868 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
25869 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
25870 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
25871 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
25872
25873 mask = (1<<(ofst+n)) - (1<<ofst);
25874 assert( n>1 || mask==(1<<ofst) );
25875 sqlite3_mutex_enter(pShmNode->mutex);
25876 if( flags & SQLITE_SHM_UNLOCK ){
25877 u16 allMask = 0; /* Mask of locks held by siblings */
25878
25879 /* See if any siblings hold this same lock */
25880 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25881 if( pX==p ) continue;
25882 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
25883 allMask |= pX->sharedMask;
25884 }
25885
25886 /* Unlock the system-level locks */
25887 if( (mask & allMask)==0 ){
25888 rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
25889 }else{
25890 rc = SQLITE_OK;
25891 }
25892
25893 /* Undo the local locks */
25894 if( rc==SQLITE_OK ){
25895 p->exclMask &= ~mask;
25896 p->sharedMask &= ~mask;
25897 }
25898 }else if( flags & SQLITE_SHM_SHARED ){
25899 u16 allShared = 0; /* Union of locks held by connections other than "p" */
25900
25901 /* Find out which shared locks are already held by sibling connections.
25902 ** If any sibling already holds an exclusive lock, go ahead and return
25903 ** SQLITE_BUSY.
25904 */
25905 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25906 if( (pX->exclMask & mask)!=0 ){
25907 rc = SQLITE_BUSY;
25908 break;
25909 }
25910 allShared |= pX->sharedMask;
25911 }
25912
25913 /* Get shared locks at the system level, if necessary */
25914 if( rc==SQLITE_OK ){
25915 if( (allShared & mask)==0 ){
25916 rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
25917 }else{
25918 rc = SQLITE_OK;
25919 }
25920 }
25921
25922 /* Get the local shared locks */
25923 if( rc==SQLITE_OK ){
25924 p->sharedMask |= mask;
25925 }
25926 }else{
25927 /* Make sure no sibling connections hold locks that will block this
25928 ** lock. If any do, return SQLITE_BUSY right away.
25929 */
25930 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
25931 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
25932 rc = SQLITE_BUSY;
25933 break;
25934 }
25935 }
25936
25937 /* Get the exclusive locks at the system level. Then if successful
25938 ** also mark the local connection as being locked.
25939 */
25940 if( rc==SQLITE_OK ){
25941 rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
25942 if( rc==SQLITE_OK ){
25943 assert( (p->sharedMask & mask)==0 );
25944 p->exclMask |= mask;
25945 }
25946 }
25947 }
25948 sqlite3_mutex_leave(pShmNode->mutex);
25949 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
25950 p->id, getpid(), p->sharedMask, p->exclMask));
25951 return rc;
25952 }
25953
25954 /*
25955 ** Implement a memory barrier or memory fence on shared memory.
25956 **
25957 ** All loads and stores begun before the barrier must complete before
25958 ** any load or store begun after the barrier.
25959 */
25960 static void unixShmBarrier(
25961 sqlite3_file *fd /* Database file holding the shared memory */
25962 ){
25963 unixEnterMutex();
25964 unixLeaveMutex();
25965 }
25966
25967 /*
25968 ** This function is called to obtain a pointer to region iRegion of the
25969 ** shared-memory associated with the database file fd. Shared-memory regions
25970 ** are numbered starting from zero. Each shared-memory region is szRegion
25971 ** bytes in size.
25972 **
25973 ** If an error occurs, an error code is returned and *pp is set to NULL.
25974 **
25975 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
25976 ** region has not been allocated (by any client, including one running in a
25977 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
25978 ** isWrite is non-zero and the requested shared-memory region has not yet
25979 ** been allocated, it is allocated by this function.
25980 **
25981 ** If the shared-memory region has already been allocated or is allocated by
25982 ** this call as described above, then it is mapped into this processes
25983 ** address space (if it is not already), *pp is set to point to the mapped
25984 ** memory and SQLITE_OK returned.
25985 */
25986 static int unixShmMap(
25987 sqlite3_file *fd, /* Handle open on database file */
25988 int iRegion, /* Region to retrieve */
25989 int szRegion, /* Size of regions */
25990 int isWrite, /* True to extend file if necessary */
25991 void volatile **pp /* OUT: Mapped memory */
25992 ){
25993 unixFile *pDbFd = (unixFile*)fd;
25994 unixShm *p = pDbFd->pShm;
25995 unixShmNode *pShmNode = p->pShmNode;
25996 int rc = SQLITE_OK;
25997
25998 sqlite3_mutex_enter(pShmNode->mutex);
25999 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
26000
26001 if( pShmNode->nRegion<=iRegion ){
26002 char **apNew; /* New apRegion[] array */
26003 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
26004 struct stat sStat; /* Used by fstat() */
26005
26006 pShmNode->szRegion = szRegion;
26007
26008 /* The requested region is not mapped into this processes address space.
26009 ** Check to see if it has been allocated (i.e. if the wal-index file is
26010 ** large enough to contain the requested region).
26011 */
26012 if( fstat(pShmNode->h, &sStat) ){
26013 rc = SQLITE_IOERR_SHMSIZE;
26014 goto shmpage_out;
26015 }
26016
26017 if( sStat.st_size<nByte ){
26018 /* The requested memory region does not exist. If isWrite is set to
26019 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
26020 **
26021 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
26022 ** the requested memory region.
26023 */
26024 if( !isWrite ) goto shmpage_out;
26025 if( ftruncate(pShmNode->h, nByte) ){
26026 rc = SQLITE_IOERR_SHMSIZE;
26027 goto shmpage_out;
26028 }
26029 }
26030
26031 /* Map the requested memory region into this processes address space. */
26032 apNew = (char **)sqlite3_realloc(
26033 pShmNode->apRegion, (iRegion+1)*sizeof(char *)
26034 );
26035 if( !apNew ){
26036 rc = SQLITE_IOERR_NOMEM;
26037 goto shmpage_out;
26038 }
26039 pShmNode->apRegion = apNew;
26040 while(pShmNode->nRegion<=iRegion){
26041 void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE,
26042 MAP_SHARED, pShmNode->h, iRegion*szRegion
26043 );
26044 if( pMem==MAP_FAILED ){
26045 rc = SQLITE_IOERR;
26046 goto shmpage_out;
26047 }
26048 pShmNode->apRegion[pShmNode->nRegion] = pMem;
26049 pShmNode->nRegion++;
26050 }
26051 }
26052
26053 shmpage_out:
26054 if( pShmNode->nRegion>iRegion ){
26055 *pp = pShmNode->apRegion[iRegion];
26056 }else{
26057 *pp = 0;
26058 }
26059 sqlite3_mutex_leave(pShmNode->mutex);
26060 return rc;
26061 }
26062
26063 #else
26064 # define unixShmOpen 0
26065 # define unixShmLock 0
26066 # define unixShmMap 0
26067 # define unixShmBarrier 0
26068 # define unixShmClose 0
26069 #endif /* #ifndef SQLITE_OMIT_WAL */
26070
26071 /*
26072 ** Here ends the implementation of all sqlite3_file methods.
26073 **
26074 ********************** End sqlite3_file Methods *******************************
26075 ******************************************************************************/
@@ -25589,13 +26106,13 @@
26106 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
26107 **
26108 ** * An I/O method finder function called FINDER that returns a pointer
26109 ** to the METHOD object in the previous bullet.
26110 */
26111 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \
26112 static const sqlite3_io_methods METHOD = { \
26113 VERSION, /* iVersion */ \
26114 CLOSE, /* xClose */ \
26115 unixRead, /* xRead */ \
26116 unixWrite, /* xWrite */ \
26117 unixTruncate, /* xTruncate */ \
26118 unixSync, /* xSync */ \
@@ -25603,11 +26120,16 @@
26120 LOCK, /* xLock */ \
26121 UNLOCK, /* xUnlock */ \
26122 CKLOCK, /* xCheckReservedLock */ \
26123 unixFileControl, /* xFileControl */ \
26124 unixSectorSize, /* xSectorSize */ \
26125 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
26126 unixShmOpen, /* xShmOpen */ \
26127 unixShmLock, /* xShmLock */ \
26128 unixShmMap, /* xShmMap */ \
26129 unixShmBarrier, /* xShmBarrier */ \
26130 unixShmClose /* xShmClose */ \
26131 }; \
26132 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
26133 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
26134 return &METHOD; \
26135 } \
@@ -25620,26 +26142,29 @@
26142 ** are also created.
26143 */
26144 IOMETHODS(
26145 posixIoFinder, /* Finder function name */
26146 posixIoMethods, /* sqlite3_io_methods object name */
26147 2, /* ShmOpen is enabled */
26148 unixClose, /* xClose method */
26149 unixLock, /* xLock method */
26150 unixUnlock, /* xUnlock method */
26151 unixCheckReservedLock /* xCheckReservedLock method */
26152 )
26153 IOMETHODS(
26154 nolockIoFinder, /* Finder function name */
26155 nolockIoMethods, /* sqlite3_io_methods object name */
26156 1, /* ShmOpen is disabled */
26157 nolockClose, /* xClose method */
26158 nolockLock, /* xLock method */
26159 nolockUnlock, /* xUnlock method */
26160 nolockCheckReservedLock /* xCheckReservedLock method */
26161 )
26162 IOMETHODS(
26163 dotlockIoFinder, /* Finder function name */
26164 dotlockIoMethods, /* sqlite3_io_methods object name */
26165 1, /* ShmOpen is disabled */
26166 dotlockClose, /* xClose method */
26167 dotlockLock, /* xLock method */
26168 dotlockUnlock, /* xUnlock method */
26169 dotlockCheckReservedLock /* xCheckReservedLock method */
26170 )
@@ -25646,10 +26171,11 @@
26171
26172 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
26173 IOMETHODS(
26174 flockIoFinder, /* Finder function name */
26175 flockIoMethods, /* sqlite3_io_methods object name */
26176 1, /* ShmOpen is disabled */
26177 flockClose, /* xClose method */
26178 flockLock, /* xLock method */
26179 flockUnlock, /* xUnlock method */
26180 flockCheckReservedLock /* xCheckReservedLock method */
26181 )
@@ -25657,10 +26183,11 @@
26183
26184 #if OS_VXWORKS
26185 IOMETHODS(
26186 semIoFinder, /* Finder function name */
26187 semIoMethods, /* sqlite3_io_methods object name */
26188 1, /* ShmOpen is disabled */
26189 semClose, /* xClose method */
26190 semLock, /* xLock method */
26191 semUnlock, /* xUnlock method */
26192 semCheckReservedLock /* xCheckReservedLock method */
26193 )
@@ -25668,10 +26195,11 @@
26195
26196 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26197 IOMETHODS(
26198 afpIoFinder, /* Finder function name */
26199 afpIoMethods, /* sqlite3_io_methods object name */
26200 1, /* ShmOpen is disabled */
26201 afpClose, /* xClose method */
26202 afpLock, /* xLock method */
26203 afpUnlock, /* xUnlock method */
26204 afpCheckReservedLock /* xCheckReservedLock method */
26205 )
@@ -25692,10 +26220,11 @@
26220 static int proxyUnlock(sqlite3_file*, int);
26221 static int proxyCheckReservedLock(sqlite3_file*, int*);
26222 IOMETHODS(
26223 proxyIoFinder, /* Finder function name */
26224 proxyIoMethods, /* sqlite3_io_methods object name */
26225 1, /* ShmOpen is disabled */
26226 proxyClose, /* xClose method */
26227 proxyLock, /* xLock method */
26228 proxyUnlock, /* xUnlock method */
26229 proxyCheckReservedLock /* xCheckReservedLock method */
26230 )
@@ -25704,10 +26233,11 @@
26233 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
26234 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26235 IOMETHODS(
26236 nfsIoFinder, /* Finder function name */
26237 nfsIoMethods, /* sqlite3_io_methods object name */
26238 1, /* ShmOpen is disabled */
26239 unixClose, /* xClose method */
26240 unixLock, /* xLock method */
26241 nfsUnlock, /* xUnlock method */
26242 unixCheckReservedLock /* xCheckReservedLock method */
26243 )
@@ -25844,23 +26374,23 @@
26374 ){
26375 const sqlite3_io_methods *pLockingStyle;
26376 unixFile *pNew = (unixFile *)pId;
26377 int rc = SQLITE_OK;
26378
26379 assert( pNew->pInode==NULL );
 
26380
26381 /* Parameter isDelete is only used on vxworks. Express this explicitly
26382 ** here to prevent compiler warnings about unused parameters.
26383 */
26384 UNUSED_PARAMETER(isDelete);
26385
26386 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
26387 pNew->h = h;
26388 pNew->dirfd = dirfd;
 
26389 pNew->fileFlags = 0;
26390 assert( zFilename==0 || zFilename[0]=='/' ); /* Never a relative pathname */
26391 pNew->zPath = zFilename;
26392
26393 #if OS_VXWORKS
26394 pNew->pId = vxworksFindFileId(zFilename);
26395 if( pNew->pId==0 ){
26396 noLock = 1;
@@ -25884,23 +26414,23 @@
26414 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26415 || pLockingStyle == &nfsIoMethods
26416 #endif
26417 ){
26418 unixEnterMutex();
26419 rc = findInodeInfo(pNew, &pNew->pInode);
26420 if( rc!=SQLITE_OK ){
26421 /* If an error occured in findInodeInfo(), close the file descriptor
26422 ** immediately, before releasing the mutex. findInodeInfo() may fail
26423 ** in two scenarios:
26424 **
26425 ** (a) A call to fstat() failed.
26426 ** (b) A malloc failed.
26427 **
26428 ** Scenario (b) may only occur if the process is holding no other
26429 ** file descriptors open on the same file. If there were other file
26430 ** descriptors on this file, then no malloc would be required by
26431 ** findInodeInfo(). If this is the case, it is quite safe to close
26432 ** handle h - as it is guaranteed that no posix locks will be released
26433 ** by doing so.
26434 **
26435 ** If scenario (a) caused the error then things are not so safe. The
26436 ** implicit assumption here is that if fstat() fails, things are in
@@ -25927,11 +26457,11 @@
26457 ** copy of the filename. */
26458 pCtx->dbPath = zFilename;
26459 pCtx->reserved = 0;
26460 srandomdev();
26461 unixEnterMutex();
26462 rc = findInodeInfo(pNew, &pNew->pInode);
26463 if( rc!=SQLITE_OK ){
26464 sqlite3_free(pNew->lockingContext);
26465 close(h);
26466 h = -1;
26467 }
@@ -25960,22 +26490,22 @@
26490 else if( pLockingStyle == &semIoMethods ){
26491 /* Named semaphore locking uses the file path so it needs to be
26492 ** included in the semLockingContext
26493 */
26494 unixEnterMutex();
26495 rc = findInodeInfo(pNew, &pNew->pInode);
26496 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
26497 char *zSemName = pNew->pInode->aSemName;
26498 int n;
26499 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
26500 pNew->pId->zCanonicalName);
26501 for( n=1; zSemName[n]; n++ )
26502 if( zSemName[n]=='/' ) zSemName[n] = '_';
26503 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
26504 if( pNew->pInode->pSem == SEM_FAILED ){
26505 rc = SQLITE_NOMEM;
26506 pNew->pInode->aSemName[0] = '\0';
26507 }
26508 }
26509 unixLeaveMutex();
26510 }
26511 #endif
@@ -26022,58 +26552,75 @@
26552 fd = open(zDirname, O_RDONLY|O_BINARY, 0);
26553 if( fd>=0 ){
26554 #ifdef FD_CLOEXEC
26555 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
26556 #endif
26557 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
26558 }
26559 }
26560 *pFd = fd;
26561 return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT);
26562 }
26563
26564 /*
26565 ** Return the name of a directory in which to put temporary files.
26566 ** If no suitable temporary file directory can be found, return NULL.
 
26567 */
26568 static const char *unixTempFileDir(int allowShm){
26569 static const char *azDirs[] = {
26570 0,
26571 0,
26572 "/var/tmp",
26573 "/usr/tmp",
26574 "/tmp",
26575 0 /* List terminator */
26576 };
26577 unsigned int i;
26578 struct stat buf;
26579 const char *zDir = 0;
26580
26581 azDirs[0] = sqlite3_temp_directory;
26582 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
26583
26584 if( allowShm ){
26585 zDir = "/dev/shm";
26586 i = 2; /* Skip the app-defined temp locations for shared-memory */
26587 }else{
26588 zDir = azDirs[0];
26589 i = 1;
26590 }
26591 for(; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
26592 if( zDir==0 ) continue;
26593 if( stat(zDir, &buf) ) continue;
26594 if( !S_ISDIR(buf.st_mode) ) continue;
26595 if( !allowShm && access(zDir, 07) ) continue;
26596 break;
26597 }
26598 return zDir;
26599 }
26600
26601 /*
26602 ** Create a temporary file name in zBuf. zBuf must be allocated
26603 ** by the calling process and must be big enough to hold at least
26604 ** pVfs->mxPathname bytes.
26605 */
26606 static int unixGetTempname(int nBuf, char *zBuf){
26607 static const unsigned char zChars[] =
26608 "abcdefghijklmnopqrstuvwxyz"
26609 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
26610 "0123456789";
26611 unsigned int i, j;
26612 const char *zDir;
 
26613
26614 /* It's odd to simulate an io-error here, but really this is just
26615 ** using the io-error infrastructure to test that SQLite handles this
26616 ** function failing.
26617 */
26618 SimulateIOError( return SQLITE_IOERR );
26619
26620 zDir = unixTempFileDir(0);
26621 if( zDir==0 ) zDir = ".";
 
 
 
 
 
 
 
 
 
 
 
26622
26623 /* Check that the output buffer is large enough for the temporary file
26624 ** name. If it is not, return SQLITE_ERROR.
26625 */
26626 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
@@ -26135,21 +26682,21 @@
26682 ** descriptor on the same path, fail, and return an error to SQLite.
26683 **
26684 ** Even if a subsequent open() call does succeed, the consequences of
26685 ** not searching for a resusable file descriptor are not dire. */
26686 if( 0==stat(zPath, &sStat) ){
26687 unixInodeInfo *pInode;
26688
26689 unixEnterMutex();
26690 pInode = inodeList;
26691 while( pInode && (pInode->fileId.dev!=sStat.st_dev
26692 || pInode->fileId.ino!=sStat.st_ino) ){
26693 pInode = pInode->pNext;
26694 }
26695 if( pInode ){
26696 UnixUnusedFd **pp;
26697 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
26698 pUnused = *pp;
26699 if( pUnused ){
26700 *pp = pUnused->pNext;
26701 }
26702 }
@@ -26259,11 +26806,11 @@
26806 }
26807 p->pUnused = pUnused;
26808 }else if( !zName ){
26809 /* If zName is NULL, the upper layer is requesting a temp file. */
26810 assert(isDelete && !isOpenDirectory);
26811 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
26812 if( rc!=SQLITE_OK ){
26813 return rc;
26814 }
26815 zName = zTmpname;
26816 }
@@ -26279,11 +26826,11 @@
26826 openFlags |= (O_LARGEFILE|O_BINARY);
26827
26828 if( fd<0 ){
26829 mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
26830 fd = open(zName, openFlags, openMode);
26831 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
26832 if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
26833 /* Failed to open the file for read/write access. Try read-only. */
26834 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
26835 openFlags &= ~(O_RDWR|O_CREAT);
26836 flags |= SQLITE_OPEN_READONLY;
@@ -26663,41 +27210,54 @@
27210 ** sqlite3OsCurrentTime() during testing.
27211 */
27212 #ifdef SQLITE_TEST
27213 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
27214 #endif
27215
27216 /*
27217 ** Find the current time (in Universal Coordinated Time). Write into *piNow
27218 ** the current time and date as a Julian Day number times 86_400_000. In
27219 ** other words, write into *piNow the number of milliseconds since the Julian
27220 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
27221 ** proleptic Gregorian calendar.
27222 **
27223 ** On success, return 0. Return 1 if the time and date cannot be found.
27224 */
27225 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
27226 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
27227 #if defined(NO_GETTOD)
27228 time_t t;
27229 time(&t);
27230 *piNow = ((sqlite3_int64)i)*1000 + unixEpoch;
27231 #elif OS_VXWORKS
27232 struct timespec sNow;
27233 clock_gettime(CLOCK_REALTIME, &sNow);
27234 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
27235 #else
27236 struct timeval sNow;
27237 gettimeofday(&sNow, 0);
27238 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
27239 #endif
27240
27241 #ifdef SQLITE_TEST
27242 if( sqlite3_current_time ){
27243 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
27244 }
27245 #endif
27246 UNUSED_PARAMETER(NotUsed);
27247 return 0;
27248 }
27249
27250 /*
27251 ** Find the current time (in Universal Coordinated Time). Write the
27252 ** current time and date as a Julian Day number into *prNow and
27253 ** return 0. Return 1 if the time and date cannot be found.
27254 */
27255 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
27256 sqlite3_int64 i;
27257 unixCurrentTimeInt64(0, &i);
27258 *prNow = i/86400000.0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27259 return 0;
27260 }
27261
27262 /*
27263 ** We added the xGetLastError() method with the intention of providing
@@ -26710,10 +27270,11 @@
27270 UNUSED_PARAMETER(NotUsed);
27271 UNUSED_PARAMETER(NotUsed2);
27272 UNUSED_PARAMETER(NotUsed3);
27273 return 0;
27274 }
27275
27276
27277 /*
27278 ************************ End of sqlite3_vfs methods ***************************
27279 ******************************************************************************/
27280
@@ -26901,12 +27462,12 @@
27462 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
27463 #else
27464 # ifdef _CS_DARWIN_USER_TEMP_DIR
27465 {
27466 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
27467 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
27468 lPath, errno, getpid()));
27469 return SQLITE_IOERR_LOCK;
27470 }
27471 len = strlcat(lPath, "sqliteplocks", maxLen);
27472 }
27473 # else
@@ -26924,11 +27485,11 @@
27485 char c = dbPath[i];
27486 lPath[i+len] = (c=='/')?'_':c;
27487 }
27488 lPath[i+len]='\0';
27489 strlcat(lPath, ":auto:", maxLen);
27490 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid()));
27491 return SQLITE_OK;
27492 }
27493
27494 /*
27495 ** Creates the lock file and any missing directories in lockPath
@@ -26949,22 +27510,22 @@
27510 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
27511 buf[i]='\0';
27512 if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
27513 int err=errno;
27514 if( err!=EEXIST ) {
27515 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
27516 "'%s' proxy lock path=%s pid=%d\n",
27517 buf, strerror(err), lockPath, getpid()));
27518 return err;
27519 }
27520 }
27521 }
27522 start=i+1;
27523 }
27524 buf[i] = lockPath[i];
27525 }
27526 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid()));
27527 return 0;
27528 }
27529
27530 /*
27531 ** Create a new VFS file descriptor (stored in memory obtained from
@@ -27252,12 +27813,12 @@
27813 int hostIdMatch = 0;
27814 int readLen = 0;
27815 int tryOldLockPath = 0;
27816 int forceNewLockPath = 0;
27817
27818 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
27819 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
27820
27821 rc = proxyGetHostID(myHostID, &pError);
27822 if( (rc&0xff)==SQLITE_IOERR ){
27823 pFile->lastErrno = pError;
27824 goto end_takeconch;
@@ -27333,11 +27894,11 @@
27894 ** has a shared lock already), if the host id matches, use the big
27895 ** stick.
27896 */
27897 futimes(conchFile->h, NULL);
27898 if( hostIdMatch && !createConch ){
27899 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
27900 /* We are trying for an exclusive lock but another thread in this
27901 ** same process is still holding a shared lock. */
27902 rc = SQLITE_BUSY;
27903 } else {
27904 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
@@ -27389,11 +27950,11 @@
27950 }
27951 }
27952 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
27953
27954 end_takeconch:
27955 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
27956 if( rc==SQLITE_OK && pFile->openFlags ){
27957 if( pFile->h>=0 ){
27958 #ifdef STRICT_CLOSE_ERROR
27959 if( close(pFile->h) ){
27960 pFile->lastErrno = errno;
@@ -27404,11 +27965,11 @@
27965 #endif
27966 }
27967 pFile->h = -1;
27968 int fd = open(pCtx->dbPath, pFile->openFlags,
27969 SQLITE_DEFAULT_FILE_PERMISSIONS);
27970 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
27971 if( fd>=0 ){
27972 pFile->h = fd;
27973 }else{
27974 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
27975 during locking */
@@ -27446,35 +28007,37 @@
28007 afpCtx->dbPath = pCtx->lockProxyPath;
28008 }
28009 } else {
28010 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
28011 }
28012 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
28013 rc==SQLITE_OK?"ok":"failed"));
28014 return rc;
28015 } while (1); /* in case we need to retry the :auto: lock file -
28016 ** we should never get here except via the 'continue' call. */
28017 }
28018 }
28019
28020 /*
28021 ** If pFile holds a lock on a conch file, then release that lock.
28022 */
28023 static int proxyReleaseConch(unixFile *pFile){
28024 int rc = SQLITE_OK; /* Subroutine return code */
28025 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
28026 unixFile *conchFile; /* Name of the conch file */
28027
28028 pCtx = (proxyLockingContext *)pFile->lockingContext;
28029 conchFile = pCtx->conchFile;
28030 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
28031 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
28032 getpid()));
28033 if( pCtx->conchHeld>0 ){
28034 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
28035 }
28036 pCtx->conchHeld = 0;
28037 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
28038 (rc==SQLITE_OK ? "ok" : "failed")));
28039 return rc;
28040 }
28041
28042 /*
28043 ** Given the name of a database file, compute the name of its conch file.
@@ -27527,11 +28090,11 @@
28090 static int switchLockProxyPath(unixFile *pFile, const char *path) {
28091 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
28092 char *oldPath = pCtx->lockProxyPath;
28093 int rc = SQLITE_OK;
28094
28095 if( pFile->eFileLock!=NO_LOCK ){
28096 return SQLITE_BUSY;
28097 }
28098
28099 /* nothing to do if the path is NULL, :auto: or matches the existing path */
28100 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
@@ -27594,22 +28157,22 @@
28157 proxyLockingContext *pCtx;
28158 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
28159 char *lockPath=NULL;
28160 int rc = SQLITE_OK;
28161
28162 if( pFile->eFileLock!=NO_LOCK ){
28163 return SQLITE_BUSY;
28164 }
28165 proxyGetDbPathForUnixFile(pFile, dbPath);
28166 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
28167 lockPath=NULL;
28168 }else{
28169 lockPath=(char *)path;
28170 }
28171
28172 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
28173 (lockPath ? lockPath : ":auto:"), getpid()));
28174
28175 pCtx = sqlite3_malloc( sizeof(*pCtx) );
28176 if( pCtx==0 ){
28177 return SQLITE_NOMEM;
28178 }
@@ -27665,12 +28228,12 @@
28228 }
28229 sqlite3_free(pCtx->lockProxyPath);
28230 sqlite3_free(pCtx->conchFilePath);
28231 sqlite3_free(pCtx);
28232 }
28233 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
28234 (rc==SQLITE_OK ? "ok" : "failed")));
28235 return rc;
28236 }
28237
28238
28239 /*
@@ -27761,11 +28324,11 @@
28324 }
28325 return rc;
28326 }
28327
28328 /*
28329 ** Lock the file with the lock specified by parameter eFileLock - one
28330 ** of the following:
28331 **
28332 ** (1) SHARED_LOCK
28333 ** (2) RESERVED_LOCK
28334 ** (3) PENDING_LOCK
@@ -27784,43 +28347,43 @@
28347 ** PENDING -> EXCLUSIVE
28348 **
28349 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
28350 ** routine to lower a locking level.
28351 */
28352 static int proxyLock(sqlite3_file *id, int eFileLock) {
28353 unixFile *pFile = (unixFile*)id;
28354 int rc = proxyTakeConch(pFile);
28355 if( rc==SQLITE_OK ){
28356 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
28357 if( pCtx->conchHeld>0 ){
28358 unixFile *proxy = pCtx->lockProxy;
28359 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
28360 pFile->eFileLock = proxy->eFileLock;
28361 }else{
28362 /* conchHeld < 0 is lockless */
28363 }
28364 }
28365 return rc;
28366 }
28367
28368
28369 /*
28370 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
28371 ** must be either NO_LOCK or SHARED_LOCK.
28372 **
28373 ** If the locking level of the file descriptor is already at or below
28374 ** the requested locking level, this routine is a no-op.
28375 */
28376 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
28377 unixFile *pFile = (unixFile*)id;
28378 int rc = proxyTakeConch(pFile);
28379 if( rc==SQLITE_OK ){
28380 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
28381 if( pCtx->conchHeld>0 ){
28382 unixFile *proxy = pCtx->lockProxy;
28383 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
28384 pFile->eFileLock = proxy->eFileLock;
28385 }else{
28386 /* conchHeld < 0 is lockless */
28387 }
28388 }
28389 return rc;
@@ -27911,11 +28474,11 @@
28474 ** more than that; it looks at the filesystem type that hosts the
28475 ** database file and tries to choose an locking method appropriate for
28476 ** that filesystem time.
28477 */
28478 #define UNIXVFS(VFSNAME, FINDER) { \
28479 2, /* iVersion */ \
28480 sizeof(unixFile), /* szOsFile */ \
28481 MAX_PATHNAME, /* mxPathname */ \
28482 0, /* pNext */ \
28483 VFSNAME, /* zName */ \
28484 (void*)&FINDER, /* pAppData */ \
@@ -27928,11 +28491,13 @@
28491 unixDlSym, /* xDlSym */ \
28492 unixDlClose, /* xDlClose */ \
28493 unixRandomness, /* xRandomness */ \
28494 unixSleep, /* xSleep */ \
28495 unixCurrentTime, /* xCurrentTime */ \
28496 unixGetLastError, /* xGetLastError */ \
28497 0, /* xRename */ \
28498 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
28499 }
28500
28501 /*
28502 ** All default VFSes for unix are contained in the following array.
28503 **
@@ -28079,27 +28644,13 @@
28644 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
28645 #endif
28646
28647 #ifdef SQLITE_DEBUG
28648 SQLITE_PRIVATE int sqlite3OSTrace = 0;
28649 #define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
 
 
 
 
 
 
 
 
28650 #else
28651 #define OSTRACE(X)
 
 
 
 
 
 
28652 #endif
28653
28654 /*
28655 ** Macros for performance tracing. Normally turned off. Only works
28656 ** on i486 hardware.
@@ -28281,10 +28832,14 @@
28832 #if SQLITE_OS_WINCE
28833 # define AreFileApisANSI() 1
28834 # define FormatMessageW(a,b,c,d,e,f,g) 0
28835 #endif
28836
28837 /* Forward references */
28838 typedef struct winShm winShm; /* A connection to shared-memory */
28839 typedef struct winShmNode winShmNode; /* A region of shared-memory */
28840
28841 /*
28842 ** WinCE lacks native support for file locking so we have to fake it
28843 ** with some code of our own.
28844 */
28845 #if SQLITE_OS_WINCE
@@ -28300,16 +28855,19 @@
28855 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
28856 ** portability layer.
28857 */
28858 typedef struct winFile winFile;
28859 struct winFile {
28860 const sqlite3_io_methods *pMethod; /*** Must be first ***/
28861 sqlite3_vfs *pVfs; /* The VFS used to open this file */
28862 HANDLE h; /* Handle for accessing the file */
28863 unsigned char locktype; /* Type of lock currently held on this file */
28864 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
28865 DWORD lastErrno; /* The Windows errno from the last I/O error */
28866 DWORD sectorSize; /* Sector size of the device file is on */
28867 winShm *pShm; /* Instance of shared memory on this file */
28868 const char *zPath; /* Full pathname of this file */
28869 #if SQLITE_OS_WINCE
28870 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
28871 HANDLE hMutex; /* Mutex used to control access to shared lock */
28872 HANDLE hShared; /* Shared memory segment used for locking */
28873 winceLock local; /* Locks obtained by this instance of winFile */
@@ -28833,11 +29391,12 @@
29391 static int winClose(sqlite3_file *id){
29392 int rc, cnt = 0;
29393 winFile *pFile = (winFile*)id;
29394
29395 assert( id!=0 );
29396 assert( pFile->pShm==0 );
29397 OSTRACE(("CLOSE %d\n", pFile->h));
29398 do{
29399 rc = CloseHandle(pFile->h);
29400 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
29401 #if SQLITE_OS_WINCE
29402 #define WINCE_DELETION_ATTEMPTS 3
@@ -28852,10 +29411,11 @@
29411 Sleep(100); /* Wait a little before trying again */
29412 }
29413 free(pFile->zDeleteOnClose);
29414 }
29415 #endif
29416 OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
29417 OpenCounter(-1);
29418 return rc ? SQLITE_OK : SQLITE_IOERR;
29419 }
29420
29421 /*
@@ -28883,11 +29443,11 @@
29443 DWORD error;
29444 DWORD got;
29445
29446 assert( id!=0 );
29447 SimulateIOError(return SQLITE_IOERR_READ);
29448 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
29449 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29450 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29451 pFile->lastErrno = error;
29452 return SQLITE_FULL;
29453 }
@@ -28922,11 +29482,11 @@
29482 DWORD wrote = 0;
29483
29484 assert( id!=0 );
29485 SimulateIOError(return SQLITE_IOERR_WRITE);
29486 SimulateDiskfullError(return SQLITE_FULL);
29487 OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
29488 rc = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29489 if( rc==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29490 pFile->lastErrno = error;
29491 return SQLITE_FULL;
29492 }
@@ -28950,28 +29510,29 @@
29510 ** Truncate an open file to a specified size
29511 */
29512 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
29513 LONG upperBits = (LONG)((nByte>>32) & 0x7fffffff);
29514 LONG lowerBits = (LONG)(nByte & 0xffffffff);
29515 DWORD dwRet;
29516 winFile *pFile = (winFile*)id;
29517 DWORD error;
29518 int rc = SQLITE_OK;
29519
29520 assert( id!=0 );
29521 OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
29522 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
29523 dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
29524 if( dwRet==INVALID_SET_FILE_POINTER && (error=GetLastError())!=NO_ERROR ){
29525 pFile->lastErrno = error;
29526 rc = SQLITE_IOERR_TRUNCATE;
 
29527 /* SetEndOfFile will fail if nByte is negative */
29528 }else if( !SetEndOfFile(pFile->h) ){
29529 pFile->lastErrno = GetLastError();
29530 rc = SQLITE_IOERR_TRUNCATE;
29531 }
29532 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc==SQLITE_OK ? "ok" : "failed"));
29533 return rc;
29534 }
29535
29536 #ifdef SQLITE_TEST
29537 /*
29538 ** Count the number of fullsyncs and normal syncs. This is used to test
@@ -28987,11 +29548,11 @@
29548 static int winSync(sqlite3_file *id, int flags){
29549 #ifndef SQLITE_NO_SYNC
29550 winFile *pFile = (winFile*)id;
29551
29552 assert( id!=0 );
29553 OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
29554 #else
29555 UNUSED_PARAMETER(id);
29556 #endif
29557 #ifndef SQLITE_TEST
29558 UNUSED_PARAMETER(flags);
@@ -29128,12 +29689,12 @@
29689 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
29690 winFile *pFile = (winFile*)id;
29691 DWORD error = NO_ERROR;
29692
29693 assert( id!=0 );
29694 OSTRACE(("LOCK %d %d was %d(%d)\n",
29695 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
29696
29697 /* If there is already a lock of this type or more restrictive on the
29698 ** OsFile, do nothing. Don't use the end_lock: exit path, as
29699 ** sqlite3OsEnterMutex() hasn't been called yet.
29700 */
@@ -29159,11 +29720,11 @@
29720 int cnt = 3;
29721 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
29722 /* Try 3 times to get the pending lock. The pending lock might be
29723 ** held by another reader process who will release it momentarily.
29724 */
29725 OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
29726 Sleep(1);
29727 }
29728 gotPendingLock = res;
29729 if( !res ){
29730 error = GetLastError();
@@ -29204,17 +29765,17 @@
29765 /* Acquire an EXCLUSIVE lock
29766 */
29767 if( locktype==EXCLUSIVE_LOCK && res ){
29768 assert( pFile->locktype>=SHARED_LOCK );
29769 res = unlockReadLock(pFile);
29770 OSTRACE(("unreadlock = %d\n", res));
29771 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
29772 if( res ){
29773 newLocktype = EXCLUSIVE_LOCK;
29774 }else{
29775 error = GetLastError();
29776 OSTRACE(("error-code = %d\n", error));
29777 getReadLock(pFile);
29778 }
29779 }
29780
29781 /* If we are holding a PENDING lock that ought to be released, then
@@ -29228,12 +29789,12 @@
29789 ** return the appropriate result code.
29790 */
29791 if( res ){
29792 rc = SQLITE_OK;
29793 }else{
29794 OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
29795 locktype, newLocktype));
29796 pFile->lastErrno = error;
29797 rc = SQLITE_BUSY;
29798 }
29799 pFile->locktype = (u8)newLocktype;
29800 return rc;
@@ -29249,18 +29810,18 @@
29810 winFile *pFile = (winFile*)id;
29811
29812 assert( id!=0 );
29813 if( pFile->locktype>=RESERVED_LOCK ){
29814 rc = 1;
29815 OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
29816 }else{
29817 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
29818 if( rc ){
29819 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
29820 }
29821 rc = !rc;
29822 OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
29823 }
29824 *pResOut = rc;
29825 return SQLITE_OK;
29826 }
29827
@@ -29279,12 +29840,12 @@
29840 int type;
29841 winFile *pFile = (winFile*)id;
29842 int rc = SQLITE_OK;
29843 assert( pFile!=0 );
29844 assert( locktype<=SHARED_LOCK );
29845 OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
29846 pFile->locktype, pFile->sharedLockByte));
29847 type = pFile->locktype;
29848 if( type>=EXCLUSIVE_LOCK ){
29849 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
29850 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
29851 /* This should never happen. We should always be able to
@@ -29316,10 +29877,15 @@
29877 }
29878 case SQLITE_LAST_ERRNO: {
29879 *(int*)pArg = (int)((winFile*)id)->lastErrno;
29880 return SQLITE_OK;
29881 }
29882 case SQLITE_FCNTL_SIZE_HINT: {
29883 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
29884 winTruncate(id, sz);
29885 return SQLITE_OK;
29886 }
29887 }
29888 return SQLITE_ERROR;
29889 }
29890
29891 /*
@@ -29343,16 +29909,535 @@
29909 static int winDeviceCharacteristics(sqlite3_file *id){
29910 UNUSED_PARAMETER(id);
29911 return 0;
29912 }
29913
29914 /****************************************************************************
29915 ********************************* Shared Memory *****************************
29916 **
29917 ** The next subdivision of code manages the shared-memory primitives.
29918 */
29919 #ifndef SQLITE_OMIT_WAL
29920
29921 /*
29922 ** Helper functions to obtain and relinquish the global mutex. The
29923 ** global mutex is used to protect the winLockInfo objects used by
29924 ** this file, all of which may be shared by multiple threads.
29925 **
29926 ** Function winShmMutexHeld() is used to assert() that the global mutex
29927 ** is held when required. This function is only used as part of assert()
29928 ** statements. e.g.
29929 **
29930 ** winShmEnterMutex()
29931 ** assert( winShmMutexHeld() );
29932 ** winEnterLeave()
29933 */
29934 static void winShmEnterMutex(void){
29935 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29936 }
29937 static void winShmLeaveMutex(void){
29938 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29939 }
29940 #ifdef SQLITE_DEBUG
29941 static int winShmMutexHeld(void) {
29942 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29943 }
29944 #endif
29945
29946 /*
29947 ** Object used to represent a single file opened and mmapped to provide
29948 ** shared memory. When multiple threads all reference the same
29949 ** log-summary, each thread has its own winFile object, but they all
29950 ** point to a single instance of this object. In other words, each
29951 ** log-summary is opened only once per process.
29952 **
29953 ** winShmMutexHeld() must be true when creating or destroying
29954 ** this object or while reading or writing the following fields:
29955 **
29956 ** nRef
29957 ** pNext
29958 **
29959 ** The following fields are read-only after the object is created:
29960 **
29961 ** fid
29962 ** zFilename
29963 **
29964 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
29965 ** winShmMutexHeld() is true when reading or writing any other field
29966 ** in this structure.
29967 **
29968 ** To avoid deadlocks, mutex and mutexBuf are always released in the
29969 ** reverse order that they are acquired. mutexBuf is always acquired
29970 ** first and released last. This invariant is check by asserting
29971 ** sqlite3_mutex_notheld() on mutex whenever mutexBuf is acquired or
29972 ** released.
29973 */
29974 struct winShmNode {
29975 sqlite3_mutex *mutex; /* Mutex to access this object */
29976 char *zFilename; /* Name of the file */
29977 winFile hFile; /* File handle from winOpen */
29978
29979 int szRegion; /* Size of shared-memory regions */
29980 int nRegion; /* Size of array apRegion */
29981 struct ShmRegion {
29982 HANDLE hMap; /* File handle from CreateFileMapping */
29983 void *pMap;
29984 } *aRegion;
29985 DWORD lastErrno; /* The Windows errno from the last I/O error */
29986
29987 int nRef; /* Number of winShm objects pointing to this */
29988 winShm *pFirst; /* All winShm objects pointing to this */
29989 winShmNode *pNext; /* Next in list of all winShmNode objects */
29990 #ifdef SQLITE_DEBUG
29991 u8 nextShmId; /* Next available winShm.id value */
29992 #endif
29993 };
29994
29995 /*
29996 ** A global array of all winShmNode objects.
29997 **
29998 ** The winShmMutexHeld() must be true while reading or writing this list.
29999 */
30000 static winShmNode *winShmNodeList = 0;
30001
30002 /*
30003 ** Structure used internally by this VFS to record the state of an
30004 ** open shared memory connection.
30005 **
30006 ** winShm.pFile->mutex must be held while reading or writing the
30007 ** winShm.pNext and winShm.locks[] elements.
30008 **
30009 ** The winShm.pFile element is initialized when the object is created
30010 ** and is read-only thereafter.
30011 */
30012 struct winShm {
30013 winShmNode *pShmNode; /* The underlying winShmNode object */
30014 winShm *pNext; /* Next winShm with the same winShmNode */
30015 u8 hasMutex; /* True if holding the winShmNode mutex */
30016 u8 hasMutexBuf; /* True if holding pFile->mutexBuf */
30017 #ifdef SQLITE_DEBUG
30018 u8 id; /* Id of this connection with its winShmNode */
30019 #endif
30020 };
30021
30022 /*
30023 ** Constants used for locking
30024 */
30025 #define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30026 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30027
30028 /*
30029 ** Apply advisory locks for all n bytes beginning at ofst.
30030 */
30031 #define _SHM_UNLCK 1
30032 #define _SHM_RDLCK 2
30033 #define _SHM_WRLCK 3
30034 static int winShmSystemLock(
30035 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
30036 int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
30037 int ofst, /* Offset to first byte to be locked/unlocked */
30038 int nByte /* Number of bytes to lock or unlock */
30039 ){
30040 OVERLAPPED ovlp;
30041 DWORD dwFlags;
30042 int rc = 0; /* Result code form Lock/UnlockFileEx() */
30043
30044 /* Access to the winShmNode object is serialized by the caller */
30045 assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
30046
30047 /* Initialize the locking parameters */
30048 dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
30049 if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
30050
30051 /* Find the first bit in lockMask that is set */
30052 memset(&ovlp, 0, sizeof(OVERLAPPED));
30053 ovlp.Offset = ofst;
30054
30055 /* Release/Acquire the system-level lock */
30056 if( lockType==_SHM_UNLCK ){
30057 rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
30058 }else{
30059 rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
30060 }
30061 if( !rc ){
30062 OSTRACE(("SHM-LOCK %d %s ERROR 0x%08lx\n",
30063 pFile->hFile.h,
30064 lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
30065 GetLastError()));
30066 }
30067 rc = (rc!=0) ? SQLITE_OK : SQLITE_BUSY;
30068
30069 return rc;
30070 }
30071
30072 /* Forward references to VFS methods */
30073 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
30074 static int winDelete(sqlite3_vfs *,const char*,int);
30075
30076 /*
30077 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
30078 **
30079 ** This is not a VFS shared-memory method; it is a utility function called
30080 ** by VFS shared-memory methods.
30081 */
30082 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
30083 winShmNode **pp;
30084 winShmNode *p;
30085 assert( winShmMutexHeld() );
30086 pp = &winShmNodeList;
30087 while( (p = *pp)!=0 ){
30088 if( p->nRef==0 ){
30089 int i;
30090 if( p->mutex ) sqlite3_mutex_free(p->mutex);
30091 for(i=0; i<p->nRegion; i++){
30092 UnmapViewOfFile(p->aRegion[i].pMap);
30093 CloseHandle(p->aRegion[i].hMap);
30094 }
30095 if( p->hFile.h != INVALID_HANDLE_VALUE ) {
30096 winClose((sqlite3_file *)&p->hFile);
30097 }
30098 if( deleteFlag ) winDelete(pVfs, p->zFilename, 0);
30099 *pp = p->pNext;
30100 sqlite3_free(p->aRegion);
30101 sqlite3_free(p);
30102 }else{
30103 pp = &p->pNext;
30104 }
30105 }
30106 }
30107
30108 /*
30109 ** Open a shared-memory area. This particular implementation uses
30110 ** mmapped files.
30111 **
30112 ** zName is a filename used to identify the shared-memory area. The
30113 ** implementation does not (and perhaps should not) use this name
30114 ** directly, but rather use it as a template for finding an appropriate
30115 ** name for the shared-memory storage. In this implementation, the
30116 ** string "-index" is appended to zName and used as the name of the
30117 ** mmapped file.
30118 **
30119 ** When opening a new shared-memory file, if no other instances of that
30120 ** file are currently open, in this process or in other processes, then
30121 ** the file must be truncated to zero length or have its header cleared.
30122 */
30123 static int winShmOpen(
30124 sqlite3_file *fd /* The file to which to attach shared memory */
30125 ){
30126 struct winFile *pDbFd; /* Database to which to attach SHM */
30127 struct winShm *p; /* The connection to be opened */
30128 struct winShmNode *pShmNode = 0; /* The underlying mmapped file */
30129 int rc; /* Result code */
30130 struct winShmNode *pNew; /* Newly allocated winShmNode */
30131 int nName; /* Size of zName in bytes */
30132
30133 pDbFd = (winFile*)fd;
30134 assert( pDbFd->pShm==0 ); /* Not previously opened */
30135
30136 /* Allocate space for the new sqlite3_shm object. Also speculatively
30137 ** allocate space for a new winShmNode and filename.
30138 */
30139 p = sqlite3_malloc( sizeof(*p) );
30140 if( p==0 ) return SQLITE_NOMEM;
30141 memset(p, 0, sizeof(*p));
30142 nName = sqlite3Strlen30(pDbFd->zPath);
30143 pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
30144 if( pNew==0 ){
30145 sqlite3_free(p);
30146 return SQLITE_NOMEM;
30147 }
30148 memset(pNew, 0, sizeof(*pNew));
30149 pNew->zFilename = (char*)&pNew[1];
30150 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-wal-index", pDbFd->zPath);
30151
30152 /* Look to see if there is an existing winShmNode that can be used.
30153 ** If no matching winShmNode currently exists, create a new one.
30154 */
30155 winShmEnterMutex();
30156 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
30157 /* TBD need to come up with better match here. Perhaps
30158 ** use FILE_ID_BOTH_DIR_INFO Structure.
30159 */
30160 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
30161 }
30162 if( pShmNode ){
30163 sqlite3_free(pNew);
30164 }else{
30165 pShmNode = pNew;
30166 pNew = 0;
30167 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
30168 pShmNode->pNext = winShmNodeList;
30169 winShmNodeList = pShmNode;
30170
30171 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
30172 if( pShmNode->mutex==0 ){
30173 rc = SQLITE_NOMEM;
30174 goto shm_open_err;
30175 }
30176 rc = winOpen(pDbFd->pVfs,
30177 pShmNode->zFilename, /* Name of the file (UTF-8) */
30178 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
30179 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
30180 0);
30181 if( SQLITE_OK!=rc ){
30182 rc = SQLITE_CANTOPEN_BKPT;
30183 goto shm_open_err;
30184 }
30185
30186 /* Check to see if another process is holding the dead-man switch.
30187 ** If not, truncate the file to zero length.
30188 */
30189 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
30190 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
30191 }
30192 if( rc==SQLITE_OK ){
30193 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
30194 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
30195 }
30196 if( rc ) goto shm_open_err;
30197 }
30198
30199 /* Make the new connection a child of the winShmNode */
30200 p->pShmNode = pShmNode;
30201 p->pNext = pShmNode->pFirst;
30202 #ifdef SQLITE_DEBUG
30203 p->id = pShmNode->nextShmId++;
30204 #endif
30205 pShmNode->pFirst = p;
30206 pShmNode->nRef++;
30207 pDbFd->pShm = p;
30208 winShmLeaveMutex();
30209 return SQLITE_OK;
30210
30211 /* Jump here on any error */
30212 shm_open_err:
30213 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
30214 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
30215 sqlite3_free(p);
30216 sqlite3_free(pNew);
30217 winShmLeaveMutex();
30218 return rc;
30219 }
30220
30221 /*
30222 ** Close a connection to shared-memory. Delete the underlying
30223 ** storage if deleteFlag is true.
30224 */
30225 static int winShmClose(
30226 sqlite3_file *fd, /* Database holding shared memory */
30227 int deleteFlag /* Delete after closing if true */
30228 ){
30229 winFile *pDbFd; /* Database holding shared-memory */
30230 winShm *p; /* The connection to be closed */
30231 winShmNode *pShmNode; /* The underlying shared-memory file */
30232 winShm **pp; /* For looping over sibling connections */
30233
30234 pDbFd = (winFile*)fd;
30235 p = pDbFd->pShm;
30236 pShmNode = p->pShmNode;
30237
30238 /* Remove connection p from the set of connections associated
30239 ** with pShmNode */
30240 sqlite3_mutex_enter(pShmNode->mutex);
30241 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
30242 *pp = p->pNext;
30243
30244 /* Free the connection p */
30245 sqlite3_free(p);
30246 pDbFd->pShm = 0;
30247 sqlite3_mutex_leave(pShmNode->mutex);
30248
30249 /* If pShmNode->nRef has reached 0, then close the underlying
30250 ** shared-memory file, too */
30251 winShmEnterMutex();
30252 assert( pShmNode->nRef>0 );
30253 pShmNode->nRef--;
30254 if( pShmNode->nRef==0 ){
30255 winShmPurge(pDbFd->pVfs, deleteFlag);
30256 }
30257 winShmLeaveMutex();
30258
30259 return SQLITE_OK;
30260 }
30261
30262 /*
30263 ** This function is called to obtain a pointer to region iRegion of the
30264 ** shared-memory associated with the database file fd. Shared-memory regions
30265 ** are numbered starting from zero. Each shared-memory region is szRegion
30266 ** bytes in size.
30267 **
30268 ** If an error occurs, an error code is returned and *pp is set to NULL.
30269 **
30270 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
30271 ** region has not been allocated (by any client, including one running in a
30272 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
30273 ** isWrite is non-zero and the requested shared-memory region has not yet
30274 ** been allocated, it is allocated by this function.
30275 **
30276 ** If the shared-memory region has already been allocated or is allocated by
30277 ** this call as described above, then it is mapped into this processes
30278 ** address space (if it is not already), *pp is set to point to the mapped
30279 ** memory and SQLITE_OK returned.
30280 */
30281 static int winShmMap(
30282 sqlite3_file *fd, /* Handle open on database file */
30283 int iRegion, /* Region to retrieve */
30284 int szRegion, /* Size of regions */
30285 int isWrite, /* True to extend file if necessary */
30286 void volatile **pp /* OUT: Mapped memory */
30287 ){
30288 winFile *pDbFd = (winFile*)fd;
30289 winShm *p = pDbFd->pShm;
30290 winShmNode *pShmNode = p->pShmNode;
30291 int rc = SQLITE_OK;
30292
30293 sqlite3_mutex_enter(pShmNode->mutex);
30294 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
30295
30296 if( pShmNode->nRegion<=iRegion ){
30297 struct ShmRegion *apNew; /* New aRegion[] array */
30298 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
30299 sqlite3_int64 sz; /* Current size of wal-index file */
30300
30301 pShmNode->szRegion = szRegion;
30302
30303 /* The requested region is not mapped into this processes address space.
30304 ** Check to see if it has been allocated (i.e. if the wal-index file is
30305 ** large enough to contain the requested region).
30306 */
30307 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
30308 if( rc!=SQLITE_OK ){
30309 goto shmpage_out;
30310 }
30311
30312 if( sz<nByte ){
30313 /* The requested memory region does not exist. If isWrite is set to
30314 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
30315 **
30316 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
30317 ** the requested memory region.
30318 */
30319 if( !isWrite ) goto shmpage_out;
30320 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
30321 if( rc!=SQLITE_OK ){
30322 goto shmpage_out;
30323 }
30324 }
30325
30326 /* Map the requested memory region into this processes address space. */
30327 apNew = (struct ShmRegion *)sqlite3_realloc(
30328 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
30329 );
30330 if( !apNew ){
30331 rc = SQLITE_IOERR_NOMEM;
30332 goto shmpage_out;
30333 }
30334 pShmNode->aRegion = apNew;
30335
30336 while( pShmNode->nRegion<=iRegion ){
30337 HANDLE hMap; /* file-mapping handle */
30338 void *pMap = 0; /* Mapped memory region */
30339
30340 hMap = CreateFileMapping(pShmNode->hFile.h,
30341 NULL, PAGE_READWRITE, 0, nByte, NULL
30342 );
30343 if( hMap ){
30344 pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
30345 0, 0, nByte
30346 );
30347 }
30348 if( !pMap ){
30349 pShmNode->lastErrno = GetLastError();
30350 rc = SQLITE_IOERR;
30351 if( hMap ) CloseHandle(hMap);
30352 goto shmpage_out;
30353 }
30354
30355 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
30356 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
30357 pShmNode->nRegion++;
30358 }
30359 }
30360
30361 shmpage_out:
30362 if( pShmNode->nRegion>iRegion ){
30363 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
30364 *pp = (void *)&p[iRegion*szRegion];
30365 }else{
30366 *pp = 0;
30367 }
30368 sqlite3_mutex_leave(pShmNode->mutex);
30369 return rc;
30370 }
30371
30372 /*
30373 ** Change the lock state for a shared-memory segment.
30374 */
30375 static int winShmLock(
30376 sqlite3_file *fd, /* Database file holding the shared memory */
30377 int ofst, /* First lock to acquire or release */
30378 int n, /* Number of locks to acquire or release */
30379 int flags /* What to do with the lock */
30380 ){
30381 winFile *pDbFd = (winFile*)fd;
30382 winShm *p = pDbFd->pShm;
30383 winShmNode *pShmNode = p->pShmNode;
30384 int rc = SQLITE_PROTOCOL;
30385
30386 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
30387 assert( n>=1 );
30388 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
30389 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
30390 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
30391 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
30392 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
30393
30394 sqlite3_mutex_enter(pShmNode->mutex);
30395 if( flags & SQLITE_SHM_UNLOCK ){
30396 rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
30397 }else if( flags & SQLITE_SHM_SHARED ){
30398 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
30399 }else{
30400 rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
30401 }
30402 sqlite3_mutex_leave(pShmNode->mutex);
30403 OSTRACE(("SHM-LOCK shmid-%d, pid-%d %s\n",
30404 p->id, (int)GetCurrentProcessId(), rc ? "failed" : "ok"));
30405 return rc;
30406 }
30407
30408 /*
30409 ** Implement a memory barrier or memory fence on shared memory.
30410 **
30411 ** All loads and stores begun before the barrier must complete before
30412 ** any load or store begun after the barrier.
30413 */
30414 static void winShmBarrier(
30415 sqlite3_file *fd /* Database holding the shared memory */
30416 ){
30417 /* MemoryBarrier(); // does not work -- do not know why not */
30418 winShmEnterMutex();
30419 winShmLeaveMutex();
30420 }
30421
30422 #else
30423 # define winShmOpen 0
30424 # define winShmMap 0
30425 # define winShmLock 0
30426 # define winShmBarrier 0
30427 # define winShmClose 0
30428 #endif /* #ifndef SQLITE_OMIT_WAL */
30429 /*
30430 ***************************** End Shared Memory *****************************
30431 ****************************************************************************/
30432
30433 /*
30434 ** This vector defines all the methods that can operate on an
30435 ** sqlite3_file for win32.
30436 */
30437 static const sqlite3_io_methods winIoMethod = {
30438 2, /* iVersion */
30439 winClose,
30440 winRead,
30441 winWrite,
30442 winTruncate,
30443 winSync,
@@ -29360,11 +30445,16 @@
30445 winLock,
30446 winUnlock,
30447 winCheckReservedLock,
30448 winFileControl,
30449 winSectorSize,
30450 winDeviceCharacteristics,
30451 winShmOpen, /* xShmOpen */
30452 winShmLock, /* xShmLock */
30453 winShmMap, /* xShmMap */
30454 winShmBarrier, /* xShmBarrier */
30455 winShmClose /* xShmClose */
30456 };
30457
30458 /***************************************************************************
30459 ** Here ends the I/O methods that form the sqlite3_io_methods object.
30460 **
@@ -29442,11 +30532,11 @@
30532 sqlite3_randomness(20, &zBuf[j]);
30533 for(i=0; i<20; i++, j++){
30534 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
30535 }
30536 zBuf[j] = 0;
30537 OSTRACE(("TEMP FILENAME: %s\n", zBuf));
30538 return SQLITE_OK;
30539 }
30540
30541 /*
30542 ** The return value of getLastErrorMsg
@@ -29533,10 +30623,12 @@
30623 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
30624 char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
30625
30626 assert( id!=0 );
30627 UNUSED_PARAMETER(pVfs);
30628
30629 pFile->h = INVALID_HANDLE_VALUE;
30630
30631 /* If the second argument to this function is NULL, generate a
30632 ** temporary file name to use
30633 */
30634 if( !zUtf8Name ){
@@ -29615,10 +30707,13 @@
30707 dwFlagsAndAttributes,
30708 NULL
30709 );
30710 #endif
30711 }
30712 OSTRACE(("OPEN %d %s 0x%lx %s\n",
30713 h, zName, dwDesiredAccess,
30714 h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
30715 if( h==INVALID_HANDLE_VALUE ){
30716 free(zConverted);
30717 if( flags & SQLITE_OPEN_READWRITE ){
30718 return winOpen(pVfs, zName, id,
30719 ((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
@@ -29635,10 +30730,13 @@
30730 }
30731 memset(pFile, 0, sizeof(*pFile));
30732 pFile->pMethod = &winIoMethod;
30733 pFile->h = h;
30734 pFile->lastErrno = NO_ERROR;
30735 pFile->pVfs = pVfs;
30736 pFile->pShm = 0;
30737 pFile->zPath = zName;
30738 pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
30739 #if SQLITE_OS_WINCE
30740 if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
30741 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
30742 && !winceCreateLock(zName, pFile)
@@ -29706,11 +30804,14 @@
30804 && (++cnt < MX_DELETION_ATTEMPTS)
30805 && (Sleep(100), 1) );
30806 #endif
30807 }
30808 free(zConverted);
30809 OSTRACE(("DELETE \"%s\" %s\n", zFilename,
30810 ( (rc==INVALID_FILE_ATTRIBUTES) && (error==ERROR_FILE_NOT_FOUND)) ?
30811 "ok" : "failed" ));
30812
30813 return ( (rc == INVALID_FILE_ATTRIBUTES)
30814 && (error == ERROR_FILE_NOT_FOUND)) ? SQLITE_OK : SQLITE_IOERR_DELETE;
30815 }
30816
30817 /*
@@ -29999,38 +31100,36 @@
31100 UNUSED_PARAMETER(pVfs);
31101 return ((microsec+999)/1000)*1000;
31102 }
31103
31104 /*
31105 ** The following variable, if set to a non-zero value, is interpreted as
31106 ** the number of seconds since 1970 and is used to set the result of
31107 ** sqlite3OsCurrentTime() during testing.
31108 */
31109 #ifdef SQLITE_TEST
31110 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
31111 #endif
31112
31113 /*
31114 ** Find the current time (in Universal Coordinated Time). Write into *piNow
31115 ** the current time and date as a Julian Day number times 86_400_000. In
31116 ** other words, write into *piNow the number of milliseconds since the Julian
31117 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
31118 ** proleptic Gregorian calendar.
31119 **
31120 ** On success, return 0. Return 1 if the time and date cannot be found.
31121 */
31122 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
 
31123 /* FILETIME structure is a 64-bit value representing the number of
31124 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
31125 */
31126 FILETIME ft;
31127 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
31128 #ifdef SQLITE_TEST
31129 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
31130 #endif
 
 
 
 
 
 
31131 /* 2^32 - to avoid use of LL and warnings in gcc */
31132 static const sqlite3_int64 max32BitValue =
31133 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
31134
31135 #if SQLITE_OS_WINCE
@@ -30041,26 +31140,38 @@
31140 return 1;
31141 }
31142 #else
31143 GetSystemTimeAsFileTime( &ft );
31144 #endif
31145
31146 *piNow = winFiletimeEpoch +
31147 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
31148 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)1000;
31149
 
 
 
 
31150 #ifdef SQLITE_TEST
31151 if( sqlite3_current_time ){
31152 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
31153 }
31154 #endif
31155 UNUSED_PARAMETER(pVfs);
31156 return 0;
31157 }
31158
31159 /*
31160 ** Find the current time (in Universal Coordinated Time). Write the
31161 ** current time and date as a Julian Day number into *prNow and
31162 ** return 0. Return 1 if the time and date cannot be found.
31163 */
31164 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
31165 int rc;
31166 sqlite3_int64 i;
31167 rc = winCurrentTimeInt64(pVfs, &i);
31168 if( !rc ){
31169 *prNow = i/86400000.0;
31170 }
31171 return rc;
31172 }
31173
31174 /*
31175 ** The idea is that this function works like a combination of
31176 ** GetLastError() and FormatMessage() on windows (or errno and
31177 ** strerror_r() on unix). After an error is returned by an OS
@@ -30092,35 +31203,38 @@
31203 */
31204 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
31205 UNUSED_PARAMETER(pVfs);
31206 return getLastErrorMsg(nBuf, zBuf);
31207 }
31208
31209
31210
31211 /*
31212 ** Initialize and deinitialize the operating system interface.
31213 */
31214 SQLITE_API int sqlite3_os_init(void){
31215 static sqlite3_vfs winVfs = {
31216 2, /* iVersion */
31217 sizeof(winFile), /* szOsFile */
31218 MAX_PATH, /* mxPathname */
31219 0, /* pNext */
31220 "win32", /* zName */
31221 0, /* pAppData */
31222 winOpen, /* xOpen */
31223 winDelete, /* xDelete */
31224 winAccess, /* xAccess */
31225 winFullPathname, /* xFullPathname */
31226 winDlOpen, /* xDlOpen */
31227 winDlError, /* xDlError */
31228 winDlSym, /* xDlSym */
31229 winDlClose, /* xDlClose */
31230 winRandomness, /* xRandomness */
31231 winSleep, /* xSleep */
31232 winCurrentTime, /* xCurrentTime */
31233 winGetLastError, /* xGetLastError */
31234 0, /* xRename */
31235 winCurrentTimeInt64, /* xCurrentTimeInt64 */
31236 };
31237
31238 sqlite3_vfs_register(&winVfs, 1);
31239 return SQLITE_OK;
31240 }
@@ -30801,19 +31915,21 @@
31915 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
31916 }
31917
31918 if( pPage ){
31919 if( !pPage->pData ){
31920 memset(pPage, 0, sizeof(PgHdr));
31921 pPage->pData = (void *)&pPage[1];
31922 pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
31923 memset(pPage->pExtra, 0, pCache->szExtra);
31924 pPage->pCache = pCache;
31925 pPage->pgno = pgno;
31926 }
31927 assert( pPage->pCache==pCache );
31928 assert( pPage->pgno==pgno );
31929 assert( pPage->pData==(void *)&pPage[1] );
31930 assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
31931
31932 if( 0==pPage->nRef ){
31933 pCache->nRef++;
31934 }
31935 pPage->nRef++;
@@ -31840,11 +32956,11 @@
32956 ** This function is called during initialization (sqlite3_initialize()) to
32957 ** install the default pluggable cache module, assuming the user has not
32958 ** already provided an alternative.
32959 */
32960 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
32961 static const sqlite3_pcache_methods defaultMethods = {
32962 0, /* pArg */
32963 pcache1Init, /* xInit */
32964 pcache1Shutdown, /* xShutdown */
32965 pcache1Create, /* xCreate */
32966 pcache1Cachesize, /* xCachesize */
@@ -32353,10 +33469,120 @@
33469 ** locking to prevent two processes from writing the same database
33470 ** file simultaneously, or one process from reading the database while
33471 ** another is writing.
33472 */
33473 #ifndef SQLITE_OMIT_DISKIO
33474 /************** Include wal.h in the middle of pager.c ***********************/
33475 /************** Begin file wal.h *********************************************/
33476 /*
33477 ** 2010 February 1
33478 **
33479 ** The author disclaims copyright to this source code. In place of
33480 ** a legal notice, here is a blessing:
33481 **
33482 ** May you do good and not evil.
33483 ** May you find forgiveness for yourself and forgive others.
33484 ** May you share freely, never taking more than you give.
33485 **
33486 *************************************************************************
33487 ** This header file defines the interface to the write-ahead logging
33488 ** system. Refer to the comments below and the header comment attached to
33489 ** the implementation of each function in log.c for further details.
33490 */
33491
33492 #ifndef _WAL_H_
33493 #define _WAL_H_
33494
33495
33496 #ifdef SQLITE_OMIT_WAL
33497 # define sqlite3WalOpen(x,y,z) 0
33498 # define sqlite3WalClose(w,x,y,z) 0
33499 # define sqlite3WalBeginReadTransaction(y,z) 0
33500 # define sqlite3WalEndReadTransaction(z)
33501 # define sqlite3WalRead(v,w,x,y,z) 0
33502 # define sqlite3WalDbsize(y,z)
33503 # define sqlite3WalBeginWriteTransaction(y) 0
33504 # define sqlite3WalEndWRiteTransaction(x) 0
33505 # define sqlite3WalUndo(x,y,z) 0
33506 # define sqlite3WalSavepoint(y,z)
33507 # define sqlite3WalSavepointUndo(y,z) 0
33508 # define sqlite3WalFrames(u,v,w,x,y,z) 0
33509 # define sqlite3WalCheckpoint(u,v,w,x) 0
33510 # define sqlite3WalCallback(z) 0
33511 #else
33512
33513 #define WAL_SAVEPOINT_NDATA 4
33514
33515 /* Connection to a write-ahead log (WAL) file.
33516 ** There is one object of this type for each pager.
33517 */
33518 typedef struct Wal Wal;
33519
33520 /* Open and close a connection to a write-ahead log. */
33521 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *zName, Wal**);
33522 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
33523
33524 /* Used by readers to open (lock) and close (unlock) a snapshot. A
33525 ** snapshot is like a read-transaction. It is the state of the database
33526 ** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
33527 ** preserves the current state even if the other threads or processes
33528 ** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
33529 ** transaction and releases the lock.
33530 */
33531 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
33532 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
33533
33534 /* Read a page from the write-ahead log, if it is present. */
33535 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
33536
33537 /* Return the size of the database as it existed at the beginning
33538 ** of the snapshot */
33539 SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno);
33540
33541 /* Obtain or release the WRITER lock. */
33542 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
33543 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
33544
33545 /* Undo any frames written (but not committed) to the log */
33546 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
33547
33548 /* Return an integer that records the current (uncommitted) write
33549 ** position in the WAL */
33550 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
33551
33552 /* Move the write position of the WAL back to iFrame. Called in
33553 ** response to a ROLLBACK TO command. */
33554 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
33555
33556 /* Write a frame or frames to the log. */
33557 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
33558
33559 /* Copy pages from the log to the database file */
33560 SQLITE_PRIVATE int sqlite3WalCheckpoint(
33561 Wal *pWal, /* Write-ahead log connection */
33562 int sync_flags, /* Flags to sync db file with (or 0) */
33563 int nBuf, /* Size of buffer nBuf */
33564 u8 *zBuf /* Temporary buffer to use */
33565 );
33566
33567 /* Return the value to pass to a sqlite3_wal_hook callback, the
33568 ** number of frames in the WAL at the point of the last commit since
33569 ** sqlite3WalCallback() was called. If no commits have occurred since
33570 ** the last call, then return 0.
33571 */
33572 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
33573
33574 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
33575 ** by the pager layer on the database file.
33576 */
33577 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
33578
33579 #endif /* ifndef SQLITE_OMIT_WAL */
33580 #endif /* _WAL_H_ */
33581
33582 /************** End of wal.h *************************************************/
33583 /************** Continuing where we left off in pager.c **********************/
33584
33585 /*
33586 ******************** NOTES ON THE DESIGN OF THE PAGER ************************
33587 **
33588 ** Within this comment block, a page is deemed to have been synced
@@ -32553,10 +33779,11 @@
33779 i64 iOffset; /* Starting offset in main journal */
33780 i64 iHdrOffset; /* See above */
33781 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
33782 Pgno nOrig; /* Original number of pages in file */
33783 Pgno iSubRec; /* Index of first record in sub-journal */
33784 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
33785 };
33786
33787 /*
33788 ** A open page cache is an instance of the following structure.
33789 **
@@ -32730,10 +33957,13 @@
33957 void *pCodec; /* First argument to xCodec... methods */
33958 #endif
33959 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
33960 PCache *pPCache; /* Pointer to page cache object */
33961 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
33962 #ifndef SQLITE_OMIT_WAL
33963 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
33964 #endif
33965 };
33966
33967 /*
33968 ** The following global variables hold counters used for
33969 ** testing purposes only. These variables do not exist in
@@ -33518,10 +34748,26 @@
34748 }
34749 }
34750 return rc;
34751 }
34752
34753 /*
34754 ** Return true if this pager uses a write-ahead log instead of the usual
34755 ** rollback journal. Otherwise false.
34756 */
34757 #ifndef SQLITE_OMIT_WAL
34758 static int pagerUseWal(Pager *pPager){
34759 return (pPager->pWal!=0);
34760 }
34761 #else
34762 # define pagerUseWal(x) 0
34763 # define pagerRollbackWal(x) 0
34764 # define pagerWalFrames(v,w,x,y,z) 0
34765 # define pagerOpenWalIfPresent(z) SQLITE_OK
34766 # define pagerBeginReadTransaction(z) SQLITE_OK
34767 #endif
34768
34769 /*
34770 ** Unlock the database file. This function is a no-op if the pager
34771 ** is in exclusive mode.
34772 **
34773 ** If the pager is currently in error state, discard the contents of
@@ -33530,11 +34776,11 @@
34776 ** on the pager file (by this or any other process), it will be
34777 ** treated as a hot-journal and rolled back.
34778 */
34779 static void pager_unlock(Pager *pPager){
34780 if( !pPager->exclusiveMode ){
34781 int rc = SQLITE_OK; /* Return code */
34782
34783 /* Always close the journal file when dropping the database lock.
34784 ** Otherwise, another connection with journal_mode=delete might
34785 ** delete the file out from under us.
34786 */
@@ -33549,11 +34795,15 @@
34795 ** until the change-counter check fails in PagerSharedLock().
34796 ** Clearing the page size cache here is being conservative.
34797 */
34798 pPager->dbSizeValid = 0;
34799
34800 if( pagerUseWal(pPager) ){
34801 sqlite3WalEndReadTransaction(pPager->pWal);
34802 }else{
34803 rc = osUnlock(pPager->fd, NO_LOCK);
34804 }
34805 if( rc ){
34806 pPager->errCode = rc;
34807 }
34808 IOTRACE(("UNLOCK %p\n", pPager))
34809
@@ -33698,10 +34948,11 @@
34948 }
34949 releaseAllSavepoints(pPager);
34950
34951 assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
34952 if( isOpen(pPager->jfd) ){
34953 assert( !pagerUseWal(pPager) );
34954
34955 /* Finalize the journal file. */
34956 if( sqlite3IsMemJournal(pPager->jfd) ){
34957 assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
34958 sqlite3OsClose(pPager->jfd);
@@ -33711,24 +34962,26 @@
34962 }else{
34963 rc = sqlite3OsTruncate(pPager->jfd, 0);
34964 }
34965 pPager->journalOff = 0;
34966 pPager->journalStarted = 0;
34967 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
34968 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
34969 ){
34970 rc = zeroJournalHdr(pPager, hasMaster);
34971 pager_error(pPager, rc);
34972 pPager->journalOff = 0;
34973 pPager->journalStarted = 0;
34974 }else{
34975 /* This branch may be executed with Pager.journalMode==MEMORY if
34976 ** a hot-journal was just rolled back. In this case the journal
34977 ** file should be closed and deleted. If this connection writes to
34978 ** the database file, it will do so using an in-memory journal.
34979 */
34980 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
34981 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
34982 || pPager->journalMode==PAGER_JOURNALMODE_WAL
34983 );
34984 sqlite3OsClose(pPager->jfd);
34985 if( !pPager->tempFile ){
34986 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
34987 }
@@ -33741,11 +34994,22 @@
34994 sqlite3BitvecDestroy(pPager->pInJournal);
34995 pPager->pInJournal = 0;
34996 pPager->nRec = 0;
34997 sqlite3PcacheCleanAll(pPager->pPCache);
34998
34999 if( pagerUseWal(pPager) ){
35000 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
35001 assert( rc2==SQLITE_OK );
35002 pPager->state = PAGER_SHARED;
35003
35004 /* If the connection was in locking_mode=exclusive mode but is no longer,
35005 ** drop the EXCLUSIVE lock held on the database file.
35006 */
35007 if( !pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, 0) ){
35008 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35009 }
35010 }else if( !pPager->exclusiveMode ){
35011 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
35012 pPager->state = PAGER_SHARED;
35013 pPager->changeCountDone = 0;
35014 }else if( pPager->state==PAGER_SYNCED ){
35015 pPager->state = PAGER_EXCLUSIVE;
@@ -33852,10 +35116,11 @@
35116 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
35117 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
35118
35119 aData = pPager->pTmpSpace;
35120 assert( aData ); /* Temp storage must have already been allocated */
35121 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
35122
35123 /* Read the page number and page data from the journal or sub-journal
35124 ** file. Return an error code to the caller if an IO error occurs.
35125 */
35126 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
@@ -33921,11 +35186,15 @@
35186 **
35187 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
35188 ** is possible to fail a statement on a database that does not yet exist.
35189 ** Do not attempt to write if database file has never been opened.
35190 */
35191 if( pagerUseWal(pPager) ){
35192 pPg = 0;
35193 }else{
35194 pPg = pager_lookup(pPager, pgno);
35195 }
35196 assert( pPg || !MEMDB );
35197 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
35198 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
35199 (isMainJrnl?"main-journal":"sub-journal")
35200 ));
@@ -33938,10 +35207,11 @@
35207 && isOpen(pPager->fd)
35208 && isSynced
35209 ){
35210 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
35211 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
35212 assert( !pagerUseWal(pPager) );
35213 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
35214 if( pgno>pPager->dbFileSize ){
35215 pPager->dbFileSize = pgno;
35216 }
35217 if( pPager->pBackup ){
@@ -34002,10 +35272,11 @@
35272 ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
35273 ** be written out into the database file before its journal file
35274 ** segment is synced. If a crash occurs during or following this,
35275 ** database corruption may ensue.
35276 */
35277 assert( !pagerUseWal(pPager) );
35278 sqlite3PcacheMakeClean(pPg);
35279 }
35280 #ifdef SQLITE_CHECK_PAGES
35281 pPg->pageHash = pager_pagehash(pPg);
35282 #endif
@@ -34453,10 +35724,13 @@
35724 testcase( rc!=SQLITE_OK );
35725 }
35726 if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
35727 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
35728 }
35729 if( rc==SQLITE_OK && pPager->noSync==0 && pPager->state>=PAGER_EXCLUSIVE ){
35730 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
35731 }
35732 if( rc==SQLITE_OK ){
35733 rc = pager_end_transaction(pPager, zMaster[0]!='\0');
35734 testcase( rc!=SQLITE_OK );
35735 }
35736 if( rc==SQLITE_OK && zMaster[0] && res ){
@@ -34473,10 +35747,269 @@
35747 */
35748 setSectorSize(pPager);
35749 return rc;
35750 }
35751
35752
35753 /*
35754 ** Read the content for page pPg out of the database file and into
35755 ** pPg->pData. A shared lock or greater must be held on the database
35756 ** file before this function is called.
35757 **
35758 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
35759 ** the value read from the database file.
35760 **
35761 ** If an IO error occurs, then the IO error is returned to the caller.
35762 ** Otherwise, SQLITE_OK is returned.
35763 */
35764 static int readDbPage(PgHdr *pPg){
35765 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
35766 Pgno pgno = pPg->pgno; /* Page number to read */
35767 int rc = SQLITE_OK; /* Return code */
35768 int isInWal = 0; /* True if page is in log file */
35769 int pgsz = pPager->pageSize; /* Number of bytes to read */
35770
35771 assert( pPager->state>=PAGER_SHARED && !MEMDB );
35772 assert( isOpen(pPager->fd) );
35773
35774 if( NEVER(!isOpen(pPager->fd)) ){
35775 assert( pPager->tempFile );
35776 memset(pPg->pData, 0, pPager->pageSize);
35777 return SQLITE_OK;
35778 }
35779
35780 if( pagerUseWal(pPager) ){
35781 /* Try to pull the page from the write-ahead log. */
35782 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
35783 }
35784 if( rc==SQLITE_OK && !isInWal ){
35785 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
35786 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
35787 if( rc==SQLITE_IOERR_SHORT_READ ){
35788 rc = SQLITE_OK;
35789 }
35790 }
35791
35792 if( pgno==1 ){
35793 if( rc ){
35794 /* If the read is unsuccessful, set the dbFileVers[] to something
35795 ** that will never be a valid file version. dbFileVers[] is a copy
35796 ** of bytes 24..39 of the database. Bytes 28..31 should always be
35797 ** zero or the size of the database in page. Bytes 32..35 and 35..39
35798 ** should be page numbers which are never 0xffffffff. So filling
35799 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
35800 **
35801 ** For an encrypted database, the situation is more complex: bytes
35802 ** 24..39 of the database are white noise. But the probability of
35803 ** white noising equaling 16 bytes of 0xff is vanishingly small so
35804 ** we should still be ok.
35805 */
35806 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
35807 }else{
35808 u8 *dbFileVers = &((u8*)pPg->pData)[24];
35809 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
35810 }
35811 }
35812 CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
35813
35814 PAGER_INCR(sqlite3_pager_readdb_count);
35815 PAGER_INCR(pPager->nRead);
35816 IOTRACE(("PGIN %p %d\n", pPager, pgno));
35817 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
35818 PAGERID(pPager), pgno, pager_pagehash(pPg)));
35819
35820 return rc;
35821 }
35822
35823 #ifndef SQLITE_OMIT_WAL
35824 /*
35825 ** This function is invoked once for each page that has already been
35826 ** written into the log file when a WAL transaction is rolled back.
35827 ** Parameter iPg is the page number of said page. The pCtx argument
35828 ** is actually a pointer to the Pager structure.
35829 **
35830 ** If page iPg is present in the cache, and has no outstanding references,
35831 ** it is discarded. Otherwise, if there are one or more outstanding
35832 ** references, the page content is reloaded from the database. If the
35833 ** attempt to reload content from the database is required and fails,
35834 ** return an SQLite error code. Otherwise, SQLITE_OK.
35835 */
35836 static int pagerUndoCallback(void *pCtx, Pgno iPg){
35837 int rc = SQLITE_OK;
35838 Pager *pPager = (Pager *)pCtx;
35839 PgHdr *pPg;
35840
35841 pPg = sqlite3PagerLookup(pPager, iPg);
35842 if( pPg ){
35843 if( sqlite3PcachePageRefcount(pPg)==1 ){
35844 sqlite3PcacheDrop(pPg);
35845 }else{
35846 rc = readDbPage(pPg);
35847 if( rc==SQLITE_OK ){
35848 pPager->xReiniter(pPg);
35849 }
35850 sqlite3PagerUnref(pPg);
35851 }
35852 }
35853
35854 /* Normally, if a transaction is rolled back, any backup processes are
35855 ** updated as data is copied out of the rollback journal and into the
35856 ** database. This is not generally possible with a WAL database, as
35857 ** rollback involves simply truncating the log file. Therefore, if one
35858 ** or more frames have already been written to the log (and therefore
35859 ** also copied into the backup databases) as part of this transaction,
35860 ** the backups must be restarted.
35861 */
35862 sqlite3BackupRestart(pPager->pBackup);
35863
35864 return rc;
35865 }
35866
35867 /*
35868 ** This function is called to rollback a transaction on a WAL database.
35869 */
35870 static int pagerRollbackWal(Pager *pPager){
35871 int rc; /* Return Code */
35872 PgHdr *pList; /* List of dirty pages to revert */
35873
35874 /* For all pages in the cache that are currently dirty or have already
35875 ** been written (but not committed) to the log file, do one of the
35876 ** following:
35877 **
35878 ** + Discard the cached page (if refcount==0), or
35879 ** + Reload page content from the database (if refcount>0).
35880 */
35881 pPager->dbSize = pPager->dbOrigSize;
35882 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
35883 pList = sqlite3PcacheDirtyList(pPager->pPCache);
35884 while( pList && rc==SQLITE_OK ){
35885 PgHdr *pNext = pList->pDirty;
35886 rc = pagerUndoCallback((void *)pPager, pList->pgno);
35887 pList = pNext;
35888 }
35889
35890 return rc;
35891 }
35892
35893 /*
35894 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
35895 ** the contents of the list of pages headed by pList (connected by pDirty),
35896 ** this function notifies any active backup processes that the pages have
35897 ** changed.
35898 */
35899 static int pagerWalFrames(
35900 Pager *pPager, /* Pager object */
35901 PgHdr *pList, /* List of frames to log */
35902 Pgno nTruncate, /* Database size after this commit */
35903 int isCommit, /* True if this is a commit */
35904 int sync_flags /* Flags to pass to OsSync() (or 0) */
35905 ){
35906 int rc; /* Return code */
35907
35908 assert( pPager->pWal );
35909 rc = sqlite3WalFrames(pPager->pWal,
35910 pPager->pageSize, pList, nTruncate, isCommit, sync_flags
35911 );
35912 if( rc==SQLITE_OK && pPager->pBackup ){
35913 PgHdr *p;
35914 for(p=pList; p; p=p->pDirty){
35915 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
35916 }
35917 }
35918 return rc;
35919 }
35920
35921 /*
35922 ** Begin a read transaction on the WAL.
35923 **
35924 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
35925 ** makes a snapshot of the database at the current point in time and preserves
35926 ** that snapshot for use by the reader in spite of concurrently changes by
35927 ** other writers or checkpointers.
35928 */
35929 static int pagerBeginReadTransaction(Pager *pPager){
35930 int rc; /* Return code */
35931 int changed = 0; /* True if cache must be reset */
35932
35933 assert( pagerUseWal(pPager) );
35934
35935 /* sqlite3WalEndReadTransaction() was not called for the previous
35936 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
35937 ** are in locking_mode=NORMAL and EndRead() was previously called,
35938 ** the duplicate call is harmless.
35939 */
35940 sqlite3WalEndReadTransaction(pPager->pWal);
35941
35942 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
35943 if( rc==SQLITE_OK ){
35944 int dummy;
35945 if( changed ){
35946 pager_reset(pPager);
35947 assert( pPager->errCode || pPager->dbSizeValid==0 );
35948 }
35949 rc = sqlite3PagerPagecount(pPager, &dummy);
35950 }
35951 pPager->state = PAGER_SHARED;
35952
35953 return rc;
35954 }
35955
35956 /*
35957 ** Check if the *-wal file that corresponds to the database opened by pPager
35958 ** exists. Assuming no error occurs, set *pExists to 1 if the file exists,
35959 ** or 0 otherwise and return SQLITE_OK. If an IO or OOM error occurs, return
35960 ** an SQLite error code.
35961 */
35962 static int pagerHasWAL(Pager *pPager, int *pExists){
35963 int rc; /* Return code */
35964 char *zWal; /* Name of the WAL file */
35965
35966 assert( !pPager->tempFile );
35967 zWal = sqlite3_mprintf("%s-wal", pPager->zFilename);
35968 if( !zWal ){
35969 rc = SQLITE_NOMEM;
35970 }else{
35971 rc = sqlite3OsAccess(pPager->pVfs, zWal, SQLITE_ACCESS_EXISTS, pExists);
35972 sqlite3_free(zWal);
35973 }
35974 return rc;
35975 }
35976
35977 /*
35978 ** Check if the *-wal file that corresponds to the database opened by pPager
35979 ** exists. If it does, open the pager in WAL mode. Otherwise, if no error
35980 ** occurs, make sure Pager.journalMode is not set to PAGER_JOURNALMODE_WAL.
35981 ** If an IO or OOM error occurs, return an SQLite error code.
35982 **
35983 ** If the WAL file is opened, also open a snapshot (read transaction).
35984 **
35985 ** The caller must hold a SHARED lock on the database file to call this
35986 ** function. Because an EXCLUSIVE lock on the db file is required to delete
35987 ** a WAL, this ensures there is no race condition between the xAccess()
35988 ** below and an xDelete() being executed by some other connection.
35989 */
35990 static int pagerOpenWalIfPresent(Pager *pPager){
35991 int rc = SQLITE_OK;
35992 if( !pPager->tempFile ){
35993 int isWal; /* True if WAL file exists */
35994 rc = pagerHasWAL(pPager, &isWal);
35995 if( rc==SQLITE_OK ){
35996 if( isWal ){
35997 pager_reset(pPager);
35998 rc = sqlite3PagerOpenWal(pPager, 0);
35999 if( rc==SQLITE_OK ){
36000 rc = pagerBeginReadTransaction(pPager);
36001 }
36002 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
36003 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
36004 }
36005 }
36006 }
36007 return rc;
36008 }
36009 #endif
36010
36011 /*
36012 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
36013 ** the entire master journal file. The case pSavepoint==NULL occurs when
36014 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
36015 ** savepoint.
@@ -34529,26 +36062,31 @@
36062
36063 /* Set the database size back to the value it was before the savepoint
36064 ** being reverted was opened.
36065 */
36066 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
36067
36068 if( !pSavepoint && pagerUseWal(pPager) ){
36069 return pagerRollbackWal(pPager);
36070 }
36071
36072 /* Use pPager->journalOff as the effective size of the main rollback
36073 ** journal. The actual file might be larger than this in
36074 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
36075 ** past pPager->journalOff is off-limits to us.
36076 */
36077 szJ = pPager->journalOff;
36078 assert( pagerUseWal(pPager)==0 || szJ==0 );
36079
36080 /* Begin by rolling back records from the main journal starting at
36081 ** PagerSavepoint.iOffset and continuing to the next journal header.
36082 ** There might be records in the main journal that have a page number
36083 ** greater than the current database size (pPager->dbSize) but those
36084 ** will be skipped automatically. Pages are added to pDone as they
36085 ** are played back.
36086 */
36087 if( pSavepoint && !pagerUseWal(pPager) ){
36088 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
36089 pPager->journalOff = pSavepoint->iOffset;
36090 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
36091 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
36092 }
@@ -34591,10 +36129,14 @@
36129 ** will be skipped. Out-of-range pages are also skipped.
36130 */
36131 if( pSavepoint ){
36132 u32 ii; /* Loop counter */
36133 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
36134
36135 if( pagerUseWal(pPager) ){
36136 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
36137 }
36138 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
36139 assert( offset==ii*(4+pPager->pageSize) );
36140 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
36141 }
36142 assert( rc!=SQLITE_DONE );
@@ -34602,10 +36144,11 @@
36144
36145 sqlite3BitvecDestroy(pDone);
36146 if( rc==SQLITE_OK ){
36147 pPager->journalOff = szJ;
36148 }
36149
36150 return rc;
36151 }
36152
36153 /*
36154 ** Change the maximum number of in-memory pages that are allowed.
@@ -34818,12 +36361,14 @@
36361 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
36362 int nPage;
36363 if( mxPage>0 ){
36364 pPager->mxPgno = mxPage;
36365 }
36366 if( pPager->state!=PAGER_UNLOCK ){
36367 sqlite3PagerPagecount(pPager, &nPage);
36368 assert( pPager->mxPgno>=nPage );
36369 }
36370 return pPager->mxPgno;
36371 }
36372
36373 /*
36374 ** The following set of routines are used to disable the simulated
@@ -34865,10 +36410,26 @@
36410 */
36411 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
36412 int rc = SQLITE_OK;
36413 memset(pDest, 0, N);
36414 assert( isOpen(pPager->fd) || pPager->tempFile );
36415
36416 /* This routine is only called by btree immediately after creating
36417 ** the Pager object. There has not been an opportunity to transition
36418 ** to WAL mode yet.
36419 */
36420 assert( !pagerUseWal(pPager) );
36421 #if 0
36422 if( pagerUseWal(pPager) ){
36423 int isInWal = 0;
36424 rc = sqlite3WalRead(pPager->pWal, 1, &isInWal, N, pDest);
36425 if( rc!=SQLITE_OK || isInWal ){
36426 return rc;
36427 }
36428 }
36429 #endif
36430
36431 if( isOpen(pPager->fd) ){
36432 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
36433 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
36434 if( rc==SQLITE_IOERR_SHORT_READ ){
36435 rc = SQLITE_OK;
@@ -34891,28 +36452,36 @@
36452 **
36453 ** Otherwise, if everything is successful, then SQLITE_OK is returned
36454 ** and *pnPage is set to the number of pages in the database.
36455 */
36456 SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager, int *pnPage){
36457 Pgno nPage = 0; /* Value to return via *pnPage */
36458
36459 /* Determine the number of pages in the file. Store this in nPage. */
36460 if( pPager->dbSizeValid ){
36461 nPage = pPager->dbSize;
36462 }else{
36463 int rc; /* Error returned by OsFileSize() */
36464 i64 n = 0; /* File size in bytes returned by OsFileSize() */
36465
36466 if( pagerUseWal(pPager) && pPager->state!=PAGER_UNLOCK ){
36467 sqlite3WalDbsize(pPager->pWal, &nPage);
36468 }
36469
36470 if( nPage==0 ){
36471 assert( isOpen(pPager->fd) || pPager->tempFile );
36472 if( isOpen(pPager->fd) ){
36473 if( SQLITE_OK!=(rc = sqlite3OsFileSize(pPager->fd, &n)) ){
36474 pager_error(pPager, rc);
36475 return rc;
36476 }
36477 }
36478 if( n>0 && n<pPager->pageSize ){
36479 nPage = 1;
36480 }else{
36481 nPage = (Pgno)(n / pPager->pageSize);
36482 }
36483 }
36484 if( pPager->state!=PAGER_UNLOCK ){
36485 pPager->dbSize = nPage;
36486 pPager->dbFileSize = nPage;
36487 pPager->dbSizeValid = 1;
@@ -35030,10 +36599,11 @@
36599 assert( pPager->dbSize>=nPage );
36600 assert( pPager->state>=PAGER_RESERVED );
36601 pPager->dbSize = nPage;
36602 assertTruncateConstraint(pPager);
36603 }
36604
36605
36606 /*
36607 ** This function is called before attempting a hot-journal rollback. It
36608 ** syncs the journal file to disk, then sets pPager->journalHdr to the
36609 ** size of the journal file so that the pager_playback() routine knows
@@ -35071,14 +36641,23 @@
36641 ** is made to roll it back. If an error occurs during the rollback
36642 ** a hot journal may be left in the filesystem but no error is returned
36643 ** to the caller.
36644 */
36645 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
36646 u8 *pTmp = (u8 *)pPager->pTmpSpace;
36647
36648 disable_simulated_io_errors();
36649 sqlite3BeginBenignMalloc();
36650 pPager->errCode = 0;
36651 pPager->exclusiveMode = 0;
36652 #ifndef SQLITE_OMIT_WAL
36653 sqlite3WalClose(pPager->pWal,
36654 (pPager->noSync ? 0 : pPager->sync_flags),
36655 pPager->pageSize, pTmp
36656 );
36657 pPager->pWal = 0;
36658 #endif
36659 pager_reset(pPager);
36660 if( MEMDB ){
36661 pager_unlock(pPager);
36662 }else{
36663 /* Set Pager.journalHdr to -1 for the benefit of the pager_playback()
@@ -35095,11 +36674,11 @@
36674 sqlite3EndBenignMalloc();
36675 enable_simulated_io_errors();
36676 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
36677 IOTRACE(("CLOSE %p\n", pPager))
36678 sqlite3OsClose(pPager->fd);
36679 sqlite3PageFree(pTmp);
36680 sqlite3PcacheClose(pPager->pPCache);
36681
36682 #ifdef SQLITE_HAS_CODEC
36683 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
36684 #endif
@@ -35196,14 +36775,14 @@
36775 ** as a temporary buffer to inspect the first couple of bytes of
36776 ** the potential journal header.
36777 */
36778 i64 iNextHdrOffset;
36779 u8 aMagic[8];
36780 u8 zHeader[sizeof(aJournalMagic)+4];
36781
36782 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
36783 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
36784
36785 iNextHdrOffset = journalHdrOffset(pPager);
36786 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
36787 if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
36788 static const u8 zerobyte = 0;
@@ -35231,11 +36810,11 @@
36810 if( rc!=SQLITE_OK ) return rc;
36811 }
36812 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
36813 rc = sqlite3OsWrite(
36814 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
36815 );
36816 if( rc!=SQLITE_OK ) return rc;
36817 }
36818 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
36819 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
36820 IOTRACE(("JSYNC %p\n", pPager))
@@ -35311,10 +36890,11 @@
36890 ** is unchanged and we can rollback without having to playback the
36891 ** journal into the original database file. Once we transition to
36892 ** EXCLUSIVE, it means the database file has been changed and any rollback
36893 ** will require a journal playback.
36894 */
36895 assert( !pagerUseWal(pList->pPager) );
36896 assert( pPager->state>=PAGER_RESERVED );
36897 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
36898
36899 /* If the file is a temp-file has not yet been opened, open it now. It
36900 ** is not possible for rc to be other than SQLITE_OK if this branch
@@ -35322,10 +36902,18 @@
36902 */
36903 if( !isOpen(pPager->fd) ){
36904 assert( pPager->tempFile && rc==SQLITE_OK );
36905 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
36906 }
36907
36908 /* Before the first write, give the VFS a hint of what the final
36909 ** file size will be.
36910 */
36911 if( pPager->dbSize > (pPager->dbOrigSize+1) && isOpen(pPager->fd) ){
36912 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
36913 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
36914 }
36915
36916 while( rc==SQLITE_OK && pList ){
36917 Pgno pgno = pList->pgno;
36918
36919 /* If there are dirty pages in the page cache with page numbers greater
@@ -35374,10 +36962,30 @@
36962 pList = pList->pDirty;
36963 }
36964
36965 return rc;
36966 }
36967
36968 /*
36969 ** Ensure that the sub-journal file is open. If it is already open, this
36970 ** function is a no-op.
36971 **
36972 ** SQLITE_OK is returned if everything goes according to plan. An
36973 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
36974 ** fails.
36975 */
36976 static int openSubJournal(Pager *pPager){
36977 int rc = SQLITE_OK;
36978 if( !isOpen(pPager->sjfd) ){
36979 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
36980 sqlite3MemJournalOpen(pPager->sjfd);
36981 }else{
36982 rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
36983 }
36984 }
36985 return rc;
36986 }
36987
36988 /*
36989 ** Append a record of the current state of page pPg to the sub-journal.
36990 ** It is the callers responsibility to use subjRequiresPage() to check
36991 ** that it is really required before calling this function.
@@ -35391,32 +36999,44 @@
36999 ** bitvec.
37000 */
37001 static int subjournalPage(PgHdr *pPg){
37002 int rc = SQLITE_OK;
37003 Pager *pPager = pPg->pPager;
37004 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
37005
37006 /* Open the sub-journal, if it has not already been opened */
37007 assert( pPager->useJournal );
37008 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
37009 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
37010 assert( pagerUseWal(pPager)
37011 || pageInJournal(pPg)
37012 || pPg->pgno>pPager->dbOrigSize
37013 );
37014 rc = openSubJournal(pPager);
37015
37016 /* If the sub-journal was opened successfully (or was already open),
37017 ** write the journal record into the file. */
37018 if( rc==SQLITE_OK ){
37019 void *pData = pPg->pData;
37020 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
37021 char *pData2;
37022
37023 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
37024 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
37025 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
37026 if( rc==SQLITE_OK ){
37027 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
37028 }
37029 }
37030 }
37031 if( rc==SQLITE_OK ){
37032 pPager->nSubRec++;
37033 assert( pPager->nSavepoint>0 );
37034 rc = addToSavepointBitvecs(pPager, pPg->pgno);
37035 }
37036 return rc;
37037 }
 
37038
37039 /*
37040 ** This function is called by the pcache layer when it has reached some
37041 ** soft memory limit. The first argument is a pointer to a Pager object
37042 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
@@ -35440,78 +37060,88 @@
37060 int rc = SQLITE_OK;
37061
37062 assert( pPg->pPager==pPager );
37063 assert( pPg->flags&PGHDR_DIRTY );
37064
37065 pPg->pDirty = 0;
37066 if( pagerUseWal(pPager) ){
37067 /* Write a single frame for this page to the log. */
37068 if( subjRequiresPage(pPg) ){
37069 rc = subjournalPage(pPg);
37070 }
37071 if( rc==SQLITE_OK ){
37072 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
37073 }
37074 }else{
37075 /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
37076 ** is journalling a set of two or more database pages that are stored
37077 ** on the same disk sector. Syncing the journal is not allowed while
37078 ** this is happening as it is important that all members of such a
37079 ** set of pages are synced to disk together. So, if the page this function
37080 ** is trying to make clean will require a journal sync and the doNotSync
37081 ** flag is set, return without doing anything. The pcache layer will
37082 ** just have to go ahead and allocate a new page buffer instead of
37083 ** reusing pPg.
37084 **
37085 ** Similarly, if the pager has already entered the error state, do not
37086 ** try to write the contents of pPg to disk.
37087 */
37088 if( NEVER(pPager->errCode)
37089 || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37090 ){
37091 return SQLITE_OK;
37092 }
37093
37094 /* Sync the journal file if required. */
37095 if( pPg->flags&PGHDR_NEED_SYNC ){
37096 rc = syncJournal(pPager);
37097 if( rc==SQLITE_OK && pPager->fullSync &&
37098 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37099 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37100 ){
37101 pPager->nRec = 0;
37102 rc = writeJournalHdr(pPager);
37103 }
37104 }
37105
37106 /* If the page number of this page is larger than the current size of
37107 ** the database image, it may need to be written to the sub-journal.
37108 ** This is because the call to pager_write_pagelist() below will not
37109 ** actually write data to the file in this case.
37110 **
37111 ** Consider the following sequence of events:
37112 **
37113 ** BEGIN;
37114 ** <journal page X>
37115 ** <modify page X>
37116 ** SAVEPOINT sp;
37117 ** <shrink database file to Y pages>
37118 ** pagerStress(page X)
37119 ** ROLLBACK TO sp;
37120 **
37121 ** If (X>Y), then when pagerStress is called page X will not be written
37122 ** out to the database file, but will be dropped from the cache. Then,
37123 ** following the "ROLLBACK TO sp" statement, reading page X will read
37124 ** data from the database file. This will be the copy of page X as it
37125 ** was when the transaction started, not as it was when "SAVEPOINT sp"
37126 ** was executed.
37127 **
37128 ** The solution is to write the current data for page X into the
37129 ** sub-journal file now (if it is not already there), so that it will
37130 ** be restored to its current value when the "ROLLBACK TO sp" is
37131 ** executed.
37132 */
37133 if( NEVER(
37134 rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
37135 ) ){
37136 rc = subjournalPage(pPg);
37137 }
37138
37139 /* Write the contents of the page out to the database file. */
37140 if( rc==SQLITE_OK ){
37141 rc = pager_write_pagelist(pPg);
37142 }
37143 }
37144
37145 /* Mark the page as clean. */
37146 if( rc==SQLITE_OK ){
37147 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
@@ -35915,71 +37545,10 @@
37545 }
37546
37547 return rc;
37548 }
37549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37550 /*
37551 ** This function is called to obtain a shared lock on the database file.
37552 ** It is illegal to call sqlite3PagerAcquire() until after this function
37553 ** has been successfully called. If a shared-lock is already held when
37554 ** this function is called, it is a no-op.
@@ -36029,11 +37598,13 @@
37598 }
37599 pPager->errCode = SQLITE_OK;
37600 pager_reset(pPager);
37601 }
37602
37603 if( pagerUseWal(pPager) ){
37604 rc = pagerBeginReadTransaction(pPager);
37605 }else if( pPager->state==PAGER_UNLOCK || isErrorReset ){
37606 sqlite3_vfs * const pVfs = pPager->pVfs;
37607 int isHotJournal = 0;
37608 assert( !MEMDB );
37609 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
37610 if( pPager->noReadlock ){
@@ -36118,11 +37689,11 @@
37689 ** rollback journal at this time. */
37690 pPager->journalStarted = 0;
37691 pPager->journalOff = 0;
37692 pPager->setMaster = 0;
37693 pPager->journalHdr = 0;
37694
37695 /* Make sure the journal file has been synced to disk. */
37696
37697 /* Playback and delete the journal. Drop the database write
37698 ** lock and reacquire the read lock. Purge the cache before
37699 ** playing back the hot-journal so that we don't end up with
@@ -36185,10 +37756,15 @@
37756 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
37757 pager_reset(pPager);
37758 }
37759 }
37760 assert( pPager->exclusiveMode || pPager->state==PAGER_SHARED );
37761
37762 /* If there is a WAL file in the file-system, open this database in WAL
37763 ** mode. Otherwise, the following function call is a no-op.
37764 */
37765 rc = pagerOpenWalIfPresent(pPager);
37766 }
37767
37768 failed:
37769 if( rc!=SQLITE_OK ){
37770 /* pager_unlock() is a no-op for exclusive mode and in-memory databases. */
@@ -36325,12 +37901,12 @@
37901 goto pager_acquire_err;
37902 }
37903
37904 if( MEMDB || nMax<(int)pgno || noContent || !isOpen(pPager->fd) ){
37905 if( pgno>pPager->mxPgno ){
37906 rc = SQLITE_FULL;
37907 goto pager_acquire_err;
37908 }
37909 if( noContent ){
37910 /* Failure to set the bits in the InJournal bit-vectors is benign.
37911 ** It merely means that we might do some extra work to journal a
37912 ** page that does not need to be journaled. Nevertheless, be sure
@@ -36410,31 +37986,10 @@
37986 sqlite3PcacheRelease(pPg);
37987 pagerUnlockIfUnused(pPager);
37988 }
37989 }
37990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37991 /*
37992 ** This function is called at the start of every write transaction.
37993 ** There must already be a RESERVED or EXCLUSIVE lock on the database
37994 ** file when this routine is called.
37995 **
@@ -36513,13 +38068,10 @@
38068 pPager->journalOff = 0;
38069 pPager->setMaster = 0;
38070 pPager->journalHdr = 0;
38071 rc = writeJournalHdr(pPager);
38072 }
 
 
 
38073
38074 if( rc!=SQLITE_OK ){
38075 sqlite3BitvecDestroy(pPager->pInJournal);
38076 pPager->pInJournal = 0;
38077 }
@@ -36553,24 +38105,60 @@
38105 */
38106 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
38107 int rc = SQLITE_OK;
38108 assert( pPager->state!=PAGER_UNLOCK );
38109 pPager->subjInMemory = (u8)subjInMemory;
38110
38111 if( pPager->state==PAGER_SHARED ){
38112 assert( pPager->pInJournal==0 );
38113 assert( !MEMDB && !pPager->tempFile );
38114
38115 if( pagerUseWal(pPager) ){
38116 /* If the pager is configured to use locking_mode=exclusive, and an
38117 ** exclusive lock on the database is not already held, obtain it now.
38118 */
38119 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
38120 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
38121 pPager->state = PAGER_SHARED;
38122 if( rc!=SQLITE_OK ){
38123 return rc;
38124 }
38125 sqlite3WalExclusiveMode(pPager->pWal, 1);
38126 }
38127
38128 /* Grab the write lock on the log file. If successful, upgrade to
38129 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
38130 ** The busy-handler is not invoked if another connection already
38131 ** holds the write-lock. If possible, the upper layer will call it.
38132 **
38133 ** WAL mode sets Pager.state to PAGER_RESERVED when it has an open
38134 ** transaction, but never to PAGER_EXCLUSIVE. This is because in
38135 ** PAGER_EXCLUSIVE state the code to roll back savepoint transactions
38136 ** may copy data from the sub-journal into the database file as well
38137 ** as into the page cache. Which would be incorrect in WAL mode.
38138 */
38139 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
38140 if( rc==SQLITE_OK ){
38141 pPager->dbOrigSize = pPager->dbSize;
38142 pPager->state = PAGER_RESERVED;
38143 pPager->journalOff = 0;
38144 }
38145
38146 assert( rc!=SQLITE_OK || pPager->state==PAGER_RESERVED );
38147 assert( rc==SQLITE_OK || pPager->state==PAGER_SHARED );
38148 }else{
38149 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
38150 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
38151 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
38152 ** lock, but not when obtaining the RESERVED lock.
38153 */
38154 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
38155 if( rc==SQLITE_OK ){
38156 pPager->state = PAGER_RESERVED;
38157 if( exFlag ){
38158 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
38159 }
38160 }
38161 }
38162
38163 /* No need to open the journal file at this time. It will be
38164 ** opened before it is written to. If we defer opening the journal,
@@ -36582,10 +38170,11 @@
38170 ** time a (read or write) transaction was successfully concluded
38171 ** by this connection. Instead of deleting the journal file it was
38172 ** kept open and either was truncated to 0 bytes or its header was
38173 ** overwritten with zeros.
38174 */
38175 assert( pagerUseWal(pPager)==0 );
38176 assert( pPager->nRec==0 );
38177 assert( pPager->dbOrigSize==0 );
38178 assert( pPager->pInJournal==0 );
38179 rc = pager_open_journal(pPager);
38180 }
@@ -36636,10 +38225,11 @@
38225 /* Mark the page as dirty. If the page has already been written
38226 ** to the journal then we can return right away.
38227 */
38228 sqlite3PcacheMakeDirty(pPg);
38229 if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
38230 assert( !pagerUseWal(pPager) );
38231 pPager->dbModified = 1;
38232 }else{
38233
38234 /* If we get this far, it means that the page needs to be
38235 ** written to the transaction journal or the ckeckpoint journal
@@ -36651,11 +38241,14 @@
38241 */
38242 rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
38243 if( rc!=SQLITE_OK ){
38244 return rc;
38245 }
38246 if( !isOpen(pPager->jfd)
38247 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38248 && !pagerUseWal(pPager)
38249 ){
38250 assert( pPager->useJournal );
38251 rc = pager_open_journal(pPager);
38252 if( rc!=SQLITE_OK ) return rc;
38253 }
38254 pPager->dbModified = 1;
@@ -36663,10 +38256,11 @@
38256 /* The transaction journal now exists and we have a RESERVED or an
38257 ** EXCLUSIVE lock on the main database file. Write the current page to
38258 ** the transaction journal if it is not there already.
38259 */
38260 if( !pageInJournal(pPg) && isOpen(pPager->jfd) ){
38261 assert( !pagerUseWal(pPager) );
38262 if( pPg->pgno<=pPager->dbOrigSize ){
38263 u32 cksum;
38264 char *pData2;
38265
38266 /* We should never write to the journal file the page that
@@ -36954,11 +38548,14 @@
38548 /* Increment the value just read and write it back to byte 24. */
38549 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
38550 change_counter++;
38551 put32bits(((char*)pPgHdr->pData)+24, change_counter);
38552
38553 /* Also store the SQLite version number in bytes 96..99 and in
38554 ** bytes 92..95 store the change counter for which the version number
38555 ** is valid. */
38556 put32bits(((char*)pPgHdr->pData)+92, change_counter);
38557 put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
38558
38559 /* If running in direct mode, write the contents of page 1 to the file. */
38560 if( DIRECT_MODE ){
38561 const void *zBuf = pPgHdr->pData;
@@ -37043,132 +38640,143 @@
38640 ** function has already been called, it is mostly a no-op. However, any
38641 ** backup in progress needs to be restarted.
38642 */
38643 sqlite3BackupRestart(pPager->pBackup);
38644 }else if( pPager->state!=PAGER_SYNCED && pPager->dbModified ){
38645 if( pagerUseWal(pPager) ){
38646 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
38647 if( pList ){
38648 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
38649 (pPager->fullSync ? pPager->sync_flags : 0)
38650 );
38651 }
38652 if( rc==SQLITE_OK ){
38653 sqlite3PcacheCleanAll(pPager->pPCache);
38654 }
38655 }else{
38656 /* The following block updates the change-counter. Exactly how it
38657 ** does this depends on whether or not the atomic-update optimization
38658 ** was enabled at compile time, and if this transaction meets the
38659 ** runtime criteria to use the operation:
38660 **
38661 ** * The file-system supports the atomic-write property for
38662 ** blocks of size page-size, and
38663 ** * This commit is not part of a multi-file transaction, and
38664 ** * Exactly one page has been modified and store in the journal file.
38665 **
38666 ** If the optimization was not enabled at compile time, then the
38667 ** pager_incr_changecounter() function is called to update the change
38668 ** counter in 'indirect-mode'. If the optimization is compiled in but
38669 ** is not applicable to this transaction, call sqlite3JournalCreate()
38670 ** to make sure the journal file has actually been created, then call
38671 ** pager_incr_changecounter() to update the change-counter in indirect
38672 ** mode.
38673 **
38674 ** Otherwise, if the optimization is both enabled and applicable,
38675 ** then call pager_incr_changecounter() to update the change-counter
38676 ** in 'direct' mode. In this case the journal file will never be
38677 ** created for this transaction.
38678 */
38679 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
38680 PgHdr *pPg;
38681 assert( isOpen(pPager->jfd) || pPager->journalMode==PAGER_JOURNALMODE_OFF );
38682 if( !zMaster && isOpen(pPager->jfd)
38683 && pPager->journalOff==jrnlBufferSize(pPager)
38684 && pPager->dbSize>=pPager->dbFileSize
38685 && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
38686 ){
38687 /* Update the db file change counter via the direct-write method. The
38688 ** following call will modify the in-memory representation of page 1
38689 ** to include the updated change counter and then write page 1
38690 ** directly to the database file. Because of the atomic-write
38691 ** property of the host file-system, this is safe.
38692 */
38693 rc = pager_incr_changecounter(pPager, 1);
38694 }else{
38695 rc = sqlite3JournalCreate(pPager->jfd);
38696 if( rc==SQLITE_OK ){
38697 rc = pager_incr_changecounter(pPager, 0);
38698 }
38699 }
38700 #else
38701 rc = pager_incr_changecounter(pPager, 0);
38702 #endif
38703 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38704
38705 /* If this transaction has made the database smaller, then all pages
38706 ** being discarded by the truncation must be written to the journal
38707 ** file. This can only happen in auto-vacuum mode.
38708 **
38709 ** Before reading the pages with page numbers larger than the
38710 ** current value of Pager.dbSize, set dbSize back to the value
38711 ** that it took at the start of the transaction. Otherwise, the
38712 ** calls to sqlite3PagerGet() return zeroed pages instead of
38713 ** reading data from the database file.
38714 **
38715 ** When journal_mode==OFF the dbOrigSize is always zero, so this
38716 ** block never runs if journal_mode=OFF.
38717 */
38718 #ifndef SQLITE_OMIT_AUTOVACUUM
38719 if( pPager->dbSize<pPager->dbOrigSize
38720 && ALWAYS(pPager->journalMode!=PAGER_JOURNALMODE_OFF)
38721 ){
38722 Pgno i; /* Iterator variable */
38723 const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
38724 const Pgno dbSize = pPager->dbSize; /* Database image size */
38725 pPager->dbSize = pPager->dbOrigSize;
38726 for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
38727 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
38728 PgHdr *pPage; /* Page to journal */
38729 rc = sqlite3PagerGet(pPager, i, &pPage);
38730 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38731 rc = sqlite3PagerWrite(pPage);
38732 sqlite3PagerUnref(pPage);
38733 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38734 }
38735 }
38736 pPager->dbSize = dbSize;
38737 }
38738 #endif
38739
38740 /* Write the master journal name into the journal file. If a master
38741 ** journal file name has already been written to the journal file,
38742 ** or if zMaster is NULL (no master journal), then this call is a no-op.
38743 */
38744 rc = writeMasterJournal(pPager, zMaster);
38745 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38746
38747 /* Sync the journal file. If the atomic-update optimization is being
38748 ** used, this call will not create the journal file or perform any
38749 ** real IO.
38750 */
38751 rc = syncJournal(pPager);
38752 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38753
38754 /* Write all dirty pages to the database file. */
38755 rc = pager_write_pagelist(sqlite3PcacheDirtyList(pPager->pPCache));
38756 if( rc!=SQLITE_OK ){
38757 assert( rc!=SQLITE_IOERR_BLOCKED );
38758 goto commit_phase_one_exit;
38759 }
38760 sqlite3PcacheCleanAll(pPager->pPCache);
38761
38762 /* If the file on disk is not the same size as the database image,
38763 ** then use pager_truncate to grow or shrink the file here.
38764 */
38765 if( pPager->dbSize!=pPager->dbFileSize ){
38766 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
38767 assert( pPager->state>=PAGER_EXCLUSIVE );
38768 rc = pager_truncate(pPager, nNew);
38769 if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
38770 }
38771
38772 /* Finally, sync the database file. */
38773 if( !pPager->noSync && !noSync ){
38774 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
38775 }
38776 IOTRACE(("DBSYNC %p\n", pPager))
38777 }
38778
38779 pPager->state = PAGER_SYNCED;
38780 }
38781
38782 commit_phase_one_exit:
@@ -37273,11 +38881,18 @@
38881 ** hot-journal rollback).
38882 */
38883 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
38884 int rc = SQLITE_OK; /* Return code */
38885 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
38886 if( pagerUseWal(pPager) ){
38887 int rc2;
38888
38889 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
38890 rc2 = pager_end_transaction(pPager, pPager->setMaster);
38891 if( rc==SQLITE_OK ) rc = rc2;
38892 rc = pager_error(pPager, rc);
38893 }else if( !pPager->dbModified || !isOpen(pPager->jfd) ){
38894 rc = pager_end_transaction(pPager, pPager->setMaster);
38895 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
38896 if( pPager->state>=PAGER_EXCLUSIVE ){
38897 pager_playback(pPager, 0);
38898 }
@@ -37399,11 +39014,10 @@
39014 if( !aNew ){
39015 return SQLITE_NOMEM;
39016 }
39017 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
39018 pPager->aSavepoint = aNew;
 
39019
39020 /* Populate the PagerSavepoint structures just allocated. */
39021 for(ii=nCurrent; ii<nSavepoint; ii++){
39022 aNew[ii].nOrig = nPage;
39023 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
@@ -37414,14 +39028,16 @@
39028 aNew[ii].iSubRec = pPager->nSubRec;
39029 aNew[ii].pInSavepoint = sqlite3BitvecCreate(nPage);
39030 if( !aNew[ii].pInSavepoint ){
39031 return SQLITE_NOMEM;
39032 }
39033 if( pagerUseWal(pPager) ){
39034 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
39035 }
39036 pPager->nSavepoint = ii+1;
39037 }
39038 assert( pPager->nSavepoint==nSavepoint );
 
 
39039 assertTruncateConstraint(pPager);
39040 }
39041
39042 return rc;
39043 }
@@ -37491,11 +39107,11 @@
39107 /* Else this is a rollback operation, playback the specified savepoint.
39108 ** If this is a temp-file, it is possible that the journal file has
39109 ** not yet been opened. In this case there have been no changes to
39110 ** the database file, so the playback operation can be skipped.
39111 */
39112 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
39113 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
39114 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
39115 assert(rc!=SQLITE_DONE);
39116 }
39117
@@ -37760,60 +39376,106 @@
39376 }
39377 return (int)pPager->exclusiveMode;
39378 }
39379
39380 /*
39381 ** Set the journal-mode for this pager. Parameter eMode must be one of:
39382 **
 
39383 ** PAGER_JOURNALMODE_DELETE
39384 ** PAGER_JOURNALMODE_TRUNCATE
39385 ** PAGER_JOURNALMODE_PERSIST
39386 ** PAGER_JOURNALMODE_OFF
39387 ** PAGER_JOURNALMODE_MEMORY
39388 ** PAGER_JOURNALMODE_WAL
39389 **
39390 ** The journalmode is set to the value specified if the change is allowed.
39391 ** The change may be disallowed for the following reasons:
 
39392 **
39393 ** * An in-memory database can only have its journal_mode set to _OFF
39394 ** or _MEMORY.
39395 **
39396 ** * Temporary databases cannot have _WAL journalmode.
39397 **
39398 ** The returned indicate the current (possibly updated) journal-mode.
39399 */
39400 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
39401 u8 eOld = pPager->journalMode; /* Prior journalmode */
39402
39403 /* The eMode parameter is always valid */
39404 assert( eMode==PAGER_JOURNALMODE_DELETE
39405 || eMode==PAGER_JOURNALMODE_TRUNCATE
39406 || eMode==PAGER_JOURNALMODE_PERSIST
39407 || eMode==PAGER_JOURNALMODE_OFF
39408 || eMode==PAGER_JOURNALMODE_WAL
39409 || eMode==PAGER_JOURNALMODE_MEMORY );
39410
39411 /* Do not allow the journalmode of a TEMP database to be changed to WAL
39412 */
39413 if( pPager->tempFile && eMode==PAGER_JOURNALMODE_WAL ){
39414 assert( eOld!=PAGER_JOURNALMODE_WAL );
39415 eMode = eOld;
39416 }
39417
39418 /* Do allow the journalmode of an in-memory database to be set to
39419 ** anything other than MEMORY or OFF
39420 */
39421 if( MEMDB ){
39422 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
39423 if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
39424 eMode = eOld;
39425 }
39426 }
39427
39428 if( eMode!=eOld ){
39429 /* When changing between rollback modes, close the journal file prior
39430 ** to the change. But when changing from a rollback mode to WAL, keep
39431 ** the journal open since there is a rollback-style transaction in play
39432 ** used to convert the version numbers in the btree header.
39433 */
39434 if( isOpen(pPager->jfd) && eMode!=PAGER_JOURNALMODE_WAL ){
39435 sqlite3OsClose(pPager->jfd);
39436 }
39437
39438 /* Change the journal mode. */
39439 pPager->journalMode = (u8)eMode;
39440
39441 /* When transistioning from TRUNCATE or PERSIST to any other journal
39442 ** mode (and we are not in locking_mode=EXCLUSIVE) then delete the
39443 ** journal file.
39444 */
39445 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
39446 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
39447 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
39448 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
39449 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
39450 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
39451 if( (eOld & 5)==1 && (eMode & 5)!=1 && !pPager->exclusiveMode ){
39452 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
39453 }
 
39454 }
39455
39456 /* Return the new journal mode */
39457 return (int)pPager->journalMode;
39458 }
39459
39460 /*
39461 ** Return the current journal mode.
39462 */
39463 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
39464 return (int)pPager->journalMode;
39465 }
39466
39467 /*
39468 ** Return TRUE if the pager is in a state where it is OK to change the
39469 ** journalmode. Journalmode changes can only happen when the database
39470 ** is unmodified.
39471 */
39472 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
39473 if( pPager->dbModified ) return 0;
39474 if( isOpen(pPager->jfd) && pPager->journalOff>0 ) return 0;
39475 return 1;
39476 }
39477
39478 /*
39479 ** Get/set the size-limit used for persistent journal files.
39480 **
39481 ** Setting the size limit to -1 means no limit is enforced.
@@ -37833,14 +39495,2621 @@
39495 ** sqlite3BackupUpdate() only.
39496 */
39497 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
39498 return &pPager->pBackup;
39499 }
39500
39501 #ifndef SQLITE_OMIT_WAL
39502 /*
39503 ** This function is called when the user invokes "PRAGMA checkpoint".
39504 */
39505 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager){
39506 int rc = SQLITE_OK;
39507 if( pPager->pWal ){
39508 u8 *zBuf = (u8 *)pPager->pTmpSpace;
39509 rc = sqlite3WalCheckpoint(pPager->pWal,
39510 (pPager->noSync ? 0 : pPager->sync_flags),
39511 pPager->pageSize, zBuf
39512 );
39513 }
39514 return rc;
39515 }
39516
39517 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
39518 return sqlite3WalCallback(pPager->pWal);
39519 }
39520
39521 /*
39522 ** Return true if the underlying VFS for the given pager supports the
39523 ** primitives necessary for write-ahead logging.
39524 */
39525 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
39526 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
39527 return pMethods->iVersion>=2 && pMethods->xShmOpen!=0;
39528 }
39529
39530 /*
39531 ** Open a connection to the write-ahead log file for pager pPager. If
39532 ** the log connection is already open, this function is a no-op.
39533 **
39534 ** The caller must be holding a SHARED lock on the database file to call
39535 ** this function.
39536 */
39537 SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen){
39538 int rc = SQLITE_OK; /* Return code */
39539
39540 assert( pPager->state>=PAGER_SHARED );
39541 if( !pPager->pWal ){
39542 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
39543
39544 /* Open the connection to the log file. If this operation fails,
39545 ** (e.g. due to malloc() failure), unlock the database file and
39546 ** return an error code.
39547 */
39548 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd,
39549 pPager->zFilename, &pPager->pWal);
39550 if( rc==SQLITE_OK ){
39551 pPager->journalMode = PAGER_JOURNALMODE_WAL;
39552 }
39553 }else{
39554 *pisOpen = 1;
39555 }
39556
39557 return rc;
39558 }
39559
39560 /*
39561 ** This function is called to close the connection to the log file prior
39562 ** to switching from WAL to rollback mode.
39563 **
39564 ** Before closing the log file, this function attempts to take an
39565 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
39566 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
39567 ** If successful, the EXCLUSIVE lock is not released before returning.
39568 */
39569 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
39570 int rc = SQLITE_OK;
39571
39572 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
39573
39574 /* If the log file is not already open, but does exist in the file-system,
39575 ** it may need to be checkpointed before the connection can switch to
39576 ** rollback mode. Open it now so this can happen.
39577 */
39578 if( !pPager->pWal ){
39579 int logexists = 0;
39580 rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_SHARED);
39581 if( rc==SQLITE_OK ){
39582 rc = pagerHasWAL(pPager, &logexists);
39583 }
39584 if( rc==SQLITE_OK && logexists ){
39585 rc = sqlite3WalOpen(pPager->pVfs, pPager->fd,
39586 pPager->zFilename, &pPager->pWal);
39587 }
39588 }
39589
39590 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
39591 ** the database file, the log and log-summary files will be deleted.
39592 */
39593 if( rc==SQLITE_OK && pPager->pWal ){
39594 rc = sqlite3OsLock(pPager->fd, SQLITE_LOCK_EXCLUSIVE);
39595 if( rc==SQLITE_OK ){
39596 rc = sqlite3WalClose(pPager->pWal,
39597 (pPager->noSync ? 0 : pPager->sync_flags),
39598 pPager->pageSize, (u8*)pPager->pTmpSpace
39599 );
39600 pPager->pWal = 0;
39601 }else{
39602 /* If we cannot get an EXCLUSIVE lock, downgrade the PENDING lock
39603 ** that we did get back to SHARED. */
39604 sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
39605 }
39606 }
39607 return rc;
39608 }
39609 #endif
39610
39611 #endif /* SQLITE_OMIT_DISKIO */
39612
39613 /************** End of pager.c ***********************************************/
39614 /************** Begin file wal.c *********************************************/
39615 /*
39616 ** 2010 February 1
39617 **
39618 ** The author disclaims copyright to this source code. In place of
39619 ** a legal notice, here is a blessing:
39620 **
39621 ** May you do good and not evil.
39622 ** May you find forgiveness for yourself and forgive others.
39623 ** May you share freely, never taking more than you give.
39624 **
39625 *************************************************************************
39626 **
39627 ** This file contains the implementation of a write-ahead log (WAL) used in
39628 ** "journal_mode=WAL" mode.
39629 **
39630 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
39631 **
39632 ** A WAL file consists of a header followed by zero or more "frames".
39633 ** Each frame records the revised content of a single page from the
39634 ** database file. All changes to the database are recorded by writing
39635 ** frames into the WAL. Transactions commit when a frame is written that
39636 ** contains a commit marker. A single WAL can and usually does record
39637 ** multiple transactions. Periodically, the content of the WAL is
39638 ** transferred back into the database file in an operation called a
39639 ** "checkpoint".
39640 **
39641 ** A single WAL file can be used multiple times. In other words, the
39642 ** WAL can fill up with frames and then be checkpointed and then new
39643 ** frames can overwrite the old ones. A WAL always grows from beginning
39644 ** toward the end. Checksums and counters attached to each frame are
39645 ** used to determine which frames within the WAL are valid and which
39646 ** are leftovers from prior checkpoints.
39647 **
39648 ** The WAL header is 24 bytes in size and consists of the following six
39649 ** big-endian 32-bit unsigned integer values:
39650 **
39651 ** 0: Magic number. 0x377f0682 or 0x377f0683
39652 ** 4: File format version. Currently 3007000
39653 ** 8: Database page size. Example: 1024
39654 ** 12: Checkpoint sequence number
39655 ** 16: Salt-1, random integer incremented with each checkpoint
39656 ** 20: Salt-2, a different random integer changing with each ckpt
39657 **
39658 ** Immediately following the wal-header are zero or more frames. Each
39659 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39660 ** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39661 ** integer values, as follows:
39662 **
39663 ** 0: Page number.
39664 ** 4: For commit records, the size of the database image in pages
39665 ** after the commit. For all other records, zero.
39666 ** 8: Salt-1 (copied from the header)
39667 ** 12: Salt-2 (copied from the header)
39668 ** 16: Checksum-1.
39669 ** 20: Checksum-2.
39670 **
39671 ** A frame is considered valid if and only if the following conditions are
39672 ** true:
39673 **
39674 ** (1) The salt-1 and salt-2 values in the frame-header match
39675 ** salt values in the wal-header
39676 **
39677 ** (2) The checksum values in the final 8 bytes of the frame-header
39678 ** exactly match the checksum computed consecutively on the
39679 ** WAL header and the first 8 bytes and the content of all frames
39680 ** up to and including the current frame.
39681 **
39682 ** The checksum is computed using 32-bit big-endian integers if the
39683 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
39684 ** is computed using little-endian if the magic number is 0x377f0682.
39685 ** The checksum values are always stored in the frame header in a
39686 ** big-endian format regardless of which byte order is used to compute
39687 ** the checksum. The checksum is computed by interpreting the input as
39688 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
39689 ** algorithm used for the checksum is as follows:
39690 **
39691 ** for i from 0 to n-1 step 2:
39692 ** s0 += x[i] + s1;
39693 ** s1 += x[i+1] + s0;
39694 ** endfor
39695 **
39696 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39697 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
39698 ** The VFS.xSync operations serve as write barriers - all writes launched
39699 ** before the xSync must complete before any write that launches after the
39700 ** xSync begins.
39701 **
39702 ** After each checkpoint, the salt-1 value is incremented and the salt-2
39703 ** value is randomized. This prevents old and new frames in the WAL from
39704 ** being considered valid at the same time and being checkpointing together
39705 ** following a crash.
39706 **
39707 ** READER ALGORITHM
39708 **
39709 ** To read a page from the database (call it page number P), a reader
39710 ** first checks the WAL to see if it contains page P. If so, then the
39711 ** last valid instance of page P that is a followed by a commit frame
39712 ** or is a commit frame itself becomes the value read. If the WAL
39713 ** contains no copies of page P that are valid and which are a commit
39714 ** frame or are followed by a commit frame, then page P is read from
39715 ** the database file.
39716 **
39717 ** To start a read transaction, the reader records the index of the last
39718 ** valid frame in the WAL. The reader uses this recorded "mxFrame" value
39719 ** for all subsequent read operations. New transactions can be appended
39720 ** to the WAL, but as long as the reader uses its original mxFrame value
39721 ** and ignores the newly appended content, it will see a consistent snapshot
39722 ** of the database from a single point in time. This technique allows
39723 ** multiple concurrent readers to view different versions of the database
39724 ** content simultaneously.
39725 **
39726 ** The reader algorithm in the previous paragraphs works correctly, but
39727 ** because frames for page P can appear anywhere within the WAL, the
39728 ** reader has to scan the entire WAL looking for page P frames. If the
39729 ** WAL is large (multiple megabytes is typical) that scan can be slow,
39730 ** and read performance suffers. To overcome this problem, a separate
39731 ** data structure called the wal-index is maintained to expedite the
39732 ** search for frames of a particular page.
39733 **
39734 ** WAL-INDEX FORMAT
39735 **
39736 ** Conceptually, the wal-index is shared memory, though VFS implementations
39737 ** might choose to implement the wal-index using a mmapped file. Because
39738 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
39739 ** on a network filesystem. All users of the database must be able to
39740 ** share memory.
39741 **
39742 ** The wal-index is transient. After a crash, the wal-index can (and should
39743 ** be) reconstructed from the original WAL file. In fact, the VFS is required
39744 ** to either truncate or zero the header of the wal-index when the last
39745 ** connection to it closes. Because the wal-index is transient, it can
39746 ** use an architecture-specific format; it does not have to be cross-platform.
39747 ** Hence, unlike the database and WAL file formats which store all values
39748 ** as big endian, the wal-index can store multi-byte values in the native
39749 ** byte order of the host computer.
39750 **
39751 ** The purpose of the wal-index is to answer this question quickly: Given
39752 ** a page number P, return the index of the last frame for page P in the WAL,
39753 ** or return NULL if there are no frames for page P in the WAL.
39754 **
39755 ** The wal-index consists of a header region, followed by an one or
39756 ** more index blocks.
39757 **
39758 ** The wal-index header contains the total number of frames within the WAL
39759 ** in the the mxFrame field.
39760 **
39761 ** Each index block except for the first contains information on
39762 ** HASHTABLE_NPAGE frames. The first index block contains information on
39763 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
39764 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
39765 ** first index block are the same size as all other index blocks in the
39766 ** wal-index.
39767 **
39768 ** Each index block contains two sections, a page-mapping that contains the
39769 ** database page number associated with each wal frame, and a hash-table
39770 ** that allows readers to query an index block for a specific page number.
39771 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
39772 ** for the first index block) 32-bit page numbers. The first entry in the
39773 ** first index-block contains the database page number corresponding to the
39774 ** first frame in the WAL file. The first entry in the second index block
39775 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
39776 ** the log, and so on.
39777 **
39778 ** The last index block in a wal-index usually contains less than the full
39779 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
39780 ** depending on the contents of the WAL file. This does not change the
39781 ** allocated size of the page-mapping array - the page-mapping array merely
39782 ** contains unused entries.
39783 **
39784 ** Even without using the hash table, the last frame for page P
39785 ** can be found by scanning the page-mapping sections of each index block
39786 ** starting with the last index block and moving toward the first, and
39787 ** within each index block, starting at the end and moving toward the
39788 ** beginning. The first entry that equals P corresponds to the frame
39789 ** holding the content for that page.
39790 **
39791 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
39792 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
39793 ** hash table for each page number in the mapping section, so the hash
39794 ** table is never more than half full. The expected number of collisions
39795 ** prior to finding a match is 1. Each entry of the hash table is an
39796 ** 1-based index of an entry in the mapping section of the same
39797 ** index block. Let K be the 1-based index of the largest entry in
39798 ** the mapping section. (For index blocks other than the last, K will
39799 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
39800 ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
39801 ** contain a value of 0.
39802 **
39803 ** To look for page P in the hash table, first compute a hash iKey on
39804 ** P as follows:
39805 **
39806 ** iKey = (P * 383) % HASHTABLE_NSLOT
39807 **
39808 ** Then start scanning entries of the hash table, starting with iKey
39809 ** (wrapping around to the beginning when the end of the hash table is
39810 ** reached) until an unused hash slot is found. Let the first unused slot
39811 ** be at index iUnused. (iUnused might be less than iKey if there was
39812 ** wrap-around.) Because the hash table is never more than half full,
39813 ** the search is guaranteed to eventually hit an unused entry. Let
39814 ** iMax be the value between iKey and iUnused, closest to iUnused,
39815 ** where aHash[iMax]==P. If there is no iMax entry (if there exists
39816 ** no hash slot such that aHash[i]==p) then page P is not in the
39817 ** current index block. Otherwise the iMax-th mapping entry of the
39818 ** current index block corresponds to the last entry that references
39819 ** page P.
39820 **
39821 ** A hash search begins with the last index block and moves toward the
39822 ** first index block, looking for entries corresponding to page P. On
39823 ** average, only two or three slots in each index block need to be
39824 ** examined in order to either find the last entry for page P, or to
39825 ** establish that no such entry exists in the block. Each index block
39826 ** holds over 4000 entries. So two or three index blocks are sufficient
39827 ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
39828 ** comparisons (on average) suffice to either locate a frame in the
39829 ** WAL or to establish that the frame does not exist in the WAL. This
39830 ** is much faster than scanning the entire 10MB WAL.
39831 **
39832 ** Note that entries are added in order of increasing K. Hence, one
39833 ** reader might be using some value K0 and a second reader that started
39834 ** at a later time (after additional transactions were added to the WAL
39835 ** and to the wal-index) might be using a different value K1, where K1>K0.
39836 ** Both readers can use the same hash table and mapping section to get
39837 ** the correct result. There may be entries in the hash table with
39838 ** K>K0 but to the first reader, those entries will appear to be unused
39839 ** slots in the hash table and so the first reader will get an answer as
39840 ** if no values greater than K0 had ever been inserted into the hash table
39841 ** in the first place - which is what reader one wants. Meanwhile, the
39842 ** second reader using K1 will see additional values that were inserted
39843 ** later, which is exactly what reader two wants.
39844 **
39845 ** When a rollback occurs, the value of K is decreased. Hash table entries
39846 ** that correspond to frames greater than the new K value are removed
39847 ** from the hash table at this point.
39848 */
39849 #ifndef SQLITE_OMIT_WAL
39850
39851
39852 /*
39853 ** Trace output macros
39854 */
39855 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
39856 SQLITE_PRIVATE int sqlite3WalTrace = 0;
39857 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39858 #else
39859 # define WALTRACE(X)
39860 #endif
39861
39862
39863 /*
39864 ** Indices of various locking bytes. WAL_NREADER is the number
39865 ** of available reader locks and should be at least 3.
39866 */
39867 #define WAL_WRITE_LOCK 0
39868 #define WAL_ALL_BUT_WRITE 1
39869 #define WAL_CKPT_LOCK 1
39870 #define WAL_RECOVER_LOCK 2
39871 #define WAL_READ_LOCK(I) (3+(I))
39872 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
39873
39874
39875 /* Object declarations */
39876 typedef struct WalIndexHdr WalIndexHdr;
39877 typedef struct WalIterator WalIterator;
39878 typedef struct WalCkptInfo WalCkptInfo;
39879
39880
39881 /*
39882 ** The following object holds a copy of the wal-index header content.
39883 **
39884 ** The actual header in the wal-index consists of two copies of this
39885 ** object.
39886 */
39887 struct WalIndexHdr {
39888 u32 iChange; /* Counter incremented each transaction */
39889 u8 isInit; /* 1 when initialized */
39890 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39891 u16 szPage; /* Database page size in bytes */
39892 u32 mxFrame; /* Index of last valid frame in the WAL */
39893 u32 nPage; /* Size of database in pages */
39894 u32 aFrameCksum[2]; /* Checksum of last frame in log */
39895 u32 aSalt[2]; /* Two salt values copied from WAL header */
39896 u32 aCksum[2]; /* Checksum over all prior fields */
39897 };
39898
39899 /*
39900 ** A copy of the following object occurs in the wal-index immediately
39901 ** following the second copy of the WalIndexHdr. This object stores
39902 ** information used by checkpoint.
39903 **
39904 ** nBackfill is the number of frames in the WAL that have been written
39905 ** back into the database. (We call the act of moving content from WAL to
39906 ** database "backfilling".) The nBackfill number is never greater than
39907 ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
39908 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
39909 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
39910 ** mxFrame back to zero when the WAL is reset.
39911 **
39912 ** There is one entry in aReadMark[] for each reader lock. If a reader
39913 ** holds read-lock K, then the value in aReadMark[K] is no greater than
39914 ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
39915 ** for any aReadMark[] means that entry is unused. aReadMark[0] is
39916 ** a special case; its value is never used and it exists as a place-holder
39917 ** to avoid having to offset aReadMark[] indexs by one. Readers holding
39918 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
39919 ** directly from the database.
39920 **
39921 ** The value of aReadMark[K] may only be changed by a thread that
39922 ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
39923 ** aReadMark[K] cannot changed while there is a reader is using that mark
39924 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
39925 **
39926 ** The checkpointer may only transfer frames from WAL to database where
39927 ** the frame numbers are less than or equal to every aReadMark[] that is
39928 ** in use (that is, every aReadMark[j] for which there is a corresponding
39929 ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
39930 ** largest value and will increase an unused aReadMark[] to mxFrame if there
39931 ** is not already an aReadMark[] equal to mxFrame. The exception to the
39932 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
39933 ** in the WAL has been backfilled into the database) then new readers
39934 ** will choose aReadMark[0] which has value 0 and hence such reader will
39935 ** get all their all content directly from the database file and ignore
39936 ** the WAL.
39937 **
39938 ** Writers normally append new frames to the end of the WAL. However,
39939 ** if nBackfill equals mxFrame (meaning that all WAL content has been
39940 ** written back into the database) and if no readers are using the WAL
39941 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
39942 ** the writer will first "reset" the WAL back to the beginning and start
39943 ** writing new content beginning at frame 1.
39944 **
39945 ** We assume that 32-bit loads are atomic and so no locks are needed in
39946 ** order to read from any aReadMark[] entries.
39947 */
39948 struct WalCkptInfo {
39949 u32 nBackfill; /* Number of WAL frames backfilled into DB */
39950 u32 aReadMark[WAL_NREADER]; /* Reader marks */
39951 };
39952 #define READMARK_NOT_USED 0xffffffff
39953
39954
39955 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
39956 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
39957 ** only support mandatory file-locks, we do not read or write data
39958 ** from the region of the file on which locks are applied.
39959 */
39960 #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
39961 #define WALINDEX_LOCK_RESERVED 16
39962 #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
39963
39964 /* Size of header before each frame in wal */
39965 #define WAL_FRAME_HDRSIZE 24
39966
39967 /* Size of write ahead log header */
39968 #define WAL_HDRSIZE 24
39969
39970 /* WAL magic value. Either this value, or the same value with the least
39971 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
39972 ** big-endian format in the first 4 bytes of a WAL file.
39973 **
39974 ** If the LSB is set, then the checksums for each frame within the WAL
39975 ** file are calculated by treating all data as an array of 32-bit
39976 ** big-endian words. Otherwise, they are calculated by interpreting
39977 ** all data as 32-bit little-endian words.
39978 */
39979 #define WAL_MAGIC 0x377f0682
39980
39981 /*
39982 ** Return the offset of frame iFrame in the write-ahead log file,
39983 ** assuming a database page size of szPage bytes. The offset returned
39984 ** is to the start of the write-ahead log frame-header.
39985 */
39986 #define walFrameOffset(iFrame, szPage) ( \
39987 WAL_HDRSIZE + ((iFrame)-1)*((szPage)+WAL_FRAME_HDRSIZE) \
39988 )
39989
39990 /*
39991 ** An open write-ahead log file is represented by an instance of the
39992 ** following object.
39993 */
39994 struct Wal {
39995 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
39996 sqlite3_file *pDbFd; /* File handle for the database file */
39997 sqlite3_file *pWalFd; /* File handle for WAL file */
39998 u32 iCallback; /* Value to pass to log callback (or 0) */
39999 int nWiData; /* Size of array apWiData */
40000 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
40001 u16 szPage; /* Database page size */
40002 i16 readLock; /* Which read lock is being held. -1 for none */
40003 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
40004 u8 isWIndexOpen; /* True if ShmOpen() called on pDbFd */
40005 u8 writeLock; /* True if in a write transaction */
40006 u8 ckptLock; /* True if holding a checkpoint lock */
40007 WalIndexHdr hdr; /* Wal-index header for current transaction */
40008 char *zWalName; /* Name of WAL file */
40009 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
40010 #ifdef SQLITE_DEBUG
40011 u8 lockError; /* True if a locking error has occurred */
40012 #endif
40013 };
40014
40015 /*
40016 ** Each page of the wal-index mapping contains a hash-table made up of
40017 ** an array of HASHTABLE_NSLOT elements of the following type.
40018 */
40019 typedef u16 ht_slot;
40020
40021 /*
40022 ** This structure is used to implement an iterator that loops through
40023 ** all frames in the WAL in database page order. Where two or more frames
40024 ** correspond to the same database page, the iterator visits only the
40025 ** frame most recently written to the WAL (in other words, the frame with
40026 ** the largest index).
40027 **
40028 ** The internals of this structure are only accessed by:
40029 **
40030 ** walIteratorInit() - Create a new iterator,
40031 ** walIteratorNext() - Step an iterator,
40032 ** walIteratorFree() - Free an iterator.
40033 **
40034 ** This functionality is used by the checkpoint code (see walCheckpoint()).
40035 */
40036 struct WalIterator {
40037 int iPrior; /* Last result returned from the iterator */
40038 int nSegment; /* Size of the aSegment[] array */
40039 struct WalSegment {
40040 int iNext; /* Next slot in aIndex[] not yet returned */
40041 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
40042 u32 *aPgno; /* Array of page numbers. */
40043 int nEntry; /* Max size of aPgno[] and aIndex[] arrays */
40044 int iZero; /* Frame number associated with aPgno[0] */
40045 } aSegment[1]; /* One for every 32KB page in the WAL */
40046 };
40047
40048 /*
40049 ** Define the parameters of the hash tables in the wal-index file. There
40050 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
40051 ** wal-index.
40052 **
40053 ** Changing any of these constants will alter the wal-index format and
40054 ** create incompatibilities.
40055 */
40056 #define HASHTABLE_NPAGE 4096 /* Must be power of 2 */
40057 #define HASHTABLE_HASH_1 383 /* Should be prime */
40058 #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */
40059
40060 /*
40061 ** The block of page numbers associated with the first hash-table in a
40062 ** wal-index is smaller than usual. This is so that there is a complete
40063 ** hash-table on each aligned 32KB page of the wal-index.
40064 */
40065 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
40066
40067 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
40068 #define WALINDEX_PGSZ ( \
40069 sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
40070 )
40071
40072 /*
40073 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
40074 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
40075 ** numbered from zero.
40076 **
40077 ** If this call is successful, *ppPage is set to point to the wal-index
40078 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
40079 ** then an SQLite error code is returned and *ppPage is set to 0.
40080 */
40081 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
40082 int rc = SQLITE_OK;
40083
40084 /* Enlarge the pWal->apWiData[] array if required */
40085 if( pWal->nWiData<=iPage ){
40086 int nByte = sizeof(u32 *)*(iPage+1);
40087 volatile u32 **apNew;
40088 apNew = (volatile u32 **)sqlite3_realloc(pWal->apWiData, nByte);
40089 if( !apNew ){
40090 *ppPage = 0;
40091 return SQLITE_NOMEM;
40092 }
40093 memset(&apNew[pWal->nWiData], 0, sizeof(u32 *)*(iPage+1-pWal->nWiData));
40094 pWal->apWiData = apNew;
40095 pWal->nWiData = iPage+1;
40096 }
40097
40098 /* Request a pointer to the required page from the VFS */
40099 if( pWal->apWiData[iPage]==0 ){
40100 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
40101 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
40102 );
40103 }
40104
40105 *ppPage = pWal->apWiData[iPage];
40106 assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
40107 return rc;
40108 }
40109
40110 /*
40111 ** Return a pointer to the WalCkptInfo structure in the wal-index.
40112 */
40113 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
40114 assert( pWal->nWiData>0 && pWal->apWiData[0] );
40115 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
40116 }
40117
40118 /*
40119 ** Return a pointer to the WalIndexHdr structure in the wal-index.
40120 */
40121 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
40122 assert( pWal->nWiData>0 && pWal->apWiData[0] );
40123 return (volatile WalIndexHdr*)pWal->apWiData[0];
40124 }
40125
40126 /*
40127 ** The argument to this macro must be of type u32. On a little-endian
40128 ** architecture, it returns the u32 value that results from interpreting
40129 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
40130 ** returns the value that would be produced by intepreting the 4 bytes
40131 ** of the input value as a little-endian integer.
40132 */
40133 #define BYTESWAP32(x) ( \
40134 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
40135 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
40136 )
40137
40138 /*
40139 ** Generate or extend an 8 byte checksum based on the data in
40140 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
40141 ** initial values of 0 and 0 if aIn==NULL).
40142 **
40143 ** The checksum is written back into aOut[] before returning.
40144 **
40145 ** nByte must be a positive multiple of 8.
40146 */
40147 static void walChecksumBytes(
40148 int nativeCksum, /* True for native byte-order, false for non-native */
40149 u8 *a, /* Content to be checksummed */
40150 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
40151 const u32 *aIn, /* Initial checksum value input */
40152 u32 *aOut /* OUT: Final checksum value output */
40153 ){
40154 u32 s1, s2;
40155 u32 *aData = (u32 *)a;
40156 u32 *aEnd = (u32 *)&a[nByte];
40157
40158 if( aIn ){
40159 s1 = aIn[0];
40160 s2 = aIn[1];
40161 }else{
40162 s1 = s2 = 0;
40163 }
40164
40165 assert( nByte>=8 );
40166 assert( (nByte&0x00000007)==0 );
40167
40168 if( nativeCksum ){
40169 do {
40170 s1 += *aData++ + s2;
40171 s2 += *aData++ + s1;
40172 }while( aData<aEnd );
40173 }else{
40174 do {
40175 s1 += BYTESWAP32(aData[0]) + s2;
40176 s2 += BYTESWAP32(aData[1]) + s1;
40177 aData += 2;
40178 }while( aData<aEnd );
40179 }
40180
40181 aOut[0] = s1;
40182 aOut[1] = s2;
40183 }
40184
40185 /*
40186 ** Write the header information in pWal->hdr into the wal-index.
40187 **
40188 ** The checksum on pWal->hdr is updated before it is written.
40189 */
40190 static void walIndexWriteHdr(Wal *pWal){
40191 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40192 const int nCksum = offsetof(WalIndexHdr, aCksum);
40193
40194 assert( pWal->writeLock );
40195 pWal->hdr.isInit = 1;
40196 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40197 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40198 sqlite3OsShmBarrier(pWal->pDbFd);
40199 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40200 }
40201
40202 /*
40203 ** This function encodes a single frame header and writes it to a buffer
40204 ** supplied by the caller. A frame-header is made up of a series of
40205 ** 4-byte big-endian integers, as follows:
40206 **
40207 ** 0: Page number.
40208 ** 4: For commit records, the size of the database image in pages
40209 ** after the commit. For all other records, zero.
40210 ** 8: Salt-1 (copied from the wal-header)
40211 ** 12: Salt-2 (copied from the wal-header)
40212 ** 16: Checksum-1.
40213 ** 20: Checksum-2.
40214 */
40215 static void walEncodeFrame(
40216 Wal *pWal, /* The write-ahead log */
40217 u32 iPage, /* Database page number for frame */
40218 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
40219 u8 *aData, /* Pointer to page data */
40220 u8 *aFrame /* OUT: Write encoded frame here */
40221 ){
40222 int nativeCksum; /* True for native byte-order checksums */
40223 u32 *aCksum = pWal->hdr.aFrameCksum;
40224 assert( WAL_FRAME_HDRSIZE==24 );
40225 sqlite3Put4byte(&aFrame[0], iPage);
40226 sqlite3Put4byte(&aFrame[4], nTruncate);
40227 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
40228
40229 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
40230 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
40231 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
40232
40233 sqlite3Put4byte(&aFrame[16], aCksum[0]);
40234 sqlite3Put4byte(&aFrame[20], aCksum[1]);
40235 }
40236
40237 /*
40238 ** Check to see if the frame with header in aFrame[] and content
40239 ** in aData[] is valid. If it is a valid frame, fill *piPage and
40240 ** *pnTruncate and return true. Return if the frame is not valid.
40241 */
40242 static int walDecodeFrame(
40243 Wal *pWal, /* The write-ahead log */
40244 u32 *piPage, /* OUT: Database page number for frame */
40245 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
40246 u8 *aData, /* Pointer to page data (for checksum) */
40247 u8 *aFrame /* Frame data */
40248 ){
40249 int nativeCksum; /* True for native byte-order checksums */
40250 u32 *aCksum = pWal->hdr.aFrameCksum;
40251 u32 pgno; /* Page number of the frame */
40252 assert( WAL_FRAME_HDRSIZE==24 );
40253
40254 /* A frame is only valid if the salt values in the frame-header
40255 ** match the salt values in the wal-header.
40256 */
40257 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
40258 return 0;
40259 }
40260
40261 /* A frame is only valid if the page number is creater than zero.
40262 */
40263 pgno = sqlite3Get4byte(&aFrame[0]);
40264 if( pgno==0 ){
40265 return 0;
40266 }
40267
40268 /* A frame is only valid if a checksum of the first 16 bytes
40269 ** of the frame-header, and the frame-data matches
40270 ** the checksum in the last 8 bytes of the frame-header.
40271 */
40272 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
40273 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
40274 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
40275 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
40276 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
40277 ){
40278 /* Checksum failed. */
40279 return 0;
40280 }
40281
40282 /* If we reach this point, the frame is valid. Return the page number
40283 ** and the new database size.
40284 */
40285 *piPage = pgno;
40286 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
40287 return 1;
40288 }
40289
40290
40291 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
40292 /*
40293 ** Names of locks. This routine is used to provide debugging output and is not
40294 ** a part of an ordinary build.
40295 */
40296 static const char *walLockName(int lockIdx){
40297 if( lockIdx==WAL_WRITE_LOCK ){
40298 return "WRITE-LOCK";
40299 }else if( lockIdx==WAL_CKPT_LOCK ){
40300 return "CKPT-LOCK";
40301 }else if( lockIdx==WAL_RECOVER_LOCK ){
40302 return "RECOVER-LOCK";
40303 }else{
40304 static char zName[15];
40305 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
40306 lockIdx-WAL_READ_LOCK(0));
40307 return zName;
40308 }
40309 }
40310 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
40311
40312
40313 /*
40314 ** Set or release locks on the WAL. Locks are either shared or exclusive.
40315 ** A lock cannot be moved directly between shared and exclusive - it must go
40316 ** through the unlocked state first.
40317 **
40318 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
40319 */
40320 static int walLockShared(Wal *pWal, int lockIdx){
40321 int rc;
40322 if( pWal->exclusiveMode ) return SQLITE_OK;
40323 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
40324 SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
40325 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
40326 walLockName(lockIdx), rc ? "failed" : "ok"));
40327 VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
40328 return rc;
40329 }
40330 static void walUnlockShared(Wal *pWal, int lockIdx){
40331 if( pWal->exclusiveMode ) return;
40332 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
40333 SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
40334 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
40335 }
40336 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
40337 int rc;
40338 if( pWal->exclusiveMode ) return SQLITE_OK;
40339 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
40340 SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
40341 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
40342 walLockName(lockIdx), n, rc ? "failed" : "ok"));
40343 VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
40344 return rc;
40345 }
40346 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
40347 if( pWal->exclusiveMode ) return;
40348 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
40349 SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
40350 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
40351 walLockName(lockIdx), n));
40352 }
40353
40354 /*
40355 ** Compute a hash on a page number. The resulting hash value must land
40356 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
40357 ** the hash to the next value in the event of a collision.
40358 */
40359 static int walHash(u32 iPage){
40360 assert( iPage>0 );
40361 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
40362 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
40363 }
40364 static int walNextHash(int iPriorHash){
40365 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
40366 }
40367
40368 /*
40369 ** Return pointers to the hash table and page number array stored on
40370 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
40371 ** numbered starting from 0.
40372 **
40373 ** Set output variable *paHash to point to the start of the hash table
40374 ** in the wal-index file. Set *piZero to one less than the frame
40375 ** number of the first frame indexed by this hash table. If a
40376 ** slot in the hash table is set to N, it refers to frame number
40377 ** (*piZero+N) in the log.
40378 **
40379 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
40380 ** first frame indexed by the hash table, frame (*piZero+1).
40381 */
40382 static int walHashGet(
40383 Wal *pWal, /* WAL handle */
40384 int iHash, /* Find the iHash'th table */
40385 volatile ht_slot **paHash, /* OUT: Pointer to hash index */
40386 volatile u32 **paPgno, /* OUT: Pointer to page number array */
40387 u32 *piZero /* OUT: Frame associated with *paPgno[0] */
40388 ){
40389 int rc; /* Return code */
40390 volatile u32 *aPgno;
40391
40392 rc = walIndexPage(pWal, iHash, &aPgno);
40393 assert( rc==SQLITE_OK || iHash>0 );
40394
40395 if( rc==SQLITE_OK ){
40396 u32 iZero;
40397 volatile ht_slot *aHash;
40398
40399 aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
40400 if( iHash==0 ){
40401 aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
40402 iZero = 0;
40403 }else{
40404 iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
40405 }
40406
40407 *paPgno = &aPgno[-1];
40408 *paHash = aHash;
40409 *piZero = iZero;
40410 }
40411 return rc;
40412 }
40413
40414 /*
40415 ** Return the number of the wal-index page that contains the hash-table
40416 ** and page-number array that contain entries corresponding to WAL frame
40417 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
40418 ** are numbered starting from 0.
40419 */
40420 static int walFramePage(u32 iFrame){
40421 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
40422 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
40423 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
40424 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
40425 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
40426 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
40427 );
40428 return iHash;
40429 }
40430
40431 /*
40432 ** Return the page number associated with frame iFrame in this WAL.
40433 */
40434 static u32 walFramePgno(Wal *pWal, u32 iFrame){
40435 int iHash = walFramePage(iFrame);
40436 if( iHash==0 ){
40437 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
40438 }
40439 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
40440 }
40441
40442 /*
40443 ** Remove entries from the hash table that point to WAL slots greater
40444 ** than pWal->hdr.mxFrame.
40445 **
40446 ** This function is called whenever pWal->hdr.mxFrame is decreased due
40447 ** to a rollback or savepoint.
40448 **
40449 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
40450 ** updated. Any later hash tables will be automatically cleared when
40451 ** pWal->hdr.mxFrame advances to the point where those hash tables are
40452 ** actually needed.
40453 */
40454 static void walCleanupHash(Wal *pWal){
40455 volatile ht_slot *aHash; /* Pointer to hash table to clear */
40456 volatile u32 *aPgno; /* Page number array for hash table */
40457 u32 iZero; /* frame == (aHash[x]+iZero) */
40458 int iLimit = 0; /* Zero values greater than this */
40459 int nByte; /* Number of bytes to zero in aPgno[] */
40460 int i; /* Used to iterate through aHash[] */
40461
40462 assert( pWal->writeLock );
40463 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
40464 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
40465 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
40466
40467 if( pWal->hdr.mxFrame==0 ) return;
40468
40469 /* Obtain pointers to the hash-table and page-number array containing
40470 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
40471 ** that the page said hash-table and array reside on is already mapped.
40472 */
40473 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
40474 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
40475 walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
40476
40477 /* Zero all hash-table entries that correspond to frame numbers greater
40478 ** than pWal->hdr.mxFrame.
40479 */
40480 iLimit = pWal->hdr.mxFrame - iZero;
40481 assert( iLimit>0 );
40482 for(i=0; i<HASHTABLE_NSLOT; i++){
40483 if( aHash[i]>iLimit ){
40484 aHash[i] = 0;
40485 }
40486 }
40487
40488 /* Zero the entries in the aPgno array that correspond to frames with
40489 ** frame numbers greater than pWal->hdr.mxFrame.
40490 */
40491 nByte = ((char *)aHash - (char *)&aPgno[iLimit+1]);
40492 memset((void *)&aPgno[iLimit+1], 0, nByte);
40493
40494 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
40495 /* Verify that the every entry in the mapping region is still reachable
40496 ** via the hash table even after the cleanup.
40497 */
40498 if( iLimit ){
40499 int i; /* Loop counter */
40500 int iKey; /* Hash key */
40501 for(i=1; i<=iLimit; i++){
40502 for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
40503 if( aHash[iKey]==i ) break;
40504 }
40505 assert( aHash[iKey]==i );
40506 }
40507 }
40508 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
40509 }
40510
40511
40512 /*
40513 ** Set an entry in the wal-index that will map database page number
40514 ** pPage into WAL frame iFrame.
40515 */
40516 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
40517 int rc; /* Return code */
40518 u32 iZero; /* One less than frame number of aPgno[1] */
40519 volatile u32 *aPgno; /* Page number array */
40520 volatile ht_slot *aHash; /* Hash table */
40521
40522 rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
40523
40524 /* Assuming the wal-index file was successfully mapped, populate the
40525 ** page number array and hash table entry.
40526 */
40527 if( rc==SQLITE_OK ){
40528 int iKey; /* Hash table key */
40529 int idx; /* Value to write to hash-table slot */
40530 TESTONLY( int nCollide = 0; /* Number of hash collisions */ )
40531
40532 idx = iFrame - iZero;
40533 assert( idx <= HASHTABLE_NSLOT/2 + 1 );
40534
40535 /* If this is the first entry to be added to this hash-table, zero the
40536 ** entire hash table and aPgno[] array before proceding.
40537 */
40538 if( idx==1 ){
40539 int nByte = (u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1];
40540 memset((void*)&aPgno[1], 0, nByte);
40541 }
40542
40543 /* If the entry in aPgno[] is already set, then the previous writer
40544 ** must have exited unexpectedly in the middle of a transaction (after
40545 ** writing one or more dirty pages to the WAL to free up memory).
40546 ** Remove the remnants of that writers uncommitted transaction from
40547 ** the hash-table before writing any new entries.
40548 */
40549 if( aPgno[idx] ){
40550 walCleanupHash(pWal);
40551 assert( !aPgno[idx] );
40552 }
40553
40554 /* Write the aPgno[] array entry and the hash-table slot. */
40555 for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
40556 assert( nCollide++ < idx );
40557 }
40558 aPgno[idx] = iPage;
40559 aHash[iKey] = idx;
40560
40561 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
40562 /* Verify that the number of entries in the hash table exactly equals
40563 ** the number of entries in the mapping region.
40564 */
40565 {
40566 int i; /* Loop counter */
40567 int nEntry = 0; /* Number of entries in the hash table */
40568 for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
40569 assert( nEntry==idx );
40570 }
40571
40572 /* Verify that the every entry in the mapping region is reachable
40573 ** via the hash table. This turns out to be a really, really expensive
40574 ** thing to check, so only do this occasionally - not on every
40575 ** iteration.
40576 */
40577 if( (idx&0x3ff)==0 ){
40578 int i; /* Loop counter */
40579 for(i=1; i<=idx; i++){
40580 for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
40581 if( aHash[iKey]==i ) break;
40582 }
40583 assert( aHash[iKey]==i );
40584 }
40585 }
40586 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
40587 }
40588
40589
40590 return rc;
40591 }
40592
40593
40594 /*
40595 ** Recover the wal-index by reading the write-ahead log file.
40596 **
40597 ** This routine first tries to establish an exclusive lock on the
40598 ** wal-index to prevent other threads/processes from doing anything
40599 ** with the WAL or wal-index while recovery is running. The
40600 ** WAL_RECOVER_LOCK is also held so that other threads will know
40601 ** that this thread is running recovery. If unable to establish
40602 ** the necessary locks, this routine returns SQLITE_BUSY.
40603 */
40604 static int walIndexRecover(Wal *pWal){
40605 int rc; /* Return Code */
40606 i64 nSize; /* Size of log file */
40607 u32 aFrameCksum[2] = {0, 0};
40608 int iLock; /* Lock offset to lock for checkpoint */
40609 int nLock; /* Number of locks to hold */
40610
40611 /* Obtain an exclusive lock on all byte in the locking range not already
40612 ** locked by the caller. The caller is guaranteed to have locked the
40613 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
40614 ** If successful, the same bytes that are locked here are unlocked before
40615 ** this function returns.
40616 */
40617 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
40618 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
40619 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
40620 assert( pWal->writeLock );
40621 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
40622 nLock = SQLITE_SHM_NLOCK - iLock;
40623 rc = walLockExclusive(pWal, iLock, nLock);
40624 if( rc ){
40625 return rc;
40626 }
40627 WALTRACE(("WAL%p: recovery begin...\n", pWal));
40628
40629 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
40630
40631 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
40632 if( rc!=SQLITE_OK ){
40633 goto recovery_error;
40634 }
40635
40636 if( nSize>WAL_HDRSIZE ){
40637 u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */
40638 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
40639 int szFrame; /* Number of bytes in buffer aFrame[] */
40640 u8 *aData; /* Pointer to data part of aFrame buffer */
40641 int iFrame; /* Index of last frame read */
40642 i64 iOffset; /* Next offset to read from log file */
40643 int szPage; /* Page size according to the log */
40644 u32 magic; /* Magic value read from WAL header */
40645
40646 /* Read in the WAL header. */
40647 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40648 if( rc!=SQLITE_OK ){
40649 goto recovery_error;
40650 }
40651
40652 /* If the database page size is not a power of two, or is greater than
40653 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
40654 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
40655 ** WAL file.
40656 */
40657 magic = sqlite3Get4byte(&aBuf[0]);
40658 szPage = sqlite3Get4byte(&aBuf[8]);
40659 if( (magic&0xFFFFFFFE)!=WAL_MAGIC
40660 || szPage&(szPage-1)
40661 || szPage>SQLITE_MAX_PAGE_SIZE
40662 || szPage<512
40663 ){
40664 goto finished;
40665 }
40666 pWal->hdr.bigEndCksum = (magic&0x00000001);
40667 pWal->szPage = szPage;
40668 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40669 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40670 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40671 aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40672 );
40673
40674 /* Malloc a buffer to read frames into. */
40675 szFrame = szPage + WAL_FRAME_HDRSIZE;
40676 aFrame = (u8 *)sqlite3_malloc(szFrame);
40677 if( !aFrame ){
40678 rc = SQLITE_NOMEM;
40679 goto recovery_error;
40680 }
40681 aData = &aFrame[WAL_FRAME_HDRSIZE];
40682
40683 /* Read all frames from the log file. */
40684 iFrame = 0;
40685 for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
40686 u32 pgno; /* Database page number for frame */
40687 u32 nTruncate; /* dbsize field from frame header */
40688 int isValid; /* True if this frame is valid */
40689
40690 /* Read and decode the next log frame. */
40691 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
40692 if( rc!=SQLITE_OK ) break;
40693 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
40694 if( !isValid ) break;
40695 rc = walIndexAppend(pWal, ++iFrame, pgno);
40696 if( rc!=SQLITE_OK ) break;
40697
40698 /* If nTruncate is non-zero, this is a commit record. */
40699 if( nTruncate ){
40700 pWal->hdr.mxFrame = iFrame;
40701 pWal->hdr.nPage = nTruncate;
40702 pWal->hdr.szPage = szPage;
40703 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
40704 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
40705 }
40706 }
40707
40708 sqlite3_free(aFrame);
40709 }
40710
40711 finished:
40712 if( rc==SQLITE_OK ){
40713 volatile WalCkptInfo *pInfo;
40714 int i;
40715 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
40716 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
40717 walIndexWriteHdr(pWal);
40718
40719 /* Reset the checkpoint-header. This is safe because this thread is
40720 ** currently holding locks that exclude all other readers, writers and
40721 ** checkpointers.
40722 */
40723 pInfo = walCkptInfo(pWal);
40724 pInfo->nBackfill = 0;
40725 pInfo->aReadMark[0] = 0;
40726 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
40727 }
40728
40729 recovery_error:
40730 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
40731 walUnlockExclusive(pWal, iLock, nLock);
40732 return rc;
40733 }
40734
40735 /*
40736 ** Close an open wal-index.
40737 */
40738 static void walIndexClose(Wal *pWal, int isDelete){
40739 if( pWal->isWIndexOpen ){
40740 sqlite3OsShmClose(pWal->pDbFd, isDelete);
40741 pWal->isWIndexOpen = 0;
40742 }
40743 }
40744
40745 /*
40746 ** Open a connection to the WAL file associated with database zDbName.
40747 ** The database file must already be opened on connection pDbFd.
40748 **
40749 ** A SHARED lock should be held on the database file when this function
40750 ** is called. The purpose of this SHARED lock is to prevent any other
40751 ** client from unlinking the WAL or wal-index file. If another process
40752 ** were to do this just after this client opened one of these files, the
40753 ** system would be badly broken.
40754 **
40755 ** If the log file is successfully opened, SQLITE_OK is returned and
40756 ** *ppWal is set to point to a new WAL handle. If an error occurs,
40757 ** an SQLite error code is returned and *ppWal is left unmodified.
40758 */
40759 SQLITE_PRIVATE int sqlite3WalOpen(
40760 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
40761 sqlite3_file *pDbFd, /* The open database file */
40762 const char *zDbName, /* Name of the database file */
40763 Wal **ppWal /* OUT: Allocated Wal handle */
40764 ){
40765 int rc; /* Return Code */
40766 Wal *pRet; /* Object to allocate and return */
40767 int flags; /* Flags passed to OsOpen() */
40768 char *zWal; /* Name of write-ahead log file */
40769 int nWal; /* Length of zWal in bytes */
40770
40771 assert( zDbName && zDbName[0] );
40772 assert( pDbFd );
40773
40774 /* In the amalgamation, the os_unix.c and os_win.c source files come before
40775 ** this source file. Verify that the #defines of the locking byte offsets
40776 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
40777 */
40778 #ifdef WIN_SHM_BASE
40779 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
40780 #endif
40781 #ifdef UNIX_SHM_BASE
40782 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
40783 #endif
40784
40785
40786 /* Allocate an instance of struct Wal to return. */
40787 *ppWal = 0;
40788 nWal = sqlite3Strlen30(zDbName) + 5;
40789 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal);
40790 if( !pRet ){
40791 return SQLITE_NOMEM;
40792 }
40793
40794 pRet->pVfs = pVfs;
40795 pRet->pWalFd = (sqlite3_file *)&pRet[1];
40796 pRet->pDbFd = pDbFd;
40797 pRet->readLock = -1;
40798 sqlite3_randomness(8, &pRet->hdr.aSalt);
40799 pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd;
40800 sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName);
40801 rc = sqlite3OsShmOpen(pDbFd);
40802
40803 /* Open file handle on the write-ahead log file. */
40804 if( rc==SQLITE_OK ){
40805 pRet->isWIndexOpen = 1;
40806 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL);
40807 rc = sqlite3OsOpen(pVfs, zWal, pRet->pWalFd, flags, &flags);
40808 }
40809
40810 if( rc!=SQLITE_OK ){
40811 walIndexClose(pRet, 0);
40812 sqlite3OsClose(pRet->pWalFd);
40813 sqlite3_free(pRet);
40814 }else{
40815 *ppWal = pRet;
40816 WALTRACE(("WAL%d: opened\n", pRet));
40817 }
40818 return rc;
40819 }
40820
40821 /*
40822 ** Find the smallest page number out of all pages held in the WAL that
40823 ** has not been returned by any prior invocation of this method on the
40824 ** same WalIterator object. Write into *piFrame the frame index where
40825 ** that page was last written into the WAL. Write into *piPage the page
40826 ** number.
40827 **
40828 ** Return 0 on success. If there are no pages in the WAL with a page
40829 ** number larger than *piPage, then return 1.
40830 */
40831 static int walIteratorNext(
40832 WalIterator *p, /* Iterator */
40833 u32 *piPage, /* OUT: The page number of the next page */
40834 u32 *piFrame /* OUT: Wal frame index of next page */
40835 ){
40836 u32 iMin; /* Result pgno must be greater than iMin */
40837 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
40838 int i; /* For looping through segments */
40839
40840 iMin = p->iPrior;
40841 assert( iMin<0xffffffff );
40842 for(i=p->nSegment-1; i>=0; i--){
40843 struct WalSegment *pSegment = &p->aSegment[i];
40844 while( pSegment->iNext<pSegment->nEntry ){
40845 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
40846 if( iPg>iMin ){
40847 if( iPg<iRet ){
40848 iRet = iPg;
40849 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
40850 }
40851 break;
40852 }
40853 pSegment->iNext++;
40854 }
40855 }
40856
40857 *piPage = p->iPrior = iRet;
40858 return (iRet==0xFFFFFFFF);
40859 }
40860
40861
40862 static void walMergesort(
40863 u32 *aContent, /* Pages in wal */
40864 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
40865 ht_slot *aList, /* IN/OUT: List to sort */
40866 int *pnList /* IN/OUT: Number of elements in aList[] */
40867 ){
40868 int nList = *pnList;
40869 if( nList>1 ){
40870 int nLeft = nList / 2; /* Elements in left list */
40871 int nRight = nList - nLeft; /* Elements in right list */
40872 int iLeft = 0; /* Current index in aLeft */
40873 int iRight = 0; /* Current index in aright */
40874 int iOut = 0; /* Current index in output buffer */
40875 ht_slot *aLeft = aList; /* Left list */
40876 ht_slot *aRight = aList+nLeft;/* Right list */
40877
40878 /* TODO: Change to non-recursive version. */
40879 walMergesort(aContent, aBuffer, aLeft, &nLeft);
40880 walMergesort(aContent, aBuffer, aRight, &nRight);
40881
40882 while( iRight<nRight || iLeft<nLeft ){
40883 ht_slot logpage;
40884 Pgno dbpage;
40885
40886 if( (iLeft<nLeft)
40887 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
40888 ){
40889 logpage = aLeft[iLeft++];
40890 }else{
40891 logpage = aRight[iRight++];
40892 }
40893 dbpage = aContent[logpage];
40894
40895 aBuffer[iOut++] = logpage;
40896 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
40897
40898 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
40899 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
40900 }
40901 memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
40902 *pnList = iOut;
40903 }
40904
40905 #ifdef SQLITE_DEBUG
40906 {
40907 int i;
40908 for(i=1; i<*pnList; i++){
40909 assert( aContent[aList[i]] > aContent[aList[i-1]] );
40910 }
40911 }
40912 #endif
40913 }
40914
40915 /*
40916 ** Free an iterator allocated by walIteratorInit().
40917 */
40918 static void walIteratorFree(WalIterator *p){
40919 sqlite3_free(p);
40920 }
40921
40922 /*
40923 ** Map the wal-index into memory owned by this thread, if it is not
40924 ** mapped already. Then construct a WalInterator object that can be
40925 ** used to loop over all pages in the WAL in ascending order.
40926 **
40927 ** On success, make *pp point to the newly allocated WalInterator object
40928 ** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error
40929 ** code.
40930 **
40931 ** The calling routine should invoke walIteratorFree() to destroy the
40932 ** WalIterator object when it has finished with it. The caller must
40933 ** also unmap the wal-index. But the wal-index must not be unmapped
40934 ** prior to the WalIterator object being destroyed.
40935 */
40936 static int walIteratorInit(Wal *pWal, WalIterator **pp){
40937 WalIterator *p; /* Return value */
40938 int nSegment; /* Number of segments to merge */
40939 u32 iLast; /* Last frame in log */
40940 int nByte; /* Number of bytes to allocate */
40941 int i; /* Iterator variable */
40942 ht_slot *aTmp; /* Temp space used by merge-sort */
40943 ht_slot *aSpace; /* Space at the end of the allocation */
40944
40945 /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other
40946 ** thread is able to write to shared memory while this routine is
40947 ** running (or, indeed, while the WalIterator object exists). Hence,
40948 ** we can cast off the volatile qualification from shared memory
40949 */
40950 assert( pWal->ckptLock );
40951 iLast = pWal->hdr.mxFrame;
40952
40953 /* Allocate space for the WalIterator object */
40954 nSegment = walFramePage(iLast) + 1;
40955 nByte = sizeof(WalIterator)
40956 + nSegment*(sizeof(struct WalSegment))
40957 + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot));
40958 p = (WalIterator *)sqlite3_malloc(nByte);
40959 if( !p ){
40960 return SQLITE_NOMEM;
40961 }
40962 memset(p, 0, nByte);
40963
40964 /* Allocate space for the WalIterator object */
40965 p->nSegment = nSegment;
40966 aSpace = (ht_slot *)&p->aSegment[nSegment];
40967 aTmp = &aSpace[HASHTABLE_NPAGE*nSegment];
40968 for(i=0; i<nSegment; i++){
40969 volatile ht_slot *aHash;
40970 int j;
40971 u32 iZero;
40972 int nEntry;
40973 volatile u32 *aPgno;
40974 int rc;
40975
40976 rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
40977 if( rc!=SQLITE_OK ){
40978 walIteratorFree(p);
40979 return rc;
40980 }
40981 aPgno++;
40982 nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
40983 iZero++;
40984
40985 for(j=0; j<nEntry; j++){
40986 aSpace[j] = j;
40987 }
40988 walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry);
40989 p->aSegment[i].iZero = iZero;
40990 p->aSegment[i].nEntry = nEntry;
40991 p->aSegment[i].aIndex = aSpace;
40992 p->aSegment[i].aPgno = (u32 *)aPgno;
40993 aSpace += HASHTABLE_NPAGE;
40994 }
40995 assert( aSpace==aTmp );
40996
40997 /* Return the fully initialized WalIterator object */
40998 *pp = p;
40999 return SQLITE_OK ;
41000 }
41001
41002 /*
41003 ** Copy as much content as we can from the WAL back into the database file
41004 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
41005 **
41006 ** The amount of information copies from WAL to database might be limited
41007 ** by active readers. This routine will never overwrite a database page
41008 ** that a concurrent reader might be using.
41009 **
41010 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
41011 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
41012 ** checkpoints are always run by a background thread or background
41013 ** process, foreground threads will never block on a lengthy fsync call.
41014 **
41015 ** Fsync is called on the WAL before writing content out of the WAL and
41016 ** into the database. This ensures that if the new content is persistent
41017 ** in the WAL and can be recovered following a power-loss or hard reset.
41018 **
41019 ** Fsync is also called on the database file if (and only if) the entire
41020 ** WAL content is copied into the database file. This second fsync makes
41021 ** it safe to delete the WAL since the new content will persist in the
41022 ** database file.
41023 **
41024 ** This routine uses and updates the nBackfill field of the wal-index header.
41025 ** This is the only routine tha will increase the value of nBackfill.
41026 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
41027 ** its value.)
41028 **
41029 ** The caller must be holding sufficient locks to ensure that no other
41030 ** checkpoint is running (in any other thread or process) at the same
41031 ** time.
41032 */
41033 static int walCheckpoint(
41034 Wal *pWal, /* Wal connection */
41035 int sync_flags, /* Flags for OsSync() (or 0) */
41036 int nBuf, /* Size of zBuf in bytes */
41037 u8 *zBuf /* Temporary buffer to use */
41038 ){
41039 int rc; /* Return code */
41040 int szPage = pWal->hdr.szPage; /* Database page-size */
41041 WalIterator *pIter = 0; /* Wal iterator context */
41042 u32 iDbpage = 0; /* Next database page to write */
41043 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
41044 u32 mxSafeFrame; /* Max frame that can be backfilled */
41045 int i; /* Loop counter */
41046 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
41047
41048 /* Allocate the iterator */
41049 rc = walIteratorInit(pWal, &pIter);
41050 if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){
41051 goto walcheckpoint_out;
41052 }
41053
41054 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
41055 if( pWal->hdr.szPage!=nBuf ){
41056 rc = SQLITE_CORRUPT_BKPT;
41057 goto walcheckpoint_out;
41058 }
41059
41060 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
41061 ** safe to write into the database. Frames beyond mxSafeFrame might
41062 ** overwrite database pages that are in use by active readers and thus
41063 ** cannot be backfilled from the WAL.
41064 */
41065 mxSafeFrame = pWal->hdr.mxFrame;
41066 pInfo = walCkptInfo(pWal);
41067 for(i=1; i<WAL_NREADER; i++){
41068 u32 y = pInfo->aReadMark[i];
41069 if( mxSafeFrame>=y ){
41070 assert( y<=pWal->hdr.mxFrame );
41071 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
41072 if( rc==SQLITE_OK ){
41073 pInfo->aReadMark[i] = READMARK_NOT_USED;
41074 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
41075 }else if( rc==SQLITE_BUSY ){
41076 mxSafeFrame = y;
41077 }else{
41078 goto walcheckpoint_out;
41079 }
41080 }
41081 }
41082
41083 if( pInfo->nBackfill<mxSafeFrame
41084 && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK
41085 ){
41086 u32 nBackfill = pInfo->nBackfill;
41087
41088 /* Sync the WAL to disk */
41089 if( sync_flags ){
41090 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
41091 }
41092
41093 /* Iterate through the contents of the WAL, copying data to the db file. */
41094 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
41095 assert( walFramePgno(pWal, iFrame)==iDbpage );
41096 if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue;
41097 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage,
41098 walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE
41099 );
41100 if( rc!=SQLITE_OK ) break;
41101 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, (iDbpage-1)*szPage);
41102 if( rc!=SQLITE_OK ) break;
41103 }
41104
41105 /* If work was actually accomplished... */
41106 if( rc==SQLITE_OK ){
41107 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
41108 rc = sqlite3OsTruncate(pWal->pDbFd, ((i64)pWal->hdr.nPage*(i64)szPage));
41109 if( rc==SQLITE_OK && sync_flags ){
41110 rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
41111 }
41112 }
41113 if( rc==SQLITE_OK ){
41114 pInfo->nBackfill = mxSafeFrame;
41115 }
41116 }
41117
41118 /* Release the reader lock held while backfilling */
41119 walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
41120 }else if( rc==SQLITE_BUSY ){
41121 /* Reset the return code so as not to report a checkpoint failure
41122 ** just because active readers prevent any backfill.
41123 */
41124 rc = SQLITE_OK;
41125 }
41126
41127 walcheckpoint_out:
41128 walIteratorFree(pIter);
41129 return rc;
41130 }
41131
41132 /*
41133 ** Close a connection to a log file.
41134 */
41135 SQLITE_PRIVATE int sqlite3WalClose(
41136 Wal *pWal, /* Wal to close */
41137 int sync_flags, /* Flags to pass to OsSync() (or 0) */
41138 int nBuf,
41139 u8 *zBuf /* Buffer of at least nBuf bytes */
41140 ){
41141 int rc = SQLITE_OK;
41142 if( pWal ){
41143 int isDelete = 0; /* True to unlink wal and wal-index files */
41144
41145 /* If an EXCLUSIVE lock can be obtained on the database file (using the
41146 ** ordinary, rollback-mode locking methods, this guarantees that the
41147 ** connection associated with this log file is the only connection to
41148 ** the database. In this case checkpoint the database and unlink both
41149 ** the wal and wal-index files.
41150 **
41151 ** The EXCLUSIVE lock is not released before returning.
41152 */
41153 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
41154 if( rc==SQLITE_OK ){
41155 pWal->exclusiveMode = 1;
41156 rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf);
41157 if( rc==SQLITE_OK ){
41158 isDelete = 1;
41159 }
41160 }
41161
41162 walIndexClose(pWal, isDelete);
41163 sqlite3OsClose(pWal->pWalFd);
41164 if( isDelete ){
41165 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
41166 }
41167 WALTRACE(("WAL%p: closed\n", pWal));
41168 sqlite3_free(pWal->apWiData);
41169 sqlite3_free(pWal);
41170 }
41171 return rc;
41172 }
41173
41174 /*
41175 ** Try to read the wal-index header. Return 0 on success and 1 if
41176 ** there is a problem.
41177 **
41178 ** The wal-index is in shared memory. Another thread or process might
41179 ** be writing the header at the same time this procedure is trying to
41180 ** read it, which might result in inconsistency. A dirty read is detected
41181 ** by verifying that both copies of the header are the same and also by
41182 ** a checksum on the header.
41183 **
41184 ** If and only if the read is consistent and the header is different from
41185 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
41186 ** and *pChanged is set to 1.
41187 **
41188 ** If the checksum cannot be verified return non-zero. If the header
41189 ** is read successfully and the checksum verified, return zero.
41190 */
41191 int walIndexTryHdr(Wal *pWal, int *pChanged){
41192 u32 aCksum[2]; /* Checksum on the header content */
41193 WalIndexHdr h1, h2; /* Two copies of the header content */
41194 WalIndexHdr volatile *aHdr; /* Header in shared memory */
41195
41196 /* The first page of the wal-index must be mapped at this point. */
41197 assert( pWal->nWiData>0 && pWal->apWiData[0] );
41198
41199 /* Read the header. This might happen currently with a write to the
41200 ** same area of shared memory on a different CPU in a SMP,
41201 ** meaning it is possible that an inconsistent snapshot is read
41202 ** from the file. If this happens, return non-zero.
41203 **
41204 ** There are two copies of the header at the beginning of the wal-index.
41205 ** When reading, read [0] first then [1]. Writes are in the reverse order.
41206 ** Memory barriers are used to prevent the compiler or the hardware from
41207 ** reordering the reads and writes.
41208 */
41209 aHdr = walIndexHdr(pWal);
41210 memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
41211 sqlite3OsShmBarrier(pWal->pDbFd);
41212 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
41213
41214 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
41215 return 1; /* Dirty read */
41216 }
41217 if( h1.isInit==0 ){
41218 return 1; /* Malformed header - probably all zeros */
41219 }
41220 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
41221 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
41222 return 1; /* Checksum does not match */
41223 }
41224
41225 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
41226 *pChanged = 1;
41227 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
41228 pWal->szPage = pWal->hdr.szPage;
41229 }
41230
41231 /* The header was successfully read. Return zero. */
41232 return 0;
41233 }
41234
41235 /*
41236 ** Read the wal-index header from the wal-index and into pWal->hdr.
41237 ** If the wal-header appears to be corrupt, try to recover the log
41238 ** before returning.
41239 **
41240 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41241 ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41242 ** to 0.
41243 **
41244 ** This routine also maps the wal-index content into memory and assigns
41245 ** ownership of that mapping to the current thread. In some implementations,
41246 ** only one thread at a time can hold a mapping of the wal-index. Hence,
41247 ** the caller should strive to invoke walIndexUnmap() as soon as possible
41248 ** after this routine returns.
41249 **
41250 ** If the wal-index header is successfully read, return SQLITE_OK.
41251 ** Otherwise an SQLite error code.
41252 */
41253 static int walIndexReadHdr(Wal *pWal, int *pChanged){
41254 int rc; /* Return code */
41255 int badHdr; /* True if a header read failed */
41256 volatile u32 *page0;
41257
41258 /* Ensure that page 0 of the wal-index (the page that contains the
41259 ** wal-index header) is mapped. Return early if an error occurs here.
41260 */
41261 assert( pChanged );
41262 rc = walIndexPage(pWal, 0, &page0);
41263 if( rc!=SQLITE_OK ){
41264 return rc;
41265 };
41266 assert( page0 || pWal->writeLock==0 );
41267
41268 /* If the first page of the wal-index has been mapped, try to read the
41269 ** wal-index header immediately, without holding any lock. This usually
41270 ** works, but may fail if the wal-index header is corrupt or currently
41271 ** being modified by another user.
41272 */
41273 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41274
41275 /* If the first attempt failed, it might have been due to a race
41276 ** with a writer. So get a WRITE lock and try again.
41277 */
41278 assert( badHdr==0 || pWal->writeLock==0 );
41279 if( badHdr && SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
41280 pWal->writeLock = 1;
41281 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
41282 badHdr = walIndexTryHdr(pWal, pChanged);
41283 if( badHdr ){
41284 /* If the wal-index header is still malformed even while holding
41285 ** a WRITE lock, it can only mean that the header is corrupted and
41286 ** needs to be reconstructed. So run recovery to do exactly that.
41287 */
41288 rc = walIndexRecover(pWal);
41289 *pChanged = 1;
41290 }
41291 }
41292 pWal->writeLock = 0;
41293 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41294 }
41295
41296 return rc;
41297 }
41298
41299 /*
41300 ** This is the value that walTryBeginRead returns when it needs to
41301 ** be retried.
41302 */
41303 #define WAL_RETRY (-1)
41304
41305 /*
41306 ** Attempt to start a read transaction. This might fail due to a race or
41307 ** other transient condition. When that happens, it returns WAL_RETRY to
41308 ** indicate to the caller that it is safe to retry immediately.
41309 **
41310 ** On success return SQLITE_OK. On a permantent failure (such an
41311 ** I/O error or an SQLITE_BUSY because another process is running
41312 ** recovery) return a positive error code.
41313 **
41314 ** On success, this routine obtains a read lock on
41315 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41316 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41317 ** that means the Wal does not hold any read lock. The reader must not
41318 ** access any database page that is modified by a WAL frame up to and
41319 ** including frame number aReadMark[pWal->readLock]. The reader will
41320 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41321 ** Or if pWal->readLock==0, then the reader will ignore the WAL
41322 ** completely and get all content directly from the database file.
41323 ** When the read transaction is completed, the caller must release the
41324 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41325 **
41326 ** This routine uses the nBackfill and aReadMark[] fields of the header
41327 ** to select a particular WAL_READ_LOCK() that strives to let the
41328 ** checkpoint process do as much work as possible. This routine might
41329 ** update values of the aReadMark[] array in the header, but if it does
41330 ** so it takes care to hold an exclusive lock on the corresponding
41331 ** WAL_READ_LOCK() while changing values.
41332 */
41333 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
41334 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
41335 u32 mxReadMark; /* Largest aReadMark[] value */
41336 int mxI; /* Index of largest aReadMark[] value */
41337 int i; /* Loop counter */
41338 int rc = SQLITE_OK; /* Return code */
41339
41340 assert( pWal->readLock<0 ); /* Not currently locked */
41341
41342 /* Take steps to avoid spinning forever if there is a protocol error. */
41343 if( cnt>5 ){
41344 if( cnt>100 ) return SQLITE_PROTOCOL;
41345 sqlite3OsSleep(pWal->pVfs, 1);
41346 }
41347
41348 if( !useWal ){
41349 rc = walIndexReadHdr(pWal, pChanged);
41350 if( rc==SQLITE_BUSY ){
41351 /* If there is not a recovery running in another thread or process
41352 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
41353 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
41354 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
41355 ** would be technically correct. But the race is benign since with
41356 ** WAL_RETRY this routine will be called again and will probably be
41357 ** right on the second iteration.
41358 */
41359 rc = walLockShared(pWal, WAL_RECOVER_LOCK);
41360 if( rc==SQLITE_OK ){
41361 walUnlockShared(pWal, WAL_RECOVER_LOCK);
41362 rc = WAL_RETRY;
41363 }else if( rc==SQLITE_BUSY ){
41364 rc = SQLITE_BUSY_RECOVERY;
41365 }
41366 }
41367 }
41368 if( rc!=SQLITE_OK ){
41369 return rc;
41370 }
41371
41372 pInfo = walCkptInfo(pWal);
41373 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41374 /* The WAL has been completely backfilled (or it is empty).
41375 ** and can be safely ignored.
41376 */
41377 rc = walLockShared(pWal, WAL_READ_LOCK(0));
41378 sqlite3OsShmBarrier(pWal->pDbFd);
41379 if( rc==SQLITE_OK ){
41380 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
41381 /* It is not safe to allow the reader to continue here if frames
41382 ** may have been appended to the log before READ_LOCK(0) was obtained.
41383 ** When holding READ_LOCK(0), the reader ignores the entire log file,
41384 ** which implies that the database file contains a trustworthy
41385 ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
41386 ** happening, this is usually correct.
41387 **
41388 ** However, if frames have been appended to the log (or if the log
41389 ** is wrapped and written for that matter) before the READ_LOCK(0)
41390 ** is obtained, that is not necessarily true. A checkpointer may
41391 ** have started to backfill the appended frames but crashed before
41392 ** it finished. Leaving a corrupt image in the database file.
41393 */
41394 walUnlockShared(pWal, WAL_READ_LOCK(0));
41395 return WAL_RETRY;
41396 }
41397 pWal->readLock = 0;
41398 return SQLITE_OK;
41399 }else if( rc!=SQLITE_BUSY ){
41400 return rc;
41401 }
41402 }
41403
41404 /* If we get this far, it means that the reader will want to use
41405 ** the WAL to get at content from recent commits. The job now is
41406 ** to select one of the aReadMark[] entries that is closest to
41407 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
41408 */
41409 mxReadMark = 0;
41410 mxI = 0;
41411 for(i=1; i<WAL_NREADER; i++){
41412 u32 thisMark = pInfo->aReadMark[i];
41413 if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
41414 assert( thisMark!=READMARK_NOT_USED );
41415 mxReadMark = thisMark;
41416 mxI = i;
41417 }
41418 }
41419 if( mxI==0 ){
41420 /* If we get here, it means that all of the aReadMark[] entries between
41421 ** 1 and WAL_NREADER-1 are zero. Try to initialize aReadMark[1] to
41422 ** be mxFrame, then retry.
41423 */
41424 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), 1);
41425 if( rc==SQLITE_OK ){
41426 pInfo->aReadMark[1] = pWal->hdr.mxFrame;
41427 walUnlockExclusive(pWal, WAL_READ_LOCK(1), 1);
41428 rc = WAL_RETRY;
41429 }else if( rc==SQLITE_BUSY ){
41430 rc = WAL_RETRY;
41431 }
41432 return rc;
41433 }else{
41434 if( mxReadMark < pWal->hdr.mxFrame ){
41435 for(i=1; i<WAL_NREADER; i++){
41436 rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
41437 if( rc==SQLITE_OK ){
41438 mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
41439 mxI = i;
41440 walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
41441 break;
41442 }else if( rc!=SQLITE_BUSY ){
41443 return rc;
41444 }
41445 }
41446 }
41447
41448 rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
41449 if( rc ){
41450 return rc==SQLITE_BUSY ? WAL_RETRY : rc;
41451 }
41452 /* Now that the read-lock has been obtained, check that neither the
41453 ** value in the aReadMark[] array or the contents of the wal-index
41454 ** header have changed.
41455 **
41456 ** It is necessary to check that the wal-index header did not change
41457 ** between the time it was read and when the shared-lock was obtained
41458 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
41459 ** that the log file may have been wrapped by a writer, or that frames
41460 ** that occur later in the log than pWal->hdr.mxFrame may have been
41461 ** copied into the database by a checkpointer. If either of these things
41462 ** happened, then reading the database with the current value of
41463 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
41464 ** instead.
41465 **
41466 ** This does not guarantee that the copy of the wal-index header is up to
41467 ** date before proceeding. That would not be possible without somehow
41468 ** blocking writers. It only guarantees that a dangerous checkpoint or
41469 ** log-wrap (either of which would require an exclusive lock on
41470 ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
41471 */
41472 sqlite3OsShmBarrier(pWal->pDbFd);
41473 if( pInfo->aReadMark[mxI]!=mxReadMark
41474 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
41475 ){
41476 walUnlockShared(pWal, WAL_READ_LOCK(mxI));
41477 return WAL_RETRY;
41478 }else{
41479 assert( mxReadMark<=pWal->hdr.mxFrame );
41480 pWal->readLock = mxI;
41481 }
41482 }
41483 return rc;
41484 }
41485
41486 /*
41487 ** Begin a read transaction on the database.
41488 **
41489 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
41490 ** it takes a snapshot of the state of the WAL and wal-index for the current
41491 ** instant in time. The current thread will continue to use this snapshot.
41492 ** Other threads might append new content to the WAL and wal-index but
41493 ** that extra content is ignored by the current thread.
41494 **
41495 ** If the database contents have changes since the previous read
41496 ** transaction, then *pChanged is set to 1 before returning. The
41497 ** Pager layer will use this to know that is cache is stale and
41498 ** needs to be flushed.
41499 */
41500 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
41501 int rc; /* Return code */
41502 int cnt = 0; /* Number of TryBeginRead attempts */
41503
41504 do{
41505 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
41506 }while( rc==WAL_RETRY );
41507 return rc;
41508 }
41509
41510 /*
41511 ** Finish with a read transaction. All this does is release the
41512 ** read-lock.
41513 */
41514 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
41515 if( pWal->readLock>=0 ){
41516 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
41517 pWal->readLock = -1;
41518 }
41519 }
41520
41521 /*
41522 ** Read a page from the WAL, if it is present in the WAL and if the
41523 ** current read transaction is configured to use the WAL.
41524 **
41525 ** The *pInWal is set to 1 if the requested page is in the WAL and
41526 ** has been loaded. Or *pInWal is set to 0 if the page was not in
41527 ** the WAL and needs to be read out of the database.
41528 */
41529 SQLITE_PRIVATE int sqlite3WalRead(
41530 Wal *pWal, /* WAL handle */
41531 Pgno pgno, /* Database page number to read data for */
41532 int *pInWal, /* OUT: True if data is read from WAL */
41533 int nOut, /* Size of buffer pOut in bytes */
41534 u8 *pOut /* Buffer to write page data to */
41535 ){
41536 u32 iRead = 0; /* If !=0, WAL frame to return data from */
41537 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
41538 int iHash; /* Used to loop through N hash tables */
41539
41540 /* This routine is only be called from within a read transaction. */
41541 assert( pWal->readLock>=0 || pWal->lockError );
41542
41543 /* If the "last page" field of the wal-index header snapshot is 0, then
41544 ** no data will be read from the wal under any circumstances. Return early
41545 ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41546 ** pWal->readLock==0, then the WAL is ignored by the reader so
41547 ** return early, as if the WAL were empty.
41548 */
41549 if( iLast==0 || pWal->readLock==0 ){
41550 *pInWal = 0;
41551 return SQLITE_OK;
41552 }
41553
41554 /* Search the hash table or tables for an entry matching page number
41555 ** pgno. Each iteration of the following for() loop searches one
41556 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41557 **
41558 ** This code may run concurrently to the code in walIndexAppend()
41559 ** that adds entries to the wal-index (and possibly to this hash
41560 ** table). This means the value just read from the hash
41561 ** slot (aHash[iKey]) may have been added before or after the
41562 ** current read transaction was opened. Values added after the
41563 ** read transaction was opened may have been written incorrectly -
41564 ** i.e. these slots may contain garbage data. However, we assume
41565 ** that any slots written before the current read transaction was
41566 ** opened remain unmodified.
41567 **
41568 ** For the reasons above, the if(...) condition featured in the inner
41569 ** loop of the following block is more stringent that would be required
41570 ** if we had exclusive access to the hash-table:
41571 **
41572 ** (aPgno[iFrame]==pgno):
41573 ** This condition filters out normal hash-table collisions.
41574 **
41575 ** (iFrame<=iLast):
41576 ** This condition filters out entries that were added to the hash
41577 ** table after the current read-transaction had started.
41578 */
41579 for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
41580 volatile ht_slot *aHash; /* Pointer to hash table */
41581 volatile u32 *aPgno; /* Pointer to array of page numbers */
41582 u32 iZero; /* Frame number corresponding to aPgno[0] */
41583 int iKey; /* Hash slot index */
41584 int rc;
41585
41586 rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
41587 if( rc!=SQLITE_OK ){
41588 return rc;
41589 }
41590 for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
41591 u32 iFrame = aHash[iKey] + iZero;
41592 if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
41593 assert( iFrame>iRead );
41594 iRead = iFrame;
41595 }
41596 }
41597 }
41598
41599 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
41600 /* If expensive assert() statements are available, do a linear search
41601 ** of the wal-index file content. Make sure the results agree with the
41602 ** result obtained using the hash indexes above. */
41603 {
41604 u32 iRead2 = 0;
41605 u32 iTest;
41606 for(iTest=iLast; iTest>0; iTest--){
41607 if( walFramePgno(pWal, iTest)==pgno ){
41608 iRead2 = iTest;
41609 break;
41610 }
41611 }
41612 assert( iRead==iRead2 );
41613 }
41614 #endif
41615
41616 /* If iRead is non-zero, then it is the log frame number that contains the
41617 ** required page. Read and return data from the log file.
41618 */
41619 if( iRead ){
41620 i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE;
41621 *pInWal = 1;
41622 return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
41623 }
41624
41625 *pInWal = 0;
41626 return SQLITE_OK;
41627 }
41628
41629
41630 /*
41631 ** Set *pPgno to the size of the database file (or zero, if unknown).
41632 */
41633 SQLITE_PRIVATE void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
41634 assert( pWal->readLock>=0 || pWal->lockError );
41635 *pPgno = pWal->hdr.nPage;
41636 }
41637
41638
41639 /*
41640 ** This function starts a write transaction on the WAL.
41641 **
41642 ** A read transaction must have already been started by a prior call
41643 ** to sqlite3WalBeginReadTransaction().
41644 **
41645 ** If another thread or process has written into the database since
41646 ** the read transaction was started, then it is not possible for this
41647 ** thread to write as doing so would cause a fork. So this routine
41648 ** returns SQLITE_BUSY in that case and no write transaction is started.
41649 **
41650 ** There can only be a single writer active at a time.
41651 */
41652 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
41653 int rc;
41654
41655 /* Cannot start a write transaction without first holding a read
41656 ** transaction. */
41657 assert( pWal->readLock>=0 );
41658
41659 /* Only one writer allowed at a time. Get the write lock. Return
41660 ** SQLITE_BUSY if unable.
41661 */
41662 rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
41663 if( rc ){
41664 return rc;
41665 }
41666 pWal->writeLock = 1;
41667
41668 /* If another connection has written to the database file since the
41669 ** time the read transaction on this connection was started, then
41670 ** the write is disallowed.
41671 */
41672 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
41673 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41674 pWal->writeLock = 0;
41675 rc = SQLITE_BUSY;
41676 }
41677
41678 return rc;
41679 }
41680
41681 /*
41682 ** End a write transaction. The commit has already been done. This
41683 ** routine merely releases the lock.
41684 */
41685 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
41686 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41687 pWal->writeLock = 0;
41688 return SQLITE_OK;
41689 }
41690
41691 /*
41692 ** If any data has been written (but not committed) to the log file, this
41693 ** function moves the write-pointer back to the start of the transaction.
41694 **
41695 ** Additionally, the callback function is invoked for each frame written
41696 ** to the WAL since the start of the transaction. If the callback returns
41697 ** other than SQLITE_OK, it is not invoked again and the error code is
41698 ** returned to the caller.
41699 **
41700 ** Otherwise, if the callback function does not return an error, this
41701 ** function returns SQLITE_OK.
41702 */
41703 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
41704 int rc = SQLITE_OK;
41705 if( pWal->writeLock ){
41706 Pgno iMax = pWal->hdr.mxFrame;
41707 Pgno iFrame;
41708
41709 /* Restore the clients cache of the wal-index header to the state it
41710 ** was in before the client began writing to the database.
41711 */
41712 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
41713
41714 for(iFrame=pWal->hdr.mxFrame+1;
41715 ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
41716 iFrame++
41717 ){
41718 /* This call cannot fail. Unless the page for which the page number
41719 ** is passed as the second argument is (a) in the cache and
41720 ** (b) has an outstanding reference, then xUndo is either a no-op
41721 ** (if (a) is false) or simply expels the page from the cache (if (b)
41722 ** is false).
41723 **
41724 ** If the upper layer is doing a rollback, it is guaranteed that there
41725 ** are no outstanding references to any page other than page 1. And
41726 ** page 1 is never written to the log until the transaction is
41727 ** committed. As a result, the call to xUndo may not fail.
41728 */
41729 assert( walFramePgno(pWal, iFrame)!=1 );
41730 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
41731 }
41732 walCleanupHash(pWal);
41733 }
41734 assert( rc==SQLITE_OK );
41735 return rc;
41736 }
41737
41738 /*
41739 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
41740 ** values. This function populates the array with values required to
41741 ** "rollback" the write position of the WAL handle back to the current
41742 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
41743 */
41744 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
41745 assert( pWal->writeLock );
41746 aWalData[0] = pWal->hdr.mxFrame;
41747 aWalData[1] = pWal->hdr.aFrameCksum[0];
41748 aWalData[2] = pWal->hdr.aFrameCksum[1];
41749 aWalData[3] = pWal->nCkpt;
41750 }
41751
41752 /*
41753 ** Move the write position of the WAL back to the point identified by
41754 ** the values in the aWalData[] array. aWalData must point to an array
41755 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
41756 ** by a call to WalSavepoint().
41757 */
41758 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
41759 int rc = SQLITE_OK;
41760
41761 assert( pWal->writeLock );
41762 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
41763
41764 if( aWalData[3]!=pWal->nCkpt ){
41765 /* This savepoint was opened immediately after the write-transaction
41766 ** was started. Right after that, the writer decided to wrap around
41767 ** to the start of the log. Update the savepoint values to match.
41768 */
41769 aWalData[0] = 0;
41770 aWalData[3] = pWal->nCkpt;
41771 }
41772
41773 if( aWalData[0]<pWal->hdr.mxFrame ){
41774 pWal->hdr.mxFrame = aWalData[0];
41775 pWal->hdr.aFrameCksum[0] = aWalData[1];
41776 pWal->hdr.aFrameCksum[1] = aWalData[2];
41777 walCleanupHash(pWal);
41778 }
41779
41780 return rc;
41781 }
41782
41783 /*
41784 ** This function is called just before writing a set of frames to the log
41785 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
41786 ** to the current log file, it is possible to overwrite the start of the
41787 ** existing log file with the new frames (i.e. "reset" the log). If so,
41788 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
41789 ** unchanged.
41790 **
41791 ** SQLITE_OK is returned if no error is encountered (regardless of whether
41792 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
41793 ** if some error
41794 */
41795 static int walRestartLog(Wal *pWal){
41796 int rc = SQLITE_OK;
41797 int cnt;
41798
41799 if( pWal->readLock==0 ){
41800 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
41801 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
41802 if( pInfo->nBackfill>0 ){
41803 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
41804 if( rc==SQLITE_OK ){
41805 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
41806 ** readers are currently using the WAL), then the transactions
41807 ** frames will overwrite the start of the existing log. Update the
41808 ** wal-index header to reflect this.
41809 **
41810 ** In theory it would be Ok to update the cache of the header only
41811 ** at this point. But updating the actual wal-index header is also
41812 ** safe and means there is no special case for sqlite3WalUndo()
41813 ** to handle if this transaction is rolled back.
41814 */
41815 int i; /* Loop counter */
41816 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
41817 pWal->nCkpt++;
41818 pWal->hdr.mxFrame = 0;
41819 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
41820 sqlite3_randomness(4, &aSalt[1]);
41821 walIndexWriteHdr(pWal);
41822 pInfo->nBackfill = 0;
41823 for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
41824 assert( pInfo->aReadMark[0]==0 );
41825 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
41826 }
41827 }
41828 walUnlockShared(pWal, WAL_READ_LOCK(0));
41829 pWal->readLock = -1;
41830 cnt = 0;
41831 do{
41832 int notUsed;
41833 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
41834 }while( rc==WAL_RETRY );
41835 }
41836 return rc;
41837 }
41838
41839 /*
41840 ** Write a set of frames to the log. The caller must hold the write-lock
41841 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
41842 */
41843 SQLITE_PRIVATE int sqlite3WalFrames(
41844 Wal *pWal, /* Wal handle to write to */
41845 int szPage, /* Database page-size in bytes */
41846 PgHdr *pList, /* List of dirty pages to write */
41847 Pgno nTruncate, /* Database size after this commit */
41848 int isCommit, /* True if this is a commit */
41849 int sync_flags /* Flags to pass to OsSync() (or 0) */
41850 ){
41851 int rc; /* Used to catch return codes */
41852 u32 iFrame; /* Next frame address */
41853 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
41854 PgHdr *p; /* Iterator to run through pList with. */
41855 PgHdr *pLast = 0; /* Last frame in list */
41856 int nLast = 0; /* Number of extra copies of last page */
41857
41858 assert( pList );
41859 assert( pWal->writeLock );
41860
41861 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
41862 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
41863 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
41864 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
41865 }
41866 #endif
41867
41868 /* See if it is possible to write these frames into the start of the
41869 ** log file, instead of appending to it at pWal->hdr.mxFrame.
41870 */
41871 if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
41872 return rc;
41873 }
41874
41875 /* If this is the first frame written into the log, write the WAL
41876 ** header to the start of the WAL file. See comments at the top of
41877 ** this source file for a description of the WAL header format.
41878 */
41879 iFrame = pWal->hdr.mxFrame;
41880 if( iFrame==0 ){
41881 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
41882 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41883 sqlite3Put4byte(&aWalHdr[4], 3007000);
41884 sqlite3Put4byte(&aWalHdr[8], szPage);
41885 pWal->szPage = szPage;
41886 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
41887 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
41888 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
41889 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
41890 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
41891 if( rc!=SQLITE_OK ){
41892 return rc;
41893 }
41894 walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
41895 }
41896 assert( pWal->szPage==szPage );
41897
41898 /* Write the log file. */
41899 for(p=pList; p; p=p->pDirty){
41900 u32 nDbsize; /* Db-size field for frame header */
41901 i64 iOffset; /* Write offset in log file */
41902
41903 iOffset = walFrameOffset(++iFrame, szPage);
41904
41905 /* Populate and write the frame header */
41906 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
41907 walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame);
41908 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41909 if( rc!=SQLITE_OK ){
41910 return rc;
41911 }
41912
41913 /* Write the page data */
41914 rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame));
41915 if( rc!=SQLITE_OK ){
41916 return rc;
41917 }
41918 pLast = p;
41919 }
41920
41921 /* Sync the log file if the 'isSync' flag was specified. */
41922 if( sync_flags ){
41923 i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
41924 i64 iOffset = walFrameOffset(iFrame+1, szPage);
41925
41926 assert( isCommit );
41927 assert( iSegment>0 );
41928
41929 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
41930 while( iOffset<iSegment ){
41931 walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame);
41932 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41933 if( rc!=SQLITE_OK ){
41934 return rc;
41935 }
41936
41937 iOffset += WAL_FRAME_HDRSIZE;
41938 rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset);
41939 if( rc!=SQLITE_OK ){
41940 return rc;
41941 }
41942 nLast++;
41943 iOffset += szPage;
41944 }
41945
41946 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
41947 }
41948
41949 /* Append data to the wal-index. It is not necessary to lock the
41950 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
41951 ** guarantees that there are no other writers, and no data that may
41952 ** be in use by existing readers is being overwritten.
41953 */
41954 iFrame = pWal->hdr.mxFrame;
41955 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
41956 iFrame++;
41957 rc = walIndexAppend(pWal, iFrame, p->pgno);
41958 }
41959 while( nLast>0 && rc==SQLITE_OK ){
41960 iFrame++;
41961 nLast--;
41962 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
41963 }
41964
41965 if( rc==SQLITE_OK ){
41966 /* Update the private copy of the header. */
41967 pWal->hdr.szPage = szPage;
41968 pWal->hdr.mxFrame = iFrame;
41969 if( isCommit ){
41970 pWal->hdr.iChange++;
41971 pWal->hdr.nPage = nTruncate;
41972 }
41973 /* If this is a commit, update the wal-index header too. */
41974 if( isCommit ){
41975 walIndexWriteHdr(pWal);
41976 pWal->iCallback = iFrame;
41977 }
41978 }
41979
41980 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
41981 return rc;
41982 }
41983
41984 /*
41985 ** This routine is called to implement sqlite3_wal_checkpoint() and
41986 ** related interfaces.
41987 **
41988 ** Obtain a CHECKPOINT lock and then backfill as much information as
41989 ** we can from WAL into the database.
41990 */
41991 SQLITE_PRIVATE int sqlite3WalCheckpoint(
41992 Wal *pWal, /* Wal connection */
41993 int sync_flags, /* Flags to sync db file with (or 0) */
41994 int nBuf, /* Size of temporary buffer */
41995 u8 *zBuf /* Temporary buffer to use */
41996 ){
41997 int rc; /* Return code */
41998 int isChanged = 0; /* True if a new wal-index header is loaded */
41999
42000 assert( pWal->ckptLock==0 );
42001
42002 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
42003 rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
42004 if( rc ){
42005 /* Usually this is SQLITE_BUSY meaning that another thread or process
42006 ** is already running a checkpoint, or maybe a recovery. But it might
42007 ** also be SQLITE_IOERR. */
42008 return rc;
42009 }
42010 pWal->ckptLock = 1;
42011
42012 /* Copy data from the log to the database file. */
42013 rc = walIndexReadHdr(pWal, &isChanged);
42014 if( rc==SQLITE_OK ){
42015 rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf);
42016 }
42017 if( isChanged ){
42018 /* If a new wal-index header was loaded before the checkpoint was
42019 ** performed, then the pager-cache associated with pWal is now
42020 ** out of date. So zero the cached wal-index header to ensure that
42021 ** next time the pager opens a snapshot on this database it knows that
42022 ** the cache needs to be reset.
42023 */
42024 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
42025 }
42026
42027 /* Release the locks. */
42028 walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
42029 pWal->ckptLock = 0;
42030 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
42031 return rc;
42032 }
42033
42034 /* Return the value to pass to a sqlite3_wal_hook callback, the
42035 ** number of frames in the WAL at the point of the last commit since
42036 ** sqlite3WalCallback() was called. If no commits have occurred since
42037 ** the last call, then return 0.
42038 */
42039 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
42040 u32 ret = 0;
42041 if( pWal ){
42042 ret = pWal->iCallback;
42043 pWal->iCallback = 0;
42044 }
42045 return (int)ret;
42046 }
42047
42048 /*
42049 ** This function is called to change the WAL subsystem into or out
42050 ** of locking_mode=EXCLUSIVE.
42051 **
42052 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
42053 ** into locking_mode=NORMAL. This means that we must acquire a lock
42054 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
42055 ** or if the acquisition of the lock fails, then return 0. If the
42056 ** transition out of exclusive-mode is successful, return 1. This
42057 ** operation must occur while the pager is still holding the exclusive
42058 ** lock on the main database file.
42059 **
42060 ** If op is one, then change from locking_mode=NORMAL into
42061 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
42062 ** be released. Return 1 if the transition is made and 0 if the
42063 ** WAL is already in exclusive-locking mode - meaning that this
42064 ** routine is a no-op. The pager must already hold the exclusive lock
42065 ** on the main database file before invoking this operation.
42066 **
42067 ** If op is negative, then do a dry-run of the op==1 case but do
42068 ** not actually change anything. The pager uses this to see if it
42069 ** should acquire the database exclusive lock prior to invoking
42070 ** the op==1 case.
42071 */
42072 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
42073 int rc;
42074 assert( pWal->writeLock==0 );
42075
42076 /* pWal->readLock is usually set, but might be -1 if there was a
42077 ** prior error while attempting to acquire are read-lock. This cannot
42078 ** happen if the connection is actually in exclusive mode (as no xShmLock
42079 ** locks are taken in this case). Nor should the pager attempt to
42080 ** upgrade to exclusive-mode following such an error.
42081 */
42082 assert( pWal->readLock>=0 || pWal->lockError );
42083 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
42084
42085 if( op==0 ){
42086 if( pWal->exclusiveMode ){
42087 pWal->exclusiveMode = 0;
42088 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
42089 pWal->exclusiveMode = 1;
42090 }
42091 rc = pWal->exclusiveMode==0;
42092 }else{
42093 /* Already in locking_mode=NORMAL */
42094 rc = 0;
42095 }
42096 }else if( op>0 ){
42097 assert( pWal->exclusiveMode==0 );
42098 assert( pWal->readLock>=0 );
42099 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
42100 pWal->exclusiveMode = 1;
42101 rc = 1;
42102 }else{
42103 rc = pWal->exclusiveMode==0;
42104 }
42105 return rc;
42106 }
42107
42108 #endif /* #ifndef SQLITE_OMIT_WAL */
42109
42110 /************** End of wal.c *************************************************/
42111 /************** Begin file btmutex.c *****************************************/
42112 /*
42113 ** 2007 August 27
42114 **
42115 ** The author disclaims copyright to this source code. In place of
@@ -38278,10 +42547,11 @@
42547 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
42548 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
42549 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
42550 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
42551 u8 inTransaction; /* Transaction state */
42552 u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
42553 int nTransaction; /* Number of open transactions (read + write) */
42554 u32 nPage; /* Number of pages in the database */
42555 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
42556 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
42557 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
@@ -41094,11 +45364,11 @@
45364 */
45365 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
45366 if( (rc = sqlite3PagerPagecount(pBt->pPager, &nPageFile))!=SQLITE_OK ){;
45367 goto page1_init_failed;
45368 }
45369 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
45370 nPage = nPageFile;
45371 }
45372 if( nPage>0 ){
45373 int pageSize;
45374 int usableSize;
@@ -41105,16 +45375,46 @@
45375 u8 *page1 = pPage1->aData;
45376 rc = SQLITE_NOTADB;
45377 if( memcmp(page1, zMagicHeader, 16)!=0 ){
45378 goto page1_init_failed;
45379 }
45380
45381 #ifdef SQLITE_OMIT_WAL
45382 if( page1[18]>1 ){
45383 pBt->readOnly = 1;
45384 }
45385 if( page1[19]>1 ){
45386 goto page1_init_failed;
45387 }
45388 #else
45389 if( page1[18]>2 ){
45390 pBt->readOnly = 1;
45391 }
45392 if( page1[19]>2 ){
45393 goto page1_init_failed;
45394 }
45395
45396 /* If the write version is set to 2, this database should be accessed
45397 ** in WAL mode. If the log is not already open, open it now. Then
45398 ** return SQLITE_OK and return without populating BtShared.pPage1.
45399 ** The caller detects this and calls this function again. This is
45400 ** required as the version of page 1 currently in the page1 buffer
45401 ** may not be the latest version - there may be a newer one in the log
45402 ** file.
45403 */
45404 if( page1[19]==2 && pBt->doNotUseWAL==0 ){
45405 int isOpen = 0;
45406 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
45407 if( rc!=SQLITE_OK ){
45408 goto page1_init_failed;
45409 }else if( isOpen==0 ){
45410 releasePage(pPage1);
45411 return SQLITE_OK;
45412 }
45413 rc = SQLITE_NOTADB;
45414 }
45415 #endif
45416
45417 /* The maximum embedded fraction must be exactly 25%. And the minimum
45418 ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
45419 ** The original design allowed these amounts to vary, but as of
45420 ** version 3.6.0, we require them to be fixed.
@@ -41360,11 +45660,11 @@
45660 }
45661
45662 if( rc!=SQLITE_OK ){
45663 unlockBtreeIfUnused(pBt);
45664 }
45665 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
45666 btreeInvokeBusyHandler(pBt) );
45667
45668 if( rc==SQLITE_OK ){
45669 if( p->inTrans==TRANS_NONE ){
45670 pBt->nTransaction++;
@@ -46807,10 +51107,46 @@
51107 assert(!pCur->isIncrblobHandle);
51108 assert(!pCur->aOverflow);
51109 pCur->isIncrblobHandle = 1;
51110 }
51111 #endif
51112
51113 /*
51114 ** Set both the "read version" (single byte at byte offset 18) and
51115 ** "write version" (single byte at byte offset 19) fields in the database
51116 ** header to iVersion.
51117 */
51118 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
51119 BtShared *pBt = pBtree->pBt;
51120 int rc; /* Return code */
51121
51122 assert( pBtree->inTrans==TRANS_NONE );
51123 assert( iVersion==1 || iVersion==2 );
51124
51125 /* If setting the version fields to 1, do not automatically open the
51126 ** WAL connection, even if the version fields are currently set to 2.
51127 */
51128 pBt->doNotUseWAL = (iVersion==1);
51129
51130 rc = sqlite3BtreeBeginTrans(pBtree, 0);
51131 if( rc==SQLITE_OK ){
51132 u8 *aData = pBt->pPage1->aData;
51133 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
51134 rc = sqlite3BtreeBeginTrans(pBtree, 2);
51135 if( rc==SQLITE_OK ){
51136 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51137 if( rc==SQLITE_OK ){
51138 aData[18] = (u8)iVersion;
51139 aData[19] = (u8)iVersion;
51140 }
51141 }
51142 }
51143 }
51144
51145 pBt->doNotUseWAL = 0;
51146 return rc;
51147 }
51148
51149 /************** End of btree.c ***********************************************/
51150 /************** Begin file backup.c ******************************************/
51151 /*
51152 ** 2009 January 28
@@ -47027,11 +51363,11 @@
51363 assert( zSrcData );
51364
51365 /* Catch the case where the destination is an in-memory database and the
51366 ** page sizes of the source and destination differ.
51367 */
51368 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
51369 rc = SQLITE_READONLY;
51370 }
51371
51372 /* This loop runs once for each destination page spanned by the source
51373 ** page. For each iteration, variable iOff is set to the byte offset
@@ -47097,10 +51433,13 @@
51433 /*
51434 ** Copy nPage pages from the source b-tree to the destination.
51435 */
51436 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
51437 int rc;
51438 int destMode; /* Destination journal mode */
51439 int pgszSrc = 0; /* Source page size */
51440 int pgszDest = 0; /* Destination page size */
51441
51442 sqlite3_mutex_enter(p->pSrcDb->mutex);
51443 sqlite3BtreeEnter(p->pSrc);
51444 if( p->pDestDb ){
51445 sqlite3_mutex_enter(p->pDestDb->mutex);
@@ -47137,10 +51476,19 @@
51476 */
51477 if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
51478 rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
51479 bCloseTrans = 1;
51480 }
51481
51482 /* Do not allow backup if the destination database is in WAL mode
51483 ** and the page sizes are different between source and destination */
51484 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
51485 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
51486 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
51487 if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
51488 rc = SQLITE_READONLY;
51489 }
51490
51491 /* Now that there is a read-lock on the source database, query the
51492 ** source pager for the number of pages in the database.
51493 */
51494 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
@@ -47173,12 +51521,10 @@
51521 ** same schema version.
51522 */
51523 if( rc==SQLITE_DONE
51524 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
51525 ){
 
 
51526 int nDestTruncate;
51527
51528 if( p->pDestDb ){
51529 sqlite3ResetInternalSchema(p->pDestDb, 0);
51530 }
@@ -47193,56 +51539,58 @@
51539 ** sqlite3PagerTruncateImage() here so that any pages in the
51540 ** destination file that lie beyond the nDestTruncate page mark are
51541 ** journalled by PagerCommitPhaseOne() before they are destroyed
51542 ** by the file truncation.
51543 */
51544 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
51545 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
51546 if( pgszSrc<pgszDest ){
51547 int ratio = pgszDest/pgszSrc;
51548 nDestTruncate = (nSrcPage+ratio-1)/ratio;
51549 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
51550 nDestTruncate--;
51551 }
51552 }else{
51553 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
51554 }
51555 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
51556
51557 if( pgszSrc<pgszDest ){
51558 /* If the source page-size is smaller than the destination page-size,
51559 ** two extra things may need to happen:
51560 **
51561 ** * The destination may need to be truncated, and
51562 **
51563 ** * Data stored on the pages immediately following the
51564 ** pending-byte page in the source database may need to be
51565 ** copied into the destination database.
51566 */
51567 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
51568 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
51569
51570 assert( pFile );
51571 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
51572 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
51573 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
51574 ));
51575 if( SQLITE_OK==(rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1))
51576 && SQLITE_OK==(rc = backupTruncateFile(pFile, iSize))
51577 && SQLITE_OK==(rc = sqlite3PagerSync(pDestPager))
51578 ){
51579 i64 iOff;
51580 i64 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
51581 for(
51582 iOff=PENDING_BYTE+pgszSrc;
51583 rc==SQLITE_OK && iOff<iEnd;
51584 iOff+=pgszSrc
51585 ){
51586 PgHdr *pSrcPg = 0;
51587 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
51588 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
51589 if( rc==SQLITE_OK ){
51590 u8 *zData = sqlite3PagerGetData(pSrcPg);
51591 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
51592 }
51593 sqlite3PagerUnref(pSrcPg);
51594 }
51595 }
51596 }else{
@@ -49366,13 +53714,16 @@
53714 ** Return the opcode for a given address. If the address is -1, then
53715 ** return the most recently inserted opcode.
53716 **
53717 ** If a memory allocation error has occurred prior to the calling of this
53718 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
53719 ** is readable but not writable, though it is cast to a writable value.
53720 ** The return of a dummy opcode allows the call to continue functioning
53721 ** after a OOM fault without having to check to see if the return from
53722 ** this routine is a valid pointer. But because the dummy.opcode is 0,
53723 ** dummy will never be written to. This is verified by code inspection and
53724 ** by running with Valgrind.
53725 **
53726 ** About the #ifdef SQLITE_OMIT_TRACE: Normally, this routine is never called
53727 ** unless p->nOp>0. This is because in the absense of SQLITE_OMIT_TRACE,
53728 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
53729 ** a new VDBE is created. So we are free to set addr to p->nOp-1 without
@@ -49379,21 +53730,21 @@
53730 ** having to double-check to make sure that the result is non-negative. But
53731 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
53732 ** check the value of p->nOp-1 before continuing.
53733 */
53734 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
53735 static const VdbeOp dummy;
53736 assert( p->magic==VDBE_MAGIC_INIT );
53737 if( addr<0 ){
53738 #ifdef SQLITE_OMIT_TRACE
53739 if( p->nOp==0 ) return (VdbeOp*)&dummy;
53740 #endif
53741 addr = p->nOp - 1;
53742 }
53743 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
53744 if( p->db->mallocFailed ){
53745 return (VdbeOp*)&dummy;
53746 }else{
53747 return &p->aOp[addr];
53748 }
53749 }
53750
@@ -49502,10 +53853,15 @@
53853 }
53854 #endif
53855
53856 /*
53857 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
53858 **
53859 ** The prepared statement has to know in advance which Btree objects
53860 ** will be used so that it can acquire mutexes on them all in sorted
53861 ** order (via sqlite3VdbeMutexArrayEnter(). Mutexes are acquired
53862 ** in order (and released in reverse order) to avoid deadlocks.
53863 */
53864 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
53865 int mask;
53866 assert( i>=0 && i<p->db->nDb && i<sizeof(u32)*8 );
53867 assert( i<(int)sizeof(p->btreeMask)*8 );
@@ -50001,10 +54357,11 @@
54357 p->magic = VDBE_MAGIC_RUN;
54358 p->nChange = 0;
54359 p->cacheCtr = 1;
54360 p->minWriteFileFormat = 255;
54361 p->iStatement = 0;
54362 p->nFkConstraint = 0;
54363 #ifdef VDBE_PROFILE
54364 {
54365 int i;
54366 for(i=0; i<p->nOp; i++){
54367 p->aOp[i].cnt = 0;
@@ -50689,14 +55046,21 @@
55046 ** must be SAVEPOINT_RELEASE. Hence the NEVER(p->rc==SQLITE_OK) in
55047 ** the following code.
55048 */
55049 if( eStatementOp ){
55050 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
55051 if( rc ){
55052 assert( eStatementOp==SAVEPOINT_ROLLBACK );
55053 if( NEVER(p->rc==SQLITE_OK) || p->rc==SQLITE_CONSTRAINT ){
55054 p->rc = rc;
55055 sqlite3DbFree(db, p->zErrMsg);
55056 p->zErrMsg = 0;
55057 }
55058 invalidateCursorsOnModifiedBtrees(db);
55059 sqlite3RollbackAll(db);
55060 sqlite3CloseSavepoints(db);
55061 db->autoCommit = 1;
55062 }
55063 }
55064
55065 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
55066 ** has been rolled back, update the database connection change-counter.
@@ -51976,10 +56340,31 @@
56340 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
56341 sqlite3VdbeMemSetNull(&pCtx->s);
56342 pCtx->isError = SQLITE_NOMEM;
56343 pCtx->s.db->mallocFailed = 1;
56344 }
56345
56346 /*
56347 ** This function is called after a transaction has been committed. It
56348 ** invokes callbacks registered with sqlite3_wal_hook() as required.
56349 */
56350 static int doWalCallbacks(sqlite3 *db){
56351 int rc = SQLITE_OK;
56352 #ifndef SQLITE_OMIT_WAL
56353 int i;
56354 for(i=0; i<db->nDb; i++){
56355 Btree *pBt = db->aDb[i].pBt;
56356 if( pBt ){
56357 int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
56358 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
56359 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
56360 }
56361 }
56362 }
56363 #endif
56364 return rc;
56365 }
56366
56367 /*
56368 ** Execute the statement pStmt, either until a row of data is ready, the
56369 ** statement is completely executed or an error occurs.
56370 **
@@ -51992,13 +56377,16 @@
56377 sqlite3 *db;
56378 int rc;
56379
56380 assert(p);
56381 if( p->magic!=VDBE_MAGIC_RUN ){
56382 /* We used to require that sqlite3_reset() be called before retrying
56383 ** sqlite3_step() after any error. But after 3.6.23, we changed this
56384 ** so that sqlite3_reset() would be called automatically instead of
56385 ** throwing the error.
56386 */
56387 sqlite3_reset((sqlite3_stmt*)p);
56388 }
56389
56390 /* Check that malloc() has not failed. If it has, return early. */
56391 db = p->db;
56392 if( db->mallocFailed ){
@@ -52022,13 +56410,11 @@
56410
56411 assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
56412
56413 #ifndef SQLITE_OMIT_TRACE
56414 if( db->xProfile && !db->init.busy ){
56415 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 
 
56416 }
56417 #endif
56418
56419 db->activeVdbeCnt++;
56420 if( p->readOnly==0 ) db->writeVdbeCnt++;
@@ -52045,19 +56431,23 @@
56431
56432 #ifndef SQLITE_OMIT_TRACE
56433 /* Invoke the profile callback if there is one
56434 */
56435 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
56436 sqlite3_int64 iNow;
56437 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
56438 db->xProfile(db->pProfileArg, p->zSql, iNow - p->startTime);
 
 
 
 
56439 }
56440 #endif
56441
56442 if( rc==SQLITE_DONE ){
56443 assert( p->rc==SQLITE_OK );
56444 p->rc = doWalCallbacks(db);
56445 if( p->rc!=SQLITE_OK ){
56446 rc = SQLITE_ERROR;
56447 }
56448 }
56449
56450 db->errCode = rc;
56451 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
56452 p->rc = SQLITE_NOMEM;
56453 }
@@ -53638,26 +58028,10 @@
58028 ** flag on jump instructions, we get a (small) speed improvement.
58029 */
58030 #define CHECK_FOR_INTERRUPT \
58031 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
58032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58033
58034 #ifndef NDEBUG
58035 /*
58036 ** This function is only called from within an assert() expression. It
58037 ** checks that the sqlite3.nTransaction variable is correctly set to
@@ -53744,13 +58118,10 @@
58118 union vdbeExecUnion {
58119 struct OP_Yield_stack_vars {
58120 int pcDest;
58121 } aa;
58122 struct OP_Variable_stack_vars {
 
 
 
58123 Mem *pVar; /* Value being transferred */
58124 } ab;
58125 struct OP_Move_stack_vars {
58126 char *zMalloc; /* Holding variable for allocated memory */
58127 int n; /* Number of registers left to copy */
@@ -54071,22 +58442,29 @@
58442 sqlite3_value **apVal;
58443 } cb;
58444 struct OP_AggFinal_stack_vars {
58445 Mem *pMem;
58446 } cc;
58447 struct OP_JournalMode_stack_vars {
58448 Btree *pBt; /* Btree to change journal mode of */
58449 Pager *pPager; /* Pager associated with pBt */
58450 int eNew; /* New journal mode */
58451 int eOld; /* The old journal mode */
58452 const char *zFilename; /* Name of database file for pPager */
58453 } cd;
58454 struct OP_IncrVacuum_stack_vars {
58455 Btree *pBt;
58456 } ce;
58457 struct OP_VBegin_stack_vars {
58458 VTable *pVTab;
58459 } cf;
58460 struct OP_VOpen_stack_vars {
58461 VdbeCursor *pCur;
58462 sqlite3_vtab_cursor *pVtabCursor;
58463 sqlite3_vtab *pVtab;
58464 sqlite3_module *pModule;
58465 } cg;
58466 struct OP_VFilter_stack_vars {
58467 int nArg;
58468 int iQuery;
58469 const sqlite3_module *pModule;
58470 Mem *pQuery;
@@ -54095,39 +58473,39 @@
58473 sqlite3_vtab *pVtab;
58474 VdbeCursor *pCur;
58475 int res;
58476 int i;
58477 Mem **apArg;
58478 } ch;
58479 struct OP_VColumn_stack_vars {
58480 sqlite3_vtab *pVtab;
58481 const sqlite3_module *pModule;
58482 Mem *pDest;
58483 sqlite3_context sContext;
58484 } ci;
58485 struct OP_VNext_stack_vars {
58486 sqlite3_vtab *pVtab;
58487 const sqlite3_module *pModule;
58488 int res;
58489 VdbeCursor *pCur;
58490 } cj;
58491 struct OP_VRename_stack_vars {
58492 sqlite3_vtab *pVtab;
58493 Mem *pName;
58494 } ck;
58495 struct OP_VUpdate_stack_vars {
58496 sqlite3_vtab *pVtab;
58497 sqlite3_module *pModule;
58498 int nArg;
58499 int i;
58500 sqlite_int64 rowid;
58501 Mem **apArg;
58502 Mem *pX;
58503 } cl;
58504 struct OP_Trace_stack_vars {
58505 char *zTrace;
58506 } cm;
58507 } u;
58508 /* End automatically generated code
58509 ********************************************************************/
58510
58511 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -54147,23 +58525,18 @@
58525 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
58526 checkProgress = db->xProgress!=0;
58527 #endif
58528 #ifdef SQLITE_DEBUG
58529 sqlite3BeginBenignMalloc();
58530 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
 
 
58531 int i;
58532 printf("VDBE Program Listing:\n");
58533 sqlite3VdbePrintSql(p);
58534 for(i=0; i<p->nOp; i++){
58535 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
58536 }
58537 }
 
 
 
58538 sqlite3EndBenignMalloc();
58539 #endif
58540 for(pc=p->pc; rc==SQLITE_OK; pc++){
58541 assert( pc>=0 && pc<p->nOp );
58542 if( db->mallocFailed ) goto no_mem;
@@ -54181,17 +58554,10 @@
58554 printf("VDBE Execution Trace:\n");
58555 sqlite3VdbePrintSql(p);
58556 }
58557 sqlite3VdbePrintOp(p->trace, pc, pOp);
58558 }
 
 
 
 
 
 
 
58559 #endif
58560
58561
58562 /* Check to see if we need to simulate an interrupt. This only happens
58563 ** if we have a special test build.
@@ -54544,44 +58910,29 @@
58910 pOut->enc = encoding;
58911 UPDATE_MAX_BLOBSIZE(pOut);
58912 break;
58913 }
58914
58915 /* Opcode: Variable P1 P2 * P4 *
58916 **
58917 ** Transfer the values of bound parameter P1 into register P2
 
58918 **
58919 ** If the parameter is named, then its name appears in P4 and P3==1.
58920 ** The P4 value is used by sqlite3_bind_parameter_name().
58921 */
58922 case OP_Variable: { /* out2-prerelease */
58923 #if 0 /* local variables moved into u.ab */
 
 
 
58924 Mem *pVar; /* Value being transferred */
58925 #endif /* local variables moved into u.ab */
58926
58927 assert( pOp->p1>0 && pOp->p1<=p->nVar );
58928 u.ab.pVar = &p->aVar[pOp->p1 - 1];
58929 if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
58930 goto too_big;
58931 }
58932 sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
58933 UPDATE_MAX_BLOBSIZE(pOut);
 
 
 
 
 
 
 
 
 
 
 
58934 break;
58935 }
58936
58937 /* Opcode: Move P1 P2 P3 * *
58938 **
@@ -58849,10 +63200,151 @@
63200 goto too_big;
63201 }
63202 break;
63203 }
63204
63205 #ifndef SQLITE_OMIT_WAL
63206 /* Opcode: Checkpoint P1 * * * *
63207 **
63208 ** Checkpoint database P1. This is a no-op if P1 is not currently in
63209 ** WAL mode.
63210 */
63211 case OP_Checkpoint: {
63212 rc = sqlite3Checkpoint(db, pOp->p1);
63213 break;
63214 };
63215 #endif
63216
63217 /* Opcode: JournalMode P1 P2 P3 * P5
63218 **
63219 ** Change the journal mode of database P1 to P3. P3 must be one of the
63220 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
63221 ** modes (delete, truncate, persist, off and memory), this is a simple
63222 ** operation. No IO is required.
63223 **
63224 ** If changing into or out of WAL mode the procedure is more complicated.
63225 **
63226 ** Write a string containing the final journal-mode to register P2.
63227 **
63228 ** If an attempt to change in to or out of WAL mode fails because another
63229 ** connection also has the same database open, then an SQLITE_BUSY error
63230 ** is raised if P5==0, or of P5!=0 the journal mode changed is skipped
63231 ** without signaling the error.
63232 */
63233 case OP_JournalMode: { /* out2-prerelease */
63234 #if 0 /* local variables moved into u.cd */
63235 Btree *pBt; /* Btree to change journal mode of */
63236 Pager *pPager; /* Pager associated with pBt */
63237 int eNew; /* New journal mode */
63238 int eOld; /* The old journal mode */
63239 const char *zFilename; /* Name of database file for pPager */
63240 #endif /* local variables moved into u.cd */
63241
63242 u.cd.eNew = pOp->p3;
63243 assert( u.cd.eNew==PAGER_JOURNALMODE_DELETE
63244 || u.cd.eNew==PAGER_JOURNALMODE_TRUNCATE
63245 || u.cd.eNew==PAGER_JOURNALMODE_PERSIST
63246 || u.cd.eNew==PAGER_JOURNALMODE_OFF
63247 || u.cd.eNew==PAGER_JOURNALMODE_MEMORY
63248 || u.cd.eNew==PAGER_JOURNALMODE_WAL
63249 || u.cd.eNew==PAGER_JOURNALMODE_QUERY
63250 );
63251 assert( pOp->p1>=0 && pOp->p1<db->nDb );
63252
63253 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
63254 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
63255 ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
63256 ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
63257 ** is not called when the statement is prepared because it requires the
63258 ** iDb index of the database as a parameter, and the database has not
63259 ** yet been attached so that index is unavailable. We have to wait
63260 ** until runtime (now) to get the mutex on the newly attached database.
63261 ** No other mutexes are required by the ATTACH command so this is safe
63262 ** to do.
63263 */
63264 assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
63265 if( p->aMutex.nMutex==0 ){
63266 /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
63267 ** database. */
63268 sqlite3VdbeUsesBtree(p, pOp->p1);
63269 sqlite3VdbeMutexArrayEnter(p);
63270 }
63271
63272 u.cd.pBt = db->aDb[pOp->p1].pBt;
63273 u.cd.pPager = sqlite3BtreePager(u.cd.pBt);
63274 u.cd.eOld = sqlite3PagerGetJournalMode(u.cd.pPager);
63275 if( u.cd.eNew==PAGER_JOURNALMODE_QUERY ) u.cd.eNew = u.cd.eOld;
63276 if( !sqlite3PagerOkToChangeJournalMode(u.cd.pPager) ) u.cd.eNew = u.cd.eOld;
63277
63278 #ifndef SQLITE_OMIT_WAL
63279 u.cd.zFilename = sqlite3PagerFilename(u.cd.pPager);
63280
63281 /* Do not allow a transition to journal_mode=WAL for a database
63282 ** in temporary storage or if the VFS does not support xShmOpen.
63283 */
63284 if( u.cd.eNew==PAGER_JOURNALMODE_WAL
63285 && (u.cd.zFilename[0]==0 /* Temp file */
63286 || !sqlite3PagerWalSupported(u.cd.pPager)) /* No xShmOpen support */
63287 ){
63288 u.cd.eNew = u.cd.eOld;
63289 }
63290
63291 if( (u.cd.eNew!=u.cd.eOld)
63292 && (u.cd.eOld==PAGER_JOURNALMODE_WAL || u.cd.eNew==PAGER_JOURNALMODE_WAL)
63293 ){
63294 if( !db->autoCommit || db->activeVdbeCnt>1 ){
63295 rc = SQLITE_ERROR;
63296 sqlite3SetString(&p->zErrMsg, db,
63297 "cannot change %s wal mode from within a transaction",
63298 (u.cd.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
63299 );
63300 break;
63301 }else{
63302
63303 if( u.cd.eOld==PAGER_JOURNALMODE_WAL ){
63304 /* If leaving WAL mode, close the log file. If successful, the call
63305 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
63306 ** file. An EXCLUSIVE lock may still be held on the database file
63307 ** after a successful return.
63308 */
63309 rc = sqlite3PagerCloseWal(u.cd.pPager);
63310 if( rc==SQLITE_OK ){
63311 sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63312 }else if( rc==SQLITE_BUSY && pOp->p5==0 ){
63313 goto abort_due_to_error;
63314 }
63315 }else{
63316 sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_DELETE);
63317 rc = SQLITE_OK;
63318 }
63319
63320 /* Open a transaction on the database file. Regardless of the journal
63321 ** mode, this transaction always uses a rollback journal.
63322 */
63323 assert( sqlite3BtreeIsInTrans(u.cd.pBt)==0 );
63324 if( rc==SQLITE_OK ){
63325 rc = sqlite3BtreeSetVersion(u.cd.pBt,
63326 (u.cd.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
63327 if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error;
63328 }
63329 if( rc==SQLITE_BUSY ){
63330 u.cd.eNew = u.cd.eOld;
63331 rc = SQLITE_OK;
63332 }
63333 }
63334 }
63335 #endif /* ifndef SQLITE_OMIT_WAL */
63336
63337 u.cd.eNew = sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63338 pOut = &aMem[pOp->p2];
63339 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
63340 pOut->z = (char *)sqlite3JournalModename(u.cd.eNew);
63341 pOut->n = sqlite3Strlen30(pOut->z);
63342 pOut->enc = SQLITE_UTF8;
63343 sqlite3VdbeChangeEncoding(pOut, encoding);
63344 break;
63345 };
63346
63347 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
63348 /* Opcode: Vacuum * * * * *
63349 **
63350 ** Vacuum the entire database. This opcode will cause other virtual
@@ -58871,18 +63363,18 @@
63363 ** Perform a single step of the incremental vacuum procedure on
63364 ** the P1 database. If the vacuum has finished, jump to instruction
63365 ** P2. Otherwise, fall through to the next instruction.
63366 */
63367 case OP_IncrVacuum: { /* jump */
63368 #if 0 /* local variables moved into u.ce */
63369 Btree *pBt;
63370 #endif /* local variables moved into u.ce */
63371
63372 assert( pOp->p1>=0 && pOp->p1<db->nDb );
63373 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
63374 u.ce.pBt = db->aDb[pOp->p1].pBt;
63375 rc = sqlite3BtreeIncrVacuum(u.ce.pBt);
63376 if( rc==SQLITE_DONE ){
63377 pc = pOp->p2 - 1;
63378 rc = SQLITE_OK;
63379 }
63380 break;
@@ -58948,19 +63440,19 @@
63440 ** Also, whether or not P4 is set, check that this is not being called from
63441 ** within a callback to a virtual table xSync() method. If it is, the error
63442 ** code will be set to SQLITE_LOCKED.
63443 */
63444 case OP_VBegin: {
63445 #if 0 /* local variables moved into u.cf */
63446 VTable *pVTab;
63447 #endif /* local variables moved into u.cf */
63448 u.cf.pVTab = pOp->p4.pVtab;
63449 rc = sqlite3VtabBegin(db, u.cf.pVTab);
63450 if( u.cf.pVTab ){
63451 sqlite3DbFree(db, p->zErrMsg);
63452 p->zErrMsg = u.cf.pVTab->pVtab->zErrMsg;
63453 u.cf.pVTab->pVtab->zErrMsg = 0;
63454 }
63455 break;
63456 }
63457 #endif /* SQLITE_OMIT_VIRTUALTABLE */
63458
@@ -58996,38 +63488,38 @@
63488 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
63489 ** P1 is a cursor number. This opcode opens a cursor to the virtual
63490 ** table and stores that cursor in P1.
63491 */
63492 case OP_VOpen: {
63493 #if 0 /* local variables moved into u.cg */
63494 VdbeCursor *pCur;
63495 sqlite3_vtab_cursor *pVtabCursor;
63496 sqlite3_vtab *pVtab;
63497 sqlite3_module *pModule;
63498 #endif /* local variables moved into u.cg */
63499
63500 u.cg.pCur = 0;
63501 u.cg.pVtabCursor = 0;
63502 u.cg.pVtab = pOp->p4.pVtab->pVtab;
63503 u.cg.pModule = (sqlite3_module *)u.cg.pVtab->pModule;
63504 assert(u.cg.pVtab && u.cg.pModule);
63505 rc = u.cg.pModule->xOpen(u.cg.pVtab, &u.cg.pVtabCursor);
63506 sqlite3DbFree(db, p->zErrMsg);
63507 p->zErrMsg = u.cg.pVtab->zErrMsg;
63508 u.cg.pVtab->zErrMsg = 0;
63509 if( SQLITE_OK==rc ){
63510 /* Initialize sqlite3_vtab_cursor base class */
63511 u.cg.pVtabCursor->pVtab = u.cg.pVtab;
63512
63513 /* Initialise vdbe cursor object */
63514 u.cg.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
63515 if( u.cg.pCur ){
63516 u.cg.pCur->pVtabCursor = u.cg.pVtabCursor;
63517 u.cg.pCur->pModule = u.cg.pVtabCursor->pVtab->pModule;
63518 }else{
63519 db->mallocFailed = 1;
63520 u.cg.pModule->xClose(u.cg.pVtabCursor);
63521 }
63522 }
63523 break;
63524 }
63525 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59050,11 +63542,11 @@
63542 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
63543 **
63544 ** A jump is made to P2 if the result set after filtering would be empty.
63545 */
63546 case OP_VFilter: { /* jump */
63547 #if 0 /* local variables moved into u.ch */
63548 int nArg;
63549 int iQuery;
63550 const sqlite3_module *pModule;
63551 Mem *pQuery;
63552 Mem *pArgc;
@@ -59062,50 +63554,50 @@
63554 sqlite3_vtab *pVtab;
63555 VdbeCursor *pCur;
63556 int res;
63557 int i;
63558 Mem **apArg;
63559 #endif /* local variables moved into u.ch */
63560
63561 u.ch.pQuery = &aMem[pOp->p3];
63562 u.ch.pArgc = &u.ch.pQuery[1];
63563 u.ch.pCur = p->apCsr[pOp->p1];
63564 REGISTER_TRACE(pOp->p3, u.ch.pQuery);
63565 assert( u.ch.pCur->pVtabCursor );
63566 u.ch.pVtabCursor = u.ch.pCur->pVtabCursor;
63567 u.ch.pVtab = u.ch.pVtabCursor->pVtab;
63568 u.ch.pModule = u.ch.pVtab->pModule;
63569
63570 /* Grab the index number and argc parameters */
63571 assert( (u.ch.pQuery->flags&MEM_Int)!=0 && u.ch.pArgc->flags==MEM_Int );
63572 u.ch.nArg = (int)u.ch.pArgc->u.i;
63573 u.ch.iQuery = (int)u.ch.pQuery->u.i;
63574
63575 /* Invoke the xFilter method */
63576 {
63577 u.ch.res = 0;
63578 u.ch.apArg = p->apArg;
63579 for(u.ch.i = 0; u.ch.i<u.ch.nArg; u.ch.i++){
63580 u.ch.apArg[u.ch.i] = &u.ch.pArgc[u.ch.i+1];
63581 sqlite3VdbeMemStoreType(u.ch.apArg[u.ch.i]);
63582 }
63583
63584 p->inVtabMethod = 1;
63585 rc = u.ch.pModule->xFilter(u.ch.pVtabCursor, u.ch.iQuery, pOp->p4.z, u.ch.nArg, u.ch.apArg);
63586 p->inVtabMethod = 0;
63587 sqlite3DbFree(db, p->zErrMsg);
63588 p->zErrMsg = u.ch.pVtab->zErrMsg;
63589 u.ch.pVtab->zErrMsg = 0;
63590 if( rc==SQLITE_OK ){
63591 u.ch.res = u.ch.pModule->xEof(u.ch.pVtabCursor);
63592 }
63593
63594 if( u.ch.res ){
63595 pc = pOp->p2 - 1;
63596 }
63597 }
63598 u.ch.pCur->nullRow = 0;
63599
63600 break;
63601 }
63602 #endif /* SQLITE_OMIT_VIRTUALTABLE */
63603
@@ -59115,56 +63607,56 @@
63607 ** Store the value of the P2-th column of
63608 ** the row of the virtual-table that the
63609 ** P1 cursor is pointing to into register P3.
63610 */
63611 case OP_VColumn: {
63612 #if 0 /* local variables moved into u.ci */
63613 sqlite3_vtab *pVtab;
63614 const sqlite3_module *pModule;
63615 Mem *pDest;
63616 sqlite3_context sContext;
63617 #endif /* local variables moved into u.ci */
63618
63619 VdbeCursor *pCur = p->apCsr[pOp->p1];
63620 assert( pCur->pVtabCursor );
63621 assert( pOp->p3>0 && pOp->p3<=p->nMem );
63622 u.ci.pDest = &aMem[pOp->p3];
63623 if( pCur->nullRow ){
63624 sqlite3VdbeMemSetNull(u.ci.pDest);
63625 break;
63626 }
63627 u.ci.pVtab = pCur->pVtabCursor->pVtab;
63628 u.ci.pModule = u.ci.pVtab->pModule;
63629 assert( u.ci.pModule->xColumn );
63630 memset(&u.ci.sContext, 0, sizeof(u.ci.sContext));
63631
63632 /* The output cell may already have a buffer allocated. Move
63633 ** the current contents to u.ci.sContext.s so in case the user-function
63634 ** can use the already allocated buffer instead of allocating a
63635 ** new one.
63636 */
63637 sqlite3VdbeMemMove(&u.ci.sContext.s, u.ci.pDest);
63638 MemSetTypeFlag(&u.ci.sContext.s, MEM_Null);
63639
63640 rc = u.ci.pModule->xColumn(pCur->pVtabCursor, &u.ci.sContext, pOp->p2);
63641 sqlite3DbFree(db, p->zErrMsg);
63642 p->zErrMsg = u.ci.pVtab->zErrMsg;
63643 u.ci.pVtab->zErrMsg = 0;
63644 if( u.ci.sContext.isError ){
63645 rc = u.ci.sContext.isError;
63646 }
63647
63648 /* Copy the result of the function to the P3 register. We
63649 ** do this regardless of whether or not an error occurred to ensure any
63650 ** dynamic allocation in u.ci.sContext.s (a Mem struct) is released.
63651 */
63652 sqlite3VdbeChangeEncoding(&u.ci.sContext.s, encoding);
63653 sqlite3VdbeMemMove(u.ci.pDest, &u.ci.sContext.s);
63654 REGISTER_TRACE(pOp->p3, u.ci.pDest);
63655 UPDATE_MAX_BLOBSIZE(u.ci.pDest);
63656
63657 if( sqlite3VdbeMemTooBig(u.ci.pDest) ){
63658 goto too_big;
63659 }
63660 break;
63661 }
63662 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -59175,44 +63667,44 @@
63667 ** Advance virtual table P1 to the next row in its result set and
63668 ** jump to instruction P2. Or, if the virtual table has reached
63669 ** the end of its result set, then fall through to the next instruction.
63670 */
63671 case OP_VNext: { /* jump */
63672 #if 0 /* local variables moved into u.cj */
63673 sqlite3_vtab *pVtab;
63674 const sqlite3_module *pModule;
63675 int res;
63676 VdbeCursor *pCur;
63677 #endif /* local variables moved into u.cj */
63678
63679 u.cj.res = 0;
63680 u.cj.pCur = p->apCsr[pOp->p1];
63681 assert( u.cj.pCur->pVtabCursor );
63682 if( u.cj.pCur->nullRow ){
63683 break;
63684 }
63685 u.cj.pVtab = u.cj.pCur->pVtabCursor->pVtab;
63686 u.cj.pModule = u.cj.pVtab->pModule;
63687 assert( u.cj.pModule->xNext );
63688
63689 /* Invoke the xNext() method of the module. There is no way for the
63690 ** underlying implementation to return an error if one occurs during
63691 ** xNext(). Instead, if an error occurs, true is returned (indicating that
63692 ** data is available) and the error code returned when xColumn or
63693 ** some other method is next invoked on the save virtual table cursor.
63694 */
63695 p->inVtabMethod = 1;
63696 rc = u.cj.pModule->xNext(u.cj.pCur->pVtabCursor);
63697 p->inVtabMethod = 0;
63698 sqlite3DbFree(db, p->zErrMsg);
63699 p->zErrMsg = u.cj.pVtab->zErrMsg;
63700 u.cj.pVtab->zErrMsg = 0;
63701 if( rc==SQLITE_OK ){
63702 u.cj.res = u.cj.pModule->xEof(u.cj.pCur->pVtabCursor);
63703 }
63704
63705 if( !u.cj.res ){
63706 /* If there is data, jump to P2 */
63707 pc = pOp->p2 - 1;
63708 }
63709 break;
63710 }
@@ -59224,24 +63716,24 @@
63716 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
63717 ** This opcode invokes the corresponding xRename method. The value
63718 ** in register P1 is passed as the zName argument to the xRename method.
63719 */
63720 case OP_VRename: {
63721 #if 0 /* local variables moved into u.ck */
63722 sqlite3_vtab *pVtab;
63723 Mem *pName;
63724 #endif /* local variables moved into u.ck */
63725
63726 u.ck.pVtab = pOp->p4.pVtab->pVtab;
63727 u.ck.pName = &aMem[pOp->p1];
63728 assert( u.ck.pVtab->pModule->xRename );
63729 REGISTER_TRACE(pOp->p1, u.ck.pName);
63730 assert( u.ck.pName->flags & MEM_Str );
63731 rc = u.ck.pVtab->pModule->xRename(u.ck.pVtab, u.ck.pName->z);
63732 sqlite3DbFree(db, p->zErrMsg);
63733 p->zErrMsg = u.ck.pVtab->zErrMsg;
63734 u.ck.pVtab->zErrMsg = 0;
63735
63736 break;
63737 }
63738 #endif
63739
@@ -59268,39 +63760,39 @@
63760 ** P1 is a boolean flag. If it is set to true and the xUpdate call
63761 ** is successful, then the value returned by sqlite3_last_insert_rowid()
63762 ** is set to the value of the rowid for the row just inserted.
63763 */
63764 case OP_VUpdate: {
63765 #if 0 /* local variables moved into u.cl */
63766 sqlite3_vtab *pVtab;
63767 sqlite3_module *pModule;
63768 int nArg;
63769 int i;
63770 sqlite_int64 rowid;
63771 Mem **apArg;
63772 Mem *pX;
63773 #endif /* local variables moved into u.cl */
63774
63775 u.cl.pVtab = pOp->p4.pVtab->pVtab;
63776 u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
63777 u.cl.nArg = pOp->p2;
63778 assert( pOp->p4type==P4_VTAB );
63779 if( ALWAYS(u.cl.pModule->xUpdate) ){
63780 u.cl.apArg = p->apArg;
63781 u.cl.pX = &aMem[pOp->p3];
63782 for(u.cl.i=0; u.cl.i<u.cl.nArg; u.cl.i++){
63783 sqlite3VdbeMemStoreType(u.cl.pX);
63784 u.cl.apArg[u.cl.i] = u.cl.pX;
63785 u.cl.pX++;
63786 }
63787 rc = u.cl.pModule->xUpdate(u.cl.pVtab, u.cl.nArg, u.cl.apArg, &u.cl.rowid);
63788 sqlite3DbFree(db, p->zErrMsg);
63789 p->zErrMsg = u.cl.pVtab->zErrMsg;
63790 u.cl.pVtab->zErrMsg = 0;
63791 if( rc==SQLITE_OK && pOp->p1 ){
63792 assert( u.cl.nArg>1 && u.cl.apArg[0] && (u.cl.apArg[0]->flags&MEM_Null) );
63793 db->lastRowid = u.cl.rowid;
63794 }
63795 p->nChange++;
63796 }
63797 break;
63798 }
@@ -59322,24 +63814,24 @@
63814 **
63815 ** If tracing is enabled (by the sqlite3_trace()) interface, then
63816 ** the UTF-8 string contained in P4 is emitted on the trace callback.
63817 */
63818 case OP_Trace: {
63819 #if 0 /* local variables moved into u.cm */
63820 char *zTrace;
63821 #endif /* local variables moved into u.cm */
63822
63823 u.cm.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
63824 if( u.cm.zTrace ){
63825 if( db->xTrace ){
63826 char *z = sqlite3VdbeExpandSql(p, u.cm.zTrace);
63827 db->xTrace(db->pTraceArg, z);
63828 sqlite3DbFree(db, z);
63829 }
63830 #ifdef SQLITE_DEBUG
63831 if( (db->flags & SQLITE_SqlTrace)!=0 ){
63832 sqlite3DebugPrintf("SQL-trace: %s\n", u.cm.zTrace);
63833 }
63834 #endif /* SQLITE_DEBUG */
63835 }
63836 break;
63837 }
@@ -60295,11 +64787,11 @@
64787 }
64788
64789 /*
64790 ** Table of methods for MemJournal sqlite3_file object.
64791 */
64792 static const struct sqlite3_io_methods MemJournalMethods = {
64793 1, /* iVersion */
64794 memjrnlClose, /* xClose */
64795 memjrnlRead, /* xRead */
64796 memjrnlWrite, /* xWrite */
64797 memjrnlTruncate, /* xTruncate */
@@ -60318,11 +64810,11 @@
64810 */
64811 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
64812 MemJournal *p = (MemJournal *)pJfd;
64813 assert( EIGHT_BYTE_ALIGNMENT(p) );
64814 memset(p, 0, sqlite3MemJournalSize());
64815 p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
64816 }
64817
64818 /*
64819 ** Return true if the file-handle passed as an argument is
64820 ** an in-memory journal
@@ -63760,10 +68252,31 @@
68252 if( p->iReg==iReg ){
68253 p->tempReg = 0;
68254 }
68255 }
68256 }
68257
68258 /*
68259 ** Generate code to extract the value of the iCol-th column of a table.
68260 */
68261 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
68262 Vdbe *v, /* The VDBE under construction */
68263 Table *pTab, /* The table containing the value */
68264 int iTabCur, /* The cursor for this table */
68265 int iCol, /* Index of the column to extract */
68266 int regOut /* Extract the valud into this register */
68267 ){
68268 if( iCol<0 || iCol==pTab->iPKey ){
68269 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
68270 }else{
68271 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
68272 sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
68273 }
68274 if( iCol>=0 ){
68275 sqlite3ColumnDefault(v, pTab, iCol, regOut);
68276 }
68277 }
68278
68279 /*
68280 ** Generate code that will extract the iColumn-th column from
68281 ** table pTab and store the column value in a register. An effort
68282 ** is made to store the column value in register iReg, but this is
@@ -63789,17 +68302,11 @@
68302 sqlite3ExprCachePinRegister(pParse, p->iReg);
68303 return p->iReg;
68304 }
68305 }
68306 assert( v!=0 );
68307 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
 
 
 
 
 
 
68308 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
68309 return iReg;
68310 }
68311
68312 /*
@@ -64032,31 +68539,16 @@
68539 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
68540 break;
68541 }
68542 #endif
68543 case TK_VARIABLE: {
 
68544 assert( !ExprHasProperty(pExpr, EP_IntValue) );
68545 assert( pExpr->u.zToken!=0 );
68546 assert( pExpr->u.zToken[0]!=0 );
68547 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
68548 if( pExpr->u.zToken[1]!=0 ){
68549 sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68550 }
68551 break;
68552 }
68553 case TK_REGISTER: {
68554 inReg = pExpr->iTable;
@@ -65119,11 +69611,10 @@
69611 ** this routine is used, it does not hurt to get an extra 2 - that
69612 ** just might result in some slightly slower code. But returning
69613 ** an incorrect 0 or 1 could lead to a malfunction.
69614 */
69615 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
 
69616 if( pA==0||pB==0 ){
69617 return pB==pA ? 0 : 2;
69618 }
69619 assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
69620 assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
@@ -65132,22 +69623,11 @@
69623 }
69624 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
69625 if( pA->op!=pB->op ) return 2;
69626 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
69627 if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
69628 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
 
 
 
 
 
 
 
 
 
 
 
69629 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
69630 if( ExprHasProperty(pA, EP_IntValue) ){
69631 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
69632 return 2;
69633 }
@@ -65160,10 +69640,35 @@
69640 if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
69641 if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
69642 return 0;
69643 }
69644
69645 /*
69646 ** Compare two ExprList objects. Return 0 if they are identical and
69647 ** non-zero if they differ in any way.
69648 **
69649 ** This routine might return non-zero for equivalent ExprLists. The
69650 ** only consequence will be disabled optimizations. But this routine
69651 ** must never return 0 if the two ExprList objects are different, or
69652 ** a malfunction will result.
69653 **
69654 ** Two NULL pointers are considered to be the same. But a NULL pointer
69655 ** always differs from a non-NULL pointer.
69656 */
69657 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
69658 int i;
69659 if( pA==0 && pB==0 ) return 0;
69660 if( pA==0 || pB==0 ) return 1;
69661 if( pA->nExpr!=pB->nExpr ) return 1;
69662 for(i=0; i<pA->nExpr; i++){
69663 Expr *pExprA = pA->a[i].pExpr;
69664 Expr *pExprB = pB->a[i].pExpr;
69665 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
69666 if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
69667 }
69668 return 0;
69669 }
69670
69671 /*
69672 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
69673 ** the new element. Return a negative number if malloc fails.
69674 */
@@ -65656,21 +70161,27 @@
70161 #endif /* !SQLITE_OMIT_TRIGGER */
70162
70163 /*
70164 ** Register built-in functions used to help implement ALTER TABLE
70165 */
70166 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
70167 static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
70168 FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc),
70169 #ifndef SQLITE_OMIT_TRIGGER
70170 FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
 
70171 #endif
70172 #ifndef SQLITE_OMIT_FOREIGN_KEY
70173 FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc),
 
70174 #endif
70175 };
70176 int i;
70177 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
70178 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
70179
70180 for(i=0; i<ArraySize(aAlterTableFuncs); i++){
70181 sqlite3FuncDefInsert(pHash, &aFunc[i]);
70182 }
70183 }
70184
70185 /*
70186 ** This function is used to create the text of expressions of the form:
70187 **
@@ -65810,19 +70321,22 @@
70321 Vdbe *v;
70322 #ifndef SQLITE_OMIT_TRIGGER
70323 char *zWhere = 0; /* Where clause to locate temp triggers */
70324 #endif
70325 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
70326 int savedDbFlags; /* Saved value of db->flags */
70327
70328 savedDbFlags = db->flags;
70329 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
70330 assert( pSrc->nSrc==1 );
70331 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
70332
70333 pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
70334 if( !pTab ) goto exit_rename_table;
70335 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
70336 zDb = db->aDb[iDb].zName;
70337 db->flags |= SQLITE_PreferBuiltin;
70338
70339 /* Get a NULL terminated version of the new table name. */
70340 zName = sqlite3NameFromToken(db, pName);
70341 if( !zName ) goto exit_rename_table;
70342
@@ -65986,10 +70500,11 @@
70500 reloadTableSchema(pParse, pTab, zName);
70501
70502 exit_rename_table:
70503 sqlite3SrcListDelete(db, pSrc);
70504 sqlite3DbFree(db, zName);
70505 db->flags = savedDbFlags;
70506 }
70507
70508
70509 /*
70510 ** Generate code to make sure the file format number is at least minFormat.
@@ -66105,21 +70620,24 @@
70620
70621 /* Modify the CREATE TABLE statement. */
70622 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
70623 if( zCol ){
70624 char *zEnd = &zCol[pColDef->n-1];
70625 int savedDbFlags = db->flags;
70626 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
70627 *zEnd-- = '\0';
70628 }
70629 db->flags |= SQLITE_PreferBuiltin;
70630 sqlite3NestedParse(pParse,
70631 "UPDATE \"%w\".%s SET "
70632 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
70633 "WHERE type = 'table' AND name = %Q",
70634 zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
70635 zTab
70636 );
70637 sqlite3DbFree(db, zCol);
70638 db->flags = savedDbFlags;
70639 }
70640
70641 /* If the default value of the new column is NULL, then set the file
70642 ** format to 2. If the default value of the new column is not NULL,
70643 ** the file format becomes 3.
@@ -66261,11 +70779,11 @@
70779 Parse *pParse, /* Parsing context */
70780 int iDb, /* The database we are looking in */
70781 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
70782 const char *zWhere /* Delete entries associated with this table */
70783 ){
70784 static const struct {
70785 const char *zName;
70786 const char *zCols;
70787 } aTable[] = {
70788 { "sqlite_stat1", "tbl,idx,stat" },
70789 #ifdef SQLITE_ENABLE_STAT2
@@ -67019,11 +71537,12 @@
71537 "attached databases must use the same text encoding as main database");
71538 rc = SQLITE_ERROR;
71539 }
71540 pPager = sqlite3BtreePager(aNew->pBt);
71541 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
71542 /* journal_mode set by the OP_JournalMode opcode that will following
71543 ** the OP_Function opcode that invoked this function. */
71544 sqlite3BtreeSecureDelete(aNew->pBt,
71545 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
71546 }
71547 aNew->safety_level = 3;
71548 aNew->zName = sqlite3DbStrDup(db, zName);
@@ -67164,11 +71683,11 @@
71683 ** sqlite_detach() or sqlite_attach() SQL user functions.
71684 */
71685 static void codeAttach(
71686 Parse *pParse, /* The parser context */
71687 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
71688 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
71689 Expr *pAuthArg, /* Expression to pass to authorization callback */
71690 Expr *pFilename, /* Name of database file */
71691 Expr *pDbname, /* Name of the database to use internally */
71692 Expr *pKey /* Database key for encryption extension */
71693 ){
@@ -67214,10 +71733,21 @@
71733 if( v ){
71734 sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
71735 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
71736 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
71737 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
71738
71739 if( type==SQLITE_ATTACH ){
71740 /* On an attach, also set the journal mode. Note that
71741 ** sqlite3VdbeUsesBtree() is not call here since the iDb index
71742 ** will be out of range prior to the new database being attached.
71743 ** The OP_JournalMode opcode will all sqlite3VdbeUsesBtree() for us.
71744 */
71745 sqlite3VdbeAddOp3(v, OP_JournalMode, db->nDb, regArgs+3,
71746 db->dfltJournalMode);
71747 sqlite3VdbeChangeP5(v, 1);
71748 }
71749
71750 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
71751 ** statement only). For DETACH, set it to false (expire all existing
71752 ** statements).
71753 */
@@ -67234,11 +71764,11 @@
71764 ** Called by the parser to compile a DETACH statement.
71765 **
71766 ** DETACH pDbname
71767 */
71768 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
71769 static const FuncDef detach_func = {
71770 1, /* nArg */
71771 SQLITE_UTF8, /* iPrefEnc */
71772 0, /* flags */
71773 0, /* pUserData */
71774 0, /* pNext */
@@ -67255,11 +71785,11 @@
71785 ** Called by the parser to compile an ATTACH statement.
71786 **
71787 ** ATTACH p AS pDbname KEY pKey
71788 */
71789 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
71790 static const FuncDef attach_func = {
71791 3, /* nArg */
71792 SQLITE_UTF8, /* iPrefEnc */
71793 0, /* flags */
71794 0, /* pUserData */
71795 0, /* pNext */
@@ -71050,11 +75580,11 @@
75580 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
75581 char *zName = sqlite3NameFromToken(pParse->db, pName);
75582 if( zName ){
75583 Vdbe *v = sqlite3GetVdbe(pParse);
75584 #ifndef SQLITE_OMIT_AUTHORIZATION
75585 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
75586 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
75587 #endif
75588 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
75589 sqlite3DbFree(pParse->db, zName);
75590 return;
@@ -71090,11 +75620,11 @@
75620 assert( db->aDb[1].pSchema );
75621 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
75622 db->mallocFailed = 1;
75623 return 1;
75624 }
75625 sqlite3PagerSetJournalMode(sqlite3BtreePager(pBt), db->dfltJournalMode);
75626 }
75627 return 0;
75628 }
75629
75630 /*
@@ -71729,19 +76259,24 @@
76259 }
76260 p = p->pNext;
76261 }
76262
76263 /* If no match is found, search the built-in functions.
76264 **
76265 ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
76266 ** functions even if a prior app-defined function was found. And give
76267 ** priority to built-in functions.
76268 **
76269 ** Except, if createFlag is true, that means that we are trying to
76270 ** install a new function. Whatever FuncDef structure is returned will
76271 ** have fields overwritten with new information appropriate for the
76272 ** new function. But the FuncDefs for built-in functions are read-only.
76273 ** So we must not search for built-ins when creating a new function.
76274 */
76275 if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
76276 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
76277 bestScore = 0;
76278 p = functionSearch(pHash, h, zName, nName);
76279 while( p ){
76280 int score = matchQuality(p, nArg, enc);
76281 if( score>bestScore ){
76282 pBest = p;
@@ -72337,13 +76872,11 @@
76872 /* Populate the OLD.* pseudo-table register array. These values will be
76873 ** used by any BEFORE and AFTER triggers that exist. */
76874 sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
76875 for(iCol=0; iCol<pTab->nCol; iCol++){
76876 if( mask==0xffffffff || mask&(1<<iCol) ){
76877 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
 
 
76878 }
76879 }
76880
76881 /* Invoke BEFORE DELETE trigger programs. */
76882 sqlite3CodeRowTrigger(pParse, pTrigger,
@@ -73875,24 +78408,19 @@
78408 }
78409 }
78410 }
78411
78412 /*
78413 ** This routine does per-connection function registration. Most
78414 ** of the built-in functions above are part of the global function set.
78415 ** This routine only deals with those that are not global.
78416 */
78417 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
78418 int rc = sqlite3_overload_function(db, "MATCH", 2);
78419 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
78420 if( rc==SQLITE_NOMEM ){
78421 db->mallocFailed = 1;
 
 
 
 
 
78422 }
78423 }
78424
78425 /*
78426 ** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -74056,10 +78584,13 @@
78584
78585 for(i=0; i<ArraySize(aBuiltinFunc); i++){
78586 sqlite3FuncDefInsert(pHash, &aFunc[i]);
78587 }
78588 sqlite3RegisterDateTimeFunctions();
78589 #ifndef SQLITE_OMIT_ALTERTABLE
78590 sqlite3AlterFunctions();
78591 #endif
78592 }
78593
78594 /************** End of func.c ************************************************/
78595 /************** Begin file fkey.c ********************************************/
78596 /*
@@ -78480,10 +83011,35 @@
83011 }
83012 return zName;
83013 }
83014 #endif
83015
83016
83017 /*
83018 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
83019 ** defined in pager.h. This function returns the associated lowercase
83020 ** journal-mode name.
83021 */
83022 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
83023 static char * const azModeName[] = {
83024 "delete", "persist", "off", "truncate", "memory"
83025 #ifndef SQLITE_OMIT_WAL
83026 , "wal"
83027 #endif
83028 };
83029 assert( PAGER_JOURNALMODE_DELETE==0 );
83030 assert( PAGER_JOURNALMODE_PERSIST==1 );
83031 assert( PAGER_JOURNALMODE_OFF==2 );
83032 assert( PAGER_JOURNALMODE_TRUNCATE==3 );
83033 assert( PAGER_JOURNALMODE_MEMORY==4 );
83034 assert( PAGER_JOURNALMODE_WAL==5 );
83035 assert( eMode>=0 && eMode<=ArraySize(azModeName) );
83036
83037 if( eMode==ArraySize(azModeName) ) return 0;
83038 return azModeName[eMode];
83039 }
83040
83041 /*
83042 ** Process a pragma statement.
83043 **
83044 ** Pragmas are of this form:
83045 **
@@ -78552,15 +83108,15 @@
83108 ** page cache size. The value returned is the maximum number of
83109 ** pages in the page cache. The second form sets both the current
83110 ** page cache size value and the persistent page cache size value
83111 ** stored in the database file.
83112 **
83113 ** Older versions of SQLite would set the default cache size to a
83114 ** negative number to indicate synchronous=OFF. These days, synchronous
83115 ** is always on by default regardless of the sign of the default cache
83116 ** size. But continue to take the absolute value of the default cache
83117 ** size of historical compatibility.
83118 */
83119 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
83120 static const VdbeOpList getCacheSize[] = {
83121 { OP_Transaction, 0, 0, 0}, /* 0 */
83122 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
@@ -78585,14 +83141,10 @@
83141 }else{
83142 int size = atoi(zRight);
83143 if( size<0 ) size = -size;
83144 sqlite3BeginWriteOperation(pParse, 0, iDb);
83145 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
 
 
 
 
83146 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
83147 pDb->pSchema->cache_size = size;
83148 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
83149 }
83150 }else
@@ -78733,66 +83285,62 @@
83285 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
83286 }else
83287
83288 /*
83289 ** PRAGMA [database.]journal_mode
83290 ** PRAGMA [database.]journal_mode =
83291 ** (delete|persist|off|truncate|memory|wal|off)
83292 */
83293 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
83294 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
83295
83296 sqlite3VdbeSetNumCols(v, 1);
83297 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
83298
83299 if( zRight==0 ){
83300 eMode = PAGER_JOURNALMODE_QUERY;
83301 }else{
83302 const char *zMode;
83303 int n = sqlite3Strlen30(zRight);
83304 for(eMode=0; (zMode = sqlite3JournalModename(eMode)); eMode++){
83305 if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
83306 }
83307 if( !zMode ){
83308 eMode = PAGER_JOURNALMODE_QUERY;
83309 }
83310 }
83311 if( pId2->n==0 && eMode==PAGER_JOURNALMODE_QUERY ){
83312 /* Simple "PRAGMA journal_mode;" statement. This is a query for
83313 ** the current default journal mode (which may be different to
83314 ** the journal-mode of the main database).
83315 */
83316 eMode = db->dfltJournalMode;
83317 sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
83318 sqlite3VdbeChangeP4(v, -1, sqlite3JournalModename(eMode), P4_STATIC);
83319 }else{
83320 int ii;
83321
83322 if( pId2->n==0 ){
83323 /* When there is no database name before the "journal_mode" keyword
83324 ** in the PRAGMA, then the journal-mode will be set on
83325 ** all attached databases, as well as the main db file.
83326 **
83327 ** Also, the sqlite3.dfltJournalMode variable is set so that
83328 ** any subsequently attached databases also use the specified
83329 ** journal mode.
83330 */
 
 
 
 
 
 
 
 
83331 db->dfltJournalMode = (u8)eMode;
83332 }
83333
83334 for(ii=db->nDb-1; ii>=0; ii--){
83335 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
83336 sqlite3VdbeUsesBtree(v, ii);
83337 sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
83338 }
83339 }
83340 }
83341
 
 
 
83342 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
83343 }else
83344
83345 /*
83346 ** PRAGMA [database.]journal_size_limit
@@ -79605,10 +84153,40 @@
84153 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
84154 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
84155 }
84156 }else
84157 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
84158
84159 #ifndef SQLITE_OMIT_WAL
84160 /*
84161 ** PRAGMA [database.]wal_checkpoint
84162 **
84163 ** Checkpoint the database.
84164 */
84165 if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
84166 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
84167 sqlite3VdbeAddOp3(v, OP_Checkpoint, pId2->z?iDb:SQLITE_MAX_ATTACHED, 0, 0);
84168 }else
84169
84170 /*
84171 ** PRAGMA wal_autocheckpoint
84172 ** PRAGMA wal_autocheckpoint = N
84173 **
84174 ** Configure a database connection to automatically checkpoint a database
84175 ** after accumulating N frames in the log. Or query for the current value
84176 ** of N.
84177 */
84178 if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
84179 if( zRight ){
84180 int nAuto = atoi(zRight);
84181 sqlite3_wal_autocheckpoint(db, nAuto);
84182 }
84183 returnSingleInt(pParse, "wal_autocheckpoint",
84184 db->xWalCallback==sqlite3WalDefaultHook ?
84185 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
84186 }else
84187 #endif
84188
84189 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
84190 /*
84191 ** Report the current state of file logs for all databases
84192 */
@@ -84278,10 +88856,22 @@
88856 p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
88857 pGroupBy = p->pGroupBy;
88858 p->selFlags &= ~SF_Distinct;
88859 isDistinct = 0;
88860 }
88861
88862 /* If there is both a GROUP BY and an ORDER BY clause and they are
88863 ** identical, then disable the ORDER BY clause since the GROUP BY
88864 ** will cause elements to come out in the correct order. This is
88865 ** an optimization - the correct answer should result regardless.
88866 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
88867 ** to disable this optimization for testing purposes.
88868 */
88869 if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
88870 && (db->flags & SQLITE_GroupByOrder)==0 ){
88871 pOrderBy = 0;
88872 }
88873
88874 /* If there is an ORDER BY clause, then this sorting
88875 ** index might end up being unused if the data can be
88876 ** extracted in pre-sorted order. If that is the case, then the
88877 ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
@@ -86161,11 +90751,11 @@
90751 **
90752 ** May you do good and not evil.
90753 ** May you find forgiveness for yourself and forgive others.
90754 ** May you share freely, never taking more than you give.
90755 **
90756 sqlite*************************************************************************
90757 ** This file contains C code routines that are called by the parser
90758 ** to handle UPDATE statements.
90759 */
90760
90761 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -86548,12 +91138,11 @@
91138 oldmask |= sqlite3TriggerColmask(pParse,
91139 pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
91140 );
91141 for(i=0; i<pTab->nCol; i++){
91142 if( aXRef[i]<0 || oldmask==0xffffffff || (oldmask & (1<<i)) ){
91143 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
 
91144 }else{
91145 sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
91146 }
91147 }
91148 if( chngRowid==0 ){
@@ -86922,11 +91511,12 @@
91511 int saved_nChange; /* Saved value of db->nChange */
91512 int saved_nTotalChange; /* Saved value of db->nTotalChange */
91513 void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
91514 Db *pDb = 0; /* Database to detach at end of vacuum */
91515 int isMemDb; /* True if vacuuming a :memory: database */
91516 int nRes; /* Bytes of reserved space at the end of each page */
91517 int nDb; /* Number of attached databases */
91518
91519 if( !db->autoCommit ){
91520 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
91521 return SQLITE_ERROR;
91522 }
@@ -86936,11 +91526,11 @@
91526 ** disable CHECK and foreign key constraints. */
91527 saved_flags = db->flags;
91528 saved_nChange = db->nChange;
91529 saved_nTotalChange = db->nTotalChange;
91530 saved_xTrace = db->xTrace;
91531 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
91532 db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
91533 db->xTrace = 0;
91534
91535 pMain = db->aDb[0].pBt;
91536 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
@@ -86957,19 +91547,22 @@
91547 ** actually occurs when doing a vacuum since the vacuum_db is initially
91548 ** empty. Only the journal header is written. Apparently it takes more
91549 ** time to parse and run the PRAGMA to turn journalling off than it does
91550 ** to write the journal header file.
91551 */
91552 nDb = db->nDb;
91553 if( sqlite3TempInMemory(db) ){
91554 zSql = "ATTACH ':memory:' AS vacuum_db;";
91555 }else{
91556 zSql = "ATTACH '' AS vacuum_db;";
91557 }
91558 rc = execSql(db, pzErrMsg, zSql);
91559 if( db->nDb>nDb ){
91560 pDb = &db->aDb[db->nDb-1];
91561 assert( strcmp(pDb->zName,"vacuum_db")==0 );
91562 }
91563 if( rc!=SQLITE_OK ) goto end_of_vacuum;
 
 
91564 pTemp = db->aDb[db->nDb-1].pBt;
91565
91566 /* The call to execSql() to attach the temp database has left the file
91567 ** locked (as there was more than one active statement when the transaction
91568 ** to read the schema was concluded. Unlock it here so that this doesn't
@@ -86986,10 +91579,16 @@
91579 char *zKey;
91580 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
91581 if( nKey ) db->nextPagesize = 0;
91582 }
91583 #endif
91584
91585 /* Do not attempt to change the page size for a WAL database */
91586 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
91587 ==PAGER_JOURNALMODE_WAL ){
91588 db->nextPagesize = 0;
91589 }
91590
91591 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
91592 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
91593 || NEVER(db->mallocFailed)
91594 ){
@@ -87123,10 +91722,11 @@
91722 /* Restore the original value of db->flags */
91723 db->flags = saved_flags;
91724 db->nChange = saved_nChange;
91725 db->nTotalChange = saved_nTotalChange;
91726 db->xTrace = saved_xTrace;
91727 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
91728
91729 /* Currently there is an SQL level transaction open on the vacuum
91730 ** database. No locks are held on any other files (since the main file
91731 ** was committed at the btree level). So it safe to end the transaction
91732 ** by manually setting the autoCommit flag to true and detaching the
@@ -90988,11 +95588,11 @@
95588 ** as we can without disabling too much. If we disabled in (1), we'd get
95589 ** the wrong answer. See ticket #813.
95590 */
95591 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
95592 if( pTerm
95593 && (pTerm->wtFlags & TERM_CODED)==0
95594 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
95595 ){
95596 pTerm->wtFlags |= TERM_CODED;
95597 if( pTerm->iParent>=0 ){
95598 WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
@@ -91186,11 +95786,13 @@
95786 for(j=0; j<nEq; j++){
95787 int r1;
95788 int k = pIdx->aiColumn[j];
95789 pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
95790 if( NEVER(pTerm==0) ) break;
95791 /* The following true for indices with redundant columns.
95792 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
95793 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
95794 r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
95795 if( r1!=regBase+j ){
95796 if( nReg==1 ){
95797 sqlite3ReleaseTempReg(pParse, regBase);
95798 regBase = r1;
@@ -91473,11 +96075,12 @@
96075 int nConstraint; /* Number of constraint terms */
96076 Index *pIdx; /* The index we will be using */
96077 int iIdxCur; /* The VDBE cursor for the index */
96078 int nExtraReg = 0; /* Number of extra registers needed */
96079 int op; /* Instruction opcode */
96080 char *zStartAff; /* Affinity for start of range constraint */
96081 char *zEndAff; /* Affinity for end of range constraint */
96082
96083 pIdx = pLevel->plan.u.pIdx;
96084 iIdxCur = pLevel->iIdxCur;
96085 k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
96086
@@ -91514,12 +96117,13 @@
96117 /* Generate code to evaluate all constraint terms using == or IN
96118 ** and store the values of those terms in an array of registers
96119 ** starting at regBase.
96120 */
96121 regBase = codeAllEqualityTerms(
96122 pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
96123 );
96124 zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
96125 addrNxt = pLevel->addrNxt;
96126
96127 /* If we are doing a reverse order scan on an ascending index, or
96128 ** a forward order scan on a descending index, interchange the
96129 ** start and end terms (pRangeStart and pRangeEnd).
@@ -91540,29 +96144,29 @@
96144 nConstraint = nEq;
96145 if( pRangeStart ){
96146 Expr *pRight = pRangeStart->pExpr->pRight;
96147 sqlite3ExprCode(pParse, pRight, regBase+nEq);
96148 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
96149 if( zStartAff ){
96150 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
96151 /* Since the comparison is to be performed with no conversions
96152 ** applied to the operands, set the affinity to apply to pRight to
96153 ** SQLITE_AFF_NONE. */
96154 zStartAff[nEq] = SQLITE_AFF_NONE;
96155 }
96156 if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
96157 zStartAff[nEq] = SQLITE_AFF_NONE;
96158 }
96159 }
96160 nConstraint++;
96161 }else if( isMinQuery ){
96162 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
96163 nConstraint++;
96164 startEq = 0;
96165 start_constraints = 1;
96166 }
96167 codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
96168 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
96169 assert( op!=0 );
96170 testcase( op==OP_Rewind );
96171 testcase( op==OP_Last );
96172 testcase( op==OP_SeekGt );
@@ -91578,25 +96182,26 @@
96182 if( pRangeEnd ){
96183 Expr *pRight = pRangeEnd->pExpr->pRight;
96184 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
96185 sqlite3ExprCode(pParse, pRight, regBase+nEq);
96186 sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
96187 if( zEndAff ){
96188 if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
96189 /* Since the comparison is to be performed with no conversions
96190 ** applied to the operands, set the affinity to apply to pRight to
96191 ** SQLITE_AFF_NONE. */
96192 zEndAff[nEq] = SQLITE_AFF_NONE;
96193 }
96194 if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
96195 zEndAff[nEq] = SQLITE_AFF_NONE;
96196 }
96197 }
96198 codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
96199 nConstraint++;
96200 }
96201 sqlite3DbFree(pParse->db, zStartAff);
96202 sqlite3DbFree(pParse->db, zEndAff);
96203
96204 /* Top of the loop body */
96205 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
96206
96207 /* Check if the index cursor is past the end of the range. */
@@ -97936,11 +102541,11 @@
102541 /* SQLITE_IOERR */ "disk I/O error",
102542 /* SQLITE_CORRUPT */ "database disk image is malformed",
102543 /* SQLITE_NOTFOUND */ 0,
102544 /* SQLITE_FULL */ "database or disk is full",
102545 /* SQLITE_CANTOPEN */ "unable to open database file",
102546 /* SQLITE_PROTOCOL */ "locking protocol",
102547 /* SQLITE_EMPTY */ "table contains no data",
102548 /* SQLITE_SCHEMA */ "database schema has changed",
102549 /* SQLITE_TOOBIG */ "string or blob too big",
102550 /* SQLITE_CONSTRAINT */ "constraint failed",
102551 /* SQLITE_MISMATCH */ "datatype mismatch",
@@ -98346,10 +102951,149 @@
102951 db->pRollbackArg = pArg;
102952 sqlite3_mutex_leave(db->mutex);
102953 return pRet;
102954 }
102955
102956 #ifndef SQLITE_OMIT_WAL
102957 /*
102958 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
102959 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
102960 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
102961 ** wal_autocheckpoint()).
102962 */
102963 SQLITE_PRIVATE int sqlite3WalDefaultHook(
102964 void *pClientData, /* Argument */
102965 sqlite3 *db, /* Connection */
102966 const char *zDb, /* Database */
102967 int nFrame /* Size of WAL */
102968 ){
102969 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
102970 sqlite3BeginBenignMalloc();
102971 sqlite3_wal_checkpoint(db, zDb);
102972 sqlite3EndBenignMalloc();
102973 }
102974 return SQLITE_OK;
102975 }
102976 #endif /* SQLITE_OMIT_WAL */
102977
102978 /*
102979 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
102980 ** a database after committing a transaction if there are nFrame or
102981 ** more frames in the log file. Passing zero or a negative value as the
102982 ** nFrame parameter disables automatic checkpoints entirely.
102983 **
102984 ** The callback registered by this function replaces any existing callback
102985 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
102986 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
102987 ** configured by this function.
102988 */
102989 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
102990 #ifndef SQLITE_OMIT_WAL
102991 if( nFrame>0 ){
102992 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
102993 }else{
102994 sqlite3_wal_hook(db, 0, 0);
102995 }
102996 #endif
102997 return SQLITE_OK;
102998 }
102999
103000 /*
103001 ** Register a callback to be invoked each time a transaction is written
103002 ** into the write-ahead-log by this database connection.
103003 */
103004 SQLITE_API void *sqlite3_wal_hook(
103005 sqlite3 *db, /* Attach the hook to this db handle */
103006 int(*xCallback)(void *, sqlite3*, const char*, int),
103007 void *pArg /* First argument passed to xCallback() */
103008 ){
103009 #ifndef SQLITE_OMIT_WAL
103010 void *pRet;
103011 sqlite3_mutex_enter(db->mutex);
103012 pRet = db->pWalArg;
103013 db->xWalCallback = xCallback;
103014 db->pWalArg = pArg;
103015 sqlite3_mutex_leave(db->mutex);
103016 return pRet;
103017 #else
103018 return 0;
103019 #endif
103020 }
103021
103022
103023 /*
103024 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
103025 ** to contains a zero-length string, all attached databases are
103026 ** checkpointed.
103027 */
103028 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
103029 #ifdef SQLITE_OMIT_WAL
103030 return SQLITE_OK;
103031 #else
103032 int rc; /* Return code */
103033 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
103034
103035 sqlite3_mutex_enter(db->mutex);
103036 if( zDb && zDb[0] ){
103037 iDb = sqlite3FindDbName(db, zDb);
103038 }
103039 if( iDb<0 ){
103040 rc = SQLITE_ERROR;
103041 sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
103042 }else{
103043 rc = sqlite3Checkpoint(db, iDb);
103044 sqlite3Error(db, rc, 0);
103045 }
103046 rc = sqlite3ApiExit(db, rc);
103047 sqlite3_mutex_leave(db->mutex);
103048 return rc;
103049 #endif
103050 }
103051
103052 #ifndef SQLITE_OMIT_WAL
103053 /*
103054 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
103055 ** not currently open in WAL mode.
103056 **
103057 ** If a transaction is open on the database being checkpointed, this
103058 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
103059 ** an error occurs while running the checkpoint, an SQLite error code is
103060 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
103061 **
103062 ** The mutex on database handle db should be held by the caller. The mutex
103063 ** associated with the specific b-tree being checkpointed is taken by
103064 ** this function while the checkpoint is running.
103065 **
103066 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
103067 ** checkpointed. If an error is encountered it is returned immediately -
103068 ** no attempt is made to checkpoint any remaining databases.
103069 */
103070 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb){
103071 int rc = SQLITE_OK; /* Return code */
103072 int i; /* Used to iterate through attached dbs */
103073
103074 assert( sqlite3_mutex_held(db->mutex) );
103075
103076 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
103077 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
103078 Btree *pBt = db->aDb[i].pBt;
103079 if( pBt ){
103080 if( sqlite3BtreeIsInReadTrans(pBt) ){
103081 rc = SQLITE_LOCKED;
103082 }else{
103083 sqlite3BtreeEnter(pBt);
103084 rc = sqlite3PagerCheckpoint(sqlite3BtreePager(pBt));
103085 sqlite3BtreeLeave(pBt);
103086 }
103087 }
103088 }
103089 }
103090
103091 return rc;
103092 }
103093 #endif /* SQLITE_OMIT_WAL */
103094
103095 /*
103096 ** This function returns true if main-memory should be used instead of
103097 ** a temporary file for transient pager files and statement journals.
103098 ** The value returned depends on the value of db->temp_store (runtime
103099 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
@@ -98906,10 +103650,12 @@
103650
103651 /* Enable the lookaside-malloc subsystem */
103652 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
103653 sqlite3GlobalConfig.nLookaside);
103654
103655 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
103656
103657 opendb_out:
103658 if( db ){
103659 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
103660 sqlite3_mutex_leave(db->mutex);
103661 }
@@ -99400,13 +104146,17 @@
104146 ** an incompatible database file format. Changing the PENDING byte
104147 ** while any database connection is open results in undefined and
104148 ** dileterious behavior.
104149 */
104150 case SQLITE_TESTCTRL_PENDING_BYTE: {
104151 rc = PENDING_BYTE;
104152 #ifndef SQLITE_OMIT_WSD
104153 {
104154 unsigned int newVal = va_arg(ap, unsigned int);
104155 if( newVal ) sqlite3PendingByte = newVal;
104156 }
104157 #endif
104158 break;
104159 }
104160
104161 /*
104162 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
@@ -99505,10 +104255,19 @@
104255 int n = sqlite3Strlen30(zWord);
104256 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
104257 break;
104258 }
104259 #endif
104260
104261 /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
104262 **
104263 ** Return the size of a pcache header in bytes.
104264 */
104265 case SQLITE_TESTCTRL_PGHDRSZ: {
104266 rc = sizeof(PgHdr);
104267 break;
104268 }
104269
104270 }
104271 va_end(ap);
104272 #endif /* SQLITE_OMIT_BUILTIN_TEST */
104273 return rc;
@@ -99671,10 +104430,11 @@
104430 sqlite3_mutex_enter(db->mutex);
104431 enterMutex();
104432
104433 if( xNotify==0 ){
104434 removeFromBlockedList(db);
104435 db->pBlockingConnection = 0;
104436 db->pUnlockConnection = 0;
104437 db->xUnlockNotify = 0;
104438 db->pUnlockArg = 0;
104439 }else if( 0==db->pBlockingConnection ){
104440 /* The blocking transaction has been concluded. Or there never was a
104441
+176 -13
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.6.23"
111
-#define SQLITE_VERSION_NUMBER 3006023
112
-#define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
110
+#define SQLITE_VERSION "3.7.0"
111
+#define SQLITE_VERSION_NUMBER 3007000
112
+#define SQLITE_SOURCE_ID "2010-06-16 12:30:11 ad3209572d0e6afe5c8b52313e334509661045e2"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -391,11 +391,11 @@
391391
#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392392
#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393393
#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
394394
#define SQLITE_FULL 13 /* Insertion failed because database is full */
395395
#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396
-#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
396
+#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
397397
#define SQLITE_EMPTY 16 /* Database is empty */
398398
#define SQLITE_SCHEMA 17 /* The database schema changed */
399399
#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
400400
#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
401401
#define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -447,11 +447,16 @@
447447
#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
448448
#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
449449
#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
450450
#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
451451
#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
452
-#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
452
+#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
453
+#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
454
+#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
455
+#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
456
+#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
457
+#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
453458
454459
/*
455460
** CAPI3REF: Flags For File Open Operations
456461
**
457462
** These bit values are intended for use in the
@@ -656,10 +661,17 @@
656661
int (*xUnlock)(sqlite3_file*, int);
657662
int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
658663
int (*xFileControl)(sqlite3_file*, int op, void *pArg);
659664
int (*xSectorSize)(sqlite3_file*);
660665
int (*xDeviceCharacteristics)(sqlite3_file*);
666
+ /* Methods above are valid for version 1 */
667
+ int (*xShmOpen)(sqlite3_file*);
668
+ int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
669
+ int (*xShmMap)(sqlite3_file*, int iPage, int pgsz, int, void volatile**);
670
+ void (*xShmBarrier)(sqlite3_file*);
671
+ int (*xShmClose)(sqlite3_file*, int deleteFlag);
672
+ /* Methods above are valid for version 2 */
661673
/* Additional methods may be added in future releases */
662674
};
663675
664676
/*
665677
** CAPI3REF: Standard File Control Opcodes
@@ -673,15 +685,23 @@
673685
** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
674686
** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
675687
** into an integer that the pArg argument points to. This capability
676688
** is used during testing and only needs to be supported when SQLITE_TEST
677689
** is defined.
690
+**
691
+** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
692
+** layer a hint of how large the database file will grow to be during the
693
+** current transaction. This hint is not guaranteed to be accurate but it
694
+** is often close. The underlying VFS might choose to preallocate database
695
+** file space based on this hint in order to help writes to the database
696
+** file run faster.
678697
*/
679698
#define SQLITE_FCNTL_LOCKSTATE 1
680699
#define SQLITE_GET_LOCKPROXYFILE 2
681700
#define SQLITE_SET_LOCKPROXYFILE 3
682701
#define SQLITE_LAST_ERRNO 4
702
+#define SQLITE_FCNTL_SIZE_HINT 5
683703
684704
/*
685705
** CAPI3REF: Mutex Handle
686706
**
687707
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -822,11 +842,11 @@
822842
** method returns a Julian Day Number for the current date and time.
823843
**
824844
*/
825845
typedef struct sqlite3_vfs sqlite3_vfs;
826846
struct sqlite3_vfs {
827
- int iVersion; /* Structure version number */
847
+ int iVersion; /* Structure version number (currently 2) */
828848
int szOsFile; /* Size of subclassed sqlite3_file */
829849
int mxPathname; /* Maximum file pathname length */
830850
sqlite3_vfs *pNext; /* Next registered VFS */
831851
const char *zName; /* Name of this virtual file system */
832852
void *pAppData; /* Pointer to application-specific data */
@@ -841,12 +861,21 @@
841861
void (*xDlClose)(sqlite3_vfs*, void*);
842862
int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
843863
int (*xSleep)(sqlite3_vfs*, int microseconds);
844864
int (*xCurrentTime)(sqlite3_vfs*, double*);
845865
int (*xGetLastError)(sqlite3_vfs*, int, char *);
846
- /* New fields may be appended in figure versions. The iVersion
847
- ** value will increment whenever this happens. */
866
+ /*
867
+ ** The methods above are in version 1 of the sqlite_vfs object
868
+ ** definition. Those that follow are added in version 2 or later
869
+ */
870
+ int (*xRename)(sqlite3_vfs*, const char *zOld, const char *zNew, int dirSync);
871
+ int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
872
+ /*
873
+ ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
874
+ ** New fields may be appended in figure versions. The iVersion
875
+ ** value will increment whenever this happens.
876
+ */
848877
};
849878
850879
/*
851880
** CAPI3REF: Flags for the xAccess VFS method
852881
**
@@ -861,10 +890,48 @@
861890
** checks whether the file is readable.
862891
*/
863892
#define SQLITE_ACCESS_EXISTS 0
864893
#define SQLITE_ACCESS_READWRITE 1
865894
#define SQLITE_ACCESS_READ 2
895
+
896
+/*
897
+** CAPI3REF: Flags for the xShmLock VFS method
898
+**
899
+** These integer constants define the various locking operations
900
+** allowed by the xShmLock method of [sqlite3_io_methods]. The
901
+** following are the only legal combinations of flags to the
902
+** xShmLock method:
903
+**
904
+** <ul>
905
+** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
906
+** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
907
+** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
908
+** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
909
+** </ul>
910
+**
911
+** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
912
+** was given no the corresponding lock.
913
+**
914
+** The xShmLock method can transition between unlocked and SHARED or
915
+** between unlocked and EXCLUSIVE. It cannot transition between SHARED
916
+** and EXCLUSIVE.
917
+*/
918
+#define SQLITE_SHM_UNLOCK 1
919
+#define SQLITE_SHM_LOCK 2
920
+#define SQLITE_SHM_SHARED 4
921
+#define SQLITE_SHM_EXCLUSIVE 8
922
+
923
+/*
924
+** CAPI3REF: Maximum xShmLock index
925
+**
926
+** The xShmLock method on [sqlite3_io_methods] may use values
927
+** between 0 and this upper bound as its "offset" argument.
928
+** The SQLite core will never attempt to acquire or release a
929
+** lock outside of this range
930
+*/
931
+#define SQLITE_SHM_NLOCK 8
932
+
866933
867934
/*
868935
** CAPI3REF: Initialize The SQLite Library
869936
**
870937
** ^The sqlite3_initialize() routine initializes the
@@ -2888,10 +2955,18 @@
28882955
** already been [sqlite3_finalize | finalized] or on one that had
28892956
** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
28902957
** be the case that the same database connection is being used by two or
28912958
** more threads at the same moment in time.
28922959
**
2960
+** For all versions of SQLite up to and including 3.6.23.1, it was required
2961
+** after sqlite3_step() returned anything other than [SQLITE_ROW] that
2962
+** [sqlite3_reset()] be called before any subsequent invocation of
2963
+** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
2964
+** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
2965
+** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
2966
+** automatically in this circumstance rather than returning [SQLITE_MISUSE].
2967
+**
28932968
** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
28942969
** API always returns a generic error code, [SQLITE_ERROR], following any
28952970
** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
28962971
** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
28972972
** specific [error codes] that better describes the error.
@@ -4986,11 +5061,12 @@
49865061
#define SQLITE_TESTCTRL_ASSERT 12
49875062
#define SQLITE_TESTCTRL_ALWAYS 13
49885063
#define SQLITE_TESTCTRL_RESERVE 14
49895064
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
49905065
#define SQLITE_TESTCTRL_ISKEYWORD 16
4991
-#define SQLITE_TESTCTRL_LAST 16
5066
+#define SQLITE_TESTCTRL_PGHDRSZ 17
5067
+#define SQLITE_TESTCTRL_LAST 17
49925068
49935069
/*
49945070
** CAPI3REF: SQLite Runtime Status
49955071
**
49965072
** ^This interface is used to retrieve runtime status information
@@ -5450,14 +5526,18 @@
54505526
** then an [error code] is returned. ^As well as [SQLITE_OK] and
54515527
** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
54525528
** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
54535529
** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
54545530
**
5455
-** ^The sqlite3_backup_step() might return [SQLITE_READONLY] if the destination
5456
-** database was opened read-only or if
5457
-** the destination is an in-memory database with a different page size
5458
-** from the source database.
5531
+** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
5532
+** <ol>
5533
+** <li> the destination database was opened read-only, or
5534
+** <li> the destination database is using write-ahead-log journaling
5535
+** and the destination and source page sizes differ, or
5536
+** <li> The destination database is an in-memory database and the
5537
+** destination and source page sizes differ.
5538
+** </ol>)^
54595539
**
54605540
** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
54615541
** the [sqlite3_busy_handler | busy-handler function]
54625542
** is invoked (if one is specified). ^If the
54635543
** busy-handler returns non-zero before the lock is available, then
@@ -5721,10 +5801,93 @@
57215801
** a few hundred characters, it will be truncated to the length of the
57225802
** buffer.
57235803
*/
57245804
SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
57255805
5806
+/*
5807
+** CAPI3REF: Write-Ahead Log Commit Hook
5808
+**
5809
+** ^The [sqlite3_wal_hook()] function is used to register a callback that
5810
+** will be invoked each time a database connection commits data to a
5811
+** [write-ahead log] (i.e. whenever a transaction is committed in
5812
+** [journal_mode | journal_mode=WAL mode]).
5813
+**
5814
+** ^The callback is invoked by SQLite after the commit has taken place and
5815
+** the associated write-lock on the database released, so the implementation
5816
+** may read, write or [checkpoint] the database as required.
5817
+**
5818
+** ^The first parameter passed to the callback function when it is invoked
5819
+** is a copy of the third parameter passed to sqlite3_wal_hook() when
5820
+** registering the callback. ^The second is a copy of the database handle.
5821
+** ^The third parameter is the name of the database that was written to -
5822
+** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
5823
+** is the number of pages currently in the write-ahead log file,
5824
+** including those that were just committed.
5825
+**
5826
+** The callback function should normally return [SQLITE_OK]. ^If an error
5827
+** code is returned, that error will propagate back up through the
5828
+** SQLite code base to cause the statement that provoked the callback
5829
+** to report an error, though the commit will have still occurred. If the
5830
+** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
5831
+** that does not correspond to any valid SQLite error code, the results
5832
+** are undefined.
5833
+**
5834
+** A single database handle may have at most a single write-ahead log callback
5835
+** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
5836
+** previously registered write-ahead log callback. ^Note that the
5837
+** [sqlite3_wal_autocheckpoint()] interface and the
5838
+** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
5839
+** those overwrite any prior [sqlite3_wal_hook()] settings.
5840
+*/
5841
+SQLITE_API void *sqlite3_wal_hook(
5842
+ sqlite3*,
5843
+ int(*)(void *,sqlite3*,const char*,int),
5844
+ void*
5845
+);
5846
+
5847
+/*
5848
+** CAPI3REF: Configure an auto-checkpoint
5849
+**
5850
+** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
5851
+** [sqlite3_wal_hook()] that causes any database on [database connection] D
5852
+** to automatically [checkpoint]
5853
+** after committing a transaction if there are N or
5854
+** more frames in the [write-ahead log] file. ^Passing zero or
5855
+** a negative value as the nFrame parameter disables automatic
5856
+** checkpoints entirely.
5857
+**
5858
+** ^The callback registered by this function replaces any existing callback
5859
+** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
5860
+** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
5861
+** configured by this function.
5862
+**
5863
+** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
5864
+** from SQL.
5865
+**
5866
+** ^Every new [database connection] defaults to having the auto-checkpoint
5867
+** enabled with a threshold of 1000 pages. The use of this interface
5868
+** is only necessary if the default setting is found to be suboptimal
5869
+** for a particular application.
5870
+*/
5871
+SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
5872
+
5873
+/*
5874
+** CAPI3REF: Checkpoint a database
5875
+**
5876
+** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
5877
+** on [database connection] D to be [checkpointed]. ^If X is NULL or an
5878
+** empty string, then a checkpoint is run on all databases of
5879
+** connection D. ^If the database connection D is not in
5880
+** [WAL | write-ahead log mode] then this interface is a harmless no-op.
5881
+**
5882
+** ^The [wal_checkpoint pragma] can be used to invoke this interface
5883
+** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
5884
+** [wal_autocheckpoint pragma] can be used to cause this interface to be
5885
+** run whenever the WAL reaches a certain size threshold.
5886
+*/
5887
+SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
5888
+
57265889
/*
57275890
** Undo the hack that converts floating point types to integer for
57285891
** builds on processors without floating point support.
57295892
*/
57305893
#ifdef SQLITE_OMIT_FLOATING_POINT
57315894
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.6.23"
111 #define SQLITE_VERSION_NUMBER 3006023
112 #define SQLITE_SOURCE_ID "2010-04-15 23:24:29 f96782b389b5b97b488dc5814f7082e0393f64cd"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -391,11 +391,11 @@
391 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
394 #define SQLITE_FULL 13 /* Insertion failed because database is full */
395 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396 #define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
397 #define SQLITE_EMPTY 16 /* Database is empty */
398 #define SQLITE_SCHEMA 17 /* The database schema changed */
399 #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
400 #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
401 #define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -447,11 +447,16 @@
447 #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
448 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
449 #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
450 #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
451 #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
452 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
 
 
 
 
 
453
454 /*
455 ** CAPI3REF: Flags For File Open Operations
456 **
457 ** These bit values are intended for use in the
@@ -656,10 +661,17 @@
656 int (*xUnlock)(sqlite3_file*, int);
657 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
658 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
659 int (*xSectorSize)(sqlite3_file*);
660 int (*xDeviceCharacteristics)(sqlite3_file*);
 
 
 
 
 
 
 
661 /* Additional methods may be added in future releases */
662 };
663
664 /*
665 ** CAPI3REF: Standard File Control Opcodes
@@ -673,15 +685,23 @@
673 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
674 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
675 ** into an integer that the pArg argument points to. This capability
676 ** is used during testing and only needs to be supported when SQLITE_TEST
677 ** is defined.
 
 
 
 
 
 
 
678 */
679 #define SQLITE_FCNTL_LOCKSTATE 1
680 #define SQLITE_GET_LOCKPROXYFILE 2
681 #define SQLITE_SET_LOCKPROXYFILE 3
682 #define SQLITE_LAST_ERRNO 4
 
683
684 /*
685 ** CAPI3REF: Mutex Handle
686 **
687 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -822,11 +842,11 @@
822 ** method returns a Julian Day Number for the current date and time.
823 **
824 */
825 typedef struct sqlite3_vfs sqlite3_vfs;
826 struct sqlite3_vfs {
827 int iVersion; /* Structure version number */
828 int szOsFile; /* Size of subclassed sqlite3_file */
829 int mxPathname; /* Maximum file pathname length */
830 sqlite3_vfs *pNext; /* Next registered VFS */
831 const char *zName; /* Name of this virtual file system */
832 void *pAppData; /* Pointer to application-specific data */
@@ -841,12 +861,21 @@
841 void (*xDlClose)(sqlite3_vfs*, void*);
842 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
843 int (*xSleep)(sqlite3_vfs*, int microseconds);
844 int (*xCurrentTime)(sqlite3_vfs*, double*);
845 int (*xGetLastError)(sqlite3_vfs*, int, char *);
846 /* New fields may be appended in figure versions. The iVersion
847 ** value will increment whenever this happens. */
 
 
 
 
 
 
 
 
 
848 };
849
850 /*
851 ** CAPI3REF: Flags for the xAccess VFS method
852 **
@@ -861,10 +890,48 @@
861 ** checks whether the file is readable.
862 */
863 #define SQLITE_ACCESS_EXISTS 0
864 #define SQLITE_ACCESS_READWRITE 1
865 #define SQLITE_ACCESS_READ 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
866
867 /*
868 ** CAPI3REF: Initialize The SQLite Library
869 **
870 ** ^The sqlite3_initialize() routine initializes the
@@ -2888,10 +2955,18 @@
2888 ** already been [sqlite3_finalize | finalized] or on one that had
2889 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
2890 ** be the case that the same database connection is being used by two or
2891 ** more threads at the same moment in time.
2892 **
 
 
 
 
 
 
 
 
2893 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
2894 ** API always returns a generic error code, [SQLITE_ERROR], following any
2895 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
2896 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
2897 ** specific [error codes] that better describes the error.
@@ -4986,11 +5061,12 @@
4986 #define SQLITE_TESTCTRL_ASSERT 12
4987 #define SQLITE_TESTCTRL_ALWAYS 13
4988 #define SQLITE_TESTCTRL_RESERVE 14
4989 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
4990 #define SQLITE_TESTCTRL_ISKEYWORD 16
4991 #define SQLITE_TESTCTRL_LAST 16
 
4992
4993 /*
4994 ** CAPI3REF: SQLite Runtime Status
4995 **
4996 ** ^This interface is used to retrieve runtime status information
@@ -5450,14 +5526,18 @@
5450 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
5451 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
5452 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
5453 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
5454 **
5455 ** ^The sqlite3_backup_step() might return [SQLITE_READONLY] if the destination
5456 ** database was opened read-only or if
5457 ** the destination is an in-memory database with a different page size
5458 ** from the source database.
 
 
 
 
5459 **
5460 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
5461 ** the [sqlite3_busy_handler | busy-handler function]
5462 ** is invoked (if one is specified). ^If the
5463 ** busy-handler returns non-zero before the lock is available, then
@@ -5721,10 +5801,93 @@
5721 ** a few hundred characters, it will be truncated to the length of the
5722 ** buffer.
5723 */
5724 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
5725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5726 /*
5727 ** Undo the hack that converts floating point types to integer for
5728 ** builds on processors without floating point support.
5729 */
5730 #ifdef SQLITE_OMIT_FLOATING_POINT
5731
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.0"
111 #define SQLITE_VERSION_NUMBER 3007000
112 #define SQLITE_SOURCE_ID "2010-06-16 12:30:11 ad3209572d0e6afe5c8b52313e334509661045e2"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -391,11 +391,11 @@
391 #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
392 #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
393 #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
394 #define SQLITE_FULL 13 /* Insertion failed because database is full */
395 #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
396 #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
397 #define SQLITE_EMPTY 16 /* Database is empty */
398 #define SQLITE_SCHEMA 17 /* The database schema changed */
399 #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
400 #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
401 #define SQLITE_MISMATCH 20 /* Data type mismatch */
@@ -447,11 +447,16 @@
447 #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
448 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
449 #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
450 #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
451 #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
452 #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
453 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
454 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
455 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
456 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
457 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
458
459 /*
460 ** CAPI3REF: Flags For File Open Operations
461 **
462 ** These bit values are intended for use in the
@@ -656,10 +661,17 @@
661 int (*xUnlock)(sqlite3_file*, int);
662 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
663 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
664 int (*xSectorSize)(sqlite3_file*);
665 int (*xDeviceCharacteristics)(sqlite3_file*);
666 /* Methods above are valid for version 1 */
667 int (*xShmOpen)(sqlite3_file*);
668 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
669 int (*xShmMap)(sqlite3_file*, int iPage, int pgsz, int, void volatile**);
670 void (*xShmBarrier)(sqlite3_file*);
671 int (*xShmClose)(sqlite3_file*, int deleteFlag);
672 /* Methods above are valid for version 2 */
673 /* Additional methods may be added in future releases */
674 };
675
676 /*
677 ** CAPI3REF: Standard File Control Opcodes
@@ -673,15 +685,23 @@
685 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
686 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
687 ** into an integer that the pArg argument points to. This capability
688 ** is used during testing and only needs to be supported when SQLITE_TEST
689 ** is defined.
690 **
691 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
692 ** layer a hint of how large the database file will grow to be during the
693 ** current transaction. This hint is not guaranteed to be accurate but it
694 ** is often close. The underlying VFS might choose to preallocate database
695 ** file space based on this hint in order to help writes to the database
696 ** file run faster.
697 */
698 #define SQLITE_FCNTL_LOCKSTATE 1
699 #define SQLITE_GET_LOCKPROXYFILE 2
700 #define SQLITE_SET_LOCKPROXYFILE 3
701 #define SQLITE_LAST_ERRNO 4
702 #define SQLITE_FCNTL_SIZE_HINT 5
703
704 /*
705 ** CAPI3REF: Mutex Handle
706 **
707 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -822,11 +842,11 @@
842 ** method returns a Julian Day Number for the current date and time.
843 **
844 */
845 typedef struct sqlite3_vfs sqlite3_vfs;
846 struct sqlite3_vfs {
847 int iVersion; /* Structure version number (currently 2) */
848 int szOsFile; /* Size of subclassed sqlite3_file */
849 int mxPathname; /* Maximum file pathname length */
850 sqlite3_vfs *pNext; /* Next registered VFS */
851 const char *zName; /* Name of this virtual file system */
852 void *pAppData; /* Pointer to application-specific data */
@@ -841,12 +861,21 @@
861 void (*xDlClose)(sqlite3_vfs*, void*);
862 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
863 int (*xSleep)(sqlite3_vfs*, int microseconds);
864 int (*xCurrentTime)(sqlite3_vfs*, double*);
865 int (*xGetLastError)(sqlite3_vfs*, int, char *);
866 /*
867 ** The methods above are in version 1 of the sqlite_vfs object
868 ** definition. Those that follow are added in version 2 or later
869 */
870 int (*xRename)(sqlite3_vfs*, const char *zOld, const char *zNew, int dirSync);
871 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
872 /*
873 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
874 ** New fields may be appended in figure versions. The iVersion
875 ** value will increment whenever this happens.
876 */
877 };
878
879 /*
880 ** CAPI3REF: Flags for the xAccess VFS method
881 **
@@ -861,10 +890,48 @@
890 ** checks whether the file is readable.
891 */
892 #define SQLITE_ACCESS_EXISTS 0
893 #define SQLITE_ACCESS_READWRITE 1
894 #define SQLITE_ACCESS_READ 2
895
896 /*
897 ** CAPI3REF: Flags for the xShmLock VFS method
898 **
899 ** These integer constants define the various locking operations
900 ** allowed by the xShmLock method of [sqlite3_io_methods]. The
901 ** following are the only legal combinations of flags to the
902 ** xShmLock method:
903 **
904 ** <ul>
905 ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
906 ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
907 ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
908 ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
909 ** </ul>
910 **
911 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
912 ** was given no the corresponding lock.
913 **
914 ** The xShmLock method can transition between unlocked and SHARED or
915 ** between unlocked and EXCLUSIVE. It cannot transition between SHARED
916 ** and EXCLUSIVE.
917 */
918 #define SQLITE_SHM_UNLOCK 1
919 #define SQLITE_SHM_LOCK 2
920 #define SQLITE_SHM_SHARED 4
921 #define SQLITE_SHM_EXCLUSIVE 8
922
923 /*
924 ** CAPI3REF: Maximum xShmLock index
925 **
926 ** The xShmLock method on [sqlite3_io_methods] may use values
927 ** between 0 and this upper bound as its "offset" argument.
928 ** The SQLite core will never attempt to acquire or release a
929 ** lock outside of this range
930 */
931 #define SQLITE_SHM_NLOCK 8
932
933
934 /*
935 ** CAPI3REF: Initialize The SQLite Library
936 **
937 ** ^The sqlite3_initialize() routine initializes the
@@ -2888,10 +2955,18 @@
2955 ** already been [sqlite3_finalize | finalized] or on one that had
2956 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
2957 ** be the case that the same database connection is being used by two or
2958 ** more threads at the same moment in time.
2959 **
2960 ** For all versions of SQLite up to and including 3.6.23.1, it was required
2961 ** after sqlite3_step() returned anything other than [SQLITE_ROW] that
2962 ** [sqlite3_reset()] be called before any subsequent invocation of
2963 ** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would
2964 ** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after
2965 ** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()]
2966 ** automatically in this circumstance rather than returning [SQLITE_MISUSE].
2967 **
2968 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
2969 ** API always returns a generic error code, [SQLITE_ERROR], following any
2970 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
2971 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
2972 ** specific [error codes] that better describes the error.
@@ -4986,11 +5061,12 @@
5061 #define SQLITE_TESTCTRL_ASSERT 12
5062 #define SQLITE_TESTCTRL_ALWAYS 13
5063 #define SQLITE_TESTCTRL_RESERVE 14
5064 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
5065 #define SQLITE_TESTCTRL_ISKEYWORD 16
5066 #define SQLITE_TESTCTRL_PGHDRSZ 17
5067 #define SQLITE_TESTCTRL_LAST 17
5068
5069 /*
5070 ** CAPI3REF: SQLite Runtime Status
5071 **
5072 ** ^This interface is used to retrieve runtime status information
@@ -5450,14 +5526,18 @@
5526 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
5527 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
5528 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
5529 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
5530 **
5531 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
5532 ** <ol>
5533 ** <li> the destination database was opened read-only, or
5534 ** <li> the destination database is using write-ahead-log journaling
5535 ** and the destination and source page sizes differ, or
5536 ** <li> The destination database is an in-memory database and the
5537 ** destination and source page sizes differ.
5538 ** </ol>)^
5539 **
5540 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
5541 ** the [sqlite3_busy_handler | busy-handler function]
5542 ** is invoked (if one is specified). ^If the
5543 ** busy-handler returns non-zero before the lock is available, then
@@ -5721,10 +5801,93 @@
5801 ** a few hundred characters, it will be truncated to the length of the
5802 ** buffer.
5803 */
5804 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
5805
5806 /*
5807 ** CAPI3REF: Write-Ahead Log Commit Hook
5808 **
5809 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
5810 ** will be invoked each time a database connection commits data to a
5811 ** [write-ahead log] (i.e. whenever a transaction is committed in
5812 ** [journal_mode | journal_mode=WAL mode]).
5813 **
5814 ** ^The callback is invoked by SQLite after the commit has taken place and
5815 ** the associated write-lock on the database released, so the implementation
5816 ** may read, write or [checkpoint] the database as required.
5817 **
5818 ** ^The first parameter passed to the callback function when it is invoked
5819 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
5820 ** registering the callback. ^The second is a copy of the database handle.
5821 ** ^The third parameter is the name of the database that was written to -
5822 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
5823 ** is the number of pages currently in the write-ahead log file,
5824 ** including those that were just committed.
5825 **
5826 ** The callback function should normally return [SQLITE_OK]. ^If an error
5827 ** code is returned, that error will propagate back up through the
5828 ** SQLite code base to cause the statement that provoked the callback
5829 ** to report an error, though the commit will have still occurred. If the
5830 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
5831 ** that does not correspond to any valid SQLite error code, the results
5832 ** are undefined.
5833 **
5834 ** A single database handle may have at most a single write-ahead log callback
5835 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
5836 ** previously registered write-ahead log callback. ^Note that the
5837 ** [sqlite3_wal_autocheckpoint()] interface and the
5838 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
5839 ** those overwrite any prior [sqlite3_wal_hook()] settings.
5840 */
5841 SQLITE_API void *sqlite3_wal_hook(
5842 sqlite3*,
5843 int(*)(void *,sqlite3*,const char*,int),
5844 void*
5845 );
5846
5847 /*
5848 ** CAPI3REF: Configure an auto-checkpoint
5849 **
5850 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
5851 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
5852 ** to automatically [checkpoint]
5853 ** after committing a transaction if there are N or
5854 ** more frames in the [write-ahead log] file. ^Passing zero or
5855 ** a negative value as the nFrame parameter disables automatic
5856 ** checkpoints entirely.
5857 **
5858 ** ^The callback registered by this function replaces any existing callback
5859 ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
5860 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
5861 ** configured by this function.
5862 **
5863 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
5864 ** from SQL.
5865 **
5866 ** ^Every new [database connection] defaults to having the auto-checkpoint
5867 ** enabled with a threshold of 1000 pages. The use of this interface
5868 ** is only necessary if the default setting is found to be suboptimal
5869 ** for a particular application.
5870 */
5871 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
5872
5873 /*
5874 ** CAPI3REF: Checkpoint a database
5875 **
5876 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
5877 ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
5878 ** empty string, then a checkpoint is run on all databases of
5879 ** connection D. ^If the database connection D is not in
5880 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
5881 **
5882 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
5883 ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
5884 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
5885 ** run whenever the WAL reaches a certain size threshold.
5886 */
5887 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
5888
5889 /*
5890 ** Undo the hack that converts floating point types to integer for
5891 ** builds on processors without floating point support.
5892 */
5893 #ifdef SQLITE_OMIT_FLOATING_POINT
5894

Keyboard Shortcuts

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