summaryrefslogtreecommitdiff
path: root/mk/pkginstall/files
blob: 0b13f4973243a3b81386dd0da678b0c504ff8d94 (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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# $NetBSD: files,v 1.6 2008/09/25 14:01:46 wiz Exp $
#
# Generate a +FILES script that reference counts config files that are
# required for the proper functioning of the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+FILES)
	${CAT} > ./+FILES << 'EOF'
#!@SH@
#
# +FILES - reference-counted configuration file management script
#
# Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]
#        ./+FILES VIEW-REMOVE depotdir viewdir
#        ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
#
# This script supports two actions, ADD and REMOVE, that will add or
# remove the configuration files needed by the package associated with
# <metadatadir>.  The CHECK-ADD action will check whether any files
# needed by the package are missing, and print an informative message
# noting those files.  The CHECK-REMOVE action will check whether
# any files needed by the package still exist, and print an informative
# message noting those files.  The CHECK-ADD and CHECK-REMOVE actions
# return non-zero if they detect either missing or existing files,
# respectively.  The VIEW-REMOVE action will remove from <viewdir> the
# links to the configuration files in <depotdir>.  The PERMS action
# will correct any ownership or permission discrepancies between the
# existing files and the data in this script, and the CHECK-PERMS
# action will check whether any files have the wrong ownership or
# permission and print an informative message noting those files.  The
# CHECK-PERMS action will return non-zero if it detects files with
# wrong ownership or permissions.
#
# Lines starting with "# FILE: " are data read by this script that
# name the files that this package requires to exist to function
# correctly, along with the locations of the example files, e.g.
#
#	# FILE: /etc/bar.conf c /example/bar.conf
#	# FILE: /etc/baz/conf c /example/baz.conf 0600 foo-user foo-group
#
# For each FILE entry, if the file path is relative, then it is taken to
# be relative to ${PKG_PREFIX}.
#
# The second field in each FILE entry is a set of flags with the following
# meaning:
#
#	c	file is copied into place
#	f	ignore ${PKG_CONFIG}
#	r	file is an rc.d script (consider ${PKG_RCD_SCRIPTS})
#
AWK="@AWK@"
CAT="@CAT@"
CP="@CP@"
CHGRP="@CHGRP@"
CHMOD="@CHMOD@"
CHOWN="@CHOWN@"
CMP="@CMP@"
ECHO="@ECHO@"
GREP="@GREP@"
LS="@LS@"
MKDIR="@MKDIR@"
MV="@MV@"
PWD_CMD="@PWD_CMD@"
RM="@RM@"
RMDIR="@RMDIR@"
SED="@SED@"
SORT="@SORT@"
TEST="@TEST@"
TRUE="@TRUE@"

SELF=$0
ACTION=$1

: ${PKG_PREFIX=@PREFIX@}

case "${PKG_CONFIG:-@PKG_CONFIG@}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CONFIG=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CONFIG=no
	;;
esac
case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CONFIG_PERMS=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CONFIG_PERMS=no
	;;
esac
case "${PKG_RCD_SCRIPTS:-@PKG_RCD_SCRIPTS@}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_RCD_SCRIPTS=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_RCD_SCRIPTS=no
	;;
esac

case $ACTION in
VIEW-REMOVE)
	DEPOTDIR="$2"
	VIEWDIR="$3"
	${TEST} -n "${DEPOTDIR}" -a -n "${VIEWDIR}" || exit 0
	;;
*)
	CURDIR=`${PWD_CMD}`
	PKG_METADATA_DIR="${2-${CURDIR}}"
	: ${PKGNAME=${PKG_METADATA_DIR##*/}}
	: ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
	: ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
	PKG_REFCOUNT_FILES_DBDIR="${PKG_REFCOUNT_DBDIR}/files"
	;;
esac

exitcode=0
case $ACTION in
ADD)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -u |
	while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		case $f_flags in
		*c*)	;;
		*)	continue ;;
		esac
		case $f_eg in
		"")	continue ;;
		[!/]*)	f_eg="${PKG_PREFIX}/$f_eg" ;;
		esac

		shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
		perms="$shadow_dir/+PERMISSIONS"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		if ${TEST} ! -d "$shadow_dir"; then
			${MKDIR} $shadow_dir
			${TEST} ! -f "$file" ||
				${ECHO} "${PKGNAME}" > $preexist
		fi
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			:
		else
			${ECHO} "${PKG_METADATA_DIR}" >> $token
		fi

		case $f_mode$f_user$f_group in
		"")	;;
	 	*)	${ECHO} "$f_mode $f_user $f_group" > $perms ;;
		esac
		if ${TEST} ! -f "$file" -a ! -f "$f_eg" -a ! -c "$f_eg"; then
			:
		else
			case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
			*f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
				if ${TEST} -f "$file"; then
					${ECHO} "${PKGNAME}: $file already exists"
				elif ${TEST} -f "$f_eg" -o -c "$f_eg"; then
					${ECHO} "${PKGNAME}: copying $f_eg to $file"
					${CP} $f_eg $file
					case $f_user in
					"")	;;
					*)	${CHOWN} $f_user $file ;;
					esac
					case $f_group in
					"")	;;
					*)	${CHGRP} $f_group $file ;;
					esac
					case $f_mode in
					"")	;;
					*)	${CHMOD} $f_mode $file ;;
					esac
				fi
				;;
			esac
		fi
	done
	;;

REMOVE)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		case $f_flags in
		*c*)	;;
		*)	continue ;;
		esac
		case $f_eg in
		"")	continue ;;
		[!/]*)	f_eg="${PKG_PREFIX}/$f_eg" ;;
		esac

		shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
		perms="$shadow_dir/+PERMISSIONS"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		tokentmp="$token.tmp.$$"
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
			case `${CAT} $tokentmp | ${SED} -n "$="` in
			"")
				if ${TEST} -f "$preexist"; then
					:
				elif ${TEST} -f "$file" -a \( -f "$f_eg" -o -c "$f_eg" \) && \
				     ${CMP} -s "$file" "$f_eg"; then
					case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
					*f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
						${RM} -f "$file"
						;;
					esac
				fi
				${RM} -f $perms $preexist $token $token.tmp.*
				${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
				;;
			*)
				${MV} -f $tokentmp $token
				;;
			esac
		fi
	done
	;;

PERMS)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	while read file f_flags f_eg f_mode f_user f_group; do
		case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
		yes:yes)	;;
		*)		continue ;;
		esac
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		case $f_user in
		"")	;;
		*)	${CHOWN} $f_user $file ;;
		esac
		case $f_group in
		"")	;;
		*)	${CHGRP} $f_group $file ;;
		esac
		case $f_mode in
		"")	;;
		*)	${CHMOD} $f_mode $file ;;
		esac
	done
	;;

VIEW-REMOVE)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		${DEPOTDIR}/*)	link="${VIEWDIR}/${file#${DEPOTDIR}/}" ;;
		[!/]*)		link="${VIEWDIR}/$file" ;;
		*)		continue ;;
		esac
		dir="${link%[^/]*}"
		if ${TEST} -h "$link"; then
			${RM} -f $link
			${RMDIR} -p $dir 2>/dev/null || ${TRUE}
		fi
	done
	;;

CHECK-ADD)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	{ while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		${TEST} ! -f "$file" || continue
		case $f_flags in
		*c*)	;;
		*)	continue ;;
		esac
		case $f_eg in
		"")	continue ;;
		[!/]*)	f_eg="${PKG_PREFIX}/$f_eg" ;;
		esac

		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following files should be created for ${PKGNAME}:"
			;;
		esac
		${ECHO} ""
		case $f_mode:$f_user:$f_group in
		::)
			${ECHO} "	$file"
			;;
		[!:]*::)
			${ECHO} "	$file (m=$f_mode)"
			;;
		[!:]*:[!:]*:)
			${ECHO} "	$file (m=$f_mode, o=$f_user)"
			;;
		[!:]*:[!:]*:[!:]*)
			${ECHO} "	$file (m=$f_mode, o=$f_user, g=$f_group)"
			;;
		esac
		${TEST} ! -f "$f_eg" || ${ECHO} "	    [$f_eg]"
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-REMOVE)
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	{ while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		${TEST} -f "$file" || continue
		shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
		${TEST} ! -d "$shadow_dir" || continue	# refcount isn't zero
		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following files are no longer being used by ${PKGNAME},"
			${ECHO} "and they can be removed if no other packages are using them:"
			${ECHO} ""
			;;
		esac
		${ECHO} "	$file"
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-PERMS)
	tmpdir="./.pkginstall.$$"
	${MKDIR} -p $tmpdir 2>/dev/null || exit 1
	${CHMOD} 0700 $tmpdir
	${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
	{ while read file f_flags f_eg f_mode f_user f_group; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac
		${TEST} -f "$file" || continue
		case $f_mode:$f_user:$f_group in
		::)	continue ;;
		esac

		perms=`${LS} -l $file | ${AWK} '{ print $1":"$3":"$4 }'`
		testpath="$tmpdir/file_perms"
		${ECHO} > $testpath
		${CHMOD} $f_mode $testpath 2>/dev/null
		longmode=`${LS} -l $testpath | ${AWK} '{ print $1 }'`
		case $f_mode:$f_user:$f_group in
		:[!:]*:)
			case "$perms" in
			*:$f_user:*)	continue ;;
			esac
			;;
		:[!:]*:[!:]*)
			case "$perms" in
			*:$f_user:$f_group)	continue ;;
			esac
			;;
		[!:]*::)
			case "$perms" in
			$longmode:*:*)	continue ;;
			esac
			;;
		[!:]*:[!:]*:)
			case "$perms" in
			$longmode:$f_user:*)	continue ;;
			esac
			;;
		[!:]*:[!:]*:[!:]*)
			case "$perms" in
			$longmode:$f_user:$f_group)	continue ;;
			esac
			;;
		esac

		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following files are used by ${PKGNAME} and have"
			${ECHO} "the wrong ownership and/or permissions:"
			${ECHO} ""
			;;
		esac
		case $f_mode:$f_user:$f_group in
		[!:]*::)
			${ECHO} "	$file (m=$f_mode)"
			;;
		[!:]*:[!:]*:)
			${ECHO} "	$file (m=$f_mode, o=$f_user)"
			;;
		[!:]*:[!:]*:[!:]*)
			${ECHO} "	$file (m=$f_mode, o=$f_user, g=$f_group)"
			;;
		esac
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	${RM} -fr $tmpdir
	;;

*)
	${ECHO} "Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]"
	${ECHO} "       ./+FILES VIEW-REMOVE depotdir viewdir"
	${ECHO} "       ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]"
	;;
esac
exit $exitcode

EOF
	${SED} -n "/^\# FILE: /p" ${SELF} >> ./+FILES
	${CHMOD} +x ./+FILES
	;;
esac