--- trunk/bin/BackupPC_tarIncCreate 2005/11/10 15:07:10 235 +++ trunk/bin/BackupPC_tarIncCreate 2005/11/10 15:38:14 236 @@ -109,14 +109,12 @@ } if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) { - print(STDERR "$0: bad host name '$opts{h}'\n"); - exit(1); + die "$0: bad host name '$opts{h}'\n"; } my $Host = $opts{h}; if ( $opts{n} !~ /^(-?\d+)$/ ) { - print(STDERR "$0: bad dump number '$opts{n}'\n"); - exit(1); + die "$0: bad dump number '$opts{n}'\n"; } my $Num = $opts{n}; @@ -132,6 +130,7 @@ my $SpecialCnt = 0; my $ErrorCnt = 0; my $current_tar_size = 0; +my $total_increment_size = 0; my $i; $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 ); @@ -139,15 +138,13 @@ last if ( $Backups[$i]{num} == $Num ); } if ( $i >= @Backups ) { - print(STDERR "$0: bad backup number $Num for host $Host\n"); - exit(1); + die "$0: bad backup number $Num for host $Host\n"; } my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ ); my $PathAdd = $1 if ( $opts{p} =~ /(.+)/ ); if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) { - print(STDERR "$0: bad share name '$opts{s}'\n"); - exit(1); + die "$0: bad share name '$opts{s}'\n"; } our $ShareName = $opts{s}; our $view = BackupPC::View->new($bpc, $Host, \@Backups); @@ -231,12 +228,17 @@ my $items_in_part = 0; sub new_tar_part { + my $arg = {@_}; + if ($fh) { return if ($current_tar_size == 0); print STDERR "# closing part $part\n" if ($opts{d}); - # finish tar archive + # + # Finish with two null 512 byte headers, + # and then round out a full block. + # my $data = "\0" x ($tar_header_length * 2); TarWrite($fh, \$data); TarWrite($fh, undef); @@ -259,6 +261,18 @@ $items_in_part, ); + $total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024; + + if ($arg->{close}) { + print STDERR "# close last part\n" if ($opts{d}); + $sth_inc_size->execute( + $total_increment_size, + $part, + $backup_id + ); + return; + } + } $part++; @@ -271,6 +285,18 @@ rmtree($tar_path); } mkdir($tar_path) || die "can't create directory $tar_path: $!"; + + sub abort_cleanup { + print STDERR "ABORTED: cleanup temp dir"; + rmtree($tar_path); + $dbh->rollback; + exit 1; + } + + $SIG{'INT'} = \&abort_cleanup; + $SIG{'QUIT'} = \&abort_cleanup; + $SIG{'__DIE__'} = \&abort_cleanup; + } my $file = $tar_path . '/' . $part; @@ -304,18 +330,7 @@ $no_files = 1; } -# -# Finish with two null 512 byte headers, and then round out a full -# block. -# -my $data = "\0" x ($tar_header_length * 2); -TarWrite($fh, \$data); -TarWrite($fh, undef); - -if (! close($fh)) { - rmtree($tar_path); - die "can't close archive\n"; -} +new_tar_part( close => 1 ); # remove temporary files if there are no files if ($no_files) { @@ -336,8 +351,7 @@ # Got errors, with no files or directories; exit with non-zero # status # - cleanup(); - exit(1); + die "got errors or no files\n"; } $sth_inc_size->finish; @@ -346,7 +360,7 @@ $dbh->commit || die "can't commit changes to database"; $dbh->disconnect(); -exit(0); +exit; ########################################################################### # Subroutines @@ -433,14 +447,12 @@ my $done = $WriteBufSz - length($WriteBuf); if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done)) != $WriteBufSz ) { - print(STDERR "Unable to write to output file ($!)\n"); - exit(1); + die "Unable to write to output file ($!)\n"; } while ( $done + $WriteBufSz <= length($$dataRef) ) { if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz)) != $WriteBufSz ) { - print(STDERR "Unable to write to output file ($!)\n"); - exit(1); + die "Unable to write to output file ($!)\n"; } $done += $WriteBufSz; }