Revert "Revert "Refactor duplicate codes as a function to reuse them"" 26/109526/1
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Jan 2017 08:55:35 +0000 (17:55 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 10 Jan 2017 08:55:39 +0000 (17:55 +0900)
This reverts commit 3f28235b928ce486c68d90e608d8a1f10f7c5eb8.

Change-Id: I146f3f039d11ec1eaaf1137c6b9ce4e352958246

plugin/app2sd/server/app2sd_interface.c

index 40e2cf8..213a284 100644 (file)
@@ -46,6 +46,69 @@ static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
        return APP2EXT_SUCCESS;
 }
 
+static int __app2sd_finalize_device_setup(const char *pkgid,
+       const char *loopback_device, const char *application_path,
+       uid_t uid)
+{
+       char *device_node = NULL;
+       int ret = APP2EXT_SUCCESS;
+
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+       device_node =
+               _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
+       if (!device_node) {
+               _E("failed to find device node");
+               return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
+       }
+#else
+       device_node = _app2sd_find_associated_device_node(loopback_device);
+       if (NULL == device_node) {
+               _E("failed to find device node");
+               return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
+       }
+#endif
+
+       ret = _app2sd_unmount_app_content(application_path);
+       if (ret) {
+               if (device_node) {
+                       free(device_node);
+                       device_node = NULL;
+               }
+               _E("unable to unmount the app content (%d)", ret);
+               return APP2EXT_ERROR_UNMOUNT;
+       }
+
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
+       if (ret) {
+               if (device_node) {
+                       free(device_node);
+                       device_node = NULL;
+               }
+               _E("close dmcrypt device error(%d)", ret);
+               return ret;
+       }
+#else
+       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
+       if (ret) {
+               if (device_node) {
+                       free(device_node);
+                       device_node = NULL;
+               }
+               _E("unable to detach the loopback encryption setup" \
+                       " for the application");
+               return APP2EXT_ERROR_UNMOUNT;
+       }
+#endif
+
+       if (device_node) {
+               free(device_node);
+               device_node = NULL;
+       }
+
+       return ret;
+}
+
 int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid_t uid)
 {
        int ret = 0;
@@ -221,7 +284,6 @@ END:
 int app2sd_usr_post_app_install(const char *pkgid,
                app2ext_status install_status, uid_t uid)
 {
-       char *device_name = NULL;
        char *sdpath = NULL;
        char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
@@ -266,58 +328,13 @@ int app2sd_usr_post_app_install(const char *pkgid,
                tzplatform_reset_user();
        }
        free(encoded_id);
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-
-       /* get the associated device node for SD card applicationer */
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       device_name =
-               _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
-       if (!device_name)
-               return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
-#else
-       device_name = _app2sd_find_associated_device_node(loopback_device);
-       if (NULL == device_name)
-               return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
-#endif
 
-       ret = _app2sd_unmount_app_content(application_path);
+       ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
+               application_path, uid);
        if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("unable to unmount the app content (%d)", ret);
-               return APP2EXT_ERROR_UNMOUNT;
-       }
-
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("close dmcrypt device error(%d)", ret);
+               _E("failed to finalize device setup");
                return ret;
        }
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("unable to detach the loopback encryption setup" \
-                       " for the application");
-               return APP2EXT_ERROR_UNMOUNT;
-       }
-#endif
-
-       if (device_name) {
-               free(device_name);
-               device_name = NULL;
-       }
 
        /* take appropriate action based on
         * installation status of application package
@@ -333,6 +350,8 @@ int app2sd_usr_post_app_install(const char *pkgid,
                if (ret)
                        _E("unable to delete info");
 
+               snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+                       application_path);
                ret = _app2sd_delete_directory(application_mmc_path);
                if (ret)
                        _E("unable to delete the directory (%s)", application_path);
@@ -581,23 +600,12 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
                fclose(fp);
        }
 
-       ret = _app2sd_unmount_app_content(application_path);
+       ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
+               application_path, uid);
        if (ret) {
-               _E("unable to unmount the SD application");
-               return APP2EXT_ERROR_UNMOUNT;
-       }
-
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret)
-               _E("close dmcrypt device error(%d)", ret);
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret) {
-               _E("unable to remove loopback setup");
-               return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
+               _E("failed to finalize device setup");
+               return ret;
        }
-#endif
 
        return ret;
 }
@@ -768,34 +776,13 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                tzplatform_reset_user();
        }
        free(encoded_id);
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
 
-       /* unmount the loopback encrypted pseudo device from
-        * the application installation path
-        */
-       ret = _app2sd_unmount_app_content(application_path);
+       ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
+               application_path, uid);
        if (ret) {
-               _E("unable to unmount the app content (%d)", ret);
-               ret = APP2EXT_ERROR_UNMOUNT;
-               goto END;
-       }
-       /* detach the loopback encryption setup for the application */
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret) {
-               _E("close dmcrypt device error(%d)", ret);
-               goto END;
-       }
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret) {
-               _E("unable to Detach the loopback encryption setup" \
-                       " for the application");
-               ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
-               goto END;
+               _E("failed to finalize device setup");
+               return ret;
        }
-#endif
 
        /* delete the loopback device from the SD card */
        ret = _app2sd_delete_loopback_device(loopback_device);
@@ -806,6 +793,8 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                goto END;
        }
 
+       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
+               application_path);
        ret = _app2sd_delete_directory(application_mmc_path);
        if (ret) {
                _E("unable to delete the directory (%s)",
@@ -951,20 +940,12 @@ int app2sd_usr_post_move_installed_app(const char *pkgid,
                tzplatform_reset_user();
        }
 
-       ret = _app2sd_unmount_app_content(application_path);
-       if (ret)
-               _E("unmount error (%d)", ret);
-
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret)
-               _E("close dmcrypt device error(%d)", ret);
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret)
-               _E("unable to detach loopback setup for (%s)",
-                       loopback_device);
-#endif
+       ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
+               application_path, uid);
+       if (ret) {
+               _E("failed to finalize device setup");
+               return ret;
+       }
 
        sync();
        return APP2EXT_SUCCESS;
@@ -1193,7 +1174,6 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        char loopback_device[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char *sdpath = NULL;
-       char *device_name = NULL;
        char *encoded_id = NULL;
        int ret = APP2EXT_SUCCESS;
        int pkgmgr_ret = 0;
@@ -1234,53 +1214,12 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        }
        free(encoded_id);
 
-       /* get the associated device node for SD card applicationer */
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       device_name =
-               _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
-       if (!device_name) {
-               _E("could not find associated dmcrypt device node" \
-                       " (%s_%d)", pkgid, uid);
-               return APP2EXT_ERROR_FIND_ASSOCIATED_DMCRYPT_DEVICE_NODE;
-       }
-#else
-       device_name = _app2sd_find_associated_device_node(loopback_device);
-       if (NULL == device_name)
-               return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
-#endif
-
-       ret = _app2sd_unmount_app_content(application_path);
+       ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
+               application_path, uid);
        if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("unable to unmount the app content (%d)", ret);
-               return APP2EXT_ERROR_UNMOUNT;
-       }
-
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("close dmcrypt device error(%d)", ret);
-               return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
-       }
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret) {
-               if (device_name) {
-                       free(device_name);
-                       device_name = NULL;
-               }
-               _E("unable to detach the loopback encryption " \
-                       "setup for the application");
-               return APP2EXT_ERROR_UNMOUNT;
+               _E("failed to finalize device setup");
+               return ret;
        }
-#endif
 
        pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
                INSTALL_EXTERNAL, loopback_device, uid);
@@ -1291,11 +1230,6 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
                return APP2EXT_ERROR_PKGMGR_ERROR;
        }
 
-       if (device_name) {
-               free(device_name);
-               device_name = NULL;
-       }
-
        sync();
        return ret;
 }