aboutsummaryrefslogtreecommitdiff
blob: 60b0838341e9b801e9a9fd2c09318652ddd361e3 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
# Copyright 1999-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

source "${PORTAGE_BIN_PATH}"/isolated-functions.sh || exit 1

if [[ -z $1 ]] ; then
	__helpers_die "${0##*/}: at least one argument needed"
	exit 1
fi

if ! ___eapi_has_prefix_variables; then
	ED=${D} EPREFIX=
fi

while [[ $# -gt 0 ]] ; do
	case $1 in
	--ignore)
		shift

		skip_dirs=()
		skip_files=()
		for skip; do
			if [[ -d ${ED%/}/${skip#/} ]]; then
				skip_dirs+=( "${ED%/}/${skip#/}" )
			else
				rm -f "${ED%/}/${skip#/}.ecompress" || die
				skip_files+=("${ED%/}/${skip#/}")
			fi
		done

		if [[ ${#skip_dirs[@]} -gt 0 ]]; then
			while read -r -d ''; do
				skip_files+=("${REPLY%.ecompress}")
			done < <(find "${skip_dirs[@]}" -name '*.ecompress' -print0 -delete || die)
		fi

		if [[ ${#skip_files[@]} -gt 0 && -s ${T}/.ecompress_had_precompressed ]]; then
			sed_args=()
			for f in "${skip_files[@]}"; do
				sed_args+=("s|^${f}\$||;")
			done
			sed_args+=('/^$/d')
			sed -f - -i "${T}/.ecompress_had_precompressed" <<< "${sed_args[@]}" || die
		fi

		exit 0
		;;
	--queue)
		shift

		find_args=()
		for path; do
			if [[ -e ${ED%/}/${path#/} ]]; then
				find_args+=( "${ED%/}/${path#/}" )
			fi
		done

		if [[ ${#find_args[@]} -gt 0 ]]; then
			find_args+=( -type f )
			[[ -n ${PORTAGE_DOCOMPRESS_SIZE_LIMIT} ]] &&
				find_args+=( -size "+${PORTAGE_DOCOMPRESS_SIZE_LIMIT}c" )

			declare -A collisions
			while IFS= read -d '' -r path; do
				# detect the horrible posibility of the ebuild installing
				# colliding compressed and/or uncompressed variants
				# and fail hard (bug #667072)
				#
				# note: to save time, we need to do this only if there's
				# at least one compressed file
				case ${path} in
					*.Z|*.gz|*.bz2|*.lzma|*.xz)
						vpath=${path%.*}
						for comp in '' .Z .gz .bz2 .lzma .xz; do
							if [[ ${vpath}${comp} != ${path} && \
									-e ${vpath}${comp} ]]; then
								collisions[${path}]=1
								collisions[${vpath}]=1
								# ignore compressed variants in that case
								continue 2
							fi
						done
						echo "${path}" >> "${T}"/.ecompress_had_precompressed
						;;
				esac

				>> "${path}.ecompress" || die
			done < <(find "${find_args[@]}" -print0 || die)

			if [[ ${#collisions[@]} -gt 0 ]]; then
				eqawarn "QA Notice: Colliding files found by ecompress:"
				eqawarn
				for x in "${!collisions[@]}"; do
					eqawarn "  ${x}"
				done
				eqawarn
				eqawarn "Please remove the extraneous compressed variants."
			fi
		fi

		exit 0
		;;
	--dequeue)
		[[ -n ${2} ]] && die "${0##*/}: --dequeue takes no additional arguments"
		break
		;;
	*)
		die "${0##*/}: unknown arguments '$*'"
		exit 1
		;;
	esac
	shift
done

# setup compression stuff
PORTAGE_COMPRESS=${PORTAGE_COMPRESS-bzip2}
if [[ -z ${PORTAGE_COMPRESS} ]]; then
	find "${ED}" -name '*.ecompress' -delete
	exit 0
fi

if [[ ${PORTAGE_COMPRESS_FLAGS+set} != "set" ]] ; then
	case ${PORTAGE_COMPRESS} in
		bzip2|gzip)  PORTAGE_COMPRESS_FLAGS="-9";;
	esac
fi

guess_suffix() {
	set -e
	tmpdir="${T}"/.ecompress$$.${RANDOM}
	mkdir "${tmpdir}"
	cd "${tmpdir}"
	# we have to fill the file enough so that there is something
	# to compress as some programs will refuse to do compression
	# if it cannot actually compress the file
	echo {0..1000} > compressme
	${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS} compressme > /dev/null
	# If PORTAGE_COMPRESS_FLAGS contains -k then we need to avoid
	# having our glob match the uncompressed file here.
	suffix=$(echo compressme.*)
	[[ -z $suffix || "$suffix" == "compressme.*" ]] && \
		suffix=$(echo compressme*)
	suffix=${suffix#compressme}
	cd /
	rm -rf "${tmpdir}"
	echo "${suffix}"
}

# figure out the new suffix
export PORTAGE_COMPRESS_SUFFIX=$(guess_suffix) || die

fix_symlinks() {
	# Repeat until nothing changes, in order to handle multiple
	# levels of indirection (see bug #470916).
	local -i indirection=0
	while true ; do
	local something_changed=
	while read -r -d $'\0' brokenlink ; do
		[[ -e ${brokenlink} ]] && continue
		olddest=$(readlink "${brokenlink}")
		newdest=${olddest}${PORTAGE_COMPRESS_SUFFIX}
		if [[ "${newdest}" == /* ]] ; then
			[[ -f "${D%/}${newdest}" ]] || continue
		else
			[[ -f "${brokenlink%/*}/${newdest}" ]] || continue
		fi
		something_changed=${brokenlink}
		rm -f "${brokenlink}" &&
		ln -snf "${newdest}" "${brokenlink}${PORTAGE_COMPRESS_SUFFIX}"
		((ret|=$?))
	done < <(find "${ED}" -type l -print0 || die)

	[[ -n ${something_changed} ]] || break
	(( indirection++ ))
	if (( indirection >= 100 )) ; then
		# Protect against possibility of a bug triggering an endless loop.
		eerror "ecompress: too many levels of indirection for" \
			"'${something_changed#${ED%/}}'"
		break
	fi
	done

	return ${ret}
}

export PORTAGE_COMPRESS PORTAGE_COMPRESS_FLAGS
find "${ED}" -name '*.ecompress' -delete -print0 |
	___parallel_xargs -0 "${PORTAGE_BIN_PATH}"/ecompress-file
ret=${?}

if [[ -s ${T}/.ecompress_had_precompressed ]]; then
	eqawarn "QA Notice: One or more compressed files were found in docompress-ed"
	eqawarn "directories. Please fix the ebuild not to install compressed files"
	eqawarn "(manpages, documentation) when automatic compression is used:"
	eqawarn
	n=0
	while read -r f; do
		eqawarn "  ${f#${D%/}}"
		if [[ $(( n++ )) -eq 10 ]]; then
			eqawarn "  ..."
			break
		fi
	done <"${T}"/.ecompress_had_precompressed
fi

fix_symlinks
: $(( ret |= ${?} ))
[[ $ret -ne 0 ]] && __helpers_die "${0##*/} failed"

exit ${ret}