There are 3 things of note here:

 - Pass extra arguments to stat_busy, to allow it to take printf-type
   messages.
 - Check to make sure that process doesn't already own the lock.  The code
   for this is mostly taken from mkarchroot; the logic had been dropped
   when the functions were pulled into common.sh
 - The old mkarchroot code used `readlink -f` on the file in /dev/fd to
   resolve symlinks.  This didn't work correctly if there is a symlink
   in the path of the lock file.  One solution would have been to use
   `readlink -f` on both files, but it is simpler to use `[[ a -ef b ]]`.
---
 lib/common.sh | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/lib/common.sh b/lib/common.sh
index d6da1ab..9cc4e1d 100644
--- a/lib/common.sh
+++ b/lib/common.sh
@@ -134,25 +134,43 @@ get_full_version() {
 }
 
 ##
-#  usage : lock( $fd, $file, $message )
+#  usage : lock( $fd, $file, $message, [ $message_arguments... ] )
 ##
 lock() {
-       eval "exec $1>"'"$2"'
-       if ! flock -n $1; then
-               stat_busy "$3"
-               flock $1
+       local fd=$1
+       local file=$2
+       local mesg=("${@:3}")
+
+       # Only reopen the FD if it wasn't handed to us
+       if ! [[ "/dev/fd/$fd" -ef "$file" ]]; then
+               mkdir -p "${file%/*}"
+               eval "exec $fd>"'"$file"'
+       fi
+
+       if ! flock -n $fd; then
+               stat_busy "${mesg[@]}"
+               flock $fd
                stat_done
        fi
 }
 
 ##
-#  usage : slock( $fd, $file, $message )
+#  usage : slock( $fd, $file, $message, [ $message_arguments... ] )
 ##
 slock() {
-       eval "exec $1>"'"$2"'
-       if ! flock -sn $1; then
-               stat_busy "$3"
-               flock -s $1
+       local fd=$1
+       local file=$2
+       local mesg=("${@:3}")
+
+       # Only reopen the FD if it wasn't handed to us
+       if ! [[ "/dev/fd/$fd" -ef "$file" ]]; then
+               mkdir -p "${file%/*}"
+               eval "exec $fd>"'"$file"'
+       fi
+
+       if ! flock -sn $fd; then
+               stat_busy "${mesg[@]}"
+               flock -s $fd
                stat_done
        fi
 }
-- 
1.9.2

Reply via email to