| Sasha Smundak | fe11851 | 2022-03-30 20:33:09 -0700 | [diff] [blame] | 1 | #! /bin/bash | 
 | 2 |  | 
 | 3 | # Recursively list Android image directory. | 
 | 4 | set -eu | 
 | 5 | set -o pipefail | 
 | 6 |  | 
 | 7 | function die() { format=$1; shift; printf "$format\n" "$@"; exit 1; } | 
 | 8 |  | 
 | 9 | # Figure out the filer utility. | 
 | 10 | declare filer= | 
 | 11 | [[ -z "${ANDROID_HOST_OUT:-}" ]] || filer=${ANDROID_HOST_OUT}/bin/debugfs_static | 
 | 12 | if [[ "${1:-}" =~ --debugfs_path=(.*) ]]; then | 
 | 13 |   filer=${BASH_REMATCH[1]} | 
 | 14 |   shift | 
 | 15 | fi | 
 | 16 | if [[ -z "${filer:-}" ]]; then | 
 | 17 |   maybefiler="$(dirname $0)/debugfs_static" | 
 | 18 |   [[ ! -x "$maybefiler" ]] || filer="$maybefiler" | 
 | 19 | fi | 
 | 20 |  | 
 | 21 | (( $# >0 )) || die "%s [--debugfs_path=<path>] IMAGE" "$0" | 
 | 22 |  | 
 | 23 | [[ -n "${filer:-}" ]] || die "cannot locate 'debugfs' executable: \ | 
 | 24 | --debugfs_path= is missing, ANDROID_HOST_OUT is not set, \ | 
 | 25 | and 'debugfs_static' is not colocated with this script" | 
 | 26 | declare -r image="$1" | 
 | 27 |  | 
 | 28 | function dolevel() { | 
 | 29 |   printf "%s/\n" "$1" | 
 | 30 |   # Each line of the file output consists of 6 fields separated with '/'. | 
 | 31 |   # The second one contains the file's attributes, and the fifth its name. | 
 | 32 |   $filer -R "ls -l -p $1" "$image" 2>/dev/null |\ | 
 | 33 |     sed -nr 's|^/.*/(.*)/.*/.*/(.+)/.*/$|\2 \1|p' | LANG=C sort | \ | 
 | 34 |   while read name attr; do | 
 | 35 |     [[ "$name" != '.' && "$name" != '..' ]] || continue | 
 | 36 |     path="$1/$name" | 
 | 37 |     # If the second char of the attributes is '4', it is a directory. | 
 | 38 |     if [[ $attr =~ ^.4 ]]; then | 
 | 39 |       dolevel "$path" | 
 | 40 |     else | 
 | 41 |       printf "%s\n" "$path" | 
 | 42 |     fi | 
 | 43 |   done | 
 | 44 | } | 
 | 45 |  | 
 | 46 | # The filer always prints its version on stderr, so we are going | 
 | 47 | # to redirect it to the bit bucket. On the other hand, the filer's | 
 | 48 | # return code on error is still 0. Let's run it once to without | 
 | 49 | # redirecting stderr to see that there is at least one entry. | 
 | 50 | $filer -R "ls -l -p" "$image" | grep -q -m1 -P '^/.*/.*/.*/.*/.+/.*/$' | 
 | 51 | dolevel . |