Debian Codegraph @

test/wal_common.tcl

raw
1 # 2010 June 03
2 #
3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
5 #
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
9 #
10 #***********************************************************************
11 #
12 # This file contains common code used by many different malloc tests
13 # within the test suite.
14 #
15
16 proc wal_file_size {nFrame pgsz} {
17 expr {32 + ($pgsz+24)*$nFrame}
18 }
19
20 proc wal_frame_count {zFile pgsz} {
21 if {[file exists $zFile]==0} { return 0 }
22 set f [file size $zFile]
23 if {$f < 32} { return 0 }
24 expr {($f - 32) / ($pgsz+24)}
25 }
26
27 proc wal_cksum_intlist {ckv1 ckv2 intlist} {
28 upvar $ckv1 c1
29 upvar $ckv2 c2
30 foreach {v1 v2} $intlist {
31 set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
32 set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
33 }
34 }
35
36
37 # This proc calculates checksums in the same way as those used by SQLite
38 # in WAL files. If the $endian argument is "big", then checksums are
39 # calculated by interpreting data as an array of big-endian integers. If
40 # it is "little", data is interpreted as an array of little-endian integers.
41 #
42 proc wal_cksum {endian ckv1 ckv2 blob} {
43 upvar $ckv1 c1
44 upvar $ckv2 c2
45
46 if {$endian!="big" && $endian!="little"} {
47 return -error "Bad value \"$endian\" - must be \"big\" or \"little\""
48 }
49 set scanpattern I*
50 if {$endian == "little"} { set scanpattern i* }
51
52 binary scan $blob $scanpattern values
53 wal_cksum_intlist c1 c2 $values
54 }
55
56 proc wal_set_walhdr {filename {intlist {}}} {
57 if {[llength $intlist]==6} {
58 set blob [binary format I6 $intlist]
59 set endian little
60 if {[lindex $intlist 0] & 0x00000001} { set endian big }
61 set c1 0
62 set c2 0
63 wal_cksum $endian c1 c2 $blob
64 append blob [binary format II $c1 $c2]
65
66 set fd [open $filename r+]
67 fconfigure $fd -translation binary
68 seek $fd 0
69 puts -nonewline $fd $blob
70 close $fd
71 }
72
73 set fd [open $filename]
74 fconfigure $fd -translation binary
75 set blob [read $fd 24]
76 close $fd
77
78 binary scan $blob I6 ints
79 set ints
80 }
81
82 proc wal_fix_walindex_cksum {hdrvar} {
83 upvar $hdrvar hdr
84 set c1 0
85 set c2 0
86 wal_cksum_intlist c1 c2 [lrange $hdr 0 9]
87 lset hdr 10 $c1
88 lset hdr 11 $c2
89 }
90
91 # This command assumes that $file is the name of a database file opened
92 # in wal mode using a [testvfs] VFS. It returns a list of the 12 32-bit
93 # integers that make up the wal-index-header for the named file.
94 #
95 proc set_tvfs_hdr {file args} {
96
97 # Set $nHdr to the number of bytes in the wal-index header:
98 set nHdr 48
99 set nInt [expr {$nHdr/4}]
100
101 if {[llength $args]>2} {
102 error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"}
103 }
104
105 set blob [tvfs shm $file]
106 if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i}
107
108 if {[llength $args]} {
109 set ia [lindex $args 0]
110 set ib $ia
111 if {[llength $args]==2} {
112 set ib [lindex $args 1]
113 }
114 binary scan $blob a[expr $nHdr*2]a* dummy tail
115 set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail]
116 tvfs shm $file $blob
117 }
118
119 binary scan $blob ${fmt}${nInt} ints
120 return $ints
121 }
122
123 proc incr_tvfs_hdr {file idx incrval} {
124 set ints [set_tvfs_hdr $file]
125 set v [lindex $ints $idx]
126 incr v $incrval
127 lset ints $idx $v
128 set_tvfs_hdr $file $ints
129 }