Lots of juicy updates
[pkgusr] / usr / bin / grep_all_regular_files_for
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 #  Many updates/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. Mount points
14 # whose filesystems are special, such as procfs or sysfs must not be
15 # in this list. While a simple find on those special filesystems
16 # should be harmless, operations such as "-exec grep something" are
17 # NOT SAFE and may have HARMFUL SIDE-EFFECTS, especially when
18 # performed as root.
19
20 ## Bastard settings
21 # fs_to_scan=(/ /opt /usr /var)
22
23 fs_to_scan=(/)
24
25 ## NOTE: if any of the directories listed in fs_to_scan contain
26 ## non-UNIX filesystems (MS-DOS, CD-ROM etc) you need to set $NOLEAF
27 ## here to `-noleaf'.  But only do so if you really need to as it
28 ## comes with a significant slow down on the find.
29 # NOLEAF='-noleaf'
30 NOLEAF=
31
32 # Files with a path prefix found in the following list are ignored. As
33 # the main function of this script is to help you find files that
34 # contain hardwired paths to /tools or other unwanted references to
35 # your build system, you will usually prune any directories that don't
36 # contain files of interest, such as /tools (whose files naturally
37 # refer to /tools) and your package users' home directories (which may
38 # also test positive if you have unpacked and configured sources lying
39 # around).
40 #
41 # NOTE: If a directory you want to prune is on a separate filesystem
42 # (separate from those listed in fs_to_scan) you don't need to list it
43 # here because of the -xdev option used in the find command.
44
45 ## Bastard settings
46 ## My /usr/src is NOT included here because it is on a separate
47 ## filesystem
48 # prune_prefixes=(\
49 #     /{,*/{,*/}}lost+found \
50 #     /etc/apache/ssl.key \
51 #     /etc/audisp/plugins.d \
52 #     /etc/cups/ssl \
53 #     /etc/firewall \
54 #     /etc/mail/{auth,spamassassin} \
55 #     /etc/pam.d \
56 #     /etc/polkit-1/rules.d \
57 #     /etc/skel \
58 #     /etc/ssl/private \
59 #     /etc/sudoers.d \
60 #     /opt/pgsql/data \
61 #     /opt/sql-ledger/{spool,templates,users,css} \
62 #     /root \
63 #     /usr/lib/deprecated \
64 #     /usr/lib/pkgusr \
65 #     /usr/share/polkit-1/rules.d \
66 #     /var/{cache,chroot,db,run,snmp,spool} \
67 #     /var/lib/{bluetooth,xdm} \
68 #     /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
69 #     /var/lib/{sshd,nfs,spamassassin,pulse,machines} \
70 #     /var/lib/colord/.cache \
71 #     /var/log \
72 #     /var/tmp) #NO TRAILING SLASHES!!!!
73
74 prune_prefixes=(\
75     /home \
76     /usr/lib/pkgusr \
77     /usr/src \
78     /dev \
79     /tools) #NO TRAILING SLASHES!!!
80
81 if [ $# -lt 1 -o "$1" = "--help" ]; then
82     echo 1>&2 
83     echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
84     echo 1>&2 
85     echo 1>&2 '  grep -l <grep-commands> -- <file>'
86     echo 1>&2 '  will be executed for each *regular file* <file>'
87     echo 1>&2 '  ATTENTION! If you override the -l switch with a switch that makes grep'
88     echo 1>&2 '  output all individual matches rather than just the matching files,'
89     echo 1>&2 '  then DO NOT redirect output to a file that is in a directory that will be'
90     echo 1>&2 '  scanned, or you risk creating an endless loop that will cause your'
91     echo 1>&2 '  output file to grow till your disk is full.'
92     echo 1>&2 
93     exit 1
94 fi
95
96 # suppress ugly debug output from shell
97 trap ':' SIGPIPE
98
99 # construct find commands that match the prune_prefixes. Each prefix will be
100 # matched as -path <prefix> -or -path <prefix>/*
101 # so that the directory itself and all subdirectories are matched.
102 y=(\( -false)
103 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) 
104 do
105     y[${#y[@]}]='-or'
106     y[${#y[@]}]=-path
107     y[${#y[@]}]="${prune_prefixes[$i]}"
108     y[${#y[@]}]='-or'
109     y[${#y[@]}]=-path
110     y[${#y[@]}]="${prune_prefixes[$i]}/*"
111 done
112 y[${#y[@]}]=')'
113
114 cmd_pre=(-type f -exec grep -l)
115 cmd_post=(-- {} \;)
116
117 ## Let root override the prune_prefixes
118 if [ $(id -u) -eq 0 ]; then
119     prune_prefixes=(/home /root /tools)
120 fi
121
122 # In the following find command, the part
123 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
124 # is responsible for preventing the files that match prune_prefixes from
125 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
126 # -prune has no effect and is always false when -depth is used (which someone
127 # might do in the future).
128 # The -true before "$@" ensures that -depth can be passed as 1st parameter
129 # of $cmd_pre (should someone change it in the future).
130 find "${fs_to_scan[@]}" -xdev $NOLEAF \
131     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
132     -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)