clear 'rpm' related code
[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 #include <system_info.h>
36
37 /* For multi-user support */
38 #include <tzplatform_config.h>
39
40 #include "pkgmgr-info.h"
41 #include "pkgmgrinfo_basic.h"
42 #include "pkgmgrinfo_debug.h"
43 #include "pkgmgr_parser_internal.h"
44 #include "pkgmgr_parser_db.h"
45
46 #ifdef LOG_TAG
47 #undef LOG_TAG
48 #endif
49 #define LOG_TAG "PKGMGR_PARSER"
50
51 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
52 #define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
53 #define MAX_QUERY_LEN           4096
54 #define BUFSIZE 4096
55 #define OWNER_ROOT 0
56
57 #define LDPI "ldpi"
58 #define MDPI "mdpi"
59 #define HDPI "hdpi"
60 #define XHDPI "xhdpi"
61 #define XXHDPI "xxhdpi"
62
63 #define LDPI_MIN 0
64 #define LDPI_MAX 240
65 #define MDPI_MIN 241
66 #define MDPI_MAX 300
67 #define HDPI_MIN 301
68 #define HDPI_MAX 380
69 #define XHDPI_MIN 381
70 #define XHDPI_MAX 480
71 #define XXHDPI_MIN 481
72 #define XXHDPI_MAX 600
73
74 #define DB_LABEL "User::Home"
75 #define SET_SMACK_LABEL(x) \
76 do { \
77         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
78                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
79         else \
80                 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
81 } while (0)
82
83 sqlite3 *pkgmgr_parser_db;
84 sqlite3 *pkgmgr_cert_db;
85
86
87 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
88                                                 "(package text primary key not null, " \
89                                                 "package_type text DEFAULT 'tpk', " \
90                                                 "package_version text, " \
91                                                 "package_api_version text, " \
92                                                 "package_tep_name text, " \
93                                                 "install_location text, " \
94                                                 "package_size text, " \
95                                                 "package_removable text DEFAULT 'true', " \
96                                                 "package_preload text DEFAULT 'false', " \
97                                                 "package_readonly text DEFAULT 'false', " \
98                                                 "package_update text DEFAULT 'false', " \
99                                                 "package_appsetting text DEFAULT 'false', " \
100                                                 "package_nodisplay text DEFAULT 'false', " \
101                                                 "package_system text DEFAULT 'false', " \
102                                                 "author_name text, " \
103                                                 "author_email text, " \
104                                                 "author_href text," \
105                                                 "installed_time text," \
106                                                 "installed_storage text," \
107                                                 "storeclient_id text," \
108                                                 "mainapp_id text," \
109                                                 "package_url text," \
110                                                 "root_path text," \
111                                                 "csc_path text," \
112                                                 "package_support_disable text DEFAULT 'false', " \
113                                                 "package_disable text DEFAULT 'false')"
114
115 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
116                                                 "(package text not null, " \
117                                                 "package_locale text DEFAULT 'No Locale', " \
118                                                 "package_label text, " \
119                                                 "package_icon text, " \
120                                                 "package_description text, " \
121                                                 "package_license text, " \
122                                                 "package_author, " \
123                                                 "PRIMARY KEY(package, package_locale), " \
124                                                 "FOREIGN KEY(package) " \
125                                                 "REFERENCES package_info(package) " \
126                                                 "ON DELETE CASCADE)"
127
128 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
129                                                 "(package text not null, " \
130                                                 "privilege text not null, " \
131                                                 "PRIMARY KEY(package, privilege) " \
132                                                 "FOREIGN KEY(package) " \
133                                                 "REFERENCES package_info(package) " \
134                                                 "ON DELETE CASCADE)"
135
136 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
137                                                 "(app_id text primary key not null, " \
138                                                 "app_component text, " \
139                                                 "app_exec text, " \
140                                                 "app_nodisplay text DEFAULT 'false', " \
141                                                 "app_type text, " \
142                                                 "app_onboot text DEFAULT 'false', " \
143                                                 "app_multiple text DEFAULT 'false', " \
144                                                 "app_autorestart text DEFAULT 'false', " \
145                                                 "app_taskmanage text DEFAULT 'false', " \
146                                                 "app_enabled text DEFAULT 'true', " \
147                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
148                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
149                                                 "app_mainapp text, " \
150                                                 "app_recentimage text, " \
151                                                 "app_launchcondition text, " \
152                                                 "app_indicatordisplay text DEFAULT 'true', " \
153                                                 "app_portraitimg text, " \
154                                                 "app_landscapeimg text, " \
155                                                 "app_guestmodevisibility text DEFAULT 'true', " \
156                                                 "app_permissiontype text DEFAULT 'normal', " \
157                                                 "app_preload text DEFAULT 'false', " \
158                                                 "app_submode text DEFAULT 'false', " \
159                                                 "app_submode_mainid text, " \
160                                                 "app_installed_storage text, " \
161                                                 "app_process_pool text DEFAULT 'false', " \
162                                                 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
163                                                 "app_ui_gadget text DEFAULT 'false', " \
164                                                 "app_support_disable text DEFAULT 'false', " \
165                                                 "app_disable text DEFAULT 'false', " \
166                                                 "app_package_type text DEFAULT 'tpk', " \
167                                                 "component_type text, " \
168                                                 "package text not null, " \
169                                                 "app_tep_name text, " \
170                                                 "app_background_category INTEGER DEFAULT 0, " \
171                                                 "FOREIGN KEY(package) " \
172                                                 "REFERENCES package_info(package) " \
173                                                 "ON DELETE CASCADE)"
174
175 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
176                                                 "(app_id text not null, " \
177                                                 "app_locale text DEFAULT 'No Locale', " \
178                                                 "app_label text, " \
179                                                 "app_icon text, " \
180                                                 "PRIMARY KEY(app_id,app_locale) " \
181                                                 "FOREIGN KEY(app_id) " \
182                                                 "REFERENCES package_app_info(app_id) " \
183                                                 "ON DELETE CASCADE)"
184
185 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
186                                                 "(app_id text not null, " \
187                                                 "app_icon text, " \
188                                                 "app_icon_section text, " \
189                                                 "app_icon_resolution text, " \
190                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
191                                                 "FOREIGN KEY(app_id) " \
192                                                 "REFERENCES package_app_info(app_id) " \
193                                                 "ON DELETE CASCADE)"
194
195 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
196                                                 "(app_id text not null, " \
197                                                 "app_locale text DEFAULT 'No Locale', " \
198                                                 "app_image_section text, " \
199                                                 "app_image text, " \
200                                                 "PRIMARY KEY(app_id,app_image_section) " \
201                                                 "FOREIGN KEY(app_id) " \
202                                                 "REFERENCES package_app_info(app_id) " \
203                                                 "ON DELETE CASCADE)"
204
205 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
206                                                 "(app_id text not null, " \
207                                                 "app_control text not null, " \
208                                                 "PRIMARY KEY(app_id,app_control) " \
209                                                 "FOREIGN KEY(app_id) " \
210                                                 "REFERENCES package_app_info(app_id) " \
211                                                 "ON DELETE CASCADE)"
212
213 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
214                                                 "(app_id text not null, " \
215                                                 "category text not null, " \
216                                                 "PRIMARY KEY(app_id,category) " \
217                                                 "FOREIGN KEY(app_id) " \
218                                                 "REFERENCES package_app_info(app_id) " \
219                                                 "ON DELETE CASCADE)"
220
221 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
222                                                 "(app_id text not null, " \
223                                                 "md_key text not null, " \
224                                                 "md_value text not null, " \
225                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
226                                                 "FOREIGN KEY(app_id) " \
227                                                 "REFERENCES package_app_info(app_id) " \
228                                                 "ON DELETE CASCADE)"
229
230 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
231                                                 "(app_id text not null, " \
232                                                 "pm_type text not null, " \
233                                                 "pm_value text not null, " \
234                                                 "PRIMARY KEY(app_id, pm_type, pm_value) " \
235                                                 "FOREIGN KEY(app_id) " \
236                                                 "REFERENCES package_app_info(app_id) " \
237                                                 "ON DELETE CASCADE)"
238
239 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
240                                                 "(app_id text not null, " \
241                                                 "data_share_path text not null, " \
242                                                 "data_share_allowed text not null, " \
243                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
244                                                 "FOREIGN KEY(app_id) " \
245                                                 "REFERENCES package_app_info(app_id) " \
246                                                 "ON DELETE CASCADE)"
247
248 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
249                                                 "(app_id text not null, " \
250                                                 "data_share_request text not null, " \
251                                                 "PRIMARY KEY(app_id,data_share_request) " \
252                                                 "FOREIGN KEY(app_id) " \
253                                                 "REFERENCES package_app_info(app_id) " \
254                                                 "ON DELETE CASCADE)"
255
256 /* FIXME: duplicated at pkgmgrinfo_db.c */
257 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
258         "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
259         " cert_info TEXT UNIQUE, " \
260         " cert_id INTEGER PRIMARY KEY, " \
261         " cert_ref_count INTEGER NOT NULL)"
262
263 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
264         "CREATE TABLE IF NOT EXISTS package_cert_info( " \
265         " package TEXT PRIMARY KEY, " \
266         " author_root_cert INTEGER, " \
267         " author_im_cert INTEGER, " \
268         " author_signer_cert INTEGER, " \
269         " dist_root_cert INTEGER, " \
270         " dist_im_cert INTEGER, " \
271         " dist_signer_cert INTEGER, " \
272         " dist2_root_cert INTEGER, " \
273         " dist2_im_cert INTEGER, " \
274         " dist2_signer_cert INTEGER)"
275
276 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
277         "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
278         "AFTER DELETE ON package_cert_info " \
279         "BEGIN" \
280         " UPDATE package_cert_index_info SET" \
281         "  cert_ref_count = cert_ref_count - 1" \
282         " WHERE cert_id = OLD.author_root_cert" \
283         "  OR cert_id = OLD.author_im_cert" \
284         "  OR cert_id = OLD.author_signer_cert" \
285         "  OR cert_id = OLD.dist_root_cert" \
286         "  OR cert_id = OLD.dist_im_cert" \
287         "  OR cert_id = OLD.dist_signer_cert" \
288         "  OR cert_id = OLD.dist2_root_cert" \
289         "  OR cert_id = OLD.dist2_im_cert" \
290         "  OR cert_id = OLD.dist2_signer_cert;" \
291         "END;"
292
293 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
294         "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
295         "AFTER UPDATE ON package_cert_index_info " \
296         "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
297         "       WHERE cert_id = OLD.cert_id) = 0) "\
298         "BEGIN" \
299         " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
300         "END;"
301
302 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
303         "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
304         "AFTER UPDATE ON package_cert_info " \
305         "WHEN (OLD.%s IS NOT NULL) " \
306         "BEGIN" \
307         " UPDATE package_cert_index_info SET" \
308         "  cert_ref_count = cert_ref_count - 1" \
309         " WHERE cert_id = OLD.%s;" \
310         "END;"
311
312 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
313                                                 "(app_id text not null, " \
314                                                 "providerid text not null, " \
315                                                 "access text not null, " \
316                                                 "type text not null, " \
317                                                 "PRIMARY KEY(app_id, providerid, access, type) " \
318                                                 "FOREIGN KEY(app_id) " \
319                                                 "REFERENCES package_app_info(app_id) " \
320                                                 "ON DELETE CASCADE)"
321
322 #define QUERY_CREATE_TABLE_PACKAGE_APP_DISABLE_FOR_USER "CREATE TABLE IF NOT EXISTS package_app_disable_for_user " \
323                                                 "(app_id text not null, " \
324                                                 "uid text not null, " \
325                                                 "PRIMARY KEY(app_id, uid))"
326
327 #define QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN \
328         "create table if not exists package_app_splash_screen " \
329         "(app_id text not null, " \
330         "src text not null, " \
331         "type text not null, " \
332         "orientation text not null, " \
333         "indicatordisplay text, " \
334         "PRIMARY KEY(app_id, orientation) " \
335         "FOREIGN KEY(app_id) " \
336         "REFERENCES package_app_info(app_id) " \
337         "ON DELETE CASCADE)"
338
339 static int __insert_application_info(manifest_x *mfx);
340 static int __insert_application_appcategory_info(manifest_x *mfx);
341 static int __insert_application_appcontrol_info(manifest_x *mfx);
342 static int __insert_application_appmetadata_info(manifest_x *mfx);
343 static int __insert_application_share_allowed_info(manifest_x *mfx);
344 static int __insert_application_share_request_info(manifest_x *mfx);
345 static int __insert_application_datacontrol_info(manifest_x *mfx);
346 static void __insert_application_locale_info(gpointer data, gpointer userdata);
347 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
348 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
349 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
350 static int __delete_subpkg_info_from_db(char *appid);
351 static int __delete_appinfo_from_db(char *db_table, const char *appid);
352 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
353 static int __exec_query(char *query);
354 static void __extract_data(gpointer data, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath,
355                 char **label, char **license, char **icon, char **description, char **author);
356 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
357 static GList *__create_locale_list(GList *locale, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath);
358 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
359 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
360 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
361 static int __parserdb_change_perm(const char *db_file, uid_t uid);
362
363 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
364 {
365         if (coltxt[0])
366                 __delete_subpkg_info_from_db(coltxt[0]);
367
368         return 0;
369 }
370
371 static const char *__get_str(const char *str)
372 {
373         if (str == NULL)
374         {
375                 return PKGMGR_PARSER_EMPTY_STR;
376         }
377
378         return str;
379 }
380
381 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
382 {
383         int ret = -1;
384         sqlite3 *handle;
385
386         ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
387         if (ret != SQLITE_OK) {
388                 _LOGD("connect db [%s] failed!\n", db_path);
389                 return -1;
390         }
391         *db_handle = handle;
392
393         return 0;
394 }
395
396 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
397 {
398         manifest_x *mfx = (manifest_x *)data;
399         int i = 0;
400         char *appid = NULL;
401         char *status = NULL;
402         application_x *app;
403         GList *tmp;
404         if (mfx->application == NULL)
405                 return -1;
406         app = (application_x *)mfx->application->data;
407         for(i = 0; i < ncols; i++)
408         {
409                 if (strcmp(colname[i], "app_id") == 0) {
410                         if (coltxt[i])
411                                 appid = strdup(coltxt[i]);
412                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
413                         if (coltxt[i])
414                                 status = strdup(coltxt[i]);
415                 }
416         }
417         if (appid == NULL) {
418                 if(status != NULL)
419                         free(status);
420                 _LOGD("app id is NULL\n");
421                 return -1;
422         }
423         /*update guest mode visibility*/
424         for (tmp = mfx->application; tmp; tmp = tmp->next) {
425                 app = (application_x *)tmp->data;
426                 if (app == NULL)
427                         continue;
428                 if (strcmp(app->appid, appid) == 0) {
429                         free((void *)app->guestmode_visibility);
430                         app->guestmode_visibility = strdup(status);
431                         break;
432                 }
433         }
434         if (appid) {
435                 free(appid);
436                 appid = NULL;
437         }
438         if (status) {
439                 free(status);
440                 status = NULL;
441         }
442
443         return 0;
444 }
445
446 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
447 {
448         char *error_message = NULL;
449         char query[MAX_QUERY_LEN] = {'\0'};
450         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
451         if (SQLITE_OK !=
452             sqlite3_exec(pkgmgr_parser_db, query,
453                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
454                 _LOGD("Don't execute query = %s error message = %s\n",
455                        query, error_message);
456                 sqlite3_free(error_message);
457         }
458         return;
459 }
460
461 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
462 {
463         char *error_message = NULL;
464         if (SQLITE_OK !=
465             sqlite3_exec(db_handle, db_query,
466                          NULL, NULL, &error_message)) {
467                 _LOGD("Don't execute query = %s error message = %s\n",
468                        db_query, error_message);
469                 sqlite3_free(error_message);
470                 return -1;
471         }
472         sqlite3_free(error_message);
473         return 0;
474 }
475
476 static int __exec_query(char *query)
477 {
478         char *error_message = NULL;
479         if (SQLITE_OK !=
480             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
481                 _LOGE("Don't execute query = %s error message = %s\n", query,
482                        error_message);
483                 sqlite3_free(error_message);
484                 return -1;
485         }
486         sqlite3_free(error_message);
487         return 0;
488 }
489
490 static int __exec_query_no_msg(char *query)
491 {
492         char *error_message = NULL;
493         if (SQLITE_OK !=
494             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
495                 sqlite3_free(error_message);
496                 return -1;
497         }
498         sqlite3_free(error_message);
499         return 0;
500 }
501
502 static GList *__create_locale_list(GList *locale, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths)
503 {
504         GList *tmp;
505         label_x *lbl;
506         license_x *lcn;
507         icon_x *icn;
508         description_x *dcn;
509         author_x *ath;
510         for (tmp = lbls; tmp; tmp = tmp->next) {
511                 lbl = (label_x *)tmp->data;
512                 if (lbl == NULL)
513                         continue;
514                 if (lbl->lang)
515                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
516         }
517         for (tmp = lcns; tmp; tmp = tmp->next) {
518                 lcn = (license_x *)tmp->data;
519                 if (lcn == NULL)
520                         continue;
521                 if (lcn->lang)
522                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
523         }
524         for (tmp = icns; tmp; tmp = tmp->next) {
525                 icn = (icon_x *)tmp->data;
526                 if (icn == NULL)
527                         continue;
528                 if (icn->lang)
529                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
530         }
531         for (tmp = dcns; tmp; tmp = tmp->next) {
532                 dcn = (description_x *)tmp->data;
533                 if (dcn == NULL)
534                         continue;
535                 if (dcn->lang)
536                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
537         }
538         for (tmp = aths; tmp; tmp = tmp->next) {
539                 ath = (author_x *)tmp->data;
540                 if (ath == NULL)
541                         continue;
542                 if (ath->lang)
543                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
544         }
545         return locale;
546
547 }
548
549 static GList *__create_icon_list(GList *appicon, GList *icns)
550 {
551         GList *tmp;
552         icon_x *icn;
553
554         for (tmp = icns; tmp; tmp = tmp->next) {
555                 icn = (icon_x *)tmp->data;
556                 if (icn == NULL)
557                         continue;
558                 if (icn->section)
559                         appicon = g_list_insert_sorted_with_data(appicon, (gpointer)icn->section, __comparefunc, NULL);
560         }
561         return appicon;
562 }
563
564 static GList *__create_image_list(GList *appimage, GList *imgs)
565 {
566         GList *tmp;
567         image_x *img;
568
569         for (tmp = imgs; tmp; tmp = tmp->next) {
570                 img = (image_x *)tmp->data;
571                 if (img == NULL)
572                         continue;
573                 if (img->section)
574                         appimage = g_list_insert_sorted_with_data(appimage, (gpointer)img->section, __comparefunc, NULL);
575         }
576         return appimage;
577 }
578
579 static void __trimfunc(GList* trim_list)
580 {
581         char *trim_data = NULL;
582         char *prev = NULL;
583
584         GList *list = NULL;
585         list = g_list_first(trim_list);
586
587         while (list) {
588                 trim_data = (char *)list->data;
589                 if (trim_data) {
590                         if (prev) {
591                                 if (strcmp(trim_data, prev) == 0) {
592                                         trim_list = g_list_remove(trim_list, trim_data);
593                                         list = g_list_first(trim_list);
594                                         prev = NULL;
595                                         continue;
596                                 } else
597                                         prev = trim_data;
598                         }
599                         else
600                                 prev = trim_data;
601                 }
602                 list = g_list_next(list);
603         }
604 }
605
606 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
607 {
608         if (a == NULL || b == NULL)
609                 return 0;
610         if (strcmp((char*)a, (char*)b) == 0)
611                 return 0;
612         if (strcmp((char*)a, (char*)b) < 0)
613                 return -1;
614         if (strcmp((char*)a, (char*)b) > 0)
615                 return 1;
616         return 0;
617 }
618
619 static int __check_dpi(const char *dpi_char, int dpi_int)
620 {
621         if (dpi_char == NULL)
622                 return -1;
623
624         if (strcasecmp(dpi_char, LDPI) == 0) {
625                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
626                         return 0;
627                 else
628                         return -1;
629         } else if (strcasecmp(dpi_char, MDPI) == 0) {
630                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
631                         return 0;
632                 else
633                         return -1;
634         } else if (strcasecmp(dpi_char, HDPI) == 0) {
635                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
636                         return 0;
637                 else
638                         return -1;
639         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
640                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
641                         return 0;
642                 else
643                         return -1;
644         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
645                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
646                         return 0;
647                 else
648                         return -1;
649         } else
650                 return -1;
651 }
652
653 static gint __compare_icon(gconstpointer a, gconstpointer b)
654 {
655         icon_x *icon = (icon_x *)a;
656
657         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
658                 return -1;
659
660         if (icon->dpi != NULL)
661                 return -1;
662
663         return 0;
664 }
665
666 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
667 {
668         icon_x *icon = (icon_x *)a;
669         int dpi = GPOINTER_TO_INT(b);
670
671         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
672                 return -1;
673
674         if (icon->dpi == NULL)
675                 return -1;
676
677         if (__check_dpi(icon->dpi, dpi) == 0)
678                 return 0;
679
680         return -1;
681 }
682
683 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
684 {
685         icon_x *icon = (icon_x *)a;
686         char *lang = (char *)b;
687
688         if (icon->dpi != NULL)
689                 return -1;
690
691         if (strcasecmp(icon->lang, lang) == 0)
692                 return 0;
693
694         return -1;
695 }
696
697 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
698 {
699         icon_x *icon = (icon_x *)a;
700         char *lang = (char *)b;
701         int dpi = -1;
702
703         system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi);
704         if (!dpi)
705                 return -1;
706
707         if (strcasecmp(icon->lang, lang) == 0 && __check_dpi(icon->dpi, dpi) == 0)
708                 return 0;
709
710         return -1;
711 }
712
713 static char *__find_icon(GList *icons, const char *lang)
714 {
715         GList *tmp;
716         icon_x *icon = NULL;
717         int dpi = 0;
718
719         // first, find icon whose locale and dpi with given lang and system's dpi has matched
720         tmp = g_list_find_custom(icons, lang, (GCompareFunc)__compare_icon_with_lang_dpi);
721         if (tmp != NULL) {
722                 icon = (icon_x *)tmp->data;
723                 return (char *)icon->text;
724         }
725
726         // if first has failed, find icon whose locale has matched
727         tmp = g_list_find_custom(icons, lang, (GCompareFunc)__compare_icon_with_lang);
728         if (tmp != NULL) {
729                 icon = (icon_x *)tmp->data;
730                 return (char *)icon->text;
731         }
732
733         // if second has failed, find icon whose dpi has matched with system's dpi
734         system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi);
735         if (!dpi)
736                 return NULL;
737         tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi), (GCompareFunc)__compare_icon_with_dpi);
738         if (tmp != NULL) {
739                 icon = (icon_x *)tmp->data;
740                 return (char *)icon->text;
741         }
742
743         // last, find default icon marked as "No Locale"
744         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
745         if (tmp != NULL) {
746                 icon = (icon_x *)tmp->data;
747                 return (char *)icon->text;
748         }
749
750         return NULL;
751 }
752
753 static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths,
754                 char **label, char **license, char **icon, char **description, char **author)
755 {
756         GList *tmp;
757         label_x *lbl;
758         license_x *lcn;
759         description_x *dcn;
760         author_x *ath;
761         for (tmp = lbls; tmp; tmp = tmp->next) {
762                 lbl = (label_x *)tmp->data;
763                 if (lbl == NULL)
764                         continue;
765                 if (lbl->lang) {
766                         if (strcmp(lbl->lang, (char *)data) == 0) {
767                                 *label = (char*)lbl->text;
768                                 break;
769                         }
770                 }
771         }
772         for (tmp = lcns; tmp; tmp = tmp->next) {
773                 lcn = (license_x *)tmp->data;
774                 if (lcn == NULL)
775                         continue;
776                 if (lcn->lang) {
777                         if (strcmp(lcn->lang, (char *)data) == 0) {
778                                 *license = (char*)lcn->text;
779                                 break;
780                         }
781                 }
782         }
783
784         *icon = __find_icon(icns, (char *)data);
785
786         for (tmp = dcns; tmp; tmp = tmp->next) {
787                 dcn = (description_x *)tmp->data;
788                 if (dcn == NULL)
789                         continue;
790                 if (dcn->lang) {
791                         if (strcmp(dcn->lang, (char *)data) == 0) {
792                                 *description = (char*)dcn->text;
793                                 break;
794                         }
795                 }
796         }
797         for (tmp = aths; tmp; tmp = tmp->next) {
798                 ath = (author_x *)tmp->data;
799                 if (ath == NULL)
800                         continue;
801                 if (ath->lang) {
802                         if (strcmp(ath->lang, (char *)data) == 0) {
803                                 *author = (char*)ath->text;
804                                 break;
805                         }
806                 }
807         }
808
809 }
810
811 static void __extract_icon_data(gpointer data, GList *icns, char **icon, char **resolution)
812 {
813         GList *tmp;
814         icon_x *icn;
815         for (tmp = icns; tmp; tmp = tmp->next) {
816                 icn = (icon_x *)tmp->data;
817                 if (icn == NULL)
818                         continue;
819                 if (icn->section) {
820                         if (strcmp(icn->section, (char *)data) == 0) {
821                                 *icon = (char*)icn->text;
822                                 *resolution = (char*)icn->resolution;
823                                 break;
824                         }
825                 }
826         }
827 }
828
829 static void __extract_image_data(gpointer data, GList *imgs, char **lang, char **image)
830 {
831         GList *tmp;
832         image_x *img;
833         for (tmp = imgs; tmp; tmp = tmp->next) {
834                 img = (image_x *)tmp->data;
835                 if (img == NULL)
836                         continue;
837                 if (img->section) {
838                         if (strcmp(img->section, (char *)data) == 0) {
839                                 *lang = (char*)img->lang;
840                                 *image = (char*)img->text;
841                                 break;
842                         }
843                 }
844         }
845 }
846
847 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
848 {
849         int ret = -1;
850         char *label = NULL;
851         char *icon = NULL;
852         char *description = NULL;
853         char *license = NULL;
854         char *author = NULL;
855         char *query = NULL;
856
857         manifest_x *mfx = (manifest_x *)userdata;
858         GList *lbl = mfx->label;
859         GList *lcn = mfx->license;
860         GList *icn = mfx->icon;
861         GList *dcn = mfx->description;
862         GList *ath = mfx->author;
863
864         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
865         if (!label && !description && !icon && !license && !author)
866                 return;
867
868         query = sqlite3_mprintf("insert into package_localized_info(package, package_locale, " \
869                 "package_label, package_icon, package_description, package_license, package_author) values " \
870                 "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
871                 mfx->package,
872                 (char*)data,
873                 __get_str(label),
874                 __get_str(icon),
875                 __get_str(description),
876                 __get_str(license),
877                 __get_str(author));
878
879         ret = __exec_query(query);
880         if (ret == -1)
881                 _LOGD("Package Localized Info DB Insert failed\n");
882
883         sqlite3_free(query);
884 }
885
886 static void __insert_application_locale_info(gpointer data, gpointer userdata)
887 {
888         int ret = -1;
889         char *label = NULL;
890         char *icon = NULL;
891         char *query = NULL;
892
893         application_x *app = (application_x*)userdata;
894         GList *lbl = app->label;
895         GList *icn = app->icon;
896
897         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
898         if (!label && !icon)
899                 return;
900
901         query = sqlite3_mprintf("insert into package_app_localized_info(app_id, app_locale, " \
902                 "app_label, app_icon) values " \
903                 "(%Q, %Q, %Q, %Q)", app->appid, (char*)data,
904                 __get_str(label), __get_str(icon));
905         ret = __exec_query(query);
906         if (ret == -1)
907                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
908
909         sqlite3_free(query);
910
911         /*insert ui app locale info to pkg locale to get mainapp data */
912         if (strcasecmp(app->mainapp, "true")==0) {
913                 query = sqlite3_mprintf("insert into package_localized_info(package, package_locale, " \
914                         "package_label, package_icon, package_description, package_license, package_author) values " \
915                         "(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
916                         app->package,
917                         (char*)data,
918                         __get_str(label),
919                         __get_str(icon),
920                         PKGMGR_PARSER_EMPTY_STR,
921                         PKGMGR_PARSER_EMPTY_STR,
922                         PKGMGR_PARSER_EMPTY_STR);
923
924                 ret = __exec_query_no_msg(query);
925                 sqlite3_free(query);
926
927                 if (icon != NULL) {
928                         query = sqlite3_mprintf("update package_localized_info set package_icon=%Q "\
929                                 "where package=%Q and package_locale=%Q", icon, app->package, (char*)data);
930                         ret = __exec_query_no_msg(query);
931                         sqlite3_free(query);
932                 }
933         }
934 }
935
936 static void __insert_application_icon_section_info(gpointer data, gpointer userdata)
937 {
938         int ret = -1;
939         char *icon = NULL;
940         char *resolution = NULL;
941         char query[MAX_QUERY_LEN] = {'\0'};
942
943         application_x *app = (application_x*)userdata;
944         GList *icn = app->icon;
945
946         __extract_icon_data(data, icn, &icon, &resolution);
947         if (!icon && !resolution)
948                 return;
949         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
950                 "app_icon, app_icon_section, app_icon_resolution) values " \
951                 "('%q', '%q', '%q', '%q')", app->appid,
952                 icon, (char*)data, resolution);
953
954         ret = __exec_query(query);
955         if (ret == -1)
956                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
957
958 }
959
960 static void __insert_application_image_info(gpointer data, gpointer userdata)
961 {
962         int ret = -1;
963         char *lang = NULL;
964         char *img = NULL;
965         char query[MAX_QUERY_LEN] = {'\0'};
966
967         application_x *app = (application_x*)userdata;
968         GList *image = app->image;
969
970         __extract_image_data(data, image, &lang, &img);
971         if (!lang && !img)
972                 return;
973         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
974                 "app_image_section, app_image) values " \
975                 "('%q', '%q', '%q', '%q')", app->appid, lang, (char*)data, img);
976
977         ret = __exec_query(query);
978         if (ret == -1)
979                 _LOGD("Package UiApp image Info DB Insert failed\n");
980
981 }
982
983
984 static int __insert_mainapp_info(manifest_x *mfx)
985 {
986         GList *tmp;
987         application_x *app;
988         int ret = -1;
989         char query[MAX_QUERY_LEN] = {'\0'};
990         for (tmp = mfx->application; tmp; tmp = tmp->next) {
991                 app = (application_x *)tmp->data;
992                 if (app == NULL)
993                         continue;
994                 snprintf(query, MAX_QUERY_LEN,
995                         "update package_app_info set app_mainapp='%s' where app_id='%s'", app->mainapp, app->appid);
996
997                 ret = __exec_query(query);
998                 if (ret == -1) {
999                         _LOGD("Package App Info DB Insert Failed\n");
1000                         return -1;
1001                 }
1002                 if (strcasecmp(app->mainapp, "True")==0)
1003                         mfx->mainapp_id = strdup(app->appid);
1004         }
1005
1006         if (mfx->mainapp_id == NULL) {
1007                 if (mfx->application == NULL)
1008                         return -1;
1009                 app = (application_x *)mfx->application->data;
1010                 if (app == NULL)
1011                         return -1;
1012                 if (app->appid) {
1013                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", app->appid);
1014                 } else {
1015                         _LOGD("Not valid appid\n");
1016                         return -1;
1017                 }
1018
1019                 ret = __exec_query(query);
1020                 if (ret == -1) {
1021                         _LOGD("Package UiApp Info DB Insert Failed\n");
1022                         return -1;
1023                 }
1024
1025                 free((void *)app->mainapp);
1026                 app->mainapp= strdup("true");
1027                 mfx->mainapp_id = strdup(app->appid);
1028         }
1029
1030         memset(query, '\0', MAX_QUERY_LEN);
1031         snprintf(query, MAX_QUERY_LEN,
1032                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
1033         ret = __exec_query(query);
1034         if (ret == -1) {
1035                 _LOGD("Package Info DB update Failed\n");
1036                 return -1;
1037         }
1038
1039         return 0;
1040 }
1041
1042 static int __convert_background_category(GList *category_list)
1043 {
1044         int ret = 0;
1045         GList *tmp_list = category_list;
1046         char *category_data = NULL;
1047
1048         if (category_list == NULL)
1049                 return 0;
1050
1051         while (tmp_list != NULL) {
1052                 category_data = (char *)tmp_list->data;
1053                 if (strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR) == 0) {
1054                         ret = ret | APP_BG_CATEGORY_MEDIA_VAL;
1055                 } else if (strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR) == 0) {
1056                         ret = ret | APP_BG_CATEGORY_DOWNLOAD_VAL;
1057                 } else if (strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR) == 0) {
1058                         ret = ret | APP_BG_CATEGORY_BGNETWORK_VAL;
1059                 } else if (strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR) == 0) {
1060                         ret = ret | APP_BG_CATEGORY_LOCATION_VAL;
1061                 } else if (strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR) == 0) {
1062                         ret = ret | APP_BG_CATEGORY_SENSOR_VAL;
1063                 } else if (strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR) == 0) {
1064                         ret = ret | APP_BG_CATEGORY_IOTCOMM_VAL;
1065                 } else if (strcmp(category_data, APP_BG_CATEGORY_SYSTEM) == 0) {
1066                         ret = ret | APP_BG_CATEGORY_SYSTEM_VAL;
1067                 } else {
1068                         _LOGE("Unidentified category [%s]", category_data);
1069                 }
1070                 tmp_list = g_list_next(tmp_list);
1071         }
1072
1073         return ret;
1074 }
1075
1076 /* _PRODUCT_LAUNCHING_ENHANCED_
1077 *  app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
1078 */
1079 static int __insert_application_info(manifest_x *mfx)
1080 {
1081         GList *tmp;
1082         application_x *app;
1083         int ret = -1;
1084         int background_value = 0;
1085         char query[MAX_QUERY_LEN] = {'\0'};
1086         char *type = NULL;
1087
1088         if (mfx->type)
1089                 type = strdup(mfx->type);
1090         else
1091                 type = strdup("tpk");
1092
1093         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1094                 app = (application_x *)tmp->data;
1095                 if (app == NULL)
1096                         continue;
1097
1098                 background_value = __convert_background_category(app->background_category);
1099                 if (background_value < 0) {
1100                         _LOGE("Failed to retrieve background value[%d]", background_value);
1101                         background_value = 0;
1102                 }
1103
1104                 snprintf(query, MAX_QUERY_LEN,
1105                         "insert into package_app_info(" \
1106                         "app_id, app_component, app_exec, app_nodisplay, app_type, " \
1107                         "app_onboot, app_multiple, app_autorestart, app_taskmanage, app_enabled, " \
1108                         "app_hwacceleration, app_screenreader, app_mainapp, app_recentimage, app_launchcondition, " \
1109                         "app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, " \
1110                         "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, " \
1111                         "app_launch_mode, app_ui_gadget, app_support_disable, component_type, package, " \
1112                         "app_tep_name, app_background_category, app_package_type) " \
1113                         "values(" \
1114                         "'%s', '%s', '%s', '%s', '%s', " \
1115                         "'%s', '%s', '%s', '%s', '%s', " \
1116                         "'%s', '%s', '%s', '%s', '%s', " \
1117                         "'%s', '%s', '%s', '%s', '%s', " \
1118                         "'%s', '%s', '%s', '%s', '%s', " \
1119                         "'%s', '%s', '%s', '%s', '%s', " \
1120                         "'%s', '%d', '%s')", \
1121                         app->appid, app->component_type, app->exec, app->nodisplay, app->type,
1122                         app->onboot, app->multiple, app->autorestart, app->taskmanage, app->enabled,
1123                         app->hwacceleration, app->screenreader, app->mainapp, __get_str(app->recentimage), app->launchcondition,
1124                         app->indicatordisplay, __get_str(app->portraitimg), __get_str(app->landscapeimg),
1125                         app->guestmode_visibility, app->permission_type,
1126                         mfx->preload, app->submode, __get_str(app->submode_mainid), mfx->installed_storage, app->process_pool,
1127                         app->launch_mode, app->ui_gadget, mfx->support_disable, app->component_type, mfx->package,
1128                         __get_str(mfx->tep_name), background_value, type);
1129
1130                 ret = __exec_query(query);
1131                 if (ret == -1) {
1132                         _LOGD("Package UiApp Info DB Insert Failed\n");
1133                         if (type)
1134                                 free(type);
1135                         return -1;
1136                 }
1137                 memset(query, '\0', MAX_QUERY_LEN);
1138         }
1139
1140         if (type)
1141                 free(type);
1142
1143         return 0;
1144 }
1145
1146 static int __insert_application_appcategory_info(manifest_x *mfx)
1147 {
1148         GList *app_tmp;
1149         application_x *app;
1150         GList *ct_tmp;
1151         const char *ct;
1152         int ret = -1;
1153         char query[MAX_QUERY_LEN] = {'\0'};
1154         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1155                 app = (application_x *)app_tmp->data;
1156                 if (app == NULL)
1157                         continue;
1158                 for (ct_tmp = app->category; ct_tmp; ct_tmp = ct_tmp->next) {
1159                         ct = (const char *)ct_tmp->data;
1160                         if (ct == NULL)
1161                                 continue;
1162                         snprintf(query, MAX_QUERY_LEN,
1163                                 "insert into package_app_app_category(app_id, category) " \
1164                                 "values('%s','%s')",\
1165                                  app->appid, ct);
1166                         ret = __exec_query(query);
1167                         if (ret == -1) {
1168                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
1169                                 return -1;
1170                         }
1171                         memset(query, '\0', MAX_QUERY_LEN);
1172                 }
1173         }
1174         return 0;
1175 }
1176
1177 static int __insert_application_appmetadata_info(manifest_x *mfx)
1178 {
1179         GList *app_tmp;
1180         application_x *app;
1181         GList *md_tmp;
1182         metadata_x *md;
1183         int ret = -1;
1184         char query[MAX_QUERY_LEN] = {'\0'};
1185         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1186                 app = (application_x *)app_tmp->data;
1187                 if (app == NULL)
1188                         continue;
1189                 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
1190                         md = (metadata_x *)md_tmp->data;
1191                         if (md == NULL)
1192                                 continue;
1193                         if (md->key) {
1194                                 snprintf(query, MAX_QUERY_LEN,
1195                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1196                                         "values('%s','%s', '%s')",\
1197                                          app->appid, md->key, md->value ? md->value : "");
1198                                 ret = __exec_query(query);
1199                                 if (ret == -1) {
1200                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
1201                                         return -1;
1202                                 }
1203                         }
1204                         memset(query, '\0', MAX_QUERY_LEN);
1205                 }
1206         }
1207         return 0;
1208 }
1209
1210 static int __insert_application_apppermission_info(manifest_x *mfx)
1211 {
1212         GList *app_tmp;
1213         application_x *app;
1214         GList *pm_tmp;
1215         permission_x *pm;
1216         int ret = -1;
1217         char query[MAX_QUERY_LEN] = {'\0'};
1218         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1219                 app = (application_x *)app_tmp->data;
1220                 if (app == NULL)
1221                         continue;
1222                 for (pm_tmp = app->permission; pm_tmp; pm_tmp = pm_tmp->next) {
1223                         pm = (permission_x *)pm_tmp->data;
1224                         if (pm == NULL)
1225                                 continue;
1226                         snprintf(query, MAX_QUERY_LEN,
1227                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1228                                 "values('%s','%s', '%s')",\
1229                                  app->appid, pm->type, pm->value);
1230                         ret = __exec_query(query);
1231                         if (ret == -1) {
1232                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
1233                                 return -1;
1234                         }
1235                         memset(query, '\0', MAX_QUERY_LEN);
1236                 }
1237         }
1238         return 0;
1239 }
1240
1241 static int __insert_application_appcontrol_info(manifest_x *mfx)
1242 {
1243         GList *app_tmp;
1244         application_x *app;
1245         GList *acontrol_tmp;
1246         appcontrol_x *acontrol;
1247         int ret = -1;
1248         char query[MAX_QUERY_LEN] = {'\0'};
1249         char buf[BUFSIZE] = {'\0'};
1250         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1251                 app = (application_x *)app_tmp->data;
1252                 if (app == NULL)
1253                         continue;
1254                 for (acontrol_tmp = app->appcontrol; acontrol_tmp; acontrol_tmp = acontrol_tmp->next) {
1255                         acontrol = (appcontrol_x *)acontrol_tmp->data;
1256                         if (acontrol == NULL)
1257                                 continue;
1258                         snprintf(buf, BUFSIZE, "%s|%s|%s",\
1259                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1260                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1261                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1262                         snprintf(query, MAX_QUERY_LEN,
1263                                         "insert into package_app_app_control(app_id, app_control) " \
1264                                         "values('%s', '%s')",\
1265                                         app->appid, buf);
1266                         ret = __exec_query(query);
1267                         if (ret == -1) {
1268                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1269                                 return -1;
1270                         }
1271                         memset(query, '\0', MAX_QUERY_LEN);
1272                 }
1273         }
1274         return 0;
1275 }
1276
1277 static int __insert_application_datacontrol_info(manifest_x *mfx)
1278 {
1279         GList *app_tmp;
1280         application_x *app;
1281         GList *dc_tmp;
1282         datacontrol_x *dc;
1283         int ret = -1;
1284         char query[MAX_QUERY_LEN] = {'\0'};
1285
1286         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1287                 app = (application_x *)app_tmp->data;
1288                 if (app == NULL)
1289                         continue;
1290                 for (dc_tmp = app->datacontrol; dc_tmp; dc_tmp = dc_tmp->next) {
1291                         dc = (datacontrol_x *)dc_tmp->data;
1292                         if (dc == NULL)
1293                                 continue;
1294                         snprintf(query, MAX_QUERY_LEN,
1295                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1296                                         "values('%s', '%s', '%s', '%s')",\
1297                                         app->appid,
1298                                         dc->providerid,
1299                                         dc->access,
1300                                         dc->type);
1301
1302                         ret = __exec_query(query);
1303                         if (ret == -1) {
1304                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1305                                 return -1;
1306                         }
1307                         memset(query, '\0', MAX_QUERY_LEN);
1308                 }
1309         }
1310         return 0;
1311 }
1312
1313 static int __insert_application_share_request_info(manifest_x *mfx)
1314 {
1315         GList *app_tmp;
1316         application_x *app;
1317         GList *ds_tmp;
1318         datashare_x *ds;
1319         GList *rq_tmp;
1320         const char *rq;
1321         int ret = -1;
1322         char query[MAX_QUERY_LEN] = {'\0'};
1323         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1324                 app = (application_x *)app_tmp->data;
1325                 if (app == NULL)
1326                         continue;
1327                 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1328                         ds = (datashare_x *)ds_tmp->data;
1329                         if (ds == NULL)
1330                                 continue;
1331                         for (rq_tmp = ds->request; rq_tmp; rq_tmp = rq_tmp->next) {
1332                                 rq = (const char *)rq_tmp->data;
1333                                 if (rq == NULL)
1334                                         continue;
1335                                 snprintf(query, MAX_QUERY_LEN,
1336                                          "insert into package_app_share_request(app_id, data_share_request) " \
1337                                         "values('%s', '%s')",\
1338                                          app->appid, rq);
1339                                 ret = __exec_query(query);
1340                                 if (ret == -1) {
1341                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1342                                         return -1;
1343                                 }
1344                                 memset(query, '\0', MAX_QUERY_LEN);
1345                         }
1346                 }
1347         }
1348         return 0;
1349 }
1350
1351 static int __insert_application_share_allowed_info(manifest_x *mfx)
1352 {
1353         GList *app_tmp;
1354         application_x *app;
1355         GList *ds_tmp;
1356         datashare_x *ds;
1357         GList *df_tmp;
1358         define_x *df;
1359         GList *al_tmp;
1360         const char *al;
1361         int ret = -1;
1362         char query[MAX_QUERY_LEN] = {'\0'};
1363         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1364                 app = (application_x *)app_tmp->data;
1365                 if (app == NULL)
1366                         continue;
1367                 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1368                         ds = (datashare_x *)ds_tmp->data;
1369                         if (ds == NULL)
1370                                 continue;
1371                         for (df_tmp = ds->define; df_tmp; df_tmp = df_tmp->next) {
1372                                 df = (define_x *)df_tmp->data;
1373                                 if (df == NULL)
1374                                         continue;
1375                                 for (al_tmp = df->allowed; al_tmp; al_tmp = al_tmp->next) {
1376                                         al = (const char *)al_tmp->data;
1377                                         if (al == NULL)
1378                                                 continue;
1379                                         snprintf(query, MAX_QUERY_LEN,
1380                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1381                                                 "values('%s', '%s', '%s')",\
1382                                                  app->appid, df->path, al);
1383                                         ret = __exec_query(query);
1384                                         if (ret == -1) {
1385                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1386                                                 return -1;
1387                                         }
1388                                         memset(query, '\0', MAX_QUERY_LEN);
1389                                 }
1390                         }
1391                 }
1392         }
1393         return 0;
1394 }
1395
1396 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a, gconstpointer b)
1397 {
1398         splashscreen_x *ss = (splashscreen_x *)a;
1399         const char *orientation = (const char *)b;
1400         int dpi = -1;
1401
1402         system_info_get_platform_int("http://tizen.org/feature/screen.dpi", &dpi);
1403         if (!dpi)
1404                 return -1;
1405
1406         if (strcasecmp(ss->orientation, orientation) == 0 && __check_dpi(ss->dpi, dpi) == 0)
1407                 return 0;
1408
1409         return -1;
1410 }
1411
1412 static gint __compare_splashscreen_with_orientation(gconstpointer a, gconstpointer b)
1413 {
1414         splashscreen_x *ss = (splashscreen_x *)a;
1415         const char *orientation = (const char *)b;
1416
1417         if (strcasecmp(ss->orientation, orientation) == 0 && (ss->dpi == NULL))
1418                 return 0;
1419
1420         return -1;
1421 }
1422
1423 static splashscreen_x *__find_splashscreen(GList *splashscreens, const char *orientation)
1424 {
1425         GList *tmp;
1426
1427         tmp = g_list_find_custom(splashscreens, orientation,
1428                         (GCompareFunc)__compare_splashscreen_with_orientation_dpi);
1429         if (tmp)
1430                 return (splashscreen_x *)tmp->data;
1431
1432         tmp = g_list_find_custom(splashscreens, orientation,
1433                         (GCompareFunc)__compare_splashscreen_with_orientation);
1434         if (tmp)
1435                 return (splashscreen_x *)tmp->data;
1436
1437         return NULL;
1438 }
1439
1440 static int __insert_application_splashscreen_info(manifest_x *mfx)
1441 {
1442         GList *app_tmp;
1443         application_x *app;
1444         splashscreen_x *ss;
1445         int ret = -1;
1446         char query[MAX_QUERY_LEN];
1447
1448         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1449                 app = (application_x *)app_tmp->data;
1450                 if (app == NULL || app->splashscreens == NULL)
1451                         continue;
1452
1453                 ss = __find_splashscreen(app->splashscreens, "portrait");
1454                 if (ss) {
1455                         snprintf(query, sizeof(query),
1456                                         "insert into package_app_splash_screen" \
1457                                         "(app_id, src, type, orientation, indicatordisplay) " \
1458                                         "values('%s', '%s', '%s', '%s', '%s')",
1459                                         app->appid, ss->src, ss->type, ss->orientation,
1460                                         ss->indicatordisplay);
1461                         ret = __exec_query(query);
1462                         if (ret == -1) {
1463                                 _LOGD("Package UiApp Splash Screen DB Insert Failed");
1464                                 return -1;
1465                         }
1466                         memset(query, '\0', MAX_QUERY_LEN);
1467                 }
1468                 ss = __find_splashscreen(app->splashscreens, "landscape");
1469                 if (ss) {
1470                         snprintf(query, sizeof(query),
1471                                         "insert into package_app_splash_screen" \
1472                                         "(app_id, src, type, orientation, indicatordisplay) " \
1473                                         "values('%s', '%s', '%s', '%s', '%s')",
1474                                         app->appid, ss->src, ss->type, ss->orientation,
1475                                         ss->indicatordisplay);
1476                         ret = __exec_query(query);
1477                         if (ret == -1) {
1478                                 _LOGD("Package UiApp Splash Screen DB Insert Failed");
1479                                 return -1;
1480                         }
1481                         memset(query, '\0', MAX_QUERY_LEN);
1482                 }
1483         }
1484         return 0;
1485 }
1486
1487 static int __insert_application_legacy_splashscreen_info(manifest_x *mfx)
1488 {
1489         GList *app_tmp;
1490         application_x *app;
1491         int ret = -1;
1492         char query[MAX_QUERY_LEN];
1493         char *tmp;
1494         const char *image_type;
1495         const char *indicatordisplay;
1496         const char *orientation;
1497
1498         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1499                 app = (application_x *)app_tmp->data;
1500                 if (app == NULL ||
1501                         (app->portraitimg == NULL && app->landscapeimg == NULL))
1502                         continue;
1503                 image_type = "img"; /* default */
1504                 if (app->effectimage_type) {
1505                         tmp = strstr(app->effectimage_type, "edj");
1506                         if (tmp)
1507                                 image_type = "edj";
1508                 }
1509                 indicatordisplay = "true"; /* default */
1510                 if (app->indicatordisplay)
1511                         indicatordisplay = app->indicatordisplay;
1512                 if (app->portraitimg) {
1513                         orientation = "portrait";
1514                         snprintf(query, sizeof(query),
1515                                         "insert into package_app_splash_screen" \
1516                                         "(app_id, src, type, orientation, indicatordisplay) " \
1517                                         "values('%s', '%s', '%s', '%s', '%s')",
1518                                         app->appid, app->portraitimg, image_type,
1519                                         orientation, indicatordisplay);
1520                         ret = __exec_query(query);
1521                         if (ret == -1) {
1522                                 _LOGD("Package UiApp Splash Screen DB Insert Failed");
1523                                 return -1;
1524                         }
1525                         memset(query, '\0', MAX_QUERY_LEN);
1526                 } else if (app->landscapeimg) {
1527                         orientation = "landscape";
1528                         snprintf(query, sizeof(query),
1529                                         "insert into package_app_splash_screen" \
1530                                         "(app_id, src, type, orientation, indicatordisplay) " \
1531                                         "values('%s', '%s', '%s', '%s', '%s')",
1532                                         app->appid, app->landscapeimg, image_type,
1533                                         orientation, indicatordisplay);
1534                         ret = __exec_query(query);
1535                         if (ret == -1) {
1536                                 _LOGD("Package UiApp Splash Screen DB Insert Failed");
1537                                 return -1;
1538                         }
1539                         memset(query, '\0', MAX_QUERY_LEN);
1540                 }
1541         }
1542         return 0;
1543 }
1544
1545 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1546 {
1547         GList *tmp;
1548         application_x *app;
1549         const char *pv = NULL;
1550         char query[MAX_QUERY_LEN] = { '\0' };
1551         int ret = -1;
1552         author_x *author;
1553         const char *auth_name = NULL;
1554         const char *auth_email = NULL;
1555         const char *auth_href = NULL;
1556
1557         GList *pkglocale = NULL;
1558         GList *applocale = NULL;
1559         GList *appicon = NULL;
1560         GList *appimage = NULL;
1561
1562         if (mfx->author && mfx->author->data) {
1563                 author = (author_x *)mfx->author->data;
1564                 if (author->text)
1565                         auth_name = author->text;
1566                 if (author->email)
1567                         auth_email = author->email;
1568                 if (author->href)
1569                         auth_href = author->href;
1570         }
1571         /*Insert in the package_cert_info CERT_DB*/
1572         pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1573         ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1574         if (ret != PMINFO_R_OK) {
1575                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1576                 _LOGE("Cert Info DB create handle failed\n");
1577                 return -1;
1578         }
1579         ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1580         if (ret != PMINFO_R_OK) {
1581                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1582                 _LOGE("Cert Info DB Insert Failed\n");
1583                 return -1;
1584         }
1585
1586         /*Insert in the package_info DB*/
1587         snprintf(query, MAX_QUERY_LEN,
1588                 "insert into package_info(" \
1589                 "package, package_type, package_version, package_api_version, package_tep_name, " \
1590                 "install_location, package_size, package_removable, package_preload, package_readonly, " \
1591                 "package_update, package_appsetting, package_nodisplay, package_system, author_name, " \
1592                 "author_email, author_href, installed_time, installed_storage, storeclient_id, " \
1593                 "mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1594                 "values(" \
1595                 "'%s', '%s', '%s', '%s', '%s', " \
1596                 "'%s', '%s', '%s', '%s', '%s', " \
1597                 "'%s', '%s', '%s', '%s', '%s', " \
1598                 "'%s', '%s', '%s', '%s', '%s', " \
1599                 "'%s', '%s', '%s', '%s', '%s')", \
1600                 mfx->package, mfx->type, mfx->version, __get_str(mfx->api_version), __get_str(mfx->tep_name),
1601                 __get_str(mfx->installlocation), __get_str(mfx->package_size), mfx->removable, mfx->preload, mfx->readonly,
1602                 mfx->update, mfx->appsetting, mfx->nodisplay_setting, mfx->system, __get_str(auth_name),
1603                 __get_str(auth_email), __get_str(auth_href), mfx->installed_time, mfx->installed_storage,
1604                 __get_str(mfx->storeclient_id),
1605                 mfx->mainapp_id, __get_str(mfx->package_url), mfx->root_path, __get_str(mfx->csc_path), mfx->support_disable);
1606
1607         ret = __exec_query(query);
1608         if (ret == -1) {
1609                 _LOGD("Package Info DB Insert Failed\n");
1610                 return -1;
1611         }
1612
1613         /*Insert in the package_privilege_info DB*/
1614         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1615                 pv = (const char *)tmp->data;
1616                 if (pv == NULL)
1617                         continue;
1618                 memset(query, '\0', MAX_QUERY_LEN);
1619                 snprintf(query, MAX_QUERY_LEN,
1620                         "insert into package_privilege_info(package, privilege) " \
1621                         "values('%s','%s')",\
1622                          mfx->package, pv);
1623                 ret = __exec_query(query);
1624                 if (ret == -1) {
1625                         _LOGD("Package Privilege Info DB Insert Failed\n");
1626                         return -1;
1627                 }
1628         }
1629
1630         if (mfx->application != NULL) {
1631                 ret = __insert_mainapp_info(mfx);
1632                 if (ret == -1)
1633                         return -1;
1634         }
1635
1636         /*Insert the package locale*/
1637         pkglocale = __create_locale_list(pkglocale, mfx->label, mfx->license, mfx->icon, mfx->description, mfx->author);
1638         /*remove duplicated data in pkglocale*/
1639         __trimfunc(pkglocale);
1640
1641         /*Insert the app locale, icon, image info */
1642         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1643                 app = (application_x *)tmp->data;
1644                 if (app == NULL)
1645                         continue;
1646                 applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL);
1647                 appicon = __create_icon_list(appicon, app->icon);
1648                 appimage = __create_image_list(appimage, app->image);
1649         }
1650         /*remove duplicated data in applocale*/
1651         __trimfunc(applocale);
1652         __trimfunc(appicon);
1653         __trimfunc(appimage);
1654
1655         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1656
1657         /*native app locale info*/
1658         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1659                 app = (application_x *)tmp->data;
1660                 if (app == NULL)
1661                         continue;
1662                 g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app);
1663                 g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app);
1664                 g_list_foreach(appimage, __insert_application_image_info, (gpointer)app);
1665         }
1666
1667         g_list_free(pkglocale);
1668         pkglocale = NULL;
1669         g_list_free(applocale);
1670         applocale = NULL;
1671         g_list_free(appicon);
1672         appicon = NULL;
1673         g_list_free(appimage);
1674         appimage = NULL;
1675
1676         /*Insert in the package_app_info DB*/
1677         ret = __insert_application_info(mfx);
1678         if (ret == -1)
1679                 return -1;
1680         /*Insert in the package_app_app_control DB*/
1681         ret = __insert_application_appcontrol_info(mfx);
1682         if (ret == -1)
1683                 return -1;
1684
1685         /*Insert in the package_app_app_category DB*/
1686         ret = __insert_application_appcategory_info(mfx);
1687         if (ret == -1)
1688                 return -1;
1689
1690         /*Insert in the package_app_app_metadata DB*/
1691         ret = __insert_application_appmetadata_info(mfx);
1692         if (ret == -1)
1693                 return -1;
1694
1695         /*Insert in the package_app_app_permission DB*/
1696         ret = __insert_application_apppermission_info(mfx);
1697         if (ret == -1)
1698                 return -1;
1699
1700         /*Insert in the package_app_share_allowed DB*/
1701         ret = __insert_application_share_allowed_info(mfx);
1702         if (ret == -1)
1703                 return -1;
1704
1705         /*Insert in the package_app_share_request DB*/
1706         ret = __insert_application_share_request_info(mfx);
1707         if (ret == -1)
1708                 return -1;
1709
1710         /*Insert in the package_app_data_control DB*/
1711         ret = __insert_application_datacontrol_info(mfx);
1712         if (ret == -1)
1713                 return -1;
1714
1715         /*Insert in the package_app_splash_screen DB*/
1716         ret = __insert_application_legacy_splashscreen_info(mfx);
1717         if (ret == -1)
1718                 return -1;
1719
1720         /*Insert in the package_app_splash_screen DB*/
1721         ret = __insert_application_splashscreen_info(mfx);
1722         if (ret == -1)
1723                 return -1;
1724
1725         return 0;
1726
1727 }
1728
1729 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1730 {
1731         char query[MAX_QUERY_LEN] = { '\0' };
1732         int ret = -1;
1733         memset(query, '\0', MAX_QUERY_LEN);
1734         snprintf(query, MAX_QUERY_LEN,
1735                  "delete from %s where app_id='%s'", db_table, appid);
1736         ret = __exec_query(query);
1737         if (ret == -1) {
1738                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1739                 return -1;
1740         }
1741         return 0;
1742 }
1743
1744 static int __delete_subpkg_info_from_db(char *appid)
1745 {
1746         int ret = -1;
1747
1748         ret = __delete_appinfo_from_db("package_app_info", appid);
1749         if (ret < 0)
1750                 return ret;
1751         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1752         if (ret < 0)
1753                 return ret;
1754         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1755         if (ret < 0)
1756                 return ret;
1757         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1758         if (ret < 0)
1759                 return ret;
1760         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1761         if (ret < 0)
1762                 return ret;
1763         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1764         if (ret < 0)
1765                 return ret;
1766         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1767         if (ret < 0)
1768                 return ret;
1769         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1770         if (ret < 0)
1771                 return ret;
1772         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1773         if (ret < 0)
1774                 return ret;
1775         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1776         if (ret < 0)
1777                 return ret;
1778         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1779         if (ret < 0)
1780                 return ret;
1781         ret = __delete_appinfo_from_db("package_app_splash_screen", appid);
1782         if (ret < 0)
1783                 return ret;
1784
1785         return 0;
1786 }
1787
1788 static int __delete_subpkg_from_db(manifest_x *mfx)
1789 {
1790         char query[MAX_QUERY_LEN] = { '\0' };
1791         char *error_message = NULL;
1792
1793         snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1794         if (SQLITE_OK !=
1795             sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1796                 _LOGE("Don't execute query = %s error message = %s\n", query,
1797                        error_message);
1798                 sqlite3_free(error_message);
1799                 return -1;
1800         }
1801         sqlite3_free(error_message);
1802
1803         return 0;
1804 }
1805
1806 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1807 {
1808         char query[MAX_QUERY_LEN] = { '\0' };
1809         int ret = -1;
1810         GList *tmp;
1811         application_x *app;
1812         /*Delete from cert table*/
1813         ret = pkgmgrinfo_delete_certinfo(mfx->package);
1814         if (ret) {
1815                 _LOGD("Cert Info  DB Delete Failed\n");
1816                 return -1;
1817         }
1818
1819         /*Delete from Package Info DB*/
1820         snprintf(query, MAX_QUERY_LEN,
1821                  "delete from package_info where package='%s'", mfx->package);
1822         ret = __exec_query(query);
1823         if (ret == -1) {
1824                 _LOGD("Package Info DB Delete Failed\n");
1825                 return -1;
1826         }
1827         memset(query, '\0', MAX_QUERY_LEN);
1828
1829         /*Delete from Package Localized Info*/
1830         snprintf(query, MAX_QUERY_LEN,
1831                  "delete from package_localized_info where package='%s'", mfx->package);
1832         ret = __exec_query(query);
1833         if (ret == -1) {
1834                 _LOGD("Package Localized Info DB Delete Failed\n");
1835                 return -1;
1836         }
1837
1838         /*Delete from Package Privilege Info*/
1839         snprintf(query, MAX_QUERY_LEN,
1840                  "delete from package_privilege_info where package='%s'", mfx->package);
1841         ret = __exec_query(query);
1842         if (ret == -1) {
1843                 _LOGD("Package Privilege Info DB Delete Failed\n");
1844                 return -1;
1845         }
1846
1847         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1848                 app = (application_x *)tmp->data;
1849                 if (app == NULL)
1850                         continue;
1851                 ret = __delete_appinfo_from_db("package_app_info", app->appid);
1852                 if (ret < 0)
1853                         return ret;
1854                 ret = __delete_appinfo_from_db("package_app_localized_info", app->appid);
1855                 if (ret < 0)
1856                         return ret;
1857                 ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid);
1858                 if (ret < 0)
1859                         return ret;
1860                 ret = __delete_appinfo_from_db("package_app_image_info", app->appid);
1861                 if (ret < 0)
1862                         return ret;
1863                 ret = __delete_appinfo_from_db("package_app_app_control", app->appid);
1864                 if (ret < 0)
1865                         return ret;
1866                 ret = __delete_appinfo_from_db("package_app_app_category", app->appid);
1867                 if (ret < 0)
1868                         return ret;
1869                 ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid);
1870                 if (ret < 0)
1871                         return ret;
1872                 ret = __delete_appinfo_from_db("package_app_app_permission", app->appid);
1873                 if (ret < 0)
1874                         return ret;
1875                 ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid);
1876                 if (ret < 0)
1877                         return ret;
1878                 ret = __delete_appinfo_from_db("package_app_share_request", app->appid);
1879                 if (ret < 0)
1880                         return ret;
1881                 ret = __delete_appinfo_from_db("package_app_data_control", app->appid);
1882                 if (ret < 0)
1883                         return ret;
1884                 ret = __delete_appinfo_from_db("package_app_splash_screen", app->appid);
1885                 if (ret < 0)
1886                         return ret;
1887         }
1888
1889         /* if main package has sub pkg, delete sub pkg data*/
1890         __delete_subpkg_from_db(mfx);
1891
1892         return 0;
1893 }
1894
1895 static int __disable_app(const char *appid)
1896 {
1897         int ret = -1;
1898         char query[MAX_QUERY_LEN] = {'\0'};
1899         sqlite3_snprintf(MAX_QUERY_LEN, query,
1900                         "UPDATE package_app_info set app_disable='true' where app_id=%Q",
1901                         appid);
1902         ret = __exec_query(query);
1903         if (ret == -1)
1904                 _LOGD("Insert global app disable failed\n");
1905
1906         return ret;
1907 }
1908
1909 static int __enable_app(const char *appid)
1910 {
1911         int ret = -1;
1912         char query[MAX_QUERY_LEN] = {'\0'};
1913         sqlite3_snprintf(MAX_QUERY_LEN, query,
1914                         "UPDATE package_app_info set app_disable='false' where app_id=%Q",
1915                         appid);
1916         ret = __exec_query(query);
1917         if (ret == -1)
1918                 _LOGD("Insert global app disable failed\n");
1919
1920         return ret;
1921 }
1922
1923 static int __disable_global_app_for_user(const char *appid, uid_t uid)
1924 {
1925         int ret = -1;
1926         char query[MAX_QUERY_LEN] = {'\0'};
1927
1928         sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO " \
1929                         "package_app_disable_for_user(app_id, uid) VALUES(%Q, '%d')",
1930                         appid, (int)uid);
1931         ret = __exec_query(query);
1932         if (ret == -1)
1933                 _LOGD("Insert global app disable failed\n");
1934
1935         return ret;
1936 }
1937
1938 static int __enable_global_app_for_user(const char *appid, uid_t uid)
1939 {
1940         int ret = -1;
1941         char query[MAX_QUERY_LEN] = {'\0'};
1942
1943         sqlite3_snprintf(MAX_QUERY_LEN, query, "DELETE FROM " \
1944                         "package_app_disable_for_user WHERE app_id=%Q AND uid='%d'",
1945                         appid, (int)uid);
1946         ret = __exec_query(query);
1947         if (ret == -1)
1948                 _LOGD("Delete global app disable failed\n");
1949
1950         return ret;
1951 }
1952
1953 static int __update_preload_condition_in_db()
1954 {
1955         int ret = -1;
1956         char query[MAX_QUERY_LEN] = {'\0'};
1957
1958         snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1959
1960         ret = __exec_query(query);
1961         if (ret == -1)
1962                 _LOGD("Package preload_condition update failed\n");
1963
1964         return ret;
1965 }
1966
1967 API int pkgmgr_parser_initialize_db(uid_t uid)
1968 {
1969         int ret = -1;
1970         int i;
1971         char query[MAX_QUERY_LEN];
1972         static const char *columns[] = {
1973                 "author_root_cert", "author_im_cert", "author_signer_cert",
1974                 "dist_root_cert", "dist_im_cert", "dist_signer_cert",
1975                 "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
1976                 NULL};
1977
1978         /*Manifest DB*/
1979         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1980         if (ret == -1) {
1981                 _LOGD("package info DB initialization failed\n");
1982                 return ret;
1983         }
1984         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1985         if (ret == -1) {
1986                 _LOGD("package localized info DB initialization failed\n");
1987                 return ret;
1988         }
1989         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1990         if (ret == -1) {
1991                 _LOGD("package app app privilege DB initialization failed\n");
1992                 return ret;
1993         }
1994         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1995         if (ret == -1) {
1996                 _LOGD("package app info DB initialization failed\n");
1997                 return ret;
1998         }
1999         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
2000         if (ret == -1) {
2001                 _LOGD("package app localized info DB initialization failed\n");
2002                 return ret;
2003         }
2004         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
2005         if (ret == -1) {
2006                 _LOGD("package app icon localized info DB initialization failed\n");
2007                 return ret;
2008         }
2009         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
2010         if (ret == -1) {
2011                 _LOGD("package app image info DB initialization failed\n");
2012                 return ret;
2013         }
2014         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
2015         if (ret == -1) {
2016                 _LOGD("package app app control DB initialization failed\n");
2017                 return ret;
2018         }
2019         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
2020         if (ret == -1) {
2021                 _LOGD("package app app category DB initialization failed\n");
2022                 return ret;
2023         }
2024         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
2025         if (ret == -1) {
2026                 _LOGD("package app app category DB initialization failed\n");
2027                 return ret;
2028         }
2029         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
2030         if (ret == -1) {
2031                 _LOGD("package app app permission DB initialization failed\n");
2032                 return ret;
2033         }
2034         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
2035         if (ret == -1) {
2036                 _LOGD("package app share allowed DB initialization failed\n");
2037                 return ret;
2038         }
2039         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
2040         if (ret == -1) {
2041                 _LOGD("package app share request DB initialization failed\n");
2042                 return ret;
2043         }
2044         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
2045         if (ret == -1) {
2046                 _LOGD("package app data control DB initialization failed\n");
2047                 return ret;
2048         }
2049
2050         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DISABLE_FOR_USER);
2051         if (ret == -1) {
2052                 _LOGD("package app disable for user DB initialization failed\n");
2053                 return ret;
2054         }
2055
2056         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN);
2057         if (ret == -1) {
2058                 _LOGD("package app splash screen DB initialization failed\n");
2059                 return ret;
2060         }
2061
2062         /*Cert DB*/
2063         /* TODO: refactor this code */
2064         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
2065         if (ret == -1) {
2066                 _LOGD("package cert info DB initialization failed\n");
2067                 return ret;
2068         }
2069         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
2070         if (ret == -1) {
2071                 _LOGD("package cert index info DB initialization failed\n");
2072                 return ret;
2073         }
2074         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO);
2075         if (ret == -1) {
2076                 _LOGD("package cert info DB initialization failed\n");
2077                 return ret;
2078         }
2079         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO);
2080         if (ret == -1) {
2081                 _LOGD("package cert index info DB initialization failed\n");
2082                 return ret;
2083         }
2084         for (i = 0; columns[i] != NULL; i++) {
2085                 snprintf(query, sizeof(query),
2086                                 QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
2087                                 columns[i], columns[i], columns[i]);
2088                 ret = __initialize_db(pkgmgr_cert_db, query);
2089                 if (ret == -1) {
2090                         _LOGD("package cert index info DB initialization failed\n");
2091                         return ret;
2092                 }
2093         }
2094
2095         if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(GLOBAL_USER), GLOBAL_USER)) {
2096                 _LOGD("Failed to change cert db permission\n");
2097         }
2098         if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
2099                 _LOGD("Failed to change parser db permission\n");
2100         }
2101
2102         return 0;
2103 }
2104
2105 static int __parserdb_change_perm(const char *db_file, uid_t uid)
2106 {
2107         char buf[BUFSIZE];
2108         char journal_file[BUFSIZE];
2109         char *files[3];
2110         int ret, i;
2111         struct passwd *userinfo = NULL;
2112         files[0] = (char *)db_file;
2113         files[1] = journal_file;
2114         files[2] = NULL;
2115         mode_t mode;
2116
2117         if (db_file == NULL)
2118                 return -1;
2119
2120         if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
2121                 return 0;
2122         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2123         if (uid == OWNER_ROOT)
2124                 uid = GLOBAL_USER;
2125         userinfo = getpwuid(uid);
2126         if (!userinfo) {
2127                 _LOGE("FAIL: user %d doesn't exist", uid);
2128                 return -1;
2129         }
2130         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2131
2132         for (i = 0; files[i]; i++) {
2133                 ret = chown(files[i], uid, userinfo->pw_gid);
2134                 if (ret == -1) {
2135                         if (strerror_r(errno, buf, sizeof(buf)))
2136                                 strcpy(buf, "");
2137                         _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
2138                                         userinfo->pw_gid, buf);
2139                         return -1;
2140                 }
2141
2142                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
2143                 if (!strcmp(db_file, getUserPkgCertDBPathUID(GLOBAL_USER)))
2144                         mode |= S_IWOTH;
2145                 ret = chmod(files[i], mode);
2146                 if (ret == -1) {
2147                         if (strerror_r(errno, buf, sizeof(buf)))
2148                                 strcpy(buf, "");
2149                         _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
2150                         return -1;
2151                 }
2152                 SET_SMACK_LABEL(files[i]);
2153         }
2154         return 0;
2155 }
2156
2157 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
2158 {
2159         int ret;
2160
2161         if (getuid() != OWNER_ROOT) {
2162                 _LOGE("Only root user is allowed");
2163                 return -1;
2164         }
2165
2166         if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
2167                 _LOGE("Manifest db for user %d is already exists", uid);
2168                 return -1;
2169         }
2170
2171         if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
2172                 _LOGE("Cert db for user %d is already exists", uid);
2173                 return -1;
2174         }
2175
2176         ret = pkgmgr_parser_check_and_create_db(uid);
2177         if (ret < 0)
2178                 return -1;
2179         ret = pkgmgr_parser_initialize_db(uid);
2180         if (ret < 0) {
2181                 pkgmgr_parser_close_db();
2182                 return -1;
2183         }
2184         pkgmgr_parser_close_db();
2185
2186         return 0;
2187 }
2188
2189 API int pkgmgr_parser_check_and_create_db(uid_t uid)
2190 {
2191         int ret = -1;
2192         /*Manifest DB*/
2193         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
2194         if (ret) {
2195                 _LOGD("Manifest DB creation Failed\n");
2196                 return -1;
2197         }
2198
2199         /*Cert DB*/
2200         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(GLOBAL_USER));
2201         if (ret) {
2202                 _LOGD("Cert DB creation Failed\n");
2203                 return -1;
2204         }
2205         return 0;
2206 }
2207
2208 void pkgmgr_parser_close_db(void)
2209 {
2210         sqlite3_close(pkgmgr_parser_db);
2211         sqlite3_close(pkgmgr_cert_db);
2212 }
2213
2214
2215 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
2216 {
2217         _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
2218         if (mfx == NULL) {
2219                 _LOGD("manifest pointer is NULL\n");
2220                 return -1;
2221         }
2222         int ret = 0;
2223         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2224         if (ret == -1) {
2225                 _LOGD("Failed to open DB\n");
2226                 return ret;
2227         }
2228         ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
2229         if (ret == -1)
2230                 goto err;
2231         /*Begin transaction*/
2232         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2233         if (ret != SQLITE_OK) {
2234                 _LOGD("Failed to begin transaction\n");
2235                 ret = -1;
2236                 goto err;
2237         }
2238         _LOGD("Transaction Begin\n");
2239         ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
2240         if (ret == -1) {
2241                 _LOGD("Insert into DB failed. Rollback now\n");
2242                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2243                 goto err;
2244         }
2245         /*Commit transaction*/
2246         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2247         if (ret != SQLITE_OK) {
2248                 _LOGD("Failed to commit transaction. Rollback now\n");
2249                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2250                 ret = -1;
2251                 goto err;
2252         }
2253         _LOGD("Transaction Commit and End\n");
2254 err:
2255         pkgmgr_parser_close_db();
2256         return ret;
2257 }
2258
2259 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2260 {
2261         _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
2262         if (mfx == NULL) {
2263                 _LOGD("manifest pointer is NULL\n");
2264                 return -1;
2265         }
2266         int ret = 0;
2267         ret = pkgmgr_parser_check_and_create_db(uid);
2268         if (ret == -1) {
2269                 _LOGD("Failed to open DB\n");
2270                 return ret;
2271         }
2272         ret = pkgmgr_parser_initialize_db(uid);
2273         if (ret == -1)
2274                 goto err;
2275         /*Begin transaction*/
2276         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2277         if (ret != SQLITE_OK) {
2278                 _LOGD("Failed to begin transaction\n");
2279                 ret = -1;
2280                 goto err;
2281         }
2282         _LOGD("Transaction Begin\n");
2283         ret = __insert_manifest_info_in_db(mfx, uid);
2284         if (ret == -1) {
2285                 _LOGD("Insert into DB failed. Rollback now\n");
2286                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2287                 goto err;
2288         }
2289         /*Commit transaction*/
2290         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2291         if (ret != SQLITE_OK) {
2292                 _LOGD("Failed to commit transaction. Rollback now\n");
2293                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2294                 ret = -1;
2295                 goto err;
2296         }
2297         _LOGD("Transaction Commit and End\n");
2298 err:
2299         pkgmgr_parser_close_db();
2300         return ret;
2301 }
2302
2303 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, const char *tep_path)
2304 {
2305         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, GLOBAL_USER);
2306 }
2307
2308 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, const char *tep_path, uid_t uid)
2309 {
2310         if (pkgid == NULL || tep_path == NULL) {
2311                 _LOGE("invalid parameter");
2312                 return -1;
2313         }
2314
2315         int ret = -1;
2316         char *query = NULL;
2317
2318         ret = pkgmgr_parser_check_and_create_db(uid);
2319         if (ret == -1) {
2320                 _LOGD("Failed to open DB\n");
2321                 return ret;
2322         }
2323         ret = pkgmgr_parser_initialize_db(uid);
2324         if (ret == -1)
2325                 goto err;
2326
2327         /*Begin transaction*/
2328         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2329         if (ret != SQLITE_OK) {
2330                 _LOGD("Failed to begin transaction\n");
2331                 ret = -1;
2332                 goto err;
2333         }
2334         _LOGD("Transaction Begin\n");
2335
2336
2337         /* Updating TEP info in "package_info" table */
2338         query = sqlite3_mprintf("UPDATE package_info "\
2339                                                 "SET package_tep_name = %Q "\
2340                                                 "WHERE package = %Q", tep_path, pkgid);
2341
2342         ret = __exec_query(query);
2343         sqlite3_free(query);
2344         if (ret != SQLITE_OK) {
2345                 ret = PM_PARSER_R_ERROR;
2346                 _LOGE("sqlite exec failed to insert entries into package_info!!");
2347                 goto err;
2348         }
2349
2350         /* Updating TEP info in "package_app_info" table */
2351         query = sqlite3_mprintf("UPDATE package_app_info "\
2352                                                 "SET app_tep_name = %Q "\
2353                                                 "WHERE package = %Q", tep_path, pkgid);
2354
2355         ret = __exec_query(query);
2356         sqlite3_free(query);
2357         if (ret != SQLITE_OK) {
2358                 ret = PM_PARSER_R_ERROR;
2359                 _LOGE("sqlite exec failed to insert entries into package_app_info!!");
2360                 goto err;
2361         }
2362
2363         /*Commit transaction*/
2364         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2365         if (ret != SQLITE_OK) {
2366                 _LOGE("Failed to commit transaction, Rollback now\n");
2367                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2368                 if (ret != SQLITE_OK)
2369                         _LOGE("Failed to Rollback\n");
2370
2371                 ret = PM_PARSER_R_ERROR;
2372                 goto err;
2373         }
2374         _LOGD("Transaction Commit and End\n");
2375         ret =  PM_PARSER_R_OK;
2376
2377 err:
2378         pkgmgr_parser_close_db();
2379         return ret;
2380 }
2381
2382
2383 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2384 {
2385         if (mfx == NULL) {
2386                 _LOGD("manifest pointer is NULL\n");
2387                 return -1;
2388         }
2389         int ret = 0;
2390         ret = pkgmgr_parser_check_and_create_db(uid);
2391         if (ret == -1) {
2392                 _LOGD("Failed to open DB\n");
2393                 return ret;
2394         }
2395         ret = pkgmgr_parser_initialize_db(uid);
2396         if (ret == -1)
2397                 goto err;
2398         /*Preserve guest mode visibility*/
2399         __preserve_guestmode_visibility_value( mfx);
2400         /*Begin transaction*/
2401         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2402         if (ret != SQLITE_OK) {
2403                 _LOGD("Failed to begin transaction\n");
2404                 ret = -1;
2405                 goto err;
2406         }
2407         _LOGD("Transaction Begin\n");
2408         ret = __delete_manifest_info_from_db(mfx, uid);
2409         if (ret == -1) {
2410                 _LOGD("Delete from DB failed. Rollback now\n");
2411                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2412                 goto err;
2413         }
2414         ret = __insert_manifest_info_in_db(mfx, uid);
2415         if (ret == -1) {
2416                 _LOGD("Insert into DB failed. Rollback now\n");
2417                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2418                 goto err;
2419         }
2420
2421         /*Commit transaction*/
2422         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2423         if (ret != SQLITE_OK) {
2424                 _LOGD("Failed to commit transaction. Rollback now\n");
2425                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2426                 ret = -1;
2427                 goto err;
2428         }
2429         _LOGD("Transaction Commit and End\n");
2430 err:
2431         pkgmgr_parser_close_db();
2432         return ret;
2433 }
2434
2435 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2436 {
2437         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
2438 }
2439
2440 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
2441 {
2442         if (mfx == NULL) {
2443                 _LOGD("manifest pointer is NULL\n");
2444                 return -1;
2445         }
2446         int ret = 0;
2447         ret = pkgmgr_parser_check_and_create_db(uid);
2448         if (ret == -1) {
2449                 _LOGD("Failed to open DB\n");
2450                 return ret;
2451         }
2452         /*Begin transaction*/
2453         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2454         if (ret != SQLITE_OK) {
2455                 _LOGD("Failed to begin transaction\n");
2456                 ret = -1;
2457                 goto err;
2458         }
2459         _LOGD("Transaction Begin\n");
2460         ret = __delete_manifest_info_from_db(mfx, uid);
2461         if (ret == -1) {
2462                 _LOGD("Delete from DB failed. Rollback now\n");
2463                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2464                 goto err;
2465         }
2466         /*Commit transaction*/
2467         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2468         if (ret != SQLITE_OK) {
2469                 _LOGD("Failed to commit transaction, Rollback now\n");
2470                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2471                 ret = -1;
2472                 goto err;
2473         }
2474         _LOGD("Transaction Commit and End\n");
2475 err:
2476         pkgmgr_parser_close_db();
2477         return ret;
2478 }
2479
2480 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2481 {
2482         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
2483 }
2484
2485 API int pkgmgr_parser_update_preload_info_in_db()
2486 {
2487         int ret = 0;
2488         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2489         if (ret == -1) {
2490                 _LOGD("Failed to open DB\n");
2491                 return ret;
2492         }
2493         /*Begin transaction*/
2494         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2495         if (ret != SQLITE_OK) {
2496                 _LOGD("Failed to begin transaction\n");
2497                 ret = -1;
2498                 goto err;
2499         }
2500         _LOGD("Transaction Begin\n");
2501         ret = __update_preload_condition_in_db();
2502         if (ret == -1) {
2503                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2504                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2505                 goto err;
2506         }
2507         /*Commit transaction*/
2508         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2509         if (ret != SQLITE_OK) {
2510                 _LOGD("Failed to commit transaction, Rollback now\n");
2511                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2512                 ret = -1;
2513                 goto err;
2514         }
2515         _LOGD("Transaction Commit and End\n");
2516 err:
2517         pkgmgr_parser_close_db();
2518         return ret;
2519 }
2520
2521 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
2522 {
2523         int ret = 0;
2524         ret = pkgmgr_parser_check_and_create_db(uid);
2525         if (ret == -1) {
2526                 _LOGD("Failed to open DB\n");
2527                 return ret;
2528         }
2529         /*Begin transaction*/
2530         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2531         if (ret != SQLITE_OK) {
2532                 _LOGD("Failed to begin transaction\n");
2533                 ret = -1;
2534                 goto err;
2535         }
2536         _LOGD("Transaction Begin\n");
2537         ret = __update_preload_condition_in_db();
2538         if (ret == -1) {
2539                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2540                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2541                 goto err;
2542         }
2543         /*Commit transaction*/
2544         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2545         if (ret != SQLITE_OK) {
2546                 _LOGD("Failed to commit transaction, Rollback now\n");
2547                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2548                 ret = -1;
2549                 goto err;
2550         }
2551         _LOGD("Transaction Commit and End\n");
2552 err:
2553         pkgmgr_parser_close_db();
2554         return ret;
2555 }
2556
2557 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(const char *appid, uid_t uid, int is_disable)
2558 {
2559         int ret = -1;
2560
2561         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2562         if (ret == -1) {
2563                 _LOGD("Failed to open DB\n");
2564                 return ret;
2565         }
2566
2567         /*Begin transaction*/
2568         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2569         if (ret != SQLITE_OK) {
2570                 _LOGD("Failed to begin transaction\n");
2571                 ret = -1;
2572                 goto err;
2573         }
2574         _LOGD("Transaction Begin\n");
2575         if (is_disable)
2576                 ret = __disable_global_app_for_user(appid, uid);
2577         else
2578                 ret = __enable_global_app_for_user(appid, uid);
2579         if (ret == -1) {
2580                 _LOGD("__update_global_app_disable_condition_in_db failed. Rollback now\n");
2581                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2582                 goto err;
2583         }
2584         /*Commit transaction*/
2585         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2586         if (ret != SQLITE_OK) {
2587                 _LOGD("Failed to commit transaction, Rollback now\n");
2588                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2589                 ret = -1;
2590                 goto err;
2591         }
2592         _LOGD("Transaction Commit and End\n");
2593 err:
2594         pkgmgr_parser_close_db();
2595         return ret;
2596
2597 }
2598
2599 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid, int is_disable)
2600 {
2601         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid, GLOBAL_USER, is_disable);
2602 }
2603
2604 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid, uid_t uid, int is_disable)
2605 {
2606         int ret = -1;
2607
2608         ret = pkgmgr_parser_check_and_create_db(uid);
2609         if (ret == -1) {
2610                 _LOGD("Failed to open DB\n");
2611                 return ret;
2612         }
2613
2614         /*Begin transaction*/
2615         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2616         if (ret != SQLITE_OK) {
2617                 _LOGD("Failed to begin transaction\n");
2618                 ret = -1;
2619                 goto err;
2620         }
2621         _LOGD("Transaction Begin\n");
2622         if (is_disable)
2623                 ret = __disable_app(appid);
2624         else
2625                 ret = __enable_app(appid);
2626         if (ret == -1) {
2627                 _LOGD("__update_app_disable_condition_in_db failed. Rollback now\n");
2628                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2629                 goto err;
2630         }
2631         /*Commit transaction*/
2632         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2633         if (ret != SQLITE_OK) {
2634                 _LOGD("Failed to commit transaction, Rollback now\n");
2635                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2636                 ret = -1;
2637                 goto err;
2638         }
2639         _LOGD("Transaction Commit and End\n");
2640 err:
2641         pkgmgr_parser_close_db();
2642         return ret;
2643
2644 }