You might wanna add some sysseek's to make sure
Digest::MD5 reads the whole file. Moreover,
you have the outfile only opened to write (prepend a plus
to the opening string of destFile.
Well, that doesn't explain the outcome of the comparison.
(have to run....hint: OOP versus function)
Jeroen
"We are not alone"(FZ)
Update: I modified your code, and it works. Tested
on linux. BTW, yer original code MD'd the objects!
#!/usr/bin/perl -w
# -*-Perl-*-
use strict;
use FileHandle;
use Digest::MD5;
my $sourceFile = $ARGV[0];
my $destFile = $ARGV[1];
my $inFile = new FileHandle;
my $outFile = new FileHandle;
my $inMD5 = Digest::MD5->new;
my $outMD5 = Digest::MD5->new;
my (
$fileLength,
$fileBuffer,
$fileOffset
);
$inFile->open ( "<$sourceFile" )
or die "Could not open $sourceFile:$!\n";
$inMD5->addfile ( $inFile );
print $inMD5->b64digest , "\n";
$outFile->open ( "+>$destFile" )
or die "Could not open $destFile:$!\n";
# borrowed from "Programming Perl"
die "Could not rewind $sourceFile: $!" unless defined sysseek $inFile,
+ 0, 0;
my $blockSize = ( stat $inFile )[11] || 16384;
while ( $fileLength = sysread $inFile, $fileBuffer, $blockSize ) {
if ( !defined $fileLength ) {
next
if $! =~ /^Interrupted/;
die "System read error: $!\n";
}
my $fileOffset = 0;
while ( $fileLength ) {
my $written = syswrite $outFile, $fileBuffer, $fileLength, $fileOffse
+t;
die "System write error: $!\n"
unless defined $written;
$fileLength -= $written;
$fileOffset += $written;
};
}
die "Could not rewind $destFile: $!" unless defined sysseek $outFile,
+0, 0;
$outMD5->addfile ( $outFile );
print $outMD5->b64digest , "\n";
$inFile->close;
$outFile->close;
Moreover, the documented behaviour of an automagical reset
right after the digest, seems not to work, at least on
b64digest and my system. Something to watch for.
|