summaryrefslogtreecommitdiff
path: root/src/chroot-tools/libremakepkg.gpl2
blob: 3f665890f5dc57401a4d4bccc5dae18086a0abf6 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# Contains code derived from devtools' "makechrootpkg"

# Copyright 2011-2012 The Arch Linux Development Team
# Copyright 2012 Luke Shumaker
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

chroot_init() {
	# make sure the chroot exists
	librechroot -n "$CHROOT" -l "$CHROOTCOPY" -m

	if [[ -r "$LIBREHOME/.gnupg/pubring.gpg" ]]; then
		install -D "$HOME/.gnupg/pubring.gpg" "$copydir/build/.gnupg/pubring.gpg"
	fi

	mkdir -p "$copydir/pkgdest"
	mkdir -p "$copydir/srcdest"
	mkdir -p "$copydir/build"

	chroot_makepkg_conf_set PKGDEST /pkgdest
	chroot_makepkg_conf_set SRCDEST /srcdest

	cat > "$copydir/etc/sudoers.d/nobody-pacman" <<EOF
Defaults env_keep += "HOME"
nobody ALL = NOPASSWD: /usr/bin/pacman
EOF
	chmod 440 "$copydir/etc/sudoers.d/nobody-pacman"
}

chroot_extract() {
	if $NOCHROOT; then
		chcleanup
		sudo -u ${LIBREUSER} ${MAKEPKG} ${makepkg_args} -o
		return
	fi
	rm -rf "$copydir"/build/*
	cp PKGBUILD "$copydir/build/"
	(
		set +euE
		source PKGBUILD

		# Copy source files
		for file in "${source[@]}"; do
			file="${file%%::*}"
			file="${file##*://*/}"
			if [[ -f $file ]]; then
				cp "$file" "$copydir/srcdest/"
			elif [[ -f $SRCDEST/$file ]]; then
				cp "$SRCDEST/$file" "$copydir/srcdest/"
			fi
		done

		# Find all changelog and install files, even inside functions
		for i in 'changelog' 'install'; do
			while read -r file; do
				# evaluate any bash variables used
				eval file=\"$(sed 's/^\(['\''"]\)\(.*\)\1$/\2/' <<< "$file")\"
				if [[ -f $file ]]; then
					cp "$file" "$copydir/build/"
				fi
			done < <(sed -n "s/^[[:space:]]*$i=//p" PKGBUILD)
		done
	)

	chown -R nobody "$copydir"/{build,pkgdest,srcdest}

	cp -a "$(which chcleanup)" "${copydir}/clean"
	chroot_exec "/clean && sudo -u nobody ${MAKEPKG} ${makepkg_args} -o"
}

chroot_build() {
	local user=$LIBREUSER
	$NOCHROOT || user=nobody
	chroot_exec -N "sudo -u ${user} ${MAKEPKG} ${makepkg_args} -e"
}

chroot_exec() {
	local flags=''
	[[ $1 == -N ]] && { flags=$1; shift; }
	local cmd="$*"
	if $NOCHROOT; then
		${cmd}
	else
		cat >"$copydir/chrootexec" <<EOF
#!/bin/bash
. /etc/profile
export HOME=/build
cd /build

${cmd}
EOF
		chmod 755 "$copydir/chrootexec"
		librechroot $flags -n "$CHROOT" -l "$CHROOTCOPY" -r /chrootexec
	fi
}

copy_pkgs() {
	for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
		mkdir -p "$copydir/repo"
		pushd "$copydir/repo" >/dev/null
		cp "$pkgfile" .
		repo-add repo.db.tar.gz "${pkgfile##*/}"
		popd >/dev/null

		chown "$LIBREUSER" "$pkgfile"
		mv "$pkgfile" "$PKGDEST"
		if [[ $PKGDEST != . ]]; then
			ln -s "$PKGDEST/${pkgfile##*/}" .
		fi
	done
}