Modify the search function 57/149157/5 accepted/tizen/unified/20170913.071136 submit/tizen/20170912.034220
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 12 Sep 2017 00:32:36 +0000 (09:32 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 12 Sep 2017 01:52:09 +0000 (10:52 +0900)
Use glib API instead of dirent API

Change-Id: I2dc135b0f53f915ad478609b6457aa3999140780
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
tests/mm_file_traverse.h
tests/mm_file_traverser.c

index 0a770a8..3057d51 100755 (executable)
 #ifndef _MM_FILE_TRAVERSE_H_
 #define _MM_FILE_TRAVERSE_H_
 
-#define MMFILE_PATH_MAX 256
+#define MMFILE_PATH_MAX 4096
 
 typedef enum {
        MMFILE_FAIL = 0,
        MMFILE_SUCCESS
 } MMFILE_RETURN;
 
-#define SAFE_STRLCPY(dst, src, n)      g_strlcpy(dst, src, n);
-#define SAFE_STRLCAT(dst, src, n)      g_strlcat(dst, src, n);
+#define SAFE_FREE(src)      { if (src) {free(src); src = NULL; } }
 
 typedef int (*MMFunc)(void *data, void *user_data, bool file_test);
 
index 2c69143..aec9ce8 100755 (executable)
@@ -19,9 +19,6 @@
  *
  */
 
-#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;
-
-
 }