bugfix related storing external_path
[platform/core/appfw/app2sd.git] / plugin / app2sd / src / app2sd_interface.c
index c08fdc1..40e2cf8 100644 (file)
  */
 
 #include <pkgmgr-info.h>
+#include <aul.h>
 
 #include "app2sd_internals.h"
 
-static int __app2sd_create_app2sd_directories(uid_t uid)
+static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
 {
        int ret = 0;
+       char app2sd_path[FILENAME_MAX] = { 0, };
        mode_t mode = DIR_PERMS;
 
-       ret = mkdir(APP2SD_PATH, mode);
+       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
+                       mmc_path, EXTIMG_DIR);
+       ret = mkdir(app2sd_path, mode);
        if (ret) {
                if (errno != EEXIST) {
                        _E("create directory failed," \
@@ -46,11 +50,13 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
 {
        int ret = 0;
        int free_mmc_mem = 0;
+       char *sdpath = NULL;
        char *device_node = NULL;
-#if !defined(_APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
+#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
        char *devi = NULL;
-#endif
        char *result = NULL;
+#endif
+       char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char *encoded_id = NULL;
@@ -63,17 +69,19 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
 
        /* find available free memory in the MMC card */
-       ret = _app2sd_get_available_free_memory(MMC_PATH, &free_mmc_mem);
+       ret = _app2sd_get_available_free_memory(mmc_path, &free_mmc_mem);
        if (ret) {
-               _E("unable to get available free memory in MMC (%d)",
-                       ret);
+               _E("unable to get available free memory in MMC (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
        _D("size details for application installation:" \
@@ -96,36 +104,29 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
 
-       ret = __app2sd_create_app2sd_directories(uid);
+       ret = __app2sd_create_app2sd_directories(uid, mmc_path);
        if (ret) {
                _E("failed to create app2sd dirs");
                return ret;
        }
 
-       /* check same loopback_device existence */
-       result = (char *)_app2sd_find_associated_device(loopback_device);
-       if (result != NULL) {
-               _E("there is same associated File (%s)", loopback_device);
-               return APP2EXT_ERROR_SAME_LOOPBACK_DEVICE_EXISTS;
-       }
-
        /* create a loopback device */
        ret = _app2sd_create_loopback_device(pkgid, loopback_device,
                (reqd_disk_size + PKG_BUF_SIZE));
 
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_setup_device(pkgid, loopback_device, false, uid);
        if (ret) {
                _E("dmcrypt setup device error");
@@ -185,7 +186,7 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
 
 FINISH_OFF:
        if (device_node) {
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret)
                _E("close dmcrypt device error(%d)", ret);
@@ -206,13 +207,14 @@ END:
                device_node = NULL;
        }
 
-#if !defined(_APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
+#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
        if (devi) {
                free(devi);
                devi = NULL;
        }
 #endif
 
+       sync();
        return ret;
 }
 
@@ -220,7 +222,10 @@ 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, };
+       char application_mmc_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char *encoded_id = NULL;
        int ret = APP2EXT_SUCCESS;
@@ -234,11 +239,13 @@ int app2sd_usr_post_app_install(const char *pkgid,
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
@@ -248,20 +255,22 @@ int app2sd_usr_post_app_install(const char *pkgid,
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                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 _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_name =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
        if (!device_name)
@@ -282,7 +291,7 @@ int app2sd_usr_post_app_install(const char *pkgid,
                return APP2EXT_ERROR_UNMOUNT;
        }
 
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret) {
                if (device_name) {
@@ -324,7 +333,7 @@ int app2sd_usr_post_app_install(const char *pkgid,
                if (ret)
                        _E("unable to delete info");
 
-               ret = _app2sd_delete_directory(application_path);
+               ret = _app2sd_delete_directory(application_mmc_path);
                if (ret)
                        _E("unable to delete the directory (%s)", application_path);
        } else {
@@ -332,7 +341,7 @@ int app2sd_usr_post_app_install(const char *pkgid,
                 * to pkgmgr_parser db
                 */
                pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-                       INSTALL_EXTERNAL, uid);
+                       INSTALL_EXTERNAL, loopback_device, uid);
                if (pkgmgr_ret < 0) {
                        _E("fail to update installed location " \
                                "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
@@ -341,17 +350,20 @@ int app2sd_usr_post_app_install(const char *pkgid,
                }
        }
 
+       sync();
        return ret;
 }
 
 int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
+       char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
        char *encoded_id = NULL;
        char *device_node = NULL;
-#if !defined(_APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
+#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
        char *result = NULL;
 #endif
        FILE *fp = NULL;
@@ -363,11 +375,13 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -377,14 +391,14 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
@@ -396,7 +410,7 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        }
        fclose(fp);
 
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
        if (device_node) {
@@ -449,13 +463,77 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        return ret;
 }
 
+static int _app2sd_application_handler(const pkgmgrinfo_appinfo_h handle, void *data)
+{
+       int ret = 0;
+       int pid = 0;
+       char *appid = NULL;
+       uid_t uid = *(uid_t *)data;
+
+       ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
+       if (ret < 0) {
+               _E("failed to get appid");
+               return APP2EXT_ERROR_PKGMGR_ERROR;
+       }
+
+       _D("appid(%s), uid(%d)", appid, uid);
+
+       ret = aul_app_is_running_for_uid(appid, uid);
+       if (ret == 0)
+               return APP2EXT_SUCCESS;
+
+       pid = aul_app_get_pid_for_uid(appid, uid);
+       if (pid < 0) {
+               _E("failed to get pid");
+               return APP2EXT_ERROR_KILLAPP_ERROR;
+       }
+
+       ret = aul_terminate_pid_sync_without_restart_for_uid(pid, uid);
+       if (ret != AUL_R_OK) {
+               _E("failed to kill app");
+               return APP2EXT_ERROR_KILLAPP_ERROR;
+       }
+
+       return APP2EXT_SUCCESS;
+}
+
+static int _app2sd_kill_running_app(const char *pkgid, uid_t uid)
+{
+       int ret = 0;
+       pkgmgrinfo_pkginfo_h handle;
+
+       ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
+       if (ret < 0) {
+               _E("failed to get pkginfo");
+               return APP2EXT_ERROR_PKGMGR_ERROR;
+       }
+
+       ret = pkgmgrinfo_appinfo_get_usr_list(handle,
+               PMINFO_ALL_APP, _app2sd_application_handler, &uid, uid);
+       if (ret < 0) {
+               _E("failed to get appinfo");
+               return APP2EXT_ERROR_PKGMGR_ERROR;
+       }
+
+       ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       if (ret < 0) {
+               _E("failed to destroy pkginfo");
+               return APP2EXT_ERROR_PKGMGR_ERROR;
+       }
+
+       return APP2EXT_SUCCESS;
+}
+
 int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
+       char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
        char *encoded_id = NULL;
        FILE *fp = NULL;
+       int mmc_present = 1;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL) {
@@ -463,12 +541,16 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
+       _app2sd_kill_running_app(pkgid, uid);
+
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
-               return APP2EXT_ERROR_MMC_STATUS;
+               _W("MMC not present OR Not ready (%d)", ret);
+               mmc_present = 0;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -478,24 +560,26 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
 
-       fp = fopen(loopback_device, "r+");
-       if (fp == NULL) {
-               _E("app entry is not present in SD Card");
-               return APP2EXT_ERROR_INVALID_PACKAGE;
+       if (mmc_present) {
+               fp = fopen(loopback_device, "r+");
+               if (fp == NULL) {
+                       _E("app entry is not present in SD Card");
+                       return APP2EXT_ERROR_INVALID_PACKAGE;
+               }
+               fclose(fp);
        }
-       fclose(fp);
 
        ret = _app2sd_unmount_app_content(application_path);
        if (ret) {
@@ -503,7 +587,7 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
                return APP2EXT_ERROR_UNMOUNT;
        }
 
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret)
                _E("close dmcrypt device error(%d)", ret);
@@ -521,8 +605,10 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
 int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
+       char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
        char *encoded_id = NULL;
        char *device_node = NULL;
        FILE *fp = NULL;
@@ -535,12 +621,15 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                ret = APP2EXT_ERROR_MMC_STATUS;
                goto END;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -549,14 +638,14 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
@@ -571,7 +660,7 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
        fclose(fp);
 
        /* get the associated device node for SD card applicationer */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
 #else
@@ -579,7 +668,7 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
 #endif
        if (NULL == device_node) {
                /* do loopback setup */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
                        false, uid, &device_node);
                if (ret) {
@@ -629,13 +718,17 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
        }
 
 END:
+       sync();
        return ret;
 }
 
 int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
 {
+       char mmc_path[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
+       char application_mmc_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
        char *encoded_id = NULL;
        int ret = APP2EXT_SUCCESS;
 
@@ -647,12 +740,15 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                ret = APP2EXT_ERROR_MMC_STATUS;
                goto END;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -661,17 +757,19 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                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
@@ -683,7 +781,7 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                goto END;
        }
        /* detach the loopback encryption setup for the application */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret) {
                _E("close dmcrypt device error(%d)", ret);
@@ -708,7 +806,7 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
                goto END;
        }
 
-       ret = _app2sd_delete_directory(application_path);
+       ret = _app2sd_delete_directory(application_mmc_path);
        if (ret) {
                _E("unable to delete the directory (%s)",
                application_path);
@@ -731,14 +829,18 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
        }
 
 END:
+       sync();
        return ret;
 }
 
-int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
-               app2ext_move_type move_type, uid_t uid)
+int app2sd_usr_pre_move_installed_app(const char *pkgid,
+               GList *dir_list, app2ext_move_type move_type, uid_t uid)
 {
        int ret = 0;
        int pkgmgr_ret = 0;
+       char *sdpath = NULL;
+       char *image_path = NULL;
+       char mmc_path[FILENAME_MAX] = { 0, };
 
        /* validate function arguments */
        if (pkgid == NULL || dir_list == NULL
@@ -748,33 +850,23 @@ int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
-       pkgmgrinfo_pkginfo_h info_handle = NULL;
-       pkgmgrinfo_installed_storage storage = PMINFO_INTERNAL_STORAGE;
-       pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &info_handle);
-       if (pkgmgr_ret < 0) {
-               _E("failed to get pkginfo for pkg(%s), uid(%d), pkgmgr_ret(%d)",
-                       pkgid, uid, pkgmgr_ret);
-               return APP2EXT_ERROR_PKGMGR_ERROR;
-       }
-       pkgmgr_ret = pkgmgrinfo_pkginfo_get_installed_storage(info_handle, &storage);
-       if (pkgmgr_ret < 0) {
-               _E("failed to get installed storage for pkg(%s) of uid(%d), pkgmgr_ret(%d)",
-                       pkgid, uid, pkgmgr_ret);
-               pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
-               return APP2EXT_ERROR_PKGMGR_ERROR;
+       /* check whether MMC is present or not */
+       ret = _app2sd_check_mmc_status(&sdpath);
+       if (ret) {
+               _E("MMC not present OR Not ready(%d)", ret);
+               return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
 
-       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;
-       } else {
-               _D("pkgid[%s] move to STORAGE [%d]", pkgid, storage);
+       ret = __app2sd_create_app2sd_directories(uid, mmc_path);
+       if (ret) {
+               _E("failed to create app2sd dirs");
+               return ret;
        }
-       pkgmgrinfo_pkginfo_destroy_pkginfo(info_handle);
 
-       ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid);
+       ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid, mmc_path, &image_path);
        if (ret) {
                _D("unable to move application");
                return ret;
@@ -782,8 +874,12 @@ int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
 
        /* if move is completed, then update installed storage to pkgmgr_parser db */
        if (move_type == APP2EXT_MOVE_TO_EXT) {
+               if (!image_path) {
+                       _E("image_path is NULL");
+                       return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+               }
                pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-                               INSTALL_EXTERNAL, uid);
+                               INSTALL_EXTERNAL, image_path, uid);
                if (pkgmgr_ret < 0) {
                        _E("failed to update installed location to db " \
                                "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
@@ -792,7 +888,7 @@ int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
                }
        } else {
                pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-                               INSTALL_INTERNAL, uid);
+                               INSTALL_INTERNAL, image_path, uid);
                if (pkgmgr_ret < 0) {
                        _E("failed to update installed location to db " \
                                "[%s, %s] of uid(%d), pkgmgr_ret(%d)",
@@ -801,6 +897,76 @@ int app2sd_usr_move_installed_app(const char *pkgid, GList *dir_list,
                }
        }
 
+       if (image_path)
+               free(image_path);
+
+       sync();
+       return APP2EXT_SUCCESS;
+}
+
+int app2sd_usr_post_move_installed_app(const char *pkgid,
+               app2ext_move_type move_type, uid_t uid)
+{
+       int ret = 0;
+       char mmc_path[FILENAME_MAX] = { 0, };
+       char application_path[FILENAME_MAX] = { 0, };
+       char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
+       char *encoded_id = NULL;
+
+       /* validate function arguments */
+       if (pkgid == NULL || move_type < APP2EXT_MOVE_TO_EXT
+               || move_type > APP2EXT_MOVE_TO_PHONE) {
+               _E("invalid function arguments");
+               return APP2EXT_ERROR_INVALID_ARGUMENTS;
+       }
+
+       if (move_type == APP2EXT_MOVE_TO_PHONE)
+               return APP2EXT_SUCCESS;
+
+       /* check whether MMC is present or not */
+       ret = _app2sd_check_mmc_status(&sdpath);
+       if (ret) {
+               _E("MMC not present OR Not ready(%d)", ret);
+               return APP2EXT_ERROR_MMC_STATUS;
+       }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
+
+       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
+       if (encoded_id == NULL)
+               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+
+       snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
+       free(encoded_id);
+       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();
+       }
+
+       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
+
+       sync();
        return APP2EXT_SUCCESS;
 }
 
@@ -808,9 +974,11 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                int size, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
+       char app2sd_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
        char temp_uid[32] = { 0, };
+       char *sdpath = NULL;
        char *temp_pkgid = NULL;
        char *temp_loopback_device = NULL;
        char *temp_application_path = NULL;
@@ -829,11 +997,15 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
+                       sdpath, EXTIMG_DIR);
+       free(sdpath);
+       sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -843,13 +1015,13 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
                snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+                       app2sd_path, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
                snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+                       app2sd_path, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
@@ -895,7 +1067,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                                free(temp_application_path);
                                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
                        }
-                       len = strlen(APP2SD_PATH) + strlen(temp_encoded_id) + 1;
+                       len = strlen(app2sd_path) + strlen(temp_encoded_id) + 1;
                        temp_loopback_device = calloc(len + 1, sizeof(char));
                        if (temp_loopback_device == NULL) {
                                _E("memory alloc failed");
@@ -905,7 +1077,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
                        }
                        snprintf(temp_loopback_device, len + 1, "%s/%s",
-                               APP2SD_PATH, temp_encoded_id);
+                               app2sd_path, temp_encoded_id);
                        free(temp_encoded_id);
                } else {
                        tzplatform_set_user(uid);
@@ -926,7 +1098,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
                        }
                        snprintf(temp_uid, 32, "%d", uid);
-                       len = strlen(APP2SD_PATH) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
+                       len = strlen(app2sd_path) + strlen(temp_uid) + strlen(temp_encoded_id) + 2;
                        temp_loopback_device = calloc(len + 1, sizeof(char));
                        if (temp_loopback_device == NULL) {
                                _E("memory alloc failed");
@@ -936,7 +1108,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
                        }
                        snprintf(temp_loopback_device, len + 1, "%s/%s",
-                               APP2SD_PATH, temp_encoded_id);
+                               app2sd_path, temp_encoded_id);
                        free(temp_encoded_id);
                        tzplatform_reset_user();
                }
@@ -954,7 +1126,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
        }
 
        /* get the associated device node for SD card applicationer */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
 #else
@@ -962,7 +1134,7 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
 #endif
        if (NULL == device_node) {
                /* do loopback setup */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
                        false, uid, &device_node);
                if (ret) {
@@ -1009,17 +1181,22 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                free(device_node);
                device_node = NULL;
        }
+
+       sync();
        return ret;
 }
 
 int app2sd_usr_post_app_upgrade(const char *pkgid,
                app2ext_status install_status, uid_t uid)
 {
-       char *device_name = NULL;
+       char mmc_path[FILENAME_MAX] = { 0, };
        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;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
@@ -1029,11 +1206,14 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        }
 
        /* check whether MMC is present or not */
-       ret = _app2sd_check_mmc_status();
+       ret = _app2sd_check_mmc_status(&sdpath);
        if (ret) {
-               _E("MMC not preset OR Not ready (%d)", ret);
+               _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
+       sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
@@ -1042,20 +1222,20 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
 
        /* get the associated device node for SD card applicationer */
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_name =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
        if (!device_name) {
@@ -1079,7 +1259,7 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
                return APP2EXT_ERROR_UNMOUNT;
        }
 
-#ifdef _APPFW_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret) {
                if (device_name) {
@@ -1102,18 +1282,30 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        }
 #endif
 
+       pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
+               INSTALL_EXTERNAL, loopback_device, uid);
+       if (pkgmgr_ret < 0) {
+               _E("fail to update installed location " \
+                       "to db[%s, %d] of uid(%d), pkgmgr ret(%d)",
+                       pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
+               return APP2EXT_ERROR_PKGMGR_ERROR;
+       }
+
        if (device_name) {
                free(device_name);
                device_name = NULL;
        }
 
+       sync();
        return ret;
 }
 
 int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
 {
+       char mmc_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
        char application_path[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
        char *encoded_id = NULL;
        int ret = APP2EXT_SUCCESS;
 
@@ -1123,6 +1315,14 @@ int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
+       /* check whether MMC is present or not */
+       ret = _app2sd_check_mmc_status(&sdpath);
+       if (ret) {
+               _E("MMC not present OR Not ready (%d)", ret);
+               return APP2EXT_ERROR_MMC_STATUS;
+       }
+       snprintf(mmc_path, FILENAME_MAX - 1, "%s", sdpath);
+       free(sdpath);
        sync();
 
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
@@ -1132,20 +1332,21 @@ int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
        if (_is_global(uid)) {
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
        } else {
                tzplatform_set_user(uid);
                snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
                        tzplatform_getenv(TZ_USER_APP), pkgid);
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, encoded_id);
+               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s/%s",
+                       mmc_path, EXTIMG_DIR, encoded_id);
                tzplatform_reset_user();
        }
        free(encoded_id);
 
        ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
 
+       sync();
        return ret;
 }
 
@@ -1154,17 +1355,29 @@ int app2sd_enable_full_pkg(void)
        int ret = APP2EXT_SUCCESS;
        int rc = 0;
        char buf[FILENAME_MAX] = { 0, };
+       char app2sd_path[FILENAME_MAX] = { 0, };
        char loopback_device[FILENAME_MAX] = { 0, };
+       char *sdpath = NULL;
+       char *pkgid = NULL;
        DIR *dir = NULL;
        struct dirent entry;
        struct dirent *result = NULL;
-       char *pkgid = NULL;
        uid_t uid = 0;
 
-       dir = opendir(APP2SD_PATH);
+       /* check whether MMC is present or not */
+       ret = _app2sd_check_mmc_status(&sdpath);
+       if (ret) {
+               _E("MMC not present OR Not ready (%d)", ret);
+               return APP2EXT_ERROR_MMC_STATUS;
+       }
+       snprintf(app2sd_path, FILENAME_MAX - 1, "%s/%s",
+                       sdpath, EXTIMG_DIR);
+       free(sdpath);
+
+       dir = opendir(app2sd_path);
        if (!dir) {
-               if (strerror_r(errno, buf, sizeof(buf)) == 0)
-                       _E("failed to opendir (%s)", buf);
+               strerror_r(errno, buf, sizeof(buf));
+               _E("failed to opendir (%s)", buf);
                return APP2EXT_ERROR_OPEN_DIR;
        }
 
@@ -1182,22 +1395,20 @@ int app2sd_enable_full_pkg(void)
                        strcmp(entry.d_name, "..") == 0)
                        continue;
                snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, entry.d_name);
+                       app2sd_path, entry.d_name);
                ret = _app2sd_get_info_from_db(loopback_device,
                        &pkgid, &uid);
                if (ret) {
                        _E("failed to get info from db");
                        break;;
                }
-               if (pkgid && uid > 0) {
+               if (pkgid) {
                        _D("pkgid(%s), uid(%d)", pkgid, uid);
                        ret = app2sd_usr_on_demand_setup_init(pkgid, uid);
                        if (ret) {
                                _E("error(%d)", ret);
                                break;
                        }
-               }
-               if (pkgid) {
                        free(pkgid);
                        pkgid = NULL;
                }
@@ -1209,68 +1420,38 @@ int app2sd_enable_full_pkg(void)
        }
        closedir(dir);
 
+       sync();
        return ret;
 }
 
-int app2sd_disable_full_pkg(void)
+static int _app2sd_info_cb_func(const char *pkgid, uid_t uid)
 {
        int ret = APP2EXT_SUCCESS;
-       int rc = 0;
-       char buf[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       DIR *dir = NULL;
-       struct dirent entry;
-       struct dirent *result = NULL;
-       char *pkgid = NULL;
-       uid_t uid = 0;
 
-       dir = opendir(APP2SD_PATH);
-       if (!dir) {
-               if (strerror_r(errno, buf, sizeof(buf)) == 0)
-                       _E("failed to opendir (%s)", buf);
-               return APP2EXT_ERROR_OPEN_DIR;
+       if (pkgid) {
+               _D("pkgid(%s), uid(%d)", pkgid, uid);
+               ret = app2sd_usr_on_demand_setup_exit(pkgid, uid);
+               if (ret)
+                       _E("error(%d)", ret);
        }
 
+       return ret;
+}
+
+int app2sd_disable_full_pkg(void)
+{
+       int ret = APP2EXT_SUCCESS;
+
        ret = _app2sd_initialize_db();
        if (ret) {
                _E("app2sd db initialize failed");
-               closedir(dir);
                return APP2EXT_ERROR_SQLITE_REGISTRY;
        }
 
-       for (rc = readdir_r(dir, &entry, &result);
-               rc == 0 && result != NULL;
-               rc = readdir_r(dir, &entry, &result)) {
-               if (strcmp(entry.d_name, ".") == 0 ||
-                       strcmp(entry.d_name, "..") == 0)
-                       continue;
-               snprintf(loopback_device, FILENAME_MAX - 1, "%s/%s",
-                       APP2SD_PATH, entry.d_name);
-               ret = _app2sd_get_info_from_db(loopback_device,
-                       &pkgid, &uid);
-               if (ret) {
-                       _E("failed to get info from db");
-                       break;
-               }
-               if (pkgid && uid > 0) {
-                       _D("pkgid(%s), uid(%d)", pkgid, uid);
-                       ret = app2sd_usr_on_demand_setup_exit(pkgid, uid);
-                       if (ret) {
-                               _E("error(%d)", ret);
-                               break;
-                       }
-               }
-               if (pkgid) {
-                       free(pkgid);
-                       pkgid = NULL;
-               }
-       }
-
-       if (pkgid) {
-               free(pkgid);
-               pkgid = NULL;
-       }
-       closedir(dir);
+       ret = _app2sd_get_foreach_info_from_db((app2sd_info_cb)_app2sd_info_cb_func);
+       if (ret)
+               _E("disable full pkg error(%d)", ret);
 
+       sync();
        return ret;
 }