From 87cbb4aea101b563f8b44c1821bf328bde74e535 Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Thu, 7 Jan 2016 16:06:12 +0900 Subject: [PATCH] Fix handling index of certinfo when save certinfo this patch makes that handle index of certinfo like before. when save the certinfo, just override what certinfo handle has, and decrease ref count of index of old certinfo. Change-Id: Ic7b6ba1e9e7689967c8973cdf4b0cfc5f634c815 Signed-off-by: Sangyoon Jang --- parser/pkgmgr_parser_db.c | 28 ++++++++++ src/pkgmgrinfo_certinfo.c | 132 ++++++++++++++++++++++++++-------------------- src/pkgmgrinfo_db.c | 27 ++++++++++ 3 files changed, 130 insertions(+), 57 deletions(-) diff --git a/parser/pkgmgr_parser_db.c b/parser/pkgmgr_parser_db.c index c0162a8..6e32460 100644 --- a/parser/pkgmgr_parser_db.c +++ b/parser/pkgmgr_parser_db.c @@ -277,6 +277,16 @@ sqlite3 *pkgmgr_cert_db; " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \ "END;" +#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \ + "CREATE TRIGGER IF NOT EXISTS update_%s_info " \ + "AFTER UPDATE ON package_cert_info " \ + "WHEN (OLD.%s IS NOT NULL) " \ + "BEGIN" \ + " UPDATE package_cert_index_info SET" \ + " cert_ref_count = cert_ref_count - 1" \ + " WHERE cert_id = OLD.%s;" \ + "END;" + #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \ "(app_id text not null, " \ "providerid text not null, " \ @@ -1549,6 +1559,14 @@ static int __update_preload_condition_in_db() API int pkgmgr_parser_initialize_db(uid_t uid) { int ret = -1; + int i; + char query[MAX_QUERY_LEN]; + static const char *columns[] = { + "author_root_cert", "author_im_cert", "author_signer_cert", + "dist_root_cert", "dist_im_cert", "dist_signer_cert", + "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert", + NULL}; + /*Manifest DB*/ ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO); if (ret == -1) { @@ -1642,6 +1660,16 @@ API int pkgmgr_parser_initialize_db(uid_t uid) _LOGD("package cert index info DB initialization failed\n"); return ret; } + for (i = 0; columns[i] != NULL; i++) { + snprintf(query, sizeof(query), + QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT, + columns[i], columns[i], columns[i], columns[i]); + ret = __initialize_db(pkgmgr_cert_db, query); + if (ret == -1) { + _LOGD("package cert index info DB initialization failed\n"); + return ret; + } + } if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(GLOBAL_USER), GLOBAL_USER)) { _LOGD("Failed to change cert db permission\n"); diff --git a/src/pkgmgrinfo_certinfo.c b/src/pkgmgrinfo_certinfo.c index c82d287..091d898 100644 --- a/src/pkgmgrinfo_certinfo.c +++ b/src/pkgmgrinfo_certinfo.c @@ -463,63 +463,67 @@ API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_i static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid, char *cert_info[]) { - static const char query[] = - "INSERT OR REPLACE INTO package_cert_info (package," + static const char query_insert[] = + "INSERT INTO package_cert_info (package," " author_root_cert, author_im_cert, author_signer_cert," " dist_root_cert, dist_im_cert, dist_signer_cert," " dist2_root_cert, dist2_im_cert, dist2_signer_cert) " "VALUES(?, " - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT author_root_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT author_im_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT author_signer_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist_root_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist_im_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist_signer_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist2_root_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist2_im_cert FROM package_cert_info" - " WHERE package=?)))," - " (COALESCE( " - " (SELECT cert_id FROM package_cert_index_info" - " WHERE cert_info=?)," - " (SELECT dist2_signer_cert FROM package_cert_info" - " WHERE package=?))))"; + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?))"; + static const char query_update[] = + "UPDATE package_cert_info SET " + " author_root_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " author_im_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " author_signer_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " dist_root_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " dist_im_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " dist_signer_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + " dist2_root_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + "dist2_im_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?)," + "dist2_signer_cert= " + " (SELECT cert_id FROM package_cert_index_info" + " WHERE cert_info=?) " + "WHERE package=?"; int ret; sqlite3_stmt *stmt; int i; int idx; - ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + ret = sqlite3_prepare_v2(db, query_insert, strlen(query_insert), + &stmt, NULL); if (ret != SQLITE_OK) { _LOGE("prepare error: %s", sqlite3_errmsg(db)); return PMINFO_R_ERROR; @@ -528,23 +532,37 @@ static int _pkginfo_save_cert_info(sqlite3 *db, const char *pkgid, idx = 1; sqlite3_bind_text(stmt, idx++, pkgid, -1, SQLITE_STATIC); for (i = 0; i < MAX_CERT_TYPE; i++) { - ret = sqlite3_bind_text(stmt, idx++, cert_info[i], -1, - SQLITE_STATIC); - if (ret != SQLITE_OK) { + if (sqlite3_bind_text(stmt, idx++, cert_info[i], -1, + SQLITE_STATIC)) { _LOGE("bind error: %s", sqlite3_errmsg(db)); sqlite3_finalize(stmt); return PMINFO_R_ERROR; } - ret = sqlite3_bind_text(stmt, idx++, pkgid, -1, - SQLITE_STATIC); + } + + ret = sqlite3_step(stmt); + sqlite3_finalize(stmt); + if (ret == SQLITE_CONSTRAINT) { + ret = sqlite3_prepare_v2(db, query_update, strlen(query_update), + &stmt, NULL); if (ret != SQLITE_OK) { - _LOGE("bind error: %s", sqlite3_errmsg(db)); - sqlite3_finalize(stmt); + _LOGE("prepare error: %s", sqlite3_errmsg(db)); return PMINFO_R_ERROR; } + idx = 1; + for (i = 0; i < MAX_CERT_TYPE; i++) { + if (sqlite3_bind_text(stmt, idx++, cert_info[i], -1, + SQLITE_STATIC)) { + _LOGE("bind error: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return PMINFO_R_ERROR; + } + } + sqlite3_bind_text(stmt, idx++, pkgid, -1, SQLITE_STATIC); + ret = sqlite3_step(stmt); + sqlite3_finalize(stmt); } - ret = sqlite3_step(stmt); - sqlite3_finalize(stmt); + if (ret != SQLITE_DONE) { _LOGE("step error: %s", sqlite3_errmsg(db)); return PMINFO_R_ERROR; diff --git a/src/pkgmgrinfo_db.c b/src/pkgmgrinfo_db.c index 7353764..5fce33b 100644 --- a/src/pkgmgrinfo_db.c +++ b/src/pkgmgrinfo_db.c @@ -64,6 +64,17 @@ "BEGIN" \ " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \ "END;" + +#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \ + "CREATE TRIGGER IF NOT EXISTS update_%s_info " \ + "AFTER UPDATE ON package_cert_info " \ + "WHEN (OLD.%s IS NOT NULL) " \ + "BEGIN" \ + " UPDATE package_cert_index_info SET" \ + " cert_ref_count = cert_ref_count - 1" \ + " WHERE cert_id = OLD.%s;" \ + "END;" + __thread db_handle manifest_db; __thread db_handle cert_db; @@ -170,6 +181,13 @@ static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callb int _check_create_cert_db(sqlite3 *certdb) { + int i; + char buf[BUFSIZE]; + static const char *columns[] = { + "author_root_cert", "author_im_cert", "author_signer_cert", + "dist_root_cert", "dist_im_cert", "dist_signer_cert", + "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert", + NULL}; int ret = 0; ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL); if (ret < 0) @@ -181,6 +199,15 @@ int _check_create_cert_db(sqlite3 *certdb) if (ret < 0) return ret; ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, NULL, NULL); + + for (i = 0; columns[i] != NULL; i++) { + snprintf(buf, sizeof(buf), + QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT, + columns[i], columns[i], columns[i], columns[i]); + ret = __exec_db_query(certdb, buf, NULL, NULL); + if (ret < 0) + return ret; + } return ret; } static gid_t _get_gid(const char *name) -- 2.7.4