Blame subr.rtl/rtl_fetch.subr

Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
#
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz 368962
rtl_fetch_mirror_urls_git() {
Lucio Andrés Illanes Albornoz 368962
	local _git_args="${1}" _tgtdir="${2}" _rc=0 _repo_dname="" _subdir="" _url="" _url_spec=""; shift 2;
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz 368962
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz 2c5ce2
		_subdir="${_url_spec%=*}"; _subdir="${_subdir##*/}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz 368962
		_repo_dname="${_url##*/}"; [ "${_repo_dname%.git}" = "${_repo_dname}" ] && _repo_dname="${_repo_dname}.git";
Lucio Andrés Illanes Albornoz 2c5ce2
Lucio Andrés Illanes Albornoz 2c5ce2
		if [ ! -e "$(rtl_dirname "${_tgtdir}")" ]; then
Lucio Andrés Illanes Albornoz 2c5ce2
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}")";
Lucio Andrés Illanes Albornoz 2c5ce2
		fi;
Lucio Andrés Illanes Albornoz 368962
		(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz 368962
		rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz 368962
		trap "rm -f \"${_tgtdir}/.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz 368962
		if [ -e "${_tgtdir}/${_repo_dname}" ]; then
Lucio Andrés Illanes Albornoz 368962
			(rtl_fileop cd "${_tgtdir}/${_repo_dname}" && git fetch ${_git_args} --all) || return 1;
Lucio Andrés Illanes Albornoz 368962
		else	(rtl_fileop cd "${_tgtdir}" && git clone ${_git_args} --mirror "${_url}") || return 1;
Lucio Andrés Illanes Albornoz 368962
		fi) 4<>"${_tgtdir}/.fetching";
Lucio Andrés Illanes Albornoz 368962
		if [ "${?}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz 368962
			_rc=1;
Lucio Andrés Illanes Albornoz 368962
		fi;
Lucio Andrés Illanes Albornoz 368962
	done; return "${_rc}";
Lucio Andrés Illanes Albornoz 368962
};
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz 63d19c
rtlp_fetch_url_git() {
Lucio Andrés Illanes Albornoz 368962
	local	_cache_dname="${1}" _git_args="${2}" _git_branch="${3}" _mirrors="${4}" _pkg_name="${5}"\
Lucio Andrés Illanes Albornoz 2c5ce2
		_subdir="${6}" _tgtdir="${7}" _url="${8}" _cache_dname_full="" _clonefl=0 _oldpwd="" _url_base="";
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz 2c5ce2
	_cache_dname_full="${_cache_dname}/${_subdir##*/}";
Lucio Andrés Illanes Albornoz e1d469
	(set -o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz 847bee
	rtl_flock_acquire 4 || exit "${?}";
Lucio Andrés Illanes Albornoz 2c5ce2
	trap "rm -f \"${_cache_dname_full%%[/]}.fetching\"" EXIT;
Lucio Andrés Illanes Albornoz 2c5ce2
	if [ -e "${_cache_dname_full}" ]; then
Lucio Andrés Illanes Albornoz 2c5ce2
		(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz 368962
			git pull ${_git_args} origin "${_git_branch:-main}") || return 1;
Lucio Andrés Illanes Albornoz 2c5ce2
		(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz 368962
			git submodule update) || return 1;
Lucio Andrés Illanes Albornoz 2c5ce2
	else	if git clone ${_git_args} -b "${_git_branch:-main}" "${_url}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz 368962
			_clonefl=1;
Lucio Andrés Illanes Albornoz 23caa1
		elif [ "${_mirrors}" = "skip" ]; then
Lucio Andrés Illanes Albornoz 23caa1
			return 1;
Lucio Andrés Illanes Albornoz 368962
		else	for _url_base in ${_mirrors}; do
Lucio Andrés Illanes Albornoz 2c5ce2
				if git clone ${_git_args} -b "${_git_branch:-main}" "${_url_base}/${_pkg_name}/${_url##*/}" "${_cache_dname_full}"; then
Lucio Andrés Illanes Albornoz 368962
					_clonefl=1; break;
Lucio Andrés Illanes Albornoz 368962
				fi;
Lucio Andrés Illanes Albornoz 368962
			done;
Lucio Andrés Illanes Albornoz 368962
		fi;
Lucio Andrés Illanes Albornoz b63b9b
		if [ "${_clonefl}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz b63b9b
			return 1;
Lucio Andrés Illanes Albornoz b63b9b
		else	if [ -n "${_git_branch}" ]; then
Lucio Andrés Illanes Albornoz 2c5ce2
				(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz 368962
					git checkout "${_git_branch}") || return 1;
Lucio Andrés Illanes Albornoz b63b9b
			fi;
Lucio Andrés Illanes Albornoz 2c5ce2
			(rtl_fileop cd "${_cache_dname_full}" &&\
Lucio Andrés Illanes Albornoz 368962
				git submodule update --init) || return 1;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	fi;
Lucio Andrés Illanes Albornoz b63b9b
	if [ "${_cache_dname}" != "${_tgtdir}" ]; then
Lucio Andrés Illanes Albornoz 368962
		_oldpwd="${PWD}"; rtl_fileop cd "${_tgtdir}" || return 1;
Lucio Andrés Illanes Albornoz 368962
		rtl_fileop rm "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz 2c5ce2
		if [ ! -e "$(rtl_dirname "${_tgtdir}/${_subdir}")" ]; then
Lucio Andrés Illanes Albornoz 2c5ce2
			rtl_fileop mkdir "$(rtl_dirname "${_tgtdir}/${_subdir}")";
Lucio Andrés Illanes Albornoz 2c5ce2
		fi;
Lucio Andrés Illanes Albornoz 2c5ce2
		rtl_fileop cp "${_cache_dname_full}" "${_tgtdir}/${_subdir}" || return 1;
Lucio Andrés Illanes Albornoz 368962
		rtl_fileop cd "${_oldpwd}" || return 1;
Lucio Andrés Illanes Albornoz 2c5ce2
	fi) 4<>"${_cache_dname_full%%[/]}.fetching";
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz 689d3c
	if [ "${?}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 689d3c
		cd "$(pwd)";
Lucio Andrés Illanes Albornoz 689d3c
	else
Lucio Andrés Illanes Albornoz bf9edf
		return 1;
Lucio Andrés Illanes Albornoz 689d3c
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fetch_urls_git() {
Lucio Andrés Illanes Albornoz 368962
	local	_cache_dname="${1}" _git_args="${2}" _tgtdir="${3}" _pkg_name="${4}" _mirrors="${5}"\
Lucio Andrés Illanes Albornoz 368962
		_git_branch="" _subdir="" _url="" _url_spec=""; shift 5;
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	for _url_spec in "${@}"; do
Lucio Andrés Illanes Albornoz b63b9b
		_subdir="${_url_spec%=*}"; _url="${_url_spec#*=}"; _url="${_url%@*}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
			_git_branch=${_url_spec#*@};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
		fi;
Lucio Andrés Illanes Albornoz 368962
		if ! rtlp_fetch_url_git	\
Lucio Andrés Illanes Albornoz 368962
				"${_cache_dname}" "${_git_args}" "${_git_branch}" "${_mirrors}"\
Lucio Andrés Illanes Albornoz 368962
				"${_pkg_name}" "${_subdir}" "${_tgtdir}" "${_url}"; then
Lucio Andrés Illanes Albornoz 6e41e8
			return 1;
Lucio Andrés Illanes Albornoz 6e41e8
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
	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
# N.B.	URLs ($1) may contain `?' or '&' characters.
Lucio Andrés Illanes Albornoz c6d6e0
rtl_fetch_url_wget() {
Lucio Andrés Illanes Albornoz 368962
	local _urls="${1}" _sha256sum_src="${2}" _target_dname="${3}" _target_fname="${4}" _target_name="${5}" _mirrors="${6:-}"\
Lucio Andrés Illanes Albornoz 4229b4
		_rc=0 _sha256sum_target="" _target_fname_full="" _url="" _url_base="" _urls_count=0 _urls_full="";
Lucio Andrés Illanes Albornoz 368962
Lucio Andrés Illanes Albornoz b63b9b
	_urls_full="${_urls}";
Lucio Andrés Illanes Albornoz b63b9b
	for _url_base in ${_mirrors}; do
Lucio Andrés Illanes Albornoz b63b9b
		_urls_full="$(rtl_lconcat "${_urls_full}" "${_url_base%/}/${_target_name}/${_target_fname}")";
Lucio Andrés Illanes Albornoz b63b9b
	done;
Lucio Andrés Illanes Albornoz b63b9b
	_urls_count="$(rtl_llength "${_urls_full}")";
Lucio Andrés Illanes Albornoz b63b9b
	for _url in ${_urls_full}; do
Lucio Andrés Illanes Albornoz b63b9b
		if [ -z "${_target_fname}" ]; then
Lucio Andrés Illanes Albornoz b63b9b
			_target_fname="$(rtl_basename "${_url}")";
Lucio Andrés Illanes Albornoz b63b9b
		fi;
Lucio Andrés Illanes Albornoz b63b9b
		_target_fname_full="${_target_dname}/${_target_fname}";
Lucio Andrés Illanes Albornoz b63b9b
		(set +o errexit -o noglob -o nounset;
Lucio Andrés Illanes Albornoz b63b9b
		rtl_flock_acquire 4 || exit 1;
Lucio Andrés Illanes Albornoz b63b9b
		trap "_rc=\"\${?}\"; rm -f \"${_target_fname_full}.fetching\"; exit \"\${_rc}\";" EXIT;
Lucio Andrés Illanes Albornoz b63b9b
		if [ -z "${_sha256sum_src}" ]\
Lucio Andrés Illanes Albornoz b63b9b
		|| ! rtl_check_digest_file "${_target_fname_full}" "${_sha256sum_src}" "${_target_fname_full}.fetched"; then
Lucio Andrés Illanes Albornoz b63b9b
			wget ${DEFAULT_WGET_ARGS} -O "${_target_fname_full}" "${_url}"; _rc="${?}";
Lucio Andrés Illanes Albornoz b63b9b
			if [ "${_rc}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				exit $((${_rc}+2));
Lucio Andrés Illanes Albornoz b63b9b
			elif [ -n "${_sha256sum_src}" ]\
Lucio Andrés Illanes Albornoz b63b9b
			&& ! rtl_check_digest "${_target_fname_full}" "${_sha256sum_src}"; then
Lucio Andrés Illanes Albornoz b63b9b
				exit 2;
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz b63b9b
				printf "%s" "${RTL_CHECK_DIGEST_DIGEST}" > "${_target_fname_full}.fetched"; exit 0;
Lucio Andrés Illanes Albornoz 689d3c
			fi;
Lucio Andrés Illanes Albornoz b63b9b
		else
Lucio Andrés Illanes Albornoz b63b9b
			exit 0;
Lucio Andrés Illanes Albornoz b63b9b
		fi;) 4<>"${_target_fname_full}.fetching"; _rc="${?}"; : $((_urls_count-=1));
Lucio Andrés Illanes Albornoz b63b9b
		case "${_rc}" in
Lucio Andrés Illanes Albornoz b63b9b
		0)	break; ;;
Lucio Andrés Illanes Albornoz b63b9b
		1)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: failed to acquire fetching lock for URL \`%s', retrying with alternative URL..." "${_url}";
Lucio Andrés Illanes Albornoz 408967
			else
Lucio Andrés Illanes Albornoz 4229b4
				rtl_log_msg fatal "Error: failed to acquire fetching lock for URL \`%s'." "${_url}";
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		2)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: hash mismatch for URL \`%s', retrying with alternative URL... (from build variables: %s.)"\
Lucio Andrés Illanes Albornoz b63b9b
						"${_url}" "${_sha256sum_src}";
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz 6c8e73
				if _sha256sum_target="$(sha256sum "${_target_fname_full}" |\
Lucio Andrés Illanes Albornoz 6c8e73
							awk '{print $1}' 2>/dev/null)"; then
Lucio Andrés Illanes Albornoz 6c8e73
					rtl_log_msg fatal "Error: hash mismatch for URL \`%s' (should be: %s vs.: %s.)"\
Lucio Andrés Illanes Albornoz 6c8e73
							"${_url}" "${_sha256sum_src}" "${_sha256sum_target}";
Lucio Andrés Illanes Albornoz 6c8e73
				else
Lucio Andrés Illanes Albornoz 6c8e73
					rtl_log_msg fatal "Error: hash mismatch for URL \`%s' (should be: %s.)"\
Lucio Andrés Illanes Albornoz 6c8e73
							"${_url}" "${_sha256sum_src}";
Lucio Andrés Illanes Albornoz 6c8e73
				fi;
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		*)	if [ "${_urls_count}" -ge 1 ]; then
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg warning "Warning: failed to fetch URL \`%s', retrying with alternative URL... (wget(1) exit status: %s)"\
Lucio Andrés Illanes Albornoz b63b9b
						"${_url}" "$((${_rc}-2))";
Lucio Andrés Illanes Albornoz b63b9b
			else
Lucio Andrés Illanes Albornoz b63b9b
				rtl_log_msg fatal "Error: failed to fetch URL \`%s' (wget(1) exit status: %s)"\
Lucio Andrés Illanes Albornoz 4229b4
						"${_url}" "$((${_rc}-2))";
Lucio Andrés Illanes Albornoz 4229b4
				rtl_fileop rm "${_target_fname_full}"; break;
Lucio Andrés Illanes Albornoz b63b9b
			fi; ;;
Lucio Andrés Illanes Albornoz b63b9b
		esac;
Lucio Andrés Illanes Albornoz b63b9b
	done;
Lucio Andrés Illanes Albornoz b63b9b
	return "${_rc}";
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
};
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
Lucio Andrés Illanes Albornoz (arab, vxp) ec19f0
# vim:filetype=sh