| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::DbDriver::Stack - stack of drivers |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::DbDriver::Stack; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Log qw{:all}; |
| 13 |
use Debconf::Iterator; |
| 14 |
use base 'Debconf::DbDriver::Copy'; |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This sets up a stack of drivers. Items in drivers higher in the stack |
| 19 |
shadow items lower in the stack, so requests for items will be passed on |
| 20 |
to the first driver in the stack that contains the item. |
| 21 |
|
| 22 |
Writing to the stack is more complex, because we meed to worry about |
| 23 |
readonly drivers. Instead of trying to write to a readonly driver and |
| 24 |
having it fail, this module will copy the item from the readonly driver |
| 25 |
to the writable driver closest to the top of the stack that accepts the |
| 26 |
given item, and then perform the write. |
| 27 |
|
| 28 |
=cut |
| 29 |
|
| 30 |
=head1 FIELDS |
| 31 |
|
| 32 |
=over 4 |
| 33 |
|
| 34 |
=item stack |
| 35 |
|
| 36 |
A reference to an array of drivers. The topmost driver should not be |
| 37 |
readonly, unless the whole stack is. |
| 38 |
|
| 39 |
In the config file, a comma-delimited list of driver names can be specified |
| 40 |
for this field. |
| 41 |
|
| 42 |
=back |
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
use fields qw(stack stack_change_errors); |
| 47 |
|
| 48 |
=head1 METHODS |
| 49 |
|
| 50 |
=head2 init |
| 51 |
|
| 52 |
On initialization, the topmost driver is checked for writability. |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
sub init { |
| 57 |
my $this=shift; |
| 58 |
|
| 59 |
# Handle value from config file. |
| 60 |
if (! ref $this->{stack}) { |
| 61 |
my @stack; |
| 62 |
foreach my $name (split(/\s*,\s/, $this->{stack})) { |
| 63 |
my $driver=$this->driver($name); |
| 64 |
unless (defined $driver) { |
| 65 |
$this->error("could not find a db named \"$name\" to use in the stack (it should be defined before the stack in the config file)"); |
| 66 |
next; |
| 67 |
} |
| 68 |
push @stack, $driver; |
| 69 |
} |
| 70 |
$this->{stack}=[@stack]; |
| 71 |
} |
| 72 |
|
| 73 |
$this->error("no stack set") if ! ref $this->{stack}; |
| 74 |
$this->error("stack is empty") if ! @{$this->{stack}}; |
| 75 |
#$this->error("topmost driver not writable") |
| 76 |
# if $this->{stack}->[0]->{readonly} && ! $this->{readonly}; |
| 77 |
} |
| 78 |
|
| 79 |
=head2 iterator |
| 80 |
|
| 81 |
Iterates over all the items in all the drivers in the whole stack. However, |
| 82 |
only return each item once, even if multiple drivers contain it. |
| 83 |
|
| 84 |
=cut |
| 85 |
|
| 86 |
sub iterator { |
| 87 |
my $this=shift; |
| 88 |
|
| 89 |
my %seen; |
| 90 |
my @iterators = map { $_->iterator } @{$this->{stack}}; |
| 91 |
my $i = pop @iterators; |
| 92 |
my $iterator=Debconf::Iterator->new(callback => sub { |
| 93 |
for (;;) { |
| 94 |
while (my $ret = $i->iterate) { |
| 95 |
next if $seen{$ret}; |
| 96 |
$seen{$ret}=1; |
| 97 |
return $ret; |
| 98 |
} |
| 99 |
$i = pop @iterators; |
| 100 |
return unless defined $i; |
| 101 |
} |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
=head2 shutdown |
| 106 |
|
| 107 |
Calls shutdown on the entire stack. If any shutdown call returns undef, returns |
| 108 |
undef too, but only after calling them all. |
| 109 |
|
| 110 |
=cut |
| 111 |
|
| 112 |
sub shutdown { |
| 113 |
my $this=shift; |
| 114 |
|
| 115 |
my $ret=1; |
| 116 |
foreach my $driver (@{$this->{stack}}) { |
| 117 |
$ret=undef if not defined $driver->shutdown(@_); |
| 118 |
} |
| 119 |
|
| 120 |
if ($this->{stack_change_errors}) { |
| 121 |
$this->error("unable to save changes to: ". |
| 122 |
join(" ", @{$this->{stack_change_errors}})); |
| 123 |
$ret=undef; |
| 124 |
} |
| 125 |
|
| 126 |
return $ret; |
| 127 |
} |
| 128 |
|
| 129 |
=head2 exists |
| 130 |
|
| 131 |
An item exists if any item in the stack contains it. So don't give up at |
| 132 |
the first failure, but keep digging down.. |
| 133 |
|
| 134 |
=cut |
| 135 |
|
| 136 |
sub exists { |
| 137 |
my $this=shift; |
| 138 |
|
| 139 |
foreach my $driver (@{$this->{stack}}) { |
| 140 |
return 1 if $driver->exists(@_); |
| 141 |
} |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
# From here on out, the methods are of two types, as explained in |
| 146 |
# the description above. Either we query the stack, or we make a |
| 147 |
# change to a writable item, copying an item from lower in the stack first |
| 148 |
# as is necessary. |
| 149 |
sub _query { |
| 150 |
my $this=shift; |
| 151 |
my $command=shift; |
| 152 |
shift; # this again |
| 153 |
|
| 154 |
debug "db $this->{name}" => "trying to $command(@_) .."; |
| 155 |
foreach my $driver (@{$this->{stack}}) { |
| 156 |
if (wantarray) { |
| 157 |
my @ret=$driver->$command(@_); |
| 158 |
debug "db $this->{name}" => "$command done by $driver->{name}" if @ret; |
| 159 |
return @ret if @ret; |
| 160 |
} |
| 161 |
else { |
| 162 |
my $ret=$driver->$command(@_); |
| 163 |
debug "db $this->{name}" => "$command done by $driver->{name}" if defined $ret; |
| 164 |
return $ret if defined $ret; |
| 165 |
} |
| 166 |
} |
| 167 |
return; # failure |
| 168 |
} |
| 169 |
|
| 170 |
sub _change { |
| 171 |
my $this=shift; |
| 172 |
my $command=shift; |
| 173 |
shift; # this again |
| 174 |
my $item=shift; |
| 175 |
|
| 176 |
debug "db $this->{name}" => "trying to $command($item @_) .."; |
| 177 |
|
| 178 |
# Check to see if we can just write to some driver in the stack. |
| 179 |
foreach my $driver (@{$this->{stack}}) { |
| 180 |
if ($driver->exists($item)) { |
| 181 |
last if $driver->{readonly}; # nope, hit a readonly one |
| 182 |
debug "db $this->{name}" => "passing to $driver->{name} .."; |
| 183 |
return $driver->$command($item, @_); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
# Set if we need to copy from something. |
| 188 |
my $src=0; |
| 189 |
|
| 190 |
# Find out what (readonly) driver on the stack first contains the item. |
| 191 |
foreach my $driver (@{$this->{stack}}) { |
| 192 |
if ($driver->exists($item)) { |
| 193 |
# Check if this modification would really have any |
| 194 |
# effect. |
| 195 |
my $ret=$this->_nochange($driver, $command, $item, @_); |
| 196 |
if (defined $ret) { |
| 197 |
debug "db $this->{name}" => "skipped $command($item) as it would have no effect"; |
| 198 |
return $ret; |
| 199 |
} |
| 200 |
|
| 201 |
# Nope, we have to copy after all. |
| 202 |
$src=$driver; |
| 203 |
last |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
# Work out what driver on the stack will be written to. |
| 208 |
# We'll take the first that accepts the item. |
| 209 |
my $writer; |
| 210 |
foreach my $driver (@{$this->{stack}}) { |
| 211 |
if ($driver == $src) { |
| 212 |
push @{$this->{stack_change_errors}}, $item; |
| 213 |
return; |
| 214 |
} |
| 215 |
if (! $driver->{readonly}) { |
| 216 |
# Adding an owner is a special case because the |
| 217 |
# item may not exist yet, and so accept() should be |
| 218 |
# told the type, if possible. Luckily the type is |
| 219 |
# the second parameter of the addowner command, or |
| 220 |
# $_[1]. |
| 221 |
if ($command eq 'addowner') { |
| 222 |
if ($driver->accept($item, $_[1])) { |
| 223 |
$writer=$driver; |
| 224 |
last; |
| 225 |
} |
| 226 |
} |
| 227 |
elsif ($driver->accept($item)) { |
| 228 |
$writer=$driver; |
| 229 |
last; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
unless ($writer) { |
| 235 |
debug "db $this->{name}" => "FAILED $command"; |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
# Do the copy if we have to. |
| 240 |
if ($src) { |
| 241 |
$this->copy($item, $src, $writer); |
| 242 |
} |
| 243 |
|
| 244 |
# Finally, do the write. |
| 245 |
debug "db $this->{name}" => "passing to $writer->{name} .."; |
| 246 |
return $writer->$command($item, @_); |
| 247 |
} |
| 248 |
|
| 249 |
# A problem occurs sometimes: A write might be attempted that will not |
| 250 |
# actually change the database at all. If we naively copy an item up the |
| 251 |
# stack in these cases, we have shadowed the real data unnecessarily. |
| 252 |
# Instead, I bothered to add a shitload of extra intelligence, to detect |
| 253 |
# such null writes, and do nothing but return whatever the current value is. |
| 254 |
# Gar gar gar! |
| 255 |
sub _nochange { |
| 256 |
my $this=shift; |
| 257 |
my $driver=shift; |
| 258 |
my $command=shift; |
| 259 |
my $item=shift; |
| 260 |
|
| 261 |
if ($command eq 'addowner') { |
| 262 |
my $value=shift; |
| 263 |
# If the owner is already there, no change. |
| 264 |
foreach my $owner ($driver->owners($item)) { |
| 265 |
return $value if $owner eq $value; |
| 266 |
} |
| 267 |
return; |
| 268 |
} |
| 269 |
elsif ($command eq 'removeowner') { |
| 270 |
my $value=shift; |
| 271 |
|
| 272 |
# If the owner is already in the list, there is a change. |
| 273 |
foreach my $owner ($driver->owners($item)) { |
| 274 |
return if $owner eq $value; |
| 275 |
} |
| 276 |
return $value; # no change |
| 277 |
} |
| 278 |
elsif ($command eq 'removefield') { |
| 279 |
my $value=shift; |
| 280 |
|
| 281 |
# If the field is not present, no change. |
| 282 |
foreach my $field ($driver->fields($item)) { |
| 283 |
return if $field eq $value; |
| 284 |
} |
| 285 |
return $value; # no change |
| 286 |
} |
| 287 |
|
| 288 |
# Ok, the rest is close to the same for fields, flags, and variables. |
| 289 |
my @list; |
| 290 |
my $get; |
| 291 |
if ($command eq 'setfield') { |
| 292 |
@list=$driver->fields($item); |
| 293 |
$get='getfield'; |
| 294 |
} |
| 295 |
elsif ($command eq 'setflag') { |
| 296 |
@list=$driver->flags($item); |
| 297 |
$get='getflag'; |
| 298 |
} |
| 299 |
elsif ($command eq 'setvariable') { |
| 300 |
@list=$driver->variables($item); |
| 301 |
$get='getvariable'; |
| 302 |
} |
| 303 |
else { |
| 304 |
$this->error("internal error; bad command: $command"); |
| 305 |
} |
| 306 |
|
| 307 |
my $thing=shift; |
| 308 |
my $value=shift; |
| 309 |
my $currentvalue=$driver->$get($item, $thing); |
| 310 |
|
| 311 |
# If the thing doesn't exist yet, there will be a change. |
| 312 |
my $exists=0; |
| 313 |
foreach my $i (@list) { |
| 314 |
if ($thing eq $i) { |
| 315 |
$exists=1; |
| 316 |
last; |
| 317 |
} |
| 318 |
} |
| 319 |
return $currentvalue unless $exists; |
| 320 |
|
| 321 |
# If the thing does not have the same value, there will be a change. |
| 322 |
return $currentvalue if $currentvalue eq $value; |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
sub addowner { $_[0]->_change('addowner', @_) } |
| 327 |
# Note that if the last owner of an item is removed, it next item |
| 328 |
# down in the stack is unshadowed and becomes active. May not be |
| 329 |
# the right behavior. |
| 330 |
sub removeowner { $_[0]->_change('removeowner', @_) } |
| 331 |
sub owners { $_[0]->_query('owners', @_) } |
| 332 |
sub getfield { $_[0]->_query('getfield', @_) } |
| 333 |
sub setfield { $_[0]->_change('setfield', @_) } |
| 334 |
sub removefield { $_[0]->_change('removefield', @_) } |
| 335 |
sub fields { $_[0]->_query('fields', @_) } |
| 336 |
sub getflag { $_[0]->_query('getflag', @_) } |
| 337 |
sub setflag { $_[0]->_change('setflag', @_) } |
| 338 |
sub flags { $_[0]->_query('flags', @_) } |
| 339 |
sub getvariable { $_[0]->_query('getvariable', @_) } |
| 340 |
sub setvariable { $_[0]->_change('setvariable', @_) } |
| 341 |
sub variables { $_[0]->_query('variables', @_) } |
| 342 |
|
| 343 |
=head1 AUTHOR |
| 344 |
|
| 345 |
Joey Hess <[email protected]> |
| 346 |
|
| 347 |
=cut |
| 348 |
|
| 349 |
1 |