remove debug output to STDERR
[BackupPC.git] / lib / BackupPC / View.pm
index cf03983..93d4e02 100644 (file)
@@ -13,7 +13,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2002-2003  Craig Barratt
+#   Copyright (C) 2002-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
@@ -31,7 +31,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0, released 28 Jan 2007.
+# Version 3.2.0, released 31 Jul 2010.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -46,6 +46,9 @@ use BackupPC::Lib;
 use BackupPC::Attrib qw(:all);
 use BackupPC::FileZIO;
 use Data::Dumper;
+use Encode qw/from_to/;
+
+use Data::Dump qw(dump);
 
 sub new
 {
@@ -64,10 +67,6 @@ sub new
                                 # are added to the returned hash.
                                 # See BackupPC::Lib::dirRead().
     }, $class;
-    for ( my $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
-       next if ( defined($m->{backups}[$i]{level}) );
-       $m->{backups}[$i]{level} = $m->{backups}[$i]{type} eq "incr" ? 1 : 0;
-    }
     $m->{topDir} = $m->{bpc}->TopDir();
     return $m;
 }
@@ -96,24 +95,42 @@ sub dirCache
     $m->{share} = $share;
     $m->{dir} = $dir;
 
+    my $previous_increment_entry;
+    my $increment_backupNum = $backupNum; # used in loop
+    my @visit_backup_ids;
+    my $i;
+    for ( $i = $m->{idx} ; $i >= 0 ; $i-- ) {
+
+       # we need two incremental backups to detect changes
+       next if $m->{backups}[$i]{level} >= $level && $#visit_backup_ids > 0;
+
+       push @visit_backup_ids, $i;
+
+       # for first full backup we need another full to detect changes
+       last if $m->{backups}[$i]{level} == 0 && $#visit_backup_ids > 0;
+    }
+    #warn "# visit_backup_ids = ",dump @visit_backup_ids;
+
     #
     # merge backups, starting at the requested one, and working
     # backwards until we get to level 0.
     #
     $m->{mergeNums} = [];
-    for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
+#    for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
+    for my $i ( @visit_backup_ids ) {
        #print(STDERR "Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
        #
        # skip backups with the same or higher level
        #
-       next if ( $m->{backups}[$i]{level} >= $level );
+       #next if ( $m->{backups}[$i]{level} >= $level ); # ASA - visit_backup_ids
 
        $level = $m->{backups}[$i]{level};
        $backupNum = $m->{backups}[$i]{num};
        push(@{$m->{mergeNums}}, $backupNum);
        my $mangle   = $m->{backups}[$i]{mangle};
        my $compress = $m->{backups}[$i]{compress};
-       my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
+       my $path     = "$m->{topDir}/pc/$m->{host}/$backupNum/";
+       my $legacyCharset = $m->{backups}[$i]{version} < 3.0;
         my $sharePathM;
         if ( $mangle ) {
             $sharePathM = $m->{bpc}->fileNameEltMangle($share)
@@ -122,9 +139,19 @@ sub dirCache
             $sharePathM = $share . $dir;
         }
         $path .= $sharePathM;
-       #print(STDERR "Opening $path (share=$share)\n");
+        #print(STDERR "Opening $path (share=$share, mangle=$mangle)\n");
+
+        my $dirOpts    = { %{$m->{dirOpts} || {} } };
+        $dirOpts->{inode} = 1 if $m->{dirOpts}->{only_increment}; # ASA - detect unchanged
+#warn "# dirOpts = ",dump $dirOpts;
+        my $attribOpts = { compress => $compress };
+        if ( $legacyCharset ) {
+            $dirOpts->{charsetLegacy}
+                    = $attribOpts->{charsetLegacy}
+                    = $m->{bpc}->{Conf}{ClientCharsetLegacy} || "iso-8859-1";
+        }
 
-        my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts});
+        my $dirInfo = $m->{bpc}->dirRead($path, $dirOpts);
        if ( !defined($dirInfo) ) {
             if ( $i == $m->{idx} ) {
                 #
@@ -137,9 +164,9 @@ sub dirCache
         }
         my $attr;
        if ( $mangle ) {
-           $attr = BackupPC::Attrib->new({ compress => $compress });
-           if ( -f $attr->fileName($path) && !$attr->read($path) ) {
-                $m->{error} = "Can't read attribute file in $path";
+           $attr = BackupPC::Attrib->new($attribOpts);
+           if ( !$attr->read($path) ) {
+                $m->{error} = "Can't read attribute file in $path: " . $attr->errStr();
                $attr = undef;
            }
        }
@@ -151,11 +178,32 @@ sub dirCache
            #
            # skip special files
            #
-            next if ( defined($m->{files}{$fileUM})
+            next if ( 0 # defined($m->{files}{$fileUM}) # ASA - need fallthrough
                    || $file eq ".."
                    || $file eq "."
                    || $file eq "backupInfo"
                    || $mangle && $file eq "attrib" );
+
+
+               # ASA - we only skip files when invoked with share name
+               if ( $m->{dirOpts}->{only_increment} && $share ) {
+                       if ( defined $previous_increment_entry->{$fileUM}
+                       && $previous_increment_entry->{$fileUM}->{inode} == $entry->{inode} ) {
+                               #warn "# -- $backupNum $fileUM\n";
+                               $previous_increment_entry->{$fileUM}->{_hide}++;
+                               next;
+                       } elsif ( $backupNum < $increment_backupNum ) {
+                               #warn "# << $backupNum $fileUM\n";
+                               $previous_increment_entry->{$fileUM}->{_hide}++;
+                               next;
+                       } else {
+                               #warn "# ++ $backupNum $fileUM ",dump $entry;
+                               $previous_increment_entry->{$fileUM} = $entry;
+                       }
+               } else {
+                       next if defined($m->{files}{$fileUM});
+               }
+
             if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
                 $m->{files}{$fileUM} = $a;
                 #
@@ -170,7 +218,12 @@ sub dirCache
                 # is on.  We have to stat the file and read compressed files
                 # to determine their size.
                 #
-                my @s = stat("$path/$file");
+                my $realPath = "$path/$file";
+
+                from_to($realPath, "utf8", $attribOpts->{charsetLegacy})
+                                if ( $attribOpts->{charsetLegacy} ne "" );
+
+                my @s = stat($realPath);
                 next if ( $i < $m->{idx} && -d _ );
                 $m->{files}{$fileUM} = {
                     type  => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
@@ -184,10 +237,10 @@ sub dirCache
                     #
                     # Compute the correct size by reading the whole file
                     #
-                    my $f = BackupPC::FileZIO->open("$path/$file",
+                    my $f = BackupPC::FileZIO->open($realPath,
                                                    0, $compress);
                     if ( !defined($f) ) {
-                        $m->{error} = "Can't open $path/$file";
+                        $m->{error} = "Can't open $realPath";
                     } else {
                         my($data, $size);
                         while ( $f->read(\$data, 65636 * 8) > 0 ) {
@@ -202,6 +255,8 @@ sub dirCache
             ($m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file")
                                                                =~ s{//+}{/}g;
             ($m->{files}{$fileUM}{fullPath}   = "$path/$file") =~ s{//+}{/}g;
+            from_to($m->{files}{$fileUM}{fullPath}, "utf8", $attribOpts->{charsetLegacy})
+                                if ( $attribOpts->{charsetLegacy} ne "" );
             $m->{files}{$fileUM}{backupNum}   = $backupNum;
             $m->{files}{$fileUM}{compress}    = $compress;
            $m->{files}{$fileUM}{nlink}       = $entry->{nlink}
@@ -209,6 +264,7 @@ sub dirCache
            $m->{files}{$fileUM}{inode}       = $entry->{inode}
                                                     if ( $m->{dirOpts}{inode} );
         }
+
        #
        # Also include deleted files
        #
@@ -222,6 +278,8 @@ sub dirCache
                $m->{files}{$fileUM}{relPath}    = "$dir/$fileUM";
                $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
                $m->{files}{$fileUM}{fullPath}   = "$path/$file";
+                from_to($m->{files}{$fileUM}{fullPath}, "utf8", $attribOpts->{charsetLegacy})
+                                    if ( $attribOpts->{charsetLegacy} ne "" );
                $m->{files}{$fileUM}{backupNum}  = $backupNum;
                $m->{files}{$fileUM}{compress}   = $compress;
                $m->{files}{$fileUM}{nlink}      = 0;
@@ -229,6 +287,16 @@ sub dirCache
            }
        }
     }
+
+       # ASA create increment
+       if ( $m->{dirOpts}->{only_increment} ) {
+               #warn "# previous_increment_entry = ",dump $previous_increment_entry;
+               foreach my $file ( grep { exists $previous_increment_entry->{$_}->{_hide} } keys %$previous_increment_entry ) {
+                       delete $m->{files}{$file};
+                       #warn "OLD $file\n";
+               }
+       }
+
     #
     # Prune deleted files
     #
@@ -382,6 +450,7 @@ sub dirHistory
        my $mangle    = $m->{backups}[$i]{mangle};
        my $compress  = $m->{backups}[$i]{compress};
        my $path      = "$m->{topDir}/pc/$m->{host}/$backupNum/";
+       my $legacyCharset = $m->{backups}[$i]{version} < 3.0;
         my $sharePathM;
         if ( $mangle ) {
             $sharePathM = $m->{bpc}->fileNameEltMangle($share)
@@ -392,7 +461,15 @@ sub dirHistory
         $path .= $sharePathM;
        #print(STDERR "Opening $path (share=$share)\n");
 
-        my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts});
+        my $dirOpts    = { %{$m->{dirOpts} || {} } };
+        my $attribOpts = { compress => $compress };
+        if ( $legacyCharset ) {
+            $dirOpts->{charsetLegacy}
+                    = $attribOpts->{charsetLegacy}
+                    = $m->{bpc}->{Conf}{ClientCharsetLegacy} || "iso-8859-1";
+        }
+
+        my $dirInfo = $m->{bpc}->dirRead($path, $dirOpts);
        if ( !defined($dirInfo) ) {
            #
            # Oops, directory doesn't exist.
@@ -401,8 +478,8 @@ sub dirHistory
         }
         my $attr;
        if ( $mangle ) {
-           $attr = BackupPC::Attrib->new({ compress => $compress });
-           if ( -f $attr->fileName($path) && !$attr->read($path) ) {
+           $attr = BackupPC::Attrib->new($attribOpts);
+           if ( !$attr->read($path) ) {
                 $m->{error} = "Can't read attribute file in $path";
                $attr = undef;
            }
@@ -419,7 +496,11 @@ sub dirHistory
                    || $file eq "."
                    || $mangle && $file eq "attrib"
                    || defined($files->{$fileUM}[$i]) );
-           my @s = stat("$path/$file");
+
+            my $realPath = "$path/$file";
+            from_to($realPath, "utf8", $attribOpts->{charsetLegacy})
+                            if ( $attribOpts->{charsetLegacy} ne "" );
+            my @s = stat($realPath);
             if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
                 $files->{$fileUM}[$i] = $a;
                $attr->set($fileUM, undef);
@@ -441,7 +522,7 @@ sub dirHistory
                     #
                     # Compute the correct size by reading the whole file
                     #
-                    my $f = BackupPC::FileZIO->open("$path/$file",
+                    my $f = BackupPC::FileZIO->open("$realPath",
                                                    0, $compress);
                     if ( !defined($f) ) {
                         $m->{error} = "Can't open $path/$file";