7d599e25fbbd88ecb98eaa4fab544b02115764b6
[pkgusr] / usr / bin / forall_direntries_from
1 #!/bin/bash
2 ## Originally...
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!
6
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 ## Loads of minor tweaks --SY.
9
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.
20
21 ## Bastard settings
22 # fs_to_scan=(/ /opt /usr /var)
23
24 fs_to_scan=(/)
25
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.
31
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.
35
36 ## Bastard settings
37 # prune_prefixes=(\
38 #       /{,*/{,*/}}lost+found \
39 #       /root \
40 #       /opt/pgsql/data \
41 #       /opt/sql-ledger/{spool,templates,users,css} \
42 #       /etc/apache/ssl.key \
43 #       /etc/audisp/plugins.d \
44 #       /etc/cups/ssl \
45 #       /etc/firewall \
46 #       /etc/polkit-1/rules.d \
47 #       /etc/skel \
48 #       /etc/ssl/private \
49 #       /etc/sudoers.d \
50 #       /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
51 #       /var/log \
52 #       /usr/lib/pkgusr \
53 #       /usr/share/polkit-1/rules.d \
54 #       /var/tmp \
55 #       /var/{cache,chroot,db,run,snmp,spool} \
56 #       /var/lib/{sshd,nfs,spamassassin,pulse}) #NO TRAILING SLASHES!!!!
57
58 prune_prefixes=(\
59         /home \
60         /root \
61         /usr/lib/pkgusr \
62         /usr/src) #NO TRAILING SLASHES!!!!
63
64 ## NOTE: 
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.
68
69 # NOLEAF='-noleaf'
70 NOLEAF=
71
72 if [ $# -lt 1 -o "$1" = "--help" ]; then
73     echo 1>&2
74     echo 1>&2 'USAGE: '"${0##*/}"' <user_or_group_name> [<find-commands>]'
75     echo 1>&2
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!'
82     echo 1>&2
83     exit 1
84 fi
85
86 # suppress ugly debug output from shell
87 trap ':' SIGPIPE
88
89 ugname="$1"
90 shift 1  # remove user_or_group_name from argument list
91
92 ugmatcher=(-false)
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"
98 fi
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"
104 fi
105
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'
109     exit 1
110 fi
111
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.
115 y=(\( -false)
116 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1))
117 do
118     y[${#y[@]}]='-or'
119     y[${#y[@]}]=-path
120     y[${#y[@]}]="${prune_prefixes[$i]}"
121     y[${#y[@]}]='-or'
122     y[${#y[@]}]=-path
123     y[${#y[@]}]="${prune_prefixes[$i]}/*"
124 done
125 y[${#y[@]}]=')'
126
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
129 # disappear.
130 # IGNORE_READDIR_RACE='-ignore_readdir_race'
131 IGNORE_READDIR_RACE=
132
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.
139
140 find "${fs_to_scan[@]}" -xdev $NOLEAF $IGNORE_READDIR_RACE \
141     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
142     -and \( "${ugmatcher[@]}" \) -and \( -true "$@" \)