[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] trouble copying file contents



At 7:44 PM -0400 5/4/00, David Ackerman wrote:
>I'm hoping someone can help me with this.  The following code is used to
>pack the entire contents of one file into another file, which contains
>other data as well.  This works fine for small files, but today I
>discovered that it fails with rather large files and I don't see why.  I
>have a 180 MB file of which the app copies aprox 45 MB. It then moves on
>to the next file and copies that fine and so on.
>
>$catInfo = FSpGetCatInfo($fileToPack);
>my $dataSize = $catInfo->ioFlLgLen();
>$t = MacPack('long', $dataSize);
>push (@temp, $t);
>$localSize += 4;
>
>$localSize += $dataSize;
>
>
>my $data = '';my $b = 1;my $remain = 0;
>open (FILETOCOPY, "<$fileToPack") || die($!);
>if ($dataSize > 262144) {
>	$b = 262144;
>	$remain = ($dataSize % 262144);
>  }
>
># the following loops 731 times
># which is the proper amount for this file #however only 45 MB of the
># 191627264 bytes is actually copied to the new file even though
># $b(blocksize) is set to 262144
>
>for ($t = 1; $t <= ($dataSize / $b); $t++) {
>	read (FILETOCOPY, $data, $b);


It's pretty trusting of you to rely on read getting the number of 
bytes you asked for.  If I were you, I'd check the return value of 
read, to make sure you're getting what you asked for.

>	print TARGETFILE $data;

Similarly, print will tell you if it succeeded.

>}
>
>read (FILETOCOPY, $data, $remain);
>print TARGETFILE $data;
>
>close (FILETOCOPY);

Personally if I were just trying to copy a file I'd use File::Copy. 
But if I wanted to do it myself with a file that had only a data 
fork, I'd do something like this:

$size = -s $file;
while ($n = read(FILETOCOPY, $data, $b)) {
	print TARGETFILE $data or die "Unable to write";
	$size -= $n;
}

print "Copy botched" if $size;

>
>
>
>David Ackerman
>
-- 
--
Paul Schinder
schinder@pobox.com

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org