From d560aea660690598eebc7f77efee51efcc8ad2a6 Mon Sep 17 00:00:00 2001
From: Sergei Trofimov <sergei.trofimov@arm.com>
Date: Thu, 5 Oct 2017 09:35:11 +0100
Subject: [PATCH] Target: fix read_tree_values for empty dir

grep was existing with 1 when passed an empty directory, resulting in
TargetError being raised unless explicitly suppressed with
check_exit_code=False. This case is now fixed to correctly return an
empty dict without error.

read_tree_values bash function has also been optimized to not
unnecessarily run find in a subshell if the path passed to the function
does not exist.
---
 devlib/bin/scripts/shutils.in | 9 ++++++---
 devlib/target.py              | 2 ++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/devlib/bin/scripts/shutils.in b/devlib/bin/scripts/shutils.in
index a678a65..401e51e 100755
--- a/devlib/bin/scripts/shutils.in
+++ b/devlib/bin/scripts/shutils.in
@@ -216,10 +216,13 @@ read_tree_values() {
     PATH=$1
     MAXDEPTH=$2
 
+    if [ ! -e $PATH ]; then
+        echo "ERROR: $PATH does not exist"
+        exit 1
+    fi
+
     PATHS=$($BUSYBOX find $PATH -follow -maxdepth $MAXDEPTH)
-    if [ ${#PATHS[@]} -eq 0 ]; then
-        echo "ERROR: '$1' does not exist"
-    else
+    if [ ${#PATHS[@]} -gt 1 ]; then
         $BUSYBOX grep -s '' $PATHS
     fi
 }
diff --git a/devlib/target.py b/devlib/target.py
index 8609fec..ec24765 100644
--- a/devlib/target.py
+++ b/devlib/target.py
@@ -620,6 +620,8 @@ class Target(object):
                                     check_exit_code=check_exit_code)
         result = {}
         for entry in output.strip().split('\n'):
+            if not entry.strip():
+                continue
             path, value = entry.strip().split(':', 1)
             result[path] = value
         return result