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