Jeff wrote: > > From: John W. Krahn [mailto:[EMAIL PROTECTED]] > > > > Jeff wrote: > > > > > > I'm use flat files to manage a list containing approx 25,000 records. For > > > updates, I write to a temp file then unlink main and rename temp file to > > > main. I use flock for both temp and main files during update. My main file > > > gets blown away on occasions. What gives? I can't figure out why this > > > happens. Thanks for any help! > > > > OS? code? > > unix, > > sub Update_db { > $main_db = $_[0]; > $tmp_db = $_[1]; > $update = $_[2]; > open IN, "<$main_db" or print "Can't open $main_db: $!\n"; > flock( IN, LOCK_EX ) or print "Unable to acquire lock: $!. Aborting"; > open OUT, ">$tmp_db" or print "Can't open temporary file $tmp_db: $!\n"; > flock( OUT, LOCK_EX ) or print "Unable to acquire lock: $!. Aborting"; > while ( <IN> ) { > ($name, $team, $location)= split(/\|/, $_); > next unless $name eq $update; > $new_location = "palm_bay"; > $_ = join( "|", $name, $team, $new_location); > } > continue { > print OUT $_ or print "Error writing $tmp_db: $!\n"; > } > close IN; > close OUT; > unlink $main_db; > rename $tmp_db, $main_db or print "Can't rename '$tmp_db' to '$main_db': > $!\n"; > }
The main problem appears to be that instead of taking a different branch if open() or flock() fails you continue on as if they had succeeded. sub Update_db { my ( $main_db, $tmp_db, $update ) = @_; local ( *IN, *OUT ); unless ( open IN, "<$main_db" ) { print STDERR "Can't open $main_db: $!"; return; } unless ( flock IN, LOCK_EX ) { print STDERR "Unable to acquire lock: $!. Aborting"; return; } unless ( open OUT, ">$tmp_db" ) { print STDERR "Can't open temporary file $tmp_db: $!"; return; } unless ( flock OUT, LOCK_EX ) { print STDERR "Unable to acquire lock: $!. Aborting"; return; } while ( <IN> ) { my ( $name, $team, $location ) = split /\|/; next unless $name eq $update; #************************************************ # Why are you assigning a literal string to this variable here???? $new_location = "palm_bay"; #************************************************ unless ( print OUT join '|', $name, $team, $new_location ) { print STDERR "Error writing $tmp_db: $!\n"; return; } } unless ( unlink $main_db ) { print STDERR "Unable to unlink $main_db: $!. Aborting"; return; } rename $tmp_db, $main_db or print STDERR "Can't rename '$tmp_db' to '$main_db': $!"; } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]