From f1b90d61f1a1d63c0b1eda165f07b0c84a8990d9 Mon Sep 17 00:00:00 2001 From: "Sabera Djelti (sdi2)" Date: Fri, 26 Dec 2014 18:13:30 +0100 Subject: [PATCH] fix insert retrieve and delete cert info fix appsvcdb, open datacontroldb Bug-Tizen: TC-2222 Change-Id: I608874c486eb70586f81cd5b4017291e35224790 Signed-off-by: Sabera Djelti (sdi2) --- include/pkgmgr-info.h | 13 ++++---- parser/pkgmgr_parser.c | 14 +++------ parser/pkgmgr_parser_db.c | 25 ++++++++++++---- src/pkgmgr-info.c | 76 ++++++++++++++++++++++++++--------------------- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/include/pkgmgr-info.h b/include/pkgmgr-info.h index 4534190..3adc8e1 100755 --- a/include/pkgmgr-info.h +++ b/include/pkgmgr-info.h @@ -442,6 +442,7 @@ typedef enum { #define PMINFO_PKGINFO_PROP_RANGE_BASIC "PMINFO_PKGINFO_PROP_RANGE_BASIC" /* For multiuser support */ +char *getIconPath(uid_t uid); char *getUserPkgParserDBPath(void); char *getUserPkgParserDBPathUID(uid_t uid); char *getUserPkgCertDBPath(void); @@ -4393,7 +4394,7 @@ static int get_cert_info(const char *pkgid) } * @endcode */ -int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle); +int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle, uid_t uid); /** * @fn int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value) @@ -5135,7 +5136,7 @@ static int set_cert_in_db(const char *pkgid) pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; } - ret = pkgmgrinfo_save_pkgdbinfo(pkgid, handle); + ret = pkgmgrinfo_save_certinfo(pkgid, handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; @@ -5177,7 +5178,7 @@ static int set_cert_in_db(const char *pkgid) pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; } - ret = pkgmgrinfo_save_pkgdbinfo(pkgid, handle); + ret = pkgmgrinfo_save_certinfo(pkgid, handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; @@ -5218,7 +5219,7 @@ static int set_cert_in_db(const char *pkgid) pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; } - ret = pkgmgrinfo_save_pkgdbinfo(pkgid, handle); + ret = pkgmgrinfo_save_certinfo(pkgid, handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; @@ -5228,7 +5229,7 @@ static int set_cert_in_db(const char *pkgid) } * @endcode */ -int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle); +int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle, uid_t uid); /** * @fn int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle) @@ -5258,7 +5259,7 @@ static int set_cert_in_db(const char *pkgid) pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; } - ret = pkgmgrinfo_save_pkgdbinfo(pkgid, handle); + ret = pkgmgrinfo_save_certinfo(pkgid, handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_destroy_certinfo_set_handle(handle); return -1; diff --git a/parser/pkgmgr_parser.c b/parser/pkgmgr_parser.c index 849a57a..9f691ae 100755 --- a/parser/pkgmgr_parser.c +++ b/parser/pkgmgr_parser.c @@ -4746,10 +4746,10 @@ static int __ps_remove_nativeapp_desktop(manifest_x *mfx, uid_t uid) #define LIBAPPSVC_PATH LIB_PATH "/libappsvc.so.0" -static int __ps_remove_appsvc_db(manifest_x *mfx) +static int __ps_remove_appsvc_db(manifest_x *mfx, uid_t uid) { void *lib_handle = NULL; - int (*appsvc_operation) (const char *); + int (*appsvc_operation) (const char *, uid_t); int ret = 0; uiapplication_x *uiapplication = mfx->uiapplication; @@ -4765,7 +4765,7 @@ static int __ps_remove_appsvc_db(manifest_x *mfx) } for(; uiapplication; uiapplication=uiapplication->next) { - ret = appsvc_operation(uiapplication->appid); + ret = appsvc_operation(uiapplication->appid, uid); if (ret <0) _LOGE("can not operation symbol \n"); } @@ -5481,12 +5481,6 @@ API int pkgmgr_parser_parse_manifest_for_uninstallation(const char *manifest, ch else _LOGD("Removing desktop file Success\n"); - ret = __ps_remove_appsvc_db(mfx); - if (ret == -1) - _LOGD("Removing appsvc_db failed\n"); - else - _LOGD("Removing appsvc_db Success\n"); - pkgmgr_parser_free_manifest_xml(mfx); _LOGD("Free Done\n"); xmlCleanupParser(); @@ -5535,7 +5529,7 @@ API int pkgmgr_parser_parse_usr_manifest_for_uninstallation(const char *manifest else _LOGD("Removing desktop file Success\n"); - ret = __ps_remove_appsvc_db(mfx); + ret = __ps_remove_appsvc_db(mfx, uid); if (ret == -1) _LOGD("Removing appsvc_db failed\n"); else diff --git a/parser/pkgmgr_parser_db.c b/parser/pkgmgr_parser_db.c index c8819a9..7d6669a 100755 --- a/parser/pkgmgr_parser_db.c +++ b/parser/pkgmgr_parser_db.c @@ -267,7 +267,7 @@ static int __insert_serviceapplication_share_request_info(manifest_x *mfx); static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata); static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata); static void __insert_pkglocale_info(gpointer data, gpointer userdata); -static int __insert_manifest_info_in_db(manifest_x *mfx); +static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid); static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid); static int __delete_subpkg_info_from_db(char *appid); static int __delete_appinfo_from_db(char *db_table, const char *appid); @@ -1559,7 +1559,7 @@ static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx) return 0; } -static int __insert_manifest_info_in_db(manifest_x *mfx) +static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid) { label_x *lbl = mfx->label; license_x *lcn = mfx->license; @@ -1595,6 +1595,21 @@ static int __insert_manifest_info_in_db(manifest_x *mfx) if (ath->href) auth_href = ath->href; } + /*Insert in the package_cert_info CERT_DB*/ + pkgmgrinfo_instcertinfo_h cert_handle = NULL; + ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate"); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_destroy_certinfo_set_handle(cert_handle); + _LOGE("Cert Info DB create handle failed\n"); + return -1; + } + ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_destroy_certinfo_set_handle(cert_handle); + _LOGE("Cert Info DB Insert Failed\n"); + return -1; + } + /*Insert in the package_info DB*/ if (mfx->type) type = strdup(mfx->type); @@ -2255,7 +2270,7 @@ API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx) goto err; } _LOGD("Transaction Begin\n"); - ret = __insert_manifest_info_in_db(mfx); + ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER); if (ret == -1) { _LOGD("Insert into DB failed. Rollback now\n"); sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); @@ -2299,7 +2314,7 @@ API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) goto err; } _LOGD("Transaction Begin\n"); - ret = __insert_manifest_info_in_db(mfx); + ret = __insert_manifest_info_in_db(mfx, uid); if (ret == -1) { _LOGD("Insert into DB failed. Rollback now\n"); sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); @@ -2350,7 +2365,7 @@ API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); goto err; } - ret = __insert_manifest_info_in_db(mfx); + ret = __insert_manifest_info_in_db(mfx, uid); if (ret == -1) { _LOGD("Insert into DB failed. Rollback now\n"); sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); diff --git a/src/pkgmgr-info.c b/src/pkgmgr-info.c index fe6b418..4405472 100755 --- a/src/pkgmgr-info.c +++ b/src/pkgmgr-info.c @@ -313,7 +313,7 @@ __thread db_handle cert_db; static int __open_manifest_db(uid_t uid); static int __close_manifest_db(void); -static int __open_cert_db(uid_t uid); +static int __open_cert_db(uid_t uid, char* mode); static int __close_cert_db(void); static int __exec_pkginfo_query(char *query, void *data); static int __exec_certinfo_query(char *query, void *data); @@ -336,7 +336,7 @@ static void __destroy_each_node(gpointer data, gpointer user_data); static void __get_filter_condition(gpointer data, char **condition); static void __get_metadata_filter_condition(gpointer data, char **condition); static gint __compare_func(gconstpointer data1, gconstpointer data2); -static int __delete_certinfo(const char *pkgid); +static int __delete_certinfo(const char *pkgid, uid_t uid); static int _check_create_Cert_db( sqlite3 *certdb); static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data); @@ -355,10 +355,6 @@ static int _pkgmgr_parser_attach_create_view_certdb(sqlite3 *handle, uid_t uid) query_attach, error_message); sqlite3_free(error_message); } - struct dbtable { - char *name_table; - char *primary_key; - }; snprintf(query_view, MAX_QUERY_LEN - 1, "CREATE temp VIEW %s as select * from (select *,0 as for_all_users from main.%s union select *,1 as for_all_users from Global.%s )", "package_cert_index_info", "package_cert_index_info", "package_cert_index_info"); if (SQLITE_OK != @@ -1045,25 +1041,32 @@ static int __close_cert_db(void) if(cert_db.ref) { if(--cert_db.ref == 0) sqlite3_close(GET_DB(cert_db)); + return 0; } _LOGE("Certificate DB is already closed !!\n"); return -1; } -static int __open_cert_db(uid_t uid) +static int __open_cert_db(uid_t uid, char* mode) { int ret = -1; + if(cert_db.ref) { + cert_db.ref ++; + return 0; + } + const char* user_cert_parser = getUserPkgCertDBPathUID(uid); if (access(user_cert_parser, F_OK) == 0) { ret = db_util_open_with_options(user_cert_parser, &GET_DB(cert_db), - SQLITE_OPEN_READWRITE, NULL); + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", user_cert_parser); - - ret = _pkgmgr_parser_attach_create_view_certdb(GET_DB(cert_db),uid); - retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n", user_cert_parser); - + cert_db.ref ++; + if ((strcmp(mode, "w") != 0)) { + ret = _pkgmgr_parser_attach_create_view_certdb(GET_DB(cert_db),uid); + retvm_if(ret != SQLITE_OK, -1, "attach db [%s] failed!\n", user_cert_parser); + } return 0; } _LOGE("Cert DB does not exists !!\n"); @@ -1076,6 +1079,7 @@ static int __close_datacontrol_db(void) if(datacontrol_db.ref) { if(--datacontrol_db.ref == 0) sqlite3_close(GET_DB(datacontrol_db)); + return 0; } _LOGE("Certificate DB is already closed !!\n"); return -1; @@ -1084,11 +1088,16 @@ static int __close_datacontrol_db(void) static int __open_datacontrol_db() { int ret = -1; + if(datacontrol_db.ref) { + datacontrol_db.ref ++; + return 0; + } if (access(DATACONTROL_DB, F_OK) == 0) { ret = db_util_open_with_options(DATACONTROL_DB, &GET_DB(datacontrol_db), SQLITE_OPEN_READONLY, NULL); retvm_if(ret != SQLITE_OK, -1, "connect db [%s] failed!\n", DATACONTROL_DB); + datacontrol_db.ref ++; return 0; } _LOGE("Datacontrol DB does not exists !!\n"); @@ -2704,7 +2713,7 @@ long long _pkgmgr_calculate_dir_size(char *dirname) } -static int __delete_certinfo(const char *pkgid) +static int __delete_certinfo(const char *pkgid, uid_t uid) { int ret = -1; int i = 0; @@ -2723,6 +2732,8 @@ static int __delete_certinfo(const char *pkgid) ret = PMINFO_R_ERROR; goto err; } + + __open_cert_db(uid, "w"); /*populate certinfo from DB*/ snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid); ret = __exec_certinfo_query(query, (void *)certinfo); @@ -2797,6 +2808,7 @@ err: (certinfo->cert_info)[i] = NULL; } } + __close_cert_db(); free(certinfo); certinfo = NULL; return ret; @@ -3697,7 +3709,7 @@ API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id, info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x)); retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!"); - ret = __open_cert_db(uid); + ret = __open_cert_db(uid, "r"); if (ret != 0) { ret = PMINFO_R_ERROR; goto err; @@ -7201,7 +7213,7 @@ API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle) return PMINFO_R_OK; } -API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle) +API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle, uid_t uid) { retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n"); retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n"); @@ -7211,15 +7223,13 @@ API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_ char query[MAX_QUERY_LEN] = {'\0'}; int exist = 0; int i = 0; - const char* user_pkg_cert = NULL; /*Open db.*/ - user_pkg_cert = getUserPkgCertDBPath(); - ret = db_util_open_with_options(user_pkg_cert, &GET_DB(cert_db), - SQLITE_OPEN_READWRITE, NULL); + ret = __open_cert_db(uid,"r"); if (ret != SQLITE_OK) { - _LOGE("connect db [%s] failed!\n", user_pkg_cert); - return PMINFO_R_ERROR; + _LOGE("connect db [%s] failed!\n"); + ret = PMINFO_R_ERROR; + goto err; } _check_create_Cert_db(GET_DB(cert_db)); /*validate pkgid*/ @@ -7325,7 +7335,7 @@ API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_i return PMINFO_R_OK; } -API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle) +API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle, uid_t uid) { retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n"); retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n"); @@ -7353,22 +7363,21 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha info->pkgid = strdup(pkgid); /*Open db.*/ - ret = db_util_open_with_options(getUserPkgCertDBPath(), &GET_DB(cert_db), - SQLITE_OPEN_READWRITE, NULL); - if (ret != SQLITE_OK) { - _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPath()); + ret =__open_cert_db(uid, "w"); + if (ret != 0) { ret = PMINFO_R_ERROR; + _LOGE("Failed to open cert db \n"); goto err; } _check_create_Cert_db(GET_DB(cert_db)); /*Begin Transaction*/ ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGE("Failed to begin transaction\n"); + if (ret == -1) { + _LOGE("Failed to begin transaction %s\n"); ret = PMINFO_R_ERROR; goto err; } - _LOGE("Transaction Begin\n"); + /*Check if request is to insert/update*/ snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid); if (SQLITE_OK != @@ -7383,7 +7392,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha /*Update request. We cant just issue update query directly. We need to manage index table also. Hence it is better to delete and insert again in case of update*/ - ret = __delete_certinfo(pkgid); + ret = __delete_certinfo(pkgid, uid); if (ret < 0) _LOGE("Certificate Deletion Failed\n"); } @@ -7435,7 +7444,6 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha (info->cert_id)[i] = indexinfo->cert_id; (info->is_new)[i] = is_new; (info->ref_count)[i] = indexinfo->cert_ref_count; - _LOGE("Id:Count = %d %d\n", indexinfo->cert_id, indexinfo->cert_ref_count); indexinfo->cert_id = 0; indexinfo->cert_ref_count = 0; is_new = 0; @@ -7507,7 +7515,7 @@ API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h ha ret = PMINFO_R_ERROR; goto err; } - _LOGE("Transaction Commit and End\n"); + ret = PMINFO_R_OK; err: __close_cert_db(); @@ -7548,7 +7556,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n"); int ret = -1; /*Open db.*/ - ret = __open_cert_db(uid); + ret = __open_cert_db(uid, "w"); if (ret != 0) { _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid)); ret = PMINFO_R_ERROR; @@ -7563,7 +7571,7 @@ API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid) goto err; } _LOGE("Transaction Begin\n"); - ret = __delete_certinfo(pkgid); + ret = __delete_certinfo(pkgid, uid); if (ret < 0) { _LOGE("Certificate Deletion Failed\n"); } else { -- 2.7.4