-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzapadd
executable file
·259 lines (241 loc) · 5.09 KB
/
zapadd
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
#!/bin/ksh
#
# SPDX-License-Identifier: CDDL-1.0
#
# {{{ CDDL HEADER
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# }}}
#
# Copyright 2024 Peter Tribble
#
#
# normally, the way that installing a package from zap works is that
# you unzip the zap file, then pkgadd the unpacked package
#
# this script emulates that, but doesn't need to run as root, nor does
# it manage the package metadata, it simply unpacks the archive(s), copies
# the files into place, and creates any links or extra directories
#
# there are assumptions baked into this about the way that zap files
# get generated and the default layout they have
#
bail() {
echo "ERROR: $1"
exit 1
}
builtin mkdir
builtin cp
builtin chmod
builtin ln
builtin cat
DESTDIR=""
ZAPFILE=""
RELOC="reloc"
# TODO filter paths that won't exist in a zone
ZONED=""
# true for quiet, echo to see debug
DEBUG=":"
while getopts "R:Z" opt; do
case $opt in
R)
DESTDIR=${OPTARG}
;;
Z)
ZONED="yes"
;;
*)
bail "invalid option"
;;
esac
done
shift $((OPTIND-1))
case $# in
0)
echo "Usage: $0 -R directory zap_file [zap_file...]"
exit 1
;;
esac
#
# sanity checks
#
for ZAPFILE in "$@"
do
if [ ! -f "${ZAPFILE}" ]; then
bail "file ${ZAPFILE} not found"
fi
case ${ZAPFILE} in
*.zap)
:
;;
*)
bail "expecting a zap file"
;;
esac
done
if [ -z "${DESTDIR}" ]; then
bail "destination directory not set"
fi
case $DESTDIR in
'/')
bail "destination directory cannot be /"
;;
/*)
:
;;
*)
bail "must use absolute path to destination"
;;
esac
#
# filter out pathnames that should not exist in a zone
#
zone_allowed() {
[ -z "${ZONED}" ] && return 0
case $1 in
kernel*|platform*|usr/kernel*)
return 1
;;
esac
return 0
}
#
# put the objects into the alternate root
#
handle_dir() {
if [ -d "$DESTDIR/$1" ]; then
chmod "$2" "$DESTDIR/$1"
else
mkdir -p -m "$2" "$DESTDIR/$1"
fi
echo "d none ${1} ${2} ${3} ${4}" >> "${DESTDIR}/prototype"
}
handle_file() {
ndir=${1%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
cp "$TDIR/${PKGNAME}/$RELOC/$1" "$DESTDIR/$1"
chmod "$2" "$DESTDIR/$1"
echo "${5} ${6} ${1}=${1} ${2} ${3} ${4}" >> "${DESTDIR}/prototype"
}
handle_softlink() {
lname=${1##*=}
rname=${1%%=*}
ndir=${rname%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
ln -s "$lname" "$DESTDIR/$rname"
echo "s none ${1}" >> "${DESTDIR}/prototype"
}
handle_hardlink() {
lname=${1##*=}
rname=${1%%=*}
ndir=${rname%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
# the hardlink target must be relative to the source name
ln "$DESTDIR/${ndir}/$lname" "$DESTDIR/$rname"
echo "l none ${1}" >> "${DESTDIR}/prototype"
}
#
# we also populate a prototype file
#
mkdir -p -m 755 "${DESTDIR}"
touch "${DESTDIR}/prototype"
#
# now loop over all the input zap files
#
for ZAPFILE in "$@"
do
PKGNAME=${ZAPFILE##*/}
PKGNAME=${PKGNAME%%.*}
VISH=$(digest -a md5 "${ZAPFILE}")
TDIR=/tmp/zapadd.${VISH}.$$
rm -fr "$TDIR"
mkdir -p "$TDIR"
if [ ! -d "$TDIR" ]; then
bail "unable to create temporary directory"
fi
#
# extract the zap file
#
unzip -q "${ZAPFILE}" -d "$TDIR"
if [ ! -d "${TDIR}/${PKGNAME}" ]; then
bail "unpack failed"
fi
if [ ! -f "${TDIR}/${PKGNAME}/pkginfo" ]; then
bail "unpack missing pkginfo"
fi
if [ ! -f "${TDIR}/${PKGNAME}/pkgmap" ]; then
bail "unpack missing pkgmap"
fi
#
# now we need to parse the pkgmap file
#
cat "${TDIR}/${PKGNAME}/pkgmap" | while read -r part type class name perms user group
do
case $type in
i)
$DEBUG "Ignoring metadata $class"
;;
b|c)
$DEBUG "Ignoring device file $name"
;;
e|f|v)
$DEBUG "File $name, perms $perms"
if [ -f "${TDIR}/${PKGNAME}/${RELOC}/$name" ]; then
zone_allowed "$name" && handle_file "$name" "$perms" "$user" "$group" "$type" "$class"
fi
;;
d|x)
$DEBUG "Directory $name, perms $perms"
zone_allowed "$name" && handle_dir "$name" "$perms" "$user" "$group"
;;
l)
$DEBUG "Defer hard link $name"
;;
s)
$DEBUG "Soft link $name"
zone_allowed "$name" && handle_softlink "$name"
;;
1)
:
# the pkgmap header, presumably
;;
*)
echo "Unrecognized $part"
;;
esac
done
#
# do the hard links in a second pass, to try and guarantee that all the
# target files exist (won't necessarily work for cross-package hard links)
#
cat "${TDIR}/${PKGNAME}/pkgmap" | while read -r part type class name perms user group
do
case $type in
l)
$DEBUG "Hard link $name"
zone_allowed "$name" && handle_hardlink "$name"
;;
esac
done
#
# clean up
#
rm -fr "$TDIR"
#
# end of loop over zap files
#
done