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