Refine art/build/apex/runtests.sh's pretty printing options.

- Rename option `-l, --list-files` as `-t, --print-tree` to better
  reflect its action.
- Make option `-t, --print-tree` also display file sizes.
- Only check for the presence of `tree` if option `-t, --print-tree`
  is used.
- Introduce a new option `-l, --list-files`, using `find` to display
  the image's contents.
- Refactor this logic into a function (`maybe_list_apex_contents`),
  so that it can be reused for all APEX packages.

Test: art/build/apex/runtests.sh -t
Test: art/build/apex/runtests.sh -l
Bug: 113373927
Change-Id: I31fefe0161dc4e4d166baa76bb5e6611bf5940dc
diff --git a/build/apex/runtests.sh b/build/apex/runtests.sh
index 924c44b..b5e8d8b 100755
--- a/build/apex/runtests.sh
+++ b/build/apex/runtests.sh
@@ -33,11 +33,7 @@
 
    sudo apt-get install libguestfs-tools
 "
-which tree > /dev/null || die "This script requires the 'tree' tool.
-On Debian-based systems, this can be installed with:
 
-   sudo apt-get install tree
-"
 [[ -n "$ANDROID_PRODUCT_OUT" ]] \
   || die "You need to source and lunch before you can use this script."
 
@@ -46,6 +42,7 @@
 
 build_apex_p=true
 list_image_files_p=false
+print_image_tree_p=false
 
 function usage {
   cat <<EOF
@@ -53,7 +50,8 @@
 Build (optional) and run tests on Android Runtime APEX package (on host).
 
   -s, --skip-build    skip the build step
-  -l, --list-files    list the contents of the ext4 image
+  -l, --list-files    list the contents of the ext4 image using `find`
+  -t, --print-tree    list the contents of the ext4 image using `tree`
   -h, --help          display this help and exit
 
 EOF
@@ -64,6 +62,7 @@
   case "$1" in
     (-s|--skip-build) build_apex_p=false;;
     (-l|--list-files) list_image_files_p=true;;
+    (-t|--print-tree) print_image_tree_p=true;;
     (-h|--help) usage;;
     (*) die "Unknown option: '$1'
 Try '$0 --help' for more information.";;
@@ -71,6 +70,14 @@
   shift
 done
 
+if $print_image_tree_p; then
+  which tree >/dev/null || die "This script requires the 'tree' tool.
+On Debian-based systems, this can be installed with:
+
+   sudo apt-get install tree
+"
+fi
+
 
 # build_apex APEX_MODULE
 # ----------------------
@@ -82,6 +89,24 @@
   fi
 }
 
+# maybe_list_apex_contents MOUNT_POINT
+# ------------------------------------
+# If any listing/printing option was used, honor them and display the contents
+# of the APEX payload at MOUNT_POINT.
+function maybe_list_apex_contents {
+  local mount_point=$1
+
+  # List the contents of the mounted image using `find` (optional).
+  if $list_image_files_p; then
+    say "Listing image files" && find "$mount_point"
+  fi
+
+  # List the contents of the mounted image using `tree` (optional).
+  if $print_image_tree_p; then
+    say "Printing image tree" && ls -ld "$mount_point" && tree -aph --du "$mount_point"
+  fi
+}
+
 function check_binary {
   [[ -x "$mount_point/bin/$1" ]] || die "Cannot find binary '$1' in mounted image"
 }
@@ -218,10 +243,6 @@
 
   # Mount the image from the Android Runtime APEX.
   guestmount -a "$image_file" -m "$partition" "$mount_point"
-
-  # List the contents of the mounted image (optional).
-  $list_image_files_p \
-    && say "Listing image files" && ls -ld "$mount_point" && tree -ap "$mount_point"
 }
 
 # Testing release APEX package (com.android.runtime.release).
@@ -229,6 +250,8 @@
 
 apex_module="com.android.runtime.release"
 
+say "Processing APEX package $apex_module"
+
 work_dir=$(mktemp -d)
 mount_point="$work_dir/image"
 
@@ -240,6 +263,9 @@
 # Set up APEX package.
 setup_target_apex "$apex_module" "$mount_point"
 
+# List the contents of the APEX image (optional).
+maybe_list_apex_contents "$mount_point"
+
 # Run tests on APEX package.
 say "Checking APEX package $apex_module"
 check_release_contents
@@ -249,12 +275,15 @@
 cleanup_target
 
 say "$apex_module tests passed"
+echo
 
 # Testing debug APEX package (com.android.runtime.debug).
 # -------------------------------------------------------
 
 apex_module="com.android.runtime.debug"
 
+say "Processing APEX package $apex_module"
+
 work_dir=$(mktemp -d)
 mount_point="$work_dir/image"
 
@@ -266,6 +295,9 @@
 # Set up APEX package.
 setup_target_apex "$apex_module" "$mount_point"
 
+# List the contents of the APEX image (optional).
+maybe_list_apex_contents "$mount_point"
+
 # Run tests on APEX package.
 say "Checking APEX package $apex_module"
 check_release_contents
@@ -279,6 +311,7 @@
 cleanup_target
 
 say "$apex_module tests passed"
+echo
 
 
 # Testing host APEX package (com.android.runtime.host).
@@ -319,6 +352,8 @@
 
 apex_module="com.android.runtime.host"
 
+say "Processing APEX package $apex_module"
+
 work_dir=$(mktemp -d)
 mount_point="$work_dir/zip"
 
@@ -330,6 +365,9 @@
 # Set up APEX package.
 setup_host_apex "$apex_module" "$mount_point"
 
+# List the contents of the APEX image (optional).
+maybe_list_apex_contents "$mount_point"
+
 # Run tests on APEX package.
 say "Checking APEX package $apex_module"
 check_release_contents