aboutsummaryrefslogtreecommitdiff
path: root/Mk/Scripts/checksum.sh
blob: e013c4239fd668761d8e7007a4317affe54f9948 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
#
# MAINTAINER: portmgr@FreeBSD.org

set -e
set -o pipefail

. "${dp_SCRIPTSDIR}/functions.sh"

validate_env dp_CHECKSUM_ALGORITHMS dp_CURDIR dp_DISTDIR dp_DISTINFO_FILE \
	dp_DIST_SUBDIR dp_ECHO_MSG dp_FETCH_REGET dp_MAKE dp_MAKEFLAGS \
	dp_DISABLE_SIZE dp_NO_CHECKSUM

[ -n "${DEBUG_MK_SCRIPTS}" -o -n "${DEBUG_MK_SCRIPTS_CHECKSUM}" ] && set -x

set -u

check_checksum_algorithms

if [ -f "${dp_DISTINFO_FILE}" ]; then
	cd "${dp_DISTDIR}"
	OK=
	refetchlist=
	for file in "${@}"; do
		ignored="true"
		for alg in ${dp_CHECKSUM_ALGORITHMS}; do
			ignore="false"
			eval "alg_executable=\$dp_${alg}"

			if [ "$alg_executable" != "NO" ]; then
				MKSUM=$($alg_executable < "${file}")
				CKSUM=$(distinfo_data "${alg}" "${file}")
			else
				ignore="true"
			fi

			if [ $ignore = "false" -a -z "$CKSUM" ]; then
				${dp_ECHO_MSG} "=> No $alg checksum recorded for $file."
				ignore="true"
			fi

			if [ $ignore != "false" ]; then
				continue
			fi

			match="false"
			for chksum in $CKSUM; do
				if [ "$chksum" = "$MKSUM" ]; then
					match="true"
					break
				fi
			done
			if [ $match = "true" ]; then
				${dp_ECHO_MSG} "=> $alg Checksum OK for $file."
				ignored="false"
			else
				${dp_ECHO_MSG} "=> $alg Checksum mismatch for $file."
				refetchlist="$refetchlist $file "
				OK="${OK:-retry}"
				[ "${OK}" = "retry" -a "${dp_FETCH_REGET}" -gt 0 ] && rm -f "${file}"
				ignored="false"
			fi
		done

		if [ $ignored = "true" ]; then
			${dp_ECHO_MSG} "=> No suitable checksum found for $file."
			OK=false
		fi
	done

	if [ "${OK:=true}" = "retry" ] && [ "${dp_FETCH_REGET}" -gt 0 ]; then
		${dp_ECHO_MSG} "===>  Refetch for ${dp_FETCH_REGET} more times files: $refetchlist"
		if ${dp_MAKE} -C "${dp_CURDIR}" ${dp_MAKEFLAGS} FORCE_FETCH_LIST="$refetchlist" FETCH_REGET="$((dp_FETCH_REGET - 1))" fetch; then
			if ${dp_MAKE} -C "${dp_CURDIR}" ${dp_MAKEFLAGS} FETCH_REGET="$((dp_FETCH_REGET - 1))" checksum ; then
			OK="true"
			fi
		fi
	fi

	if [ "$OK" != "true" -a "${dp_FETCH_REGET}" -eq 0 ]; then
		${dp_ECHO_MSG} "===>  Giving up on fetching files: $refetchlist"
		${dp_ECHO_MSG} "Make sure the Makefile and distinfo file (${dp_DISTINFO_FILE})"
		${dp_ECHO_MSG} "are up to date.  If you are absolutely sure you want to override this"
		${dp_ECHO_MSG} "check, type \"make NO_CHECKSUM=yes [other args]\"."
		exit 1
	fi
	if [ "$OK" != "true" ]; then
		exit 1
	fi
elif [ -n "${*}" ]; then
	${dp_ECHO_MSG} "=> No checksum file (${dp_DISTINFO_FILE})."
	exit 1
fi