cvsimport: introduce _fetchfile() method and used a 1M buffer to read()
File retrieval from the socket is now moved to _fetchfile() and we now cap reads at 1MB. This should limit the memory growth of the cvsimport process. Signed-off-by: Martin Langhoff <martin@catalyst.net.nz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Martin Langhoff committed
May 23, 2006 at 20:08 UTC
55cad8429954c7d08d0ce86155e7f9adf2f4c6ad
1 file changed
+19
-17
git-cvsimport.perl
+19
-17
@@ -315,15 +315,7 @@ sub _line {
315
chomp $cnt;
316
die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/;
317
$line="";
318
- $res=0;
319
- while($cnt) {
320
- my $buf;
321
- my $num = $self->{'socketi'}->read($buf,$cnt);
322
- die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
323
- print $fh $buf;
324
- $res += $num;
325
- $cnt -= $num;
326
- }
318
+ $res = $self->_fetchfile($fh, $cnt);
319
} elsif($line =~ s/^ //) {
320
print $fh $line;
321
$res += length($line);
@@ -335,14 +327,7 @@ sub _line {
327
chomp $cnt;
328
die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1;
329
$line="";
338
- while($cnt) {
339
- my $buf;
340
- my $num = $self->{'socketi'}->read($buf,$cnt);
341
- die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0;
342
- print $fh $buf;
343
- $res += $num;
344
- $cnt -= $num;
345
- }
330
+ $res += $self->_fetchfile($fh, $cnt);
331
} else {
332
chomp $line;
333
if($line eq "ok") {
@@ -384,6 +369,23 @@ sub file {
369
370
return ($name, $res);
371
}
372
+sub _fetchfile {
373
+ my ($self, $fh, $cnt) = @_;
374
+ my $res;
375
+ my $bufsize = 1024 * 1024;
376
+ while($cnt) {
377
+ if ($bufsize > $cnt) {
378
+ $bufsize = $cnt;
379
+ }
380
+ my $buf;
381
+ my $num = $self->{'socketi'}->read($buf,$bufsize);
382
+ die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0;
383
+ print $fh $buf;
384
+ $res += $num;
385
+ $cnt -= $num;
386
+ }
387
+ return $res;
388
+}
389
390
391
package main;