1 # $NetBSD: dirs,v 1.3 2006/12/15 12:46:24 martti Exp $
3 # Generate a +DIRS script that reference counts directories that are
4 # required for the proper functioning of the package.
8 ${CAT} > ./+DIRS << 'EOF'
11 # +DIRS - reference-counted directory management script
13 # Usage: ./+DIRS ADD|REMOVE|PERMS [metadatadir]
14 # ./+DIRS CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
16 # This script supports two actions, ADD and REMOVE, that will add or
17 # remove the directories needed by the package associated with
18 # <metadatadir>. The CHECK-ADD action will check whether any directories
19 # needed by the package are missing, and print an informative message
20 # noting those directories. The CHECK-REMOVE action will check whether
21 # any directories needed by the package still exist, and print an
22 # informative message noting those directories. The CHECK-ADD and
23 # CHECK-REMOVE actions return non-zero if they detect either missing
24 # or existing directories, respectively. The PERMS action will correct
25 # any ownership or permission discrepancies between the existing
26 # directories and the data in this script, and the CHECK-PERMS action
27 # will check whether any directories have the wrong ownership or
28 # permission and print an informative message noting those directories.
29 # The CHECK-PERMS action will return non-zero if it detects directories
30 # with wrong ownership or permissions.
32 # Lines starting with "# DIR: " are data read by this script that
33 # name the directories that this package requires to exist to function
37 # # DIR: /var/log/foo/tmp mo 0700 foo-user foo-group
38 # # DIR: share/foo-plugins fm
40 # For each DIR entry, if the directory path is relative, then it is taken
41 # to be relative to ${PKG_PREFIX}.
43 # The second field in each DIR entry is a set of flags with the following
46 # f ignore ${PKG_CONFIG}
47 # m create (make) the directory when ADDing
48 # o directory is owned by the package
72 PKG_METADATA_DIR="${2-${CURDIR}}"
73 : ${PKGNAME=${PKG_METADATA_DIR##*/}}
74 : ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
75 : ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
76 : ${PKG_PREFIX=@PREFIX@}
78 PKG_REFCOUNT_DIRS_DBDIR="${PKG_REFCOUNT_DBDIR}/dirs"
80 case "${PKG_CONFIG:-@PKG_CONFIG@}" in
81 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
84 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
88 case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
89 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
92 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
100 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
101 while read dir d_flags d_mode d_user d_group; do
104 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
110 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
111 perms="$shadow_dir/+PERMISSIONS"
112 preexist="$shadow_dir/+PREEXISTING"
113 token="$shadow_dir/${PKGNAME}"
114 if ${TEST} ! -d "$shadow_dir"; then
116 ${TEST} ! -d "$dir" ||
117 ${ECHO} "${PKGNAME}" > $preexist
119 if ${TEST} -f "$token" && \
120 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
123 ${ECHO} "${PKG_METADATA_DIR}" >> $token
125 case $d_mode$d_user$d_group in
127 *) ${ECHO} "$d_user $d_group $d_mode" > $perms ;;
129 case $d_flags:$_PKG_CONFIG in
134 *) ${CHOWN} $d_user $dir ;;
138 *) ${CHGRP} $d_group $dir ;;
142 *) ${CHMOD} $d_mode $dir ;;
150 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
151 while read dir d_flags d_mode d_user d_group; do
154 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
160 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
161 perms="$shadow_dir/+PERMISSIONS"
162 preexist="$shadow_dir/+PREEXISTING"
163 token="$shadow_dir/${PKGNAME}"
164 tokentmp="$token.tmp.$$"
165 if ${TEST} -f "$token" && \
166 ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
167 ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
168 case `${CAT} $tokentmp | ${SED} -n "$="` in
170 ${TEST} -f "$preexist" ||
171 { case $d_flags:$_PKG_CONFIG in
173 ${RMDIR} -p $dir 2>/dev/null || ${TRUE};
176 ${RM} -f $perms $preexist $token $token.tmp.*
177 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
180 ${MV} -f $tokentmp $token
188 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
189 while read dir d_flags d_mode d_user d_group; do
190 case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
196 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
200 *) ${CHOWN} $d_user $dir ;;
204 *) ${CHGRP} $d_group $dir ;;
208 *) ${CHMOD} $d_mode $dir ;;
214 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
215 { while read dir d_flags d_mode d_user d_group; do
218 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
220 ${TEST} ! -d "$dir" || continue
225 case "$printed_header" in
227 *) printed_header=yes
228 ${ECHO} "==========================================================================="
229 ${ECHO} "The following directories should be created for ${PKGNAME}:"
233 case $d_user:$d_group:$d_mode in
235 ${ECHO} " $dir (m=$d_mode, o=$d_user, g=$d_group)"
242 case "$printed_header" in
244 ${ECHO} "==========================================================================="
248 ${TEST} $? -eq 0 || exitcode=1
252 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
253 { while read dir d_flags d_mode d_user d_group; do
256 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
258 ${TEST} -d "$dir" || continue
263 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
264 ${TEST} ! -d "$shadow_dir" || continue # refcount isn't zero
265 case "$printed_header" in
267 *) printed_header=yes
268 ${ECHO} "==========================================================================="
269 ${ECHO} "The following directories are no longer being used by ${PKGNAME},"
270 ${ECHO} "and they can be removed if no other packages are using them:"
276 case "$printed_header" in
278 ${ECHO} "==========================================================================="
282 ${TEST} $? -eq 0 || exitcode=1
286 tmpdir="./.pkginstall.$$"
287 ${MKDIR} -p $tmpdir 2>/dev/null || exit 1
288 ${CHMOD} 0700 $tmpdir
289 ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
290 { while read dir d_flags d_mode d_user d_group; do
293 [!/]*) dir="${PKG_PREFIX}/$dir" ;;
295 ${TEST} -d "$dir" || continue
296 case $d_user:$d_group:$d_mode in
300 perms=`${LS} -ld $dir | ${AWK} '{ print $1":"$3":"$4 }'`
301 testpath="$tmpdir/dir_perms"
302 ${MKDIR} -p $testpath
303 ${CHMOD} $d_mode $testpath 2>/dev/null
304 longmode=`${LS} -ld $testpath | ${AWK} '{ print $1 }'`
305 case $d_mode:$d_user:$d_group in
308 *:$d_user:*) continue ;;
313 *:$d_user:$d_group) continue ;;
318 $longmode:*:*) continue ;;
323 $longmode:$d_user:*) continue ;;
328 $longmode:$d_user:$d_group) continue ;;
333 case "$printed_header" in
335 *) printed_header=yes
336 ${ECHO} "==========================================================================="
337 ${ECHO} "The following directories are used by ${PKGNAME} and"
338 ${ECHO} "have the wrong ownership and/or permissions:"
342 case $d_mode:$d_user:$d_group in
344 ${ECHO} " $dir (m=$d_mode)"
347 ${ECHO} " $dir (m=$d_mode, o=$d_user)"
350 ${ECHO} " $dir (m=$d_mode, o=$d_user, g=$d_group)"
354 case "$printed_header" in
356 ${ECHO} "==========================================================================="
360 ${TEST} $? -eq 0 || exitcode=1
365 ${ECHO} "Usage: ./+DIRS ADD|REMOVE|PERMS [metadatadir]"
366 ${ECHO} " ./+DIRS CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]"
372 ${SED} -n "/^\# DIR: /p" ${SELF} >> ./+DIRS