Unify uiapplication_x and serviceapplication_x
[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 <grp.h>
31 #include <pwd.h>
32
33 #include <db-util.h>
34 #include <glib.h>
35 /* For multi-user support */
36 #include <tzplatform_config.h>
37
38 #include "pkgmgr-info.h"
39 #include "pkgmgrinfo_basic.h"
40 #include "pkgmgrinfo_debug.h"
41 #include "pkgmgr_parser_internal.h"
42 #include "pkgmgr_parser_db.h"
43
44 #ifdef LOG_TAG
45 #undef LOG_TAG
46 #endif
47 #define LOG_TAG "PKGMGR_PARSER"
48
49 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
50 #define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
51 #define MAX_QUERY_LEN           4096
52 #define BUFSIZE 4096
53 #define OWNER_ROOT 0
54
55 #define DB_LABEL "User::Home"
56 #define SET_SMACK_LABEL(x) \
57 do { \
58         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
59                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
60         else \
61                 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
62 } while (0)
63
64 sqlite3 *pkgmgr_parser_db;
65 sqlite3 *pkgmgr_cert_db;
66
67
68 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
69                                                 "(package text primary key not null, " \
70                                                 "package_type text DEFAULT 'rpm', " \
71                                                 "package_version text, " \
72                                                 "package_api_version text, " \
73                                                 "install_location text, " \
74                                                 "package_size text, " \
75                                                 "package_removable text DEFAULT 'true', " \
76                                                 "package_preload text DEFAULT 'false', " \
77                                                 "package_readonly text DEFAULT 'false', " \
78                                                 "package_update text DEFAULT 'false', " \
79                                                 "package_appsetting text DEFAULT 'false', " \
80                                                 "package_nodisplay text DEFAULT 'false', " \
81                                                 "package_system text DEFAULT 'false', " \
82                                                 "author_name text, " \
83                                                 "author_email text, " \
84                                                 "author_href text," \
85                                                 "installed_time text," \
86                                                 "installed_storage text," \
87                                                 "storeclient_id text," \
88                                                 "mainapp_id text," \
89                                                 "package_url text," \
90                                                 "root_path text," \
91                                                 "csc_path text," \
92                                                 "package_support_disable text DEFAULT 'false')"
93
94 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
95                                                 "(package text not null, " \
96                                                 "package_locale text DEFAULT 'No Locale', " \
97                                                 "package_label text, " \
98                                                 "package_icon text, " \
99                                                 "package_description text, " \
100                                                 "package_license text, " \
101                                                 "package_author, " \
102                                                 "PRIMARY KEY(package, package_locale), " \
103                                                 "FOREIGN KEY(package) " \
104                                                 "REFERENCES package_info(package) " \
105                                                 "ON DELETE CASCADE)"
106
107 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
108                                                 "(package text not null, " \
109                                                 "privilege text not null, " \
110                                                 "PRIMARY KEY(package, privilege) " \
111                                                 "FOREIGN KEY(package) " \
112                                                 "REFERENCES package_info(package) " \
113                                                 "ON DELETE CASCADE)"
114
115 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
116                                                 "(app_id text primary key not null, " \
117                                                 "app_component text, " \
118                                                 "app_exec text, " \
119                                                 "app_nodisplay text DEFAULT 'false', " \
120                                                 "app_type text, " \
121                                                 "app_onboot text DEFAULT 'false', " \
122                                                 "app_multiple text DEFAULT 'false', " \
123                                                 "app_autorestart text DEFAULT 'false', " \
124                                                 "app_taskmanage text DEFAULT 'false', " \
125                                                 "app_enabled text DEFAULT 'true', " \
126                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
127                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
128                                                 "app_mainapp text, " \
129                                                 "app_recentimage text, " \
130                                                 "app_launchcondition text, " \
131                                                 "app_indicatordisplay text DEFAULT 'true', " \
132                                                 "app_portraitimg text, " \
133                                                 "app_landscapeimg text, " \
134                                                 "app_guestmodevisibility text DEFAULT 'true', " \
135                                                 "app_permissiontype text DEFAULT 'normal', " \
136                                                 "app_preload text DEFAULT 'false', " \
137                                                 "app_submode text DEFAULT 'false', " \
138                                                 "app_submode_mainid text, " \
139                                                 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
140                                                 "app_ui_gadget text DEFAULT 'false', " \
141                                                 "app_support_disable text DEFAULT 'false', " \
142                                                 "component_type text, " \
143                                                 "package text not null, " \
144                                                 "FOREIGN KEY(package) " \
145                                                 "REFERENCES package_info(package) " \
146                                                 "ON DELETE CASCADE)"
147
148 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
149                                                 "(app_id text not null, " \
150                                                 "app_locale text DEFAULT 'No Locale', " \
151                                                 "app_label text, " \
152                                                 "app_icon text, " \
153                                                 "PRIMARY KEY(app_id,app_locale) " \
154                                                 "FOREIGN KEY(app_id) " \
155                                                 "REFERENCES package_app_info(app_id) " \
156                                                 "ON DELETE CASCADE)"
157
158 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
159                                                 "(app_id text not null, " \
160                                                 "app_icon text, " \
161                                                 "app_icon_section text, " \
162                                                 "app_icon_resolution text, " \
163                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
164                                                 "FOREIGN KEY(app_id) " \
165                                                 "REFERENCES package_app_info(app_id) " \
166                                                 "ON DELETE CASCADE)"
167
168 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
169                                                 "(app_id text not null, " \
170                                                 "app_locale text DEFAULT 'No Locale', " \
171                                                 "app_image_section text, " \
172                                                 "app_image text, " \
173                                                 "PRIMARY KEY(app_id,app_image_section) " \
174                                                 "FOREIGN KEY(app_id) " \
175                                                 "REFERENCES package_app_info(app_id) " \
176                                                 "ON DELETE CASCADE)"
177
178 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
179                                                 "(app_id text not null, " \
180                                                 "app_control text not null, " \
181                                                 "PRIMARY KEY(app_id,app_control) " \
182                                                 "FOREIGN KEY(app_id) " \
183                                                 "REFERENCES package_app_info(app_id) " \
184                                                 "ON DELETE CASCADE)"
185
186 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
187                                                 "(app_id text not null, " \
188                                                 "operation text not null, " \
189                                                 "uri_scheme text, " \
190                                                 "mime_type text, " \
191                                                 "subapp_name text, " \
192                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
193                                                 "FOREIGN KEY(app_id) " \
194                                                 "REFERENCES package_app_info(app_id) " \
195                                                 "ON DELETE CASCADE)"
196
197 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
198                                                 "(app_id text not null, " \
199                                                 "category text not null, " \
200                                                 "PRIMARY KEY(app_id,category) " \
201                                                 "FOREIGN KEY(app_id) " \
202                                                 "REFERENCES package_app_info(app_id) " \
203                                                 "ON DELETE CASCADE)"
204
205 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
206                                                 "(app_id text not null, " \
207                                                 "md_key text not null, " \
208                                                 "md_value text not null, " \
209                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
210                                                 "FOREIGN KEY(app_id) " \
211                                                 "REFERENCES package_app_info(app_id) " \
212                                                 "ON DELETE CASCADE)"
213
214 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
215                                                 "(app_id text not null, " \
216                                                 "pm_type text not null, " \
217                                                 "pm_value text not null, " \
218                                                 "PRIMARY KEY(app_id, pm_type, pm_value) " \
219                                                 "FOREIGN KEY(app_id) " \
220                                                 "REFERENCES package_app_info(app_id) " \
221                                                 "ON DELETE CASCADE)"
222
223 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
224                                                 "(app_id text not null, " \
225                                                 "data_share_path text not null, " \
226                                                 "data_share_allowed text not null, " \
227                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
228                                                 "FOREIGN KEY(app_id) " \
229                                                 "REFERENCES package_app_info(app_id) " \
230                                                 "ON DELETE CASCADE)"
231
232 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
233                                                 "(app_id text not null, " \
234                                                 "data_share_request text not null, " \
235                                                 "PRIMARY KEY(app_id,data_share_request) " \
236                                                 "FOREIGN KEY(app_id) " \
237                                                 "REFERENCES package_app_info(app_id) " \
238                                                 "ON DELETE CASCADE)"
239
240 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
241                                                 "(cert_info text not null, " \
242                                                 "cert_id integer, " \
243                                                 "cert_ref_count integer, " \
244                                                 "PRIMARY KEY(cert_id)) "
245
246 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
247                                                 "(package text not null, " \
248                                                 "author_root_cert integer, " \
249                                                 "author_im_cert integer, " \
250                                                 "author_signer_cert integer, " \
251                                                 "dist_root_cert integer, " \
252                                                 "dist_im_cert integer, " \
253                                                 "dist_signer_cert integer, " \
254                                                 "dist2_root_cert integer, " \
255                                                 "dist2_im_cert integer, " \
256                                                 "dist2_signer_cert integer, " \
257                                                 "PRIMARY KEY(package)) "
258
259 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
260                                                 "(app_id text not null, " \
261                                                 "providerid text not null, " \
262                                                 "access text not null, " \
263                                                 "type text not null, " \
264                                                 "PRIMARY KEY(app_id, providerid, access, type) " \
265                                                 "FOREIGN KEY(app_id) " \
266                                                 "REFERENCES package_app_info(app_id) " \
267                                                 "ON DELETE CASCADE)"
268
269 static int __insert_application_info(manifest_x *mfx);
270 static int __insert_application_appsvc_info(manifest_x *mfx);
271 static int __insert_application_appcategory_info(manifest_x *mfx);
272 static int __insert_application_appcontrol_info(manifest_x *mfx);
273 static int __insert_application_appmetadata_info(manifest_x *mfx);
274 static int __insert_application_share_allowed_info(manifest_x *mfx);
275 static int __insert_application_share_request_info(manifest_x *mfx);
276 static int __insert_application_datacontrol_info(manifest_x *mfx);
277 static void __insert_application_locale_info(gpointer data, gpointer userdata);
278 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
279 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
280 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
281 static int __delete_subpkg_info_from_db(char *appid);
282 static int __delete_appinfo_from_db(char *db_table, const char *appid);
283 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
284 static int __exec_query(char *query);
285 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
286                 char **label, char **license, char **icon, char **description, char **author);
287 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
288 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath);
289 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
290 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
291 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
292 static int __parserdb_change_perm(const char *db_file, uid_t uid);
293
294 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
295 {
296         if (coltxt[0])
297                 __delete_subpkg_info_from_db(coltxt[0]);
298
299         return 0;
300 }
301
302 static const char *__get_str(const char *str)
303 {
304         if (str == NULL)
305         {
306                 return PKGMGR_PARSER_EMPTY_STR;
307         }
308
309         return str;
310 }
311
312 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
313 {
314         int ret = -1;
315         sqlite3 *handle;
316
317         ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
318         if (ret != SQLITE_OK) {
319                 _LOGD("connect db [%s] failed!\n", db_path);
320                 return -1;
321         }
322         *db_handle = handle;
323
324         return 0;
325 }
326
327 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
328 {
329         manifest_x *mfx = (manifest_x *)data;
330         int i = 0;
331         char *appid = NULL;
332         char *status = NULL;
333         application_x *app = NULL;
334         for(i = 0; i < ncols; i++)
335         {
336                 app = mfx->application;
337                 if (strcmp(colname[i], "app_id") == 0) {
338                         if (coltxt[i])
339                                 appid = strdup(coltxt[i]);
340                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
341                         if (coltxt[i])
342                                 status = strdup(coltxt[i]);
343                 }
344         }
345         if (appid == NULL) {
346                 _LOGD("app id is NULL\n");
347                 return -1;
348         }
349         /*update guest mode visibility*/
350         for (; app != NULL; app = app->next) {
351                 if (strcmp(app->appid, appid) == 0) {
352                         free((void *)app->guestmode_visibility);
353                         app->guestmode_visibility = strdup(status);
354                         break;
355                 }
356         }
357         if (appid) {
358                 free(appid);
359                 appid = NULL;
360         }
361         if (status) {
362                 free(status);
363                 status = NULL;
364         }
365
366         return 0;
367 }
368
369 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
370 {
371         char *error_message = NULL;
372         char query[MAX_QUERY_LEN] = {'\0'};
373         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
374         if (SQLITE_OK !=
375             sqlite3_exec(pkgmgr_parser_db, query,
376                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
377                 _LOGD("Don't execute query = %s error message = %s\n",
378                        query, error_message);
379                 sqlite3_free(error_message);
380         }
381         return;
382 }
383
384 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
385 {
386         char *error_message = NULL;
387         if (SQLITE_OK !=
388             sqlite3_exec(db_handle, db_query,
389                          NULL, NULL, &error_message)) {
390                 _LOGD("Don't execute query = %s error message = %s\n",
391                        db_query, error_message);
392                 sqlite3_free(error_message);
393                 return -1;
394         }
395         sqlite3_free(error_message);
396         return 0;
397 }
398
399 static int __exec_query(char *query)
400 {
401         char *error_message = NULL;
402         if (SQLITE_OK !=
403             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
404                 _LOGD("Don't execute query = %s error message = %s\n", query,
405                        error_message);
406                 sqlite3_free(error_message);
407                 return -1;
408         }
409         sqlite3_free(error_message);
410         return 0;
411 }
412
413 static int __exec_query_no_msg(char *query)
414 {
415         char *error_message = NULL;
416         if (SQLITE_OK !=
417             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
418                 sqlite3_free(error_message);
419                 return -1;
420         }
421         sqlite3_free(error_message);
422         return 0;
423 }
424
425 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
426 {
427
428         while(lbl != NULL)
429         {
430                 if (lbl->lang)
431                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
432                 lbl = lbl->next;
433         }
434         while(lcn != NULL)
435         {
436                 if (lcn->lang)
437                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
438                 lcn = lcn->next;
439         }
440         while(icn != NULL)
441         {
442                 if (icn->lang)
443                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
444                 icn = icn->next;
445         }
446         while(dcn != NULL)
447         {
448                 if (dcn->lang)
449                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
450                 dcn = dcn->next;
451         }
452         while(ath != NULL)
453         {
454                 if (ath->lang)
455                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
456                 ath = ath->next;
457         }
458         return locale;
459
460 }
461
462 static GList *__create_icon_list(GList *locale, icon_x *icn)
463 {
464         while(icn != NULL)
465         {
466                 if (icn->section)
467                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
468                 icn = icn->next;
469         }
470         return locale;
471 }
472
473 static GList *__create_image_list(GList *locale, image_x *image)
474 {
475         while(image != NULL)
476         {
477                 if (image->section)
478                         locale = g_list_insert_sorted_with_data(locale, (gpointer)image->section, __comparefunc, NULL);
479                 image = image->next;
480         }
481         return locale;
482 }
483
484 static void __trimfunc(GList* trim_list)
485 {
486         char *trim_data = NULL;
487         char *prev = NULL;
488
489         GList *list = NULL;
490         list = g_list_first(trim_list);
491
492         while (list) {
493                 trim_data = (char *)list->data;
494                 if (trim_data) {
495                         if (prev) {
496                                 if (strcmp(trim_data, prev) == 0) {
497                                         trim_list = g_list_remove(trim_list, trim_data);
498                                         list = g_list_first(trim_list);
499                                         prev = NULL;
500                                         continue;
501                                 } else
502                                         prev = trim_data;
503                         }
504                         else
505                                 prev = trim_data;
506                 }
507                 list = g_list_next(list);
508         }
509 }
510
511 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
512 {
513         if (a == NULL || b == NULL)
514                 return 0;
515         if (strcmp((char*)a, (char*)b) == 0)
516                 return 0;
517         if (strcmp((char*)a, (char*)b) < 0)
518                 return -1;
519         if (strcmp((char*)a, (char*)b) > 0)
520                 return 1;
521         return 0;
522 }
523
524 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
525                 char **label, char **license, char **icon, char **description, char **author)
526 {
527         while(lbl != NULL)
528         {
529                 if (lbl->lang) {
530                         if (strcmp(lbl->lang, (char *)data) == 0) {
531                                 *label = (char*)lbl->text;
532                                 break;
533                         }
534                 }
535                 lbl = lbl->next;
536         }
537         while(lcn != NULL)
538         {
539                 if (lcn->lang) {
540                         if (strcmp(lcn->lang, (char *)data) == 0) {
541                                 *license = (char*)lcn->text;
542                                 break;
543                         }
544                 }
545                 lcn = lcn->next;
546         }
547         while(icn != NULL)
548         {
549                 if (icn->lang) {
550                         if (strcmp(icn->lang, (char *)data) == 0) {
551                                 *icon = (char*)icn->text;
552                                 break;
553                         }
554                 }
555                 icn = icn->next;
556         }
557         while(dcn != NULL)
558         {
559                 if (dcn->lang) {
560                         if (strcmp(dcn->lang, (char *)data) == 0) {
561                                 *description = (char*)dcn->text;
562                                 break;
563                         }
564                 }
565                 dcn = dcn->next;
566         }
567         while(ath != NULL)
568         {
569                 if (ath->lang) {
570                         if (strcmp(ath->lang, (char *)data) == 0) {
571                                 *author = (char*)ath->text;
572                                 break;
573                         }
574                 }
575                 ath = ath->next;
576         }
577
578 }
579
580 static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **resolution)
581 {
582         while(icn != NULL)
583         {
584                 if (icn->section) {
585                         if (strcmp(icn->section, (char *)data) == 0) {
586                                 *icon = (char*)icn->text;
587                                 *resolution = (char*)icn->resolution;
588                                 break;
589                         }
590                 }
591                 icn = icn->next;
592         }
593 }
594
595 static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
596 {
597         while(image != NULL)
598         {
599                 if (image->section) {
600                         if (strcmp(image->section, (char *)data) == 0) {
601                                 *lang = (char*)image->lang;
602                                 *img = (char*)image->text;
603                                 break;
604                         }
605                 }
606                 image = image->next;
607         }
608 }
609
610 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
611 {
612         int ret = -1;
613         char *label = NULL;
614         char *icon = NULL;
615         char *description = NULL;
616         char *license = NULL;
617         char *author = NULL;
618         char query[MAX_QUERY_LEN] = {'\0'};
619
620         manifest_x *mfx = (manifest_x *)userdata;
621         label_x *lbl = mfx->label;
622         license_x *lcn = mfx->license;
623         icon_x *icn = mfx->icon;
624         description_x *dcn = mfx->description;
625         author_x *ath = mfx->author;
626
627         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
628         if (!label && !description && !icon && !license && !author)
629                 return;
630
631         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
632                 "package_label, package_icon, package_description, package_license, package_author) values " \
633                 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
634                 mfx->package,
635                 (char*)data,
636                 label,
637                 icon,
638                 __get_str(description),
639                 __get_str(license),
640                 __get_str(author));
641
642         ret = __exec_query(query);
643         if (ret == -1)
644                 _LOGD("Package Localized Info DB Insert failed\n");
645 }
646
647 static void __insert_application_locale_info(gpointer data, gpointer userdata)
648 {
649         int ret = -1;
650         char *label = NULL;
651         char *icon = NULL;
652         char query[MAX_QUERY_LEN] = {'\0'};
653
654         application_x *app = (application_x*)userdata;
655         label_x *lbl = app->label;
656         icon_x *icn = app->icon;
657
658         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
659         if (!label && !icon)
660                 return;
661         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
662                 "app_label, app_icon) values " \
663                 "('%q', '%q', '%q', '%q')", app->appid, (char*)data,
664                 label, icon);
665         ret = __exec_query(query);
666         if (ret == -1)
667                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
668
669         /*insert ui app locale info to pkg locale to get mainapp data */
670         if (strcasecmp(app->mainapp, "true")==0) {
671                 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
672                         "package_label, package_icon, package_description, package_license, package_author) values " \
673                         "('%q', '%q', '%q', '%q', '%q', '%q', '%q')",
674                         app->package,
675                         (char*)data,
676                         label,
677                         icon,
678                         PKGMGR_PARSER_EMPTY_STR,
679                         PKGMGR_PARSER_EMPTY_STR,
680                         PKGMGR_PARSER_EMPTY_STR);
681
682                 ret = __exec_query_no_msg(query);
683
684                 if (icon != NULL) {
685                         sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
686                                 "where package='%s' and package_locale='%s'", icon, app->package, (char*)data);
687                         ret = __exec_query_no_msg(query);
688                 }
689         }
690 }
691
692 static void __insert_application_icon_section_info(gpointer data, gpointer userdata)
693 {
694         int ret = -1;
695         char *icon = NULL;
696         char *resolution = NULL;
697         char query[MAX_QUERY_LEN] = {'\0'};
698
699         application_x *app = (application_x*)userdata;
700         icon_x *icn = app->icon;
701
702         __extract_icon_data(data, icn, &icon, &resolution);
703         if (!icon && !resolution)
704                 return;
705         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
706                 "app_icon, app_icon_section, app_icon_resolution) values " \
707                 "('%q', '%q', '%q', '%q')", app->appid,
708                 icon, (char*)data, resolution);
709
710         ret = __exec_query(query);
711         if (ret == -1)
712                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
713
714 }
715
716 static void __insert_application_image_info(gpointer data, gpointer userdata)
717 {
718         int ret = -1;
719         char *lang = NULL;
720         char *img = NULL;
721         char query[MAX_QUERY_LEN] = {'\0'};
722
723         application_x *app = (application_x*)userdata;
724         image_x *image = app->image;
725
726         __extract_image_data(data, image, &lang, &img);
727         if (!lang && !img)
728                 return;
729         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
730                 "app_image_section, app_image) values " \
731                 "('%q', '%q', '%q', '%q')", app->appid, lang, (char*)data, img);
732
733         ret = __exec_query(query);
734         if (ret == -1)
735                 _LOGD("Package UiApp image Info DB Insert failed\n");
736
737 }
738
739
740 static int __insert_mainapp_info(manifest_x *mfx)
741 {
742         application_x *app = mfx->application;
743         int ret = -1;
744         char query[MAX_QUERY_LEN] = {'\0'};
745         while (app != NULL) {
746                 snprintf(query, MAX_QUERY_LEN,
747                         "update package_app_info set app_mainapp='%s' where app_id='%s'", app->mainapp, app->appid);
748
749                 ret = __exec_query(query);
750                 if (ret == -1) {
751                         _LOGD("Package App Info DB Insert Failed\n");
752                         return -1;
753                 }
754                 if (strcasecmp(app->mainapp, "True")==0)
755                         mfx->mainapp_id = strdup(app->appid);
756
757                 app = app->next;
758                 memset(query, '\0', MAX_QUERY_LEN);
759         }
760
761         if (mfx->mainapp_id == NULL) {
762                 if (mfx->application && mfx->application->appid) {
763                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->application->appid);
764                 } else {
765                         _LOGD("Not valid appid\n");
766                         return -1;
767                 }
768
769                 ret = __exec_query(query);
770                 if (ret == -1) {
771                         _LOGD("Package UiApp Info DB Insert Failed\n");
772                         return -1;
773                 }
774
775                 free((void *)mfx->application->mainapp);
776                 mfx->application->mainapp= strdup("true");
777                 mfx->mainapp_id = strdup(mfx->application->appid);
778         }
779
780         memset(query, '\0', MAX_QUERY_LEN);
781         snprintf(query, MAX_QUERY_LEN,
782                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
783         ret = __exec_query(query);
784         if (ret == -1) {
785                 _LOGD("Package Info DB update Failed\n");
786                 return -1;
787         }
788
789         return 0;
790 }
791 /* _PRODUCT_LAUNCHING_ENHANCED_
792 *  app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
793 */
794 static int __insert_application_info(manifest_x *mfx)
795 {
796         application_x *app = mfx->application;
797         int ret = -1;
798         char query[MAX_QUERY_LEN] = {'\0'};
799         while (app != NULL) {
800                 snprintf(query, MAX_QUERY_LEN,
801                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
802                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
803                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
804                         "app_preload, app_submode, app_submode_mainid, app_launch_mode, app_ui_gadget, app_support_disable, component_type, package) " \
805                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
806                          app->appid,
807                          app->component_type,
808                          app->exec,
809                          app->nodisplay,
810                          app->type,
811                          app->onboot,
812                          app->multiple,
813                          app->autorestart,
814                          app->taskmanage,
815                          app->enabled,
816                          app->hwacceleration,
817                          app->screenreader,
818                          app->mainapp,
819                          __get_str(app->recentimage),
820                          app->launchcondition,
821                          app->indicatordisplay,
822                          __get_str(app->portraitimg),
823                          __get_str(app->landscapeimg),
824                          app->guestmode_visibility,
825                          app->permission_type,
826                          mfx->preload,
827                          app->submode,
828                          __get_str(app->submode_mainid),
829                          app->launch_mode,
830                          app->ui_gadget,
831                          mfx->support_disable,
832                          app->component_type,
833                          mfx->package);
834
835                 ret = __exec_query(query);
836                 if (ret == -1) {
837                         _LOGD("Package UiApp Info DB Insert Failed\n");
838                         return -1;
839                 }
840                 app = app->next;
841                 memset(query, '\0', MAX_QUERY_LEN);
842         }
843         return 0;
844 }
845
846 static int __insert_application_appcategory_info(manifest_x *mfx)
847 {
848         application_x *app = mfx->application;
849         category_x *ct = NULL;
850         int ret = -1;
851         char query[MAX_QUERY_LEN] = {'\0'};
852         while (app != NULL) {
853                 ct = app->category;
854                 while (ct != NULL) {
855                         snprintf(query, MAX_QUERY_LEN,
856                                 "insert into package_app_app_category(app_id, category) " \
857                                 "values('%s','%s')",\
858                                  app->appid, ct->name);
859                         ret = __exec_query(query);
860                         if (ret == -1) {
861                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
862                                 return -1;
863                         }
864                         ct = ct->next;
865                         memset(query, '\0', MAX_QUERY_LEN);
866                 }
867                 app = app->next;
868         }
869         return 0;
870 }
871
872 static int __insert_application_appmetadata_info(manifest_x *mfx)
873 {
874         application_x *app = mfx->application;
875         metadata_x *md = NULL;
876         int ret = -1;
877         char query[MAX_QUERY_LEN] = {'\0'};
878         while (app != NULL) {
879                 md = app->metadata;
880                 while (md != NULL) {
881                         if (md->key) {
882                                 snprintf(query, MAX_QUERY_LEN,
883                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
884                                         "values('%s','%s', '%s')",\
885                                          app->appid, md->key, md->value ? md->value : "");
886                                 ret = __exec_query(query);
887                                 if (ret == -1) {
888                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
889                                         return -1;
890                                 }
891                         }
892                         md = md->next;
893                         memset(query, '\0', MAX_QUERY_LEN);
894                 }
895                 app = app->next;
896         }
897         return 0;
898 }
899
900 static int __insert_application_apppermission_info(manifest_x *mfx)
901 {
902         application_x *app = mfx->application;
903         permission_x *pm = NULL;
904         int ret = -1;
905         char query[MAX_QUERY_LEN] = {'\0'};
906         while (app != NULL) {
907                 pm = app->permission;
908                 while (pm != NULL) {
909                         snprintf(query, MAX_QUERY_LEN,
910                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
911                                 "values('%s','%s', '%s')",\
912                                  app->appid, pm->type, pm->value);
913                         ret = __exec_query(query);
914                         if (ret == -1) {
915                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
916                                 return -1;
917                         }
918                         pm = pm->next;
919                         memset(query, '\0', MAX_QUERY_LEN);
920                 }
921                 app = app->next;
922         }
923         return 0;
924 }
925
926 static int __insert_application_appcontrol_info(manifest_x *mfx)
927 {
928         application_x *app = mfx->application;
929         appcontrol_x *acontrol = NULL;
930         int ret = -1;
931         char query[MAX_QUERY_LEN] = {'\0'};
932         char buf[BUFSIZE] = {'\0'};
933         while (app != NULL) {
934                 acontrol = app->appcontrol;
935                 while (acontrol != NULL) {
936                         snprintf(buf, BUFSIZE, "%s|%s|%s",\
937                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
938                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
939                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
940                         snprintf(query, MAX_QUERY_LEN,
941                                         "insert into package_app_app_control(app_id, app_control) " \
942                                         "values('%s', '%s')",\
943                                         app->appid, buf);
944                         ret = __exec_query(query);
945                         if (ret == -1) {
946                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
947                                 return -1;
948                         }
949                         memset(query, '\0', MAX_QUERY_LEN);
950                         acontrol = acontrol->next;
951                 }
952                 app = app->next;
953         }
954         return 0;
955 }
956
957 /* FIXME: should be changed to app_control */
958 static int __insert_application_appsvc_info(manifest_x *mfx)
959 {
960         application_x *app = mfx->application;
961         appsvc_x *asvc = NULL;
962         operation_x *op = NULL;
963         mime_x *mi = NULL;
964         uri_x *ui = NULL;
965         subapp_x *sub = NULL;
966         int ret = -1;
967         char query[MAX_QUERY_LEN] = {'\0'};
968         const char *operation = NULL;
969         const char *mime = NULL;
970         const char *uri = NULL;
971         const char *subapp = NULL;
972         while(app != NULL)
973         {
974                 asvc = app->appsvc;
975                 while(asvc != NULL)
976                 {
977                         op = asvc->operation;
978                         while(op != NULL)
979                         {
980                                 if (op)
981                                         operation = op->name;
982                                 mi = asvc->mime;
983
984                                 do
985                                 {
986                                         if (mi)
987                                                 mime = mi->name;
988                                         sub = asvc->subapp;
989                                         do
990                                         {
991                                                 if (sub)
992                                                         subapp = sub->name;
993                                                 ui = asvc->uri;
994                                                 do
995                                                 {
996                                                         if (ui)
997                                                                 uri = ui->name;
998                                                         snprintf(query, MAX_QUERY_LEN,
999                                                                  "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1000                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1001                                                                  app->appid,
1002                                                                  operation,
1003                                                                  __get_str(uri),
1004                                                                  __get_str(mime),
1005                                                                  __get_str(subapp));
1006
1007                                                         ret = __exec_query(query);
1008                                                         if (ret == -1) {
1009                                                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1010                                                                 return -1;
1011                                                         }
1012                                                         memset(query, '\0', MAX_QUERY_LEN);
1013                                                         if (ui)
1014                                                                 ui = ui->next;
1015                                                         uri = NULL;
1016                                                 } while(ui != NULL);
1017                                                 if (sub)
1018                                                         sub = sub->next;
1019                                                 subapp = NULL;
1020                                         }while(sub != NULL);
1021                                         if (mi)
1022                                                 mi = mi->next;
1023                                         mime = NULL;
1024                                 }while(mi != NULL);
1025                                 if (op)
1026                                         op = op->next;
1027                                 operation = NULL;
1028                         }
1029                         asvc = asvc->next;
1030                 }
1031                 app = app->next;
1032         }
1033         return 0;
1034 }
1035
1036 static int __insert_application_datacontrol_info(manifest_x *mfx)
1037 {
1038         application_x *app = mfx->application;
1039         datacontrol_x *dc = NULL;
1040         int ret = -1;
1041         char query[MAX_QUERY_LEN] = {'\0'};
1042
1043         while (app != NULL) {
1044                 dc = app->datacontrol;
1045                 while (dc != NULL) {
1046                         snprintf(query, MAX_QUERY_LEN,
1047                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1048                                         "values('%s', '%s', '%s', '%s')",\
1049                                         mfx->application->appid,
1050                                         dc->providerid,
1051                                         dc->access,
1052                                         dc->type);
1053
1054                         ret = __exec_query(query);
1055                         if (ret == -1) {
1056                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1057                                 return -1;
1058                         }
1059                         memset(query, '\0', MAX_QUERY_LEN);
1060                         dc = dc->next;
1061                 }
1062                 app = app->next;
1063         }
1064         return 0;
1065 }
1066
1067 static int __insert_application_share_request_info(manifest_x *mfx)
1068 {
1069         application_x *app = mfx->application;
1070         datashare_x *ds = NULL;
1071         request_x *rq = NULL;
1072         int ret = -1;
1073         char query[MAX_QUERY_LEN] = {'\0'};
1074         while (app != NULL) {
1075                 ds = app->datashare;
1076                 while (ds != NULL) {
1077                         rq = ds->request;
1078                         while (rq != NULL) {
1079                                 snprintf(query, MAX_QUERY_LEN,
1080                                          "insert into package_app_share_request(app_id, data_share_request) " \
1081                                         "values('%s', '%s')",\
1082                                          app->appid, rq->text);
1083                                 ret = __exec_query(query);
1084                                 if (ret == -1) {
1085                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1086                                         return -1;
1087                                 }
1088                                 memset(query, '\0', MAX_QUERY_LEN);
1089                                 rq = rq->next;
1090                         }
1091                         ds = ds->next;
1092                 }
1093                 app = app->next;
1094         }
1095         return 0;
1096 }
1097
1098 static int __insert_application_share_allowed_info(manifest_x *mfx)
1099 {
1100         application_x *app = mfx->application;
1101         datashare_x *ds = NULL;
1102         define_x *df = NULL;
1103         allowed_x *al = NULL;
1104         int ret = -1;
1105         char query[MAX_QUERY_LEN] = {'\0'};
1106         while (app != NULL) {
1107                 ds = app->datashare;
1108                 while (ds != NULL) {
1109                         df = ds->define;
1110                         while (df != NULL) {
1111                                 al = df->allowed;
1112                                 while (al != NULL) {
1113                                         snprintf(query, MAX_QUERY_LEN,
1114                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1115                                                 "values('%s', '%s', '%s')",\
1116                                                  app->appid, df->path, al->text);
1117                                         ret = __exec_query(query);
1118                                         if (ret == -1) {
1119                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1120                                                 return -1;
1121                                         }
1122                                         memset(query, '\0', MAX_QUERY_LEN);
1123                                         al = al->next;
1124                                 }
1125                                 df = df->next;
1126                         }
1127                         ds = ds->next;
1128                 }
1129                 app = app->next;
1130         }
1131         return 0;
1132 }
1133
1134 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1135 {
1136         label_x *lbl = mfx->label;
1137         license_x *lcn = mfx->license;
1138         icon_x *icn = mfx->icon;
1139         description_x *dcn = mfx->description;
1140         author_x *ath = mfx->author;
1141         application_x *app = mfx->application;
1142         application_x *app_icn = mfx->application;
1143         application_x *app_image = mfx->application;
1144         privileges_x *pvs = NULL;
1145         privilege_x *pv = NULL;
1146         char query[MAX_QUERY_LEN] = { '\0' };
1147         int ret = -1;
1148         const char *auth_name = NULL;
1149         const char *auth_email = NULL;
1150         const char *auth_href = NULL;
1151
1152         GList *pkglocale = NULL;
1153         GList *applocale = NULL;
1154         GList *appicon = NULL;
1155         GList *appimage = NULL;
1156
1157         if (ath) {
1158                 if (ath->text)
1159                         auth_name = ath->text;
1160                 if (ath->email)
1161                         auth_email = ath->email;
1162                 if (ath->href)
1163                         auth_href = ath->href;
1164         }
1165         /*Insert in the package_cert_info CERT_DB*/
1166         pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1167         ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1168         if (ret != PMINFO_R_OK) {
1169                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1170                 _LOGE("Cert Info DB create handle failed\n");
1171                 return -1;
1172         }
1173         ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1174         if (ret != PMINFO_R_OK) {
1175                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1176                 _LOGE("Cert Info DB Insert Failed\n");
1177                 return -1;
1178         }
1179
1180         /*Insert in the package_info DB*/
1181         snprintf(query, MAX_QUERY_LEN,
1182                  "insert into package_info(package, package_type, package_version, package_api_version, install_location, package_size, " \
1183                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1184                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1185                 "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')",\
1186                  mfx->package,
1187                  mfx->type,
1188                  mfx->version,
1189                  __get_str(mfx->api_version),
1190                  __get_str(mfx->installlocation),
1191                  __get_str(mfx->package_size),
1192                  mfx->removable,
1193                  mfx->preload,
1194                  mfx->readonly,
1195                  mfx->update,
1196                  mfx->appsetting,
1197                  mfx->nodisplay_setting,
1198                  mfx->system,
1199                  __get_str(auth_name),
1200                  __get_str(auth_email),
1201                  __get_str(auth_href),
1202                  mfx->installed_time,
1203                  mfx->installed_storage,
1204                  __get_str(mfx->storeclient_id),
1205                  mfx->mainapp_id,
1206                  __get_str(mfx->package_url),
1207                  mfx->root_path,
1208                  __get_str(mfx->csc_path),
1209                  mfx->support_disable);
1210
1211         ret = __exec_query(query);
1212         if (ret == -1) {
1213                 _LOGD("Package Info DB Insert Failed\n");
1214                 return -1;
1215         }
1216
1217         /*Insert in the package_privilege_info DB*/
1218         pvs = mfx->privileges;
1219         while (pvs != NULL) {
1220                 pv = pvs->privilege;
1221                 while (pv != NULL) {
1222                         memset(query, '\0', MAX_QUERY_LEN);
1223                         snprintf(query, MAX_QUERY_LEN,
1224                                 "insert into package_privilege_info(package, privilege) " \
1225                                 "values('%s','%s')",\
1226                                  mfx->package, pv->text);
1227                         ret = __exec_query(query);
1228                         if (ret == -1) {
1229                                 _LOGD("Package Privilege Info DB Insert Failed\n");
1230                                 return -1;
1231                         }
1232                         pv = pv->next;
1233                 }
1234                 pvs = pvs->next;
1235         }
1236
1237         if (app != NULL) {
1238                 ret = __insert_mainapp_info(mfx);
1239                 if (ret == -1)
1240                         return -1;
1241         }
1242
1243         /*Insert the package locale*/
1244         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1245         /*remove duplicated data in pkglocale*/
1246         __trimfunc(pkglocale);
1247
1248         /*Insert the app locale info */
1249         while (app != NULL) {
1250                 applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL);
1251                 app = app->next;
1252         }
1253         /*remove duplicated data in applocale*/
1254         __trimfunc(applocale);
1255
1256         /*Insert the app icon info */
1257         while (app_icn != NULL) {
1258                 appicon = __create_icon_list(appicon, app_icn->icon);
1259                 app_icn = app_icn->next;
1260         }
1261         /*remove duplicated data in appicon*/
1262         __trimfunc(appicon);
1263
1264         /*Insert the image info */
1265         while (app_image != NULL) {
1266                 appimage = __create_image_list(appimage, app_image->image);
1267                 app_image = app_image->next;
1268         }
1269         /*remove duplicated data in appimage*/
1270         __trimfunc(appimage);
1271
1272         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1273
1274         /*native app locale info*/
1275         app = mfx->application;
1276         while (app != NULL) {
1277                 g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app);
1278                 app = app->next;
1279         }
1280
1281         /*app icon locale info*/
1282         app_icn = mfx->application;
1283         while (app_icn != NULL) {
1284                 g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app_icn);
1285                 app_icn = app_icn->next;
1286         }
1287
1288         /*app image info*/
1289         app_image = mfx->application;
1290         while (app_image != NULL) {
1291                 g_list_foreach(appimage, __insert_application_image_info, (gpointer)app_image);
1292                 app_image = app_image->next;
1293         }
1294
1295         g_list_free(pkglocale);
1296         pkglocale = NULL;
1297         g_list_free(applocale);
1298         applocale = NULL;
1299         g_list_free(appicon);
1300         appicon = NULL;
1301         g_list_free(appimage);
1302         appimage = NULL;
1303
1304         /*Insert in the package_app_info DB*/
1305         ret = __insert_application_info(mfx);
1306         if (ret == -1)
1307                 return -1;
1308         /*Insert in the package_app_app_control DB*/
1309         ret = __insert_application_appcontrol_info(mfx);
1310         if (ret == -1)
1311                 return -1;
1312
1313         /*Insert in the package_app_app_category DB*/
1314         ret = __insert_application_appcategory_info(mfx);
1315         if (ret == -1)
1316                 return -1;
1317
1318         /*Insert in the package_app_app_metadata DB*/
1319         ret = __insert_application_appmetadata_info(mfx);
1320         if (ret == -1)
1321                 return -1;
1322
1323         /*Insert in the package_app_app_permission DB*/
1324         ret = __insert_application_apppermission_info(mfx);
1325         if (ret == -1)
1326                 return -1;
1327
1328         /*Insert in the package_app_app_svc DB*/
1329         ret = __insert_application_appsvc_info(mfx);
1330         if (ret == -1)
1331                 return -1;
1332
1333         /*Insert in the package_app_share_allowed DB*/
1334         ret = __insert_application_share_allowed_info(mfx);
1335         if (ret == -1)
1336                 return -1;
1337
1338         /*Insert in the package_app_share_request DB*/
1339         ret = __insert_application_share_request_info(mfx);
1340         if (ret == -1)
1341                 return -1;
1342
1343         /*Insert in the package_app_data_control DB*/
1344         ret = __insert_application_datacontrol_info(mfx);
1345         if (ret == -1)
1346                 return -1;
1347
1348         return 0;
1349
1350 }
1351
1352 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1353 {
1354         char query[MAX_QUERY_LEN] = { '\0' };
1355         int ret = -1;
1356         memset(query, '\0', MAX_QUERY_LEN);
1357         snprintf(query, MAX_QUERY_LEN,
1358                  "delete from %s where app_id='%s'", db_table, appid);
1359         ret = __exec_query(query);
1360         if (ret == -1) {
1361                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1362                 return -1;
1363         }
1364         return 0;
1365 }
1366
1367 static int __delete_subpkg_info_from_db(char *appid)
1368 {
1369         int ret = -1;
1370
1371         ret = __delete_appinfo_from_db("package_app_info", appid);
1372         if (ret < 0)
1373                 return ret;
1374         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1375         if (ret < 0)
1376                 return ret;
1377         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1378         if (ret < 0)
1379                 return ret;
1380         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1381         if (ret < 0)
1382                 return ret;
1383         ret = __delete_appinfo_from_db("package_app_app_svc", appid);
1384         if (ret < 0)
1385                 return ret;
1386         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1387         if (ret < 0)
1388                 return ret;
1389         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1390         if (ret < 0)
1391                 return ret;
1392         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1393         if (ret < 0)
1394                 return ret;
1395         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1396         if (ret < 0)
1397                 return ret;
1398         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1399         if (ret < 0)
1400                 return ret;
1401         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1402         if (ret < 0)
1403                 return ret;
1404         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1405         if (ret < 0)
1406                 return ret;
1407
1408         return 0;
1409 }
1410
1411 static int __delete_subpkg_from_db(manifest_x *mfx)
1412 {
1413         char query[MAX_QUERY_LEN] = { '\0' };
1414         char *error_message = NULL;
1415
1416         snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1417         if (SQLITE_OK !=
1418             sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1419                 _LOGE("Don't execute query = %s error message = %s\n", query,
1420                        error_message);
1421                 sqlite3_free(error_message);
1422                 return -1;
1423         }
1424         sqlite3_free(error_message);
1425
1426         return 0;
1427 }
1428
1429 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1430 {
1431         char query[MAX_QUERY_LEN] = { '\0' };
1432         int ret = -1;
1433         application_x *app = mfx->application;
1434         /*Delete from cert table*/
1435         if (uid != GLOBAL_USER)
1436                 ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
1437         else
1438                 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1439         if (ret) {
1440                 _LOGD("Cert Info  DB Delete Failed\n");
1441                 return -1;
1442         }
1443
1444         /*Delete from Package Info DB*/
1445         snprintf(query, MAX_QUERY_LEN,
1446                  "delete from package_info where package='%s'", mfx->package);
1447         ret = __exec_query(query);
1448         if (ret == -1) {
1449                 _LOGD("Package Info DB Delete Failed\n");
1450                 return -1;
1451         }
1452         memset(query, '\0', MAX_QUERY_LEN);
1453
1454         /*Delete from Package Localized Info*/
1455         snprintf(query, MAX_QUERY_LEN,
1456                  "delete from package_localized_info where package='%s'", mfx->package);
1457         ret = __exec_query(query);
1458         if (ret == -1) {
1459                 _LOGD("Package Localized Info DB Delete Failed\n");
1460                 return -1;
1461         }
1462
1463         /*Delete from Package Privilege Info*/
1464         snprintf(query, MAX_QUERY_LEN,
1465                  "delete from package_privilege_info where package='%s'", mfx->package);
1466         ret = __exec_query(query);
1467         if (ret == -1) {
1468                 _LOGD("Package Privilege Info DB Delete Failed\n");
1469                 return -1;
1470         }
1471
1472         while (app != NULL) {
1473                 ret = __delete_appinfo_from_db("package_app_info", app->appid);
1474                 if (ret < 0)
1475                         return ret;
1476                 ret = __delete_appinfo_from_db("package_app_localized_info", app->appid);
1477                 if (ret < 0)
1478                         return ret;
1479                 ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid);
1480                 if (ret < 0)
1481                         return ret;
1482                 ret = __delete_appinfo_from_db("package_app_image_info", app->appid);
1483                 if (ret < 0)
1484                         return ret;
1485                 ret = __delete_appinfo_from_db("package_app_app_svc", app->appid);
1486                 if (ret < 0)
1487                         return ret;
1488                 ret = __delete_appinfo_from_db("package_app_app_control", app->appid);
1489                 if (ret < 0)
1490                         return ret;
1491                 ret = __delete_appinfo_from_db("package_app_app_category", app->appid);
1492                 if (ret < 0)
1493                         return ret;
1494                 ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid);
1495                 if (ret < 0)
1496                         return ret;
1497                 ret = __delete_appinfo_from_db("package_app_app_permission", app->appid);
1498                 if (ret < 0)
1499                         return ret;
1500                 ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid);
1501                 if (ret < 0)
1502                         return ret;
1503                 ret = __delete_appinfo_from_db("package_app_share_request", app->appid);
1504                 if (ret < 0)
1505                         return ret;
1506                 ret = __delete_appinfo_from_db("package_app_data_control", app->appid);
1507                 if (ret < 0)
1508                         return ret;
1509                 app = app->next;
1510         }
1511
1512         /* if main package has sub pkg, delete sub pkg data*/
1513         __delete_subpkg_from_db(mfx);
1514
1515         return 0;
1516 }
1517
1518 static int __update_preload_condition_in_db()
1519 {
1520         int ret = -1;
1521         char query[MAX_QUERY_LEN] = {'\0'};
1522
1523         snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1524
1525         ret = __exec_query(query);
1526         if (ret == -1)
1527                 _LOGD("Package preload_condition update failed\n");
1528
1529         return ret;
1530 }
1531
1532 API int pkgmgr_parser_initialize_db(uid_t uid)
1533 {
1534         int ret = -1;
1535         /*Manifest DB*/
1536         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1537         if (ret == -1) {
1538                 _LOGD("package info DB initialization failed\n");
1539                 return ret;
1540         }
1541         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1542         if (ret == -1) {
1543                 _LOGD("package localized info DB initialization failed\n");
1544                 return ret;
1545         }
1546         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1547         if (ret == -1) {
1548                 _LOGD("package app app privilege DB initialization failed\n");
1549                 return ret;
1550         }
1551         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1552         if (ret == -1) {
1553                 _LOGD("package app info DB initialization failed\n");
1554                 return ret;
1555         }
1556         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1557         if (ret == -1) {
1558                 _LOGD("package app localized info DB initialization failed\n");
1559                 return ret;
1560         }
1561         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1562         if (ret == -1) {
1563                 _LOGD("package app icon localized info DB initialization failed\n");
1564                 return ret;
1565         }
1566         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1567         if (ret == -1) {
1568                 _LOGD("package app image info DB initialization failed\n");
1569                 return ret;
1570         }
1571         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1572         if (ret == -1) {
1573                 _LOGD("package app app control DB initialization failed\n");
1574                 return ret;
1575         }
1576         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1577         if (ret == -1) {
1578                 _LOGD("package app app category DB initialization failed\n");
1579                 return ret;
1580         }
1581         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1582         if (ret == -1) {
1583                 _LOGD("package app app category DB initialization failed\n");
1584                 return ret;
1585         }
1586         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1587         if (ret == -1) {
1588                 _LOGD("package app app permission DB initialization failed\n");
1589                 return ret;
1590         }
1591         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
1592         if (ret == -1) {
1593                 _LOGD("package app app svc DB initialization failed\n");
1594                 return ret;
1595         }
1596         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1597         if (ret == -1) {
1598                 _LOGD("package app share allowed DB initialization failed\n");
1599                 return ret;
1600         }
1601         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1602         if (ret == -1) {
1603                 _LOGD("package app share request DB initialization failed\n");
1604                 return ret;
1605         }
1606         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
1607         if (ret == -1) {
1608                 _LOGD("package app data control DB initialization failed\n");
1609                 return ret;
1610         }
1611         /*Cert DB*/
1612         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1613         if (ret == -1) {
1614                 _LOGD("package cert info DB initialization failed\n");
1615                 return ret;
1616         }
1617         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1618         if (ret == -1) {
1619                 _LOGD("package cert index info DB initialization failed\n");
1620                 return ret;
1621         }
1622         
1623         if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
1624                 _LOGD("Failed to change cert db permission\n");
1625         }
1626         if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
1627                 _LOGD("Failed to change parser db permission\n");
1628         }
1629
1630         return 0;
1631 }
1632
1633 static int __parserdb_change_perm(const char *db_file, uid_t uid)
1634 {
1635         char buf[BUFSIZE];
1636         char journal_file[BUFSIZE];
1637         char *files[3];
1638         int ret, i;
1639         struct passwd *userinfo = NULL;
1640         files[0] = (char *)db_file;
1641         files[1] = journal_file;
1642         files[2] = NULL;
1643
1644         if (db_file == NULL)
1645                 return -1;
1646
1647         if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
1648                 return 0;
1649         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1650         if (uid == OWNER_ROOT)
1651                 uid = GLOBAL_USER;
1652         userinfo = getpwuid(uid);
1653         if (!userinfo) {
1654                 _LOGE("FAIL: user %d doesn't exist", uid);
1655                 return -1;
1656         }
1657         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1658
1659         for (i = 0; files[i]; i++) {
1660                 ret = chown(files[i], uid, userinfo->pw_gid);
1661                 if (ret == -1) {
1662                         if (strerror_r(errno, buf, sizeof(buf)))
1663                                 strcpy(buf, "");
1664                         _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
1665                                         userinfo->pw_gid, buf);
1666                         return -1;
1667                 }
1668
1669                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1670                 if (ret == -1) {
1671                         if (strerror_r(errno, buf, sizeof(buf)))
1672                                 strcpy(buf, "");
1673                         _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
1674                         return -1;
1675                 }
1676                 SET_SMACK_LABEL(files[i]);
1677         }
1678         return 0;
1679 }
1680
1681 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
1682 {
1683         int ret;
1684
1685         if (getuid() != OWNER_ROOT) {
1686                 _LOGE("Only root user is allowed");
1687                 return -1;
1688         }
1689
1690         if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
1691                 _LOGE("Manifest db for user %d is already exists", uid);
1692                 return -1;
1693         }
1694
1695         if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
1696                 _LOGE("Cert db for user %d is already exists", uid);
1697                 return -1;
1698         }
1699
1700         ret = pkgmgr_parser_check_and_create_db(uid);
1701         if (ret < 0)
1702                 return -1;
1703         ret = pkgmgr_parser_initialize_db(uid);
1704         if (ret < 0) {
1705                 pkgmgr_parser_close_db();
1706                 return -1;
1707         }
1708         pkgmgr_parser_close_db();
1709
1710         return 0;
1711 }
1712
1713 API int pkgmgr_parser_check_and_create_db(uid_t uid)
1714 {
1715         int ret = -1;
1716         /*Manifest DB*/
1717         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
1718         if (ret) {
1719                 _LOGD("Manifest DB creation Failed\n");
1720                 return -1;
1721         }
1722
1723         /*Cert DB*/
1724         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
1725         if (ret) {
1726                 _LOGD("Cert DB creation Failed\n");
1727                 return -1;
1728         }
1729         return 0;
1730 }
1731
1732 void pkgmgr_parser_close_db(void)
1733 {
1734         sqlite3_close(pkgmgr_parser_db);
1735         sqlite3_close(pkgmgr_cert_db);
1736 }
1737
1738
1739 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1740 {
1741         _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
1742         if (mfx == NULL) {
1743                 _LOGD("manifest pointer is NULL\n");
1744                 return -1;
1745         }
1746         int ret = 0;
1747         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1748         if (ret == -1) {
1749                 _LOGD("Failed to open DB\n");
1750                 return ret;
1751         }
1752         ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
1753         if (ret == -1)
1754                 goto err;
1755         /*Begin transaction*/
1756         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1757         if (ret != SQLITE_OK) {
1758                 _LOGD("Failed to begin transaction\n");
1759                 ret = -1;
1760                 goto err;
1761         }
1762         _LOGD("Transaction Begin\n");
1763         ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
1764         if (ret == -1) {
1765                 _LOGD("Insert into DB failed. Rollback now\n");
1766                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1767                 goto err;
1768         }
1769         /*Commit transaction*/
1770         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1771         if (ret != SQLITE_OK) {
1772                 _LOGD("Failed to commit transaction. Rollback now\n");
1773                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1774                 ret = -1;
1775                 goto err;
1776         }
1777         _LOGD("Transaction Commit and End\n");
1778 err:
1779         pkgmgr_parser_close_db();
1780         return ret;
1781 }
1782
1783 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1784 {
1785         _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
1786         if (mfx == NULL) {
1787                 _LOGD("manifest pointer is NULL\n");
1788                 return -1;
1789         }
1790         int ret = 0;
1791         ret = pkgmgr_parser_check_and_create_db(uid);
1792         if (ret == -1) {
1793                 _LOGD("Failed to open DB\n");
1794                 return ret;
1795         }
1796         ret = pkgmgr_parser_initialize_db(uid);
1797         if (ret == -1)
1798                 goto err;
1799         /*Begin transaction*/
1800         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1801         if (ret != SQLITE_OK) {
1802                 _LOGD("Failed to begin transaction\n");
1803                 ret = -1;
1804                 goto err;
1805         }
1806         _LOGD("Transaction Begin\n");
1807         ret = __insert_manifest_info_in_db(mfx, uid);
1808         if (ret == -1) {
1809                 _LOGD("Insert into DB failed. Rollback now\n");
1810                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1811                 goto err;
1812         }
1813         /*Commit transaction*/
1814         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1815         if (ret != SQLITE_OK) {
1816                 _LOGD("Failed to commit transaction. Rollback now\n");
1817                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1818                 ret = -1;
1819                 goto err;
1820         }
1821         _LOGD("Transaction Commit and End\n");
1822 err:
1823         pkgmgr_parser_close_db();
1824         return ret;
1825 }
1826
1827 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1828 {
1829         if (mfx == NULL) {
1830                 _LOGD("manifest pointer is NULL\n");
1831                 return -1;
1832         }
1833         int ret = 0;
1834         ret = pkgmgr_parser_check_and_create_db(uid);
1835         if (ret == -1) {
1836                 _LOGD("Failed to open DB\n");
1837                 return ret;
1838         }
1839         ret = pkgmgr_parser_initialize_db(uid);
1840         if (ret == -1)
1841                 goto err;
1842         /*Preserve guest mode visibility*/
1843         __preserve_guestmode_visibility_value( mfx);
1844         /*Begin transaction*/
1845         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1846         if (ret != SQLITE_OK) {
1847                 _LOGD("Failed to begin transaction\n");
1848                 ret = -1;
1849                 goto err;
1850         }
1851         _LOGD("Transaction Begin\n");
1852         ret = __delete_manifest_info_from_db(mfx, uid);
1853         if (ret == -1) {
1854                 _LOGD("Delete from DB failed. Rollback now\n");
1855                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1856                 goto err;
1857         }
1858         ret = __insert_manifest_info_in_db(mfx, uid);
1859         if (ret == -1) {
1860                 _LOGD("Insert into DB failed. Rollback now\n");
1861                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1862                 goto err;
1863         }
1864
1865         /*Commit transaction*/
1866         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1867         if (ret != SQLITE_OK) {
1868                 _LOGD("Failed to commit transaction. Rollback now\n");
1869                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1870                 ret = -1;
1871                 goto err;
1872         }
1873         _LOGD("Transaction Commit and End\n");
1874 err:
1875         pkgmgr_parser_close_db();
1876         return ret;
1877 }
1878
1879 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1880 {
1881         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
1882 }
1883
1884 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
1885 {
1886         if (mfx == NULL) {
1887                 _LOGD("manifest pointer is NULL\n");
1888                 return -1;
1889         }
1890         int ret = 0;
1891         ret = pkgmgr_parser_check_and_create_db(uid);
1892         if (ret == -1) {
1893                 _LOGD("Failed to open DB\n");
1894                 return ret;
1895         }
1896         /*Begin transaction*/
1897         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1898         if (ret != SQLITE_OK) {
1899                 _LOGD("Failed to begin transaction\n");
1900                 ret = -1;
1901                 goto err;
1902         }
1903         _LOGD("Transaction Begin\n");
1904         ret = __delete_manifest_info_from_db(mfx, uid);
1905         if (ret == -1) {
1906                 _LOGD("Delete from DB failed. Rollback now\n");
1907                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1908                 goto err;
1909         }
1910         /*Commit transaction*/
1911         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1912         if (ret != SQLITE_OK) {
1913                 _LOGD("Failed to commit transaction, Rollback now\n");
1914                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1915                 ret = -1;
1916                 goto err;
1917         }
1918         _LOGD("Transaction Commit and End\n");
1919 err:
1920         pkgmgr_parser_close_db();
1921         return ret;
1922 }
1923
1924 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1925 {
1926         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
1927 }
1928
1929 API int pkgmgr_parser_update_preload_info_in_db()
1930 {
1931         int ret = 0;
1932         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1933         if (ret == -1) {
1934                 _LOGD("Failed to open DB\n");
1935                 return ret;
1936         }
1937         /*Begin transaction*/
1938         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1939         if (ret != SQLITE_OK) {
1940                 _LOGD("Failed to begin transaction\n");
1941                 ret = -1;
1942                 goto err;
1943         }
1944         _LOGD("Transaction Begin\n");
1945         ret = __update_preload_condition_in_db();
1946         if (ret == -1) {
1947                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
1948                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1949                 goto err;
1950         }
1951         /*Commit transaction*/
1952         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1953         if (ret != SQLITE_OK) {
1954                 _LOGD("Failed to commit transaction, Rollback now\n");
1955                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1956                 ret = -1;
1957                 goto err;
1958         }
1959         _LOGD("Transaction Commit and End\n");
1960 err:
1961         pkgmgr_parser_close_db();
1962         return ret;
1963 }
1964
1965 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
1966 {
1967         int ret = 0;
1968         ret = pkgmgr_parser_check_and_create_db(uid);
1969         if (ret == -1) {
1970                 _LOGD("Failed to open DB\n");
1971                 return ret;
1972         }
1973         /*Begin transaction*/
1974         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1975         if (ret != SQLITE_OK) {
1976                 _LOGD("Failed to begin transaction\n");
1977                 ret = -1;
1978                 goto err;
1979         }
1980         _LOGD("Transaction Begin\n");
1981         ret = __update_preload_condition_in_db();
1982         if (ret == -1) {
1983                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
1984                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1985                 goto err;
1986         }
1987         /*Commit transaction*/
1988         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1989         if (ret != SQLITE_OK) {
1990                 _LOGD("Failed to commit transaction, Rollback now\n");
1991                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1992                 ret = -1;
1993                 goto err;
1994         }
1995         _LOGD("Transaction Commit and End\n");
1996 err:
1997         pkgmgr_parser_close_db();
1998         return ret;
1999 }