From 72be0e39bbc2acf12f1379af569b98f9a5afc015 Mon Sep 17 00:00:00 2001 From: Wolfgang Wiedmeyer Date: Fri, 16 Jun 2017 23:35:23 +0200 Subject: Don't assume that d_type works when browsing directory For example, exfat always returns DT_UNKNOWN which makes it impossible to determine the file type using d_type. Signed-off-by: Wolfgang Wiedmeyer --- recovery.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 0b59d12..eed7d04 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -747,6 +747,8 @@ static char* browse_directory(const char* path, Device* device) { return NULL; } + char pathbuf[PATH_MAX]; + struct stat st; int d_size = 0; int d_alloc = 10; char** dirs = (char**)malloc(d_alloc * sizeof(char*)); @@ -758,8 +760,12 @@ static char* browse_directory(const char* path, Device* device) { struct dirent* de; while ((de = readdir(d)) != NULL) { int name_len = strlen(de->d_name); + snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name); - if (de->d_type == DT_DIR) { + if (lstat(pathbuf, &st) != 0) { + LOGE("Failed to stat %s\n", pathbuf); + break; + } else if (S_ISDIR(st.st_mode)) { // skip "." and ".." entries if (name_len == 1 && de->d_name[0] == '.') continue; if (name_len == 2 && de->d_name[0] == '.' && @@ -774,7 +780,7 @@ static char* browse_directory(const char* path, Device* device) { dirs[d_size][name_len] = '/'; dirs[d_size][name_len+1] = '\0'; ++d_size; - } else if (de->d_type == DT_REG && + } else if (S_ISREG(st.st_mode) && name_len >= 4 && strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) { if (z_size >= z_alloc) { -- cgit v1.2.3