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.2, released 5 Sep 2005.
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, $only_one) = @_;
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();
68 $m->{only_one} = $only_one;
74 my($m, $backupNum, $share, $dir) = @_;
77 #print STDERR "dirCache($backupNum, $share, $dir)\n";
78 $dir = "/$dir" if ( $dir !~ m{^/} );
80 return if ( $m->{num} == $backupNum
81 && $m->{share} eq $share
83 && $m->{dir} eq $dir );
84 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
85 return if ( $m->{idx} < 0 );
88 $level = $m->{backups}[$m->{idx}]{level} + 1;
91 # Remember the requested share and dir
97 # merge backups, starting at the requested one, and working
98 # backwards until we get to level 0.
100 $m->{mergeNums} = [];
101 for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
102 #print(STDERR "Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
104 # skip backups with the same or higher level
106 next if ( $m->{backups}[$i]{level} >= $level );
108 last if ( $m->{only_one} && $i != $m->{idx} );
110 $level = $m->{backups}[$i]{level};
111 $backupNum = $m->{backups}[$i]{num};
112 push(@{$m->{mergeNums}}, $backupNum);
113 my $mangle = $m->{backups}[$i]{mangle};
114 my $compress = $m->{backups}[$i]{compress};
115 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
118 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
119 . $m->{bpc}->fileNameMangle($dir);
121 $sharePathM = $share . $dir;
123 $path .= $sharePathM;
124 #print(STDERR "Opening $path (share=$share)\n");
125 if ( !opendir(DIR, $path) ) {
126 if ( $i == $m->{idx} ) {
128 # Oops, directory doesn't exist.
135 my @dir = readdir(DIR);
139 $attr = BackupPC::Attrib->new({ compress => $compress });
140 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
141 $m->{error} = "Can't read attribute file in $path";
145 foreach my $file ( @dir ) {
146 $file = $1 if ( $file =~ /(.*)/ );
148 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
149 #print(STDERR "Doing $fileUM\n");
153 next if ( defined($m->{files}{$fileUM})
156 || $mangle && $file eq "attrib" );
158 # skip directories in earlier backups (each backup always
159 # has the complete directory tree).
161 my @s = stat("$path/$file");
162 next if ( $i < $m->{idx} && -d _ );
163 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
164 $m->{files}{$fileUM} = $a;
165 $attr->set($fileUM, undef);
168 # Very expensive in the non-attribute case when compresseion
169 # is on. We have to stat the file and read compressed files
170 # to determine their size.
172 $m->{files}{$fileUM} = {
173 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
177 size => -f _ ? $s[7] : 0,
180 if ( $compress && -f _ ) {
182 # Compute the correct size by reading the whole file
184 my $f = BackupPC::FileZIO->open("$path/$file",
186 if ( !defined($f) ) {
187 $m->{error} = "Can't open $path/$file";
190 while ( $f->read(\$data, 65636 * 8) > 0 ) {
191 $size += length($data);
194 $m->{files}{$fileUM}{size} = $size;
198 ($m->{files}{$fileUM}{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
199 ($m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file")
201 ($m->{files}{$fileUM}{fullPath} = "$path/$file") =~ s{//+}{/}g;
202 $m->{files}{$fileUM}{backupNum} = $backupNum;
203 $m->{files}{$fileUM}{compress} = $compress;
204 $m->{files}{$fileUM}{nlink} = $s[3];
205 $m->{files}{$fileUM}{inode} = $s[1];
208 # Also include deleted files
210 if ( defined($attr) ) {
212 foreach my $fileUM ( keys(%$a) ) {
213 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
215 $file = $m->{bpc}->fileNameMangle($fileUM) if ( $mangle );
216 $m->{files}{$fileUM} = $a->{$fileUM};
217 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
218 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
219 $m->{files}{$fileUM}{fullPath} = "$path/$file";
220 $m->{files}{$fileUM}{backupNum} = $backupNum;
221 $m->{files}{$fileUM}{compress} = $compress;
222 $m->{files}{$fileUM}{nlink} = 0;
223 $m->{files}{$fileUM}{inode} = 0;
228 # Prune deleted files
230 foreach my $file ( keys(%{$m->{files}}) ) {
231 next if ( $m->{files}{$file}{type} != BPC_FTYPE_DELETED );
232 delete($m->{files}{$file});
234 #print STDERR "Returning:\n", Dumper($m->{files});
238 # Return list of shares for this backup
242 my($m, $backupNum) = @_;
245 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
246 return if ( $m->{idx} < 0 );
248 my $mangle = $m->{backups}[$m->{idx}]{mangle};
249 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
250 return if ( !opendir(DIR, $path) );
251 my @dir = readdir(DIR);
253 foreach my $file ( @dir ) {
254 $file = $1 if ( $file =~ /(.*)/ );
255 next if ( $file eq "attrib" && $mangle
259 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
260 push(@shareList, $fileUM);
268 my($m, $backupNum) = @_;
270 if ( $m->{num} != $backupNum ) {
272 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
273 last if ( $m->{backups}[$i]{num} == $backupNum );
275 if ( $i >= @{$m->{backups}} ) {
279 $m->{num} = $backupNum;
285 # Return the attributes of a specific file
289 my($m, $backupNum, $share, $path) = @_;
291 #print(STDERR "fileAttrib($backupNum, $share, $path)\n");
292 if ( $path =~ s{(.*)/+(.+)}{$1} ) {
294 $m->dirCache($backupNum, $share, $path);
295 return $m->{files}{$file};
297 #print STDERR "Got empty $path\n";
298 $m->dirCache($backupNum, "", "");
299 my $attr = $m->{files}{$share};
300 return if ( !defined($attr) );
301 $attr->{relPath} = "/";
307 # Return the contents of a directory
311 my($m, $backupNum, $share, $dir) = @_;
313 $m->dirCache($backupNum, $share, $dir);
318 # Return a listref of backup numbers that are merged to create this view
324 return $m->{mergeNums};
328 # Return a list of backup indexes for which the directory exists
332 my($m, $share, $dir) = @_;
335 $dir = "/$dir" if ( $dir !~ m{^/} );
338 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
339 my $backupNum = $m->{backups}[$i]{num};
340 my $mangle = $m->{backups}[$i]{mangle};
341 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
344 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
345 . $m->{bpc}->fileNameMangle($dir);
347 $sharePathM = $share . $dir;
349 $path .= $sharePathM;
350 next if ( !-d $path );
351 push(@backupList, $i);
357 # Return the history of all backups for a particular directory
361 my($m, $share, $dir) = @_;
365 $dir = "/$dir" if ( $dir !~ m{^/} );
369 # merge backups, starting at the first one, and working
372 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
373 $level = $m->{backups}[$i]{level};
374 my $backupNum = $m->{backups}[$i]{num};
375 my $mangle = $m->{backups}[$i]{mangle};
376 my $compress = $m->{backups}[$i]{compress};
377 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
380 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
381 . $m->{bpc}->fileNameMangle($dir);
383 $sharePathM = $share . $dir;
385 $path .= $sharePathM;
386 #print(STDERR "Opening $path (share=$share)\n");
387 if ( !opendir(DIR, $path) ) {
389 # Oops, directory doesn't exist.
393 my @dir = readdir(DIR);
397 $attr = BackupPC::Attrib->new({ compress => $compress });
398 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
399 $m->{error} = "Can't read attribute file in $path";
403 foreach my $file ( @dir ) {
404 $file = $1 if ( $file =~ /(.*)/ );
406 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
407 #print(STDERR "Doing $fileUM\n");
411 next if ( $file eq ".."
413 || $mangle && $file eq "attrib"
414 || defined($files->{$fileUM}[$i]) );
415 my @s = stat("$path/$file");
416 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
417 $files->{$fileUM}[$i] = $a;
418 $attr->set($fileUM, undef);
421 # Very expensive in the non-attribute case when compresseion
422 # is on. We have to stat the file and read compressed files
423 # to determine their size.
425 $files->{$fileUM}[$i] = {
426 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
430 size => -f _ ? $s[7] : 0,
433 if ( $compress && -f _ ) {
435 # Compute the correct size by reading the whole file
437 my $f = BackupPC::FileZIO->open("$path/$file",
439 if ( !defined($f) ) {
440 $m->{error} = "Can't open $path/$file";
443 while ( $f->read(\$data, 65636 * 8) > 0 ) {
444 $size += length($data);
447 $files->{$fileUM}[$i]{size} = $size;
451 ($files->{$fileUM}[$i]{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
452 ($files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file")
454 ($files->{$fileUM}[$i]{fullPath} = "$path/$file") =~ s{//+}{/}g;
455 $files->{$fileUM}[$i]{backupNum} = $backupNum;
456 $files->{$fileUM}[$i]{compress} = $compress;
457 $files->{$fileUM}[$i]{nlink} = $s[3];
458 $files->{$fileUM}[$i]{inode} = $s[1];
462 # Merge old backups. Don't merge directories from old
463 # backups because every backup has an accurate directory
466 for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) {
467 next if ( $m->{backups}[$k]{level} >= $level );
468 $level = $m->{backups}[$k]{level};
469 foreach my $fileUM ( keys(%$files) ) {
470 next if ( !defined($files->{$fileUM}[$k])
471 || defined($files->{$fileUM}[$i])
472 || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR );
473 $files->{$fileUM}[$i] = $files->{$fileUM}[$k];
478 # Finally, remove deleted files
480 if ( defined($attr) ) {
482 foreach my $fileUM ( keys(%$a) ) {
483 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
484 $files->{$fileUM}[$i] = undef if ( defined($files->{$fileUM}) );
488 #print STDERR "Returning:\n", Dumper($files);
494 # Do a recursive find starting at the given path (either a file
495 # or directory). The callback function $callback is called on each
496 # file and directory. The function arguments are the attrs hashref,
497 # and additional callback arguments. The search is depth-first if
498 # depth is set. Returns -1 if $path does not exist.
502 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
504 #print(STDERR "find: got $backupNum, $share, $path\n");
506 # First call the callback on the given $path
508 my $attr = $m->fileAttrib($backupNum, $share, $path);
509 return -1 if ( !defined($attr) );
510 &$callback($attr, @callbackArgs);
511 return if ( $attr->{type} != BPC_FTYPE_DIR );
514 # Now recurse into subdirectories
516 $m->findRecurse($backupNum, $share, $path, $depth,
517 $callback, @callbackArgs);
521 # Same as find(), except the callback is not called on the current
522 # $path, only on the contents of $path. So if $path is a file then
523 # no callback or recursion occurs.
527 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
529 my $attr = $m->dirAttrib($backupNum, $share, $path);
530 return if ( !defined($attr) );
531 foreach my $file ( sort(keys(%$attr)) ) {
532 &$callback($attr->{$file}, @callbackArgs);
533 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
535 # For depth-first, recurse as we hit each directory
537 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
538 $callback, @callbackArgs);
542 # For non-depth, recurse directories after we finish current dir
544 foreach my $file ( keys(%{$attr}) ) {
545 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
546 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
547 $callback, @callbackArgs);