2 #============================================================= -*-perl-*-
4 # BackupPC_tarExtract: extract data from a dump
9 # Craig Barratt <cbarratt@users.sourceforge.net>
12 # Copyright (C) 2001-2003 Craig Barratt
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #========================================================================
30 # Version 2.1.0_CVS, released 8 Feb 2004.
32 # See http://backuppc.sourceforge.net.
34 #========================================================================
38 use lib "/usr/local/BackupPC/lib";
40 use BackupPC::Attrib qw(:all);
41 use BackupPC::FileZIO;
42 use BackupPC::PoolWrite;
45 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
46 my $TopDir = $bpc->TopDir();
47 my $BinDir = $bpc->BinDir();
48 my %Conf = $bpc->Conf();
51 print("usage: $0 <host> <shareName> <compressLevel>\n");
54 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
55 print("$0: bad host name '$ARGV[0]'\n");
59 if ( $ARGV[1] !~ /^([\w\s\.\/\$-]+)$/ ) {
60 print("$0: bad share name '$ARGV[1]'\n");
64 my $ShareName = $bpc->fileNameEltMangle($ShareNameUM);
65 if ( $ARGV[2] !~ /^(\d+)$/ ) {
66 print("$0: bad compress level '$ARGV[2]'\n");
72 # This constant and the line of code below that uses it is borrowed
73 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
76 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
77 # Copyright 1998 Stephen Zander. All rights reserved.
80 = 'Z100 A8 A8 A8 A12 A12 A8 A1 Z100 A6 A2 Z32 Z32 A8 A8 A155 x12';
81 my $tar_header_length = 512;
83 my $BufSize = 1048576; # 1MB or 2^20
86 my $OutDir = "$TopDir/pc/$host/new";
90 my $ExistFileSize = 0;
91 my $ExistFileCompSize = 0;
93 my $TotalFileSize = 0;
97 my($fh, $totBytes) = @_;
98 my($numBytes, $newBytes, $data);
100 $data = "\0" x $totBytes;
101 while ( $numBytes < $totBytes ) {
102 $newBytes = sysread($fh,
103 substr($data, $numBytes, $totBytes - $numBytes),
104 $totBytes - $numBytes);
105 if ( $newBytes <= 0 ) {
106 print(STDERR "Unexpected end of tar archive (tot = $totBytes,"
107 . " num = $numBytes, posn = " . sysseek($fh, 0, 1) . ")\n");
111 $numBytes += $newBytes;
120 return $1 if ( TarRead($fh, $tar_header_length) =~ /(.*)/s );
128 if ( $size % $tar_header_length ) {
129 TarRead($fh, $tar_header_length - ($size % $tar_header_length));
136 my($head, $longName, $longLink);
137 my($name, $mode, $uid, $gid, $size, $mtime, $chksum, $type,
138 $linkname, $magic, $version, $uname, $gname, $devmajor,
142 $head = TarReadHeader($fh);
143 return if ( $head eq "" || $head eq "\0" x $tar_header_length );
145 $mode, # octal number
148 $size, # octal number
149 $mtime, # octal number
150 $chksum, # octal number
154 $version, # two bytes
157 $devmajor, # octal number
158 $devminor, # octal number
159 $prefix) = unpack($tar_unpack_header, $head);
164 if ( ord($size) == 128 ) {
166 # GNU tar extension: for >=8GB files the size is stored
167 # in big endian binary.
169 $size = 65536 * 65536 * unpack("N", substr($size, 4, 4))
170 + unpack("N", substr($size, 8, 4));
173 # We used to have a patch here for smbclient 2.2.x. For file
174 # sizes between 2 and 4GB it sent the wrong size. But since
175 # samba 3.0.0 has been released we no longer support this
176 # patch since valid files could have sizes that start with
177 # 6 or 7 in octal (eg: 6-8GB files).
179 # $size =~ s/^6/2/; # fix bug in smbclient for >=2GB files
180 # $size =~ s/^7/3/; # fix bug in smbclient for >=2GB files
182 # To avoid integer overflow in case we are in the 4GB - 8GB
183 # range, we do the conversion in two parts.
185 if ( $size =~ /([0-9]{9,})/ ) {
186 my $len = length($1);
187 $size = oct(substr($1, 0, $len - 8)) * (1 << 24)
188 + oct(substr($1, $len - 8));
194 $chksum = oct $chksum;
195 $devmajor = oct $devmajor;
196 $devminor = oct $devminor;
197 $name = "$prefix/$name" if $prefix;
199 substr ($head, 148, 8) = " ";
200 if (unpack ("%16C*", $head) != $chksum) {
201 print(STDERR "$name: checksum error at "
202 . sysseek($fh, 0, 1) , "\n");
205 if ( $type eq "L" ) {
206 $longName = TarRead($fh, $size) || return;
207 # remove trailing NULL
208 $longName = substr($longName, 0, $size - 1);
209 TarFlush($fh, $size);
211 } elsif ( $type eq "K" ) {
212 $longLink = TarRead($fh, $size) || return;
213 # remove trailing NULL
214 $longLink = substr($longLink, 0, $size - 1);
215 TarFlush($fh, $size);
218 $name = $longName if ( defined($longName) );
219 $linkname = $longLink if ( defined($longLink) );
225 mangleName => $bpc->fileNameMangle($name),
232 linkname => $linkname,
233 devmajor => $devmajor,
234 devminor => $devminor,
242 my $f = TarReadFileInfo($fh) || return;
245 if ( $f->{name} eq "" ) {
248 $file = $ShareNameUM;
250 ($file = $f->{name}) =~ s{.*?([^/]*)$}{$1}; # unmangled file
251 if ( ($dir = $f->{mangleName}) =~ m{(.*)/.*} ) {
252 $dir = "$ShareName/$1";
257 if ( !defined($Attrib{$dir}) ) {
258 foreach my $d ( keys(%Attrib) ) {
259 next if ( $dir =~ m{^\Q$d/} );
262 $Attrib{$dir} = BackupPC::Attrib->new({ compress => $Compress });
263 if ( -f $Attrib{$dir}->fileName("$OutDir/$dir")
264 && !$Attrib{$dir}->read("$OutDir/$dir") ) {
265 printf(STDERR "Unable to read attribute file %s\n",
266 $Attrib{$dir}->fileName("$OutDir/$dir"));
270 if ( $f->{type} == BPC_FTYPE_DIR ) {
274 mkpath("$OutDir/$ShareName/$f->{mangleName}", 0, 0777)
275 if ( !-d "$OutDir/$ShareName/$f->{mangleName}" );
276 } elsif ( $f->{type} == BPC_FTYPE_FILE ) {
281 #print("Reading $f->{name}, $f->{size} bytes, type $f->{type}\n");
282 my $poolWrite = BackupPC::PoolWrite->new($bpc,
283 "$OutDir/$ShareName/$f->{mangleName}",
284 $f->{size}, $Compress);
285 while ( $nRead < $f->{size} ) {
286 my $thisRead = $f->{size} - $nRead < $BufSize
287 ? $f->{size} - $nRead : $BufSize;
288 my $data = TarRead($fh, $thisRead);
290 print(STDERR "Unexpected end of tar archive during read\n");
294 $poolWrite->write(\$data);
297 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
298 TarFlush($fh, $f->{size});
299 } elsif ( $f->{type} == BPC_FTYPE_HARDLINK ) {
301 # Hardlink to another file. GNU tar is clever about files
302 # that are hardlinks to each other. The first link will be
303 # sent as a regular file. The additional links will be sent
304 # as this type. We store the hardlink just like a symlink:
305 # the link name (path of the linked-to file) is stored in
308 $f->{size} = length($f->{linkname});
309 my $poolWrite = BackupPC::PoolWrite->new($bpc,
310 "$OutDir/$ShareName/$f->{mangleName}",
311 $f->{size}, $Compress);
312 $poolWrite->write(\$f->{linkname});
313 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
314 } elsif ( $f->{type} == BPC_FTYPE_SYMLINK ) {
316 # Symbolic link: write the value of the link to a plain file,
317 # that we pool as usual (ie: we don't create a symlink).
318 # The attributes remember the original file type.
319 # We also change the size to reflect the size of the link
322 $f->{size} = length($f->{linkname});
323 my $poolWrite = BackupPC::PoolWrite->new($bpc,
324 "$OutDir/$ShareName/$f->{mangleName}",
325 $f->{size}, $Compress);
326 $poolWrite->write(\$f->{linkname});
327 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
328 } elsif ( $f->{type} == BPC_FTYPE_CHARDEV
329 || $f->{type} == BPC_FTYPE_BLOCKDEV
330 || $f->{type} == BPC_FTYPE_FIFO ) {
332 # Special files: for char and block special we write the
333 # major and minor numbers to a plain file, that we pool
334 # as usual. For a pipe file we create an empty file.
335 # The attributes remember the original file type.
338 if ( $f->{type} == BPC_FTYPE_FIFO ) {
341 $data = "$f->{devmajor},$f->{devminor}";
343 my $poolWrite = BackupPC::PoolWrite->new($bpc,
344 "$OutDir/$ShareName/$f->{mangleName}",
345 length($data), $Compress);
346 $poolWrite->write(\$data);
347 $f->{size} = length($data);
348 processClose($poolWrite, "$ShareName/$f->{mangleName}", length($data));
350 print("Got unknown type $f->{type} for $f->{name}\n");
353 $Attrib{$dir}->set($file, {
359 mtime => $f->{mtime},
369 return if ( !defined($Attrib{$d}) );
370 if ( $Attrib{$d}->fileCount ) {
371 my $data = $Attrib{$d}->writeData;
372 my $fileName = $Attrib{$d}->fileName("$OutDir/$d");
373 my $poolWrite = BackupPC::PoolWrite->new($bpc, $fileName,
374 length($data), $Compress);
375 $poolWrite->write(\$data);
376 processClose($poolWrite, $Attrib{$d}->fileName($d), length($data));
383 my($poolWrite, $fileName, $origSize) = @_;
384 my($exists, $digest, $outSize, $errs) = $poolWrite->close;
387 print(STDERR join("", @$errs));
391 $TotalFileSize += $origSize;
394 $ExistFileSize += $origSize;
395 $ExistFileCompSize += $outSize;
396 } elsif ( $outSize > 0 ) {
397 print(NEW_FILES "$digest $origSize $fileName\n");
401 mkpath("$OutDir/$ShareName", 0, 0777);
402 open(NEW_FILES, ">>", "$TopDir/pc/$host/NewFileList")
403 || die("can't open $TopDir/pc/$host/NewFileList");
406 1 while ( TarReadFile(*STDIN) );
407 1 while ( sysread(STDIN, my $discard, 1024) );
410 # Flush out remaining attributes.
412 foreach my $d ( keys(%Attrib) ) {
418 # Report results to BackupPC_dump
420 print("Done: $Errors errors, $ExistFileCnt filesExist,"
421 . " $ExistFileSize sizeExist, $ExistFileCompSize sizeExistComp,"
422 . " $TotalFileCnt filesTotal, $TotalFileSize sizeTotal\n");