X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=lib%2Ftools.in;h=4ca438f05e8dc17fc7d44537e66145609a0d1dc4;hb=refs%2Fheads%2Fvsfixes;hp=37fa2129105ede12e8da22113870056e32a4297b;hpb=c6a1b1230dcaaf14efa296cd29e748e749a0ed3a;p=matthijs%2Fupstream%2Fbackupninja.git diff --git a/lib/tools.in b/lib/tools.in index 37fa212..4ca438f 100644 --- a/lib/tools.in +++ b/lib/tools.in @@ -10,14 +10,7 @@ # create a temporary file in a secure way. # function maketemp() { - if [ -x /bin/mktemp ] - then - local tempfile=`mktemp /tmp/$1.XXXXXXXX` - else - DATE=`date` - sectmp=`echo $DATE | @MD5SUM@ | cut -d- -f1` - local tempfile=/tmp/$1.$sectmp - fi + local tempfile=`mktemp /tmp/$1.XXXXXXXX` echo $tempfile } @@ -53,3 +46,62 @@ function getconf() { # because it is in an 'eval' statement. eval $1='$ret' } + +# +# Replaces escape sequences in $1 with the proper values for the vserver $2. +# The result is put on stdout. If $2 is empty, values for the host are +# replaced. The following values are replaced: +# +# %% Literal % +# %h The short hostname (as returned by hostname -s) +# %H The full hostname (as returned by hostname --fqdn) +# %n The vserver name, or empty for the host +# %N The vserver name, or "host" for the host +# %v The vserver root directory, or empty for the host +# +# Note that the given vserver must be running! +# +function interpolate() { + path=$1 + vsname=$2 + vexec=${vsname:+$VSERVER $vsname exec } + # Find the values to replace + h=`$vexec hostname -s` + H=`$vexec hostname --fqdn` + n=$vsname + N=${vsname:-host} + v=${vsname:+$VROOTDIR/$vsname} + + expr='' + for var in h H n N v; do + # Do indirect lookup of the value to replace + val=${!var} + # Escape slashes (twice, bash seems to eat one somewhere) + val=`echo "$val" | sed 's#/#\\\\/#g'` + # Add replacement pattern. The first part checks that there is + # an odd number of percent signs before the variable, so + # double % is properly left alone. + expr="${expr}s/\(\(^\|[^%]\)\(%%\)*\)%$var/\1$val/g;" + done + # Finally replace literal % signs + expr="${expr}s/%%/%/g" + + # Do the actual interpolation + echo $path | sed "$expr" +} + +# Does the $1 start with $2 ? +function starts_with() { + # Get the head of $1 as long as $2 + head=`echo $1 | head -c ${#2}` + # Is it equal to $2? + [ "$head" = "$2" ] +} + +# Strip the prefix $2 from $1. Assumes that $1 actually starts with $1. +# The result is put on stdout. +function strip_prefix() { + # Strip the first ${#2} (the length of $2) characters from $1. + # tail -c +N means start at the Nth byte, 1-based, so we add 1. + echo $1 | tail -c +$((${#2}+1)) +}