Improve __msc_dir_scan function 91/212691/3
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 26 Aug 2019 05:44:06 +0000 (14:44 +0900)
committerhj kim <backto.kim@samsung.com>
Fri, 30 Aug 2019 00:34:27 +0000 (00:34 +0000)
Change-Id: I0427f03e26f592743dac7b29bd5e3f23e08085f3
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-common-utils.c
src/scanner/media-scanner-scan.c

index 2e6bac3..7403d4f 100755 (executable)
@@ -290,11 +290,6 @@ int ms_check_scan_ignore(char * path, uid_t uid)
        char *mediashared = NULL;
 #endif
 
-       if (strstr(path, "/.")) {
-               MS_DBG_ERR("hidden path");
-               return MS_MEDIA_ERR_INVALID_PATH;
-       }
-
        /* Check for symbolic link */
        tmp_path = realpath(path, NULL);
        /* Get trimmed path */
index c7b4631..a60a88c 100755 (executable)
@@ -38,14 +38,14 @@ GMutex scan_req_mutex;
 
 static int __msc_clear_file_list(GArray *path_array);
 
-static int __msc_check_stop_status()
+static bool __msc_is_stop_needed()
 {
        if (power_off) {
-               MS_DBG_ERR("Power off");
-               return MS_MEDIA_ERR_SCANNER_FORCE_STOP;
-       } else {
-               return MS_MEDIA_ERR_NONE;
+               MS_DBG_WARN("Power off");
+               return true;
        }
+
+       return false;
 }
 
 static int __msc_dir_scan(sqlite3 *handle, const char *storage_id, const char*start_path, bool check_exists, bool is_recursive, uid_t uid)
@@ -55,25 +55,22 @@ static int __msc_dir_scan(sqlite3 *handle, const char *storage_id, const char*st
        GError *error = NULL;
        const char *name;
        GArray *dir_array = NULL;
-       char *new_path = NULL;
        char *current_path = NULL;
-       char path[MS_FILE_PATH_LEN_MAX] = { 0 };
+       char *path = NULL;
        int (*scan_function)(sqlite3 *, const char*, const char*, uid_t) = NULL;
 
        dir_array = g_array_new(FALSE, FALSE, sizeof(char*));
-       if (dir_array == NULL) {
-               MS_DBG_ERR("g_array_new failed");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
+       MS_DBG_RETV_IF(dir_array == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY);
 
        g_array_append_val(dir_array, start_path);
 
        scan_function = (check_exists == false) ? ms_insert_item_batch : ms_validate_item;
 
        while (dir_array->len != 0) {
-               ret = __msc_check_stop_status();
-               if (ret != MS_MEDIA_ERR_NONE)
+               if (__msc_is_stop_needed()) {
+                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                        goto STOP_SCAN;
+               }
 
                current_path = g_array_index(dir_array , char*, 0);
                g_array_remove_index(dir_array, 0);
@@ -85,69 +82,63 @@ static int __msc_dir_scan(sqlite3 *handle, const char *storage_id, const char*st
                }
 
                dir = g_dir_open(current_path, 0, &error);
-               if (error == NULL && dir != NULL) {
-                       if (ms_insert_folder(handle, storage_id, current_path, uid) != MS_MEDIA_ERR_NONE)
-                               MS_DBG_ERR("insert folder failed");
+               if (error != NULL) {
+                       MS_DBG_ERR("g_dir_open fails [%s]", current_path);
+                       MS_SAFE_FREE(current_path);
+                       g_error_free(error);
+                       error = NULL;
+                       continue;
+               }
 
-                       while ((name = g_dir_read_name(dir))) {
-                               ret = __msc_check_stop_status();
-                               if (ret != MS_MEDIA_ERR_NONE)
-                                       goto STOP_SCAN;
+               if (ms_insert_folder(handle, storage_id, current_path, uid) != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("insert folder failed");
 
-                               if (name[0] == '.')
-                                       continue;
+               while ((name = g_dir_read_name(dir))) {
+                       if (__msc_is_stop_needed()) {
+                               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                               goto STOP_SCAN;
+                       }
 
-                               if (ms_strappend(path, sizeof(path), "%s/%s", current_path, name) != MS_MEDIA_ERR_NONE) {
-                                       MS_DBG_ERR("ms_strappend failed");
+                       if (name[0] == '.')
+                               continue;
+
+                       path = g_build_path(G_DIR_SEPARATOR_S, current_path, name, NULL);
+
+                       if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
+                               /* Check symbolic link file */
+                               if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
+                                       MS_DBG_WARN("Symbolic link.. Skip this file");
+                                       MS_SAFE_FREE(path);
                                        continue;
                                }
 
-                               if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
-                                       /* Check symbolic link file */
-                                       if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
-                                               MS_DBG_WARN("Symbolic link.. Skip this file");
-                                               continue;
-                                       }
+                               /* Check content.scanning.others feature */
+                               if (!ms_check_support_media_type(path)) {
+                                       MS_DBG("Unsupported media type");
+                                       MS_SAFE_FREE(path);
+                                       continue;
+                               }
 
-                                       /* Check content.scanning.others feature */
-                                       if (!ms_check_support_media_type(path)) {
-                                               MS_DBG("Unsupported media type");
-                                               continue;
-                                       }
+                               if (scan_function(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE)
+                                       MS_DBG_ERR("failed to update db");
 
-                                       if (scan_function(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE) {
-                                               MS_DBG_ERR("failed to update db");
-                                               continue;
-                                       }
-                               } else if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
-                                       if (is_recursive) {
-                                               new_path = strdup(path);
-                                               if (new_path != NULL) {
-                                                       g_array_append_val(dir_array, new_path);
-                                               } else {
-                                                       MS_DBG_ERR("strdup failed");
-                                                       continue;
-                                               }
-                                       } else {
-                                               if (ms_insert_folder(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE)
-                                                       MS_DBG_ERR("insert folder failed");
-                                       }
+                               MS_SAFE_FREE(path);
+                       } else {
+                               if (is_recursive) {
+                                       g_array_append_val(dir_array, path);
+                               } else {
+                                       ms_insert_folder(handle, storage_id, path, uid);
+                                       MS_SAFE_FREE(path);
                                }
                        }
-               } else {
-                       MS_DBG_ERR("g_dir_open fails [%s]", current_path);
-                       if (error != NULL) {
-                               g_error_free(error);
-                               error = NULL;
-                       }
                }
 
+               MS_SAFE_FREE(current_path);
+
                if (dir) {
                        g_dir_close(dir);
                        dir = NULL;
                }
-
-               MS_SAFE_FREE(current_path);
        }
 
 STOP_SCAN:
@@ -156,6 +147,8 @@ STOP_SCAN:
                dir = NULL;
        }
 
+       MS_SAFE_FREE(current_path);
+
        __msc_clear_file_list(dir_array);
 
        return ret;
@@ -319,10 +312,8 @@ gboolean msc_directory_scan_thread(void *data)
                        ms_send_dir_update_noti(scan_data->msg, folder_uuid, noti_type, scan_data->pid);
                }
 
-               if (power_off) {
-                       MS_DBG_WARN("power off");
+               if (__msc_is_stop_needed())
                        goto _POWEROFF;
-               }
 
                /*disconnect from media db*/
                if (handle) ms_disconnect_db(handle);
@@ -440,10 +431,8 @@ NEXT:
                MS_SAFE_FREE(update_path);
                MS_SAFE_FREE(storage_id);
 
-               if (power_off) {
-                       MS_DBG_ERR("[No-Error] power off");
+               if (__msc_is_stop_needed())
                        goto _POWEROFF;
-               }
 
                /*disconnect from media db*/
                if (handle) ms_disconnect_db(handle);
@@ -584,8 +573,7 @@ static int __msc_batch_insert(int pid, GArray *path_array, uid_t uid)
                /* insert to db */
                err = ms_insert_item_batch(handle, storage_id, insert_path, uid);
 
-               if (power_off) {
-                       MS_DBG_ERR("power off");
+               if (__msc_is_stop_needed()) {
                        /*call for bundle commit*/
                        ms_register_end(uid);
                        break;
@@ -754,9 +742,10 @@ static int __msc_dir_scan_meta_update(const char*start_path, const char *storage
        g_array_append_val(dir_array, tmp_path);
 
        while (dir_array->len != 0) {
-               ret = __msc_check_stop_status();
-               if (ret != MS_MEDIA_ERR_NONE)
+               if (__msc_is_stop_needed()) {
+                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                        goto STOP_SCAN;
+               }
 
                current_path = g_array_index(dir_array , char*, 0);
                g_array_remove_index(dir_array, 0);
@@ -770,14 +759,15 @@ static int __msc_dir_scan_meta_update(const char*start_path, const char *storage
                dir = g_dir_open(current_path, 0, &error);
                if (error == NULL && dir != NULL) {
                        while ((name = g_dir_read_name(dir))) {
-                               ret = __msc_check_stop_status();
-                               if (ret != MS_MEDIA_ERR_NONE)
+                               if (__msc_is_stop_needed()) {
+                                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                                        goto STOP_SCAN;
+                               }
 
                                if (name[0] == '.')
                                        continue;
 
-                                if (ms_strappend(path, sizeof(path), "%s/%s", current_path, name) != MS_MEDIA_ERR_NONE) {
+                               if (ms_strappend(path, sizeof(path), "%s/%s", current_path, name) != MS_MEDIA_ERR_NONE) {
                                        MS_DBG_ERR("ms_strappend failed");
                                        continue;
                                }
@@ -866,11 +856,10 @@ gboolean msc_metadata_update(void *data)
                stg_info = g_array_index(storage_list, ms_stg_info_s *, idx);
 
                /* Check power off status.. storage list vacating for g_array_free.. */
-               if (power_off) {
+               if (__msc_is_stop_needed()) {
                        MS_SAFE_FREE(stg_info->stg_path);
                        MS_SAFE_FREE(stg_info->storage_id);
                        MS_SAFE_FREE(stg_info);
-                       MS_DBG_WARN("power off");
                        continue;
                }
 
@@ -888,10 +877,8 @@ gboolean msc_metadata_update(void *data)
        /*call for bundle commit*/
        ms_update_end(scan_data->uid);
 
-       if (power_off) {
-               MS_DBG_WARN("power off");
+       if (__msc_is_stop_needed())
                goto _POWEROFF;
-       }
 
        /*disconnect form media db*/
        if (handle) ms_disconnect_db(handle);