Fossil SCM

Updated ttmrichter branch to latest from trunk.

michael 2010-06-28 14:30 ttmrichter merge
Commit 4c41c80e04dcd3d9242a26cdd2b4036d6288fb3d
+2 -4
--- Makefile
+++ Makefile
@@ -14,12 +14,11 @@
1414
#### C Compiler and options for use in building executables that
1515
# will run on the platform that is doing the build. This is used
1616
# to compile code-generator programs as part of the build process.
1717
# See TCC below for the C compiler for building the finished binary.
1818
#
19
-#BCC = gcc -g -O2
20
-BCC = clang -g -O2
19
+BCC = gcc -g -O2
2120
2221
#### The suffix to add to executable files. ".exe" for windows.
2322
# Nothing for unix.
2423
#
2524
E =
@@ -30,12 +29,11 @@
3029
# the finished binary for fossil. The BCC compiler above is used
3130
# for building intermediate code-generator tools.
3231
#
3332
#TCC = gcc -O6
3433
#TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
35
-#TCC = gcc -g -Os -Wall
36
-TCC = clang -g -Os -Wall
34
+TCC = gcc -g -Os -Wall
3735
3836
# To add support for HTTPS
3937
TCC += -DFOSSIL_ENABLE_SSL
4038
4139
#### Extra arguments for linking the finished binary. Fossil needs
4240
--- Makefile
+++ Makefile
@@ -14,12 +14,11 @@
14 #### C Compiler and options for use in building executables that
15 # will run on the platform that is doing the build. This is used
16 # to compile code-generator programs as part of the build process.
17 # See TCC below for the C compiler for building the finished binary.
18 #
19 #BCC = gcc -g -O2
20 BCC = clang -g -O2
21
22 #### The suffix to add to executable files. ".exe" for windows.
23 # Nothing for unix.
24 #
25 E =
@@ -30,12 +29,11 @@
30 # the finished binary for fossil. The BCC compiler above is used
31 # for building intermediate code-generator tools.
32 #
33 #TCC = gcc -O6
34 #TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
35 #TCC = gcc -g -Os -Wall
36 TCC = clang -g -Os -Wall
37
38 # To add support for HTTPS
39 TCC += -DFOSSIL_ENABLE_SSL
40
41 #### Extra arguments for linking the finished binary. Fossil needs
42
--- Makefile
+++ Makefile
@@ -14,12 +14,11 @@
14 #### C Compiler and options for use in building executables that
15 # will run on the platform that is doing the build. This is used
16 # to compile code-generator programs as part of the build process.
17 # See TCC below for the C compiler for building the finished binary.
18 #
19 BCC = gcc -g -O2
 
20
21 #### The suffix to add to executable files. ".exe" for windows.
22 # Nothing for unix.
23 #
24 E =
@@ -30,12 +29,11 @@
29 # the finished binary for fossil. The BCC compiler above is used
30 # for building intermediate code-generator tools.
31 #
32 #TCC = gcc -O6
33 #TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
34 TCC = gcc -g -Os -Wall
 
35
36 # To add support for HTTPS
37 TCC += -DFOSSIL_ENABLE_SSL
38
39 #### Extra arguments for linking the finished binary. Fossil needs
40
+3 -1
--- src/checkout.c
+++ src/checkout.c
@@ -159,18 +159,20 @@
159159
void checkout_cmd(void){
160160
int forceFlag; /* Force checkout even if edits exist */
161161
int keepFlag; /* Do not change any files on disk */
162162
int latestFlag; /* Checkout the latest version */
163163
char *zVers; /* Version to checkout */
164
+ int promptFlag; /* True to prompt before overwriting */
164165
int vid, prior;
165166
Blob cksum1, cksum1b, cksum2;
166167
167168
db_must_be_within_tree();
168169
db_begin_transaction();
169170
forceFlag = find_option("force","f",0)!=0;
170171
keepFlag = find_option("keep",0,0)!=0;
171172
latestFlag = find_option("latest",0,0)!=0;
173
+ promptFlag = find_option("prompt",0,0)!=0; /* Prompt user before overwrite */
172174
if( (latestFlag!=0 && g.argc!=2) || (latestFlag==0 && g.argc!=3) ){
173175
usage("VERSION|--latest ?--force? ?--keep?");
174176
}
175177
if( !forceFlag && unsaved_changes()==1 ){
176178
fossil_fatal("there are unsaved changes in the current checkout");
@@ -204,11 +206,11 @@
204206
if( !keepFlag ){
205207
uncheckout(prior);
206208
}
207209
db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
208210
if( !keepFlag ){
209
- vfile_to_disk(vid, 0, 1);
211
+ vfile_to_disk(vid, 0, 1, promptFlag);
210212
}
211213
manifest_to_disk(vid);
212214
db_lset_int("checkout", vid);
213215
undo_reset();
214216
db_multi_exec("DELETE FROM vmerge");
215217
--- src/checkout.c
+++ src/checkout.c
@@ -159,18 +159,20 @@
159 void checkout_cmd(void){
160 int forceFlag; /* Force checkout even if edits exist */
161 int keepFlag; /* Do not change any files on disk */
162 int latestFlag; /* Checkout the latest version */
163 char *zVers; /* Version to checkout */
 
164 int vid, prior;
165 Blob cksum1, cksum1b, cksum2;
166
167 db_must_be_within_tree();
168 db_begin_transaction();
169 forceFlag = find_option("force","f",0)!=0;
170 keepFlag = find_option("keep",0,0)!=0;
171 latestFlag = find_option("latest",0,0)!=0;
 
172 if( (latestFlag!=0 && g.argc!=2) || (latestFlag==0 && g.argc!=3) ){
173 usage("VERSION|--latest ?--force? ?--keep?");
174 }
175 if( !forceFlag && unsaved_changes()==1 ){
176 fossil_fatal("there are unsaved changes in the current checkout");
@@ -204,11 +206,11 @@
204 if( !keepFlag ){
205 uncheckout(prior);
206 }
207 db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
208 if( !keepFlag ){
209 vfile_to_disk(vid, 0, 1);
210 }
211 manifest_to_disk(vid);
212 db_lset_int("checkout", vid);
213 undo_reset();
214 db_multi_exec("DELETE FROM vmerge");
215
--- src/checkout.c
+++ src/checkout.c
@@ -159,18 +159,20 @@
159 void checkout_cmd(void){
160 int forceFlag; /* Force checkout even if edits exist */
161 int keepFlag; /* Do not change any files on disk */
162 int latestFlag; /* Checkout the latest version */
163 char *zVers; /* Version to checkout */
164 int promptFlag; /* True to prompt before overwriting */
165 int vid, prior;
166 Blob cksum1, cksum1b, cksum2;
167
168 db_must_be_within_tree();
169 db_begin_transaction();
170 forceFlag = find_option("force","f",0)!=0;
171 keepFlag = find_option("keep",0,0)!=0;
172 latestFlag = find_option("latest",0,0)!=0;
173 promptFlag = find_option("prompt",0,0)!=0; /* Prompt user before overwrite */
174 if( (latestFlag!=0 && g.argc!=2) || (latestFlag==0 && g.argc!=3) ){
175 usage("VERSION|--latest ?--force? ?--keep?");
176 }
177 if( !forceFlag && unsaved_changes()==1 ){
178 fossil_fatal("there are unsaved changes in the current checkout");
@@ -204,11 +206,11 @@
206 if( !keepFlag ){
207 uncheckout(prior);
208 }
209 db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
210 if( !keepFlag ){
211 vfile_to_disk(vid, 0, 1, promptFlag);
212 }
213 manifest_to_disk(vid);
214 db_lset_int("checkout", vid);
215 undo_reset();
216 db_multi_exec("DELETE FROM vmerge");
217
+1 -1
--- src/configure.c
+++ src/configure.c
@@ -375,11 +375,11 @@
375375
** accept the -R or --repository option to specific a repository.
376376
**
377377
** %fossil configuration export AREA FILENAME
378378
**
379379
** Write to FILENAME exported configuraton information for AREA.
380
-** AREA can be one of: all ticket skin project
380
+** AREA can be one of: all email project shun skin ticket user
381381
**
382382
** %fossil configuration import FILENAME
383383
**
384384
** Read a configuration from FILENAME, overwriting the current
385385
** configuration.
386386
--- src/configure.c
+++ src/configure.c
@@ -375,11 +375,11 @@
375 ** accept the -R or --repository option to specific a repository.
376 **
377 ** %fossil configuration export AREA FILENAME
378 **
379 ** Write to FILENAME exported configuraton information for AREA.
380 ** AREA can be one of: all ticket skin project
381 **
382 ** %fossil configuration import FILENAME
383 **
384 ** Read a configuration from FILENAME, overwriting the current
385 ** configuration.
386
--- src/configure.c
+++ src/configure.c
@@ -375,11 +375,11 @@
375 ** accept the -R or --repository option to specific a repository.
376 **
377 ** %fossil configuration export AREA FILENAME
378 **
379 ** Write to FILENAME exported configuraton information for AREA.
380 ** AREA can be one of: all email project shun skin ticket user
381 **
382 ** %fossil configuration import FILENAME
383 **
384 ** Read a configuration from FILENAME, overwriting the current
385 ** configuration.
386
+2 -2
--- src/db.c
+++ src/db.c
@@ -1410,11 +1410,11 @@
14101410
*/
14111411
void cmd_open(void){
14121412
Blob path;
14131413
int vid;
14141414
int keepFlag;
1415
- static char *azNewArgv[] = { 0, "checkout", "--latest", 0, 0, 0 };
1415
+ static char *azNewArgv[] = { 0, "checkout", "--prompt", "--latest", 0, 0 };
14161416
url_proxy_options();
14171417
keepFlag = find_option("keep",0,0)!=0;
14181418
if( g.argc!=3 && g.argc!=4 ){
14191419
usage("REPOSITORY-FILENAME ?VERSION?");
14201420
}
@@ -1435,11 +1435,11 @@
14351435
char **oldArgv = g.argv;
14361436
int oldArgc = g.argc;
14371437
db_lset_int("checkout", vid);
14381438
azNewArgv[0] = g.argv[0];
14391439
g.argv = azNewArgv;
1440
- g.argc = 3;
1440
+ g.argc = 4;
14411441
if( oldArgc==4 ){
14421442
azNewArgv[g.argc-1] = oldArgv[3];
14431443
}
14441444
if( keepFlag ){
14451445
azNewArgv[g.argc++] = "--keep";
14461446
--- src/db.c
+++ src/db.c
@@ -1410,11 +1410,11 @@
1410 */
1411 void cmd_open(void){
1412 Blob path;
1413 int vid;
1414 int keepFlag;
1415 static char *azNewArgv[] = { 0, "checkout", "--latest", 0, 0, 0 };
1416 url_proxy_options();
1417 keepFlag = find_option("keep",0,0)!=0;
1418 if( g.argc!=3 && g.argc!=4 ){
1419 usage("REPOSITORY-FILENAME ?VERSION?");
1420 }
@@ -1435,11 +1435,11 @@
1435 char **oldArgv = g.argv;
1436 int oldArgc = g.argc;
1437 db_lset_int("checkout", vid);
1438 azNewArgv[0] = g.argv[0];
1439 g.argv = azNewArgv;
1440 g.argc = 3;
1441 if( oldArgc==4 ){
1442 azNewArgv[g.argc-1] = oldArgv[3];
1443 }
1444 if( keepFlag ){
1445 azNewArgv[g.argc++] = "--keep";
1446
--- src/db.c
+++ src/db.c
@@ -1410,11 +1410,11 @@
1410 */
1411 void cmd_open(void){
1412 Blob path;
1413 int vid;
1414 int keepFlag;
1415 static char *azNewArgv[] = { 0, "checkout", "--prompt", "--latest", 0, 0 };
1416 url_proxy_options();
1417 keepFlag = find_option("keep",0,0)!=0;
1418 if( g.argc!=3 && g.argc!=4 ){
1419 usage("REPOSITORY-FILENAME ?VERSION?");
1420 }
@@ -1435,11 +1435,11 @@
1435 char **oldArgv = g.argv;
1436 int oldArgc = g.argc;
1437 db_lset_int("checkout", vid);
1438 azNewArgv[0] = g.argv[0];
1439 g.argv = azNewArgv;
1440 g.argc = 4;
1441 if( oldArgc==4 ){
1442 azNewArgv[g.argc-1] = oldArgv[3];
1443 }
1444 if( keepFlag ){
1445 azNewArgv[g.argc++] = "--keep";
1446
+1 -1
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -197,11 +197,10 @@
197197
prompt = mprintf("\nUnknown SSL certificate:\n\n%s\n\n%s\n"
198198
"Accept certificate [a=always/y/N]? ", desc, warning);
199199
BIO_free(mem);
200200
201201
Blob ans;
202
- blob_zero(&ans);
203202
prompt_user(prompt, &ans);
204203
free(prompt);
205204
if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
206205
X509_free(cert);
207206
ssl_set_errmsg("SSL certificate declined");
@@ -209,10 +208,11 @@
209208
return 1;
210209
}
211210
if( blob_str(&ans)[0]=='a' ) {
212211
ssl_save_certificate(cert);
213212
}
213
+ blob_reset(&ans);
214214
}
215215
X509_free(cert);
216216
return 0;
217217
}
218218
219219
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -197,11 +197,10 @@
197 prompt = mprintf("\nUnknown SSL certificate:\n\n%s\n\n%s\n"
198 "Accept certificate [a=always/y/N]? ", desc, warning);
199 BIO_free(mem);
200
201 Blob ans;
202 blob_zero(&ans);
203 prompt_user(prompt, &ans);
204 free(prompt);
205 if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
206 X509_free(cert);
207 ssl_set_errmsg("SSL certificate declined");
@@ -209,10 +208,11 @@
209 return 1;
210 }
211 if( blob_str(&ans)[0]=='a' ) {
212 ssl_save_certificate(cert);
213 }
 
214 }
215 X509_free(cert);
216 return 0;
217 }
218
219
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -197,11 +197,10 @@
197 prompt = mprintf("\nUnknown SSL certificate:\n\n%s\n\n%s\n"
198 "Accept certificate [a=always/y/N]? ", desc, warning);
199 BIO_free(mem);
200
201 Blob ans;
 
202 prompt_user(prompt, &ans);
203 free(prompt);
204 if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
205 X509_free(cert);
206 ssl_set_errmsg("SSL certificate declined");
@@ -209,10 +208,11 @@
208 return 1;
209 }
210 if( blob_str(&ans)[0]=='a' ) {
211 ssl_save_certificate(cert);
212 }
213 blob_reset(&ans);
214 }
215 X509_free(cert);
216 return 0;
217 }
218
219
+2 -2
--- src/merge.c
+++ src/merge.c
@@ -211,11 +211,11 @@
211211
idv = db_last_insert_rowid();
212212
db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
213213
zName = db_column_text(&q, 2);
214214
printf("ADDED %s\n", zName);
215215
undo_save(zName);
216
- vfile_to_disk(0, idm, 0);
216
+ vfile_to_disk(0, idm, 0, 0);
217217
}
218218
db_finalize(&q);
219219
220220
/*
221221
** Find files that have changed from pid->mid but not pid->vid.
@@ -234,11 +234,11 @@
234234
printf("UPDATE %s\n", zName);
235235
undo_save(zName);
236236
db_multi_exec(
237237
"UPDATE vfile SET mrid=%d, chnged=2 WHERE id=%d", ridm, idv
238238
);
239
- vfile_to_disk(0, idv, 0);
239
+ vfile_to_disk(0, idv, 0, 0);
240240
free(zName);
241241
}
242242
db_finalize(&q);
243243
244244
/*
245245
--- src/merge.c
+++ src/merge.c
@@ -211,11 +211,11 @@
211 idv = db_last_insert_rowid();
212 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
213 zName = db_column_text(&q, 2);
214 printf("ADDED %s\n", zName);
215 undo_save(zName);
216 vfile_to_disk(0, idm, 0);
217 }
218 db_finalize(&q);
219
220 /*
221 ** Find files that have changed from pid->mid but not pid->vid.
@@ -234,11 +234,11 @@
234 printf("UPDATE %s\n", zName);
235 undo_save(zName);
236 db_multi_exec(
237 "UPDATE vfile SET mrid=%d, chnged=2 WHERE id=%d", ridm, idv
238 );
239 vfile_to_disk(0, idv, 0);
240 free(zName);
241 }
242 db_finalize(&q);
243
244 /*
245
--- src/merge.c
+++ src/merge.c
@@ -211,11 +211,11 @@
211 idv = db_last_insert_rowid();
212 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
213 zName = db_column_text(&q, 2);
214 printf("ADDED %s\n", zName);
215 undo_save(zName);
216 vfile_to_disk(0, idm, 0, 0);
217 }
218 db_finalize(&q);
219
220 /*
221 ** Find files that have changed from pid->mid but not pid->vid.
@@ -234,11 +234,11 @@
234 printf("UPDATE %s\n", zName);
235 undo_save(zName);
236 db_multi_exec(
237 "UPDATE vfile SET mrid=%d, chnged=2 WHERE id=%d", ridm, idv
238 );
239 vfile_to_disk(0, idv, 0, 0);
240 free(zName);
241 }
242 db_finalize(&q);
243
244 /*
245
--- src/rebuild.c
+++ src/rebuild.c
@@ -214,10 +214,11 @@
214214
"SELECT name FROM sqlite_master"
215215
" WHERE type='table'"
216216
" AND name NOT IN ('blob','delta','rcvfrom','user',"
217217
"'config','shun','private','reportfmt',"
218218
"'concealed')"
219
+ " AND name NOT GLOB 'sqlite_*'"
219220
);
220221
if( zTable==0 ) break;
221222
db_multi_exec("DROP TABLE %Q", zTable);
222223
free(zTable);
223224
}
224225
--- src/rebuild.c
+++ src/rebuild.c
@@ -214,10 +214,11 @@
214 "SELECT name FROM sqlite_master"
215 " WHERE type='table'"
216 " AND name NOT IN ('blob','delta','rcvfrom','user',"
217 "'config','shun','private','reportfmt',"
218 "'concealed')"
 
219 );
220 if( zTable==0 ) break;
221 db_multi_exec("DROP TABLE %Q", zTable);
222 free(zTable);
223 }
224
--- src/rebuild.c
+++ src/rebuild.c
@@ -214,10 +214,11 @@
214 "SELECT name FROM sqlite_master"
215 " WHERE type='table'"
216 " AND name NOT IN ('blob','delta','rcvfrom','user',"
217 "'config','shun','private','reportfmt',"
218 "'concealed')"
219 " AND name NOT GLOB 'sqlite_*'"
220 );
221 if( zTable==0 ) break;
222 db_multi_exec("DROP TABLE %Q", zTable);
223 free(zTable);
224 }
225
+453 -266
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636636
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637637
** [sqlite_version()] and [sqlite_source_id()].
638638
*/
639639
#define SQLITE_VERSION "3.7.0"
640640
#define SQLITE_VERSION_NUMBER 3007000
641
-#define SQLITE_SOURCE_ID "2010-06-21 12:47:41 ee0acef1faffd480fd2136f81fb2b6f6a17b5388"
641
+#define SQLITE_SOURCE_ID "2010-06-26 20:25:31 f149b498b6ada3fc9f71ee104c351554c80c7f8a"
642642
643643
/*
644644
** CAPI3REF: Run-Time Library Version Numbers
645645
** KEYWORDS: sqlite3_version, sqlite3_sourceid
646646
**
@@ -7858,10 +7858,14 @@
78587858
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
78597859
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
78607860
78617861
/* Functions used to truncate the database file. */
78627862
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
7863
+
7864
+#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
7865
+SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
7866
+#endif
78637867
78647868
/* Functions to support testing and debugging. */
78657869
#if !defined(NDEBUG) || defined(SQLITE_TEST)
78667870
SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
78677871
SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
@@ -16798,15 +16802,15 @@
1679816802
SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
1679916803
void *p;
1680016804
assert( n>0 );
1680116805
1680216806
#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16803
- /* Verify that no more than one scratch allocation per thread
16807
+ /* Verify that no more than two scratch allocation per thread
1680416808
** is outstanding at one time. (This is only checked in the
1680516809
** single-threaded case since checking in the multi-threaded case
1680616810
** would be much more complicated.) */
16807
- assert( scratchAllocOut==0 );
16811
+ assert( scratchAllocOut<=1 );
1680816812
#endif
1680916813
1681016814
if( sqlite3GlobalConfig.szScratch<n ){
1681116815
goto scratch_overflow;
1681216816
}else{
@@ -16847,20 +16851,10 @@
1684716851
#endif
1684816852
return p;
1684916853
}
1685016854
SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
1685116855
if( p ){
16852
-
16853
-#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16854
- /* Verify that no more than one scratch allocation per thread
16855
- ** is outstanding at one time. (This is only checked in the
16856
- ** single-threaded case since checking in the multi-threaded case
16857
- ** would be much more complicated.) */
16858
- assert( scratchAllocOut==1 );
16859
- scratchAllocOut = 0;
16860
-#endif
16861
-
1686216856
if( sqlite3GlobalConfig.pScratch==0
1686316857
|| p<sqlite3GlobalConfig.pScratch
1686416858
|| p>=(void*)mem0.aScratchFree ){
1686516859
assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
1686616860
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
@@ -16882,10 +16876,20 @@
1688216876
sqlite3_mutex_enter(mem0.mutex);
1688316877
assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
1688416878
mem0.aScratchFree[mem0.nScratchFree++] = i;
1688516879
sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
1688616880
sqlite3_mutex_leave(mem0.mutex);
16881
+
16882
+#if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16883
+ /* Verify that no more than two scratch allocation per thread
16884
+ ** is outstanding at one time. (This is only checked in the
16885
+ ** single-threaded case since checking in the multi-threaded case
16886
+ ** would be much more complicated.) */
16887
+ assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
16888
+ scratchAllocOut = 0;
16889
+#endif
16890
+
1688716891
}
1688816892
}
1688916893
}
1689016894
1689116895
/*
@@ -25582,11 +25586,11 @@
2558225586
};
2558325587
2558425588
/*
2558525589
** Constants used for locking
2558625590
*/
25587
-#define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25591
+#define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
2558825592
#define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
2558925593
2559025594
/*
2559125595
** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
2559225596
**
@@ -30013,11 +30017,11 @@
3001330017
};
3001430018
3001530019
/*
3001630020
** Constants used for locking
3001730021
*/
30018
-#define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30022
+#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
3001930023
#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
3002030024
3002130025
/*
3002230026
** Apply advisory locks for all n bytes beginning at ofst.
3002330027
*/
@@ -30138,11 +30142,11 @@
3013830142
sqlite3_free(p);
3013930143
return SQLITE_NOMEM;
3014030144
}
3014130145
memset(pNew, 0, sizeof(*pNew));
3014230146
pNew->zFilename = (char*)&pNew[1];
30143
- sqlite3_snprintf(nName+15, pNew->zFilename, "%s-wal-index", pDbFd->zPath);
30147
+ sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
3014430148
3014530149
/* Look to see if there is an existing winShmNode that can be used.
3014630150
** If no matching winShmNode currently exists, create a new one.
3014730151
*/
3014830152
winShmEnterMutex();
@@ -31136,11 +31140,11 @@
3113631140
GetSystemTimeAsFileTime( &ft );
3113731141
#endif
3113831142
3113931143
*piNow = winFiletimeEpoch +
3114031144
((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
31141
- (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)1000;
31145
+ (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
3114231146
3114331147
#ifdef SQLITE_TEST
3114431148
if( sqlite3_current_time ){
3114531149
*piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
3114631150
}
@@ -33856,16 +33860,19 @@
3385633860
** may attempt to commit the transaction again later (calling
3385733861
** CommitPhaseOne() again). This flag is used to ensure that the
3385833862
** master journal name is only written to the journal file the first
3385933863
** time CommitPhaseOne() is called.
3386033864
**
33861
-** doNotSync
33865
+** doNotSpill, doNotSyncSpill
3386233866
**
33863
-** When enabled, cache spills are prohibited and the journal file cannot
33864
-** be synced. This variable is set and cleared by sqlite3PagerWrite()
33865
-** in order to prevent a journal sync from happening in between the
33866
-** journalling of two pages on the same sector.
33867
+** When enabled, cache spills are prohibited. The doNotSpill variable
33868
+** inhibits all cache spill and doNotSyncSpill inhibits those spills that
33869
+** would require a journal sync. The doNotSyncSpill is set and cleared
33870
+** by sqlite3PagerWrite() in order to prevent a journal sync from happening
33871
+** in between the journalling of two pages on the same sector. The
33872
+** doNotSpill value set to prevent pagerStress() from trying to use
33873
+** the journal during a rollback.
3386733874
**
3386833875
** needSync
3386933876
**
3387033877
** TODO: It might be easier to set this variable in writeJournalHdr()
3387133878
** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -33905,11 +33912,12 @@
3390533912
u8 dbModified; /* True if there are any changes to the Db */
3390633913
u8 needSync; /* True if an fsync() is needed on the journal */
3390733914
u8 journalStarted; /* True if header of journal is synced */
3390833915
u8 changeCountDone; /* Set after incrementing the change-counter */
3390933916
u8 setMaster; /* True if a m-j name has been written to jrnl */
33910
- u8 doNotSync; /* Boolean. While true, do not spill the cache */
33917
+ u8 doNotSpill; /* Do not spill the cache when non-zero */
33918
+ u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
3391133919
u8 dbSizeValid; /* Set when dbSize is correct */
3391233920
u8 subjInMemory; /* True to use in-memory sub-journals */
3391333921
Pgno dbSize; /* Number of pages in the database */
3391433922
Pgno dbOrigSize; /* dbSize before the current transaction */
3391533923
Pgno dbFileSize; /* Number of pages in the database file */
@@ -34772,13 +34780,14 @@
3477234780
static void pager_unlock(Pager *pPager){
3477334781
if( !pPager->exclusiveMode ){
3477434782
int rc = SQLITE_OK; /* Return code */
3477534783
int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
3477634784
34777
- /* Always close the journal file when dropping the database lock.
34778
- ** Otherwise, another connection with journal_mode=delete might
34779
- ** delete the file out from under us.
34785
+ /* If the operating system support deletion of open files, then
34786
+ ** close the journal file when dropping the database lock. Otherwise
34787
+ ** another connection with journal_mode=delete might delete the file
34788
+ ** out from under us.
3478034789
*/
3478134790
assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
3478234791
assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
3478334792
assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
3478434793
assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
@@ -34835,11 +34844,11 @@
3483534844
** structure, the second the error-code about to be returned by a pager
3483634845
** API function. The value returned is a copy of the second argument
3483734846
** to this function.
3483834847
**
3483934848
** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34840
-** the error becomes persistent. Until the persisten error is cleared,
34849
+** the error becomes persistent. Until the persistent error is cleared,
3484134850
** subsequent API calls on this Pager will immediately return the same
3484234851
** error code.
3484334852
**
3484434853
** A persistent error indicates that the contents of the pager-cache
3484534854
** cannot be trusted. This state can be cleared by completely discarding
@@ -35240,13 +35249,16 @@
3524035249
** the data just read from the sub-journal. Mark the page as dirty
3524135250
** and if the pager requires a journal-sync, then mark the page as
3524235251
** requiring a journal-sync before it is written.
3524335252
*/
3524435253
assert( isSavepnt );
35245
- if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1))!=SQLITE_OK ){
35246
- return rc;
35247
- }
35254
+ assert( pPager->doNotSpill==0 );
35255
+ pPager->doNotSpill++;
35256
+ rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
35257
+ assert( pPager->doNotSpill==1 );
35258
+ pPager->doNotSpill--;
35259
+ if( rc!=SQLITE_OK ) return rc;
3524835260
pPg->flags &= ~PGHDR_NEED_READ;
3524935261
sqlite3PcacheMakeDirty(pPg);
3525035262
}
3525135263
if( pPg ){
3525235264
/* No page should ever be explicitly rolled back that is in use, except
@@ -35346,10 +35358,13 @@
3534635358
int rc; /* Return code */
3534735359
sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
3534835360
sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
3534935361
char *zMasterJournal = 0; /* Contents of master journal file */
3535035362
i64 nMasterJournal; /* Size of master journal file */
35363
+ char *zJournal; /* Pointer to one journal within MJ file */
35364
+ char *zMasterPtr; /* Space to hold MJ filename from a journal file */
35365
+ int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
3535135366
3535235367
/* Allocate space for both the pJournal and pMaster file descriptors.
3535335368
** If successful, open the master journal file for reading.
3535435369
*/
3535535370
pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
@@ -35360,77 +35375,72 @@
3536035375
const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
3536135376
rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
3536235377
}
3536335378
if( rc!=SQLITE_OK ) goto delmaster_out;
3536435379
35365
- rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
35366
- if( rc!=SQLITE_OK ) goto delmaster_out;
35367
-
35368
- if( nMasterJournal>0 ){
35369
- char *zJournal;
35370
- char *zMasterPtr = 0;
35371
- int nMasterPtr = pVfs->mxPathname+1;
35372
-
35373
- /* Load the entire master journal file into space obtained from
35374
- ** sqlite3_malloc() and pointed to by zMasterJournal.
35375
- */
35376
- zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
35377
- if( !zMasterJournal ){
35378
- rc = SQLITE_NOMEM;
35379
- goto delmaster_out;
35380
- }
35381
- zMasterPtr = &zMasterJournal[nMasterJournal+1];
35382
- rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
35383
- if( rc!=SQLITE_OK ) goto delmaster_out;
35384
- zMasterJournal[nMasterJournal] = 0;
35385
-
35386
- zJournal = zMasterJournal;
35387
- while( (zJournal-zMasterJournal)<nMasterJournal ){
35388
- int exists;
35389
- rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
35390
- if( rc!=SQLITE_OK ){
35391
- goto delmaster_out;
35392
- }
35393
- if( exists ){
35394
- /* One of the journals pointed to by the master journal exists.
35395
- ** Open it and check if it points at the master journal. If
35396
- ** so, return without deleting the master journal file.
35397
- */
35398
- int c;
35399
- int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
35400
- rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
35401
- if( rc!=SQLITE_OK ){
35402
- goto delmaster_out;
35403
- }
35404
-
35405
- rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
35406
- sqlite3OsClose(pJournal);
35407
- if( rc!=SQLITE_OK ){
35408
- goto delmaster_out;
35409
- }
35410
-
35411
- c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
35412
- if( c ){
35413
- /* We have a match. Do not delete the master journal file. */
35414
- goto delmaster_out;
35415
- }
35416
- }
35417
- zJournal += (sqlite3Strlen30(zJournal)+1);
35418
- }
35419
- }
35420
-
35380
+ /* Load the entire master journal file into space obtained from
35381
+ ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
35382
+ ** sufficient space (in zMasterPtr) to hold the names of master
35383
+ ** journal files extracted from regular rollback-journals.
35384
+ */
35385
+ rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
35386
+ if( rc!=SQLITE_OK ) goto delmaster_out;
35387
+ nMasterPtr = pVfs->mxPathname+1;
35388
+ zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
35389
+ if( !zMasterJournal ){
35390
+ rc = SQLITE_NOMEM;
35391
+ goto delmaster_out;
35392
+ }
35393
+ zMasterPtr = &zMasterJournal[nMasterJournal+1];
35394
+ rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
35395
+ if( rc!=SQLITE_OK ) goto delmaster_out;
35396
+ zMasterJournal[nMasterJournal] = 0;
35397
+
35398
+ zJournal = zMasterJournal;
35399
+ while( (zJournal-zMasterJournal)<nMasterJournal ){
35400
+ int exists;
35401
+ rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
35402
+ if( rc!=SQLITE_OK ){
35403
+ goto delmaster_out;
35404
+ }
35405
+ if( exists ){
35406
+ /* One of the journals pointed to by the master journal exists.
35407
+ ** Open it and check if it points at the master journal. If
35408
+ ** so, return without deleting the master journal file.
35409
+ */
35410
+ int c;
35411
+ int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
35412
+ rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
35413
+ if( rc!=SQLITE_OK ){
35414
+ goto delmaster_out;
35415
+ }
35416
+
35417
+ rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
35418
+ sqlite3OsClose(pJournal);
35419
+ if( rc!=SQLITE_OK ){
35420
+ goto delmaster_out;
35421
+ }
35422
+
35423
+ c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
35424
+ if( c ){
35425
+ /* We have a match. Do not delete the master journal file. */
35426
+ goto delmaster_out;
35427
+ }
35428
+ }
35429
+ zJournal += (sqlite3Strlen30(zJournal)+1);
35430
+ }
35431
+
35432
+ sqlite3OsClose(pMaster);
3542135433
rc = sqlite3OsDelete(pVfs, zMaster, 0);
3542235434
3542335435
delmaster_out:
35424
- if( zMasterJournal ){
35425
- sqlite3_free(zMasterJournal);
35426
- }
35436
+ sqlite3_free(zMasterJournal);
3542735437
if( pMaster ){
3542835438
sqlite3OsClose(pMaster);
3542935439
assert( !isOpen(pJournal) );
35440
+ sqlite3_free(pMaster);
3543035441
}
35431
- sqlite3_free(pMaster);
3543235442
return rc;
3543335443
}
3543435444
3543535445
3543635446
/*
@@ -36125,11 +36135,11 @@
3612536135
for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
3612636136
rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
3612736137
}
3612836138
assert( rc!=SQLITE_DONE );
3612936139
}
36130
- assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
36140
+ assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
3613136141
3613236142
/* Finally, rollback pages from the sub-journal. Page that were
3613336143
** previously rolled back out of the main journal (and are hence in pDone)
3613436144
** will be skipped. Out-of-range pages are also skipped.
3613536145
*/
@@ -37065,10 +37075,26 @@
3706537075
Pager *pPager = (Pager *)p;
3706637076
int rc = SQLITE_OK;
3706737077
3706837078
assert( pPg->pPager==pPager );
3706937079
assert( pPg->flags&PGHDR_DIRTY );
37080
+
37081
+ /* The doNotSyncSpill flag is set during times when doing a sync of
37082
+ ** journal (and adding a new header) is not allowed. This occurs
37083
+ ** during calls to sqlite3PagerWrite() while trying to journal multiple
37084
+ ** pages belonging to the same sector.
37085
+ **
37086
+ ** The doNotSpill flag inhibits all cache spilling regardless of whether
37087
+ ** or not a sync is required. This is set during a rollback.
37088
+ **
37089
+ ** Spilling is also inhibited when in an error state.
37090
+ */
37091
+ if( pPager->errCode ) return SQLITE_OK;
37092
+ if( pPager->doNotSpill ) return SQLITE_OK;
37093
+ if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
37094
+ return SQLITE_OK;
37095
+ }
3707037096
3707137097
pPg->pDirty = 0;
3707237098
if( pagerUseWal(pPager) ){
3707337099
/* Write a single frame for this page to the log. */
3707437100
if( subjRequiresPage(pPg) ){
@@ -37076,33 +37102,16 @@
3707637102
}
3707737103
if( rc==SQLITE_OK ){
3707837104
rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
3707937105
}
3708037106
}else{
37081
- /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
37082
- ** is journalling a set of two or more database pages that are stored
37083
- ** on the same disk sector. Syncing the journal is not allowed while
37084
- ** this is happening as it is important that all members of such a
37085
- ** set of pages are synced to disk together. So, if the page this function
37086
- ** is trying to make clean will require a journal sync and the doNotSync
37087
- ** flag is set, return without doing anything. The pcache layer will
37088
- ** just have to go ahead and allocate a new page buffer instead of
37089
- ** reusing pPg.
37090
- **
37091
- ** Similarly, if the pager has already entered the error state, do not
37092
- ** try to write the contents of pPg to disk.
37093
- */
37094
- if( NEVER(pPager->errCode)
37095
- || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37096
- ){
37097
- return SQLITE_OK;
37098
- }
3709937107
3710037108
/* Sync the journal file if required. */
3710137109
if( pPg->flags&PGHDR_NEED_SYNC ){
37110
+ assert( !pPager->noSync );
3710237111
rc = syncJournal(pPager);
37103
- if( rc==SQLITE_OK && pPager->fullSync &&
37112
+ if( rc==SQLITE_OK &&
3710437113
!(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
3710537114
!(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
3710637115
){
3710737116
pPager->nRec = 0;
3710837117
rc = writeJournalHdr(pPager);
@@ -38256,11 +38265,11 @@
3825638265
*/
3825738266
rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
3825838267
if( rc!=SQLITE_OK ){
3825938268
return rc;
3826038269
}
38261
- if( !isOpen(pPager->jfd)
38270
+ if( pPager->pInJournal==0
3826238271
&& pPager->journalMode!=PAGER_JOURNALMODE_OFF
3826338272
&& !pagerUseWal(pPager)
3826438273
){
3826538274
assert( pPager->useJournal );
3826638275
rc = pager_open_journal(pPager);
@@ -38384,39 +38393,41 @@
3838438393
Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
3838538394
3838638395
if( nPagePerSector>1 ){
3838738396
Pgno nPageCount; /* Total number of pages in database file */
3838838397
Pgno pg1; /* First page of the sector pPg is located on. */
38389
- int nPage; /* Number of pages starting at pg1 to journal */
38398
+ int nPage = 0; /* Number of pages starting at pg1 to journal */
3839038399
int ii; /* Loop counter */
3839138400
int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
3839238401
38393
- /* Set the doNotSync flag to 1. This is because we cannot allow a journal
38394
- ** header to be written between the pages journaled by this function.
38402
+ /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
38403
+ ** a journal header to be written between the pages journaled by
38404
+ ** this function.
3839538405
*/
3839638406
assert( !MEMDB );
38397
- assert( pPager->doNotSync==0 );
38398
- pPager->doNotSync = 1;
38407
+ assert( pPager->doNotSyncSpill==0 );
38408
+ pPager->doNotSyncSpill++;
3839938409
3840038410
/* This trick assumes that both the page-size and sector-size are
3840138411
** an integer power of 2. It sets variable pg1 to the identifier
3840238412
** of the first page of the sector pPg is located on.
3840338413
*/
3840438414
pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
3840538415
3840638416
rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount);
38407
- if( rc ) return rc;
38408
- if( pPg->pgno>nPageCount ){
38409
- nPage = (pPg->pgno - pg1)+1;
38410
- }else if( (pg1+nPagePerSector-1)>nPageCount ){
38411
- nPage = nPageCount+1-pg1;
38412
- }else{
38413
- nPage = nPagePerSector;
38414
- }
38415
- assert(nPage>0);
38416
- assert(pg1<=pPg->pgno);
38417
- assert((pg1+nPage)>pPg->pgno);
38417
+ if( rc==SQLITE_OK ){
38418
+ if( pPg->pgno>nPageCount ){
38419
+ nPage = (pPg->pgno - pg1)+1;
38420
+ }else if( (pg1+nPagePerSector-1)>nPageCount ){
38421
+ nPage = nPageCount+1-pg1;
38422
+ }else{
38423
+ nPage = nPagePerSector;
38424
+ }
38425
+ assert(nPage>0);
38426
+ assert(pg1<=pPg->pgno);
38427
+ assert((pg1+nPage)>pPg->pgno);
38428
+ }
3841838429
3841938430
for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
3842038431
Pgno pg = pg1+ii;
3842138432
PgHdr *pPage;
3842238433
if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38455,12 +38466,12 @@
3845538466
}
3845638467
}
3845738468
assert(pPager->needSync);
3845838469
}
3845938470
38460
- assert( pPager->doNotSync==1 );
38461
- pPager->doNotSync = 0;
38471
+ assert( pPager->doNotSyncSpill==1 );
38472
+ pPager->doNotSyncSpill--;
3846238473
}else{
3846338474
rc = pager_write(pDbPage);
3846438475
}
3846538476
return rc;
3846638477
}
@@ -38571,13 +38582,16 @@
3857138582
put32bits(((char*)pPgHdr->pData)+92, change_counter);
3857238583
put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
3857338584
3857438585
/* If running in direct mode, write the contents of page 1 to the file. */
3857538586
if( DIRECT_MODE ){
38576
- const void *zBuf = pPgHdr->pData;
38587
+ const void *zBuf;
3857738588
assert( pPager->dbFileSize>0 );
38578
- rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
38589
+ CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
38590
+ if( rc==SQLITE_OK ){
38591
+ rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
38592
+ }
3857938593
if( rc==SQLITE_OK ){
3858038594
pPager->changeCountDone = 1;
3858138595
}
3858238596
}else{
3858338597
pPager->changeCountDone = 1;
@@ -38643,11 +38657,11 @@
3864338657
3864438658
/* The dbOrigSize is never set if journal_mode=OFF */
3864538659
assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
3864638660
3864738661
/* If a prior error occurred, report that error again. */
38648
- if( NEVER(pPager->errCode) ) return pPager->errCode;
38662
+ if( pPager->errCode ) return pPager->errCode;
3864938663
3865038664
PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
3865138665
pPager->zFilename, zMaster, pPager->dbSize));
3865238666
3865338667
if( MEMDB && pPager->dbModified ){
@@ -38821,14 +38835,15 @@
3882138835
** But if (due to a coding error elsewhere in the system) it does get
3882238836
** called, just return the same error code without doing anything. */
3882338837
if( NEVER(pPager->errCode) ) return pPager->errCode;
3882438838
3882538839
/* This function should not be called if the pager is not in at least
38826
- ** PAGER_RESERVED state. And indeed SQLite never does this. But it is
38827
- ** nice to have this defensive test here anyway.
38840
+ ** PAGER_RESERVED state. **FIXME**: Make it so that this test always
38841
+ ** fails - make it so that we never reach this point if we do not hold
38842
+ ** all necessary locks.
3882838843
*/
38829
- if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
38844
+ if( pPager->state<PAGER_RESERVED ) return SQLITE_ERROR;
3883038845
3883138846
/* An optimization. If the database was not actually modified during
3883238847
** this transaction, the pager is running in exclusive-mode and is
3883338848
** using persistent journals, then this function is a no-op.
3883438849
**
@@ -38873,11 +38888,11 @@
3887338888
** performed. If successful, task (2). Regardless of the outcome
3887438889
** of either, the error state error code is returned to the caller
3887538890
** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
3887638891
**
3887738892
** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38878
-** or not (1) is succussful, also attempt (2). If successful, return
38893
+** or not (1) is successful, also attempt (2). If successful, return
3887938894
** SQLITE_OK. Otherwise, enter the error state and return the first
3888038895
** error code encountered.
3888138896
**
3888238897
** In this case there is no chance that the database was written to.
3888338898
** So is safe to finalize the journal file even if the playback
@@ -39647,11 +39662,27 @@
3964739662
sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
3964839663
}
3964939664
}
3965039665
return rc;
3965139666
}
39652
-#endif
39667
+
39668
+#ifdef SQLITE_HAS_CODEC
39669
+/*
39670
+** This function is called by the wal module when writing page content
39671
+** into the log file.
39672
+**
39673
+** This function returns a pointer to a buffer containing the encrypted
39674
+** page content. If a malloc fails, this function may return NULL.
39675
+*/
39676
+SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
39677
+ void *aData = 0;
39678
+ CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
39679
+ return aData;
39680
+}
39681
+#endif /* SQLITE_HAS_CODEC */
39682
+
39683
+#endif /* !SQLITE_OMIT_WAL */
3965339684
3965439685
#endif /* SQLITE_OMIT_DISKIO */
3965539686
3965639687
/************** End of pager.c ***********************************************/
3965739688
/************** Begin file wal.c *********************************************/
@@ -39686,23 +39717,25 @@
3968639717
** frames can overwrite the old ones. A WAL always grows from beginning
3968739718
** toward the end. Checksums and counters attached to each frame are
3968839719
** used to determine which frames within the WAL are valid and which
3968939720
** are leftovers from prior checkpoints.
3969039721
**
39691
-** The WAL header is 24 bytes in size and consists of the following six
39722
+** The WAL header is 32 bytes in size and consists of the following eight
3969239723
** big-endian 32-bit unsigned integer values:
3969339724
**
3969439725
** 0: Magic number. 0x377f0682 or 0x377f0683
3969539726
** 4: File format version. Currently 3007000
3969639727
** 8: Database page size. Example: 1024
3969739728
** 12: Checkpoint sequence number
3969839729
** 16: Salt-1, random integer incremented with each checkpoint
3969939730
** 20: Salt-2, a different random integer changing with each ckpt
39731
+** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
39732
+** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
3970039733
**
3970139734
** Immediately following the wal-header are zero or more frames. Each
3970239735
** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39703
-** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39736
+** of page data. The frame-header is six big-endian 32-bit unsigned
3970439737
** integer values, as follows:
3970539738
**
3970639739
** 0: Page number.
3970739740
** 4: For commit records, the size of the database image in pages
3970839741
** after the commit. For all other records, zero.
@@ -39733,10 +39766,15 @@
3973339766
**
3973439767
** for i from 0 to n-1 step 2:
3973539768
** s0 += x[i] + s1;
3973639769
** s1 += x[i+1] + s0;
3973739770
** endfor
39771
+**
39772
+** Note that s0 and s1 are both weighted checksums using fibonacci weights
39773
+** in reverse order (the largest fibonacci weight occurs on the first element
39774
+** of the sequence being summed.) The s1 value spans all 32-bit
39775
+** terms of the sequence whereas s0 omits the final term.
3973839776
**
3973939777
** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
3974039778
** WAL is transferred into the database, then the database is VFS.xSync-ed.
3974139779
** The VFS.xSync operations serve as write barriers - all writes launched
3974239780
** before the xSync must complete before any write that launches after the
@@ -39900,10 +39938,25 @@
3990039938
# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
3990139939
#else
3990239940
# define WALTRACE(X)
3990339941
#endif
3990439942
39943
+/*
39944
+** The maximum (and only) versions of the wal and wal-index formats
39945
+** that may be interpreted by this version of SQLite.
39946
+**
39947
+** If a client begins recovering a WAL file and finds that (a) the checksum
39948
+** values in the wal-header are correct and (b) the version field is not
39949
+** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
39950
+**
39951
+** Similarly, if a client successfully reads a wal-index header (i.e. the
39952
+** checksum test is successful) and finds that the version field is not
39953
+** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
39954
+** returns SQLITE_CANTOPEN.
39955
+*/
39956
+#define WAL_MAX_VERSION 3007000
39957
+#define WALINDEX_MAX_VERSION 3007000
3990539958
3990639959
/*
3990739960
** Indices of various locking bytes. WAL_NREADER is the number
3990839961
** of available reader locks and should be at least 3.
3990939962
*/
@@ -39926,10 +39979,12 @@
3992639979
**
3992739980
** The actual header in the wal-index consists of two copies of this
3992839981
** object.
3992939982
*/
3993039983
struct WalIndexHdr {
39984
+ u32 iVersion; /* Wal-index version */
39985
+ u32 unused; /* Unused (padding) field */
3993139986
u32 iChange; /* Counter incremented each transaction */
3993239987
u8 isInit; /* 1 when initialized */
3993339988
u8 bigEndCksum; /* True if checksums in WAL are big-endian */
3993439989
u16 szPage; /* Database page size in bytes */
3993539990
u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40005,12 +40060,13 @@
4000540060
#define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
4000640061
4000740062
/* Size of header before each frame in wal */
4000840063
#define WAL_FRAME_HDRSIZE 24
4000940064
40010
-/* Size of write ahead log header */
40011
-#define WAL_HDRSIZE 24
40065
+/* Size of write ahead log header, including checksum. */
40066
+/* #define WAL_HDRSIZE 24 */
40067
+#define WAL_HDRSIZE 32
4001240068
4001340069
/* WAL magic value. Either this value, or the same value with the least
4001440070
** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
4001540071
** big-endian format in the first 4 bytes of a WAL file.
4001640072
**
@@ -40234,10 +40290,11 @@
4023440290
volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
4023540291
const int nCksum = offsetof(WalIndexHdr, aCksum);
4023640292
4023740293
assert( pWal->writeLock );
4023840294
pWal->hdr.isInit = 1;
40295
+ pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
4023940296
walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
4024040297
memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
4024140298
sqlite3OsShmBarrier(pWal->pDbFd);
4024240299
memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
4024340300
}
@@ -40683,10 +40740,11 @@
4068340740
u8 *aData; /* Pointer to data part of aFrame buffer */
4068440741
int iFrame; /* Index of last frame read */
4068540742
i64 iOffset; /* Next offset to read from log file */
4068640743
int szPage; /* Page size according to the log */
4068740744
u32 magic; /* Magic value read from WAL header */
40745
+ u32 version; /* Magic value read from WAL header */
4068840746
4068940747
/* Read in the WAL header. */
4069040748
rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
4069140749
if( rc!=SQLITE_OK ){
4069240750
goto recovery_error;
@@ -40708,13 +40766,28 @@
4070840766
}
4070940767
pWal->hdr.bigEndCksum = (magic&0x00000001);
4071040768
pWal->szPage = szPage;
4071140769
pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
4071240770
memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40771
+
40772
+ /* Verify that the WAL header checksum is correct */
4071340773
walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40714
- aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40774
+ aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
4071540775
);
40776
+ if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
40777
+ || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
40778
+ ){
40779
+ goto finished;
40780
+ }
40781
+
40782
+ /* Verify that the version number on the WAL format is one that
40783
+ ** are able to understand */
40784
+ version = sqlite3Get4byte(&aBuf[4]);
40785
+ if( version!=WAL_MAX_VERSION ){
40786
+ rc = SQLITE_CANTOPEN_BKPT;
40787
+ goto finished;
40788
+ }
4071640789
4071740790
/* Malloc a buffer to read frames into. */
4071840791
szFrame = szPage + WAL_FRAME_HDRSIZE;
4071940792
aFrame = (u8 *)sqlite3_malloc(szFrame);
4072040793
if( !aFrame ){
@@ -40899,53 +40972,101 @@
4089940972
4090040973
*piPage = p->iPrior = iRet;
4090140974
return (iRet==0xFFFFFFFF);
4090240975
}
4090340976
40977
+/*
40978
+** This function merges two sorted lists into a single sorted list.
40979
+*/
40980
+static void walMerge(
40981
+ u32 *aContent, /* Pages in wal */
40982
+ ht_slot *aLeft, /* IN: Left hand input list */
40983
+ int nLeft, /* IN: Elements in array *paLeft */
40984
+ ht_slot **paRight, /* IN/OUT: Right hand input list */
40985
+ int *pnRight, /* IN/OUT: Elements in *paRight */
40986
+ ht_slot *aTmp /* Temporary buffer */
40987
+){
40988
+ int iLeft = 0; /* Current index in aLeft */
40989
+ int iRight = 0; /* Current index in aRight */
40990
+ int iOut = 0; /* Current index in output buffer */
40991
+ int nRight = *pnRight;
40992
+ ht_slot *aRight = *paRight;
4090440993
40994
+ assert( nLeft>0 && nRight>0 );
40995
+ while( iRight<nRight || iLeft<nLeft ){
40996
+ ht_slot logpage;
40997
+ Pgno dbpage;
40998
+
40999
+ if( (iLeft<nLeft)
41000
+ && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
41001
+ ){
41002
+ logpage = aLeft[iLeft++];
41003
+ }else{
41004
+ logpage = aRight[iRight++];
41005
+ }
41006
+ dbpage = aContent[logpage];
41007
+
41008
+ aTmp[iOut++] = logpage;
41009
+ if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
41010
+
41011
+ assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
41012
+ assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
41013
+ }
41014
+
41015
+ *paRight = aLeft;
41016
+ *pnRight = iOut;
41017
+ memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
41018
+}
41019
+
41020
+/*
41021
+** Sort the elements in list aList, removing any duplicates.
41022
+*/
4090541023
static void walMergesort(
4090641024
u32 *aContent, /* Pages in wal */
4090741025
ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
4090841026
ht_slot *aList, /* IN/OUT: List to sort */
4090941027
int *pnList /* IN/OUT: Number of elements in aList[] */
4091041028
){
40911
- int nList = *pnList;
40912
- if( nList>1 ){
40913
- int nLeft = nList / 2; /* Elements in left list */
40914
- int nRight = nList - nLeft; /* Elements in right list */
40915
- int iLeft = 0; /* Current index in aLeft */
40916
- int iRight = 0; /* Current index in aright */
40917
- int iOut = 0; /* Current index in output buffer */
40918
- ht_slot *aLeft = aList; /* Left list */
40919
- ht_slot *aRight = aList+nLeft;/* Right list */
40920
-
40921
- /* TODO: Change to non-recursive version. */
40922
- walMergesort(aContent, aBuffer, aLeft, &nLeft);
40923
- walMergesort(aContent, aBuffer, aRight, &nRight);
40924
-
40925
- while( iRight<nRight || iLeft<nLeft ){
40926
- ht_slot logpage;
40927
- Pgno dbpage;
40928
-
40929
- if( (iLeft<nLeft)
40930
- && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
40931
- ){
40932
- logpage = aLeft[iLeft++];
40933
- }else{
40934
- logpage = aRight[iRight++];
40935
- }
40936
- dbpage = aContent[logpage];
40937
-
40938
- aBuffer[iOut++] = logpage;
40939
- if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
40940
-
40941
- assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
40942
- assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
40943
- }
40944
- memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
40945
- *pnList = iOut;
40946
- }
41029
+ struct Sublist {
41030
+ int nList; /* Number of elements in aList */
41031
+ ht_slot *aList; /* Pointer to sub-list content */
41032
+ };
41033
+
41034
+ const int nList = *pnList; /* Size of input list */
41035
+ int nMerge; /* Number of elements in list aMerge */
41036
+ ht_slot *aMerge; /* List to be merged */
41037
+ int iList; /* Index into input list */
41038
+ int iSub = 0; /* Index into aSub array */
41039
+ struct Sublist aSub[13]; /* Array of sub-lists */
41040
+
41041
+ memset(aSub, 0, sizeof(aSub));
41042
+ assert( nList<=HASHTABLE_NPAGE && nList>0 );
41043
+ assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
41044
+
41045
+ for(iList=0; iList<nList; iList++){
41046
+ nMerge = 1;
41047
+ aMerge = &aList[iList];
41048
+ for(iSub=0; iList & (1<<iSub); iSub++){
41049
+ struct Sublist *p = &aSub[iSub];
41050
+ assert( p->aList && p->nList<=(1<<iSub) );
41051
+ assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
41052
+ walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
41053
+ }
41054
+ aSub[iSub].aList = aMerge;
41055
+ aSub[iSub].nList = nMerge;
41056
+ }
41057
+
41058
+ for(iSub++; iSub<ArraySize(aSub); iSub++){
41059
+ if( nList & (1<<iSub) ){
41060
+ struct Sublist *p = &aSub[iSub];
41061
+ assert( p->nList<=(1<<iSub) );
41062
+ assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
41063
+ walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
41064
+ }
41065
+ }
41066
+ assert( aMerge==aList );
41067
+ *pnList = nMerge;
4094741068
4094841069
#ifdef SQLITE_DEBUG
4094941070
{
4095041071
int i;
4095141072
for(i=1; i<*pnList; i++){
@@ -40957,91 +41078,94 @@
4095741078
4095841079
/*
4095941080
** Free an iterator allocated by walIteratorInit().
4096041081
*/
4096141082
static void walIteratorFree(WalIterator *p){
40962
- sqlite3_free(p);
41083
+ sqlite3ScratchFree(p);
4096341084
}
4096441085
4096541086
/*
40966
-** Map the wal-index into memory owned by this thread, if it is not
40967
-** mapped already. Then construct a WalInterator object that can be
40968
-** used to loop over all pages in the WAL in ascending order.
41087
+** Construct a WalInterator object that can be used to loop over all
41088
+** pages in the WAL in ascending order. The caller must hold the checkpoint
4096941089
**
4097041090
** On success, make *pp point to the newly allocated WalInterator object
40971
-** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error
40972
-** code.
41091
+** return SQLITE_OK. Otherwise, return an error code. If this routine
41092
+** returns an error, the value of *pp is undefined.
4097341093
**
4097441094
** The calling routine should invoke walIteratorFree() to destroy the
40975
-** WalIterator object when it has finished with it. The caller must
40976
-** also unmap the wal-index. But the wal-index must not be unmapped
40977
-** prior to the WalIterator object being destroyed.
41095
+** WalIterator object when it has finished with it.
4097841096
*/
4097941097
static int walIteratorInit(Wal *pWal, WalIterator **pp){
4098041098
WalIterator *p; /* Return value */
4098141099
int nSegment; /* Number of segments to merge */
4098241100
u32 iLast; /* Last frame in log */
4098341101
int nByte; /* Number of bytes to allocate */
4098441102
int i; /* Iterator variable */
4098541103
ht_slot *aTmp; /* Temp space used by merge-sort */
40986
- ht_slot *aSpace; /* Space at the end of the allocation */
40987
-
40988
- /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other
40989
- ** thread is able to write to shared memory while this routine is
40990
- ** running (or, indeed, while the WalIterator object exists). Hence,
40991
- ** we can cast off the volatile qualification from shared memory
40992
- */
40993
- assert( pWal->ckptLock );
41104
+ int rc = SQLITE_OK; /* Return Code */
41105
+
41106
+ /* This routine only runs while holding the checkpoint lock. And
41107
+ ** it only runs if there is actually content in the log (mxFrame>0).
41108
+ */
41109
+ assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
4099441110
iLast = pWal->hdr.mxFrame;
4099541111
40996
- /* Allocate space for the WalIterator object */
41112
+ /* Allocate space for the WalIterator object. */
4099741113
nSegment = walFramePage(iLast) + 1;
4099841114
nByte = sizeof(WalIterator)
40999
- + nSegment*(sizeof(struct WalSegment))
41000
- + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot));
41001
- p = (WalIterator *)sqlite3_malloc(nByte);
41115
+ + (nSegment-1)*sizeof(struct WalSegment)
41116
+ + iLast*sizeof(ht_slot);
41117
+ p = (WalIterator *)sqlite3ScratchMalloc(nByte);
4100241118
if( !p ){
4100341119
return SQLITE_NOMEM;
4100441120
}
4100541121
memset(p, 0, nByte);
41006
-
41007
- /* Allocate space for the WalIterator object */
4100841122
p->nSegment = nSegment;
41009
- aSpace = (ht_slot *)&p->aSegment[nSegment];
41010
- aTmp = &aSpace[HASHTABLE_NPAGE*nSegment];
41011
- for(i=0; i<nSegment; i++){
41123
+
41124
+ /* Allocate temporary space used by the merge-sort routine. This block
41125
+ ** of memory will be freed before this function returns.
41126
+ */
41127
+ aTmp = (ht_slot *)sqlite3ScratchMalloc(
41128
+ sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
41129
+ );
41130
+ if( !aTmp ){
41131
+ rc = SQLITE_NOMEM;
41132
+ }
41133
+
41134
+ for(i=0; rc==SQLITE_OK && i<nSegment; i++){
4101241135
volatile ht_slot *aHash;
41013
- int j;
4101441136
u32 iZero;
41015
- int nEntry;
4101641137
volatile u32 *aPgno;
41017
- int rc;
4101841138
4101941139
rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
41020
- if( rc!=SQLITE_OK ){
41021
- walIteratorFree(p);
41022
- return rc;
41023
- }
41024
- aPgno++;
41025
- nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
41026
- iZero++;
41027
-
41028
- for(j=0; j<nEntry; j++){
41029
- aSpace[j] = j;
41030
- }
41031
- walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry);
41032
- p->aSegment[i].iZero = iZero;
41033
- p->aSegment[i].nEntry = nEntry;
41034
- p->aSegment[i].aIndex = aSpace;
41035
- p->aSegment[i].aPgno = (u32 *)aPgno;
41036
- aSpace += HASHTABLE_NPAGE;
41037
- }
41038
- assert( aSpace==aTmp );
41039
-
41040
- /* Return the fully initialized WalIterator object */
41140
+ if( rc==SQLITE_OK ){
41141
+ int j; /* Counter variable */
41142
+ int nEntry; /* Number of entries in this segment */
41143
+ ht_slot *aIndex; /* Sorted index for this segment */
41144
+
41145
+ aPgno++;
41146
+ nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
41147
+ aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
41148
+ iZero++;
41149
+
41150
+ for(j=0; j<nEntry; j++){
41151
+ aIndex[j] = j;
41152
+ }
41153
+ walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
41154
+ p->aSegment[i].iZero = iZero;
41155
+ p->aSegment[i].nEntry = nEntry;
41156
+ p->aSegment[i].aIndex = aIndex;
41157
+ p->aSegment[i].aPgno = (u32 *)aPgno;
41158
+ }
41159
+ }
41160
+ sqlite3ScratchFree(aTmp);
41161
+
41162
+ if( rc!=SQLITE_OK ){
41163
+ walIteratorFree(p);
41164
+ }
4104141165
*pp = p;
41042
- return SQLITE_OK ;
41166
+ return rc;
4104341167
}
4104441168
4104541169
/*
4104641170
** Copy as much content as we can from the WAL back into the database file
4104741171
** in response to an sqlite3_wal_checkpoint() request or the equivalent.
@@ -41086,15 +41210,18 @@
4108641210
u32 iFrame = 0; /* Wal frame containing data for iDbpage */
4108741211
u32 mxSafeFrame; /* Max frame that can be backfilled */
4108841212
int i; /* Loop counter */
4108941213
volatile WalCkptInfo *pInfo; /* The checkpoint status information */
4109041214
41215
+ if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
41216
+
4109141217
/* Allocate the iterator */
4109241218
rc = walIteratorInit(pWal, &pIter);
41093
- if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){
41094
- goto walcheckpoint_out;
41219
+ if( rc!=SQLITE_OK ){
41220
+ return rc;
4109541221
}
41222
+ assert( pIter );
4109641223
4109741224
/*** TODO: Move this test out to the caller. Make it an assert() here ***/
4109841225
if( pWal->hdr.szPage!=nBuf ){
4109941226
rc = SQLITE_CORRUPT_BKPT;
4110041227
goto walcheckpoint_out;
@@ -41275,30 +41402,24 @@
4127541402
return 0;
4127641403
}
4127741404
4127841405
/*
4127941406
** Read the wal-index header from the wal-index and into pWal->hdr.
41280
-** If the wal-header appears to be corrupt, try to recover the log
41281
-** before returning.
41407
+** If the wal-header appears to be corrupt, try to reconstruct the
41408
+** wal-index from the WAL before returning.
4128241409
**
4128341410
** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
4128441411
** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
4128541412
** to 0.
4128641413
**
41287
-** This routine also maps the wal-index content into memory and assigns
41288
-** ownership of that mapping to the current thread. In some implementations,
41289
-** only one thread at a time can hold a mapping of the wal-index. Hence,
41290
-** the caller should strive to invoke walIndexUnmap() as soon as possible
41291
-** after this routine returns.
41292
-**
4129341414
** If the wal-index header is successfully read, return SQLITE_OK.
4129441415
** Otherwise an SQLite error code.
4129541416
*/
4129641417
static int walIndexReadHdr(Wal *pWal, int *pChanged){
4129741418
int rc; /* Return code */
4129841419
int badHdr; /* True if a header read failed */
41299
- volatile u32 *page0;
41420
+ volatile u32 *page0; /* Chunk of wal-index containing header */
4130041421
4130141422
/* Ensure that page 0 of the wal-index (the page that contains the
4130241423
** wal-index header) is mapped. Return early if an error occurs here.
4130341424
*/
4130441425
assert( pChanged );
@@ -41309,11 +41430,11 @@
4130941430
assert( page0 || pWal->writeLock==0 );
4131041431
4131141432
/* If the first page of the wal-index has been mapped, try to read the
4131241433
** wal-index header immediately, without holding any lock. This usually
4131341434
** works, but may fail if the wal-index header is corrupt or currently
41314
- ** being modified by another user.
41435
+ ** being modified by another thread or process.
4131541436
*/
4131641437
badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
4131741438
4131841439
/* If the first attempt failed, it might have been due to a race
4131941440
** with a writer. So get a WRITE lock and try again.
@@ -41333,10 +41454,18 @@
4133341454
}
4133441455
}
4133541456
pWal->writeLock = 0;
4133641457
walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
4133741458
}
41459
+
41460
+ /* If the header is read successfully, check the version number to make
41461
+ ** sure the wal-index was not constructed with some future format that
41462
+ ** this version of SQLite cannot understand.
41463
+ */
41464
+ if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
41465
+ rc = SQLITE_CANTOPEN_BKPT;
41466
+ }
4133841467
4133941468
return rc;
4134041469
}
4134141470
4134241471
/*
@@ -41348,13 +41477,33 @@
4134841477
/*
4134941478
** Attempt to start a read transaction. This might fail due to a race or
4135041479
** other transient condition. When that happens, it returns WAL_RETRY to
4135141480
** indicate to the caller that it is safe to retry immediately.
4135241481
**
41353
-** On success return SQLITE_OK. On a permantent failure (such an
41482
+** On success return SQLITE_OK. On a permanent failure (such an
4135441483
** I/O error or an SQLITE_BUSY because another process is running
4135541484
** recovery) return a positive error code.
41485
+**
41486
+** The useWal parameter is true to force the use of the WAL and disable
41487
+** the case where the WAL is bypassed because it has been completely
41488
+** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
41489
+** to make a copy of the wal-index header into pWal->hdr. If the
41490
+** wal-index header has changed, *pChanged is set to 1 (as an indication
41491
+** to the caller that the local paget cache is obsolete and needs to be
41492
+** flushed.) When useWal==1, the wal-index header is assumed to already
41493
+** be loaded and the pChanged parameter is unused.
41494
+**
41495
+** The caller must set the cnt parameter to the number of prior calls to
41496
+** this routine during the current read attempt that returned WAL_RETRY.
41497
+** This routine will start taking more aggressive measures to clear the
41498
+** race conditions after multiple WAL_RETRY returns, and after an excessive
41499
+** number of errors will ultimately return SQLITE_PROTOCOL. The
41500
+** SQLITE_PROTOCOL return indicates that some other process has gone rogue
41501
+** and is not honoring the locking protocol. There is a vanishingly small
41502
+** chance that SQLITE_PROTOCOL could be returned because of a run of really
41503
+** bad luck when there is lots of contention for the wal-index, but that
41504
+** possibility is so small that it can be safely neglected, we believe.
4135641505
**
4135741506
** On success, this routine obtains a read lock on
4135841507
** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
4135941508
** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
4136041509
** that means the Wal does not hold any read lock. The reader must not
@@ -41361,10 +41510,12 @@
4136141510
** access any database page that is modified by a WAL frame up to and
4136241511
** including frame number aReadMark[pWal->readLock]. The reader will
4136341512
** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
4136441513
** Or if pWal->readLock==0, then the reader will ignore the WAL
4136541514
** completely and get all content directly from the database file.
41515
+** If the useWal parameter is 1 then the WAL will never be ignored and
41516
+** this routine will always set pWal->readLock>0 on success.
4136641517
** When the read transaction is completed, the caller must release the
4136741518
** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
4136841519
**
4136941520
** This routine uses the nBackfill and aReadMark[] fields of the header
4137041521
** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41405,13 +41556,13 @@
4140541556
rc = WAL_RETRY;
4140641557
}else if( rc==SQLITE_BUSY ){
4140741558
rc = SQLITE_BUSY_RECOVERY;
4140841559
}
4140941560
}
41410
- }
41411
- if( rc!=SQLITE_OK ){
41412
- return rc;
41561
+ if( rc!=SQLITE_OK ){
41562
+ return rc;
41563
+ }
4141341564
}
4141441565
4141541566
pInfo = walCkptInfo(pWal);
4141641567
if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
4141741568
/* The WAL has been completely backfilled (or it is empty).
@@ -41583,13 +41734,13 @@
4158341734
/* This routine is only be called from within a read transaction. */
4158441735
assert( pWal->readLock>=0 || pWal->lockError );
4158541736
4158641737
/* If the "last page" field of the wal-index header snapshot is 0, then
4158741738
** no data will be read from the wal under any circumstances. Return early
41588
- ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41589
- ** pWal->readLock==0, then the WAL is ignored by the reader so
41590
- ** return early, as if the WAL were empty.
41739
+ ** in this case as an optimization. Likewise, if pWal->readLock==0,
41740
+ ** then the WAL is ignored by the reader so return early, as if the
41741
+ ** WAL were empty.
4159141742
*/
4159241743
if( iLast==0 || pWal->readLock==0 ){
4159341744
*pInWal = 0;
4159441745
return SQLITE_OK;
4159541746
}
@@ -41596,11 +41747,11 @@
4159641747
4159741748
/* Search the hash table or tables for an entry matching page number
4159841749
** pgno. Each iteration of the following for() loop searches one
4159941750
** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
4160041751
**
41601
- ** This code may run concurrently to the code in walIndexAppend()
41752
+ ** This code might run concurrently to the code in walIndexAppend()
4160241753
** that adds entries to the wal-index (and possibly to this hash
4160341754
** table). This means the value just read from the hash
4160441755
** slot (aHash[iKey]) may have been added before or after the
4160541756
** current read transaction was opened. Values added after the
4160641757
** read transaction was opened may have been written incorrectly -
@@ -41919,44 +42070,59 @@
4191942070
** header to the start of the WAL file. See comments at the top of
4192042071
** this source file for a description of the WAL header format.
4192142072
*/
4192242073
iFrame = pWal->hdr.mxFrame;
4192342074
if( iFrame==0 ){
41924
- u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
42075
+ u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
42076
+ u32 aCksum[2]; /* Checksum for wal-header */
42077
+
4192542078
sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41926
- sqlite3Put4byte(&aWalHdr[4], 3007000);
42079
+ sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
4192742080
sqlite3Put4byte(&aWalHdr[8], szPage);
41928
- pWal->szPage = szPage;
41929
- pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
4193042081
sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
4193142082
memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
42083
+ walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
42084
+ sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
42085
+ sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
42086
+
42087
+ pWal->szPage = szPage;
42088
+ pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
42089
+ pWal->hdr.aFrameCksum[0] = aCksum[0];
42090
+ pWal->hdr.aFrameCksum[1] = aCksum[1];
42091
+
4193242092
rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
4193342093
WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
4193442094
if( rc!=SQLITE_OK ){
4193542095
return rc;
4193642096
}
41937
- walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
4193842097
}
4193942098
assert( pWal->szPage==szPage );
4194042099
4194142100
/* Write the log file. */
4194242101
for(p=pList; p; p=p->pDirty){
4194342102
u32 nDbsize; /* Db-size field for frame header */
4194442103
i64 iOffset; /* Write offset in log file */
41945
-
42104
+ void *pData;
42105
+
42106
+
4194642107
iOffset = walFrameOffset(++iFrame, szPage);
4194742108
4194842109
/* Populate and write the frame header */
4194942110
nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
41950
- walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame);
42111
+#if defined(SQLITE_HAS_CODEC)
42112
+ if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
42113
+#else
42114
+ pData = p->pData;
42115
+#endif
42116
+ walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
4195142117
rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
4195242118
if( rc!=SQLITE_OK ){
4195342119
return rc;
4195442120
}
4195542121
4195642122
/* Write the page data */
41957
- rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame));
42123
+ rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
4195842124
if( rc!=SQLITE_OK ){
4195942125
return rc;
4196042126
}
4196142127
pLast = p;
4196242128
}
@@ -41969,18 +42135,23 @@
4196942135
assert( isCommit );
4197042136
assert( iSegment>0 );
4197142137
4197242138
iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
4197342139
while( iOffset<iSegment ){
41974
- walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame);
42140
+ void *pData;
42141
+#if defined(SQLITE_HAS_CODEC)
42142
+ if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
42143
+#else
42144
+ pData = pLast->pData;
42145
+#endif
42146
+ walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
4197542147
rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
4197642148
if( rc!=SQLITE_OK ){
4197742149
return rc;
4197842150
}
41979
-
4198042151
iOffset += WAL_FRAME_HDRSIZE;
41981
- rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset);
42152
+ rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
4198242153
if( rc!=SQLITE_OK ){
4198342154
return rc;
4198442155
}
4198542156
nLast++;
4198642157
iOffset += szPage;
@@ -52844,13 +53015,20 @@
5284453015
if( !pExpr ){
5284553016
*ppVal = 0;
5284653017
return SQLITE_OK;
5284753018
}
5284853019
op = pExpr->op;
52849
- if( op==TK_REGISTER ){
52850
- op = pExpr->op2; /* This only happens with SQLITE_ENABLE_STAT2 */
52851
- }
53020
+
53021
+ /* op can only be TK_REGISTER is we have compiled with SQLITE_ENABLE_STAT2.
53022
+ ** The ifdef here is to enable us to achieve 100% branch test coverage even
53023
+ ** when SQLITE_ENABLE_STAT2 is omitted.
53024
+ */
53025
+#ifdef SQLITE_ENABLE_STAT2
53026
+ if( op==TK_REGISTER ) op = pExpr->op2;
53027
+#else
53028
+ if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
53029
+#endif
5285253030
5285353031
if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
5285453032
pVal = sqlite3ValueNew(db);
5285553033
if( pVal==0 ) goto no_mem;
5285653034
if( ExprHasProperty(pExpr, EP_IntValue) ){
@@ -63351,10 +63529,14 @@
6335163529
*/
6335263530
rc = sqlite3PagerCloseWal(u.cd.pPager);
6335363531
if( rc==SQLITE_OK ){
6335463532
sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
6335563533
}
63534
+ }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){
63535
+ /* Cannot transition directly from MEMORY to WAL. Use mode OFF
63536
+ ** as an intermediate */
63537
+ sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF);
6335663538
}
6335763539
6335863540
/* Open a transaction on the database file. Regardless of the journal
6335963541
** mode, this transaction always uses a rollback journal.
6336063542
*/
@@ -103905,21 +104087,26 @@
103905104087
** a low-level error is first detected.
103906104088
*/
103907104089
SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
103908104090
testcase( sqlite3GlobalConfig.xLog!=0 );
103909104091
sqlite3_log(SQLITE_CORRUPT,
103910
- "database corruption found by source line %d", lineno);
104092
+ "database corruption at line %d of [%.10s]",
104093
+ lineno, 20+sqlite3_sourceid());
103911104094
return SQLITE_CORRUPT;
103912104095
}
103913104096
SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
103914104097
testcase( sqlite3GlobalConfig.xLog!=0 );
103915
- sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno);
104098
+ sqlite3_log(SQLITE_MISUSE,
104099
+ "misuse at line %d of [%.10s]",
104100
+ lineno, 20+sqlite3_sourceid());
103916104101
return SQLITE_MISUSE;
103917104102
}
103918104103
SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
103919104104
testcase( sqlite3GlobalConfig.xLog!=0 );
103920
- sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno);
104105
+ sqlite3_log(SQLITE_CANTOPEN,
104106
+ "cannot open file at line %d of [%.10s]",
104107
+ lineno, 20+sqlite3_sourceid());
103921104108
return SQLITE_CANTOPEN;
103922104109
}
103923104110
103924104111
103925104112
#ifndef SQLITE_OMIT_DEPRECATED
103926104113
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637 ** [sqlite_version()] and [sqlite_source_id()].
638 */
639 #define SQLITE_VERSION "3.7.0"
640 #define SQLITE_VERSION_NUMBER 3007000
641 #define SQLITE_SOURCE_ID "2010-06-21 12:47:41 ee0acef1faffd480fd2136f81fb2b6f6a17b5388"
642
643 /*
644 ** CAPI3REF: Run-Time Library Version Numbers
645 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
646 **
@@ -7858,10 +7858,14 @@
7858 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
7859 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
7860
7861 /* Functions used to truncate the database file. */
7862 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 
 
 
 
7863
7864 /* Functions to support testing and debugging. */
7865 #if !defined(NDEBUG) || defined(SQLITE_TEST)
7866 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
7867 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
@@ -16798,15 +16802,15 @@
16798 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
16799 void *p;
16800 assert( n>0 );
16801
16802 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16803 /* Verify that no more than one scratch allocation per thread
16804 ** is outstanding at one time. (This is only checked in the
16805 ** single-threaded case since checking in the multi-threaded case
16806 ** would be much more complicated.) */
16807 assert( scratchAllocOut==0 );
16808 #endif
16809
16810 if( sqlite3GlobalConfig.szScratch<n ){
16811 goto scratch_overflow;
16812 }else{
@@ -16847,20 +16851,10 @@
16847 #endif
16848 return p;
16849 }
16850 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
16851 if( p ){
16852
16853 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16854 /* Verify that no more than one scratch allocation per thread
16855 ** is outstanding at one time. (This is only checked in the
16856 ** single-threaded case since checking in the multi-threaded case
16857 ** would be much more complicated.) */
16858 assert( scratchAllocOut==1 );
16859 scratchAllocOut = 0;
16860 #endif
16861
16862 if( sqlite3GlobalConfig.pScratch==0
16863 || p<sqlite3GlobalConfig.pScratch
16864 || p>=(void*)mem0.aScratchFree ){
16865 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
16866 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
@@ -16882,10 +16876,20 @@
16882 sqlite3_mutex_enter(mem0.mutex);
16883 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
16884 mem0.aScratchFree[mem0.nScratchFree++] = i;
16885 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
16886 sqlite3_mutex_leave(mem0.mutex);
 
 
 
 
 
 
 
 
 
 
16887 }
16888 }
16889 }
16890
16891 /*
@@ -25582,11 +25586,11 @@
25582 };
25583
25584 /*
25585 ** Constants used for locking
25586 */
25587 #define UNIX_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25588 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25589
25590 /*
25591 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25592 **
@@ -30013,11 +30017,11 @@
30013 };
30014
30015 /*
30016 ** Constants used for locking
30017 */
30018 #define WIN_SHM_BASE ((18+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30019 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30020
30021 /*
30022 ** Apply advisory locks for all n bytes beginning at ofst.
30023 */
@@ -30138,11 +30142,11 @@
30138 sqlite3_free(p);
30139 return SQLITE_NOMEM;
30140 }
30141 memset(pNew, 0, sizeof(*pNew));
30142 pNew->zFilename = (char*)&pNew[1];
30143 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-wal-index", pDbFd->zPath);
30144
30145 /* Look to see if there is an existing winShmNode that can be used.
30146 ** If no matching winShmNode currently exists, create a new one.
30147 */
30148 winShmEnterMutex();
@@ -31136,11 +31140,11 @@
31136 GetSystemTimeAsFileTime( &ft );
31137 #endif
31138
31139 *piNow = winFiletimeEpoch +
31140 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
31141 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)1000;
31142
31143 #ifdef SQLITE_TEST
31144 if( sqlite3_current_time ){
31145 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
31146 }
@@ -33856,16 +33860,19 @@
33856 ** may attempt to commit the transaction again later (calling
33857 ** CommitPhaseOne() again). This flag is used to ensure that the
33858 ** master journal name is only written to the journal file the first
33859 ** time CommitPhaseOne() is called.
33860 **
33861 ** doNotSync
33862 **
33863 ** When enabled, cache spills are prohibited and the journal file cannot
33864 ** be synced. This variable is set and cleared by sqlite3PagerWrite()
33865 ** in order to prevent a journal sync from happening in between the
33866 ** journalling of two pages on the same sector.
 
 
 
33867 **
33868 ** needSync
33869 **
33870 ** TODO: It might be easier to set this variable in writeJournalHdr()
33871 ** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -33905,11 +33912,12 @@
33905 u8 dbModified; /* True if there are any changes to the Db */
33906 u8 needSync; /* True if an fsync() is needed on the journal */
33907 u8 journalStarted; /* True if header of journal is synced */
33908 u8 changeCountDone; /* Set after incrementing the change-counter */
33909 u8 setMaster; /* True if a m-j name has been written to jrnl */
33910 u8 doNotSync; /* Boolean. While true, do not spill the cache */
 
33911 u8 dbSizeValid; /* Set when dbSize is correct */
33912 u8 subjInMemory; /* True to use in-memory sub-journals */
33913 Pgno dbSize; /* Number of pages in the database */
33914 Pgno dbOrigSize; /* dbSize before the current transaction */
33915 Pgno dbFileSize; /* Number of pages in the database file */
@@ -34772,13 +34780,14 @@
34772 static void pager_unlock(Pager *pPager){
34773 if( !pPager->exclusiveMode ){
34774 int rc = SQLITE_OK; /* Return code */
34775 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
34776
34777 /* Always close the journal file when dropping the database lock.
34778 ** Otherwise, another connection with journal_mode=delete might
34779 ** delete the file out from under us.
 
34780 */
34781 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
34782 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
34783 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
34784 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
@@ -34835,11 +34844,11 @@
34835 ** structure, the second the error-code about to be returned by a pager
34836 ** API function. The value returned is a copy of the second argument
34837 ** to this function.
34838 **
34839 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34840 ** the error becomes persistent. Until the persisten error is cleared,
34841 ** subsequent API calls on this Pager will immediately return the same
34842 ** error code.
34843 **
34844 ** A persistent error indicates that the contents of the pager-cache
34845 ** cannot be trusted. This state can be cleared by completely discarding
@@ -35240,13 +35249,16 @@
35240 ** the data just read from the sub-journal. Mark the page as dirty
35241 ** and if the pager requires a journal-sync, then mark the page as
35242 ** requiring a journal-sync before it is written.
35243 */
35244 assert( isSavepnt );
35245 if( (rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1))!=SQLITE_OK ){
35246 return rc;
35247 }
 
 
 
35248 pPg->flags &= ~PGHDR_NEED_READ;
35249 sqlite3PcacheMakeDirty(pPg);
35250 }
35251 if( pPg ){
35252 /* No page should ever be explicitly rolled back that is in use, except
@@ -35346,10 +35358,13 @@
35346 int rc; /* Return code */
35347 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
35348 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
35349 char *zMasterJournal = 0; /* Contents of master journal file */
35350 i64 nMasterJournal; /* Size of master journal file */
 
 
 
35351
35352 /* Allocate space for both the pJournal and pMaster file descriptors.
35353 ** If successful, open the master journal file for reading.
35354 */
35355 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
@@ -35360,77 +35375,72 @@
35360 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
35361 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
35362 }
35363 if( rc!=SQLITE_OK ) goto delmaster_out;
35364
35365 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
35366 if( rc!=SQLITE_OK ) goto delmaster_out;
35367
35368 if( nMasterJournal>0 ){
35369 char *zJournal;
35370 char *zMasterPtr = 0;
35371 int nMasterPtr = pVfs->mxPathname+1;
35372
35373 /* Load the entire master journal file into space obtained from
35374 ** sqlite3_malloc() and pointed to by zMasterJournal.
35375 */
35376 zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
35377 if( !zMasterJournal ){
35378 rc = SQLITE_NOMEM;
35379 goto delmaster_out;
35380 }
35381 zMasterPtr = &zMasterJournal[nMasterJournal+1];
35382 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
35383 if( rc!=SQLITE_OK ) goto delmaster_out;
35384 zMasterJournal[nMasterJournal] = 0;
35385
35386 zJournal = zMasterJournal;
35387 while( (zJournal-zMasterJournal)<nMasterJournal ){
35388 int exists;
35389 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
35390 if( rc!=SQLITE_OK ){
35391 goto delmaster_out;
35392 }
35393 if( exists ){
35394 /* One of the journals pointed to by the master journal exists.
35395 ** Open it and check if it points at the master journal. If
35396 ** so, return without deleting the master journal file.
35397 */
35398 int c;
35399 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
35400 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
35401 if( rc!=SQLITE_OK ){
35402 goto delmaster_out;
35403 }
35404
35405 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
35406 sqlite3OsClose(pJournal);
35407 if( rc!=SQLITE_OK ){
35408 goto delmaster_out;
35409 }
35410
35411 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
35412 if( c ){
35413 /* We have a match. Do not delete the master journal file. */
35414 goto delmaster_out;
35415 }
35416 }
35417 zJournal += (sqlite3Strlen30(zJournal)+1);
35418 }
35419 }
35420
35421 rc = sqlite3OsDelete(pVfs, zMaster, 0);
35422
35423 delmaster_out:
35424 if( zMasterJournal ){
35425 sqlite3_free(zMasterJournal);
35426 }
35427 if( pMaster ){
35428 sqlite3OsClose(pMaster);
35429 assert( !isOpen(pJournal) );
 
35430 }
35431 sqlite3_free(pMaster);
35432 return rc;
35433 }
35434
35435
35436 /*
@@ -36125,11 +36135,11 @@
36125 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
36126 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
36127 }
36128 assert( rc!=SQLITE_DONE );
36129 }
36130 assert( rc!=SQLITE_OK || pPager->journalOff==szJ );
36131
36132 /* Finally, rollback pages from the sub-journal. Page that were
36133 ** previously rolled back out of the main journal (and are hence in pDone)
36134 ** will be skipped. Out-of-range pages are also skipped.
36135 */
@@ -37065,10 +37075,26 @@
37065 Pager *pPager = (Pager *)p;
37066 int rc = SQLITE_OK;
37067
37068 assert( pPg->pPager==pPager );
37069 assert( pPg->flags&PGHDR_DIRTY );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37070
37071 pPg->pDirty = 0;
37072 if( pagerUseWal(pPager) ){
37073 /* Write a single frame for this page to the log. */
37074 if( subjRequiresPage(pPg) ){
@@ -37076,33 +37102,16 @@
37076 }
37077 if( rc==SQLITE_OK ){
37078 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
37079 }
37080 }else{
37081 /* The doNotSync flag is set by the sqlite3PagerWrite() function while it
37082 ** is journalling a set of two or more database pages that are stored
37083 ** on the same disk sector. Syncing the journal is not allowed while
37084 ** this is happening as it is important that all members of such a
37085 ** set of pages are synced to disk together. So, if the page this function
37086 ** is trying to make clean will require a journal sync and the doNotSync
37087 ** flag is set, return without doing anything. The pcache layer will
37088 ** just have to go ahead and allocate a new page buffer instead of
37089 ** reusing pPg.
37090 **
37091 ** Similarly, if the pager has already entered the error state, do not
37092 ** try to write the contents of pPg to disk.
37093 */
37094 if( NEVER(pPager->errCode)
37095 || (pPager->doNotSync && pPg->flags&PGHDR_NEED_SYNC)
37096 ){
37097 return SQLITE_OK;
37098 }
37099
37100 /* Sync the journal file if required. */
37101 if( pPg->flags&PGHDR_NEED_SYNC ){
 
37102 rc = syncJournal(pPager);
37103 if( rc==SQLITE_OK && pPager->fullSync &&
37104 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37105 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37106 ){
37107 pPager->nRec = 0;
37108 rc = writeJournalHdr(pPager);
@@ -38256,11 +38265,11 @@
38256 */
38257 rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
38258 if( rc!=SQLITE_OK ){
38259 return rc;
38260 }
38261 if( !isOpen(pPager->jfd)
38262 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38263 && !pagerUseWal(pPager)
38264 ){
38265 assert( pPager->useJournal );
38266 rc = pager_open_journal(pPager);
@@ -38384,39 +38393,41 @@
38384 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
38385
38386 if( nPagePerSector>1 ){
38387 Pgno nPageCount; /* Total number of pages in database file */
38388 Pgno pg1; /* First page of the sector pPg is located on. */
38389 int nPage; /* Number of pages starting at pg1 to journal */
38390 int ii; /* Loop counter */
38391 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
38392
38393 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
38394 ** header to be written between the pages journaled by this function.
 
38395 */
38396 assert( !MEMDB );
38397 assert( pPager->doNotSync==0 );
38398 pPager->doNotSync = 1;
38399
38400 /* This trick assumes that both the page-size and sector-size are
38401 ** an integer power of 2. It sets variable pg1 to the identifier
38402 ** of the first page of the sector pPg is located on.
38403 */
38404 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
38405
38406 rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount);
38407 if( rc ) return rc;
38408 if( pPg->pgno>nPageCount ){
38409 nPage = (pPg->pgno - pg1)+1;
38410 }else if( (pg1+nPagePerSector-1)>nPageCount ){
38411 nPage = nPageCount+1-pg1;
38412 }else{
38413 nPage = nPagePerSector;
38414 }
38415 assert(nPage>0);
38416 assert(pg1<=pPg->pgno);
38417 assert((pg1+nPage)>pPg->pgno);
 
38418
38419 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
38420 Pgno pg = pg1+ii;
38421 PgHdr *pPage;
38422 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38455,12 +38466,12 @@
38455 }
38456 }
38457 assert(pPager->needSync);
38458 }
38459
38460 assert( pPager->doNotSync==1 );
38461 pPager->doNotSync = 0;
38462 }else{
38463 rc = pager_write(pDbPage);
38464 }
38465 return rc;
38466 }
@@ -38571,13 +38582,16 @@
38571 put32bits(((char*)pPgHdr->pData)+92, change_counter);
38572 put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
38573
38574 /* If running in direct mode, write the contents of page 1 to the file. */
38575 if( DIRECT_MODE ){
38576 const void *zBuf = pPgHdr->pData;
38577 assert( pPager->dbFileSize>0 );
38578 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
 
 
 
38579 if( rc==SQLITE_OK ){
38580 pPager->changeCountDone = 1;
38581 }
38582 }else{
38583 pPager->changeCountDone = 1;
@@ -38643,11 +38657,11 @@
38643
38644 /* The dbOrigSize is never set if journal_mode=OFF */
38645 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
38646
38647 /* If a prior error occurred, report that error again. */
38648 if( NEVER(pPager->errCode) ) return pPager->errCode;
38649
38650 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
38651 pPager->zFilename, zMaster, pPager->dbSize));
38652
38653 if( MEMDB && pPager->dbModified ){
@@ -38821,14 +38835,15 @@
38821 ** But if (due to a coding error elsewhere in the system) it does get
38822 ** called, just return the same error code without doing anything. */
38823 if( NEVER(pPager->errCode) ) return pPager->errCode;
38824
38825 /* This function should not be called if the pager is not in at least
38826 ** PAGER_RESERVED state. And indeed SQLite never does this. But it is
38827 ** nice to have this defensive test here anyway.
 
38828 */
38829 if( NEVER(pPager->state<PAGER_RESERVED) ) return SQLITE_ERROR;
38830
38831 /* An optimization. If the database was not actually modified during
38832 ** this transaction, the pager is running in exclusive-mode and is
38833 ** using persistent journals, then this function is a no-op.
38834 **
@@ -38873,11 +38888,11 @@
38873 ** performed. If successful, task (2). Regardless of the outcome
38874 ** of either, the error state error code is returned to the caller
38875 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
38876 **
38877 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38878 ** or not (1) is succussful, also attempt (2). If successful, return
38879 ** SQLITE_OK. Otherwise, enter the error state and return the first
38880 ** error code encountered.
38881 **
38882 ** In this case there is no chance that the database was written to.
38883 ** So is safe to finalize the journal file even if the playback
@@ -39647,11 +39662,27 @@
39647 sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
39648 }
39649 }
39650 return rc;
39651 }
39652 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39653
39654 #endif /* SQLITE_OMIT_DISKIO */
39655
39656 /************** End of pager.c ***********************************************/
39657 /************** Begin file wal.c *********************************************/
@@ -39686,23 +39717,25 @@
39686 ** frames can overwrite the old ones. A WAL always grows from beginning
39687 ** toward the end. Checksums and counters attached to each frame are
39688 ** used to determine which frames within the WAL are valid and which
39689 ** are leftovers from prior checkpoints.
39690 **
39691 ** The WAL header is 24 bytes in size and consists of the following six
39692 ** big-endian 32-bit unsigned integer values:
39693 **
39694 ** 0: Magic number. 0x377f0682 or 0x377f0683
39695 ** 4: File format version. Currently 3007000
39696 ** 8: Database page size. Example: 1024
39697 ** 12: Checkpoint sequence number
39698 ** 16: Salt-1, random integer incremented with each checkpoint
39699 ** 20: Salt-2, a different random integer changing with each ckpt
 
 
39700 **
39701 ** Immediately following the wal-header are zero or more frames. Each
39702 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39703 ** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned
39704 ** integer values, as follows:
39705 **
39706 ** 0: Page number.
39707 ** 4: For commit records, the size of the database image in pages
39708 ** after the commit. For all other records, zero.
@@ -39733,10 +39766,15 @@
39733 **
39734 ** for i from 0 to n-1 step 2:
39735 ** s0 += x[i] + s1;
39736 ** s1 += x[i+1] + s0;
39737 ** endfor
 
 
 
 
 
39738 **
39739 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39740 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
39741 ** The VFS.xSync operations serve as write barriers - all writes launched
39742 ** before the xSync must complete before any write that launches after the
@@ -39900,10 +39938,25 @@
39900 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39901 #else
39902 # define WALTRACE(X)
39903 #endif
39904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39905
39906 /*
39907 ** Indices of various locking bytes. WAL_NREADER is the number
39908 ** of available reader locks and should be at least 3.
39909 */
@@ -39926,10 +39979,12 @@
39926 **
39927 ** The actual header in the wal-index consists of two copies of this
39928 ** object.
39929 */
39930 struct WalIndexHdr {
 
 
39931 u32 iChange; /* Counter incremented each transaction */
39932 u8 isInit; /* 1 when initialized */
39933 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39934 u16 szPage; /* Database page size in bytes */
39935 u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40005,12 +40060,13 @@
40005 #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
40006
40007 /* Size of header before each frame in wal */
40008 #define WAL_FRAME_HDRSIZE 24
40009
40010 /* Size of write ahead log header */
40011 #define WAL_HDRSIZE 24
 
40012
40013 /* WAL magic value. Either this value, or the same value with the least
40014 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
40015 ** big-endian format in the first 4 bytes of a WAL file.
40016 **
@@ -40234,10 +40290,11 @@
40234 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40235 const int nCksum = offsetof(WalIndexHdr, aCksum);
40236
40237 assert( pWal->writeLock );
40238 pWal->hdr.isInit = 1;
 
40239 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40240 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40241 sqlite3OsShmBarrier(pWal->pDbFd);
40242 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40243 }
@@ -40683,10 +40740,11 @@
40683 u8 *aData; /* Pointer to data part of aFrame buffer */
40684 int iFrame; /* Index of last frame read */
40685 i64 iOffset; /* Next offset to read from log file */
40686 int szPage; /* Page size according to the log */
40687 u32 magic; /* Magic value read from WAL header */
 
40688
40689 /* Read in the WAL header. */
40690 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40691 if( rc!=SQLITE_OK ){
40692 goto recovery_error;
@@ -40708,13 +40766,28 @@
40708 }
40709 pWal->hdr.bigEndCksum = (magic&0x00000001);
40710 pWal->szPage = szPage;
40711 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40712 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
 
 
40713 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40714 aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum
40715 );
 
 
 
 
 
 
 
 
 
 
 
 
 
40716
40717 /* Malloc a buffer to read frames into. */
40718 szFrame = szPage + WAL_FRAME_HDRSIZE;
40719 aFrame = (u8 *)sqlite3_malloc(szFrame);
40720 if( !aFrame ){
@@ -40899,53 +40972,101 @@
40899
40900 *piPage = p->iPrior = iRet;
40901 return (iRet==0xFFFFFFFF);
40902 }
40903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40905 static void walMergesort(
40906 u32 *aContent, /* Pages in wal */
40907 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
40908 ht_slot *aList, /* IN/OUT: List to sort */
40909 int *pnList /* IN/OUT: Number of elements in aList[] */
40910 ){
40911 int nList = *pnList;
40912 if( nList>1 ){
40913 int nLeft = nList / 2; /* Elements in left list */
40914 int nRight = nList - nLeft; /* Elements in right list */
40915 int iLeft = 0; /* Current index in aLeft */
40916 int iRight = 0; /* Current index in aright */
40917 int iOut = 0; /* Current index in output buffer */
40918 ht_slot *aLeft = aList; /* Left list */
40919 ht_slot *aRight = aList+nLeft;/* Right list */
40920
40921 /* TODO: Change to non-recursive version. */
40922 walMergesort(aContent, aBuffer, aLeft, &nLeft);
40923 walMergesort(aContent, aBuffer, aRight, &nRight);
40924
40925 while( iRight<nRight || iLeft<nLeft ){
40926 ht_slot logpage;
40927 Pgno dbpage;
40928
40929 if( (iLeft<nLeft)
40930 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
40931 ){
40932 logpage = aLeft[iLeft++];
40933 }else{
40934 logpage = aRight[iRight++];
40935 }
40936 dbpage = aContent[logpage];
40937
40938 aBuffer[iOut++] = logpage;
40939 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
40940
40941 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
40942 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
40943 }
40944 memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
40945 *pnList = iOut;
40946 }
 
 
 
40947
40948 #ifdef SQLITE_DEBUG
40949 {
40950 int i;
40951 for(i=1; i<*pnList; i++){
@@ -40957,91 +41078,94 @@
40957
40958 /*
40959 ** Free an iterator allocated by walIteratorInit().
40960 */
40961 static void walIteratorFree(WalIterator *p){
40962 sqlite3_free(p);
40963 }
40964
40965 /*
40966 ** Map the wal-index into memory owned by this thread, if it is not
40967 ** mapped already. Then construct a WalInterator object that can be
40968 ** used to loop over all pages in the WAL in ascending order.
40969 **
40970 ** On success, make *pp point to the newly allocated WalInterator object
40971 ** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error
40972 ** code.
40973 **
40974 ** The calling routine should invoke walIteratorFree() to destroy the
40975 ** WalIterator object when it has finished with it. The caller must
40976 ** also unmap the wal-index. But the wal-index must not be unmapped
40977 ** prior to the WalIterator object being destroyed.
40978 */
40979 static int walIteratorInit(Wal *pWal, WalIterator **pp){
40980 WalIterator *p; /* Return value */
40981 int nSegment; /* Number of segments to merge */
40982 u32 iLast; /* Last frame in log */
40983 int nByte; /* Number of bytes to allocate */
40984 int i; /* Iterator variable */
40985 ht_slot *aTmp; /* Temp space used by merge-sort */
40986 ht_slot *aSpace; /* Space at the end of the allocation */
40987
40988 /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other
40989 ** thread is able to write to shared memory while this routine is
40990 ** running (or, indeed, while the WalIterator object exists). Hence,
40991 ** we can cast off the volatile qualification from shared memory
40992 */
40993 assert( pWal->ckptLock );
40994 iLast = pWal->hdr.mxFrame;
40995
40996 /* Allocate space for the WalIterator object */
40997 nSegment = walFramePage(iLast) + 1;
40998 nByte = sizeof(WalIterator)
40999 + nSegment*(sizeof(struct WalSegment))
41000 + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot));
41001 p = (WalIterator *)sqlite3_malloc(nByte);
41002 if( !p ){
41003 return SQLITE_NOMEM;
41004 }
41005 memset(p, 0, nByte);
41006
41007 /* Allocate space for the WalIterator object */
41008 p->nSegment = nSegment;
41009 aSpace = (ht_slot *)&p->aSegment[nSegment];
41010 aTmp = &aSpace[HASHTABLE_NPAGE*nSegment];
41011 for(i=0; i<nSegment; i++){
 
 
 
 
 
 
 
 
 
41012 volatile ht_slot *aHash;
41013 int j;
41014 u32 iZero;
41015 int nEntry;
41016 volatile u32 *aPgno;
41017 int rc;
41018
41019 rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
41020 if( rc!=SQLITE_OK ){
41021 walIteratorFree(p);
41022 return rc;
41023 }
41024 aPgno++;
41025 nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
41026 iZero++;
41027
41028 for(j=0; j<nEntry; j++){
41029 aSpace[j] = j;
41030 }
41031 walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry);
41032 p->aSegment[i].iZero = iZero;
41033 p->aSegment[i].nEntry = nEntry;
41034 p->aSegment[i].aIndex = aSpace;
41035 p->aSegment[i].aPgno = (u32 *)aPgno;
41036 aSpace += HASHTABLE_NPAGE;
41037 }
41038 assert( aSpace==aTmp );
41039
41040 /* Return the fully initialized WalIterator object */
 
 
 
 
41041 *pp = p;
41042 return SQLITE_OK ;
41043 }
41044
41045 /*
41046 ** Copy as much content as we can from the WAL back into the database file
41047 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
@@ -41086,15 +41210,18 @@
41086 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
41087 u32 mxSafeFrame; /* Max frame that can be backfilled */
41088 int i; /* Loop counter */
41089 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
41090
 
 
41091 /* Allocate the iterator */
41092 rc = walIteratorInit(pWal, &pIter);
41093 if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){
41094 goto walcheckpoint_out;
41095 }
 
41096
41097 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
41098 if( pWal->hdr.szPage!=nBuf ){
41099 rc = SQLITE_CORRUPT_BKPT;
41100 goto walcheckpoint_out;
@@ -41275,30 +41402,24 @@
41275 return 0;
41276 }
41277
41278 /*
41279 ** Read the wal-index header from the wal-index and into pWal->hdr.
41280 ** If the wal-header appears to be corrupt, try to recover the log
41281 ** before returning.
41282 **
41283 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41284 ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41285 ** to 0.
41286 **
41287 ** This routine also maps the wal-index content into memory and assigns
41288 ** ownership of that mapping to the current thread. In some implementations,
41289 ** only one thread at a time can hold a mapping of the wal-index. Hence,
41290 ** the caller should strive to invoke walIndexUnmap() as soon as possible
41291 ** after this routine returns.
41292 **
41293 ** If the wal-index header is successfully read, return SQLITE_OK.
41294 ** Otherwise an SQLite error code.
41295 */
41296 static int walIndexReadHdr(Wal *pWal, int *pChanged){
41297 int rc; /* Return code */
41298 int badHdr; /* True if a header read failed */
41299 volatile u32 *page0;
41300
41301 /* Ensure that page 0 of the wal-index (the page that contains the
41302 ** wal-index header) is mapped. Return early if an error occurs here.
41303 */
41304 assert( pChanged );
@@ -41309,11 +41430,11 @@
41309 assert( page0 || pWal->writeLock==0 );
41310
41311 /* If the first page of the wal-index has been mapped, try to read the
41312 ** wal-index header immediately, without holding any lock. This usually
41313 ** works, but may fail if the wal-index header is corrupt or currently
41314 ** being modified by another user.
41315 */
41316 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41317
41318 /* If the first attempt failed, it might have been due to a race
41319 ** with a writer. So get a WRITE lock and try again.
@@ -41333,10 +41454,18 @@
41333 }
41334 }
41335 pWal->writeLock = 0;
41336 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41337 }
 
 
 
 
 
 
 
 
41338
41339 return rc;
41340 }
41341
41342 /*
@@ -41348,13 +41477,33 @@
41348 /*
41349 ** Attempt to start a read transaction. This might fail due to a race or
41350 ** other transient condition. When that happens, it returns WAL_RETRY to
41351 ** indicate to the caller that it is safe to retry immediately.
41352 **
41353 ** On success return SQLITE_OK. On a permantent failure (such an
41354 ** I/O error or an SQLITE_BUSY because another process is running
41355 ** recovery) return a positive error code.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41356 **
41357 ** On success, this routine obtains a read lock on
41358 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41359 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41360 ** that means the Wal does not hold any read lock. The reader must not
@@ -41361,10 +41510,12 @@
41361 ** access any database page that is modified by a WAL frame up to and
41362 ** including frame number aReadMark[pWal->readLock]. The reader will
41363 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41364 ** Or if pWal->readLock==0, then the reader will ignore the WAL
41365 ** completely and get all content directly from the database file.
 
 
41366 ** When the read transaction is completed, the caller must release the
41367 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41368 **
41369 ** This routine uses the nBackfill and aReadMark[] fields of the header
41370 ** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41405,13 +41556,13 @@
41405 rc = WAL_RETRY;
41406 }else if( rc==SQLITE_BUSY ){
41407 rc = SQLITE_BUSY_RECOVERY;
41408 }
41409 }
41410 }
41411 if( rc!=SQLITE_OK ){
41412 return rc;
41413 }
41414
41415 pInfo = walCkptInfo(pWal);
41416 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41417 /* The WAL has been completely backfilled (or it is empty).
@@ -41583,13 +41734,13 @@
41583 /* This routine is only be called from within a read transaction. */
41584 assert( pWal->readLock>=0 || pWal->lockError );
41585
41586 /* If the "last page" field of the wal-index header snapshot is 0, then
41587 ** no data will be read from the wal under any circumstances. Return early
41588 ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if
41589 ** pWal->readLock==0, then the WAL is ignored by the reader so
41590 ** return early, as if the WAL were empty.
41591 */
41592 if( iLast==0 || pWal->readLock==0 ){
41593 *pInWal = 0;
41594 return SQLITE_OK;
41595 }
@@ -41596,11 +41747,11 @@
41596
41597 /* Search the hash table or tables for an entry matching page number
41598 ** pgno. Each iteration of the following for() loop searches one
41599 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41600 **
41601 ** This code may run concurrently to the code in walIndexAppend()
41602 ** that adds entries to the wal-index (and possibly to this hash
41603 ** table). This means the value just read from the hash
41604 ** slot (aHash[iKey]) may have been added before or after the
41605 ** current read transaction was opened. Values added after the
41606 ** read transaction was opened may have been written incorrectly -
@@ -41919,44 +42070,59 @@
41919 ** header to the start of the WAL file. See comments at the top of
41920 ** this source file for a description of the WAL header format.
41921 */
41922 iFrame = pWal->hdr.mxFrame;
41923 if( iFrame==0 ){
41924 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */
 
 
41925 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
41926 sqlite3Put4byte(&aWalHdr[4], 3007000);
41927 sqlite3Put4byte(&aWalHdr[8], szPage);
41928 pWal->szPage = szPage;
41929 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
41930 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
41931 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
 
 
 
 
 
 
 
 
 
41932 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
41933 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
41934 if( rc!=SQLITE_OK ){
41935 return rc;
41936 }
41937 walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum);
41938 }
41939 assert( pWal->szPage==szPage );
41940
41941 /* Write the log file. */
41942 for(p=pList; p; p=p->pDirty){
41943 u32 nDbsize; /* Db-size field for frame header */
41944 i64 iOffset; /* Write offset in log file */
41945
 
 
41946 iOffset = walFrameOffset(++iFrame, szPage);
41947
41948 /* Populate and write the frame header */
41949 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
41950 walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame);
 
 
 
 
 
41951 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41952 if( rc!=SQLITE_OK ){
41953 return rc;
41954 }
41955
41956 /* Write the page data */
41957 rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame));
41958 if( rc!=SQLITE_OK ){
41959 return rc;
41960 }
41961 pLast = p;
41962 }
@@ -41969,18 +42135,23 @@
41969 assert( isCommit );
41970 assert( iSegment>0 );
41971
41972 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
41973 while( iOffset<iSegment ){
41974 walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame);
 
 
 
 
 
 
41975 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
41976 if( rc!=SQLITE_OK ){
41977 return rc;
41978 }
41979
41980 iOffset += WAL_FRAME_HDRSIZE;
41981 rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset);
41982 if( rc!=SQLITE_OK ){
41983 return rc;
41984 }
41985 nLast++;
41986 iOffset += szPage;
@@ -52844,13 +53015,20 @@
52844 if( !pExpr ){
52845 *ppVal = 0;
52846 return SQLITE_OK;
52847 }
52848 op = pExpr->op;
52849 if( op==TK_REGISTER ){
52850 op = pExpr->op2; /* This only happens with SQLITE_ENABLE_STAT2 */
52851 }
 
 
 
 
 
 
 
52852
52853 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
52854 pVal = sqlite3ValueNew(db);
52855 if( pVal==0 ) goto no_mem;
52856 if( ExprHasProperty(pExpr, EP_IntValue) ){
@@ -63351,10 +63529,14 @@
63351 */
63352 rc = sqlite3PagerCloseWal(u.cd.pPager);
63353 if( rc==SQLITE_OK ){
63354 sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63355 }
 
 
 
 
63356 }
63357
63358 /* Open a transaction on the database file. Regardless of the journal
63359 ** mode, this transaction always uses a rollback journal.
63360 */
@@ -103905,21 +104087,26 @@
103905 ** a low-level error is first detected.
103906 */
103907 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
103908 testcase( sqlite3GlobalConfig.xLog!=0 );
103909 sqlite3_log(SQLITE_CORRUPT,
103910 "database corruption found by source line %d", lineno);
 
103911 return SQLITE_CORRUPT;
103912 }
103913 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
103914 testcase( sqlite3GlobalConfig.xLog!=0 );
103915 sqlite3_log(SQLITE_MISUSE, "misuse detected by source line %d", lineno);
 
 
103916 return SQLITE_MISUSE;
103917 }
103918 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
103919 testcase( sqlite3GlobalConfig.xLog!=0 );
103920 sqlite3_log(SQLITE_CANTOPEN, "cannot open file at source line %d", lineno);
 
 
103921 return SQLITE_CANTOPEN;
103922 }
103923
103924
103925 #ifndef SQLITE_OMIT_DEPRECATED
103926
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -636,11 +636,11 @@
636 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
637 ** [sqlite_version()] and [sqlite_source_id()].
638 */
639 #define SQLITE_VERSION "3.7.0"
640 #define SQLITE_VERSION_NUMBER 3007000
641 #define SQLITE_SOURCE_ID "2010-06-26 20:25:31 f149b498b6ada3fc9f71ee104c351554c80c7f8a"
642
643 /*
644 ** CAPI3REF: Run-Time Library Version Numbers
645 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
646 **
@@ -7858,10 +7858,14 @@
7858 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
7859 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
7860
7861 /* Functions used to truncate the database file. */
7862 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
7863
7864 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
7865 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
7866 #endif
7867
7868 /* Functions to support testing and debugging. */
7869 #if !defined(NDEBUG) || defined(SQLITE_TEST)
7870 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
7871 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
@@ -16798,15 +16802,15 @@
16802 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
16803 void *p;
16804 assert( n>0 );
16805
16806 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16807 /* Verify that no more than two scratch allocation per thread
16808 ** is outstanding at one time. (This is only checked in the
16809 ** single-threaded case since checking in the multi-threaded case
16810 ** would be much more complicated.) */
16811 assert( scratchAllocOut<=1 );
16812 #endif
16813
16814 if( sqlite3GlobalConfig.szScratch<n ){
16815 goto scratch_overflow;
16816 }else{
@@ -16847,20 +16851,10 @@
16851 #endif
16852 return p;
16853 }
16854 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
16855 if( p ){
 
 
 
 
 
 
 
 
 
 
16856 if( sqlite3GlobalConfig.pScratch==0
16857 || p<sqlite3GlobalConfig.pScratch
16858 || p>=(void*)mem0.aScratchFree ){
16859 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
16860 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
@@ -16882,10 +16876,20 @@
16876 sqlite3_mutex_enter(mem0.mutex);
16877 assert( mem0.nScratchFree<(u32)sqlite3GlobalConfig.nScratch );
16878 mem0.aScratchFree[mem0.nScratchFree++] = i;
16879 sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
16880 sqlite3_mutex_leave(mem0.mutex);
16881
16882 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
16883 /* Verify that no more than two scratch allocation per thread
16884 ** is outstanding at one time. (This is only checked in the
16885 ** single-threaded case since checking in the multi-threaded case
16886 ** would be much more complicated.) */
16887 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
16888 scratchAllocOut = 0;
16889 #endif
16890
16891 }
16892 }
16893 }
16894
16895 /*
@@ -25582,11 +25586,11 @@
25586 };
25587
25588 /*
25589 ** Constants used for locking
25590 */
25591 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
25592 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
25593
25594 /*
25595 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
25596 **
@@ -30013,11 +30017,11 @@
30017 };
30018
30019 /*
30020 ** Constants used for locking
30021 */
30022 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
30023 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
30024
30025 /*
30026 ** Apply advisory locks for all n bytes beginning at ofst.
30027 */
@@ -30138,11 +30142,11 @@
30142 sqlite3_free(p);
30143 return SQLITE_NOMEM;
30144 }
30145 memset(pNew, 0, sizeof(*pNew));
30146 pNew->zFilename = (char*)&pNew[1];
30147 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
30148
30149 /* Look to see if there is an existing winShmNode that can be used.
30150 ** If no matching winShmNode currently exists, create a new one.
30151 */
30152 winShmEnterMutex();
@@ -31136,11 +31140,11 @@
31140 GetSystemTimeAsFileTime( &ft );
31141 #endif
31142
31143 *piNow = winFiletimeEpoch +
31144 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
31145 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
31146
31147 #ifdef SQLITE_TEST
31148 if( sqlite3_current_time ){
31149 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
31150 }
@@ -33856,16 +33860,19 @@
33860 ** may attempt to commit the transaction again later (calling
33861 ** CommitPhaseOne() again). This flag is used to ensure that the
33862 ** master journal name is only written to the journal file the first
33863 ** time CommitPhaseOne() is called.
33864 **
33865 ** doNotSpill, doNotSyncSpill
33866 **
33867 ** When enabled, cache spills are prohibited. The doNotSpill variable
33868 ** inhibits all cache spill and doNotSyncSpill inhibits those spills that
33869 ** would require a journal sync. The doNotSyncSpill is set and cleared
33870 ** by sqlite3PagerWrite() in order to prevent a journal sync from happening
33871 ** in between the journalling of two pages on the same sector. The
33872 ** doNotSpill value set to prevent pagerStress() from trying to use
33873 ** the journal during a rollback.
33874 **
33875 ** needSync
33876 **
33877 ** TODO: It might be easier to set this variable in writeJournalHdr()
33878 ** and writeMasterJournal() only. Change its meaning to "unsynced data
@@ -33905,11 +33912,12 @@
33912 u8 dbModified; /* True if there are any changes to the Db */
33913 u8 needSync; /* True if an fsync() is needed on the journal */
33914 u8 journalStarted; /* True if header of journal is synced */
33915 u8 changeCountDone; /* Set after incrementing the change-counter */
33916 u8 setMaster; /* True if a m-j name has been written to jrnl */
33917 u8 doNotSpill; /* Do not spill the cache when non-zero */
33918 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
33919 u8 dbSizeValid; /* Set when dbSize is correct */
33920 u8 subjInMemory; /* True to use in-memory sub-journals */
33921 Pgno dbSize; /* Number of pages in the database */
33922 Pgno dbOrigSize; /* dbSize before the current transaction */
33923 Pgno dbFileSize; /* Number of pages in the database file */
@@ -34772,13 +34780,14 @@
34780 static void pager_unlock(Pager *pPager){
34781 if( !pPager->exclusiveMode ){
34782 int rc = SQLITE_OK; /* Return code */
34783 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
34784
34785 /* If the operating system support deletion of open files, then
34786 ** close the journal file when dropping the database lock. Otherwise
34787 ** another connection with journal_mode=delete might delete the file
34788 ** out from under us.
34789 */
34790 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 );
34791 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 );
34792 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 );
34793 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 );
@@ -34835,11 +34844,11 @@
34844 ** structure, the second the error-code about to be returned by a pager
34845 ** API function. The value returned is a copy of the second argument
34846 ** to this function.
34847 **
34848 ** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
34849 ** the error becomes persistent. Until the persistent error is cleared,
34850 ** subsequent API calls on this Pager will immediately return the same
34851 ** error code.
34852 **
34853 ** A persistent error indicates that the contents of the pager-cache
34854 ** cannot be trusted. This state can be cleared by completely discarding
@@ -35240,13 +35249,16 @@
35249 ** the data just read from the sub-journal. Mark the page as dirty
35250 ** and if the pager requires a journal-sync, then mark the page as
35251 ** requiring a journal-sync before it is written.
35252 */
35253 assert( isSavepnt );
35254 assert( pPager->doNotSpill==0 );
35255 pPager->doNotSpill++;
35256 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
35257 assert( pPager->doNotSpill==1 );
35258 pPager->doNotSpill--;
35259 if( rc!=SQLITE_OK ) return rc;
35260 pPg->flags &= ~PGHDR_NEED_READ;
35261 sqlite3PcacheMakeDirty(pPg);
35262 }
35263 if( pPg ){
35264 /* No page should ever be explicitly rolled back that is in use, except
@@ -35346,10 +35358,13 @@
35358 int rc; /* Return code */
35359 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
35360 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
35361 char *zMasterJournal = 0; /* Contents of master journal file */
35362 i64 nMasterJournal; /* Size of master journal file */
35363 char *zJournal; /* Pointer to one journal within MJ file */
35364 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
35365 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
35366
35367 /* Allocate space for both the pJournal and pMaster file descriptors.
35368 ** If successful, open the master journal file for reading.
35369 */
35370 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
@@ -35360,77 +35375,72 @@
35375 const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
35376 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
35377 }
35378 if( rc!=SQLITE_OK ) goto delmaster_out;
35379
35380 /* Load the entire master journal file into space obtained from
35381 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
35382 ** sufficient space (in zMasterPtr) to hold the names of master
35383 ** journal files extracted from regular rollback-journals.
35384 */
35385 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
35386 if( rc!=SQLITE_OK ) goto delmaster_out;
35387 nMasterPtr = pVfs->mxPathname+1;
35388 zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
35389 if( !zMasterJournal ){
35390 rc = SQLITE_NOMEM;
35391 goto delmaster_out;
35392 }
35393 zMasterPtr = &zMasterJournal[nMasterJournal+1];
35394 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
35395 if( rc!=SQLITE_OK ) goto delmaster_out;
35396 zMasterJournal[nMasterJournal] = 0;
35397
35398 zJournal = zMasterJournal;
35399 while( (zJournal-zMasterJournal)<nMasterJournal ){
35400 int exists;
35401 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
35402 if( rc!=SQLITE_OK ){
35403 goto delmaster_out;
35404 }
35405 if( exists ){
35406 /* One of the journals pointed to by the master journal exists.
35407 ** Open it and check if it points at the master journal. If
35408 ** so, return without deleting the master journal file.
35409 */
35410 int c;
35411 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
35412 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
35413 if( rc!=SQLITE_OK ){
35414 goto delmaster_out;
35415 }
35416
35417 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
35418 sqlite3OsClose(pJournal);
35419 if( rc!=SQLITE_OK ){
35420 goto delmaster_out;
35421 }
35422
35423 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
35424 if( c ){
35425 /* We have a match. Do not delete the master journal file. */
35426 goto delmaster_out;
35427 }
35428 }
35429 zJournal += (sqlite3Strlen30(zJournal)+1);
35430 }
35431
35432 sqlite3OsClose(pMaster);
 
 
 
35433 rc = sqlite3OsDelete(pVfs, zMaster, 0);
35434
35435 delmaster_out:
35436 sqlite3_free(zMasterJournal);
 
 
35437 if( pMaster ){
35438 sqlite3OsClose(pMaster);
35439 assert( !isOpen(pJournal) );
35440 sqlite3_free(pMaster);
35441 }
 
35442 return rc;
35443 }
35444
35445
35446 /*
@@ -36125,11 +36135,11 @@
36135 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
36136 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
36137 }
36138 assert( rc!=SQLITE_DONE );
36139 }
36140 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
36141
36142 /* Finally, rollback pages from the sub-journal. Page that were
36143 ** previously rolled back out of the main journal (and are hence in pDone)
36144 ** will be skipped. Out-of-range pages are also skipped.
36145 */
@@ -37065,10 +37075,26 @@
37075 Pager *pPager = (Pager *)p;
37076 int rc = SQLITE_OK;
37077
37078 assert( pPg->pPager==pPager );
37079 assert( pPg->flags&PGHDR_DIRTY );
37080
37081 /* The doNotSyncSpill flag is set during times when doing a sync of
37082 ** journal (and adding a new header) is not allowed. This occurs
37083 ** during calls to sqlite3PagerWrite() while trying to journal multiple
37084 ** pages belonging to the same sector.
37085 **
37086 ** The doNotSpill flag inhibits all cache spilling regardless of whether
37087 ** or not a sync is required. This is set during a rollback.
37088 **
37089 ** Spilling is also inhibited when in an error state.
37090 */
37091 if( pPager->errCode ) return SQLITE_OK;
37092 if( pPager->doNotSpill ) return SQLITE_OK;
37093 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
37094 return SQLITE_OK;
37095 }
37096
37097 pPg->pDirty = 0;
37098 if( pagerUseWal(pPager) ){
37099 /* Write a single frame for this page to the log. */
37100 if( subjRequiresPage(pPg) ){
@@ -37076,33 +37102,16 @@
37102 }
37103 if( rc==SQLITE_OK ){
37104 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
37105 }
37106 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37107
37108 /* Sync the journal file if required. */
37109 if( pPg->flags&PGHDR_NEED_SYNC ){
37110 assert( !pPager->noSync );
37111 rc = syncJournal(pPager);
37112 if( rc==SQLITE_OK &&
37113 !(pPager->journalMode==PAGER_JOURNALMODE_MEMORY) &&
37114 !(sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
37115 ){
37116 pPager->nRec = 0;
37117 rc = writeJournalHdr(pPager);
@@ -38256,11 +38265,11 @@
38265 */
38266 rc = sqlite3PagerBegin(pPager, 0, pPager->subjInMemory);
38267 if( rc!=SQLITE_OK ){
38268 return rc;
38269 }
38270 if( pPager->pInJournal==0
38271 && pPager->journalMode!=PAGER_JOURNALMODE_OFF
38272 && !pagerUseWal(pPager)
38273 ){
38274 assert( pPager->useJournal );
38275 rc = pager_open_journal(pPager);
@@ -38384,39 +38393,41 @@
38393 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
38394
38395 if( nPagePerSector>1 ){
38396 Pgno nPageCount; /* Total number of pages in database file */
38397 Pgno pg1; /* First page of the sector pPg is located on. */
38398 int nPage = 0; /* Number of pages starting at pg1 to journal */
38399 int ii; /* Loop counter */
38400 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
38401
38402 /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
38403 ** a journal header to be written between the pages journaled by
38404 ** this function.
38405 */
38406 assert( !MEMDB );
38407 assert( pPager->doNotSyncSpill==0 );
38408 pPager->doNotSyncSpill++;
38409
38410 /* This trick assumes that both the page-size and sector-size are
38411 ** an integer power of 2. It sets variable pg1 to the identifier
38412 ** of the first page of the sector pPg is located on.
38413 */
38414 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
38415
38416 rc = sqlite3PagerPagecount(pPager, (int *)&nPageCount);
38417 if( rc==SQLITE_OK ){
38418 if( pPg->pgno>nPageCount ){
38419 nPage = (pPg->pgno - pg1)+1;
38420 }else if( (pg1+nPagePerSector-1)>nPageCount ){
38421 nPage = nPageCount+1-pg1;
38422 }else{
38423 nPage = nPagePerSector;
38424 }
38425 assert(nPage>0);
38426 assert(pg1<=pPg->pgno);
38427 assert((pg1+nPage)>pPg->pgno);
38428 }
38429
38430 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
38431 Pgno pg = pg1+ii;
38432 PgHdr *pPage;
38433 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
@@ -38455,12 +38466,12 @@
38466 }
38467 }
38468 assert(pPager->needSync);
38469 }
38470
38471 assert( pPager->doNotSyncSpill==1 );
38472 pPager->doNotSyncSpill--;
38473 }else{
38474 rc = pager_write(pDbPage);
38475 }
38476 return rc;
38477 }
@@ -38571,13 +38582,16 @@
38582 put32bits(((char*)pPgHdr->pData)+92, change_counter);
38583 put32bits(((char*)pPgHdr->pData)+96, SQLITE_VERSION_NUMBER);
38584
38585 /* If running in direct mode, write the contents of page 1 to the file. */
38586 if( DIRECT_MODE ){
38587 const void *zBuf;
38588 assert( pPager->dbFileSize>0 );
38589 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
38590 if( rc==SQLITE_OK ){
38591 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
38592 }
38593 if( rc==SQLITE_OK ){
38594 pPager->changeCountDone = 1;
38595 }
38596 }else{
38597 pPager->changeCountDone = 1;
@@ -38643,11 +38657,11 @@
38657
38658 /* The dbOrigSize is never set if journal_mode=OFF */
38659 assert( pPager->journalMode!=PAGER_JOURNALMODE_OFF || pPager->dbOrigSize==0 );
38660
38661 /* If a prior error occurred, report that error again. */
38662 if( pPager->errCode ) return pPager->errCode;
38663
38664 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
38665 pPager->zFilename, zMaster, pPager->dbSize));
38666
38667 if( MEMDB && pPager->dbModified ){
@@ -38821,14 +38835,15 @@
38835 ** But if (due to a coding error elsewhere in the system) it does get
38836 ** called, just return the same error code without doing anything. */
38837 if( NEVER(pPager->errCode) ) return pPager->errCode;
38838
38839 /* This function should not be called if the pager is not in at least
38840 ** PAGER_RESERVED state. **FIXME**: Make it so that this test always
38841 ** fails - make it so that we never reach this point if we do not hold
38842 ** all necessary locks.
38843 */
38844 if( pPager->state<PAGER_RESERVED ) return SQLITE_ERROR;
38845
38846 /* An optimization. If the database was not actually modified during
38847 ** this transaction, the pager is running in exclusive-mode and is
38848 ** using persistent journals, then this function is a no-op.
38849 **
@@ -38873,11 +38888,11 @@
38888 ** performed. If successful, task (2). Regardless of the outcome
38889 ** of either, the error state error code is returned to the caller
38890 ** (i.e. either SQLITE_IOERR or SQLITE_CORRUPT).
38891 **
38892 ** * If the pager is in PAGER_RESERVED state, then attempt (1). Whether
38893 ** or not (1) is successful, also attempt (2). If successful, return
38894 ** SQLITE_OK. Otherwise, enter the error state and return the first
38895 ** error code encountered.
38896 **
38897 ** In this case there is no chance that the database was written to.
38898 ** So is safe to finalize the journal file even if the playback
@@ -39647,11 +39662,27 @@
39662 sqlite3OsUnlock(pPager->fd, SQLITE_LOCK_SHARED);
39663 }
39664 }
39665 return rc;
39666 }
39667
39668 #ifdef SQLITE_HAS_CODEC
39669 /*
39670 ** This function is called by the wal module when writing page content
39671 ** into the log file.
39672 **
39673 ** This function returns a pointer to a buffer containing the encrypted
39674 ** page content. If a malloc fails, this function may return NULL.
39675 */
39676 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
39677 void *aData = 0;
39678 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
39679 return aData;
39680 }
39681 #endif /* SQLITE_HAS_CODEC */
39682
39683 #endif /* !SQLITE_OMIT_WAL */
39684
39685 #endif /* SQLITE_OMIT_DISKIO */
39686
39687 /************** End of pager.c ***********************************************/
39688 /************** Begin file wal.c *********************************************/
@@ -39686,23 +39717,25 @@
39717 ** frames can overwrite the old ones. A WAL always grows from beginning
39718 ** toward the end. Checksums and counters attached to each frame are
39719 ** used to determine which frames within the WAL are valid and which
39720 ** are leftovers from prior checkpoints.
39721 **
39722 ** The WAL header is 32 bytes in size and consists of the following eight
39723 ** big-endian 32-bit unsigned integer values:
39724 **
39725 ** 0: Magic number. 0x377f0682 or 0x377f0683
39726 ** 4: File format version. Currently 3007000
39727 ** 8: Database page size. Example: 1024
39728 ** 12: Checkpoint sequence number
39729 ** 16: Salt-1, random integer incremented with each checkpoint
39730 ** 20: Salt-2, a different random integer changing with each ckpt
39731 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
39732 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
39733 **
39734 ** Immediately following the wal-header are zero or more frames. Each
39735 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
39736 ** of page data. The frame-header is six big-endian 32-bit unsigned
39737 ** integer values, as follows:
39738 **
39739 ** 0: Page number.
39740 ** 4: For commit records, the size of the database image in pages
39741 ** after the commit. For all other records, zero.
@@ -39733,10 +39766,15 @@
39766 **
39767 ** for i from 0 to n-1 step 2:
39768 ** s0 += x[i] + s1;
39769 ** s1 += x[i+1] + s0;
39770 ** endfor
39771 **
39772 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
39773 ** in reverse order (the largest fibonacci weight occurs on the first element
39774 ** of the sequence being summed.) The s1 value spans all 32-bit
39775 ** terms of the sequence whereas s0 omits the final term.
39776 **
39777 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
39778 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
39779 ** The VFS.xSync operations serve as write barriers - all writes launched
39780 ** before the xSync must complete before any write that launches after the
@@ -39900,10 +39938,25 @@
39938 # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
39939 #else
39940 # define WALTRACE(X)
39941 #endif
39942
39943 /*
39944 ** The maximum (and only) versions of the wal and wal-index formats
39945 ** that may be interpreted by this version of SQLite.
39946 **
39947 ** If a client begins recovering a WAL file and finds that (a) the checksum
39948 ** values in the wal-header are correct and (b) the version field is not
39949 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
39950 **
39951 ** Similarly, if a client successfully reads a wal-index header (i.e. the
39952 ** checksum test is successful) and finds that the version field is not
39953 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
39954 ** returns SQLITE_CANTOPEN.
39955 */
39956 #define WAL_MAX_VERSION 3007000
39957 #define WALINDEX_MAX_VERSION 3007000
39958
39959 /*
39960 ** Indices of various locking bytes. WAL_NREADER is the number
39961 ** of available reader locks and should be at least 3.
39962 */
@@ -39926,10 +39979,12 @@
39979 **
39980 ** The actual header in the wal-index consists of two copies of this
39981 ** object.
39982 */
39983 struct WalIndexHdr {
39984 u32 iVersion; /* Wal-index version */
39985 u32 unused; /* Unused (padding) field */
39986 u32 iChange; /* Counter incremented each transaction */
39987 u8 isInit; /* 1 when initialized */
39988 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
39989 u16 szPage; /* Database page size in bytes */
39990 u32 mxFrame; /* Index of last valid frame in the WAL */
@@ -40005,12 +40060,13 @@
40060 #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
40061
40062 /* Size of header before each frame in wal */
40063 #define WAL_FRAME_HDRSIZE 24
40064
40065 /* Size of write ahead log header, including checksum. */
40066 /* #define WAL_HDRSIZE 24 */
40067 #define WAL_HDRSIZE 32
40068
40069 /* WAL magic value. Either this value, or the same value with the least
40070 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
40071 ** big-endian format in the first 4 bytes of a WAL file.
40072 **
@@ -40234,10 +40290,11 @@
40290 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
40291 const int nCksum = offsetof(WalIndexHdr, aCksum);
40292
40293 assert( pWal->writeLock );
40294 pWal->hdr.isInit = 1;
40295 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
40296 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
40297 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40298 sqlite3OsShmBarrier(pWal->pDbFd);
40299 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
40300 }
@@ -40683,10 +40740,11 @@
40740 u8 *aData; /* Pointer to data part of aFrame buffer */
40741 int iFrame; /* Index of last frame read */
40742 i64 iOffset; /* Next offset to read from log file */
40743 int szPage; /* Page size according to the log */
40744 u32 magic; /* Magic value read from WAL header */
40745 u32 version; /* Magic value read from WAL header */
40746
40747 /* Read in the WAL header. */
40748 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
40749 if( rc!=SQLITE_OK ){
40750 goto recovery_error;
@@ -40708,13 +40766,28 @@
40766 }
40767 pWal->hdr.bigEndCksum = (magic&0x00000001);
40768 pWal->szPage = szPage;
40769 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
40770 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
40771
40772 /* Verify that the WAL header checksum is correct */
40773 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
40774 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
40775 );
40776 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
40777 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
40778 ){
40779 goto finished;
40780 }
40781
40782 /* Verify that the version number on the WAL format is one that
40783 ** are able to understand */
40784 version = sqlite3Get4byte(&aBuf[4]);
40785 if( version!=WAL_MAX_VERSION ){
40786 rc = SQLITE_CANTOPEN_BKPT;
40787 goto finished;
40788 }
40789
40790 /* Malloc a buffer to read frames into. */
40791 szFrame = szPage + WAL_FRAME_HDRSIZE;
40792 aFrame = (u8 *)sqlite3_malloc(szFrame);
40793 if( !aFrame ){
@@ -40899,53 +40972,101 @@
40972
40973 *piPage = p->iPrior = iRet;
40974 return (iRet==0xFFFFFFFF);
40975 }
40976
40977 /*
40978 ** This function merges two sorted lists into a single sorted list.
40979 */
40980 static void walMerge(
40981 u32 *aContent, /* Pages in wal */
40982 ht_slot *aLeft, /* IN: Left hand input list */
40983 int nLeft, /* IN: Elements in array *paLeft */
40984 ht_slot **paRight, /* IN/OUT: Right hand input list */
40985 int *pnRight, /* IN/OUT: Elements in *paRight */
40986 ht_slot *aTmp /* Temporary buffer */
40987 ){
40988 int iLeft = 0; /* Current index in aLeft */
40989 int iRight = 0; /* Current index in aRight */
40990 int iOut = 0; /* Current index in output buffer */
40991 int nRight = *pnRight;
40992 ht_slot *aRight = *paRight;
40993
40994 assert( nLeft>0 && nRight>0 );
40995 while( iRight<nRight || iLeft<nLeft ){
40996 ht_slot logpage;
40997 Pgno dbpage;
40998
40999 if( (iLeft<nLeft)
41000 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
41001 ){
41002 logpage = aLeft[iLeft++];
41003 }else{
41004 logpage = aRight[iRight++];
41005 }
41006 dbpage = aContent[logpage];
41007
41008 aTmp[iOut++] = logpage;
41009 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
41010
41011 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
41012 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
41013 }
41014
41015 *paRight = aLeft;
41016 *pnRight = iOut;
41017 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
41018 }
41019
41020 /*
41021 ** Sort the elements in list aList, removing any duplicates.
41022 */
41023 static void walMergesort(
41024 u32 *aContent, /* Pages in wal */
41025 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
41026 ht_slot *aList, /* IN/OUT: List to sort */
41027 int *pnList /* IN/OUT: Number of elements in aList[] */
41028 ){
41029 struct Sublist {
41030 int nList; /* Number of elements in aList */
41031 ht_slot *aList; /* Pointer to sub-list content */
41032 };
41033
41034 const int nList = *pnList; /* Size of input list */
41035 int nMerge; /* Number of elements in list aMerge */
41036 ht_slot *aMerge; /* List to be merged */
41037 int iList; /* Index into input list */
41038 int iSub = 0; /* Index into aSub array */
41039 struct Sublist aSub[13]; /* Array of sub-lists */
41040
41041 memset(aSub, 0, sizeof(aSub));
41042 assert( nList<=HASHTABLE_NPAGE && nList>0 );
41043 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
41044
41045 for(iList=0; iList<nList; iList++){
41046 nMerge = 1;
41047 aMerge = &aList[iList];
41048 for(iSub=0; iList & (1<<iSub); iSub++){
41049 struct Sublist *p = &aSub[iSub];
41050 assert( p->aList && p->nList<=(1<<iSub) );
41051 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
41052 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
41053 }
41054 aSub[iSub].aList = aMerge;
41055 aSub[iSub].nList = nMerge;
41056 }
41057
41058 for(iSub++; iSub<ArraySize(aSub); iSub++){
41059 if( nList & (1<<iSub) ){
41060 struct Sublist *p = &aSub[iSub];
41061 assert( p->nList<=(1<<iSub) );
41062 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
41063 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
41064 }
41065 }
41066 assert( aMerge==aList );
41067 *pnList = nMerge;
41068
41069 #ifdef SQLITE_DEBUG
41070 {
41071 int i;
41072 for(i=1; i<*pnList; i++){
@@ -40957,91 +41078,94 @@
41078
41079 /*
41080 ** Free an iterator allocated by walIteratorInit().
41081 */
41082 static void walIteratorFree(WalIterator *p){
41083 sqlite3ScratchFree(p);
41084 }
41085
41086 /*
41087 ** Construct a WalInterator object that can be used to loop over all
41088 ** pages in the WAL in ascending order. The caller must hold the checkpoint
 
41089 **
41090 ** On success, make *pp point to the newly allocated WalInterator object
41091 ** return SQLITE_OK. Otherwise, return an error code. If this routine
41092 ** returns an error, the value of *pp is undefined.
41093 **
41094 ** The calling routine should invoke walIteratorFree() to destroy the
41095 ** WalIterator object when it has finished with it.
 
 
41096 */
41097 static int walIteratorInit(Wal *pWal, WalIterator **pp){
41098 WalIterator *p; /* Return value */
41099 int nSegment; /* Number of segments to merge */
41100 u32 iLast; /* Last frame in log */
41101 int nByte; /* Number of bytes to allocate */
41102 int i; /* Iterator variable */
41103 ht_slot *aTmp; /* Temp space used by merge-sort */
41104 int rc = SQLITE_OK; /* Return Code */
41105
41106 /* This routine only runs while holding the checkpoint lock. And
41107 ** it only runs if there is actually content in the log (mxFrame>0).
41108 */
41109 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
 
 
41110 iLast = pWal->hdr.mxFrame;
41111
41112 /* Allocate space for the WalIterator object. */
41113 nSegment = walFramePage(iLast) + 1;
41114 nByte = sizeof(WalIterator)
41115 + (nSegment-1)*sizeof(struct WalSegment)
41116 + iLast*sizeof(ht_slot);
41117 p = (WalIterator *)sqlite3ScratchMalloc(nByte);
41118 if( !p ){
41119 return SQLITE_NOMEM;
41120 }
41121 memset(p, 0, nByte);
 
 
41122 p->nSegment = nSegment;
41123
41124 /* Allocate temporary space used by the merge-sort routine. This block
41125 ** of memory will be freed before this function returns.
41126 */
41127 aTmp = (ht_slot *)sqlite3ScratchMalloc(
41128 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
41129 );
41130 if( !aTmp ){
41131 rc = SQLITE_NOMEM;
41132 }
41133
41134 for(i=0; rc==SQLITE_OK && i<nSegment; i++){
41135 volatile ht_slot *aHash;
 
41136 u32 iZero;
 
41137 volatile u32 *aPgno;
 
41138
41139 rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
41140 if( rc==SQLITE_OK ){
41141 int j; /* Counter variable */
41142 int nEntry; /* Number of entries in this segment */
41143 ht_slot *aIndex; /* Sorted index for this segment */
41144
41145 aPgno++;
41146 nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)aPgno;
41147 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
41148 iZero++;
41149
41150 for(j=0; j<nEntry; j++){
41151 aIndex[j] = j;
41152 }
41153 walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
41154 p->aSegment[i].iZero = iZero;
41155 p->aSegment[i].nEntry = nEntry;
41156 p->aSegment[i].aIndex = aIndex;
41157 p->aSegment[i].aPgno = (u32 *)aPgno;
41158 }
41159 }
41160 sqlite3ScratchFree(aTmp);
41161
41162 if( rc!=SQLITE_OK ){
41163 walIteratorFree(p);
41164 }
41165 *pp = p;
41166 return rc;
41167 }
41168
41169 /*
41170 ** Copy as much content as we can from the WAL back into the database file
41171 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
@@ -41086,15 +41210,18 @@
41210 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
41211 u32 mxSafeFrame; /* Max frame that can be backfilled */
41212 int i; /* Loop counter */
41213 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
41214
41215 if( pWal->hdr.mxFrame==0 ) return SQLITE_OK;
41216
41217 /* Allocate the iterator */
41218 rc = walIteratorInit(pWal, &pIter);
41219 if( rc!=SQLITE_OK ){
41220 return rc;
41221 }
41222 assert( pIter );
41223
41224 /*** TODO: Move this test out to the caller. Make it an assert() here ***/
41225 if( pWal->hdr.szPage!=nBuf ){
41226 rc = SQLITE_CORRUPT_BKPT;
41227 goto walcheckpoint_out;
@@ -41275,30 +41402,24 @@
41402 return 0;
41403 }
41404
41405 /*
41406 ** Read the wal-index header from the wal-index and into pWal->hdr.
41407 ** If the wal-header appears to be corrupt, try to reconstruct the
41408 ** wal-index from the WAL before returning.
41409 **
41410 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
41411 ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged
41412 ** to 0.
41413 **
 
 
 
 
 
 
41414 ** If the wal-index header is successfully read, return SQLITE_OK.
41415 ** Otherwise an SQLite error code.
41416 */
41417 static int walIndexReadHdr(Wal *pWal, int *pChanged){
41418 int rc; /* Return code */
41419 int badHdr; /* True if a header read failed */
41420 volatile u32 *page0; /* Chunk of wal-index containing header */
41421
41422 /* Ensure that page 0 of the wal-index (the page that contains the
41423 ** wal-index header) is mapped. Return early if an error occurs here.
41424 */
41425 assert( pChanged );
@@ -41309,11 +41430,11 @@
41430 assert( page0 || pWal->writeLock==0 );
41431
41432 /* If the first page of the wal-index has been mapped, try to read the
41433 ** wal-index header immediately, without holding any lock. This usually
41434 ** works, but may fail if the wal-index header is corrupt or currently
41435 ** being modified by another thread or process.
41436 */
41437 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
41438
41439 /* If the first attempt failed, it might have been due to a race
41440 ** with a writer. So get a WRITE lock and try again.
@@ -41333,10 +41454,18 @@
41454 }
41455 }
41456 pWal->writeLock = 0;
41457 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
41458 }
41459
41460 /* If the header is read successfully, check the version number to make
41461 ** sure the wal-index was not constructed with some future format that
41462 ** this version of SQLite cannot understand.
41463 */
41464 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
41465 rc = SQLITE_CANTOPEN_BKPT;
41466 }
41467
41468 return rc;
41469 }
41470
41471 /*
@@ -41348,13 +41477,33 @@
41477 /*
41478 ** Attempt to start a read transaction. This might fail due to a race or
41479 ** other transient condition. When that happens, it returns WAL_RETRY to
41480 ** indicate to the caller that it is safe to retry immediately.
41481 **
41482 ** On success return SQLITE_OK. On a permanent failure (such an
41483 ** I/O error or an SQLITE_BUSY because another process is running
41484 ** recovery) return a positive error code.
41485 **
41486 ** The useWal parameter is true to force the use of the WAL and disable
41487 ** the case where the WAL is bypassed because it has been completely
41488 ** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
41489 ** to make a copy of the wal-index header into pWal->hdr. If the
41490 ** wal-index header has changed, *pChanged is set to 1 (as an indication
41491 ** to the caller that the local paget cache is obsolete and needs to be
41492 ** flushed.) When useWal==1, the wal-index header is assumed to already
41493 ** be loaded and the pChanged parameter is unused.
41494 **
41495 ** The caller must set the cnt parameter to the number of prior calls to
41496 ** this routine during the current read attempt that returned WAL_RETRY.
41497 ** This routine will start taking more aggressive measures to clear the
41498 ** race conditions after multiple WAL_RETRY returns, and after an excessive
41499 ** number of errors will ultimately return SQLITE_PROTOCOL. The
41500 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
41501 ** and is not honoring the locking protocol. There is a vanishingly small
41502 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
41503 ** bad luck when there is lots of contention for the wal-index, but that
41504 ** possibility is so small that it can be safely neglected, we believe.
41505 **
41506 ** On success, this routine obtains a read lock on
41507 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
41508 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
41509 ** that means the Wal does not hold any read lock. The reader must not
@@ -41361,10 +41510,12 @@
41510 ** access any database page that is modified by a WAL frame up to and
41511 ** including frame number aReadMark[pWal->readLock]. The reader will
41512 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
41513 ** Or if pWal->readLock==0, then the reader will ignore the WAL
41514 ** completely and get all content directly from the database file.
41515 ** If the useWal parameter is 1 then the WAL will never be ignored and
41516 ** this routine will always set pWal->readLock>0 on success.
41517 ** When the read transaction is completed, the caller must release the
41518 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
41519 **
41520 ** This routine uses the nBackfill and aReadMark[] fields of the header
41521 ** to select a particular WAL_READ_LOCK() that strives to let the
@@ -41405,13 +41556,13 @@
41556 rc = WAL_RETRY;
41557 }else if( rc==SQLITE_BUSY ){
41558 rc = SQLITE_BUSY_RECOVERY;
41559 }
41560 }
41561 if( rc!=SQLITE_OK ){
41562 return rc;
41563 }
41564 }
41565
41566 pInfo = walCkptInfo(pWal);
41567 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
41568 /* The WAL has been completely backfilled (or it is empty).
@@ -41583,13 +41734,13 @@
41734 /* This routine is only be called from within a read transaction. */
41735 assert( pWal->readLock>=0 || pWal->lockError );
41736
41737 /* If the "last page" field of the wal-index header snapshot is 0, then
41738 ** no data will be read from the wal under any circumstances. Return early
41739 ** in this case as an optimization. Likewise, if pWal->readLock==0,
41740 ** then the WAL is ignored by the reader so return early, as if the
41741 ** WAL were empty.
41742 */
41743 if( iLast==0 || pWal->readLock==0 ){
41744 *pInWal = 0;
41745 return SQLITE_OK;
41746 }
@@ -41596,11 +41747,11 @@
41747
41748 /* Search the hash table or tables for an entry matching page number
41749 ** pgno. Each iteration of the following for() loop searches one
41750 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
41751 **
41752 ** This code might run concurrently to the code in walIndexAppend()
41753 ** that adds entries to the wal-index (and possibly to this hash
41754 ** table). This means the value just read from the hash
41755 ** slot (aHash[iKey]) may have been added before or after the
41756 ** current read transaction was opened. Values added after the
41757 ** read transaction was opened may have been written incorrectly -
@@ -41919,44 +42070,59 @@
42070 ** header to the start of the WAL file. See comments at the top of
42071 ** this source file for a description of the WAL header format.
42072 */
42073 iFrame = pWal->hdr.mxFrame;
42074 if( iFrame==0 ){
42075 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
42076 u32 aCksum[2]; /* Checksum for wal-header */
42077
42078 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
42079 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
42080 sqlite3Put4byte(&aWalHdr[8], szPage);
 
 
42081 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
42082 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
42083 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
42084 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
42085 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
42086
42087 pWal->szPage = szPage;
42088 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
42089 pWal->hdr.aFrameCksum[0] = aCksum[0];
42090 pWal->hdr.aFrameCksum[1] = aCksum[1];
42091
42092 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
42093 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
42094 if( rc!=SQLITE_OK ){
42095 return rc;
42096 }
 
42097 }
42098 assert( pWal->szPage==szPage );
42099
42100 /* Write the log file. */
42101 for(p=pList; p; p=p->pDirty){
42102 u32 nDbsize; /* Db-size field for frame header */
42103 i64 iOffset; /* Write offset in log file */
42104 void *pData;
42105
42106
42107 iOffset = walFrameOffset(++iFrame, szPage);
42108
42109 /* Populate and write the frame header */
42110 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
42111 #if defined(SQLITE_HAS_CODEC)
42112 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
42113 #else
42114 pData = p->pData;
42115 #endif
42116 walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
42117 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
42118 if( rc!=SQLITE_OK ){
42119 return rc;
42120 }
42121
42122 /* Write the page data */
42123 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
42124 if( rc!=SQLITE_OK ){
42125 return rc;
42126 }
42127 pLast = p;
42128 }
@@ -41969,18 +42135,23 @@
42135 assert( isCommit );
42136 assert( iSegment>0 );
42137
42138 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
42139 while( iOffset<iSegment ){
42140 void *pData;
42141 #if defined(SQLITE_HAS_CODEC)
42142 if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
42143 #else
42144 pData = pLast->pData;
42145 #endif
42146 walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
42147 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
42148 if( rc!=SQLITE_OK ){
42149 return rc;
42150 }
 
42151 iOffset += WAL_FRAME_HDRSIZE;
42152 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
42153 if( rc!=SQLITE_OK ){
42154 return rc;
42155 }
42156 nLast++;
42157 iOffset += szPage;
@@ -52844,13 +53015,20 @@
53015 if( !pExpr ){
53016 *ppVal = 0;
53017 return SQLITE_OK;
53018 }
53019 op = pExpr->op;
53020
53021 /* op can only be TK_REGISTER is we have compiled with SQLITE_ENABLE_STAT2.
53022 ** The ifdef here is to enable us to achieve 100% branch test coverage even
53023 ** when SQLITE_ENABLE_STAT2 is omitted.
53024 */
53025 #ifdef SQLITE_ENABLE_STAT2
53026 if( op==TK_REGISTER ) op = pExpr->op2;
53027 #else
53028 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
53029 #endif
53030
53031 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
53032 pVal = sqlite3ValueNew(db);
53033 if( pVal==0 ) goto no_mem;
53034 if( ExprHasProperty(pExpr, EP_IntValue) ){
@@ -63351,10 +63529,14 @@
63529 */
63530 rc = sqlite3PagerCloseWal(u.cd.pPager);
63531 if( rc==SQLITE_OK ){
63532 sqlite3PagerSetJournalMode(u.cd.pPager, u.cd.eNew);
63533 }
63534 }else if( u.cd.eOld==PAGER_JOURNALMODE_MEMORY ){
63535 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
63536 ** as an intermediate */
63537 sqlite3PagerSetJournalMode(u.cd.pPager, PAGER_JOURNALMODE_OFF);
63538 }
63539
63540 /* Open a transaction on the database file. Regardless of the journal
63541 ** mode, this transaction always uses a rollback journal.
63542 */
@@ -103905,21 +104087,26 @@
104087 ** a low-level error is first detected.
104088 */
104089 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
104090 testcase( sqlite3GlobalConfig.xLog!=0 );
104091 sqlite3_log(SQLITE_CORRUPT,
104092 "database corruption at line %d of [%.10s]",
104093 lineno, 20+sqlite3_sourceid());
104094 return SQLITE_CORRUPT;
104095 }
104096 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
104097 testcase( sqlite3GlobalConfig.xLog!=0 );
104098 sqlite3_log(SQLITE_MISUSE,
104099 "misuse at line %d of [%.10s]",
104100 lineno, 20+sqlite3_sourceid());
104101 return SQLITE_MISUSE;
104102 }
104103 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
104104 testcase( sqlite3GlobalConfig.xLog!=0 );
104105 sqlite3_log(SQLITE_CANTOPEN,
104106 "cannot open file at line %d of [%.10s]",
104107 lineno, 20+sqlite3_sourceid());
104108 return SQLITE_CANTOPEN;
104109 }
104110
104111
104112 #ifndef SQLITE_OMIT_DEPRECATED
104113
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.0"
111111
#define SQLITE_VERSION_NUMBER 3007000
112
-#define SQLITE_SOURCE_ID "2010-06-21 12:47:41 ee0acef1faffd480fd2136f81fb2b6f6a17b5388"
112
+#define SQLITE_SOURCE_ID "2010-06-26 20:25:31 f149b498b6ada3fc9f71ee104c351554c80c7f8a"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.0"
111 #define SQLITE_VERSION_NUMBER 3007000
112 #define SQLITE_SOURCE_ID "2010-06-21 12:47:41 ee0acef1faffd480fd2136f81fb2b6f6a17b5388"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.0"
111 #define SQLITE_VERSION_NUMBER 3007000
112 #define SQLITE_SOURCE_ID "2010-06-26 20:25:31 f149b498b6ada3fc9f71ee104c351554c80c7f8a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
+3 -3
--- src/update.c
+++ src/update.c
@@ -211,22 +211,22 @@
211211
printf("CONFLICT %s\n", zName);
212212
}else if( idt>0 && idv==0 ){
213213
/* File added in the target. */
214214
printf("ADD %s\n", zName);
215215
undo_save(zName);
216
- if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
216
+ if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
217217
}else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
218218
/* The file is unedited. Change it to the target version */
219219
printf("UPDATE %s\n", zName);
220220
undo_save(zName);
221
- if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
221
+ if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
222222
}else if( idt>0 && idv>0 && file_size(zFullPath)<0 ){
223223
/* The file missing from the local check-out. Restore it to the
224224
** version that appears in the target. */
225225
printf("UPDATE %s\n", zName);
226226
undo_save(zName);
227
- if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
227
+ if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
228228
}else if( idt==0 && idv>0 ){
229229
if( ridv==0 ){
230230
/* Added in current checkout. Continue to hold the file as
231231
** as an addition */
232232
db_multi_exec("UPDATE vfile SET vid=%d WHERE id=%d", tid, idv);
233233
--- src/update.c
+++ src/update.c
@@ -211,22 +211,22 @@
211 printf("CONFLICT %s\n", zName);
212 }else if( idt>0 && idv==0 ){
213 /* File added in the target. */
214 printf("ADD %s\n", zName);
215 undo_save(zName);
216 if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
217 }else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
218 /* The file is unedited. Change it to the target version */
219 printf("UPDATE %s\n", zName);
220 undo_save(zName);
221 if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
222 }else if( idt>0 && idv>0 && file_size(zFullPath)<0 ){
223 /* The file missing from the local check-out. Restore it to the
224 ** version that appears in the target. */
225 printf("UPDATE %s\n", zName);
226 undo_save(zName);
227 if( !nochangeFlag ) vfile_to_disk(0, idt, 0);
228 }else if( idt==0 && idv>0 ){
229 if( ridv==0 ){
230 /* Added in current checkout. Continue to hold the file as
231 ** as an addition */
232 db_multi_exec("UPDATE vfile SET vid=%d WHERE id=%d", tid, idv);
233
--- src/update.c
+++ src/update.c
@@ -211,22 +211,22 @@
211 printf("CONFLICT %s\n", zName);
212 }else if( idt>0 && idv==0 ){
213 /* File added in the target. */
214 printf("ADD %s\n", zName);
215 undo_save(zName);
216 if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
217 }else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
218 /* The file is unedited. Change it to the target version */
219 printf("UPDATE %s\n", zName);
220 undo_save(zName);
221 if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
222 }else if( idt>0 && idv>0 && file_size(zFullPath)<0 ){
223 /* The file missing from the local check-out. Restore it to the
224 ** version that appears in the target. */
225 printf("UPDATE %s\n", zName);
226 undo_save(zName);
227 if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
228 }else if( idt==0 && idv>0 ){
229 if( ridv==0 ){
230 /* Added in current checkout. Continue to hold the file as
231 ** as an addition */
232 db_multi_exec("UPDATE vfile SET vid=%d WHERE id=%d", tid, idv);
233
+23 -1
--- src/vfile.c
+++ src/vfile.c
@@ -204,11 +204,16 @@
204204
205205
/*
206206
** Write all files from vid to the disk. Or if vid==0 and id!=0
207207
** write just the specific file where VFILE.ID=id.
208208
*/
209
-void vfile_to_disk(int vid, int id, int verbose){
209
+void vfile_to_disk(
210
+ int vid, /* vid to write to disk */
211
+ int id, /* Write this one file, if not zero */
212
+ int verbose, /* Output progress information */
213
+ int promptFlag /* Prompt user to confirm overwrites */
214
+){
210215
Stmt q;
211216
Blob content;
212217
int nRepos = strlen(g.zLocalRoot);
213218
214219
if( vid>0 && id==0 ){
@@ -228,10 +233,27 @@
228233
const char *zName;
229234
230235
id = db_column_int(&q, 0);
231236
zName = db_column_text(&q, 1);
232237
rid = db_column_int(&q, 2);
238
+ if( promptFlag ){
239
+ if( file_size(zName)>=0 ){
240
+ Blob ans;
241
+ char *zMsg;
242
+ char cReply;
243
+ zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName);
244
+ prompt_user(zMsg, &ans);
245
+ free(zMsg);
246
+ cReply = blob_str(&ans)[0];
247
+ blob_reset(&ans);
248
+ if( cReply=='a' || cReply=='A' ){
249
+ promptFlag = 0;
250
+ cReply = 'y';
251
+ }
252
+ if( cReply=='n' || cReply=='N' ) continue;
253
+ }
254
+ }
233255
content_get(rid, &content);
234256
if( verbose ) printf("%s\n", &zName[nRepos]);
235257
blob_write_to_file(&content, zName);
236258
blob_reset(&content);
237259
db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
238260
--- src/vfile.c
+++ src/vfile.c
@@ -204,11 +204,16 @@
204
205 /*
206 ** Write all files from vid to the disk. Or if vid==0 and id!=0
207 ** write just the specific file where VFILE.ID=id.
208 */
209 void vfile_to_disk(int vid, int id, int verbose){
 
 
 
 
 
210 Stmt q;
211 Blob content;
212 int nRepos = strlen(g.zLocalRoot);
213
214 if( vid>0 && id==0 ){
@@ -228,10 +233,27 @@
228 const char *zName;
229
230 id = db_column_int(&q, 0);
231 zName = db_column_text(&q, 1);
232 rid = db_column_int(&q, 2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233 content_get(rid, &content);
234 if( verbose ) printf("%s\n", &zName[nRepos]);
235 blob_write_to_file(&content, zName);
236 blob_reset(&content);
237 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
238
--- src/vfile.c
+++ src/vfile.c
@@ -204,11 +204,16 @@
204
205 /*
206 ** Write all files from vid to the disk. Or if vid==0 and id!=0
207 ** write just the specific file where VFILE.ID=id.
208 */
209 void vfile_to_disk(
210 int vid, /* vid to write to disk */
211 int id, /* Write this one file, if not zero */
212 int verbose, /* Output progress information */
213 int promptFlag /* Prompt user to confirm overwrites */
214 ){
215 Stmt q;
216 Blob content;
217 int nRepos = strlen(g.zLocalRoot);
218
219 if( vid>0 && id==0 ){
@@ -228,10 +233,27 @@
233 const char *zName;
234
235 id = db_column_int(&q, 0);
236 zName = db_column_text(&q, 1);
237 rid = db_column_int(&q, 2);
238 if( promptFlag ){
239 if( file_size(zName)>=0 ){
240 Blob ans;
241 char *zMsg;
242 char cReply;
243 zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName);
244 prompt_user(zMsg, &ans);
245 free(zMsg);
246 cReply = blob_str(&ans)[0];
247 blob_reset(&ans);
248 if( cReply=='a' || cReply=='A' ){
249 promptFlag = 0;
250 cReply = 'y';
251 }
252 if( cReply=='n' || cReply=='N' ) continue;
253 }
254 }
255 content_get(rid, &content);
256 if( verbose ) printf("%s\n", &zName[nRepos]);
257 blob_write_to_file(&content, zName);
258 blob_reset(&content);
259 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
260

Keyboard Shortcuts

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