Brent Clark wrote:
> Hi List
> 
> I have a scenario / debate if you wish with my colleague.
> 
> The problem is I have to update a CSV file and the way I do it open
> the file while reading / editting it I then send it to a temp file,
> unlink the original and the rename the temp to the original file name
> (Baring in mind I do flock the file)  
> 
> My colleague, disagrees with my approach to this, he  is adamant 
> that I must open the file get the file in memory and use seek etc to
> update the file (also using flock etc.)  
> 
> So basically from the millisecond it takes to unlock ,  close,
> unlink, and then rename the file, I am open to race condition. 
> 
> So my question is, whats the best what to update / amend a file.

He's right; there is a race condition. But worse than that, if an other
process is continuously writing to the file, your unlink and rename
operation will cause the data from the other process to be lost.

Instead of unlinking and renaming, you could just copy the data from the
temp file back into the original file. Or, use Tie::File and update the file
in place.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to