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 e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
# Private globals and subroutines
Lucio Andrés Illanes Albornoz b6a9a1
#
Lucio Andrés Illanes Albornoz b6a9a1
RTLP_FILEOP_LOG=0;
Lucio Andrés Illanes Albornoz b6a9a1
rtl_fileop_set_log() { RTLP_FILEOP_LOG="${1}"; };
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b6a9a1
rtlp_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 b6a9a1
rtlp_fileop_log() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _msg="${1}";
Lucio Andrés Illanes Albornoz b6a9a1
	if [ "${RTLP_FILEOP_LOG:-0}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		rtl_log_msg debug "${_msg}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b6a9a1
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 b6a9a1
			rtl_log_msg fatalexit "Invalid or non-existent directory \`%s'." "${1}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz b6a9a1
			rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: chmod %s.)" "${*}";
Lucio Andrés Illanes Albornoz bf9edf
		elif _mode="${1}" && shift\
Lucio Andrés Illanes Albornoz e1d469
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
Lucio Andrés Illanes Albornoz b6a9a1
			rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: chown %s.)" "${*}";
Lucio Andrés Illanes Albornoz bf9edf
		elif _owner="${1}" && shift\
Lucio Andrés Illanes Albornoz e1d469
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
Lucio Andrés Illanes Albornoz b6a9a1
			rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: cp_follow %s}.)" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_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 b6a9a1
			rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: cp %s.)" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_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 b6a9a1
			rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: install %s.)" "${*}";
Lucio Andrés Illanes Albornoz b6a9a1
		else	_dst="$(while [ ""${#}"" -gt 2 ]; do shift; done; printf "%s" "${2}")";
Lucio Andrés Illanes Albornoz b6a9a1
			_install_args="$(while [ ""${#}"" -gt 2 ]; do printf "%s" "${1}"; shift; done)";
Lucio Andrés Illanes Albornoz b6a9a1
			_src="$(while [ ""${#}"" -gt 2 ]; do shift; done; printf "%s" "${1}")";
Lucio Andrés Illanes Albornoz e1d469
			if rtlp_fileop_check "${PREFIX:-}" "${_dst}" "${_src}"; then
Lucio Andrés Illanes Albornoz b6a9a1
				rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: ln_symbolic %s.)" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_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 b6a9a1
				rtlp_fileop_log "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 b6a9a1
			rtl_log_msg fatalexit "Missing parameters (in: mv %s.)" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_fileop_check "${PREFIX:-}" "${1}" "${2}"; then
Lucio Andrés Illanes Albornoz b6a9a1
			rtlp_fileop_log "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 cb9a05
	mkdir|mkfifo|rm|source|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 b6a9a1
				rtl_log_msg fatalexit "Missing parameters (in: %s %s.)" "${_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 e1d469
			&& rtlp_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 b6a9a1
				rtlp_fileop_log "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 e1d469
			&& rtlp_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 b6a9a1
				rtlp_fileop_log "Creating FIFO \`${1}'.";
Lucio Andrés Illanes Albornoz e1d469
				rtlp_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 e1d469
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz b6a9a1
				rtlp_fileop_log "Removing directory or file \`${1}'.";
Lucio Andrés Illanes Albornoz 2b85d0
				rm -rf -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz cb9a05
			elif [ "${_op}" = source ]\
Lucio Andrés Illanes Albornoz cb9a05
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz cb9a05
				rtlp_fileop_log "Sourcing file \`${1}'.";
Lucio Andrés Illanes Albornoz cb9a05
				. "${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 e1d469
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz b6a9a1
				rtlp_fileop_log "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 e1d469
			&& rtlp_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 e1d469
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz b6a9a1
				rtlp_fileop_log "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 b6a9a1
	*)	rtl_log_msg fatalexit "Error: rtl_fileop() called w/ invalid parameter(s): %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