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-2009 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.2.0, released 31 Jul 2010.
36 # See http://backuppc.sourceforge.net.
38 #========================================================================
40 package BackupPC::View;
46 use BackupPC::Attrib qw(:all);
47 use BackupPC::FileZIO;
49 use Encode qw/from_to/;
51 use Data::Dump qw(dump);
55 my($class, $bpc, $host, $backups, $options) = @_;
57 bpc => $bpc, # BackupPC::Lib object
58 host => $host, # host name
59 backups => $backups, # all backups for this host
60 num => -1, # backup number
61 idx => -1, # index into backups for backup
63 dirPath => undef, # path to current directory
64 dirAttr => undef, # attributes of current directory
65 dirOpts => $options, # $options is a hash of file attributes we need:
66 # type, inode, or nlink. If set, these parameters
67 # are added to the returned hash.
68 # See BackupPC::Lib::dirRead().
70 $m->{topDir} = $m->{bpc}->TopDir();
76 my($m, $backupNum, $share, $dir) = @_;
79 #print STDERR "dirCache($backupNum, $share, $dir)\n";
80 $dir = "/$dir" if ( $dir !~ m{^/} );
82 return if ( $m->{num} == $backupNum
83 && $m->{share} eq $share
85 && $m->{dir} eq $dir );
86 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
87 return if ( $m->{idx} < 0 );
90 $level = $m->{backups}[$m->{idx}]{level} + 1;
93 # Remember the requested share and dir
98 my $previous_increment_entry;
99 my $increment_backupNum = $backupNum; # used in loop
100 my @visit_backup_ids;
102 for ( $i = $m->{idx} ; $i >= 0 ; $i-- ) {
104 # we need two incremental backups to detect changes
105 next if $m->{backups}[$i]{level} >= $level && $#visit_backup_ids > 0;
107 push @visit_backup_ids, $i;
109 # for first full backup we need another full to detect changes
110 last if $m->{backups}[$i]{level} == 0 && $#visit_backup_ids > 0;
112 #warn "# visit_backup_ids = ",dump @visit_backup_ids;
115 # merge backups, starting at the requested one, and working
116 # backwards until we get to level 0.
118 $m->{mergeNums} = [];
119 # for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
120 for my $i ( @visit_backup_ids ) {
121 #print(STDERR "Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
123 # skip backups with the same or higher level
125 #next if ( $m->{backups}[$i]{level} >= $level ); # ASA - visit_backup_ids
127 $level = $m->{backups}[$i]{level};
128 $backupNum = $m->{backups}[$i]{num};
129 push(@{$m->{mergeNums}}, $backupNum);
130 my $mangle = $m->{backups}[$i]{mangle};
131 my $compress = $m->{backups}[$i]{compress};
132 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
133 my $legacyCharset = $m->{backups}[$i]{version} < 3.0;
136 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
137 . $m->{bpc}->fileNameMangle($dir);
139 $sharePathM = $share . $dir;
141 $path .= $sharePathM;
142 #print(STDERR "Opening $path (share=$share, mangle=$mangle)\n");
144 my $dirOpts = { %{$m->{dirOpts} || {} } };
145 $dirOpts->{inode} = 1 if $m->{dirOpts}->{only_increment}; # ASA - detect unchanged
146 #warn "# dirOpts = ",dump $dirOpts;
147 my $attribOpts = { compress => $compress };
148 if ( $legacyCharset ) {
149 $dirOpts->{charsetLegacy}
150 = $attribOpts->{charsetLegacy}
151 = $m->{bpc}->{Conf}{ClientCharsetLegacy} || "iso-8859-1";
154 my $dirInfo = $m->{bpc}->dirRead($path, $dirOpts);
155 if ( !defined($dirInfo) ) {
156 if ( $i == $m->{idx} ) {
158 # Oops, directory doesn't exist.
167 $attr = BackupPC::Attrib->new($attribOpts);
168 if ( !$attr->read($path) ) {
169 $m->{error} = "Can't read attribute file in $path: " . $attr->errStr();
173 foreach my $entry ( @$dirInfo ) {
174 my $file = $1 if ( $entry->{name} =~ /(.*)/s );
176 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
177 #print(STDERR "Doing $fileUM\n");
181 next if ( 0 # defined($m->{files}{$fileUM}) # ASA - need fallthrough
184 || $file eq "backupInfo"
185 || $mangle && $file eq "attrib" );
188 # ASA - we only skip files when invoked with share name
189 if ( $m->{dirOpts}->{only_increment} && $share ) {
190 if ( defined $previous_increment_entry->{$fileUM}
191 && $previous_increment_entry->{$fileUM}->{inode} == $entry->{inode} ) {
192 #warn "# -- $backupNum $fileUM\n";
193 $previous_increment_entry->{$fileUM}->{_hide}++;
195 } elsif ( $backupNum < $increment_backupNum ) {
196 #warn "# << $backupNum $fileUM\n";
197 $previous_increment_entry->{$fileUM}->{_hide}++;
200 #warn "# ++ $backupNum $fileUM ",dump $entry;
201 $previous_increment_entry->{$fileUM} = $entry;
204 next if defined($m->{files}{$fileUM});
207 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
208 $m->{files}{$fileUM} = $a;
210 # skip directories in earlier backups (each backup always
211 # has the complete directory tree).
213 next if ( $i < $m->{idx} && $a->{type} == BPC_FTYPE_DIR );
214 $attr->set($fileUM, undef);
217 # Very expensive in the non-attribute case when compresseion
218 # is on. We have to stat the file and read compressed files
219 # to determine their size.
221 my $realPath = "$path/$file";
223 from_to($realPath, "utf8", $attribOpts->{charsetLegacy})
224 if ( $attribOpts->{charsetLegacy} ne "" );
226 my @s = stat($realPath);
227 next if ( $i < $m->{idx} && -d _ );
228 $m->{files}{$fileUM} = {
229 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
233 size => -f _ ? $s[7] : 0,
236 if ( $compress && -f _ ) {
238 # Compute the correct size by reading the whole file
240 my $f = BackupPC::FileZIO->open($realPath,
242 if ( !defined($f) ) {
243 $m->{error} = "Can't open $realPath";
246 while ( $f->read(\$data, 65636 * 8) > 0 ) {
247 $size += length($data);
250 $m->{files}{$fileUM}{size} = $size;
254 ($m->{files}{$fileUM}{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
255 ($m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file")
257 ($m->{files}{$fileUM}{fullPath} = "$path/$file") =~ s{//+}{/}g;
258 from_to($m->{files}{$fileUM}{fullPath}, "utf8", $attribOpts->{charsetLegacy})
259 if ( $attribOpts->{charsetLegacy} ne "" );
260 $m->{files}{$fileUM}{backupNum} = $backupNum;
261 $m->{files}{$fileUM}{compress} = $compress;
262 $m->{files}{$fileUM}{nlink} = $entry->{nlink}
263 if ( $m->{dirOpts}{nlink} );
264 $m->{files}{$fileUM}{inode} = $entry->{inode}
265 if ( $m->{dirOpts}{inode} );
269 # Also include deleted files
271 if ( defined($attr) ) {
273 foreach my $fileUM ( keys(%$a) ) {
274 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
276 $file = $m->{bpc}->fileNameMangle($fileUM) if ( $mangle );
277 $m->{files}{$fileUM} = $a->{$fileUM};
278 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
279 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
280 $m->{files}{$fileUM}{fullPath} = "$path/$file";
281 from_to($m->{files}{$fileUM}{fullPath}, "utf8", $attribOpts->{charsetLegacy})
282 if ( $attribOpts->{charsetLegacy} ne "" );
283 $m->{files}{$fileUM}{backupNum} = $backupNum;
284 $m->{files}{$fileUM}{compress} = $compress;
285 $m->{files}{$fileUM}{nlink} = 0;
286 $m->{files}{$fileUM}{inode} = 0;
291 # ASA create increment
292 if ( $m->{dirOpts}->{only_increment} ) {
293 #warn "# previous_increment_entry = ",dump $previous_increment_entry;
294 foreach my $file ( grep { exists $previous_increment_entry->{$_}->{_hide} } keys %$previous_increment_entry ) {
295 delete $m->{files}{$file};
301 # Prune deleted files
303 foreach my $file ( keys(%{$m->{files}}) ) {
304 next if ( $m->{files}{$file}{type} != BPC_FTYPE_DELETED );
305 delete($m->{files}{$file});
307 #print STDERR "Returning:\n", Dumper($m->{files});
311 # Return list of shares for this backup
315 my($m, $backupNum) = @_;
318 $m->backupNumCache($backupNum) if ( $m->{num} != $backupNum );
319 return if ( $m->{idx} < 0 );
321 my $mangle = $m->{backups}[$m->{idx}]{mangle};
322 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
323 return if ( !opendir(DIR, $path) );
324 my @dir = readdir(DIR);
326 foreach my $file ( @dir ) {
327 $file = $1 if ( $file =~ /(.*)/s );
328 next if ( $file eq "attrib" && $mangle
331 || $file eq "backupInfo"
334 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
335 push(@shareList, $fileUM);
343 my($m, $backupNum) = @_;
345 if ( $m->{num} != $backupNum ) {
347 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
348 last if ( $m->{backups}[$i]{num} == $backupNum );
350 if ( $i >= @{$m->{backups}} ) {
354 $m->{num} = $backupNum;
360 # Return the attributes of a specific file
364 my($m, $backupNum, $share, $path) = @_;
366 #print(STDERR "fileAttrib($backupNum, $share, $path)\n");
367 if ( $path =~ s{(.*)/+(.+)}{$1}s ) {
369 $m->dirCache($backupNum, $share, $path);
370 return $m->{files}{$file};
372 #print STDERR "Got empty $path\n";
373 $m->dirCache($backupNum, "", "");
374 my $attr = $m->{files}{$share};
375 return if ( !defined($attr) );
376 $attr->{relPath} = "/";
382 # Return the contents of a directory
386 my($m, $backupNum, $share, $dir) = @_;
388 $m->dirCache($backupNum, $share, $dir);
393 # Return a listref of backup numbers that are merged to create this view
399 return $m->{mergeNums};
403 # Return a list of backup indexes for which the directory exists
407 my($m, $share, $dir) = @_;
410 $dir = "/$dir" if ( $dir !~ m{^/} );
413 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
414 my $backupNum = $m->{backups}[$i]{num};
415 my $mangle = $m->{backups}[$i]{mangle};
416 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
419 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
420 . $m->{bpc}->fileNameMangle($dir);
422 $sharePathM = $share . $dir;
424 $path .= $sharePathM;
425 next if ( !-d $path );
426 push(@backupList, $i);
432 # Return the history of all backups for a particular directory
436 my($m, $share, $dir) = @_;
440 $dir = "/$dir" if ( $dir !~ m{^/} );
444 # merge backups, starting at the first one, and working
447 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
448 $level = $m->{backups}[$i]{level};
449 my $backupNum = $m->{backups}[$i]{num};
450 my $mangle = $m->{backups}[$i]{mangle};
451 my $compress = $m->{backups}[$i]{compress};
452 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
453 my $legacyCharset = $m->{backups}[$i]{version} < 3.0;
456 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
457 . $m->{bpc}->fileNameMangle($dir);
459 $sharePathM = $share . $dir;
461 $path .= $sharePathM;
462 #print(STDERR "Opening $path (share=$share)\n");
464 my $dirOpts = { %{$m->{dirOpts} || {} } };
465 my $attribOpts = { compress => $compress };
466 if ( $legacyCharset ) {
467 $dirOpts->{charsetLegacy}
468 = $attribOpts->{charsetLegacy}
469 = $m->{bpc}->{Conf}{ClientCharsetLegacy} || "iso-8859-1";
472 my $dirInfo = $m->{bpc}->dirRead($path, $dirOpts);
473 if ( !defined($dirInfo) ) {
475 # Oops, directory doesn't exist.
481 $attr = BackupPC::Attrib->new($attribOpts);
482 if ( !$attr->read($path) ) {
483 $m->{error} = "Can't read attribute file in $path";
487 foreach my $entry ( @$dirInfo ) {
488 my $file = $1 if ( $entry->{name} =~ /(.*)/s );
490 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
491 #print(STDERR "Doing $fileUM\n");
495 next if ( $file eq ".."
497 || $mangle && $file eq "attrib"
498 || defined($files->{$fileUM}[$i]) );
500 my $realPath = "$path/$file";
501 from_to($realPath, "utf8", $attribOpts->{charsetLegacy})
502 if ( $attribOpts->{charsetLegacy} ne "" );
503 my @s = stat($realPath);
504 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
505 $files->{$fileUM}[$i] = $a;
506 $attr->set($fileUM, undef);
509 # Very expensive in the non-attribute case when compresseion
510 # is on. We have to stat the file and read compressed files
511 # to determine their size.
513 $files->{$fileUM}[$i] = {
514 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
518 size => -f _ ? $s[7] : 0,
521 if ( $compress && -f _ ) {
523 # Compute the correct size by reading the whole file
525 my $f = BackupPC::FileZIO->open("$realPath",
527 if ( !defined($f) ) {
528 $m->{error} = "Can't open $path/$file";
531 while ( $f->read(\$data, 65636 * 8) > 0 ) {
532 $size += length($data);
535 $files->{$fileUM}[$i]{size} = $size;
539 ($files->{$fileUM}[$i]{relPath} = "$dir/$fileUM") =~ s{//+}{/}g;
540 ($files->{$fileUM}[$i]{sharePathM} = "$sharePathM/$file")
542 ($files->{$fileUM}[$i]{fullPath} = "$path/$file") =~ s{//+}{/}g;
543 $files->{$fileUM}[$i]{backupNum} = $backupNum;
544 $files->{$fileUM}[$i]{compress} = $compress;
545 $files->{$fileUM}[$i]{nlink} = $entry->{nlink}
546 if ( $m->{dirOpts}{nlink} );
547 $files->{$fileUM}[$i]{inode} = $entry->{inode}
548 if ( $m->{dirOpts}{inode} );
554 if ( defined($attr) ) {
556 foreach my $fileUM ( keys(%$a) ) {
557 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
558 $files->{$fileUM}[$i]{type} = BPC_FTYPE_DELETED;
563 # Merge old backups. Don't merge directories from old
564 # backups because every backup has an accurate directory
567 for ( my $k = $i - 1 ; $level > 0 && $k >= 0 ; $k-- ) {
568 next if ( $m->{backups}[$k]{level} >= $level );
569 $level = $m->{backups}[$k]{level};
570 foreach my $fileUM ( keys(%$files) ) {
571 next if ( !defined($files->{$fileUM}[$k])
572 || defined($files->{$fileUM}[$i])
573 || $files->{$fileUM}[$k]{type} == BPC_FTYPE_DIR );
574 $files->{$fileUM}[$i] = $files->{$fileUM}[$k];
580 # Remove deleted files
582 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
583 foreach my $fileUM ( keys(%$files) ) {
584 next if ( !defined($files->{$fileUM}[$i])
585 || $files->{$fileUM}[$i]{type} != BPC_FTYPE_DELETED );
586 $files->{$fileUM}[$i] = undef;
590 #print STDERR "Returning:\n", Dumper($files);
596 # Do a recursive find starting at the given path (either a file
597 # or directory). The callback function $callback is called on each
598 # file and directory. The function arguments are the attrs hashref,
599 # and additional callback arguments. The search is depth-first if
600 # depth is set. Returns -1 if $path does not exist.
604 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
606 #print(STDERR "find: got $backupNum, $share, $path\n");
608 # First call the callback on the given $path
610 my $attr = $m->fileAttrib($backupNum, $share, $path);
611 return -1 if ( !defined($attr) );
612 &$callback($attr, @callbackArgs);
613 return if ( $attr->{type} != BPC_FTYPE_DIR );
616 # Now recurse into subdirectories
618 $m->findRecurse($backupNum, $share, $path, $depth,
619 $callback, @callbackArgs);
623 # Same as find(), except the callback is not called on the current
624 # $path, only on the contents of $path. So if $path is a file then
625 # no callback or recursion occurs.
629 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
631 my $attr = $m->dirAttrib($backupNum, $share, $path);
632 return if ( !defined($attr) );
633 foreach my $file ( sort(keys(%$attr)) ) {
634 &$callback($attr->{$file}, @callbackArgs);
635 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
637 # For depth-first, recurse as we hit each directory
639 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
640 $callback, @callbackArgs);
644 # For non-depth, recurse directories after we finish current dir
646 foreach my $file ( keys(%{$attr}) ) {
647 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
648 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
649 $callback, @callbackArgs);