- path_len = strlen(path);
-
- if (path_len >= PATH_MAX) {
- ret = BT_PLUGIN_STATUS_ERROR;
- goto end;
- }
-
- entry = alloc_dirent(path);
- if (!entry) {
- ret = BT_PLUGIN_STATUS_ERROR;
- goto end;
- }
-
- file_path = zmalloc(PATH_MAX);
- if (!file_path) {
- ret = BT_PLUGIN_STATUS_NOMEM;
- goto end;
- }
-
- strncpy(file_path, path, path_len);
- /* Append a trailing '/' to the path */
- if (file_path[path_len - 1] != '/') {
- file_path[path_len++] = '/';
- }
-
- directory = opendir(file_path);
- if (!directory) {
- perror("Failed to open plug-in directory");
- 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 (result->d_name[0] == '.') {
- /* Skip hidden files, . and .. */
- continue;
- }
-
- file_name_len = strlen(result->d_name);
-
- if (path_len + file_name_len >= PATH_MAX) {
- 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. */
- printf_perror("Failed to stat() plugin file\n");
- continue;
- }
-
- if (S_ISDIR(st.st_mode) && recurse) {
- ret = bt_plugin_create_append_all_from_dir(plugins,
- file_path, true);
- if (ret < 0) {
- goto end;
- }
- } else if (S_ISREG(st.st_mode)) {
- struct bt_plugin **plugins_from_file =
- bt_plugin_create_all_from_file(file_path);
-
- if (plugins_from_file) {
- struct bt_plugin **plugin;
-
- for (plugin = plugins_from_file; *plugin; plugin++) {
- /* Transfer ownership to array */
- g_ptr_array_add(plugins, *plugin);
- }
-
- free(plugins_from_file);
- }
- }
- }
-end:
- if (directory) {
- if (closedir(directory)) {
- /*
- * We don't want to override the error since there is
- * nothing could do.
- */
- perror("Failed to close plug-in directory");
- }
- }
- free(entry);
- free(file_path);