1#!/bin/bash
2#
3#   generate_checksum.sh - functions for generating source checksums
4#
5#   Copyright (c) 2014-2018 Pacman Development Team <pacman-dev@archlinux.org>
6#
7#   This program is free software; you can redistribute it and/or modify
8#   it under the terms of the GNU General Public License as published by
9#   the Free Software Foundation; either version 2 of the License, or
10#   (at your option) any later version.
11#
12#   This program is distributed in the hope that it will be useful,
13#   but WITHOUT ANY WARRANTY; without even the implied warranty of
14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#   GNU General Public License for more details.
16#
17#   You should have received a copy of the GNU General Public License
18#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21[[ -n "$LIBMAKEPKG_INTEGRITY_GENERATE_CHECKSUM_SH" ]] && return
22LIBMAKEPKG_INTEGRITY_GENERATE_CHECKSUM_SH=1
23
24LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
25
26source "$LIBRARY/util/message.sh"
27source "$LIBRARY/util/pkgbuild.sh"
28
29generate_one_checksum() {
30	local integ=$1 arch=$2 sources numsrc indentsz idx
31
32	if [[ $arch ]]; then
33		array_build sources "source_$arch"
34	else
35		array_build sources 'source'
36	fi
37
38	numsrc=${#sources[*]}
39	if (( numsrc == 0 )); then
40		return
41	fi
42
43	if [[ $arch ]]; then
44		printf "%ssums_%s=(%n" "$integ" "$arch" indentsz
45	else
46		printf "%ssums=(%n" "$integ" indentsz
47	fi
48
49	for (( idx = 0; idx < numsrc; ++idx )); do
50		local netfile=${sources[idx]}
51		local proto sum
52		proto="$(get_protocol "$netfile")"
53
54		case $proto in
55			bzr*|git*|hg*|svn*)
56				sum="SKIP"
57				;;
58			*)
59				if [[ ${netfile%%::*} != *.@(sig?(n)|asc) ]]; then
60					local file
61					file="$(get_filepath "$netfile")" || missing_source_file "$netfile"
62					sum="$("${integ}sum" "$file")"
63					sum=${sum%% *}
64				else
65					sum="SKIP"
66				fi
67				;;
68		esac
69
70		# indent checksum on lines after the first
71		printf "%*s%s" $(( idx ? indentsz : 0 )) '' "'$sum'"
72
73		# print a newline on lines before the last
74		(( idx < (numsrc - 1) )) && echo
75	done
76
77	echo ")"
78}
79
80generate_checksums() {
81	msg "$(gettext "Generating checksums for source files...")"
82
83	local integlist
84	if (( $# == 0 )); then
85		IFS=$'\n' read -rd '' -a integlist < <(get_integlist)
86	else
87		integlist=("$@")
88	fi
89
90	local integ
91	for integ in "${integlist[@]}"; do
92		if ! in_array "$integ" "${known_hash_algos[@]}"; then
93			error "$(gettext "Invalid integrity algorithm '%s' specified.")" "$integ"
94			exit 1 # $E_CONFIG_ERROR
95		fi
96
97		generate_one_checksum "$integ"
98		for a in "${arch[@]}"; do
99			generate_one_checksum "$integ" "$a"
100		done
101	done
102}
103