-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpkg.sh
executable file
·305 lines (286 loc) · 7.13 KB
/
lpkg.sh
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/busybox sh
fail() {
echo "ERROR: $1" >&2
if [ -z "$2" ]; then
exit 1
fi
exit "$2"
}
fetch() {
local fetcher
if [ -z "$bootstrap" ]; then
fetcher=$(ls "$LPKGDIR/fetchers" | sort -nr | head -n 1)
# turn into a complete path
fetcher="$LPKGDIR/fetchers/$fetcher"
else
fetcher="$bootstrap/fetchers/https.sh"
fi
if [ ! -e "$fetcher" ]; then
fail "No fetcher found!"
fi
"$fetcher" "$@" || return $?
}
infoval() {
(
. "$1"
local val="$2"
eval "echo \$$val"
) || return $?
}
# fetch and verify package info
fetchinfo() {
if [ -e "$LPKGDIR/db/$1/pkginfo.sh" -a -z "$UPDATE" ]; then
cp "$LPKGDIR/db/$1/pkginfo.sh" "$tmpdir/$1.pkginfo" || return $?
else
echo "Fetching info for $1"
fetch "$REPO" "pkgs/$1.pkginfo" "$tmpdir/$1.pkginfo" || return $?
fetch "$REPO" "pkgs/$1.pkginfo.minisig" "$tmpdir/$1.pkginfo.minisig" || return $?
minisign -q -Vm "$tmpdir/$1.pkginfo" -p "$LPKGDIR/pubkey.pub" || return 4
local iv="$(infoval "$tmpdir/$1.pkginfo" NAME)" || return $?
if [ "$iv" != "$1" ]; then
echo "Package name mismatch: requested $1 but got $iv" >&2
return 4
fi
fi
}
contains() {
local i a="$1"
shift
for i in $@; do
if [ "$i" == "$a" ]; then
return 0
fi
done
return 1
}
resdep() {
if contains $1 $deps; then
return 0
fi
fetchinfo $1 || return $?
deps="$deps $1"
for i in $(infoval "$tmpdir/$1.pkginfo" DEPENDENCIES); do
resdep "$i" || return $?
done
depo="$depo $1"
}
resdeptree() {
deps=
depo=
for i in $@; do
resdep $i || return $?
done
}
fetchpkg() {
local hash=$(infoval "$tmpdir/$1.pkginfo" SHA256SUM) || return 4
if [ -z "$hash" ]; then
echo "No hash in pkginfo!" >&2
return 4
fi
echo "Downloading $1"
fetch "$REPO" "pkgs/$1.tar.gz" "$tmpdir/$1.tar.gz" || return $?
if [ "$hash" != "$(sha256sum "$tmpdir/$1.tar.gz" | awk '{print $1}')" ]; then
echo "SHA256 hash does not match on $1.tar.gz!" >&2
return 4
fi
}
instpkg() {
if [ -z "$bootstrap" ]; then
lpkg-inst "$tmpdir/$1.tar.gz" || return $?
else
sh "$bootstrap"/inst.sh "$tmpdir/$1.tar.gz" || return $?
fi
}
rmpkg() {
if [ -z "$bootstrap" ]; then
lpkg-rm "$@" || return $?
else
sh "$bootstrap"/rm.sh $@ || return $?
fi
}
calcchange() {
toinstall=
toupdate=
toremove=
insto=
for i in $depo; do
if [ -d "$LPKGDIR/db/$i" ]; then
local nver=$(infoval "$tmpdir/$i.pkginfo" VERSION)
if [ -z "$nver" ]; then
echo "No version??????" >&2
return 2
fi
local over=$(infoval "$LPKGDIR/db/$i/pkginfo.sh" VERSION)
if [ -z "$over" ]; then
echo "No version??????" >&2
return 2
fi
if vercmp "$nver" "$over"; then
toupdate="$toupdate $i"
insto="$insto $i"
fi
else
toinstall="$toinstall $i"
insto="$insto $i"
fi
done
for i in $(ls "$LPKGDIR/db"); do
if ! contains "$i" $deps; then
toremove="$toremove $i"
fi
done
}
ynprompt() {
echo -n "$1"
read ok
if [ "$ok" == y ]; then
return 0
elif [ "$ok" == Y ]; then
return 0
elif [ "$ok" == n ]; then
return 1
elif [ "$ok" == N ]; then
return 1
else
return "$2"
fi
}
prompt() {
echo "To install:"
for i in $toinstall; do
echo $'\t'"$i"
done
echo "To update:"
for i in $toupdate; do
echo $'\t'"$i"
done
echo "To remove:"
for i in $toremove; do
echo $'\t'"$i"
done
if [ "$NONINTERACTIVE" != 1 ]; then
if ynprompt "Ok? (y/N): "; then
return 0
else
echo "Transaction cancelled" >&2
return 1
fi
fi
}
transact() {
resdeptree "$@" || return 2
calcchange
prompt || return $?
echo "Downloading packages"
for i in $insto; do
fetchpkg $i || return 2
done
echo "Installing packages"
for i in $insto; do
instpkg $i || return 3
done
for i in $toremove; do
rmpkg $i || return 3
done
}
setup() {
if [ -z "$LPKGDIR" ]; then
echo "LPKGDIR not set. Assuming $ROOTFS/etc/lpkg.d"
LPKGDIR="$ROOTFS/etc/lpkg.d"
fi
if [ ! -d "$LPKGDIR" ]; then
fail "$LPKGDIR does not exist" 2
fi
if [ ! -e "$LPKGDIR/lpkg.conf" ]; then
fail "lpkg.conf is missing" 3
fi
. "$LPKGDIR/lpkg.conf"
export LPKGDIR
tmpdir=$(mktemp -d) || return 2
}
tmpcleanup() {
rm -rf "$tmpdir"
}
if [ $# -lt 1 ]; then
echo "Usage: $0 [subcommand]"
echo "Run $0 help for more help"
exit 1
fi
if [ "$1" == "help" ]; then
echo "Subcommands: install, bootstrap, update, remove"
elif [ "$1" == "install" ]; then
if [ $# -lt 2 ]; then
echo "Nothing specified to install!" >&2
exit 1
fi
shift
setup || fail "Failed to create temporary directory" 2
pins=$(cat "$LPKGDIR/pins.list") || fail "Failed to read pin list" 2
transact $pins $@ || fail "Transaction failed" 3
for i in $@; do
if ! contains $i $pins; then
echo $i >> "$LPKGDIR/pins.list"
fi
done
tmpcleanup
elif [ "$1" == "update" ]; then
UPDATE=1
setup || fail "Failed to create temporary directory" 2
pins=$(cat "$LPKGDIR/pins.list") || fail "Failed to read pin list" 2
transact $pins || fail "Transaction failed" 3
tmpcleanup
elif [ "$1" == "remove" ]; then
shift
setup || fail "Failed to create temporary directory" 2
opins=$(cat "$LPKGDIR/pins.list") || fail "Failed to read pin list" 2
for i in $@; do
if ! contains $i $opins; then
tmpcleanup
fail "$i is not pinned" 1
fi
done
pins=
for i in $opins; do
if ! contains $i $@; then
pins="$pins $i"
fi
done
transact $pins || fail "Transaction failed" 3
tmpcleanup
(
for i in $pins; do
echo $i
done
) > "$LPKGDIR/pins.list"
elif [ "$1" == "bootstrap" ]; then
if [ $# -lt 5 ]; then
echo "Missing arguments" >&2
exit 1
fi
export bootstrap="$PWD"
export ROOTFS="$2"
REPO="$3/$4/$5"
mkdir "$ROOTFS" || fail "Failed to mkdir $ROOTFS" 2
mkdir -p "$ROOTFS/etc/lpkg.d/db" || fail "Failed to create lpkg.d" 2
curl -s "https://$3/minisign.pub" > "$ROOTFS/etc/lpkg.d/pubkey.pub" || fail "Failed to download public key" 4
shift
shift
shift
shift
shift
if [ $# -lt 1 ]; then
pkgs=base
else
pkgs="$@"
fi
echo "REPO=\"$REPO\"" > "$ROOTFS/etc/lpkg.d/lpkg.conf"
setup || fail "Failed to create temporary directory" 2
transact $pkgs || fail "Failed transaction" 3
for i in $pkgs; do
echo $i >> "$LPKGDIR/pins.list"
done
echo "Bootstrap complete!"
tmpcleanup
else
fail "Invalid subcommand" 1
fi