Remove warnings
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.c
old mode 100755 (executable)
new mode 100644 (file)
index 3ccf12a..1a4a0bd
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sys/smack.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <db-util.h>
 #include <glib.h>
+#include <grp.h>
+#include <pwd.h>
+
+/* For multi-user support */
+#include <tzplatform_config.h>
+
+#include "pkgmgr-info.h"
 #include "pkgmgr_parser_internal.h"
 #include "pkgmgr_parser_db.h"
 
-#define PKGMGR_PARSER_DB_FILE "/opt/dbspace/.pkgmgr_parser.db"
-#define PKGMGR_CERT_DB_FILE "/opt/dbspace/.pkgmgr_cert.db"
+#include "pkgmgr-info-debug.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "PKGMGR_PARSER"
+
+#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
+#define OWNER_ROOT 0
+
+#define SET_SMACK_LABEL(x) \
+do { \
+       if (smack_setlabel((x), "System::Shared", SMACK_LABEL_ACCESS)) \
+               _LOGE("failed chsmack -a \"System::Shared\" %s", x); \
+       else \
+               _LOGD("chsmack -a \"System::Shared\" %s", x); \
+} while (0)
+
 sqlite3 *pkgmgr_parser_db;
 sqlite3 *pkgmgr_cert_db;
-GList *pkglocale = NULL;
-GList *applocale = NULL;
-GList *appicon = NULL;
-GList *appimage = NULL;
-char *prev = NULL;
+
 
 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
                                                "(package text primary key not null, " \
                                                "package_type text DEFAULT 'rpm', " \
                                                "package_version text, " \
+                                               "package_api_version text, " \
                                                "install_location text, " \
                                                "package_size text, " \
                                                "package_removable text DEFAULT 'true', " \
@@ -53,6 +76,8 @@ char *prev = NULL;
                                                "package_readonly text DEFAULT 'false', " \
                                                "package_update text DEFAULT 'false', " \
                                                "package_appsetting text DEFAULT 'false', " \
+                                               "package_nodisplay text DEFAULT 'false', " \
+                                               "package_system text DEFAULT 'false', " \
                                                "author_name text, " \
                                                "author_email text, " \
                                                "author_href text," \
@@ -61,7 +86,8 @@ char *prev = NULL;
                                                "storeclient_id text," \
                                                "mainapp_id text," \
                                                "package_url text," \
-                                               "root_path text)"
+                                               "root_path text," \
+                                               "csc_path text )"
 
 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
                                                "(package text not null, " \
@@ -76,6 +102,14 @@ char *prev = NULL;
                                                "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, " \
+                                               "PRIMARY KEY(package, privilege) " \
+                                               "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, " \
@@ -88,12 +122,20 @@ char *prev = NULL;
                                                "app_taskmanage text DEFAULT 'false', " \
                                                "app_enabled text DEFAULT 'true', " \
                                                "app_hwacceleration text DEFAULT 'use-system-setting', " \
+                                               "app_screenreader text DEFAULT 'use-system-setting', " \
                                                "app_mainapp text, " \
                                                "app_recentimage text, " \
+                                               "app_launchcondition text, " \
                                                "app_indicatordisplay text DEFAULT 'true', " \
                                                "app_portraitimg text, " \
                                                "app_landscapeimg text, " \
                                                "app_guestmodevisibility text DEFAULT 'true', " \
+                                               "app_permissiontype text DEFAULT 'normal', " \
+                                               "app_preload text DEFAULT 'false', " \
+                                               "app_submode text DEFAULT 'false', " \
+                                               "app_submode_mainid text, " \
+                                               "app_launch_mode text NOT NULL DEFAULT 'caller', " \
+                                               "component_type text, " \
                                                "package text not null, " \
                                                "FOREIGN KEY(package) " \
                                                "REFERENCES package_info(package) " \
@@ -131,11 +173,8 @@ char *prev = NULL;
 
 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
                                                "(app_id text not null, " \
-                                               "operation text not null, " \
-                                               "uri_scheme text, " \
-                                               "mime_type text, " \
-                                               "subapp_name text, " \
-                                               "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
+                                               "app_control text not null, " \
+                                               "PRIMARY KEY(app_id,app_control) " \
                                                "FOREIGN KEY(app_id) " \
                                                "REFERENCES package_app_info(app_id) " \
                                                "ON DELETE CASCADE)"
@@ -172,7 +211,7 @@ char *prev = NULL;
                                                "(app_id text not null, " \
                                                "pm_type text not null, " \
                                                "pm_value text not null, " \
-                                               "PRIMARY KEY(app_id) " \
+                                               "PRIMARY KEY(app_id, pm_type, pm_value) " \
                                                "FOREIGN KEY(app_id) " \
                                                "REFERENCES package_app_info(app_id) " \
                                                "ON DELETE CASCADE)"
@@ -213,6 +252,28 @@ char *prev = NULL;
                                                "dist2_signer_cert integer, " \
                                                "PRIMARY KEY(package)) "
 
+#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_RESOURCE_INFO "CREATE TABLE IF NOT EXISTS package_resource_info" \
+                                               "(pkg_id text NOT NULL," \
+                                               "group_folder text NOT NULL," \
+                                               "group_type text NOT NULL," \
+                                               "PRIMARY KEY(pkg_id, group_type))"
+
+#define QUERY_CREATE_TABLE_PACKAGE_RESOURCE_DATA "CREATE TABLE IF NOT EXISTS package_resource_data" \
+                                               "(id integer REFERENCES package_resource_info(rowid)," \
+                                               "node_folder text NOT NULL," \
+                                               "attr_name text NOT NULL," \
+                                               "attr_value test NOT NULL," \
+                                               "PRIMARY KEY(id, node_folder, attr_name))"
+
 static int __insert_uiapplication_info(manifest_x *mfx);
 static int __insert_serviceapplication_info(manifest_x *mfx);
 static int __insert_uiapplication_appsvc_info(manifest_x *mfx);
@@ -227,36 +288,58 @@ static int __insert_uiapplication_share_allowed_info(manifest_x *mfx);
 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx);
 static int __insert_uiapplication_share_request_info(manifest_x *mfx);
 static int __insert_serviceapplication_share_request_info(manifest_x *mfx);
+static int __insert_uiapplication_datacontrol_info(manifest_x *mfx);
+static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx);
 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata);
 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata);
 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
-static int __insert_manifest_info_in_db(manifest_x *mfx);
-static int __update_manifest_info_in_db(manifest_x *mfx);
-static int __delete_manifest_info_from_db(manifest_x *mfx);
-static int __initialize_package_info_db();
-static int __initialize_package_localized_info_db();
-static int __initialize_package_app_info_db();
-static int __initialize_package_cert_info_db();
-static int __initialize_package_cert_index_info_db();
-static int __initialize_package_app_localized_info_db();
-static int __initialize_package_app_icon_section_info_db();
-static int __initialize_package_app_image_info_db();
-static int __initialize_package_app_app_svc_db();
-static int __initialize_package_app_app_category_db();
-static int __initialize_package_app_app_control_db();
-static int __initialize_package_app_app_metadata_db();
-static int __initialize_package_app_share_allowed_db();
-static int __initialize_package_app_share_request_db();
+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, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
                char **label, char **license, char **icon, char **description, char **author);
-
 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
-static void __trimfunc1(gpointer data, gpointer userdata);
-static void __trimfunc2(gpointer data, gpointer userdata);
 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *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);
+
+static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
+{
+       if (coltxt[0])
+               __delete_subpkg_info_from_db(coltxt[0]);
+
+       return 0;
+}
+
+static const char *__get_str(const char *str)
+{
+       if (str == NULL)
+       {
+               return PKGMGR_PARSER_EMPTY_STR;
+       }
+
+       return str;
+}
+
+static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
+{
+       int ret = -1;
+       sqlite3 *handle;
+
+       ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
+       if (ret != SQLITE_OK) {
+               _LOGD("connect db [%s] failed!\n", db_path);
+               return -1;
+       }
+       *db_handle = handle;
+
+       return 0;
+}
 
 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
 {
@@ -276,6 +359,10 @@ static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char
                                status = strdup(coltxt[i]);
                }
        }
+       if (appid == NULL) {
+               _LOGD("app id is NULL\n");
+               return -1;
+       }
        /*update guest mode visibility*/
        for (; uiapp != NULL; uiapp = uiapp->next) {
                if (strcmp(uiapp->appid, appid) == 0) {
@@ -304,268 +391,21 @@ static void __preserve_guestmode_visibility_value(manifest_x *mfx)
        if (SQLITE_OK !=
            sqlite3_exec(pkgmgr_parser_db, query,
                         __guestmode_visibility_cb, (void *)mfx, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
+               _LOGD("Don't execute query = %s error message = %s\n",
                       query, error_message);
                sqlite3_free(error_message);
        }
        return;
 }
 
-static int __initialize_package_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO,
-                        NULL, NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_INFO, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __pkgmgr_parser_cert_create_db()
-{
-       int ret = -1;
-       if (access(PKGMGR_CERT_DB_FILE, F_OK) == 0) {
-               ret =
-                   db_util_open(PKGMGR_CERT_DB_FILE, &pkgmgr_cert_db,
-                                DB_UTIL_REGISTER_HOOK_METHOD);
-               if (ret != SQLITE_OK) {
-                       DBG("connect db [%s] failed!\n",
-                              PKGMGR_CERT_DB_FILE);
-                       return -1;
-               }
-               return 0;
-       }
-       DBG("Pkgmgr DB does not exists. Create one!!\n");
-
-       ret =
-           db_util_open(PKGMGR_CERT_DB_FILE, &pkgmgr_cert_db,
-                        DB_UTIL_REGISTER_HOOK_METHOD);
-
-       if (ret != SQLITE_OK) {
-               DBG("connect db [%s] failed!\n", PKGMGR_CERT_DB_FILE);
-               return -1;
-       }
-       return 0;
-}
-
-static int __initialize_package_cert_info_db()
-{
-       char *error_message = NULL;
-       int ret = -1;
-       ret = __pkgmgr_parser_cert_create_db();
-       if (ret == -1) {
-               DBG("Failed to open DB\n");
-               return ret;
-       }
-
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO,
-                        NULL, NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_CERT_INFO, error_message);
-               sqlite3_free(error_message);
-               sqlite3_close(pkgmgr_cert_db);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_cert_index_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO,
-                        NULL, NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO, error_message);
-               sqlite3_free(error_message);
-               sqlite3_close(pkgmgr_cert_db);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       sqlite3_close(pkgmgr_cert_db);
-       return 0;
-}
-
-static int __initialize_package_localized_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO,
-                      error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_info_db()
+static int __initialize_db(sqlite3 *db_handle, const char *db_query)
 {
        char *error_message = NULL;
        if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO,
+           sqlite3_exec(db_handle, db_query,
                         NULL, NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_INFO, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_localized_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO, NULL,
-                        NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO,
-                      error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_icon_section_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO, NULL,
-                        NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO,
-                      error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_image_info_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO, NULL,
-                        NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO,
-                      error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_app_control_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_app_category_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_app_metadata_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_app_permission_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_app_svc_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC, NULL, NULL,
-                        &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC, error_message);
-               sqlite3_free(error_message);
-               return -1;
-       }
-       sqlite3_free(error_message);
-       return 0;
-}
-
-static int __initialize_package_app_share_allowed_db()
-{
-       char *error_message = NULL;
-       if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED, NULL,
-                        NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED,
-                      error_message);
+               _LOGD("Don't execute query = %s error message = %s\n",
+                      db_query, error_message);
                sqlite3_free(error_message);
                return -1;
        }
@@ -573,15 +413,12 @@ static int __initialize_package_app_share_allowed_db()
        return 0;
 }
 
-static int __initialize_package_app_share_request_db()
+static int __exec_query(char *query)
 {
        char *error_message = NULL;
        if (SQLITE_OK !=
-           sqlite3_exec(pkgmgr_parser_db,
-                        QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST, NULL,
-                        NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n",
-                      QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST,
+           sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
+               _LOGD("Don't execute query = %s error message = %s\n", query,
                       error_message);
                sqlite3_free(error_message);
                return -1;
@@ -590,19 +427,18 @@ static int __initialize_package_app_share_request_db()
        return 0;
 }
 
-static int __exec_query(char *query)
+static int __exec_query_no_msg(char *query)
 {
        char *error_message = NULL;
        if (SQLITE_OK !=
            sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
-               DBG("Don't execute query = %s error message = %s\n", query,
-                      error_message);
                sqlite3_free(error_message);
                return -1;
        }
        sqlite3_free(error_message);
        return 0;
 }
+
 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
 {
 
@@ -662,57 +498,31 @@ static GList *__create_image_list(GList *locale, image_x *image)
        return locale;
 }
 
-static void __printfunc(gpointer data, gpointer userdata)
-{
-       DBG("%s  ", (char*)data);
-}
-
-static void __trimfunc1(gpointer data, gpointer userdata)
-{
-       if (prev) {
-               if (strcmp((char *)data, prev) == 0) {
-                       pkglocale = g_list_remove(pkglocale, data);
-               } else
-                       prev = (char *)data;
-       }
-       else
-               prev = (char *)data;
-}
-
-static void __trimfunc2(gpointer data, gpointer userdata)
-{
-       if (prev) {
-               if (strcmp((char *)data, prev) == 0) {
-                       applocale = g_list_remove(applocale, data);
-               } else
-                       prev = (char *)data;
-       }
-       else
-               prev = (char *)data;
-}
-
-static void __trimfunc3(gpointer data, gpointer userdata)
-{
-       if (prev) {
-               if (strcmp((char *)data, prev) == 0) {
-                       appicon = g_list_remove(appicon, data);
-               } else
-                       prev = (char *)data;
-       }
-       else
-               prev = (char *)data;
-}
-
-static void __trimfunc4(gpointer data, gpointer userdata)
+static void __trimfunc(GList* trim_list)
 {
-       if (prev) {
-               if (strcmp((char *)data, prev) == 0) {
-                       appimage = g_list_remove(appimage, data);
-               } else
-                       prev = (char *)data;
+       char *trim_data = NULL;
+       char *prev = NULL;
+
+       GList *list = NULL;
+       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);
        }
-       else
-               prev = (char *)data;
 }
 
 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
@@ -725,6 +535,7 @@ static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
                return -1;
        if (strcmp((char*)a, (char*)b) > 0)
                return 1;
+       return 0;
 }
 
 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
@@ -798,7 +609,7 @@ static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **
        }
 }
 
-static void __extract_image_data(gpointer data, icon_x *image, char **lang, char **img)
+static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
 {
        while(image != NULL)
        {
@@ -833,13 +644,21 @@ static void __insert_pkglocale_info(gpointer data, gpointer userdata)
        __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
        if (!label && !description && !icon && !license && !author)
                return;
-       snprintf(query, MAX_QUERY_LEN, "insert into package_localized_info(package, package_locale, " \
+
+       sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
                "package_label, package_icon, package_description, package_license, package_author) values " \
-               "('%s', '%s', '%s', '%s', '%s', '%s', '%s')", mfx->package, (char*)data,
-               label, icon, description, license, author);
+               "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
+               mfx->package,
+               (char*)data,
+               label,
+               icon,
+               __get_str(description),
+               __get_str(license),
+               __get_str(author));
+
        ret = __exec_query(query);
        if (ret == -1)
-               DBG("Package Localized Info DB Insert failed\n");
+               _LOGD("Package Localized Info DB Insert failed\n");
 }
 
 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
@@ -862,8 +681,29 @@ static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
                label, icon);
        ret = __exec_query(query);
        if (ret == -1)
-               DBG("Package UiApp Localized Info DB Insert failed\n");
-
+               _LOGD("Package UiApp Localized Info DB Insert failed\n");
+
+       /*insert ui app locale info to pkg locale to get mainapp data */
+       if (strcasecmp(up->mainapp, "true")==0) {
+               sqlite3_snprintf(MAX_QUERY_LEN, query, "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')",
+                       up->package,
+                       (char*)data,
+                       label,
+                       icon,
+                       PKGMGR_PARSER_EMPTY_STR,
+                       PKGMGR_PARSER_EMPTY_STR,
+                       PKGMGR_PARSER_EMPTY_STR);
+
+               ret = __exec_query_no_msg(query);
+
+               if (icon != NULL) {
+                       sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
+                               "where package='%s' and package_locale='%s'", icon, up->package, (char*)data);
+                       ret = __exec_query_no_msg(query);
+               }
+       }
 }
 
 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
@@ -886,7 +726,7 @@ static void __insert_uiapplication_icon_section_info(gpointer data, gpointer use
 
        ret = __exec_query(query);
        if (ret == -1)
-               DBG("Package UiApp Localized Info DB Insert failed\n");
+               _LOGD("Package UiApp Localized Info DB Insert failed\n");
 
 }
 
@@ -909,7 +749,7 @@ static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
 
        ret = __exec_query(query);
        if (ret == -1)
-               DBG("Package UiApp image Info DB Insert failed\n");
+               _LOGD("Package UiApp image Info DB Insert failed\n");
 
 }
 
@@ -934,7 +774,7 @@ static void __insert_serviceapplication_locale_info(gpointer data, gpointer user
                label, icon);
        ret = __exec_query(query);
        if (ret == -1)
-               DBG("Package ServiceApp Localized Info DB Insert failed\n");
+               _LOGD("Package ServiceApp Localized Info DB Insert failed\n");
 }
 
 static int __insert_ui_mainapp_info(manifest_x *mfx)
@@ -949,7 +789,7 @@ static int __insert_ui_mainapp_info(manifest_x *mfx)
 
                ret = __exec_query(query);
                if (ret == -1) {
-                       DBG("Package UiApp Info DB Insert Failed\n");
+                       _LOGD("Package UiApp Info DB Insert Failed\n");
                        return -1;
                }
                if (strcasecmp(up->mainapp, "True")==0)
@@ -960,37 +800,30 @@ static int __insert_ui_mainapp_info(manifest_x *mfx)
        }
 
        if (mfx->mainapp_id == NULL){
-               if (mfx->uiapplication
-               && mfx->uiapplication->appid) {
-                       snprintf(query, MAX_QUERY_LEN,
-                       "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
-               } else if (mfx->serviceapplication
-                       && mfx->serviceapplication->appid) {
-                       snprintf(query, MAX_QUERY_LEN,
-                        "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->serviceapplication->appid);
-                } else {
-                       DBG("Not valid appid\n");
-                        return -1;
+               if (mfx->uiapplication && mfx->uiapplication->appid) {
+                       snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
+               } else {
+                       _LOGD("Not valid appid\n");
+                       return -1;
                }
 
                ret = __exec_query(query);
                if (ret == -1) {
-                       DBG("Package UiApp Info DB Insert Failed\n");
+                       _LOGD("Package UiApp Info DB Insert Failed\n");
                        return -1;
                }
-                if (mfx->uiapplication && mfx->uiapplication->appid)
-                        mfx->mainapp_id = strdup(mfx->uiapplication->appid);
-                else if (mfx->serviceapplication && mfx->serviceapplication->appid)
-                        mfx->mainapp_id = strdup(mfx->serviceapplication->appid);
 
-}
+               free((void *)mfx->uiapplication->mainapp);
+               mfx->uiapplication->mainapp= strdup("true");
+               mfx->mainapp_id = strdup(mfx->uiapplication->appid);
+       }
 
        memset(query, '\0', MAX_QUERY_LEN);
        snprintf(query, MAX_QUERY_LEN,
                "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
        ret = __exec_query(query);
        if (ret == -1) {
-               DBG("Package Info DB update Failed\n");
+               _LOGD("Package Info DB update Failed\n");
                return -1;
        }
 
@@ -1008,13 +841,40 @@ static int __insert_uiapplication_info(manifest_x *mfx)
        {
                snprintf(query, MAX_QUERY_LEN,
                         "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_mainapp , app_recentimage, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, package) " \
-                       "values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
-                        up->appid, "uiapp", up->exec, up->nodisplay, up->type, "\0", up->multiple,
-                        "\0", up->taskmanage, up->enabled, up->hwacceleration,up->mainapp, up->recentimage, up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_visibility, mfx->package);
+                       "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, component_type, package, app_launch_mode) " \
+                       "values('%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
+                        up->appid,
+                        "uiapp",
+                        up->exec,
+                        up->nodisplay,
+                        up->type,
+                        PKGMGR_PARSER_EMPTY_STR,
+                        up->multiple,
+                        PKGMGR_PARSER_EMPTY_STR,
+                        up->taskmanage,
+                        up->enabled,
+                        up->hwacceleration,
+                        up->screenreader,
+                        up->mainapp,
+                        __get_str(up->recentimage),
+                        up->launchcondition,
+                        up->indicatordisplay,
+                        __get_str(up->portraitimg),
+                        __get_str(up->landscapeimg),
+                        up->guestmode_visibility,
+                        up->permission_type,
+                        mfx->preload,
+                        up->submode,
+                        __get_str(up->submode_mainid),
+                        up->component_type,
+                        mfx->package,
+                        up->launch_mode);
+
                ret = __exec_query(query);
                if (ret == -1) {
-                       DBG("Package UiApp Info DB Insert Failed\n");
+                       _LOGD("Package UiApp Info DB Insert Failed\n");
                        return -1;
                }
                up = up->next;
@@ -1040,7 +900,7 @@ static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
                                 up->appid, ct->name);
                        ret = __exec_query(query);
                        if (ret == -1) {
-                               DBG("Package UiApp Category Info DB Insert Failed\n");
+                               _LOGD("Package UiApp Category Info DB Insert Failed\n");
                                return -1;
                        }
                        ct = ct->next;
@@ -1069,7 +929,7 @@ static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
                                         up->appid, md->key, md->value);
                                ret = __exec_query(query);
                                if (ret == -1) {
-                                       DBG("Package UiApp Metadata Info DB Insert Failed\n");
+                                       _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
                                        return -1;
                                }
                        }
@@ -1098,7 +958,7 @@ static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
                                 up->appid, pm->type, pm->value);
                        ret = __exec_query(query);
                        if (ret == -1) {
-                               DBG("Package UiApp permission Info DB Insert Failed\n");
+                               _LOGD("Package UiApp permission Info DB Insert Failed\n");
                                return -1;
                        }
                        pm = pm->next;
@@ -1113,69 +973,26 @@ static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
 {
        uiapplication_x *up = mfx->uiapplication;
        appcontrol_x *acontrol = NULL;
-       operation_x *op = NULL;
-       mime_x *mi = NULL;
-       uri_x *ui = NULL;
-       subapp_x *sub = NULL;
        int ret = -1;
        char query[MAX_QUERY_LEN] = {'\0'};
-       char *operation = NULL;
-       char *mime = NULL;
-       char *uri = NULL;
-       char *subapp = NULL;
-       while(up != NULL)
-       {
+       char buf[BUFSIZE] = {'\0'};
+       while (up != NULL) {
                acontrol = up->appcontrol;
-               while(acontrol != NULL)
-               {
-                       op = acontrol->operation;
-                       while(op != NULL)
-                       {
-                               if (op)
-                                       operation = op->name;
-                               mi = acontrol->mime;
-
-                               do
-                               {
-                                       if (mi)
-                                               mime = mi->name;
-                                       sub = acontrol->subapp;
-                                       do
-                                       {
-                                               if (sub)
-                                                       subapp = sub->name;
-                                               ui = acontrol->uri;
-                                               do
-                                               {
-                                                       if (ui)
-                                                               uri = ui->name;
-                                                       snprintf(query, MAX_QUERY_LEN,
-                                                                "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type, subapp_name) " \
-                                                               "values('%s', '%s', '%s', '%s', '%s')",\
-                                                                up->appid, operation, uri, mime, subapp);
-
-                                                       ret = __exec_query(query);
-                                                       if (ret == -1) {
-                                                               DBG("Package UiApp AppSvc DB Insert Failed\n");
-                                                               return -1;
-                                                       }
-                                                       memset(query, '\0', MAX_QUERY_LEN);
-                                                       if (ui)
-                                                               ui = ui->next;
-                                                       uri = NULL;
-                                               } while(ui != NULL);
-                                               if (sub)
-                                                       sub = sub->next;
-                                               subapp = NULL;
-                                       }while(sub != NULL);
-                                       if (mi)
-                                               mi = mi->next;
-                                       mime = NULL;
-                               }while(mi != NULL);
-                               if (op)
-                                       op = op->next;
-                               operation = NULL;
+               while (acontrol != NULL) {
+                       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");
+                       snprintf(query, MAX_QUERY_LEN,
+                                       "insert into package_app_app_control(app_id, app_control) " \
+                                       "values('%s', '%s')",\
+                                       up->appid, buf);
+                       ret = __exec_query(query);
+                       if (ret == -1) {
+                               _LOGD("Package UiApp AppSvc DB Insert Failed\n");
+                               return -1;
                        }
+                       memset(query, '\0', MAX_QUERY_LEN);
                        acontrol = acontrol->next;
                }
                up = up->next;
@@ -1193,10 +1010,10 @@ static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
        subapp_x *sub = NULL;
        int ret = -1;
        char query[MAX_QUERY_LEN] = {'\0'};
-       char *operation = NULL;
-       char *mime = NULL;
-       char *uri = NULL;
-       char *subapp = NULL;
+       const char *operation = NULL;
+       const char *mime = NULL;
+       const char *uri = NULL;
+       const char *subapp = NULL;
        while(up != NULL)
        {
                asvc = up->appsvc;
@@ -1226,11 +1043,15 @@ static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
                                                        snprintf(query, MAX_QUERY_LEN,
                                                                 "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
                                                                "values('%s', '%s', '%s', '%s', '%s')",\
-                                                                up->appid, operation, uri, mime, subapp);
+                                                                up->appid,
+                                                                operation,
+                                                                __get_str(uri),
+                                                                __get_str(mime),
+                                                                __get_str(subapp));
 
                                                        ret = __exec_query(query);
                                                        if (ret == -1) {
-                                                               DBG("Package UiApp AppSvc DB Insert Failed\n");
+                                                               _LOGD("Package UiApp AppSvc DB Insert Failed\n");
                                                                return -1;
                                                        }
                                                        memset(query, '\0', MAX_QUERY_LEN);
@@ -1257,6 +1078,39 @@ static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
        return 0;
 }
 
+static int __insert_uiapplication_datacontrol_info(manifest_x *mfx)
+{
+       uiapplication_x *up = mfx->uiapplication;
+       datacontrol_x *dc = NULL;
+       int ret = -1;
+       char query[MAX_QUERY_LEN] = {'\0'};
+
+       while(up != NULL)
+       {
+               dc = up->datacontrol;
+               while(dc != NULL)
+               {
+                       snprintf(query, MAX_QUERY_LEN,
+                                       "insert into package_app_data_control(app_id, providerid, access, type) " \
+                                       "values('%s', '%s', '%s', '%s')",\
+                                       mfx->uiapplication->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);
+                       dc = dc->next;
+               }
+               up = up->next;
+       }
+       return 0;
+}
+
 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
 {
        uiapplication_x *up = mfx->uiapplication;
@@ -1278,7 +1132,7 @@ static int __insert_uiapplication_share_request_info(manifest_x *mfx)
                                         up->appid, rq->text);
                                ret = __exec_query(query);
                                if (ret == -1) {
-                                       DBG("Package UiApp Share Request DB Insert Failed\n");
+                                       _LOGD("Package UiApp Share Request DB Insert Failed\n");
                                        return -1;
                                }
                                memset(query, '\0', MAX_QUERY_LEN);
@@ -1316,7 +1170,7 @@ static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
                                                 up->appid, df->path, al->text);
                                        ret = __exec_query(query);
                                        if (ret == -1) {
-                                               DBG("Package UiApp Share Allowed DB Insert Failed\n");
+                                               _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
                                                return -1;
                                        }
                                        memset(query, '\0', MAX_QUERY_LEN);
@@ -1340,13 +1194,13 @@ static int __insert_serviceapplication_info(manifest_x *mfx)
        {
                snprintf(query, MAX_QUERY_LEN,
                         "insert into package_app_info(app_id, app_component, app_exec, app_type, app_onboot, " \
-                       "app_multiple, app_autorestart, app_enabled, package) " \
-                       "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
+                       "app_multiple, app_autorestart, app_enabled, app_permissiontype, package) " \
+                       "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
                         sp->appid, "svcapp", sp->exec, sp->type, sp->onboot, "\0",
-                        sp->autorestart, sp->enabled, mfx->package);
+                        sp->autorestart, sp->enabled, sp->permission_type, mfx->package);
                ret = __exec_query(query);
                if (ret == -1) {
-                       DBG("Package ServiceApp Info DB Insert Failed\n");
+                       _LOGD("Package ServiceApp Info DB Insert Failed\n");
                        return -1;
                }
                sp = sp->next;
@@ -1372,7 +1226,7 @@ static int __insert_serviceapplication_appcategory_info(manifest_x *mfx)
                                 sp->appid, ct->name);
                        ret = __exec_query(query);
                        if (ret == -1) {
-                               DBG("Package ServiceApp Category Info DB Insert Failed\n");
+                               _LOGD("Package ServiceApp Category Info DB Insert Failed\n");
                                return -1;
                        }
                        ct = ct->next;
@@ -1401,7 +1255,7 @@ static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx)
                                         sp->appid, md->key, md->value);
                                ret = __exec_query(query);
                                if (ret == -1) {
-                                       DBG("Package ServiceApp Metadata Info DB Insert Failed\n");
+                                       _LOGD("Package ServiceApp Metadata Info DB Insert Failed\n");
                                        return -1;
                                }
                        }
@@ -1430,7 +1284,7 @@ static int __insert_serviceapplication_apppermission_info(manifest_x *mfx)
                                 sp->appid, pm->type, pm->value);
                        ret = __exec_query(query);
                        if (ret == -1) {
-                               DBG("Package ServiceApp permission Info DB Insert Failed\n");
+                               _LOGD("Package ServiceApp permission Info DB Insert Failed\n");
                                return -1;
                        }
                        pm = pm->next;
@@ -1447,65 +1301,24 @@ static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx)
        appcontrol_x *acontrol = NULL;
        int ret = -1;
        char query[MAX_QUERY_LEN] = {'\0'};
-       operation_x *op = NULL;
-       mime_x *mi = NULL;
-       uri_x *ui = NULL;
-       subapp_x *sub = NULL;
-       char *operation = NULL;
-       char *mime = NULL;
-       char *uri = NULL;
-       char *subapp = NULL;
-       while(sp != NULL)
-       {
+       char buf[BUFSIZE] = {'\0'};
+       while (sp != NULL) {
                acontrol = sp->appcontrol;
-               while(acontrol != NULL)
-               {
-                       op = acontrol->operation;
-                       while(op != NULL)
-                       {
-                       if (op)
-                               operation = op->name;
-                       mi = acontrol->mime;
-                               do
-                               {
-                               if (mi)
-                                       mime = mi->name;
-                               sub = acontrol->subapp;
-                                       do
-                                       {
-                                       if (sub)
-                                               subapp = sub->name;
-                                       ui = acontrol->uri;
-                                               do
-                                               {
-                                                       if (ui)
-                                                               uri = ui->name;
-                                                       snprintf(query, MAX_QUERY_LEN,
-                                                                "insert into package_app_app_control(app_id, operation, uri_scheme, mime_type,subapp_name) " \
-                                                               "values('%s', '%s', '%s', '%s', '%s')",\
-                                                                sp->appid, operation, uri, mime, subapp);
-                                                       ret = __exec_query(query);
-                                                       if (ret == -1) {
-                                                               DBG("Package UiApp AppSvc DB Insert Failed\n");
-                                                               return -1;
-                                                       }
-                                                       memset(query, '\0', MAX_QUERY_LEN);
-                                                       if (ui)
-                                                               ui = ui->next;
-                                                       uri = NULL;
-                                               } while(ui != NULL);
-                                               if (sub)
-                                                       sub = sub->next;
-                                               subapp = NULL;
-                                               }while(sub != NULL);
-                                       if (mi)
-                                               mi = mi->next;
-                                       mime = NULL;
-                               }while(mi != NULL);
-                               if (op)
-                                       op = op->next;
-                               operation = NULL;
+               while (acontrol != NULL) {
+                       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");
+                       snprintf(query, MAX_QUERY_LEN,
+                                       "insert into package_app_app_control(app_id, app_control) " \
+                                       "values('%s', '%s')",\
+                                       sp->appid, buf);
+                       ret = __exec_query(query);
+                       if (ret == -1) {
+                               _LOGD("Package UiApp AppSvc DB Insert Failed\n");
+                               return -1;
                        }
+                       memset(query, '\0', MAX_QUERY_LEN);
                        acontrol = acontrol->next;
                }
                sp = sp->next;
@@ -1523,10 +1336,10 @@ static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
        mime_x *mi = NULL;
        uri_x *ui = NULL;
        subapp_x *sub = NULL;
-       char *operation = NULL;
-       char *mime = NULL;
-       char *uri = NULL;
-       char *subapp = NULL;
+       const char *operation = NULL;
+       const char *mime = NULL;
+       const char *uri = NULL;
+       const char *subapp = NULL;
        while(sp != NULL)
        {
                asvc = sp->appsvc;
@@ -1555,10 +1368,14 @@ static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
                                                                snprintf(query, MAX_QUERY_LEN,
                                                                         "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
                                                                        "values('%s', '%s', '%s', '%s', '%s')",\
-                                                                        sp->appid, operation, uri, mime, subapp);
+                                                                        sp->appid,
+                                                                        operation,
+                                                                       __get_str(uri),
+                                                                       __get_str(mime),
+                                                                       __get_str(subapp));
                                                                ret = __exec_query(query);
                                                                if (ret == -1) {
-                                                                       DBG("Package UiApp AppSvc DB Insert Failed\n");
+                                                                       _LOGD("Package UiApp AppSvc DB Insert Failed\n");
                                                                        return -1;
                                                                }
                                                                memset(query, '\0', MAX_QUERY_LEN);
@@ -1585,7 +1402,38 @@ static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
        return 0;
 }
 
+static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx)
+{
+       serviceapplication_x *sp = mfx->serviceapplication;
+       datacontrol_x *dc = NULL;
+       int ret = -1;
+       char query[MAX_QUERY_LEN] = {'\0'};
+
+       while(sp != NULL)
+       {
+               dc = sp->datacontrol;
+               while(dc != NULL)
+               {
+                       snprintf(query, MAX_QUERY_LEN,
+                                       "insert into package_app_data_control(app_id, providerid, access, type) " \
+                                       "values('%s', '%s', '%s', '%s')",\
+                                       mfx->serviceapplication->appid,
+                                       dc->providerid,
+                                       dc->access,
+                                       dc->type);
 
+                       ret = __exec_query(query);
+                       if (ret == -1) {
+                               _LOGD("Package ServiceApp Data Control DB Insert Failed\n");
+                               return -1;
+                       }
+                       memset(query, '\0', MAX_QUERY_LEN);
+                       dc = dc->next;
+               }
+               sp = sp->next;
+       }
+       return 0;
+}
 
 static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
 {
@@ -1608,7 +1456,7 @@ static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
                                         sp->appid, rq->text);
                                ret = __exec_query(query);
                                if (ret == -1) {
-                                       DBG("Package ServiceApp Share Request DB Insert Failed\n");
+                                       _LOGD("Package ServiceApp Share Request DB Insert Failed\n");
                                        return -1;
                                }
                                memset(query, '\0', MAX_QUERY_LEN);
@@ -1648,7 +1496,7 @@ static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
                                                 sp->appid, df->path, al->text);
                                        ret = __exec_query(query);
                                        if (ret == -1) {
-                                               DBG("Package App Share Allowed DB Insert Failed\n");
+                                               _LOGD("Package App Share Allowed DB Insert Failed\n");
                                                return -1;
                                        }
                                        memset(query, '\0', MAX_QUERY_LEN);
@@ -1663,7 +1511,7 @@ static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
        return 0;
 }
 
-static int __insert_manifest_info_in_db(manifest_x *mfx)
+static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
 {
        label_x *lbl = mfx->label;
        license_x *lcn = mfx->license;
@@ -1674,13 +1522,19 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
        uiapplication_x *up_icn = mfx->uiapplication;
        uiapplication_x *up_image = mfx->uiapplication;
        serviceapplication_x *sp = mfx->serviceapplication;
+       privileges_x *pvs = NULL;
+       privilege_x *pv = NULL;
        char query[MAX_QUERY_LEN] = { '\0' };
        int ret = -1;
-       char *type = NULL;
-       char *path = NULL;
-       char *auth_name = NULL;
-       char *auth_email = NULL;
-       char *auth_href = NULL;
+       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;
+
        if (ath) {
                if (ath->text)
                        auth_name = ath->text;
@@ -1689,53 +1543,89 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
                if (ath->href)
                        auth_href = ath->href;
        }
+       /*Insert in the package_cert_info CERT_DB*/
+       pkgmgrinfo_instcertinfo_h cert_handle = NULL;
+       ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
+               _LOGE("Cert Info DB create handle failed\n");
+               return -1;
+       }
+       ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
+       if (ret != PMINFO_R_OK) {
+               pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
+               _LOGE("Cert Info DB Insert Failed\n");
+               return -1;
+       }
 
        /*Insert in the package_info DB*/
-       if (mfx->type)
-               type = strdup(mfx->type);
-       else
-               type = strdup("rpm");
-       /*Insert in the package_info DB*/
-       if (mfx->root_path)
-               path = strdup(mfx->root_path);
-       else{
-               if (strcmp(type,"rpm")==0)
-                       path = strdup("/usr/apps");
-               else
-                       path = strdup("/opt/usr/apps");
-       }
        snprintf(query, MAX_QUERY_LEN,
-                "insert into package_info(package, package_type, package_version, install_location, package_size, " \
-               "package_removable, package_preload, package_readonly, package_update, package_appsetting, author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path) " \
-               "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
-                mfx->package, type, mfx->version, mfx->installlocation, mfx->package_size, mfx->removable, mfx->preload,
-                mfx->readonly, mfx->update, mfx->appsetting, auth_name, auth_email, auth_href, mfx->installed_time, mfx->installed_storage, mfx->storeclient_id, mfx->mainapp_id, mfx->package_url, path);
+                "insert into package_info(package, package_type, package_version, package_api_version, 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) " \
+               "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
+                mfx->package,
+                mfx->type,
+                mfx->version,
+                __get_str(mfx->api_version),
+                __get_str(mfx->installlocation),
+                __get_str(mfx->package_size),
+                mfx->removable,
+                mfx->preload,
+                mfx->readonly,
+                mfx->update,
+                mfx->appsetting,
+                mfx->nodisplay_setting,
+                mfx->system,
+                __get_str(auth_name),
+                __get_str(auth_email),
+                __get_str(auth_href),
+                mfx->installed_time,
+                mfx->installed_storage,
+                __get_str(mfx->storeclient_id),
+                mfx->mainapp_id,
+                __get_str(mfx->package_url),
+                mfx->root_path,
+                __get_str(mfx->csc_path));
+
        ret = __exec_query(query);
        if (ret == -1) {
-               DBG("Package Info DB Insert Failed\n");
-               if (type) {
-                       free(type);
-                       type = NULL;
-               }
-               if (path) {
-                       free(path);
-                       path = NULL;
-               }
+               _LOGD("Package Info DB Insert Failed\n");
                return -1;
        }
-       if (type) {
-               free(type);
-               type = NULL;
+
+       /*Insert in the package_privilege_info DB*/
+       pvs = mfx->privileges;
+       while (pvs != NULL) {
+               pv = pvs->privilege;
+               while (pv != NULL) {
+                       memset(query, '\0', MAX_QUERY_LEN);
+                       snprintf(query, MAX_QUERY_LEN,
+                               "insert into package_privilege_info(package, privilege) " \
+                               "values('%s','%s')",\
+                                mfx->package, pv->text);
+                       ret = __exec_query(query);
+                       if (ret == -1) {
+                               _LOGD("Package Privilege Info DB Insert Failed\n");
+                               return -1;
+                       }
+                       pv = pv->next;
+               }
+               pvs = pvs->next;
        }
-       if (path) {
-               free(path);
-               path = NULL;
+
+       if (up != NULL) {
+               ret = __insert_ui_mainapp_info(mfx);
+               if (ret == -1)
+                       return -1;
        }
-       /*Insert the package locale and app locale info */
+
+       /*Insert the package locale*/
        pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
-       g_list_foreach(pkglocale, __trimfunc1, NULL);
-       prev = NULL;
+       /*remove duplicated data in pkglocale*/
+       __trimfunc(pkglocale);
 
+       /*Insert the app locale info */
        while(up != NULL)
        {
                applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
@@ -1746,8 +1636,8 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
                applocale = __create_locale_list(applocale, sp->label, NULL, sp->icon, NULL, NULL);
                sp = sp->next;
        }
-       g_list_foreach(applocale, __trimfunc2, NULL);
-       prev = NULL;
+       /*remove duplicated data in applocale*/
+       __trimfunc(applocale);
 
        /*Insert the app icon info */
        while(up_icn != NULL)
@@ -1755,8 +1645,8 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
                appicon = __create_icon_list(appicon, up_icn->icon);
                up_icn = up_icn->next;
        }
-       g_list_foreach(appicon, __trimfunc3, NULL);
-       prev = NULL;
+       /*remove duplicated data in appicon*/
+       __trimfunc(appicon);
 
        /*Insert the image info */
        while(up_image != NULL)
@@ -1764,15 +1654,11 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
                appimage = __create_image_list(appimage, up_image->image);
                up_image = up_image->next;
        }
-       g_list_foreach(appimage, __trimfunc4, NULL);
-       prev = NULL;
+       /*remove duplicated data in appimage*/
+       __trimfunc(appimage);
 
-       /*g_list_foreach(pkglocale, __printfunc, NULL);*/
-       /*DBG("\n");*/
-       /*g_list_foreach(applocale, __printfunc, NULL);*/
-
-       /*package locale info*/
        g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
+
        /*native app locale info*/
        up = mfx->uiapplication;
        while(up != NULL)
@@ -1813,18 +1699,13 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
        g_list_free(appimage);
        appimage = NULL;
 
-
        /*Insert in the package_app_info DB*/
        ret = __insert_uiapplication_info(mfx);
        if (ret == -1)
                return -1;
-       ret = __insert_ui_mainapp_info(mfx);
-       if (ret == -1)
-               return -1;
        ret = __insert_serviceapplication_info(mfx);
        if (ret == -1)
                return -1;
-
        /*Insert in the package_app_app_control DB*/
        ret = __insert_uiapplication_appcontrol_info(mfx);
        if (ret == -1)
@@ -1881,21 +1762,108 @@ static int __insert_manifest_info_in_db(manifest_x *mfx)
        if (ret == -1)
                return -1;
 
+       /*Insert in the package_app_data_control DB*/
+       ret = __insert_uiapplication_datacontrol_info(mfx);
+       if (ret == -1)
+               return -1;
+       ret = __insert_serviceapplication_datacontrol_info(mfx);
+       if (ret == -1)
+               return -1;
+
+       return 0;
+
+}
+
+static int __delete_appinfo_from_db(char *db_table, const char *appid)
+{
+       char query[MAX_QUERY_LEN] = { '\0' };
+       int ret = -1;
+       memset(query, '\0', MAX_QUERY_LEN);
+       snprintf(query, MAX_QUERY_LEN,
+                "delete from %s where app_id='%s'", db_table, appid);
+       ret = __exec_query(query);
+       if (ret == -1) {
+               _LOGD("DB Deletion from table (%s) Failed\n", db_table);
+               return -1;
+       }
+       return 0;
+}
+
+static int __delete_subpkg_info_from_db(char *appid)
+{
+       int ret = -1;
+
+       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_svc", 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;
+
        return 0;
+}
+
+static int __delete_subpkg_from_db(manifest_x *mfx)
+{
+       char query[MAX_QUERY_LEN] = { '\0' };
+       char *error_message = NULL;
 
+       snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", 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 0;
 }
 
-static int __delete_manifest_info_from_db(manifest_x *mfx)
+static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
 {
        char query[MAX_QUERY_LEN] = { '\0' };
        int ret = -1;
        uiapplication_x *up = mfx->uiapplication;
        serviceapplication_x *sp = mfx->serviceapplication;
-
        /*Delete from cert table*/
-       ret = pkgmgrinfo_delete_certinfo(mfx->package);
+       if (uid != GLOBAL_USER)
+               ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
+       else
+               ret = pkgmgrinfo_delete_certinfo(mfx->package);
        if (ret) {
-               DBG("Cert Info  DB Delete Failed\n");
+               _LOGD("Cert Info  DB Delete Failed\n");
                return -1;
        }
 
@@ -1904,7 +1872,7 @@ static int __delete_manifest_info_from_db(manifest_x *mfx)
                 "delete from package_info where package='%s'", mfx->package);
        ret = __exec_query(query);
        if (ret == -1) {
-               DBG("Package Info DB Delete Failed\n");
+               _LOGD("Package Info DB Delete Failed\n");
                return -1;
        }
        memset(query, '\0', MAX_QUERY_LEN);
@@ -1914,540 +1882,595 @@ static int __delete_manifest_info_from_db(manifest_x *mfx)
                 "delete from package_localized_info where package='%s'", mfx->package);
        ret = __exec_query(query);
        if (ret == -1) {
-               DBG("Package Localized Info DB Delete Failed\n");
+               _LOGD("Package Localized Info DB Delete Failed\n");
                return -1;
        }
-       memset(query, '\0', MAX_QUERY_LEN);
 
-       /*Delete from Package App Info*/
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_info where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_info where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
+       /*Delete from Package Privilege Info*/
+       snprintf(query, MAX_QUERY_LEN,
+                "delete from package_privilege_info where package='%s'", mfx->package);
+       ret = __exec_query(query);
+       if (ret == -1) {
+               _LOGD("Package Privilege Info DB Delete Failed\n");
+               return -1;
        }
 
-       /*Delete from Package App Localized Info*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_localized_info where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Localized Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
+       while (up != NULL) {
+               ret = __delete_appinfo_from_db("package_app_info", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_localized_info", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_icon_section_info", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_image_info", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_svc", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_control", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_category", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_metadata", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_permission", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_share_allowed", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_share_request", up->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_data_control", up->appid);
+               if (ret < 0)
+                       return ret;
                up = up->next;
        }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_localized_info where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Localized Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
+
+       while (sp != NULL) {
+               ret = __delete_appinfo_from_db("package_app_info", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_localized_info", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_icon_section_info", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_image_info", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_svc", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_control", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_category", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_metadata", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_app_permission", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_share_allowed", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_share_request", sp->appid);
+               if (ret < 0)
+                       return ret;
+               ret = __delete_appinfo_from_db("package_app_data_control", sp->appid);
+               if (ret < 0)
+                       return ret;
                sp = sp->next;
        }
 
-       /*Delete from  App icon localized Info*/
-       up = mfx->uiapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_icon_section_info where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App image Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
+       /* if main package has sub pkg, delete sub pkg data*/
+       __delete_subpkg_from_db(mfx);
 
-       /*Delete from  App image Info*/
-       up = mfx->uiapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_image_info where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App image Info DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-
-       /*Delete from Package App App-Svc*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_svc where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Svc DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_svc where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Svc DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
-       }
+       return 0;
+}
 
-       /*Delete from Package App App-Control*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_control where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Control DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_control where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Control DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
-       }
+static int __update_preload_condition_in_db()
+{
+       int ret = -1;
+       char query[MAX_QUERY_LEN] = {'\0'};
 
-       /*Delete from Package App App-Category*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_category where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Category DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_category where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Category DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
-       }
+       snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
 
-       /*Delete from Package App App-Metadata*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_metadata where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Metadata DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_metadata where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-Metadata DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
-       }
+       ret = __exec_query(query);
+       if (ret == -1)
+               _LOGD("Package preload_condition update failed\n");
 
-       /*Delete from Package App App-permission*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_permission where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-permission DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
-       }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_app_permission where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App App-permission DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
-       }
+       return ret;
+}
 
-       /*Delete from Package App Share Allowed*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_share_allowed where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Share Allowed DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
+API int pkgmgr_parser_initialize_db(uid_t uid)
+{
+       int ret = -1;
+       /*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;
        }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_share_allowed where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Share Allowed DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
+       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;
        }
-
-       /*Delete from Package App Share Request*/
-       up = mfx->uiapplication;
-       sp = mfx->serviceapplication;
-       while(up != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_share_request where app_id='%s'", up->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Share Request DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               up = up->next;
+       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;
        }
-       while(sp != NULL)
-       {
-               snprintf(query, MAX_QUERY_LEN,
-                        "delete from package_app_share_request where app_id='%s'", sp->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       DBG("Package App Share Request DB Delete Failed\n");
-                       return -1;
-               }
-               memset(query, '\0', MAX_QUERY_LEN);
-               sp = sp->next;
+       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;
        }
-       return 0;
-}
-
-
-int pkgmgr_parser_initialize_db()
-{
-       int ret = -1;
-       ret = __initialize_package_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
        if (ret == -1) {
-               DBG("package info DB initialization failed\n");
+               _LOGD("package app localized info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_localized_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
        if (ret == -1) {
-               DBG("package localized info DB initialization failed\n");
+               _LOGD("package app icon localized info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_cert_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
        if (ret == -1) {
-               DBG("package cert info DB initialization failed\n");
+               _LOGD("package app image info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_cert_index_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
        if (ret == -1) {
-               DBG("package cert index info DB initialization failed\n");
+               _LOGD("package app app control DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
        if (ret == -1) {
-               DBG("package app info DB initialization failed\n");
+               _LOGD("package app app category DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_localized_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
        if (ret == -1) {
-               DBG("package app localized info DB initialization failed\n");
+               _LOGD("package app app category DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_icon_section_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
        if (ret == -1) {
-               DBG("package app icon localized info DB initialization failed\n");
+               _LOGD("package app app permission DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_image_info_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
        if (ret == -1) {
-               DBG("package app image info DB initialization failed\n");
+               _LOGD("package app app svc DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_app_control_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
        if (ret == -1) {
-               DBG("package app app control DB initialization failed\n");
+               _LOGD("package app share allowed DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_app_category_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
        if (ret == -1) {
-               DBG("package app app category DB initialization failed\n");
+               _LOGD("package app share request DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_app_metadata_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
        if (ret == -1) {
-               DBG("package app app category DB initialization failed\n");
+               _LOGD("package app data control DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_app_permission_db();
+       /*Cert DB*/
+       ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
        if (ret == -1) {
-               DBG("package app app permission DB initialization failed\n");
+               _LOGD("package cert info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_app_svc_db();
+       ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
        if (ret == -1) {
-               DBG("package app app svc DB initialization failed\n");
+               _LOGD("package cert index info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_share_allowed_db();
+       /*resource DB*/
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_RESOURCE_INFO);
        if (ret == -1) {
-               DBG("package app share allowed DB initialization failed\n");
+               _LOGD("package resource info DB initialization failed\n");
                return ret;
        }
-       ret = __initialize_package_app_share_request_db();
+       ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_RESOURCE_DATA);
        if (ret == -1) {
-               DBG("package app share request DB initialization failed\n");
+               _LOGD("package resource data DB initialization failed\n");
                return ret;
        }
+
+       if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
+               _LOGD("Failed to change cert db permission\n");
+       }
+       if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
+               _LOGD("Failed to change parser db permission\n");
+       }
+
        return 0;
 }
 
-int pkgmgr_parser_check_and_create_db()
+static int __parserdb_change_perm(const char *db_file, uid_t uid)
 {
-       int ret = -1;
-       if (access(PKGMGR_PARSER_DB_FILE, F_OK) == 0) {
-               ret =
-                   db_util_open(PKGMGR_PARSER_DB_FILE, &pkgmgr_parser_db,
-                                DB_UTIL_REGISTER_HOOK_METHOD);
-               if (ret != SQLITE_OK) {
-                       DBG("connect db [%s] failed!\n",
-                              PKGMGR_PARSER_DB_FILE);
+       char buf[BUFSIZE];
+       char journal_file[BUFSIZE];
+       char *files[3];
+       int ret, i;
+       struct passwd *userinfo = NULL;
+       files[0] = (char *)db_file;
+       files[1] = journal_file;
+       files[2] = NULL;
+
+       if (db_file == NULL)
+               return -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;
+       userinfo = getpwuid(uid);
+       if (!userinfo) {
+               _LOGE("FAIL: user %d doesn't exist", uid);
+               return -1;
+       }
+       snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
+
+       for (i = 0; files[i]; i++) {
+               ret = chown(files[i], uid, userinfo->pw_gid);
+               if (ret == -1) {
+                       if (strerror_r(errno, buf, sizeof(buf)))
+                               strcpy(buf, "");
+                       _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
+                                       userinfo->pw_gid, buf);
                        return -1;
                }
-               ret = chmod(PKGMGR_PARSER_DB_FILE, 0664);
-               if (ret)
-                       DBG("Failed to change mode of manifest DB\n");
-               return 0;
+
+               ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+               if (ret == -1) {
+                       if (strerror_r(errno, buf, sizeof(buf)))
+                               strcpy(buf, "");
+                       _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
+                       return -1;
+               }
+               SET_SMACK_LABEL(files[i]);
        }
-       DBG("Pkgmgr DB does not exists. Create one!!\n");
+       return 0;
+}
 
-       ret =
-           db_util_open(PKGMGR_PARSER_DB_FILE, &pkgmgr_parser_db,
-                        DB_UTIL_REGISTER_HOOK_METHOD);
+API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
+{
+       int ret;
 
-       if (ret != SQLITE_OK) {
-               DBG("connect db [%s] failed!\n", PKGMGR_PARSER_DB_FILE);
+       if (getuid() != OWNER_ROOT) {
+               _LOGE("Only root user is allowed");
+               return -1;
+       }
+
+       if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
+               _LOGE("Manifest db for user %d is already exists", uid);
                return -1;
        }
-       ret = chmod(PKGMGR_PARSER_DB_FILE, 0664);
-       if (ret)
-               DBG("Failed to change mode of manifest DB\n");
+
+       if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
+               _LOGE("Cert db for user %d is already exists", uid);
+               return -1;
+       }
+
+       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();
+               return -1;
+       }
+       pkgmgr_parser_close_db();
+
        return 0;
 }
 
+API int pkgmgr_parser_check_and_create_db(uid_t uid)
+{
+       int ret = -1;
+       /*Manifest DB*/
+       ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
+       if (ret) {
+               _LOGD("Manifest DB creation Failed\n");
+               return -1;
+       }
+
+       /*Cert DB*/
+       ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
+       if (ret) {
+               _LOGD("Cert DB creation Failed\n");
+               return -1;
+       }
+       return 0;
+}
+
+void pkgmgr_parser_close_db(void)
+{
+       sqlite3_close(pkgmgr_parser_db);
+       sqlite3_close(pkgmgr_cert_db);
+}
+
+
 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
 {
+       _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
        if (mfx == NULL) {
-               DBG("manifest pointer is NULL\n");
+               _LOGD("manifest pointer is NULL\n");
                return -1;
        }
-       int ret = -1;
-       ret = pkgmgr_parser_check_and_create_db();
+       int ret = 0;
+       ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
        if (ret == -1) {
-               DBG("Failed to open DB\n");
+               _LOGD("Failed to open DB\n");
                return ret;
        }
-       ret = pkgmgr_parser_initialize_db();
+       ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
        if (ret == -1)
-               return ret;
+               goto err;
        /*Begin transaction*/
        ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               DBG("Failed to begin transaction\n");
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               _LOGD("Failed to begin transaction\n");
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Begin\n");
-       ret = __insert_manifest_info_in_db(mfx);
+       _LOGD("Transaction Begin\n");
+       ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
        if (ret == -1) {
-               DBG("Insert into DB failed. Rollback now\n");
+               _LOGD("Insert into DB failed. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               goto err;
        }
        /*Commit transaction*/
        ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               DBG("Failed to commit transaction. Rollback now\n");
+               _LOGD("Failed to commit transaction. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Commit and End\n");
-       sqlite3_close(pkgmgr_parser_db);
-       return 0;
+       _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)
+API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
 {
+       _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
        if (mfx == NULL) {
-               DBG("manifest pointer is NULL\n");
+               _LOGD("manifest pointer is NULL\n");
                return -1;
        }
-       int ret = -1;
-       ret = pkgmgr_parser_check_and_create_db();
+       int ret = 0;
+       ret = pkgmgr_parser_check_and_create_db(uid);
        if (ret == -1) {
-               DBG("Failed to open DB\n");
+               _LOGD("Failed to open DB\n");
                return ret;
        }
-       ret = pkgmgr_parser_initialize_db();
+       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);
+       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_usr_db(manifest_x *mfx, 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;
+       }
+       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) {
-               DBG("Failed to begin transaction\n");
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               _LOGD("Failed to begin transaction\n");
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Begin\n");
-       ret = __delete_manifest_info_from_db(mfx);
+       _LOGD("Transaction Begin\n");
+       ret = __delete_manifest_info_from_db(mfx, uid);
        if (ret == -1) {
-               DBG("Delete from DB failed. Rollback now\n");
+               _LOGD("Delete from DB failed. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               goto err;
        }
-       ret = __insert_manifest_info_in_db(mfx);
+       ret = __insert_manifest_info_in_db(mfx, uid);
        if (ret == -1) {
-               DBG("Insert into DB failed. Rollback now\n");
+               _LOGD("Insert into DB failed. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               goto err;
        }
 
        /*Commit transaction*/
        ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               DBG("Failed to commit transaction. Rollback now\n");
+               _LOGD("Failed to commit transaction. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Commit and End\n");
-       sqlite3_close(pkgmgr_parser_db);
-       return 0;
+       _LOGD("Transaction Commit and End\n");
+err:
+       pkgmgr_parser_close_db();
+       return ret;
 }
 
-API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
+API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
+{
+       return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
+}
+
+API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
 {
        if (mfx == NULL) {
-               DBG("manifest pointer is NULL\n");
+               _LOGD("manifest pointer is NULL\n");
                return -1;
        }
-       int ret = -1;
-       ret = pkgmgr_parser_check_and_create_db();
+       int ret = 0;
+       ret = pkgmgr_parser_check_and_create_db(uid);
        if (ret == -1) {
-               DBG("Failed to open DB\n");
+               _LOGD("Failed to open DB\n");
                return ret;
        }
        /*Begin transaction*/
        ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               DBG("Failed to begin transaction\n");
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               _LOGD("Failed to begin transaction\n");
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Begin\n");
-       ret = __delete_manifest_info_from_db(mfx);
+       _LOGD("Transaction Begin\n");
+       ret = __delete_manifest_info_from_db(mfx, uid);
        if (ret == -1) {
-               DBG("Delete from DB failed. Rollback now\n");
+               _LOGD("Delete from DB failed. Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               goto err;
        }
        /*Commit transaction*/
        ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
-               DBG("Failed to commit transaction, Rollback now\n");
+               _LOGD("Failed to commit transaction, Rollback now\n");
                sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
-               sqlite3_close(pkgmgr_parser_db);
-               return -1;
+               ret = -1;
+               goto err;
        }
-       DBG("Transaction Commit and End\n");
-       sqlite3_close(pkgmgr_parser_db);
-       return 0;
+       _LOGD("Transaction Commit and End\n");
+err:
+       pkgmgr_parser_close_db();
+       return ret;
+}
+
+API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
+{
+       return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
+}
+
+API int pkgmgr_parser_update_preload_info_in_db()
+{
+       int ret = 0;
+       ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
+       if (ret == -1) {
+               _LOGD("Failed to open DB\n");
+               return ret;
+       }
+       /*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 = __update_preload_condition_in_db();
+       if (ret == -1) {
+               _LOGD("__update_preload_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;
+}
+
+API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
+{
+       int ret = 0;
+       ret = pkgmgr_parser_check_and_create_db(uid);
+       if (ret == -1) {
+               _LOGD("Failed to open DB\n");
+               return ret;
+       }
+       /*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 = __update_preload_condition_in_db();
+       if (ret == -1) {
+               _LOGD("__update_preload_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;
 }