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.0_CVS, released 18 Jan 2003.
36 # See http://backuppc.sourceforge.net.
38 #========================================================================
40 package BackupPC::View;
46 use BackupPC::Attrib qw(:all);
47 use BackupPC::FileZIO;
51 my($class, $bpc, $host, $backups) = @_;
53 bpc => $bpc, # BackupPC::Lib object
54 host => $host, # host name
55 backups => $backups, # all backups for this host
56 num => -1, # backup number
57 idx => -1, # index into backups for backup
59 dirPath => undef, # path to current directory
60 dirAttr => undef, # attributes of current directory
62 for ( my $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
63 next if ( defined($m->{backups}[$i]{level}) );
64 $m->{backups}[$i]{level} = $m->{backups}[$i]{type} eq "full" ? 0 : 1;
66 $m->{topDir} = $m->{bpc}->TopDir();
72 my($m, $backupNum, $share, $dir) = @_;
75 $dir = "/$dir" if ( $dir !~ m{^/} );
77 return if ( $m->{num} == $backupNum
78 && $m->{share} eq $share
79 && $m->{dir} eq $dir );
80 if ( $m->{num} != $backupNum ) {
81 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
82 last if ( $m->{backups}[$i]{num} == $backupNum );
84 if ( $i >= @{$m->{backups}} ) {
88 $m->{num} = $backupNum;
92 $level = $m->{backups}[$m->{idx}]{level} + 1;
95 # Remember the requested share and dir
101 # merge backups, starting at the requested one, and working
102 # backwards until we get to level 0.
104 $m->{mergeNums} = [];
105 for ( $i = $m->{idx} ; $level > 0 && $i >= 0 ; $i-- ) {
106 #print("Do $i ($m->{backups}[$i]{noFill},$m->{backups}[$i]{level})\n");
108 # skip backups with the same or higher level
110 next if ( $m->{backups}[$i]{level} >= $level );
112 $level = $m->{backups}[$i]{level};
113 $backupNum = $m->{backups}[$i]{num};
114 push(@{$m->{mergeNums}}, $backupNum);
115 my $mangle = $m->{backups}[$i]{mangle};
116 my $compress = $m->{backups}[$i]{compress};
117 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
120 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
121 . $m->{bpc}->fileNameMangle($dir);
123 $sharePathM = $share . $dir;
125 $path .= $sharePathM;
126 #print("Opening $path\n");
127 if ( !opendir(DIR, $path) ) {
128 if ( $i == $m->{idx} ) {
130 # Oops, directory doesn't exist.
137 my @dir = readdir(DIR);
141 $attr = BackupPC::Attrib->new({ compress => $compress });
142 if ( -f $attr->fileName($path) && !$attr->read($path) ) {
143 $m->{error} = "Can't read attribute file in $path";
147 foreach my $file ( @dir ) {
148 $file = $1 if ( $file =~ /(.*)/ );
150 $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
154 next if ( defined($m->{files}{$fileUM})
157 || $mangle && $file eq "attrib" );
159 # skip directories in earlier backups (each backup always
160 # has the complete directory tree).
162 my @s = stat("$path/$file");
163 next if ( $i < $m->{idx} && -d _ );
164 if ( defined($attr) && defined(my $a = $attr->get($fileUM)) ) {
165 $m->{files}{$fileUM} = $a;
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 $m->{files}{$fileUM} = {
174 type => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
178 size => -f _ ? $s[7] : 0,
181 if ( $compress && -f _ ) {
183 # Compute the correct size by reading the whole file
185 my $f = BackupPC::FileZIO->open("$path/$file",
187 if ( !defined($f) ) {
188 $m->{error} = "Can't open $path/$file";
191 while ( $f->read(\$data, 65636 * 8) > 0 ) {
192 $size += length($data);
195 $m->{files}{$fileUM}{size} = $size;
199 $m->{files}{$fileUM}{relPath} = "$dir/$fileUM";
200 $m->{files}{$fileUM}{sharePathM} = "$sharePathM/$file";
201 $m->{files}{$fileUM}{fullPath} = "$path/$file";
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});
237 # Return the attributes of a specific file
241 my($m, $backupNum, $share, $path) = @_;
243 $dir =~ s{(.*)/(.*)}{$1};
246 $m->dirCache($backupNum, $share, $dir);
247 return $m->{files}{$file};
251 # Return the contents of a directory
255 my($m, $backupNum, $share, $dir) = @_;
257 $m->dirCache($backupNum, $share, $dir);
265 return $m->{mergeNums};
270 my($m, $share, $dir) = @_;
273 $dir = "/$dir" if ( $dir !~ m{^/} );
276 for ( $i = 0 ; $i < @{$m->{backups}} ; $i++ ) {
277 my $backupNum = $m->{backups}[$i]{num};
278 my $mangle = $m->{backups}[$i]{mangle};
279 my $path = "$m->{topDir}/pc/$m->{host}/$backupNum/";
282 $sharePathM = $m->{bpc}->fileNameEltMangle($share)
283 . $m->{bpc}->fileNameMangle($dir);
285 $sharePathM = $share . $dir;
287 $path .= $sharePathM;
288 next if ( !-d $path );
289 push(@backupList, $backupNum);
295 # Do a recursive find starting at the given path (either a file
296 # or directory). The callback function $callback is called on each
297 # file and directory. The function arguments are the attrs hashref,
298 # and additional callback arguments. The search is depth-first if
299 # depth is set. Returns -1 if $path does not exist.
303 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
306 # First call the callback on the given $path
308 my $attr = $m->fileAttrib($backupNum, $share, $path);
309 return -1 if ( !defined($attr) );
310 &$callback($attr, @callbackArgs);
311 return if ( $attr->{type} != BPC_FTYPE_DIR );
314 # Now recurse into subdirectories
316 $m->findRecurse($backupNum, $share, $path, $depth,
317 $callback, @callbackArgs);
321 # Same as find(), except the callback is not called on the current
322 # $path, only on the contents of $path. So if $path is a file then
323 # no callback or recursion occurs.
327 my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
329 my $attr = $m->dirAttrib($backupNum, $share, $path);
330 return if ( !defined($attr) );
331 foreach my $file ( keys(%$attr) ) {
332 &$callback($attr->{$file}, @callbackArgs);
333 next if ( !$depth || $attr->{$file}{type} != BPC_FTYPE_DIR );
335 # For depth-first, recurse as we hit each directory
337 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
338 $callback, @callbackArgs);
342 # For non-depth, recurse directories after we finish current dir
344 foreach my $file ( keys(%{$attr}) ) {
345 next if ( $attr->{$file}{type} != BPC_FTYPE_DIR );
346 $m->findRecurse($backupNum, $share, "$path/$file", $depth,
347 $callback, @callbackArgs);