Blame subr.rtl/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 b1d432
# Private subroutines
Lucio Andrés Illanes Albornoz b6a9a1
#
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 dc07ba
	rtl_log_msg "fileop" "0;${1}";
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b1d432
#
Lucio Andrés Illanes Albornoz b1d432
# Public subroutines
Lucio Andrés Illanes Albornoz b1d432
#
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fileop() {
Lucio Andrés Illanes Albornoz b6b013
	local _op="${1}" _dst="" _group="" _install_args="" _mode="" _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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invalid_dir}" "${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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_chmod}" "${*}";
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 b6b013
	chgrp)	if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_chgrp}" "${*}";
Lucio Andrés Illanes Albornoz b6b013
		elif _group="${1}" && shift\
Lucio Andrés Illanes Albornoz b6b013
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
Lucio Andrés Illanes Albornoz b6b013
			rtlp_fileop_log "Changing file group of \`${*}' to \`${_group}'.";
Lucio Andrés Illanes Albornoz b6b013
			chgrp -- "${_group}" "${@}"; _rc="${?}";
Lucio Andrés Illanes Albornoz b6b013
		fi; ;;
Lucio Andrés Illanes Albornoz bf9edf
	chown)	if [ "${#}" -lt 2 ]; then
Lucio Andrés Illanes Albornoz dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_chown}" "${*}";
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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_cp_follow}" "${*}";
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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_cp}" "${*}";
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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_install}" "${*}";
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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_ln_symbolic}" "${*}";
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 dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_mv}" "${*}";
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 b6b013
	touch)	if [ -z "${1}" ]; then
Lucio Andrés Illanes Albornoz dc07ba
			rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_touch}" "${*}";
Lucio Andrés Illanes Albornoz b6b013
		elif rtlp_fileop_check "${PREFIX:-}" "${1}"; then
Lucio Andrés Illanes Albornoz b6b013
			rtlp_fileop_log "Touching file \`${1}'${2:+ w/ timestamp \`${2}\'}.";
Lucio Andrés Illanes Albornoz b6b013
			touch ${2:+-t "${2}"} -- "${1}"; _rc="${?}";
Lucio Andrés Illanes Albornoz b6b013
		fi; ;;
Lucio Andrés Illanes Albornoz b6b013
	mkdir|mkfifo|rm|source|source_opt|test)
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 dc07ba
				rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm}" "${_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 (arab, vxp) 32ad21
			fi; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		done; ;;
Lucio Andrés Illanes Albornoz dc07ba
	*)	rtl_log_msg "fatalexit" "${MSG_rtl_fileop_invparm_fileop}" "${*}"; ;;
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