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.41 by root, Wed Dec 24 04:09:27 2008 UTC vs.
Revision 1.43 by root, Fri Jan 9 22:38:17 2009 UTC

18use utf8; 18use utf8;
19 19
20use File::Path (); 20use File::Path ();
21use Carp (); 21use Carp ();
22use Storable (); 22use Storable ();
23use AnyEvent::Util ();
23use Config; 24use Config;
24use BDB; 25use BDB;
26use Fcntl ();
25 27
26use DC; 28use DC;
27 29
28our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 30our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
29our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; 31our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
50our $DB_ENV; 52our $DB_ENV;
51our $DB_STATE; 53our $DB_STATE;
52our %DB_TABLE; 54our %DB_TABLE;
53our $TILE_SEQ; 55our $TILE_SEQ;
54 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
55sub try_open_db { 106sub try_open_db {
56 File::Path::mkpath [$DB_HOME]; 107 File::Path::mkpath [$DB_HOME];
57 108
58 my $env = db_env_create; 109 my $env = db_env_create;
59 110
73 db_env_open $env, $DB_HOME, 124 db_env_open $env, $DB_HOME,
74 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,
75 0666; 126 0666;
76 127
77 $! 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";
78 134
79 $DB_ENV = $env; 135 $DB_ENV = $env;
80 136
81 1 137 1
82} 138}
355 411
356 print { $LOG_FH{$path} } "$ts $line\n" 412 print { $LOG_FH{$path} } "$ts $line\n"
357} 413}
358 414
359sub run { 415sub run {
360 ($FH, my $fh) = DC::socketpipe; 416 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
417 or die "unable to create database socketpair: $!";
361 418
362 my $oldfh = select $FH; $| = 1; select $oldfh; 419 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 420 my $oldfh = select $fh; $| = 1; select $oldfh;
364 421
365 my $pid = fork; 422 my $pid = fork;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines