3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 ## Loads of minor tweaks --SY.
10 # The following list should contain the mount points of all
11 # filesystems that are to be scanned as a space-separated list within
12 # parentheses. / will usually be in this list and if you have /usr on
13 # a separate partition, it will also be in this list. Other
14 # non-special filesystems where package users could own files should
15 # also be put in this list. Mount points whose filesystems are
16 # special, such as procfs or sysfs must not be in this list. While a
17 # simple find on those special filesystems should be harmless,
18 # operations such as "-exec grep something" are NOT SAFE and may have
19 # HARMFUL SIDE-EFFECTS, especially when performed as root.
22 # fs_to_scan=(/ /opt /usr /var)
26 # Files with a path prefix found in the following list are ignored. This
27 # list will usually contain the parent directory of your package users'
28 # home directories, because normally you don't want to scan those. You
29 # can also add other directories that will never contain package user
30 # files, such as /home. This reduces scan time.
32 # Directories that are on separate filesystems (separate from the ones
33 # listed in fs_to_scan above) don't need to be listed here because of
34 # the -xdev option used in the find command.
38 # /{,*/{,*/}}lost+found \
41 # /opt/sql-ledger/{spool,templates,users,css} \
42 # /etc/apache/ssl.key \
43 # /etc/audisp/plugins.d \
46 # /etc/polkit-1/rules.d \
50 # /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
53 # /usr/share/polkit-1/rules.d \
55 # /var/{cache,chroot,db,run,snmp,spool} \
56 # /var/lib/{sshd,nfs,spamassassin,pulse,machines}) #NO TRAILING SLASHES!!!!
62 /usr/src) #NO TRAILING SLASHES!!!!
65 # If you are scanning MS-DOS, CD-ROM, or AFS volumes you need
66 # to set $NOLEAF here to '-noleaf'. Setting this does significantly
67 # slow down the search so only do so if you really need to.
72 if [ $# -lt 1 -o "$1" = "--help" ]; then
74 echo 1>&2 'USAGE: '"${0##*/}"' <user_or_group_name> [<find-commands>]'
76 echo 1>&2 ' If <find-commands> contains no action other than -prune, -print will be'
77 echo 1>&2 ' executed for all matching files.'
78 echo 1>&2 ' Entries will be matched if group and/or user equals <user_or_group_name>'
79 echo 1>&2 ' (numeric UID/GID allowed).'
80 echo 1>&2 ' All matching entries will be acted on, including device special files, so'
81 echo 1>&2 ' you should be extra careful with the <find-commands> you provide!'
86 # suppress ugly debug output from shell
90 shift 1 # remove user_or_group_name from argument list
93 # test if find accepts ugname as a user, and append to ugmatcher if it does
94 if find / -maxdepth 0 -user "$ugname" >/dev/null 2>&1 ; then
95 ugmatcher[${#ugmatcher[@]}]="-or"
96 ugmatcher[${#ugmatcher[@]}]="-user"
97 ugmatcher[${#ugmatcher[@]}]="$ugname"
99 # test if find accepts ugname as a group, and append to ugmatcher if it does
100 if find / -maxdepth 0 -group "$ugname" >/dev/null 2>&1 ; then
101 ugmatcher[${#ugmatcher[@]}]="-or"
102 ugmatcher[${#ugmatcher[@]}]="-group"
103 ugmatcher[${#ugmatcher[@]}]="$ugname"
106 # if find accepted ugname as neither user nor group, then exit
107 if [ "${#ugmatcher[@]}" = 1 ]; then
108 echo 1>&2 'find does not accept `'"$ugname'"' as group or user name'
112 # construct find commands that match the prune_prefixes. Each prefix will be
113 # matched as -path <prefix> -or -path <prefix>/*
114 # so that the directory itself and all subdirectories are matched.
116 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
120 y[${#y[@]}]="${prune_prefixes[$i]}"
123 y[${#y[@]}]="${prune_prefixes[$i]}/*"
127 # The uninstall_package script sets this to `-ignore_readdir_race' so
128 # that find doesn't print errors when things it is searching for
130 # IGNORE_READDIR_RACE='-ignore_readdir_race'
133 # In the following find command, the part
134 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
135 # is responsible for preventing the files that match prune_prefixes from
136 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
137 # -prune has no effect and is always false when -depth is used.
138 # The -true before "$@" ensures that -depth can be passed as only parameter.
140 find "${fs_to_scan[@]}" -xdev $NOLEAF $IGNORE_READDIR_RACE \
141 -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
142 -and \( "${ugmatcher[@]}" \) -and \( -true "$@" \)