Small tool that attempts to repair a journal header.

FossilOrigin-Name: 05b18b5f6a38f513ff49656faada725eb44715b7
This commit is contained in:
shaneh 2010-01-07 21:49:48 +00:00
parent 1d9da70ad7
commit ea6800c5c4
3 changed files with 120 additions and 17 deletions

View File

@ -1,8 +1,5 @@
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
C Fix\sthe\sexpression\scomparison\slogic\sto\stake\sthe\sCOLLATE\soperator\sinto\saccount.\nTicket\s[360c6073e197]
D 2010-01-07T15:17:02
C Small\stool\sthat\sattempts\sto\srepair\sa\sjournal\sheader.
D 2010-01-07T21:49:48
F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
F Makefile.in c5827ead754ab32b9585487177c93bb00b9497b3
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@ -773,6 +770,7 @@ F tool/mksqlite3h.tcl eb100dce83f24b501b325b340f8b5eb8e5106b3b
F tool/mksqlite3internalh.tcl 7b43894e21bcb1bb39e11547ce7e38a063357e87
F tool/omittest.tcl 27d6f6e3b1e95aeb26a1c140e6eb57771c6d794a
F tool/opcodeDoc.awk b3a2a3d5d3075b8bd90b7afe24283efdd586659c
F tool/restore_jrnl.tcl f8ed56dbfa7d30fcb51ac0ea79a430928a42d1f4
F tool/shell1.test ef08a3e738b9fee4fc228920956950bc35db0575
F tool/shell2.test 91824fff77514b6b00227744c475c09c34134a4e
F tool/shell3.test ff663e83100670a295d473515c12beb8103a78b6
@ -787,14 +785,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
P d6ee5ff6c815e3aadd92d331560b529394eae661
R 8a8b46968fc71c4a14b23f3c5b724baa
U drh
Z 8aa04fbf144e000aa38deafde10e964c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFLRfryoxKgR168RlERAoWYAJkBzgNbrhQEOwANUltOu1MbTLB+fQCeNI4J
z1KroLxlemLylQvQzuGxJ/c=
=1fiA
-----END PGP SIGNATURE-----
P 44bb1bfe5dedd8054ddd933941ee4112ed8d3b68
R 80d6fe28fe35c61c732f56f7d98c7c79
U shaneh
Z d814ae1179cd7f19acd5f1901fe58bc2

View File

@ -1 +1 @@
44bb1bfe5dedd8054ddd933941ee4112ed8d3b68
05b18b5f6a38f513ff49656faada725eb44715b7

112
tool/restore_jrnl.tcl Normal file
View File

@ -0,0 +1,112 @@
# 2010 January 7
#
# 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.
#
#***********************************************************************
# This file implements utility functions for SQLite library.
#
# This file attempts to restore the header of a journal.
# This may be useful for rolling-back the last committed
# transaction from a recovered journal.
#
# $Id: restore_jrnl.tcl,v 1.14 2009/07/11 06:55:34 danielk1977 Exp $
package require sqlite3
if { $argc != 2 } {
puts "USAGE: restore_jrnl.tcl db_name jrnl_name"
puts "Example: restore_jrnl.tcl foo.sqlite foo.sqlite-journal"
return
} else {
set db_name [lindex $argv 0]
set jrnl_name [lindex $argv 1]
}
# is there a way to determine this?
set sectsz 512
# Copy file $from into $to
#
proc copy_file {from to} {
file copy -force $from $to
}
# Execute some SQL
#
proc catchsql {sql} {
set rc [catch {uplevel [list db eval $sql]} msg]
list $rc $msg
}
# Perform a test
#
proc do_test {name cmd expected} {
puts -nonewline "$name ..."
set res [uplevel $cmd]
if {$res eq $expected} {
puts Ok
} else {
puts Error
puts " Got: $res"
puts " Expected: $expected"
}
}
# Setup for the tests. Make a backup copy of the files.
#
if [file exist $db_name.org] {
puts "ERROR: during back-up: $db_name.org exists already."
return;
}
if [file exist $jrnl_name.org] {
puts "ERROR: during back-up: $jrnl_name.org exists already."
return
}
copy_file $db_name $db_name.org
copy_file $jrnl_name $jrnl_name.org
set db_fsize [file size $db_name]
sqlite3 db $db_name
set db_pgsz [db eval {PRAGMA page_size}]
db close
set db_npage [expr {$db_fsize / $db_pgsz}]
# calculate checksum nonce
set pgno 1
set pg_offset [expr $sectsz+((4+$db_pgsz+4)*$pgno)]
set nonce [hexio_get_int [hexio_read $jrnl_name [expr $pg_offset+4+$db_pgsz] 4]]
for {set i [expr $db_pgsz-200]} {$i>0} {set i [expr $i-200]} {
set byte [hexio_get_int [hexio_read $jrnl_name [expr $pg_offset+4+$i] 1]]
set nonce [expr $nonce-$byte]
}
# write the 8 byte magic string
hexio_write $jrnl_name 0 d9d505f920a163d7
# write -1 for number of records
hexio_write $jrnl_name 8 ffffffff
# write 00 for checksum nonce
hexio_write $jrnl_name 12 [format %08x $nonce]
# write page count
hexio_write $jrnl_name 16 [format %08x $db_npage]
# write sector size
hexio_write $jrnl_name 20 [format %08x $sectsz]
# write page size
hexio_write $jrnl_name 24 [format %08x $db_pgsz]
# check the integrity of the database.
sqlite3 db $db_name
do_test restore_jrnl-1.0 {
catchsql {PRAGMA integrity_check}
} {0 {ok}}
db close