diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/common.sh | 69 |
1 files changed, 49 insertions, 20 deletions
diff --git a/lib/common.sh b/lib/common.sh index f6aea93..b1e7d6e 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -4,7 +4,7 @@ export LANG=C shopt -s extglob # check if messages are to be printed using color -unset ALL_OFF BOLD BLUE GREEN RED YELLOW +declare ALL_OFF= BOLD= BLUE= GREEN= RED= YELLOW= if [[ -t 2 ]]; then # prefer terminal safe colored and bold text when tput is supported if tput setaf 0 &>/dev/null; then @@ -42,29 +42,35 @@ msg2() { warning() { local mesg=$1; shift - printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 + printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } error() { local mesg=$1; shift - printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 + printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2 } stat_busy() { local mesg=$1; shift - printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" >&2 + printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" "$@" >&2 } stat_done() { - printf "${BOLD}done${ALL_OFF}\n" >&2 + printf "${BOLD}$(gettext "done")${ALL_OFF}\n" >&2 } +_setup_workdir=false setup_workdir() { - [[ -z $WORKDIR ]] && WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX") + [[ -z ${WORKDIR:-} ]] && WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX") + _setup_workdir=true + trap 'trap_abort' INT QUIT TERM HUP + trap 'trap_exit' EXIT } cleanup() { - [[ -n $WORKDIR ]] && rm -rf "$WORKDIR" + if [[ -n ${WORKDIR:-} ]] && $_setup_workdir; then + rm -rf "$WORKDIR" + fi exit ${1:-0} } @@ -89,9 +95,6 @@ die() { cleanup 255 } -trap 'trap_abort' INT QUIT TERM HUP -trap 'trap_exit' EXIT - ## # usage : in_array( $needle, $haystack ) # return : 0 - found @@ -135,30 +138,56 @@ 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 } ## +# usage : lock_close( $fd ) +## +lock_close() { + local fd=$1 + exec {fd}>&- +} + +## # usage: pkgver_equal( $pkgver1, $pkgver2 ) ## pkgver_equal() { |