Blame subr.rtl/rtl_fileop.subr

Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
#
8d7a8a
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
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() {
e9fa07
	local _rpfc_prefix="${1}" _rpfc_pname="" _rpfc_rname=""; shift;
e9fa07
	return 0;
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() {
4243a1
	rtl_log_msgV "fileop" "0;${1}";
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz b1d432
#
b2c1ea
# Public globals
b2c1ea
#
b2c1ea
b2c1ea
RTL_FILEOP_SOURCE_FNAME="";
b2c1ea
b2c1ea
#
Lucio Andrés Illanes Albornoz b1d432
# Public subroutines
Lucio Andrés Illanes Albornoz b1d432
#
Lucio Andrés Illanes Albornoz b6a9a1
550c18
#
550c18
# rtl_fileop() - clean download cache for single package
131fe2
# @_op:		file operation (one of: cd, chmod, chgrp, chown, cp_follow, cp, install, ln_symbolic, mv, touch, mkdir, mkfifo, rm, source, source_opt, test)
550c18
# @...:		file operation arguments
550c18
#
550c18
# Returns:	zero (0) on success, non-zero (>0) on failure
550c18
#
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fileop() {
e9fa07
	local	_rf_op="${1}"					\
e9fa07
		_rf_dst="" _rf_group="" _rf_install_args=""	\
e9fa07
		_rf_mode="" _rf_owner="" _rf_rc=0 _rf_src=""; shift;
e9fa07
e9fa07
	case "${_rf_op}" in
e9fa07
e9fa07
	cd)	if [ \( "${1:+1}" != 1 \) -o ! \( -L "${1}" -o -e "${1}" \) ]; then
4243a1
			rtl_log_msgV "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}'.";
e9fa07
			cd -- "${1}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz bf9edf
	chmod)	if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_chmod}" "${*}";
e9fa07
		elif _rf_mode="${1}" && shift\
Lucio Andrés Illanes Albornoz e1d469
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
e9fa07
			rtlp_fileop_log "Changing file mode bits of \`${*}' to \`${_rf_mode}'.";
e9fa07
			chmod -- "${_rf_mode}" "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz bf9edf
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz b6b013
	chgrp)	if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_chgrp}" "${*}";
e9fa07
		elif _rf_group="${1}" && shift\
Lucio Andrés Illanes Albornoz b6b013
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
e9fa07
			rtlp_fileop_log "Changing file group of \`${*}' to \`${_rf_group}'.";
e9fa07
			chgrp -- "${_rf_group}" "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz b6b013
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz bf9edf
	chown)	if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_chown}" "${*}";
e9fa07
		elif _rf_owner="${1}" && shift\
Lucio Andrés Illanes Albornoz e1d469
		&&   rtlp_fileop_check "${PREFIX:-}" "${*}"; then
e9fa07
			rtlp_fileop_log "Changing file owner of \`${*}' to \`${_rf_owner}'.";
e9fa07
			chown -- "${_rf_owner}" "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz bf9edf
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
	cp_follow)
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_cp_follow}" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_fileop_check "${PREFIX:-}" "${*}"; then
e9fa07
			_rf_src="${*}"; _rf_src="${_rf_src% *}";
e9fa07
			_rf_dst="${*}"; _rf_dst="${_rf_dst##* }";
e9fa07
			rtlp_fileop_log "Copying \`${_rf_src}' into \`${_rf_dst}' w/ -pLR.";
e9fa07
			cp -pLR -- "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) b7b9af
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz 2b85d0
	cp)	if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_cp}" "${*}";
Lucio Andrés Illanes Albornoz e1d469
		elif rtlp_fileop_check "${PREFIX:-}" "${*}"; then
e9fa07
			_rf_src="${*}"; _rf_src="${_rf_src% *}";
e9fa07
			_rf_dst="${*}"; _rf_dst="${_rf_dst##* }";
e9fa07
			rtlp_fileop_log "Copying \`${_rf_src}' into \`${_rf_dst}' w/ -pPR.";
e9fa07
			cp -pPR -- "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz 2b85d0
	install)
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${#}" -lt 2 ]; then
4243a1
			rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_install}" "${*}";
e9fa07
		else
e9fa07
			rtl_lindexV \$_rf_dst -1 "${@}";
e9fa07
			rtl_lrangeV \$_rf_install_args 0 1 "${@}";
e9fa07
			rtl_lindexV \$_rf_install_args -2 "${@}";
e9fa07
e9fa07
			if rtlp_fileop_check "${PREFIX:-}" "${_rf_dst}" "${_rf_src}"; then
e9fa07
				rtlp_fileop_log "Installing \`${_rf_src}' into \`${_rf_dst}' w/ ${_rf_install_args}.";
e9fa07
				install "${@}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
e9fa07
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	ln_symbolic)
e9fa07
		if [ \( "${1:+1}" != 1 \) -o \( "${2:+1}" != 1 \) ]; then
4243a1
			rtl_log_msgV "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";
e9fa07
				ln -fs -- "${1}" "${2}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
e9fa07
e9fa07
	mv)	if [ \( "${1:+1}" != 1 \) -o \( "${2:+1}" != 1 \) ]; then
4243a1
			rtl_log_msgV "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";
e9fa07
			mv -f -- "${1}" "${2}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi; ;;
e9fa07
e9fa07
	touch)	if [ "${1:+1}" != 1 ]; then
4243a1
			rtl_log_msgV "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}\'}.";
e9fa07
			touch ${2:+-t "${2}"} -- "${1}"; _rf_rc="${?}";
Lucio Andrés Illanes Albornoz b6b013
		fi; ;;
e9fa07
b2c1ea
	mkdir|mkfifo|rm|source|source_with_fnamevar|source_opt|source_opt_with_fnamevar|test)
Lucio Andrés Illanes Albornoz 2b85d0
		while [ \( "${?}" -eq 0 \) -a \( ""${#}"" -gt 0 \) ]; do
e9fa07
			if [ "${1:+1}" != 1 ]; then
4243a1
				rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm}" "${_rf_op}" "${*}";
e9fa07
			elif [ "${_rf_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}'.";
e9fa07
				mkdir -p -- "${1}"; _rf_rc="${?}";
e9fa07
			elif [ "${_rf_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}";
e9fa07
				mkfifo -- "${1}"; _rf_rc="${?}";
e9fa07
			elif [ "${_rf_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}'.";
e9fa07
				rm -rf -- "${1}"; _rf_rc="${?}";
e9fa07
			elif [ "${_rf_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}'.";
e9fa07
				. "${1}"; _rf_rc="${?}";
b2c1ea
			elif [ "${_rf_op}" = source_with_fnamevar ]\
b2c1ea
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
b2c1ea
				rtlp_fileop_log "Sourcing file \`${1}'.";
b2c1ea
				RTL_FILEOP_SOURCE_FNAME="${1}";
b2c1ea
				. "${1}"; _rf_rc="${?}";
e9fa07
			elif [ "${_rf_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}'.";
e9fa07
				. "${1}"; _rf_rc="${?}";
b2c1ea
			elif [ "${_rf_op}" = source_opt_with_fnamevar ]\
b2c1ea
			&& rtl_fileop test "${1}"\
b2c1ea
			&& rtlp_fileop_check "${PREFIX:-}" "${1}"; then
b2c1ea
				rtlp_fileop_log "Sourcing file \`${1}'.";
b2c1ea
				RTL_FILEOP_SOURCE_FNAME="${1}";
b2c1ea
				. "${1}"; _rf_rc="${?}";
e9fa07
			elif [ "${_rf_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; ;;
e9fa07
4243a1
	*)	rtl_log_msgV "fatalexit" "${MSG_rtl_fileop_invparm_fileop}" "${*}"; ;;
e9fa07
e9fa07
	esac;
e9fa07
e9fa07
	return "${_rf_rc}";
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
};
Lucio Andrés Illanes Albornoz (arab, vxp) 32ad21
64844b
# vim:filetype=sh textwidth=0