Debian Codegraph @

Debconf/Db.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::Db - debconf databases
6
7 =cut
8
9 package Debconf::Db;
10 use warnings;
11 use strict;
12 use Debconf::Log qw{:all};
13 use Debconf::Config;
14 use Debconf::DbDriver;
15 our $config;
16 our $templates;
17
18 =head1 DESCRIPTION
19
20 This class makes available a $Debconf::Db::config, which is the root db
21 driver for storing state, and a $Debconf::Db::templates, which is the root
22 db driver for storing template data.
23
24 Requests can be sent directly to the db's by things like
25 $Debconf::Db::config->setfield(...)
26
27 =head1 CLASS METHODS
28
29 =item load
30
31 Loads up the database drivers.
32
33 If a hash of parameters are passed, those parameters are used as the defaults
34 for *every* database driver that is loaded up. Practically, setting
35 (readonly => "true") is the only use of this.
36
37 =cut
38
39 sub load {
40 my $class=shift;
41
42 Debconf::Config->load('', @_); # load default config file
43 $config=Debconf::DbDriver->driver(Debconf::Config->config);
44 if (not ref $config) {
45 die "Configuration database \"".Debconf::Config->config.
46 "\" was not initialized.\n";
47 }
48 $templates=Debconf::DbDriver->driver(Debconf::Config->templates);
49 if (not ref $templates) {
50 die "Template database \"".Debconf::Config->templates.
51 "\" was not initialized.\n";
52 }
53 }
54
55 =item makedriver
56
57 Set up a driver. Pass it all the fields the driver needs, and one more
58 field, called "driver" that specifies the type of driver to make.
59
60 =cut
61
62 sub makedriver {
63 my $class=shift;
64 my %config=@_;
65
66 my $type=$config{driver} or die "driver type not specified (perhaps you need to re-read debconf.conf(5))";
67
68 # Make sure that the class is loaded..
69 if (! UNIVERSAL::can("Debconf::DbDriver::$type", 'new')) {
70 eval qq{use Debconf::DbDriver::$type};
71 die $@ if $@;
72 }
73 delete $config{driver}; # not a field for the object
74
75 # Make object, and pass in the config, and we're done with it.
76 debug db => "making DbDriver of type $type";
77 "Debconf::DbDriver::$type"->new(%config);
78 }
79
80 =item save
81
82 Save the databases, and shutdown the drivers.
83
84 =cut
85
86 sub save {
87 # FIXME: Debconf::Db->save shutdown only
88 # drivers which are declared in Config and Templates fields
89 # in conf file while load method (see above) make and init ALL drivers
90
91 $config->shutdown if $config;
92 # FIXME: if debconf is killed right here, the db is inconsistent.
93 $templates->shutdown if $templates;
94 $config='';
95 $templates='';
96 }
97
98 =head1 AUTHOR
99
100 Joey Hess <[email protected]>
101
102 =cut
103
104 1