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