Replace readdir_r to readdir to fix build error 42/118542/1 accepted/tizen/common/20170313.162525 accepted/tizen/ivi/20170313.120228 accepted/tizen/mobile/20170313.120141 accepted/tizen/tv/20170313.120157 accepted/tizen/unified/20170313.120247 accepted/tizen/wearable/20170313.120212 submit/tizen/20170313.062244
authorJiyong Min <jiyong.min@samsung.com>
Mon, 13 Mar 2017 06:15:41 +0000 (15:15 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Mon, 13 Mar 2017 06:19:30 +0000 (15:19 +0900)
 [Problem] After update tool chain to GCC6, 'readdir_r is deprecated' build error happen

'readdir_r' is deprecated because of below reasons.
 *  On systems where NAME_MAX is undefined, calling readdir_r() may be
    unsafe because the interface does not allow the caller to specify
    the length of the buffer used for the returned directory entry.

 *  On some systems, readdir_r() can't read directory entries with
    very long names.  When the glibc implementation encounters such a
    name, readdir_r() fails with the error ENAMETOOLONG after the
    final directory entry has been read.  On some other systems,
    readdir_r() may return a success status, but the returned d_name
    field may not be null terminated or may be truncated.

Change-Id: Iaeabf4b09ab77dfedbc161c725c27a39ec4325c2
Signed-off-by: jiyong.min <jiyong.min@samsung.com>
tests/mm_file_traverser.c

index eef3f1b..1006120 100755 (executable)
 #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;
-       struct dirent *result = NULL;
+       struct dirent *dirp;
        DIR *dp;
 
        char pdirname[MMFILE_PATH_MAX + 1];
@@ -54,6 +54,9 @@ int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
                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));
 
 
@@ -64,22 +67,24 @@ int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
                        strncpy(pdirname, (char *) element_data, strlen((char *) element_data));
 
                        if ((dp = opendir(pdirname)) != NULL) {
-                               while (!readdir_r(dp, &dirp, &result)) {
+                               while ((dirp = readdir(dp)) != NULL) {
                                        char cdirname[MMFILE_PATH_MAX + 1];
 
-                                       if (strcmp(dirp.d_name, ".") == 0 ||
-                                           strcmp(dirp.d_name, "..") == 0) {
+                                       if (strcmp(dirp->d_name, ".") == 0 ||
+                                           strcmp(dirp->d_name, "..") == 0) {
                                                continue;
                                        }
 
                                        memset(cdirname, 0x00, MMFILE_PATH_MAX + 1);
                                        strncpy(cdirname, pdirname, strlen(pdirname));
                                        strncat(cdirname, "/", 1);
-                                       strncat(cdirname, dirp.d_name, strlen(dirp.d_name));
+                                       strncat(cdirname, dirp->d_name, strlen(dirp->d_name));
 
                                        if (lstat(cdirname, &statbuf) < 0) {
                                                printf("lstat error\n");
+                                               free(dirp);
                                                closedir(dp);
+                                               g_mutex_unlock(&g_fileinfo_mutex);
                                                return MMFILE_FAIL;
                                        }
 
@@ -92,7 +97,7 @@ int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
                                                        cbfunc(cdirname, user_data, true);
                                                }
                                        }
-
+                                       free(dirp);
                                }
 
                                closedir(dp);
@@ -104,6 +109,8 @@ int mmfile_get_file_names(char *root_dir, MMFunc cbfunc, void *user_data)
 
        g_list_free(g_directories);
 
+       g_mutex_unlock(&g_fileinfo_mutex);
+
        return MMFILE_SUCCESS;