1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Add a version number to the wal-index header. If SQLite encounters a version number in either the wal or wal-index files that it does not understand, the operation is abandoned and SQLITE_CANTOPEN returned.

FossilOrigin-Name: 8d0f8a7f70d6fb42369411a934b30f8c8ca8322f
This commit is contained in:
dan
2010-06-23 15:55:43 +00:00
parent b316cb22de
commit 10f5a50e57
13 changed files with 324 additions and 152 deletions

View File

@ -13,35 +13,10 @@
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/wal_common.tcl
ifcapable !wal {finish_test ; return }
# This proc calculates checksums in the same way as those used by SQLite
# in WAL files. If the $endian argument is "big", then checksums are
# calculated by interpreting data as an array of big-endian integers. If
# it is "little", data is interpreted as an array of little-endian integers.
#
proc log_cksum {endian ckv1 ckv2 blob} {
upvar $ckv1 c1
upvar $ckv2 c2
if {$endian!="big" && $endian!="little"} {
return -error "Bad value \"$endian\" - must be \"big\" or \"little\""
}
set scanpattern I*
if {$endian == "little"} { set scanpattern i* }
binary scan $blob $scanpattern values
foreach {v1 v2} $values {
set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
}
}
proc log_file_size {nFrame pgsz} {
expr {24 + ($pgsz+24)*$nFrame}
}
# Read and return the contents of file $filename. Treat the content as
# binary data.
#
@ -72,7 +47,6 @@ proc log_checksum_verify {filename iFrame endian} {
expr {$c1==$expect1 && $c2==$expect2}
}
#
# File $filename must be a WAL file on disk. Compute the checksum for frame
# $iFrame in the file by interpreting data as $endian-endian integers
# ($endian must be either "big" or "little"). Then write the computed
@ -92,21 +66,38 @@ proc log_checksum_write {filename iFrame endian} {
close $fd
}
# Calculate and return the checksum for a particular frame in a WAL.
#
# Arguments are:
#
# $data Blob containing the entire contents of a WAL.
#
# $iFrame Frame number within the $data WAL. Frames are numbered
# starting at 1.
#
# $endian One of "big" or "little".
#
# Returns a list of three elements, as follows:
#
# * The byte offset of the checksum belonging to frame $iFrame in the WAL.
# * The first integer in the calculated version of the checksum.
# * The second integer in the calculated version of the checksum.
#
proc log_checksum_calc {data iFrame endian} {
binary scan [string range $data 8 11] I pgsz
if {$iFrame > 1} {
set n [log_file_size [expr $iFrame-2] $pgsz]
set n [wal_file_size [expr $iFrame-2] $pgsz]
binary scan [string range $data [expr $n+16] [expr $n+23]] II c1 c2
} else {
set c1 0
set c2 0
log_cksum $endian c1 c2 [string range $data 0 23]
wal_cksum $endian c1 c2 [string range $data 0 23]
}
set n [log_file_size [expr $iFrame-1] $pgsz]
log_cksum $endian c1 c2 [string range $data $n [expr $n+7]]
log_cksum $endian c1 c2 [string range $data [expr $n+24] [expr $n+24+$pgsz-1]]
set n [wal_file_size [expr $iFrame-1] $pgsz]
wal_cksum $endian c1 c2 [string range $data $n [expr $n+7]]
wal_cksum $endian c1 c2 [string range $data [expr $n+24] [expr $n+24+$pgsz-1]]
list [expr $n+16] $c1 $c2
}
@ -116,6 +107,9 @@ proc log_checksum_calc {data iFrame endian} {
# WAL header to indicate that checksums are $endian-endian ($endian must be
# either "big" or "little").
#
# Also update the wal header checksum (since the wal header contents may
# have changed).
#
proc log_checksum_writemagic {filename endian} {
set val [expr {0x377f0682 | ($endian == "big" ? 1 : 0)}]
set bin [binary format I $val]
@ -123,6 +117,15 @@ proc log_checksum_writemagic {filename endian} {
fconfigure $fd -encoding binary
fconfigure $fd -translation binary
puts -nonewline $fd $bin
seek $fd 0
set blob [read $fd 24]
set c1 0
set c2 0
wal_cksum $endian c1 c2 $blob
seek $fd 24
puts -nonewline $fd [binary format II $c1 $c2]
close $fd
}
@ -174,7 +177,7 @@ foreach endian {big little} {
db close
list [file size test2.db] [file size test2.db-wal]
} [list [expr 1024*3] [log_file_size 6 1024]]
} [list [expr 1024*3] [wal_file_size 6 1024]]
# Verify that the checksums are valid for all frames and that they
# are calculated by interpreting data in native byte-order.
@ -211,7 +214,7 @@ foreach endian {big little} {
INSERT INTO t1 VALUES(34, 'thirtyfour');
}
list [file size test.db] [file size test.db-wal]
} [list [expr 1024*3] [log_file_size 8 1024]]
} [list [expr 1024*3] [wal_file_size 8 1024]]
for {set f 1} {$f <= 8} {incr f} {
do_test walcksum-1.$endian.5.$f {
log_checksum_verify test.db-wal $f $endian
@ -235,7 +238,7 @@ foreach endian {big little} {
INSERT INTO t1 VALUES(55, 'fiftyfive');
} db2
list [file size test.db] [file size test.db-wal]
} [list [expr 1024*3] [log_file_size 10 1024]]
} [list [expr 1024*3] [wal_file_size 10 1024]]
for {set f 1} {$f <= 10} {incr f} {
do_test walcksum-1.$endian.7.$f {
log_checksum_verify test.db-wal $f $endian
@ -351,7 +354,7 @@ do_test walcksum-3.1 {
}
file size test.db-wal
} [log_file_size 1 1024]
} [wal_file_size 1 1024]
do_test walcksum-3.2 {
file copy -force test.db-wal test2.db-wal
file copy -force test.db test2.db
@ -365,7 +368,7 @@ file copy -force test.db test2.db
foreach incr {1 2 3 20 40 60 80 100 120 140 160 180 200 220 240 253 254 255} {
do_test walcksum-3.3.$incr {
set FAIL 0
for {set iOff 0} {$iOff < [log_file_size 1 1024]} {incr iOff} {
for {set iOff 0} {$iOff < [wal_file_size 1 1024]} {incr iOff} {
file copy -force test.db-wal test2.db-wal
set fd [open test2.db-wal r+]