X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fscripts%2Ffunctions;h=82554878c2ae6b95955ae4360ccee316b33fbc17;hb=75385100bedf6d457e88bc2bb942a6f0771b0e06;hp=f9215f760517eb70e7d9ab2ccefaff78cecd5045;hpb=0125615e9a648dbfd11f4ddfa6179f1ee98a25fe;p=util-vserver.git diff --git a/util-vserver/scripts/functions b/util-vserver/scripts/functions index f9215f7..8255487 100644 --- a/util-vserver/scripts/functions +++ b/util-vserver/scripts/functions @@ -1,6 +1,6 @@ # $Id$ --*- sh -*-- -# Copyright (C) 2003 Enrico Scholz +# Copyright (C) 2003,2004,2005,2006 Enrico Scholz # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,29 +15,33 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +_VS_LOGFILE= +_VS_ERRFILE= + _VS_NEWLINE=' ' declare -r _VS_NEWLINE=${_VS_NEWLINE:0-1} +declare -r VS_ALLVSERVERS_ARGS=all,marked,unmarked,stopped,running function findObject { - local mod=$1 - local var=$2 - local file= - local i=X + local _fo_mod=$1 + local _fo_var=$2 + local _fo_file= + local _fo_i=X shift 2 - for i; do - test "$i" || continue - test ! $mod "$i" || { file=$i; break; } + for _fo_i; do + test -n "$_fo_i" || continue + test ! $_fo_mod "$_fo_i" || { _fo_file=$_fo_i; break; } done - test -z "$i" -o "$file" || { - echo "Can not find file for '$var'; aborting" + test -z "$_fo_i" -o -n "$_fo_file" || { + echo "Can not find file for '$_fo_var'; aborting" exit 1 } >&2 - eval "$var=\"$file\"" + eval "$_fo_var=\"$_fo_file\"" } function findFile @@ -59,8 +63,20 @@ function findAndCopy shift findFile tmp "$@" - test "$tmp" -a -s "$tmp" || return 0 - cp -af "$tmp" "$dst" + test -n "$tmp" -a -s "$tmp" || return 0 + $_CP -af "$tmp" "$dst" +} + +## Usage: isRegularFile [] +function isRegularFile +{ + test ${2:--f} "$1" || return 1 + + case $1 in + (*.rpmsave|*.rpmnew|*.rpmorig|*.cfsaved*|*.~*~) return 1;; + esac + + return 0 } function getPhysicalDir @@ -68,17 +84,201 @@ function getPhysicalDir ( set -P && cd "$1" && pwd ) } +## Usage:: isDirectoryEmpty [] +function isDirectoryEmpty +{ + ( + shopt -s dotglob || * + shopt -s nullglob || * + + ignore_lostfound=1 + test -z "$2" -o "$2" = 0 || ignore_lostfound= + + for i in "$1"/*; do + case "$i" in + ($1/lost+found) + test "$ignore_lostfound" || exit 1 + ;; + (*) echo "$i"; exit 1 + esac + done + + exit 0 + ) +} + +## Usage: logging +function logging +{ + if test -n "$_VS_LOGFILE"; then + echo "$@" >>"$_VS_LOGFILE" + else + echo "$@" + fi +} + +## Usage: warning +function warning +{ + if test -n "$_VS_ERRFILE"; then + echo "$@" >>"$_VS_ERRFILE" + else + echo "$@" >&2 + fi +} + +## Usage: panic +function panic +{ + if test -n "$_VS_ERRFILE"; then + echo "$@" >>"$_VS_ERRFILE" + else + echo "$@" >&2 + fi + + exit 1 +} + +## Usage: execute +function execute +{ + test -z "${DEBUG_EXEC:-}" || echo "$@" + test "${DEBUG_EXEC:-}" = noexec || exec "$@" + exit 0 +} + + +## Usage: spawn +function spawn +{ + test -z "${DEBUG_EXEC:-}" || echo "$@" + test "${DEBUG_EXEC:-}" = noexec || "$@" +} + +## Usage: isNumber +function isNumber +{ + local tmp + let tmp=$1+0 2>/dev/null || test -z "${1//0/}" -a -n "$1" || return 1 + return 0 +} + +## Usage: hasSubstring + +function hasSubstring +{ + local pat=$1 + local i + + shift + + for i; do + test x"${pat/*$i*/$i}" = x"$i" || continue + return 0 + done + + return 1 +} + +## Usage: colorize