summaryrefslogtreecommitdiffhomepage
path: root/subr/ex_rtl_fileop.subr
diff options
context:
space:
mode:
Diffstat (limited to 'subr/ex_rtl_fileop.subr')
-rw-r--r--subr/ex_rtl_fileop.subr103
1 files changed, 78 insertions, 25 deletions
diff --git a/subr/ex_rtl_fileop.subr b/subr/ex_rtl_fileop.subr
index 9cf44231..0666e7d0 100644
--- a/subr/ex_rtl_fileop.subr
+++ b/subr/ex_rtl_fileop.subr
@@ -2,38 +2,91 @@
# set -o errexit -o noglob are assumed.
#
-ex_build_fileop() {
- local _op="${1}"; shift;
- if [ "${_op}" = cd ]; then
- ex_log_msg vvvo "Changing working directory to \`${1}'.";
- [ \( -n "${1}" \) -a \( -L "${1}" -o -e "${1}" \) ] && cd -- "${1}";
- elif [ "${_op}" = cp ]; then
- ex_log_msg vvvo "Copying \`${1}' to \`${2}' w/ -pPR.";
- [ ${#} -ge 2 ] && cp -pPR -- "${@}";
- elif [ "${_op}" = ln_symbolic ]; then
- ex_log_msg vvvo "Linking \`${1}' to \`${2}' w/ -fs";
- [ \( -n "${1}" \) -a \( -n "${2}" \) ] && ln -fs -- "${1}" "${2}";
- elif [ "${_op}" = mv ]; then
- ex_log_msg vvvo "Moving \`${1}' to \`${2}' w/ -fs";
- [ \( -n "${1}" \) -a \( -n "${2}" \) ] && mv -f -- "${1}" "${2}";
- elif [ "${_op}" = mkdir ]\
- || [ "${_op}" = rm ]; then
+exp_rtl_fileop_check() {
+ local _prefix="${1}" _pname _rname; shift;
+ while [ ${#} -gt 0 ]; do
+ return 0;
+ shift; done;
+};
+
+ex_rtl_fileop() {
+ local _op="${1}" _src _dst; shift;
+ case "${_op}" in
+ cd) if [ \( -z "${1}" \) -o ! \( -L "${1}" -o -e "${1}" \) ]; then
+ ex_rtl_log_msg failexit "Invalid or non-existent directory \`${1}'.";
+ else
+ ex_rtl_log_msg vvvo "Changing working directory to \`${1}'.";
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ cd -- "${1}";
+ fi; ;;
+ cp) if [ ${#} -lt 2 ]; then
+ ex_rtl_log_msg failexit "Missing parameters (in: cp ${*}.)";
+ else
+ _src="${*}"; _src="${_src% *}";
+ _dst="${*}"; _dst="${_dst##* }";
+ ex_rtl_log_msg vvvo "Copying \`${_src}' into \`${_dst}' w/ -pPR.";
+ exp_rtl_fileop_check "${PREFIX}" "${*}";
+ cp -pPR -- "${@}";
+ fi; ;;
+ ln_symbolic)
+ if [ \( -z "${1}" \) -o \( -z "${2}" \) ]; then
+ ex_rtl_log_msg failexit "Missing parameters (in: ln_symbolic ${*}.)";
+ else
+ exp_rtl_fileop_check "${PREFIX}" "${2}";
+ if ex_rtl_fileop test "${2}"; then
+ ex_rtl_fileop rm "${2}";
+ fi;
+ ex_rtl_log_msg vvvo "Linking \`${1}' to \`${2}' w/ -fs";
+ ln -fs -- "${1}" "${2}";
+ fi; ;;
+ mv) if [ \( -z "${1}" \) -o \( -z "${2}" \) ]; then
+ ex_rtl_log_msg failexit "Missing parameters (in: mv ${*}.)";
+ else
+ ex_rtl_log_msg vvvo "Moving \`${1}' to \`${2}' w/ -fs";
+ exp_rtl_fileop_check "${PREFIX}" "${1}" "${2}";
+ mv -f -- "${1}" "${2}";
+ fi; ;;
+ mkdir|mkfifo|rm|source_opt|test|touch)
while [ ${#} -gt 0 ]; do
if [ -z "${1}" ]; then
- return 1;
+ ex_rtl_log_msg failexit "Missing parameters (in: ${_op} ${*}.)";
elif [ "${_op}" = mkdir ]\
- && [ ! -e "${1}" ]; then
- ex_log_msg vvvo "Making directory \`${1}'.";
+ && [ ! -d "${1}" ]; then
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ if ex_rtl_fileop test "${1}"; then
+ ex_rtl_fileop rm "${1}";
+ fi;
+ ex_rtl_log_msg vvvo "Making directory \`${1}'.";
mkdir -p -- "${1}";
+ elif [ "${_op}" = mkfifo ]; then
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ if ex_rtl_fileop test "${1}"; then
+ ex_rtl_fileop rm "${1}";
+ fi;
+ ex_rtl_log_msg vvvo "Creating FIFO \`${1}'.";
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ mkfifo -- "${1}";
elif [ "${_op}" = rm ]\
- && [ \( -L "${1}" \) -o \( -e "${1}" \) ]; then
- ex_log_msg vvvo "Removing directory or file \`${1}'.";
+ && ex_rtl_fileop test "${1}"; then
+ ex_rtl_log_msg vvvo "Removing directory or file \`${1}'.";
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
rm -rf -- "${1}";
+ elif [ "${_op}" = source_opt ]\
+ && ex_rtl_fileop test "${1}"; then
+ ex_rtl_log_msg vvvo "Sourcing file \`${1}'.";
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ . "${1}";
+ elif [ "${_op}" = test ]\
+ && ! [ \( -L "${1}" \) -o \( -e "${1}" \) ]; then
+ return 1;
+ elif [ "${_op}" = touch ]; then
+ ex_rtl_log_msg vvvo "Touching file \`${1}'.";
+ exp_rtl_fileop_check "${PREFIX}" "${1}";
+ touch -- "${1}";
fi; shift;
- done;
- else
- ex_log_msg failexit "Error: ex_build_fileop() called w/ invalid parameter(s) \`${@}'.";
- fi;
+ done; ;;
+ *) ex_rtl_log_msg failexit "Error: ex_rtl_fileop() called w/ invalid parameter(s): ${*}"; ;;
+ esac; return 0;
};
# vim:filetype=sh