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