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