summaryrefslogtreecommitdiff
blob: 128e7b5ddf2cced3236feb7d8c9ffb80f0872fb1 (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
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/profiles/prefix/windows/interix/profile.bashrc,v 1.1 2009/06/06 03:55:07 solar Exp $

# One basically always wants -D_ALL_SOURCE, it's some stupid restriction
# to hide useful stuff. newer GCCs have this define built-int, so no need
# for it anymore. if there are problems, uncomment this again!
#[[ ${CFLAGS} != *"-D_ALL_SOURCE"* ]] && CFLAGS="-D_ALL_SOURCE ${CFLAGS}"
#[[ ${CXXFLAGS} != *"-D_ALL_SOURCE"* ]] && CXXFLAGS="-D_ALL_SOURCE ${CXXFLAGS}"

# On interix, binary files (executables, shared libraries) in use
# cannot be replaced during merge.
# But it is possible to rename them and remove lateron when they are
# not used any more by any running program.
#
# This is a workaround for portage bug#199868,
# and should be dropped once portage does sth. like this itself.

interix_cleanup_removed_files() {
	local removedlist=$1
	rm -f "${removedlist}".new

	if [[ -r ${removedlist} ]]; then
		rm -f "${removedlist}".old
	fi
	# restore in case of system fault
	if [[ -r ${removedlist}.old ]]; then
		mv "${removedlist}"{.old,}
	fi

	touch "${removedlist}"{,.new} # ensure they exist

	while read rmstem; do
		# try to remove previously recorded files
		for f in "${ROOT}${rmstem}"*; do
			ebegin "trying to remove ${f}"
			rm -f "${f}" > /dev/null 2>&1
			eend $?
		done
		# but keep it in list if still exists
		for f in "${ROOT}${rmstem}"*; do
			[[ -f ${f} ]] && echo "${rmstem}" >> "${removedlist}".new
			break
		done
	done < "${removedlist}"

	# update the list
	mv "${removedlist}"{,.old}
	mv "${removedlist}"{.new,}
	rm "${removedlist}".old
}

interix_find_removed_slot() {
	local f=$1
	local n=0
	while [[ ${n} -lt 100 && -f "${f}${n}" ]]; do
		n=$((n=n+1))
	done

	if [[ ${n} -ge 100 ]]; then
		echo "too many (>=100) old text files busy of '${f}'" >&2
		exit 1
	fi

	echo $n
}

interix_prepare_file() {
	local failed=0
	if [[ ${PN} == libiconv ]]; then
		# when moving around libiconv, the prefix' coreutils will
		# be damaged, so we really need to use the systems ones.
		/bin/cp -p "${1}" "${1}.new" || failed=1
		/bin/mv "${1}" "${2}" || failed=1
		/bin/mv "${1}.new" "${1}" || failed=1
	else
		my_mv=mv

		[[ "${1}" == */mv ]] && my_mv="${1}.new"
		[[ -f "${1}.new" ]] && rm -f "${1}.new"

		cp -p "${1}" "${1}.new" || failed=1
		${my_mv} "${1}" "${2}" || failed=1
		${my_mv} "${1}.new" "${1}" || failed=1
	fi

	echo $failed
}

post_pkg_preinst() {
	local removedlist="${EROOT}var/lib/portage/files2bremoved"
	interix_cleanup_removed_files $removedlist
	
	# now go for current package
	cd "${D}"
	find ".${EROOT}" -type f | xargs -r /usr/bin/file | grep ' PE ' | while read f t
	do
		f=${f#./} # find prints: "./path/to/file"
		f=${f%:} # file prints: "file-argument: type-of-file"
		test -r "${ROOT}${f}" || continue
		rmstem="${f}.removedbyportage"
		# keep list of old busy text files unique
		grep "^${rmstem}$" "${removedlist}" >/dev/null \
			|| echo "${rmstem}" >> "${removedlist}"

		local n=$(interix_find_removed_slot ${ROOT}${rmstem})
		ebegin "preparing ${ROOT}${f} for merge (${n})"
		eend $(interix_prepare_file "${ROOT}${f}" "${ROOT}${rmstem}${n}")
	done
}

post_pkg_prerm() {
	local removedlist="${EROOT}var/lib/portage/files2bremoved"
	save_IFS=$IFS
	IFS='
';
	local MY_PR=${PR}
	[[ ${MY_PR} == r0 ]] && MY_PR=
	local -a contents=($(<"${EROOT}var/db/pkg/${CATEGORY}/${P}${MY_PR:+-}${MY_PR}/CONTENTS"));
	IFS=$save_IFS
	local -a cont
	for content in "${contents[@]}"; do
		cont=($content)
		f=${cont[1]}
		f=${f#/}

		test -r "${ROOT}${f}" || continue

		if /usr/bin/file "${ROOT}${f}" | grep ' PE ' > /dev/null; then
			# $f should be an absolute path to the installed file
			rmstem="${f}.removedbyportage"

			grep "^${rmstem}$" "${removedlist}" > /dev/null \
				|| echo "${rmstem}" >> "${removedlist}"

			local n=$(interix_find_removed_slot ${ROOT}${rmstem})
			ebegin "preparing ${ROOT}${f} for unmerge ($n)"
			eend $(interix_prepare_file "${ROOT}${f}" "${ROOT}${rmstem}${n}")
		fi
	done
}

pre_pkg_postrm() {
	local removedlist="${EROOT}var/lib/portage/files2bremoved"
	interix_cleanup_removed_files $removedlist
}