Debian Codegraph @

Debconf/DbDriver/PackageDir.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::DbDriver::PackageDir - store database in a directory
6
7 =cut
8
9 package Debconf::DbDriver::PackageDir;
10 use warnings;
11 use strict;
12 use Debconf::Log qw(:all);
13 use IO::File;
14 use Fcntl qw(:DEFAULT :flock);
15 use Debconf::Iterator;
16 use base 'Debconf::DbDriver::Directory';
17
18 =head1 DESCRIPTION
19
20 This is a debconf database driver that uses a plain text file for each
21 individual "subdirectory" of the internal tree.
22
23 It uses a Format module to handle reading and writing the files, so the
24 files can be of any format.
25
26 =head1 FIELDS
27
28 =over 4
29
30 =item directory
31
32 The directory to put the files in.
33
34 =item extension
35
36 An optional extension to tack on the end of each filename.
37
38 =item mode
39
40 The (octal) permissions to create the files with if they do not exist.
41 Defaults to 600, since the files could contain passwords in some
42 circumstances.
43
44 =item format
45
46 The Format object to use for reading and writing files.
47
48 In the config file, just the name of the format to use, such as '822' can
49 be specified. Default is 822.
50
51 =back
52
53 =head1 METHODS
54
55 =cut
56
57 use fields qw(mode _loaded);
58
59 =head2 init
60
61 On initialization, we ensure that the directory exists.
62
63 =cut
64
65 sub init {
66 my $this=shift;
67
68 if (exists $this->{mode}) {
69 # Convert user input to octal.
70 $this->{mode} = oct($this->{mode});
71 }
72 else {
73 $this->{mode} = 0600;
74 }
75 $this->SUPER::init(@_);
76 }
77
78 =head2 loadfile(filename)
79
80 Loads up a file by name, after checking to make sure it's not ben loaded
81 already. Omit the directory from the filename.
82
83 =cut
84
85 sub loadfile {
86 my $this=shift;
87 my $file=$this->{directory}."/".shift;
88
89 return if $this->{_loaded}->{$file};
90 $this->{_loaded}->{$file}=1;
91
92 debug "db $this->{name}" => "loading $file";
93 return unless -e $file;
94
95 my $fh=IO::File->new;
96 open($fh, "<", $file) or $this->error("$file: $!");
97 my @item = $this->{format}->read($fh);
98 while (@item) {
99 $this->cacheadd(@item);
100 @item = $this->{format}->read($fh);
101 }
102 close $fh;
103 }
104
105 =head2 load(itemname)
106
107 After checking the cache, find the file that contains the item, then use
108 the format object to load up the item (and all other items from that file,
109 which get cached).
110
111 =cut
112
113 sub load {
114 my $this=shift;
115 my $item=shift;
116 $this->loadfile($this->filename($item));
117 }
118
119 =head2 filename(itemname)
120
121 Converts the item name into a filename. (Minus the base directory.)
122
123 =cut
124
125 sub filename {
126 my $this=shift;
127 my $item=shift;
128
129 if ($item =~ m!^([^/]+)(?:/|$)!) {
130 return $1.$this->{extension};
131 }
132 else {
133 $this->error("failed parsing item name \"$item\"\n");
134 }
135 }
136
137 =head2 iterator
138
139 This iterator is not very well written in general, as it loads up all files
140 that were not previously loaded, and then lets the super class iterate over
141 the populated cache. However, all iteration in debconf so far iterates over
142 the whole set, so it doesn't matter.
143
144 =cut
145
146 sub iterator {
147 my $this=shift;
148
149 my $handle;
150 opendir($handle, $this->{directory}) ||
151 $this->error("opendir: $!");
152
153 while (my $file=readdir($handle)) {
154 next if length $this->{extension} and
155 not $file=~m/$this->{extension}/;
156 next unless -f $this->{directory}."/".$file;
157 next if $file eq '.lock' || $file =~ /-old$/;
158 $this->loadfile($file);
159 }
160
161 # grandparent's method; parent's does unwanted stuff
162 $this->SUPER::iterator;
163 }
164
165 =head2 exists(itemname)
166
167 Check the cache first, then check to see if a file that might contain the
168 item exists, load it, and test existence.
169
170 =cut
171
172 sub exists {
173 my $this=shift;
174 my $name=shift;
175 # Check the cache first.
176 my $incache=$this->Debconf::DbDriver::Cache::exists($name);
177 return $incache if not defined $incache or $incache;
178 my $file=$this->{directory}.'/'.$this->filename($name);
179 return unless -e $file;
180
181 $this->load($name);
182
183 # Now check the cache again; if it exists load will have put it
184 # into the cache.
185 return $this->Debconf::DbDriver::Cache::exists($name);
186 }
187
188 =head2 shutdown
189
190 This has to break the abstraction and access the underlying cache directly.
191
192 =cut
193
194 sub shutdown {
195 my $this=shift;
196
197 return if $this->{readonly};
198
199 my (%files, %filecontents, %killfiles, %dirtyfiles);
200 foreach my $item (keys %{$this->{cache}}) {
201 my $file=$this->filename($item);
202 $files{$file}++;
203
204 if (! defined $this->{cache}->{$item}) {
205 $killfiles{$file}++;
206 delete $this->{cache}->{$item};
207 }
208 else {
209 push @{$filecontents{$file}}, $item;
210 }
211
212 if ($this->{dirty}->{$item}) {
213 $dirtyfiles{$file}++;
214 $this->{dirty}->{$item}=0;
215 }
216 }
217
218 foreach my $file (keys %files) {
219 if (! $filecontents{$file} && $killfiles{$file}) {
220 debug "db $this->{name}" => "removing $file";
221 my $filename=$this->{directory}."/".$file;
222 unlink $filename or
223 $this->error("unable to remove $filename: $!");
224 if (-e $filename."-old") {
225 unlink $filename."-old" or
226 $this->error("unable to remove $filename-old: $!");
227 }
228 }
229 elsif ($dirtyfiles{$file}) {
230 debug "db $this->{name}" => "saving $file";
231 my $filename=$this->{directory}."/".$file;
232
233 sysopen(my $fh, $filename."-new",
234 O_WRONLY|O_TRUNC|O_CREAT,$this->{mode}) or
235 $this->error("could not write $filename-new: $!");
236 $this->{format}->beginfile;
237 foreach my $item (@{$filecontents{$file}}) {
238 $this->{format}->write($fh, $this->{cache}->{$item}, $item)
239 or $this->error("could not write $filename-new: $!");
240 }
241 $this->{format}->endfile;
242
243 # Ensure -new is flushed.
244 $fh->flush or $this->error("could not flush $filename-new: $!");
245 # Ensure it is synced, because I've had problems with
246 # disk caching resulting in truncated files.
247 $fh->sync or $this->error("could not sync $filename-new: $!");
248
249 # Now rename the old file to -old (if doing backups),
250 # and put -new in its place.
251 if (-e $filename && $this->{backup}) {
252 rename($filename, $filename."-old") or
253 debug "db $this->{name}" => "rename failed: $!";
254 }
255 rename($filename."-new", $filename) or
256 $this->error("rename failed: $!");
257 }
258 }
259
260 $this->SUPER::shutdown(@_);
261 return 1;
262 }
263
264 =head1 AUTHOR
265
266 Joey Hess <[email protected]>
267
268 =cut
269
270 1