From aeb998cac207ca2134618c5567fb2d5503d94245 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Fri, 11 Jul 2014 10:07:03 +0300 Subject: [PATCH] core: Add support for secondary dirent type lookup On some systems readdir() may only return DT_UNKNOWN for the dirent type. In such a case we need to do a secondary lookup e.g. with lstat to figure out the real time (for our purposes we're only interested in whether it's a directory or not). --- src/adapter.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 010ec2e8c..c7b9b7e47 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -2805,9 +2805,21 @@ static uint8_t get_le_addr_type(GKeyFile *keyfile) return addr_type; } +static unsigned char dirent_type(const char *parent, const char *name) +{ + char filename[PATH_MAX]; + struct stat st; + + snprintf(filename, sizeof(filename), "%s/%s", parent, name); + if (lstat(filename, &st) == 0 && S_ISDIR(st.st_mode)) + return DT_DIR; + + return DT_UNKNOWN; +} + static void load_devices(struct btd_adapter *adapter) { - char filename[PATH_MAX + 1]; + char filename[PATH_MAX + 1], dirname[PATH_MAX + 1]; char srcaddr[18]; GSList *keys = NULL; GSList *ltks = NULL; @@ -2818,10 +2830,10 @@ static void load_devices(struct btd_adapter *adapter) ba2str(&adapter->bdaddr, srcaddr); - snprintf(filename, PATH_MAX, STORAGEDIR "/%s", srcaddr); - filename[PATH_MAX] = '\0'; + snprintf(dirname, PATH_MAX, STORAGEDIR "/%s", srcaddr); + dirname[PATH_MAX] = '\0'; - dir = opendir(filename); + dir = opendir(dirname); if (!dir) { error("Unable to open adapter storage directory: %s", filename); return; @@ -2837,6 +2849,9 @@ static void load_devices(struct btd_adapter *adapter) struct conn_param *param; uint8_t bdaddr_type; + if (entry->d_type == DT_UNKNOWN) + entry->d_type = dirent_type(dirname, entry->d_name); + if (entry->d_type != DT_DIR || bachk(entry->d_name) < 0) continue; -- 2.47.3