summaryrefslogtreecommitdiffhomepage
path: root/subr/rtl_fetch.subr
blob: 350a01e8e1b918b64bd59aac46de497db24a4e25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
# set +o errexit -o noglob -o nounset is assumed.
#

rtlp_fetch_url_git() {
	local _tgtdir="${1}" _subdir="${2}" _url="${3}" _branch="${4}"	\
		_oldpwd="";
	(set -o errexit -o noglob -o nounset;
	rtl_flock_acquire 4 || exit "${?}";
	trap "rm -f \"${BUILD_DLCACHEDIR}/${_subdir%%[/]}.fetching\"" EXIT;
	if [ -e "${BUILD_DLCACHEDIR}/${_subdir}" ]; then
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
			git pull ${DEFAULT_GIT_ARGS} origin "${_branch:-main}");
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
			git submodule update);
	else
		git clone ${DEFAULT_GIT_ARGS} "${_url}" "${BUILD_DLCACHEDIR}/${_subdir}";
		if [ -n "${_branch}" ]; then
			(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
				git checkout "${_branch}");
		fi;
		(rtl_fileop cd "${BUILD_DLCACHEDIR}/${_subdir}" &&\
			git submodule update --init);
	fi;
	_oldpwd="${PWD}"; rtl_fileop cd "${PKG_BASE_DIR}";
	rtl_fileop rm "${_tgtdir}/${_subdir}";
	rtl_fileop cp "${BUILD_DLCACHEDIR}/${_subdir}" "${_tgtdir}";
	rtl_fileop cd "${_oldpwd}";) 4<>"${BUILD_DLCACHEDIR}/${_subdir%%[/]}.fetching";
	if [ "${?}" -eq 0 ]; then
		cd "$(pwd)";
	else
		return 1;
	fi;
};

rtl_fetch_urls_git() {
	local _tgtdir="" _url_spec="" _subdir="" _url="" _git_branch="";
	_tgtdir="${1}"; shift;
	if [ "${ARG_FETCH_FORCE}" = "offline" ]; then
		return 0;
	fi;
	for _url_spec in "${@}"; do
		_subdir="${_url_spec%=*}";
		_url="${_url_spec#*=}";
		_url="${_url%@*}";
		if [ "${_url_spec#*@}" != "${_url_spec}" ]; then
			_git_branch=${_url_spec#*@};
		fi;
		rtlp_fetch_url_git "${_tgtdir}" "${_subdir}"	\
			"${_url}" "${_git_branch}";
	done;
};

# N.B.	URLs ($1) may contain `?' or '&' characters.
rtl_fetch_url_wget() {
	local _url="${1}" _sha256sum_src="${2}" _target_fname="${3}" _target_fname_full="" RTL_CHECK_DIGEST_DIGEST="";
	if [ "${ARG_FETCH_FORCE}" = "offline" ]; then
		return 0;
	else	if [ -z "${_target_fname}" ]; then
			_target_fname="$(rtl_basename "${_url}")";
		fi;
		_target_fname_full="${BUILD_DLCACHEDIR}/${_target_fname}";
		(set -o errexit -o noglob -o nounset;
		rtl_flock_acquire 4 || exit "${?}";
		trap "_rc=\"\${?}\"; rm -f \"${_target_fname_full}.fetching\"; exit \"\${_rc}\";" EXIT;
		if [ -z "${_sha256sum_src}" ]\
		|| ! rtl_check_digest_file "${_target_fname_full}" "${_sha256sum_src}" "${_target_fname_full}.fetched"; then
			wget ${DEFAULT_WGET_ARGS} -O "${_target_fname_full}" "${_url}";
			if [ -n "${_sha256sum_src}" ]\
			&& ! rtl_check_digest "${_target_fname_full}" "${_sha256sum_src}"; then
				rtl_log_msg fatalexit "Error: hash mismatch for URL \`%s' (downloaded file: %s, from build variables: %s.)"\
						"${_url}" "${RTL_CHECK_DIGEST_DIGEST}" "${_sha256sum_src}";
			else
				printf "%s" "${RTL_CHECK_DIGEST_DIGEST}" > "${_target_fname_full}.fetched";
			fi;
		fi;) 4<>"${_target_fname_full}.fetching";
	fi;
};

# vim:filetype=sh