checkflist revision 1.41
1#! /bin/sh --
2#
3#	$NetBSD: checkflist,v 1.41 2009/12/10 17:18:33 uebayasi Exp $
4#
5# Verify output of makeflist against contents of ${DESTDIR} and ${metalog}.
6
7if [ -z "${DESTDIR}" ]; then
8	echo "DESTDIR must be set"
9	exit 1
10fi
11
12prog="${0##*/}"
13rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
14. "${rundir}/sets.subr"
15
16SDIR="$(${MKTEMP} -d "/tmp/${prog}.XXXXXX")"
17
18es=0
19cleanup()
20{
21	/bin/rm -rf "${SDIR}"
22	if [ ${es} -gt 255 ]; then
23		es=255
24	fi
25	exit ${es}
26}
27trap cleanup 0 2 3 13		# EXIT INT QUIT PIPE
28
29origin=.
30xargs=""
31dargs=""
32metalog=
33allowextra=false
34allowmissing=false
35
36# handle args
37while getopts xybL:M:em ch; do
38	case ${ch} in
39	x)
40		xargs="-x"
41		origin="./etc/X11 ./etc/fonts ./usr/X11R6"
42		;;
43	y)
44		xargs="-y"
45		origin="./etc/ext ./usr/ext"
46		;;
47	# backward compat
48	b)
49		xargs="-b"
50		;;
51	L)
52		xargs="-L ${OPTARG}"
53		;;
54	M)
55		metalog="${OPTARG}"
56		;;
57	e)
58		allowextra=true
59		;;
60	m)
61		allowmissing=true
62		;;
63	*)
64		cat 1>&2 <<USAGE
65Usage: ${prog} [-x|-y|-b|-L lists] [-M metalog] [-e] [-m]
66	-x		check only x11 lists
67	-y		check only extsrc lists
68	-b		check netbsd + x11 lists
69	-L base,x,ext	check specified lists
70	-M metalog	metalog file
71	-e		extra files are not considered an error
72	-m		missing files are not considered an error
73USAGE
74		exit 1
75		;;
76	esac
77done
78shift $((${OPTIND} - 1))
79
80#
81# Exceptions to flist checking (all begin with "./"):
82#
83# * ignore var/db/syspkg and its contents
84# * ignore METALOG and METALOG.*
85# * ignore etc/mtree/set.*
86#
87ignore_exceptions()
88{
89IGNORE_REGEXP_SYSPKG="^\./var/db/syspkg(\$|/)"
90IGNORE_REGEXP_METALOG="^\./METALOG(\..*)?\$"
91IGNORE_REGEXP_MTREE="^\./etc/mtree/set\.[a-z]*\$"
92
93	${EGREP} -v \
94		-e "${IGNORE_REGEXP_SYSPKG}" \
95		-e "${IGNORE_REGEXP_METALOG}" \
96		-e "${IGNORE_REGEXP_MTREE}"
97}
98
99#
100# Here would be a good place to add custom exceptions to flist checking.
101#
102
103#
104# Make three lists:
105# * ${SDIR}/files: files present in DESTDIR.
106# * ${SDIR}/flist: files mentioned in flist;
107# * ${SDIR}/mlist: files mentioned in metalog;
108#
109# All three lists are filtered against ${IGNORE_REGEXP}.
110#
111
112generate_files()
113{
114( cd "${DESTDIR}" && ${FIND} ${origin} \
115	\( -type d -o -type f -o -type l \) -print ) \
116	| ${SORT} -u | ignore_exceptions >"${SDIR}/files"
117}
118
119generate_flist()
120{
121${HOST_SH} "${rundir}/makeflist" ${xargs} ${dargs} \
122	| ${SORT} -u | ignore_exceptions >"${SDIR}/flist"
123}
124
125generate_mlist()
126{
127if [ -n "${metalog}" ]; then
128	${AWK} '{print $1}' <"${metalog}" \
129	| ${SORT} -u | ignore_exceptions >"${SDIR}/mlist"
130fi
131}
132
133generate_mlist_missing()
134{
135	${COMM} -23 "${SDIR}/files" "${SDIR}/mlist" > "${SDIR}/missing"
136}
137
138generate_mlist_extra()
139{
140	${COMM} -13 "${SDIR}/files" "${SDIR}/mlist" > "${SDIR}/extra"
141}
142
143generate_files_missing()
144{
145	${COMM} -23 "${SDIR}/flist" "${SDIR}/files" > "${SDIR}/missing"
146}
147
148generate_files_extra()
149{
150	${COMM} -13 "${SDIR}/flist" "${SDIR}/files" > "${SDIR}/extra"
151}
152
153exist_case_insensitive()
154{
155	while read f; do
156		[ -f "${DESTDIR}/${f}" ] || \
157		[ -d "${DESTDIR}/${f}" ] || \
158		[ -L "${DESTDIR}/${f}" ] || \
159		echo "$f"
160	done
161}
162
163#
164# compare DESTDIR with METALOG, and report on differences.
165#
166compare_metalog()
167{
168    # Handle case insensitive filesystems
169    mv -f "${SDIR}/extra" "${SDIR}/extra.all"
170    exist_case_insensitive < "${SDIR}/extra.all" > "${SDIR}/extra"
171
172    check_metalog_extra
173    check_metalog_missing
174}
175
176check_metalog_extra()
177{
178    if [ -s "${SDIR}/extra" ]; then
179	count="$(${AWK} 'END {print NR}' "${SDIR}/extra")"
180	echo ""
181	echo "=======  ${count} extra files in METALOG  ========="
182	echo "Files in METALOG but missing from DESTDIR."
183	echo "File was deleted after installation ?"
184	echo "------------------------------------------"
185	cat "${SDIR}/extra"
186	echo "=========  end of ${count} extra files  ==========="
187	echo ""
188	es=1 # this is fatal even if ${allowextra} is true
189    fi
190}
191
192check_metalog_missing()
193{
194    if [ -s "${SDIR}/missing" ]; then
195	count="$(${AWK} 'END {print NR}' "${SDIR}/missing")"
196	echo ""
197	echo "======  ${count} missing files in METALOG  ========"
198	echo "Files in DESTDIR but missing from METALOG."
199	echo "File installed but not registered in METALOG ?"
200	echo "------------------------------------------"
201	cat "${SDIR}/missing"
202	echo "========  end of ${count} missing files  =========="
203	echo ""
204	es=1 # this is fatal even if ${allowmissing} is true
205    fi
206}
207
208#
209# compare flist with DESTDIR, and report on differences.
210#
211compare_destdir()
212{
213# Handle case insensitive filesystems
214mv -f "${SDIR}/missing" "${SDIR}/missing.all"
215exist_case_insensitive < "${SDIR}/missing.all" > "${SDIR}/missing"
216
217check_destdir_extra
218check_destdir_missing
219}
220
221check_destdir_extra()
222{
223if [ -s "${SDIR}/extra" ]; then
224	count="$(${AWK} 'END {print NR}' "${SDIR}/extra")"
225	echo ""
226	echo "=======  ${count} extra files in DESTDIR  ========="
227	echo "Files in DESTDIR but missing from flist."
228	echo "File is obsolete or flist is out of date ?"
229	if ${allowextra}; then
230		echo "This is non-fatal, due to '-e' option."
231	else
232		es=1
233	fi
234	echo "------------------------------------------"
235	cat "${SDIR}/extra"
236	echo "=========  end of ${count} extra files  ==========="
237	echo ""
238fi
239}
240
241check_destdir_missing()
242{
243if [ -s "${SDIR}/missing" ]; then
244	count="$(${AWK} 'END {print NR}' "${SDIR}/missing")"
245	echo ""
246	echo "======  ${count} missing files in DESTDIR  ========"
247	echo "Files in flist but missing from DESTDIR."
248	echo "File wasn't installed ?"
249	if ${allowmissing}; then
250		echo "This is non-fatal, due to '-m' option."
251	else
252		es=1
253	fi
254	echo "------------------------------------------"
255	cat "${SDIR}/missing"
256	echo "========  end of ${count} missing files  =========="
257	echo ""
258fi
259}
260
261generate_files
262generate_flist
263generate_mlist
264
265if false && [ -n "${metalog}" ]; then
266	# XXX: Temporarily disabled due to problems with obsolete files in metalog
267	generate_mlist_missing
268	generate_mlist_extra
269	compare_metalog
270else
271	generate_files_missing
272	generate_files_extra
273	compare_destdir
274fi
275
276exit 0		# cleanup will exit with ${es}
277