dtbtool: fix for filesystems that don't return d_type from readdir

readdir() doesn't return d_type for all filesystems, in particular XFS
doesn't fill in this field. fix by using stat() instead of d_type to
determine if a dtb is a regular file. code is also rearranged slightly
to avoid doing a redundant stat().

Change-Id: I26b402f517cf8ecefa78d8ae45e51219b333b682
diff --git a/qcom/dtbtool/dtbtool.c b/qcom/dtbtool/dtbtool.c
index 77d7aef..cbe8d06 100644
--- a/qcom/dtbtool/dtbtool.c
+++ b/qcom/dtbtool/dtbtool.c
@@ -635,16 +635,6 @@
        extract "qcom,msm-id" parameter
      */
     while ((dp = readdir(dir)) != NULL) {
-        if ((dp->d_type != DT_REG)) {
-            continue;
-        }
-
-        flen = strlen(dp->d_name);
-        if ((flen <= 4) ||
-            (strncmp(&dp->d_name[flen-4], ".dtb", 4) != 0)) {
-            continue;
-        }
-        log_info("Found file: %s ... ", dp->d_name);
 
         flen = strlen(input_dir) + strlen(dp->d_name) + 1;
         filename = (char *)malloc(flen);
@@ -656,6 +646,19 @@
         strncpy(filename, input_dir, flen);
         strncat(filename, dp->d_name, flen);
 
+        if (stat(filename, &st) != 0 || !S_ISREG(st.st_mode)) {
+            free(filename);
+            continue;
+        }
+
+        flen = strlen(dp->d_name);
+        if ((flen <= 4) || (strncmp(&dp->d_name[flen-4], ".dtb", 4) != 0)) {
+            free(filename);
+            continue;
+        }
+
+        log_info("Found file: %s ... ", dp->d_name);
+
         /* To identify the version number */
         msmversion = force_v2 ? GetVersionInfo(filename) : 1;
 
@@ -679,8 +682,7 @@
             }
         }
 
-        if ((stat(filename, &st) != 0) ||
-            (st.st_size == 0)) {
+        if (st.st_size == 0) {
             log_err("skip, failed to get DTB size\n");
             free(filename);
             continue;