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