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