Blame subr/ex_rtl_fileop.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz (arab, vxp) 0185bc
# set -o noglob is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
exp_rtl_fileop_check() {
Lucio Andrés Illanes Albornoz 907bd3
	local _prefix="${1}" _pname="" _rname=""; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	shift; done;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
ex_rtl_fileop() {
Lucio Andrés Illanes Albornoz 907bd3
	local _op="${1}" _src="" _dst=""; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	case "${_op}" in
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	cd)	if [ \( -z "${1}" \) -o ! \( -L "${1}" -o -e "${1}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			ex_rtl_log_msg failexit "Invalid or non-existent directory \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		else
Lucio Andrés Illanes Albornoz f0dfe1
			ex_rtl_log_msg vvvv "Changing working directory to \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			cd -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
	cp_follow)
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
		if [ ${#} -lt 2 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
			ex_rtl_log_msg failexit "Missing parameters (in: cp_follow ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
		else
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
			_src="${*}"; _src="${_src% *}";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
			_dst="${*}"; _dst="${_dst##* }";
Lucio Andrés Illanes Albornoz f0dfe1
			ex_rtl_log_msg vvvv "Copying \`${_src}' into \`${_dst}' w/ -pLR.";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
			exp_rtl_fileop_check "${PREFIX}" "${*}";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
			cp -pLR -- "${@}";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	cp)	if [ ${#} -lt 2 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			ex_rtl_log_msg failexit "Missing parameters (in: cp ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		else
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			_src="${*}"; _src="${_src% *}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			_dst="${*}"; _dst="${_dst##* }";
Lucio Andrés Illanes Albornoz f0dfe1
			ex_rtl_log_msg vvvv "Copying \`${_src}' into \`${_dst}' w/ -pPR.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			exp_rtl_fileop_check "${PREFIX}" "${*}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			cp -pPR -- "${@}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	ln_symbolic)
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		if [ \( -z "${1}" \) -o \( -z "${2}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			ex_rtl_log_msg failexit "Missing parameters (in: ln_symbolic ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		else
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			exp_rtl_fileop_check "${PREFIX}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			if ex_rtl_fileop test "${2}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				ex_rtl_fileop rm "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			fi;
Lucio Andrés Illanes Albornoz f0dfe1
			ex_rtl_log_msg vvvv "Linking \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			ln -fs -- "${1}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	mv)	if [ \( -z "${1}" \) -o \( -z "${2}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			ex_rtl_log_msg failexit "Missing parameters (in: mv ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		else
Lucio Andrés Illanes Albornoz f0dfe1
			ex_rtl_log_msg vvvv "Moving \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			exp_rtl_fileop_check "${PREFIX}" "${1}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			mv -f -- "${1}" "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	mkdir|mkfifo|rm|source_opt|test|touch)
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
		while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				ex_rtl_log_msg failexit "Missing parameters (in: ${_op} ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			elif [ "${_op}" = mkdir ]\
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			&& [ ! -d "${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				if ex_rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
					ex_rtl_fileop rm "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				fi;
Lucio Andrés Illanes Albornoz f0dfe1
				ex_rtl_log_msg vvvv "Making directory \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
				mkdir -p -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = mkfifo ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				if ex_rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
					ex_rtl_fileop rm "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				fi;
Lucio Andrés Illanes Albornoz f0dfe1
				ex_rtl_log_msg vvvv "Creating FIFO \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				mkfifo -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			elif [ "${_op}" = rm ]\
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			&& ex_rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz f0dfe1
				ex_rtl_log_msg vvvv "Removing directory or file \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
				rm -rf -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = source_opt ]\
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			&& ex_rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz f0dfe1
				ex_rtl_log_msg vvvv "Sourcing file \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				. "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = test ]\
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			&& ! [ \( -L "${1}" \) -o \( -e "${1}" \) ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = touch ]; then
Lucio Andrés Illanes Albornoz f0dfe1
				ex_rtl_log_msg vvvv "Touching file \`${1}'.";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				exp_rtl_fileop_check "${PREFIX}" "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				touch -- "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		done; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	*)	ex_rtl_log_msg failexit "Error: ex_rtl_fileop() called w/ invalid parameter(s): ${*}"; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	esac; return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
};
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
# vim:filetype=sh