Improve msc_metadata_update 68/212968/6
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 29 Aug 2019 03:09:42 +0000 (12:09 +0900)
committerhj kim <backto.kim@samsung.com>
Tue, 3 Sep 2019 05:11:24 +0000 (14:11 +0900)
Change-Id: I6ae478e656c6da1447caf9ac6b1da69064548039
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/include/media-common-db-svc.h
src/common/media-common-db-svc.c
src/scanner/media-scanner-scan.c
src/server/media-server-main.c
src/server/media-server-socket.c

index f36cf04..3cc4c4a 100755 (executable)
 #define STORAGE_REMOVED "storage_removed"
 #define DATADISC_STORAGE_ID    "data_disc"
 
-typedef struct ms_dir_info_s {
-       char *dir_path;
-       int modified_time;
-} ms_dir_info_s;
-
-typedef struct ms_stg_info_s {
-       char *stg_path;
-       char *storage_id;
-       int scan_status;
-} ms_stg_info_s;
-
 typedef enum {
        MS_ITEM_INSERT          = 0,
        MS_ITEM_DELETE          = 1,
@@ -79,9 +68,7 @@ typedef int (*UPDATE_STORAGE)(sqlite3 *, const char *, const char *, uid_t);
 typedef int (*SET_STORAGE_VALIDITY)(sqlite3 *, const char *, int, uid_t uid);
 typedef int (*SET_ALL_STORAGE_VALIDITY)(sqlite3 *, int, uid_t);
 
-typedef int (*UPDATE_ITEM_META)(const char *, const char *, int, uid_t);
-typedef int (*UPDATE_ITEM_BEGIN)(int);
-typedef int (*UPDATE_ITEM_END)(uid_t);
+typedef int (*UPDATE_ITEM_META)(sqlite3 *, uid_t);
 
 typedef int (*DELETE_INVALID_FOLDER_BY_PATH)(sqlite3 *, const char *, const char *, uid_t);
 typedef int (*CHECK_FOLDER_EXIST)(sqlite3*, const char*, const char*);
@@ -106,7 +93,6 @@ int ms_send_dir_update_noti(const char *path, const char *folder_id, ms_noti_typ
 int ms_update_folder_time(sqlite3 *handle, const char *storage_id, char *folder_path, uid_t uid);
 int ms_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid);
 int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_status_e scan_status, uid_t uid);
-int ms_get_storage_list(sqlite3 *handle, GArray **storage_array);
 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid);
 int ms_delete_invalid_folder(const char *storage_id, uid_t uid);
 int ms_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
@@ -125,7 +111,7 @@ int ms_insert_storage(sqlite3 *handle, const char *storage_id, const char *stora
 int ms_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid);
 int ms_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid);
 int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid);
-int ms_update_meta_batch(const char *path, const char *storage_id, uid_t uid);
+int ms_update_meta_batch(sqlite3 *handle, uid_t uid);
 int ms_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid);
 int ms_check_folder_exist(sqlite3 *handle, const char *storage_id, const char *folder_path);
 int ms_get_folder_id(sqlite3 *handle, const char *storage_id, const char *path, char **folder_id);
@@ -139,8 +125,6 @@ void ms_validate_start();
 void ms_validate_end(uid_t uid);
 void ms_insert_folder_start();
 void ms_insert_folder_end(uid_t uid);
-void ms_update_start();
-void ms_update_end(uid_t uid);
 
 int ms_get_extract_list(sqlite3 *handle, const char* storage_id, int storage_type, int scan_type, const char* path, int is_end, uid_t uid, void* array);
 int ms_update_one_extract_item(sqlite3 *handle, const char* storage_id, int storage_type, void* data);
index e6d18ba..b706889 100755 (executable)
@@ -68,8 +68,6 @@ enum func_list {
        eSET_STORAGE_VALIDITY,
        eSET_ALL_STORAGE_VALIDITY,
        eUPDATE_ITEM_META,
-       eUPDATE_ITEM_BEGIN,
-       eUPDATE_ITEM_END,
        eDELETE_INVALID_FOLDER_BY_PATH,
        eCHECK_FOLDER_EXIST,
        eGET_MEDIA_TYPE,
@@ -114,8 +112,6 @@ int ms_load_functions(void)
                "set_storage_validity",
                "set_all_storage_validity",
                "update_item_meta",
-               "update_item_begin",
-               "update_item_end",
                "delete_invalid_folder_by_path",
                "check_folder_exist",
                "get_media_type",
@@ -510,38 +506,6 @@ int ms_set_storage_scan_status(sqlite3 *handle, char *storage_id, media_scan_sta
        return ret;
 }
 
-int ms_get_storage_list(sqlite3 *handle, GArray **storage_array)
-{
-       int ret = MS_MEDIA_ERR_NONE;
-       sqlite3_stmt *sql_stmt = NULL;
-
-       char *sql = sqlite3_mprintf("SELECT storage_path, storage_id FROM storage WHERE validity=1");
-
-       ret = media_db_get_result(handle, sql, &sql_stmt);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("prepare error [%s]", sqlite3_errmsg(handle));
-               return ret;
-       }
-
-       *storage_array = g_array_new(FALSE, FALSE, sizeof(ms_stg_info_s*));
-
-       while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
-               ms_stg_info_s* stg_info = malloc(sizeof(ms_stg_info_s));
-               if (stg_info == NULL) {
-                       MS_DBG_ERR("Allcation failed");
-                       continue;
-               }
-               stg_info->stg_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
-               stg_info->storage_id = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
-               MS_DBG("Get path[%s] id[%s]", stg_info->stg_path, stg_info->storage_id);
-               g_array_append_val(*storage_array, stg_info);
-       }
-
-       MS_SQLITE3_FINALIZE(sql_stmt);
-
-       return ret;
-}
-
 int ms_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -672,17 +636,13 @@ int ms_set_all_storage_validity(sqlite3 *handle, int validity, uid_t uid)
        return ret;
 }
 
-int ms_update_meta_batch(const char *path, const char *storage_id, uid_t uid)
+int ms_update_meta_batch(sqlite3 *handle, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       ms_user_storage_type_e storage_type;
-
-       ret = ms_user_get_storage_type(uid, path, &storage_type);
-       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get storage type");
 
        MS_DBG_FENTER();
 
-       ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(path, storage_id, storage_type, uid); /*dlopen*/
+       ret = ((UPDATE_ITEM_META)func_array[eUPDATE_ITEM_META])(handle, uid); /*dlopen*/
        MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "UPDATE_ITEM_META failed [%d]", ret);
 
        return ret;
@@ -790,24 +750,6 @@ void ms_insert_folder_end(uid_t uid)
                MS_DBG_ERR("INSERT_FOLDER_END failed [%d]", ret);
 }
 
-void ms_update_start()
-{
-       int ret = MS_MEDIA_ERR_NONE;
-
-       ret = ((UPDATE_ITEM_BEGIN)func_array[eUPDATE_ITEM_BEGIN])(MSC_VALID_COUNT);/*dlopen*/
-       if (ret != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("UPDATE_ITEM_BEGIN failed [%d]", ret);
-}
-
-void ms_update_end(uid_t uid)
-{
-       int ret = MS_MEDIA_ERR_NONE;
-
-       ret = ((UPDATE_ITEM_END)func_array[eUPDATE_ITEM_END])(uid);/*dlopen*/
-       if (ret != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("UPDATE_ITEM_END failed [%d]", ret);
-}
-
 int ms_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
 {
        int ret = MS_MEDIA_ERR_NONE;
index e72d283..d743a79 100755 (executable)
@@ -708,186 +708,26 @@ _POWEROFF:
        return false;
 }
 
-static int __msc_dir_scan_meta_update(const char*start_path, const char *storage_id, uid_t uid)
-{
-       int ret = MS_MEDIA_ERR_NONE;
-       GDir *dir = NULL;
-       GError *error = NULL;
-       const char *name;
-       GArray *dir_array = NULL;
-       char *new_path = NULL;
-       char *current_path = NULL;
-       char *tmp_path = NULL;
-       char path[MS_FILE_PATH_LEN_MAX] = { 0 };
-
-       if (MS_STRING_VALID(start_path)) {
-               tmp_path = strndup(start_path, strlen(start_path));
-       } else {
-               MS_DBG_ERR("start_path is invalid parameter");
-               return MS_MEDIA_ERR_INVALID_PATH;
-       }
-
-       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;
-       }
-
-       g_array_append_val(dir_array, tmp_path);
-
-       while (dir_array->len != 0) {
-               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);
-
-               if (ms_check_scan_ignore(current_path, uid) != MS_MEDIA_ERR_NONE) {
-                       MS_DBG_ERR("%s is ignore", current_path);
-                       MS_SAFE_FREE(current_path);
-                       continue;
-               }
-
-               dir = g_dir_open(current_path, 0, &error);
-               if (error == NULL && dir != NULL) {
-                       while ((name = g_dir_read_name(dir))) {
-                               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) {
-                                       MS_DBG_ERR("ms_strappend failed");
-                                       continue;
-                               }
-
-                               if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
-                                       if (ms_update_meta_batch(path, storage_id, uid) != MS_MEDIA_ERR_NONE) {
-                                               MS_DBG_ERR("failed to update db");
-                                               continue;
-                                       }
-                               } else if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
-                                       new_path = strdup(path);
-                                       g_array_append_val(dir_array, new_path);
-                               }
-                       }
-               } else {
-                       MS_DBG_ERR("%s folder opendir fails", current_path);
-                       if (error) {
-                               g_error_free(error);
-                               error = NULL;
-                       }
-               }
-
-               if (dir) {
-                       g_dir_close(dir);
-                       dir = NULL;
-               }
-
-               MS_SAFE_FREE(current_path);
-       }               /*db update while */
-STOP_SCAN:
-       if (dir) {
-               g_dir_close(dir);
-               dir = NULL;
-       }
-
-       __msc_clear_file_list(dir_array);
-
-       if (ret != MS_MEDIA_ERR_NONE) MS_DBG_INFO("ret : %d", ret);
-
-       return ret;
-}
-
-
 gboolean msc_metadata_update(void *data)
 {
        ms_comm_msg_s *scan_data = data;
        int ret = MS_MEDIA_ERR_NONE;
-       int idx = 0;
        sqlite3 *handle = NULL;
-       GArray *storage_list = NULL;
-       ms_stg_info_s *stg_info = NULL;
-       char *usr_path = NULL;
-
-       MS_DBG_INFO("META UPDATE START");
 
-       /*connect to media db, if conneting is failed, db updating is stopped*/
        ret = ms_connect_db(&handle, scan_data->uid);
-       if (ret != MS_MEDIA_ERR_NONE)
-               return false;
-
-       /*call for bundle commit*/
-       ms_update_start();
-
-       /*insert data into media db */
-       ret = ms_user_get_internal_root_path(scan_data->uid, &usr_path);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("ms_user_get_internal_root_path() fail");
-               if (handle) ms_disconnect_db(handle);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-
-       ret = __msc_dir_scan_meta_update(usr_path, INTERNAL_STORAGE_ID, scan_data->uid);
-       /* send notification */
-       ms_send_dir_update_noti(usr_path, NULL, MS_ITEM_UPDATE, scan_data->pid);
-       MS_SAFE_FREE(usr_path);
-
-       /*__msc_dir_scan_meta_update For Each External Storage*/
-       ret = ms_get_storage_list(handle, &storage_list);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("ms_get_storage_list() fail");
-               if (handle) ms_disconnect_db(handle);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-
-       for (idx = 0; idx < storage_list->len; idx++) {
-               stg_info = g_array_index(storage_list, ms_stg_info_s *, idx);
-
-               /* Check power off status.. storage list vacating for g_array_free.. */
-               if (__msc_is_stop_needed()) {
-                       MS_SAFE_FREE(stg_info->stg_path);
-                       MS_SAFE_FREE(stg_info->storage_id);
-                       MS_SAFE_FREE(stg_info);
-                       continue;
-               }
-
-               ret = __msc_dir_scan_meta_update(stg_info->stg_path, stg_info->storage_id, scan_data->uid);
-               /* send notification */
-               ms_send_dir_update_noti(stg_info->stg_path, NULL, MS_ITEM_UPDATE, scan_data->pid);
-
-               MS_SAFE_FREE(stg_info->stg_path);
-               MS_SAFE_FREE(stg_info->storage_id);
-               MS_SAFE_FREE(stg_info);
-       }
-
-       g_array_free(storage_list, FALSE);
-
-       /*call for bundle commit*/
-       ms_update_end(scan_data->uid);
+       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_connect_db failed");
 
-       if (__msc_is_stop_needed())
-               goto _POWEROFF;
+       MS_DBG_INFO("META UPDATE START");
 
-       /*disconnect form media db*/
-       if (handle) ms_disconnect_db(handle);
+       ret = ms_update_meta_batch(handle, scan_data->uid);
+       ms_disconnect_db(handle);
 
-       /*Active flush */
-       malloc_trim(0);
        msc_send_result(ret, scan_data);
        MS_SAFE_FREE(scan_data);
-       MS_DBG_INFO("META UPDATE END [%d]", ret);
 
-_POWEROFF:
-       MS_SAFE_FREE(scan_data);
-       if (handle) ms_disconnect_db(handle);
+       MS_DBG_INFO("META UPDATE END [%d]", ret);
 
-       return false;
+       return FALSE;
 }
 
 
index 49b2ccc..0645990 100755 (executable)
@@ -376,7 +376,7 @@ void _ms_change_lang_vconf_cb(keynode_t *key, void* data)
        if (need_update) {
                uid_t uid = MEDIA_DEFAULT_UID;
                ms_sys_get_uid(&uid);
-               ms_send_storage_scan_request(NULL, INTERNAL_STORAGE_ID, MS_SCAN_META, uid);
+               ms_send_storage_scan_request(INTERNAL_STORAGE_ID, INTERNAL_STORAGE_ID, MS_SCAN_META, uid);
        } else {
                MS_DBG_WARN("language is changed but do not update meta data");
        }
index d573e12..12ec606 100755 (executable)
@@ -53,9 +53,6 @@ typedef struct ms_req_owner_data {
 
 static int __ms_add_owner(int pid, int client_sock, char *path)
 {
-       if (!MS_STRING_VALID(path))
-               return MS_MEDIA_ERR_INVALID_PATH;
-
        if (pid != 0) {
                ms_req_owner_data *owner_data = NULL;
                int len = strlen(path);