| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::DbDriver::Pipe - read/write database from file descriptors |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::DbDriver::Pipe; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Log qw(:all); |
| 13 |
use base 'Debconf::DbDriver::Cache'; |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
This is a debconf database driver that reads the db from a file descriptor when |
| 18 |
it starts, and writes it out to another when it saves it. By default, stdin |
| 19 |
and stdout are used. |
| 20 |
|
| 21 |
=head1 FIELDS |
| 22 |
|
| 23 |
=over 4 |
| 24 |
|
| 25 |
=item infd |
| 26 |
|
| 27 |
File descriptor number to read from. Defaults to reading from stdin. If |
| 28 |
it's set to "none", the db won't bother to try to read in an initial |
| 29 |
database. |
| 30 |
|
| 31 |
=item outfd |
| 32 |
|
| 33 |
File descriptor number to write to. Defaults to writing to stdout. If |
| 34 |
it's set to "none", the db will be thrown away rather than saved. |
| 35 |
|
| 36 |
Setting both infd and outfd to none gets you a writable temporary db in |
| 37 |
memory. |
| 38 |
|
| 39 |
=item format |
| 40 |
|
| 41 |
The Format object to use for reading and writing. |
| 42 |
|
| 43 |
In the config file, just the name of the format to use, such as '822' can |
| 44 |
be specified. Default is 822. |
| 45 |
|
| 46 |
=back |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
use fields qw(infd outfd format); |
| 51 |
|
| 52 |
=head1 METHODS |
| 53 |
|
| 54 |
=head2 init |
| 55 |
|
| 56 |
On initialization, load the entire db into memory and populate the cache. |
| 57 |
|
| 58 |
=cut |
| 59 |
|
| 60 |
sub init { |
| 61 |
my $this=shift; |
| 62 |
|
| 63 |
$this->{format} = "822" unless exists $this->{format}; |
| 64 |
|
| 65 |
$this->error("No format specified") unless $this->{format}; |
| 66 |
eval "use Debconf::Format::$this->{format}"; |
| 67 |
if ($@) { |
| 68 |
$this->error("Error setting up format object $this->{format}: $@"); |
| 69 |
} |
| 70 |
$this->{format}="Debconf::Format::$this->{format}"->new; |
| 71 |
if (not ref $this->{format}) { |
| 72 |
$this->error("Unable to make format object"); |
| 73 |
} |
| 74 |
|
| 75 |
my $fh; |
| 76 |
if (defined $this->{infd}) { |
| 77 |
if ($this->{infd} ne 'none') { |
| 78 |
open ($fh, "<&=", $this->{infd}) or |
| 79 |
$this->error("could not open file descriptor #$this->{infd}: $!"); |
| 80 |
} |
| 81 |
} |
| 82 |
else { |
| 83 |
open ($fh, '<&', \*STDIN); |
| 84 |
} |
| 85 |
|
| 86 |
$this->SUPER::init(@_); |
| 87 |
|
| 88 |
debug "db $this->{name}" => "loading database"; |
| 89 |
|
| 90 |
# Now read in the whole file using the Format object. |
| 91 |
if (defined $fh) { |
| 92 |
while (! eof $fh) { |
| 93 |
my ($item, $cache)=$this->{format}->read($fh); |
| 94 |
$this->{cache}->{$item}=$cache; |
| 95 |
} |
| 96 |
close $fh; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
=sub shutdown |
| 101 |
|
| 102 |
Save the entire cache out to the fd. Always writes the cache, even if it's |
| 103 |
not dirty, for consistency's sake. |
| 104 |
|
| 105 |
=cut |
| 106 |
|
| 107 |
sub shutdown { |
| 108 |
my $this=shift; |
| 109 |
|
| 110 |
return if $this->{readonly}; |
| 111 |
|
| 112 |
my $fh; |
| 113 |
if (defined $this->{outfd}) { |
| 114 |
if ($this->{outfd} ne 'none') { |
| 115 |
open ($fh, ">&=", $this->{outfd}) or |
| 116 |
$this->error("could not open file descriptor #$this->{outfd}: $!"); |
| 117 |
} |
| 118 |
} |
| 119 |
else { |
| 120 |
open ($fh, '>&', \*STDOUT); |
| 121 |
} |
| 122 |
|
| 123 |
if (defined $fh) { |
| 124 |
$this->{format}->beginfile; |
| 125 |
foreach my $item (sort keys %{$this->{cache}}) { |
| 126 |
next unless defined $this->{cache}->{$item}; # skip deleted |
| 127 |
$this->{format}->write($fh, $this->{cache}->{$item}, $item) |
| 128 |
or $this->error("could not write to pipe: $!"); |
| 129 |
} |
| 130 |
$this->{format}->endfile; |
| 131 |
close $fh or $this->error("could not close pipe: $!"); |
| 132 |
} |
| 133 |
|
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
=sub load |
| 138 |
|
| 139 |
Sorry bud, if it's not in the cache, it doesn't exist. |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
sub load { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
=head1 AUTHOR |
| 148 |
|
| 149 |
Joey Hess <[email protected]> |
| 150 |
|
| 151 |
=cut |
| 152 |
|
| 153 |
1 |