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