4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
27 #include <sys/smack.h>
35 /* For multi-user support */
36 #include <tzplatform_config.h>
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"
47 #define LOG_TAG "PKGMGR_PARSER"
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
55 #define DB_LABEL "User::Home"
56 #define SET_SMACK_LABEL(x) \
58 if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
59 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
61 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
64 sqlite3 *pkgmgr_parser_db;
65 sqlite3 *pkgmgr_cert_db;
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 "install_location text, " \
74 "package_size text, " \
75 "package_removable text DEFAULT 'true', " \
76 "package_preload text DEFAULT 'false', " \
77 "package_readonly text DEFAULT 'false', " \
78 "package_update text DEFAULT 'false', " \
79 "package_appsetting text DEFAULT 'false', " \
80 "package_nodisplay text DEFAULT 'false', " \
81 "package_system text DEFAULT 'false', " \
82 "author_name text, " \
83 "author_email text, " \
85 "installed_time text," \
86 "installed_storage text," \
87 "storeclient_id text," \
92 "package_support_disable text DEFAULT 'false')"
94 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
95 "(package text not null, " \
96 "package_locale text DEFAULT 'No Locale', " \
97 "package_label text, " \
98 "package_icon text, " \
99 "package_description text, " \
100 "package_license text, " \
102 "PRIMARY KEY(package, package_locale), " \
103 "FOREIGN KEY(package) " \
104 "REFERENCES package_info(package) " \
107 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
108 "(package text not null, " \
109 "privilege text not null, " \
110 "PRIMARY KEY(package, privilege) " \
111 "FOREIGN KEY(package) " \
112 "REFERENCES package_info(package) " \
115 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
116 "(app_id text primary key not null, " \
117 "app_component text, " \
119 "app_nodisplay text DEFAULT 'false', " \
121 "app_onboot text DEFAULT 'false', " \
122 "app_multiple text DEFAULT 'false', " \
123 "app_autorestart text DEFAULT 'false', " \
124 "app_taskmanage text DEFAULT 'false', " \
125 "app_enabled text DEFAULT 'true', " \
126 "app_hwacceleration text DEFAULT 'use-system-setting', " \
127 "app_screenreader text DEFAULT 'use-system-setting', " \
128 "app_mainapp text, " \
129 "app_recentimage text, " \
130 "app_launchcondition text, " \
131 "app_indicatordisplay text DEFAULT 'true', " \
132 "app_portraitimg text, " \
133 "app_landscapeimg text, " \
134 "app_guestmodevisibility text DEFAULT 'true', " \
135 "app_permissiontype text DEFAULT 'normal', " \
136 "app_preload text DEFAULT 'false', " \
137 "app_submode text DEFAULT 'false', " \
138 "app_submode_mainid text, " \
139 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
140 "app_ui_gadget text DEFAULT 'false', " \
141 "app_support_disable text DEFAULT 'false', " \
142 "component_type text, " \
143 "package text not null, " \
144 "FOREIGN KEY(package) " \
145 "REFERENCES package_info(package) " \
148 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
149 "(app_id text not null, " \
150 "app_locale text DEFAULT 'No Locale', " \
153 "PRIMARY KEY(app_id,app_locale) " \
154 "FOREIGN KEY(app_id) " \
155 "REFERENCES package_app_info(app_id) " \
158 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
159 "(app_id text not null, " \
161 "app_icon_section text, " \
162 "app_icon_resolution text, " \
163 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
164 "FOREIGN KEY(app_id) " \
165 "REFERENCES package_app_info(app_id) " \
168 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
169 "(app_id text not null, " \
170 "app_locale text DEFAULT 'No Locale', " \
171 "app_image_section text, " \
173 "PRIMARY KEY(app_id,app_image_section) " \
174 "FOREIGN KEY(app_id) " \
175 "REFERENCES package_app_info(app_id) " \
178 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
179 "(app_id text not null, " \
180 "app_control text not null, " \
181 "PRIMARY KEY(app_id,app_control) " \
182 "FOREIGN KEY(app_id) " \
183 "REFERENCES package_app_info(app_id) " \
186 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
187 "(app_id text not null, " \
188 "operation text not null, " \
189 "uri_scheme text, " \
191 "subapp_name text, " \
192 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
193 "FOREIGN KEY(app_id) " \
194 "REFERENCES package_app_info(app_id) " \
197 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
198 "(app_id text not null, " \
199 "category text not null, " \
200 "PRIMARY KEY(app_id,category) " \
201 "FOREIGN KEY(app_id) " \
202 "REFERENCES package_app_info(app_id) " \
205 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
206 "(app_id text not null, " \
207 "md_key text not null, " \
208 "md_value text not null, " \
209 "PRIMARY KEY(app_id, md_key, md_value) " \
210 "FOREIGN KEY(app_id) " \
211 "REFERENCES package_app_info(app_id) " \
214 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
215 "(app_id text not null, " \
216 "pm_type text not null, " \
217 "pm_value text not null, " \
218 "PRIMARY KEY(app_id, pm_type, pm_value) " \
219 "FOREIGN KEY(app_id) " \
220 "REFERENCES package_app_info(app_id) " \
223 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
224 "(app_id text not null, " \
225 "data_share_path text not null, " \
226 "data_share_allowed text not null, " \
227 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
228 "FOREIGN KEY(app_id) " \
229 "REFERENCES package_app_info(app_id) " \
232 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
233 "(app_id text not null, " \
234 "data_share_request text not null, " \
235 "PRIMARY KEY(app_id,data_share_request) " \
236 "FOREIGN KEY(app_id) " \
237 "REFERENCES package_app_info(app_id) " \
240 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
241 "(cert_info text not null, " \
242 "cert_id integer, " \
243 "cert_ref_count integer, " \
244 "PRIMARY KEY(cert_id)) "
246 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
247 "(package text not null, " \
248 "author_root_cert integer, " \
249 "author_im_cert integer, " \
250 "author_signer_cert integer, " \
251 "dist_root_cert integer, " \
252 "dist_im_cert integer, " \
253 "dist_signer_cert integer, " \
254 "dist2_root_cert integer, " \
255 "dist2_im_cert integer, " \
256 "dist2_signer_cert integer, " \
257 "PRIMARY KEY(package)) "
259 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
260 "(app_id text not null, " \
261 "providerid text not null, " \
262 "access text not null, " \
263 "type text not null, " \
264 "PRIMARY KEY(app_id, providerid, access, type) " \
265 "FOREIGN KEY(app_id) " \
266 "REFERENCES package_app_info(app_id) " \
269 static int __insert_application_info(manifest_x *mfx);
270 static int __insert_application_appsvc_info(manifest_x *mfx);
271 static int __insert_application_appcategory_info(manifest_x *mfx);
272 static int __insert_application_appcontrol_info(manifest_x *mfx);
273 static int __insert_application_appmetadata_info(manifest_x *mfx);
274 static int __insert_application_share_allowed_info(manifest_x *mfx);
275 static int __insert_application_share_request_info(manifest_x *mfx);
276 static int __insert_application_datacontrol_info(manifest_x *mfx);
277 static void __insert_application_locale_info(gpointer data, gpointer userdata);
278 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
279 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
280 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
281 static int __delete_subpkg_info_from_db(char *appid);
282 static int __delete_appinfo_from_db(char *db_table, const char *appid);
283 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
284 static int __exec_query(char *query);
285 static void __extract_data(gpointer data, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath,
286 char **label, char **license, char **icon, char **description, char **author);
287 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
288 static GList *__create_locale_list(GList *locale, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath);
289 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
290 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
291 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
292 static int __parserdb_change_perm(const char *db_file, uid_t uid);
294 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
297 __delete_subpkg_info_from_db(coltxt[0]);
302 static const char *__get_str(const char *str)
306 return PKGMGR_PARSER_EMPTY_STR;
312 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
317 ret = db_util_open(db_path, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
318 if (ret != SQLITE_OK) {
319 _LOGD("connect db [%s] failed!\n", db_path);
327 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
329 manifest_x *mfx = (manifest_x *)data;
335 if (mfx->application == NULL)
337 app = (application_x *)mfx->application->data;
338 for(i = 0; i < ncols; i++)
340 if (strcmp(colname[i], "app_id") == 0) {
342 appid = strdup(coltxt[i]);
343 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
345 status = strdup(coltxt[i]);
349 _LOGD("app id is NULL\n");
352 /*update guest mode visibility*/
353 for (tmp = mfx->application; tmp; tmp = tmp->next) {
354 app = (application_x *)tmp->data;
357 if (strcmp(app->appid, appid) == 0) {
358 free((void *)app->guestmode_visibility);
359 app->guestmode_visibility = strdup(status);
375 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
377 char *error_message = NULL;
378 char query[MAX_QUERY_LEN] = {'\0'};
379 snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
381 sqlite3_exec(pkgmgr_parser_db, query,
382 __guestmode_visibility_cb, (void *)mfx, &error_message)) {
383 _LOGD("Don't execute query = %s error message = %s\n",
384 query, error_message);
385 sqlite3_free(error_message);
390 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
392 char *error_message = NULL;
394 sqlite3_exec(db_handle, db_query,
395 NULL, NULL, &error_message)) {
396 _LOGD("Don't execute query = %s error message = %s\n",
397 db_query, error_message);
398 sqlite3_free(error_message);
401 sqlite3_free(error_message);
405 static int __exec_query(char *query)
407 char *error_message = NULL;
409 sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
410 _LOGD("Don't execute query = %s error message = %s\n", query,
412 sqlite3_free(error_message);
415 sqlite3_free(error_message);
419 static int __exec_query_no_msg(char *query)
421 char *error_message = NULL;
423 sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
424 sqlite3_free(error_message);
427 sqlite3_free(error_message);
431 static GList *__create_locale_list(GList *locale, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths)
439 for (tmp = lbls; tmp; tmp = tmp->next) {
440 lbl = (label_x *)tmp->data;
444 locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
446 for (tmp = lcns; tmp; tmp = tmp->next) {
447 lcn = (license_x *)tmp->data;
451 locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
453 for (tmp = icns; tmp; tmp = tmp->next) {
454 icn = (icon_x *)tmp->data;
458 locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
460 for (tmp = dcns; tmp; tmp = tmp->next) {
461 dcn = (description_x *)tmp->data;
465 locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
467 for (tmp = aths; tmp; tmp = tmp->next) {
468 ath = (author_x *)tmp->data;
472 locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
478 static GList *__create_icon_list(GList *locale, GList *icns)
483 for (tmp = icns; tmp; tmp = tmp->next) {
484 icn = (icon_x *)tmp->data;
488 locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
493 static GList *__create_image_list(GList *locale, GList *imgs)
498 for (tmp = imgs; tmp; tmp = tmp->next) {
499 img = (image_x *)tmp->data;
503 locale = g_list_insert_sorted_with_data(locale, (gpointer)img->section, __comparefunc, NULL);
508 static void __trimfunc(GList* trim_list)
510 char *trim_data = NULL;
514 list = g_list_first(trim_list);
517 trim_data = (char *)list->data;
520 if (strcmp(trim_data, prev) == 0) {
521 trim_list = g_list_remove(trim_list, trim_data);
522 list = g_list_first(trim_list);
531 list = g_list_next(list);
535 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
537 if (a == NULL || b == NULL)
539 if (strcmp((char*)a, (char*)b) == 0)
541 if (strcmp((char*)a, (char*)b) < 0)
543 if (strcmp((char*)a, (char*)b) > 0)
548 static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths,
549 char **label, char **license, char **icon, char **description, char **author)
557 for (tmp = lbls; tmp; tmp = tmp->next) {
558 lbl = (label_x *)tmp->data;
562 if (strcmp(lbl->lang, (char *)data) == 0) {
563 *label = (char*)lbl->text;
568 for (tmp = lcns; tmp; tmp = tmp->next) {
569 lcn = (license_x *)tmp->data;
573 if (strcmp(lcn->lang, (char *)data) == 0) {
574 *license = (char*)lcn->text;
579 for (tmp = icns; tmp; tmp = tmp->next) {
580 icn = (icon_x *)tmp->data;
584 if (strcmp(icn->lang, (char *)data) == 0) {
585 *icon = (char*)icn->text;
590 for (tmp = dcns; tmp; tmp = tmp->next) {
591 dcn = (description_x *)tmp->data;
595 if (strcmp(dcn->lang, (char *)data) == 0) {
596 *description = (char*)dcn->text;
601 for (tmp = aths; tmp; tmp = tmp->next) {
602 ath = (author_x *)tmp->data;
606 if (strcmp(ath->lang, (char *)data) == 0) {
607 *author = (char*)ath->text;
615 static void __extract_icon_data(gpointer data, GList *icns, char **icon, char **resolution)
619 for (tmp = icns; tmp; tmp = tmp->next) {
620 icn = (icon_x *)tmp->data;
624 if (strcmp(icn->section, (char *)data) == 0) {
625 *icon = (char*)icn->text;
626 *resolution = (char*)icn->resolution;
633 static void __extract_image_data(gpointer data, GList *imgs, char **lang, char **image)
637 for (tmp = imgs; tmp; tmp = tmp->next) {
638 img = (image_x *)tmp->data;
642 if (strcmp(img->section, (char *)data) == 0) {
643 *lang = (char*)img->lang;
644 *image = (char*)img->text;
651 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
656 char *description = NULL;
657 char *license = NULL;
659 char query[MAX_QUERY_LEN] = {'\0'};
661 manifest_x *mfx = (manifest_x *)userdata;
662 GList *lbl = mfx->label;
663 GList *lcn = mfx->license;
664 GList *icn = mfx->icon;
665 GList *dcn = mfx->description;
666 GList *ath = mfx->author;
668 __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
669 if (!label && !description && !icon && !license && !author)
672 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
673 "package_label, package_icon, package_description, package_license, package_author) values " \
674 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
679 __get_str(description),
683 ret = __exec_query(query);
685 _LOGD("Package Localized Info DB Insert failed\n");
688 static void __insert_application_locale_info(gpointer data, gpointer userdata)
693 char query[MAX_QUERY_LEN] = {'\0'};
695 application_x *app = (application_x*)userdata;
696 GList *lbl = app->label;
697 GList *icn = app->icon;
699 __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
702 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
703 "app_label, app_icon) values " \
704 "('%q', '%q', '%q', '%q')", app->appid, (char*)data,
706 ret = __exec_query(query);
708 _LOGD("Package UiApp Localized Info DB Insert failed\n");
710 /*insert ui app locale info to pkg locale to get mainapp data */
711 if (strcasecmp(app->mainapp, "true")==0) {
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', '%q', '%q', '%q')",
719 PKGMGR_PARSER_EMPTY_STR,
720 PKGMGR_PARSER_EMPTY_STR,
721 PKGMGR_PARSER_EMPTY_STR);
723 ret = __exec_query_no_msg(query);
726 sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
727 "where package='%s' and package_locale='%s'", icon, app->package, (char*)data);
728 ret = __exec_query_no_msg(query);
733 static void __insert_application_icon_section_info(gpointer data, gpointer userdata)
737 char *resolution = NULL;
738 char query[MAX_QUERY_LEN] = {'\0'};
740 application_x *app = (application_x*)userdata;
741 GList *icn = app->icon;
743 __extract_icon_data(data, icn, &icon, &resolution);
744 if (!icon && !resolution)
746 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
747 "app_icon, app_icon_section, app_icon_resolution) values " \
748 "('%q', '%q', '%q', '%q')", app->appid,
749 icon, (char*)data, resolution);
751 ret = __exec_query(query);
753 _LOGD("Package UiApp Localized Info DB Insert failed\n");
757 static void __insert_application_image_info(gpointer data, gpointer userdata)
762 char query[MAX_QUERY_LEN] = {'\0'};
764 application_x *app = (application_x*)userdata;
765 GList *image = app->image;
767 __extract_image_data(data, image, &lang, &img);
770 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
771 "app_image_section, app_image) values " \
772 "('%q', '%q', '%q', '%q')", app->appid, lang, (char*)data, img);
774 ret = __exec_query(query);
776 _LOGD("Package UiApp image Info DB Insert failed\n");
781 static int __insert_mainapp_info(manifest_x *mfx)
786 char query[MAX_QUERY_LEN] = {'\0'};
787 for (tmp = mfx->application; tmp; tmp = tmp->next) {
788 app = (application_x *)tmp->data;
791 snprintf(query, MAX_QUERY_LEN,
792 "update package_app_info set app_mainapp='%s' where app_id='%s'", app->mainapp, app->appid);
794 ret = __exec_query(query);
796 _LOGD("Package App Info DB Insert Failed\n");
799 if (strcasecmp(app->mainapp, "True")==0)
800 mfx->mainapp_id = strdup(app->appid);
803 if (mfx->mainapp_id == NULL) {
804 if (mfx->application == NULL)
806 app = (application_x *)mfx->application->data;
810 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", app->appid);
812 _LOGD("Not valid appid\n");
816 ret = __exec_query(query);
818 _LOGD("Package UiApp Info DB Insert Failed\n");
822 free((void *)app->mainapp);
823 app->mainapp= strdup("true");
824 mfx->mainapp_id = strdup(app->appid);
827 memset(query, '\0', MAX_QUERY_LEN);
828 snprintf(query, MAX_QUERY_LEN,
829 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
830 ret = __exec_query(query);
832 _LOGD("Package Info DB update Failed\n");
838 /* _PRODUCT_LAUNCHING_ENHANCED_
839 * app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
841 static int __insert_application_info(manifest_x *mfx)
846 char query[MAX_QUERY_LEN] = {'\0'};
847 for (tmp = mfx->application; tmp; tmp = tmp->next) {
848 app = (application_x *)tmp->data;
851 snprintf(query, MAX_QUERY_LEN,
852 "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
853 "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
854 "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
855 "app_preload, app_submode, app_submode_mainid, app_launch_mode, app_ui_gadget, app_support_disable, component_type, package) " \
856 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
870 __get_str(app->recentimage),
871 app->launchcondition,
872 app->indicatordisplay,
873 __get_str(app->portraitimg),
874 __get_str(app->landscapeimg),
875 app->guestmode_visibility,
876 app->permission_type,
879 __get_str(app->submode_mainid),
882 mfx->support_disable,
886 ret = __exec_query(query);
888 _LOGD("Package UiApp Info DB Insert Failed\n");
891 memset(query, '\0', MAX_QUERY_LEN);
896 static int __insert_application_appcategory_info(manifest_x *mfx)
903 char query[MAX_QUERY_LEN] = {'\0'};
904 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
905 app = (application_x *)app_tmp->data;
908 for (ct_tmp = app->category; ct_tmp; ct_tmp = ct_tmp->next) {
909 ct = (const char *)ct_tmp->data;
912 snprintf(query, MAX_QUERY_LEN,
913 "insert into package_app_app_category(app_id, category) " \
914 "values('%s','%s')",\
916 ret = __exec_query(query);
918 _LOGD("Package UiApp Category Info DB Insert Failed\n");
921 memset(query, '\0', MAX_QUERY_LEN);
927 static int __insert_application_appmetadata_info(manifest_x *mfx)
934 char query[MAX_QUERY_LEN] = {'\0'};
935 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
936 app = (application_x *)app_tmp->data;
939 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
940 md = (metadata_x *)md_tmp->data;
944 snprintf(query, MAX_QUERY_LEN,
945 "insert into package_app_app_metadata(app_id, md_key, md_value) " \
946 "values('%s','%s', '%s')",\
947 app->appid, md->key, md->value ? md->value : "");
948 ret = __exec_query(query);
950 _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
954 memset(query, '\0', MAX_QUERY_LEN);
960 static int __insert_application_apppermission_info(manifest_x *mfx)
967 char query[MAX_QUERY_LEN] = {'\0'};
968 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
969 app = (application_x *)app_tmp->data;
972 for (pm_tmp = app->permission; pm_tmp; pm_tmp = pm_tmp->next) {
973 pm = (permission_x *)pm_tmp->data;
976 snprintf(query, MAX_QUERY_LEN,
977 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
978 "values('%s','%s', '%s')",\
979 app->appid, pm->type, pm->value);
980 ret = __exec_query(query);
982 _LOGD("Package UiApp permission Info DB Insert Failed\n");
985 memset(query, '\0', MAX_QUERY_LEN);
991 static int __insert_application_appcontrol_info(manifest_x *mfx)
996 appcontrol_x *acontrol;
998 char query[MAX_QUERY_LEN] = {'\0'};
999 char buf[BUFSIZE] = {'\0'};
1000 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1001 app = (application_x *)app_tmp->data;
1004 for (acontrol_tmp = app->appcontrol; acontrol_tmp; acontrol_tmp = acontrol_tmp->next) {
1005 acontrol = (appcontrol_x *)acontrol_tmp->data;
1006 if (acontrol == NULL)
1008 snprintf(buf, BUFSIZE, "%s|%s|%s",\
1009 acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1010 acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1011 acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1012 snprintf(query, MAX_QUERY_LEN,
1013 "insert into package_app_app_control(app_id, app_control) " \
1014 "values('%s', '%s')",\
1016 ret = __exec_query(query);
1018 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1021 memset(query, '\0', MAX_QUERY_LEN);
1027 /* FIXME: should be changed to app_control */
1028 static int __insert_application_appsvc_info(manifest_x *mfx)
1035 char query[MAX_QUERY_LEN] = {'\0'};
1036 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1037 app = (application_x *)app_tmp->data;
1040 for (asvc_tmp = app->appsvc; asvc_tmp; asvc_tmp = asvc_tmp->next) {
1041 asvc = (appsvc_x *)asvc_tmp->data;
1044 snprintf(query, MAX_QUERY_LEN,
1045 "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1046 "values('%s', '%s', '%s', '%s', '%s')",\
1049 __get_str(asvc->uri),
1050 __get_str(asvc->mime),
1051 __get_str(asvc->subapp));
1053 ret = __exec_query(query);
1055 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1058 memset(query, '\0', MAX_QUERY_LEN);
1064 static int __insert_application_datacontrol_info(manifest_x *mfx)
1071 char query[MAX_QUERY_LEN] = {'\0'};
1073 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1074 app = (application_x *)app_tmp->data;
1077 for (dc_tmp = app->datacontrol; dc_tmp; dc_tmp = dc_tmp->next) {
1078 dc = (datacontrol_x *)dc_tmp->data;
1081 snprintf(query, MAX_QUERY_LEN,
1082 "insert into package_app_data_control(app_id, providerid, access, type) " \
1083 "values('%s', '%s', '%s', '%s')",\
1089 ret = __exec_query(query);
1091 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1094 memset(query, '\0', MAX_QUERY_LEN);
1100 static int __insert_application_share_request_info(manifest_x *mfx)
1109 char query[MAX_QUERY_LEN] = {'\0'};
1110 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1111 app = (application_x *)app_tmp->data;
1114 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1115 ds = (datashare_x *)ds_tmp->data;
1118 for (rq_tmp = ds->request; rq_tmp; rq_tmp = rq_tmp->next) {
1119 rq = (const char *)rq_tmp->data;
1122 snprintf(query, MAX_QUERY_LEN,
1123 "insert into package_app_share_request(app_id, data_share_request) " \
1124 "values('%s', '%s')",\
1126 ret = __exec_query(query);
1128 _LOGD("Package UiApp Share Request DB Insert Failed\n");
1131 memset(query, '\0', MAX_QUERY_LEN);
1138 static int __insert_application_share_allowed_info(manifest_x *mfx)
1149 char query[MAX_QUERY_LEN] = {'\0'};
1150 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1151 app = (application_x *)app_tmp->data;
1154 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1155 ds = (datashare_x *)ds_tmp->data;
1158 for (df_tmp = ds->define; df_tmp; df_tmp = df_tmp->next) {
1159 df = (define_x *)df_tmp->data;
1162 for (al_tmp = df->allowed; al_tmp; al_tmp = al_tmp->next) {
1163 al = (const char *)al_tmp->data;
1166 snprintf(query, MAX_QUERY_LEN,
1167 "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1168 "values('%s', '%s', '%s')",\
1169 app->appid, df->path, al);
1170 ret = __exec_query(query);
1172 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1175 memset(query, '\0', MAX_QUERY_LEN);
1183 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1187 const char *pv = NULL;
1188 char query[MAX_QUERY_LEN] = { '\0' };
1191 const char *auth_name = NULL;
1192 const char *auth_email = NULL;
1193 const char *auth_href = NULL;
1195 GList *pkglocale = NULL;
1196 GList *applocale = NULL;
1197 GList *appicon = NULL;
1198 GList *appimage = NULL;
1200 if (mfx->author && mfx->author->data) {
1201 author = (author_x *)mfx->author->data;
1203 auth_name = author->text;
1205 auth_email = author->email;
1207 auth_href = author->href;
1209 /*Insert in the package_cert_info CERT_DB*/
1210 pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1211 ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1212 if (ret != PMINFO_R_OK) {
1213 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1214 _LOGE("Cert Info DB create handle failed\n");
1217 ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1218 if (ret != PMINFO_R_OK) {
1219 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1220 _LOGE("Cert Info DB Insert Failed\n");
1224 /*Insert in the package_info DB*/
1225 snprintf(query, MAX_QUERY_LEN,
1226 "insert into package_info(package, package_type, package_version, package_api_version, install_location, package_size, " \
1227 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1228 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1229 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1233 __get_str(mfx->api_version),
1234 __get_str(mfx->installlocation),
1235 __get_str(mfx->package_size),
1241 mfx->nodisplay_setting,
1243 __get_str(auth_name),
1244 __get_str(auth_email),
1245 __get_str(auth_href),
1246 mfx->installed_time,
1247 mfx->installed_storage,
1248 __get_str(mfx->storeclient_id),
1250 __get_str(mfx->package_url),
1252 __get_str(mfx->csc_path),
1253 mfx->support_disable);
1255 ret = __exec_query(query);
1257 _LOGD("Package Info DB Insert Failed\n");
1261 /*Insert in the package_privilege_info DB*/
1262 for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1263 pv = (const char *)tmp->data;
1266 memset(query, '\0', MAX_QUERY_LEN);
1267 snprintf(query, MAX_QUERY_LEN,
1268 "insert into package_privilege_info(package, privilege) " \
1269 "values('%s','%s')",\
1271 ret = __exec_query(query);
1273 _LOGD("Package Privilege Info DB Insert Failed\n");
1278 if (mfx->application != NULL) {
1279 ret = __insert_mainapp_info(mfx);
1284 /*Insert the package locale*/
1285 pkglocale = __create_locale_list(pkglocale, mfx->label, mfx->license, mfx->icon, mfx->description, mfx->author);
1286 /*remove duplicated data in pkglocale*/
1287 __trimfunc(pkglocale);
1289 /*Insert the app locale, icon, image info */
1290 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1291 app = (application_x *)tmp->data;
1294 applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL);
1295 appicon = __create_icon_list(appicon, app->icon);
1296 appimage = __create_image_list(appimage, app->image);
1298 /*remove duplicated data in applocale*/
1299 __trimfunc(applocale);
1300 __trimfunc(appicon);
1301 __trimfunc(appimage);
1303 g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1305 /*native app locale info*/
1306 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1307 app = (application_x *)tmp->data;
1310 g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app);
1311 g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app);
1312 g_list_foreach(appimage, __insert_application_image_info, (gpointer)app);
1315 g_list_free(pkglocale);
1317 g_list_free(applocale);
1319 g_list_free(appicon);
1321 g_list_free(appimage);
1324 /*Insert in the package_app_info DB*/
1325 ret = __insert_application_info(mfx);
1328 /*Insert in the package_app_app_control DB*/
1329 ret = __insert_application_appcontrol_info(mfx);
1333 /*Insert in the package_app_app_category DB*/
1334 ret = __insert_application_appcategory_info(mfx);
1338 /*Insert in the package_app_app_metadata DB*/
1339 ret = __insert_application_appmetadata_info(mfx);
1343 /*Insert in the package_app_app_permission DB*/
1344 ret = __insert_application_apppermission_info(mfx);
1348 /*Insert in the package_app_app_svc DB*/
1349 ret = __insert_application_appsvc_info(mfx);
1353 /*Insert in the package_app_share_allowed DB*/
1354 ret = __insert_application_share_allowed_info(mfx);
1358 /*Insert in the package_app_share_request DB*/
1359 ret = __insert_application_share_request_info(mfx);
1363 /*Insert in the package_app_data_control DB*/
1364 ret = __insert_application_datacontrol_info(mfx);
1372 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1374 char query[MAX_QUERY_LEN] = { '\0' };
1376 memset(query, '\0', MAX_QUERY_LEN);
1377 snprintf(query, MAX_QUERY_LEN,
1378 "delete from %s where app_id='%s'", db_table, appid);
1379 ret = __exec_query(query);
1381 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1387 static int __delete_subpkg_info_from_db(char *appid)
1391 ret = __delete_appinfo_from_db("package_app_info", appid);
1394 ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1397 ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1400 ret = __delete_appinfo_from_db("package_app_image_info", appid);
1403 ret = __delete_appinfo_from_db("package_app_app_svc", appid);
1406 ret = __delete_appinfo_from_db("package_app_app_control", appid);
1409 ret = __delete_appinfo_from_db("package_app_app_category", appid);
1412 ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1415 ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1418 ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1421 ret = __delete_appinfo_from_db("package_app_share_request", appid);
1424 ret = __delete_appinfo_from_db("package_app_data_control", appid);
1431 static int __delete_subpkg_from_db(manifest_x *mfx)
1433 char query[MAX_QUERY_LEN] = { '\0' };
1434 char *error_message = NULL;
1436 snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1438 sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1439 _LOGE("Don't execute query = %s error message = %s\n", query,
1441 sqlite3_free(error_message);
1444 sqlite3_free(error_message);
1449 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1451 char query[MAX_QUERY_LEN] = { '\0' };
1455 /*Delete from cert table*/
1456 if (uid != GLOBAL_USER)
1457 ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
1459 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1461 _LOGD("Cert Info DB Delete Failed\n");
1465 /*Delete from Package Info DB*/
1466 snprintf(query, MAX_QUERY_LEN,
1467 "delete from package_info where package='%s'", mfx->package);
1468 ret = __exec_query(query);
1470 _LOGD("Package Info DB Delete Failed\n");
1473 memset(query, '\0', MAX_QUERY_LEN);
1475 /*Delete from Package Localized Info*/
1476 snprintf(query, MAX_QUERY_LEN,
1477 "delete from package_localized_info where package='%s'", mfx->package);
1478 ret = __exec_query(query);
1480 _LOGD("Package Localized Info DB Delete Failed\n");
1484 /*Delete from Package Privilege Info*/
1485 snprintf(query, MAX_QUERY_LEN,
1486 "delete from package_privilege_info where package='%s'", mfx->package);
1487 ret = __exec_query(query);
1489 _LOGD("Package Privilege Info DB Delete Failed\n");
1493 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1494 app = (application_x *)tmp->data;
1497 ret = __delete_appinfo_from_db("package_app_info", app->appid);
1500 ret = __delete_appinfo_from_db("package_app_localized_info", app->appid);
1503 ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid);
1506 ret = __delete_appinfo_from_db("package_app_image_info", app->appid);
1509 ret = __delete_appinfo_from_db("package_app_app_svc", app->appid);
1512 ret = __delete_appinfo_from_db("package_app_app_control", app->appid);
1515 ret = __delete_appinfo_from_db("package_app_app_category", app->appid);
1518 ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid);
1521 ret = __delete_appinfo_from_db("package_app_app_permission", app->appid);
1524 ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid);
1527 ret = __delete_appinfo_from_db("package_app_share_request", app->appid);
1530 ret = __delete_appinfo_from_db("package_app_data_control", app->appid);
1535 /* if main package has sub pkg, delete sub pkg data*/
1536 __delete_subpkg_from_db(mfx);
1541 static int __update_preload_condition_in_db()
1544 char query[MAX_QUERY_LEN] = {'\0'};
1546 snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1548 ret = __exec_query(query);
1550 _LOGD("Package preload_condition update failed\n");
1555 API int pkgmgr_parser_initialize_db(uid_t uid)
1559 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1561 _LOGD("package info DB initialization failed\n");
1564 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1566 _LOGD("package localized info DB initialization failed\n");
1569 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1571 _LOGD("package app app privilege DB initialization failed\n");
1574 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1576 _LOGD("package app info DB initialization failed\n");
1579 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1581 _LOGD("package app localized info DB initialization failed\n");
1584 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1586 _LOGD("package app icon localized info DB initialization failed\n");
1589 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1591 _LOGD("package app image info DB initialization failed\n");
1594 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1596 _LOGD("package app app control DB initialization failed\n");
1599 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1601 _LOGD("package app app category DB initialization failed\n");
1604 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1606 _LOGD("package app app category DB initialization failed\n");
1609 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1611 _LOGD("package app app permission DB initialization failed\n");
1614 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
1616 _LOGD("package app app svc DB initialization failed\n");
1619 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1621 _LOGD("package app share allowed DB initialization failed\n");
1624 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1626 _LOGD("package app share request DB initialization failed\n");
1629 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
1631 _LOGD("package app data control DB initialization failed\n");
1635 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1637 _LOGD("package cert info DB initialization failed\n");
1640 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1642 _LOGD("package cert index info DB initialization failed\n");
1646 if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
1647 _LOGD("Failed to change cert db permission\n");
1649 if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
1650 _LOGD("Failed to change parser db permission\n");
1656 static int __parserdb_change_perm(const char *db_file, uid_t uid)
1659 char journal_file[BUFSIZE];
1662 struct passwd *userinfo = NULL;
1663 files[0] = (char *)db_file;
1664 files[1] = journal_file;
1667 if (db_file == NULL)
1670 if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
1672 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1673 if (uid == OWNER_ROOT)
1675 userinfo = getpwuid(uid);
1677 _LOGE("FAIL: user %d doesn't exist", uid);
1680 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1682 for (i = 0; files[i]; i++) {
1683 ret = chown(files[i], uid, userinfo->pw_gid);
1685 if (strerror_r(errno, buf, sizeof(buf)))
1687 _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
1688 userinfo->pw_gid, buf);
1692 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1694 if (strerror_r(errno, buf, sizeof(buf)))
1696 _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
1699 SET_SMACK_LABEL(files[i]);
1704 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
1708 if (getuid() != OWNER_ROOT) {
1709 _LOGE("Only root user is allowed");
1713 if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
1714 _LOGE("Manifest db for user %d is already exists", uid);
1718 if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
1719 _LOGE("Cert db for user %d is already exists", uid);
1723 ret = pkgmgr_parser_check_and_create_db(uid);
1726 ret = pkgmgr_parser_initialize_db(uid);
1728 pkgmgr_parser_close_db();
1731 pkgmgr_parser_close_db();
1736 API int pkgmgr_parser_check_and_create_db(uid_t uid)
1740 ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
1742 _LOGD("Manifest DB creation Failed\n");
1747 ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
1749 _LOGD("Cert DB creation Failed\n");
1755 void pkgmgr_parser_close_db(void)
1757 sqlite3_close(pkgmgr_parser_db);
1758 sqlite3_close(pkgmgr_cert_db);
1762 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1764 _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
1766 _LOGD("manifest pointer is NULL\n");
1770 ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1772 _LOGD("Failed to open DB\n");
1775 ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
1778 /*Begin transaction*/
1779 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1780 if (ret != SQLITE_OK) {
1781 _LOGD("Failed to begin transaction\n");
1785 _LOGD("Transaction Begin\n");
1786 ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
1788 _LOGD("Insert into DB failed. Rollback now\n");
1789 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1792 /*Commit transaction*/
1793 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1794 if (ret != SQLITE_OK) {
1795 _LOGD("Failed to commit transaction. Rollback now\n");
1796 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1800 _LOGD("Transaction Commit and End\n");
1802 pkgmgr_parser_close_db();
1806 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1808 _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
1810 _LOGD("manifest pointer is NULL\n");
1814 ret = pkgmgr_parser_check_and_create_db(uid);
1816 _LOGD("Failed to open DB\n");
1819 ret = pkgmgr_parser_initialize_db(uid);
1822 /*Begin transaction*/
1823 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1824 if (ret != SQLITE_OK) {
1825 _LOGD("Failed to begin transaction\n");
1829 _LOGD("Transaction Begin\n");
1830 ret = __insert_manifest_info_in_db(mfx, uid);
1832 _LOGD("Insert into DB failed. Rollback now\n");
1833 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1836 /*Commit transaction*/
1837 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1838 if (ret != SQLITE_OK) {
1839 _LOGD("Failed to commit transaction. Rollback now\n");
1840 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1844 _LOGD("Transaction Commit and End\n");
1846 pkgmgr_parser_close_db();
1850 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1853 _LOGD("manifest pointer is NULL\n");
1857 ret = pkgmgr_parser_check_and_create_db(uid);
1859 _LOGD("Failed to open DB\n");
1862 ret = pkgmgr_parser_initialize_db(uid);
1865 /*Preserve guest mode visibility*/
1866 __preserve_guestmode_visibility_value( mfx);
1867 /*Begin transaction*/
1868 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1869 if (ret != SQLITE_OK) {
1870 _LOGD("Failed to begin transaction\n");
1874 _LOGD("Transaction Begin\n");
1875 ret = __delete_manifest_info_from_db(mfx, uid);
1877 _LOGD("Delete from DB failed. Rollback now\n");
1878 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1881 ret = __insert_manifest_info_in_db(mfx, uid);
1883 _LOGD("Insert into DB failed. Rollback now\n");
1884 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1888 /*Commit transaction*/
1889 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1890 if (ret != SQLITE_OK) {
1891 _LOGD("Failed to commit transaction. Rollback now\n");
1892 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1896 _LOGD("Transaction Commit and End\n");
1898 pkgmgr_parser_close_db();
1902 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1904 return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
1907 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
1910 _LOGD("manifest pointer is NULL\n");
1914 ret = pkgmgr_parser_check_and_create_db(uid);
1916 _LOGD("Failed to open DB\n");
1919 /*Begin transaction*/
1920 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1921 if (ret != SQLITE_OK) {
1922 _LOGD("Failed to begin transaction\n");
1926 _LOGD("Transaction Begin\n");
1927 ret = __delete_manifest_info_from_db(mfx, uid);
1929 _LOGD("Delete from DB failed. Rollback now\n");
1930 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1933 /*Commit transaction*/
1934 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1935 if (ret != SQLITE_OK) {
1936 _LOGD("Failed to commit transaction, Rollback now\n");
1937 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1941 _LOGD("Transaction Commit and End\n");
1943 pkgmgr_parser_close_db();
1947 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1949 return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
1952 API int pkgmgr_parser_update_preload_info_in_db()
1955 ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1957 _LOGD("Failed to open DB\n");
1960 /*Begin transaction*/
1961 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1962 if (ret != SQLITE_OK) {
1963 _LOGD("Failed to begin transaction\n");
1967 _LOGD("Transaction Begin\n");
1968 ret = __update_preload_condition_in_db();
1970 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
1971 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1974 /*Commit transaction*/
1975 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1976 if (ret != SQLITE_OK) {
1977 _LOGD("Failed to commit transaction, Rollback now\n");
1978 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1982 _LOGD("Transaction Commit and End\n");
1984 pkgmgr_parser_close_db();
1988 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
1991 ret = pkgmgr_parser_check_and_create_db(uid);
1993 _LOGD("Failed to open DB\n");
1996 /*Begin transaction*/
1997 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1998 if (ret != SQLITE_OK) {
1999 _LOGD("Failed to begin transaction\n");
2003 _LOGD("Transaction Begin\n");
2004 ret = __update_preload_condition_in_db();
2006 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2007 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2010 /*Commit transaction*/
2011 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2012 if (ret != SQLITE_OK) {
2013 _LOGD("Failed to commit transaction, Rollback now\n");
2014 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2018 _LOGD("Transaction Commit and End\n");
2020 pkgmgr_parser_close_db();