[arch-general] [PATCH 08/48] Slightly simplify hook-running infrastructure.

Thomas Bächler thomas at archlinux.org
Wed Jun 30 18:00:41 EDT 2010


Am 30.06.2010 23:47, schrieb Victor Lowther:
> Go ahead and declare add_hook and run_hook as readonly functions
> to prevent hooks from overwriting them.  Too bad bash does not have
> lexically scoped variables -- if it does, we could do the same with
> the hook_funcs associative array.

Lots of hunks below that have nothing to do with the actual change made
here:

> diff --git a/functions b/functions
> index 8bbdfc6..d9f55fa 100644
> --- a/functions
> +++ b/functions
> @@ -148,38 +148,39 @@ in_array() {
>  # daemons:
>  
>  add_daemon() {
> -	[ -d /var/run/daemons ] || /bin/mkdir -p /var/run/daemons
> -	/bin/touch /var/run/daemons/$1
> +	[[ -d /var/run/daemons ]] || /bin/mkdir -p /var/run/daemons
> +	> /var/run/daemons/"$1"
>  }
>  
>  rm_daemon() {
> -	/bin/rm -f /var/run/daemons/$1
> +	/bin/rm -f /var/run/daemons/"$1"
>  }
>  
>  ck_daemon() {
> -	[ -f /var/run/daemons/$1 ] && return 1
> -	return 0
> +	[[ ! -f /var/run/daemons/$1 ]]
>  }
>  
> -ck_depends() {
> -	for daemon in $@; do
> -		if ck_daemon $daemon; then
> -			/etc/rc.d/$daemon start
> -		fi
> -	done
> +have_daemon() {
> +    [[ -x /etc/rc.d/"$1" ]]
>  }
>  
>  start_daemon() {
> -	/etc/rc.d/$1 start
> +    have_daemon "$1" && /etc/rc.d/"$1" start
> +}
> +
> +ck_depends() {
> +    for daemon in "$@"; do
> +	ck_daemon "$daemon" && start_daemon "$daemon" 
> +    done
>  }
>  
>  start_daemon_bkgd() {
>  	stat_bkgd "Starting $1"
> -	(/etc/rc.d/$1 start) &>/dev/null &
> +	have_daemon "$1" && (start_daemon "$1") &>/dev/null &
>  }
>  
>  stop_daemon() {
> -	/etc/rc.d/$1 stop
> +	have_daemon "$1" && /etc/rc.d/"$1" stop
>  }

Here the actual patch starts, the above doesn't belong into this patch.
You should _really_ check out git add -p/-i

>  # Status functions
> @@ -234,24 +235,26 @@ ck_status() {
>  # single_postkillall: after all processes have been killed in rc.single
>  # shutdown_poweroff: directly before powering off in rc.shutdown
>  #
> -# Make sure to never override the add_hook and run_hook functions via functions.d
> +# Declare add_hook and run_hook as read-only to prevent overwriting them.
> +# Too bad we cannot do the same thing with hook_funcs 
>  
>  declare -A hook_funcs
>  
>  add_hook() {
> -	[ -z "$1" -o -z "$2" ] && return 1
> -	hook_funcs["$1"]="${hook_funcs["$1"]} $2"
> +	[[ $1 && $2 ]] || return 1
> +	hook_funcs["$1"]+=" $2"
>  }

+= in bash? Really? Didn't know that worked.

>  run_hook() {
> -	local func
> -
> -	[ -z "$1" ] && return 1
> -	for func in ${hook_funcs["$1"]}; do
> -		${func}
> -	done
> +    [[ $1 ]] || return 1
> +    local func
> +    for func in ${hook_funcs["$1"]}; do
> +	"${func}"
> +    done
>  }
>  
> +declare -r add_hook run_hook
> +

Nice, I didn't know you could do that.

>  # Function for setting console font if required
>  set_consolefont() {
>      if [ -n "$CONSOLEFONT" ]; then


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://mailman.archlinux.org/pipermail/arch-general/attachments/20100701/d94a654f/attachment.bin>


More information about the arch-general mailing list