aboutsummaryrefslogtreecommitdiff
blob: 64d2af23cf08f8add70e1b022fbc434d6d88eabb (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
#!/bin/bash
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

source "${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"/isolated-functions.sh

if [ $# -lt 1 ] ; then
	echo "${0##*/}: at least one argument needed" 1>&2
	exit 1
fi

if [[ "$1" == "-r" ]] ; then
	DOINSRECUR=y
	shift
else
	DOINSRECUR=n
fi

if [[ ${INSDESTTREE#${D}} != "${INSDESTTREE}" ]]; then
	vecho "-------------------------------------------------------" 1>&2
	vecho "You should not use \${D} with helpers." 1>&2
	vecho "  --> ${INSDESTTREE}" 1>&2
	vecho "-------------------------------------------------------" 1>&2
	exit 1
fi

case "$EAPI" in
	0|1|2|3|3_pre2)
		PRESERVE_SYMLINKS=n
		;;
	*)
		PRESERVE_SYMLINKS=y
		;;
esac

export TMP=$T/.doins_tmp
# Use separate directories to avoid potential name collisions.
mkdir -p "$TMP"/{1,2}

[[ ! -d ${D}${INSDESTTREE} ]] && dodir "${INSDESTTREE}"

_doins() {
	local mysrc="$1" mydir="$2" cleanup="" rval

	if [ -L "$mysrc" ] ; then
		if [ $PRESERVE_SYMLINKS = y ] ; then
			rm -rf "$D$INSDESTTREE/$mydir/${mysrc##*/}" || return $?
			cp -P "$mysrc" "$D$INSDESTTREE/$mydir/${mysrc##*/}"
			return $?
		else
			cp "$mysrc" "$TMP/2/${mysrc##*/}" || return $?
			mysrc="$TMP/2/${mysrc##*/}"
			cleanup=$mysrc
		fi
	fi

	install ${INSOPTIONS} "${mysrc}" "${D}${INSDESTTREE}/${mydir}"
	rval=$?
	[[ -n ${cleanup} ]] && rm -f "${cleanup}"
	[ $rval -ne 0 ] && echo "!!! ${0##*/}: $mysrc does not exist" 1>&2
	return $rval
}

_xdoins() {
	local -i failed=0
	while read -d $'\0' x ; do
		_doins "$x" "${x%/*}"
		((failed|=$?))
	done
	return $failed
}

success=0
failed=0

for x in "$@" ; do
	if [[ $PRESERVE_SYMLINKS = n && -d $x ]] || \
		[[ $PRESERVE_SYMLINKS = y && -d $x && ! -L $x ]] ; then
		if [ "${DOINSRECUR}" == "n" ] ; then
			continue
		fi

		while [ "$x" != "${x%/}" ] ; do
			x=${x%/}
		done
		if [ "$x" = "${x%/*}" ] ; then
			pushd "$PWD" >/dev/null
		else
			pushd "${x%/*}" >/dev/null
		fi
		x=${x##*/}
		x_orig=$x
		# Follow any symlinks recursively until we've got
		# a normal directory for 'find' to traverse.
		while [ -L "$x" ] ; do
			pushd "$(readlink "$x")" >/dev/null
			x=${PWD##*/}
			pushd "${PWD%/*}" >/dev/null
		done
		if [[ $x != $x_orig ]] ; then
			mv "$x" "$TMP/1/$x_orig"
			pushd "$TMP/1" >/dev/null
		fi
		find "$x_orig" -type d -exec dodir "${INSDESTTREE}/{}" \;
		find "$x_orig" \( -type f -or -type l \) -print0 | _xdoins
		if [[ ${PIPESTATUS[1]} -eq 0 ]] ; then
			# NOTE: Even if only an empty directory is installed here, it
			# still counts as success, since an empty directory given as
			# an argument to doins -r should not trigger failure.
			((success|=1))
		else
			((failed|=1))
		fi
		if [[ $x != $x_orig ]] ; then
			popd >/dev/null
			mv "$TMP/1/$x_orig" "$x"
		fi
		while popd >/dev/null 2>&1 ; do true ; done
	else
		_doins "${x}"
		if [[ $? -eq 0 ]] ; then
			((success|=1))
		else
			((failed|=1))
		fi
	fi
done
rm -rf "$TMP"
[[ $failed -ne 0 || $success -eq 0 ]] && exit 1 || exit 0