list_image.sh 1.7 KB

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