Change codes for performance
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_db.c
index b92bfa2..0ead442 100644 (file)
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_private.h"
 #include "pkgmgr_parser.h"
-#include "pkgmgr_parser_internal.h"
-
-#define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
-       "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
-       " cert_info TEXT UNIQUE, " \
-       " cert_id INTEGER PRIMARY KEY, " \
-       " cert_ref_count INTEGER NOT NULL)"
-
-#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, " \
-       " dist_root_cert INTEGER, " \
-       " dist_im_cert INTEGER, " \
-       " dist_signer_cert INTEGER, " \
-       " dist2_root_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 " \
-       "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_UPDATE_CERT_INDEX_INFO \
-       "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
-       "AFTER UPDATE ON package_cert_index_info " \
-       "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
-       "       WHERE cert_id = OLD.cert_id) = 0) "\
-       "BEGIN" \
-       " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
-       "END;"
+#include "pkgmgr_parser_db.h"
 
 __thread db_handle manifest_db;
 __thread db_handle cert_db;
@@ -184,7 +112,7 @@ static char *_get_db_path(uid_t uid)
 static int __attach_and_create_view(sqlite3 *handle, const char *db, const char *tables[], uid_t uid)
 {
        int i;
-       char *err;
+       char *err = NULL;
        char query[MAX_QUERY_LEN];
 
        if (uid != GLOBAL_USER && uid != ROOT_UID) {
@@ -215,41 +143,11 @@ static int __attach_and_create_view(sqlite3 *handle, const char *db, const char
        return SQLITE_OK;
 }
 
-static int __exec_db_query(sqlite3 *db, char *query, sqlite_query_callback callback, void *data)
+int _check_create_cert_db(void)
 {
-       char *error_message = NULL;
-       int ret = sqlite3_exec(db, query, callback, data, &error_message);
-       if (SQLITE_OK != ret) {
-               _LOGE("Don't execute query = %s error message = %s   ret = %d\n", query,
-                      error_message, ret);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
+       return pkgmgr_parser_initialize_cert_db();
 }
 
-int _check_create_cert_db(sqlite3 *certdb)
-{
-       int ret = 0;
-       ret = __exec_db_query(certdb, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, NULL, NULL);
-       if (ret < 0)
-               return ret;
-       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);
-       return ret;
-}
 static gid_t _get_gid(const char *name)
 {
        char buf[BUFSIZE];
@@ -273,19 +171,16 @@ API const char *getIconPath(uid_t uid, bool readonly)
        gid_t gid = ROOT_UID;
 
        if (uid != GLOBAL_USER && uid != ROOT_UID) {
-               tzplatform_set_user(uid);
-               path = tzplatform_mkpath(TZ_USER_ICONS, "/");
-               gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
-               tzplatform_reset_user();
-       } else {
-               if (readonly)
-                       path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
-               else
-                       path = tzplatform_mkpath(TZ_SYS_RW_ICONS, "/");
+               _LOGD("not supported target user");
+               return NULL;
        }
 
+       if (readonly)
+               path = tzplatform_mkpath(TZ_SYS_RO_ICONS, "/");
+
        /* just allow certain users to create the icon directory if needed. */
-       if (uid_caller == ROOT_UID || uid_caller == uid)
+       if (path && (uid_caller == ROOT_UID  ||
+               uid_caller == APPFW_UID || uid_caller == uid))
                _mkdir_for_user(path, uid, gid);
 
        return path;
@@ -310,17 +205,17 @@ API char *getUserPkgParserDBPathUID(uid_t uid)
        }
        snprintf(pkgmgr_parser_db, sizeof(pkgmgr_parser_db),
                        "%s/.pkgmgr_parser.db", db_path);
-
-       if (uid != GLOBAL_USER && uid != ROOT_UID) {
-               tzplatform_set_user(uid);
-               gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
-               tzplatform_reset_user();
+       if (access(db_path, F_OK) != 0) {
+               if (uid != GLOBAL_USER && uid != ROOT_UID) {
+                       tzplatform_set_user(uid);
+                       gid = _get_gid(tzplatform_getenv(TZ_SYS_USER_GROUP));
+                       tzplatform_reset_user();
+               }
+               /* just allow certain users to create the dbspace directory if needed. */
+               if (uid_caller == ROOT_UID || uid_caller == APPFW_UID ||
+                               uid_caller == uid)
+                       _mkdir_for_user(db_path, uid, gid);
        }
-
-       // just allow certain users to create the dbspace directory if needed.
-       if (uid_caller == ROOT_UID || uid_caller == uid)
-               _mkdir_for_user(db_path, uid, gid);
-
        free(db_path);
 
        return strdup(pkgmgr_parser_db);
@@ -352,8 +247,8 @@ API char *getUserPkgCertDBPathUID(uid_t uid)
                tzplatform_reset_user();
        }
 
-       // just allow certain users to create the dbspace directory if needed.
-       if (uid_caller == ROOT_UID || uid_caller == uid)
+       /* just allow certain users to create the dbspace directory if needed. */
+       if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
                _mkdir_for_user(db_path, uid, gid);
 
        free(db_path);
@@ -380,7 +275,7 @@ API const char *getUserManifestPath(uid_t uid, bool readonly)
        }
 
        /* just allow certain users to create the icon directory if needed. */
-       if (uid_caller == ROOT_UID || uid_caller == uid)
+       if (uid_caller == ROOT_UID || uid_caller == APPFW_UID || uid_caller == uid)
                _mkdir_for_user(path, uid, gid);
 
        return path;
@@ -467,7 +362,7 @@ int __close_cert_db(void)
        if (cert_db.ref) {
                if (--cert_db.ref == 0)
                        sqlite3_close_v2(GET_DB(cert_db));
-                       return 0;
+               return 0;
        }
        _LOGE("Certificate DB is already closed !!\n");
        return -1;
@@ -531,7 +426,7 @@ void _save_column_int(sqlite3_stmt *stmt, int idx, int *i)
        *i = sqlite3_column_int(stmt, idx);
 }
 
-void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
+inline void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
 {
        const char *val;
 
@@ -540,173 +435,7 @@ void _save_column_str(sqlite3_stmt *stmt, int idx, char **str)
                *str = strdup(val);
 }
 
-API int pkgmgrinfo_pkginfo_set_state_enabled(const char *pkgid, bool enabled)
-{
-       /* Should be implemented later */
-       return 0;
-}
-
-API int pkgmgrinfo_appinfo_set_usr_state_enabled(const char *appid, bool enabled, uid_t uid)
-{
-       int ret;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char *error_message;
-
-       retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
-
-       /* Open db.*/
-       ret = __open_manifest_db(uid, false);
-       if (ret != SQLITE_OK) {
-               _LOGE("connect db [%d] failed!\n", uid);
-               return PMINFO_R_ERROR;
-       }
-
-       /*Begin transaction*/
-       ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
-       if (ret != SQLITE_OK) {
-               _LOGE("Failed to begin transaction\n");
-               __close_manifest_db();
-               return PMINFO_R_ERROR;
-       }
-       _LOGD("Transaction Begin\n");
-
-       memset(query, '\0', MAX_QUERY_LEN);
-       snprintf(query, MAX_QUERY_LEN,
-               "update package_app_info set app_enabled='%s' where app_id='%s'",
-               enabled ? "true" : "false", appid);
-
-       if (SQLITE_OK !=
-           sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
-               _LOGE("Don't execute query = %s error message = %s\n", query,
-                      error_message);
-               sqlite3_free(error_message);
-               return PMINFO_R_ERROR;
-       }
-       sqlite3_free(error_message);
-
-       /*Commit transaction*/
-       ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
-       if (ret != SQLITE_OK) {
-               _LOGE("Failed to commit transaction. Rollback now\n");
-               sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
-               __close_manifest_db();
-               return PMINFO_R_ERROR;
-       }
-       _LOGD("Transaction Commit and End\n");
-       __close_manifest_db();
-       return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_appinfo_set_state_enabled(const char *appid, bool enabled)
-{
-       return pkgmgrinfo_appinfo_set_usr_state_enabled(appid, enabled, _getuid());
-}
-
-API int pkgmgrinfo_appinfo_set_usr_default_label(const char *appid, const char *label, uid_t uid)
-{
-       int ret;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char *error_message;
-
-       retvm_if(appid == NULL, PMINFO_R_EINVAL, "appid is NULL\n");
-
-       ret = __open_manifest_db(uid, false);
-       if (ret == -1) {
-               _LOGE("Fail to open manifest DB\n");
-               return PMINFO_R_ERROR;
-       }
-
-       /*Begin transaction*/
-       ret = sqlite3_exec(GET_DB(manifest_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
-       if (ret != SQLITE_OK) {
-               _LOGE("Failed to begin transaction\n");
-               __close_manifest_db();
-               return PMINFO_R_ERROR;
-       }
-       _LOGD("Transaction Begin\n");
-
-       memset(query, '\0', MAX_QUERY_LEN);
-       snprintf(query, MAX_QUERY_LEN,
-               "update package_app_localized_info set app_label='%s' where app_id='%s' and app_locale='No Locale'", label, appid);
-
-       if (SQLITE_OK !=
-           sqlite3_exec(GET_DB(manifest_db), query, NULL, NULL, &error_message)) {
-               _LOGE("Don't execute query = %s error message = %s\n", query,
-                      error_message);
-               sqlite3_free(error_message);
-               return PMINFO_R_ERROR;
-       }
-
-       /*Commit transaction*/
-       ret = sqlite3_exec(GET_DB(manifest_db), "COMMIT", NULL, NULL, NULL);
-       if (ret != SQLITE_OK) {
-               _LOGE("Failed to commit transaction. Rollback now\n");
-               sqlite3_exec(GET_DB(manifest_db), "ROLLBACK", NULL, NULL, NULL);
-               __close_manifest_db();
-               return PMINFO_R_ERROR;
-       }
-       _LOGD("Transaction Commit and End\n");
-       __close_manifest_db();
-       return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_appinfo_set_default_label(const char *appid, const char *label)
-{
-       return pkgmgrinfo_appinfo_set_usr_default_label(appid, label, _getuid());
-}
-
-API int pkgmgrinfo_appinfo_set_usr_guestmode_visibility(pkgmgrinfo_appinfo_h handle, uid_t uid, bool status)
-{
-       const char *val;
-       int ret;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char *errmsg;
-       sqlite3 *pkgmgr_parser_db;
-       char *db_path;
-
-       retvm_if(handle == NULL, PMINFO_R_EINVAL, "appinfo handle is NULL\n");
-
-       pkgmgr_appinfo_x *info = (pkgmgr_appinfo_x *)handle;
-       val = info->app_info->guestmode_visibility;
-       if (val) {
-               db_path = getUserPkgParserDBPathUID(uid);
-               if (db_path == NULL) {
-                       _LOGE("Failed to get pkg parser db path - %d", uid);
-                       return PMINFO_R_ERROR;
-               }
-
-               ret = db_util_open_with_options(db_path, &pkgmgr_parser_db,
-                               SQLITE_OPEN_READWRITE, NULL);
-               if (ret != SQLITE_OK) {
-                       _LOGE("DB Open Failed\n");
-                       free(db_path);
-                       return PMINFO_R_ERROR;
-               }
-               free(db_path);
-
-               /*TODO: Write to DB here*/
-               if (status == true)
-                       snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'true' where app_id = '%s'", (char *)info->app_info->appid);
-               else
-                       snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_guestmodevisibility = 'false' where app_id = '%s'", (char *)info->app_info->appid);
-
-               ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &errmsg);
-               sqlite3_close(pkgmgr_parser_db);
-               if (ret != SQLITE_OK) {
-                       _LOGE("DB update [%s] failed, error message = %s\n", query, errmsg);
-                       free(errmsg);
-                       return PMINFO_R_ERROR;
-               }
-       }
-       return PMINFO_R_OK;
-}
-
-API int pkgmgrinfo_appinfo_set_guestmode_visibility(pkgmgrinfo_appinfo_h handle, bool status)
-{
-       return pkgmgrinfo_appinfo_set_usr_guestmode_visibility(handle, _getuid(), status);
-}
-
-API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, uid_t uid)
+API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path, uid_t uid)
 {
        retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "pkgid is NULL\n");
        int ret = -1;
@@ -737,8 +466,8 @@ API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_
 
        /* pkgcakge_info table */
        query = sqlite3_mprintf(
-                       "update package_info set installed_storage=%Q where package=%Q",
-                       location ? "installed_external" : "installed_internal", pkgid);
+                       "update package_info set installed_storage=%Q, external_path=%Q where package=%Q",
+                       location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
 
        ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
        tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
@@ -746,8 +475,8 @@ API int pkgmgrinfo_pkginfo_set_usr_installed_storage(const char *pkgid, INSTALL_
 
        /* package_app_info table */
        query = sqlite3_mprintf(
-                       "update package_app_info set app_installed_storage=%Q where package=%Q",
-                       location ? "installed_external" : "installed_internal", pkgid);
+                       "update package_app_info set app_installed_storage=%Q, app_external_path=%Q where package=%Q",
+                       location ? "installed_external" : "installed_internal", external_pkg_path, pkgid);
 
        ret = sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, NULL);
        tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
@@ -768,7 +497,7 @@ catch:
        return ret;
 }
 
-API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location)
+API int pkgmgrinfo_pkginfo_set_installed_storage(const char *pkgid, INSTALL_LOCATION location, const char *external_pkg_path)
 {
-       return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, _getuid());
+       return pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid, location, external_pkg_path, _getuid());
 }