c5e9c7122bb5de5994539edfd6b791741861c99c
[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 # prune_prefixes=(\
47 #       /{,*/{,*/}}lost+found \
48 #       /root \
49 #       /opt/pgsql/data \
50 #       /opt/sql-ledger/{spool,templates,users,css} \
51 #       /etc/apache/ssl.key \
52 #       /etc/audisp/plugins.d \
53 #       /etc/cups/ssl \
54 #       /etc/firewall \
55 #       /etc/polkit-1/rules.d \
56 #       /etc/skel \
57 #       /etc/ssl/private \
58 #       /etc/sudoers.d \
59 #       /var/lib/{sasl,sudo,net-snmp,udisks{,2},NetworkManager} \
60 #       /var/log \
61 #       /usr/lib/pkgusr \
62 #       /usr/share/polkit-1/rules.d \
63 #       /var/tmp \
64 #       /var/{cache,chroot,run,snmp,spool} \
65 #       /var/lib/{sshd,nfs,spamassassin,pulse}) #NO TRAILING SLASHES!!!!
66
67 prune_prefixes=(\
68         /home \
69         /usr/lib/pkgusr \
70         /usr/src \
71         /dev \
72         /tools) #NO TRAILING SLASHES!!!
73
74 if [ $# -lt 1 -o "$1" = "--help" ]; then
75     echo 1>&2 
76     echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
77     echo 1>&2 
78     echo 1>&2 '  grep -l <grep-commands> -- <file>'
79     echo 1>&2 '  will be executed for each *regular file* <file>'
80     echo 1>&2 '  ATTENTION! If you override the -l switch with a switch that makes grep'
81     echo 1>&2 '  output all individual matches rather than just the matching files,'
82     echo 1>&2 '  then DO NOT redirect output to a file that is in a directory that will be'
83     echo 1>&2 '  scanned, or you risk creating an endless loop that will cause your'
84     echo 1>&2 '  output file to grow till your disk is full.'
85     echo 1>&2 
86     exit 1
87 fi
88
89 # suppress ugly debug output from shell
90 trap ':' SIGPIPE
91
92 # construct find commands that match the prune_prefixes. Each prefix will be
93 # matched as -path <prefix> -or -path <prefix>/*
94 # so that the directory itself and all subdirectories are matched.
95 y=(\( -false)
96 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) 
97 do
98     y[${#y[@]}]='-or'
99     y[${#y[@]}]=-path
100     y[${#y[@]}]="${prune_prefixes[$i]}"
101     y[${#y[@]}]='-or'
102     y[${#y[@]}]=-path
103     y[${#y[@]}]="${prune_prefixes[$i]}/*"
104 done
105 y[${#y[@]}]=')'
106
107 cmd_pre=(-type f -exec grep -l)
108 cmd_post=(-- {} \;)
109
110 # In the following find command, the part
111 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
112 # is responsible for preventing the files that match prune_prefixes from
113 # being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
114 # -prune has no effect and is always false when -depth is used (which someone
115 # might do in the future).
116 # The -true before "$@" ensures that -depth can be passed as 1st parameter
117 # of $cmd_pre (should someone change it in the future).
118 find "${fs_to_scan[@]}" -xdev $NOLEAF \
119     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
120     -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)