- fixed configure.pl and makeDist.
[BackupPC.git] / lib / BackupPC / Lib.pm
index 0b03282..251f909 100644 (file)
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
 #
 #========================================================================
 #
-# Version 2.1.0, released 20 Jun 2004.
+# Version 3.0.0alpha, released 23 Jan 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -54,23 +54,53 @@ use Config;
 sub new
 {
     my $class = shift;
 sub new
 {
     my $class = shift;
-    my($topDir, $installDir, $noUserCheck) = @_;
+    my($topDir, $installDir, $confDir, $noUserCheck) = @_;
 
 
-    my $paths = {
-        TopDir  => $topDir || '/data/BackupPC',
-        BinDir  => $installDir || '/usr/local/BackupPC',
-        LibDir  => $installDir || '/usr/local/BackupPC',
-    };
-    $paths->{BinDir} .= "/bin";
-    $paths->{LibDir} .= "/lib";
+    #
+    # Whether to use filesystem hierarchy standard for file layout.
+    # If set, text config files are below /etc/BackupPC.
+    #
+    my $useFHS = 0;
+    my $paths;
+
+    #
+    # Set defaults for $topDir and $installDir.
+    #
+    $topDir     = '/tera0/backup/BackupPC' if ( $topDir eq "" );
+    $installDir = '/usr/local/BackupPC'    if ( $installDir eq "" );
 
 
-    $paths->{storage} = BackupPC::Storage->new($paths);
+    #
+    # Pick some initial defaults.  For FHS the only critical
+    # path is the ConfDir, since we get everything else out
+    # of the main config file.
+    #
+    if ( $useFHS ) {
+        $paths = {
+            useFHS  => $useFHS,
+            TopDir  => $topDir,
+            BinDir  => "$installDir/bin",
+            LibDir  => "$installDir/lib",
+            ConfDir => $confDir eq "" ? '/etc/BackupPC' : $confDir,
+            LogDir  => '/var/log/BackupPC',
+        };
+    } else {
+        $paths = {
+            useFHS  => $useFHS,
+            TopDir  => $topDir,
+            BinDir  => "$installDir/bin",
+            LibDir  => "$installDir/lib",
+            ConfDir => $confDir eq "" ? "$topDir/conf" : $confDir,
+            LogDir  => "$topDir/log",
+        };
+    }
 
     my $bpc = bless {
        %$paths,
 
     my $bpc = bless {
        %$paths,
-        Version => '2.1.0',
+        Version => '3.0.0alpha',
     }, $class;
 
     }, $class;
 
+    $bpc->{storage} = BackupPC::Storage->new($paths);
+
     #
     # Clean up %ENV and setup other variables.
     #
     #
     # Clean up %ENV and setup other variables.
     #
@@ -82,6 +112,15 @@ sub new
         return;
     }
 
         return;
     }
 
+    #
+    # Update the paths based on the config file
+    #
+    foreach my $dir ( qw(TopDir BinDir LibDir ConfDir LogDir) ) {
+        next if ( !defined($bpc->{Conf}{$dir}) );
+        $paths->{$dir} = $bpc->{$dir} = $bpc->{Conf}{$dir};
+    }
+    $bpc->{storage}->setPaths($paths);
+
     #
     # Verify we are running as the correct user
     #
     #
     # Verify we are running as the correct user
     #
@@ -107,6 +146,30 @@ sub BinDir
     return $bpc->{BinDir};
 }
 
     return $bpc->{BinDir};
 }
 
+sub LogDir
+{
+    my($bpc) = @_;
+    return $bpc->{LogDir};
+}
+
+sub ConfDir
+{
+    my($bpc) = @_;
+    return $bpc->{ConfDir};
+}
+
+sub LibDir
+{
+    my($bpc) = @_;
+    return $bpc->{LibDir};
+}
+
+sub useFHS
+{
+    my($bpc) = @_;
+    return $bpc->{useFHS};
+}
+
 sub Version
 {
     my($bpc) = @_;
 sub Version
 {
     my($bpc) = @_;
@@ -319,6 +382,13 @@ sub HostInfoRead
     return $bpc->{storage}->HostInfoRead($host);
 }
 
     return $bpc->{storage}->HostInfoRead($host);
 }
 
+sub HostInfoWrite
+{
+    my($bpc, $host) = @_;
+
+    return $bpc->{storage}->HostInfoWrite($host);
+}
+
 #
 # Return the mtime of the hosts file
 #
 #
 # Return the mtime of the hosts file
 #
@@ -441,7 +511,7 @@ sub ServerConnect
     #
     # First try the unix-domain socket
     #
     #
     # First try the unix-domain socket
     #
-    my $sockFile = "$bpc->{TopDir}/log/BackupPC.sock";
+    my $sockFile = "$bpc->{LogDir}/BackupPC.sock";
     socket(*FH, PF_UNIX, SOCK_STREAM, 0)     || return "unix socket: $!";
     if ( !connect(*FH, sockaddr_un($sockFile)) ) {
         my $err = "unix connect: $!";
     socket(*FH, PF_UNIX, SOCK_STREAM, 0)     || return "unix socket: $!";
     if ( !connect(*FH, sockaddr_un($sockFile)) ) {
         my $err = "unix connect: $!";
@@ -711,10 +781,12 @@ sub CheckHostAlive
                        if ( $bpc->{verbose} );
        return -1;
     }
                        if ( $bpc->{verbose} );
        return -1;
     }
-    if ( $s =~ /time=([\d\.]+)\s*ms/i ) {
+    if ( $s =~ /rtt\s*min\/avg\/max\/mdev\s*=\s*[\d.]+\/([\d.]+)\/[\d.]+\/[\d.]+\s*(ms|usec)/i ) {
+        $ret = $1;
+        $ret /= 1000 if ( lc($2) eq "usec" );
+    } elsif ( $s =~ /time=([\d.]+)\s*(ms|usec)/i ) {
        $ret = $1;
        $ret = $1;
-    } elsif ( $s =~ /time=([\d\.]+)\s*usec/i ) {
-       $ret =  $1/1000;
+        $ret /= 1000 if ( lc($2) eq "usec" );
     } else {
        print(STDERR "CheckHostAlive: can't extract round-trip time"
                   . " (not fatal)\n") if ( $bpc->{verbose} );
     } else {
        print(STDERR "CheckHostAlive: can't extract round-trip time"
                   . " (not fatal)\n") if ( $bpc->{verbose} );
@@ -1109,10 +1181,9 @@ sub cmdSystemOrEval
     return $bpc->cmdSystemOrEvalLong($cmd, $stdoutCB, 0, undef, @args);
 }
 
     return $bpc->cmdSystemOrEvalLong($cmd, $stdoutCB, 0, undef, @args);
 }
 
-
 #
 # Promotes $conf->{BackupFilesOnly}, $conf->{BackupFilesExclude}
 #
 # Promotes $conf->{BackupFilesOnly}, $conf->{BackupFilesExclude}
-# to hashes and $conf->{$shareName} to an array
+# to hashes and $conf->{$shareName} to an array.
 #
 sub backupFileConfFix
 {
 #
 sub backupFileConfFix
 {
@@ -1121,10 +1192,25 @@ sub backupFileConfFix
     $conf->{$shareName} = [ $conf->{$shareName} ]
                     if ( ref($conf->{$shareName}) ne "ARRAY" );
     foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
     $conf->{$shareName} = [ $conf->{$shareName} ]
                     if ( ref($conf->{$shareName}) ne "ARRAY" );
     foreach my $param qw(BackupFilesOnly BackupFilesExclude) {
-        next if ( !defined($conf->{$param}) || ref($conf->{$param}) eq "HASH" );
-        $conf->{$param} = [ $conf->{$param} ]
-                               if ( ref($conf->{$param}) ne "ARRAY" );
-        $conf->{$param} = { map { $_ => $conf->{$param} }                                                       @{$conf->{$shareName}} };
+        next if ( !defined($conf->{$param}) );
+        if ( ref($conf->{$param}) eq "HASH" ) {
+            #
+            # A "*" entry means wildcard - it is the default for
+            # all shares.  Replicate the "*" entry for all shares,
+            # but still allow override of specific entries.
+            #
+            next if ( !defined($conf->{$param}{"*"}) );
+            $conf->{$param} = {
+                                    map({ $_ => $conf->{$param}{"*"} }
+                                            @{$conf->{$shareName}}),
+                                    %{$conf->{$param}}
+                              };
+        } else {
+            $conf->{$param} = [ $conf->{$param} ]
+                                    if ( ref($conf->{$param}) ne "ARRAY" );
+            $conf->{$param} = { map { $_ => $conf->{$param} }
+                                    @{$conf->{$shareName}} };
+        }
     }
 }
 
     }
 }