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 3.0.0, released 28 Jan 2007.
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, $options) = @_;
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
62 dirOpts => $options, # $options is a hash of file attributes we need:
63 # type, inode, or nlink. If set, these parameters
64 # are added to the returned hash.
65 # See BackupPC::Lib::dirRead().
67 for ( my $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
68 next if ( defined($m->{backups}[$i]{level}) );
69 $m->{backups}[$i]{level} = $m->{backups}[$i]{type} eq "incr" ? 1 : 0;
71 $m->{topDir} = $m->{bpc}->TopDir();
77 my($m, $backupNum, $share, $dir) = @_;
80 #print STDERR "dirCache($backupNum, $share, $dir)\n";
81 $dir = "/$dir" if ( $dir !~ m{^/} );
83 return if ( $m->{num} == $backupNum
84 && $m->{share} eq $share
86 && $m->{dir} eq $dir );
87 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
88 return if ( $m->{idx} < 0 );
91 $level = $m->{backups}[$m->{idx}]{level} + 1;
94 # Remember the requested share and dir
100 # merge backups, starting at the requested one, and working
101 # backwards until we get to level 0.
103 $m->{mergeNums} = [];
104 for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
105 #print(STDERR "Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
107 # skip backups with the same or higher level
109 next if ( $m->{backups}[$i]{level} >= $level );
111 $level = $m->{backups}[$i]{level};
112 $backupNum = $m->{backups}[$i]{num};
113 push(@{$m->{mergeNums}}, $backupNum);
114 my $mangle = $m->{backups}[$i]{mangle};
115 my $compress = $m->{backups}[$i]{compress};
116 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
119 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
120 . $m->{bpc}->fileNameMangle($dir);
122 $sharePathM = $share . $dir;
124 $path .= $sharePathM;
125 #print(STDERR "Opening $path (share=$share)\n");
127 my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts});
128 if ( !defined($dirInfo) ) {
129 if ( $i == $m->{idx} ) {
131 # Oops, directory doesn't exist.
140 $attr = BackupPC::Attrib->new({ compress => $compress });
141 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
142 $m->{error} = "Can't read attribute file in $path";
146 foreach my $entry ( @$dirInfo ) {
147 my $file = $1 if ( $entry->{name} =~ /(.*)/s );
149 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
150 #print(STDERR "Doing $fileUM\n");
154 next if ( defined($m->{files}{$fileUM})
157 || $file eq "backupInfo"
158 || $mangle && $file eq "attrib" );
159 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
160 $m->{files}{$fileUM} = $a;
162 # skip directories in earlier backups (each backup always
163 # has the complete directory tree).
165 next if ( $i < $m->{idx} && $a->{type} == BPC_FTYPE_DIR );
166 $attr->set($fileUM, undef);
169 # Very expensive in the non-attribute case when compresseion
170 # is on. We have to stat the file and read compressed files
171 # to determine their size.
173 my @s = stat("$path/$file");
174 next if ( $i < $m->{idx} && -d _ );
175 $m->{files}{$fileUM} = {
176 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
180 size => -f _ ? $s[7] : 0,
183 if ( $compress && -f _ ) {
185 # Compute the correct size by reading the whole file
187 my $f = BackupPC::FileZIO->open("$path/$file",
189 if ( !defined($f) ) {
190 $m->{error} = "Can't open $path/$file";
193 while ( $f->read(\$data, 65636 * 8) > 0 ) {
194 $size += length($data);
197 $m->{files}{$fileUM}{size} = $size;
201 ($m->{files}{$fileUM}{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
202 ($m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file")
204 ($m->{files}{$fileUM}{fullPath} = "$path/$file") =~ s{//+}{/}g;
205 $m->{files}{$fileUM}{backupNum} = $backupNum;
206 $m->{files}{$fileUM}{compress} = $compress;
207 $m->{files}{$fileUM}{nlink} = $entry->{nlink}
208 if ( $m->{dirOpts}{nlink} );
209 $m->{files}{$fileUM}{inode} = $entry->{inode}
210 if ( $m->{dirOpts}{inode} );
213 # Also include deleted files
215 if ( defined($attr) ) {
217 foreach my $fileUM ( keys(%$a) ) {
218 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
220 $file = $m->{bpc}->fileNameMangle($fileUM) if ( $mangle );
221 $m->{files}{$fileUM} = $a->{$fileUM};
222 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
223 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
224 $m->{files}{$fileUM}{fullPath} = "$path/$file";
225 $m->{files}{$fileUM}{backupNum} = $backupNum;
226 $m->{files}{$fileUM}{compress} = $compress;
227 $m->{files}{$fileUM}{nlink} = 0;
228 $m->{files}{$fileUM}{inode} = 0;
233 # Prune deleted files
235 foreach my $file ( keys(%{$m->{files}}) ) {
236 next if ( $m->{files}{$file}{type} != BPC_FTYPE_DELETED );
237 delete($m->{files}{$file});
239 #print STDERR "Returning:\n", Dumper($m->{files});
243 # Return list of shares for this backup
247 my($m, $backupNum) = @_;
250 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
251 return if ( $m->{idx} < 0 );
253 my $mangle = $m->{backups}[$m->{idx}]{mangle};
254 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
255 return if ( !opendir(DIR, $path) );
256 my @dir = readdir(DIR);
258 foreach my $file ( @dir ) {
259 $file = $1 if ( $file =~ /(.*)/s );
260 next if ( $file eq "attrib" && $mangle
263 || $file eq "backupInfo"
266 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
267 push(@shareList, $fileUM);
275 my($m, $backupNum) = @_;
277 if ( $m->{num} != $backupNum ) {
279 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
280 last if ( $m->{backups}[$i]{num} == $backupNum );
282 if ( $i >= @{$m->{backups}} ) {
286 $m->{num} = $backupNum;
292 # Return the attributes of a specific file
296 my($m, $backupNum, $share, $path) = @_;
298 #print(STDERR "fileAttrib($backupNum, $share, $path)\n");
299 if ( $path =~ s{(.*)/+(.+)}{$1}s ) {
301 $m->dirCache($backupNum, $share, $path);
302 return $m->{files}{$file};
304 #print STDERR "Got empty $path\n";
305 $m->dirCache($backupNum, "", "");
306 my $attr = $m->{files}{$share};
307 return if ( !defined($attr) );
308 $attr->{relPath} = "/";
314 # Return the contents of a directory
318 my($m, $backupNum, $share, $dir) = @_;
320 $m->dirCache($backupNum, $share, $dir);
325 # Return a listref of backup numbers that are merged to create this view
331 return $m->{mergeNums};
335 # Return a list of backup indexes for which the directory exists
339 my($m, $share, $dir) = @_;
342 $dir = "/$dir" if ( $dir !~ m{^/} );
345 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
346 my $backupNum = $m->{backups}[$i]{num};
347 my $mangle = $m->{backups}[$i]{mangle};
348 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
351 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
352 . $m->{bpc}->fileNameMangle($dir);
354 $sharePathM = $share . $dir;
356 $path .= $sharePathM;
357 next if ( !-d $path );
358 push(@backupList, $i);
364 # Return the history of all backups for a particular directory
368 my($m, $share, $dir) = @_;
372 $dir = "/$dir" if ( $dir !~ m{^/} );
376 # merge backups, starting at the first one, and working
379 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
380 $level = $m->{backups}[$i]{level};
381 my $backupNum = $m->{backups}[$i]{num};
382 my $mangle = $m->{backups}[$i]{mangle};
383 my $compress = $m->{backups}[$i]{compress};
384 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
387 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
388 . $m->{bpc}->fileNameMangle($dir);
390 $sharePathM = $share . $dir;
392 $path .= $sharePathM;
393 #print(STDERR "Opening $path (share=$share)\n");
395 my $dirInfo = $m->{bpc}->dirRead($path, $m->{dirOpts});
396 if ( !defined($dirInfo) ) {
398 # Oops, directory doesn't exist.
404 $attr = BackupPC::Attrib->new({ compress => $compress });
405 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
406 $m->{error} = "Can't read attribute file in $path";
410 foreach my $entry ( @$dirInfo ) {
411 my $file = $1 if ( $entry->{name} =~ /(.*)/s );
413 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
414 #print(STDERR "Doing $fileUM\n");
418 next if ( $file eq ".."
420 || $mangle && $file eq "attrib"
421 || defined($files->{$fileUM}[$i]) );
422 my @s = stat("$path/$file");
423 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
424 $files->{$fileUM}[$i] = $a;
425 $attr->set($fileUM, undef);
428 # Very expensive in the non-attribute case when compresseion
429 # is on. We have to stat the file and read compressed files
430 # to determine their size.
432 $files->{$fileUM}[$i] = {
433 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
437 size => -f _ ? $s[7] : 0,
440 if ( $compress && -f _ ) {
442 # Compute the correct size by reading the whole file
444 my $f = BackupPC::FileZIO->open("$path/$file",
446 if ( !defined($f) ) {
447 $m->{error} = "Can't open $path/$file";
450 while ( $f->read(\$data, 65636 * 8) > 0 ) {
451 $size += length($data);
454 $files->{$fileUM}[$i]{size} = $size;
458 ($files->{$fileUM}[$i]{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
459 ($files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file")
461 ($files->{$fileUM}[$i]{fullPath} = "$path/$file") =~ s{//+}{/}g;
462 $files->{$fileUM}[$i]{backupNum} = $backupNum;
463 $files->{$fileUM}[$i]{compress} = $compress;
464 $files->{$fileUM}[$i]{nlink} = $entry->{nlink}
465 if ( $m->{dirOpts}{nlink} );
466 $files->{$fileUM}[$i]{inode} = $entry->{inode}
467 if ( $m->{dirOpts}{inode} );
473 if ( defined($attr) ) {
475 foreach my $fileUM ( keys(%$a) ) {
476 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
477 $files->{$fileUM}[$i]{type} = BPC_FTYPE_DELETED;
482 # Merge old backups. Don't merge directories from old
483 # backups because every backup has an accurate directory
486 for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) {
487 next if ( $m->{backups}[$k]{level} >= $level );
488 $level = $m->{backups}[$k]{level};
489 foreach my $fileUM ( keys(%$files) ) {
490 next if ( !defined($files->{$fileUM}[$k])
491 || defined($files->{$fileUM}[$i])
492 || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR );
493 $files->{$fileUM}[$i] = $files->{$fileUM}[$k];
499 # Remove deleted files
501 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
502 foreach my $fileUM ( keys(%$files) ) {
503 next if ( !defined($files->{$fileUM}[$i])
504 || $files->{$fileUM}[$i]{type} != BPC_FTYPE_DELETED );
505 $files->{$fileUM}[$i] = undef;
509 #print STDERR "Returning:\n", Dumper($files);
515 # Do a recursive find starting at the given path (either a file
516 # or directory). The callback function $callback is called on each
517 # file and directory. The function arguments are the attrs hashref,
518 # and additional callback arguments. The search is depth-first if
519 # depth is set. Returns -1 if $path does not exist.
523 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
525 #print(STDERR "find: got $backupNum, $share, $path\n");
527 # First call the callback on the given $path
529 my $attr = $m->fileAttrib($backupNum, $share, $path);
530 return -1 if ( !defined($attr) );
531 &$callback($attr, @callbackArgs);
532 return if ( $attr->{type} != BPC_FTYPE_DIR );
535 # Now recurse into subdirectories
537 $m->findRecurse($backupNum, $share, $path, $depth,
538 $callback, @callbackArgs);
542 # Same as find(), except the callback is not called on the current
543 # $path, only on the contents of $path. So if $path is a file then
544 # no callback or recursion occurs.
548 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
550 my $attr = $m->dirAttrib($backupNum, $share, $path);
551 return if ( !defined($attr) );
552 foreach my $file ( sort(keys(%$attr)) ) {
553 &$callback($attr->{$file}, @callbackArgs);
554 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
556 # For depth-first, recurse as we hit each directory
558 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
559 $callback, @callbackArgs);
563 # For non-depth, recurse directories after we finish current dir
565 foreach my $file ( keys(%{$attr}) ) {
566 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
567 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
568 $callback, @callbackArgs);