#!/usr/bin/perl # # This is the file locking routines which come with Agora. Code is # either partially modified or directly ripped from that program. # # Agora is by Arthur Secret and Hugh Sasse # . Agora's copyright is at # http://www.w3.org/hypertext/COPYRIGHT.html. # # SetLockFile(Directory, Filename) # # Returns 1 on success, 0 on error. sub SetLockFile { local($Dir, $FileName) = @_; local($have_exclusive_lock) = 0; srand $$; while(!$have_exclusive_lock) { # Is there a lockfile? opendir(LOCKDIR, "$Dir") || return 0; @lockfiles = readdir(LOCKDIR); closedir(LOCKDIR); @lockfiles = grep(($_ =~ /${FileName}_[0-9]+.lck/i), @lockfiles); if( $#lockfiles == -1) { # there are none here already, so we can create one. open(LOCKFILE, ">$Dir/${FileName}_$$.lck") || return 0; print LOCKFILE "Lock file\n"; close(LOCKFILE); ## So we have now created a lock file. We # may still not have an exclusive lock. Another # process may have got the lock at the same time. opendir(LOCKDIR, "$Dir") || return 0; @lockfiles = readdir(LOCKDIR); closedir(LOCKDIR); @lockfiles = grep(($_ =~ /${FileName}_[0-9]+.lck/i), @lockfiles); if($#lockfiles == 0) { # there is only one lock file so it must be # ours. $have_exclusive_lock = 1; } else { # we need to remove the lockfile we have made... unlink("$Dir/${FileName}_$$.lck") || return 0; # and wait a random length of time. sleep(rand($$ % 3)); }; # end if $#lockfiles == 0 ...else.. } else { # there is a lock file. # wait a random length of time. sleep(rand($$ % 3)); }; # end if ($#lockfiles == -1) ... else.... }; # end while(!$have_exclusive_lock) # there is only one lockfile -- ours -- so we can proceed return "$Dir/${FileName}_$$.lck"; }; # end sub quota_write_file(); 1; # Must be the last line