Fix usage of GVariant in "g_dbus_proxy_call_sync"
[platform/core/appfw/app2sd.git] / plugin / app2sd / lib / app2sd_client_interface.c
index 5c66bd5..efbf557 100644 (file)
@@ -28,6 +28,8 @@
 #include <gio/gio.h>
 
 #include <pkgmgr-info.h>
+#include <sqlite3.h>
+#include <tzplatform_config.h>
 
 #include "app2sd_client_interface.h"
 #include "app2sd_utils.h"
@@ -67,17 +69,22 @@ static int __app2sd_call_server_method(const gchar *method_name,
        GError *error = NULL;
        GVariant *value = NULL;
 
+       if (param)
+               g_variant_ref_sink(param);
+
        /* get gdbus connection */
        ret = app2sd_gdbus_shared_connection(&conn);
        if (ret) {
                _E("app2sd error : dbus connection error");
+               if (param)
+                       g_variant_unref(param);
                return ret;
        }
 
        /* method call */
        do {
                proxy = g_dbus_proxy_new_sync(conn,
-                       G_DBUS_PROXY_FLAGS_NONE, NULL,
+                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
                        APP2SD_BUS_NAME, APP2SD_OBJECT_PATH,
                        APP2SD_INTERFACE_NAME,
                        NULL, &error);
@@ -85,6 +92,7 @@ static int __app2sd_call_server_method(const gchar *method_name,
                        _E("failed to create new proxy, error(%s)",
                                error->message);
                        g_error_free(error);
+                       error = NULL;
                        ret = APP2EXT_ERROR_DBUS_FAILED;
                        retry_cnt++;
                        usleep(CONNECTION_WAIT_USEC);
@@ -97,6 +105,7 @@ static int __app2sd_call_server_method(const gchar *method_name,
                if (error != NULL) {
                        _E("proxy call sync error(%s)", error->message);
                        g_error_free(error);
+                       error = NULL;
                        ret = APP2EXT_ERROR_DBUS_FAILED;
                        retry_cnt++;
                        usleep(CONNECTION_WAIT_USEC);
@@ -115,6 +124,8 @@ static int __app2sd_call_server_method(const gchar *method_name,
 
        if (conn)
                g_object_unref(conn);
+       if (param)
+               g_variant_unref(param);
 
        return ret;
 }
@@ -131,19 +142,17 @@ static int __app2sd_delete_temp_directories(const char *pkgid,
                app2sd_cmd cmd, uid_t uid)
 {
        int ret = 0;
+       char app_path[FILENAME_MAX] = { 0, };
        char temp_path[FILENAME_MAX] = { 0, };
 
        if (cmd != APP2SD_PRE_UPGRADE)
                return APP2EXT_SUCCESS;
 
-       if (_is_global(uid)) {
-               snprintf(temp_path, FILENAME_MAX - 1, "%s/%s.new",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(temp_path, FILENAME_MAX - 1, "%s/%s.new",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
+       _app2sd_set_application_path(pkgid, uid, app_path, sizeof(app_path));
+       ret = snprintf(temp_path, sizeof(temp_path), "%s.new", app_path);
+       if (ret < 0 || ret > sizeof(temp_path)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_DELETE_DIRECTORY;
        }
 
        /* this will delete all files under temp_path */
@@ -162,19 +171,11 @@ static int __app2sd_create_default_directories(const char *pkgid,
        int ret = 0;
        mode_t mode = DIR_PERMS;
        char application_path[FILENAME_MAX] = { 0, };
-       char application_mmc_path[FILENAME_MAX] = { 0, };
+       char app_mmc_path[FILENAME_MAX] = { 0, };
        char temp_path[FILENAME_MAX] = { 0, };
 
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
-
+       _app2sd_set_application_path(pkgid, uid, application_path,
+               sizeof(application_path));
        ret = mkdir(application_path, mode);
        if (ret) {
                if (errno != EEXIST) {
@@ -184,10 +185,13 @@ static int __app2sd_create_default_directories(const char *pkgid,
                }
        }
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                application_path);
-
-       ret = mkdir(application_mmc_path, mode);
+       if (ret < 0 || ret > sizeof(app_mmc_path)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_CREATE_DIRECTORY;
+       }
+       ret = mkdir(app_mmc_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("create directory failed," \
@@ -198,8 +202,12 @@ static int __app2sd_create_default_directories(const char *pkgid,
 
        if (cmd == APP2SD_PRE_UPGRADE) {
                /* application_path for {pkgid}.new */
-               snprintf(temp_path, FILENAME_MAX - 1, "%s.new",
+               ret = snprintf(temp_path, sizeof(temp_path), "%s.new",
                        application_path);
+               if (ret < 0 || ret > sizeof(temp_path)) {
+                       _E("snprintf fail");
+                       return APP2EXT_ERROR_CREATE_DIRECTORY;
+               }
                ret = mkdir(temp_path, mode);
                if (ret) {
                        if (errno != EEXIST) {
@@ -208,10 +216,14 @@ static int __app2sd_create_default_directories(const char *pkgid,
                                return APP2EXT_ERROR_CREATE_DIRECTORY;
                        }
                }
-               /* application_mmc_path for {pkgid}.new */
-               snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+               /* app_mmc_path for {pkgid}.new */
+               ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                        temp_path);
-               ret = mkdir(application_mmc_path, mode);
+               if (ret < 0 || ret > sizeof(app_mmc_path)) {
+                       _E("snprintf fail");
+                       return APP2EXT_ERROR_CREATE_DIRECTORY;
+               }
+               ret = mkdir(app_mmc_path, mode);
                if (ret) {
                        if (errno != EEXIST) {
                                _E("create directory failed," \
@@ -537,14 +549,16 @@ int app2sd_client_usr_pre_move_installed_app(const char *pkgid, GList *dir_list,
        }
        ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
        if (ret < 0) {
-               _E("failed to get installed storage for pkg(%s) of uid(%d), ret(%d)",
-                       pkgid, uid, ret);
+               _E("failed to get installed storage for pkg(%s) " \
+                       "of uid(%d), ret(%d)", pkgid, uid, ret);
                pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
                return APP2EXT_ERROR_PKGMGR_ERROR;
        }
 
-       if ((move_type == APP2EXT_MOVE_TO_EXT && storage == PMINFO_EXTERNAL_STORAGE)
-               || (move_type == APP2EXT_MOVE_TO_PHONE && storage == PMINFO_INTERNAL_STORAGE)) {
+       if ((move_type == APP2EXT_MOVE_TO_EXT && storage ==
+                       PMINFO_EXTERNAL_STORAGE)
+               || (move_type == APP2EXT_MOVE_TO_PHONE &&
+                       storage == PMINFO_INTERNAL_STORAGE)) {
                        _E("PKG_EXISTS in [%d] STORAGE", storage);
                        pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
                        return APP2EXT_ERROR_PKG_EXISTS;
@@ -613,6 +627,108 @@ int app2sd_client_post_move_installed_app(const char *pkgid,
        return ret;
 }
 
+int app2sd_client_usr_pre_migrate_legacy(const char *pkgid, uid_t uid)
+{
+       int ret = 0;
+       GVariant *param = NULL;
+
+       /* validate the function parameter recieved */
+       if (pkgid == NULL) {
+               _E("invalid function arguments");
+               return APP2EXT_ERROR_INVALID_ARGUMENTS;
+       }
+
+       param = g_variant_new("(si)", pkgid, uid);
+       ret = __app2sd_call_server_method("PreMigrateLegacy", param);
+
+       return ret;
+}
+
+int app2sd_client_usr_post_migrate_legacy(const char *pkgid, uid_t uid)
+{
+       int ret = 0;
+       GVariant *param = NULL;
+
+       /* validate the function parameter recieved */
+       if (pkgid == NULL) {
+               _E("invalid function arguments");
+               return APP2EXT_ERROR_INVALID_ARGUMENTS;
+       }
+
+       param = g_variant_new("(si)", pkgid, uid);
+       ret = __app2sd_call_server_method("PostMigrateLegacy", param);
+
+       return ret;
+}
+
+int app2sd_client_migrate_legacy_all()
+{
+       int ret = 0;
+
+       ret = __app2sd_call_server_method("MigrateLegacyAll", NULL);
+
+       return ret;
+}
+
+char *app2sd_client_usr_getname_image(const char *pkgid, uid_t uid)
+{
+       return _app2sd_get_encoded_name(pkgid, uid);
+}
+
+char *app2sd_client_usr_get_image_path(const char *pkgid, uid_t uid)
+{
+       static const char query[] =
+               "SELECT filename FROM app2sd_info WHERE pkgid=? AND uid=?";
+       int ret;
+       sqlite3 *db;
+       sqlite3_stmt *stmt;
+       const char *dbpath;
+       char *image_path = NULL;
+
+       dbpath = tzplatform_mkpath(TZ_SYS_DB, ".app2sd.db");
+       if (dbpath == NULL)
+               return NULL;
+
+       ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
+       if (ret != SQLITE_OK) {
+               LOGE("open failed: %s", sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return NULL;
+       }
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               sqlite3_close_v2(db);
+               return NULL;
+       }
+
+       ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC);
+       if (ret != SQLITE_OK) {
+               LOGE("bind failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               sqlite3_close_v2(db);
+               return NULL;
+       }
+
+       ret = sqlite3_bind_int(stmt, 2, uid);
+       if (ret != SQLITE_OK) {
+               LOGE("bind failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               sqlite3_close_v2(db);
+               return NULL;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW)
+               image_path = strdup((char *)sqlite3_column_text(stmt, 0));
+
+       sqlite3_finalize(stmt);
+       sqlite3_close_v2(db);
+
+       return image_path;
+}
+
 void app2ext_on_load(app2ext_interface *interface)
 {
        /* Plug-in Binding.*/
@@ -629,6 +745,7 @@ void app2ext_on_load(app2ext_interface *interface)
        interface->client_disable_full_pkg = app2sd_client_disable_full_pkg;
        interface->client_pre_move = app2sd_client_pre_move_installed_app;
        interface->client_post_move = app2sd_client_post_move_installed_app;
+       interface->client_migrate_legacy_all = app2sd_client_migrate_legacy_all;
 
        interface->client_usr_pre_install = app2sd_client_usr_pre_app_install;
        interface->client_usr_post_install = app2sd_client_usr_post_app_install;
@@ -641,4 +758,8 @@ void app2ext_on_load(app2ext_interface *interface)
        interface->client_usr_disable = app2sd_client_usr_on_demand_setup_exit;
        interface->client_usr_pre_move = app2sd_client_usr_pre_move_installed_app;
        interface->client_usr_post_move = app2sd_client_usr_post_move_installed_app;
+       interface->client_usr_getname_image = app2sd_client_usr_getname_image;
+       interface->client_usr_pre_migrate_legacy = app2sd_client_usr_pre_migrate_legacy;
+       interface->client_usr_post_migrate_legacy = app2sd_client_usr_post_migrate_legacy;
+       interface->client_usr_get_image_path = app2sd_client_usr_get_image_path;
 }