*
*/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "mm_file_traverse.h"
-static GList *g_directories = NULL;
-static GMutex g_fileinfo_mutex;
-
int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
{
- struct stat statbuf;
- struct dirent *dirp;
- DIR *dp;
-
- char pdirname[MMFILE_PATH_MAX + 1];
-
- memset(pdirname, 0x00, MMFILE_PATH_MAX + 1);
-
- if (lstat(root_dir, &statbuf) < 0) {
- printf("lstat error\n");
+ GDir *dir = NULL;
+ GError *error = NULL;
+ GArray *dir_array = NULL;
+ const char *name;
+ char pdirname[MMFILE_PATH_MAX + 1] = {0, };
+ char *current_path = NULL;
+ char *start_path = NULL;
+ char *tmp_path = NULL;
+
+ if (!g_file_test(root_dir, G_FILE_TEST_IS_DIR)) {
+ printf("it is not directory\n");
return MMFILE_FAIL;
}
- if (S_ISDIR(statbuf.st_mode) == 0) {
- printf("it is not directory\n");
+ dir_array = g_array_new(false, false, sizeof(char*));
+ if (dir_array == NULL) {
+ printf("g_array_new failed");
return MMFILE_FAIL;
}
- /* "readdir" is thread-unsafty, so mutex lock is necessary for thread-safety*/
- g_mutex_lock(&g_fileinfo_mutex);
-
- g_directories = g_list_append(g_directories, strdup(root_dir));
-
-
- int i = 0;
- gpointer element_data = NULL;
- while ((element_data = g_list_nth_data(g_directories, i)) != NULL) {
- if (strlen((char *) element_data) > 0 && strlen((char *) element_data) <= MMFILE_PATH_MAX) {
- SAFE_STRLCPY(pdirname, (char *) element_data, sizeof(pdirname));
-
- if ((dp = opendir(pdirname)) != NULL) {
- while ((dirp = readdir(dp)) != NULL) {
- char cdirname[MMFILE_PATH_MAX + 1];
-
- if (strcmp(dirp->d_name, ".") == 0 ||
- strcmp(dirp->d_name, "..") == 0) {
- continue;
- }
-
- memset(cdirname, 0x00, MMFILE_PATH_MAX + 1);
- SAFE_STRLCPY(cdirname, pdirname, sizeof(cdirname));
- SAFE_STRLCAT(cdirname, "/", sizeof(cdirname));
- SAFE_STRLCAT(cdirname, dirp->d_name, sizeof(cdirname));
-
- if (lstat(cdirname, &statbuf) < 0) {
- printf("lstat error\n");
- free(dirp);
- closedir(dp);
- g_mutex_unlock(&g_fileinfo_mutex);
- return MMFILE_FAIL;
- }
-
- if (S_ISDIR(statbuf.st_mode)) {
- printf("directory: %s\n", cdirname);
- g_directories = g_list_append(g_directories, strdup(cdirname));
- } else {
- printf("file: %s\n", cdirname);
- if (cbfunc != NULL) {
- cbfunc(cdirname, user_data, true);
- }
- }
- free(dirp);
+ start_path = g_strdup(root_dir);
+ if (start_path != NULL)
+ g_array_append_val(dir_array, start_path);
+
+ while (dir_array->len != 0) {
+ current_path = g_array_index(dir_array , char*, 0);
+ g_array_remove_index(dir_array, 0);
+
+ dir = g_dir_open(current_path, 0, &error);
+ if (dir != NULL && error == NULL) {
+ while ((name = g_dir_read_name(dir))) {
+ memset(pdirname, 0, sizeof(pdirname));
+ snprintf(pdirname, sizeof(pdirname), "%s/%s", current_path, name);
+
+ if (g_file_test(pdirname, G_FILE_TEST_IS_DIR)) {
+ tmp_path = g_strdup(pdirname);
+ g_array_append_val(dir_array, tmp_path);
+ } else if (g_file_test(pdirname, G_FILE_TEST_IS_REGULAR)) {
+ if (cbfunc != NULL)
+ cbfunc(pdirname, user_data, true);
}
-
- closedir(dp);
}
+ } else {
+ printf("g_dir_open failed");
}
- i++;
- }
+ if (dir)
+ g_dir_close(dir);
- g_list_free(g_directories);
+ SAFE_FREE(current_path);
+ }
- g_mutex_unlock(&g_fileinfo_mutex);
+ if (dir_array)
+ g_array_free(dir_array, FALSE);
return MMFILE_SUCCESS;
-
-
}