Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=7324963c77ea75381f253a7fffb46441d9c072df

commit 7324963c77ea75381f253a7fffb46441d9c072df
Author: Michel Hermier <[email protected]>
Date:   Fri Nov 30 16:43:57 2012 +0100

scripts/makepkg

* Introduce makepkg_file_lock function, a utility to do subcommands
while helding a lockfile.

diff --git a/scripts/makepkg b/scripts/makepkg
index ca0ce48..56fd86a 100755
--- a/scripts/makepkg
+++ b/scripts/makepkg
@@ -2429,6 +2429,104 @@ makepkg_distcc() {
)
}

+_makepkg_file_lock() {
+       local i unlock_list
+
+       for i in "$@"; do
+               if (set -o noclobber; echo "$$" > "$i") 2> /dev/null; then
+                       unlock_list+=("$i")
+               else
+                       _makepkg_file_unlock "${unlock_list[@]}"
+                       return 1
+               fi
+       done
+       return 0
+}
+
+_makepkg_file_unlock() {
+       local i ret=0
+
+       for i in "$@"; do
+               rm "$i"
+               if [ $? -ne 0 ]; then
+                       error "Unable to release lock file: $i"
+                       ret=1
+               fi
+       done
+       return $ret
+}
+
+makepkg_file_lock_usage() {
+       $ECHO "makepkg"
+       $ECHO
+       $ECHO "Usage: $0 file_lock [OPTIONS] [--] LOCK_FILE [COMMAND [ARG]...]"
+       $ECHO
+       $ECHO "Options for chroot command:"
+       $ECHO "  -a, --alias            Lock to held while LOCK_FILE is held"
+       $ECHO "  -h, --help             This help"
+       $ECHO "  -p, --parent           Parent lock to be held until LOCK_FILE 
is held"
+       $ECHO
+       $ECHO "If no command is given, run '\${SHELL} -i' (default: '/bin/sh 
-i')."
+}
+
+# FIXME: Reimplement in C with O_EXCL
+makepkg_file_lock() {
+       local lock_list unlock_list
+       local parent_lock_list parent_unlock_list
+       local consumed i ret
+
+       while [ $# -gt 0 ]; do
+               consumed=1
+               case "$1" in
+               -a|--alias)             lock_list+=("$2"); consumed=2 ;;
+               -h|--help)              makepkg_file_lock_usage; return 0 ;;
+               -p|--parent)            parent_lock_list+=("$2"); consumed=2 ;;
+# special options
+               --)                     shift; break;; # End of options
+               --*|-?)                 makepkg_file_lock_usage; return 1 ;;
+               -*)                     # Combined short options
+                                       split="$1"
+                                       shift
+                                       set -- \
+                                               "$(echo "$split}" | cut -c -2)" 
\
+                                               "-$(echo "${split}" | cut -c 
3-)" \
+                                               "$@"
+                                       continue ;;
+               *)                      break ;; # End of options
+               esac
+               if ! shift "$consumed"; then
+                       consumed=$(($consumed - 1))
+                       error "Option $1 expects $consumed argument(s)"
+                       return 1
+               fi
+       done
+
+       if [ $# -eq 0 ]; then
+               error "Argument LOCK_FILE is missing"
+               return 1
+       fi
+       lock_list+=("$1")
+       shift
+
+       ret=1
+       if _makepkg_file_lock "${parent_lock_list[@]}"; then
+               if _makepkg_file_lock "${lock_list[@]}"; then
+                       if _makepkg_file_unlock "${parent_lock_list[@]}"; then
+                               ( # In a subshell so makepkg env is not altered
+                               makepkg_shell "$@"
+                               )
+                               ret=$?
+                       fi
+                       if ! _makepkg_file_unlock "${lock_list[@]}"; then
+                               ret=1
+                       fi
+               else
+                       _makepkg_file_unlock "${parent_lock_list[@]}"
+               fi
+       fi
+       return $ret
+}
+
makepkg_fakeroot_usage() {
$ECHO "makepkg"
$ECHO
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to