… | |
… | |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use File::Path (); |
20 | use File::Path (); |
21 | use Carp (); |
21 | use Carp (); |
22 | use Storable (); |
22 | use Storable (); |
|
|
23 | use AnyEvent::Util (); |
23 | use Config; |
24 | use Config; |
24 | use BDB; |
25 | use BDB; |
|
|
26 | use Fcntl (); |
25 | |
27 | |
26 | use DC; |
28 | use DC; |
27 | |
29 | |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
31 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
32 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
33 | |
|
|
34 | sub FIRST_TILE_ID () { 64 } |
31 | |
35 | |
32 | unless (-d $DB_HOME) { |
36 | unless (-d $DB_HOME) { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
37 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
38 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
39 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
… | |
… | |
48 | our $DB_ENV; |
52 | our $DB_ENV; |
49 | our $DB_STATE; |
53 | our $DB_STATE; |
50 | our %DB_TABLE; |
54 | our %DB_TABLE; |
51 | our $TILE_SEQ; |
55 | our $TILE_SEQ; |
52 | |
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 | |
53 | sub try_open_db { |
106 | sub try_open_db { |
54 | File::Path::mkpath [$DB_HOME]; |
107 | File::Path::mkpath [$DB_HOME]; |
55 | |
108 | |
56 | my $env = db_env_create; |
109 | my $env = db_env_create; |
57 | |
110 | |
58 | $env->set_errfile (\*STDERR); |
111 | $env->set_errfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
112 | $env->set_msgfile (\*STDERR); |
60 | $env->set_verbose (-1, 1); |
113 | $env->set_verbose (-1, 1); |
61 | |
114 | |
62 | $env->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
115 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
116 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
117 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
118 | |
|
|
119 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
120 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
121 | |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
122 | $env->set_cachesize (0, 2048 * 1024, 0); |
64 | |
123 | |
65 | db_env_open $env, $DB_HOME, |
124 | db_env_open $env, $DB_HOME, |
66 | 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, |
67 | 0666; |
126 | 0666; |
68 | |
127 | |
69 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
128 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
70 | |
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"; |
|
|
134 | |
71 | $DB_ENV = $env; |
135 | $DB_ENV = $env; |
72 | |
136 | |
73 | 1 |
137 | 1 |
74 | } |
138 | } |
75 | |
139 | |
76 | sub table($) { |
140 | sub table($) { |
77 | $DB_TABLE{$_[0]} ||= do { |
141 | $DB_TABLE{$_[0]} ||= do { |
78 | my ($table) = @_; |
142 | my ($table) = @_; |
79 | |
143 | |
80 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
144 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
145 | |
|
|
146 | $DB_ENV#d# |
|
|
147 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
81 | |
148 | |
82 | my $db = db_create $DB_ENV; |
149 | my $db = db_create $DB_ENV; |
83 | $db->set_flags (BDB::CHKSUM); |
150 | $db->set_flags (BDB::CHKSUM); |
84 | |
151 | |
85 | db_open $db, undef, $table, undef, BDB::BTREE, |
152 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
150 | db_get $table, undef, $name => $id, 0; |
217 | db_get $table, undef, $name => $id, 0; |
151 | $! or return $cb->($id); |
218 | $! or return $cb->($id); |
152 | |
219 | |
153 | unless ($TILE_SEQ) { |
220 | unless ($TILE_SEQ) { |
154 | $TILE_SEQ = $table->sequence; |
221 | $TILE_SEQ = $table->sequence; |
155 | $TILE_SEQ->initial_value (64); |
222 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
156 | $TILE_SEQ->set_cachesize (0); |
223 | $TILE_SEQ->set_cachesize (0); |
157 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
224 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
158 | } |
225 | } |
159 | |
226 | |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
227 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
… | |
… | |
344 | |
411 | |
345 | print { $LOG_FH{$path} } "$ts $line\n" |
412 | print { $LOG_FH{$path} } "$ts $line\n" |
346 | } |
413 | } |
347 | |
414 | |
348 | sub run { |
415 | sub run { |
349 | ($FH, my $fh) = DC::socketpipe; |
416 | ($FH, my $fh) = AnyEvent::Util::portable_socketpair |
|
|
417 | or die "unable to create database socketpair: $!"; |
350 | |
418 | |
351 | my $oldfh = select $FH; $| = 1; select $oldfh; |
419 | my $oldfh = select $FH; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
420 | my $oldfh = select $fh; $| = 1; select $oldfh; |
353 | |
421 | |
354 | my $pid = fork; |
422 | my $pid = fork; |