Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: uses "zsystem flock" for calendar file locking



This uses the new file locking for the calendar system where available.
This is probably a useful enhancement for the one and a half of us using
it.

Note I deliberately didn't use a timeout for the new form.  Locking
should be much more reliable and the file only locked for short periods
of time, while experience suggests blocking for a lock is much more
effective than waking up to poll, which has pathologies if multiple
files are trying to grab the lock.  The only case where I can see this
leading to long delays is with calendar_edit.

I've also added a test to abandon lock attempts if an interrupt sets the
error flag.

Index: Doc/Zsh/calsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/calsys.yo,v
retrieving revision 1.16
diff -p -u -r1.16 calsys.yo
--- Doc/Zsh/calsys.yo	2 Mar 2009 10:12:15 -0000	1.16
+++ Doc/Zsh/calsys.yo	26 Feb 2010 11:17:53 -0000
@@ -470,7 +470,9 @@ tt(calendar -s) is called to update it.
 
 This function locks out the calendar system during the edit.
 Hence it should be used to edit the calendar file if there is any
-possibility of a calendar event occurring meanwhile.
+possibility of a calendar event occurring meanwhile.  Note this
+can lead to another shell with calendar functions enabled hanging waiting
+for a lock, so it is necessary to quit the editor as soon as possible.
 )
 findex(calendar_parse)
 item(tt(calendar_parse) var(calendar-entry))(
Index: Functions/Calendar/calendar
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Calendar/calendar,v
retrieving revision 1.11
diff -p -u -r1.11 calendar
--- Functions/Calendar/calendar	3 Nov 2009 09:57:39 -0000	1.11
+++ Functions/Calendar/calendar	26 Feb 2010 11:17:53 -0000
@@ -253,13 +253,21 @@ if (( verbose )); then
   fi
 fi
 
+# start of subshell for OS file locking
+(
 # start of block for following always to clear up lockfiles.
+# Not needed but harmless if OS file locking is used.
 {
   if [[ -n $donefile ]]; then
     # Attempt to lock both $donefile and $calendar.
     # Don't lock $newfile; we've tried our best to make
     # the name unique.
-    calendar_lockfiles $calendar $donefile || return 1
+    if zmodload -F zsh/system b:zsystem && zsystem supports flock; then
+      zsystem flock $calendar
+      zsystem flock $donefile
+    else
+      calendar_lockfiles $calendar $donefile || exit 1
+    fi
   fi
 
   calendar_read $calendar
@@ -414,4 +422,5 @@ Old calendar left in $calendar.old." >&2
   (( ${#lockfiles} )) && rm -f $lockfiles
 }
 
-return $rstat
+exit $rstat
+)
Index: Functions/Calendar/calendar_add
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Calendar/calendar_add,v
retrieving revision 1.10
diff -p -u -r1.10 calendar_add
--- Functions/Calendar/calendar_add	22 Feb 2008 02:56:14 -0000	1.10
+++ Functions/Calendar/calendar_add	26 Feb 2010 11:17:53 -0000
@@ -68,9 +68,18 @@ if [[ $addline = ${~uidpat} ]]; then
   my_uid=${(U)match[1]}
 fi
 
+# start of subshell for OS file locking
+(
 # start of block for following always to clear up lockfiles.
+# Not needed but harmless if OS file locking is used.
 {
-  (( nolock )) || calendar_lockfiles $calendar || return 1
+  if (( ! nolock )); then
+    if zmodload -F zsh/system b:zsystem && zsystem supports flock; then
+      zsystem flock $calendar
+    else
+      calendar_lockfiles $calendar || exit 1
+    fi
+  fi
 
   if [[ -f $calendar ]]; then
     calendar_read $calendar
@@ -158,4 +167,5 @@ Old calendar left in $calendar.old." >&2
   (( ${#lockfiles} )) && rm -f $lockfiles
 }
 
-return $rstat
+exit $rstat
+)
Index: Functions/Calendar/calendar_edit
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Calendar/calendar_edit,v
retrieving revision 1.1
diff -p -u -r1.1 calendar_edit
--- Functions/Calendar/calendar_edit	26 Mar 2007 14:33:33 -0000	1.1
+++ Functions/Calendar/calendar_edit	26 Feb 2010 11:17:53 -0000
@@ -10,12 +10,21 @@ done
 
 zstyle -s ':datetime:calendar:' calendar-file calendar || calendar=~/calendar
 
+# start of subshell for OS file locking
+(
+# start of block for following always to clear up lockfiles.
+# Not needed but harmless if OS file locking is used.
 {
-  calendar_lockfiles $calendar || return 1
+  if zmodload -F zsh/system b:zsystem && zsystem supports flock; then
+    zsystem flock $calendar
+  else
+    calendar_lockfiles $calendar || exit 1
+  fi
 
   eval $editor \$calendar
 } always {
   (( ${#lockfiles} )) && rm -f $lockfiles
 }
+)
 
 (( cal_running )) && calendar -s
Index: Functions/Calendar/calendar_sort
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Calendar/calendar_sort,v
retrieving revision 1.1
diff -p -u -r1.1 calendar_sort
--- Functions/Calendar/calendar_sort	1 Dec 2006 10:23:07 -0000	1.1
+++ Functions/Calendar/calendar_sort	26 Feb 2010 11:17:53 -0000
@@ -11,9 +11,16 @@ integer i
 # Read the calendar file from the calendar-file style
 zstyle -s ':datetime:calendar:' calendar-file calendar || calendar=~/calendar
 
-# Start block for "always" to handle lockfile
+# start of subshell for OS file locking
+(
+# start of block for following always to clear up lockfiles.
+# Not needed but harmless if OS file locking is used.
 {
-  calendar_lockfiles $calendar || return 1
+  if zmodload -F zsh/system b:zsystem && zsystem supports flock; then
+    zsystem flock $calendar
+  else
+    calendar_lockfiles $calendar || exit 1
+  fi
 
   new=$calendar.new.$$
   calendar_read $calendar
@@ -65,3 +72,4 @@ Old calendar left in $calendar.old"
 } always {
   (( ${#lockfiles} )) && rm -rf $lockfiles
 }
+)
Index: Src/Modules/system.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/system.c,v
retrieving revision 1.11
diff -p -u -r1.11 system.c
--- Src/Modules/system.c	25 Feb 2010 11:16:08 -0000	1.11
+++ Src/Modules/system.c	26 Feb 2010 11:17:53 -0000
@@ -463,6 +463,8 @@ bin_zsystem_flock(char *nam, char **args
     if (timeout > 0) {
 	time_t end = time(NULL) + (time_t)timeout;
 	while (fcntl(flock_fd, F_SETLK, &lck) < 0) {
+	    if (errflag)
+		return 1;
 	    if (errno != EINTR && errno != EACCES && errno != EAGAIN) {
 		zwarnnam(nam, "failed to lock file %s: %e", args[0], errno);
 		return 1;
@@ -473,6 +475,8 @@ bin_zsystem_flock(char *nam, char **args
 	}
     } else {
 	while (fcntl(flock_fd, F_SETLKW, &lck) < 0) {
+	    if (errflag)
+		return 1;
 	    if (errno == EINTR)
 		continue;
 	    zwarnnam(nam, "failed to lock file %s: %e", args[0], errno);


-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



Messages sorted by: Reverse Date, Date, Thread, Author