|
File::TinyLock - Utility to lock and unlock files. |
File::TinyLock - Utility to lock and unlock files.
use File::TinyLock;
my $result = File::TinyLock::lock($file); my $result = File::TinyLock::lock(file => $file timeout => 10, debug => 0);
my $result = File::TinyLock::unlock($file); my $result = File::TinyLock::unlock(file => $file);
my $result = File::TinyLock::check($file); my $result = File::TinyLock::check(file => $file);
=head1 DESCRIPTION
File::TinyLock provides a lock, unlock, and check function for
working with file locks.
FILE is not given, then it may instead be passed as the file
option described below.
OPTIONS are passed in a hash like fashion, using key and value
pairs. Possible options are:
file - The file to lock
timeout - Number of seconds to continue trying to lock (Default: 10).
debug - Print debugging info to STDERR (0=Off, 1=On).
Here are a list of return codes of the lock function and what they mean:
check function:
unlock function:
use File::TinyLock;
my $file = shift;
unless(defined($file)){
print "file to lock: ";
chop($file=<STDIN>);
}
my $result = File::TinyLock::lock($file);
if($result){
print "Could not lock: ${result}\n";
}else{
print "$file is now locked\n";
}
# do stuff to file
if($result = File::TinyLock::unlock($file)){
print "could not unlock $file: $result\n";
}
exit;
This utility must be used by all code that works with the file you're
trying to lock. Locking with File::TinyLock will not keep someone
from using vi and editing the file.
If you leave lock files around (from not unlocking the file before
your code exits), File::TinyLock will try its best to determine if the
lock files are stale or not. This is best effort, and may yield
false positives. For example, if your code was running as pid 1234
and exited without unlocking, stale detection may fail if there is
a new process running with pid 1234.
Locking will only remain successfull while your code is active. You can not lock a file, let your code exit, and expect the file to remain locked; doing so will result in stale lock files left behind.
lock file -> do stuff -> unlock file -> exit;
<a href=``http://jeremy.kister.net./''>Jeremy Kister</a>
|
File::TinyLock - Utility to lock and unlock files. |