* Commit for 2.1.0.
[BackupPC.git] / lib / BackupPC / PoolWrite.pm
index f05223c..d0be0e6 100644 (file)
@@ -38,7 +38,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001  Craig Barratt
+#   Copyright (C) 2001-2003  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -56,7 +56,7 @@
 #
 #========================================================================
 #
 #
 #========================================================================
 #
-# Version 2.0.0beta2, released 13 Apr 2003.
+# Version 2.1.0, released 20 Jun 2004.
 #
 # See http://backuppc.sourceforge.net.
 #
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -98,8 +98,8 @@ sub new
     return $self;
 }
 
     return $self;
 }
 
-my $BufSize  = 1048576;     # 1MB or 2^20
-my $MaxFiles = 20;
+my $BufSize  = 1048576;  # 1MB or 2^20
+my $MaxFiles = 20;       # max number of compare files open at one time
 
 sub write
 {
 
 sub write
 {
@@ -115,10 +115,12 @@ sub write
     # file list if the file changes between the file list sending
     # and the file sending).  Here we only catch the case where
     # we haven't computed the digest (ie: we have written no more
     # file list if the file changes between the file list sending
     # and the file sending).  Here we only catch the case where
     # we haven't computed the digest (ie: we have written no more
-    # than $BufSize.  We catch the big file case below.
+    # than $BufSize).  We catch the big file case below.
     #
     if ( !defined($dataRef) && !defined($a->{digest})
                && $a->{fileSize} != length($a->{data}) ) {
     #
     if ( !defined($dataRef) && !defined($a->{digest})
                && $a->{fileSize} != length($a->{data}) ) {
+       #my $newSize = length($a->{data});
+       #print("Fixing file size from $a->{fileSize} to $newSize\n");
        $a->{fileSize} = length($a->{data});
     }
 
        $a->{fileSize} = length($a->{data});
     }
 
@@ -270,9 +272,6 @@ sub write
     # We are at EOF, so finish up
     #
     $a->{eof} = 1;
     # We are at EOF, so finish up
     #
     $a->{eof} = 1;
-    foreach my $f ( @{$a->{files}} ) {
-        $f->{fh}->close();
-    }
 
     #
     # Make sure the fileSize was correct.  See above for comments about
 
     #
     # Make sure the fileSize was correct.  See above for comments about
@@ -291,27 +290,39 @@ sub write
 
        my($fh, $fileName);
        $a->{fileSize} = $a->{nWrite};
 
        my($fh, $fileName);
        $a->{fileSize} = $a->{nWrite};
-       if ( $a->{fileName} =~ /(.*)\// ) {
-           $fileName = $1;
-       } else {
-           $fileName = ".";
-       }
 
 
-       #
-       # Find a unique target temporary file name
-       #
-       my $i = 0;
-       while ( -f "$fileName/t$$.$i" ) {
-           $i++;
+       if ( defined($a->{fhOut}) ) {
+           if ( $a->{fileName} =~ /(.*)\// ) {
+               $fileName = $1;
+           } else {
+               $fileName = ".";
+           }
+           #
+           # Find a unique target temporary file name
+           #
+           my $i = 0;
+           while ( -f "$fileName/t$$.$i" ) {
+               $i++;
+           }
+           $fileName = "$fileName/t$$.$i";
+           $a->{fhOut}->close();
+           if ( !rename($a->{fileName}, $fileName)
+             || !defined($fh = BackupPC::FileZIO->open($fileName, 0,
+                                                $a->{compress})) ) {
+               push(@{$a->{errors}}, "Can't rename $a->{fileName} -> $fileName"
+                                   . " or open during size fixup\n");
+           }
+           #print("Using temporary name $fileName\n");
+       } elsif ( defined($a->{files}) && defined($a->{files}[0]) ) {
+           #
+           # We haven't written anything yet, so just use the
+           # compare file to copy from.
+           #
+           $fh = $a->{files}[0]->{fh};
+           $fh->rewind;
+           #print("Using compare file $a->{files}[0]->{name}\n");
        }
        }
-       $fileName = "$fileName/t$$.$i";
-        $a->{fhOut}->close();
-       if ( !rename($a->{fileName}, $fileName)
-         || !defined($fh = BackupPC::FileZIO->open($fileName, 0,
-                                            $a->{compress})) ) {
-            push(@{$a->{errors}}, "Can't rename $a->{fileName} -> $fileName"
-                               . " or open during size fixup\n");
-       } else {
+       if ( defined($fh) ) {
            my $poolWrite = BackupPC::PoolWrite->new($a->{bpc}, $a->{fileName},
                                        $a->{fileSize}, $a->{compress});
            my $nRead = 0;
            my $poolWrite = BackupPC::PoolWrite->new($a->{bpc}, $a->{fileName},
                                        $a->{fileSize}, $a->{compress});
            my $nRead = 0;
@@ -331,7 +342,7 @@ sub write
                $nRead += $thisRead;
            }
            $fh->close;
                $nRead += $thisRead;
            }
            $fh->close;
-           unlink($fileName);
+           unlink($fileName) if ( defined($fileName) );
            if ( @{$a->{errors}} ) {
                $poolWrite->close;
                return (0, $a->{digest}, -s $a->{fileName}, $a->{errors});
            if ( @{$a->{errors}} ) {
                $poolWrite->close;
                return (0, $a->{digest}, -s $a->{fileName}, $a->{errors});
@@ -341,6 +352,13 @@ sub write
        }
     }
 
        }
     }
 
+    #
+    # Close the compare files
+    #
+    foreach my $f ( @{$a->{files}} ) {
+        $f->{fh}->close();
+    }
+
     if ( $a->{fileSize} == 0 ) {
         #
         # Simply create an empty file
     if ( $a->{fileSize} == 0 ) {
         #
         # Simply create an empty file
@@ -394,6 +412,23 @@ sub close
     return $a->write(undef);
 }
 
     return $a->write(undef);
 }
 
+#
+# Abort a pool write
+#
+sub abort
+{
+    my($a) = @_;
+
+    if ( defined($a->{fhOut}) ) {
+       $a->{fhOut}->close();
+       unlink($a->{fileName});
+    }
+    foreach my $f ( @{$a->{files}} ) {
+        $f->{fh}->close();
+    }
+    $a->{files} = [];
+}
+
 #
 # Copy $nBytes from files $fhIn to $fhOut.
 #
 #
 # Copy $nBytes from files $fhIn to $fhOut.
 #