* Added multi-level incrementals. Still needs testing.
[BackupPC.git] / bin / BackupPC_tarCreate
index 57a8d63..f704255 100755 (executable)
@@ -23,6 +23,8 @@
 #       -p pathAdd      new path prefix
 #       -b BLOCKS       BLOCKS x 512 bytes per record (default 20; same as tar)
 #       -w writeBufSz   write buffer size (default 1MB)
+#       -e charset      charset for encoding file names (default: value of
+#                       $Conf{ClientCharset} when backup was done)
 #
 #     The -h, -n and -s options specify which dump is used to generate
 #     the tar archive.  The -r and -p options can be used to relocate
@@ -51,7 +53,7 @@
 #
 #========================================================================
 #
-# Version 2.1.0_CVS, released 8 Feb 2004.
+# Version 3.0.0alpha, released 23 Jan 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -62,19 +64,17 @@ no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use File::Path;
 use Getopt::Std;
+use Encode qw/from_to/;
 use BackupPC::Lib;
 use BackupPC::Attrib qw(:all);
 use BackupPC::FileZIO;
 use BackupPC::View;
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
-my $TopDir = $bpc->TopDir();
-my $BinDir = $bpc->BinDir();
-my %Conf   = $bpc->Conf();
 
 my %opts;
 
-if ( !getopts("th:n:p:r:s:b:w:", \%opts) || @ARGV < 1 ) {
+if ( !getopts("te:h:n:p:r:s:b:w:", \%opts) || @ARGV < 1 ) {
     print STDERR <<EOF;
 usage: $0 [options] files/directories...
   Required options:
@@ -90,11 +90,14 @@ usage: $0 [options] files/directories...
      -p pathAdd      new path prefix
      -b BLOCKS       BLOCKS x 512 bytes per record (default 20; same as tar)
      -w writeBufSz   write buffer size (default 1048576 = 1MB)
+     -e charset      charset for encoding file names (default: value of
+                     \$Conf{ClientCharset} when backup was done)
 EOF
     exit(1);
 }
 
-if ( $opts{h} !~ /^([\w\.\s-]+)$/ ) {
+if ( $opts{h} !~ /^([\w\.\s-]+)$/
+        || $opts{h} =~ m{(^|/)\.\.(/|$)} ) {
     print(STDERR "$0: bad host name '$opts{h}'\n");
     exit(1);
 }
@@ -123,12 +126,17 @@ if ( $i >= @Backups ) {
     exit(1);
 }
 
+my $Charset = $Backups[$i]{charset};
+$Charset = $opts{e} if ( $opts{e} ne "" );
+
 my $PathRemove = $1 if ( $opts{r} =~ /(.+)/ );
 my $PathAdd    = $1 if ( $opts{p} =~ /(.+)/ );
-if ( $opts{s} !~ /^([\w\s\.\/\$-]+)$/ && $opts{s} ne "*" ) {
+if ( ($opts{s} !~ /^([\w\s.@\/$(){}[\]-]+)$/
+        || $opts{s} =~ m{(^|/)\.\.(/|$)}) && $opts{s} ne "*" ) {
     print(STDERR "$0: bad share name '$opts{s}'\n");
     exit(1);
 }
+
 our $ShareName = $opts{s};
 our $view = BackupPC::View->new($bpc, $Host, \@Backups);
 
@@ -227,9 +235,14 @@ sub archiveWrite
 #
 sub archiveWriteHardLinks
 {
-    my $fh = @_;
+    my($fh) = @_;
     foreach my $hdr ( @HardLinks ) {
         $hdr->{size} = 0;
+       my $name = $hdr->{linkname};
+       $name =~ s{^\./}{/};
+       if ( defined($HardLinkExtraFiles{$name}) ) {
+            $hdr->{linkname} = $HardLinkExtraFiles{$name};
+        }
         if ( defined($PathRemove)
               && substr($hdr->{linkname}, 0, length($PathRemove)+1)
                         eq ".$PathRemove" ) {
@@ -324,8 +337,8 @@ sub TarWriteHeader
        #
        # sprintf octal only handles up to 2^32 - 1
        #
-       my $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
-                   . sprintf("%08o", $hdr->{size} % (1 << 24));
+       $sizeStr = sprintf("%03o", $hdr->{size} / (1 << 24))
+                . sprintf("%08o", $hdr->{size} % (1 << 24));
     } else {
        $sizeStr = sprintf("%011o", $hdr->{size});
     }
@@ -355,6 +368,14 @@ sub TarWriteFileInfo
 {
     my($fh, $hdr) = @_;
 
+    #
+    # Convert path names to requested (eg: client) charset
+    #
+    if ( $Charset ne "" ) {
+        from_to($hdr->{name},     "utf8", $Charset);
+        from_to($hdr->{linkname}, "utf8", $Charset);
+    }
+
     #
     # Handle long link names (symbolic links)
     #
@@ -368,6 +389,7 @@ sub TarWriteFileInfo
         TarWrite($fh, \$data);
         TarWritePad($fh, length($data));
     }
+
     #
     # Handle long file names
     #
@@ -423,10 +445,28 @@ sub TarWriteFile
         TarWriteFileInfo($fh, $hdr);
         my($data, $size);
         while ( $f->read(\$data, $BufSize) > 0 ) {
+            if ( $size + length($data) > $hdr->{size} ) {
+                print(STDERR "Error: truncating $hdr->{fullPath} to"
+                           . " $hdr->{size} bytes\n");
+                $data = substr($data, 0, $hdr->{size} - $size);
+                $ErrorCnt++;
+            }
             TarWrite($fh, \$data);
             $size += length($data);
         }
         $f->close;
+        if ( $size != $hdr->{size} ) {
+            print(STDERR "Error: padding $hdr->{fullPath} to $hdr->{size}"
+                       . " bytes from $size bytes\n");
+            $ErrorCnt++;
+            while ( $size < $hdr->{size} ) {
+                my $len = $hdr->{size} - $size;
+                $len = $BufSize if ( $len > $BufSize );
+                $data = "\0" x $len;
+                TarWrite($fh, \$data);
+                $size += $len;
+            }
+        }
         TarWritePad($fh, $size);
        $FileCnt++;
        $ByteCnt += $size;
@@ -456,10 +496,11 @@ sub TarWriteFile
        my $done = 0;
        my $name = $hdr->{linkname};
        $name =~ s{^\./}{/};
-       if ( $HardLinkExtraFiles{$name} ) {
+       if ( defined($HardLinkExtraFiles{$name}) ) {
            $done = 1;
        } else {
            foreach my $arg ( @ARGV ) {
+               $arg = "/" if ( $arg eq "." );
                $arg =~ s{^\./+}{/};
                $arg =~ s{/+$}{};
                $done = 1 if ( $name eq $arg || $name =~ /^\Q$arg\// );
@@ -478,8 +519,10 @@ sub TarWriteFile
            # routine, so that we save the hassle of dealing with
            # mangling, merging and attributes.
            #
-           $HardLinkExtraFiles{$hdr->{linkname}} = 1;
-           archiveWrite($fh, $hdr->{linkname}, $hdr->{name});
+            my $name = $hdr->{linkname};
+            $name =~ s{^\./}{/};
+           $HardLinkExtraFiles{$name} = $hdr->{name};
+           archiveWrite($fh, $name, $hdr->{name});
        }
     } elsif ( $hdr->{type} == BPC_FTYPE_SYMLINK ) {
         #