From 8490b7d917c46951e3bc6708000fb542350b814b Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 5 Nov 2019 16:56:46 -0800 Subject: [PATCH] intel/perf: adapt to platforms like Solaris without d_type in struct dirent Signed-off-by: Alan Coopersmith [Eric: factor out the is_dir_or_link() check and fix a bug in v1] Signed-off-by: Eric Engestrom v3: include directory path when lstat'ing files v4: fix inverted check in enumerate_sysfs_metrics() Reviewed-by: Eric Engestrom Tested-by: Marge Bot Part-of: --- meson.build | 5 +++++ src/intel/perf/gen_perf.c | 25 ++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/meson.build b/meson.build index 35cd2e6f632..2cf0ee7fe0c 100644 --- a/meson.build +++ b/meson.build @@ -1178,6 +1178,11 @@ if host_machine.system() != 'windows' endif endif +if cc.has_member('struct dirent', 'd_type', prefix: '''#include + #include ''') + pre_args += '-DHAVE_DIRENT_D_TYPE' +endif + # strtod locale support if cc.links(''' #define _GNU_SOURCE diff --git a/src/intel/perf/gen_perf.c b/src/intel/perf/gen_perf.c index 421dfb8db52..38dc0166106 100644 --- a/src/intel/perf/gen_perf.c +++ b/src/intel/perf/gen_perf.c @@ -29,6 +29,10 @@ #include #include +#ifndef HAVE_DIRENT_D_TYPE +#include // PATH_MAX +#endif + #include #include "common/gen_gem.h" @@ -395,6 +399,20 @@ static inline uint64_t to_user_pointer(void *ptr) return (uintptr_t) ptr; } +static bool +is_dir_or_link(const struct dirent *entry, const char *parent_dir) +{ +#ifdef HAVE_DIRENT_D_TYPE + return entry->d_type == DT_DIR || entry->d_type == DT_LNK; +#else + struct stat st; + char path[PATH_MAX + 1]; + snprintf(path, sizeof(path), "%s/%s", parent_dir, entry->d_name); + lstat(path, &st); + return S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode); +#endif +} + static bool get_sysfs_dev_dir(struct gen_perf_config *perf, int fd) { @@ -434,8 +452,7 @@ get_sysfs_dev_dir(struct gen_perf_config *perf, int fd) } while ((drm_entry = readdir(drmdir))) { - if ((drm_entry->d_type == DT_DIR || - drm_entry->d_type == DT_LNK) && + if (is_dir_or_link(drm_entry, perf->sysfs_dev_dir) && strncmp(drm_entry->d_name, "card", 4) == 0) { len = snprintf(perf->sysfs_dev_dir, @@ -551,9 +568,7 @@ enumerate_sysfs_metrics(struct gen_perf_config *perf) while ((metric_entry = readdir(metricsdir))) { struct hash_entry *entry; - - if ((metric_entry->d_type != DT_DIR && - metric_entry->d_type != DT_LNK) || + if (!is_dir_or_link(metric_entry, buf) || metric_entry->d_name[0] == '.') continue; -- 2.30.2