Updates in preparation for 3.0.0beta2:
[BackupPC.git] / lib / BackupPC / Lib.pm
index 251f909..86a3b3f 100644 (file)
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0alpha, released 23 Jan 2006.
+# Version 3.0.0beta2, released 11 Nov 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -76,27 +76,25 @@ sub new
     #
     if ( $useFHS ) {
         $paths = {
-            useFHS  => $useFHS,
-            TopDir  => $topDir,
-            BinDir  => "$installDir/bin",
-            LibDir  => "$installDir/lib",
-            ConfDir => $confDir eq "" ? '/etc/BackupPC' : $confDir,
-            LogDir  => '/var/log/BackupPC',
+            useFHS     => $useFHS,
+            TopDir     => $topDir,
+            InstallDir => $installDir,
+            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",
+            useFHS     => $useFHS,
+            TopDir     => $topDir,
+            InstallDir => $installDir,
+            ConfDir    => $confDir eq "" ? "$topDir/conf" : $confDir,
+            LogDir     => "$topDir/log",
         };
     }
 
     my $bpc = bless {
        %$paths,
-        Version => '3.0.0alpha',
+        Version => '3.0.0beta2',
     }, $class;
 
     $bpc->{storage} = BackupPC::Storage->new($paths);
@@ -115,8 +113,8 @@ sub new
     #
     # Update the paths based on the config file
     #
-    foreach my $dir ( qw(TopDir BinDir LibDir ConfDir LogDir) ) {
-        next if ( !defined($bpc->{Conf}{$dir}) );
+    foreach my $dir ( qw(TopDir ConfDir InstallDir LogDir) ) {
+        next if ( $bpc->{Conf}{$dir} eq "" );
         $paths->{$dir} = $bpc->{$dir} = $bpc->{Conf}{$dir};
     }
     $bpc->{storage}->setPaths($paths);
@@ -127,8 +125,9 @@ sub new
     if ( !$noUserCheck
            && $bpc->{Conf}{BackupPCUserVerify}
            && $> != (my $uid = (getpwnam($bpc->{Conf}{BackupPCUser}))[2]) ) {
-       print(STDERR "Wrong user: my userid is $>, instead of $uid"
+       print(STDERR "$0: Wrong user: my userid is $>, instead of $uid"
            . " ($bpc->{Conf}{BackupPCUser})\n");
+       print(STDERR "Please su $bpc->{Conf}{BackupPCUser} first\n");
        return;
     }
     return $bpc;
@@ -143,7 +142,7 @@ sub TopDir
 sub BinDir
 {
     my($bpc) = @_;
-    return $bpc->{BinDir};
+    return "$bpc->{InstallDir}/bin";
 }
 
 sub LogDir
@@ -161,7 +160,13 @@ sub ConfDir
 sub LibDir
 {
     my($bpc) = @_;
-    return $bpc->{LibDir};
+    return "$bpc->{InstallDir}/lib";
+}
+
+sub InstallDir
+{
+    my($bpc) = @_;
+    return $bpc->{InstallDir};
 }
 
 sub useFHS
@@ -347,7 +352,7 @@ sub ConfigRead
     # Load language file
     #
     return "No language setting" if ( !defined($bpc->{Conf}{Language}) );
-    my $langFile = "$bpc->{LibDir}/BackupPC/Lang/$bpc->{Conf}{Language}.pm";
+    my $langFile = "$bpc->{InstallDir}/lib/BackupPC/Lang/$bpc->{Conf}{Language}.pm";
     if ( !defined($ret = do $langFile) && ($! || $@) ) {
        $mesg = "Couldn't open language file $langFile: $!" if ( $! );
        $mesg = "Couldn't execute language file $langFile: $@" if ( $@ );
@@ -1103,6 +1108,7 @@ sub cmdSystemOrEvalLong
     my($pid, $out, $allOut);
     local(*CHILD);
     
+    $? = 0;
     if ( (ref($cmd) eq "ARRAY" ? $cmd->[0] : $cmd) =~ /^\&/ ) {
         $cmd = join(" ", $cmd) if ( ref($cmd) eq "ARRAY" );
        print(STDERR "cmdSystemOrEval: about to eval perl code $cmd\n")
@@ -1214,4 +1220,62 @@ sub backupFileConfFix
     }
 }
 
+#
+# This is sort() compare function, used below.
+#
+# New client LOG names are LOG.MMYYYY.  Old style names are
+# LOG, LOG.0, LOG.1 etc.  Sort them so new names are
+# first, and newest to oldest.
+#
+sub compareLOGName
+{
+    my $na = $1 if ( $a =~ /LOG\.(\d+)(\.z)?$/ );
+    my $nb = $1 if ( $b =~ /LOG\.(\d+)(\.z)?$/ );
+
+    $na = -1 if ( !defined($na) );
+    $nb = -1 if ( !defined($nb) );
+
+    if ( length($na) >= 5 && length($nb) >= 5 ) {
+        #
+        # Both new style.  Bigger numbers are more recent.
+        #
+        return $nb - $na;
+    } elsif ( length($na) >= 5 && length($nb) < 5 ) {
+        return -1;
+    } elsif ( length($na) < 5 && length($nb) >= 5 ) {
+        return 1;
+    } else {
+        #
+        # Both old style.  Smaller numbers are more recent.
+        #
+        return $na - $nb;
+    }
+}
+
+#
+# Returns list of paths to a clients's (or main) LOG files,
+# most recent first.
+#
+sub sortedPCLogFiles
+{
+    my($bpc, $host) = @_;
+
+    my(@files, $dir);
+
+    if ( $host ne "" ) {
+        $dir = "$bpc->{TopDir}/pc/$host";
+    } else {
+        $dir = "$bpc->{LogDir}";
+    }
+    if ( opendir(DIR, $dir) ) {
+        foreach my $file ( readdir(DIR) ) {
+            next if ( !-f "$dir/$file" );
+            next if ( $file ne "LOG" && $file !~ /^LOG\.\d/ );
+            push(@files, "$dir/$file");
+        }
+        closedir(DIR);
+    }
+    return sort(compareLOGName @files);
+}
+
 1;