X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FView.pm;h=e050544941a42fe58db02e7cfb9d003a35719fb0;hp=f0089681db1acc6f240934bcf24343cd310ff014;hb=5b3e6091d542c2e7445d5dd511cdf6e20aec8b8d;hpb=8d7768574bb328c669a44c6efbff01e8a7e93d7e diff --git a/lib/BackupPC/View.pm b/lib/BackupPC/View.pm index f008968..e050544 100644 --- a/lib/BackupPC/View.pm +++ b/lib/BackupPC/View.pm @@ -13,7 +13,7 @@ # Craig Barratt # # COPYRIGHT -# Copyright (C) 2002 Craig Barratt +# Copyright (C) 2002-2003 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 2.0.0, released 14 Jun 2003. +# Version 3.0.0alpha, released 23 Jan 2006. # # See http://backuppc.sourceforge.net. # @@ -62,7 +62,7 @@ sub new }, $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 +78,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; @@ -157,6 +150,7 @@ 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 @@ -199,13 +193,14 @@ 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} = $s[3]; + $m->{files}{$fileUM}{inode} = $s[1]; } # # Also include deleted files @@ -237,6 +232,53 @@ 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 =~ /(.*)/ ); + next if ( $file eq "attrib" && $mangle + || $file eq "." + || $file eq ".." ); + 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,6 +286,7 @@ sub fileAttrib { my($m, $backupNum, $share, $path) = @_; + #print(STDERR "fileAttrib($backupNum, $share, $path)\n"); if ( $path =~ s{(.*)/+(.+)}{$1} ) { my $file = $2; $m->dirCache($backupNum, $share, $path); @@ -251,9 +294,10 @@ sub fileAttrib } 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 +312,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 +322,9 @@ sub mergeNums return $m->{mergeNums}; } +# +# Return a list of backup indexes for which the directory exists +# sub backupList { my($m, $share, $dir) = @_; @@ -296,11 +346,148 @@ sub backupList } $path .= $sharePathM; next if ( !-d $path ); - push(@backupList, $backupNum); + push(@backupList, $i); } return @backupList; } +# +# Return the history of all backups for a particular directory +# +sub dirHistory +{ + my($m, $share, $dir) = @_; + my($i, $level); + my $files = {}; + + $dir = "/$dir" if ( $dir !~ m{^/} ); + $dir =~ s{/+$}{}; + + # + # merge backups, starting at the first one, and working + # forward. + # + for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) { + $level = $m->{backups}[$i]{level}; + my $backupNum = $m->{backups}[$i]{num}; + my $mangle = $m->{backups}[$i]{mangle}; + my $compress = $m->{backups}[$i]{compress}; + my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/"; + my $sharePathM; + if ( $mangle ) { + $sharePathM = $m->{bpc}->fileNameEltMangle($share) + . $m->{bpc}->fileNameMangle($dir); + } else { + $sharePathM = $share . $dir; + } + $path .= $sharePathM; + #print(STDERR "Opening $path (share=$share)\n"); + if ( !opendir(DIR, $path) ) { + # + # Oops, directory doesn't exist. + # + next; + } + my @dir = readdir(DIR); + closedir(DIR); + 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 = undef; + } + } + foreach my $file ( @dir ) { + $file = $1 if ( $file =~ /(.*)/ ); + my $fileUM = $file; + $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle ); + #print(STDERR "Doing $fileUM\n"); + # + # skip special files + # + next if ( $file eq ".." + || $file eq "." + || $mangle && $file eq "attrib" + || defined($files->{$fileUM}[$i]) ); + my @s = stat("$path/$file"); + if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) { + $files->{$fileUM}[$i] = $a; + $attr->set($fileUM, undef); + } else { + # + # Very expensive in the non-attribute case when compresseion + # is on. We have to stat the file and read compressed files + # to determine their size. + # + $files->{$fileUM}[$i] = { + type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE, + mode => $s[2], + uid => $s[4], + gid => $s[5], + size => -f _ ? $s[7] : 0, + mtime => $s[9], + }; + if ( $compress && -f _ ) { + # + # Compute the correct size by reading the whole file + # + my $f = BackupPC::FileZIO->open("$path/$file", + 0, $compress); + if ( !defined($f) ) { + $m->{error} = "Can't open $path/$file"; + } else { + my($data, $size); + while ( $f->read(\$data, 65636 * 8) > 0 ) { + $size += length($data); + } + $f->close; + $files->{$fileUM}[$i]{size} = $size; + } + } + } + ($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} = $s[3]; + $files->{$fileUM}[$i]{inode} = $s[1]; + } + + # + # Merge old backups. Don't merge directories from old + # backups because every backup has an accurate directory + # tree. + # + for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) { + next if ( $m->{backups}[$k]{level} >= $level ); + $level = $m->{backups}[$k]{level}; + foreach my $fileUM ( keys(%$files) ) { + next if ( !defined($files->{$fileUM}[$k]) + || defined($files->{$fileUM}[$i]) + || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR ); + $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 if ( defined($files->{$fileUM}) ); + } + } + } + #print STDERR "Returning:\n", Dumper($files); + return $files; +} + + # # Do a recursive find starting at the given path (either a file # or directory). The callback function $callback is called on each @@ -312,6 +499,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 #