From f8f0ddd8f819662703f2c7373f075748858a3ce3 Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Fri, 1 Sep 2017 14:05:39 +0900 Subject: [PATCH] Move logic for reset mediadb Change-Id: I9a023ce952e61ac0378489d2bc083b57da6df01f Signed-off-by: Minje Ahn --- include/media-svc.h | 1 + plugin/media-content-plugin.c | 18 +++++++++++++ src/common/media-svc-db-utils.c | 48 +++++++++++++++++++++++++++++++++ src/common/media-svc.c | 19 +++++++------ src/include/common/media-svc-db-utils.h | 1 + 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/include/media-svc.h b/include/media-svc.h index af8a1bb..40f2912 100755 --- a/include/media-svc.h +++ b/include/media-svc.h @@ -35,6 +35,7 @@ extern "C" { int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write); int media_svc_disconnect(MediaSvcHandle *handle); +int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid); int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version); int media_svc_create_table(uid_t uid); int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path); diff --git a/plugin/media-content-plugin.c b/plugin/media-content-plugin.c index ca8e2f4..f40aa14 100755 --- a/plugin/media-content-plugin.c +++ b/plugin/media-content-plugin.c @@ -120,6 +120,24 @@ int disconnect_db(void *handle, char **err_msg) return MEDIA_SVC_PLUGIN_ERROR_NONE; } +int cleanup_db(void *handle, uid_t uid, char **err_msg) +{ + int ret = MEDIA_SVC_PLUGIN_ERROR_NONE; + + if (handle == NULL) { + __set_error_message(ERR_HANDLE, err_msg); + return MEDIA_SVC_PLUGIN_ERROR; + } + + ret = media_svc_cleanup_db(handle, uid); + if (ret < 0) { + __set_error_message(ret, err_msg); + return MEDIA_SVC_PLUGIN_ERROR; + } + + return MEDIA_SVC_PLUGIN_ERROR_NONE; +} + int check_item_exist(void *handle, const char *storage_id, const char *file_path, bool *modified, char **err_msg) { int ret = MEDIA_SVC_PLUGIN_ERROR_NONE; diff --git a/src/common/media-svc-db-utils.c b/src/common/media-svc-db-utils.c index 0c06bde..a9f3fc2 100755 --- a/src/common/media-svc-db-utils.c +++ b/src/common/media-svc-db-utils.c @@ -1418,6 +1418,54 @@ int _media_svc_get_user_version(sqlite3 *db_handle, int *user_version) return MS_MEDIA_ERR_NONE; } +int _media_svc_do_cleanup(sqlite3 *db_handle, uid_t uid) +{ + int ret = MS_MEDIA_ERR_NONE; + sqlite3_stmt *sql_stmt = NULL; + GList *sql_list = NULL; + int item_cnt = 0; + int idx = 0; + /*Make one query to drop table, delete folder, delete storage.. And only for external USB storage */ + char *sql = sqlite3_mprintf("select 'DROP TABLE ''' || storage_uuid || ''';DELETE FROM folder WHERE storage_uuid = ''' || storage_uuid || ''';DELETE FROM storage WHERE storage_uuid = ''' || storage_uuid || ''';' from storage where validity = 0 AND storage_type=2;"); + + ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt); + if (ret != MS_MEDIA_ERR_NONE) { + media_svc_error("error when get user_version."); + return ret; + } + + while (sqlite3_step(sql_stmt) == SQLITE_ROW) { + if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) + sql_list = g_list_append(sql_list, strdup((char *)sqlite3_column_text(sql_stmt, 0))); + } + + SQLITE3_FINALIZE(sql_stmt); + + if ((sql_list != NULL) && (g_list_length(sql_list) > 0)) { + item_cnt = g_list_length(sql_list); + + for (idx = 0; idx < item_cnt; idx++) { + char *query = NULL; + query = g_list_nth_data(sql_list, idx); + + if (STRING_VALID(query)) { + sql = sqlite3_mprintf("%s", query); + _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); + SAFE_FREE(query); + } + } + g_list_free(sql_list); + } + + /*Rebuild index*/ + sql = sqlite3_mprintf("VACUUM;"); + _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); + + return MS_MEDIA_ERR_NONE; +} + int _media_svc_sql_prepare_to_step(sqlite3 *handle, const char *sql_str, sqlite3_stmt **stmt) { int err = -1; diff --git a/src/common/media-svc.c b/src/common/media-svc.c index 4af9bde..52310ac 100755 --- a/src/common/media-svc.c +++ b/src/common/media-svc.c @@ -98,6 +98,17 @@ int media_svc_disconnect(MediaSvcHandle *handle) return ret; } +int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid) +{ + MediaDBHandle *db_handle = (MediaDBHandle *)handle; + + media_svc_debug_fenter(); + + media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + + return _media_svc_do_cleanup(db_handle, uid); +} + int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version) { sqlite3 *db_handle = (sqlite3 *)handle; @@ -1017,14 +1028,7 @@ int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, cons if (no_thumb) { update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid); } else { -#if 0 - if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) - update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid); - else - update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_thumb_path, media_uuid); -#else update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid); -#endif } ret = media_db_request_update_db_batch(update_item_sql, uid); @@ -2053,4 +2057,3 @@ int media_svc_get_media_type(const char *path, int *mediatype) return _media_svc_get_media_type(path, mediatype); } - diff --git a/src/include/common/media-svc-db-utils.h b/src/include/common/media-svc-db-utils.h index 74fdc99..7881ec7 100755 --- a/src/include/common/media-svc-db-utils.h +++ b/src/include/common/media-svc-db-utils.h @@ -38,6 +38,7 @@ int _media_svc_drop_media_table(const char *storage_id, uid_t uid); int _media_svc_update_media_view(sqlite3 *db_handle, uid_t uid); int _media_svc_sql_query(const char *sql_str, uid_t uid); int _media_svc_get_user_version(sqlite3 *db_handle, int *user_version); +int _media_svc_do_cleanup(sqlite3 *db_handle, uid_t uid); int _media_svc_sql_prepare_to_step(sqlite3 *handle, const char *sql_str, sqlite3_stmt **stmt); int _media_svc_sql_prepare_to_step_simple(sqlite3 *handle, const char *sql_str, sqlite3_stmt **stmt); int _media_svc_sql_begin_trans(uid_t uid); -- 2.7.4