#define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
"CREATE TABLE IF NOT EXISTS package_cert_info( " \
" package TEXT PRIMARY KEY, " \
+ " package_count INTEGER, " \
" author_root_cert INTEGER, " \
" author_im_cert INTEGER, " \
" author_signer_cert INTEGER, " \
" dist2_im_cert INTEGER, " \
" dist2_signer_cert INTEGER)"
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
+ "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
+ "AFTER UPDATE ON package_cert_info " \
+ "WHEN (NEW.package_count = 0) " \
+ "BEGIN" \
+ " DELETE FROM package_cert_info WHERE package=OLD.package;" \
+ "END;"
+
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
+ "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
+ "AFTER UPDATE ON package_cert_info " \
+ "WHEN (NEW.package_count = OLD.package_count + 1) " \
+ "BEGIN" \
+ " UPDATE package_cert_index_info SET" \
+ " cert_ref_count = cert_ref_count - 1" \
+ " WHERE cert_id = OLD.author_root_cert" \
+ " OR cert_id = OLD.author_im_cert" \
+ " OR cert_id = OLD.author_signer_cert" \
+ " OR cert_id = OLD.dist_root_cert" \
+ " OR cert_id = OLD.dist_im_cert" \
+ " OR cert_id = OLD.dist_signer_cert" \
+ " OR cert_id = OLD.dist2_root_cert" \
+ " OR cert_id = OLD.dist2_im_cert" \
+ " OR cert_id = OLD.dist2_signer_cert;" \
+ "END;"
+
#define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
"CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
"AFTER DELETE ON package_cert_info " \
" 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;"
-
static int __insert_application_info(manifest_x *mfx);
static int __insert_application_appcategory_info(manifest_x *mfx);
static int __insert_application_appcontrol_info(manifest_x *mfx);
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);
_LOGD("package cert index info DB initialization failed\n");
return ret;
}
+ ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO);
+ if (ret == -1) {
+ _LOGD("package cert info DB initialization failed\n");
+ return ret;
+ }
+ ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2);
+ if (ret == -1) {
+ _LOGD("package cert info DB initialization failed\n");
+ return ret;
+ }
ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO);
if (ret == -1) {
_LOGD("package cert info DB initialization failed\n");
_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]);
- 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");
char *cert_info[])
{
static const char query_insert[] =
- "INSERT INTO package_cert_info (package,"
+ "INSERT INTO package_cert_info (package, package_count,"
" 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(?, "
+ "VALUES(?, 1,"
" (SELECT cert_id FROM package_cert_index_info"
" WHERE cert_info=?),"
" (SELECT cert_id FROM package_cert_index_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=?) "
+ "UPDATE package_cert_info "
+ "SET package_count = package_count + 1 "
"WHERE package=?";
int ret;
sqlite3_stmt *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);
+ sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC);
ret = sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
sqlite3_close_v2(db);
return PMINFO_R_ERROR;
}
+
if (_pkginfo_save_cert_info(db, pkgid, info->cert_info)) {
_LOGE("failed to save cert info, rollback now");
sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
static int _pkginfo_delete_certinfo(sqlite3 *db, const char *pkgid)
{
static const char query[] =
- "DELETE FROM package_cert_info WHERE package=?";
+ "UPDATE package_cert_info "
+ "SET package_count = package_count - 1 "
+ "WHERE package=?";
int ret;
sqlite3_stmt *stmt;
#define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
"CREATE TABLE IF NOT EXISTS package_cert_info( " \
" package TEXT PRIMARY KEY, " \
+ " package_count INTEGER, " \
" author_root_cert INTEGER, " \
" author_im_cert INTEGER, " \
" author_signer_cert INTEGER, " \
" dist2_im_cert INTEGER, " \
" dist2_signer_cert INTEGER)"
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO \
+ "CREATE TRIGGER IF NOT EXISTS update_cert_info " \
+ "AFTER UPDATE ON package_cert_info " \
+ "WHEN (NEW.package_count = 0) " \
+ "BEGIN" \
+ " DELETE FROM package_cert_info WHERE package=OLD.package;" \
+ "END;"
+
+#define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2 \
+ "CREATE TRIGGER IF NOT EXISTS update_cert_info2 " \
+ "AFTER UPDATE ON package_cert_info " \
+ "WHEN (NEW.package_count = OLD.package_count + 1) " \
+ "BEGIN" \
+ " UPDATE package_cert_index_info SET" \
+ " cert_ref_count = cert_ref_count - 1" \
+ " WHERE cert_id = OLD.author_root_cert" \
+ " OR cert_id = OLD.author_im_cert" \
+ " OR cert_id = OLD.author_signer_cert" \
+ " OR cert_id = OLD.dist_root_cert" \
+ " OR cert_id = OLD.dist_im_cert" \
+ " OR cert_id = OLD.dist_signer_cert" \
+ " OR cert_id = OLD.dist2_root_cert" \
+ " OR cert_id = OLD.dist2_im_cert" \
+ " OR cert_id = OLD.dist2_signer_cert;" \
+ "END;"
+
#define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
"CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
"AFTER DELETE ON package_cert_info " \
" 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;
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)
ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, NULL, NULL);
if (ret < 0)
return ret;
+ ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, NULL, NULL);
+ if (ret < 0)
+ return ret;
+ ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, NULL, NULL);
+ if (ret < 0)
+ return ret;
ret = __exec_db_query(certdb, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, NULL, NULL);
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]);
- ret = __exec_db_query(certdb, buf, NULL, NULL);
- if (ret < 0)
- return ret;
- }
return ret;
}
static gid_t _get_gid(const char *name)