-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpp.model
370 lines (323 loc) · 11.3 KB
/
pp.model
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#@ $pp_shlib_suffix: used to replace .%so at end of %files paths
# -- platform initialisation functions may (should) change this
pp_shlib_suffix='.so*'
#@ pp_model_init(): initialise model variables and files
pp_model_init () {
#@ $pp_components: whitespace-delimited list of components seen in %files
pp_components=
#@ $pp_services: whitespace-delimited list of %service seen
pp_services=
rm -f $pp_wrkdir/%files.* \
$pp_wrkdir/%post.* \
$pp_wrkdir/%pre.* \
$pp_wrkdir/%preun.* \
$pp_wrkdir/%postup.* \
$pp_wrkdir/%postun.* \
$pp_wrkdir/%service.* \
$pp_wrkdir/%set \
$pp_wrkdir/%fixup
}
#@ pp_have_component(component): return true if component was defined
pp_have_component () {
pp_contains "$pp_components" "$1"
}
#@ pp_have_all_components(component...): return true if all components defined
pp_have_all_components () {
pp_contains_all "$pp_components" "$@"
}
#@ pp_add_component(component): adds component to $pp_components
pp_add_component () {
pp_add_to_list 'pp_components' "$1"
}
#@ pp_add_service(service): adds service to $pp_services
pp_add_service () {
pp_add_to_list 'pp_services' "$1"
}
#@ pp_service_init_vars(): initialises user service variables
pp_service_init_vars () {
cmd=
pidfile=
stop_signal=15 # SIGTERM
user=root
group=
enable=yes # make it so the service starts on boot
optional=no # Whether installing this service is optional
pp_backend_init_svc_vars
}
#@ pp_service_check_vars(service): error if required user variables are unset
pp_service_check_vars () {
test -n "$cmd" ||
pp_error "%service $1: cmd not defined"
case "$enable" in
yes|no) : ;;
*) pp_error "%service $1: \$enable must be set to yes or no";;
esac
}
#@ pp_load_service_vars(svc): source a file for service definitions
pp_load_service_vars () {
pp_service_init_vars
. "$pp_wrkdir/%service.$1"
pp_service_check_vars "$1"
}
#@ pp_files_expand(path [m] [[u]:[g]] [f] [t]): expand to a file path
# writes multiple lines of the form
# type path mode owner group flags [target]
# flags contains the letter v if the file is volatile, letter m for missingok
pp_files_expand () {
typeset _p _mode _group _owner _flags _path _optional _has_target _tree
typeset _target _file _tgt _m _o _g _f _type _lm _ll _lo _lg _ls _lx
typeset _ignore _a
test $# -eq 0 && return
pp_debug "pp_files_expand: path is: $1"
case "$1" in "#"*) return;; esac
_p="$1"; shift
pp_debug "pp_files_expand: other arguments: $*"
#-- the mode must be an octal number of at least three digits
_mode="="
_a=`eval echo \"$1\"`
case "$_a" in
*:*) :;;
-|=|[01234567][01234567][01234567]*) _mode="$_a"; shift;;
esac
#-- the owner:group field may have optional parts
_a=`eval echo \"$1\"`
case "$_a" in
*:*) _group=${_a#*:}; _owner=${_a%:*}; shift;;
=|-) _group=$_a; _owner=$_a; shift;;
*) _group=; _owner=;;
esac
#-- process the flags argument
_flags=
_target=
_optional=false
_has_target=false
_ignore=false
if test $# -gt 0; then
_a=`eval echo \"$1\"`
case ",$_a," in *,volatile,*) _flags="${_flags}v";; esac
case ",$_a," in *,missingok,*) _flags="${_flags}m";; esac
case ",$_a," in *,optional,*) _optional=true;; esac
case ",$_a," in *,symlink,*) _has_target=true;; esac
case ",$_a," in *,ignore-others,*) _flags="${_flags}i";; esac
case ",$_a," in *,ignore,*) _ignore=true;; esac
shift
fi
#-- process the target argument
if $_has_target; then
test $# -ne 0 || pp_error "$_p: missing target"
_a=`eval echo \"$1\"`
_target="$_a"
shift
fi
pp_debug "pp_files_expand: $_mode|$_owner:$_group|$_flags|$_target|$*"
test $# -eq 0 || pp_error "$_p: too many arguments"
#-- process speciall suffixes
tree=
case "$_p" in
*"/**") _p="${_p%"/**"}"; tree="**";;
*".%so") _p="${_p%".%so"}$pp_shlib_suffix";;
esac
#-- expand the path using the shell glob
pp_debug "expanding .$_p ... with $pp_expand_path"
(cd ${pp_destdir} && $pp_expand_path ".$_p") > $pp_wrkdir/tmp.files.exp
#-- expand path/** by rewriting the glob output file
case "$tree" in
"") : ;;
"**")
pp_debug "expanding /** tree ..."
while read _path; do
_path="${_path#.}"
pp_find_recurse "$pp_destdir${_path%/}"
done < $pp_wrkdir/tmp.files.exp |
sort -u > $pp_wrkdir/tmp.files.exp2
mv $pp_wrkdir/tmp.files.exp2 $pp_wrkdir/tmp.files.exp
;;
esac
while read _path; do
_path="${_path#.}"
_file="${pp_destdir}${_path}"
_tgt=
_m="$_mode"
_o="${_owner:--}"
_g="${_group:--}"
_f="$_flags"
case "$_path" in
/*) :;;
*) pp_warn "$_path: inserting leading /"
_path="/$_path";; # ensure leading /
esac
#-- sanity checks
case "$_path" in
*/../*|*/..) pp_error "$_path: invalid .. in path";;
*/./*|*/.) pp_warn "$_path: invalid component . in path";;
*//*) pp_warn "$_path: redundant / in path";;
esac
#-- set the type based on the real file's type
if $_ignore; then
_type=f _m=_ _o=_ _g=_
elif test -h "$_file"; then
case "$_path" in
*/) pp_warn "$_path (symlink $_file): removing trailing /"
_path="${_path%/}"
;;
esac
_type=s
if test x"$_target" != x"=" -a -n "$_target"; then
_tgt="$_target"
pp_debug "symlink target is $_tgt"
else
_tgt=`pp_readlink "$_file"`;
test -z "$_tgt" && pp_error "can't readlink $_file"
case "$_tgt" in
${pp_destdir}/*)
pp_warn "stripped \$destdir from symlink ($_path)"
_tgt="${_tgt#$pp_destdir}";;
esac
fi
_m=777
elif test -d "$_file"; then
#-- display a warning if the user forgot the trailing /
case "$_path" in
*/) :;;
*) pp_warn "$_path (matching $_file): adding trailing /"
_path="$_path/";;
esac
_type=d
$_has_target && pp_error "$_file: not a symlink"
elif test -f "$_file"; then
case "$_path" in
*/) pp_warn "$_path (matching $_file): removing trailing /"
_path="${_path%/}"
;;
esac
_type=f
$_has_target && pp_error "$_file: not a symlink"
else
$_optional && continue
pp_error "$_file: missing"
_type=f
fi
#-- convert '=' shortcuts into mode/owner/group from ls
case ":$_m:$_o:$_g:" in *:=:*)
if LS_OPTIONS=--color=never /bin/ls -ld "$_file" \
> $pp_wrkdir/ls.tmp
then
read _lm _ll _lo _lg _ls _lx < $pp_wrkdir/ls.tmp
test x"$_m" = x"=" && _m=`pp_mode_from_ls "$_lm"`
test x"$_o" = x"=" && _o="$_lo"
test x"$_g" = x"=" && _g="$_lg"
else
pp_error "cannot read $_file"
test x"$_m" = x"=" && _m=-
test x"$_o" = x"=" && _o=-
test x"$_g" = x"=" && _g=-
fi
;;
esac
test -n "$_f" || _f=-
#-- sanity checks
test -n "$_type" || pp_die "_type empty"
test -n "$_path" || pp_die "_path empty"
test -n "$_m" || pp_die "_m empty"
test -n "$_o" || pp_die "_o empty"
test -n "$_g" || pp_die "_g empty"
#-- setuid/gid files must be given an explicit owner/group (or =)
case "$_o:$_g:$_m" in
-:*:[4657][1357]??|-:*:[4657]?[1357]?|-:*:[4657]??[1357])
pp_error "$_path: setuid file ($_m) missing explicit owner";;
*:-:[2367][1357]??|*:-:[2367]?[1357]?|*:-:[2367]??[1357])
pp_error "$_path: setgid file ($_m) missing explicit group";;
esac
# convert numeric uids into usernames; only works for /etc/passwd
case "$_o" in [0-9]*) _o=`pp_getpwuid $_o`;; esac
case "$_g" in [0-9]*) _g=`pp_getgrgid $_g`;; esac
pp_debug "$_type $_m $_o $_g $_f $_path" $_tgt
$_ignore || echo "$_type $_m $_o $_g $_f $_path" $_tgt
pp_note_file_used "$_path"
case "$_f" in *i*) echo "$_path" >> $pp_wrkdir/ign.files;; esac
done < $pp_wrkdir/tmp.files.exp
}
#@ pp_files_check_duplicates(): raise an error on duplicate files
pp_files_check_duplicates () {
typeset _path
if test -s $pp_wrkdir/all.files; then
sort < $pp_wrkdir/all.files | uniq -d > $pp_wrkdir/duplicate.files
if test -f $pp_wrkdir/ign.awk; then
# Remove ignored files
mv $pp_wrkdir/duplicate.files $pp_wrkdir/duplicate.files.ign
sed -e 's/^/_ _ _ _ _ /' < $pp_wrkdir/duplicate.files.ign |
awk -f $pp_wrkdir/ign.awk |
sed -e 's/^_ _ _ _ _ //' > $pp_wrkdir/duplicate.files
fi
while read _path; do
pp_warn "$_path: file declared more than once"
done <$pp_wrkdir/duplicate.files
fi
}
#@ pp_files_check_coverage(): raise an error if files aren't listed
# compares the list of files under pp_destdir with the list of
# all files inside %files sections. Only generates warnings (for now.)
pp_files_check_coverage () {
pp_find_recurse "$pp_destdir" | sort > $pp_wrkdir/coverage.avail
if test -s $pp_wrkdir/all.files; then
sort -u < $pp_wrkdir/all.files
else
:
fi > $pp_wrkdir/coverage.used
join -v1 $pp_wrkdir/coverage.avail $pp_wrkdir/coverage.used \
> $pp_wrkdir/coverage.not-packaged
if test -s $pp_wrkdir/coverage.not-packaged; then
pp_warn "The following files/directories were found but not packaged:"
sed -e 's,^, ,' < $pp_wrkdir/coverage.not-packaged >&2
fi
join -v2 $pp_wrkdir/coverage.avail $pp_wrkdir/coverage.used \
> $pp_wrkdir/coverage.not-avail
if test -s $pp_wrkdir/coverage.not-avail; then
pp_warn "The following files/directories were named but not found:"
sed -e 's,^, ,' < $pp_wrkdir/coverage.not-avail >&2
fi
}
#@ pp_files_ignore_others(): remove ignored files
pp_files_ignore_others () {
typeset p f
test -s $pp_wrkdir/ign.files || return
#-- for each file in ign.files, we remove it from all the
# other %files.* lists, except where it has an i flag.
# rather than scan each list multiple times, we build
# an awk script
pp_debug "stripping ignore files"
while read p; do
echo '$6 == "'"$p"'" && $5 !~ /i/ { next }'
done < $pp_wrkdir/ign.files > $pp_wrkdir/ign.awk
echo '{ print }' >> $pp_wrkdir/ign.awk
$pp_opt_debug && cat $pp_wrkdir/ign.awk
for f in $pp_wrkdir/%files.*; do
mv $f $f.ign
awk -f $pp_wrkdir/ign.awk < $f.ign > $f || pp_error "awk"
done
}
#@ pp_service_scan_groups(): scan services for groups
# Populates the list $pp_service_groups with all defined groups,
# and creates the files %svcgrp.$group each containing the
# names of services inside each that group, one per line.
pp_service_scan_groups () {
typeset svc
#-- scan for "group" commands, and build a list of groups
pp_service_groups=
if test -n "$pp_services"; then
for svc in $pp_services; do
group=
. $pp_wrkdir/%service.$svc
if test -n "$group"; then
pp_contains "$pp_services" "$group" && pp_error \
"%service $svc: group name $group in use by a service"
pp_add_to_list 'pp_service_groups' "$group"
echo "$svc" >> $pp_wrkdir/%svcgrp.$group
fi
done
fi
}
#@ pp_service_get_svc_group: prints the services with a group on one line
pp_service_get_svc_group () {
(tr '\012' ' ' < $pp_wrkdir/%svcgrp.$1 ; echo) | sed -e 's/ $//'
}