Blame subr.rtl/rtl_fetch_wget.subr

e9fa07
#
8d7a8a
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 LucĂ­a Andrea Illanes Albornoz <lucia@luciaillanes.de>
e9fa07
# set +o errexit -o noglob -o nounset is assumed.
e9fa07
#
e9fa07
550c18
#
550c18
# rtl_fetch_url_wget() - fetch archive file(s) for item
550c18
# @_urls:		list of URLs
550c18
# @_sha256sum_src:	SHA256SUM digest file to check authenticity of archive file(s) with
550c18
# @_target_dname:	target directory namne
550c18
# @_target_fname:	target filename
550c18
# @_target_name:	target subdirectory name
550c18
# @_mirrors:		optional list of mirror base URLs
550c18
#
550c18
# Returns:		zero (0) on success, non-zero (>0) on failure
550c18
# N.B.			URLs ($1) may contain `?' or '&' characters.
550c18
#
e9fa07
rtl_fetch_url_wget() {
e9fa07
	local	_rfuw_urls="${1}" _rfuw_sha256sum_src="${2}" _rfuw_target_dname="${3}"		\
e9fa07
		_rfuw_target_fname="${4}" _rfuw_target_name="${5}" _rfuw_mirrors="${6:-}"	\
e9fa07
		_rfuw_rc=0 _rfuw_sha256sum_target="" _rfuw_target_fname_full="" _rfuw_url=""	\
e9fa07
		_rfuw_url_base="" _rfuw_urls_count=0 _rfuw_urls_full="";
e9fa07
e9fa07
	_rfuw_urls_full="${_rfuw_urls}";
e9fa07
	for _rfuw_url_base in ${_rfuw_mirrors}; do
e9fa07
		rtl_lconcat \$_rfuw_urls_full "${_rfuw_url_base%/}/${_rfuw_target_name}/${_rfuw_target_fname}";
e9fa07
	done;
e9fa07
	rtl_llength \$_rfuw_urls_count \$_rfuw_urls_full;
e9fa07
e9fa07
	for _rfuw_url in ${_rfuw_urls_full}; do
e9fa07
		if [ "${_rfuw_target_fname:+1}" != 1 ]; then
e9fa07
			rtl_basename2 \$_rfuw_url \$_rfuw_target_fname;
e9fa07
		fi;
e9fa07
		_rfuw_target_fname_full="${_rfuw_target_dname}/${_rfuw_target_fname}";
e9fa07
e9fa07
		(set +o errexit -o noglob -o nounset;
e9fa07
		rtl_flock_acquire 4 || exit 1;
e9fa07
		trap "_rfuw_rc=\"\${?}\"; rm -f \"${_rfuw_target_fname_full}.fetching\"; exit \"\${_rfuw_rc}\";" EXIT;
e9fa07
e9fa07
		if [ "${_rfuw_sha256sum_src:+1}" != 1 ]\
e9fa07
		|| ! rtl_check_digest_file "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}" "${_rfuw_target_fname_full}.fetched"; then
e9fa07
			wget ${DEFAULT_WGET_ARGS} -O "${_rfuw_target_fname_full}" "${_rfuw_url}"; _rfuw_rc="${?}";
e9fa07
			if [ "${_rfuw_rc}" -ne 0 ]; then
e9fa07
				exit $((${_rfuw_rc}+2));
e9fa07
			elif [ "${_rfuw_sha256sum_src:+1}" = 1 ]\
e9fa07
			&& ! rtl_check_digest \$_digest "${_rfuw_target_fname_full}" "${_rfuw_sha256sum_src}"; then
e9fa07
				exit 2;
e9fa07
			else
e9fa07
				printf "%s" "${_digest}" > "${_rfuw_target_fname_full}.fetched"; exit 0;
e9fa07
			fi;
e9fa07
		else
e9fa07
			exit 0;
e9fa07
		fi;) 4<>"${_rfuw_target_fname_full}.fetching"; _rfuw_rc="${?}"; : $((_rfuw_urls_count-=1));
e9fa07
e9fa07
		case "${_rfuw_rc}" in
e9fa07
e9fa07
		0)	break; ;;
e9fa07
e9fa07
		1)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_lockfail_retryurl}" "${_rfuw_url}";
e9fa07
			else
e9fa07
				rtl_log_msg "fatal" "${MSG_rtl_fetch_lockfail}" "${_rfuw_url}";
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		2)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_hashfail_retryurl}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
e9fa07
			else
e9fa07
				if _rfuw_sha256sum_target="$(sha256sum "${_rfuw_target_fname_full}" |\
e9fa07
							awk '{print $1}' 2>/dev/null)"; then
e9fa07
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail1}" "${_rfuw_url}" "${_rfuw_sha256sum_src}" "${_rfuw_sha256sum_target}";
e9fa07
				else
e9fa07
					rtl_log_msg "fatal" "${MSG_rtl_fetch_hashfail2}" "${_rfuw_url}" "${_rfuw_sha256sum_src}";
e9fa07
				fi;
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		*)	if [ "${_rfuw_urls_count}" -ge 1 ]; then
e9fa07
				rtl_log_msg "warning" "${MSG_rtl_fetch_fail_retryurl}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
e9fa07
			else
e9fa07
				rtl_log_msg "fatal" "${MSG_rtl_fetch_fail}" "${_rfuw_url}" "$((${_rfuw_rc}-2))";
e9fa07
				rtl_fileop rm "${_rfuw_target_fname_full}"; break;
e9fa07
			fi; ;;
e9fa07
e9fa07
		esac;
e9fa07
	done;
e9fa07
e9fa07
	return "${_rfuw_rc}";
e9fa07
};
e9fa07
64844b
# vim:filetype=sh textwidth=0