|
SDF::Lock::Advisory - Lock/Unlock a file or filehandle using ADVISORY locking |
SDF::Lock::Advisory - Lock/Unlock a file or filehandle using ADVISORY locking
This document describes version 0.07, released Feb 15, 2003.
use PTools::SDF::Lock::Advisory;
$lockObj = new PTools::SDF::Lock::Advisory;
($stat,$err) = $lockObj->lock( $fileName );
or $lockObj->lock( $fileName, $maxRetries, $sleepTime, $lockMode, $openMode );
($stat,$err) = $lockObj->status;
($stat,$err) = $lockObj->unlock;
The '$lockMode' parameter is valid for the PTools::SDF::Lock::Selective class but is not currently used in this parent class, and is ignored here.
The '$fileName' need not exist prior to calling the 'lock' method, but it will exist if the lock succeeds.
Default for the '$openMode' variable is 0644, or '-rw-r--r--')
Explicit unlock is unnecessary. Simply allow the '$lockObj' variable to fall out of scope (or exit the script, undefine, etc.) to release the lock.
use Fcntl;
use PTools::SDF::Lock::Advisory;
$lockObj = new PTools::SDF::Lock::Advisory;
local(*FH);
sysopen(FH, "/some/file", O_RDWR|O_CREAT, 0644) || die $!;
$fh = *FH;
($stat,$err) = $lockObj->lock( $fh );
or ($stat,$err) = $lockObj->lock( $fh, $maxRetries, $sleepTime );
The 'status' and 'unlock' methods are the same as above, and an '$openMode' parameter is obviously not needed here.
use PTools::SDF::INI; # or PTools::SDF::SDF, or PTools::SDF::TAG
$iniObj = new PTools::SDF::INI( $fileName );
($stat,$err) = $iniObj->lock( $maxRetries, $sleepTime );
The 'status' and 'unlock' methods are the same as above,
The lock and unlock methods are implemented as extendible methods in an abstract base class. See the PTools::SDF::File manpage or the Extender manpage for discussion and examples of this mechanism.
The discussion and syntax here is intended for:
1) anyone who would like to use this class as a 'standalone' mechanism to obtain a lock on an arbitrary filedescriptor or open filehandle
2) PTools::SDF::<module> designers who would like to incorporate a locking mechanism within their modules (whether extendible or not)
Note: For end users of the PTools::SDF::<module> classes (e.g., PTools::SDF::INI, SDF::SDF and PTools::SDF::TAG), refer to the documentation accompanying these classes for a discussion of the lock and unlock methods.
The explanations, below, are intended to be complete enough for the module designers without being too confusing for users wishing to use this as a 'standalone' lock/unlock mechanism.
WARNING: This module implements simple advisory locking only. Any module that chooses not to check for and honor the existance of an advisory lock has full access to modify and/or delete the 'locked' data file.
When using with a filename or filehandle, as shown in the Synopsis sections, above, the lock method will return the newly instantiated lock object.
When called from within a PTools::SDF::<module> class, this module is expected to be implemented as a extendible method. See the abstract base class PTools::SDF::File for further details.
Use the second format of the lock method from within the various SDF::<module> classes to lock the file associated with that object.
(For information on how to obtain a lock on an instantiated PTools::SDF::<module> object, see the documentation accompanying that class.)
1) one of a filename, a filepath or a filehandle to an open file
2) a reference to an PTools::SDF::<module>
Examples of locking a filename or filehandle:
($stat,$err) = $lockObj->lock( "myFile" );
($stat,$err) = $lockObj->lock( "/some/path/to/myFile" );
($stat,$err) = $lockObj->lock( $fh );
See the Synopsis examples, above, for a full example of obtaining and passing an open filehandle.
An Example of invoking a lock from within an PTools::SDF::<module> class can be found in the abstract base class PTools::SDF::File.
The second format of the unlock method is used by the various PTools::SDF::<module> classes to unlock the file associated with that object.
In either case the FH parameter is optional. The filehandle used to release the advisory lock is expected to be maintained within the various objects involved. Module designers should make a note of this.
Examples:
($stat,$err) = $lockObj->unlock;
($stat,$err) = $lockObj->unlock( $fileHandle );
($stat,$err) = $lockObj->status;
$stat = $lockObj->stat; # scalar context returns status number ($err) = $lockObj->stat; # array context returns error message
($err) = $lockObj->err;
Module designers should note that the PTools::SDF::<module> objects should make any error status and message available from within the calling objects, too.
$boolean = $lockObj->isLocked;
$boolean = $lockObj->notLocked;
$self->setErr( 0, "" ); # initialize to "no error" state
$self->setErr( -1, "Error text" ); # set to an "error" state
None currently.
See the PTools::SDF::Overview manpage, the PTools::SDF::ARRAY manpage, the PTools::SDF::CSV manpage, the PTools::SDF::DB manpage, the PTools::SDF::DIR manpage, the PTools::SDF::DSET manpage, the PTools::SDF::File manpage, the PTools::SDF::IDX manpage, the PTools::SDF::INI manpage, the PTools::SDF::SDF manpage, the PTools::SDF::TAG manpage, the PTools::SDF::Lock::Selective manpage, the PTools::SDF::Sort::Bubble manpage, the PTools::SDF::Sort::Quick manpage and the PTools::SDF::Sort::Shell manpage.
Chris Cobb, <nospamplease@ccobb.net>
Copyright (c) 1997-2007 by Chris Cobb. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|
SDF::Lock::Advisory - Lock/Unlock a file or filehandle using ADVISORY locking |