in reply to detecting directory in perl
This operation:
open CRONTABFILE, '>crontab'; flock CRONTABFILE, 2;
...is a potentially catastrophic bug waiting to happen, and it's repeated on a couple of different files in your sample script. You are opening crontab in write mode. More specifically, write-and-truncate mode. Imagine this sequence of events:
MJD likens this scenario to taking a dump on someone's front porch and then ringing the doorbell to ask for tissue.
If you wish to obtain a lock on the crontab file, you will need to open it in a mode that doesn't clobber it before you ever get the lock. < or +< would be a reasonable approach if you are certain the file already exists. If you do not have such a guarantee, and you don't mind not knowing for sure whether or not you created the file, you could use +>> and then truncate and seek after obtaining the lock. A common idiom would also be to open and lock the target, then open a tempfile with a sufficiently randomized name (see File::Temp, do your writing there, and then rename or move the tempfile into place over the output file. The move becomes an atomic operation, which is about the least risky approach if you aren't guaranteed that other actors aren't occasionally touching the same file without proper locking.
Updated to reflect the order I intended to describe in the first place but managed to still present out of order when I typed in my thoughts (thanks MidLifeXis).
Also see http://www.plover.com/~mjd/perl/yak/flock/
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: detecting directory in perl
by MidLifeXis (Monsignor) on Sep 09, 2016 at 14:43 UTC | |
by davido (Cardinal) on Sep 09, 2016 at 15:17 UTC |