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