-# Windows section (no symlinks)
-#
-
-### sub w_lock_file ($;$) ######################################################
-#
-# set read lock (shared lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - file to lock
-# $timeout - Lock Timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_lock_file ($;$) {
- my $filename = shift;
- my $timeout = +shift || $Timeout;
-
- if (-f &masterlockfile($filename)) {
- for (0..$timeout) {
-
- # try to increment the reference counter
- #
- &set_ref($filename,1,$timeout) and return 1;
- sleep (1);
- }
- }
-
- else {
- # master lock is set
- # or file has not been realeased yet
- #
- return;
- }
-
- # time out
- # maybe the system is occupied
- 0;
-}
-
-### sub w_unlock_file ($;$) ####################################################
-#
-# remove read lock (shared lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - locked file
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_unlock_file ($;$) {
- my $filename = shift;
- my $timeout = shift || $Timeout;
-
- if (-f &masterlockfile($filename)) {
-
- # try do decrement the reference counter
- #
- &set_ref($filename,-1,$timeout) and return 1;
- }
-
- # time out
- # maybe the system is occupied
- # or file has not been released yet
- #
- return;
-}
-
-### sub w_write_lock_file ($;$) ################################################
-#
-# set write lock (exclusive lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_write_lock_file ($;$) {
- my $filename=shift;
- my $timeout= shift || $Timeout;
-
- if (-f &masterlockfile($filename) or $iAmMaster) {
-
- # announce the write lock
- # and wait $timeout seconds for
- # references == 0 (no shared locks set)
- #
- &simple_lock ($filename,$timeout) or return;
- for (0..$timeout) {
- # lock reference counter
- # or fail
- #
- unless (&simple_lock (&reffile($filename),$timeout)) {
- &simple_unlock($filename,$timeout);
- return;
- }
-
- # ready if we have no shared locks
- #
- return 1 if (&get_ref ($filename) == 0);
-
- # release reference counter
- # shared locks get the chance to be removed
- #
- unless (&simple_unlock (&reffile($filename),$timeout)) {
- &simple_unlock($filename,$timeout);
- return;
- }
- sleep(1);
- }
-
- # write lock failed
- # remove the announcement
- #
- &simple_unlock ($filename);}
-
- else {
- # master lock is set
- # or file has not been released yet
- #
- return;}
-
- # time out
- # maybe the system is occupied
- #
- 0;
-}
-
-### sub w_write_unlock_file ($;$) ##############################################
-#
-# remove write lock (exclusive lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - locked file
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_write_unlock_file ($;$) {
- my $filename = shift;
- my $timeout = shift || $Timeout;
-
- if (-f &masterlockfile($filename) or $iAmMaster) {
-
- # remove reference counter lock
- #
- &simple_unlock (&reffile($filename),$timeout) or return;
-
- # remove the write lock announce
- #
- &simple_unlock ($filename,$timeout) or return;}
-
- # done
- 1;
-}
-
-### sub w_violent_unlock_file ($) ##############################################
-#
-# remove any lock violent (excl. master lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - locked file
-#
-# Return: -none- (the success is not defined)
-#
-sub w_violent_unlock_file ($) {
- my $filename = shift;
-
- if (-f &masterlockfile($filename)) {
-
- # find out last modification time
- # and do nothing unless 'violent-timout' is over
- #
- my $reffile;
- if (-f ($reffile = $filename) or -f ($reffile = &lockfile($filename))) {
- my $time = (stat $reffile)[9];
- return if ((time - $time) < $violentTimeout);}
-
- write_lock_file ($filename,1); # last try, to set an exclusive lock on $filename
- unlink (&reffile($filename)); # reference counter = 0
- simple_unlock (&reffile($filename)); # release reference counter file
- simple_unlock ($filename);} # release file
-
- return;
-}
-
-### sub w_set_master_lock ($;$) ################################################
-#
-# set master lock
-# (for no-symlink-systems)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_set_master_lock ($;$) {
- my $filename = shift;
- my $timeout = shift || $masterTimeout;
-
- # set exclusive lock or fail
- #
- return unless (&write_lock_file ($filename,$timeout));
-
- # set master lock
- #
- unlink &masterlockfile($filename) and return 1;
-
- # no chance (occupied?, master lock set yet?)
- return;
-}
-
-### sub w_release_file ($) #####################################################
-#
-# remove any locks (incl. master lock)
-# (for no-symlink-systems)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub w_release_file ($) {
- my $filename=shift;
-
- unlink (&reffile($filename)); # reference counter = 0
- return if (-f &reffile($filename)); # really?
- return unless (simple_unlock (&reffile($filename))); # release reference counter
- return unless (&simple_unlock ($filename)); # remove any write lock announce
- return unless (&simple_unlock (&masterfile($filename))); # remove master lock
-
- # done
- 1;
-}
-
-################################################################################
-#
-# *n*x section (symlinks possible)
-#
-
-### sub x_lock_file ($;$) ######################################################
-#
-# set read lock (shared lock)
-# (symlinks possible)
-#
-# Params: $filename - file to lock
-# $timeout - Lock Timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub x_lock_file ($;$) {
- my $filename = shift;
- my $timeout = shift || $Timeout;
-
- unless (-l &masterlockfile($filename)) {
- for (0..$timeout) {
-
- # try to increment the reference counter
- #
- &set_ref($filename,1,$timeout) and return 1;
- sleep (1);
- }
- }
-
- else {
- # master lock is set
- # or file has not been realeased yet
- #
- return;
- }
-
- # time out
- # maybe the system is occupied
- 0;
-}
-
-### sub x_unlock_file ($;$) ####################################################
-#
-# remove read lock (shared lock)
-# (symlinks possible)
-#
-# Params: $filename - locked file
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub x_unlock_file ($;$) {
- my $filename=shift;
- my ($timeout)=(shift (@_) or $Timeout);
-
- unless (-l &masterlockfile($filename)) {
- # try do decrement the reference counter
- #
- &set_ref($filename,-1,$timeout) and return 1;}
-
- # time out
- # maybe the system is occupied
- # or file has not been released yet
- #
- return;
-}
-
-### sub x_write_lock_file ($;$) ################################################
-#
-# set write lock (exclusive lock)
-# (symlinks possible)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub x_write_lock_file ($;$) {
- my $filename = shift;
- my $timeout = shift || $Timeout;
-
- unless (-l &masterlockfile($filename) and not $iAmMaster) {
- # announce the write lock
- # and wait $timeout seconds for
- # references == 0 (no shared locks set)
- #
- &simple_lock ($filename,$timeout) or return;
- for (0..$timeout) {
-
- # lock reference counter
- # or fail
- #
- unless (&simple_lock (&reffile($filename),$timeout)) {
- &simple_unlock($filename,$timeout);
- return;
- }
-
- # ready if we have no shared locks
- #
- return 1 if (&get_ref ($filename) == 0);
-
- # release reference counter
- # shared locks get the chance to be removed
- #
- unless (&simple_unlock (&reffile($filename),$timeout)) {
- &simple_unlock($filename,$timeout);
- return;
- }
- sleep(1);
- }
-
- # write lock failed
- # remove the announcement
- #
- &simple_unlock ($filename);}
-
- else {
- # master lock is set
- # or file has not been released yet
- #
- return;
- }
-
- # time out
- # maybe the system is occupied
- #
- 0;
-}
-
-### sub x_write_unlock_file ($;$) ##############################################
-#
-# remove write lock (exclusive lock)
-# (symlinks possible)
-#
-# Params: $filename - locked file
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub x_write_unlock_file ($;$) {
- my $filename = shift;
- my $timeout = shift || $Timeout;
-
- unless (-l &masterlockfile($filename) and not $iAmMaster) {
- # remove reference counter lock
- #
- &simple_unlock (&reffile($filename),$timeout) or return;
-
- # remove the write lock announce
- #
- &simple_unlock ($filename,$timeout) or return;
- }
-
- # done
- 1;
-}
-
-### sub x_violent_unlock_file ($) ##############################################
-#
-# remove any lock violent (excl. master lock)
-# (symlinks possible)
-#
-# Params: $filename - locked file
-#
-# Return: -none- (the success is not defined)
-#
-sub x_violent_unlock_file ($) {
- my $filename=shift;
-
- unless (-l &masterlockfile($filename)) {
-
- # find out last modification time
- # and do nothing unless 'violent-timout' is over
- #
- my ($reffile,$time);
-
- if (-f ($reffile = $filename)) {
- $time = (stat $reffile)[9];}
-
- elsif (-l ($reffile = &lockfile($filename))) {
- $time = (lstat $reffile)[9];}
-
- if ($reffile) {
- return if ((time - $time) < $violentTimeout);}
-
- write_lock_file ($filename,1); # last try, to set an exclusive lock on $filename
- unlink (&reffile($filename)); # reference counter = 0
- simple_unlock (&reffile($filename)); # release reference counter file
- simple_unlock ($filename);} # release file
-}
-
-### sub x_set_master_lock ($;$) ################################################
-#
-# set master lock
-# (symlinks possible)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)
-#
-# Return: Status Code (Bool)
-#
-sub x_set_master_lock ($;$) {
- my $filename = shift;
- my $timeout = shift || $masterTimeout;
-
- # set exclusive lock or fail
- #
- return unless (&write_lock_file ($filename,$timeout));
-
- # set master lock
- #
- symlink $filename, &masterlockfile($filename) and return 1;
-
- # no chance (occupied?, master lock set yet?)
- return;
-}
-
-### sub x_release_file ($) #####################################################
-#
-# remove any locks (incl. master lock)
-# (symlinks possible)
-#
-# Params: $filename - file to lock
-# $timeout - timeout (sec.)