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