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