3.1.0 changes:
[BackupPC.git] / lib / BackupPC / Storage / Text.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::Storage::Text package
4 #
5 # DESCRIPTION
6 #
7 #   This library defines a BackupPC::Storage::Text class that implements
8 #   BackupPC's persistent state storage (config, host info, backup
9 #   and restore info) using text files.
10 #
11 # AUTHOR
12 #   Craig Barratt  <cbarratt@users.sourceforge.net>
13 #
14 # COPYRIGHT
15 #   Copyright (C) 2004-2007  Craig Barratt
16 #
17 #   This program is free software; you can redistribute it and/or modify
18 #   it under the terms of the GNU General Public License as published by
19 #   the Free Software Foundation; either version 2 of the License, or
20 #   (at your option) any later version.
21 #
22 #   This program is distributed in the hope that it will be useful,
23 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
24 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 #   GNU General Public License for more details.
26 #
27 #   You should have received a copy of the GNU General Public License
28 #   along with this program; if not, write to the Free Software
29 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 #
31 #========================================================================
32 #
33 # Version 3.1.0, released 25 Nov 2007.
34 #
35 # See http://backuppc.sourceforge.net.
36 #
37 #========================================================================
38
39 package BackupPC::Storage::Text;
40
41 use strict;
42 use vars qw(%Conf);
43 use Data::Dumper;
44 use File::Path;
45 use Fcntl qw/:flock/;
46
47 sub new
48 {
49     my $class = shift;
50     my($flds, $paths) = @_;
51
52     my $s = bless {
53         %$flds,
54         %$paths,
55     }, $class;
56     return $s;
57 }
58
59 sub setPaths
60 {
61     my $class = shift;
62     my($paths) = @_;
63
64     foreach my $v ( keys(%$paths) ) {
65         $class->{$v} = $paths->{$v};
66     }
67 }
68
69 sub BackupInfoRead
70 {
71     my($s, $host) = @_;
72     local(*BK_INFO, *LOCK);
73     my(@Backups);
74
75     flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
76     if ( open(BK_INFO, "$s->{TopDir}/pc/$host/backups") ) {
77         binmode(BK_INFO);
78         while ( <BK_INFO> ) {
79             s/[\n\r]+//;
80             next if ( !/^(\d+\t(incr|full|partial).*)/ );
81             $_ = $1;
82             @{$Backups[@Backups]}{@{$s->{BackupFields}}} = split(/\t/);
83         }
84         close(BK_INFO);
85     }
86     close(LOCK);
87     #
88     # Default the version field.  Prior to 3.0.0 the xferMethod
89     # field is empty, so we use that to figure out the version.
90     #
91     for ( my $i = 0 ; $i < @Backups ; $i++ ) {
92         next if ( $Backups[$i]{version} ne "" );
93         if ( $Backups[$i]{xferMethod} eq "" ) {
94             $Backups[$i]{version} = "2.1.2";
95         } else {
96             $Backups[$i]{version} = "3.0.0";
97         }
98     }
99     return @Backups;
100 }
101
102 sub BackupInfoWrite
103 {
104     my($s, $host, @Backups) = @_;
105     my($i, $contents, $fileOk);
106
107     #
108     # Generate the file contents
109     #
110     for ( $i = 0 ; $i < @Backups ; $i++ ) {
111         my %b = %{$Backups[$i]};
112         $contents .= join("\t", @b{@{$s->{BackupFields}}}) . "\n";
113     }
114     
115     #
116     # Write the file
117     #
118     return $s->TextFileWrite("$s->{TopDir}/pc/$host/backups", $contents);
119 }
120
121 sub RestoreInfoRead
122 {
123     my($s, $host) = @_;
124     local(*RESTORE_INFO, *LOCK);
125     my(@Restores);
126
127     flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
128     if ( open(RESTORE_INFO, "$s->{TopDir}/pc/$host/restores") ) {
129         binmode(RESTORE_INFO);
130         while ( <RESTORE_INFO> ) {
131             s/[\n\r]+//;
132             next if ( !/^(\d+.*)/ );
133             $_ = $1;
134             @{$Restores[@Restores]}{@{$s->{RestoreFields}}} = split(/\t/);
135         }
136         close(RESTORE_INFO);
137     }
138     close(LOCK);
139     return @Restores;
140 }
141
142 sub RestoreInfoWrite
143 {
144     my($s, $host, @Restores) = @_;
145     local(*RESTORE_INFO, *LOCK);
146     my($i, $contents, $fileOk);
147
148     #
149     # Generate the file contents
150     #
151     for ( $i = 0 ; $i < @Restores ; $i++ ) {
152         my %b = %{$Restores[$i]};
153         $contents .= join("\t", @b{@{$s->{RestoreFields}}}) . "\n";
154     }
155
156     #
157     # Write the file
158     #
159     return $s->TextFileWrite("$s->{TopDir}/pc/$host/restores", $contents);
160 }
161
162 sub ArchiveInfoRead
163 {
164     my($s, $host) = @_;
165     local(*ARCHIVE_INFO, *LOCK);
166     my(@Archives);
167
168     flock(LOCK, LOCK_EX) if open(LOCK, "$s->{TopDir}/pc/$host/LOCK");
169     if ( open(ARCHIVE_INFO, "$s->{TopDir}/pc/$host/archives") ) {
170         binmode(ARCHIVE_INFO);
171         while ( <ARCHIVE_INFO> ) {
172             s/[\n\r]+//;
173             next if ( !/^(\d+.*)/ );
174             $_ = $1;
175             @{$Archives[@Archives]}{@{$s->{ArchiveFields}}} = split(/\t/);
176         }
177         close(ARCHIVE_INFO);
178     }
179     close(LOCK);
180     return @Archives;
181 }
182
183 sub ArchiveInfoWrite
184 {
185     my($s, $host, @Archives) = @_;
186     local(*ARCHIVE_INFO, *LOCK);
187     my($i, $contents, $fileOk);
188
189     #
190     # Generate the file contents
191     #
192     for ( $i = 0 ; $i < @Archives ; $i++ ) {
193         my %b = %{$Archives[$i]};
194         $contents .= join("\t", @b{@{$s->{ArchiveFields}}}) . "\n";
195     }
196
197     #
198     # Write the file
199     #
200     return $s->TextFileWrite("$s->{TopDir}/pc/$host/archives", $contents);
201 }
202
203 #
204 # Write a text file as safely as possible.  We write to
205 # a new file, verify the file, and the rename the file.
206 # The previous version of the file is renamed with a
207 # .old extension.
208 #
209 sub TextFileWrite
210 {
211     my($s, $file, $contents) = @_;
212     local(*FD, *LOCK);
213     my($fileOk);
214
215     (my $dir = $file) =~ s{(.+)/(.+)}{$1};
216
217     mkpath($dir, 0, 0775) if ( !-d $dir );
218     if ( open(FD, ">", "$file.new") ) {
219         binmode(FD);
220         print FD $contents;
221         close(FD);
222         #
223         # verify the file
224         #
225         if ( open(FD, "<", "$file.new") ) {
226             binmode(FD);
227             if ( join("", <FD>) ne $contents ) {
228                 return "TextFileWrite: Failed to verify $file.new";
229             } else {
230                 $fileOk = 1;
231             }
232             close(FD);
233         }
234     }
235     if ( $fileOk ) {
236         my $lock;
237         
238         if ( open(LOCK, "$dir/LOCK") || open(LOCK, ">", "$dir/LOCK") ) {
239             $lock = 1;
240             flock(LOCK, LOCK_EX);
241         }
242         if ( -s "$file" ) {
243             unlink("$file.old")           if ( -f "$file.old" );
244             rename("$file", "$file.old")  if ( -f "$file" );
245         } else {
246             unlink("$file") if ( -f "$file" );
247         }
248         rename("$file.new", "$file") if ( -f "$file.new" );
249         close(LOCK) if ( $lock );
250     } else {
251         return "TextFileWrite: Failed to write $file.new";
252     }
253     return;
254 }
255
256 sub ConfigPath
257 {
258     my($s, $host) = @_;
259
260     return "$s->{ConfDir}/config.pl" if ( !defined($host) );
261     if ( $s->{useFHS} ) {
262         return "$s->{ConfDir}/pc/$host.pl";
263     } else {
264         return "$s->{TopDir}/pc/$host/config.pl"
265             if ( -f "$s->{TopDir}/pc/$host/config.pl" );
266         return "$s->{ConfDir}/$host.pl"
267             if ( $host ne "config" && -f "$s->{ConfDir}/$host.pl" );
268         return "$s->{ConfDir}/pc/$host.pl";
269     }
270 }
271
272 sub ConfigDataRead
273 {
274     my($s, $host) = @_;
275     my($ret, $mesg, $config, @configs);
276
277     #
278     # TODO: add lock
279     #
280     my $conf = {};
281     my $configPath = $s->ConfigPath($host);
282
283     push(@configs, $configPath) if ( -f $configPath );
284     foreach $config ( @configs ) {
285         %Conf = ();
286         if ( !defined($ret = do $config) && ($! || $@) ) {
287             $mesg = "Couldn't open $config: $!" if ( $! );
288             $mesg = "Couldn't execute $config: $@" if ( $@ );
289             $mesg =~ s/[\n\r]+//;
290             return ($mesg, $conf);
291         }
292         %$conf = ( %$conf, %Conf );
293     }
294
295     #
296     # Promote BackupFilesOnly and BackupFilesExclude to hashes
297     #
298     foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
299         next if ( !defined($conf->{$param}) || ref($conf->{$param}) eq "HASH" );
300         $conf->{$param} = [ $conf->{$param} ]
301                                 if ( ref($conf->{$param}) ne "ARRAY" );
302         $conf->{$param} = { "*" => $conf->{$param} };
303     }
304
305     #
306     # Handle backward compatibility with defunct BlackoutHourBegin,
307     # BlackoutHourEnd, and BlackoutWeekDays parameters.
308     #
309     if ( defined($conf->{BlackoutHourBegin}) ) {
310         push(@{$conf->{BlackoutPeriods}},
311              {
312                  hourBegin => $conf->{BlackoutHourBegin},
313                  hourEnd   => $conf->{BlackoutHourEnd},
314                  weekDays  => $conf->{BlackoutWeekDays},
315              }
316         );
317         delete($conf->{BlackoutHourBegin});
318         delete($conf->{BlackoutHourEnd});
319         delete($conf->{BlackoutWeekDays});
320     }
321
322     return (undef, $conf);
323 }
324
325 sub ConfigDataWrite
326 {
327     my($s, $host, $newConf) = @_;
328
329     my $configPath = $s->ConfigPath($host);
330
331     my($err, $contents) = $s->ConfigFileMerge("$configPath", $newConf);
332     if ( defined($err) ) {
333         return $err;
334     } else {
335         #
336         # Write the file
337         #
338         return $s->TextFileWrite($configPath, $contents);
339     }
340 }
341
342 sub ConfigFileMerge
343 {
344     my($s, $inFile, $newConf) = @_;
345     local(*C);
346     my($contents, $skipExpr, $fakeVar);
347     my $done = {};
348
349     if ( -f $inFile ) {
350         #
351         # Match existing settings in current config file
352         #
353         open(C, $inFile)
354             || return ("ConfigFileMerge: can't open/read $inFile", undef);
355         binmode(C);
356
357         while ( <C> ) {
358             if ( /^\s*\$Conf\{([^}]*)\}\s*=(.*)/ ) {
359                 my $var = $1;
360                 $skipExpr = "\$fakeVar = $2\n";
361                 if ( exists($newConf->{$var}) ) {
362                     my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
363                     $d->Indent(1);
364                     $d->Terse(1);
365                     my $value = $d->Dump;
366                     $value =~ s/(.*)\n/$1;\n/s;
367                     $contents .= "\$Conf{$var} = " . $value;
368                     $done->{$var} = 1;
369                 }
370             } elsif ( defined($skipExpr) ) {
371                 $skipExpr .= $_;
372             } else {
373                 $contents .= $_;
374             }
375             if ( defined($skipExpr)
376                     && ($skipExpr =~ /^\$fakeVar = *<</
377                         || $skipExpr =~ /;[\n\r]*$/) ) {
378                 #
379                 # if we have a complete expression, then we are done
380                 # skipping text from the original config file.
381                 #
382                 $skipExpr = $1 if ( $skipExpr =~ /(.*)/s );
383                 eval($skipExpr);
384                 $skipExpr = undef if ( $@ eq "" );
385             }
386         }
387         close(C);
388     }
389
390     #
391     # Add new entries not matched in current config file
392     #
393     foreach my $var ( sort(keys(%$newConf)) ) {
394         next if ( $done->{$var} );
395         my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
396         $d->Indent(1);
397         $d->Terse(1);
398         my $value = $d->Dump;
399         $value =~ s/(.*)\n/$1;\n/s;
400         $contents .= "\$Conf{$var} = " . $value;
401         $done->{$var} = 1;
402     }
403     return (undef, $contents);
404 }
405
406 #
407 # Return the mtime of the config file
408 #
409 sub ConfigMTime
410 {
411     my($s) = @_;
412     return (stat($s->ConfigPath()))[9];
413 }
414
415 #
416 # Returns information from the host file in $s->{ConfDir}/hosts.
417 # With no argument a ref to a hash of hosts is returned.  Each
418 # hash contains fields as specified in the hosts file.  With an
419 # argument a ref to a single hash is returned with information
420 # for just that host.
421 #
422 sub HostInfoRead
423 {
424     my($s, $host) = @_;
425     my(%hosts, @hdr, @fld);
426     local(*HOST_INFO, *LOCK);
427
428     flock(LOCK, LOCK_EX) if open(LOCK, "$s->{ConfDir}/LOCK");
429     if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
430         print(STDERR "Can't open $s->{ConfDir}/hosts\n");
431         close(LOCK);
432         return {};
433     }
434     binmode(HOST_INFO);
435     while ( <HOST_INFO> ) {
436         s/[\n\r]+//;
437         s/#.*//;
438         s/\s+$//;
439         next if ( /^\s*$/ || !/^([\w\.\\-]+\s+.*)/ );
440         #
441         # Split on white space, except if preceded by \
442         # using zero-width negative look-behind assertion
443         # (always wanted to use one of those).
444         #
445         @fld = split(/(?<!\\)\s+/, $1);
446         #
447         # Remove any \
448         #
449         foreach ( @fld ) {
450             s{\\(\s)}{$1}g;
451         }
452         if ( @hdr ) {
453             if ( defined($host) ) {
454                 next if ( lc($fld[0]) ne lc($host) );
455                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
456                 close(HOST_INFO);
457                 close(LOCK);
458                 return \%hosts;
459             } else {
460                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
461             }
462         } else {
463             @hdr = @fld;
464         }
465     }
466     close(HOST_INFO);
467     close(LOCK);
468     return \%hosts;
469 }
470
471 #
472 # Writes new hosts information to the hosts file in $s->{ConfDir}/hosts.
473 # With no argument a ref to a hash of hosts is returned.  Each
474 # hash contains fields as specified in the hosts file.  With an
475 # argument a ref to a single hash is returned with information
476 # for just that host.
477 #
478 sub HostInfoWrite
479 {
480     my($s, $hosts) = @_;
481     my($gotHdr, @fld, $hostText, $contents);
482     local(*HOST_INFO);
483
484     if ( !open(HOST_INFO, "$s->{ConfDir}/hosts") ) {
485         return "Can't open $s->{ConfDir}/hosts";
486     }
487     foreach my $host ( keys(%$hosts) ) {
488         my $name = "$hosts->{$host}{host}";
489         my $rest = "\t$hosts->{$host}{dhcp}"
490                  . "\t$hosts->{$host}{user}"
491                  . "\t$hosts->{$host}{moreUsers}";
492         $name =~ s/ /\\ /g;
493         $rest =~ s/ //g;
494         $hostText->{$host} = $name . $rest;
495     }
496     binmode(HOST_INFO);
497     while ( <HOST_INFO> ) {
498         s/[\n\r]+//;
499         if ( /^\s*$/ || /^\s*#/ ) {
500             $contents .= $_ . "\n";
501             next;
502         }
503         if ( !$gotHdr ) {
504             $contents .= $_ . "\n";
505             $gotHdr = 1;
506             next;
507         }
508         @fld = split(/(?<!\\)\s+/, $1);
509         #
510         # Remove any \
511         #
512         foreach ( @fld ) {
513             s{\\(\s)}{$1}g;
514         }
515         if ( defined($hostText->{$fld[0]}) ) {
516             $contents .= $hostText->{$fld[0]} . "\n";
517             delete($hostText->{$fld[0]});
518         }
519     }
520     foreach my $host ( sort(keys(%$hostText)) ) {
521         $contents .= $hostText->{$host} . "\n";
522         delete($hostText->{$host});
523     }
524     close(HOST_INFO);
525
526     #
527     # Write and verify the new host file
528     #
529     return $s->TextFileWrite("$s->{ConfDir}/hosts", $contents);
530 }
531
532 #
533 # Return the mtime of the hosts file
534 #
535 sub HostsMTime
536 {
537     my($s) = @_;
538     return (stat("$s->{ConfDir}/hosts"))[9];
539 }
540
541 1;