* Completed support for rsync and rsyncd, including restore.
[BackupPC.git] / lib / BackupPC / View.pm
1 #============================================================= -*-perl-*-
2 #
3 # BackupPC::View package
4 #
5 # DESCRIPTION
6 #
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.
11 #
12 # AUTHOR
13 #   Craig Barratt  <cbarratt@users.sourceforge.net>
14 #
15 # COPYRIGHT
16 #   Copyright (C) 2002  Craig Barratt
17 #
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.
22 #
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.
27 #
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
31 #
32 #========================================================================
33 #
34 # Version 1.6.0_CVS, released 10 Dec 2002.
35 #
36 # See http://backuppc.sourceforge.net.
37 #
38 #========================================================================
39
40 package BackupPC::View;
41
42 use strict;
43
44 use File::Path;
45 use BackupPC::Lib;
46 use BackupPC::Attrib qw(:all);
47 use BackupPC::FileZIO;
48
49 sub new
50 {
51     my($class, $bpc, $host, $backups) = @_;
52     my $m = bless {
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
58                                         #   we are viewing
59         dirPath   => undef,             # path to current directory
60         dirAttr   => undef,             # attributes of current directory
61     }, $class;
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;
65     }
66     $m->{topDir} = $m->{bpc}->TopDir();
67     return $m;
68 }
69
70 sub dirCache
71 {
72     my($m, $backupNum, $share, $dir) = @_;
73     my($i, $level);
74
75     $dir = "/$dir" if ( $dir !~ m{^/} );
76     $dir =~ s{/+$}{};
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 );
83         }
84         if ( $i >= @{$m->{backups}} ) {
85             $m->{idx} = -1;
86             return;
87         }
88         $m->{num} = $backupNum;
89         $m->{idx} = $i;
90     }
91     $m->{files} = {};
92     $level = $m->{backups}[$m->{idx}]{level} + 1;
93
94     #
95     # Remember the requested share and dir
96     #
97     $m->{share} = $share;
98     $m->{dir} = $dir;
99
100     #
101     # merge backups, starting at the requested one, and working
102     # backwards until we get to level 0.
103     #
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");
107         #
108         # skip backups with the same or higher level
109         #
110         next if ( $m->{backups}[$i]{level} >= $level );
111
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/";
118         my $sharePathM;
119         if ( $mangle ) {
120             $sharePathM = $m->{bpc}->fileNameEltMangle($share)
121                         . $m->{bpc}->fileNameMangle($dir);
122         } else {
123             $sharePathM = $share . $dir;
124         }
125         $path .= $sharePathM;
126         #print("Opening $path\n");
127         if ( !opendir(DIR, $path) ) {
128             if ( $i == $m->{idx} ) {
129                 #
130                 # Oops, directory doesn't exist.
131                 #
132                 $m->{files} = undef;
133                 return;
134             }
135             next;
136         }
137         my @dir = readdir(DIR);
138         closedir(DIR);
139         my $attr;
140         if ( $mangle ) {
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";
144                 $attr = undef;
145             }
146         }
147         foreach my $file ( @dir ) {
148             $file = $1 if ( $file =~ /(.*)/ );
149             my $fileUM = $file;
150             $fileUM = $m->{bpc}->fileNameUnmangle($fileUM) if ( $mangle );
151             #
152             # skip special files
153             #
154             next if ( defined($m->{files}{$fileUM})
155                     || $file eq ".."
156                     || $file eq "."
157                     || $mangle && $file eq "attrib" );
158             #
159             # skip directories in earlier backups (each backup always
160             # has the complete directory tree).
161             #
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);
167             } else {
168                 #
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.
172                 #
173                 $m->{files}{$fileUM} = {
174                     type  => -d _ ? BPC_FTYPE_DIR : BPC_FTYPE_FILE,
175                     mode  => $s[2],
176                     uid   => $s[4],
177                     gid   => $s[5],
178                     size  => -f _ ? $s[7] : 0,
179                     mtime => $s[9],
180                 };
181                 if ( $compress && -f _ ) {
182                     #
183                     # Compute the correct size by reading the whole file
184                     #
185                     my $f = BackupPC::FileZIO->open("$path/$file",
186                                                     0, $compress);
187                     if ( !defined($f) ) {
188                         $m->{error} = "Can't open $path/$file";
189                     } else {
190                         my($data, $size);
191                         while ( $f->read(\$data, 65636 * 8) > 0 ) {
192                             $size += length($data);
193                         }
194                         $f->close;
195                         $m->{files}{$fileUM}{size} = $size;
196                     }
197                 }
198             }
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];
206         }
207         #
208         # Also include deleted files
209         #
210         if ( defined($attr) ) {
211             my $a = $attr->get;
212             foreach my $fileUM ( keys(%$a) ) {
213                 next if ( $a->{$fileUM}{type} != BPC_FTYPE_DELETED );
214                 my $file = $fileUM;
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;
224             }
225         }
226     }
227     #
228     # Prune deleted files
229     #
230     foreach my $file ( keys(%{$m->{files}}) ) {
231         next if ( $m->{files}{$file}{type} != BPC_FTYPE_DELETED );
232         delete($m->{files}{$file});
233     }
234 }
235
236 #
237 # Return the attributes of a specific file
238 #
239 sub fileAttrib
240 {
241     my($m, $backupNum, $share, $path) = @_;
242     my $dir = $path;
243     $dir =~ s{(.*)/(.*)}{$1};
244     my $file = $2;
245
246     $m->dirCache($backupNum, $share, $dir);
247     return $m->{files}{$file};
248 }
249
250 #
251 # Return the contents of a directory
252 #
253 sub dirAttrib
254 {
255     my($m, $backupNum, $share, $dir) = @_;
256
257     $m->dirCache($backupNum, $share, $dir);
258     return $m->{files};
259 }
260
261 sub mergeNums
262 {
263     my($m) = @_;
264
265     return $m->{mergeNums};
266 }
267
268 sub backupList
269 {
270     my($m, $share, $dir) = @_;
271     my($i, @backupList);
272
273     $dir = "/$dir" if ( $dir !~ m{^/} );
274     $dir =~ s{/+$}{};
275
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/";
280         my $sharePathM;
281         if ( $mangle ) {
282             $sharePathM = $m->{bpc}->fileNameEltMangle($share)
283                         . $m->{bpc}->fileNameMangle($dir);
284         } else {
285             $sharePathM = $share . $dir;
286         }
287         $path .= $sharePathM;
288         next if ( !-d $path );
289         push(@backupList, $backupNum);
290     }
291     return @backupList;
292 }
293
294 #
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.
300 #
301 sub find
302 {
303     my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
304
305     #
306     # First call the callback on the given $path
307     #
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 );
312
313     #
314     # Now recurse into subdirectories
315     #
316     $m->findRecurse($backupNum, $share, $path, $depth,
317                     $callback, @callbackArgs);
318 }
319
320 #
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.
324 #
325 sub findRecurse
326 {
327     my($m, $backupNum, $share, $path, $depth, $callback, @callbackArgs) = @_;
328
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 );
334         #
335         # For depth-first, recurse as we hit each directory
336         #
337         $m->findRecurse($backupNum, $share, "$path/$file", $depth,
338                              $callback, @callbackArgs);
339     }
340     if ( !$depth ) {
341         #
342         # For non-depth, recurse directories after we finish current dir
343         #
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);
348         }
349     }
350 }
351
352 1;