midipix / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone

Blame subr/rtl_fileop.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz 2b85d0
# set +o errexit -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 2b85d0
	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 c6d6e0
rtl_fileop() {
Lucio Andrés Illanes Albornoz bf9edf
	local _op="${1}" _dst="" _mode="" _install_args="" _owner="" _rc=0 _src=""; 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 c6d6e0
			rtl_log_msg failexit "Invalid or non-existent directory \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
		elif exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg vvvv "Changing working directory to \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
			cd -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
Lucio Andrés Illanes Albornoz bf9edf
	chmod)	if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz bf9edf
			rtl_log_msg failexit "Missing parameters (in: chmod ${*}.)";
Lucio Andrés Illanes Albornoz bf9edf
		elif _mode="${1}" && shift\
Lucio Andrés Illanes Albornoz bf9edf
		&&   exp_rtl_fileop_check "${PREFIX}" "${*}"; then
Lucio Andrés Illanes Albornoz bf9edf
			rtl_log_msg vvvv "Changing file mode bits of \`${*}' to \`${_mode}'.";
Lucio Andrés Illanes Albornoz bf9edf
			chmod -- "${_mode}" "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz bf9edf
		fi; ;;
Lucio Andrés Illanes Albornoz bf9edf
	chown)	if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz bf9edf
			rtl_log_msg failexit "Missing parameters (in: chown ${*}.)";
Lucio Andrés Illanes Albornoz bf9edf
		elif _owner="${1}" && shift\
Lucio Andrés Illanes Albornoz bf9edf
		&&   exp_rtl_fileop_check "${PREFIX}" "${*}"; then
Lucio Andrés Illanes Albornoz bf9edf
			rtl_log_msg vvvv "Changing file owner of \`${*}' to \`${_owner}'.";
Lucio Andrés Illanes Albornoz bf9edf
			chown -- "${_owner}" "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz bf9edf
		fi; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
	cp_follow)
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg failexit "Missing parameters (in: cp_follow ${*}.)";
Lucio Andrés Illanes Albornoz 2b85d0
		elif exp_rtl_fileop_check "${PREFIX}" "${*}"; then
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 c6d6e0
			rtl_log_msg vvvv "Copying \`${_src}' into \`${_dst}' w/ -pLR.";
Lucio Andrés Illanes Albornoz 2b85d0
			cp -pLR -- "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
		fi; ;;
Lucio Andrés Illanes Albornoz 2b85d0
	cp)	if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg failexit "Missing parameters (in: cp ${*}.)";
Lucio Andrés Illanes Albornoz 2b85d0
		elif exp_rtl_fileop_check "${PREFIX}" "${*}"; then
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 c6d6e0
			rtl_log_msg vvvv "Copying \`${_src}' into \`${_dst}' w/ -pPR.";
Lucio Andrés Illanes Albornoz 2b85d0
			cp -pPR -- "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
		fi; ;;
Lucio Andrés Illanes Albornoz 2b85d0
	install)
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg failexit "Missing parameters (in: install ${*}.)";
Lucio Andrés Illanes Albornoz 2b85d0
		else	_dst="$(while [ ""${#}"" -gt 2 ]; do shift; done; echo "${2}")";
Lucio Andrés Illanes Albornoz 2b85d0
			_install_args="$(while [ ""${#}"" -gt 2 ]; do echo "${1}"; shift; done)";
Lucio Andrés Illanes Albornoz 2b85d0
			_src="$(while [ ""${#}"" -gt 2 ]; do shift; done; echo "${1}")";
Lucio Andrés Illanes Albornoz 2b85d0
			if exp_rtl_fileop_check "${PREFIX}" "${_dst}" "${_src}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Installing \`${_src}' into \`${_dst}' w/ ${_install_args}.";
Lucio Andrés Illanes Albornoz 2b85d0
				install "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
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 c6d6e0
			rtl_log_msg failexit "Missing parameters (in: ln_symbolic ${*}.)";
Lucio Andrés Illanes Albornoz 2b85d0
		elif exp_rtl_fileop_check "${PREFIX}" "${2}"; then
Lucio Andrés Illanes Albornoz c6d6e0
			if rtl_fileop test "${2}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_fileop rm "${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			fi;
Lucio Andrés Illanes Albornoz 2b85d0
			if [ "${?}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Linking \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz 2b85d0
				ln -fs -- "${1}" "${2}"; _rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
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 c6d6e0
			rtl_log_msg failexit "Missing parameters (in: mv ${*}.)";
Lucio Andrés Illanes Albornoz 2b85d0
		elif exp_rtl_fileop_check "${PREFIX}" "${1}" "${2}"; then
Lucio Andrés Illanes Albornoz c6d6e0
			rtl_log_msg vvvv "Moving \`${1}' to \`${2}' w/ -fs";
Lucio Andrés Illanes Albornoz 2b85d0
			mv -f -- "${1}" "${2}"; _rc="${?}";
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 2b85d0
		while [ \( "${?}" -eq 0 \) -a \( ""${#}"" -gt 0 \) ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg failexit "Missing parameters (in: ${_op} ${*}.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			elif [ "${_op}" = mkdir ]\
Lucio Andrés Illanes Albornoz 2b85d0
			&& [ ! -d "${1}" ]\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				if rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
					rtl_fileop rm "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				fi;
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Making directory \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
				mkdir -p -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
			elif [ "${_op}" = mkfifo ]\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				if rtl_fileop test "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
					rtl_fileop rm "${1}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
				fi;
Lucio Andrés Illanes Albornoz c6d6e0
				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 2b85d0
				mkfifo -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			elif [ "${_op}" = rm ]\
Lucio Andrés Illanes Albornoz c6d6e0
			&& rtl_fileop test "${1}"\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Removing directory or file \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
				rm -rf -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = source_opt ]\
Lucio Andrés Illanes Albornoz c6d6e0
			&& rtl_fileop test "${1}"\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Sourcing file \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
				. "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			elif [ "${_op}" = test ]\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"\
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 2b85d0
			elif [ "${_op}" = touch ]\
Lucio Andrés Illanes Albornoz 2b85d0
			&& exp_rtl_fileop_check "${PREFIX}" "${1}"; then
Lucio Andrés Illanes Albornoz c6d6e0
				rtl_log_msg vvvv "Touching file \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
				touch -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
			fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		done; ;;
Lucio Andrés Illanes Albornoz c6d6e0
	*)	rtl_log_msg failexit "Error: rtl_fileop() called w/ invalid parameter(s): ${*}"; ;;
Lucio Andrés Illanes Albornoz 2b85d0
	esac; return "${_rc}";
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