summaryrefslogtreecommitdiff
path: root/functions/packages.sh
blob: 83d15e09eb6fb5fc88adf02a72ffd882dcd17d44 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


# The file that records temporarily installed packages.
Installed_tmp_packages_file ()
{
	echo "chroot.installed_tmp_pkgs"
}

# Note, writes to _LB_PACKAGES
Check_package ()
{
	local CHROOT="${1}"
	local FILE="${2}"
	local PACKAGE="${3}"

	Check_installed "${CHROOT}" "${FILE}" "${PACKAGE}"

	if [ "${INSTALL_STATUS}" -ne 0 ]
	then
		if [ "${LB_BUILD_WITH_CHROOT}" != "false" ] && [ "${CHROOT}" = "chroot" ]
		then
			_LB_PACKAGES="${_LB_PACKAGES} ${PACKAGE}"
		else
			Echo_error "You need to install %s on your host system." "${PACKAGE}"
			exit 1
		fi
	fi
}

# Note, reads from _LB_PACKAGES
Install_packages ()
{
	if [ -z "${_LB_PACKAGES}" ] || [ "${LB_BUILD_WITH_CHROOT}" != "true" ]; then
		return
	fi

	# Record in file to survive failure such that recovery can take place.
	local LIST_FILE
	LIST_FILE="$(Installed_tmp_packages_file)"
	local PACKAGE
	for PACKAGE in ${_LB_PACKAGES}; do
		echo "${PACKAGE}" >> "${LIST_FILE}"
	done

	case "${LB_APT}" in
		apt|apt-get)
			Chroot chroot "apt-get install -o APT::Install-Recommends=false ${APT_OPTIONS} ${_LB_PACKAGES}"
			;;

		aptitude)
			Chroot chroot "aptitude install --without-recommends ${APTITUDE_OPTIONS} ${_LB_PACKAGES}"
			;;
	esac
	unset _LB_PACKAGES # Can clear this now
}

Remove_packages ()
{
	if [ "${LB_BUILD_WITH_CHROOT}" != "true" ]; then
		return
	fi

	local LIST_FILE
	LIST_FILE="$(Installed_tmp_packages_file)"

	# List is read from file to ensure packages from any past failure are
	# included in the list on re-running scripts to recover.
	local PACKAGES=""
	if [ -e "${LIST_FILE}" ]; then
		local PACKAGE
		while read -r PACKAGE; do
			PACKAGES="${PACKAGES} ${PACKAGE}"
		done < "${LIST_FILE}"
	fi

	if [ -n "${PACKAGES}" ]; then
		case "${LB_APT}" in
			apt|apt-get)
				Chroot chroot "apt-get remove --auto-remove --purge ${APT_OPTIONS} ${PACKAGES}"
				;;

			aptitude)
				Chroot chroot "aptitude purge --purge-unused ${APTITUDE_OPTIONS} ${PACKAGES}"
				;;
		esac
	fi

	rm -f "${LIST_FILE}"
}

#FIXME: make use of this. see commit log that added this for details.
# Perform temp package removal for recovery if necessary
Cleanup_temp_packages ()
{
	if [ -e "$(Installed_tmp_packages_file)" ]; then
		Remove_packages
	fi
}

# Check_installed
# uses as return value global var INSTALL_STATUS
# INSTALL_STATUS : 0 if package is installed
#                  1 if package isn't installed and we're in an apt managed system
#                  2 if package isn't installed and we aren't in an apt managed system
Check_installed ()
{
	local CHROOT="${1}"
	local FILE="${2}"
	local PACKAGE="${3}"

	if [ "${LB_BUILD_WITH_CHROOT}" = "true" ] && [ "${CHROOT}" = "chroot" ]
	then
		if Chroot chroot "dpkg-query -s ${PACKAGE}" 2> /dev/null | grep -qs "Status: install"
		then
			INSTALL_STATUS=0
		else
			INSTALL_STATUS=1
		fi
	else
		if command -v dpkg-query >/dev/null
		then
			if dpkg-query -s "${PACKAGE}" 2> /dev/null | grep -qs "Status: install"
			then
				INSTALL_STATUS=0
			else
				INSTALL_STATUS=1
			fi
		else
			if [ ! -e "${FILE}" ]
			then
				INSTALL_STATUS=2
			else
				INSTALL_STATUS=0
			fi
		fi
	fi
}