X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FView.pm;h=cf03983a67fab1864f4edfd1d5c1bf8ede5a6676;hp=357c9e33bb2b60a815d10eae4bf91a1270d2acdf;hb=3f3d4f4adbd990e15969d9cbc5e99e89e613e502;hpb=329e870f56fb6572fa697998d33676588034c149 diff --git a/lib/BackupPC/View.pm b/lib/BackupPC/View.pm index 357c9e3..cf03983 100644 --- a/lib/BackupPC/View.pm +++ b/lib/BackupPC/View.pm @@ -31,7 +31,7 @@ # #======================================================================== # -# Version 2.1.0_CVS, released 3 Jul 2003. +# Version 3.0.0, released 28 Jan 2007. # # See http://backuppc.sourceforge.net. # @@ -49,20 +49,24 @@ use Data::Dumper; sub new { - my($class, $bpc, $host, $backups) = @_; + my($class, $bpc, $host, $backups, $options) = @_; my $m = bless { - bpc => $bpc, # BackupPC::Lib object - host => $host, # host name - backups => $backups, # all backups for this host - num => -1, # backup number - idx => -1, # index into backups for backup - # we are viewing - dirPath => undef, # path to current directory - dirAttr => undef, # attributes of current directory + bpc => $bpc, # BackupPC::Lib object + host => $host, # host name + backups => $backups, # all backups for this host + num => -1, # backup number + idx => -1, # index into backups for backup + # we are viewing + dirPath => undef, # path to current directory + dirAttr => undef, # attributes of current directory + dirOpts => $options, # $options is a hash of file attributes we need: + # type, inode, or nlink. If set, these parameters + # 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 "full" ? 0 : 1; + $m->{backups}[$i]{level} = $m->{backups}[$i]{type} eq "incr" ? 1 : 0; } $m->{topDir} = $m->{bpc}->TopDir(); return $m; @@ -78,18 +82,11 @@ sub dirCache $dir =~ s{/+$}{}; return if ( $m->{num} == $backupNum && $m->{share} eq $share + && defined($m->{dir}) && $m->{dir} eq $dir ); - if ( $m->{num} != $backupNum ) { - for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) { - last if ( $m->{backups}[$i]{num} == $backupNum ); - } - if ( $i >= @{$m->{backups}} ) { - $m->{idx} = -1; - return; - } - $m->{num} = $backupNum; - $m->{idx} = $i; - } + $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum ); + return if ( $m->{idx} < 0 ); + $m->{files} = {}; $level = $m->{backups}[$m->{idx}]{level} + 1; @@ -126,7 +123,9 @@ sub dirCache } $path .= $sharePathM; #print(STDERR "Opening $path (share=$share)\n"); - if ( !opendir(DIR, $path) ) { + + my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts}); + if ( !defined($dirInfo) ) { if ( $i == $m->{idx} ) { # # Oops, directory doesn't exist. @@ -136,8 +135,6 @@ sub dirCache } next; } - my @dir = readdir(DIR); - closedir(DIR); my $attr; if ( $mangle ) { $attr = BackupPC::Attrib->new({ compress => $compress }); @@ -146,8 +143,8 @@ sub dirCache $attr = undef; } } - foreach my $file ( @dir ) { - $file = $1 if ( $file =~ /(.*)/ ); + foreach my $entry ( @$dirInfo ) { + my $file = $1 if ( $entry->{name} =~ /(.*)/s ); my $fileUM = $file; $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle ); #print(STDERR "Doing $fileUM\n"); @@ -157,15 +154,15 @@ sub dirCache next if ( defined($m->{files}{$fileUM}) || $file eq ".." || $file eq "." + || $file eq "backupInfo" || $mangle && $file eq "attrib" ); - # - # skip directories in earlier backups (each backup always - # has the complete directory tree). - # - my @s = stat("$path/$file"); - next if ( $i < $m->{idx} && -d _ ); if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) { $m->{files}{$fileUM} = $a; + # + # skip directories in earlier backups (each backup always + # has the complete directory tree). + # + next if ( $i < $m->{idx} && $a->{type} == BPC_FTYPE_DIR ); $attr->set($fileUM, undef); } else { # @@ -173,6 +170,8 @@ sub dirCache # is on. We have to stat the file and read compressed files # to determine their size. # + my @s = stat("$path/$file"); + next if ( $i < $m->{idx} && -d _ ); $m->{files}{$fileUM} = { type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE, mode => $s[2], @@ -199,13 +198,16 @@ sub dirCache } } } - $m->{files}{$fileUM}{relPath} = "$dir/$fileUM"; - $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file"; - $m->{files}{$fileUM}{fullPath} = "$path/$file"; - $m->{files}{$fileUM}{backupNum} = $backupNum; - $m->{files}{$fileUM}{compress} = $compress; - $m->{files}{$fileUM}{nlink} = $s[3]; - $m->{files}{$fileUM}{inode} = $s[1]; + ($m->{files}{$fileUM}{relPath} = "$dir/$fileUM") =~ s{//+}{/}g; + ($m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file") + =~ s{//+}{/}g; + ($m->{files}{$fileUM}{fullPath} = "$path/$file") =~ s{//+}{/}g; + $m->{files}{$fileUM}{backupNum} = $backupNum; + $m->{files}{$fileUM}{compress} = $compress; + $m->{files}{$fileUM}{nlink} = $entry->{nlink} + if ( $m->{dirOpts}{nlink} ); + $m->{files}{$fileUM}{inode} = $entry->{inode} + if ( $m->{dirOpts}{inode} ); } # # Also include deleted files @@ -237,6 +239,55 @@ sub dirCache #print STDERR "Returning:\n", Dumper($m->{files}); } +# +# Return list of shares for this backup +# +sub shareList +{ + my($m, $backupNum) = @_; + my @shareList; + + $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum ); + return if ( $m->{idx} < 0 ); + + my $mangle = $m->{backups}[$m->{idx}]{mangle}; + my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/"; + return if ( !opendir(DIR, $path) ); + my @dir = readdir(DIR); + closedir(DIR); + foreach my $file ( @dir ) { + $file = $1 if ( $file =~ /(.*)/s ); + next if ( $file eq "attrib" && $mangle + || $file eq "." + || $file eq ".." + || $file eq "backupInfo" + ); + my $fileUM = $file; + $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle ); + push(@shareList, $fileUM); + } + $m->{dir} = undef; + return @shareList; +} + +sub backupNumCache +{ + my($m, $backupNum) = @_; + + if ( $m->{num} != $backupNum ) { + my $i; + for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) { + last if ( $m->{backups}[$i]{num} == $backupNum ); + } + if ( $i >= @{$m->{backups}} ) { + $m->{idx} = -1; + return; + } + $m->{num} = $backupNum; + $m->{idx} = $i; + } +} + # # Return the attributes of a specific file # @@ -244,16 +295,18 @@ sub fileAttrib { my($m, $backupNum, $share, $path) = @_; - if ( $path =~ s{(.*)/+(.+)}{$1} ) { + #print(STDERR "fileAttrib($backupNum, $share, $path)\n"); + if ( $path =~ s{(.*)/+(.+)}{$1}s ) { my $file = $2; $m->dirCache($backupNum, $share, $path); return $m->{files}{$file}; } else { #print STDERR "Got empty $path\n"; $m->dirCache($backupNum, "", ""); - my %attr = %{$m->{files}{$share}}; - $attr{relPath} = "/"; - return \%attr; + my $attr = $m->{files}{$share}; + return if ( !defined($attr) ); + $attr->{relPath} = "/"; + return $attr; } } @@ -268,6 +321,9 @@ sub dirAttrib return $m->{files}; } +# +# Return a listref of backup numbers that are merged to create this view +# sub mergeNums { my($m) = @_; @@ -275,6 +331,9 @@ sub mergeNums return $m->{mergeNums}; } +# +# Return a list of backup indexes for which the directory exists +# sub backupList { my($m, $share, $dir) = @_; @@ -296,7 +355,7 @@ sub backupList } $path .= $sharePathM; next if ( !-d $path ); - push(@backupList, $backupNum); + push(@backupList, $i); } return @backupList; } @@ -332,14 +391,14 @@ sub dirHistory } $path .= $sharePathM; #print(STDERR "Opening $path (share=$share)\n"); - if ( !opendir(DIR, $path) ) { + + my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts}); + if ( !defined($dirInfo) ) { # # Oops, directory doesn't exist. # next; } - my @dir = readdir(DIR); - closedir(DIR); my $attr; if ( $mangle ) { $attr = BackupPC::Attrib->new({ compress => $compress }); @@ -348,8 +407,8 @@ sub dirHistory $attr = undef; } } - foreach my $file ( @dir ) { - $file = $1 if ( $file =~ /(.*)/ ); + foreach my $entry ( @$dirInfo ) { + my $file = $1 if ( $entry->{name} =~ /(.*)/s ); my $fileUM = $file; $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle ); #print(STDERR "Doing $fileUM\n"); @@ -396,15 +455,29 @@ sub dirHistory } } } - $files->{$fileUM}[$i]{relPath} = "$dir/$fileUM"; - $files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file"; - $files->{$fileUM}[$i]{fullPath} = "$path/$file"; - $files->{$fileUM}[$i]{backupNum} = $backupNum; - $files->{$fileUM}[$i]{compress} = $compress; - $files->{$fileUM}[$i]{nlink} = $s[3]; - $files->{$fileUM}[$i]{inode} = $s[1]; + ($files->{$fileUM}[$i]{relPath} = "$dir/$fileUM") =~ s{//+}{/}g; + ($files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file") + =~ s{//+}{/}g; + ($files->{$fileUM}[$i]{fullPath} = "$path/$file") =~ s{//+}{/}g; + $files->{$fileUM}[$i]{backupNum} = $backupNum; + $files->{$fileUM}[$i]{compress} = $compress; + $files->{$fileUM}[$i]{nlink} = $entry->{nlink} + if ( $m->{dirOpts}{nlink} ); + $files->{$fileUM}[$i]{inode} = $entry->{inode} + if ( $m->{dirOpts}{inode} ); } + # + # Flag deleted files + # + if ( defined($attr) ) { + my $a = $attr->get; + foreach my $fileUM ( keys(%$a) ) { + next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED ); + $files->{$fileUM}[$i]{type} = BPC_FTYPE_DELETED; + } + } + # # Merge old backups. Don't merge directories from old # backups because every backup has an accurate directory @@ -420,18 +493,19 @@ sub dirHistory $files->{$fileUM}[$i] = $files->{$fileUM}[$k]; } } + } - # - # Finally, remove deleted files - # - if ( defined($attr) ) { - my $a = $attr->get; - foreach my $fileUM ( keys(%$a) ) { - next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED ); - $files->{$fileUM}[$i] = undef; - } - } + # + # Remove deleted files + # + for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) { + foreach my $fileUM ( keys(%$files) ) { + next if ( !defined($files->{$fileUM}[$i]) + || $files->{$fileUM}[$i]{type} != BPC_FTYPE_DELETED ); + $files->{$fileUM}[$i] = undef; + } } + #print STDERR "Returning:\n", Dumper($files); return $files; } @@ -448,6 +522,7 @@ sub find { my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_; + #print(STDERR "find: got $backupNum, $share, $path\n"); # # First call the callback on the given $path #