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