mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Add tests for detecting page size of databases.
FossilOrigin-Name: 31f9e2369fcf59032b7c4c9f5bfc85e7ef7c174003b0b9e2757dad5a4c79b370
This commit is contained in:
103
ext/recover/recoverpgsz.test
Normal file
103
ext/recover/recoverpgsz.test
Normal file
@ -0,0 +1,103 @@
|
||||
# 2022 October 14
|
||||
#
|
||||
# The author disclaims copyright to this source code. In place of
|
||||
# a legal notice, here is a blessing:
|
||||
#
|
||||
# May you do good and not evil.
|
||||
# May you find forgiveness for yourself and forgive others.
|
||||
# May you share freely, never taking more than you give.
|
||||
#
|
||||
#***********************************************************************
|
||||
#
|
||||
|
||||
if {![info exists testdir]} {
|
||||
set testdir [file join [file dirname [info script]] .. .. test]
|
||||
}
|
||||
source [file join [file dirname [info script]] recover_common.tcl]
|
||||
source $testdir/tester.tcl
|
||||
|
||||
db close
|
||||
sqlite3_test_control_pending_byte 0x1000000
|
||||
|
||||
set testprefix recoverpgsz
|
||||
|
||||
foreach {pgsz bOverflow} {
|
||||
512 0 1024 0 2048 0 4096 0 8192 0 16384 0 32768 0 65536 0
|
||||
512 1 1024 1 2048 1 4096 1 8192 1 16384 1 32768 1 65536 1
|
||||
} {
|
||||
reset_db
|
||||
execsql "PRAGMA page_size = $pgsz"
|
||||
do_execsql_test 1.$pgsz.$bOverflow.1 {
|
||||
CREATE TABLE t1(a, b, c);
|
||||
CREATE INDEX i1 ON t1(b, a, c);
|
||||
INSERT INTO t1(a, b) VALUES(1, 2), (3, 4), (5, 6);
|
||||
DELETE FROM t1 WHERE a=3;
|
||||
}
|
||||
if {$bOverflow} {
|
||||
do_execsql_test 1.$pgsz.$bOverflow.1a {
|
||||
UPDATE t1 SET c = randomblob(100000);
|
||||
}
|
||||
}
|
||||
db close
|
||||
|
||||
|
||||
set fd [open test.db]
|
||||
fconfigure $fd -encoding binary -translation binary
|
||||
seek $fd $pgsz
|
||||
set pg1 [read $fd $pgsz]
|
||||
set pg2 [read $fd $pgsz]
|
||||
close $fd
|
||||
|
||||
set fd2 [open test.db2 w]
|
||||
fconfigure $fd2 -encoding binary -translation binary
|
||||
seek $fd2 $pgsz
|
||||
puts -nonewline $fd2 $pg1
|
||||
close $fd2
|
||||
|
||||
sqlite3 db2 test.db2
|
||||
do_test 1.$pgsz.$bOverflow.2 {
|
||||
set R [sqlite3_recover_init db2 main test.db3]
|
||||
$R run
|
||||
$R finish
|
||||
} {}
|
||||
|
||||
sqlite3 db3 test.db3
|
||||
do_test 1.$pgsz.$bOverflow.3 {
|
||||
db3 eval { SELECT * FROM sqlite_schema }
|
||||
db3 eval { PRAGMA page_size }
|
||||
} $pgsz
|
||||
|
||||
db2 close
|
||||
db3 close
|
||||
|
||||
forcedelete test.db3
|
||||
forcedelete test.db2
|
||||
|
||||
set fd2 [open test.db2 w]
|
||||
fconfigure $fd2 -encoding binary -translation binary
|
||||
seek $fd2 $pgsz
|
||||
puts -nonewline $fd2 $pg2
|
||||
close $fd2
|
||||
|
||||
sqlite3 db2 test.db2
|
||||
do_test 1.$pgsz.$bOverflow.4 {
|
||||
set R [sqlite3_recover_init db2 main test.db3]
|
||||
$R run
|
||||
$R finish
|
||||
} {}
|
||||
|
||||
sqlite3 db3 test.db3
|
||||
do_test 1.$pgsz.$bOverflow.5 {
|
||||
db3 eval { SELECT * FROM sqlite_schema }
|
||||
db3 eval { PRAGMA page_size }
|
||||
} $pgsz
|
||||
|
||||
db2 close
|
||||
db3 close
|
||||
}
|
||||
|
||||
|
||||
finish_test
|
||||
|
||||
|
||||
|
@ -193,6 +193,7 @@ struct sqlite3_recover {
|
||||
int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */
|
||||
|
||||
int pgsz;
|
||||
int detected_pgsz;
|
||||
u8 *pPage1Disk;
|
||||
u8 *pPage1Cache;
|
||||
|
||||
@ -1980,7 +1981,7 @@ static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){
|
||||
iNext = recoverGetU16(&a[iFree]);
|
||||
nByte = recoverGetU16(&a[iFree+2]);
|
||||
if( iFree+nByte>n ) return 0;
|
||||
if( iNext<iFree+nByte ) return 0;
|
||||
if( iNext && iNext<iFree+nByte ) return 0;
|
||||
memset(&aUsed[iFree], 0xFF, nByte);
|
||||
iFree = iNext;
|
||||
}
|
||||
@ -2093,7 +2094,11 @@ static void recoverPutU32(u8 *a, u32 v){
|
||||
a[3] = (v>>0) & 0x00FF;
|
||||
}
|
||||
|
||||
static int recoverVfsDetectPagesize(sqlite3_file *pFd, i64 nSz, u32 *pPgsz){
|
||||
static int recoverVfsDetectPagesize(
|
||||
sqlite3_recover *p,
|
||||
sqlite3_file *pFd,
|
||||
i64 nSz
|
||||
){
|
||||
int rc = SQLITE_OK;
|
||||
const int nMin = 512;
|
||||
const int nMax = 65536;
|
||||
@ -2129,7 +2134,7 @@ static int recoverVfsDetectPagesize(sqlite3_file *pFd, i64 nSz, u32 *pPgsz){
|
||||
}
|
||||
}
|
||||
|
||||
*pPgsz = pgsz;
|
||||
p->detected_pgsz = pgsz;
|
||||
sqlite3_free(aPg);
|
||||
return rc;
|
||||
}
|
||||
@ -2192,12 +2197,12 @@ static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){
|
||||
if( pgsz==0x01 ) pgsz = 65536;
|
||||
rc = pFd->pMethods->xFileSize(pFd, &dbFileSize);
|
||||
|
||||
if( rc==SQLITE_OK ){
|
||||
if( rc==SQLITE_OK && p->detected_pgsz==0 ){
|
||||
u32 pgsz2 = 0;
|
||||
rc = recoverVfsDetectPagesize(pFd, dbFileSize, &pgsz2);
|
||||
if( pgsz2!=0 ){
|
||||
pgsz = pgsz2;
|
||||
}
|
||||
rc = recoverVfsDetectPagesize(p, pFd, dbFileSize);
|
||||
}
|
||||
if( p->detected_pgsz ){
|
||||
pgsz = p->detected_pgsz;
|
||||
}
|
||||
|
||||
if( pgsz ){
|
||||
|
Reference in New Issue
Block a user