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-2003 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.1.0_CVS, released 3 Jul 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 "incr" ? 1 : 0;
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 # Return the history of all backups for a particular directory
309 my($m, $share, $dir) = @_;
313 $dir = "/$dir" if ( $dir !~ m{^/} );
317 # merge backups, starting at the first one, and working
320 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
321 $level = $m->{backups}[$i]{level};
322 my $backupNum = $m->{backups}[$i]{num};
323 my $mangle = $m->{backups}[$i]{mangle};
324 my $compress = $m->{backups}[$i]{compress};
325 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
328 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
329 . $m->{bpc}->fileNameMangle($dir);
331 $sharePathM = $share . $dir;
333 $path .= $sharePathM;
334 #print(STDERR "Opening $path (share=$share)\n");
335 if ( !opendir(DIR, $path) ) {
337 # Oops, directory doesn't exist.
341 my @dir = readdir(DIR);
345 $attr = BackupPC::Attrib->new({ compress => $compress });
346 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
347 $m->{error} = "Can't read attribute file in $path";
351 foreach my $file ( @dir ) {
352 $file = $1 if ( $file =~ /(.*)/ );
354 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
355 #print(STDERR "Doing $fileUM\n");
359 next if ( $file eq ".."
361 || $mangle && $file eq "attrib"
362 || defined($files->{$fileUM}[$i]) );
363 my @s = stat("$path/$file");
364 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
365 $files->{$fileUM}[$i] = $a;
366 $attr->set($fileUM, undef);
369 # Very expensive in the non-attribute case when compresseion
370 # is on. We have to stat the file and read compressed files
371 # to determine their size.
373 $files->{$fileUM}[$i] = {
374 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
378 size => -f _ ? $s[7] : 0,
381 if ( $compress && -f _ ) {
383 # Compute the correct size by reading the whole file
385 my $f = BackupPC::FileZIO->open("$path/$file",
387 if ( !defined($f) ) {
388 $m->{error} = "Can't open $path/$file";
391 while ( $f->read(\$data, 65636 * 8) > 0 ) {
392 $size += length($data);
395 $files->{$fileUM}[$i]{size} = $size;
399 $files->{$fileUM}[$i]{relPath} = "$dir/$fileUM";
400 $files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file";
401 $files->{$fileUM}[$i]{fullPath} = "$path/$file";
402 $files->{$fileUM}[$i]{backupNum} = $backupNum;
403 $files->{$fileUM}[$i]{compress} = $compress;
404 $files->{$fileUM}[$i]{nlink} = $s[3];
405 $files->{$fileUM}[$i]{inode} = $s[1];
409 # Merge old backups. Don't merge directories from old
410 # backups because every backup has an accurate directory
413 for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) {
414 next if ( $m->{backups}[$k]{level} >= $level );
415 $level = $m->{backups}[$k]{level};
416 foreach my $fileUM ( keys(%$files) ) {
417 next if ( !defined($files->{$fileUM}[$k])
418 || defined($files->{$fileUM}[$i])
419 || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR );
420 $files->{$fileUM}[$i] = $files->{$fileUM}[$k];
425 # Finally, remove deleted files
427 if ( defined($attr) ) {
429 foreach my $fileUM ( keys(%$a) ) {
430 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
431 $files->{$fileUM}[$i] = undef if ( defined($files->{$fileUM}) );
435 #print STDERR "Returning:\n", Dumper($files);
441 # Do a recursive find starting at the given path (either a file
442 # or directory). The callback function $callback is called on each
443 # file and directory. The function arguments are the attrs hashref,
444 # and additional callback arguments. The search is depth-first if
445 # depth is set. Returns -1 if $path does not exist.
449 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
452 # First call the callback on the given $path
454 my $attr = $m->fileAttrib($backupNum, $share, $path);
455 return -1 if ( !defined($attr) );
456 &$callback($attr, @callbackArgs);
457 return if ( $attr->{type} != BPC_FTYPE_DIR );
460 # Now recurse into subdirectories
462 $m->findRecurse($backupNum, $share, $path, $depth,
463 $callback, @callbackArgs);
467 # Same as find(), except the callback is not called on the current
468 # $path, only on the contents of $path. So if $path is a file then
469 # no callback or recursion occurs.
473 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
475 my $attr = $m->dirAttrib($backupNum, $share, $path);
476 return if ( !defined($attr) );
477 foreach my $file ( sort(keys(%$attr)) ) {
478 &$callback($attr->{$file}, @callbackArgs);
479 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
481 # For depth-first, recurse as we hit each directory
483 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
484 $callback, @callbackArgs);
488 # For non-depth, recurse directories after we finish current dir
490 foreach my $file ( keys(%{$attr}) ) {
491 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
492 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
493 $callback, @callbackArgs);