refcodes-batch.0.3.0.source-code.lib-filesystem.inc Maven / Gradle / Ivy
#!/bin/bash
# ------------------------------------------------------------------------------
# This lib relies on the "apply.sh" shell script.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Makes a path recursively and sets the according access rights an folders not
# existing before:
#
# $1: The path to create
# $2: The filesystem user to set for newly created folders
# $3: The filesystem group to set for newly created folders
# $4: The filesystem access rights to set for newly created folders
# ------------------------------------------------------------------------------
function makePath {
if [[ $# == 0 ]] ; then
showError "Arguments missing: The function \"makePath\" expects at least one argument !!!"
exit $EXIT_BUG
fi
MAKE_DIR=$1
FS_USER=$2
FS_GROUP=$3
FS_ACCESS=$4
PATH_CHAIN=""
OLDIFS=$IFS
FIRST=y
IFS='/'
for filename in $MAKE_DIR ; do
if [[ $FIRST = y ]] ; then
if [ -z "$filename" ] ; then
PATH_CHAIN="/"
fi
if [ ! -z "$filename" ] ; then
PATH_CHAIN="$filename"
fi
fi
if [[ $FIRST = n ]] ; then
if [[ "$PATH_CHAIN" != "/" ]] ; then
PATH_CHAIN="$PATH_CHAIN/$filename"
fi
if [[ "$PATH_CHAIN" == "/" ]] ; then
PATH_CHAIN="$PATH_CHAIN$filename"
fi
fi
if [ ! -e "$PATH_CHAIN" ] ; then
mkdir "$PATH_CHAIN"
exitOnError "Unable to create directory \"$PATH_CHAIN\" !!!"
if [ -n "$FS_USER" ] ; then
chown "$FS_USER" "$PATH_CHAIN"
exitOnError "Unable to change owner to \"$FS_USER\" for \"$PATH_CHAIN\" !!!"
fi
if [ -n "$FS_GROUP" ] ; then
chgrp "$FS_GROUP" "$PATH_CHAIN"
exitOnError "Unable to change group to \"$FS_GROUP\" for \"$PATH_CHAIN\" !!!"
fi
if [ -n "$FS_ACCESS" ] ; then
chmod "$FS_ACCESS" "$PATH_CHAIN"
exitOnError "Unable to change access rights \"$FS_ACCESS\" for \"$PATH_CHAIN\" !!!"
fi
fi
FIRST=n
done
IFS=$OLDIFS
}