X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=parser%2Fsrc%2Fpkgmgr_parser_db.c;h=b6db5238f24d2d3db4ada0e8e187dffe4fe62760;hb=aa88d97f98ae9093f07947ec8397e15e07e1dac3;hp=934133a3b85c00306f13bcb5f0263b9231309608;hpb=d407f49256d91e76839cc6e829886bed13e05f7b;p=platform%2Fcore%2Fappfw%2Fpkgmgr-info.git diff --git a/parser/src/pkgmgr_parser_db.c b/parser/src/pkgmgr_parser_db.c index 934133a..b6db523 100644 --- a/parser/src/pkgmgr_parser_db.c +++ b/parser/src/pkgmgr_parser_db.c @@ -1,10 +1,5 @@ /* - * pkgmgr-info - * - * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. - * - * Contact: Jayoun Lee , Sewook Park , - * Jaeho Lee , Shobhit Srivastava + * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,37 +15,42 @@ * */ -#define _GNU_SOURCE +#include #include -#include +#include #include #include -#include #include -#include +#include +#include #include -#include #include -#include #include -#include +#include +#include -/* For multi-user support */ #include +#include #include "pkgmgr-info.h" #include "pkgmgrinfo_basic.h" -#include "pkgmgr_parser_internal.h" -#include "pkgmgr_parser_db.h" +#include "pkgmgr_parser.h" +#include "pkgmgr_parser_db_queries.h" #include "pkgmgr_parser_debug.h" +#include "pkgmgr_parser_internal.h" -#define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db") -#define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db") -#define MAX_QUERY_LEN 4096 -#define BUFSIZE 4096 +#ifndef OWNER_ROOT #define OWNER_ROOT 0 -#define MAX_INTEGER_LENGTH 10 +#endif +#ifndef GLOBAL_USER +#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) +#endif +#ifndef APPFW_USER +#define APPFW_USER "app_fw" +#endif + +#define BUFSIZE 4096 #define LDPI "ldpi" #define MDPI "mdpi" @@ -69,344 +69,29 @@ #define XXHDPI_MIN 481 #define XXHDPI_MAX 600 -#define DB_LABEL "User::Home" -#define SET_SMACK_LABEL(x) \ -do { \ - if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \ - _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \ - else \ - _LOGD("chsmack -a %s %s", DB_LABEL, x); \ -} while (0) - -sqlite3 *pkgmgr_parser_db; -sqlite3 *pkgmgr_cert_db; - - -#define QUERY_CREATE_TABLE_PACKAGE_INFO "CREATE TABLE IF NOT EXISTS package_info " \ - "(package TEXT PRIMARY KEY NOT NULL, " \ - "package_type TEXT DEFAULT 'tpk', " \ - "package_version TEXT, " \ - "package_api_version TEXT, " \ - "package_tep_name TEXT, " \ - "package_zip_mount_file TEXT, " \ - "install_location TEXT NOT NULL , " \ - "package_size TEXT, " \ - "package_removable TEXT NOT NULL DEFAULT 'true', " \ - "package_preload TEXT NOT NULL DEFAULT 'false', " \ - "package_readonly TEXT NOT NULL DEFAULT 'false', " \ - "package_update TEXT NOT NULL DEFAULT 'false', " \ - "package_appsetting TEXT NOT NULL DEFAULT 'false', " \ - "package_nodisplay TEXT NOT NULL DEFAULT 'false', " \ - "package_system TEXT NOT NULL DEFAULT 'false', " \ - "author_name TEXT, " \ - "author_email TEXT, " \ - "author_href TEXT," \ - "installed_time TEXT, " \ - "installed_storage TEXT, " \ - "storeclient_id TEXT, " \ - "mainapp_id TEXT, " \ - "package_url TEXT, " \ - "root_path TEXT, " \ - "external_path TEXT, " \ - "csc_path TEXT, " \ - "package_support_mode TEXT, " \ - "package_support_disable TEXT NOT NULL DEFAULT 'false', " \ - "package_disable TEXT NOT NULL DEFAULT 'false')" - -#define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "CREATE TABLE IF NOT EXISTS package_localized_info " \ - "(package TEXT NOT NULL, " \ - "package_locale TEXT NOT NULL DEFAULT 'No Locale', " \ - "package_label TEXT, " \ - "package_icon TEXT, " \ - "package_description TEXT, " \ - "package_license TEXT, " \ - "package_author TEXT, " \ - "PRIMARY KEY(package, package_locale), " \ - "FOREIGN KEY(package) " \ - "REFERENCES package_info(package) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "CREATE TABLE IF NOT EXISTS package_privilege_info " \ - "(package TEXT NOT NULL, " \ - "privilege TEXT NOT NULL, " \ - "type TEXT NOT NULL, " \ - "PRIMARY KEY(package, privilege, type) " \ - "FOREIGN KEY(package) " \ - "REFERENCES package_info(package) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "CREATE TABLE IF NOT EXISTS package_app_info " \ - "(app_id TEXT PRIMARY KEY NOT NULL, " \ - "app_component TEXT NOT NULL, " \ - "app_exec TEXT, " \ - "app_nodisplay TEXT NOT NULL DEFAULT 'false', " \ - "app_type TEXT, " \ - "app_onboot TEXT NOT NULL DEFAULT 'false', " \ - "app_multiple TEXT NOT NULL DEFAULT 'false', " \ - "app_autorestart TEXT NOT NULL DEFAULT 'false', " \ - "app_taskmanage TEXT NOT NULL DEFAULT 'false', " \ - "app_enabled TEXT NOT NULL DEFAULT 'true', " \ - "app_hwacceleration TEXT NOT NULL DEFAULT 'use-system-setting', " \ - "app_screenreader TEXT NOT NULL DEFAULT 'use-system-setting', " \ - "app_mainapp TEXT NOT NULL, " \ - "app_recentimage TEXT NOT NULL DEFAULT 'false', " \ - "app_launchcondition TEXT, " \ - "app_indicatordisplay TEXT NOT NULL DEFAULT 'true', " \ - "app_portraitimg TEXT, " \ - "app_landscapeimg TEXT, " \ - "app_guestmodevisibility TEXT NOT NULL DEFAULT 'true', " \ - "app_permissiontype TEXT NOT NULL DEFAULT 'normal', " \ - "app_preload TEXT NOT NULL DEFAULT 'false', " \ - "app_submode TEXT NOT NULL DEFAULT 'false', " \ - "app_submode_mainid TEXT, " \ - "app_installed_storage TEXT NOT NULL, " \ - "app_process_pool TEXT NOT NULL DEFAULT 'false', " \ - "app_launch_mode TEXT NOT NULL DEFAULT 'caller', " \ - "app_ui_gadget TEXT NOT NULL DEFAULT 'false', " \ - "app_support_mode TEXT, " \ - "app_support_disable TEXT NOT NULL DEFAULT 'false', " \ - "app_disable TEXT NOT NULL DEFAULT 'false', " \ - "app_package_type TEXT DEFAULT 'tpk', " \ - "app_package_system TEXT NOT NULL, " \ - "app_removable TEXT NOT NULL DEFAULT 'false', " \ - "app_package_installed_time TEXT, " \ - "component_type TEXT, " \ - "package TEXT NOT NULL, " \ - "app_tep_name TEXT, " \ - "app_zip_mount_file TEXT, " \ - "app_background_category INTEGER DEFAULT 0, " \ - "app_root_path TEXT, " \ - "app_api_version TEXT, " \ - "app_effective_appid TEXT, " \ - "app_splash_screen_display TEXT NOT NULL DEFAULT 'true', " \ - "app_external_path TEXT, " \ - "FOREIGN KEY(package) " \ - "REFERENCES package_info(package) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "CREATE TABLE IF NOT EXISTS package_app_localized_info " \ - "(app_id TEXT NOT NULL, " \ - "app_locale TEXT NOT NULL DEFAULT 'No Locale', " \ - "app_label TEXT, " \ - "app_icon TEXT, " \ - "PRIMARY KEY(app_id,app_locale) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "CREATE TABLE IF NOT EXISTS package_app_icon_section_info " \ - "(app_id TEXT NOT NULL, " \ - "app_icon TEXT, " \ - "app_icon_section TEXT NOT NULL, " \ - "app_icon_resolution TEXT NOT NULL, " \ - "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "CREATE TABLE IF NOT EXISTS package_app_image_info " \ - "(app_id TEXT NOT NULL, " \ - "app_locale TEXT DEFAULT 'No Locale', " \ - "app_image_section TEXT NOT NULL, " \ - "app_image TEXT, " \ - "PRIMARY KEY(app_id,app_image_section) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "CREATE TABLE IF NOT EXISTS package_app_app_control " \ - "(app_id TEXT NOT NULL, " \ - "app_control TEXT NOT NULL, " \ - "PRIMARY KEY(app_id,app_control) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "CREATE TABLE IF NOT EXISTS package_app_app_category " \ - "(app_id TEXT NOT NULL, " \ - "category TEXT NOT NULL, " \ - "PRIMARY KEY(app_id,category) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "CREATE TABLE IF NOT EXISTS package_app_app_metadata " \ - "(app_id TEXT NOT NULL, " \ - "md_key TEXT NOT NULL, " \ - "md_value TEXT, " \ - "PRIMARY KEY(app_id, md_key, md_value) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "CREATE TABLE IF NOT EXISTS package_app_app_permission " \ - "(app_id TEXT NOT NULL, " \ - "pm_type TEXT NOT NULL, " \ - "pm_value TEXT NOT NULL, " \ - "PRIMARY KEY(app_id, pm_type, pm_value) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "CREATE TABLE IF NOT EXISTS package_app_share_allowed " \ - "(app_id TEXT NOT NULL, " \ - "data_share_path TEXT NOT NULL, " \ - "data_share_allowed TEXT NOT NULL, " \ - "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "CREATE TABLE IF NOT EXISTS package_app_share_request " \ - "(app_id TEXT NOT NULL, " \ - "data_share_request TEXT NOT NULL, " \ - "PRIMARY KEY(app_id,data_share_request) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#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, " \ - "access TEXT NOT NULL, " \ - "type TEXT NOT NULL, " \ - "PRIMARY KEY(app_id, providerid, access, type) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID "CREATE TABLE IF NOT EXISTS package_app_info_for_uid " \ - "(app_id TEXT NOT NULL, " \ - "uid INTEGER NOT NULL, " \ - "is_disabled TEXT NOT NULL DEFAULT 'false', " \ - "is_splash_screen_enabled TEXT NOT NULL, " \ - "PRIMARY KEY(app_id, uid))" - -#define QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID \ - "CREATE TRIGGER IF NOT EXISTS update_package_appinfo_for_uid "\ - "AFTER UPDATE ON package_app_info_for_uid " \ - "BEGIN" \ - " DELETE FROM package_app_info_for_uid WHERE " \ - " is_splash_screen_enabled=" \ - " (SELECT package_app_info.app_splash_screen_display FROM " \ - " package_app_info, package_app_info_for_uid WHERE " \ - " package_app_info.app_id=OLD.app_id) AND is_disabled='false';" \ - "END;" - -#define QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN \ - "CREATE TABLE IF NOT EXISTS package_app_splash_screen " \ - "(app_id TEXT NOT NULL, " \ - "src TEXT NOT NULL, " \ - "type TEXT NOT NULL, " \ - "orientation TEXT NOT NULL, " \ - "indicatordisplay TEXT, " \ - "operation TEXT NOT NULL, " \ - "color_depth TEXT NOT NULL DEFAULT '24', " \ - "PRIMARY KEY(app_id, orientation, operation) " \ - "FOREIGN KEY(app_id) " \ - "REFERENCES package_app_info(app_id) " \ - "ON DELETE CASCADE)" - - -/* FIXME: duplicated at pkgmgrinfo_db.c */ -#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;" - -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); -static int __insert_application_appmetadata_info(manifest_x *mfx); -static int __insert_application_share_allowed_info(manifest_x *mfx); -static int __insert_application_share_request_info(manifest_x *mfx); -static int __insert_application_datacontrol_info(manifest_x *mfx); -static void __insert_application_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, 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); -static int __initialize_db(sqlite3 *db_handle, const char *db_query); -static int __exec_query(char *query); -static void __extract_data(gpointer data, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath, - char **label, char **license, char **icon, char **description, char **author); -static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata); -static GList *__create_locale_list(GList *locale, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath); -static void __preserve_guestmode_visibility_value(manifest_x *mfx); -static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname); -static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path); -static int __parserdb_change_perm(const char *db_file, uid_t uid); +/* app background category value */ +#define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000 +#define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL 0x00001 +#define APP_BG_CATEGORY_MEDIA_VAL 0x00002 +#define APP_BG_CATEGORY_DOWNLOAD_VAL 0x00004 +#define APP_BG_CATEGORY_BGNETWORK_VAL 0x00008 +#define APP_BG_CATEGORY_LOCATION_VAL 0x00010 +#define APP_BG_CATEGORY_SENSOR_VAL 0x00020 +#define APP_BG_CATEGORY_IOTCOMM_VAL 0x00040 +#define APP_BG_CATEGORY_SYSTEM_VAL 0x00080 + +#define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable" +#define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR "disable" +#define APP_BG_CATEGORY_MEDIA_STR "media" +#define APP_BG_CATEGORY_DOWNLOAD_STR "download" +#define APP_BG_CATEGORY_BGNETWORK_STR "background-network" +#define APP_BG_CATEGORY_LOCATION_STR "location" +#define APP_BG_CATEGORY_SENSOR_STR "sensor" +#define APP_BG_CATEGORY_IOTCOMM_STR "iot-communication" +#define APP_BG_CATEGORY_SYSTEM "system" #define REGULAR_USER 5000 -static inline uid_t _getuid(void) +static inline uid_t __getuid(void) { uid_t uid = getuid(); @@ -416,264 +101,916 @@ static inline uid_t _getuid(void) return uid; } -static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname) +static const char *__get_bool(char *value, bool is_true) { - if (coltxt[0]) - __delete_subpkg_info_from_db(coltxt[0]); + if (value != NULL) { + if (!strcmp(value, "")) + return (is_true) ? "true" : "false"; + return value; + } - return 0; + return (is_true) ? "true" : "false"; +} + +#define __BEGIN_TRANSACTION(db) \ +do { \ + if (sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL) != \ + SQLITE_OK) { \ + _LOGE("begin transaction failed: %s", sqlite3_errmsg(db)); \ + sqlite3_close_v2(db); \ + return PM_PARSER_R_ERROR; \ + } \ +} while (0) \ + +#define __DO_TRANSACTION(db, func) \ +do { \ + if (func) { \ + _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \ + if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) != \ + SQLITE_OK) \ + _LOGE("roll back transaction failed: %s", \ + sqlite3_errmsg(db)); \ + sqlite3_close_v2(db); \ + return PM_PARSER_R_ERROR; \ + } \ +} while (0) \ + +#define __END_TRANSACTION(db) \ +do { \ + if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != \ + SQLITE_OK) { \ + _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db)); \ + if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) != \ + SQLITE_OK) \ + _LOGE("roll back transaction failed: %s", \ + sqlite3_errmsg(db)); \ + sqlite3_close_v2(db); \ + return PM_PARSER_R_ERROR; \ + } \ +} while (0) \ + +#define __BIND_TEXT(db, stmt, i, text) \ +do { \ + if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\ + _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db)); \ + sqlite3_finalize(stmt); \ + return -1; \ + } \ +} while (0) + +#define __BIND_INT(db, stmt, i, int) \ +do { \ + if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) { \ + _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db)); \ + sqlite3_finalize(stmt); \ + return -1; \ + } \ +} while (0) + +static const char *__get_parser_db_path(uid_t uid) +{ + char buf[PATH_MAX]; + const char *path; + + if (uid == GLOBAL_USER || uid == OWNER_ROOT) { + path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db"); + } else { + snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid); + path = tzplatform_mkpath(TZ_SYS_DB, buf); + } + + return path; +} + +static const char *__get_cert_db_path(void) +{ + return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db"); } -static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path) +#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt" +static int __set_db_version(sqlite3 *db) { - int ret = -1; - sqlite3 *handle; + int ret; + FILE *fp = NULL; + char version[PKG_STRING_LEN_MAX] = { 0 }; char *query = NULL; - char *error_message = NULL; - ret = db_util_open(db_path, &handle, DB_UTIL_REGISTER_HOOK_METHOD); - if (ret != SQLITE_OK) { - _LOGD("connect db [%s] failed!\n", db_path); + fp = fopen(DB_VERSION_PATH, "r"); + retvm_if(fp == NULL, -1, "Failed to open db version file"); + if (fgets(version, sizeof(version), fp) == NULL) { + _LOGE("Failed to get version information"); + fclose(fp); + return -1; + } + fclose(fp); + + query = sqlite3_mprintf("PRAGMA user_version=%Q", version); + if (!query) { + _LOGE("Out of memory"); return -1; } - *db_handle = handle; - /* add user_version for db upgrade*/ - query = sqlite3_mprintf("PRAGMA user_version=%d", (atoi(TIZEN_MAJOR_VER) * 10000 + atoi(TIZEN_MINOR_VER) * 100 + atoi(TIZEN_PATCH_VER))); - if (SQLITE_OK != - sqlite3_exec(handle, query, NULL, NULL, &error_message)) { - _LOGE("Don't execute query = %s error message = %s\n", query, - error_message); + ret = sqlite3_exec(db, query, NULL, NULL, NULL); + if (ret != SQLITE_OK) { + _LOGE("exec failed: %s", sqlite3_errmsg(db)); + sqlite3_free(query); + return -1; } - sqlite3_free(error_message); sqlite3_free(query); return 0; } -static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname) +/* TODO: Do not labeling directly */ +#define DB_LABEL "User::Home" +#define SET_SMACK_LABEL(x) \ +do { \ + if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \ + _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \ + else \ + _LOGD("chsmack -a %s %s", DB_LABEL, x); \ +} while (0) + +static int __set_db_permission(const char *path, uid_t uid) { - manifest_x *mfx = (manifest_x *)data; - int i = 0; - char *appid = NULL; - char *status = NULL; - application_x *app; - GList *tmp; - if (mfx->application == NULL) - return -1; - app = (application_x *)mfx->application->data; - for (i = 0; i < ncols; i++) { - if (strcmp(colname[i], "app_id") == 0) { - if (coltxt[i]) - appid = strdup(coltxt[i]); - } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) { - if (coltxt[i]) - status = strdup(coltxt[i]); + int fd; + const char *files[2]; + char journal_file[BUFSIZE]; + struct stat sb; + mode_t mode; + struct passwd pwd; + struct passwd *result; + char buf[BUFSIZE]; + int ret; + int i; + + if (getuid() != OWNER_ROOT) + return 0; + + if (uid == OWNER_ROOT || uid == GLOBAL_USER) { + ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result); + if (result == NULL) { + if (ret == 0) + _LOGE("no such user: %d", uid); + else + _LOGE("getpwuid_r failed: %d", errno); + return -1; } + uid = pwd.pw_uid; } - if (appid == NULL) { - if (status != NULL) - free(status); - _LOGD("app id is NULL\n"); + + snprintf(journal_file, sizeof(journal_file), "%s-journal", path); + files[0] = path; + files[1] = journal_file; + + ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result); + if (result == NULL) { + if (ret == 0) + _LOGE("no such user: %d", uid); + else + _LOGE("getpwuid_r failed: %d", errno); return -1; } - /*update guest mode visibility*/ - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) - continue; - if (strcmp(app->appid, appid) == 0) { - free((void *)app->guestmode_visibility); - app->guestmode_visibility = strdup(status); - break; + + for (i = 0; i < 2; i++) { + fd = open(files[i], O_RDONLY); + if (fd == -1) { + _LOGE("open %s failed: %d", files[i], errno); + return -1; } - } - if (appid) { - free(appid); - appid = NULL; - } - if (status) { - free(status); - status = NULL; + ret = fstat(fd, &sb); + if (ret == -1) { + _LOGE("stat %s failed: %d", files[i], errno); + close(fd); + return -1; + } + if (S_ISLNK(sb.st_mode)) { + _LOGE("%s is symlink!", files[i]); + close(fd); + return -1; + } + ret = fchown(fd, uid, pwd.pw_gid); + if (ret == -1) { + _LOGE("fchown %s failed: %d", files[i], errno); + close(fd); + return -1; + } + + mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; + if (!strcmp(path, __get_cert_db_path())) + mode |= S_IWOTH; + ret = fchmod(fd, mode); + if (ret == -1) { + _LOGD("fchmod %s failed: %d", files[i], errno); + close(fd); + return -1; + } + close(fd); + SET_SMACK_LABEL(files[i]); } return 0; } -static void __preserve_guestmode_visibility_value(manifest_x *mfx) +static const char *parser_init_queries[] = { + QUERY_CREATE_TABLE_PACKAGE_INFO, + QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO, + QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO, + QUERY_CREATE_TABLE_PACKAGE_APPDEFINED_PRIVILEGE_INFO, + QUERY_CREATE_TABLE_PACKAGE_UPDATE_INFO, + QUERY_CREATE_TABLE_PACKAGE_APP_INFO, + QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO, + QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL, + QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL_PRIVILEGE, + QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY, + QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA, + QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL, + QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL_PRIVILEGE, + QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID, + QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID, + QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN, + QUERY_CREATE_TABLE_PACKAGE_DEPENDENCY_INFO, + QUERY_CREATE_TABLE_PACKAGE_PLUGIN_INFO, + NULL, +}; + +static const char *cert_init_queries[] = { + QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, + QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, + QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO, + QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2, + QUERY_CREATE_TRIGGER_DELETE_CERT_INFO, + QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO, + NULL +}; + +static int __create_tables(sqlite3 *db, const char **queries) { - char *error_message = NULL; - char query[MAX_QUERY_LEN] = {'\0'}; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "SELECT app_id, app_guestmodevisibility FROM package_app_info " \ - "WHERE package=%Q", mfx->package); - if (SQLITE_OK != - sqlite3_exec(pkgmgr_parser_db, query, - __guestmode_visibility_cb, (void *)mfx, &error_message)) { - _LOGD("Don't execute query = %s error message = %s\n", - query, error_message); - sqlite3_free(error_message); + int ret; + int i; + for (i = 0; queries[i] != NULL; i++) { + ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL); + if (ret != SQLITE_OK) { + _LOGE("exec failed: %s", sqlite3_errmsg(db)); + return -1; + } } - return; + return 0; } -static int __initialize_db(sqlite3 *db_handle, const char *db_query) +static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid) { - char *error_message = NULL; - if (SQLITE_OK != - sqlite3_exec(db_handle, db_query, - NULL, NULL, &error_message)) { - _LOGD("Don't execute query = %s error message = %s\n", - db_query, error_message); - sqlite3_free(error_message); + const char **queries; + + if (__set_db_version(db)) return -1; - } - sqlite3_free(error_message); - return 0; -} -static int __exec_query(char *query) -{ - char *error_message = NULL; - if (SQLITE_OK != - sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) { - _LOGE("Don't execute query = %s error message = %s\n", query, - error_message); - sqlite3_free(error_message); + if (strstr(dbpath, ".pkgmgr_parser.db")) { + queries = parser_init_queries; + } else if (strstr(dbpath, ".pkgmgr_cert.db")) { + queries = cert_init_queries; + } else { + _LOGE("unexpected dbpath: %s", dbpath); return -1; } - sqlite3_free(error_message); + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __create_tables(db, queries)); + __END_TRANSACTION(db); + + if (__set_db_permission(dbpath, uid)) + _LOGE("failed to set db permission"); + return 0; } -static GList *__create_locale_list(GList *locale, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths) +#define RESOURCED_BUS_NAME "org.tizen.resourced" +#define RESOURCED_PROC_PATH "/Org/Tizen/ResourceD/Process" +#define RESOURCED_PROC_INTERFACE "org.tizen.resourced.process" +#define RESOURCED_PROC_METHOD "ProcExclude" +static void __send_wakeup_signal_to_resourced(pid_t pid) { - GList *tmp; - label_x *lbl; - license_x *lcn; - icon_x *icn; - description_x *dcn; - author_x *ath; - for (tmp = lbls; tmp; tmp = tmp->next) { - lbl = (label_x *)tmp->data; - if (lbl == NULL) - continue; - if (lbl->lang) - locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL); + GError *error = NULL; + GDBusConnection *conn; + GDBusProxy *proxy; + GVariant *reply; + + conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (conn == NULL) { + _LOGE("Failed to connect to dbus: %s", error->message); + g_error_free(error); + return; } - for (tmp = lcns; tmp; tmp = tmp->next) { - lcn = (license_x *)tmp->data; - if (lcn == NULL) - continue; - if (lcn->lang) - locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL); + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, + NULL, RESOURCED_BUS_NAME, + RESOURCED_PROC_PATH, RESOURCED_PROC_INTERFACE, + NULL, &error); + if (proxy == NULL) { + _LOGE("failed to get proxy object: %s", error->message); + g_error_free(error); + g_object_unref(conn); + return; } - for (tmp = icns; tmp; tmp = tmp->next) { - icn = (icon_x *)tmp->data; - if (icn == NULL) - continue; - if (icn->lang) - locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL); + + reply = g_dbus_proxy_call_sync(proxy, RESOURCED_PROC_METHOD, + g_variant_new("(si)", "wakeup", pid), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (reply == NULL) + _LOGE("failed to get reply from resourced"); + if (error) { + _LOGE("failed to send request: %s", error->message); + g_error_free(error); } - for (tmp = dcns; tmp; tmp = tmp->next) { - dcn = (description_x *)tmp->data; - if (dcn == NULL) - continue; - if (dcn->lang) - locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL); + + g_object_unref(proxy); + g_object_unref(conn); +} + +static void __check_db_lock(const char *dbpath) +{ + FILE *fp; + FILE *fp_cmdline; + struct stat sb; + int pid; + unsigned int maj; + unsigned int min; + ino_t ino; + char cmdline[BUFSIZE]; + char name[BUFSIZE]; + size_t len; + + if (stat(dbpath, &sb) == -1) { + _LOGE("get db file(%s) status failed: %d", dbpath, errno); + return; } - for (tmp = aths; tmp; tmp = tmp->next) { - ath = (author_x *)tmp->data; - if (ath == NULL) + + fp = fopen("/proc/locks", "r"); + if (fp == NULL) { + _LOGE("Failed to open lock info: %d", errno); + return; + } + + while (fscanf(fp, "%*s %*s %*s %*s %d %x:%x:%lu %*s %*s", + &pid, &maj, &min, &ino) != EOF) { + if (maj != major(sb.st_dev) || min != minor(sb.st_dev) || + ino != sb.st_ino || pid == getpid()) continue; - if (ath->lang) - locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL); + + snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid); + fp_cmdline = fopen(cmdline, "r"); + name[0] = '\0'; + if (fp_cmdline != NULL) { + len = fread(name, sizeof(char), sizeof(name) - 1, + fp_cmdline); + if (len > 0) { + if (name[len - 1] == '\n') + name[len - 1] = '\0'; + else + name[len] = '\0'; + } + fclose(fp_cmdline); + } + + _LOGE("%s (%d) has lock on pkgmgr db(%s)!", name, pid, dbpath); + __send_wakeup_signal_to_resourced(pid); } - return locale; + fclose(fp); } -static GList *__create_icon_list(GList *appicon, GList *icns) +#define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */ +#define BUSY_WAITING_MAX 40 /* wait for max 2 sec */ +static int __db_busy_handler(void *data, int count) { - GList *tmp; - icon_x *icn; - - for (tmp = icns; tmp; tmp = tmp->next) { - icn = (icon_x *)tmp->data; - if (icn == NULL) - continue; - if (icn->section) - appicon = g_list_insert_sorted_with_data(appicon, (gpointer)icn->section, __comparefunc, NULL); + if (count < (BUSY_WAITING_MAX / 2)) { + usleep(BUSY_WAITING_USEC); + return 1; + } else if (count == (BUSY_WAITING_MAX / 2)) { + __check_db_lock((const char *)data); + usleep(BUSY_WAITING_USEC); + return 1; + } else if (count < BUSY_WAITING_MAX) { + usleep(BUSY_WAITING_USEC); + return 1; + } else { + /* sqlite3_prepare_v2 will return SQLITE_BUSY */ + return 0; } - return appicon; } -static GList *__create_image_list(GList *appimage, GList *imgs) +API int pkgmgr_parser_initialize_parser_db(uid_t uid) { - GList *tmp; - image_x *img; + int ret; + const char *dbpath; + sqlite3 *db; - for (tmp = imgs; tmp; tmp = tmp->next) { - img = (image_x *)tmp->data; - if (img == NULL) - continue; - if (img->section) - appimage = g_list_insert_sorted_with_data(appimage, (gpointer)img->section, __comparefunc, NULL); + dbpath = __get_parser_db_path(uid); + if (access(dbpath, F_OK) != -1) { + _LOGE("Manifest db for user %d is already exists", uid); + return PM_PARSER_R_ERROR; + } + + ret = sqlite3_open_v2(dbpath, &db, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - return appimage; + + ret = sqlite3_busy_handler(db, __db_busy_handler, NULL); + if (ret != SQLITE_OK) { + _LOGE("failed to register busy handler: %s", + sqlite3_errmsg(db)); + sqlite3_close_v2(db); + return ret; + } + + if (__initialize_db(db, dbpath, uid)) { + sqlite3_close_v2(db); + return PM_PARSER_R_ERROR; + } + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static void __trimfunc(GList *trim_list) +API int pkgmgr_parser_initialize_cert_db(void) { - char *trim_data = NULL; - char *prev = NULL; + int ret; + const char *dbpath; + sqlite3 *db; - GList *list = NULL; - list = g_list_first(trim_list); + dbpath = __get_cert_db_path(); + if (access(dbpath, F_OK) != -1) { + _LOGE("Cert db is already exists"); + return PM_PARSER_R_ERROR; + } - while (list) { - trim_data = (char *)list->data; - if (trim_data) { - if (prev) { - if (strcmp(trim_data, prev) == 0) { - trim_list = g_list_remove(trim_list, trim_data); - list = g_list_first(trim_list); - prev = NULL; - continue; - } else - prev = trim_data; - } else { - prev = trim_data; - } - } - list = g_list_next(list); + ret = sqlite3_open_v2(dbpath, &db, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + ret = sqlite3_busy_handler(db, __db_busy_handler, NULL); + if (ret != SQLITE_OK) { + _LOGE("failed to register busy handler: %s", + sqlite3_errmsg(db)); + sqlite3_close_v2(db); + return ret; } + + if (__initialize_db(db, dbpath, GLOBAL_USER)) { + sqlite3_close_v2(db); + return PM_PARSER_R_ERROR; + } + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata) +API int pkgmgr_parser_create_and_initialize_db(uid_t uid) { - if (a == NULL || b == NULL) - return 0; - if (strcmp((char *)a, (char *)b) == 0) - return 0; - if (strcmp((char *)a, (char *)b) < 0) - return -1; - if (strcmp((char *)a, (char *)b) > 0) - return 1; - return 0; + int ret; + struct passwd pwd; + struct passwd *result; + char buf[BUFSIZE]; + + ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result); + if (result == NULL) { + if (ret == 0) + _LOGE("no such user: %s", APPFW_USER); + else + _LOGE("getpwnam_r failed: %d", errno); + return PM_PARSER_R_ERROR; + } + + if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) { + _LOGE("Only root or app_fw user is allowed"); + return PM_PARSER_R_EINVAL; + } + + if (pkgmgr_parser_initialize_parser_db(uid)) + return PM_PARSER_R_ERROR; + + if (uid == OWNER_ROOT || uid == GLOBAL_USER) + if (pkgmgr_parser_initialize_cert_db()) + return PM_PARSER_R_ERROR; + + return PM_PARSER_R_OK; } -static int __check_dpi(const char *dpi_char, int dpi_int) +static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags) { - if (dpi_char == NULL) - return -1; + int ret; - if (strcasecmp(dpi_char, LDPI) == 0) { - if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX) - return 0; - else + /* FIXME: always open with OPEN_CREATE flag for keeping previous + * implementation + */ + if (flags & SQLITE_OPEN_READWRITE) + flags = flags | SQLITE_OPEN_CREATE; + + ret = sqlite3_open_v2(path, db, flags, NULL); + if (ret != SQLITE_OK) + return ret; + + ret = sqlite3_busy_handler(*db, __db_busy_handler, (void *)path); + if (ret != SQLITE_OK) { + _LOGE("failed to register busy handler: %s", + sqlite3_errmsg(*db)); + sqlite3_close_v2(*db); + return ret; + } + + if (flags & SQLITE_OPEN_CREATE) { + ret = __initialize_db(*db, path, uid); + if (ret) { + _LOGE("failed to initialize db: %s", path); + sqlite3_close_v2(*db); return -1; - } else if (strcasecmp(dpi_char, MDPI) == 0) { + } + } + + ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL); + if (ret != SQLITE_OK) { + _LOGE("failed to enable foreign key support: %s", + sqlite3_errmsg(*db)); + sqlite3_close_v2(*db); + return ret; + } + + return ret; +} + + +static int __convert_background_category(GList *category_list) +{ + int ret = 0; + GList *tmp; + char *category_data; + + if (category_list == NULL) + return 0; + + for (tmp = category_list; tmp; tmp = tmp->next) { + category_data = (char *)tmp->data; + if (category_data == NULL) + continue; + if (!strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR)) + ret |= APP_BG_CATEGORY_MEDIA_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR)) + ret |= APP_BG_CATEGORY_DOWNLOAD_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR)) + ret |= APP_BG_CATEGORY_BGNETWORK_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR)) + ret |= APP_BG_CATEGORY_LOCATION_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR)) + ret |= APP_BG_CATEGORY_SENSOR_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR)) + ret |= APP_BG_CATEGORY_IOTCOMM_VAL; + else if (!strcmp(category_data, APP_BG_CATEGORY_SYSTEM)) + ret |= APP_BG_CATEGORY_SYSTEM_VAL; + else + _LOGE("Unidentified category [%s]", category_data); + } + + return ret; +} + +#define EFFECTIVE_APPID_KEY "http://tizen.org/metadata/effective-appid" +static const char *__find_effective_appid(GList *metadata_list) +{ + GList *tmp; + metadata_x *md; + + for (tmp = metadata_list; tmp; tmp = tmp->next) { + md = (metadata_x *)tmp->data; + if (md == NULL || md->key == NULL) + continue; + + if (strcmp(md->key, EFFECTIVE_APPID_KEY) == 0) { + if (md->value) + return md->value; + } + } + + return NULL; +} + +static int __insert_appcontrol_privilege_info(sqlite3 *db, const char *appid, + appcontrol_x *ac) +{ + static const char query[] = + "INSERT INTO package_app_app_control_privilege (app_id," + " app_control, privilege) VALUES (?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + char app_control[BUFSIZE]; + GList *tmp; + char *privilege; + + if (ac == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = ac->privileges; tmp; tmp = tmp->next) { + privilege = (char *)tmp->data; + if (privilege == NULL || !strlen(privilege)) + continue; + + idx = 1; + snprintf(app_control, sizeof(app_control), "%s|%s|%s", + ac->operation ? (strlen(ac->operation) > 0 ? + ac->operation : "NULL") : "NULL", + ac->uri ? (strlen(ac->uri) > 0 ? + ac->uri : "NULL") : "NULL", + ac->mime ? (strlen(ac->mime) > 0 ? + ac->mime : "NULL") : "NULL"); + __BIND_TEXT(db, stmt, idx++, appid); + __BIND_TEXT(db, stmt, idx++, app_control); + __BIND_TEXT(db, stmt, idx++, privilege); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +static int __insert_appcontrol_info(sqlite3 *db, application_x *app) +{ + static const char query[] = + "INSERT INTO package_app_app_control (app_id, app_control," + " visibility, app_control_id) " + "VALUES (?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + char app_control[BUFSIZE]; + GList *tmp; + appcontrol_x *ac; + + if (app->appcontrol == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = app->appcontrol; tmp; tmp = tmp->next) { + ac = (appcontrol_x *)tmp->data; + if (ac == NULL) + continue; + idx = 1; + snprintf(app_control, sizeof(app_control), "%s|%s|%s", + ac->operation ? (strlen(ac->operation) > 0 ? + ac->operation : "NULL") : "NULL", + ac->uri ? (strlen(ac->uri) > 0 ? + ac->uri : "NULL") : "NULL", + ac->mime ? (strlen(ac->mime) > 0 ? + ac->mime : "NULL") : "NULL"); + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, app_control); + __BIND_TEXT(db, stmt, idx++, ac->visibility); + __BIND_TEXT(db, stmt, idx++, ac->id); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + if (__insert_appcontrol_privilege_info(db, app->appid, ac)) { + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +static int __insert_category_info(sqlite3 *db, application_x *app) +{ + static const char query[] = + "INSERT INTO package_app_app_category (app_id, category) " + "VALUES (?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + const char *category; + + if (app->category == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = app->category; tmp; tmp = tmp->next) { + category = (const char *)tmp->data; + if (category == NULL) + continue; + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, category); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +static int __insert_metadata_info(sqlite3 *db, application_x *app) +{ + static const char query[] = + "INSERT INTO package_app_app_metadata (app_id," + " md_key, md_value) VALUES (?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + metadata_x *md; + + if (app->metadata == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = app->metadata; tmp; tmp = tmp->next) { + md = (metadata_x *)tmp->data; + if (md == NULL) + continue; + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, md->key); + __BIND_TEXT(db, stmt, idx++, md->value); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +static int __insert_app_data_control_privilege_info(sqlite3 *db, + datacontrol_x *datacontrol) +{ + static const char query[] = + "INSERT INTO package_app_data_control_privilege (providerid," + " privilege, type) VALUES (?, ?, ?)"; + + int ret; + sqlite3_stmt *stmt; + int idx; + GList *privileges; + char *priv; + + if (datacontrol == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (privileges = datacontrol->privileges; privileges; + privileges = privileges->next) { + priv = (char *)privileges->data; + if (priv == NULL) + continue; + + idx = 1; + __BIND_TEXT(db, stmt, idx++, datacontrol->providerid); + __BIND_TEXT(db, stmt, idx++, priv); + __BIND_TEXT(db, stmt, idx++, datacontrol->type); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + return 0; +} + +static int __insert_datacontrol_info(sqlite3 *db, application_x *app) +{ + static const char query[] = + "INSERT INTO package_app_data_control (app_id, providerid," + " access, type, trusted) VALUES (?, ?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + datacontrol_x *dc; + + if (app->datacontrol == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = app->datacontrol; tmp; tmp = tmp->next) { + dc = (datacontrol_x *)tmp->data; + if (dc == NULL) + continue; + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, dc->providerid); + __BIND_TEXT(db, stmt, idx++, dc->access); + __BIND_TEXT(db, stmt, idx++, dc->type); + __BIND_TEXT(db, stmt, idx++, dc->trusted); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + if (dc->privileges && + __insert_app_data_control_privilege_info(db, dc)) { + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +/* TODO: move to installer */ +static int __check_dpi(const char *dpi_char, int dpi_int) +{ + if (dpi_char == NULL) + return -1; + + if (strcasecmp(dpi_char, LDPI) == 0) { + if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX) + return 0; + else + return -1; + } else if (strcasecmp(dpi_char, MDPI) == 0) { if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX) return 0; else @@ -697,20 +1034,299 @@ static int __check_dpi(const char *dpi_char, int dpi_int) return -1; } -static gint __check_icon_folder(const char *orig_icon_path, char **new_icon_path) +static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a, + gconstpointer b) +{ + splashscreen_x *ss = (splashscreen_x *)a; + const char *orientation = (const char *)b; + int dpi = -1; + int ret; + + if (ss->operation || ss->dpi == NULL) + return -1; + + ret = system_info_get_platform_int( + "http://tizen.org/feature/screen.dpi", &dpi); + if (ret != SYSTEM_INFO_ERROR_NONE) + return -1; + + if (strcasecmp(ss->orientation, orientation) == 0 && + __check_dpi(ss->dpi, dpi) == 0) + return 0; + + return -1; +} + +static gint __compare_splashscreen_with_orientation(gconstpointer a, + gconstpointer b) +{ + splashscreen_x *ss = (splashscreen_x *)a; + const char *orientation = (const char *)b; + + if (ss->operation || ss->dpi) + return -1; + + if (strcasecmp(ss->orientation, orientation) == 0) + return 0; + + return -1; +} + +static splashscreen_x *__find_default_splashscreen(GList *splashscreens, + const char *orientation) +{ + GList *tmp; + + tmp = g_list_find_custom(splashscreens, orientation, + (GCompareFunc) + __compare_splashscreen_with_orientation_dpi); + if (tmp) + return (splashscreen_x *)tmp->data; + + tmp = g_list_find_custom(splashscreens, orientation, + (GCompareFunc)__compare_splashscreen_with_orientation); + if (tmp) + return (splashscreen_x *)tmp->data; + + return NULL; +} + +static void __find_appcontrol_splashscreen_with_dpi(gpointer data, + gpointer user_data) +{ + splashscreen_x *ss = (splashscreen_x *)data; + GList **list = (GList **)user_data; + int dpi = -1; + int ret; + + if (ss->operation == NULL || ss->dpi == NULL) + return; + + ret = system_info_get_platform_int( + "http://tizen.org/feature/screen.dpi", &dpi); + if (ret != SYSTEM_INFO_ERROR_NONE) + return; + + if (__check_dpi(ss->dpi, dpi) != 0) + return; + + *list = g_list_append(*list, ss); +} + +static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data) +{ + splashscreen_x *ss = (splashscreen_x *)data; + GList **list = (GList **)user_data; + splashscreen_x *ss_tmp; + GList *tmp; + + if (ss->operation == NULL || ss->dpi) + return; + + for (tmp = *list; tmp; tmp = tmp->next) { + ss_tmp = (splashscreen_x *)tmp->data; + if (ss_tmp->operation + && strcmp(ss_tmp->operation, ss->operation) == 0 + && strcmp(ss_tmp->orientation, ss->orientation) == 0) + return; + } + + *list = g_list_append(*list, ss); +} + +static GList *__find_splashscreens(GList *splashscreens) +{ + GList *list = NULL; + splashscreen_x *ss; + + if (splashscreens == NULL) + return NULL; + + g_list_foreach(splashscreens, + __find_appcontrol_splashscreen_with_dpi, &list); + g_list_foreach(splashscreens, + __find_appcontrol_splashscreen, &list); + + ss = __find_default_splashscreen(splashscreens, "portrait"); + if (ss) + list = g_list_append(list, ss); + ss = __find_default_splashscreen(splashscreens, "landscape"); + if (ss) + list = g_list_append(list, ss); + + return list; +} + +static int __insert_splashscreen_info(sqlite3 *db, application_x *app, + GList *ss_list) +{ + static const char query[] = + "INSERT INTO package_app_splash_screen (app_id, src, type," + " orientation, indicatordisplay, operation, color_depth) " + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + splashscreen_x *ss; + + if (app->splashscreens == NULL) + return 0; + + if (ss_list == NULL) + return 0; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + for (tmp = ss_list; tmp; tmp = tmp->next) { + ss = (splashscreen_x *)tmp->data; + if (ss == NULL) + continue; + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, ss->src); + __BIND_TEXT(db, stmt, idx++, ss->type); + __BIND_TEXT(db, stmt, idx++, ss->orientation); + __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay); + __BIND_TEXT(db, stmt, idx++, ss->operation); + __BIND_TEXT(db, stmt, idx++, ss->color_depth); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; +} + +static void __trimfunc(GList *trim_list) +{ + char *trim_data; + char *prev = NULL; + GList *list = g_list_first(trim_list); + + while (list) { + trim_data = (char *)list->data; + if (trim_data) { + if (prev) { + if (strcmp(trim_data, prev) == 0) { + trim_list = g_list_remove(trim_list, + trim_data); + list = g_list_first(trim_list); + prev = NULL; + continue; + } else + prev = trim_data; + } else { + prev = trim_data; + } + } + list = g_list_next(list); + } +} + +static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata) +{ + if (a == NULL || b == NULL) + return 0; + if (strcmp((char *)a, (char *)b) == 0) + return 0; + if (strcmp((char *)a, (char *)b) < 0) + return -1; + if (strcmp((char *)a, (char *)b) > 0) + return 1; + return 0; +} + +/* TODO: refactor inserting localized info */ +static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns, + GList *dcns, GList *aths) +{ + GList *locale = NULL; + GList *tmp; + label_x *lbl; + license_x *lcn; + icon_x *icn; + description_x *dcn; + author_x *ath; + + for (tmp = lbls; tmp; tmp = tmp->next) { + lbl = (label_x *)tmp->data; + if (lbl == NULL) + continue; + if (lbl->lang) + locale = g_list_insert_sorted_with_data( + locale, (gpointer)lbl->lang, + __comparefunc, NULL); + } + for (tmp = lcns; tmp; tmp = tmp->next) { + lcn = (license_x *)tmp->data; + if (lcn == NULL) + continue; + if (lcn->lang) + locale = g_list_insert_sorted_with_data( + locale, (gpointer)lcn->lang, + __comparefunc, NULL); + } + for (tmp = icns; tmp; tmp = tmp->next) { + icn = (icon_x *)tmp->data; + if (icn == NULL) + continue; + if (icn->lang) + locale = g_list_insert_sorted_with_data( + locale, (gpointer)icn->lang, + __comparefunc, NULL); + } + for (tmp = dcns; tmp; tmp = tmp->next) { + dcn = (description_x *)tmp->data; + if (dcn == NULL) + continue; + if (dcn->lang) + locale = g_list_insert_sorted_with_data( + locale, (gpointer)dcn->lang, + __comparefunc, NULL); + } + for (tmp = aths; tmp; tmp = tmp->next) { + ath = (author_x *)tmp->data; + if (ath == NULL) + continue; + if (ath->lang) + locale = g_list_insert_sorted_with_data( + locale, (gpointer)ath->lang, + __comparefunc, NULL); + } + __trimfunc(locale); + return locale; +} + +static gint __check_icon_resolution(const char *orig_icon_path, + char **new_icon_path) { + int ret; char *dpi_path[2]; - char *icon_filename = NULL; - char modified_iconpath[BUFSIZE] = { '\0' }; - char icon_path[BUFSIZE] = { '\0' }; + char *icon_filename; + char modified_iconpath[BUFSIZE]; + char icon_path[BUFSIZE]; int i; int dpi = -1; if (orig_icon_path == NULL) return -1; - system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi); - if (!dpi) + ret = system_info_get_platform_int( + "http://tizen.org/feature/screen.dpi", &dpi); + if (ret != SYSTEM_INFO_ERROR_NONE) return -1; if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) { @@ -737,9 +1353,12 @@ static gint __check_icon_folder(const char *orig_icon_path, char **new_icon_path if (icon_filename == NULL) return -1; - snprintf(icon_path, strlen(orig_icon_path) - (strlen(icon_filename) - 1), "%s", orig_icon_path); + snprintf(icon_path, + strlen(orig_icon_path) - (strlen(icon_filename) - 1), + "%s", orig_icon_path); for (i = 0; i < 2; i++) { - snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s", icon_path, dpi_path[i], icon_filename); + snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s", + icon_path, dpi_path[i], icon_filename); if (access(modified_iconpath, F_OK) != -1) { /* if exists, return modified icon path */ *new_icon_path = strdup(modified_iconpath); @@ -753,8 +1372,7 @@ static gint __check_icon_folder(const char *orig_icon_path, char **new_icon_path static gint __compare_icon(gconstpointer a, gconstpointer b) { icon_x *icon = (icon_x *)a; - - char *icon_folder_path = NULL; + char *icon_path; if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0) return -1; @@ -762,9 +1380,9 @@ static gint __compare_icon(gconstpointer a, gconstpointer b) if (icon->dpi != NULL) return -1; - if (__check_icon_folder(icon->text, &icon_folder_path) == 0) { + if (__check_icon_resolution(icon->text, &icon_path) == 0) { free(icon->text); - icon->text = icon_folder_path; + icon->text = icon_path; } return 0; @@ -791,17 +1409,20 @@ static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b) { icon_x *icon = (icon_x *)a; char *lang = (char *)b; - char *icon_folder_path = NULL; + char *icon_path; if (icon->dpi != NULL) return -1; if (strcasecmp(icon->lang, lang) == 0) { if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) { - /* icon for no locale. check existance of folder-hierachied default icons */ - if (__check_icon_folder(icon->text, &icon_folder_path) == 0) { + /* icon for no locale. check existance of + * folder-hierachied default icons + */ + if (__check_icon_resolution(icon->text, + &icon_path) == 0) { free(icon->text); - icon->text = icon_folder_path; + icon->text = icon_path; } } return 0; @@ -812,15 +1433,18 @@ static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b) static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b) { + int ret; icon_x *icon = (icon_x *)a; char *lang = (char *)b; int dpi = -1; - system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi); - if (!dpi) + ret = system_info_get_platform_int( + "http://tizen.org/feature/screen.dpi", &dpi); + if (ret != SYSTEM_INFO_ERROR_NONE) return -1; - if (strcasecmp(icon->lang, lang) == 0 && __check_dpi(icon->dpi, dpi) == 0) + if (strcasecmp(icon->lang, lang) == 0 && + __check_dpi(icon->dpi, dpi) == 0) return 0; return -1; @@ -829,28 +1453,36 @@ static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b) static char *__find_icon(GList *icons, const char *lang) { GList *tmp; - icon_x *icon = NULL; + icon_x *icon; int dpi = 0; int ret; - /* first, find icon whose locale and dpi with given lang and system's dpi has matched */ - tmp = g_list_find_custom(icons, lang, (GCompareFunc)__compare_icon_with_lang_dpi); + /* first, find icon whose locale and dpi with given lang and + * system's dpi has matched + */ + tmp = g_list_find_custom(icons, lang, + (GCompareFunc)__compare_icon_with_lang_dpi); if (tmp != NULL) { icon = (icon_x *)tmp->data; return (char *)icon->text; } /* if first has failed, find icon whose locale has matched */ - tmp = g_list_find_custom(icons, lang, (GCompareFunc)__compare_icon_with_lang); + tmp = g_list_find_custom(icons, lang, + (GCompareFunc)__compare_icon_with_lang); if (tmp != NULL) { icon = (icon_x *)tmp->data; return (char *)icon->text; } - /* if second has failed, find icon whose dpi has matched with system's dpi */ - ret = system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi); + /* if second has failed, find icon whose dpi has matched with + * system's dpi + */ + ret = system_info_get_platform_int( + "http://tizen.org/feature/screen.dpi", &dpi); if (ret == SYSTEM_INFO_ERROR_NONE) { - tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi), (GCompareFunc)__compare_icon_with_dpi); + tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi), + (GCompareFunc)__compare_icon_with_dpi); if (tmp != NULL) { icon = (icon_x *)tmp->data; return (char *)icon->text; @@ -867,20 +1499,22 @@ static char *__find_icon(GList *icons, const char *lang) return NULL; } -static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths, - char **label, char **license, char **icon, char **description, char **author) +static void __extract_data(const char *locale, GList *lbls, GList *lcns, + GList *icns, GList *dcns, GList *aths, char **label, + char **license, char **icon, char **description, char **author) { GList *tmp; label_x *lbl; license_x *lcn; description_x *dcn; author_x *ath; + for (tmp = lbls; tmp; tmp = tmp->next) { lbl = (label_x *)tmp->data; if (lbl == NULL) continue; if (lbl->lang) { - if (strcmp(lbl->lang, (char *)data) == 0) { + if (strcmp(lbl->lang, locale) == 0) { *label = (char *)lbl->text; break; } @@ -891,2439 +1525,1710 @@ static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, if (lcn == NULL) continue; if (lcn->lang) { - if (strcmp(lcn->lang, (char *)data) == 0) { + if (strcmp(lcn->lang, locale) == 0) { *license = (char *)lcn->text; break; } } } - *icon = __find_icon(icns, (char *)data); + *icon = __find_icon(icns, locale); for (tmp = dcns; tmp; tmp = tmp->next) { dcn = (description_x *)tmp->data; if (dcn == NULL) continue; if (dcn->lang) { - if (strcmp(dcn->lang, (char *)data) == 0) { + if (strcmp(dcn->lang, locale) == 0) { *description = (char *)dcn->text; - break; - } - } - } - for (tmp = aths; tmp; tmp = tmp->next) { - ath = (author_x *)tmp->data; - if (ath == NULL) - continue; - if (ath->lang) { - if (strcmp(ath->lang, (char *)data) == 0) { - *author = (char *)ath->text; - break; - } - } - } - -} - -static void __extract_icon_data(gpointer data, GList *icns, char **icon, char **resolution) -{ - GList *tmp; - icon_x *icn; - for (tmp = icns; tmp; tmp = tmp->next) { - icn = (icon_x *)tmp->data; - if (icn == NULL) - continue; - if (icn->section) { - if (strcmp(icn->section, (char *)data) == 0) { - *icon = (char *)icn->text; - *resolution = (char *)icn->resolution; - break; - } - } - } -} - -static void __extract_image_data(gpointer data, GList *imgs, char **lang, char **image) -{ - GList *tmp; - image_x *img; - for (tmp = imgs; tmp; tmp = tmp->next) { - img = (image_x *)tmp->data; - if (img == NULL) - continue; - if (img->section) { - if (strcmp(img->section, (char *)data) == 0) { - *lang = (char *)img->lang; - *image = (char *)img->text; - break; - } - } - } -} - -static void __insert_pkglocale_info(gpointer data, gpointer userdata) -{ - int ret = -1; - char *label = NULL; - char *icon = NULL; - char *description = NULL; - char *license = NULL; - char *author = NULL; - char *query = NULL; - - manifest_x *mfx = (manifest_x *)userdata; - GList *lbl = mfx->label; - GList *lcn = mfx->license; - GList *icn = mfx->icon; - GList *dcn = mfx->description; - GList *ath = mfx->author; - - __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author); - if (!label && !description && !icon && !license && !author) - return; - - query = sqlite3_mprintf("INSERT INTO package_localized_info(package, package_locale, " \ - "package_label, package_icon, package_description, package_license, package_author) VALUES" \ - "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - mfx->package, - (char *)data, - label, - icon, - description, - license, - author); - - ret = __exec_query(query); - if (ret == -1) - _LOGD("Package Localized Info DB Insert failed\n"); - - sqlite3_free(query); -} - -static void __insert_application_locale_info(gpointer data, gpointer userdata) -{ - int ret = -1; - char *label = NULL; - char *icon = NULL; - char *query = NULL; - char *locale = (char *)data; - - application_x *app = (application_x *)userdata; - GList *lbl = app->label; - GList *icn = app->icon; - - __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL); - if (!label && !icon) - return; - - query = sqlite3_mprintf("INSERT INTO package_app_localized_info(app_id, app_locale, " \ - "app_label, app_icon) VALUES" \ - "(%Q, %Q, %Q, %Q)", app->appid, (char *)data, - label, icon); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Package UiApp Localized Info DB Insert failed\n"); - - sqlite3_free(query); - - /*insert ui app locale info to pkg locale to get mainapp data */ - if (strcasecmp(app->mainapp, "true") == 0) { - query = sqlite3_mprintf("INSERT OR REPLACE INTO package_localized_info(package, package_locale, " - "package_label, package_icon, package_description, package_license, package_author) VALUES" - "(%Q, %Q, " - "COALESCE((SELECT package_label FROM package_localized_info WHERE package=%Q AND package_locale=%Q), %Q), " - "COALESCE((SELECT package_icon FROM package_localized_info WHERE package=%Q AND package_locale=%Q), %Q), " - "(SELECT package_description FROM package_localized_info WHERE package=%Q AND package_locale=%Q), " - "(SELECT package_license FROM package_localized_info WHERE package=%Q AND package_locale=%Q), " - "(SELECT package_author FROM package_localized_info WHERE package=%Q AND package_locale=%Q))", - app->package, locale, - app->package, locale, label, - app->package, locale, icon, - app->package, locale, - app->package, locale, - app->package, locale); - - ret = __exec_query(query); - if (ret == -1) - _LOGE("Package Localized Info DB Insert failed"); - sqlite3_free(query); - } -} - -static void __insert_application_icon_section_info(gpointer data, gpointer userdata) -{ - int ret = -1; - char *icon = NULL; - char *resolution = NULL; - char query[MAX_QUERY_LEN] = {'\0'}; - - application_x *app = (application_x *)userdata; - GList *icn = app->icon; - - __extract_icon_data(data, icn, &icon, &resolution); - if (!icon && !resolution) - return; - sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO package_app_icon_section_info(app_id, " \ - "app_icon, app_icon_section, app_icon_resolution) VALUES " \ - "(%Q, %Q, %Q, %Q)", app->appid, - icon, (char *)data, resolution); - - ret = __exec_query(query); - if (ret == -1) - _LOGD("Package UiApp Localized Info DB Insert failed\n"); - -} - -static void __insert_application_image_info(gpointer data, gpointer userdata) -{ - int ret = -1; - char *lang = NULL; - char *img = NULL; - char query[MAX_QUERY_LEN] = {'\0'}; - - application_x *app = (application_x *)userdata; - GList *image = app->image; - - __extract_image_data(data, image, &lang, &img); - if (!lang && !img) - return; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_image_info(app_id, app_locale, " \ - "app_image_section, app_image) VALUES" \ - "(%Q, %Q, %Q, %Q)", app->appid, lang, (char *)data, img); - - ret = __exec_query(query); - if (ret == -1) - _LOGD("Package UiApp image Info DB Insert failed\n"); - -} - - -static int __insert_mainapp_info(manifest_x *mfx) -{ - GList *tmp; - application_x *app; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) - continue; - - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_mainapp=%Q WHERE app_id=%Q", - app->mainapp, app->appid); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package App Info DB Insert Failed\n"); - return -1; - } - if (strcasecmp(app->mainapp, "True") == 0) - mfx->mainapp_id = strdup(app->appid); - } - - if (mfx->mainapp_id == NULL) { - if (mfx->application == NULL) - return -1; - app = (application_x *)mfx->application->data; - if (app == NULL) - return -1; - if (app->appid) { - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_mainapp='true' WHERE app_id=%Q", - app->appid); - } else { - _LOGD("Not valid appid\n"); - return -1; - } - - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Info DB Insert Failed\n"); - return -1; - } - - free((void *)app->mainapp); - app->mainapp = strdup("true"); - mfx->mainapp_id = strdup(app->appid); - } - - memset(query, '\0', MAX_QUERY_LEN); - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_info SET mainapp_id=%Q WHERE package=%Q", mfx->mainapp_id, mfx->package); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Info DB update Failed\n"); - return -1; - } - - return 0; -} - -static int __convert_background_category(GList *category_list) -{ - int ret = 0; - GList *tmp_list = category_list; - char *category_data = NULL; - - if (category_list == NULL) - return 0; - - while (tmp_list != NULL) { - category_data = (char *)tmp_list->data; - if (strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR) == 0) - ret = ret | APP_BG_CATEGORY_MEDIA_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR) == 0) - ret = ret | APP_BG_CATEGORY_DOWNLOAD_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR) == 0) - ret = ret | APP_BG_CATEGORY_BGNETWORK_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR) == 0) - ret = ret | APP_BG_CATEGORY_LOCATION_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR) == 0) - ret = ret | APP_BG_CATEGORY_SENSOR_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR) == 0) - ret = ret | APP_BG_CATEGORY_IOTCOMM_VAL; - else if (strcmp(category_data, APP_BG_CATEGORY_SYSTEM) == 0) - ret = ret | APP_BG_CATEGORY_SYSTEM_VAL; - else - _LOGE("Unidentified category [%s]", category_data); - tmp_list = g_list_next(tmp_list); - } - - return ret; -} - -static const char *__find_effective_appid(GList *metadata_list) -{ - GList *tmp_list; - metadata_x *md; - - for (tmp_list = metadata_list; tmp_list; tmp_list = tmp_list->next) { - md = (metadata_x *)tmp_list->data; - if (md == NULL || md->key == NULL) - continue; - - if (strcmp(md->key, "http://tizen.org/metadata/effective-appid") == 0) { - if (md->value) - return md->value; - } - } - - return NULL; -} - -static char *__get_bool(char *value, bool is_true) -{ - if (value != NULL) { - if (!strcmp(value, "")) - return (is_true) ? "true" : "false"; - return value; - } - - return (is_true) ? "true" : "false"; -} - -/* _PRODUCT_LAUNCHING_ENHANCED_ -* app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus -*/ -static int __insert_application_info(manifest_x *mfx) -{ - GList *tmp; - application_x *app; - int ret = -1; - int background_value = 0; - char query[MAX_QUERY_LEN] = {'\0'}; - char *type = NULL; - const char *effective_appid; - - if (mfx->type) - type = strdup(mfx->type); - else - type = strdup("tpk"); - - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) - continue; - - background_value = __convert_background_category(app->background_category); - if (background_value < 0) { - _LOGE("Failed to retrieve background value[%d]", background_value); - background_value = 0; - } - - effective_appid = __find_effective_appid(app->metadata); - - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_info(" \ - "app_id, app_component, app_exec, app_nodisplay, app_type, " \ - "app_onboot, app_multiple, app_autorestart, app_taskmanage, app_enabled, " \ - "app_hwacceleration, app_screenreader, app_mainapp, app_recentimage, app_launchcondition, " \ - "app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, " \ - "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, " \ - "app_launch_mode, app_ui_gadget, app_support_mode, app_support_disable, component_type, package, " \ - "app_tep_name, app_zip_mount_file, app_background_category, app_package_type, app_root_path, " \ - "app_api_version, app_effective_appid, app_splash_screen_display, app_package_system, app_removable, " \ - "app_package_installed_time) " \ - "VALUES(" \ - "%Q, %Q, %Q, LOWER(%Q), %Q, " \ - "LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), " \ - "%Q, %Q, %Q, %Q, %Q, " \ - "LOWER(%Q), %Q, %Q, LOWER(%Q), %Q, " \ - "LOWER(%Q), LOWER(%Q), %Q, %Q, LOWER(%Q), " \ - "COALESCE(%Q, 'caller'), LOWER(%Q), %Q, LOWER(%Q), %Q, %Q, " \ - "%Q, %Q, %d, %Q, %Q, " \ - "%Q, %Q, LOWER(%Q), LOWER(%Q), LOWER(%Q), " \ - "%Q)", \ - app->appid, app->component_type, app->exec, __get_bool(app->nodisplay, false), app->type, - __get_bool(app->onboot, false), __get_bool(app->multiple, false), __get_bool(app->autorestart, false), __get_bool(app->taskmanage, false), __get_bool(app->enabled, true), - app->hwacceleration, app->screenreader, app->mainapp, app->recentimage, app->launchcondition, - __get_bool(app->indicatordisplay, true), app->portraitimg, app->landscapeimg, - __get_bool(app->guestmode_visibility, true), app->permission_type, - __get_bool(mfx->preload, false), __get_bool(app->submode, false), app->submode_mainid, mfx->installed_storage, __get_bool(app->process_pool, false), - app->launch_mode, __get_bool(app->ui_gadget, false), mfx->support_mode, __get_bool(mfx->support_disable, false), app->component_type, mfx->package, - mfx->tep_name, mfx->zip_mount_file, background_value, type, mfx->root_path, mfx->api_version, - effective_appid, __get_bool(app->splash_screen_display, false), __get_bool(mfx->system, false), __get_bool(mfx->removable, true), mfx->installed_time); - - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Info DB Insert Failed\n"); - if (type) - free(type); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - - if (type) - free(type); - - return 0; -} - -static int __insert_application_appcategory_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *ct_tmp; - const char *ct; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) - continue; - for (ct_tmp = app->category; ct_tmp; ct_tmp = ct_tmp->next) { - ct = (const char *)ct_tmp->data; - if (ct == NULL) - continue; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_app_category(app_id, category) " \ - "VALUES(%Q, %Q)",\ - app->appid, ct); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Category Info DB Insert Failed\n"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - } - return 0; -} - -static int __insert_application_appmetadata_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *md_tmp; - metadata_x *md; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) - continue; - for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) { - md = (metadata_x *)md_tmp->data; - if (md == NULL) - continue; - if (md->key) { - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_app_metadata(app_id, md_key, md_value) " \ - "VALUES(%Q, %Q, %Q)",\ - app->appid, md->key, md->value ? md->value : NULL); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Metadata Info DB Insert Failed\n"); - return -1; - } - } - memset(query, '\0', MAX_QUERY_LEN); - } - } - return 0; -} - -static int __insert_application_apppermission_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *pm_tmp; - permission_x *pm; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) - continue; - for (pm_tmp = app->permission; pm_tmp; pm_tmp = pm_tmp->next) { - pm = (permission_x *)pm_tmp->data; - if (pm == NULL) - continue; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_app_permission(app_id, pm_type, pm_value) " \ - "VALUES(%Q, %Q, %Q)",\ - app->appid, pm->type, pm->value); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp permission Info DB Insert Failed\n"); - return -1; + break; } - memset(query, '\0', MAX_QUERY_LEN); } } - return 0; -} - -static int __insert_application_appcontrol_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *acontrol_tmp; - appcontrol_x *acontrol; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - char buf[BUFSIZE] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) + for (tmp = aths; tmp; tmp = tmp->next) { + ath = (author_x *)tmp->data; + if (ath == NULL) continue; - for (acontrol_tmp = app->appcontrol; acontrol_tmp; acontrol_tmp = acontrol_tmp->next) { - acontrol = (appcontrol_x *)acontrol_tmp->data; - if (acontrol == NULL) - continue; - snprintf(buf, BUFSIZE, "%s|%s|%s",\ - acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL", - acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL", - acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL"); - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_app_control(app_id, app_control) " \ - "VALUES(%Q, %Q)",\ - app->appid, buf); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp AppSvc DB Insert Failed\n"); - return -1; + if (ath->lang) { + if (strcmp(ath->lang, locale) == 0) { + *author = (char *)ath->text; + break; } - memset(query, '\0', MAX_QUERY_LEN); } } - return 0; } -static int __insert_application_datacontrol_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *dc_tmp; - datacontrol_x *dc; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; +static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app, + const char *locale, const char *label, const char *icon) +{ + static const char query[] = + "INSERT OR REPLACE INTO package_localized_info (" + " package, package_locale, package_label, package_icon," + " package_description, package_license, package_author) " + "VALUES (?, ?," + " COALESCE((SELECT package_label FROM package_localized_info" + " WHERE package=? AND package_locale=?), ?)," + " COALESCE((SELECT package_icon FROM package_localized_info" + " WHERE package=? AND package_icon=?), ?)," + " (SELECT package_description FROM package_localized_info" + " WHERE package=? AND package_locale=?)," + " (SELECT package_description FROM package_localized_info" + " WHERE package=? AND package_locale=?)," + " (SELECT package_description FROM package_localized_info" + " WHERE package=? AND package_locale=?))"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) - continue; - for (dc_tmp = app->datacontrol; dc_tmp; dc_tmp = dc_tmp->next) { - dc = (datacontrol_x *)dc_tmp->data; - if (dc == NULL) - continue; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_data_control(app_id, providerid, access, type) " \ - "VALUES(%Q, %Q, %Q, %Q)",\ - app->appid, - dc->providerid, - dc->access, - dc->type); - - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Data Control DB Insert Failed\n"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, label); + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, icon); + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, app->package); + __BIND_TEXT(db, stmt, idx++, locale); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } + + sqlite3_finalize(stmt); + return 0; } -static int __insert_application_share_request_info(manifest_x *mfx) +static int __insert_app_localized_info(sqlite3 *db, application_x *app) { - GList *app_tmp; - application_x *app; - GList *ds_tmp; - datashare_x *ds; - GList *rq_tmp; - const char *rq; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) - continue; - for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) { - ds = (datashare_x *)ds_tmp->data; - if (ds == NULL) - continue; - for (rq_tmp = ds->request; rq_tmp; rq_tmp = rq_tmp->next) { - rq = (const char *)rq_tmp->data; - if (rq == NULL) - continue; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_share_request(app_id, data_share_request) " \ - "VALUEES(%Q, %Q)",\ - app->appid, rq); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Share Request DB Insert Failed\n"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - } + static const char query[] = + "INSERT INTO package_app_localized_info (app_id, app_locale," + " app_label, app_icon) " + "VALUES (?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + GList *locales; + const char *locale; + char *label; + char *icon; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - return 0; -} -static int __insert_application_share_allowed_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *ds_tmp; - datashare_x *ds; - GList *df_tmp; - define_x *df; - GList *al_tmp; - const char *al; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL) + locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL); + for (tmp = locales; tmp; tmp = tmp->next) { + locale = (const char *)tmp->data; + label = NULL; + icon = NULL; + __extract_data(locale, app->label, NULL, app->icon, NULL, NULL, + &label, NULL, &icon, NULL, NULL); + if (!label && !icon) continue; - for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) { - ds = (datashare_x *)ds_tmp->data; - if (ds == NULL) - continue; - for (df_tmp = ds->define; df_tmp; df_tmp = df_tmp->next) { - df = (define_x *)df_tmp->data; - if (df == NULL) - continue; - for (al_tmp = df->allowed; al_tmp; al_tmp = al_tmp->next) { - al = (const char *)al_tmp->data; - if (al == NULL) - continue; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \ - "VALUES(%Q, %Q, %Q)",\ - app->appid, df->path, al); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Share Allowed DB Insert Failed\n"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - } + + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, label); + __BIND_TEXT(db, stmt, idx++, icon); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + g_list_free(locales); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_reset(stmt); + + if (strcasecmp(app->mainapp, "true") == 0) { + if (__insert_mainapp_localized_info(db, app, locale, + label, icon)) + _LOGE("insert mainapp localized info failed"); } } + + g_list_free(locales); + sqlite3_finalize(stmt); + return 0; } -static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a, gconstpointer b) +static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx) { - splashscreen_x *ss = (splashscreen_x *)a; - const char *orientation = (const char *)b; - int dpi = -1; + static const char query[] = + "INSERT INTO package_privilege_info (package, privilege, type) " + "VALUES (?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + privilege_x *priv; - if (ss->operation || ss->dpi == NULL) + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; + } - system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi); - if (!dpi) - return -1; + for (tmp = mfx->privileges; tmp; tmp = tmp->next) { + priv = (privilege_x *)tmp->data; + if (priv == NULL) + continue; - if (strcasecmp(ss->orientation, orientation) == 0 && __check_dpi(ss->dpi, dpi) == 0) - return 0; + idx = 1; + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, priv->value); + __BIND_TEXT(db, stmt, idx++, priv->type); - return -1; + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + sqlite3_reset(stmt); + } + + sqlite3_finalize(stmt); + + return 0; } -static gint __compare_splashscreen_with_orientation(gconstpointer a, gconstpointer b) +static int __insert_package_plugin_execution_info(sqlite3 *db, + manifest_x *mfx) { - splashscreen_x *ss = (splashscreen_x *)a; - const char *orientation = (const char *)b; - - if (ss->operation || ss->dpi) - return -1; + static const char query[] = + "INSERT INTO package_plugin_info " + "(pkgid, appid, plugin_type, plugin_name) " + "VALUES (?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + plugin_x *plugin; - if (strcasecmp(ss->orientation, orientation) == 0) + if (!mfx->plugin) return 0; - return -1; -} + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } -static splashscreen_x *__find_default_splashscreen(GList *splashscreens, - const char *orientation) -{ - GList *tmp; + for (tmp = mfx->plugin; tmp; tmp = tmp->next) { + plugin = (plugin_x *)tmp->data; + if (plugin == NULL) + continue; - tmp = g_list_find_custom(splashscreens, orientation, - (GCompareFunc)__compare_splashscreen_with_orientation_dpi); - if (tmp) - return (splashscreen_x *)tmp->data; + idx = 1; + __BIND_TEXT(db, stmt, idx++, plugin->pkgid); + __BIND_TEXT(db, stmt, idx++, plugin->appid); + __BIND_TEXT(db, stmt, idx++, plugin->plugin_type); + __BIND_TEXT(db, stmt, idx++, plugin->plugin_name); - tmp = g_list_find_custom(splashscreens, orientation, - (GCompareFunc)__compare_splashscreen_with_orientation); - if (tmp) - return (splashscreen_x *)tmp->data; + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + sqlite3_reset(stmt); + } - return NULL; + sqlite3_finalize(stmt); + + return 0; } -static void __find_appcontrol_splashscreen_with_dpi(gpointer data, gpointer user_data) +static int __delete_package_plugin_execution_info(sqlite3 *db, + const char *pkgid) { - splashscreen_x *ss = (splashscreen_x *)data; - GList **list = (GList **)user_data; - int dpi = -1; + static const char query[] = + "DELETE FROM package_plugin_info WHERE pkgid=?"; + int ret; + sqlite3_stmt *stmt; - if (ss->operation == NULL || ss->dpi == NULL) - return; + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } - system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi); - if (!dpi) - return; + __BIND_TEXT(db, stmt, 1, pkgid); - if (__check_dpi(ss->dpi, dpi) != 0) - return; + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } - *list = g_list_append(*list, ss); + sqlite3_finalize(stmt); + + return 0; } -static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data) +static int __insert_package_appdefined_privilege_info(sqlite3 *db, + manifest_x *mfx) { - splashscreen_x *ss = (splashscreen_x *)data; - GList **list = (GList **)user_data; - splashscreen_x *ss_tmp; + static const char query[] = + "INSERT INTO package_appdefined_privilege_info " + "(package, privilege, license, type) " + "VALUES (?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; GList *tmp; + appdefined_privilege_x *priv; - if (ss->operation == NULL || ss->dpi) - return; - - for (tmp = *list; tmp; tmp = tmp->next) { - ss_tmp = (splashscreen_x *)tmp->data; - if (ss_tmp->operation - && strcmp(ss_tmp->operation, ss->operation) == 0 - && strcmp(ss_tmp->orientation, ss->orientation) == 0) - return; + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - *list = g_list_append(*list, ss); -} + for (tmp = mfx->appdefined_privileges; tmp; tmp = tmp->next) { + priv = (appdefined_privilege_x *)tmp->data; + if (priv == NULL) + continue; -static GList *__find_splashscreens(GList *splashscreens) -{ - GList *list = NULL; - splashscreen_x *ss; + idx = 1; + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, priv->value); + __BIND_TEXT(db, stmt, idx++, priv->license); + __BIND_TEXT(db, stmt, idx++, priv->type); - g_list_foreach(splashscreens, - __find_appcontrol_splashscreen_with_dpi, &list); - g_list_foreach(splashscreens, - __find_appcontrol_splashscreen, &list); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + sqlite3_reset(stmt); + } - ss = __find_default_splashscreen(splashscreens, "portrait"); - if (ss) - list = g_list_append(list, ss); - ss = __find_default_splashscreen(splashscreens, "landscape"); - if (ss) - list = g_list_append(list, ss); + sqlite3_finalize(stmt); - return list; + return 0; } -static int __insert_application_splashscreen_info(manifest_x *mfx) +static int __insert_package_dependency_info(sqlite3 *db, manifest_x *mfx) { - GList *app_tmp; - application_x *app; - GList *ss_tmp; - splashscreen_x *ss; + static const char query[] = + "INSERT INTO package_dependency_info" + " (package, depends_on, type, required_version) " + "VALUES (?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; GList *tmp; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; + dependency_x *dep; - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL || app->splashscreens == NULL) - continue; + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } - ss_tmp = __find_splashscreens(app->splashscreens); - if (ss_tmp == NULL) + for (tmp = mfx->dependencies; tmp; tmp = tmp->next) { + dep = (dependency_x *)tmp->data; + if (dep == NULL) continue; - for (tmp = ss_tmp; tmp; tmp = tmp->next) { - ss = (splashscreen_x *)tmp->data; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_splash_screen" \ - "(app_id, src, type, orientation, indicatordisplay, " \ - "operation, color_depth) " \ - "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - app->appid, ss->src, ss->type, ss->orientation, - ss->indicatordisplay, ss->operation, - ss->color_depth); - - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Splash Screen DB Insert Failed"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); + idx = 1; + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, dep->depends_on); + __BIND_TEXT(db, stmt, idx++, dep->type); + __BIND_TEXT(db, stmt, idx++, dep->required_version); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - g_list_free(ss_tmp); + sqlite3_reset(stmt); } + + sqlite3_finalize(stmt); + return 0; } -static int __insert_application_legacy_splashscreen_info(manifest_x *mfx) -{ - GList *app_tmp; +/* _PRODUCT_LAUNCHING_ENHANCED_ + * app->indicatordisplay, app->portraitimg, app->landscapeimg, + * app->guestmode_appstatus + */ +static int __insert_application_info(sqlite3 *db, manifest_x *mfx) +{ + static const char query[] = + "INSERT INTO package_app_info (app_id, app_component," + " app_exec, app_nodisplay, app_type, app_onboot, app_multiple," + " app_autorestart, app_taskmanage, app_hwacceleration," + " app_screenreader, app_mainapp, app_recentimage," + " app_launchcondition, app_indicatordisplay, app_portraitimg," + " app_landscapeimg, app_guestmodevisibility," + " app_permissiontype, app_preload, app_submode," + " app_submode_mainid, app_installed_storage, app_process_pool," + " app_launch_mode, app_ui_gadget, app_support_mode," + " app_support_disable, component_type, package, app_tep_name," + " app_zip_mount_file, app_background_category," + " app_package_type, app_root_path, app_api_version," + " app_effective_appid, app_splash_screen_display," + " app_package_system, app_removable," + " app_package_installed_time, app_support_ambient," + " app_external_path, app_setup_appid) " + "VALUES (?, ?, " + " ?, LOWER(?), ?, LOWER(?), LOWER(?)," + " LOWER(?), LOWER(?), ?," + " ?, LOWER(?), ?," + " ?, LOWER(?), ?," + " ?, LOWER(?)," + " ?, LOWER(?), LOWER(?)," + " ?, ?, LOWER(?)," + " COALESCE(?, 'single'), LOWER(?), ?," + " LOWER(?), ?, ?, ?," + " ?, ?," + " ?, ?, ?," + " ?, LOWER(?)," + " LOWER(?), LOWER(?)," + " ?, LOWER(?)," + " ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; application_x *app; - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - char *tmp; - const char *image_type; - const char *indicatordisplay; - const char *orientation; - const char *operation = "launch-effect"; - const char *color_depth = "24"; /* default */ - - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; - if (app == NULL || - (app->portraitimg == NULL && app->landscapeimg == NULL)) - continue; - image_type = "img"; /* default */ - if (app->effectimage_type) { - tmp = strstr(app->effectimage_type, "edj"); - if (tmp) - image_type = "edj"; - } - indicatordisplay = "true"; /* default */ - if (app->indicatordisplay) - indicatordisplay = app->indicatordisplay; - if (app->portraitimg) { - orientation = "portrait"; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_splash_screen" \ - "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \ - "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - app->appid, app->portraitimg, image_type, - orientation, indicatordisplay, operation, - color_depth); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Splash Screen DB Insert Failed"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - if (app->landscapeimg) { - orientation = "landscape"; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_splash_screen" \ - "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \ - "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - app->appid, app->landscapeimg, image_type, - orientation, indicatordisplay, operation, - color_depth); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Splash Screen DB Insert Failed"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } + int bg_category; + const char *effective_appid; + GList *ss_list; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - return 0; -} -static int __insert_application_metadata_splashscreen_info(manifest_x *mfx) -{ - GList *app_tmp; - application_x *app; - GList *md_tmp; - metadata_x *md; - int ret; - char query[MAX_QUERY_LEN] = {'\0'}; - char *token; - char *tmpptr = NULL; - const char *operation; - const char *portraitimg; - const char *landscapeimg; - const char *indicatordisplay; - const char *orientation; - const char *image_type; - const char *color_depth = "24"; /* default */ - - for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) { - app = (application_x *)app_tmp->data; + for (tmp = mfx->application; tmp; tmp = tmp->next) { + app = (application_x *)tmp->data; if (app == NULL) continue; - for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) { - md = (metadata_x *)md_tmp->data; - if (md == NULL || md->key == NULL || md->value == NULL) - continue; + bg_category = __convert_background_category( + app->background_category); + effective_appid = __find_effective_appid(app->metadata); - if (strcasestr(md->key, "operation_effect=")) { - operation = index(md->key, '='); - if (operation && operation[1] != '\0') - operation++; - else - operation = "launch-effect"; - } else if (strcasestr(md->key, "launch_effect")) { - operation = "launch-effect"; - } else { - continue; - } + idx = 1; + __BIND_TEXT(db, stmt, idx++, app->appid); + __BIND_TEXT(db, stmt, idx++, app->component_type); + __BIND_TEXT(db, stmt, idx++, app->exec); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false)); + __BIND_TEXT(db, stmt, idx++, app->type); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false)); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->autorestart, false)); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->taskmanage, false)); + __BIND_TEXT(db, stmt, idx++, app->hwacceleration); + __BIND_TEXT(db, stmt, idx++, app->screenreader); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false)); + __BIND_TEXT(db, stmt, idx++, app->recentimage); + __BIND_TEXT(db, stmt, idx++, app->launchcondition); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->indicatordisplay, true)); + __BIND_TEXT(db, stmt, idx++, app->portraitimg); + __BIND_TEXT(db, stmt, idx++, app->landscapeimg); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->guestmode_visibility, true)); + __BIND_TEXT(db, stmt, idx++, app->permission_type); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false)); + __BIND_TEXT(db, stmt, idx++, app->submode_mainid); + __BIND_TEXT(db, stmt, idx++, mfx->installed_storage); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->process_pool, false)); + __BIND_TEXT(db, stmt, idx++, app->launch_mode); + __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false)); + __BIND_TEXT(db, stmt, idx++, app->support_mode); + __BIND_TEXT(db, stmt, idx++, + __get_bool(mfx->support_disable, false)); + __BIND_TEXT(db, stmt, idx++, app->component_type); + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, mfx->tep_name); + __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file); + __BIND_INT(db, stmt, idx++, bg_category); + __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk"); + __BIND_TEXT(db, stmt, idx++, mfx->root_path); + __BIND_TEXT(db, stmt, idx++, mfx->api_version); + __BIND_TEXT(db, stmt, idx++, effective_appid); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->splash_screen_display, true)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false)); + __BIND_TEXT(db, stmt, idx++, mfx->installed_time); + __BIND_TEXT(db, stmt, idx++, + __get_bool(app->support_ambient, false)); + __BIND_TEXT(db, stmt, idx++, mfx->external_path); + __BIND_TEXT(db, stmt, idx++, app->setup_appid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } - portraitimg = NULL; - landscapeimg = NULL; - indicatordisplay = "true"; /* default */ - token = strtok_r(md->value, "|", &tmpptr); - while (token != NULL) { - if (strcasestr(token, "portrait-effectimage=")) { - portraitimg = index(token, '='); - if (portraitimg && portraitimg[1] != '\0') - portraitimg++; - else - portraitimg = NULL; - } else if (strcasestr(token, "landscape-effectimage=")) { - landscapeimg = index(token, '='); - if (landscapeimg && landscapeimg[1] != '\0') - landscapeimg++; - else - landscapeimg = NULL; - } else if (strcasestr(token, "indicatordisplay=")) { - indicatordisplay = index(token, '='); - if (indicatordisplay && indicatordisplay[1] != '\0') - indicatordisplay++; - else - indicatordisplay = "true"; - } - - token = strtok_r(NULL, "|", &tmpptr); - } + sqlite3_reset(stmt); - if (portraitimg) { - orientation = "portrait"; - image_type = "img"; - if (strcasestr(portraitimg, "edj")) - image_type = "edj"; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_splash_screen" \ - "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \ - "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - app->appid, portraitimg, image_type, - orientation, indicatordisplay, operation, - color_depth); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Splash Screen DB Insert Failed"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } - if (landscapeimg) { - orientation = "landscape"; - image_type = "img"; - if (strcasestr(landscapeimg, "edj")) - image_type = "edj"; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_app_splash_screen" \ - "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \ - "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)", - app->appid, landscapeimg, image_type, - orientation, indicatordisplay, operation, - color_depth); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package UiApp Splash Screen DB Insert Failed"); - return -1; - } - memset(query, '\0', MAX_QUERY_LEN); - } + if (__insert_appcontrol_info(db, app)) { + sqlite3_finalize(stmt); + return -1; + } + if (__insert_category_info(db, app)) { + sqlite3_finalize(stmt); + return -1; + } + if (__insert_metadata_info(db, app)) { + sqlite3_finalize(stmt); + return -1; + } + if (__insert_datacontrol_info(db, app)) { + sqlite3_finalize(stmt); + return -1; + } + ss_list = __find_splashscreens(app->splashscreens); + if (__insert_splashscreen_info(db, app, ss_list)) { + g_list_free(ss_list); + sqlite3_finalize(stmt); + return -1; + } + g_list_free(ss_list); + if (__insert_app_localized_info(db, app)) { + sqlite3_finalize(stmt); + return -1; } } + sqlite3_finalize(stmt); + return 0; } -static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid) +static int __insert_package_update_info(sqlite3 *db, manifest_x *mfx) { - GList *tmp; - application_x *app; - privilege_x *pv = NULL; - char query[MAX_QUERY_LEN] = { '\0' }; - int ret = -1; - author_x *author; - const char *auth_name = NULL; - const char *auth_email = NULL; - const char *auth_href = NULL; - - GList *pkglocale = NULL; - GList *applocale = NULL; - GList *appicon = NULL; - GList *appimage = NULL; - - int temp_pkg_mode = 0; - int temp_app_mode = 0; - char pkg_mode[MAX_INTEGER_LENGTH] = {'\0'}; - application_x *up_support_mode = NULL; - GList *list_up = NULL; + static const char query[] = + "INSERT INTO package_update_info (package, update_version) " + "VALUES (?, ?)"; + int ret; + int idx; + sqlite3_stmt *stmt; - if (mfx->author && mfx->author->data) { - author = (author_x *)mfx->author->data; - if (author->text) - auth_name = author->text; - if (author->email) - auth_email = author->email; - if (author->href) - auth_href = author->href; - } - - if (mfx->support_mode) - temp_pkg_mode = atoi(mfx->support_mode); - - /* FIXME : OR-ing should be fixed to not to store whole support mode */ - list_up = mfx->application; - while (list_up != NULL) { - up_support_mode = (application_x *)list_up->data; - if (up_support_mode->support_mode) { - temp_app_mode = atoi(up_support_mode->support_mode); - temp_pkg_mode |= temp_app_mode; - } - list_up = list_up->next; - } - snprintf(pkg_mode, MAX_INTEGER_LENGTH, "%d", temp_pkg_mode); - - if (mfx->support_mode) - free((void *)mfx->support_mode); - mfx->support_mode = strdup(pkg_mode); - - /*Insert in the package_info DB*/ - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_info(" \ - "package, package_type, package_version, package_api_version, package_tep_name, package_zip_mount_file, " \ - "install_location, package_size, package_removable, package_preload, package_readonly, " \ - "package_update, package_appsetting, package_nodisplay, package_system, author_name, " \ - "author_email, author_href, installed_time, installed_storage, storeclient_id, " \ - "mainapp_id, package_url, root_path, csc_path, package_support_mode, package_support_disable) " \ - "VALUES(" \ - "%Q, %Q, %Q, %Q, %Q, %Q, " \ - "%Q, %Q, LOWER(%Q), LOWER(%Q), LOWER(%Q), " \ - "LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), %Q, " \ - "%Q, %Q, %Q, %Q, %Q, " \ - "%Q, %Q, %Q, %Q, %Q, LOWER(%Q))", - mfx->package, mfx->type, mfx->version, mfx->api_version, mfx->tep_name, mfx->zip_mount_file, - mfx->installlocation, mfx->package_size, __get_bool(mfx->removable, true), __get_bool(mfx->preload, false), __get_bool(mfx->readonly, false), - __get_bool(mfx->update, false), __get_bool(mfx->appsetting, false), __get_bool(mfx->nodisplay_setting, false), __get_bool(mfx->system, false), auth_name, - auth_email, auth_href, mfx->installed_time, mfx->installed_storage, - mfx->storeclient_id, - mfx->mainapp_id, mfx->package_url, mfx->root_path, mfx->csc_path, mfx->support_mode, __get_bool(mfx->support_disable, false)); - - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Info DB Insert Failed\n"); - return -1; - } - - /*Insert in the package_privilege_info DB*/ - for (tmp = mfx->privileges; tmp; tmp = tmp->next) { - pv = (privilege_x *)tmp->data; - if (pv == NULL) - continue; - memset(query, '\0', MAX_QUERY_LEN); - sqlite3_snprintf(MAX_QUERY_LEN, query, - "INSERT INTO package_privilege_info(package, privilege, type) " \ - "VALUES(%Q, %Q, %Q)",\ - mfx->package, pv->value, pv->type); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Privilege Info DB Insert Failed\n"); - return -1; - } + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - if (mfx->application != NULL) { - ret = __insert_mainapp_info(mfx); - if (ret == -1) - return -1; + idx = 1; + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx, mfx->version); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } + sqlite3_finalize(stmt); - /*Insert the package locale*/ - pkglocale = __create_locale_list(pkglocale, mfx->label, mfx->license, mfx->icon, mfx->description, mfx->author); - /*remove duplicated data in pkglocale*/ - __trimfunc(pkglocale); + return 0; +} - /*Insert the app locale, icon, image info */ - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) +static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx) +{ + static const char query[] = + "INSERT INTO package_localized_info (package, package_locale," + " package_label, package_icon, package_description," + " package_license, package_author) " + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx; + GList *tmp; + GList *locales; + const char *locale; + char *label; + char *icon; + char *description; + char *license; + char *author; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; + } + + locales = __create_locale_list(mfx->label, mfx->license, mfx->icon, + mfx->description, mfx->author); + for (tmp = locales; tmp; tmp = tmp->next) { + locale = (const char *)tmp->data; + label = NULL; + icon = NULL; + description = NULL; + license = NULL; + author = NULL; + __extract_data(locale, mfx->label, mfx->license, mfx->icon, + mfx->description, mfx->author, + &label, &license, &icon, &description, &author); + if (!label && !license && !icon && !description && !author) continue; - applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL); - appicon = __create_icon_list(appicon, app->icon); - appimage = __create_image_list(appimage, app->image); - } - /*remove duplicated data in applocale*/ - __trimfunc(applocale); - __trimfunc(appicon); - __trimfunc(appimage); - g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx); + idx = 1; + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, locale); + __BIND_TEXT(db, stmt, idx++, label); + __BIND_TEXT(db, stmt, idx++, icon); + __BIND_TEXT(db, stmt, idx++, description); + __BIND_TEXT(db, stmt, idx++, license); + __BIND_TEXT(db, stmt, idx++, author); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + g_list_free(locales); + sqlite3_finalize(stmt); + return -1; + } - /*native app locale info*/ - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) - continue; - g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app); - g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app); - g_list_foreach(appimage, __insert_application_image_info, (gpointer)app); + sqlite3_reset(stmt); } - g_list_free(pkglocale); - pkglocale = NULL; - g_list_free(applocale); - applocale = NULL; - g_list_free(appicon); - appicon = NULL; - g_list_free(appimage); - appimage = NULL; + g_list_free(locales); + sqlite3_finalize(stmt); - /*Insert in the package_app_info DB*/ - ret = __insert_application_info(mfx); - if (ret == -1) - return -1; - /*Insert in the package_app_app_control DB*/ - ret = __insert_application_appcontrol_info(mfx); - if (ret == -1) - return -1; + return 0; +} - /*Insert in the package_app_app_category DB*/ - ret = __insert_application_appcategory_info(mfx); - if (ret == -1) - return -1; +static int __insert_package_info(sqlite3 *db, manifest_x *mfx) +{ + static const char query[] = + "INSERT INTO package_info (package, package_type," + " package_version, package_api_version, package_tep_name," + " package_zip_mount_file, install_location, package_size," + " package_removable, package_preload, package_readonly," + " package_update, package_appsetting, package_nodisplay," + " package_system, author_name, author_email, author_href," + " installed_time, installed_storage, storeclient_id," + " mainapp_id, package_url, root_path, external_path," + " csc_path, package_support_mode, package_support_disable) " + "VALUES (?, ?," + " ?, ?, ?," + " ?, ?, ?," + " LOWER(?), LOWER(?), LOWER(?)," + " LOWER(?), LOWER(?), LOWER(?)," + " LOWER(?), ?, ?, ?," + " ?, ?, ?," + " ?, ?, ?, ?," + " ?, ?, LOWER(?))"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; + const char *author_name = NULL; + const char *author_email = NULL; + const char *author_href = NULL; - /*Insert in the package_app_app_metadata DB*/ - ret = __insert_application_appmetadata_info(mfx); - if (ret == -1) + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; + } - /*Insert in the package_app_app_permission DB*/ - ret = __insert_application_apppermission_info(mfx); - if (ret == -1) + if (mfx->author && mfx->author->data) { + author_name = ((author_x *)mfx->author->data)->text; + author_email = ((author_x *)mfx->author->data)->email; + author_href = ((author_x *)mfx->author->data)->href; + } + + __BIND_TEXT(db, stmt, idx++, mfx->package); + __BIND_TEXT(db, stmt, idx++, mfx->type); + __BIND_TEXT(db, stmt, idx++, mfx->version); + __BIND_TEXT(db, stmt, idx++, mfx->api_version); + __BIND_TEXT(db, stmt, idx++, mfx->tep_name); + __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file); + __BIND_TEXT(db, stmt, idx++, mfx->installlocation); + __BIND_TEXT(db, stmt, idx++, mfx->package_size); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false)); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false)); + __BIND_TEXT(db, stmt, idx++, author_name); + __BIND_TEXT(db, stmt, idx++, author_email); + __BIND_TEXT(db, stmt, idx++, author_href); + __BIND_TEXT(db, stmt, idx++, mfx->installed_time); + __BIND_TEXT(db, stmt, idx++, mfx->installed_storage); + __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id); + __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id); + __BIND_TEXT(db, stmt, idx++, mfx->package_url); + __BIND_TEXT(db, stmt, idx++, mfx->root_path); + __BIND_TEXT(db, stmt, idx++, mfx->external_path); + __BIND_TEXT(db, stmt, idx++, mfx->csc_path); + __BIND_TEXT(db, stmt, idx++, mfx->support_mode); + __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false)); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); return -1; + } - /*Insert in the package_app_share_allowed DB*/ - ret = __insert_application_share_allowed_info(mfx); - if (ret == -1) - return -1; + sqlite3_finalize(stmt); - /*Insert in the package_app_share_request DB*/ - ret = __insert_application_share_request_info(mfx); - if (ret == -1) + if (__insert_package_update_info(db, mfx)) return -1; - - /*Insert in the package_app_data_control DB*/ - ret = __insert_application_datacontrol_info(mfx); - if (ret == -1) + if (__insert_package_localized_info(db, mfx)) return -1; - - /*Insert in the package_app_splash_screen DB (backward compatibility)*/ - ret = __insert_application_legacy_splashscreen_info(mfx); - if (ret == -1) + if (__insert_application_info(db, mfx)) return -1; - - /*Insert in the package_app_splash_screen DB (backward compatibility)*/ - ret = __insert_application_metadata_splashscreen_info(mfx); - if (ret == -1) + if (__insert_package_privilege_info(db, mfx)) return -1; - - /*Insert in the package_app_splash_screen DB*/ - ret = __insert_application_splashscreen_info(mfx); - if (ret == -1) + if (__insert_package_appdefined_privilege_info(db, mfx)) + return -1; + if (__insert_package_dependency_info(db, mfx)) return -1; return 0; - } -static int __delete_appinfo_from_db(char *db_table, const char *appid) +API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) { - char query[MAX_QUERY_LEN] = { '\0' }; - int ret = -1; + int ret; + const char *dbpath; + sqlite3 *db; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "DELETE FROM %q where app_id=%Q", db_table, appid); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("DB Deletion from table (%s) Failed\n", db_table); - return -1; + if (mfx == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - return 0; -} -static int __delete_subpkg_info_from_db(char *appid) -{ - int ret = -1; + dbpath = __get_parser_db_path(uid); - ret = __delete_appinfo_from_db("package_app_info", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_localized_info", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_icon_section_info", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_image_info", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_control", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_category", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_metadata", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_permission", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_share_allowed", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_share_request", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_data_control", appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_splash_screen", appid); - if (ret < 0) - return ret; + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } - return 0; -} + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __insert_package_info(db, mfx)); + __END_TRANSACTION(db); -static int __delete_subpkg_from_db(manifest_x *mfx) -{ - char query[MAX_QUERY_LEN] = { '\0' }; - char *error_message = NULL; + sqlite3_close_v2(db); - sqlite3_snprintf(MAX_QUERY_LEN, query, "SELECT app_id FROM package_app_info WHERE package=%Q", mfx->package); - if (SQLITE_OK != - sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) { - _LOGE("Don't execute query = %s error message = %s\n", query, - error_message); - sqlite3_free(error_message); - return -1; - } - sqlite3_free(error_message); + return PM_PARSER_R_OK; +} - return 0; +API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx) +{ + return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid()); } -static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid) +static int __delete_package_info(sqlite3 *db, const char *pkgid) { - char query[MAX_QUERY_LEN] = { '\0' }; - int ret = -1; - GList *tmp; - application_x *app; + static const char query[] = + "DELETE FROM package_info WHERE package=?"; + int ret; + sqlite3_stmt *stmt; - /*Delete from Package Info DB*/ - sqlite3_snprintf(MAX_QUERY_LEN, query, - "DELETE FROM package_info WHERE package=%Q", mfx->package); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Info DB Delete Failed\n"); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - memset(query, '\0', MAX_QUERY_LEN); - /*Delete from Package Localized Info*/ - sqlite3_snprintf(MAX_QUERY_LEN, query, - "DELETE FROM package_localized_info WHERE package=%Q", mfx->package); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Localized Info DB Delete Failed\n"); - return -1; - } + __BIND_TEXT(db, stmt, 1, pkgid); - /*Delete from Package Privilege Info*/ - sqlite3_snprintf(MAX_QUERY_LEN, query, - "DELETE FROM package_privilege_info WHERE package=%Q", mfx->package); - ret = __exec_query(query); - if (ret == -1) { - _LOGD("Package Privilege Info DB Delete Failed\n"); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); return -1; } - for (tmp = mfx->application; tmp; tmp = tmp->next) { - app = (application_x *)tmp->data; - if (app == NULL) - continue; - ret = __delete_appinfo_from_db("package_app_info", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_localized_info", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_image_info", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_control", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_category", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_app_permission", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_share_request", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_data_control", app->appid); - if (ret < 0) - return ret; - ret = __delete_appinfo_from_db("package_app_splash_screen", app->appid); - if (ret < 0) - return ret; - } - - /* if main package has sub pkg, delete sub pkg data*/ - __delete_subpkg_from_db(mfx); + sqlite3_finalize(stmt); return 0; } -static int __disable_app(const char *appid) +API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, + uid_t uid) { - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_disable='true' WHERE app_id=%Q", - appid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Insert global app disable failed\n"); + int ret; + const char *dbpath; + sqlite3 *db; - return ret; + if (mfx == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; + } + + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __delete_package_info(db, mfx->package)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static int __enable_app(const char *appid) +API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx) { - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_disable='false' WHERE app_id=%Q", - appid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Insert global app disable failed\n"); - - return ret; + return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid()); } -static int __disable_pkg(const char *pkgid) +API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) { - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_info SET package_disable='true' WHERE package=%Q", - pkgid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Update pkg disable info has failed\n"); + int ret; + const char *dbpath; + sqlite3 *db; - return ret; + if (mfx == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; + } + + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __delete_package_info(db, mfx->package)); + __DO_TRANSACTION(db, __insert_package_info(db, mfx)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static int __enable_pkg(const char *pkgid) +API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx) { - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_info SET package_disable='false' WHERE package=%Q", - pkgid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Update pkg disable info has failed\n"); - - return ret; + return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid()); } -static int __check_appinfo_for_uid_table(const char *appid, uid_t uid) +static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid, + uid_t uid, bool is_disable) { - int ret = -1; - char query[MAX_QUERY_LEN] = { '\0', }; + static const char query[] = + "INSERT OR REPLACE INTO package_app_info_for_uid (" + " app_id, uid, is_disabled, is_splash_screen_enabled) " + "VALUES (?, ?, ?," + " (SELECT app_splash_screen_display FROM package_app_info" + " WHERE app_id=?))"; + int ret; sqlite3_stmt *stmt; - const char *val = NULL; + int idx = 1; - if (appid == NULL) - return -1; - - sqlite3_snprintf(MAX_QUERY_LEN, query, - "SELECT COUNT(*) FROM " - "package_app_info_for_uid WHERE app_id=%Q " - "AND uid=%d", appid, (int)uid); - - ret = sqlite3_prepare_v2(pkgmgr_parser_db, query, strlen(query), &stmt, NULL); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); if (ret != SQLITE_OK) { - LOGE("prepare failed: %s", sqlite3_errmsg(pkgmgr_parser_db)); - return PMINFO_R_ERROR; + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - if (sqlite3_step(stmt) != SQLITE_ROW) { - LOGE("failed to step"); + __BIND_TEXT(db, stmt, idx++, appid); + __BIND_INT(db, stmt, idx++, uid); + __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false"); + __BIND_TEXT(db, stmt, idx++, appid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); sqlite3_finalize(stmt); - return PMINFO_R_ERROR; + return -1; } - val = (const char *)sqlite3_column_text(stmt, 0); - ret = atoi(val); sqlite3_finalize(stmt); - return ret; + return 0; } -static int __disable_global_app_for_user(const char *appid, uid_t uid) +API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db( + const char *appid, uid_t uid, int is_disable) { - int ret = -1; - char query[MAX_QUERY_LEN] = { '\0', }; + int ret; + const char *dbpath; + sqlite3 *db; - ret = __check_appinfo_for_uid_table(appid, uid); - if (ret < 0) { - _LOGE("Failed to check package_app_info_for_uid with appid[%s], uid[%d]", - appid, (int)uid); - return -1; - } else if (ret == 0) { - sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO " - "package_app_info_for_uid(app_id, uid, is_disabled, is_splash_screen_enabled) " - "VALUES(%Q, %d, 'true', " - "(SELECT app_splash_screen_display FROM package_app_info WHERE appid='%Q'))", - appid, (int)uid, appid); - } else { - sqlite3_snprintf(MAX_QUERY_LEN, query, "UPDATE " - "package_app_info_for_uid SET is_disabled='true' " - "WHERE app_id=%Q AND uid=%d", appid, (int)uid); + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - ret = __exec_query(query); - if (ret == -1) - _LOGD("Add global app disable info failed\n"); + dbpath = __get_parser_db_path(GLOBAL_USER); - return ret; + ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid, + uid, (bool)is_disable)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static int __enable_global_app_for_user(const char *appid, uid_t uid) +static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid, + bool is_disable) { - int ret = -1; - char query[MAX_QUERY_LEN] = {'\0'}; + static const char query[] = + "UPDATE package_app_info SET app_disable=? " + "WHERE app_id=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - ret = __check_appinfo_for_uid_table(appid, uid); - if (ret < 0) { - _LOGE("Failed to check package_app_info_for_uid with appid[%s], uid[%d]", - appid, (int)uid); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; - } else if (ret > 0) { - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info_for_uid SET " - "is_disabled='false' WHERE app_id=%Q AND " - "uid=%d", appid, (int)uid); } - ret = __exec_query(query); - if (ret == -1) - _LOGD("Remove global app disable info failed\n"); - - return ret; -} - -static int __update_global_app_splash_screen_for_user(const char *appid, - uid_t uid, int flag) -{ - int ret = -1; - char query[MAX_QUERY_LEN] = { '\0', }; + __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false"); + __BIND_TEXT(db, stmt, idx++, appid); - ret = __check_appinfo_for_uid_table(appid, uid); - if (ret < 0) { - _LOGE("Failed to check package_app_info_for_uid with appid[%s], uid[%d]", - appid, (int)uid); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); return -1; - } else if (ret == 0) { - sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO " - "package_app_info_for_uid(app_id, uid, is_splash_screen_enabled) " - "VALUES(%Q, %d, %Q)", appid, (int)uid, - flag ? "true" : "false"); - } else { - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info_for_uid SET " - "is_splash_screen_enabled=%Q WHERE app_id=%Q AND " - "uid=%d", flag ? "true" : "false", appid, (int)uid); } - ret = __exec_query(query); - if (ret == -1) - _LOGD("update global app splash screen info failed\n"); + sqlite3_finalize(stmt); - return ret; + return 0; } -static int __disable_app_splash_screen(const char *appid) +API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid, + uid_t uid, int is_disable) { int ret; - char query[MAX_QUERY_LEN] = {'\0'}; + const char *dbpath; + sqlite3 *db; - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_splash_screen_display='false' WHERE app_id=%Q", - appid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Failed to update app_palsh_screen_display"); + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; + } - return ret; -} + dbpath = __get_parser_db_path(uid); -static int __enable_app_splash_screen(const char *appid) -{ - int ret; - char query[MAX_QUERY_LEN] = {'\0'}; + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_info SET app_splash_screen_display='true' WHERE app_id=%Q", - appid); - ret = __exec_query(query); - if (ret == -1) - _LOGD("Failed to update app_splash_screen_display"); + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_app_disable(db, appid, uid, + (bool)is_disable)); + __END_TRANSACTION(db); - return ret; + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -API int pkgmgr_parser_initialize_db(uid_t uid) +API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid, + int is_disable) { - int ret = -1; - char *db_path; + return pkgmgr_parser_update_app_disable_info_in_usr_db(appid, + __getuid(), is_disable); +} - /*Manifest DB*/ - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO); - if (ret == -1) { - _LOGD("package info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO); - if (ret == -1) { - _LOGD("package localized info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO); - if (ret == -1) { - _LOGD("package app app privilege DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO); - if (ret == -1) { - _LOGD("package app info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO); - if (ret == -1) { - _LOGD("package app localized info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO); - if (ret == -1) { - _LOGD("package app icon localized info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO); - if (ret == -1) { - _LOGD("package app image info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL); - if (ret == -1) { - _LOGD("package app app control DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY); - if (ret == -1) { - _LOGD("package app app category DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA); - if (ret == -1) { - _LOGD("package app app category DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION); - if (ret == -1) { - _LOGD("package app app permission DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED); - if (ret == -1) { - _LOGD("package app share allowed DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST); - if (ret == -1) { - _LOGD("package app share request DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL); - if (ret == -1) { - _LOGD("package app data control DB initialization failed\n"); - return ret; - } +static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid, + bool is_disable) +{ + static const char query[] = + "UPDATE package_info SET package_disable=? " + "WHERE package=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID); - if (ret == -1) { - _LOGD("package_app_info_for_uid for user DB initialization failed\n"); - return ret; + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN); - if (ret == -1) { - _LOGD("package app splash screen DB initialization failed\n"); - return ret; - } + __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false"); + __BIND_TEXT(db, stmt, idx++, pkgid); - /* Trigger package_app_info_for_uid */ - ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID); - if (ret == -1) { - _LOGD("package app info for uid DB initialization failed\n"); - return ret; + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - /*Cert DB*/ - /* TODO: refactor this code */ - ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO); - if (ret == -1) { - _LOGD("package cert info DB initialization failed\n"); - return ret; - } - ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO); - if (ret == -1) { - _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"); - return ret; - } - ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO); - if (ret == -1) { - _LOGD("package cert index info DB initialization failed\n"); - return ret; - } + sqlite3_finalize(stmt); - db_path = getUserPkgCertDBPathUID(GLOBAL_USER); - if (db_path == NULL) { - _LOGD("Failed to get user cert db path - GLOBAL_USER"); - } else { - ret = __parserdb_change_perm(db_path, GLOBAL_USER); - if (ret != 0) - _LOGD("Failed to change cert db permission"); - free(db_path); + return 0; +} + +API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid, + uid_t uid, int is_disable) +{ + int ret; + const char *dbpath; + sqlite3 *db; + + if (pkgid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - db_path = getUserPkgParserDBPathUID(uid); - if (db_path == NULL) { - _LOGD("Failed to get user pkg parser db path - %d", uid); - } else { - ret = __parserdb_change_perm(db_path, uid); - if (ret != 0) - _LOGD("Failed to change parser db permission\n"); - free(db_path); + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - return 0; + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid, + (bool)is_disable)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -static int __parserdb_change_perm(const char *db_file, uid_t uid) +API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid, + int is_disable) { - char buf[BUFSIZE]; - char pwuid_buf[1024]; - char journal_file[BUFSIZE]; - int fd; - struct stat sb; - char *files[3]; - int ret, i; - struct passwd userinfo, *result = NULL; - files[0] = (char *)db_file; - files[1] = journal_file; - files[2] = NULL; - mode_t mode; + return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid, + __getuid(), is_disable); +} - if (db_file == NULL) - return -1; +static int __set_global_app_splash_screen_for_uid(sqlite3 *db, + const char *appid, uid_t uid, bool is_enabled) +{ + static const char query[] = + "INSERT OR REPLACE INTO package_app_info_for_uid(" + " appid, uid, is_splash_screen_enabled) " + "VALUES (?, ?, ?)"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - if (getuid() != OWNER_ROOT) /* At this time we should be root to apply this */ - return 0; - snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); - if (uid == OWNER_ROOT) - uid = GLOBAL_USER; - ret = getpwuid_r(uid, &userinfo, pwuid_buf, sizeof(pwuid_buf), &result); - if (ret != 0 || result == NULL) { - _LOGE("FAIL: user %d doesn't exist", uid); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); - - for (i = 0; files[i]; i++) { - fd = open(files[i], O_RDONLY); - if (fd == -1) { - if (strerror_r(errno, buf, sizeof(buf))) - strncpy(buf, "", BUFSIZE - 1); - _LOGD("FAIL : open %s : %s", files[i], buf); - return -1; - } - ret = fstat(fd, &sb); - if (ret == -1) { - if (strerror_r(errno, buf, sizeof(buf))) - strncpy(buf, "", BUFSIZE - 1); - _LOGD("FAIL : fstat %s : %s", files[i], buf); - close(fd); - return -1; - } - if (S_ISLNK(sb.st_mode)) { - _LOGE("FAIL : %s is symlink!", files[i]); - close(fd); - return -1; - } - ret = fchown(fd, uid, userinfo.pw_gid); - if (ret == -1) { - if (strerror_r(errno, buf, sizeof(buf))) - strncpy(buf, "", BUFSIZE - 1); - _LOGD("FAIL : fchown %s %d.%d : %s", files[i], uid, - userinfo.pw_gid, buf); - close(fd); - return -1; - } - - mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; - if (!strcmp(db_file, getUserPkgCertDBPathUID(GLOBAL_USER))) - mode |= S_IWOTH; - ret = fchmod(fd, mode); - if (ret == -1) { - if (strerror_r(errno, buf, sizeof(buf))) - strncpy(buf, "", BUFSIZE - 1); - _LOGD("FAIL : fchmod %s 0664 : %s", files[i], buf); - close(fd); - return -1; - } - close(fd); - SET_SMACK_LABEL(files[i]); - } + + __BIND_TEXT(db, stmt, idx++, appid); + __BIND_INT(db, stmt, idx++, uid); + __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false"); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_finalize(stmt); + return 0; } -API int pkgmgr_parser_create_and_initialize_db(uid_t uid) +API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db( + const char *appid, uid_t uid, int flag) { int ret; - char *db_path; + const char *dbpath; + sqlite3 *db; - if (getuid() != OWNER_ROOT) { - _LOGE("Only root user is allowed"); - return -1; + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - db_path = getUserPkgParserDBPathUID(uid); - if (db_path == NULL) { - _LOGE("Failed to get pkg parser db path - %d", uid); - return -1; - } + dbpath = __get_parser_db_path(GLOBAL_USER); - if (access(db_path, F_OK) != -1) { - _LOGE("Manifest db for user %d is already exists", uid); - free(db_path); - return -1; + ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - free(db_path); - db_path = getUserPkgCertDBPathUID(uid); - if (db_path == NULL) { - _LOGE("Failed to get pkg cert db path - %d", uid); - return -1; - } + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db, + appid, uid, (bool)flag)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; +} + +static int __set_app_splash_screen(sqlite3 *db, const char *appid, + bool is_enabled) +{ + static const char query[] = + "UPDATE package_app_info SET app_splash_screen_display=? " + "WHERE app_id=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - if (access(db_path, F_OK) != -1) { - _LOGE("Cert db for user %d is already exists", uid); - free(db_path); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - free(db_path); - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret < 0) - return -1; - ret = pkgmgr_parser_initialize_db(uid); - if (ret < 0) { - pkgmgr_parser_close_db(); + __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false"); + __BIND_TEXT(db, stmt, idx++, appid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); return -1; } - pkgmgr_parser_close_db(); + + sqlite3_finalize(stmt); return 0; } -API int pkgmgr_parser_check_and_create_db(uid_t uid) +API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db( + const char *appid, uid_t uid, int flag) { - int ret = -1; - char *db_path; + int ret; + const char *dbpath; + sqlite3 *db; - db_path = getUserPkgParserDBPathUID(uid); - if (db_path == NULL) { - _LOGD("Failed to get pkg parser db path - %d", uid); - return -1; + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - /*Manifest DB*/ - ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, db_path); - if (ret) { - _LOGD("Manifest DB creation Failed\n"); - free(db_path); - return -1; - } - free(db_path); + dbpath = __get_parser_db_path(uid); - db_path = getUserPkgCertDBPathUID(GLOBAL_USER); - if (db_path == NULL) { - _LOGE("Failed to get pkg cert db path - GLOBAL_USER"); - return -1; + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - /*Cert DB*/ - ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, db_path); - if (ret) { - _LOGD("Cert DB creation Failed\n"); - free(db_path); - return -1; - } + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag)); + __END_TRANSACTION(db); - free(db_path); + sqlite3_close_v2(db); - return 0; + return PM_PARSER_R_OK; } -void pkgmgr_parser_close_db(void) +API int pkgmgr_parser_update_app_splash_screen_display_info_in_db( + const char *appid, int flag) { - sqlite3_close(pkgmgr_parser_db); - sqlite3_close(pkgmgr_cert_db); + return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db( + appid, __getuid(), flag); } - -API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx) +static int __set_app_label(sqlite3 *db, const char *appid, const char *label) { - _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n"); - if (mfx == NULL) { - _LOGD("manifest pointer is NULL\n"); + static const char query[] = + "UPDATE package_app_localized_info SET app_label=? " + "WHERE app_id=? AND app_label IS NOT NULL"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - int ret = 0; - ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + + __BIND_TEXT(db, stmt, idx++, label); + __BIND_TEXT(db, stmt, idx++, appid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - ret = pkgmgr_parser_initialize_db(GLOBAL_USER); - if (ret == -1) - goto err; - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin\n"); - 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); - goto err; - } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + + sqlite3_finalize(stmt); + + return 0; } -API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) +API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid, + uid_t uid, const char *label) { - _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n"); - if (mfx == NULL) { - _LOGD("manifest pointer is NULL\n"); - return -1; - } - int ret = 0; - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + int ret; + const char *dbpath; + sqlite3 *db; + + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - ret = pkgmgr_parser_initialize_db(uid); - if (ret == -1) - goto err; - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin\n"); - 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); - goto err; - } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); + + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_app_label(db, appid, label)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, const char *tep_path) +API int pkgmgr_parser_update_app_label_info_in_db(const char *appid, + const char *label) { - return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, _getuid()); + return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(), + label); } -API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, const char *tep_path, uid_t uid) +static int __set_app_icon(sqlite3 *db, const char *appid, const char *icon_path) { - if (pkgid == NULL || tep_path == NULL) { - _LOGE("invalid parameter"); + static const char query[] = + "UPDATE package_app_localized_info SET app_icon=? " + "WHERE app_id=? AND app_icon IS NOT NULL"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - int ret = -1; - char *query = NULL; + __BIND_TEXT(db, stmt, idx++, icon_path); + __BIND_TEXT(db, stmt, idx++, appid); - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - ret = pkgmgr_parser_initialize_db(uid); - if (ret == -1) - goto err; - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin\n"); + sqlite3_finalize(stmt); + return 0; +} - /* Updating TEP info in "package_info" table */ - query = sqlite3_mprintf("UPDATE package_info "\ - "SET package_tep_name = %Q "\ - "WHERE package = %Q", tep_path, pkgid); +API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid, + uid_t uid, const char *icon_path) +{ + int ret; + const char *dbpath; + sqlite3 *db; - ret = __exec_query(query); - sqlite3_free(query); - if (ret != SQLITE_OK) { - ret = PM_PARSER_R_ERROR; - _LOGE("sqlite exec failed to insert entries into package_info!!"); - goto err; + if (appid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - /* Updating TEP info in "package_app_info" table */ - query = sqlite3_mprintf("UPDATE package_app_info "\ - "SET app_tep_name = %Q "\ - "WHERE package = %Q", tep_path, pkgid); + dbpath = __get_parser_db_path(uid); - ret = __exec_query(query); - sqlite3_free(query); + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - ret = PM_PARSER_R_ERROR; - _LOGE("sqlite exec failed to insert entries into package_app_info!!"); - goto err; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGE("Failed to commit transaction, Rollback now\n"); - ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - if (ret != SQLITE_OK) - _LOGE("Failed to Rollback\n"); + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_app_icon(db, appid, icon_path)); + __END_TRANSACTION(db); - ret = PM_PARSER_R_ERROR; - goto err; - } - _LOGD("Transaction Commit and End\n"); - ret = PM_PARSER_R_OK; + sqlite3_close_v2(db); -err: - pkgmgr_parser_close_db(); - return ret; + return PM_PARSER_R_OK; } +API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid, + const char *icon_path) +{ + return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(), + icon_path); +} -API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid) +static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path) { - if (mfx == NULL) { - _LOGD("manifest pointer is NULL\n"); + static const char query[] = + "UPDATE package_info SET package_tep_name=? " + "WHERE package=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - int ret = 0; - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + + __BIND_TEXT(db, stmt, idx++, tep_path); + __BIND_TEXT(db, stmt, idx++, pkgid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - ret = pkgmgr_parser_initialize_db(uid); - if (ret == -1) - goto err; - /*Preserve guest mode visibility*/ - __preserve_guestmode_visibility_value(mfx); - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin\n"); - ret = __delete_manifest_info_from_db(mfx, uid); - if (ret == -1) { - _LOGD("Delete from DB failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - 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); - goto err; - } - - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; -} -API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx) -{ - return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, _getuid()); + sqlite3_finalize(stmt); + + return 0; } -API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid) +API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, + const char *tep_path, uid_t uid) { - if (mfx == NULL) { - _LOGD("manifest pointer is NULL\n"); - return -1; - } - int ret = 0; - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + int ret; + const char *dbpath; + sqlite3 *db; + + if (pkgid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin\n"); - ret = __delete_manifest_info_from_db(mfx, uid); - if (ret == -1) { - _LOGD("Delete from DB failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); + + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx) +API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, + const char *tep_path) { - return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, _getuid()); + return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, + __getuid()); } -API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(const char *appid, uid_t uid, int is_disable) +static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type, + const char **update_type) { - int ret = -1; + if (type == PMINFO_UPDATEINFO_NONE) + *update_type = PMINFO_UPDATEINFO_TYPE_NONE; + else if (type == PMINFO_UPDATEINFO_FORCE) + *update_type = PMINFO_UPDATEINFO_TYPE_FORCE; + else if (type == PMINFO_UPDATEINFO_OPTIONAL) + *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL; + else + return -1; + return 0; +} - ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; - } +static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info, + const char *update_type) +{ + static const char query[] = + "UPDATE package_update_info " + "SET update_version=?, update_type=? " + "WHERE package=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - _LOGD("Transaction Begin\n"); - if (is_disable) - ret = __disable_global_app_for_user(appid, uid); - else - ret = __enable_global_app_for_user(appid, uid); - if (ret == -1) { - _LOGD("__update_global_app_disable_condition_in_db failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; -} + __BIND_TEXT(db, stmt, idx++, info->version); + __BIND_TEXT(db, stmt, idx++, update_type); + __BIND_TEXT(db, stmt, idx++, info->pkgid); -API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid, int is_disable) -{ - return pkgmgr_parser_update_app_disable_info_in_usr_db(appid, _getuid(), is_disable); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; + } + + sqlite3_finalize(stmt); + + return 0; } -API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid, uid_t uid, int is_disable) +API int pkgmgr_parser_register_pkg_update_info_in_usr_db( + pkgmgrinfo_updateinfo_h handle, uid_t uid) { - int ret = -1; + int ret; + updateinfo_x *update_info; + updateinfo_x *prev_update_info; + pkgmgrinfo_updateinfo_h prev_update_handle; + pkgmgrinfo_pkginfo_h pkginfo; + pkgmgrinfo_version_compare_type compare_result; + bool is_global_pkg; + const char *update_type; + const char *dbpath; + sqlite3 *db; + + if (handle == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; + } - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + update_info = (updateinfo_x *)handle; + if (update_info->pkgid == NULL || update_info->version == NULL) + return PM_PARSER_R_EINVAL; + if (__convert_update_type(update_info->type, &update_type) != 0) + return PM_PARSER_R_EINVAL; + + ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid, + &prev_update_handle, uid); + if (ret != PMINFO_R_OK) + return PM_PARSER_R_ERROR; + + prev_update_info = (updateinfo_x *)prev_update_handle; + ret = pkgmgrinfo_compare_package_version(update_info->version, + prev_update_info->version, &compare_result); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_updateinfo_destroy(prev_update_handle); + return PM_PARSER_R_ERROR; } - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; + if (compare_result == PMINFO_VERSION_SAME && + prev_update_info->type == PMINFO_UPDATEINFO_NONE) { + _LOGI("Given update info version[%s] of pkgid[%s] " + "will be ignored", + update_info->version, update_info->pkgid); + pkgmgrinfo_updateinfo_destroy(prev_update_handle); + return PM_PARSER_R_OK; } + pkgmgrinfo_updateinfo_destroy(prev_update_handle); - _LOGD("Transaction Begin\n"); - if (is_disable) - ret = __disable_pkg(pkgid); - else - ret = __enable_pkg(pkgid); + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid, + &pkginfo); + if (ret != PMINFO_R_OK) + return PM_PARSER_R_ERROR; - if (ret == -1) { - _LOGD("__update_pkg_disable_condition_in_db failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; + ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + return PM_PARSER_R_ERROR; } + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + + dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid); - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info, + update_type)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid, int is_disable) +API int pkgmgr_parser_register_pkg_update_info_in_db( + pkgmgrinfo_updateinfo_h handle) { - return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid, _getuid(), is_disable); + return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle, + __getuid()); } -API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid, uid_t uid, int is_disable) +static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid) { - int ret = -1; + static const char query[] = + "UPDATE package_update_info SET update_type='none' " + "WHERE package=?"; + int ret; + sqlite3_stmt *stmt; + int idx = 1; - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); + return -1; } - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; + __BIND_TEXT(db, stmt, idx++, pkgid); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); + return -1; } - _LOGD("Transaction Begin\n"); - if (is_disable) - ret = __disable_app(appid); - else - ret = __enable_app(appid); - if (ret == -1) { - _LOGD("__update_app_disable_condition_in_db failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - /*Commit transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + sqlite3_finalize(stmt); + + return 0; } -API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(const char *appid, uid_t uid, int flag) +API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid, + uid_t uid) { - int ret = -1; + int ret; + const char *dbpath; + sqlite3 *db; + pkgmgrinfo_pkginfo_h pkginfo; + bool is_global_pkg; - if (appid == NULL) { - _LOGD("Invalid parameter"); - return -1; + if (pkgid == NULL) { + _LOGE("invalid parameter"); + return PM_PARSER_R_EINVAL; } - ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER); - if (ret == -1) { - _LOGD("Failed to open DB\n"); - return ret; - } + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo); + if (ret != PMINFO_R_OK) + return PM_PARSER_R_EINVAL; - /* Begin transaction */ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); + ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + if (ret != PMINFO_R_OK) + return PM_PARSER_R_ERROR; + + dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - _LOGD("Transaction Begin\n"); - ret = __update_global_app_splash_screen_for_user(appid, uid, flag); - if (ret == -1) { - _LOGD("__update_splash_screen_disable_condition_in_db failed. Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - /* Commit transaction */ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now\n"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; - } - _LOGD("Transaction Commit and End\n"); -err: - pkgmgr_parser_close_db(); - return ret; + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } -API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(const char *appid, int flag) +API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid) { - return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(appid, _getuid(), flag); + return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid, + __getuid()); } -API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(const char *appid, uid_t uid, int flag) +static int __unregister_all_pkg_update_info(sqlite3 *db) { + static const char query[] = + "UPDATE package_update_info SET update_type='none'"; int ret; + sqlite3_stmt *stmt; - if (appid == NULL) { - _LOGD("Invalid parameter"); + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _LOGE("prepare failed: %s", sqlite3_errmsg(db)); return -1; } - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB"); + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _LOGE("step failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(stmt); return -1; } - /* Begin transaction */ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction"); - ret = -1; - goto err; - } - _LOGD("Transaction Begin"); + sqlite3_finalize(stmt); - if (flag) - ret = __enable_app_splash_screen(appid); - else - ret = __disable_app_splash_screen(appid); - if (ret == -1) { - _LOGD("__update_app_splash_screen_condition_in_db. Rollback now"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } - /* Commit transaction */ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); + return 0; +} + +API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid) +{ + int ret; + const char *dbpath; + sqlite3 *db; + + dbpath = __get_parser_db_path(uid); + + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - _LOGD("Transaction Commit and End"); -err: - pkgmgr_parser_close_db(); - return ret; + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; +} + +API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void) +{ + return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db( + __getuid()); } -API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid, uid_t uid, const char *label) +API int pkgmgr_parser_register_pkg_plugin_info_in_usr_db( + manifest_x *mfx, uid_t uid) { int ret; - char query[MAX_QUERY_LEN] = {'\0'}; + const char *dbpath; + sqlite3 *db; - if (appid == NULL || label == NULL) { - _LOGE("Invalid parameter"); - return -1; + if (!mfx) + return PM_PARSER_R_EINVAL; + dbpath = __get_parser_db_path(uid); + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - ret = pkgmgr_parser_check_and_create_db(uid); - if (ret == -1) { - _LOGD("Failed to open DB"); - return -1; - } + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx)); + __END_TRANSACTION(db); - /*Begin transaction*/ - ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _LOGD("Failed to begin transaction\n"); - ret = -1; - goto err; - } + sqlite3_close_v2(db); - sqlite3_snprintf(MAX_QUERY_LEN, query, - "UPDATE package_app_localized_info SET app_label=%Q WHERE " \ - "app_id=%Q AND app_label IS NOT NULL", - label, appid); - ret = __exec_query(query); - if (ret == -1) { - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - goto err; - } + return PM_PARSER_R_OK; +} + +API int pkgmgr_parser_register_pkg_plugin_info_in_db(manifest_x *mfx) +{ + return pkgmgr_parser_register_pkg_plugin_info_in_usr_db(mfx, __getuid()); +} + +API int pkgmgr_parser_update_pkg_plugin_info_in_usr_db( + manifest_x *mfx, uid_t uid) +{ + int ret; + const char *dbpath; + sqlite3 *db; - /* Commit transaction */ - ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL); + if (!mfx) + return PM_PARSER_R_EINVAL; + dbpath = __get_parser_db_path(uid); + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); if (ret != SQLITE_OK) { - _LOGD("Failed to commit transaction, Rollback now"); - sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL); - ret = -1; - goto err; + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; } - _LOGD("Transaction Commit and End"); -err: - pkgmgr_parser_close_db(); - return ret; + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, mfx->package)); + __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; +} + +API int pkgmgr_parser_update_pkg_plugin_info_in_db(manifest_x *mfx) +{ + return pkgmgr_parser_update_pkg_plugin_info_in_usr_db(mfx, __getuid()); } -API int pkgmgr_parser_update_app_label_info_in_db(const char *appid, const char *label) +API int pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db( + const char *pkgid, uid_t uid) { - return pkgmgr_parser_update_app_label_info_in_usr_db(appid, _getuid(), label); + int ret; + const char *dbpath; + sqlite3 *db; + + dbpath = __get_parser_db_path(uid); + ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE); + if (ret != SQLITE_OK) { + _LOGE("open db failed: %d", ret); + return PM_PARSER_R_ERROR; + } + + __BEGIN_TRANSACTION(db); + __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, pkgid)); + __END_TRANSACTION(db); + + sqlite3_close_v2(db); + + return PM_PARSER_R_OK; } +API int pkgmgr_parser_unregister_pkg_plugin_info_in_db(const char *pkgid) +{ + return pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid, __getuid()); +} \ No newline at end of file