Remove warnings
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/smack.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <db-util.h>
31 #include <glib.h>
32 #include <grp.h>
33 #include <pwd.h>
34
35 /* For multi-user support */
36 #include <tzplatform_config.h>
37
38 #include "pkgmgr-info.h"
39 #include "pkgmgr_parser_internal.h"
40 #include "pkgmgr_parser_db.h"
41
42 #include "pkgmgr-info-debug.h"
43
44 #ifdef LOG_TAG
45 #undef LOG_TAG
46 #endif
47 #define LOG_TAG "PKGMGR_PARSER"
48
49 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
50 #define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
51 #define MAX_QUERY_LEN           4096
52 #define BUFSIZE 4096
53 #define OWNER_ROOT 0
54
55 #define SET_SMACK_LABEL(x) \
56 do { \
57         if (smack_setlabel((x), "System::Shared", SMACK_LABEL_ACCESS)) \
58                 _LOGE("failed chsmack -a \"System::Shared\" %s", x); \
59         else \
60                 _LOGD("chsmack -a \"System::Shared\" %s", x); \
61 } while (0)
62
63 sqlite3 *pkgmgr_parser_db;
64 sqlite3 *pkgmgr_cert_db;
65
66
67 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
68                                                 "(package text primary key not null, " \
69                                                 "package_type text DEFAULT 'rpm', " \
70                                                 "package_version text, " \
71                                                 "package_api_version text, " \
72                                                 "install_location text, " \
73                                                 "package_size text, " \
74                                                 "package_removable text DEFAULT 'true', " \
75                                                 "package_preload text DEFAULT 'false', " \
76                                                 "package_readonly text DEFAULT 'false', " \
77                                                 "package_update text DEFAULT 'false', " \
78                                                 "package_appsetting text DEFAULT 'false', " \
79                                                 "package_nodisplay text DEFAULT 'false', " \
80                                                 "package_system text DEFAULT 'false', " \
81                                                 "author_name text, " \
82                                                 "author_email text, " \
83                                                 "author_href text," \
84                                                 "installed_time text," \
85                                                 "installed_storage text," \
86                                                 "storeclient_id text," \
87                                                 "mainapp_id text," \
88                                                 "package_url text," \
89                                                 "root_path text," \
90                                                 "csc_path text )"
91
92 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
93                                                 "(package text not null, " \
94                                                 "package_locale text DEFAULT 'No Locale', " \
95                                                 "package_label text, " \
96                                                 "package_icon text, " \
97                                                 "package_description text, " \
98                                                 "package_license text, " \
99                                                 "package_author, " \
100                                                 "PRIMARY KEY(package, package_locale), " \
101                                                 "FOREIGN KEY(package) " \
102                                                 "REFERENCES package_info(package) " \
103                                                 "ON DELETE CASCADE)"
104
105 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
106                                                 "(package text not null, " \
107                                                 "privilege text not null, " \
108                                                 "PRIMARY KEY(package, privilege) " \
109                                                 "FOREIGN KEY(package) " \
110                                                 "REFERENCES package_info(package) " \
111                                                 "ON DELETE CASCADE)"
112
113 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
114                                                 "(app_id text primary key not null, " \
115                                                 "app_component text, " \
116                                                 "app_exec text, " \
117                                                 "app_nodisplay text DEFAULT 'false', " \
118                                                 "app_type text, " \
119                                                 "app_onboot text DEFAULT 'false', " \
120                                                 "app_multiple text DEFAULT 'false', " \
121                                                 "app_autorestart text DEFAULT 'false', " \
122                                                 "app_taskmanage text DEFAULT 'false', " \
123                                                 "app_enabled text DEFAULT 'true', " \
124                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
125                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
126                                                 "app_mainapp text, " \
127                                                 "app_recentimage text, " \
128                                                 "app_launchcondition text, " \
129                                                 "app_indicatordisplay text DEFAULT 'true', " \
130                                                 "app_portraitimg text, " \
131                                                 "app_landscapeimg text, " \
132                                                 "app_guestmodevisibility text DEFAULT 'true', " \
133                                                 "app_permissiontype text DEFAULT 'normal', " \
134                                                 "app_preload text DEFAULT 'false', " \
135                                                 "app_submode text DEFAULT 'false', " \
136                                                 "app_submode_mainid text, " \
137                                                 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
138                                                 "component_type text, " \
139                                                 "package text not null, " \
140                                                 "FOREIGN KEY(package) " \
141                                                 "REFERENCES package_info(package) " \
142                                                 "ON DELETE CASCADE)"
143
144 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
145                                                 "(app_id text not null, " \
146                                                 "app_locale text DEFAULT 'No Locale', " \
147                                                 "app_label text, " \
148                                                 "app_icon text, " \
149                                                 "PRIMARY KEY(app_id,app_locale) " \
150                                                 "FOREIGN KEY(app_id) " \
151                                                 "REFERENCES package_app_info(app_id) " \
152                                                 "ON DELETE CASCADE)"
153
154 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
155                                                 "(app_id text not null, " \
156                                                 "app_icon text, " \
157                                                 "app_icon_section text, " \
158                                                 "app_icon_resolution text, " \
159                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
160                                                 "FOREIGN KEY(app_id) " \
161                                                 "REFERENCES package_app_info(app_id) " \
162                                                 "ON DELETE CASCADE)"
163
164 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
165                                                 "(app_id text not null, " \
166                                                 "app_locale text DEFAULT 'No Locale', " \
167                                                 "app_image_section text, " \
168                                                 "app_image text, " \
169                                                 "PRIMARY KEY(app_id,app_image_section) " \
170                                                 "FOREIGN KEY(app_id) " \
171                                                 "REFERENCES package_app_info(app_id) " \
172                                                 "ON DELETE CASCADE)"
173
174 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
175                                                 "(app_id text not null, " \
176                                                 "app_control text not null, " \
177                                                 "PRIMARY KEY(app_id,app_control) " \
178                                                 "FOREIGN KEY(app_id) " \
179                                                 "REFERENCES package_app_info(app_id) " \
180                                                 "ON DELETE CASCADE)"
181
182 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
183                                                 "(app_id text not null, " \
184                                                 "operation text not null, " \
185                                                 "uri_scheme text, " \
186                                                 "mime_type text, " \
187                                                 "subapp_name text, " \
188                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
189                                                 "FOREIGN KEY(app_id) " \
190                                                 "REFERENCES package_app_info(app_id) " \
191                                                 "ON DELETE CASCADE)"
192
193 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
194                                                 "(app_id text not null, " \
195                                                 "category text not null, " \
196                                                 "PRIMARY KEY(app_id,category) " \
197                                                 "FOREIGN KEY(app_id) " \
198                                                 "REFERENCES package_app_info(app_id) " \
199                                                 "ON DELETE CASCADE)"
200
201 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
202                                                 "(app_id text not null, " \
203                                                 "md_key text not null, " \
204                                                 "md_value text not null, " \
205                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
206                                                 "FOREIGN KEY(app_id) " \
207                                                 "REFERENCES package_app_info(app_id) " \
208                                                 "ON DELETE CASCADE)"
209
210 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
211                                                 "(app_id text not null, " \
212                                                 "pm_type text not null, " \
213                                                 "pm_value text not null, " \
214                                                 "PRIMARY KEY(app_id, pm_type, pm_value) " \
215                                                 "FOREIGN KEY(app_id) " \
216                                                 "REFERENCES package_app_info(app_id) " \
217                                                 "ON DELETE CASCADE)"
218
219 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
220                                                 "(app_id text not null, " \
221                                                 "data_share_path text not null, " \
222                                                 "data_share_allowed text not null, " \
223                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
224                                                 "FOREIGN KEY(app_id) " \
225                                                 "REFERENCES package_app_info(app_id) " \
226                                                 "ON DELETE CASCADE)"
227
228 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
229                                                 "(app_id text not null, " \
230                                                 "data_share_request text not null, " \
231                                                 "PRIMARY KEY(app_id,data_share_request) " \
232                                                 "FOREIGN KEY(app_id) " \
233                                                 "REFERENCES package_app_info(app_id) " \
234                                                 "ON DELETE CASCADE)"
235
236 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
237                                                 "(cert_info text not null, " \
238                                                 "cert_id integer, " \
239                                                 "cert_ref_count integer, " \
240                                                 "PRIMARY KEY(cert_id)) "
241
242 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
243                                                 "(package text not null, " \
244                                                 "author_root_cert integer, " \
245                                                 "author_im_cert integer, " \
246                                                 "author_signer_cert integer, " \
247                                                 "dist_root_cert integer, " \
248                                                 "dist_im_cert integer, " \
249                                                 "dist_signer_cert integer, " \
250                                                 "dist2_root_cert integer, " \
251                                                 "dist2_im_cert integer, " \
252                                                 "dist2_signer_cert integer, " \
253                                                 "PRIMARY KEY(package)) "
254
255 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
256                                                 "(app_id text not null, " \
257                                                 "providerid text not null, " \
258                                                 "access text not null, " \
259                                                 "type text not null, " \
260                                                 "PRIMARY KEY(app_id, providerid, access, type) " \
261                                                 "FOREIGN KEY(app_id) " \
262                                                 "REFERENCES package_app_info(app_id) " \
263                                                 "ON DELETE CASCADE)"
264 #define QUERY_CREATE_TABLE_PACKAGE_RESOURCE_INFO "CREATE TABLE IF NOT EXISTS package_resource_info" \
265                                                 "(pkg_id text NOT NULL," \
266                                                 "group_folder text NOT NULL," \
267                                                 "group_type text NOT NULL," \
268                                                 "PRIMARY KEY(pkg_id, group_type))"
269
270 #define QUERY_CREATE_TABLE_PACKAGE_RESOURCE_DATA "CREATE TABLE IF NOT EXISTS package_resource_data" \
271                                                 "(id integer REFERENCES package_resource_info(rowid)," \
272                                                 "node_folder text NOT NULL," \
273                                                 "attr_name text NOT NULL," \
274                                                 "attr_value test NOT NULL," \
275                                                 "PRIMARY KEY(id, node_folder, attr_name))"
276
277 static int __insert_uiapplication_info(manifest_x *mfx);
278 static int __insert_serviceapplication_info(manifest_x *mfx);
279 static int __insert_uiapplication_appsvc_info(manifest_x *mfx);
280 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx);
281 static int __insert_uiapplication_appcategory_info(manifest_x *mfx);
282 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx);
283 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx);
284 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx);
285 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx);
286 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx);
287 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx);
288 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx);
289 static int __insert_uiapplication_share_request_info(manifest_x *mfx);
290 static int __insert_serviceapplication_share_request_info(manifest_x *mfx);
291 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx);
292 static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx);
293 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata);
294 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata);
295 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
296 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
297 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
298 static int __delete_subpkg_info_from_db(char *appid);
299 static int __delete_appinfo_from_db(char *db_table, const char *appid);
300 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
301 static int __exec_query(char *query);
302 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
303                 char **label, char **license, char **icon, char **description, char **author);
304 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
305 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath);
306 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
307 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
308 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
309 static int __parserdb_change_perm(const char *db_file, uid_t uid);
310
311 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
312 {
313         if (coltxt[0])
314                 __delete_subpkg_info_from_db(coltxt[0]);
315
316         return 0;
317 }
318
319 static const char *__get_str(const char *str)
320 {
321         if (str == NULL)
322         {
323                 return PKGMGR_PARSER_EMPTY_STR;
324         }
325
326         return str;
327 }
328
329 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
330 {
331         int ret = -1;
332         sqlite3 *handle;
333
334         ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
335         if (ret != SQLITE_OK) {
336                 _LOGD("connect db [%s] failed!\n", db_path);
337                 return -1;
338         }
339         *db_handle = handle;
340
341         return 0;
342 }
343
344 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
345 {
346         manifest_x *mfx = (manifest_x *)data;
347         int i = 0;
348         char *appid = NULL;
349         char *status = NULL;
350         uiapplication_x *uiapp = NULL;
351         for(i = 0; i < ncols; i++)
352         {
353                 uiapp = mfx->uiapplication;
354                 if (strcmp(colname[i], "app_id") == 0) {
355                         if (coltxt[i])
356                                 appid = strdup(coltxt[i]);
357                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
358                         if (coltxt[i])
359                                 status = strdup(coltxt[i]);
360                 }
361         }
362         if (appid == NULL) {
363                 _LOGD("app id is NULL\n");
364                 return -1;
365         }
366         /*update guest mode visibility*/
367         for (; uiapp != NULL; uiapp = uiapp->next) {
368                 if (strcmp(uiapp->appid, appid) == 0) {
369                         free((void *)uiapp->guestmode_visibility);
370                         uiapp->guestmode_visibility = strdup(status);
371                         break;
372                 }
373         }
374         if (appid) {
375                 free(appid);
376                 appid = NULL;
377         }
378         if (status) {
379                 free(status);
380                 status = NULL;
381         }
382
383         return 0;
384 }
385
386 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
387 {
388         char *error_message = NULL;
389         char query[MAX_QUERY_LEN] = {'\0'};
390         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
391         if (SQLITE_OK !=
392             sqlite3_exec(pkgmgr_parser_db, query,
393                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
394                 _LOGD("Don't execute query = %s error message = %s\n",
395                        query, error_message);
396                 sqlite3_free(error_message);
397         }
398         return;
399 }
400
401 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
402 {
403         char *error_message = NULL;
404         if (SQLITE_OK !=
405             sqlite3_exec(db_handle, db_query,
406                          NULL, NULL, &error_message)) {
407                 _LOGD("Don't execute query = %s error message = %s\n",
408                        db_query, error_message);
409                 sqlite3_free(error_message);
410                 return -1;
411         }
412         sqlite3_free(error_message);
413         return 0;
414 }
415
416 static int __exec_query(char *query)
417 {
418         char *error_message = NULL;
419         if (SQLITE_OK !=
420             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
421                 _LOGD("Don't execute query = %s error message = %s\n", query,
422                        error_message);
423                 sqlite3_free(error_message);
424                 return -1;
425         }
426         sqlite3_free(error_message);
427         return 0;
428 }
429
430 static int __exec_query_no_msg(char *query)
431 {
432         char *error_message = NULL;
433         if (SQLITE_OK !=
434             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
435                 sqlite3_free(error_message);
436                 return -1;
437         }
438         sqlite3_free(error_message);
439         return 0;
440 }
441
442 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
443 {
444
445         while(lbl != NULL)
446         {
447                 if (lbl->lang)
448                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
449                 lbl = lbl->next;
450         }
451         while(lcn != NULL)
452         {
453                 if (lcn->lang)
454                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
455                 lcn = lcn->next;
456         }
457         while(icn != NULL)
458         {
459                 if (icn->lang)
460                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
461                 icn = icn->next;
462         }
463         while(dcn != NULL)
464         {
465                 if (dcn->lang)
466                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
467                 dcn = dcn->next;
468         }
469         while(ath != NULL)
470         {
471                 if (ath->lang)
472                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
473                 ath = ath->next;
474         }
475         return locale;
476
477 }
478
479 static GList *__create_icon_list(GList *locale, icon_x *icn)
480 {
481         while(icn != NULL)
482         {
483                 if (icn->section)
484                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
485                 icn = icn->next;
486         }
487         return locale;
488 }
489
490 static GList *__create_image_list(GList *locale, image_x *image)
491 {
492         while(image != NULL)
493         {
494                 if (image->section)
495                         locale = g_list_insert_sorted_with_data(locale, (gpointer)image->section, __comparefunc, NULL);
496                 image = image->next;
497         }
498         return locale;
499 }
500
501 static void __trimfunc(GList* trim_list)
502 {
503         char *trim_data = NULL;
504         char *prev = NULL;
505
506         GList *list = NULL;
507         list = g_list_first(trim_list);
508
509         while (list) {
510                 trim_data = (char *)list->data;
511                 if (trim_data) {
512                         if (prev) {
513                                 if (strcmp(trim_data, prev) == 0) {
514                                         trim_list = g_list_remove(trim_list, trim_data);
515                                         list = g_list_first(trim_list);
516                                         prev = NULL;
517                                         continue;
518                                 } else
519                                         prev = trim_data;
520                         }
521                         else
522                                 prev = trim_data;
523                 }
524                 list = g_list_next(list);
525         }
526 }
527
528 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
529 {
530         if (a == NULL || b == NULL)
531                 return 0;
532         if (strcmp((char*)a, (char*)b) == 0)
533                 return 0;
534         if (strcmp((char*)a, (char*)b) < 0)
535                 return -1;
536         if (strcmp((char*)a, (char*)b) > 0)
537                 return 1;
538         return 0;
539 }
540
541 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
542                 char **label, char **license, char **icon, char **description, char **author)
543 {
544         while(lbl != NULL)
545         {
546                 if (lbl->lang) {
547                         if (strcmp(lbl->lang, (char *)data) == 0) {
548                                 *label = (char*)lbl->text;
549                                 break;
550                         }
551                 }
552                 lbl = lbl->next;
553         }
554         while(lcn != NULL)
555         {
556                 if (lcn->lang) {
557                         if (strcmp(lcn->lang, (char *)data) == 0) {
558                                 *license = (char*)lcn->text;
559                                 break;
560                         }
561                 }
562                 lcn = lcn->next;
563         }
564         while(icn != NULL)
565         {
566                 if (icn->lang) {
567                         if (strcmp(icn->lang, (char *)data) == 0) {
568                                 *icon = (char*)icn->text;
569                                 break;
570                         }
571                 }
572                 icn = icn->next;
573         }
574         while(dcn != NULL)
575         {
576                 if (dcn->lang) {
577                         if (strcmp(dcn->lang, (char *)data) == 0) {
578                                 *description = (char*)dcn->text;
579                                 break;
580                         }
581                 }
582                 dcn = dcn->next;
583         }
584         while(ath != NULL)
585         {
586                 if (ath->lang) {
587                         if (strcmp(ath->lang, (char *)data) == 0) {
588                                 *author = (char*)ath->text;
589                                 break;
590                         }
591                 }
592                 ath = ath->next;
593         }
594
595 }
596
597 static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **resolution)
598 {
599         while(icn != NULL)
600         {
601                 if (icn->section) {
602                         if (strcmp(icn->section, (char *)data) == 0) {
603                                 *icon = (char*)icn->text;
604                                 *resolution = (char*)icn->resolution;
605                                 break;
606                         }
607                 }
608                 icn = icn->next;
609         }
610 }
611
612 static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
613 {
614         while(image != NULL)
615         {
616                 if (image->section) {
617                         if (strcmp(image->section, (char *)data) == 0) {
618                                 *lang = (char*)image->lang;
619                                 *img = (char*)image->text;
620                                 break;
621                         }
622                 }
623                 image = image->next;
624         }
625 }
626
627 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
628 {
629         int ret = -1;
630         char *label = NULL;
631         char *icon = NULL;
632         char *description = NULL;
633         char *license = NULL;
634         char *author = NULL;
635         char query[MAX_QUERY_LEN] = {'\0'};
636
637         manifest_x *mfx = (manifest_x *)userdata;
638         label_x *lbl = mfx->label;
639         license_x *lcn = mfx->license;
640         icon_x *icn = mfx->icon;
641         description_x *dcn = mfx->description;
642         author_x *ath = mfx->author;
643
644         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
645         if (!label && !description && !icon && !license && !author)
646                 return;
647
648         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
649                 "package_label, package_icon, package_description, package_license, package_author) values " \
650                 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
651                 mfx->package,
652                 (char*)data,
653                 label,
654                 icon,
655                 __get_str(description),
656                 __get_str(license),
657                 __get_str(author));
658
659         ret = __exec_query(query);
660         if (ret == -1)
661                 _LOGD("Package Localized Info DB Insert failed\n");
662 }
663
664 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
665 {
666         int ret = -1;
667         char *label = NULL;
668         char *icon = NULL;
669         char query[MAX_QUERY_LEN] = {'\0'};
670
671         uiapplication_x *up = (uiapplication_x*)userdata;
672         label_x *lbl = up->label;
673         icon_x *icn = up->icon;
674
675         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
676         if (!label && !icon)
677                 return;
678         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
679                 "app_label, app_icon) values " \
680                 "('%q', '%q', '%q', '%q')", up->appid, (char*)data,
681                 label, icon);
682         ret = __exec_query(query);
683         if (ret == -1)
684                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
685
686         /*insert ui app locale info to pkg locale to get mainapp data */
687         if (strcasecmp(up->mainapp, "true")==0) {
688                 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
689                         "package_label, package_icon, package_description, package_license, package_author) values " \
690                         "('%q', '%q', '%q', '%q', '%q', '%q', '%q')",
691                         up->package,
692                         (char*)data,
693                         label,
694                         icon,
695                         PKGMGR_PARSER_EMPTY_STR,
696                         PKGMGR_PARSER_EMPTY_STR,
697                         PKGMGR_PARSER_EMPTY_STR);
698
699                 ret = __exec_query_no_msg(query);
700
701                 if (icon != NULL) {
702                         sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
703                                 "where package='%s' and package_locale='%s'", icon, up->package, (char*)data);
704                         ret = __exec_query_no_msg(query);
705                 }
706         }
707 }
708
709 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
710 {
711         int ret = -1;
712         char *icon = NULL;
713         char *resolution = NULL;
714         char query[MAX_QUERY_LEN] = {'\0'};
715
716         uiapplication_x *up = (uiapplication_x*)userdata;
717         icon_x *icn = up->icon;
718
719         __extract_icon_data(data, icn, &icon, &resolution);
720         if (!icon && !resolution)
721                 return;
722         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
723                 "app_icon, app_icon_section, app_icon_resolution) values " \
724                 "('%q', '%q', '%q', '%q')", up->appid,
725                 icon, (char*)data, resolution);
726
727         ret = __exec_query(query);
728         if (ret == -1)
729                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
730
731 }
732
733 static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
734 {
735         int ret = -1;
736         char *lang = NULL;
737         char *img = NULL;
738         char query[MAX_QUERY_LEN] = {'\0'};
739
740         uiapplication_x *up = (uiapplication_x*)userdata;
741         image_x *image = up->image;
742
743         __extract_image_data(data, image, &lang, &img);
744         if (!lang && !img)
745                 return;
746         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
747                 "app_image_section, app_image) values " \
748                 "('%q', '%q', '%q', '%q')", up->appid, lang, (char*)data, img);
749
750         ret = __exec_query(query);
751         if (ret == -1)
752                 _LOGD("Package UiApp image Info DB Insert failed\n");
753
754 }
755
756
757 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata)
758 {
759         int ret = -1;
760         char *icon = NULL;
761         char *label = NULL;
762         char query[MAX_QUERY_LEN] = {'\0'};
763
764         serviceapplication_x *sp = (serviceapplication_x*)userdata;
765         label_x *lbl = sp->label;
766         icon_x *icn = sp->icon;
767
768         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
769         if (!icon && !label)
770                 return;
771         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
772                 "app_label, app_icon) values " \
773                 "('%q', '%q', '%q', '%q')", sp->appid, (char*)data,
774                 label, icon);
775         ret = __exec_query(query);
776         if (ret == -1)
777                 _LOGD("Package ServiceApp Localized Info DB Insert failed\n");
778 }
779
780 static int __insert_ui_mainapp_info(manifest_x *mfx)
781 {
782         uiapplication_x *up = mfx->uiapplication;
783         int ret = -1;
784         char query[MAX_QUERY_LEN] = {'\0'};
785         while(up != NULL)
786         {
787                 snprintf(query, MAX_QUERY_LEN,
788                         "update package_app_info set app_mainapp='%s' where app_id='%s'", up->mainapp, up->appid);
789
790                 ret = __exec_query(query);
791                 if (ret == -1) {
792                         _LOGD("Package UiApp Info DB Insert Failed\n");
793                         return -1;
794                 }
795                 if (strcasecmp(up->mainapp, "True")==0)
796                         mfx->mainapp_id = strdup(up->appid);
797
798                 up = up->next;
799                 memset(query, '\0', MAX_QUERY_LEN);
800         }
801
802         if (mfx->mainapp_id == NULL){
803                 if (mfx->uiapplication && mfx->uiapplication->appid) {
804                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
805                 } else {
806                         _LOGD("Not valid appid\n");
807                         return -1;
808                 }
809
810                 ret = __exec_query(query);
811                 if (ret == -1) {
812                         _LOGD("Package UiApp Info DB Insert Failed\n");
813                         return -1;
814                 }
815
816                 free((void *)mfx->uiapplication->mainapp);
817                 mfx->uiapplication->mainapp= strdup("true");
818                 mfx->mainapp_id = strdup(mfx->uiapplication->appid);
819         }
820
821         memset(query, '\0', MAX_QUERY_LEN);
822         snprintf(query, MAX_QUERY_LEN,
823                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
824         ret = __exec_query(query);
825         if (ret == -1) {
826                 _LOGD("Package Info DB update Failed\n");
827                 return -1;
828         }
829
830         return 0;
831 }
832 /* _PRODUCT_LAUNCHING_ENHANCED_
833 *  up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_appstatus
834 */
835 static int __insert_uiapplication_info(manifest_x *mfx)
836 {
837         uiapplication_x *up = mfx->uiapplication;
838         int ret = -1;
839         char query[MAX_QUERY_LEN] = {'\0'};
840         while(up != NULL)
841         {
842                 snprintf(query, MAX_QUERY_LEN,
843                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
844                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
845                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
846                         "app_preload, app_submode, app_submode_mainid, component_type, package, app_launch_mode) " \
847                         "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')",\
848                          up->appid,
849                          "uiapp",
850                          up->exec,
851                          up->nodisplay,
852                          up->type,
853                          PKGMGR_PARSER_EMPTY_STR,
854                          up->multiple,
855                          PKGMGR_PARSER_EMPTY_STR,
856                          up->taskmanage,
857                          up->enabled,
858                          up->hwacceleration,
859                          up->screenreader,
860                          up->mainapp,
861                          __get_str(up->recentimage),
862                          up->launchcondition,
863                          up->indicatordisplay,
864                          __get_str(up->portraitimg),
865                          __get_str(up->landscapeimg),
866                          up->guestmode_visibility,
867                          up->permission_type,
868                          mfx->preload,
869                          up->submode,
870                          __get_str(up->submode_mainid),
871                          up->component_type,
872                          mfx->package,
873                          up->launch_mode);
874
875                 ret = __exec_query(query);
876                 if (ret == -1) {
877                         _LOGD("Package UiApp Info DB Insert Failed\n");
878                         return -1;
879                 }
880                 up = up->next;
881                 memset(query, '\0', MAX_QUERY_LEN);
882         }
883         return 0;
884 }
885
886 static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
887 {
888         uiapplication_x *up = mfx->uiapplication;
889         category_x *ct = NULL;
890         int ret = -1;
891         char query[MAX_QUERY_LEN] = {'\0'};
892         while(up != NULL)
893         {
894                 ct = up->category;
895                 while (ct != NULL)
896                 {
897                         snprintf(query, MAX_QUERY_LEN,
898                                 "insert into package_app_app_category(app_id, category) " \
899                                 "values('%s','%s')",\
900                                  up->appid, ct->name);
901                         ret = __exec_query(query);
902                         if (ret == -1) {
903                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
904                                 return -1;
905                         }
906                         ct = ct->next;
907                         memset(query, '\0', MAX_QUERY_LEN);
908                 }
909                 up = up->next;
910         }
911         return 0;
912 }
913
914 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
915 {
916         uiapplication_x *up = mfx->uiapplication;
917         metadata_x *md = NULL;
918         int ret = -1;
919         char query[MAX_QUERY_LEN] = {'\0'};
920         while(up != NULL)
921         {
922                 md = up->metadata;
923                 while (md != NULL)
924                 {
925                         if (md->key) {
926                                 snprintf(query, MAX_QUERY_LEN,
927                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
928                                         "values('%s','%s', '%s')",\
929                                          up->appid, md->key, md->value);
930                                 ret = __exec_query(query);
931                                 if (ret == -1) {
932                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
933                                         return -1;
934                                 }
935                         }
936                         md = md->next;
937                         memset(query, '\0', MAX_QUERY_LEN);
938                 }
939                 up = up->next;
940         }
941         return 0;
942 }
943
944 static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
945 {
946         uiapplication_x *up = mfx->uiapplication;
947         permission_x *pm = NULL;
948         int ret = -1;
949         char query[MAX_QUERY_LEN] = {'\0'};
950         while(up != NULL)
951         {
952                 pm = up->permission;
953                 while (pm != NULL)
954                 {
955                         snprintf(query, MAX_QUERY_LEN,
956                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
957                                 "values('%s','%s', '%s')",\
958                                  up->appid, pm->type, pm->value);
959                         ret = __exec_query(query);
960                         if (ret == -1) {
961                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
962                                 return -1;
963                         }
964                         pm = pm->next;
965                         memset(query, '\0', MAX_QUERY_LEN);
966                 }
967                 up = up->next;
968         }
969         return 0;
970 }
971
972 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
973 {
974         uiapplication_x *up = mfx->uiapplication;
975         appcontrol_x *acontrol = NULL;
976         int ret = -1;
977         char query[MAX_QUERY_LEN] = {'\0'};
978         char buf[BUFSIZE] = {'\0'};
979         while (up != NULL) {
980                 acontrol = up->appcontrol;
981                 while (acontrol != NULL) {
982                         snprintf(buf, BUFSIZE, "%s|%s|%s",\
983                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
984                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
985                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
986                         snprintf(query, MAX_QUERY_LEN,
987                                         "insert into package_app_app_control(app_id, app_control) " \
988                                         "values('%s', '%s')",\
989                                         up->appid, buf);
990                         ret = __exec_query(query);
991                         if (ret == -1) {
992                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
993                                 return -1;
994                         }
995                         memset(query, '\0', MAX_QUERY_LEN);
996                         acontrol = acontrol->next;
997                 }
998                 up = up->next;
999         }
1000         return 0;
1001 }
1002
1003 static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
1004 {
1005         uiapplication_x *up = mfx->uiapplication;
1006         appsvc_x *asvc = NULL;
1007         operation_x *op = NULL;
1008         mime_x *mi = NULL;
1009         uri_x *ui = NULL;
1010         subapp_x *sub = NULL;
1011         int ret = -1;
1012         char query[MAX_QUERY_LEN] = {'\0'};
1013         const char *operation = NULL;
1014         const char *mime = NULL;
1015         const char *uri = NULL;
1016         const char *subapp = NULL;
1017         while(up != NULL)
1018         {
1019                 asvc = up->appsvc;
1020                 while(asvc != NULL)
1021                 {
1022                         op = asvc->operation;
1023                         while(op != NULL)
1024                         {
1025                                 if (op)
1026                                         operation = op->name;
1027                                 mi = asvc->mime;
1028
1029                                 do
1030                                 {
1031                                         if (mi)
1032                                                 mime = mi->name;
1033                                         sub = asvc->subapp;
1034                                         do
1035                                         {
1036                                                 if (sub)
1037                                                         subapp = sub->name;
1038                                                 ui = asvc->uri;
1039                                                 do
1040                                                 {
1041                                                         if (ui)
1042                                                                 uri = ui->name;
1043                                                         snprintf(query, MAX_QUERY_LEN,
1044                                                                  "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1045                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1046                                                                  up->appid,
1047                                                                  operation,
1048                                                                  __get_str(uri),
1049                                                                  __get_str(mime),
1050                                                                  __get_str(subapp));
1051
1052                                                         ret = __exec_query(query);
1053                                                         if (ret == -1) {
1054                                                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1055                                                                 return -1;
1056                                                         }
1057                                                         memset(query, '\0', MAX_QUERY_LEN);
1058                                                         if (ui)
1059                                                                 ui = ui->next;
1060                                                         uri = NULL;
1061                                                 } while(ui != NULL);
1062                                                 if (sub)
1063                                                         sub = sub->next;
1064                                                 subapp = NULL;
1065                                         }while(sub != NULL);
1066                                         if (mi)
1067                                                 mi = mi->next;
1068                                         mime = NULL;
1069                                 }while(mi != NULL);
1070                                 if (op)
1071                                         op = op->next;
1072                                 operation = NULL;
1073                         }
1074                         asvc = asvc->next;
1075                 }
1076                 up = up->next;
1077         }
1078         return 0;
1079 }
1080
1081 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx)
1082 {
1083         uiapplication_x *up = mfx->uiapplication;
1084         datacontrol_x *dc = NULL;
1085         int ret = -1;
1086         char query[MAX_QUERY_LEN] = {'\0'};
1087
1088         while(up != NULL)
1089         {
1090                 dc = up->datacontrol;
1091                 while(dc != NULL)
1092                 {
1093                         snprintf(query, MAX_QUERY_LEN,
1094                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1095                                         "values('%s', '%s', '%s', '%s')",\
1096                                         mfx->uiapplication->appid,
1097                                         dc->providerid,
1098                                         dc->access,
1099                                         dc->type);
1100
1101                         ret = __exec_query(query);
1102                         if (ret == -1) {
1103                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1104                                 return -1;
1105                         }
1106                         memset(query, '\0', MAX_QUERY_LEN);
1107                         dc = dc->next;
1108                 }
1109                 up = up->next;
1110         }
1111         return 0;
1112 }
1113
1114 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
1115 {
1116         uiapplication_x *up = mfx->uiapplication;
1117         datashare_x *ds = NULL;
1118         request_x *rq = NULL;
1119         int ret = -1;
1120         char query[MAX_QUERY_LEN] = {'\0'};
1121         while(up != NULL)
1122         {
1123                 ds = up->datashare;
1124                 while(ds != NULL)
1125                 {
1126                         rq = ds->request;
1127                         while(rq != NULL)
1128                         {
1129                                 snprintf(query, MAX_QUERY_LEN,
1130                                          "insert into package_app_share_request(app_id, data_share_request) " \
1131                                         "values('%s', '%s')",\
1132                                          up->appid, rq->text);
1133                                 ret = __exec_query(query);
1134                                 if (ret == -1) {
1135                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1136                                         return -1;
1137                                 }
1138                                 memset(query, '\0', MAX_QUERY_LEN);
1139                                 rq = rq->next;
1140                         }
1141                         ds = ds->next;
1142                 }
1143                 up = up->next;
1144         }
1145         return 0;
1146 }
1147
1148 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
1149 {
1150         uiapplication_x *up = mfx->uiapplication;
1151         datashare_x *ds = NULL;
1152         define_x *df = NULL;
1153         allowed_x *al = NULL;
1154         int ret = -1;
1155         char query[MAX_QUERY_LEN] = {'\0'};
1156         while(up != NULL)
1157         {
1158                 ds = up->datashare;
1159                 while(ds != NULL)
1160                 {
1161                         df = ds->define;
1162                         while(df != NULL)
1163                         {
1164                                 al = df->allowed;
1165                                 while(al != NULL)
1166                                 {
1167                                         snprintf(query, MAX_QUERY_LEN,
1168                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1169                                                 "values('%s', '%s', '%s')",\
1170                                                  up->appid, df->path, al->text);
1171                                         ret = __exec_query(query);
1172                                         if (ret == -1) {
1173                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1174                                                 return -1;
1175                                         }
1176                                         memset(query, '\0', MAX_QUERY_LEN);
1177                                         al = al->next;
1178                                 }
1179                                 df = df->next;
1180                         }
1181                         ds = ds->next;
1182                 }
1183                 up = up->next;
1184         }
1185         return 0;
1186 }
1187
1188 static int __insert_serviceapplication_info(manifest_x *mfx)
1189 {
1190         serviceapplication_x *sp = mfx->serviceapplication;
1191         int ret = -1;
1192         char query[MAX_QUERY_LEN] = {'\0'};
1193         while(sp != NULL)
1194         {
1195                 snprintf(query, MAX_QUERY_LEN,
1196                          "insert into package_app_info(app_id, app_component, app_exec, app_type, app_onboot, " \
1197                         "app_multiple, app_autorestart, app_enabled, app_permissiontype, package) " \
1198                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1199                          sp->appid, "svcapp", sp->exec, sp->type, sp->onboot, "\0",
1200                          sp->autorestart, sp->enabled, sp->permission_type, mfx->package);
1201                 ret = __exec_query(query);
1202                 if (ret == -1) {
1203                         _LOGD("Package ServiceApp Info DB Insert Failed\n");
1204                         return -1;
1205                 }
1206                 sp = sp->next;
1207                 memset(query, '\0', MAX_QUERY_LEN);
1208         }
1209         return 0;
1210 }
1211
1212 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx)
1213 {
1214         serviceapplication_x *sp = mfx->serviceapplication;
1215         category_x *ct = NULL;
1216         int ret = -1;
1217         char query[MAX_QUERY_LEN] = {'\0'};
1218         while(sp != NULL)
1219         {
1220                 ct = sp->category;
1221                 while (ct != NULL)
1222                 {
1223                         snprintf(query, MAX_QUERY_LEN,
1224                                 "insert into package_app_app_category(app_id, category) " \
1225                                 "values('%s','%s')",\
1226                                  sp->appid, ct->name);
1227                         ret = __exec_query(query);
1228                         if (ret == -1) {
1229                                 _LOGD("Package ServiceApp Category Info DB Insert Failed\n");
1230                                 return -1;
1231                         }
1232                         ct = ct->next;
1233                         memset(query, '\0', MAX_QUERY_LEN);
1234                 }
1235                 sp = sp->next;
1236         }
1237         return 0;
1238 }
1239
1240 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx)
1241 {
1242         serviceapplication_x *sp = mfx->serviceapplication;
1243         metadata_x *md = NULL;
1244         int ret = -1;
1245         char query[MAX_QUERY_LEN] = {'\0'};
1246         while(sp != NULL)
1247         {
1248                 md = sp->metadata;
1249                 while (md != NULL)
1250                 {
1251                         if (md->key) {
1252                                 snprintf(query, MAX_QUERY_LEN,
1253                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1254                                         "values('%s','%s', '%s')",\
1255                                          sp->appid, md->key, md->value);
1256                                 ret = __exec_query(query);
1257                                 if (ret == -1) {
1258                                         _LOGD("Package ServiceApp Metadata Info DB Insert Failed\n");
1259                                         return -1;
1260                                 }
1261                         }
1262                         md = md->next;
1263                         memset(query, '\0', MAX_QUERY_LEN);
1264                 }
1265                 sp = sp->next;
1266         }
1267         return 0;
1268 }
1269
1270 static int __insert_serviceapplication_apppermission_info(manifest_x *mfx)
1271 {
1272         serviceapplication_x *sp = mfx->serviceapplication;
1273         permission_x *pm = NULL;
1274         int ret = -1;
1275         char query[MAX_QUERY_LEN] = {'\0'};
1276         while(sp != NULL)
1277         {
1278                 pm = sp->permission;
1279                 while (pm != NULL)
1280                 {
1281                         snprintf(query, MAX_QUERY_LEN,
1282                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1283                                 "values('%s','%s', '%s')",\
1284                                  sp->appid, pm->type, pm->value);
1285                         ret = __exec_query(query);
1286                         if (ret == -1) {
1287                                 _LOGD("Package ServiceApp permission Info DB Insert Failed\n");
1288                                 return -1;
1289                         }
1290                         pm = pm->next;
1291                         memset(query, '\0', MAX_QUERY_LEN);
1292                 }
1293                 sp = sp->next;
1294         }
1295         return 0;
1296 }
1297
1298 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx)
1299 {
1300         serviceapplication_x *sp = mfx->serviceapplication;
1301         appcontrol_x *acontrol = NULL;
1302         int ret = -1;
1303         char query[MAX_QUERY_LEN] = {'\0'};
1304         char buf[BUFSIZE] = {'\0'};
1305         while (sp != NULL) {
1306                 acontrol = sp->appcontrol;
1307                 while (acontrol != NULL) {
1308                         snprintf(buf, BUFSIZE, "%s|%s|%s",
1309                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1310                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1311                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1312                         snprintf(query, MAX_QUERY_LEN,
1313                                         "insert into package_app_app_control(app_id, app_control) " \
1314                                         "values('%s', '%s')",\
1315                                         sp->appid, buf);
1316                         ret = __exec_query(query);
1317                         if (ret == -1) {
1318                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1319                                 return -1;
1320                         }
1321                         memset(query, '\0', MAX_QUERY_LEN);
1322                         acontrol = acontrol->next;
1323                 }
1324                 sp = sp->next;
1325         }
1326         return 0;
1327 }
1328
1329 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
1330 {
1331         serviceapplication_x *sp = mfx->serviceapplication;
1332         appsvc_x *asvc = NULL;
1333         int ret = -1;
1334         char query[MAX_QUERY_LEN] = {'\0'};
1335         operation_x *op = NULL;
1336         mime_x *mi = NULL;
1337         uri_x *ui = NULL;
1338         subapp_x *sub = NULL;
1339         const char *operation = NULL;
1340         const char *mime = NULL;
1341         const char *uri = NULL;
1342         const char *subapp = NULL;
1343         while(sp != NULL)
1344         {
1345                 asvc = sp->appsvc;
1346                 while(asvc != NULL)
1347                 {
1348                         op = asvc->operation;
1349                         while(op != NULL)
1350                         {
1351                         if (op)
1352                                 operation = op->name;
1353                         mi = asvc->mime;
1354                                 do
1355                                 {
1356                                 if (mi)
1357                                         mime = mi->name;
1358                                 sub = asvc->subapp;
1359                                         do
1360                                         {
1361                                         if (sub)
1362                                                 subapp = sub->name;
1363                                         ui = asvc->uri;
1364                                                         do
1365                                                         {
1366                                                                 if (ui)
1367                                                                         uri = ui->name;
1368                                                                 snprintf(query, MAX_QUERY_LEN,
1369                                                                          "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1370                                                                         "values('%s', '%s', '%s', '%s', '%s')",\
1371                                                                          sp->appid,
1372                                                                          operation,
1373                                                                         __get_str(uri),
1374                                                                         __get_str(mime),
1375                                                                         __get_str(subapp));
1376                                                                 ret = __exec_query(query);
1377                                                                 if (ret == -1) {
1378                                                                         _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1379                                                                         return -1;
1380                                                                 }
1381                                                                 memset(query, '\0', MAX_QUERY_LEN);
1382                                                                 if (ui)
1383                                                                         ui = ui->next;
1384                                                                 uri = NULL;
1385                                                         } while(ui != NULL);
1386                                                 if (sub)
1387                                                         sub     = sub->next;
1388                                                 subapp = NULL;
1389                                         }while(sub != NULL);
1390                                         if (mi)
1391                                                 mi = mi->next;
1392                                         mime = NULL;
1393                                 }while(mi != NULL);
1394                                 if (op)
1395                                         op = op->next;
1396                                 operation = NULL;
1397                         }
1398                         asvc = asvc->next;
1399                 }
1400                 sp = sp->next;
1401         }
1402         return 0;
1403 }
1404
1405 static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx)
1406 {
1407         serviceapplication_x *sp = mfx->serviceapplication;
1408         datacontrol_x *dc = NULL;
1409         int ret = -1;
1410         char query[MAX_QUERY_LEN] = {'\0'};
1411
1412         while(sp != NULL)
1413         {
1414                 dc = sp->datacontrol;
1415                 while(dc != NULL)
1416                 {
1417                         snprintf(query, MAX_QUERY_LEN,
1418                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1419                                         "values('%s', '%s', '%s', '%s')",\
1420                                         mfx->serviceapplication->appid,
1421                                         dc->providerid,
1422                                         dc->access,
1423                                         dc->type);
1424
1425                         ret = __exec_query(query);
1426                         if (ret == -1) {
1427                                 _LOGD("Package ServiceApp Data Control DB Insert Failed\n");
1428                                 return -1;
1429                         }
1430                         memset(query, '\0', MAX_QUERY_LEN);
1431                         dc = dc->next;
1432                 }
1433                 sp = sp->next;
1434         }
1435         return 0;
1436 }
1437
1438 static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
1439 {
1440         serviceapplication_x *sp = mfx->serviceapplication;
1441         datashare_x *ds = NULL;
1442         request_x *rq = NULL;
1443         int ret = -1;
1444         char query[MAX_QUERY_LEN] = {'\0'};
1445         while(sp != NULL)
1446         {
1447                 ds = sp->datashare;
1448                 while(ds != NULL)
1449                 {
1450                         rq = ds->request;
1451                         while(rq != NULL)
1452                         {
1453                                 snprintf(query, MAX_QUERY_LEN,
1454                                          "insert into package_app_share_request(app_id, data_share_request) " \
1455                                         "values('%s', '%s')",\
1456                                          sp->appid, rq->text);
1457                                 ret = __exec_query(query);
1458                                 if (ret == -1) {
1459                                         _LOGD("Package ServiceApp Share Request DB Insert Failed\n");
1460                                         return -1;
1461                                 }
1462                                 memset(query, '\0', MAX_QUERY_LEN);
1463                                 rq = rq->next;
1464                         }
1465                         ds = ds->next;
1466                 }
1467                 sp = sp->next;
1468         }
1469         return 0;
1470 }
1471
1472
1473
1474 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
1475 {
1476         serviceapplication_x *sp = mfx->serviceapplication;
1477         datashare_x *ds = NULL;
1478         define_x *df = NULL;
1479         allowed_x *al = NULL;
1480         int ret = -1;
1481         char query[MAX_QUERY_LEN] = {'\0'};
1482         while(sp != NULL)
1483         {
1484                 ds = sp->datashare;
1485                 while(ds != NULL)
1486                 {
1487                         df = ds->define;
1488                         while(df != NULL)
1489                         {
1490                                 al = df->allowed;
1491                                 while(al != NULL)
1492                                 {
1493                                         snprintf(query, MAX_QUERY_LEN,
1494                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1495                                                 "values('%s', '%s', '%s')",\
1496                                                  sp->appid, df->path, al->text);
1497                                         ret = __exec_query(query);
1498                                         if (ret == -1) {
1499                                                 _LOGD("Package App Share Allowed DB Insert Failed\n");
1500                                                 return -1;
1501                                         }
1502                                         memset(query, '\0', MAX_QUERY_LEN);
1503                                         al = al->next;
1504                                 }
1505                                 df = df->next;
1506                         }
1507                         ds = ds->next;
1508                 }
1509                 sp = sp->next;
1510         }
1511         return 0;
1512 }
1513
1514 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1515 {
1516         label_x *lbl = mfx->label;
1517         license_x *lcn = mfx->license;
1518         icon_x *icn = mfx->icon;
1519         description_x *dcn = mfx->description;
1520         author_x *ath = mfx->author;
1521         uiapplication_x *up = mfx->uiapplication;
1522         uiapplication_x *up_icn = mfx->uiapplication;
1523         uiapplication_x *up_image = mfx->uiapplication;
1524         serviceapplication_x *sp = mfx->serviceapplication;
1525         privileges_x *pvs = NULL;
1526         privilege_x *pv = NULL;
1527         char query[MAX_QUERY_LEN] = { '\0' };
1528         int ret = -1;
1529         const char *auth_name = NULL;
1530         const char *auth_email = NULL;
1531         const char *auth_href = NULL;
1532
1533         GList *pkglocale = NULL;
1534         GList *applocale = NULL;
1535         GList *appicon = NULL;
1536         GList *appimage = NULL;
1537
1538         if (ath) {
1539                 if (ath->text)
1540                         auth_name = ath->text;
1541                 if (ath->email)
1542                         auth_email = ath->email;
1543                 if (ath->href)
1544                         auth_href = ath->href;
1545         }
1546         /*Insert in the package_cert_info CERT_DB*/
1547         pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1548         ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1549         if (ret != PMINFO_R_OK) {
1550                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1551                 _LOGE("Cert Info DB create handle failed\n");
1552                 return -1;
1553         }
1554         ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1555         if (ret != PMINFO_R_OK) {
1556                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1557                 _LOGE("Cert Info DB Insert Failed\n");
1558                 return -1;
1559         }
1560
1561         /*Insert in the package_info DB*/
1562         snprintf(query, MAX_QUERY_LEN,
1563                  "insert into package_info(package, package_type, package_version, package_api_version, install_location, package_size, " \
1564                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1565                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path) " \
1566                 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1567                  mfx->package,
1568                  mfx->type,
1569                  mfx->version,
1570                  __get_str(mfx->api_version),
1571                  __get_str(mfx->installlocation),
1572                  __get_str(mfx->package_size),
1573                  mfx->removable,
1574                  mfx->preload,
1575                  mfx->readonly,
1576                  mfx->update,
1577                  mfx->appsetting,
1578                  mfx->nodisplay_setting,
1579                  mfx->system,
1580                  __get_str(auth_name),
1581                  __get_str(auth_email),
1582                  __get_str(auth_href),
1583                  mfx->installed_time,
1584                  mfx->installed_storage,
1585                  __get_str(mfx->storeclient_id),
1586                  mfx->mainapp_id,
1587                  __get_str(mfx->package_url),
1588                  mfx->root_path,
1589                  __get_str(mfx->csc_path));
1590
1591         ret = __exec_query(query);
1592         if (ret == -1) {
1593                 _LOGD("Package Info DB Insert Failed\n");
1594                 return -1;
1595         }
1596
1597         /*Insert in the package_privilege_info DB*/
1598         pvs = mfx->privileges;
1599         while (pvs != NULL) {
1600                 pv = pvs->privilege;
1601                 while (pv != NULL) {
1602                         memset(query, '\0', MAX_QUERY_LEN);
1603                         snprintf(query, MAX_QUERY_LEN,
1604                                 "insert into package_privilege_info(package, privilege) " \
1605                                 "values('%s','%s')",\
1606                                  mfx->package, pv->text);
1607                         ret = __exec_query(query);
1608                         if (ret == -1) {
1609                                 _LOGD("Package Privilege Info DB Insert Failed\n");
1610                                 return -1;
1611                         }
1612                         pv = pv->next;
1613                 }
1614                 pvs = pvs->next;
1615         }
1616
1617         if (up != NULL) {
1618                 ret = __insert_ui_mainapp_info(mfx);
1619                 if (ret == -1)
1620                         return -1;
1621         }
1622
1623         /*Insert the package locale*/
1624         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1625         /*remove duplicated data in pkglocale*/
1626         __trimfunc(pkglocale);
1627
1628         /*Insert the app locale info */
1629         while(up != NULL)
1630         {
1631                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
1632                 up = up->next;
1633         }
1634         while(sp != NULL)
1635         {
1636                 applocale = __create_locale_list(applocale, sp->label, NULL, sp->icon, NULL, NULL);
1637                 sp = sp->next;
1638         }
1639         /*remove duplicated data in applocale*/
1640         __trimfunc(applocale);
1641
1642         /*Insert the app icon info */
1643         while(up_icn != NULL)
1644         {
1645                 appicon = __create_icon_list(appicon, up_icn->icon);
1646                 up_icn = up_icn->next;
1647         }
1648         /*remove duplicated data in appicon*/
1649         __trimfunc(appicon);
1650
1651         /*Insert the image info */
1652         while(up_image != NULL)
1653         {
1654                 appimage = __create_image_list(appimage, up_image->image);
1655                 up_image = up_image->next;
1656         }
1657         /*remove duplicated data in appimage*/
1658         __trimfunc(appimage);
1659
1660         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1661
1662         /*native app locale info*/
1663         up = mfx->uiapplication;
1664         while(up != NULL)
1665         {
1666                 g_list_foreach(applocale, __insert_uiapplication_locale_info, (gpointer)up);
1667                 up = up->next;
1668         }
1669         /*agent app locale info*/
1670         sp = mfx->serviceapplication;
1671         while(sp != NULL)
1672         {
1673                 g_list_foreach(applocale, __insert_serviceapplication_locale_info, (gpointer)sp);
1674                 sp = sp->next;
1675         }
1676
1677         /*app icon locale info*/
1678         up_icn = mfx->uiapplication;
1679         while(up_icn != NULL)
1680         {
1681                 g_list_foreach(appicon, __insert_uiapplication_icon_section_info, (gpointer)up_icn);
1682                 up_icn = up_icn->next;
1683         }
1684
1685         /*app image info*/
1686         up_image = mfx->uiapplication;
1687         while(up_image != NULL)
1688         {
1689                 g_list_foreach(appimage, __insert_uiapplication_image_info, (gpointer)up_image);
1690                 up_image = up_image->next;
1691         }
1692
1693         g_list_free(pkglocale);
1694         pkglocale = NULL;
1695         g_list_free(applocale);
1696         applocale = NULL;
1697         g_list_free(appicon);
1698         appicon = NULL;
1699         g_list_free(appimage);
1700         appimage = NULL;
1701
1702         /*Insert in the package_app_info DB*/
1703         ret = __insert_uiapplication_info(mfx);
1704         if (ret == -1)
1705                 return -1;
1706         ret = __insert_serviceapplication_info(mfx);
1707         if (ret == -1)
1708                 return -1;
1709         /*Insert in the package_app_app_control DB*/
1710         ret = __insert_uiapplication_appcontrol_info(mfx);
1711         if (ret == -1)
1712                 return -1;
1713         ret = __insert_serviceapplication_appcontrol_info(mfx);
1714         if (ret == -1)
1715                 return -1;
1716
1717         /*Insert in the package_app_app_category DB*/
1718         ret = __insert_uiapplication_appcategory_info(mfx);
1719         if (ret == -1)
1720                 return -1;
1721         ret = __insert_serviceapplication_appcategory_info(mfx);
1722         if (ret == -1)
1723                 return -1;
1724
1725         /*Insert in the package_app_app_metadata DB*/
1726         ret = __insert_uiapplication_appmetadata_info(mfx);
1727         if (ret == -1)
1728                 return -1;
1729         ret = __insert_serviceapplication_appmetadata_info(mfx);
1730         if (ret == -1)
1731                 return -1;
1732
1733         /*Insert in the package_app_app_permission DB*/
1734         ret = __insert_uiapplication_apppermission_info(mfx);
1735         if (ret == -1)
1736                 return -1;
1737         ret = __insert_serviceapplication_apppermission_info(mfx);
1738         if (ret == -1)
1739                 return -1;
1740
1741         /*Insert in the package_app_app_svc DB*/
1742         ret = __insert_uiapplication_appsvc_info(mfx);
1743         if (ret == -1)
1744                 return -1;
1745         ret = __insert_serviceapplication_appsvc_info(mfx);
1746         if (ret == -1)
1747                 return -1;
1748
1749         /*Insert in the package_app_share_allowed DB*/
1750         ret = __insert_uiapplication_share_allowed_info(mfx);
1751         if (ret == -1)
1752                 return -1;
1753         ret = __insert_serviceapplication_share_allowed_info(mfx);
1754         if (ret == -1)
1755                 return -1;
1756
1757         /*Insert in the package_app_share_request DB*/
1758         ret = __insert_uiapplication_share_request_info(mfx);
1759         if (ret == -1)
1760                 return -1;
1761         ret = __insert_serviceapplication_share_request_info(mfx);
1762         if (ret == -1)
1763                 return -1;
1764
1765         /*Insert in the package_app_data_control DB*/
1766         ret = __insert_uiapplication_datacontrol_info(mfx);
1767         if (ret == -1)
1768                 return -1;
1769         ret = __insert_serviceapplication_datacontrol_info(mfx);
1770         if (ret == -1)
1771                 return -1;
1772
1773         return 0;
1774
1775 }
1776
1777 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1778 {
1779         char query[MAX_QUERY_LEN] = { '\0' };
1780         int ret = -1;
1781         memset(query, '\0', MAX_QUERY_LEN);
1782         snprintf(query, MAX_QUERY_LEN,
1783                  "delete from %s where app_id='%s'", db_table, appid);
1784         ret = __exec_query(query);
1785         if (ret == -1) {
1786                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1787                 return -1;
1788         }
1789         return 0;
1790 }
1791
1792 static int __delete_subpkg_info_from_db(char *appid)
1793 {
1794         int ret = -1;
1795
1796         ret = __delete_appinfo_from_db("package_app_info", appid);
1797         if (ret < 0)
1798                 return ret;
1799         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1800         if (ret < 0)
1801                 return ret;
1802         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1803         if (ret < 0)
1804                 return ret;
1805         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1806         if (ret < 0)
1807                 return ret;
1808         ret = __delete_appinfo_from_db("package_app_app_svc", appid);
1809         if (ret < 0)
1810                 return ret;
1811         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1812         if (ret < 0)
1813                 return ret;
1814         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1815         if (ret < 0)
1816                 return ret;
1817         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1818         if (ret < 0)
1819                 return ret;
1820         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1821         if (ret < 0)
1822                 return ret;
1823         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1824         if (ret < 0)
1825                 return ret;
1826         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1827         if (ret < 0)
1828                 return ret;
1829         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1830         if (ret < 0)
1831                 return ret;
1832
1833         return 0;
1834 }
1835
1836 static int __delete_subpkg_from_db(manifest_x *mfx)
1837 {
1838         char query[MAX_QUERY_LEN] = { '\0' };
1839         char *error_message = NULL;
1840
1841         snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1842         if (SQLITE_OK !=
1843             sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1844                 _LOGE("Don't execute query = %s error message = %s\n", query,
1845                        error_message);
1846                 sqlite3_free(error_message);
1847                 return -1;
1848         }
1849         sqlite3_free(error_message);
1850
1851         return 0;
1852 }
1853
1854 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1855 {
1856         char query[MAX_QUERY_LEN] = { '\0' };
1857         int ret = -1;
1858         uiapplication_x *up = mfx->uiapplication;
1859         serviceapplication_x *sp = mfx->serviceapplication;
1860         /*Delete from cert table*/
1861         if (uid != GLOBAL_USER)
1862                 ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
1863         else
1864                 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1865         if (ret) {
1866                 _LOGD("Cert Info  DB Delete Failed\n");
1867                 return -1;
1868         }
1869
1870         /*Delete from Package Info DB*/
1871         snprintf(query, MAX_QUERY_LEN,
1872                  "delete from package_info where package='%s'", mfx->package);
1873         ret = __exec_query(query);
1874         if (ret == -1) {
1875                 _LOGD("Package Info DB Delete Failed\n");
1876                 return -1;
1877         }
1878         memset(query, '\0', MAX_QUERY_LEN);
1879
1880         /*Delete from Package Localized Info*/
1881         snprintf(query, MAX_QUERY_LEN,
1882                  "delete from package_localized_info where package='%s'", mfx->package);
1883         ret = __exec_query(query);
1884         if (ret == -1) {
1885                 _LOGD("Package Localized Info DB Delete Failed\n");
1886                 return -1;
1887         }
1888
1889         /*Delete from Package Privilege Info*/
1890         snprintf(query, MAX_QUERY_LEN,
1891                  "delete from package_privilege_info where package='%s'", mfx->package);
1892         ret = __exec_query(query);
1893         if (ret == -1) {
1894                 _LOGD("Package Privilege Info DB Delete Failed\n");
1895                 return -1;
1896         }
1897
1898         while (up != NULL) {
1899                 ret = __delete_appinfo_from_db("package_app_info", up->appid);
1900                 if (ret < 0)
1901                         return ret;
1902                 ret = __delete_appinfo_from_db("package_app_localized_info", up->appid);
1903                 if (ret < 0)
1904                         return ret;
1905                 ret = __delete_appinfo_from_db("package_app_icon_section_info", up->appid);
1906                 if (ret < 0)
1907                         return ret;
1908                 ret = __delete_appinfo_from_db("package_app_image_info", up->appid);
1909                 if (ret < 0)
1910                         return ret;
1911                 ret = __delete_appinfo_from_db("package_app_app_svc", up->appid);
1912                 if (ret < 0)
1913                         return ret;
1914                 ret = __delete_appinfo_from_db("package_app_app_control", up->appid);
1915                 if (ret < 0)
1916                         return ret;
1917                 ret = __delete_appinfo_from_db("package_app_app_category", up->appid);
1918                 if (ret < 0)
1919                         return ret;
1920                 ret = __delete_appinfo_from_db("package_app_app_metadata", up->appid);
1921                 if (ret < 0)
1922                         return ret;
1923                 ret = __delete_appinfo_from_db("package_app_app_permission", up->appid);
1924                 if (ret < 0)
1925                         return ret;
1926                 ret = __delete_appinfo_from_db("package_app_share_allowed", up->appid);
1927                 if (ret < 0)
1928                         return ret;
1929                 ret = __delete_appinfo_from_db("package_app_share_request", up->appid);
1930                 if (ret < 0)
1931                         return ret;
1932                 ret = __delete_appinfo_from_db("package_app_data_control", up->appid);
1933                 if (ret < 0)
1934                         return ret;
1935                 up = up->next;
1936         }
1937
1938         while (sp != NULL) {
1939                 ret = __delete_appinfo_from_db("package_app_info", sp->appid);
1940                 if (ret < 0)
1941                         return ret;
1942                 ret = __delete_appinfo_from_db("package_app_localized_info", sp->appid);
1943                 if (ret < 0)
1944                         return ret;
1945                 ret = __delete_appinfo_from_db("package_app_icon_section_info", sp->appid);
1946                 if (ret < 0)
1947                         return ret;
1948                 ret = __delete_appinfo_from_db("package_app_image_info", sp->appid);
1949                 if (ret < 0)
1950                         return ret;
1951                 ret = __delete_appinfo_from_db("package_app_app_svc", sp->appid);
1952                 if (ret < 0)
1953                         return ret;
1954                 ret = __delete_appinfo_from_db("package_app_app_control", sp->appid);
1955                 if (ret < 0)
1956                         return ret;
1957                 ret = __delete_appinfo_from_db("package_app_app_category", sp->appid);
1958                 if (ret < 0)
1959                         return ret;
1960                 ret = __delete_appinfo_from_db("package_app_app_metadata", sp->appid);
1961                 if (ret < 0)
1962                         return ret;
1963                 ret = __delete_appinfo_from_db("package_app_app_permission", sp->appid);
1964                 if (ret < 0)
1965                         return ret;
1966                 ret = __delete_appinfo_from_db("package_app_share_allowed", sp->appid);
1967                 if (ret < 0)
1968                         return ret;
1969                 ret = __delete_appinfo_from_db("package_app_share_request", sp->appid);
1970                 if (ret < 0)
1971                         return ret;
1972                 ret = __delete_appinfo_from_db("package_app_data_control", sp->appid);
1973                 if (ret < 0)
1974                         return ret;
1975                 sp = sp->next;
1976         }
1977
1978         /* if main package has sub pkg, delete sub pkg data*/
1979         __delete_subpkg_from_db(mfx);
1980
1981         return 0;
1982 }
1983
1984 static int __update_preload_condition_in_db()
1985 {
1986         int ret = -1;
1987         char query[MAX_QUERY_LEN] = {'\0'};
1988
1989         snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1990
1991         ret = __exec_query(query);
1992         if (ret == -1)
1993                 _LOGD("Package preload_condition update failed\n");
1994
1995         return ret;
1996 }
1997
1998 API int pkgmgr_parser_initialize_db(uid_t uid)
1999 {
2000         int ret = -1;
2001         /*Manifest DB*/
2002         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
2003         if (ret == -1) {
2004                 _LOGD("package info DB initialization failed\n");
2005                 return ret;
2006         }
2007         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
2008         if (ret == -1) {
2009                 _LOGD("package localized info DB initialization failed\n");
2010                 return ret;
2011         }
2012         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
2013         if (ret == -1) {
2014                 _LOGD("package app app privilege DB initialization failed\n");
2015                 return ret;
2016         }
2017         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
2018         if (ret == -1) {
2019                 _LOGD("package app info DB initialization failed\n");
2020                 return ret;
2021         }
2022         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
2023         if (ret == -1) {
2024                 _LOGD("package app localized info DB initialization failed\n");
2025                 return ret;
2026         }
2027         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
2028         if (ret == -1) {
2029                 _LOGD("package app icon localized info DB initialization failed\n");
2030                 return ret;
2031         }
2032         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
2033         if (ret == -1) {
2034                 _LOGD("package app image info DB initialization failed\n");
2035                 return ret;
2036         }
2037         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
2038         if (ret == -1) {
2039                 _LOGD("package app app control DB initialization failed\n");
2040                 return ret;
2041         }
2042         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
2043         if (ret == -1) {
2044                 _LOGD("package app app category DB initialization failed\n");
2045                 return ret;
2046         }
2047         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
2048         if (ret == -1) {
2049                 _LOGD("package app app category DB initialization failed\n");
2050                 return ret;
2051         }
2052         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
2053         if (ret == -1) {
2054                 _LOGD("package app app permission DB initialization failed\n");
2055                 return ret;
2056         }
2057         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
2058         if (ret == -1) {
2059                 _LOGD("package app app svc DB initialization failed\n");
2060                 return ret;
2061         }
2062         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
2063         if (ret == -1) {
2064                 _LOGD("package app share allowed DB initialization failed\n");
2065                 return ret;
2066         }
2067         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
2068         if (ret == -1) {
2069                 _LOGD("package app share request DB initialization failed\n");
2070                 return ret;
2071         }
2072         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
2073         if (ret == -1) {
2074                 _LOGD("package app data control DB initialization failed\n");
2075                 return ret;
2076         }
2077         /*Cert DB*/
2078         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
2079         if (ret == -1) {
2080                 _LOGD("package cert info DB initialization failed\n");
2081                 return ret;
2082         }
2083         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
2084         if (ret == -1) {
2085                 _LOGD("package cert index info DB initialization failed\n");
2086                 return ret;
2087         }
2088         /*resource DB*/
2089         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_RESOURCE_INFO);
2090         if (ret == -1) {
2091                 _LOGD("package resource info DB initialization failed\n");
2092                 return ret;
2093         }
2094         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_RESOURCE_DATA);
2095         if (ret == -1) {
2096                 _LOGD("package resource data DB initialization failed\n");
2097                 return ret;
2098         }
2099
2100         if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
2101                 _LOGD("Failed to change cert db permission\n");
2102         }
2103         if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
2104                 _LOGD("Failed to change parser db permission\n");
2105         }
2106
2107         return 0;
2108 }
2109
2110 static int __parserdb_change_perm(const char *db_file, uid_t uid)
2111 {
2112         char buf[BUFSIZE];
2113         char journal_file[BUFSIZE];
2114         char *files[3];
2115         int ret, i;
2116         struct passwd *userinfo = NULL;
2117         files[0] = (char *)db_file;
2118         files[1] = journal_file;
2119         files[2] = NULL;
2120
2121         if (db_file == NULL)
2122                 return -1;
2123
2124         if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
2125                 return 0;
2126         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2127         if (uid == OWNER_ROOT)
2128                 uid = GLOBAL_USER;
2129         userinfo = getpwuid(uid);
2130         if (!userinfo) {
2131                 _LOGE("FAIL: user %d doesn't exist", uid);
2132                 return -1;
2133         }
2134         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2135
2136         for (i = 0; files[i]; i++) {
2137                 ret = chown(files[i], uid, userinfo->pw_gid);
2138                 if (ret == -1) {
2139                         if (strerror_r(errno, buf, sizeof(buf)))
2140                                 strcpy(buf, "");
2141                         _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
2142                                         userinfo->pw_gid, buf);
2143                         return -1;
2144                 }
2145
2146                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
2147                 if (ret == -1) {
2148                         if (strerror_r(errno, buf, sizeof(buf)))
2149                                 strcpy(buf, "");
2150                         _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
2151                         return -1;
2152                 }
2153                 SET_SMACK_LABEL(files[i]);
2154         }
2155         return 0;
2156 }
2157
2158 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
2159 {
2160         int ret;
2161
2162         if (getuid() != OWNER_ROOT) {
2163                 _LOGE("Only root user is allowed");
2164                 return -1;
2165         }
2166
2167         if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
2168                 _LOGE("Manifest db for user %d is already exists", uid);
2169                 return -1;
2170         }
2171
2172         if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
2173                 _LOGE("Cert db for user %d is already exists", uid);
2174                 return -1;
2175         }
2176
2177         ret = pkgmgr_parser_check_and_create_db(uid);
2178         if (ret < 0)
2179                 return -1;
2180         ret = pkgmgr_parser_initialize_db(uid);
2181         if (ret < 0) {
2182                 pkgmgr_parser_close_db();
2183                 return -1;
2184         }
2185         pkgmgr_parser_close_db();
2186
2187         return 0;
2188 }
2189
2190 API int pkgmgr_parser_check_and_create_db(uid_t uid)
2191 {
2192         int ret = -1;
2193         /*Manifest DB*/
2194         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
2195         if (ret) {
2196                 _LOGD("Manifest DB creation Failed\n");
2197                 return -1;
2198         }
2199
2200         /*Cert DB*/
2201         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
2202         if (ret) {
2203                 _LOGD("Cert DB creation Failed\n");
2204                 return -1;
2205         }
2206         return 0;
2207 }
2208
2209 void pkgmgr_parser_close_db(void)
2210 {
2211         sqlite3_close(pkgmgr_parser_db);
2212         sqlite3_close(pkgmgr_cert_db);
2213 }
2214
2215
2216 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
2217 {
2218         _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
2219         if (mfx == NULL) {
2220                 _LOGD("manifest pointer is NULL\n");
2221                 return -1;
2222         }
2223         int ret = 0;
2224         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2225         if (ret == -1) {
2226                 _LOGD("Failed to open DB\n");
2227                 return ret;
2228         }
2229         ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
2230         if (ret == -1)
2231                 goto err;
2232         /*Begin transaction*/
2233         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2234         if (ret != SQLITE_OK) {
2235                 _LOGD("Failed to begin transaction\n");
2236                 ret = -1;
2237                 goto err;
2238         }
2239         _LOGD("Transaction Begin\n");
2240         ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
2241         if (ret == -1) {
2242                 _LOGD("Insert into DB failed. Rollback now\n");
2243                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2244                 goto err;
2245         }
2246         /*Commit transaction*/
2247         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2248         if (ret != SQLITE_OK) {
2249                 _LOGD("Failed to commit transaction. Rollback now\n");
2250                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2251                 ret = -1;
2252                 goto err;
2253         }
2254         _LOGD("Transaction Commit and End\n");
2255 err:
2256         pkgmgr_parser_close_db();
2257         return ret;
2258 }
2259
2260 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2261 {
2262         _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
2263         if (mfx == NULL) {
2264                 _LOGD("manifest pointer is NULL\n");
2265                 return -1;
2266         }
2267         int ret = 0;
2268         ret = pkgmgr_parser_check_and_create_db(uid);
2269         if (ret == -1) {
2270                 _LOGD("Failed to open DB\n");
2271                 return ret;
2272         }
2273         ret = pkgmgr_parser_initialize_db(uid);
2274         if (ret == -1)
2275                 goto err;
2276         /*Begin transaction*/
2277         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2278         if (ret != SQLITE_OK) {
2279                 _LOGD("Failed to begin transaction\n");
2280                 ret = -1;
2281                 goto err;
2282         }
2283         _LOGD("Transaction Begin\n");
2284         ret = __insert_manifest_info_in_db(mfx, uid);
2285         if (ret == -1) {
2286                 _LOGD("Insert into DB failed. Rollback now\n");
2287                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2288                 goto err;
2289         }
2290         /*Commit transaction*/
2291         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2292         if (ret != SQLITE_OK) {
2293                 _LOGD("Failed to commit transaction. Rollback now\n");
2294                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2295                 ret = -1;
2296                 goto err;
2297         }
2298         _LOGD("Transaction Commit and End\n");
2299 err:
2300         pkgmgr_parser_close_db();
2301         return ret;
2302 }
2303
2304 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2305 {
2306         if (mfx == NULL) {
2307                 _LOGD("manifest pointer is NULL\n");
2308                 return -1;
2309         }
2310         int ret = 0;
2311         ret = pkgmgr_parser_check_and_create_db(uid);
2312         if (ret == -1) {
2313                 _LOGD("Failed to open DB\n");
2314                 return ret;
2315         }
2316         ret = pkgmgr_parser_initialize_db(uid);
2317         if (ret == -1)
2318                 goto err;
2319         /*Preserve guest mode visibility*/
2320         __preserve_guestmode_visibility_value( mfx);
2321         /*Begin transaction*/
2322         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2323         if (ret != SQLITE_OK) {
2324                 _LOGD("Failed to begin transaction\n");
2325                 ret = -1;
2326                 goto err;
2327         }
2328         _LOGD("Transaction Begin\n");
2329         ret = __delete_manifest_info_from_db(mfx, uid);
2330         if (ret == -1) {
2331                 _LOGD("Delete from DB failed. Rollback now\n");
2332                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2333                 goto err;
2334         }
2335         ret = __insert_manifest_info_in_db(mfx, uid);
2336         if (ret == -1) {
2337                 _LOGD("Insert into DB failed. Rollback now\n");
2338                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2339                 goto err;
2340         }
2341
2342         /*Commit transaction*/
2343         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2344         if (ret != SQLITE_OK) {
2345                 _LOGD("Failed to commit transaction. Rollback now\n");
2346                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2347                 ret = -1;
2348                 goto err;
2349         }
2350         _LOGD("Transaction Commit and End\n");
2351 err:
2352         pkgmgr_parser_close_db();
2353         return ret;
2354 }
2355
2356 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2357 {
2358         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
2359 }
2360
2361 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
2362 {
2363         if (mfx == NULL) {
2364                 _LOGD("manifest pointer is NULL\n");
2365                 return -1;
2366         }
2367         int ret = 0;
2368         ret = pkgmgr_parser_check_and_create_db(uid);
2369         if (ret == -1) {
2370                 _LOGD("Failed to open DB\n");
2371                 return ret;
2372         }
2373         /*Begin transaction*/
2374         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2375         if (ret != SQLITE_OK) {
2376                 _LOGD("Failed to begin transaction\n");
2377                 ret = -1;
2378                 goto err;
2379         }
2380         _LOGD("Transaction Begin\n");
2381         ret = __delete_manifest_info_from_db(mfx, uid);
2382         if (ret == -1) {
2383                 _LOGD("Delete from DB failed. Rollback now\n");
2384                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2385                 goto err;
2386         }
2387         /*Commit transaction*/
2388         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2389         if (ret != SQLITE_OK) {
2390                 _LOGD("Failed to commit transaction, Rollback now\n");
2391                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2392                 ret = -1;
2393                 goto err;
2394         }
2395         _LOGD("Transaction Commit and End\n");
2396 err:
2397         pkgmgr_parser_close_db();
2398         return ret;
2399 }
2400
2401 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2402 {
2403         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
2404 }
2405
2406 API int pkgmgr_parser_update_preload_info_in_db()
2407 {
2408         int ret = 0;
2409         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2410         if (ret == -1) {
2411                 _LOGD("Failed to open DB\n");
2412                 return ret;
2413         }
2414         /*Begin transaction*/
2415         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2416         if (ret != SQLITE_OK) {
2417                 _LOGD("Failed to begin transaction\n");
2418                 ret = -1;
2419                 goto err;
2420         }
2421         _LOGD("Transaction Begin\n");
2422         ret = __update_preload_condition_in_db();
2423         if (ret == -1) {
2424                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2425                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2426                 goto err;
2427         }
2428         /*Commit transaction*/
2429         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2430         if (ret != SQLITE_OK) {
2431                 _LOGD("Failed to commit transaction, Rollback now\n");
2432                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2433                 ret = -1;
2434                 goto err;
2435         }
2436         _LOGD("Transaction Commit and End\n");
2437 err:
2438         pkgmgr_parser_close_db();
2439         return ret;
2440 }
2441
2442 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
2443 {
2444         int ret = 0;
2445         ret = pkgmgr_parser_check_and_create_db(uid);
2446         if (ret == -1) {
2447                 _LOGD("Failed to open DB\n");
2448                 return ret;
2449         }
2450         /*Begin transaction*/
2451         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2452         if (ret != SQLITE_OK) {
2453                 _LOGD("Failed to begin transaction\n");
2454                 ret = -1;
2455                 goto err;
2456         }
2457         _LOGD("Transaction Begin\n");
2458         ret = __update_preload_condition_in_db();
2459         if (ret == -1) {
2460                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2461                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2462                 goto err;
2463         }
2464         /*Commit transaction*/
2465         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2466         if (ret != SQLITE_OK) {
2467                 _LOGD("Failed to commit transaction, Rollback now\n");
2468                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2469                 ret = -1;
2470                 goto err;
2471         }
2472         _LOGD("Transaction Commit and End\n");
2473 err:
2474         pkgmgr_parser_close_db();
2475         return ret;
2476 }