From 8c6f8fdd2ec404ce481983585509ab7c0ae3f277 Mon Sep 17 00:00:00 2001 From: "cls%seawood.org" Date: Wed, 12 Sep 2001 06:32:35 +0000 Subject: [PATCH] Add support for adding multiple items to list at once. Read entire file into mem to avoid overhead of exec'ing multiple greps. Bug #59454 r=bryner sr=alecf git-svn-id: svn://10.0.0.236/trunk@102793 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/config/build-list.pl | 46 ++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/mozilla/config/build-list.pl b/mozilla/config/build-list.pl index b622290a930..32c1d3d8bdb 100755 --- a/mozilla/config/build-list.pl +++ b/mozilla/config/build-list.pl @@ -25,7 +25,7 @@ # A generic script to add entries to a file # if the entry does not already exist # -# Usage: $0 [-l] +# Usage: $0 [-l] [ ] # # -l do not attempt flock the file. @@ -38,7 +38,6 @@ sub usage() { exit(1); } -$lockfile = $nofilelocks = 0; getopts("l"); @@ -46,7 +45,11 @@ getopts("l"); $nofilelocks = 1 if defined($::opt_l); $file = shift; -$entry = shift; + +undef @entrylist; +while ($entry = shift) { + push @entrylist, $entry; +} $lockfile = $file . ".lck"; @@ -57,19 +60,36 @@ if ( ! -e "$file") { } # This needs to be atomic -open(OUT, ">>$file") || die ("$file: $!\n"); mozLock($lockfile) unless $nofilelocks; -open(RES, "grep -c '^$entry\$' $file |") or $err = $!; -if ($err) { - flock(OUT,LOCK_UN) unless $nofilelocks; - die ("grep: $err\n"); + +# Read entire file into mem +undef @inbuf; +if ( -e "$file" ) { + open(IN, "$file") || die ("$file: $!\n"); + while ($tmp = ) { + chomp($tmp); + push @inbuf, $tmp; + } + close(IN); } -chomp($val = ); -close(RES); -if (!$val) { - print OUT "$entry\n"; + +undef @outbuf; +# Add each entry to file if it's not already there +foreach $entry (@entrylist) { + push @outbuf, $entry if (!grep(/^$entry$/, @inbuf)); } + +$count = $#outbuf + 1; + +# Append new entry to file +if ($count) { + open(OUT, ">>$file") || die ("$file: $!\n"); + foreach $entry (@outbuf) { + print OUT "$entry\n"; + } + close(OUT); +} + mozUnlock($lockfile) unless $nofilelocks; -close(OUT); exit(0);