Extractor-v2 code cleanup 35/240935/4
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 13 Aug 2020 02:46:47 +0000 (11:46 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Thu, 13 Aug 2020 06:46:05 +0000 (15:46 +0900)
Change-Id: I9fa036e4b90abfebf951c54e73176aa5ac9af1cb
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/scanner-v2/media-scanner-extract-v2.c

index d427eb6..f4af287 100644 (file)
  *
  */
 
-#include <dirent.h>    /* Defines DT_* constants */
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <sys/syscall.h>
-
-#include <dirent.h>
 #include <malloc.h>
-#include <vconf.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
@@ -51,8 +41,8 @@ static int stg_extract_status;
 
 static GMutex extract_item_mutex;
 
-static s_extract_itemcancel_extract_item = NULL;
-static s_extract_itemcur_extract_item = NULL;
+static s_extract_item *cancel_extract_item = NULL;
+static s_extract_item *cur_extract_item = NULL;
 
 static GCond extract_data_cond;
 static GMutex extract_data_mutex;
@@ -133,11 +123,6 @@ gpointer msc_folder_extract_thread(gpointer data)
        ms_comm_msg_s *extract_data = NULL;
        int ret = MS_MEDIA_ERR_NONE;
        sqlite3 *handle = NULL;
-       int scan_type;
-       char *storage_id = NULL;
-       char *update_path = NULL;
-       uid_t uid = MEDIA_DEFAULT_UID;
-       ms_noti_type_e noti_type = MS_ITEM_INSERT;
 
        while (1) {
                extract_data = g_async_queue_pop(folder_extract_queue);
@@ -150,51 +135,41 @@ gpointer msc_folder_extract_thread(gpointer data)
 
                __msc_set_cur_extract_item(extract_data->msg, extract_data->pid);
                g_directory_extract_processing = true;
-               uid = extract_data->uid;
 
-               /*connect to media db, if connecting is failed, db updating is stopped*/
-               ret = ms_connect_db(&handle, uid);
+               ret = ms_connect_db(&handle, extract_data->uid);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("ms_connect_db failed");
                        goto NEXT;
                }
 
-               scan_type = extract_data->msg_type;
-               noti_type = extract_data->noti_type;
+               MS_DBG("path[%.*s] storage_id[%.*s]", MAX_MSG_SIZE, extract_data->msg, MS_UUID_SIZE, extract_data->storage_id);
 
-               storage_id = g_strdup(extract_data->storage_id);
-               if (storage_id == NULL) {
-                       MS_DBG_ERR("storage_id NULL");
+               if (strlen(extract_data->storage_id) == 0) {
+                       MS_DBG_ERR("storage_id length is 0");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               MS_DBG("path : [%.*s], storage_id : [%s]", MAX_MSG_SIZE, extract_data->msg, storage_id);
-
-               update_path = g_strdup(extract_data->msg);
-
-               if (strlen(storage_id) == 0) {
-                       MS_DBG_ERR("storage_id length is 0. There is no information of your request [%.*s]", MAX_MSG_SIZE, extract_data->msg);
+               if (strlen(extract_data->msg) == 0) {
+                       MS_DBG_ERR("path length is 0");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               if (scan_type != MS_MSG_DIRECTORY_SCANNING
-                       && scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+               if (extract_data->msg_type != MS_MSG_DIRECTORY_SCANNING &&
+                       extract_data->msg_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
                        MS_DBG_ERR("Invalid request");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               ret = __msc_check_extract_stop_status(extract_data->msg_type, update_path, extract_data->pid);
-
+               ret = __msc_check_extract_stop_status(extract_data->msg_type, extract_data->msg, extract_data->pid);
                if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
                        MS_DBG_ERR("MS_MEDIA_ERR_SCANNER_FORCE_STOP");
                        goto NEXT;
                }
 
-               /*insert data into media db */
-               ret = msc_check_db_size(uid, extract_data->msg_type);
+               ret = msc_check_db_size(extract_data->uid, extract_data->msg_type);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("NOT ENOUGH MEMORY");
                        if (ret == MS_MEDIA_ERR_DB_FULL_FAIL)
@@ -203,24 +178,22 @@ gpointer msc_folder_extract_thread(gpointer data)
                        goto NEXT;
                }
 
-               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, update_path, scan_type, extract_data->pid);
+               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, extract_data->msg, extract_data->msg_type, extract_data->pid);
 
 NEXT:
                g_directory_extract_processing = false;
                if (_msc_is_power_off())
                        goto _POWEROFF;
 
-               /*Active flush */
                malloc_trim(0);
 
                if (extract_data->result) {
                        MS_DBG_ERR("MS_MSG_SCANNER_COMPLETE");
-                       if (noti_type == MS_ITEM_INSERT || noti_type == MS_ITEM_UPDATE) {
-                               /*send notification*/
+                       if (extract_data->noti_type == MS_ITEM_INSERT || extract_data->noti_type == MS_ITEM_UPDATE) {
                                char *folder_uuid = NULL;
-                               ms_get_folder_id(handle, storage_id, extract_data->msg, &folder_uuid);
-                               MS_DBG_WARN("storage_id = [%s], dir Path = [%s], folder_uuid = [%s], noti_type = [%d]", storage_id, update_path, folder_uuid, noti_type);
-                               ms_send_dir_update_noti(update_path, folder_uuid, noti_type, extract_data->pid);
+                               ms_get_folder_id(handle, extract_data->storage_id, extract_data->msg, &folder_uuid);
+                               MS_DBG_WARN("storage_id[%.*s] Path[%.*s] folder_id[%s] noti_type[%d]", MS_UUID_SIZE, extract_data->storage_id, MAX_MSG_SIZE, extract_data->msg, folder_uuid, extract_data->noti_type);
+                               ms_send_dir_update_noti(extract_data->msg, folder_uuid, extract_data->noti_type, extract_data->pid);
                        }
                        extract_data->msg_type = MS_MSG_EXTRACTOR_COMPLETE;
                        msc_send_result(ret, extract_data);
@@ -229,12 +202,8 @@ NEXT:
                        msc_send_result_partial(ret, MS_MSG_SCANNER_PARTIAL, extract_data->pid, extract_data->msg);
                }
 
-               g_free(update_path);
-               update_path = NULL;
                g_free(extract_data);
                extract_data = NULL;
-               g_free(storage_id);
-               storage_id = NULL;
 
                __msc_del_cur_extract_item();
                __msc_del_cancel_extract_item();
@@ -244,23 +213,19 @@ NEXT:
                ms_storage_scan_status_e storage_scan_status = MS_STORAGE_SCAN_NONE;
                __msc_get_storage_extract_status(&storage_scan_status);
 
-               /*get storage list and scan status from media db*/
                if (storage_scan_status != MS_STORAGE_SCAN_COMPLETE) {
                        __msc_resume_extract();
                        MS_DBG_WARN("extract RESUME OK");
                }
 
-               /*disconnect form media db*/
                ms_disconnect_db(handle);
                usleep(SCAN_SLEEP_TIME);
-       }                       /*thread while*/
+       }
 
 _POWEROFF:
        __msc_resume_extract();
 
-       g_free(update_path);
        g_free(extract_data);
-       g_free(storage_id);
        ms_disconnect_db(handle);
 
        return NULL;
@@ -272,10 +237,6 @@ gpointer msc_storage_extract_thread(gpointer data)
        int ret = MS_MEDIA_ERR_NONE;
        sqlite3 *handle = NULL;
        ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
-       int scan_type;
-       char *update_path = NULL;
-       uid_t uid = MEDIA_DEFAULT_UID;
-       int tem_ret = MS_MEDIA_ERR_NONE;
 
        while (1) {
                __msc_set_storage_extract_status(MS_STORAGE_SCAN_DONE);
@@ -289,48 +250,50 @@ gpointer msc_storage_extract_thread(gpointer data)
 
                MS_DBG_WARN("STORAGE extract START extract len is %d ", g_async_queue_length(storage_extract_queue));
 
-               scan_type = extract_data->msg_type;
-               if (scan_type != MS_MSG_STORAGE_ALL
-                       && scan_type != MS_MSG_STORAGE_PARTIAL) {
-                       MS_DBG_ERR("Invalid request[%d]", scan_type);
+               if (extract_data->msg_type != MS_MSG_STORAGE_ALL && extract_data->msg_type != MS_MSG_STORAGE_PARTIAL) {
+                       MS_DBG_ERR("Invalid request[%d]", extract_data->msg_type);
                        msc_remove_extract_request(extract_data);
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
-               uid = extract_data->uid;
-
-               /*connect to media db, if connecting is failed, db updating is stopped*/
-               ret = ms_connect_db(&handle, uid);
+               ret = ms_connect_db(&handle, extract_data->uid);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("ms_connect_db falied!");
                        continue;
                }
 
-               update_path = g_strdup(extract_data->msg);
-               if (!MS_STRING_VALID(update_path)) {
-                       MS_DBG_ERR("Invalid update_path");
+               MS_DBG("path[%.*s] storage_id[%.*s]", MAX_MSG_SIZE, extract_data->msg, MS_UUID_SIZE, extract_data->storage_id);
+
+               if (strlen(extract_data->storage_id) == 0) {
+                       MS_DBG_ERR("storage_id length is 0");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto NEXT;
+               }
+
+               if (strlen(extract_data->msg) == 0) {
+                       MS_DBG_ERR("path length is 0");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
-               MS_DBG_WARN("extract storage_id is [%s], path [%s]", extract_data->storage_id, update_path);
-               ret = ms_user_get_storage_type(uid, extract_data->msg, &storage_type);
+
+               ret = ms_user_get_storage_type(extract_data->uid, extract_data->msg, &storage_type);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("ms_user_get_storage_type failed");
                        goto NEXT;
                }
                if (storage_type == MS_USER_STORAGE_EXTERNAL_USB) {
-                       if (!ms_storage_mount_status(update_path)) {
-                               MS_DBG_ERR("%s is unmounted", update_path);
+                       if (!ms_storage_mount_status(extract_data->msg)) {
+                               MS_DBG_ERR("%s is unmounted", extract_data->msg);
                                ret = MS_MEDIA_ERR_USB_UNMOUNTED;
                                msc_remove_extract_request(extract_data);
                                goto NEXT;
                        }
                }
-               ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_PROCESSING, uid);
+               ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_PROCESSING, extract_data->uid);
                __msc_extract_set_db_status(MS_DB_UPDATING);
 
-               ret = msc_check_db_size(uid, extract_data->msg_type);
+               ret = msc_check_db_size(extract_data->uid, extract_data->msg_type);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("NOT ENOUGH MEMORY");
                        __msc_extract_set_db_status(MS_DB_STOPPED);
@@ -338,14 +301,13 @@ gpointer msc_storage_extract_thread(gpointer data)
                }
 
                /*extract meta*/
-               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, update_path, scan_type, extract_data->pid);
+               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, extract_data->msg, extract_data->msg_type, extract_data->pid);
                MS_DBG_WARN("extract PAUSE");
                __msc_pause_extract();
                MS_DBG_WARN("extract RESUME");
 
-               tem_ret = __msc_check_extract_stop_status(extract_data->msg_type, update_path, extract_data->pid);
-               if (tem_ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
-                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_STOP, uid);
+               if (__msc_check_extract_stop_status(extract_data->msg_type, extract_data->msg, extract_data->pid) == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_STOP, extract_data->uid);
                        __msc_set_storage_extract_status(MS_STORAGE_SCAN_META_STOP);
                        MS_DBG_WARN("storage scan force stopped");
                        /* set vconf key db extract status */
@@ -357,19 +319,14 @@ gpointer msc_storage_extract_thread(gpointer data)
 
                if (extract_data->result) {
                        MS_DBG_WARN("extract_data->result is true, MS_STORAGE_SCAN_COMPLETE");
-                       __msc_del_extract_blocked_path(update_path);
-                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_COMPLETE, uid);
+                       __msc_del_extract_blocked_path(extract_data->msg);
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_COMPLETE, extract_data->uid);
                        __msc_set_storage_extract_status(MS_STORAGE_SCAN_COMPLETE);
 
-                       /* send notification */
-                       ms_send_dir_update_noti(update_path, NULL, MS_ITEM_UPDATE, extract_data->pid);
-                       /* set vconf key db extract status */
+                       ms_send_dir_update_noti(extract_data->msg, NULL, MS_ITEM_UPDATE, extract_data->pid);
                        __msc_extract_set_db_status(MS_DB_UPDATED);
                }
 NEXT:
-               g_free(update_path);
-               update_path = NULL;
-
                if (_msc_is_power_off())
                        goto _POWEROFF;
 
@@ -679,7 +636,6 @@ static void __msc_pause_extract(void)
 static int __msc_extract_set_db_status(ms_db_status_type_t status)
 {
        int res = MS_MEDIA_ERR_NONE;
-       //int err = 0;
 
        if (!ms_config_set_int(VCONFKEY_PRIVATE_EXTRACTSTATUS, status)) {
                res = MS_MEDIA_ERR_INTERNAL;