ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.42 by root, Sun Jan 4 10:22:19 2009 UTC vs.
Revision 1.43 by root, Fri Jan 9 22:38:17 2009 UTC

21use Carp (); 21use Carp ();
22use Storable (); 22use Storable ();
23use AnyEvent::Util (); 23use AnyEvent::Util ();
24use Config; 24use Config;
25use BDB; 25use BDB;
26use Fcntl ();
26 27
27use DC; 28use DC;
28 29
29our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 30our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
30our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 31our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
51our $DB_ENV; 52our $DB_ENV;
52our $DB_STATE; 53our $DB_STATE;
53our %DB_TABLE; 54our %DB_TABLE;
54our $TILE_SEQ; 55our $TILE_SEQ;
55 56
57sub all_databases {
58 opendir my $fh, $DB_HOME
59 or return;
60
61 grep !/^(?:\.|log\.|_)/, readdir $fh
62}
63
64sub try_verify_env($) {
65 my ($env) = @_;
66
67 # we lock the __db.register env file that has been created by now
68 # and check for the number of registered processes - if there is
69 # only one, we verify all databases, otherwise we skip this
70 open my $fh, "+<$DB_HOME/__db.register"
71 or die "__db.register: $!";
72
73 open my $lock, "+>$DB_HOME/__lock"
74 or die "__lock: $!";
75
76 flock $lock, &Fcntl::LOCK_EX
77 or die "flock: $!";
78
79 # __db.register contains one record per process, with X signifying
80 # empty records (of course, this is completely private to bdb...)
81 my $count = grep /^[^X]/, <$fh>;
82
83 if ($count == 1) {
84 # if any databases are corrupted, we simply delete all of them
85
86 for (all_databases) {
87 my $dbh = db_create $env
88 or last;
89
90 # a failed verify will panic the environment, which is fine with us
91 db_verify $dbh, "$DB_HOME/$_";
92
93 return if $!; # nuke database and recreate if verification failure
94 }
95
96 }
97
98 # close probably cleans those up, but we also want to run on windows,
99 # so better be safe.
100 flock $lock, &Fcntl::LOCK_UN
101 or die "funlock: $!";
102
103 1
104}
105
56sub try_open_db { 106sub try_open_db {
57 File::Path::mkpath [$DB_HOME]; 107 File::Path::mkpath [$DB_HOME];
58 108
59 my $env = db_env_create; 109 my $env = db_env_create;
60 110
74 db_env_open $env, $DB_HOME, 124 db_env_open $env, $DB_HOME,
75 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 125 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
76 0666; 126 0666;
77 127
78 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 128 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
129
130 # now we go through the registered processes, if there is only one, we verify all files
131 # to make sure windows didn'T corrupt them (as windows does....)
132 try_verify_env $env
133 or die "database environment failed verification";
79 134
80 $DB_ENV = $env; 135 $DB_ENV = $env;
81 136
82 1 137 1
83} 138}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines