1 #============================================================= -*-perl-*-
3 # BackupPC::View package
7 # This library defines a BackupPC::View class for merging of
8 # incremental backups and file attributes. This provides the
9 # caller with a single view of a merged backup, without worrying
10 # about which backup contributes which files.
13 # Craig Barratt <cbarratt@users.sourceforge.net>
16 # Copyright (C) 2002 Craig Barratt
18 # This program is free software; you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation; either version 2 of the License, or
21 # (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #========================================================================
34 # Version 2.0.0beta3, released 1 Jun 2003.
36 # See http://backuppc.sourceforge.net.
38 #========================================================================
40 package BackupPC::View;
46 use BackupPC::Attrib qw(:all);
47 use BackupPC::FileZIO;
52 my($class, $bpc, $host, $backups) = @_;
54 bpc => $bpc, # BackupPC::Lib object
55 host => $host, # host name
56 backups => $backups, # all backups for this host
57 num => -1, # backup number
58 idx => -1, # index into backups for backup
60 dirPath => undef, # path to current directory
61 dirAttr => undef, # attributes of current directory
63 for ( my $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
64 next if ( defined($m->{backups}[$i]{level}) );
65 $m->{backups}[$i]{level} = $m->{backups}[$i]{type} eq "full" ? 0 : 1;
67 $m->{topDir} = $m->{bpc}->TopDir();
73 my($m, $backupNum, $share, $dir) = @_;
76 #print STDERR "dirCache($backupNum, $share, $dir)\n";
77 $dir = "/$dir" if ( $dir !~ m{^/} );
79 return if ( $m->{num} == $backupNum
80 && $m->{share} eq $share
81 && $m->{dir} eq $dir );
82 if ( $m->{num} != $backupNum ) {
83 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
84 last if ( $m->{backups}[$i]{num} == $backupNum );
86 if ( $i >= @{$m->{backups}} ) {
90 $m->{num} = $backupNum;
94 $level = $m->{backups}[$m->{idx}]{level} + 1;
97 # Remember the requested share and dir
103 # merge backups, starting at the requested one, and working
104 # backwards until we get to level 0.
106 $m->{mergeNums} = [];
107 for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
108 #print(STDERR "Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
110 # skip backups with the same or higher level
112 next if ( $m->{backups}[$i]{level} >= $level );
114 $level = $m->{backups}[$i]{level};
115 $backupNum = $m->{backups}[$i]{num};
116 push(@{$m->{mergeNums}}, $backupNum);
117 my $mangle = $m->{backups}[$i]{mangle};
118 my $compress = $m->{backups}[$i]{compress};
119 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
122 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
123 . $m->{bpc}->fileNameMangle($dir);
125 $sharePathM = $share . $dir;
127 $path .= $sharePathM;
128 #print(STDERR "Opening $path (share=$share)\n");
129 if ( !opendir(DIR, $path) ) {
130 if ( $i == $m->{idx} ) {
132 # Oops, directory doesn't exist.
139 my @dir = readdir(DIR);
143 $attr = BackupPC::Attrib->new({ compress => $compress });
144 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
145 $m->{error} = "Can't read attribute file in $path";
149 foreach my $file ( @dir ) {
150 $file = $1 if ( $file =~ /(.*)/ );
152 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
153 #print(STDERR "Doing $fileUM\n");
157 next if ( defined($m->{files}{$fileUM})
160 || $mangle && $file eq "attrib" );
162 # skip directories in earlier backups (each backup always
163 # has the complete directory tree).
165 my @s = stat("$path/$file");
166 next if ( $i < $m->{idx} && -d _ );
167 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
168 $m->{files}{$fileUM} = $a;
169 $attr->set($fileUM, undef);
172 # Very expensive in the non-attribute case when compresseion
173 # is on. We have to stat the file and read compressed files
174 # to determine their size.
176 $m->{files}{$fileUM} = {
177 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
181 size => -f _ ? $s[7] : 0,
184 if ( $compress && -f _ ) {
186 # Compute the correct size by reading the whole file
188 my $f = BackupPC::FileZIO->open("$path/$file",
190 if ( !defined($f) ) {
191 $m->{error} = "Can't open $path/$file";
194 while ( $f->read(\$data, 65636 * 8) > 0 ) {
195 $size += length($data);
198 $m->{files}{$fileUM}{size} = $size;
202 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
203 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
204 $m->{files}{$fileUM}{fullPath} = "$path/$file";
205 $m->{files}{$fileUM}{backupNum} = $backupNum;
206 $m->{files}{$fileUM}{compress} = $compress;
207 $m->{files}{$fileUM}{nlink} = $s[3];
208 $m->{files}{$fileUM}{inode} = $s[1];
211 # Also include deleted files
213 if ( defined($attr) ) {
215 foreach my $fileUM ( keys(%$a) ) {
216 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
218 $file = $m->{bpc}->fileNameMangle($fileUM) if ( $mangle );
219 $m->{files}{$fileUM} = $a->{$fileUM};
220 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
221 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
222 $m->{files}{$fileUM}{fullPath} = "$path/$file";
223 $m->{files}{$fileUM}{backupNum} = $backupNum;
224 $m->{files}{$fileUM}{compress} = $compress;
225 $m->{files}{$fileUM}{nlink} = 0;
226 $m->{files}{$fileUM}{inode} = 0;
231 # Prune deleted files
233 foreach my $file ( keys(%{$m->{files}}) ) {
234 next if ( $m->{files}{$file}{type} != BPC_FTYPE_DELETED );
235 delete($m->{files}{$file});
237 #print STDERR "Returning:\n", Dumper($m->{files});
241 # Return the attributes of a specific file
245 my($m, $backupNum, $share, $path) = @_;
247 if ( $path =~ s{(.*)/+(.+)}{$1} ) {
249 $m->dirCache($backupNum, $share, $path);
250 return $m->{files}{$file};
252 #print STDERR "Got empty $path\n";
253 $m->dirCache($backupNum, "", "");
254 my %attr = %{$m->{files}{$share}};
255 $attr{relPath} = "/";
261 # Return the contents of a directory
265 my($m, $backupNum, $share, $dir) = @_;
267 $m->dirCache($backupNum, $share, $dir);
275 return $m->{mergeNums};
280 my($m, $share, $dir) = @_;
283 $dir = "/$dir" if ( $dir !~ m{^/} );
286 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
287 my $backupNum = $m->{backups}[$i]{num};
288 my $mangle = $m->{backups}[$i]{mangle};
289 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
292 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
293 . $m->{bpc}->fileNameMangle($dir);
295 $sharePathM = $share . $dir;
297 $path .= $sharePathM;
298 next if ( !-d $path );
299 push(@backupList, $backupNum);
305 # Do a recursive find starting at the given path (either a file
306 # or directory). The callback function $callback is called on each
307 # file and directory. The function arguments are the attrs hashref,
308 # and additional callback arguments. The search is depth-first if
309 # depth is set. Returns -1 if $path does not exist.
313 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
316 # First call the callback on the given $path
318 my $attr = $m->fileAttrib($backupNum, $share, $path);
319 return -1 if ( !defined($attr) );
320 &$callback($attr, @callbackArgs);
321 return if ( $attr->{type} != BPC_FTYPE_DIR );
324 # Now recurse into subdirectories
326 $m->findRecurse($backupNum, $share, $path, $depth,
327 $callback, @callbackArgs);
331 # Same as find(), except the callback is not called on the current
332 # $path, only on the contents of $path. So if $path is a file then
333 # no callback or recursion occurs.
337 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
339 my $attr = $m->dirAttrib($backupNum, $share, $path);
340 return if ( !defined($attr) );
341 foreach my $file ( sort(keys(%$attr)) ) {
342 &$callback($attr->{$file}, @callbackArgs);
343 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
345 # For depth-first, recurse as we hit each directory
347 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
348 $callback, @callbackArgs);
352 # For non-depth, recurse directories after we finish current dir
354 foreach my $file ( keys(%{$attr}) ) {
355 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
356 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
357 $callback, @callbackArgs);