From e1f4c4f71aba2f9fa07717025f2052d7eb2e212e Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 8 Nov 2016 18:48:10 -0500 Subject: [PATCH] Port: Replace readdir_r by nftw MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit readdir_r is deprecated and not available on all platforms, replace it by nftw. Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- configure.ac | 1 + lib/plugin/plugin.c | 200 +++++++++++++++----------------------------- tests/lib/common.c | 46 +++++----- 3 files changed, 89 insertions(+), 158 deletions(-) diff --git a/configure.ac b/configure.ac index 9051405d..0addb4c5 100644 --- a/configure.ac +++ b/configure.ac @@ -62,6 +62,7 @@ AC_HEADER_STDBOOL AC_CHECK_HEADERS([ \ fcntl.h \ float.h \ + ftw.h \ libintl.h \ limits.h \ malloc.h \ diff --git a/lib/plugin/plugin.c b/lib/plugin/plugin.c index a1a522ec..b1193147 100644 --- a/lib/plugin/plugin.c +++ b/lib/plugin/plugin.c @@ -45,14 +45,18 @@ #include #include #include -#include +#include +#include #define PYTHON_PLUGIN_PROVIDER_FILENAME "libbabeltrace-python-plugin-provider." G_MODULE_SUFFIX #define PYTHON_PLUGIN_PROVIDER_SYM_NAME bt_plugin_python_create_all_from_file #define PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR TOSTRING(PYTHON_PLUGIN_PROVIDER_SYM_NAME) +#define APPEND_ALL_FROM_DIR_NFDOPEN_MAX 8 + #ifdef BT_BUILT_IN_PYTHON_PLUGIN_SUPPORT #include + static struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *path) = bt_plugin_python_create_all_from_file; @@ -380,21 +384,64 @@ end: return comp_cls; } -/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ +static struct { + pthread_mutex_t lock; + struct bt_plugin_set *plugin_set; + bt_bool recurse; +} append_all_from_dir_info = { + .lock = PTHREAD_MUTEX_INITIALIZER +}; + static -struct dirent *alloc_dirent(const char *path) +int nftw_append_all_from_dir(const char *file, const struct stat *sb, int flag, + struct FTW *s) { - size_t len; - long name_max; - struct dirent *entry; + int ret = 0; + const char *name = file + s->base; + + /* Check for recursion */ + if (!append_all_from_dir_info.recurse && s->level > 1) { + goto end; + } + + switch (flag) { + case FTW_F: + if (name[0] == '.') { + /* Skip hidden files */ + BT_LOGV("Skipping hidden file: path=\"%s\"", file); + goto end; + } + struct bt_plugin_set *plugins_from_file = + bt_plugin_create_all_from_file(file); + + if (plugins_from_file) { + size_t j; + + for (j = 0; j < plugins_from_file->plugins->len; j++) { + struct bt_plugin *plugin = + g_ptr_array_index(plugins_from_file->plugins, j); + + BT_LOGD("Adding plugin to plugin set: " + "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"", + file, plugin, bt_plugin_get_name(plugin)); + bt_plugin_set_add_plugin(append_all_from_dir_info.plugin_set, plugin); + } - name_max = pathconf(path, _PC_NAME_MAX); - if (name_max == -1) { - name_max = PATH_MAX; + bt_put(plugins_from_file); + } + break; + case FTW_DNR: + /* Continue to next file / directory. */ + BT_LOGW("Cannot enter directory: continuing: path=\"%s\"", file); + break; + case FTW_NS: + /* Continue to next file / directory. */ + BT_LOGD("Cannot get file information: continuing: path=\"%s\"", file); + break; } - len = offsetof(struct dirent, d_name) + name_max + 1; - entry = zmalloc(len); - return entry; + +end: + return ret; } static @@ -402,9 +449,7 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir( struct bt_plugin_set *plugin_set, const char *path, bt_bool recurse) { - DIR *directory = NULL; - struct dirent *entry = NULL, *result = NULL; - char *file_path = NULL; + int nftw_flags = FTW_PHYS; size_t path_len; enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK; @@ -422,129 +467,22 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir( goto end; } - entry = alloc_dirent(path); - if (!entry) { - BT_LOGE_STR("Failed to allocate a directory entry."); - ret = BT_PLUGIN_STATUS_ERROR; - goto end; - } + pthread_mutex_lock(&append_all_from_dir_info.lock); - file_path = zmalloc(PATH_MAX); - if (!file_path) { - BT_LOGE("Failed to allocate %zu bytes.", (size_t) PATH_MAX); - ret = BT_PLUGIN_STATUS_NOMEM; - goto end; - } + append_all_from_dir_info.plugin_set = plugin_set; + append_all_from_dir_info.recurse = recurse; + ret = nftw(path, nftw_append_all_from_dir, + APPEND_ALL_FROM_DIR_NFDOPEN_MAX, nftw_flags); - strncpy(file_path, path, path_len); - /* Append a trailing '/' to the path */ - if (file_path[path_len - 1] != '/') { - file_path[path_len++] = '/'; - } + pthread_mutex_unlock(&append_all_from_dir_info.lock); - directory = opendir(file_path); - if (!directory) { - if (errno == EACCES) { - BT_LOGD("Cannot open directory: %s: continuing: " - "path=\"%s\", errno=%d", - strerror(errno), file_path, errno); - goto end; - } - - BT_LOGW("Cannot open directory: %s: " - "path=\"%s\", errno=%d", - strerror(errno), file_path, errno); + if (ret != 0) { + BT_LOGW("Cannot open directory: %s: path=\"%s\", errno=%d", + strerror(errno), path, errno); ret = BT_PLUGIN_STATUS_ERROR; - goto end; - } - - /* Recursively walk directory */ - while (!readdir_r(directory, entry, &result) && result) { - struct stat st; - int stat_ret; - size_t file_name_len; - - if (strcmp(result->d_name, ".") == 0 || - strcmp(result->d_name, "..") == 0) { - /* Obviously not logging this */ - continue; - } - - if (result->d_name[0] == '.') { - /* Skip hidden files, . and .. */ - BT_LOGV("Skipping hidden file: path=\"%s/%s\"", - path, result->d_name); - continue; - } - - file_name_len = strlen(result->d_name); - - if (path_len + file_name_len >= PATH_MAX) { - BT_LOGD("Skipping file because its path length is too large: continuing: " - "path=\"%s/%s\", length=%zu", - path, result->d_name, - (size_t) (path_len + file_name_len)); - continue; - } - - strncpy(file_path + path_len, result->d_name, file_name_len); - file_path[path_len + file_name_len] = '\0'; - stat_ret = stat(file_path, &st); - if (stat_ret < 0) { - /* Continue to next file / directory. */ - BT_LOGD("Cannot get file information: %s: continuing: " - "path=\"%s\", errno=%d", - strerror(errno), file_path, errno); - continue; - } - - if (S_ISDIR(st.st_mode) && recurse) { - ret = bt_plugin_create_append_all_from_dir(plugin_set, - file_path, BT_TRUE); - if (ret < 0) { - BT_LOGW("Cannot recurse into directory to find plugins: " - "path=\"%s\", ret=%d", file_path, ret); - goto end; - } - } else if (S_ISREG(st.st_mode)) { - struct bt_plugin_set *plugins_from_file = - bt_plugin_create_all_from_file(file_path); - - if (plugins_from_file) { - size_t j; - - for (j = 0; j < plugins_from_file->plugins->len; j++) { - struct bt_plugin *plugin = - g_ptr_array_index(plugins_from_file->plugins, j); - - BT_LOGD("Adding plugin to plugin set: " - "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"", - file_path, plugin, - bt_plugin_get_name(plugin)); - bt_plugin_set_add_plugin(plugin_set, - plugin); - } - - bt_put(plugins_from_file); - } - } } end: - if (directory) { - if (closedir(directory)) { - /* - * We don't want to override the error since there is - * nothing could do. - */ - BT_LOGE("Cannot close directory entry: %s: " - "path=\"%s\", errno=%d", - strerror(errno), path, errno); - } - } - - free(entry); - free(file_path); return ret; } diff --git a/tests/lib/common.c b/tests/lib/common.c index 08bc1185..1878d777 100644 --- a/tests/lib/common.c +++ b/tests/lib/common.c @@ -22,37 +22,29 @@ #include #include #include -#include -#include +#include -void recursive_rmdir(const char *path) -{ - struct dirent *entry; - DIR *dir = opendir(path); +#define RMDIR_NFDOPEN 8 - if (!dir) { - perror("# opendir"); - return; +static +int nftw_recursive_rmdir(const char *file, const struct stat *sb, int flag, + struct FTW *s) +{ + switch (flag) { + case FTW_F: + unlink(file); + break; + case FTW_DP: + rmdir(file); + break; } - while ((entry = readdir(dir))) { - struct stat st; - char filename[PATH_MAX]; - - if (snprintf(filename, sizeof(filename), "%s/%s", - path, entry->d_name) <= 0) { - continue; - } - - if (stat(filename, &st)) { - continue; - } + return 0; +} - if (S_ISREG(st.st_mode)) { - unlinkat(bt_dirfd(dir), entry->d_name, 0); - } - } +void recursive_rmdir(const char *path) +{ + int nftw_flags = FTW_PHYS | FTW_DEPTH; - rmdir(path); - closedir(dir); + nftw(path, nftw_recursive_rmdir, RMDIR_NFDOPEN, nftw_flags); } -- 2.34.1