* Changes in preparation for 3.2.0beta1...
[BackupPC.git] / lib / BackupPC / Storage / Text.pm
index 4213106..0f22932 100644 (file)
@@ -12,7 +12,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2004  Craig Barratt
+#   Copyright (C) 2004-2009  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -30,7 +30,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0alpha, released 23 Jan 2006.
+# Version 3.2.0beta0, released 5 April 2009.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -84,6 +84,18 @@ sub BackupInfoRead
         close(BK_INFO);
     }
     close(LOCK);
+    #
+    # Default the version field.  Prior to 3.0.0 the xferMethod
+    # field is empty, so we use that to figure out the version.
+    #
+    for ( my $i = 0 ; $i < @Backups ; $i++ ) {
+        next if ( $Backups[$i]{version} ne "" );
+        if ( $Backups[$i]{xferMethod} eq "" ) {
+            $Backups[$i]{version} = "2.1.2";
+        } else {
+            $Backups[$i]{version} = "3.0.0";
+        }
+    }
     return @Backups;
 }
 
@@ -202,7 +214,10 @@ sub TextFileWrite
 
     (my $dir = $file) =~ s{(.+)/(.+)}{$1};
 
-    mkpath($dir, 0, 0775) if ( !-d $dir );
+    if ( !-d $dir ) {
+        eval { mkpath($dir, 0, 0775) };
+        return "TextFileWrite: can't create directory $dir" if ( $@ );
+    }
     if ( open(FD, ">", "$file.new") ) {
        binmode(FD);
         print FD $contents;
@@ -247,36 +262,39 @@ sub ConfigPath
 
     return "$s->{ConfDir}/config.pl" if ( !defined($host) );
     if ( $s->{useFHS} ) {
-        return "$s->{ConfDir}/host/$host.pl";
+        return "$s->{ConfDir}/pc/$host.pl";
     } else {
+        return "$s->{TopDir}/pc/$host/config.pl"
+            if ( -f "$s->{TopDir}/pc/$host/config.pl" );
         return "$s->{ConfDir}/$host.pl"
             if ( $host ne "config" && -f "$s->{ConfDir}/$host.pl" );
-        return "$s->{TopDir}/pc/$host/config.pl";
+        return "$s->{ConfDir}/pc/$host.pl";
     }
 }
 
 sub ConfigDataRead
 {
-    my($s, $host) = @_;
+    my($s, $host, $prevConfig) = @_;
     my($ret, $mesg, $config, @configs);
 
     #
     # TODO: add lock
     #
-    my $conf = {};
+    my $conf = $prevConfig || {};
     my $configPath = $s->ConfigPath($host);
 
     push(@configs, $configPath) if ( -f $configPath );
     foreach $config ( @configs ) {
-        %Conf = ();
+        %Conf = %$conf;
         if ( !defined($ret = do $config) && ($! || $@) ) {
             $mesg = "Couldn't open $config: $!" if ( $! );
             $mesg = "Couldn't execute $config: $@" if ( $@ );
             $mesg =~ s/[\n\r]+//;
             return ($mesg, $conf);
         }
-        %$conf = ( %$conf, %Conf );
+        %$conf = %Conf;
     }
+
     #
     # Promote BackupFilesOnly and BackupFilesExclude to hashes
     #
@@ -287,6 +305,23 @@ sub ConfigDataRead
         $conf->{$param} = { "*" => $conf->{$param} };
     }
 
+    #
+    # Handle backward compatibility with defunct BlackoutHourBegin,
+    # BlackoutHourEnd, and BlackoutWeekDays parameters.
+    #
+    if ( defined($conf->{BlackoutHourBegin}) ) {
+        push(@{$conf->{BlackoutPeriods}},
+             {
+                 hourBegin => $conf->{BlackoutHourBegin},
+                 hourEnd   => $conf->{BlackoutHourEnd},
+                 weekDays  => $conf->{BlackoutWeekDays},
+             }
+        );
+        delete($conf->{BlackoutHourBegin});
+        delete($conf->{BlackoutHourEnd});
+        delete($conf->{BlackoutWeekDays});
+    }
+
     return (undef, $conf);
 }
 
@@ -311,10 +346,7 @@ sub ConfigFileMerge
 {
     my($s, $inFile, $newConf) = @_;
     local(*C);
-    my($contents, $out);
-    my $comment = 1;
-    my $skipVar = 0;
-    my $endLine = undef;
+    my($contents, $skipExpr, $fakeVar);
     my $done = {};
 
     if ( -f $inFile ) {
@@ -326,12 +358,10 @@ sub ConfigFileMerge
         binmode(C);
 
         while ( <C> ) {
-            if ( $comment && /^\s*#/ ) {
-                $out .= $_;
-            } elsif ( /^\s*\$Conf\{([^}]*)\}\s*=/ ) {
+            if ( /^\s*\$Conf\{([^}]*)\}\s*=(.*)/ ) {
                 my $var = $1;
-                if ( exists($newConf->{$var}) ) { 
-                    $contents .= $out;
+                $skipExpr = "\$fakeVar = $2\n";
+                if ( exists($newConf->{$var}) ) {
                     my $d = Data::Dumper->new([$newConf->{$var}], [*value]);
                     $d->Indent(1);
                     $d->Terse(1);
@@ -340,31 +370,24 @@ sub ConfigFileMerge
                     $contents .= "\$Conf{$var} = " . $value;
                     $done->{$var} = 1;
                 }
-                if ( /^\s*\$Conf\{[^}]*} *= *<<(.*);/
-                        || /^\s*\$Conf\{[^}]*} *= *<<'(.*)';/ ) {
-                    $endLine = $1;
-                    $skipVar = 1;
-                } else {
-                    $endLine = undef;
-                    $skipVar = /^[^#]*;/ ? 0 : 1;
-                }
-                $out = "";
-            } elsif ( $skipVar ) {
-                if ( !defined($endLine) && /^[^#]*;/ ) {
-                    $skipVar = 0;
-                    $comment = 1;
-                }
-                if ( defined($endLine) && /^\Q$endLine\E[\n\r]*$/ ) {
-                    $endLine = undef;
-                    $skipVar = 0;
-                    $comment = 1;
-                }
+            } elsif ( defined($skipExpr) ) {
+                $skipExpr .= $_;
             } else {
-                $out .= $_;
+                $contents .= $_;
+            }
+            if ( defined($skipExpr)
+                    && ($skipExpr =~ /^\$fakeVar = *<</
+                        || $skipExpr =~ /;[\n\r]*$/) ) {
+                #
+                # if we have a complete expression, then we are done
+                # skipping text from the original config file.
+                #
+                $skipExpr = $1 if ( $skipExpr =~ /(.*)/s );
+                eval($skipExpr);
+                $skipExpr = undef if ( $@ eq "" );
             }
         }
         close(C);
-        $contents .= $out;
     }
 
     #
@@ -432,12 +455,12 @@ sub HostInfoRead
         if ( @hdr ) {
             if ( defined($host) ) {
                 next if ( lc($fld[0]) ne lc($host) );
-                @{$hosts{$fld[0]}}{@hdr} = @fld;
+                @{$hosts{lc($fld[0])}}{@hdr} = @fld;
                close(HOST_INFO);
                 close(LOCK);
                 return \%hosts;
             } else {
-                @{$hosts{$fld[0]}}{@hdr} = @fld;
+                @{$hosts{lc($fld[0])}}{@hdr} = @fld;
             }
         } else {
             @hdr = @fld;