/[BackupPC]/trunk/bin/BackupPC_tarIncCreate
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bin/BackupPC_tarIncCreate

Parent Directory Parent Directory | Revision Log Revision Log


Revision 251 - (show annotations)
Mon Dec 12 12:16:01 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 22988 byte(s)
 r11634@llin:  dpavlin | 2005-12-12 14:15:47 +0100
 rename finished increment to final name, better output for -v

1 #!/usr/bin/perl -w
2 #============================================================= -*-perl-*-
3 #
4 # BackupPC_tarIncCreate: create a tar archive of an existing incremental dump
5 #
6 #
7 # DESCRIPTION
8 #
9 # Usage: BackupPC_tarIncCreate [options]
10 #
11 # Flags:
12 # Required options:
13 #
14 # -h host Host from which the tar archive is created.
15 # -n dumpNum Dump number from which the tar archive is created.
16 # A negative number means relative to the end (eg -1
17 # means the most recent dump, -2 2nd most recent etc).
18 # -s shareName Share name from which the tar archive is created.
19 #
20 # Other options:
21 # -t print summary totals
22 # -r pathRemove path prefix that will be replaced with pathAdd
23 # -p pathAdd new path prefix
24 # -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
25 # -w writeBufSz write buffer size (default 1MB)
26 #
27 # The -h, -n and -s options specify which dump is used to generate
28 # the tar archive. The -r and -p options can be used to relocate
29 # the paths in the tar archive so extracted files can be placed
30 # in a location different from their original location.
31 #
32 # AUTHOR
33 # Craig Barratt <cbarratt@users.sourceforge.net>
34 # Ivan Klaric <iklaric@gmail.com>
35 # Dobrica Pavlinusic <dpavlin@rot13.org>
36 #
37 # COPYRIGHT
38 # Copyright (C) 2001-2003 Craig Barratt
39 #
40 # This program is free software; you can redistribute it and/or modify
41 # it under the terms of the GNU General Public License as published by
42 # the Free Software Foundation; either version 2 of the License, or
43 # (at your option) any later version.
44 #
45 # This program is distributed in the hope that it will be useful,
46 # but WITHOUT ANY WARRANTY; without even the implied warranty of
47 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 # GNU General Public License for more details.
49 #
50 # You should have received a copy of the GNU General Public License
51 # along with this program; if not, write to the Free Software
52 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 #
54 #========================================================================
55 #
56 # Version 2.1.0, released 20 Jun 2004.
57 #
58 # See http://backuppc.sourceforge.net.
59 #
60 #========================================================================
61
62 use strict;
63 no utf8;
64 use lib "__INSTALLDIR__/lib";
65 use File::Path;
66 use Getopt::Std;
67 use DBI;
68 use BackupPC::Lib;
69 use BackupPC::Attrib qw(:all);
70 use BackupPC::FileZIO;
71 use BackupPC::View;
72 use BackupPC::SearchLib;
73 use Time::HiRes qw/time/;
74 use POSIX qw/strftime/;
75 use File::Which;
76 use File::Path;
77 use File::Slurp;
78 use Data::Dumper; ### FIXME
79
80 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
81 my $TopDir = $bpc->TopDir();
82 my $BinDir = $bpc->BinDir();
83 my %Conf = $bpc->Conf();
84 %BackupPC::SearchLib::Conf = %Conf;
85 my %opts;
86 my $in_backup_increment;
87
88
89 if ( !getopts("th:n:p:r:s:b:w:vd", \%opts) ) {
90 print STDERR <<EOF;
91 usage: $0 [options]
92 Required options:
93 -h host host from which the tar archive is created
94 -n dumpNum dump number from which the tar archive is created
95 A negative number means relative to the end (eg -1
96 means the most recent dump, -2 2nd most recent etc).
97 -s shareName share name from which the tar archive is created
98
99 Other options:
100 -t print summary totals
101 -r pathRemove path prefix that will be replaced with pathAdd
102 -p pathAdd new path prefix
103 -b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar)
104 -w writeBufSz write buffer size (default 1048576 = 1MB)
105 -v verbose output
106 -d debug output
107 EOF
108 exit(1);
109 }
110
111 if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
112 die "$0: bad host name '$opts{h}'\n";
113 }
114 my $Host = $opts{h};
115
116 if ( $opts{n} !~ /^(-?\d+)$/ ) {
117 die "$0: bad dump number '$opts{n}'\n";
118 }
119 my $Num = $opts{n};
120
121 my $bin;
122 foreach my $c (qw/gzip md5sum tee/) {
123 $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
124 }
125
126 my @Backups = $bpc->BackupInfoRead($Host);
127 my $FileCnt = 0;
128 my $ByteCnt = 0;
129 my $DirCnt = 0;
130 my $SpecialCnt = 0;
131 my $ErrorCnt = 0;
132 my $current_tar_size = 0;
133 my $total_increment_size = 0;
134
135 my $i;
136 $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
137 for ( $i = 0 ; $i < @Backups ; $i++ ) {
138 last if ( $Backups[$i]{num} == $Num );
139 }
140 if ( $i >= @Backups ) {
141 die "$0: bad backup number $Num for host $Host\n";
142 }
143
144 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
145 my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ );
146 if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
147 die "$0: bad share name '$opts{s}'\n";
148 }
149 our $ShareName = $opts{s};
150 our $view = BackupPC::View->new($bpc, $Host, \@Backups);
151
152 # database
153
154 my $dsn = $Conf{SearchDSN};
155 my $db_user = $Conf{SearchUser} || '';
156
157 my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} );
158
159 my $sth_inc_size = $dbh->prepare(qq{
160 update backups set
161 inc_size = ?,
162 parts = ?,
163 inc_deleted = false
164 where id = ?
165 });
166 my $sth_backup_parts = $dbh->prepare(qq{
167 insert into backup_parts (
168 backup_id,
169 part_nr,
170 tar_size,
171 size,
172 md5,
173 items
174 ) values (?,?,?,?,?,?)
175 });
176
177 #
178 # This constant and the line of code below that uses it are borrowed
179 # from Archive::Tar. Thanks to Calle Dybedahl and Stephen Zander.
180 # See www.cpan.org.
181 #
182 # Archive::Tar is Copyright 1997 Calle Dybedahl. All rights reserved.
183 # Copyright 1998 Stephen Zander. All rights reserved.
184 #
185 my $tar_pack_header
186 = 'a100 a8 a8 a8 a12 a12 A8 a1 a100 a6 a2 a32 a32 a8 a8 a155 x12';
187 my $tar_header_length = 512;
188
189 my $BufSize = $opts{w} || 1048576; # 1MB or 2^20
190 my $WriteBuf = "";
191 my $WriteBufSz = ($opts{b} || 20) * $tar_header_length;
192
193 my(%UidCache, %GidCache);
194 my(%HardLinkExtraFiles, @HardLinks);
195
196 #
197 # Write out all the requested files/directories
198 #
199
200 my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar";
201 $max_file_size *= 1024;
202
203 my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
204 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
205
206 my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)";
207
208 my $tar_path_final = $tar_dir . '/' . $tar_file;
209 my $tar_path = $tar_path_final . '.tmp';
210
211 $tar_path =~ s#//#/#g;
212
213 my $sth = $dbh->prepare(qq{
214 SELECT
215 backups.id
216 FROM backups
217 JOIN shares on shares.id = shareid
218 JOIN hosts on hosts.id = shares.hostid
219 WHERE hosts.name = ? and shares.name = ? and backups.num = ?
220 });
221 $sth->execute($Host, $ShareName, $Num);
222 my ($backup_id) = $sth->fetchrow_array;
223 $sth->finish;
224
225 print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d});
226
227
228 my $fh;
229 my $part = 0;
230 my $no_files = 0;
231 my $items_in_part = 0;
232
233 sub new_tar_part {
234 my $arg = {@_};
235
236 if ($fh) {
237 return if ($current_tar_size == 0);
238
239 print STDERR " $part" if ($opts{v});
240
241 #
242 # Finish with two null 512 byte headers,
243 # and then round out a full block.
244 #
245 my $data = "\0" x ($tar_header_length * 2);
246 TarWrite($fh, \$data);
247 TarWrite($fh, undef);
248
249 close($fh) || die "can't close archive part $part: $!";
250
251 my $file = $tar_path . '/' . $part;
252
253 my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5";
254 $md5 =~ s/\s.*$//;
255
256 my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz";
257
258 $sth_backup_parts->execute(
259 $backup_id,
260 $part,
261 $current_tar_size,
262 $size,
263 $md5,
264 $items_in_part,
265 );
266
267 $total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024;
268
269 if ($arg->{close}) {
270
271 if ($part == 1) {
272 print STDERR " single";
273 rename "${tar_path}/1.tar.gz", "${tar_path_final}/.tar.gz" || die "can't move tar ${tar_path}: $! ";
274 rename "${tar_path}/1.md5", "${tar_path_final}/.md5" || die "can't move md5 ${tar_path}: $! ";
275 } else {
276 print STDERR " [last]";
277 rename "${tar_path}", "${tar_path_final}" || die "can't move tar ${tar_path}: $! ";
278 }
279
280 $sth_inc_size->execute(
281 $total_increment_size,
282 $part,
283 $backup_id
284 );
285
286 print STDERR ", $total_increment_size bytes\n" if ($opts{v});
287
288 return;
289 }
290
291 }
292
293 $part++;
294
295 # if this is first part, create directory
296
297 if ($part == 1) {
298 if (-d $tar_path) {
299 print STDERR "# deleting existing $tar_path\n" if ($opts{d});
300 rmtree($tar_path);
301 }
302 mkdir($tar_path) || die "can't create directory $tar_path: $!";
303
304 sub abort_cleanup {
305 print STDERR "ABORTED: cleanup temp dir";
306 rmtree($tar_path);
307 $dbh->rollback;
308 exit 1;
309 }
310
311 $SIG{'INT'} = \&abort_cleanup;
312 $SIG{'QUIT'} = \&abort_cleanup;
313 $SIG{'__DIE__'} = \&abort_cleanup;
314
315 }
316
317 my $file = $tar_path . '/' . $part;
318
319 #
320 # create comprex pipe which will pass output through gzip
321 # for compression, create file on disk using tee
322 # and pipe same output to md5sum to create checksum
323 #
324
325 my $cmd = '| ' . $bin->{'gzip'} . ' ' . $Conf{GzipLevel} . ' ' .
326 '| ' . $bin->{'tee'} . ' ' . $file . '.tar.gz' . ' ' .
327 '| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5';
328
329 print STDERR "## $cmd\n" if ($opts{d});
330
331 open($fh, $cmd) or die "can't open $cmd: $!";
332 binmode($fh);
333
334 $current_tar_size = 0;
335 $items_in_part = 0;
336 }
337
338 new_tar_part();
339
340 if (seedCache($Host, $ShareName, $Num)) {
341 archiveWrite($fh, '/');
342 archiveWriteHardLinks($fh);
343 } else {
344 print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v});
345 $no_files = 1;
346 }
347
348 new_tar_part( close => 1 );
349
350 # remove temporary files if there are no files
351 if ($no_files) {
352 rmtree($tar_path);
353 } elsif ($part == 1) {
354 warn "FIXME: if there is only one part move to parent directory and rename";
355 }
356
357 #
358 # print out totals if requested
359 #
360 if ( $opts{t} ) {
361 print STDERR "Done: $FileCnt files, $ByteCnt bytes, $DirCnt dirs,",
362 " $SpecialCnt specials, $ErrorCnt errors\n";
363 }
364 if ( $ErrorCnt && !$FileCnt && !$DirCnt ) {
365 #
366 # Got errors, with no files or directories; exit with non-zero
367 # status
368 #
369 die "got errors or no files\n";
370 }
371
372 $sth_inc_size->finish;
373 $sth_backup_parts->finish;
374
375 $dbh->commit || die "can't commit changes to database";
376 $dbh->disconnect();
377
378 exit;
379
380 ###########################################################################
381 # Subroutines
382 ###########################################################################
383
384 sub archiveWrite
385 {
386 my($fh, $dir, $tarPathOverride) = @_;
387
388 if ( $dir =~ m{(^|/)\.\.(/|$)} ) {
389 print(STDERR "$0: bad directory '$dir'\n");
390 $ErrorCnt++;
391 return;
392 }
393 $dir = "/" if ( $dir eq "." );
394 #print(STDERR "calling find with $Num, $ShareName, $dir\n");
395
396 if ( $view->find($Num, $ShareName, $dir, 0, \&TarWriteFile,
397 $fh, $tarPathOverride) < 0 ) {
398 print(STDERR "$0: bad share or directory '$ShareName/$dir'\n");
399 $ErrorCnt++;
400 return;
401 }
402 }
403
404 #
405 # Write out any hardlinks (if any)
406 #
407 sub archiveWriteHardLinks
408 {
409 my $fh = @_;
410 foreach my $hdr ( @HardLinks ) {
411 $hdr->{size} = 0;
412 if ( defined($PathRemove)
413 && substr($hdr->{linkname}, 0, length($PathRemove)+1)
414 eq ".$PathRemove" ) {
415 substr($hdr->{linkname}, 0, length($PathRemove)+1) = ".$PathAdd";
416 }
417 TarWriteFileInfo($fh, $hdr);
418 }
419 @HardLinks = ();
420 %HardLinkExtraFiles = ();
421 }
422
423 sub UidLookup
424 {
425 my($uid) = @_;
426
427 $UidCache{$uid} = (getpwuid($uid))[0] if ( !exists($UidCache{$uid}) );
428 return $UidCache{$uid};
429 }
430
431 sub GidLookup
432 {
433 my($gid) = @_;
434
435 $GidCache{$gid} = (getgrgid($gid))[0] if ( !exists($GidCache{$gid}) );
436 return $GidCache{$gid};
437 }
438
439 sub TarWrite
440 {
441 my($fh, $dataRef) = @_;
442
443
444 if ( !defined($dataRef) ) {
445 #
446 # do flush by padding to a full $WriteBufSz
447 #
448 my $data = "\0" x ($WriteBufSz - length($WriteBuf));
449 $dataRef = \$data;
450 }
451
452 # poor man's tell :-)
453 $current_tar_size += length($$dataRef);
454
455 if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
456 #
457 # just buffer and return
458 #
459 $WriteBuf .= $$dataRef;
460 return;
461 }
462 my $done = $WriteBufSz - length($WriteBuf);
463 if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
464 != $WriteBufSz ) {
465 die "Unable to write to output file ($!)\n";
466 }
467 while ( $done + $WriteBufSz <= length($$dataRef) ) {
468 if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
469 != $WriteBufSz ) {
470 die "Unable to write to output file ($!)\n";
471 }
472 $done += $WriteBufSz;
473 }
474 $WriteBuf = substr($$dataRef, $done);
475 }
476
477 sub TarWritePad
478 {
479 my($fh, $size) = @_;
480
481 if ( $size % $tar_header_length ) {
482 my $data = "\0" x ($tar_header_length - ($size % $tar_header_length));
483 TarWrite($fh, \$data);
484 }
485 }
486
487 sub TarWriteHeader
488 {
489 my($fh, $hdr) = @_;
490
491 $hdr->{uname} = UidLookup($hdr->{uid}) if ( !defined($hdr->{uname}) );
492 $hdr->{gname} = GidLookup($hdr->{gid}) if ( !defined($hdr->{gname}) );
493 my $devmajor = defined($hdr->{devmajor}) ? sprintf("%07o", $hdr->{devmajor})
494 : "";
495 my $devminor = defined($hdr->{devminor}) ? sprintf("%07o", $hdr->{devminor})
496 : "";
497 my $sizeStr;
498 if ( $hdr->{size} >= 2 * 65536 * 65536 ) {
499 #
500 # GNU extension for files >= 8GB: send size in big-endian binary
501 #
502 $sizeStr = pack("c4 N N", 0x80, 0, 0, 0,
503 $hdr->{size} / (65536 * 65536),
504 $hdr->{size} % (65536 * 65536));
505 } elsif ( $hdr->{size} >= 1 * 65536 * 65536 ) {
506 #
507 # sprintf octal only handles up to 2^32 - 1
508 #
509 $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
510 . sprintf("%08o", $hdr->{size} % (1 << 24));
511 } else {
512 $sizeStr = sprintf("%011o", $hdr->{size});
513 }
514 my $data = pack($tar_pack_header,
515 substr($hdr->{name}, 0, 99),
516 sprintf("%07o", $hdr->{mode}),
517 sprintf("%07o", $hdr->{uid}),
518 sprintf("%07o", $hdr->{gid}),
519 $sizeStr,
520 sprintf("%011o", $hdr->{mtime}),
521 "", #checksum field - space padded by pack("A8")
522 $hdr->{type},
523 substr($hdr->{linkname}, 0, 99),
524 $hdr->{magic} || 'ustar ',
525 $hdr->{version} || ' ',
526 $hdr->{uname},
527 $hdr->{gname},
528 $devmajor,
529 $devminor,
530 "" # prefix is empty
531 );
532 substr($data, 148, 7) = sprintf("%06o\0", unpack("%16C*",$data));
533 TarWrite($fh, \$data);
534 }
535
536 sub TarWriteFileInfo
537 {
538 my($fh, $hdr) = @_;
539
540 #
541 # Handle long link names (symbolic links)
542 #
543 if ( length($hdr->{linkname}) > 99 ) {
544 my %h;
545 my $data = $hdr->{linkname} . "\0";
546 $h{name} = "././\@LongLink";
547 $h{type} = "K";
548 $h{size} = length($data);
549 TarWriteHeader($fh, \%h);
550 TarWrite($fh, \$data);
551 TarWritePad($fh, length($data));
552 }
553 #
554 # Handle long file names
555 #
556 if ( length($hdr->{name}) > 99 ) {
557 my %h;
558 my $data = $hdr->{name} . "\0";
559 $h{name} = "././\@LongLink";
560 $h{type} = "L";
561 $h{size} = length($data);
562 TarWriteHeader($fh, \%h);
563 TarWrite($fh, \$data);
564 TarWritePad($fh, length($data));
565 }
566 TarWriteHeader($fh, $hdr);
567 }
568
569 #
570 # seed cache of files in this increment
571 #
572 sub seedCache($$$) {
573 my ($host, $share, $dumpNo) = @_;
574
575 print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v});
576 my $sql = q{
577 SELECT path,size
578 FROM files
579 JOIN shares on shares.id = shareid
580 JOIN hosts on hosts.id = shares.hostid
581 WHERE hosts.name = ? and shares.name = ? and backupnum = ?
582 };
583
584 my $sth = $dbh->prepare($sql);
585 $sth->execute($host, $share, $dumpNo);
586 my $count = $sth->rows;
587 print STDERR " $count items, parts:" if ($opts{v});
588 while (my $row = $sth->fetchrow_arrayref) {
589 #print STDERR "+ ", $row->[0],"\n";
590 $in_backup_increment->{ $row->[0] } = $row->[1];
591 }
592
593 $sth->finish();
594
595 return $count;
596 }
597
598 #
599 # calculate overhad for one file in tar
600 #
601 sub tar_overhead($) {
602 my $name = shift || '';
603
604 # header, padding of file and two null blocks at end
605 my $len = 4 * $tar_header_length;
606
607 # if filename is longer than 99 chars subtract blocks for
608 # long filename
609 if ( length($name) > 99 ) {
610 $len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length;
611 }
612
613 return $len;
614 }
615
616 my $Attr;
617 my $AttrDir;
618
619 sub TarWriteFile
620 {
621 my($hdr, $fh, $tarPathOverride) = @_;
622
623 my $tarPath = $hdr->{relPath};
624 $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
625
626 $tarPath =~ s{//+}{/}g;
627
628 #print STDERR "? $tarPath\n" if ($opts{d});
629 my $size = $in_backup_increment->{$tarPath};
630 return unless (defined($size));
631
632 # is this file too large to fit into MaxArchiveFileSize?
633
634 if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) {
635 print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d});
636 new_tar_part();
637 }
638
639 #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d});
640 $items_in_part++;
641
642 if ( defined($PathRemove)
643 && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
644 substr($tarPath, 0, length($PathRemove)) = $PathAdd;
645 }
646 $tarPath = "./" . $tarPath if ( $tarPath !~ /^\.\// );
647 $tarPath =~ s{//+}{/}g;
648 $hdr->{name} = $tarPath;
649
650 if ( $hdr->{type} == BPC_FTYPE_DIR ) {
651 #
652 # Directory: just write the header
653 #
654 $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
655 TarWriteFileInfo($fh, $hdr);
656 $DirCnt++;
657 } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
658 #
659 # Regular file: write the header and file
660 #
661 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
662 if ( !defined($f) ) {
663 print(STDERR "Unable to open file $hdr->{fullPath}\n");
664 $ErrorCnt++;
665 return;
666 }
667 # do we need to split file?
668 if ($hdr->{size} < $max_file_size) {
669 TarWriteFileInfo($fh, $hdr);
670 my($data, $size);
671 while ( $f->read(\$data, $BufSize) > 0 ) {
672 TarWrite($fh, \$data);
673 $size += length($data);
674 }
675 $f->close;
676 TarWritePad($fh, $size);
677 $FileCnt++;
678 $ByteCnt += $size;
679 } else {
680 my $full_size = $hdr->{size};
681 my $orig_name = $hdr->{name};
682 my $max_part_size = $max_file_size - tar_overhead($hdr->{name});
683
684 my $parts = int(($full_size + $max_part_size - 1) / $max_part_size);
685 print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d});
686 foreach my $subpart ( 1 .. $parts ) {
687 new_tar_part();
688 if ($subpart < $parts) {
689 $hdr->{size} = $max_part_size;
690 } else {
691 $hdr->{size} = $full_size % $max_part_size;
692 }
693 $hdr->{name} = $orig_name . '/' . $subpart;
694 print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n";
695
696 TarWriteFileInfo($fh, $hdr);
697 my($data, $size);
698 if (0) {
699 for ( 1 .. int($hdr->{size} / $BufSize) ) {
700 my $r_size = $f->read(\$data, $BufSize);
701 die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize);
702 TarWrite($fh, \$data);
703 $size += length($data);
704 }
705 }
706 my $size_left = $hdr->{size} % $BufSize;
707 my $r_size = $f->read(\$data, $size_left);
708 die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left);
709
710 TarWrite($fh, \$data);
711 $size += length($data);
712 TarWritePad($fh, $size);
713
714 $items_in_part++;
715 }
716 $f->close;
717 $FileCnt++;
718 $ByteCnt += $full_size;
719 new_tar_part();
720 }
721 } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
722 #
723 # Hardlink file: either write a hardlink or the complete file
724 # depending upon whether the linked-to file will be written
725 # to the archive.
726 #
727 # Start by reading the contents of the link.
728 #
729 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
730 if ( !defined($f) ) {
731 print(STDERR "Unable to open file $hdr->{fullPath}\n");
732 $ErrorCnt++;
733 return;
734 }
735 my $data;
736 while ( $f->read(\$data, $BufSize) > 0 ) {
737 $hdr->{linkname} .= $data;
738 }
739 $f->close;
740 my $done = 0;
741 my $name = $hdr->{linkname};
742 $name =~ s{^\./}{/};
743 if ( $HardLinkExtraFiles{$name} ) {
744 #
745 # Target file will be or was written, so just remember
746 # the hardlink so we can dump it later.
747 #
748 push(@HardLinks, $hdr);
749 $SpecialCnt++;
750 } else {
751 #
752 # Have to dump the original file. Just call the top-level
753 # routine, so that we save the hassle of dealing with
754 # mangling, merging and attributes.
755 #
756 $HardLinkExtraFiles{$hdr->{linkname}} = 1;
757 archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
758 }
759 } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
760 #
761 # Symbolic link: read the symbolic link contents into the header
762 # and write the header.
763 #
764 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
765 if ( !defined($f) ) {
766 print(STDERR "Unable to open symlink file $hdr->{fullPath}\n");
767 $ErrorCnt++;
768 return;
769 }
770 my $data;
771 while ( $f->read(\$data, $BufSize) > 0 ) {
772 $hdr->{linkname} .= $data;
773 }
774 $f->close;
775 $hdr->{size} = 0;
776 TarWriteFileInfo($fh, $hdr);
777 $SpecialCnt++;
778 } elsif ( $hdr->{type} == BPC_FTYPE_CHARDEV
779 || $hdr->{type} == BPC_FTYPE_BLOCKDEV
780 || $hdr->{type} == BPC_FTYPE_FIFO ) {
781 #
782 # Special files: for char and block special we read the
783 # major and minor numbers from a plain file.
784 #
785 if ( $hdr->{type} != BPC_FTYPE_FIFO ) {
786 my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0,
787 $hdr->{compress});
788 my $data;
789 if ( !defined($f) || $f->read(\$data, $BufSize) < 0 ) {
790 print(STDERR "Unable to open/read char/block special file"
791 . " $hdr->{fullPath}\n");
792 $f->close if ( defined($f) );
793 $ErrorCnt++;
794 return;
795 }
796 $f->close;
797 if ( $data =~ /(\d+),(\d+)/ ) {
798 $hdr->{devmajor} = $1;
799 $hdr->{devminor} = $2;
800 }
801 }
802 $hdr->{size} = 0;
803 TarWriteFileInfo($fh, $hdr);
804 $SpecialCnt++;
805 } else {
806 print(STDERR "Got unknown type $hdr->{type} for $hdr->{name}\n");
807 $ErrorCnt++;
808 }
809 }
810
811 my $t_fmt = '%Y-%m-%d %H:%M:%S';
812 sub curr_time {
813 return strftime($t_fmt,localtime());
814 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26