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