Change permission of pkgmgr db files
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
index 4fdbf5a..d5c516c 100644 (file)
@@ -50,6 +50,7 @@
 #define MAX_QUERY_LEN          4096
 #define BUFSIZE 4096
 #define OWNER_ROOT 0
+#define APPFW_USER "app_fw"
 #define MAX_INTEGER_LENGTH 10
 
 #define LDPI "ldpi"
@@ -145,7 +146,6 @@ sqlite3 *pkgmgr_cert_db;
                                                "app_multiple TEXT NOT NULL DEFAULT 'false', " \
                                                "app_autorestart TEXT NOT NULL DEFAULT 'false', " \
                                                "app_taskmanage TEXT NOT NULL DEFAULT 'false', " \
-                                               "app_enabled TEXT NOT NULL DEFAULT 'true', " \
                                                "app_hwacceleration TEXT NOT NULL DEFAULT 'use-system-setting', " \
                                                "app_screenreader TEXT NOT NULL DEFAULT 'use-system-setting', " \
                                                "app_mainapp TEXT NOT NULL, " \
@@ -170,6 +170,7 @@ sqlite3 *pkgmgr_cert_db;
                                                "app_package_system TEXT NOT NULL, " \
                                                "app_removable TEXT NOT NULL DEFAULT 'false', " \
                                                "app_package_installed_time TEXT, " \
+                                               "app_support_ambient TEXT NOT NULL DEFAULT 'false', " \
                                                "component_type TEXT, " \
                                                "package TEXT NOT NULL, " \
                                                "app_tep_name TEXT, " \
@@ -543,6 +544,11 @@ static int __exec_query(char *query)
                return -1;
        }
        sqlite3_free(error_message);
+
+       if (strncmp(query, "DELETE", 6) && sqlite3_changes(pkgmgr_parser_db) == 0) {
+               _LOGE("No such record matches with given query : %s\n", query);
+               return -1;
+       }
        return 0;
 }
 
@@ -1109,15 +1115,6 @@ static int __insert_mainapp_info(manifest_x *mfx)
                app = (application_x *)tmp->data;
                if (app == NULL)
                        continue;
-
-               sqlite3_snprintf(MAX_QUERY_LEN, query,
-                               "UPDATE package_app_info SET app_mainapp=%Q WHERE app_id=%Q",
-                               app->mainapp, app->appid);
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       _LOGD("Package App Info DB Insert Failed\n");
-                       return -1;
-               }
                if (strcasecmp(app->mainapp, "True") == 0)
                        mfx->mainapp_id = strdup(app->appid);
        }
@@ -1128,21 +1125,6 @@ static int __insert_mainapp_info(manifest_x *mfx)
                app = (application_x *)mfx->application->data;
                if (app == NULL)
                        return -1;
-               if (app->appid) {
-                       sqlite3_snprintf(MAX_QUERY_LEN, query,
-                                       "UPDATE package_app_info SET app_mainapp='true' WHERE app_id=%Q",
-                                       app->appid);
-               } else {
-                       _LOGD("Not valid appid\n");
-                       return -1;
-               }
-
-               ret = __exec_query(query);
-               if (ret == -1) {
-                       _LOGD("Package UiApp Info DB Insert Failed\n");
-                       return -1;
-               }
-
                free((void *)app->mainapp);
                app->mainapp = strdup("true");
                mfx->mainapp_id = strdup(app->appid);
@@ -1257,33 +1239,33 @@ static int __insert_application_info(manifest_x *mfx)
                sqlite3_snprintf(MAX_QUERY_LEN, query,
                        "INSERT INTO package_app_info(" \
                        "app_id, app_component, app_exec, app_nodisplay, app_type, " \
-                       "app_onboot, app_multiple, app_autorestart, app_taskmanage, app_enabled, " \
+                       "app_onboot, app_multiple, app_autorestart, app_taskmanage, " \
                        "app_hwacceleration, app_screenreader, app_mainapp, app_recentimage, app_launchcondition, " \
                        "app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, " \
                        "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, " \
                        "app_launch_mode, app_ui_gadget, app_support_mode, app_support_disable, component_type, package, " \
                        "app_tep_name, app_zip_mount_file, app_background_category, app_package_type, app_root_path, " \
                        "app_api_version, app_effective_appid, app_splash_screen_display, app_package_system, app_removable, " \
-                       "app_package_installed_time, app_support_mode) " \
+                       "app_package_installed_time, app_support_ambient) " \
                        "VALUES(" \
                        "%Q, %Q, %Q, LOWER(%Q), %Q, " \
-                       "LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), " \
+                       "LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), " \
                        "%Q, %Q, %Q, %Q, %Q, " \
                        "LOWER(%Q), %Q, %Q, LOWER(%Q), %Q, " \
                        "LOWER(%Q), LOWER(%Q), %Q, %Q, LOWER(%Q), " \
                        "COALESCE(%Q, 'caller'), LOWER(%Q), %Q, LOWER(%Q), %Q, %Q, " \
                        "%Q, %Q, %d, %Q, %Q, " \
                        "%Q, %Q, LOWER(%Q), LOWER(%Q), LOWER(%Q), " \
-                       "%Q, %Q)", \
+                       "%Q, LOWER(%Q))", \
                        app->appid, app->component_type, app->exec, __get_bool(app->nodisplay, false), app->type,
-                       __get_bool(app->onboot, false), __get_bool(app->multiple, false), __get_bool(app->autorestart, false), __get_bool(app->taskmanage, false), __get_bool(app->enabled, true),
+                       __get_bool(app->onboot, false), __get_bool(app->multiple, false), __get_bool(app->autorestart, false), __get_bool(app->taskmanage, false),
                        app->hwacceleration, app->screenreader, app->mainapp, app->recentimage, app->launchcondition,
                        __get_bool(app->indicatordisplay, true), app->portraitimg, app->landscapeimg,
                        __get_bool(app->guestmode_visibility, true), app->permission_type,
                        __get_bool(mfx->preload, false), __get_bool(app->submode, false), app->submode_mainid, mfx->installed_storage, __get_bool(app->process_pool, false),
                        app->launch_mode, __get_bool(app->ui_gadget, false), mfx->support_mode, __get_bool(mfx->support_disable, false), app->component_type, mfx->package,
                        mfx->tep_name, mfx->zip_mount_file, background_value, type, mfx->root_path, mfx->api_version,
-                       effective_appid, __get_bool(app->splash_screen_display, false), __get_bool(mfx->system, false), __get_bool(mfx->removable, true), mfx->installed_time, mfx->support_mode);
+                       effective_appid, __get_bool(app->splash_screen_display, false), __get_bool(mfx->system, false), __get_bool(mfx->removable, true), mfx->installed_time, __get_bool(app->support_ambient, false));
 
                ret = __exec_query(query);
                if (ret == -1) {
@@ -1704,181 +1686,6 @@ static int __insert_application_splashscreen_info(manifest_x *mfx)
        return 0;
 }
 
-static int __insert_application_legacy_splashscreen_info(manifest_x *mfx)
-{
-       GList *app_tmp;
-       application_x *app;
-       int ret = -1;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char *tmp;
-       const char *image_type;
-       const char *indicatordisplay;
-       const char *orientation;
-       const char *operation = "launch-effect";
-       const char *color_depth = "24"; /* default */
-
-       for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
-               app = (application_x *)app_tmp->data;
-               if (app == NULL ||
-                       (app->portraitimg == NULL && app->landscapeimg == NULL))
-                       continue;
-               image_type = "img"; /* default */
-               if (app->effectimage_type) {
-                       tmp = strstr(app->effectimage_type, "edj");
-                       if (tmp)
-                               image_type = "edj";
-               }
-               indicatordisplay = "true"; /* default */
-               if (app->indicatordisplay)
-                       indicatordisplay = app->indicatordisplay;
-               if (app->portraitimg) {
-                       orientation = "portrait";
-                       sqlite3_snprintf(MAX_QUERY_LEN, query,
-                                       "INSERT INTO package_app_splash_screen" \
-                                       "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \
-                                       "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
-                                       app->appid, app->portraitimg, image_type,
-                                       orientation, indicatordisplay, operation,
-                                       color_depth);
-                       ret = __exec_query(query);
-                       if (ret == -1) {
-                               _LOGD("Package UiApp Splash Screen DB Insert Failed");
-                               return -1;
-                       }
-                       memset(query, '\0', MAX_QUERY_LEN);
-               }
-               if (app->landscapeimg) {
-                       orientation = "landscape";
-                       sqlite3_snprintf(MAX_QUERY_LEN, query,
-                                       "INSERT INTO package_app_splash_screen" \
-                                       "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \
-                                       "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
-                                       app->appid, app->landscapeimg, image_type,
-                                       orientation, indicatordisplay, operation,
-                                       color_depth);
-                       ret = __exec_query(query);
-                       if (ret == -1) {
-                               _LOGD("Package UiApp Splash Screen DB Insert Failed");
-                               return -1;
-                       }
-                       memset(query, '\0', MAX_QUERY_LEN);
-               }
-       }
-       return 0;
-}
-
-static int __insert_application_metadata_splashscreen_info(manifest_x *mfx)
-{
-       GList *app_tmp;
-       application_x *app;
-       GList *md_tmp;
-       metadata_x *md;
-       int ret;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char *token;
-       char *tmpptr = NULL;
-       const char *operation;
-       const char *portraitimg;
-       const char *landscapeimg;
-       const char *indicatordisplay;
-       const char *orientation;
-       const char *image_type;
-       const char *color_depth = "24"; /* default */
-
-       for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
-               app = (application_x *)app_tmp->data;
-               if (app == NULL)
-                       continue;
-
-               for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
-                       md = (metadata_x *)md_tmp->data;
-                       if (md == NULL || md->key == NULL || md->value == NULL)
-                               continue;
-
-                       if (strcasestr(md->key, "operation_effect=")) {
-                               operation = index(md->key, '=');
-                               if (operation && operation[1] != '\0')
-                                       operation++;
-                               else
-                                       operation = "launch-effect";
-                       } else if (strcasestr(md->key, "launch_effect")) {
-                               operation = "launch-effect";
-                       } else {
-                               continue;
-                       }
-
-                       portraitimg = NULL;
-                       landscapeimg = NULL;
-                       indicatordisplay = "true"; /* default */
-                       token = strtok_r(md->value, "|", &tmpptr);
-                       while (token != NULL) {
-                               if (strcasestr(token, "portrait-effectimage=")) {
-                                       portraitimg = index(token, '=');
-                                       if (portraitimg && portraitimg[1] != '\0')
-                                               portraitimg++;
-                                       else
-                                               portraitimg = NULL;
-                               } else if (strcasestr(token, "landscape-effectimage=")) {
-                                       landscapeimg = index(token, '=');
-                                       if (landscapeimg && landscapeimg[1] != '\0')
-                                               landscapeimg++;
-                                       else
-                                               landscapeimg = NULL;
-                               } else if (strcasestr(token, "indicatordisplay=")) {
-                                       indicatordisplay = index(token, '=');
-                                       if (indicatordisplay && indicatordisplay[1] != '\0')
-                                               indicatordisplay++;
-                                       else
-                                               indicatordisplay = "true";
-                               }
-
-                               token = strtok_r(NULL, "|", &tmpptr);
-                       }
-
-                       if (portraitimg) {
-                               orientation = "portrait";
-                               image_type = "img";
-                               if (strcasestr(portraitimg, "edj"))
-                                       image_type = "edj";
-                               sqlite3_snprintf(MAX_QUERY_LEN, query,
-                                       "INSERT INTO package_app_splash_screen" \
-                                       "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \
-                                       "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
-                                       app->appid, portraitimg, image_type,
-                                       orientation, indicatordisplay, operation,
-                                       color_depth);
-                               ret = __exec_query(query);
-                               if (ret == -1) {
-                                       _LOGD("Package UiApp Splash Screen DB Insert Failed");
-                                       return -1;
-                               }
-                               memset(query, '\0', MAX_QUERY_LEN);
-                       }
-                       if (landscapeimg) {
-                               orientation = "landscape";
-                               image_type = "img";
-                               if (strcasestr(landscapeimg, "edj"))
-                                       image_type = "edj";
-                               sqlite3_snprintf(MAX_QUERY_LEN, query,
-                                       "INSERT INTO package_app_splash_screen" \
-                                       "(app_id, src, type, orientation, indicatordisplay, operation, color_depth) " \
-                                       "VALUES(%Q, %Q, %Q, %Q, %Q, %Q, %Q)",
-                                       app->appid, landscapeimg, image_type,
-                                       orientation, indicatordisplay, operation,
-                                       color_depth);
-                               ret = __exec_query(query);
-                               if (ret == -1) {
-                                       _LOGD("Package UiApp Splash Screen DB Insert Failed");
-                                       return -1;
-                               }
-                               memset(query, '\0', MAX_QUERY_LEN);
-                       }
-               }
-       }
-
-       return 0;
-}
-
 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
 {
        GList *tmp;
@@ -1915,6 +1722,7 @@ static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
        if (mfx->support_mode)
                temp_pkg_mode = atoi(mfx->support_mode);
 
+       /* FIXME : OR-ing should be fixed to not to store whole support mode */
        list_up = mfx->application;
        while (list_up != NULL) {
                up_support_mode = (application_x *)list_up->data;
@@ -1926,7 +1734,7 @@ static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
        }
        snprintf(pkg_mode, MAX_INTEGER_LENGTH, "%d", temp_pkg_mode);
 
-       if(mfx->support_mode)
+       if (mfx->support_mode)
                free((void *)mfx->support_mode);
        mfx->support_mode = strdup(pkg_mode);
 
@@ -1937,19 +1745,19 @@ static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
                "install_location, package_size, package_removable, package_preload, package_readonly, " \
                "package_update, package_appsetting, package_nodisplay, package_system, author_name, " \
                "author_email, author_href, installed_time, installed_storage, storeclient_id, " \
-               "mainapp_id, package_url, root_path, csc_path, package_support_mode, package_support_disable, package_support_mode) " \
+               "mainapp_id, package_url, root_path, csc_path, package_support_mode, package_support_disable) " \
                "VALUES(" \
                "%Q, %Q, %Q, %Q, %Q, %Q, " \
                "%Q, %Q, LOWER(%Q), LOWER(%Q), LOWER(%Q), " \
                "LOWER(%Q), LOWER(%Q), LOWER(%Q), LOWER(%Q), %Q, " \
                "%Q, %Q, %Q, %Q, %Q, " \
-               "%Q, %Q, %Q, %Q, %Q, LOWER(%Q), %Q)",
+               "%Q, %Q, %Q, %Q, %Q, LOWER(%Q))",
                mfx->package, mfx->type, mfx->version, mfx->api_version, mfx->tep_name, mfx->zip_mount_file,
                mfx->installlocation, mfx->package_size, __get_bool(mfx->removable, true), __get_bool(mfx->preload, false), __get_bool(mfx->readonly, false),
                __get_bool(mfx->update, false), __get_bool(mfx->appsetting, false), __get_bool(mfx->nodisplay_setting, false), __get_bool(mfx->system, false), auth_name,
                auth_email, auth_href, mfx->installed_time, mfx->installed_storage,
                mfx->storeclient_id,
-               mfx->mainapp_id, mfx->package_url, mfx->root_path, mfx->csc_path, mfx->support_mode, __get_bool(mfx->support_disable, false), mfx->support_mode);
+               mfx->mainapp_id, mfx->package_url, mfx->root_path, mfx->csc_path, mfx->support_mode, __get_bool(mfx->support_disable, false));
 
        ret = __exec_query(query);
        if (ret == -1) {
@@ -2059,16 +1867,6 @@ static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
        if (ret == -1)
                return -1;
 
-       /*Insert in the package_app_splash_screen DB (backward compatibility)*/
-       ret = __insert_application_legacy_splashscreen_info(mfx);
-       if (ret == -1)
-               return -1;
-
-       /*Insert in the package_app_splash_screen DB (backward compatibility)*/
-       ret = __insert_application_metadata_splashscreen_info(mfx);
-       if (ret == -1)
-               return -1;
-
        /*Insert in the package_app_splash_screen DB*/
        ret = __insert_application_splashscreen_info(mfx);
        if (ret == -1)
@@ -2342,7 +2140,7 @@ static int __disable_global_app_for_user(const char *appid, uid_t uid)
                sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO "
                                "package_app_info_for_uid(app_id, uid, is_disabled, is_splash_screen_enabled) "
                                "VALUES(%Q, %d, 'true', "
-                               "(SELECT app_splash_screen_display FROM package_app_info WHERE appid='%Q'))",
+                               "(SELECT app_splash_screen_display FROM package_app_info WHERE app_id=%Q))",
                                appid, (int)uid, appid);
        } else {
                sqlite3_snprintf(MAX_QUERY_LEN, query, "UPDATE "
@@ -2614,8 +2412,13 @@ static int __parserdb_change_perm(const char *db_file, uid_t uid)
        if (getuid() != OWNER_ROOT) /* At this time we should be root to apply this */
                return 0;
        snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
-       if (uid == OWNER_ROOT)
-               uid = GLOBAL_USER;
+       if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
+               ret = getpwnam_r(APPFW_USER, &userinfo, buf, sizeof(buf), &result);
+               if (ret != 0 || result == NULL)
+                       return -1;
+               uid = userinfo.pw_uid;
+       }
+
        ret = getpwuid_r(uid, &userinfo, pwuid_buf, sizeof(pwuid_buf), &result);
        if (ret != 0 || result == NULL) {
                _LOGE("FAIL: user %d doesn't exist", uid);