Port: Replace readdir_r by nftw
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 8 Nov 2016 23:48:10 +0000 (18:48 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 5 Jul 2017 19:46:16 +0000 (15:46 -0400)
readdir_r is deprecated and not available on all platforms, replace it
by nftw.

Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
configure.ac
lib/plugin/plugin.c
tests/lib/common.c

index 9051405dc2b63668523d18551aa1d8b35e9475d7..0addb4c57672372a4ffe03ca6563081b1050802c 100644 (file)
@@ -62,6 +62,7 @@ AC_HEADER_STDBOOL
 AC_CHECK_HEADERS([ \
        fcntl.h \
        float.h \
+       ftw.h \
        libintl.h \
        limits.h \
        malloc.h \
index a1a522ecb4cc6068692e8519a0769bc82be6ce6e..b11931474ac6cfce5330400d8009e8eeb2fb5d5a 100644 (file)
 #include <stdint.h>
 #include <inttypes.h>
 #include <sys/stat.h>
-#include <dirent.h>
+#include <ftw.h>
+#include <pthread.h>
 
 #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 <babeltrace/plugin/python-plugin-provider-internal.h>
+
 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;
 }
 
index 08bc1185bebddca93cfefa4c37b4d26aa1f40b74..1878d777d3eba4b78f43523a4dcc8f48fd8719a0 100644 (file)
 #include <unistd.h>
 #include <stdio.h>
 #include <dirent.h>
-#include <babeltrace/compat/limits-internal.h>
-#include <sys/stat.h>
+#include <ftw.h>
 
-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);
 }
This page took 0.029269 seconds and 4 git commands to generate.