This commit was generated by cvs2svn to compensate for changes in r2562,
[util-vserver.git] / scripts / functions
index a8f451d..d2e00d3 100644 (file)
@@ -1,6 +1,6 @@
-# $Id: functions,v 1.62 2005/07/03 17:42:49 ensc Exp $ --*- sh -*--
+# $Id: functions 2420 2006-12-09 16:02:03Z dhozac $    --*- sh -*--
 
-# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
+# Copyright (C) 2003,2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 #  
 # 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
@@ -84,6 +84,29 @@ function getPhysicalDir
     ( set -P && cd "$1" && pwd )
 }
 
+## Usage:: isDirectoryEmpty <dir> [<ignore-lost+found>]
+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 <message>
 function logging
 {
@@ -180,11 +203,34 @@ function colorize
          esac
        fi
            
-       "$@"
+       ( "$@" )
        echo -ne "\e[m"
     fi
 }
 
+function colpanic
+{
+    if test -n "$_VS_ERRFILE"; then
+       echo "$@" >>"$_VS_ERRFILE"
+    else
+       colorize error echo -n "$@" >&2
+    fi
+    echo
+
+    exit 1
+}
+
+function colwarning
+{
+    colorize warning warning "$@"
+}
+
+function colinfo
+{
+    colorize info    echo "$@"
+}
+
+
 ## Usage: xtermTitle <title>
 function xtermTitle
 {
@@ -196,7 +242,7 @@ _VS_LOCKS=''
 ## Usage: lock <lockfile> [<timeout>]
 function lock
 {
-    local tmp=$($_MKTEMP /tmp/vserver-lock.XXXXXX)
+    local tmp=$($_MKTEMP vserver-lock.XXXXXX)
     $_RM -f $tmp
     $_MKFIFO -m600 $tmp
 
@@ -222,6 +268,30 @@ function unlock
     _VS_LOCKS="$@"
 }
 
+function get_init_cwd
+{
+    if test -n "$INIT_CWD"; then
+       echo "$INIT_CWD"
+    else
+       pwd
+    fi
+}
+function set_init_cwd
+{
+    INIT_CWD="`pwd`"
+    export INIT_CWD
+}
+
+function _getVserverDir
+{
+    local vserver="$1"
+    case "$vserver" in
+       ./*) VSERVER_DIR="`get_init_cwd`/$vserver";;
+       /*)  VSERVER_DIR="$vserver"               ;;
+       *)   VSERVER_DIR="$__CONFDIR/$vserver"    ;;
+    esac
+}
+
 function _pkgMountBindDir()
 {
     test "$1" != "$2" || return 0
@@ -434,6 +504,18 @@ function isAvoidNamespace
          -e "$cfgdir"/nonamespace
 }
 
+function isNamespaceCleanup
+{
+    local cfgdir
+
+    $_VSERVER_INFO - FEATURE namespace   || return 1
+    cfgdir=$($_VSERVER_INFO "$1" CFGDIR) || return 1
+    test -e "$cfgdir"/namespace-cleanup  && return 0
+    test -e "$cfgdir"/nonamespace-cleanup -o \
+         -e "$__CONFDIR"/.defaults/nonamespace-cleanup && return 1
+    return 0
+}
+
 ## Usage: getAllVservers <var> [<KIND>*]
 function getAllVservers
 {
@@ -554,13 +636,17 @@ function getFileValue
     eval read "$_gfv_var" <"$_gfv_file"
 }
 
-## Called as 'getFileArray <varname> <filename>'
+## Called as 'getFileArray <varname> <filename>+'
 function getFileArray
 {
-    test -r "$2" || return 1
-    
+    local _gfa_var=$1
+    local _gfa_file
+    shift
+
+    findFile _gfa_file "$@" ''
+    test -n "$_gfa_file" -a -r "$_gfa_file" || return 0
     local IFS=$_VS_NEWLINE
-    eval "$1"='( $(< "$2") )'
+    eval "$_gfa_var"='( $(< "$_gfa_file") )'
 }
 
 function checkComponents
@@ -643,6 +729,11 @@ function killContext
     #$_VKILL -s CONT   --xid "$1" 1 &>/dev/null || :
 }
 
+function useVlogin
+{
+    test ! -e "$__CONFDIR/.defaults/apps/vlogin/disable"
+}
+
 ## Usage: pkgmgmt.guessStyle <vserver> <resultvar>
 function pkgmgmt.guessStyle()
 {
@@ -923,7 +1014,7 @@ function vshelper.doDestroy
 function vshelper.initSync
 {
     local _vis_tmpdir
-    _vis_tmpdir=$($_MKTEMPDIR /tmp/vserver-stop.XXXXXX) || {
+    _vis_tmpdir=$($_MKTEMPDIR vserver-stop.XXXXXX) || {
        warning $"Failed to generate temporary directory for vshelper sync"
        return 1
     }
@@ -946,6 +1037,30 @@ function vshelper.getSyncTimeout
     eval $2=\$_vgst_tmp
 }
 
+function vshelper.initStopSync
+{
+       local _iss_sync_dir=$($_MKTEMPDIR vshelper-stop-sync.XXXXXX) || {
+               warning $"Failed to generate directory for vshelper sync"
+               exit 1
+       }
+       $_MKFIFO -m700 "$_iss_sync_dir/pipe"
+
+       eval "$1"=\$_iss_sync_dir
+       VSHELPER_STOP_SYNC="$_iss_sync_dir/pipe"
+       export VSHELPER_STOP_SYNC
+}
+
+function vshelper.waitForStopSync
+{
+       local sync_dir=$1
+       cat "$sync_dir/pipe" &> /dev/null
+       rm -fr "$sync_dir"
+}
+
+function vshelper.doStopSync
+{
+       test ! -p "$VSHELPER_STOP_SYNC" || echo stopped > "$VSHELPER_STOP_SYNC"
+}
 
 function _rpmFake.getCapFlags
 {