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