S
Sara
After querying this group, perldoc, Camel, and my local Perl
associates, I came up empty on how to deterimine if a file is locked (
with flock() ). Most of the posts in CLPM suggest where is no way to
do it. Happily though, there is. Perhaps this post will prevent others
from spending 2-3 hours going down the same road.
Looking at the Camel v3 doc for "flock", it states it returns 1 for
success 0 otherwise. If you're using Camel V2 however, there is no
discussion about returned value from flock(), so upgrade
The PROBLEM is, if one uses:
flock(LOCK, LOCK_EX)
on a locked file, it's blocked until its released. So it's not really
a test since we can't evaluate (blocked indefinitely), particularly
when the lock may be perpetual. A third param, an optional timeout,
would be
The trick is use this:
flock(LOCK, LOCK_EX|LOCK_NB)
which produces exactly what we're after- 0 for a locked file, and 1
for a non-locked file (with the side-effect of actually LOCKING the
file, so if you ONLY wanted to test, you'll need to release the lock).
I hope this assists some with a test for file locking. It would be
nice if stat returned a boolean for file lock, but it doesn't.
All append the usual comments here to save the naysayers the trouble.
Good day!
*****************************************************************************
This is stupid - why post it?
Read Camel idiot!
Did you check perldoc?
This will never work.
There is an easier way using a simple 10 line program with eval and
alarms.
*******************************************************************************
G
associates, I came up empty on how to deterimine if a file is locked (
with flock() ). Most of the posts in CLPM suggest where is no way to
do it. Happily though, there is. Perhaps this post will prevent others
from spending 2-3 hours going down the same road.
Looking at the Camel v3 doc for "flock", it states it returns 1 for
success 0 otherwise. If you're using Camel V2 however, there is no
discussion about returned value from flock(), so upgrade
The PROBLEM is, if one uses:
flock(LOCK, LOCK_EX)
on a locked file, it's blocked until its released. So it's not really
a test since we can't evaluate (blocked indefinitely), particularly
when the lock may be perpetual. A third param, an optional timeout,
would be
The trick is use this:
flock(LOCK, LOCK_EX|LOCK_NB)
which produces exactly what we're after- 0 for a locked file, and 1
for a non-locked file (with the side-effect of actually LOCKING the
file, so if you ONLY wanted to test, you'll need to release the lock).
I hope this assists some with a test for file locking. It would be
nice if stat returned a boolean for file lock, but it doesn't.
All append the usual comments here to save the naysayers the trouble.
Good day!
*****************************************************************************
This is stupid - why post it?
Read Camel idiot!
Did you check perldoc?
This will never work.
There is an easier way using a simple 10 line program with eval and
alarms.
*******************************************************************************
G