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