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