* BackupPC_Admin now uses $Conf{UmaskMode}, so config.pl files
[BackupPC.git] / lib / BackupPC / Lib.pm
index 48c4766..faa66b1 100644 (file)
@@ -29,7 +29,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0alpha, released 23 Jan 2006.
+# Version 3.0.0beta3, released 3 Dec 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -94,7 +94,7 @@ sub new
 
     my $bpc = bless {
        %$paths,
-        Version => '3.0.0alpha',
+        Version => '3.0.0beta3',
     }, $class;
 
     $bpc->{storage} = BackupPC::Storage->new($paths);
@@ -125,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;
@@ -1107,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")
@@ -1218,4 +1220,65 @@ 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: format is MMYYYY.  Bigger dates are
+        # more recent.
+        #
+        my $ma = $2 * 12 + $1 if ( $na =~ /(\d+)(\d{4})/ );
+        my $mb = $2 * 12 + $1 if ( $nb =~ /(\d+)(\d{4})/ );
+        return $mb - $ma;
+    } 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;