1#! /bin/bash 2 3# Recursively list Android image directory. 4set -eu 5set -o pipefail 6 7function die() { format=$1; shift; printf "$format\n" "$@"; exit 1; } 8 9# Figure out the filer utility. 10declare filer= 11[[ -z "${ANDROID_HOST_OUT:-}" ]] || filer=${ANDROID_HOST_OUT}/bin/debugfs_static 12if [[ "${1:-}" =~ --debugfs_path=(.*) ]]; then 13 filer=${BASH_REMATCH[1]} 14 shift 15fi 16if [[ -z "${filer:-}" ]]; then 17 maybefiler="$(dirname $0)/debugfs_static" 18 [[ ! -x "$maybefiler" ]] || filer="$maybefiler" 19fi 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, \ 25and 'debugfs_static' is not colocated with this script" 26declare -r image="$1" 27 28function 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 '^/.*/.*/.*/.*/.+/.*/$' 51dolevel . 52