Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: detecting directory in perl

by MidLifeXis (Monsignor)
on Sep 09, 2016 at 14:43 UTC ( [id://1171465]=note: print w/replies, xml ) Need Help??


in reply to Re: detecting directory in perl
in thread detecting directory in perl

A common idiom would also be to open a tempfile with a sufficiently randomized name (see File::Temp, do your writing there, and then after obtaining a lock on the output file, rename or move the tempfile into place over the output file. (emphasis added)

Race condition waiting to happen (at least as I am reading your statement). Have the lock signal the ability to modify the file (lock first, write second). If you don't lock first, you risk clobbering someone else's changes:

  • You create a tmp file
  • They create a tmp file
  • They lock
  • They overwrite
  • They unlock
  • You lock
  • You overwrite
  • You unlock
The changes made by "They" are now gone. Lock before read just to ensure that you have the newest data. Otherwise, I agree.

--MidLifeXis

Replies are listed 'Best First'.
Re^3: detecting directory in perl
by davido (Cardinal) on Sep 09, 2016 at 15:17 UTC

    You lock the target before creating a temp, and as a final step move the temp in place over the target. If you fail to get a lock on the target to begin with, stop there.

    After re-reading my post I see that my words didn't reflect the process I had in my head when I wrote them. I've updated. Thanks for noticing and bringing it to my attention! :)


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1171465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found