… | |
… | |
21 | use Carp (); |
21 | use Carp (); |
22 | use Storable (); |
22 | use Storable (); |
23 | use AnyEvent::Util (); |
23 | use AnyEvent::Util (); |
24 | use Config; |
24 | use Config; |
25 | use BDB; |
25 | use BDB; |
|
|
26 | use Fcntl (); |
26 | |
27 | |
27 | use DC; |
28 | use DC; |
28 | |
29 | |
29 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
31 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
… | |
… | |
51 | our $DB_ENV; |
52 | our $DB_ENV; |
52 | our $DB_STATE; |
53 | our $DB_STATE; |
53 | our %DB_TABLE; |
54 | our %DB_TABLE; |
54 | our $TILE_SEQ; |
55 | our $TILE_SEQ; |
55 | |
56 | |
|
|
57 | sub all_databases { |
|
|
58 | opendir my $fh, $DB_HOME |
|
|
59 | or return; |
|
|
60 | |
|
|
61 | grep !/^(?:\.|log\.|_)/, readdir $fh |
|
|
62 | } |
|
|
63 | |
|
|
64 | sub 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 | |
56 | sub try_open_db { |
106 | sub 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 | } |