re-implement only_increment by comparing inodes
[BackupPC.git] / lib / BackupPC / View.pm
index 64d5abe..cfebe69 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
 {
@@ -73,7 +76,7 @@ sub dirCache
     my($m, $backupNum, $share, $dir) = @_;
     my($i, $level);
 
-    #print STDERR "dirCache($backupNum, $share, $dir)\n";
+    print STDERR "dirCache($backupNum, $share, $dir)\n";
     $dir = "/$dir" if ( $dir !~ m{^/} );
     $dir =~ s{/+$}{};
     return if ( $m->{num} == $backupNum
@@ -92,6 +95,9 @@ sub dirCache
     $m->{share} = $share;
     $m->{dir} = $dir;
 
+    my $previous_increment_entry;
+    my $increment_backupNum = $backupNum; # used in loop
+
     #
     # merge backups, starting at the requested one, and working
     # backwards until we get to level 0.
@@ -102,14 +108,16 @@ sub dirCache
        #
        # skip backups with the same or higher level
        #
-       next if ( $m->{backups}[$i]{level} >= $level );
+       #next if ( $m->{backups}[$i]{level} >= $level );
+       # FIXME ASA skip all but first?
 
        $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)
@@ -118,9 +126,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} ) {
                 #
@@ -133,9 +151,9 @@ sub dirCache
         }
         my $attr;
        if ( $mangle ) {
-           $attr = BackupPC::Attrib->new({ compress => $compress });
+           $attr = BackupPC::Attrib->new($attribOpts);
            if ( !$attr->read($path) ) {
-                $m->{error} = "Can't read attribute file in $path";
+                $m->{error} = "Can't read attribute file in $path: " . $attr->errStr();
                $attr = undef;
            }
        }
@@ -147,11 +165,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;
                 #
@@ -166,7 +205,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,
@@ -180,10 +224,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 ) {
@@ -198,6 +242,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}
@@ -205,6 +251,7 @@ sub dirCache
            $m->{files}{$fileUM}{inode}       = $entry->{inode}
                                                     if ( $m->{dirOpts}{inode} );
         }
+
        #
        # Also include deleted files
        #
@@ -218,6 +265,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;
@@ -225,6 +274,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
     #
@@ -378,6 +437,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)
@@ -388,7 +448,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.
@@ -397,7 +465,7 @@ sub dirHistory
         }
         my $attr;
        if ( $mangle ) {
-           $attr = BackupPC::Attrib->new({ compress => $compress });
+           $attr = BackupPC::Attrib->new($attribOpts);
            if ( !$attr->read($path) ) {
                 $m->{error} = "Can't read attribute file in $path";
                $attr = undef;
@@ -415,7 +483,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);
@@ -437,7 +509,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";