pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/mk/scripts Add a lock_file function that generates loc...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/f95b8cd37771
branches:  trunk
changeset: 480847:f95b8cd37771
user:      jlam <jlam%pkgsrc.org@localhost>
date:      Tue Sep 21 15:14:08 2004 +0000

description:
Add a lock_file function that generates lockfiles that are usable on
NFS-mounted directories.

diffstat:

 mk/scripts/shell-lib |  58 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)

diffs (79 lines):

diff -r 50976a86a9e3 -r f95b8cd37771 mk/scripts/shell-lib
--- a/mk/scripts/shell-lib      Tue Sep 21 15:10:26 2004 +0000
+++ b/mk/scripts/shell-lib      Tue Sep 21 15:14:08 2004 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: shell-lib,v 1.1 2004/09/06 18:33:23 jlam Exp $
+# $NetBSD: shell-lib,v 1.2 2004/09/21 15:14:08 jlam Exp $
 #
 # Copyright (c) 2004 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -33,6 +33,7 @@
 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
+#
 
 ######################################################################
 # msg_log logfile msg
@@ -119,6 +120,61 @@
 }
 
 ######################################################################
+# lock_file -f path [-n token]
+#      Attempt to create a lockfile at $path.  Any directories in the
+#      path should already exist.  If $token is specified, then assume
+#      that it is unique between machines sharing an NFS mount.
+#
+# (1) Create globally-unique filename in the same filesystem as the
+#     lockfile.
+# (2) Try to create a hard-link from this file to the lockfile, but
+#     ignoring any errors.
+# (3) If the two files are the same file, then the lock was successfully
+#     obtained; otherwise, the lock attempt wasn't successful.
+######################################################################
+lock_file()
+{
+       : ${dirname=dirname}
+       : ${echo=echo}
+       : ${link=link}
+       : ${mkdir=mkdir}
+       : ${mktemp=mktemp}
+       : ${rm=rm}
+       : ${test=test}
+       : ${touch=touch}
+
+       _lf_lockfile=
+       _lf_nfs=
+       while $test $# -gt 0; do
+               case $1 in
+               -f)     _lf_lockfile="$2"; shift ;;
+               -n)     _lf_nfs="$2"; shift ;;
+               esac
+               shift
+       done
+       if $test -z "$_lf_lockfile"; then
+               $echo 1>&2 "$0: no lock file specified."
+               exit
+       fi
+       _lf_pid=$$
+       _lf_lockdir=`$dirname $_lf_lockfile`
+       _lf_uniqfile=`$mktemp "$_lf_lockdir/.lock.$_lf_nfs.$_lf_pid.XXXXXX" 2>/dev/null` || return 1
+       if $test -n "$_lf_nfs"; then
+               { $echo $_lf_pid; $echo $_lf_nfs; } > $_lf_uniqfile
+       else
+               $echo $_lf_pid > $_lf_uniqfile
+       fi
+       $link $_lf_uniqfile $_lf_lockfile 2>/dev/null
+       if $test $_lf_uniqfile -ef $_lf_lockfile; then
+               _lf_result=0
+       else
+               _lf_result=1
+       fi
+       $rm -f $_lf_uniqfile
+       return $_lf_result
+}
+
+######################################################################
 ######################################################################
 ###
 ### Queue routines.  The queue is implemented as a set of variables



Home | Main Index | Thread Index | Old Index