Handle recovering databases with reserved bytes at the end of each page.

FossilOrigin-Name: 52d0235ef3f21683daadf2343514f7b95a67c3589266f75c1e00e04f11dc6214
This commit is contained in:
dan 2022-10-14 15:46:19 +00:00
parent 249efd04d7
commit 3fa10f82f4
5 changed files with 67 additions and 37 deletions

View File

@ -47,7 +47,7 @@
** **
** sqlite3 *db; ** sqlite3 *db;
** sqlite3_open(":memory:", &db); ** sqlite3_open(":memory:", &db);
** sqlite3_load_extention(db, "./cksumvfs"); ** sqlite3_load_extension(db, "./cksumvfs");
** sqlite3_close(db); ** sqlite3_close(db);
** **
** If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and ** If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and

View File

@ -222,5 +222,29 @@ do_test 12.3 {
$R finish $R finish
} {} } {}
#-------------------------------------------------------------------------
reset_db
file_control_reservebytes db 16
do_execsql_test 12.1 {
PRAGMA auto_vacuum = 2;
PRAGMA user_version = 45;
PRAGMA application_id = 22;
CREATE TABLE u1(u, v);
CREATE UNIQUE INDEX i1 ON u1(u, v);
INSERT INTO u1 VALUES(1, 2), (3, 4);
CREATE TABLE u2(u, v);
CREATE UNIQUE INDEX i2 ON u1(u, v);
INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000)));
INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000)));
INSERT INTO u2 VALUES(hex(randomblob(500)), hex(randomblob(1000)));
INSERT INTO u2 VALUES(hex(randomblob(50000)), hex(randomblob(20000)));
}
do_recover_test 12
finish_test finish_test

View File

@ -194,6 +194,7 @@ struct sqlite3_recover {
int pgsz; int pgsz;
int detected_pgsz; int detected_pgsz;
int nReserve;
u8 *pPage1Disk; u8 *pPage1Disk;
u8 *pPage1Cache; u8 *pPage1Cache;
@ -667,20 +668,13 @@ static void recoverGetPage(
if( pStmt ){ if( pStmt ){
sqlite3_bind_int64(pStmt, 1, pgno); sqlite3_bind_int64(pStmt, 1, pgno);
if( SQLITE_ROW==sqlite3_step(pStmt) ){ if( SQLITE_ROW==sqlite3_step(pStmt) ){
int bDone = 0;
assert( p->errCode==SQLITE_OK ); assert( p->errCode==SQLITE_OK );
if( pgno==1 ){ const u8 *aPg = sqlite3_column_blob(pStmt, 0);
const u8 *aPg = sqlite3_column_blob(pStmt, 0); int nPg = sqlite3_column_bytes(pStmt, 0);
int nPg = sqlite3_column_bytes(pStmt, 0); if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){
if( nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){ aPg = p->pPage1Disk;
sqlite3_result_blob(pCtx, p->pPage1Disk, nPg, SQLITE_STATIC);
bDone = 1;
}
}
if( !bDone ){
sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0));
} }
sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT);
} }
recoverReset(p, pStmt); recoverReset(p, pStmt);
} }
@ -2097,6 +2091,7 @@ static void recoverPutU32(u8 *a, u32 v){
static int recoverVfsDetectPagesize( static int recoverVfsDetectPagesize(
sqlite3_recover *p, sqlite3_recover *p,
sqlite3_file *pFd, sqlite3_file *pFd,
u32 nReserve, /* Possible nReserve value */
i64 nSz i64 nSz
){ ){
int rc = SQLITE_OK; int rc = SQLITE_OK;
@ -2116,23 +2111,31 @@ static int recoverVfsDetectPagesize(
nBlk = (nSz+nMax-1)/nMax; nBlk = (nSz+nMax-1)/nMax;
if( nBlk>nMaxBlk ) nBlk = nMaxBlk; if( nBlk>nMaxBlk ) nBlk = nMaxBlk;
for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){ do {
int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax); for(iBlk=0; rc==SQLITE_OK && iBlk<nBlk; iBlk++){
memset(aPg, 0, nMax); int nByte = (nSz>=((iBlk+1)*nMax)) ? nMax : (nSz % nMax);
rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax); memset(aPg, 0, nMax);
if( rc==SQLITE_OK ){ rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax);
int pgsz2; if( rc==SQLITE_OK ){
for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){ int pgsz2;
int iOff; for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){
for(iOff=0; iOff<nMax; iOff+=pgsz2){ int iOff;
if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2) ){ for(iOff=0; iOff<nMax; iOff+=pgsz2){
pgsz = pgsz2; if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){
break; pgsz = pgsz2;
break;
}
} }
} }
} }
} }
} if( pgsz>p->detected_pgsz ){
p->detected_pgsz = pgsz;
p->nReserve = nReserve;
}
if( nReserve==0 ) break;
nReserve = 0;
}while( 1 );
p->detected_pgsz = pgsz; p->detected_pgsz = pgsz;
sqlite3_free(aPg); sqlite3_free(aPg);
@ -2188,6 +2191,7 @@ static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
u8 *a = (u8*)aBuf; u8 *a = (u8*)aBuf;
u32 pgsz = recoverGetU16(&a[16]); u32 pgsz = recoverGetU16(&a[16]);
u32 nReserve = a[20];
u32 enc = recoverGetU32(&a[56]); u32 enc = recoverGetU32(&a[56]);
u32 dbsz = 0; u32 dbsz = 0;
i64 dbFileSize = 0; i64 dbFileSize = 0;
@ -2199,10 +2203,11 @@ static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
if( rc==SQLITE_OK && p->detected_pgsz==0 ){ if( rc==SQLITE_OK && p->detected_pgsz==0 ){
u32 pgsz2 = 0; u32 pgsz2 = 0;
rc = recoverVfsDetectPagesize(p, pFd, dbFileSize); rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize);
} }
if( p->detected_pgsz ){ if( p->detected_pgsz ){
pgsz = p->detected_pgsz; pgsz = p->detected_pgsz;
nReserve = p->nReserve;
} }
if( pgsz ){ if( pgsz ){
@ -2224,9 +2229,10 @@ static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
recoverPutU32(&aHdr[28], dbsz); recoverPutU32(&aHdr[28], dbsz);
recoverPutU32(&aHdr[56], enc); recoverPutU32(&aHdr[56], enc);
recoverPutU16(&aHdr[105], pgsz); recoverPutU16(&aHdr[105], pgsz-nReserve);
if( pgsz==65536 ) pgsz = 1; if( pgsz==65536 ) pgsz = 1;
recoverPutU16(&aHdr[16], pgsz); recoverPutU16(&aHdr[16], pgsz);
aHdr[20] = nReserve;
for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){ for(ii=0; ii<sizeof(aPreserve)/sizeof(aPreserve[0]); ii++){
memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4); memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4);
} }

View File

@ -1,5 +1,5 @@
C Add\stests\sfor\sdetecting\spage\ssize\sof\sdatabases. C Handle\srecovering\sdatabases\swith\sreserved\sbytes\sat\sthe\send\sof\seach\spage.
D 2022-10-13T20:06:17.168 D 2022-10-14T15:46:19.783
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -293,7 +293,7 @@ F ext/misc/blobio.c a867c4c4617f6ec223a307ebfe0eabb45e0992f74dd47722b96f3e631c0e
F ext/misc/btreeinfo.c d28ce349b40054eaa9473e835837bad7a71deec33ba13e39f963d50933bfa0f9 F ext/misc/btreeinfo.c d28ce349b40054eaa9473e835837bad7a71deec33ba13e39f963d50933bfa0f9
F ext/misc/carray.c b752f46411e4e47e34dce6f0c88bc8e51bb821ba9e49bfcd882506451c928f69 F ext/misc/carray.c b752f46411e4e47e34dce6f0c88bc8e51bb821ba9e49bfcd882506451c928f69
F ext/misc/carray.h d2b1b12486d531367c37832d3d0dad34eea4bdd83ed839d445521ef01f0bc4e3 F ext/misc/carray.h d2b1b12486d531367c37832d3d0dad34eea4bdd83ed839d445521ef01f0bc4e3
F ext/misc/cksumvfs.c b42ef52eaaa510d54ec320c87bea149e934a3b06cd232be2093562bf669bd572 F ext/misc/cksumvfs.c 9224e33cc0cb6aa61ff1d7d7b8fd6fe56beca9f9c47954fa4ae0a69bef608f69
F ext/misc/closure.c dbfd8543b2a017ae6b1a5843986b22ddf99ff126ec9634a2f4047cd14c85c243 F ext/misc/closure.c dbfd8543b2a017ae6b1a5843986b22ddf99ff126ec9634a2f4047cd14c85c243
F ext/misc/completion.c 6dafd7f4348eecc7be9e920d4b419d1fb2af75d938cd9c59a20cfe8beb2f22b9 F ext/misc/completion.c 6dafd7f4348eecc7be9e920d4b419d1fb2af75d938cd9c59a20cfe8beb2f22b9
F ext/misc/compress.c 3354c77a7c8e86e07d849916000cdac451ed96500bfb5bd83b20eb61eee012c9 F ext/misc/compress.c 3354c77a7c8e86e07d849916000cdac451ed96500bfb5bd83b20eb61eee012c9
@ -386,7 +386,7 @@ F ext/rbu/rbuvacuum4.test a78898e438a44803eb2bc897ba3323373c9f277418e2d6d76e90f2
F ext/rbu/sqlite3rbu.c 8737cabdfbee84bb25a7851ecef8b1312be332761238da9be6ddb10c62ad4291 F ext/rbu/sqlite3rbu.c 8737cabdfbee84bb25a7851ecef8b1312be332761238da9be6ddb10c62ad4291
F ext/rbu/sqlite3rbu.h 1dc88ab7bd32d0f15890ea08d23476c4198d3da3056985403991f8c9cd389812 F ext/rbu/sqlite3rbu.h 1dc88ab7bd32d0f15890ea08d23476c4198d3da3056985403991f8c9cd389812
F ext/rbu/test_rbu.c 03f6f177096a5f822d68d8e4069ad8907fe572c62ff2d19b141f59742821828a F ext/rbu/test_rbu.c 03f6f177096a5f822d68d8e4069ad8907fe572c62ff2d19b141f59742821828a
F ext/recover/recover1.test 6b59ef31e3c0beef8b66e112d50af160e6f7ad8df95bee10192a9e44169d03ec F ext/recover/recover1.test 93acc42f95259f8b34050ad75873685a305da76d6cb1727d003f45157a4a6402
F ext/recover/recover_common.tcl 6679af7dffc858e345053a91c9b0a897595b4a13007aceffafca75304ccb137c F ext/recover/recover_common.tcl 6679af7dffc858e345053a91c9b0a897595b4a13007aceffafca75304ccb137c
F ext/recover/recoverclobber.test 294dcc894124ab4ca3a7b35766630742a3d25810fceac22220beb64f70a33a60 F ext/recover/recoverclobber.test 294dcc894124ab4ca3a7b35766630742a3d25810fceac22220beb64f70a33a60
F ext/recover/recovercorrupt.test 69af3d68aedc2cf1c3c41dbd6afb45b3cebadb57796d513550b5fd1e2a8b3fba F ext/recover/recovercorrupt.test 69af3d68aedc2cf1c3c41dbd6afb45b3cebadb57796d513550b5fd1e2a8b3fba
@ -398,7 +398,7 @@ F ext/recover/recoverpgsz.test 93e970eab05e4e89f8fd6b1bd23f9ec137ea09857e66ba0d4
F ext/recover/recoverrowid.test 1694a1a5526d825f71279f3d02ab02a1ee4c5265de18858bf54cb8ec54487ac8 F ext/recover/recoverrowid.test 1694a1a5526d825f71279f3d02ab02a1ee4c5265de18858bf54cb8ec54487ac8
F ext/recover/recoverslowidx.test f356bb9fba7ffd6fc50e045e419464f0129ac6e24decf6e919584f79c3493727 F ext/recover/recoverslowidx.test f356bb9fba7ffd6fc50e045e419464f0129ac6e24decf6e919584f79c3493727
F ext/recover/recoversql.test f9872ff2114e13ffd8ee31e1de06919f62b9b48bc080191b5bd076d10becb60f F ext/recover/recoversql.test f9872ff2114e13ffd8ee31e1de06919f62b9b48bc080191b5bd076d10becb60f
F ext/recover/sqlite3recover.c 2e9244bcc552f84bb60ea67060b793af2eb050bcbb9c8caed13510308e2039fb F ext/recover/sqlite3recover.c 3ab2017a947fa8c9286aa6257d0b19443ec04e557af9d989227b89f36ff50dd6
F ext/recover/sqlite3recover.h f698ccc94bd4da38761035415ad08c4549a408491ff9fd5f52d34d2214f64e36 F ext/recover/sqlite3recover.h f698ccc94bd4da38761035415ad08c4549a408491ff9fd5f52d34d2214f64e36
F ext/recover/test_recover.c 61ec931e47abca6b2210f46239cafd9f3060741605e3d3c45a7c7a53f63dd957 F ext/recover/test_recover.c 61ec931e47abca6b2210f46239cafd9f3060741605e3d3c45a7c7a53f63dd957
F ext/repair/README.md 92f5e8aae749a4dae14f02eea8e1bb42d4db2b6ce5e83dbcdd6b1446997e0c15 F ext/repair/README.md 92f5e8aae749a4dae14f02eea8e1bb42d4db2b6ce5e83dbcdd6b1446997e0c15
@ -2015,8 +2015,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 078520f2825bcb82d01b30209409e9bf8b6c99084c192345df41c9f531d08f51 P 31f9e2369fcf59032b7c4c9f5bfc85e7ef7c174003b0b9e2757dad5a4c79b370
R c9070f986a9313d4a9d177b6562a0478 R 5cdbc6428c0fa2e93f2a85abaf27f095
U dan U dan
Z 7bf274fb579b6c3146f808450c95f374 Z b6e8d5631de2a7b5df9849ef8598b9a3
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
31f9e2369fcf59032b7c4c9f5bfc85e7ef7c174003b0b9e2757dad5a4c79b370 52d0235ef3f21683daadf2343514f7b95a67c3589266f75c1e00e04f11dc6214