Debian Codegraph @

Debconf/DbDriver/File.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::DbDriver::File - store database in flat file
6
7 =cut
8
9 package Debconf::DbDriver::File;
10 use warnings;
11 use strict;
12 use Debconf::Log qw(:all);
13 use Cwd 'abs_path';
14 use POSIX ();
15 use Fcntl qw(:DEFAULT :flock);
16 use IO::Handle;
17 use base 'Debconf::DbDriver::Cache';
18
19 =head1 DESCRIPTION
20
21 This is a debconf database driver that uses a single flat file for storing
22 the database. It uses more memory than most other drivers, has a slower
23 startup time (it reads the whole file at startup), and is very fast
24 thereafter until shutdown time (when it writes the whole file out). Of
25 course, the resulting single file is very handy to manage.
26
27 =head1 FIELDS
28
29 =over 4
30
31 =item filename
32
33 The file to use as the database
34
35 =item mode
36
37 The (octal) permissions to create the file with if it does not exist.
38 Defaults to 600, since the file could contain passwords in some circumstances.
39
40 =item format
41
42 The Format object to use for reading and writing the file.
43
44 In the config file, just the name of the format to use, such as '822' can
45 be specified. Default is 822.
46
47 =back
48
49 =cut
50
51 use fields qw(filename mode format _fh);
52
53 =head1 METHODS
54
55 =head2 init
56
57 On initialization, load the entire file into memory and populate the cache.
58
59 =cut
60
61 sub init {
62 my $this=shift;
63
64 if (exists $this->{mode}) {
65 # Convert user input to octal.
66 $this->{mode} = oct($this->{mode});
67 }
68 else {
69 $this->{mode} = 0600;
70 }
71 $this->{format} = "822" unless exists $this->{format};
72 $this->{backup} = 1 unless exists $this->{backup};
73
74 $this->error("No format specified") unless $this->{format};
75 eval "use Debconf::Format::$this->{format}";
76 if ($@) {
77 $this->error("Error setting up format object $this->{format}: $@");
78 }
79 $this->{format}="Debconf::Format::$this->{format}"->new;
80 if (not ref $this->{format}) {
81 $this->error("Unable to make format object");
82 }
83
84 $this->error("No filename specified") unless $this->{filename};
85
86 my ($directory)=$this->{filename}=~m!^(.*)/[^/]+!;
87 if (length $directory and not -d $directory) {
88 mkdir $directory || $this->error("mkdir $directory:$!");
89 }
90
91 if (exists $this->{root}) {
92 $this->{filename} = $this->{root} . $this->{filename};
93 }
94 $this->{filename} = abs_path($this->{filename});
95
96 debug "db $this->{name}" => "started; filename is $this->{filename}";
97
98 # Make sure that the file exists, and set the mode too.
99 if (! -e $this->{filename}) {
100 $this->{backup}=0;
101 sysopen(my $fh, $this->{filename},
102 O_WRONLY|O_TRUNC|O_CREAT,$this->{mode}) or
103 $this->error("could not open $this->{filename}");
104 close $fh;
105 }
106
107 my $implicit_readonly=0;
108 if (! $this->{readonly}) {
109 # Open file for read but also with write access so
110 # exclusive lock can be done portably.
111 if (open ($this->{_fh}, "+<", $this->{filename})) {
112 # Now lock the file with flock locking. I don't
113 # wait on locks, just error out. Since I open a
114 # lexical filehandle, the lock is dropped when
115 # this object is destroyed.
116 while (! flock($this->{_fh}, LOCK_EX | LOCK_NB)) {
117 next if $! == &POSIX::EINTR;
118 $this->error("$this->{filename} is locked by another process: $!");
119 last;
120 }
121 }
122 else {
123 # fallthrough to readonly mode
124 $implicit_readonly=1;
125 }
126 }
127 if ($this->{readonly} || $implicit_readonly) {
128 if (! open ($this->{_fh}, "<", $this->{filename})) {
129 $this->error("could not open $this->{filename}: $!");
130 return; # always abort, even if not throwing fatal error
131 }
132 }
133
134 $this->SUPER::init(@_);
135
136 debug "db $this->{name}" => "loading database";
137
138 # Now read in the whole file using the Format object.
139 while (! eof $this->{_fh}) {
140 my ($item, $cache)=$this->{format}->read($this->{_fh});
141 $this->{cache}->{$item}=$cache;
142 }
143 # Close only if we are not keeping a lock.
144 if ($this->{readonly} || $implicit_readonly) {
145 close $this->{_fh};
146 }
147 }
148
149 =sub shutdown
150
151 Save the entire cache out to the file, then close the file.
152
153 =cut
154
155 sub shutdown {
156 my $this=shift;
157
158 return if $this->{readonly};
159
160 if (grep { $this->{dirty}->{$_} } keys %{$this->{cache}}) {
161 debug "db $this->{name}" => "saving database";
162 }
163 else {
164 debug "db $this->{name}" => "no database changes, not saving";
165
166 # But do drop the lock.
167 delete $this->{_fh};
168
169 return 1;
170 }
171
172 # Write out the file to -new, locking it as we go.
173 sysopen(my $fh, $this->{filename}."-new",
174 O_WRONLY|O_TRUNC|O_CREAT,$this->{mode}) or
175 $this->error("could not write $this->{filename}-new: $!");
176 while (! flock($fh, LOCK_EX | LOCK_NB)) {
177 next if $! == &POSIX::EINTR;
178 $this->error("$this->{filename}-new is locked by another process: $!");
179 last;
180 }
181 $this->{format}->beginfile;
182 foreach my $item (sort keys %{$this->{cache}}) {
183 next unless defined $this->{cache}->{$item}; # skip deleted
184 $this->{format}->write($fh, $this->{cache}->{$item}, $item)
185 or $this->error("could not write $this->{filename}-new: $!");
186 }
187 $this->{format}->endfile;
188
189 # Ensure -new is flushed.
190 $fh->flush or $this->error("could not flush $this->{filename}-new: $!");
191 # Ensure it is synced, because I've had problems with disk caching
192 # resulting in truncated files.
193 $fh->sync or $this->error("could not sync $this->{filename}-new: $!");
194
195 # Now rename the old file to -old (if doing backups), and put -new
196 # in its place.
197 if (-e $this->{filename} && $this->{backup}) {
198 rename($this->{filename}, $this->{filename}."-old") or
199 debug "db $this->{name}" => "rename failed: $!";
200 }
201 rename($this->{filename}."-new", $this->{filename}) or
202 $this->error("rename failed: $!");
203
204 # Now drop the lock on -old (the lock on -new will be removed
205 # when this function returns and $fh goes out of scope).
206 delete $this->{_fh};
207
208 return 1;
209 }
210
211 =sub load
212
213 Sorry bud, if it's not in the cache, it doesn't exist.
214
215 =cut
216
217 sub load {
218 return;
219 }
220
221 =head1 AUTHOR
222
223 Joey Hess <[email protected]>
224
225 =cut
226
227 1