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