/
pkg_install
executable file
·166 lines (141 loc) · 3.61 KB
/
pkg_install
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
#!/bin/bash
TARPKG=tar-1.23-1
CURLPKG=curl-7.49.1-1
PERMANENT=no
FORCE=no
LIST=no
INTERNAL=no
FROMPKG=""
while [ "${1:0:1}" = "-" ]; do
[ "$1" = --permanent ] && PERMANENT=yes && shift && continue
[ "$1" = --force ] && FORCE=yes && shift && continue
[ "$1" = -l ] && LIST=yes && shift && continue
[ "$1" = --internal ] && INTERNAL=yes && shift && continue
[ "$1" = --list ] && LIST=yes && shift && continue
[ "$1" = --frompkg ] && FROMPKG="$2" && shift 2 && continue
break
done
pkgfile=""
STATE=/var/state/installed_files
STATEPKGS=/var/state/installed_pkgs
PERMPKGS=/var/state/permanent_pkgs
PERMFILES=/var/state/permanent_files
MOVEFILES=/var/state/movedfiles.tar
TMPFILE=/tmp/filelist.$$
trap "rm -f $TMPFILE" EXIT
[ "$LIST" = yes ] && (cat $STATEPKGS $PERMPKGS|tr -d ':') 2>/dev/null && exit 0
if [ -z "$1" ]; then
echo "Usage: pkg_install [--permanent] [--force] [-l] [--frompkg PKG] <pkg>"
exit 0
fi
pkg="$(basename $1)"
mkdir -p /var/state
if grep -qs ":$pkg:" $PERMPKGS; then
echo $pkg already permanently installed
exit 0
fi
if grep -qs ":$pkg:" $STATEPKGS; then
echo $pkg already installed
exit 0
fi
[ -f "/var/spool/pkg/$pkg.tar.gz" ] && pkgfile="/var/spool/pkg/$pkg.tar.gz"
[ -f "/var/spool/pkg/$pkg.tgz" ] && pkgfile="/var/spool/pkg/$pkg.tgz"
if [ -z "$pkgfile" ]; then
echo "$pkg not found in spool"
if [ "$RECURSIVEBUILD" = yes ]; then
if [ "$FROMPKG" ]; then
/var/lib/build/pkg_build "$FROMPKG"
else
/var/lib/build/pkg_build $pkg
fi
exit 1
fi
exit 1
fi
if [ "$INTERNAL" = no ]; then
if ! grep -qs ":$TARPKG:" $PERMPKGS; then
echo "Trying to install $TARPKG for full functionality."
if ! /var/lib/build/pkg_install --internal --permanent $TARPKG; then
echo "I need pkg $TARPKG permanently installed!"
echo "Please run: \"/var/lib/build/pkg_build $TARPKG\""
exit 1
fi
fi
if ! /var/lib/build/pkg_available $CURLPKG; then
if ! /var/lib/build/pkg_build $CURLPKG; then
echo Failed to build base dependency $CURLPKG
exit 1
fi
fi
fi
tar tf "$pkgfile" > $TMPFILE
function movefiles {
local rc=0
cd /
while read f; do
if [ -d "$f" ]; then
continue
fi
if [ -e "$f" ]; then
grep -qs "^$f$" $STATE && continue
if [ -f $MOVEFILES ]; then
tar tf $MOVEFILES "$f" &>/dev/null && continue
fi
if ! tar rf $MOVEFILES "$f"; then
echo "Failed to take backup of $f"
rc=1
fi
fi
done
return $rc
}
function rmfiles {
local rc=0
cd /
while read f; do
if [ -d "$f" -a ! -L "$f" ]; then
/tmp/rmdir "$f" &>/dev/null
continue
fi
if [ -e "$f" ]; then
if ! /tmp/rm -f "$f"; then
echo "Failed to remove $f"
rc=1
fi
fi
done
return $rc
}
echo -n "Installing $pkg: ."
if [ "$PERMANENT" = no ]; then
movefiles < $TMPFILE
[ $? = 0 ] || exit 1
fi
[ -f /sbin/remount ] && remount w
cd /
# make sure we have access to some commands
cp -p /bin/tar /tmp/tar || exit 1
cp -p /bin/tac /tmp/tac || exit 1
cp -p /bin/cat /tmp/cat || exit 1
cp -p /bin/rm /tmp/rm || exit 1
cp -p /bin/rmdir /tmp/rmdir || exit 1
# remove files that we are about to install
/tmp/tac $TMPFILE | rmfiles
if [ "$PERMANENT" = no ]; then
/tmp/cat $TMPFILE >> $STATE
else
/tmp/cat $TMPFILE >> $PERMFILES
fi
if [ "$INTERNAL" = yes ]; then
/tmp/tar xf "$pkgfile" || exit 1
else
/tmp/tar xf "$pkgfile" --checkpoint=500 --checkpoint-action="dot" || exit 1
fi
/tmp/rm -f /tmp/tar /tmp/tac /tmp/rm /tmp/rmdir /tmp/cat
echo " Done!"
[ -f /sbin/remount ] && remount r
if [ "$PERMANENT" = no ]; then
echo ":$pkg:" >> $STATEPKGS
else
echo ":$pkg:" >> $PERMPKGS
fi