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