# See bottom of file for license and copyright information =begin TML ---+ test for flock Test for htpasswd format password files. =cut #package Foswiki::Users::TestFLockMechanism; #use base 'Foswiki::Users::Password'; use strict; #use Assert; use Error qw( :try ); use Fcntl qw( :DEFAULT :flock ); # Lock the htpasswd semaphore file (create if it does not exist) # Returns a file handle that you can later simply close with _unlockPasswdFile sub _lock { my $lockFileName = './test.lock'; print " Opening file.\n"; sysopen(my $fh, $lockFileName, O_RDWR|O_CREAT, 0666) || throw Error::Simple( $lockFileName . ' open or create test lock file failed -' . 'check access rights: ' . $! ); print " Getting lock.\n"; flock $fh, LOCK_EX; return $fh; } # Unlock the semaphore file. You must pass the filehandle for the lock file # which was returned by _lockPasswdFile sub _unlock { print " Closing file handle.\n"; my $fh = shift; close ($fh); } #sub test { try { print " Now getting lock.\n"; my $lockHandle = _lock(); print " Lock established.\n"; #sleep(1); #print " Now getting 2nd lock.\n"; #my $lock2 = _lock(); #print " 2nd lock established.\n"; sleep(10); #print " Now unlocking 2nd lock.\n"; #_unlock( $lock2 ); #print " 2nd unlocking finished.\n"; #sleep(1); print " Now unlocking.\n"; _unlock( $lockHandle ); print " Unlocking finished.\n"; } catch Error::Simple with { print STDERR "ERROR: failed to process the main issue.\n"; return undef; }; } 1; __DATA__ # Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/ # # Copyright (C) 2008-2009 Foswiki Contributors. Foswiki Contributors # are listed in the AUTHORS file in the root of this distribution. # NOTE: Please extend that file, not this notice. # # Additional copyrights apply to some or all of the code in this # file as follows: # # Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org # and TWiki Contributors. All Rights Reserved. TWiki Contributors # are listed in the AUTHORS file in the root of this distribution. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. For # more details read LICENSE in the root of this distribution. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # As per the GPL, removal of this notice is prohibited.