2 #============================================================= -*-perl-*-
4 # BackupPC_tarExtract: extract data from a dump
9 # Craig Barratt <cbarratt@users.sourceforge.net>
12 # Copyright (C) 2001 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.0.0_CVS, released 18 Jan 2003.
32 # See http://backuppc.sourceforge.net.
34 #========================================================================
37 use lib "/usr/local/BackupPC/lib";
39 use BackupPC::Attrib qw(:all);
40 use BackupPC::FileZIO;
41 use BackupPC::PoolWrite;
44 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
45 my $TopDir = $bpc->TopDir();
46 my $BinDir = $bpc->BinDir();
47 my %Conf = $bpc->Conf();
50 print("usage: $0 <host> <shareName> <compressLevel>\n");
53 if ( $ARGV[0] !~ /^([\w\.\s-]+)$/ ) {
54 print("$0: bad host name '$ARGV[0]'\n");
58 if ( $ARGV[1] !~ /^([\w\s\.\/\$-]+)$/ ) {
59 print("$0: bad share name '$ARGV[1]'\n");
63 my $ShareName = $bpc->fileNameEltMangle($ShareNameUM);
64 if ( $ARGV[2] !~ /^(\d+)$/ ) {
65 print("$0: bad compress level '$ARGV[2]'\n");
71 # This constant and the line of code below that uses it is borrowed
72 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
75 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
76 # Copyright 1998 Stephen Zander. All rights reserved.
79 = 'Z100 A8 A8 A8 A12 A12 A8 A1 Z100 A6 A2 Z32 Z32 A8 A8 A155 x12';
80 my $tar_header_length = 512;
82 my $BufSize = 1048576; # 1MB or 2^20
85 my $OutDir = "$TopDir/pc/$host/new";
89 my $ExistFileSize = 0;
90 my $ExistFileCompSize = 0;
92 my $TotalFileSize = 0;
96 my($fh, $totBytes) = @_;
97 my($numBytes, $newBytes, $data);
99 $data = "\0" x $totBytes;
100 while ( $numBytes < $totBytes ) {
101 $newBytes = sysread($fh,
102 substr($data, $numBytes, $totBytes - $numBytes),
103 $totBytes - $numBytes);
104 if ( $newBytes <= 0 ) {
105 print(STDERR "Unexpected end of tar archive (tot = $totBytes,"
106 . " num = $numBytes, posn = " . sysseek($fh, 0, 1) . ")\n");
110 $numBytes += $newBytes;
119 return $1 if ( TarRead($fh, $tar_header_length) =~ /(.*)/s );
127 if ( $size % $tar_header_length ) {
128 TarRead($fh, $tar_header_length - ($size % $tar_header_length));
135 my($head, $longName, $longLink);
136 my($name, $mode, $uid, $gid, $size, $mtime, $chksum, $type,
137 $linkname, $magic, $version, $uname, $gname, $devmajor,
141 $head = TarReadHeader($fh);
142 return if ( $head eq "" || $head eq "\0" x $tar_header_length );
144 $mode, # octal number
147 $size, # octal number
148 $mtime, # octal number
149 $chksum, # octal number
153 $version, # two bytes
156 $devmajor, # octal number
157 $devminor, # octal number
158 $prefix) = unpack($tar_unpack_header, $head);
163 $size =~ s/^6/2/; # fix bug in smbclient for >=2GB files
164 $size =~ s/^7/3/; # fix bug in smbclient for >=2GB files
167 $chksum = oct $chksum;
168 $devmajor = oct $devmajor;
169 $devminor = oct $devminor;
170 $name = "$prefix/$name" if $prefix;
172 substr ($head, 148, 8) = " ";
173 if (unpack ("%16C*", $head) != $chksum) {
174 print(STDERR "$name: checksum error at "
175 . sysseek($fh, 0, 1) , "\n");
178 if ( $type eq "L" ) {
179 $longName = TarRead($fh, $size) || return;
180 # remove trailing NULL
181 $longName = substr($longName, 0, $size - 1);
182 TarFlush($fh, $size);
184 } elsif ( $type eq "K" ) {
185 $longLink = TarRead($fh, $size) || return;
186 # remove trailing NULL
187 $longLink = substr($longLink, 0, $size - 1);
188 TarFlush($fh, $size);
191 $name = $longName if ( defined($longName) );
192 $linkname = $longLink if ( defined($longLink) );
198 mangleName => $bpc->fileNameMangle($name),
205 linkname => $linkname,
206 devmajor => $devmajor,
207 devminor => $devminor,
215 my $f = TarReadFileInfo($fh) || return;
218 if ( $f->{name} eq "" ) {
221 $file = $ShareNameUM;
223 ($file = $f->{name}) =~ s{.*?([^/]*)$}{$1}; # unmangled file
224 if ( ($dir = $f->{mangleName}) =~ m{(.*)/.*} ) {
225 $dir = "$ShareName/$1";
230 if ( !defined($Attrib{$dir}) ) {
231 foreach my $d ( keys(%Attrib) ) {
232 next if ( $dir =~ m{^\Q$d/} );
235 $Attrib{$dir} = BackupPC::Attrib->new({ compress => $Compress });
236 if ( -f $Attrib{$dir}->fileName("$OutDir/$dir")
237 && !$Attrib{$dir}->read("$OutDir/$dir") ) {
238 printf(STDERR "Unable to read attribute file %s\n",
239 $Attrib{$dir}->fileName("$OutDir/$dir"));
243 if ( $f->{type} == BPC_FTYPE_DIR ) {
247 mkpath("$OutDir/$ShareName/$f->{mangleName}", 0, 0777)
248 if ( !-d "$OutDir/$ShareName/$f->{mangleName}" );
249 } elsif ( $f->{type} == BPC_FTYPE_FILE ) {
254 #print("Reading $f->{name}, $f->{size} bytes, type $f->{type}\n");
255 my $poolWrite = BackupPC::PoolWrite->new($bpc,
256 "$OutDir/$ShareName/$f->{mangleName}",
257 $f->{size}, $Compress);
258 while ( $nRead < $f->{size} ) {
259 my $thisRead = $f->{size} - $nRead < $BufSize
260 ? $f->{size} - $nRead : $BufSize;
261 my $data = TarRead($fh, $thisRead);
263 print(STDERR "Unexpected end of tar archive during read\n");
267 $poolWrite->write(\$data);
270 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
271 TarFlush($fh, $f->{size});
272 } elsif ( $f->{type} == BPC_FTYPE_HARDLINK ) {
274 # Hardlink to another file. GNU tar is clever about files
275 # that are hardlinks to each other. The first link will be
276 # sent as a regular file. The additional links will be sent
277 # as this type. We store the hardlink just like a symlink:
278 # the link name (path of the linked-to file) is stored in
281 $f->{size} = length($f->{linkname});
282 my $poolWrite = BackupPC::PoolWrite->new($bpc,
283 "$OutDir/$ShareName/$f->{mangleName}",
284 $f->{size}, $Compress);
285 $poolWrite->write(\$f->{linkname});
286 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
287 } elsif ( $f->{type} == BPC_FTYPE_SYMLINK ) {
289 # Symbolic link: write the value of the link to a plain file,
290 # that we pool as usual (ie: we don't create a symlink).
291 # The attributes remember the original file type.
292 # We also change the size to reflect the size of the link
295 $f->{size} = length($f->{linkname});
296 my $poolWrite = BackupPC::PoolWrite->new($bpc,
297 "$OutDir/$ShareName/$f->{mangleName}",
298 $f->{size}, $Compress);
299 $poolWrite->write(\$f->{linkname});
300 processClose($poolWrite, "$ShareName/$f->{mangleName}", $f->{size});
301 } elsif ( $f->{type} == BPC_FTYPE_CHARDEV
302 || $f->{type} == BPC_FTYPE_BLOCKDEV
303 || $f->{type} == BPC_FTYPE_FIFO ) {
305 # Special files: for char and block special we write the
306 # major and minor numbers to a plain file, that we pool
307 # as usual. For a pipe file we create an empty file.
308 # The attributes remember the original file type.
311 if ( $f->{type} == BPC_FTYPE_FIFO ) {
314 $data = "$f->{devmajor},$f->{devminor}";
316 my $poolWrite = BackupPC::PoolWrite->new($bpc,
317 "$OutDir/$ShareName/$f->{mangleName}",
318 length($data), $Compress);
319 $poolWrite->write(\$data);
320 $f->{size} = length($data);
321 processClose($poolWrite, "$ShareName/$f->{mangleName}", length($data));
323 print("Got unknown type $f->{type} for $f->{name}\n");
326 $Attrib{$dir}->set($file, {
332 mtime => $f->{mtime},
342 return if ( !defined($Attrib{$d}) );
343 if ( $Attrib{$d}->fileCount ) {
344 my $data = $Attrib{$d}->writeData;
345 my $fileName = $Attrib{$d}->fileName("$OutDir/$d");
346 my $poolWrite = BackupPC::PoolWrite->new($bpc, $fileName,
347 length($data), $Compress);
348 $poolWrite->write(\$data);
349 processClose($poolWrite, $Attrib{$d}->fileName($d), length($data));
356 my($poolWrite, $fileName, $origSize) = @_;
357 my($exists, $digest, $outSize, $errs) = $poolWrite->close;
360 print(STDERR join("", @$errs));
364 $TotalFileSize += $origSize;
367 $ExistFileSize += $origSize;
368 $ExistFileCompSize += $outSize;
369 } elsif ( $outSize > 0 ) {
370 print(NEW_FILES "$digest $origSize $fileName\n");
374 mkpath("$OutDir/$ShareName", 0, 0777);
375 open(NEW_FILES, ">>", "$TopDir/pc/$host/NewFileList")
376 || die("can't open $TopDir/pc/$host/NewFileList");
377 1 while ( TarReadFile(*STDIN) );
378 1 while ( sysread(STDIN, my $discard, 1024) );
381 # Flush out remaining attributes.
383 foreach my $d ( keys(%Attrib) ) {
389 # Report results to BackupPC_dump
391 print("Done: $Errors errors, $ExistFileCnt filesExist,"
392 . " $ExistFileSize sizeExist, $ExistFileCompSize sizeExistComp,"
393 . " $TotalFileCnt filesTotal, $TotalFileSize sizeTotal\n");