1 # $NetBSD: files,v 1.5 2008/01/04 21:50:27 heinz Exp $
3 # Generate a +FILES script that reference counts config files that are
4 # required for the proper functioning of the package.
8 ${CAT} > ./+FILES << 'EOF'
11 # +FILES - reference-counted configuration file management script
13 # Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]
14 # ./+FILES VIEW-REMOVE depotdir viewdir
15 # ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
17 # This script supports two actions, ADD and REMOVE, that will add or
18 # remove the configuration files needed by the package associated with
19 # <metadatadir>. The CHECK-ADD action will check whether any files
20 # needed by the package are missing, and print an informative message
21 # noting those files. The CHECK-REMOVE action will check whether
22 # any files needed by the package still exist, and print an informative
23 # message noting those files. The CHECK-ADD and CHECK-REMOVE actions
24 # return non-zero if they detect either missing or existing files,
25 # respectively. The VIEW-REMOVE action will remove from <viewdir> the
26 # links to the configuration files in <depotdir>. The PERMS action
27 # will correct any ownership or permission discrepancies between the
28 # existing files and the data in this script, and the CHECK-PERMS
29 # action will check whether any files have the wrong ownership or
30 # permission and print an informative message noting those files. The
31 # CHECK-PERMS action will return non-zero if it detects files with
32 # wrong ownership or permissions.
34 # Lines starting with "# FILE: " are data read by this script that
35 # name the files that this package requires to exist to function
36 # correctly, along with the locations of the example files, e.g.
38 # # FILE: /etc/bar.conf c /example/bar.conf
39 # # FILE: /etc/baz/conf c /example/baz.conf 0600 foo-user foo-group
41 # For each FILE entry, if the file path is relative, then it is taken to
42 # be relative to ${PKG_PREFIX}.
44 # The second field in each FILE entry is a set of flags with the following
47 # c file is copied into place
48 # f ignore ${PKG_CONFIG}
49 # r file is an rc.d script (consider ${PKG_RCD_SCRIPTS})
74 : ${PKG_PREFIX=@PREFIX@}
76 case "${PKG_CONFIG:-@PKG_CONFIG@}" in
77 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
80 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
84 case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
85 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
88 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
92 case "${PKG_RCD_SCRIPTS:-@PKG_RCD_SCRIPTS@}" in
93 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
96 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
105 ${TEST} -n "${DEPOTDIR}" -a -n "${VIEWDIR}" || exit 0
109 PKG_METADATA_DIR="${2-${CURDIR}}"
110 : ${PKGNAME=${PKG_METADATA_DIR##*/}}
111 : ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
112 : ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
113 PKG_REFCOUNT_FILES_DBDIR="${PKG_REFCOUNT_DBDIR}/files"
120 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -u |
121 while read file f_flags f_eg f_mode f_user f_group; do
124 [!/]*) file="${PKG_PREFIX}/$file" ;;
132 [!/]*) f_eg="${PKG_PREFIX}/$f_eg" ;;
135 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
136 perms="$shadow_dir/+PERMISSIONS"
137 preexist="$shadow_dir/+PREEXISTING"
138 token="$shadow_dir/${PKGNAME}"
139 if ${TEST} ! -d "$shadow_dir"; then
141 ${TEST} ! -f "$file" ||
142 ${ECHO} "${PKGNAME}" > $preexist
144 if ${TEST} -f "$token" && \
145 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
148 ${ECHO} "${PKG_METADATA_DIR}" >> $token
151 case $f_mode$f_user$f_group in
153 *) ${ECHO} "$f_mode $f_user $f_group" > $perms ;;
155 if ${TEST} ! -f "$file" -a ! -f "$f_eg" -a ! -c "$f_eg"; then
158 case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
159 *f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
160 if ${TEST} -f "$file"; then
161 ${ECHO} "${PKGNAME}: $file already exists"
162 elif ${TEST} -f "$f_eg" -o -c "$f_eg"; then
163 ${ECHO} "${PKGNAME}: copying $f_eg to $file"
167 *) ${CHOWN} $f_user $file ;;
171 *) ${CHGRP} $f_group $file ;;
175 *) ${CHMOD} $f_mode $file ;;
185 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
186 while read file f_flags f_eg f_mode f_user f_group; do
189 [!/]*) file="${PKG_PREFIX}/$file" ;;
197 [!/]*) f_eg="${PKG_PREFIX}/$f_eg" ;;
200 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
201 perms="$shadow_dir/+PERMISSIONS"
202 preexist="$shadow_dir/+PREEXISTING"
203 token="$shadow_dir/${PKGNAME}"
204 tokentmp="$token.tmp.$$"
205 if ${TEST} -f "$token" && \
206 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
207 ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
208 case `${CAT} $tokentmp | ${SED} -n "$="` in
210 if ${TEST} -f "$preexist"; then
212 elif ${TEST} -f "$file" -a \( -f "$f_eg" -o -c "$f_eg" \) && \
213 ${CMP} -s "$file" "$f_eg"; then
214 case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
215 *f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
220 ${RM} -f $perms $preexist $token $token.tmp.*
221 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
224 ${MV} -f $tokentmp $token
232 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
233 while read file f_flags f_eg f_mode f_user f_group; do
234 case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
240 [!/]*) file="${PKG_PREFIX}/$file" ;;
244 *) ${CHOWN} $f_user $file ;;
248 *) ${CHGRP} $f_group $file ;;
252 *) ${CHMOD} $f_mode $file ;;
258 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
259 while read file f_flags f_eg f_mode f_user f_group; do
261 ${DEPOTDIR}/*) link="${VIEWDIR}/${file#${DEPOTDIR}/}" ;;
262 [!/]*) link="${VIEWDIR}/$file" ;;
266 if ${TEST} -h "$link"; then
268 ${RMDIR} -p $dir 2>/dev/null || ${TRUE}
274 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
275 { while read file f_flags f_eg f_mode f_user f_group; do
278 [!/]*) file="${PKG_PREFIX}/$file" ;;
280 ${TEST} ! -f "$file" || continue
287 [!/]*) f_eg="${PKG_PREFIX}/$f_eg" ;;
290 case "$printed_header" in
292 *) printed_header=yes
293 ${ECHO} "==========================================================================="
294 ${ECHO} "The following files should be created for ${PKGNAME}:"
298 case $f_mode:$f_user:$f_group in
303 ${ECHO} " $file (m=$f_mode)"
306 ${ECHO} " $file (m=$f_mode, o=$f_user)"
309 ${ECHO} " $file (m=$f_mode, o=$f_user, g=$f_group)"
312 ${TEST} ! -f "$f_eg" || ${ECHO} " [$f_eg]"
314 case "$printed_header" in
316 ${ECHO} "==========================================================================="
320 ${TEST} $? -eq 0 || exitcode=1
324 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
325 { while read file f_flags f_eg f_mode f_user f_group; do
328 [!/]*) file="${PKG_PREFIX}/$file" ;;
330 ${TEST} -f "$file" || continue
331 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
332 ${TEST} ! -d "$shadow_dir" || continue # refcount isn't zero
333 case "$printed_header" in
335 *) printed_header=yes
336 ${ECHO} "==========================================================================="
337 ${ECHO} "The following files are no longer being used by ${PKGNAME},"
338 ${ECHO} "and they can be removed if no other packages are using them:"
344 case "$printed_header" in
346 ${ECHO} "==========================================================================="
350 ${TEST} $? -eq 0 || exitcode=1
354 tmpdir="./.pkginstall.$$"
355 ${MKDIR} -p $tmpdir 2>/dev/null || exit 1
356 ${CHMOD} 0700 $tmpdir
357 ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
358 { while read file f_flags f_eg f_mode f_user f_group; do
361 [!/]*) file="${PKG_PREFIX}/$file" ;;
363 ${TEST} -f "$file" || continue
364 case $f_mode:$f_user:$f_group in
368 perms=`${LS} -l $file | ${AWK} '{ print $1":"$3":"$4 }'`
369 testpath="$tmpdir/file_perms"
371 ${CHMOD} $f_mode $testpath 2>/dev/null
372 longmode=`${LS} -l $testpath | ${AWK} '{ print $1 }'`
373 case $f_mode:$f_user:$f_group in
376 *:$f_user:*) continue ;;
381 *:$f_user:$f_group) continue ;;
386 $longmode:*:*) continue ;;
391 $longmode:$f_user:*) continue ;;
396 $longmode:$f_user:$f_group) continue ;;
401 case "$printed_header" in
403 *) printed_header=yes
404 ${ECHO} "==========================================================================="
405 ${ECHO} "The following files are used by ${PKGNAME} and have"
406 ${ECHO} "the wrong ownership and/or permissions:"
410 case $f_mode:$f_user:$f_group in
412 ${ECHO} " $file (m=$f_mode)"
415 ${ECHO} " $file (m=$f_mode, o=$f_user)"
418 ${ECHO} " $file (m=$f_mode, o=$f_user, g=$f_group)"
422 case "$printed_header" in
424 ${ECHO} "==========================================================================="
428 ${TEST} $? -eq 0 || exitcode=1
433 ${ECHO} "Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]"
434 ${ECHO} " ./+FILES VIEW-REMOVE depotdir viewdir"
435 ${ECHO} " ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]"
441 ${SED} -n "/^\# FILE: /p" ${SELF} >> ./+FILES