Debian Codegraph @

Debconf/DbDriver/Directory.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::DbDriver::Directory - store database in a directory
6
7 =cut
8
9 package Debconf::DbDriver::Directory;
10 use warnings;
11 use strict;
12 use Debconf::Log qw(:all);
13 use IO::File;
14 use POSIX ();
15 use Fcntl qw(:DEFAULT :flock);
16 use Debconf::Iterator;
17 use base 'Debconf::DbDriver::Cache';
18
19 =head1 DESCRIPTION
20
21 This is a debconf database driver that uses a plain text file for
22 each individual item. The files are contained in a directory tree, and
23 are named according to item names, with slashes replaced by colons.
24
25 It uses a Format module to handle reading and writing the files, so the
26 files can be of any format.
27
28 This is a foundation for other DbDrivers, and is not itself usable as one.
29
30 =head1 FIELDS
31
32 =over 4
33
34 =item directory
35
36 The directory to put the files in.
37
38 =item extension
39
40 An optional extension to tack on the end of each filename.
41
42 =item format
43
44 The Format object to use for reading and writing files.
45
46 In the config file, just the name of the format to use, such as '822' can
47 be specified. Default is 822.
48
49 =back
50
51 =head1 METHODS
52
53 =cut
54
55 use fields qw(directory extension lock format);
56
57 =head2 init
58
59 On initialization, we ensure that the directory exists.
60
61 =cut
62
63 sub init {
64 my $this=shift;
65
66 $this->{extension} = "" unless exists $this->{extension};
67 $this->{format} = "822" unless exists $this->{format};
68 $this->{backup} = 1 unless exists $this->{backup};
69
70 $this->error("No format specified") unless $this->{format};
71 eval "use Debconf::Format::$this->{format}";
72 if ($@) {
73 $this->error("Error setting up format object $this->{format}: $@");
74 }
75 $this->{format}="Debconf::Format::$this->{format}"->new;
76 if (not ref $this->{format}) {
77 $this->error("Unable to make format object");
78 }
79
80 $this->error("No directory specified") unless $this->{directory};
81 if (exists $this->{root}) {
82 $this->{directory} = $this->{root} . $this->{directory};
83 }
84 if (not -d $this->{directory} and not $this->{readonly}) {
85 mkdir $this->{directory} ||
86 $this->error("mkdir $this->{directory}:$!");
87 }
88 if (not -d $this->{directory}) {
89 $this->error($this->{directory}." does not exist");
90 }
91 debug "db $this->{name}" => "started; directory is $this->{directory}";
92
93 if (! $this->{readonly}) {
94 # Now lock the directory. I use a lockfile named '.lock' in the
95 # directory, and flock locking. I don't wait on locks, just
96 # error out. Since I open a lexical filehandle, the lock is
97 # dropped when this object is destroyed.
98 open ($this->{lock}, ">", $this->{directory}."/.lock") or
99 $this->error("could not lock $this->{directory}: $!");
100 while (! flock($this->{lock}, LOCK_EX | LOCK_NB)) {
101 next if $! == &POSIX::EINTR;
102 $this->error("$this->{directory} is locked by another process: $!");
103 last;
104 }
105 }
106 }
107
108 =head2 load(itemname)
109
110 Uses the format object to load up the item.
111
112 =cut
113
114 sub load {
115 my $this=shift;
116 my $item=shift;
117
118 debug "db $this->{name}" => "loading $item";
119 my $file=$this->{directory}.'/'.$this->filename($item);
120 return unless -e $file;
121
122 my $fh=IO::File->new;
123 open($fh, "<", $file) or $this->error("$file: $!");
124 $this->cacheadd($this->{format}->read($fh));
125 close $fh;
126 }
127
128 =head2 save(itemname,value)
129
130 Use the format object to write out the item.
131
132 Makes sure that items with a type of "password" are written out to mode 600
133 files.
134
135 =cut
136
137 sub save {
138 my $this=shift;
139 my $item=shift;
140 my $data=shift;
141
142 return unless $this->accept($item);
143 return if $this->{readonly};
144 debug "db $this->{name}" => "saving $item";
145
146 my $file=$this->{directory}.'/'.$this->filename($item);
147
148 # Write out passwords mode 600.
149 my $fh=IO::File->new;
150 if ($this->ispassword($item)) {
151 sysopen($fh, $file."-new", O_WRONLY|O_TRUNC|O_CREAT, 0600)
152 or $this->error("$file-new: $!");
153 }
154 else {
155 open($fh, ">", "$file-new") or $this->error("$file-new: $!");
156 }
157 $this->{format}->beginfile;
158 $this->{format}->write($fh, $data, $item)
159 or $this->error("could not write $file-new: $!");
160 $this->{format}->endfile;
161
162 # Ensure it is synced, to disk buffering doesn't result in
163 # inconsistencies.
164 $fh->flush or $this->error("could not flush $file-new: $!");
165 $fh->sync or $this->error("could not sync $file-new: $!");
166 close $fh or $this->error("could not close $file-new: $!");
167
168 # Now rename the old file to -old (if doing backups),
169 # and put -new in its place.
170 if (-e $file && $this->{backup}) {
171 rename($file, $file."-old") or
172 debug "db $this->{name}" => "rename failed: $!";
173 }
174 rename("$file-new", $file) or $this->error("rename failed: $!");
175 }
176
177 =sub shutdown
178
179 All this function needs to do is unlock the database. Saving happens
180 whenever something is saved.
181
182 =cut
183
184 sub shutdown {
185 my $this=shift;
186
187 $this->SUPER::shutdown(@_);
188 delete $this->{lock};
189 return 1;
190 }
191
192 =head2 exists(itemname)
193
194 Simply check for file existance, after querying the cache.
195
196 =cut
197
198 sub exists {
199 my $this=shift;
200 my $name=shift;
201
202 # Check the cache first.
203 my $incache=$this->SUPER::exists($name);
204 return $incache if not defined $incache or $incache;
205
206 return -e $this->{directory}.'/'.$this->filename($name);
207 }
208
209 =head2 remove(itemname)
210
211 Unlink a file.
212
213 =cut
214
215 sub remove {
216 my $this=shift;
217 my $name=shift;
218
219 return if $this->{readonly} or not $this->accept($name);
220 debug "db $this->{name}" => "removing $name";
221 my $file=$this->{directory}.'/'.$this->filename($name);
222 unlink $file or return;
223 if (-e $file."-old") {
224 unlink $file."-old" or return;
225 }
226 return 1;
227 }
228
229 =head2 accept(itemname)
230
231 Accept is overridden to reject any item names that contain either "../" or
232 "/..". Either could be used to break out of the directory tree.
233
234 =cut
235
236 sub accept {
237 my $this=shift;
238 my $name=shift;
239
240 return if $name=~m#\.\./# or $name=~m#/\.\.#;
241 $this->SUPER::accept($name, @_);
242 }
243
244 =head1 AUTHOR
245
246 Joey Hess <[email protected]>
247
248 =cut
249
250 1