summaryrefslogtreecommitdiffstats
path: root/tools/findleaves.sh
blob: e7900f71a675c978c7c32d064013b6acf666a09a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#
# Copyright (C) 2008 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Finds files with the specified name under a particular directory, stopping
# the search in a given subdirectory when the file is found.
#

set -o nounset  # fail when dereferencing unset variables
set -o errexit  # fail if any subcommand fails

progName=`basename $0`

function warn() {
    echo "$progName: $@" >&2
}

function trace() {
    echo "$progName: $@"
}

function usage() {
    if [[ $# > 0 ]]
    then
        warn $@
    fi
    cat <<-EOF
Usage: $progName [<options>] <dirlist> <filename>
Options:
       --mindepth=<mindepth>
       --maxdepth=<maxdepth>
       Both behave in the same way as their find(1) equivalents.
       --prune=<glob>
       Avoids returning results from any path matching the given glob-style
       pattern (e.g., "*/out/*"). May be used multiple times.
EOF
    exit 1
}

function fail() {
    warn $@
    exit 1
}

if [ $# -lt 2 ]
then
    usage
fi

findargs=""
while [[ "${1:0:2}" == "--" ]]
do
    arg=${1:2}
    name=${arg%%=*}
    value=${arg##*=}
    if [[ "$name" == "mindepth" || "$name" == "maxdepth" ]]
    then
        # Add to beginning of findargs; these must come before the expression.
        findargs="-$name $value $findargs"
    elif [[ "$name" == "prune" ]]
    then
        # Add to end of findargs; these are part of the expression.
        findargs="$findargs -path $value -prune -or"
    fi
    shift
done

nargs=$#
# The filename is the last argument
filename="${!nargs}"

# Print out all files that match, as long as the path isn't explicitly
# pruned. This will print out extraneous results from directories whose
# parents have a match. These are filtered out by the awk script below.
find "${@:0:$nargs}" $findargs -type f -name "$filename" -print |

# Only pass along the directory of each match.
sed -e 's/\/[^\/]*$/\//' |

# Sort the output, so directories appear immediately before their contents.
# If there are any duplicates, the awk script will implicitly ignore them.
sort |

# Always print the first line, which can't possibly be covered by a
# parent directory match. After that, only print lines where the last
# line printed isn't a prefix.
awk -v "filename=$filename" '
    (NR == 1) || (index($0, last) != 1) {
        last = $0;
        printf("%s%s\n", $0, filename);
    }
'