/[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

Diff of /trunk/bin/BackupPC_tarIncCreate

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 100 by dpavlin, Tue Aug 30 20:29:26 2005 UTC revision 253 by dpavlin, Mon Dec 12 13:41:08 2005 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl -w
2  #============================================================= -*-perl-*-  #============================================================= -*-perl-*-
3  #  #
4  # BackupPC_tarCreate: create a tar archive of an existing dump  # BackupPC_tarIncCreate: create a tar archive of an existing incremental dump
5  # for restore on a client.  #
6  #  #
7  # DESCRIPTION  # DESCRIPTION
8  #    #  
9  #   Usage: BackupPC_tarCreate [options] files/directories...  #   Usage: BackupPC_tarIncCreate [options]
10  #  #
11  #   Flags:  #   Flags:
12  #     Required options:  #     Required options:
# Line 31  Line 31 
31  #  #
32  # AUTHOR  # AUTHOR
33  #   Craig Barratt  <cbarratt@users.sourceforge.net>  #   Craig Barratt  <cbarratt@users.sourceforge.net>
34    #   Ivan Klaric <iklaric@gmail.com>
35    #   Dobrica Pavlinusic <dpavlin@rot13.org>
36  #  #
37  # COPYRIGHT  # COPYRIGHT
38  #   Copyright (C) 2001-2003  Craig Barratt  #   Copyright (C) 2001-2003  Craig Barratt
# Line 68  use BackupPC::Attrib qw(:all); Line 70  use BackupPC::Attrib qw(:all);
70  use BackupPC::FileZIO;  use BackupPC::FileZIO;
71  use BackupPC::View;  use BackupPC::View;
72  use BackupPC::SearchLib;  use BackupPC::SearchLib;
73  use Data::Dumper;  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) );  die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
81  my $TopDir = $bpc->TopDir();  my $TopDir = $bpc->TopDir();
82  my $BinDir = $bpc->BinDir();  my $BinDir = $bpc->BinDir();
83  my %Conf   = $bpc->Conf();  my %Conf   = $bpc->Conf();
84  my @DBCache;  %BackupPC::SearchLib::Conf = %Conf;
 my $db_done = 0;  
85  my %opts;  my %opts;
86    my $in_backup_increment;
87    
88  if ( !getopts("th:n:p:r:s:b:w:", \%opts) || @ARGV < 1 ) {  
89    if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) {
90      print STDERR <<EOF;      print STDERR <<EOF;
91  usage: $0 [options] files/directories...  usage: $0 [options]
92    Required options:    Required options:
93       -h host         host from which the tar archive is created       -h host         host from which the tar archive is created
94       -n dumpNum      dump number from which the tar archive is created       -n dumpNum      dump number from which the tar archive is created
# Line 94  usage: $0 [options] files/directories... Line 102  usage: $0 [options] files/directories...
102       -p pathAdd      new path prefix       -p pathAdd      new path prefix
103       -b BLOCKS       BLOCKS x 512 bytes per record (default 20; same as tar)       -b BLOCKS       BLOCKS x 512 bytes per record (default 20; same as tar)
104       -w writeBufSz   write buffer size (default 1048576 = 1MB)       -w writeBufSz   write buffer size (default 1048576 = 1MB)
105         -f              overwrite existing parts
106         -v              verbose output
107         -d              debug output
108  EOF  EOF
109      exit(1);      exit(1);
110  }  }
111    
112  if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {  if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
113      print(STDERR "$0: bad host name '$opts{h}'\n");      die "$0: bad host name '$opts{h}'\n";
     exit(1);  
114  }  }
115  my $Host = $opts{h};  my $Host = $opts{h};
116    
117  if ( $opts{n} !~ /^(-?\d+)$/ ) {  if ( $opts{n} !~ /^(-?\d+)$/ ) {
118      print(STDERR "$0: bad dump number '$opts{n}'\n");      die "$0: bad dump number '$opts{n}'\n";
     exit(1);  
119  }  }
120  my $Num = $opts{n};  my $Num = $opts{n};
121    
122    my $bin;
123    foreach my $c (qw/gzip md5sum tee/) {
124            $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
125    }
126    
127  my @Backups = $bpc->BackupInfoRead($Host);  my @Backups = $bpc->BackupInfoRead($Host);
128  my $FileCnt = 0;  my $FileCnt = 0;
129  my $ByteCnt = 0;  my $ByteCnt = 0;
130  my $DirCnt = 0;  my $DirCnt = 0;
131  my $SpecialCnt = 0;  my $SpecialCnt = 0;
132  my $ErrorCnt = 0;  my $ErrorCnt = 0;
133    my $current_tar_size = 0;
134    my $total_increment_size = 0;
135    
136  my $i;  my $i;
137  $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );  $Num = $Backups[@Backups + $Num]{num} if ( -@Backups <= $Num && $Num < 0 );
# Line 123  for ( $i = 0 ; $i < @Backups ; $i++ ) { Line 139  for ( $i = 0 ; $i < @Backups ; $i++ ) {
139      last if ( $Backups[$i]{num} == $Num );      last if ( $Backups[$i]{num} == $Num );
140  }  }
141  if ( $i >= @Backups ) {  if ( $i >= @Backups ) {
142      print(STDERR "$0: bad backup number $Num for host $Host\n");      die "$0: bad backup number $Num for host $Host\n";
     exit(1);  
143  }  }
144    
145  my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );  my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
146  my $PathAdd    = $1 if ( $opts{p} =~ /(.+)/ );  my $PathAdd    = $1 if ( $opts{p} =~ /(.+)/ );
147  if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {  if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
148      print(STDERR "$0: bad share name '$opts{s}'\n");      die "$0: bad share name '$opts{s}'\n";
     exit(1);  
149  }  }
150  our $ShareName = $opts{s};  our $ShareName = $opts{s};
151  our $view = BackupPC::View->new($bpc, $Host, \@Backups);  our $view = BackupPC::View->new($bpc, $Host, \@Backups);
152    
153    # database
154    
155    my $dsn = $Conf{SearchDSN};
156    my $db_user = $Conf{SearchUser} || '';
157    
158    my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 0} );
159    
160    my $sth_inc_size = $dbh->prepare(qq{
161            update backups set
162                    inc_size = ?,
163                    parts = ?,
164                    inc_deleted = false
165            where id = ?
166    });
167    my $sth_backup_parts = $dbh->prepare(qq{
168            insert into backup_parts (
169                    backup_id,
170                    part_nr,
171                    tar_size,
172                    size,
173                    md5,
174                    items
175            ) values (?,?,?,?,?,?)
176    });
177    
178  #  #
179  # This constant and the line of code below that uses it are borrowed  # This constant and the line of code below that uses it are borrowed
180  # from Archive::Tar.  Thanks to Calle Dybedahl and Stephen Zander.  # from Archive::Tar.  Thanks to Calle Dybedahl and Stephen Zander.
# Line 158  my(%HardLinkExtraFiles, @HardLinks); Line 197  my(%HardLinkExtraFiles, @HardLinks);
197  #  #
198  # Write out all the requested files/directories  # Write out all the requested files/directories
199  #  #
200  binmode(STDOUT);  
201  my $fh = *STDOUT;  my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar";
202  if ( $ShareName eq "*" ) {  $max_file_size *= 1024;
203      my $PathRemoveOrig = $PathRemove;  
204      my $PathAddOrig    = $PathAdd;  my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir};
205      foreach $ShareName ( $view->shareList($Num) ) {  die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir);
206          #print(STDERR "Doing share ($ShareName)\n");  
207          $PathRemove = "/" if ( !defined($PathRemoveOrig) );  my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)";
208          ($PathAdd = "/$ShareName/$PathAddOrig") =~ s{//+}{/}g;  
209          foreach my $dir ( @ARGV ) {  my $tar_path_final = $tar_dir . '/' . $tar_file;
210              archiveWrite($fh, $dir);  my $tar_path = $tar_path_final . '.tmp';
211          }  
212          archiveWriteHardLinks($fh);  $tar_path =~ s#//#/#g;
213      }  
214  } else {  my $sth = $dbh->prepare(qq{
215      foreach my $dir ( @ARGV ) {          SELECT
216          archiveWrite($fh, $dir);                  backups.id
217      }          FROM backups
218      archiveWriteHardLinks($fh);                  JOIN shares on shares.id = shareid
219                    JOIN hosts on hosts.id = shares.hostid
220            WHERE hosts.name = ? and shares.name = ? and backups.num = ?
221    });
222    $sth->execute($Host, $ShareName, $Num);
223    my ($backup_id) = $sth->fetchrow_array;
224    $sth->finish;
225    
226    print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d});
227    
228    if (-e $tar_path_final) {
229            if ($opts{f}) {
230                    rmtree $tar_path_final || die "can't remove $tar_path_final: $!";
231            } else {
232                    die "$tar_path_final allready exists\n";
233            }
234  }  }
235    
236  #  my $fh;
237  # Finish with two null 512 byte headers, and then round out a full  my $part = 0;
238  # block.  my $no_files = 0;
239  #  my $items_in_part = 0;
240  my $data = "\0" x ($tar_header_length * 2);  
241  TarWrite($fh, \$data);  sub new_tar_part {
242  TarWrite($fh, undef);          my $arg = {@_};
243    
244            if ($fh) {
245                    return if ($current_tar_size == 0);
246    
247                    print STDERR " $part" if ($opts{v});
248    
249                    #
250                    # Finish with two null 512 byte headers,
251                    # and then round out a full block.
252                    #
253                    my $data = "\0" x ($tar_header_length * 2);
254                    TarWrite($fh, \$data);
255                    TarWrite($fh, undef);
256    
257                    close($fh) || die "can't close archive part $part: $!";
258    
259                    my $file = $tar_path . '/' . $part;
260    
261                    my $md5 = read_file( $file . '.md5' ) || die "can't read md5sum file ${file}.md5";
262                    $md5 =~ s/\s.*$//;
263    
264                    my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz";
265    
266                    $sth_backup_parts->execute(
267                            $backup_id,
268                            $part,
269                            $current_tar_size,
270                            $size,
271                            $md5,
272                            $items_in_part,
273                    );
274    
275                    #$total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024;
276                    $total_increment_size += $size;
277    
278                    if ($arg->{close}) {
279    
280                            sub move($$) {
281                                    my ($from,$to) = @_;
282                                    print STDERR "# rename $from -> $to\n" if ($opts{d});
283                                    rename $from, $to || die "can't move $from -> $to: $!\n";
284                            }
285    
286                            if ($part == 1) {
287                                    print STDERR " single";
288                                    move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz");
289                                    move("${tar_path}/1.md5", "${tar_path_final}.md5");
290                                    rmtree $tar_path or die "can't remove temporary dir $tar_path: $!";
291                            } else {
292                                    print STDERR " [last]";
293                                    move("${tar_path}", "${tar_path_final}");
294                            }
295    
296                            $sth_inc_size->execute(
297                                    $total_increment_size,
298                                    $part,
299                                    $backup_id
300                            );
301    
302                            print STDERR ", $total_increment_size bytes\n" if ($opts{v});
303    
304                            return;
305                    }
306    
307            }
308    
309            $part++;
310    
311            # if this is first part, create directory
312    
313            if ($part == 1) {
314                    if (-e $tar_path) {
315                            print STDERR "# deleting existing $tar_path\n" if ($opts{d});
316                            rmtree($tar_path);
317                    }
318                    mkdir($tar_path) || die "can't create directory $tar_path: $!";
319    
320                    sub abort_cleanup {
321                            print STDERR "ABORTED: cleanup temp dir";
322                            rmtree($tar_path);
323                            $dbh->rollback;
324                            exit 1;
325                    }
326    
327                    $SIG{'INT'}  = \&abort_cleanup;
328                    $SIG{'QUIT'} = \&abort_cleanup;
329                    $SIG{'__DIE__'} = \&abort_cleanup;
330    
331            }
332    
333            my $file = $tar_path . '/' . $part;
334    
335            #
336            # create comprex pipe which will pass output through gzip
337            # for compression, create file on disk using tee
338            # and pipe same output to md5sum to create checksum
339            #
340    
341            my $cmd = '| ' . $bin->{'gzip'}   . ' ' . $Conf{GzipLevel} .      ' ' .
342                      '| ' . $bin->{'tee'}    . ' ' . $file . '.tar.gz' . ' ' .
343                      '| ' . $bin->{'md5sum'} . ' - > ' . $file . '.md5';
344    
345            print STDERR "## $cmd\n" if ($opts{d});
346    
347            open($fh, $cmd) or die "can't open $cmd: $!";
348            binmode($fh);
349    
350            $current_tar_size = 0;
351            $items_in_part = 0;
352    }
353    
354    new_tar_part();
355    
356    if (seedCache($Host, $ShareName, $Num)) {
357            archiveWrite($fh, '/');
358            archiveWriteHardLinks($fh);
359            new_tar_part( close => 1 );
360    } else {
361            print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v});
362            # remove temporary files if there are no files
363            rmtree($tar_path);
364    }
365    
366  #  #
367  # print out totals if requested  # print out totals if requested
# Line 199  if ( $ErrorCnt && !$FileCnt && !$DirCnt Line 375  if ( $ErrorCnt && !$FileCnt && !$DirCnt
375      # Got errors, with no files or directories; exit with non-zero      # Got errors, with no files or directories; exit with non-zero
376      # status      # status
377      #      #
378      exit(1);      die "got errors or no files\n";
379  }  }
380  exit(0);  
381    $sth_inc_size->finish;
382    $sth_backup_parts->finish;
383    
384    $dbh->commit || die "can't commit changes to database";
385    $dbh->disconnect();
386    
387    exit;
388    
389  ###########################################################################  ###########################################################################
390  # Subroutines  # Subroutines
# Line 266  sub TarWrite Line 449  sub TarWrite
449  {  {
450      my($fh, $dataRef) = @_;      my($fh, $dataRef) = @_;
451    
452    
453      if ( !defined($dataRef) ) {      if ( !defined($dataRef) ) {
454          #          #
455          # do flush by padding to a full $WriteBufSz          # do flush by padding to a full $WriteBufSz
# Line 273  sub TarWrite Line 457  sub TarWrite
457          my $data = "\0" x ($WriteBufSz - length($WriteBuf));          my $data = "\0" x ($WriteBufSz - length($WriteBuf));
458          $dataRef = \$data;          $dataRef = \$data;
459      }      }
460    
461        # poor man's tell :-)
462        $current_tar_size += length($$dataRef);
463    
464      if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {      if ( length($WriteBuf) + length($$dataRef) < $WriteBufSz ) {
465          #          #
466          # just buffer and return          # just buffer and return
# Line 283  sub TarWrite Line 471  sub TarWrite
471      my $done = $WriteBufSz - length($WriteBuf);      my $done = $WriteBufSz - length($WriteBuf);
472      if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))      if ( syswrite($fh, $WriteBuf . substr($$dataRef, 0, $done))
473                                  != $WriteBufSz ) {                                  != $WriteBufSz ) {
474          print(STDERR "Unable to write to output file ($!)\n");          die "Unable to write to output file ($!)\n";
         exit(1);  
475      }      }
476      while ( $done + $WriteBufSz <= length($$dataRef) ) {      while ( $done + $WriteBufSz <= length($$dataRef) ) {
477          if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))          if ( syswrite($fh, substr($$dataRef, $done, $WriteBufSz))
478                              != $WriteBufSz ) {                              != $WriteBufSz ) {
479              print(STDERR "Unable to write to output file ($!)\n");              die "Unable to write to output file ($!)\n";
             exit(1);  
480          }          }
481          $done += $WriteBufSz;          $done += $WriteBufSz;
482      }      }
# Line 390  sub TarWriteFileInfo Line 576  sub TarWriteFileInfo
576  }  }
577    
578  #  #
579  # returns 1 if a given directory has files somewhere under it  # seed cache of files in this increment
 # in a given dump of a given share  
580  #  #
581  sub checkSubDirs($$$$) {  sub seedCache($$$) {
582          my ($dir, $share, $host, $dumpNo) = @_;          my ($host, $share, $dumpNo) = @_;
583          my $ret;  
584          my $dsn = $Conf{SearchDSN};          print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v});
585          my $db_user = $Conf{SearchUser} || '';          my $sql = q{
586          my $search_sql;                  SELECT path,size
587                            FROM files
588          print(STDERR $dir);                          JOIN shares on shares.id = shareid
589          # erase first dot                          JOIN hosts on hosts.id = shares.hostid
590          if (substr($dir, 0, 1) == '.')                  WHERE hosts.name = ? and shares.name = ? and backupnum = ?
591          {          };
592                  $dir = substr($dir, 1, length($dir));  
593          }          my $sth = $dbh->prepare($sql);  
594          # erase first slash          $sth->execute($host, $share, $dumpNo);
595          if (substr($dir, 0, 1) == '/')          my $count = $sth->rows;
596          {          print STDERR " $count items, parts:" if ($opts{v});
597                  $dir = substr($dir, 1, length($dir));                    while (my $row = $sth->fetchrow_arrayref) {
598          }  #print STDERR "+ ", $row->[0],"\n";
599          # erase last slash                  $in_backup_increment->{ $row->[0] } = $row->[1];
         if (substr($dir, length($dir)-1, 1) == '/')  
         {  
                 $dir = substr($dir, 0, length($dir)-1);  
600          }          }
601            
602            $sth->finish();
603    
604          if (! $db_done)          return $count;
605          {  }
                 print STDERR "doing db...";  
                 my $search_sql = q{  
                                 SELECT hosts.name, shares.name, startfiles.name, COUNT(files.*) AS subfiles  
                                 FROM files startfiles  
                                          INNER JOIN shares ON (shares.id=startfiles.shareid)  
                                          INNER JOIN hosts ON  (hosts.id=shares.hostid)  
                                          INNER JOIN backups ON (  
                                                 backups.num=startfiles.backupnum AND  
                                                 backups.hostid=hosts.id AND backups.shareid=shares.id  
                                          )  
                                          LEFT JOIN files ON  (  
                                                 files.backupnum=startfiles.backupnum AND  
                                                 files.shareid=startfiles.shareid AND  
                                                 files.path LIKE startfiles.path || '/%' AND                
                                                 files.type<>startfiles.type AND                  
                                                 files.id <> startfiles.id  
                                         )                
                                 WHERE                            
                                         hosts.name=? AND                  
                                         shares.name=? AND            
                                         startfiles.type=? AND            
                                         startfiles.backupnum=?  
                                 GROUP BY hosts.name, shares.name, startfiles.name, startfiles.backupnum;  
                         };        
                 my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1} );        
                 my $sth = $dbh->prepare($search_sql);    
                 $sth->execute($host, $share, BPC_FTYPE_DIR, $dumpNo);  
                 print STDERR "done\n";  
                 while (my @r_data = $sth->fetchrow_array())  
                 {  
                         $DBCache[$r_data[0]][$r_data[1]][$r_data[2]] = 1;  
                 }  
                   
                 $sth->finish();  
606    
607                  $DBCache[$host][$share][$dir] = $ret;            #
608                  $dbh->disconnect();      # calculate overhad for one file in tar
609                  $db_done = 1;  #
610          }  sub tar_overhead($) {
611            my $name = shift || '';
612    
613          if ($DBCache[$host][$share][$dir] != undef && $DBCache[$host][$share][$dir] == 1)          # header, padding of file and two null blocks at end
614          {          my $len = 4 * $tar_header_length;
615                  return 1;  
616            # if filename is longer than 99 chars subtract blocks for
617            # long filename
618            if ( length($name) > 99 ) {
619                    $len += int( ( length($name) + $tar_header_length ) / $tar_header_length ) * $tar_header_length;
620          }          }
621          return 0;  
622            return $len;
623  }  }
624    
625  my $Attr;  my $Attr;
# Line 473  sub TarWriteFile Line 629  sub TarWriteFile
629  {  {
630      my($hdr, $fh, $tarPathOverride) = @_;      my($hdr, $fh, $tarPathOverride) = @_;
631    
   
632      my $tarPath = $hdr->{relPath};      my $tarPath = $hdr->{relPath};
633      $tarPath = $tarPathOverride if ( defined($tarPathOverride) );      $tarPath = $tarPathOverride if ( defined($tarPathOverride) );
634    
635      $tarPath =~ s{//+}{/}g;      $tarPath =~ s{//+}{/}g;
636    
637        #print STDERR "? $tarPath\n" if ($opts{d});
638        my $size = $in_backup_increment->{$tarPath};
639        return unless (defined($size));
640    
641        # is this file too large to fit into MaxArchiveFileSize?
642    
643        if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) {
644            print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d});
645            new_tar_part();
646        }
647    
648        #print STDERR "A $tarPath [$size] tell: $current_tar_size\n" if ($opts{d});
649        $items_in_part++;
650    
651      if ( defined($PathRemove)      if ( defined($PathRemove)
652              && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {              && substr($tarPath, 0, length($PathRemove)) eq $PathRemove ) {
653          substr($tarPath, 0, length($PathRemove)) = $PathAdd;          substr($tarPath, 0, length($PathRemove)) = $PathAdd;
# Line 490  sub TarWriteFile Line 660  sub TarWriteFile
660          #          #
661          # Directory: just write the header          # Directory: just write the header
662          #          #
           
                   
663          $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );          $hdr->{name} .= "/" if ( $hdr->{name} !~ m{/$} );
664          # check if it has files under it in the database                  TarWriteFileInfo($fh, $hdr);
665                  if ( checkSubDirs($hdr->{path}, $ShareName, $Host, $Num) != 0 )          $DirCnt++;
                 {  
                 TarWriteFileInfo($fh, $hdr);  
                         $DirCnt++;  
                 }  
666      } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {      } elsif ( $hdr->{type} == BPC_FTYPE_FILE ) {
667          #          #
668          # Regular file: write the header and file          # Regular file: write the header and file
# Line 509  sub TarWriteFile Line 673  sub TarWriteFile
673              $ErrorCnt++;              $ErrorCnt++;
674              return;              return;
675          }          }
676          TarWriteFileInfo($fh, $hdr);          # do we need to split file?
677          my($data, $size);          if ($hdr->{size} < $max_file_size) {
678          while ( $f->read(\$data, $BufSize) > 0 ) {                  TarWriteFileInfo($fh, $hdr);
679              TarWrite($fh, \$data);                  my($data, $size);
680              $size += length($data);                  while ( $f->read(\$data, $BufSize) > 0 ) {
681          }                      TarWrite($fh, \$data);
682          $f->close;                      $size += length($data);
683          TarWritePad($fh, $size);                  }
684                    $f->close;
685                    TarWritePad($fh, $size);
686                  $FileCnt++;                  $FileCnt++;
687                  $ByteCnt += $size;                  $ByteCnt += $size;
688            } else {
689                    my $full_size = $hdr->{size};
690                    my $orig_name = $hdr->{name};
691                    my $max_part_size = $max_file_size - tar_overhead($hdr->{name});
692    
693                    my $parts = int(($full_size + $max_part_size - 1) / $max_part_size);
694                    print STDERR "# splitting $orig_name [$full_size bytes] into $parts parts\n" if ($opts{d});
695                    foreach my $subpart ( 1 .. $parts ) {
696                            new_tar_part();
697                            if ($subpart < $parts) {
698                                    $hdr->{size} = $max_part_size;
699                            } else {
700                                    $hdr->{size} = $full_size % $max_part_size;
701                            }
702                            $hdr->{name} = $orig_name . '/' . $subpart;
703                            print STDERR "## creating part $subpart ",$hdr->{name}, " [", $hdr->{size}," bytes]\n";
704    
705                            TarWriteFileInfo($fh, $hdr);
706                            my($data, $size);
707    if (0) {
708                            for ( 1 .. int($hdr->{size} / $BufSize) ) {
709                                    my $r_size = $f->read(\$data, $BufSize);
710                                    die "expected $BufSize bytes read, got $r_size bytes!" if ($r_size != $BufSize);
711                                    TarWrite($fh, \$data);
712                                    $size += length($data);
713                            }
714    }
715                            my $size_left = $hdr->{size} % $BufSize;
716                            my $r_size = $f->read(\$data, $size_left);
717                            die "expected $size_left bytes last read, got $r_size bytes!" if ($r_size != $size_left);
718    
719                            TarWrite($fh, \$data);
720                            $size += length($data);
721                            TarWritePad($fh, $size);
722    
723                            $items_in_part++;
724                    }
725                    $f->close;
726                    $FileCnt++;
727                    $ByteCnt += $full_size;
728                    new_tar_part();
729            }
730      } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {      } elsif ( $hdr->{type} == BPC_FTYPE_HARDLINK ) {
731          #          #
732          # Hardlink file: either write a hardlink or the complete file          # Hardlink file: either write a hardlink or the complete file
733                  # depending upon whether the linked-to file will be written          # depending upon whether the linked-to file will be written
734                  # to the archive.          # to the archive.
735          #          #
736                  # Start by reading the contents of the link.          # Start by reading the contents of the link.
737                  #          #
738          my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});          my $f = BackupPC::FileZIO->open($hdr->{fullPath}, 0, $hdr->{compress});
739          if ( !defined($f) ) {          if ( !defined($f) ) {
740              print(STDERR "Unable to open file $hdr->{fullPath}\n");              print(STDERR "Unable to open file $hdr->{fullPath}\n");
# Line 537  sub TarWriteFile Line 745  sub TarWriteFile
745          while ( $f->read(\$data, $BufSize) > 0 ) {          while ( $f->read(\$data, $BufSize) > 0 ) {
746              $hdr->{linkname} .= $data;              $hdr->{linkname} .= $data;
747          }          }
748                  $f->close;          $f->close;
749                  #          my $done = 0;
750                  # Check @ARGV and the list of hardlinked files we have explicity          my $name = $hdr->{linkname};
751                  # dumped to see if we have dumped this file or not          $name =~ s{^\./}{/};
752                  #          if ( $HardLinkExtraFiles{$name} ) {
753                  my $done = 0;              #
754                  my $name = $hdr->{linkname};              # Target file will be or was written, so just remember
755                  $name =~ s{^\./}{/};              # the hardlink so we can dump it later.
756                  if ( $HardLinkExtraFiles{$name} ) {              #
757                      $done = 1;              push(@HardLinks, $hdr);
758                  } else {              $SpecialCnt++;
759                      foreach my $arg ( @ARGV ) {          } else {
760                          $arg =~ s{^\./+}{/};              #
761                          $arg =~ s{/+$}{};              # Have to dump the original file.  Just call the top-level
762                          $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );              # routine, so that we save the hassle of dealing with
763                      }              # mangling, merging and attributes.
764                  }              #
765                  if ( $done ) {              $HardLinkExtraFiles{$hdr->{linkname}} = 1;
766                      #              archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
767                      # Target file will be or was written, so just remember          }
                     # the hardlink so we can dump it later.  
                     #  
                     push(@HardLinks, $hdr);  
                     $SpecialCnt++;  
                 } else {  
                     #  
                     # Have to dump the original file.  Just call the top-level  
                     # routine, so that we save the hassle of dealing with  
                     # mangling, merging and attributes.  
                     #  
                     $HardLinkExtraFiles{$hdr->{linkname}} = 1;  
                     archiveWrite($fh, $hdr->{linkname}, $hdr->{name});  
                 }  
768      } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {      } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
769          #          #
770          # Symbolic link: read the symbolic link contents into the header          # Symbolic link: read the symbolic link contents into the header
# Line 621  sub TarWriteFile Line 816  sub TarWriteFile
816          $ErrorCnt++;          $ErrorCnt++;
817      }      }
818  }  }
819    
820    my $t_fmt = '%Y-%m-%d %H:%M:%S';
821    sub curr_time {
822            return strftime($t_fmt,localtime());
823    }

Legend:
Removed from v.100  
changed lines
  Added in v.253

  ViewVC Help
Powered by ViewVC 1.1.26